isl_ast_codegen.c: refine_eliminated: do not add stride guard
[isl.git] / isl_ast_codegen.c
blob95a779b3192c49b9a1803f23997d00bb8e45b10d
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 /* Data used in generate_domain.
24 * "build" is the input build.
25 * "list" collects the results.
27 struct isl_generate_domain_data {
28 isl_ast_build *build;
30 isl_ast_graft_list *list;
33 static __isl_give isl_ast_graft_list *generate_next_level(
34 __isl_take isl_union_map *executed,
35 __isl_take isl_ast_build *build);
36 static __isl_give isl_ast_graft_list *generate_code(
37 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
38 int internal);
40 /* Generate an AST for a single domain based on
41 * the (non single valued) inverse schedule "executed".
43 * We extend the schedule with the iteration domain
44 * and continue generating through a call to generate_code.
46 * In particular, if executed has the form
48 * S -> D
50 * then we continue generating code on
52 * [S -> D] -> D
54 * The extended inverse schedule is clearly single valued
55 * ensuring that the nested generate_code will not reach this function,
56 * but will instead create calls to all elements of D that need
57 * to be executed from the current schedule domain.
59 static int generate_non_single_valued(__isl_take isl_map *executed,
60 struct isl_generate_domain_data *data)
62 isl_map *identity;
63 isl_ast_build *build;
64 isl_ast_graft_list *list;
66 build = isl_ast_build_copy(data->build);
68 identity = isl_set_identity(isl_map_range(isl_map_copy(executed)));
69 executed = isl_map_domain_product(executed, identity);
70 build = isl_ast_build_set_single_valued(build, 1);
72 list = generate_code(isl_union_map_from_map(executed), build, 1);
74 data->list = isl_ast_graft_list_concat(data->list, list);
76 return 0;
79 /* Call the at_each_domain callback, if requested by the user,
80 * after recording the current inverse schedule in the build.
82 static __isl_give isl_ast_graft *at_each_domain(__isl_take isl_ast_graft *graft,
83 __isl_keep isl_map *executed, __isl_keep isl_ast_build *build)
85 if (!graft || !build)
86 return isl_ast_graft_free(graft);
87 if (!build->at_each_domain)
88 return graft;
90 build = isl_ast_build_copy(build);
91 build = isl_ast_build_set_executed(build,
92 isl_union_map_from_map(isl_map_copy(executed)));
93 if (!build)
94 return isl_ast_graft_free(graft);
96 graft->node = build->at_each_domain(graft->node,
97 build, build->at_each_domain_user);
98 isl_ast_build_free(build);
100 if (!graft->node)
101 graft = isl_ast_graft_free(graft);
103 return graft;
106 /* Generate an AST for a single domain based on
107 * the inverse schedule "executed" and add it to data->list.
109 * If there is more than one domain element associated to the current
110 * schedule "time", then we need to continue the generation process
111 * in generate_non_single_valued.
112 * Note that the inverse schedule being single-valued may depend
113 * on constraints that are only available in the original context
114 * domain specified by the user. We therefore first introduce
115 * the constraints from data->build->domain.
116 * On the other hand, we only perform the test after having taken the gist
117 * of the domain as the resulting map is the one from which the call
118 * expression is constructed. Using this map to construct the call
119 * expression usually yields simpler results.
120 * Because we perform the single-valuedness test on the gisted map,
121 * we may in rare cases fail to recognize that the inverse schedule
122 * is single-valued. This becomes problematic if this happens
123 * from the recursive call through generate_non_single_valued
124 * as we would then end up in an infinite recursion.
125 * We therefore check if we are inside a call to generate_non_single_valued
126 * and revert to the ungisted map if the gisted map turns out not to be
127 * single-valued.
129 * Otherwise, we generate a call expression for the single executed
130 * domain element and put a guard around it based on the (simplified)
131 * domain of "executed".
133 * If the user has set an at_each_domain callback, it is called
134 * on the constructed call expression node.
136 static int generate_domain(__isl_take isl_map *executed, void *user)
138 struct isl_generate_domain_data *data = user;
139 isl_ast_graft *graft;
140 isl_ast_graft_list *list;
141 isl_set *guard;
142 isl_map *map = NULL;
143 int empty, sv;
145 executed = isl_map_intersect_domain(executed,
146 isl_set_copy(data->build->domain));
147 empty = isl_map_is_empty(executed);
148 if (empty < 0)
149 goto error;
150 if (empty) {
151 isl_map_free(executed);
152 return 0;
155 executed = isl_map_coalesce(executed);
156 map = isl_map_copy(executed);
157 map = isl_ast_build_compute_gist_map_domain(data->build, map);
158 sv = isl_map_is_single_valued(map);
159 if (sv < 0)
160 goto error;
161 if (!sv) {
162 isl_map_free(map);
163 if (data->build->single_valued)
164 map = isl_map_copy(executed);
165 else
166 return generate_non_single_valued(executed, data);
168 guard = isl_map_domain(isl_map_copy(map));
169 guard = isl_set_compute_divs(guard);
170 guard = isl_set_coalesce(guard);
171 guard = isl_ast_build_compute_gist(data->build, guard);
172 graft = isl_ast_graft_alloc_domain(map, data->build);
173 graft = at_each_domain(graft, executed, data->build);
175 isl_map_free(executed);
176 graft = isl_ast_graft_add_guard(graft, guard, data->build);
178 list = isl_ast_graft_list_from_ast_graft(graft);
179 data->list = isl_ast_graft_list_concat(data->list, list);
181 return 0;
182 error:
183 isl_map_free(map);
184 isl_map_free(executed);
185 return -1;
188 /* Call build->create_leaf to a create "leaf" node in the AST,
189 * encapsulate the result in an isl_ast_graft and return the result
190 * as a 1-element list.
192 * Note that the node returned by the user may be an entire tree.
194 * Before we pass control to the user, we first clear some information
195 * from the build that is (presumbably) only meaningful
196 * for the current code generation.
197 * This includes the create_leaf callback itself, so we make a copy
198 * of the build first.
200 static __isl_give isl_ast_graft_list *call_create_leaf(
201 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
203 isl_ast_node *node;
204 isl_ast_graft *graft;
205 isl_ast_build *user_build;
207 user_build = isl_ast_build_copy(build);
208 user_build = isl_ast_build_set_executed(user_build, executed);
209 user_build = isl_ast_build_clear_local_info(user_build);
210 if (!user_build)
211 node = NULL;
212 else
213 node = build->create_leaf(user_build, build->create_leaf_user);
214 graft = isl_ast_graft_alloc(node, build);
215 isl_ast_build_free(build);
216 return isl_ast_graft_list_from_ast_graft(graft);
219 /* Generate an AST after having handled the complete schedule
220 * of this call to the code generator.
222 * If the user has specified a create_leaf callback, control
223 * is passed to the user in call_create_leaf.
225 * Otherwise, we generate one or more calls for each individual
226 * domain in generate_domain.
228 static __isl_give isl_ast_graft_list *generate_inner_level(
229 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
231 isl_ctx *ctx;
232 struct isl_generate_domain_data data = { build };
234 if (!build || !executed)
235 goto error;
237 if (build->create_leaf)
238 return call_create_leaf(executed, build);
240 ctx = isl_union_map_get_ctx(executed);
241 data.list = isl_ast_graft_list_alloc(ctx, 0);
242 if (isl_union_map_foreach_map(executed, &generate_domain, &data) < 0)
243 data.list = isl_ast_graft_list_free(data.list);
245 if (0)
246 error: data.list = NULL;
247 isl_ast_build_free(build);
248 isl_union_map_free(executed);
249 return data.list;
252 /* Call the before_each_for callback, if requested by the user.
254 static __isl_give isl_ast_node *before_each_for(__isl_take isl_ast_node *node,
255 __isl_keep isl_ast_build *build)
257 isl_id *id;
259 if (!node || !build)
260 return isl_ast_node_free(node);
261 if (!build->before_each_for)
262 return node;
263 id = build->before_each_for(build, build->before_each_for_user);
264 node = isl_ast_node_set_annotation(node, id);
265 return node;
268 /* Call the after_each_for callback, if requested by the user.
270 static __isl_give isl_ast_graft *after_each_for(__isl_take isl_ast_graft *graft,
271 __isl_keep isl_ast_build *build)
273 if (!graft || !build)
274 return isl_ast_graft_free(graft);
275 if (!build->after_each_for)
276 return graft;
277 graft->node = build->after_each_for(graft->node, build,
278 build->after_each_for_user);
279 if (!graft->node)
280 return isl_ast_graft_free(graft);
281 return graft;
284 /* Plug in all the know values of the current and outer dimensions
285 * in the domain of "executed". In principle, we only need to plug
286 * in the known value of the current dimension since the values of
287 * outer dimensions have been plugged in already.
288 * However, it turns out to be easier to just plug in all known values.
290 static __isl_give isl_union_map *plug_in_values(
291 __isl_take isl_union_map *executed, __isl_keep isl_ast_build *build)
293 return isl_ast_build_substitute_values_union_map_domain(build,
294 executed);
297 /* Check if the constraint "c" is a lower bound on dimension "pos",
298 * an upper bound, or independent of dimension "pos".
300 static int constraint_type(isl_constraint *c, int pos)
302 if (isl_constraint_is_lower_bound(c, isl_dim_set, pos))
303 return 1;
304 if (isl_constraint_is_upper_bound(c, isl_dim_set, pos))
305 return 2;
306 return 0;
309 /* Compare the types of the constraints "a" and "b",
310 * resulting in constraints that are independent of "depth"
311 * to be sorted before the lower bounds on "depth", which in
312 * turn are sorted before the upper bounds on "depth".
314 static int cmp_constraint(__isl_keep isl_constraint *a,
315 __isl_keep isl_constraint *b, void *user)
317 int *depth = user;
318 int t1 = constraint_type(a, *depth);
319 int t2 = constraint_type(b, *depth);
321 return t1 - t2;
324 /* Extract a lower bound on dimension "pos" from constraint "c".
326 * If the constraint is of the form
328 * a x + f(...) >= 0
330 * then we essentially return
332 * l = ceil(-f(...)/a)
334 * However, if the current dimension is strided, then we need to make
335 * sure that the lower bound we construct is of the form
337 * f + s a
339 * with f the offset and s the stride.
340 * We therefore compute
342 * f + s * ceil((l - f)/s)
344 static __isl_give isl_aff *lower_bound(__isl_keep isl_constraint *c,
345 int pos, __isl_keep isl_ast_build *build)
347 isl_aff *aff;
349 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
350 aff = isl_aff_ceil(aff);
352 if (isl_ast_build_has_stride(build, pos)) {
353 isl_aff *offset;
354 isl_val *stride;
356 offset = isl_ast_build_get_offset(build, pos);
357 stride = isl_ast_build_get_stride(build, pos);
359 aff = isl_aff_sub(aff, isl_aff_copy(offset));
360 aff = isl_aff_scale_down_val(aff, isl_val_copy(stride));
361 aff = isl_aff_ceil(aff);
362 aff = isl_aff_scale_val(aff, stride);
363 aff = isl_aff_add(aff, offset);
366 aff = isl_ast_build_compute_gist_aff(build, aff);
368 return aff;
371 /* Return the exact lower bound (or upper bound if "upper" is set)
372 * of "domain" as a piecewise affine expression.
374 * If we are computing a lower bound (of a strided dimension), then
375 * we need to make sure it is of the form
377 * f + s a
379 * where f is the offset and s is the stride.
380 * We therefore need to include the stride constraint before computing
381 * the minimum.
383 static __isl_give isl_pw_aff *exact_bound(__isl_keep isl_set *domain,
384 __isl_keep isl_ast_build *build, int upper)
386 isl_set *stride;
387 isl_map *it_map;
388 isl_pw_aff *pa;
389 isl_pw_multi_aff *pma;
391 domain = isl_set_copy(domain);
392 if (!upper) {
393 stride = isl_ast_build_get_stride_constraint(build);
394 domain = isl_set_intersect(domain, stride);
396 it_map = isl_ast_build_map_to_iterator(build, domain);
397 if (upper)
398 pma = isl_map_lexmax_pw_multi_aff(it_map);
399 else
400 pma = isl_map_lexmin_pw_multi_aff(it_map);
401 pa = isl_pw_multi_aff_get_pw_aff(pma, 0);
402 isl_pw_multi_aff_free(pma);
403 pa = isl_ast_build_compute_gist_pw_aff(build, pa);
404 pa = isl_pw_aff_coalesce(pa);
406 return pa;
409 /* Extract a lower bound on dimension "pos" from each constraint
410 * in "constraints" and return the list of lower bounds.
411 * If "constraints" has zero elements, then we extract a lower bound
412 * from "domain" instead.
414 static __isl_give isl_pw_aff_list *lower_bounds(
415 __isl_keep isl_constraint_list *constraints, int pos,
416 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
418 isl_ctx *ctx;
419 isl_pw_aff_list *list;
420 int i, n;
422 if (!build)
423 return NULL;
425 n = isl_constraint_list_n_constraint(constraints);
426 if (n == 0) {
427 isl_pw_aff *pa;
428 pa = exact_bound(domain, build, 0);
429 return isl_pw_aff_list_from_pw_aff(pa);
432 ctx = isl_ast_build_get_ctx(build);
433 list = isl_pw_aff_list_alloc(ctx,n);
435 for (i = 0; i < n; ++i) {
436 isl_aff *aff;
437 isl_constraint *c;
439 c = isl_constraint_list_get_constraint(constraints, i);
440 aff = lower_bound(c, pos, build);
441 isl_constraint_free(c);
442 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
445 return list;
448 /* Extract an upper bound on dimension "pos" from each constraint
449 * in "constraints" and return the list of upper bounds.
450 * If "constraints" has zero elements, then we extract an upper bound
451 * from "domain" instead.
453 static __isl_give isl_pw_aff_list *upper_bounds(
454 __isl_keep isl_constraint_list *constraints, int pos,
455 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
457 isl_ctx *ctx;
458 isl_pw_aff_list *list;
459 int i, n;
461 n = isl_constraint_list_n_constraint(constraints);
462 if (n == 0) {
463 isl_pw_aff *pa;
464 pa = exact_bound(domain, build, 1);
465 return isl_pw_aff_list_from_pw_aff(pa);
468 ctx = isl_ast_build_get_ctx(build);
469 list = isl_pw_aff_list_alloc(ctx,n);
471 for (i = 0; i < n; ++i) {
472 isl_aff *aff;
473 isl_constraint *c;
475 c = isl_constraint_list_get_constraint(constraints, i);
476 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
477 isl_constraint_free(c);
478 aff = isl_aff_floor(aff);
479 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
482 return list;
485 /* Callback for sorting the isl_pw_aff_list passed to reduce_list.
487 static int reduce_list_cmp(__isl_keep isl_pw_aff *a, __isl_keep isl_pw_aff *b,
488 void *user)
490 return isl_pw_aff_plain_cmp(a, b);
493 /* Return an isl_ast_expr that performs the reduction of type "type"
494 * on AST expressions corresponding to the elements in "list".
496 * The list is assumed to contain at least one element.
497 * If the list contains exactly one element, then the returned isl_ast_expr
498 * simply computes that affine expression.
499 * If the list contains more than one element, then we sort it
500 * using a fairly abitrary but hopefully reasonably stable order.
502 static __isl_give isl_ast_expr *reduce_list(enum isl_ast_op_type type,
503 __isl_keep isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
505 int i, n;
506 isl_ctx *ctx;
507 isl_ast_expr *expr;
509 if (!list)
510 return NULL;
512 n = isl_pw_aff_list_n_pw_aff(list);
514 if (n == 1)
515 return isl_ast_build_expr_from_pw_aff_internal(build,
516 isl_pw_aff_list_get_pw_aff(list, 0));
518 ctx = isl_pw_aff_list_get_ctx(list);
519 expr = isl_ast_expr_alloc_op(ctx, type, n);
520 if (!expr)
521 return NULL;
523 list = isl_pw_aff_list_copy(list);
524 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
525 if (!list)
526 return isl_ast_expr_free(expr);
528 for (i = 0; i < n; ++i) {
529 isl_ast_expr *expr_i;
531 expr_i = isl_ast_build_expr_from_pw_aff_internal(build,
532 isl_pw_aff_list_get_pw_aff(list, i));
533 if (!expr_i)
534 goto error;
535 expr->u.op.args[i] = expr_i;
538 isl_pw_aff_list_free(list);
539 return expr;
540 error:
541 isl_pw_aff_list_free(list);
542 isl_ast_expr_free(expr);
543 return NULL;
546 /* Add a guard to "graft" based on "bound" in the case of a degenerate
547 * level (including the special case of an eliminated level).
549 * We eliminate the current dimension, simplify the result in the current
550 * build and add the result as guards to the graft.
552 * Note that we cannot simply drop the constraints on the current dimension
553 * even in the eliminated case, because the single affine expression may
554 * not be explicitly available in "bounds". Moreover, the single affine
555 * expression may only be defined on a subset of the build domain,
556 * so we do in some cases need to insert a guard even in the eliminated case.
558 static __isl_give isl_ast_graft *add_degenerate_guard(
559 __isl_take isl_ast_graft *graft, __isl_keep isl_basic_set *bounds,
560 __isl_keep isl_ast_build *build)
562 int depth;
563 isl_set *dom;
565 depth = isl_ast_build_get_depth(build);
567 dom = isl_set_from_basic_set(isl_basic_set_copy(bounds));
568 dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
569 dom = isl_ast_build_compute_gist(build, dom);
571 graft = isl_ast_graft_add_guard(graft, dom, build);
573 return graft;
576 /* Add the guard implied by the current stride constraint (if any),
577 * but not (necessarily) enforced by the generated AST to "graft".
579 static __isl_give isl_ast_graft *add_stride_guard(
580 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
582 int depth;
583 isl_set *dom;
585 depth = isl_ast_build_get_depth(build);
586 if (!isl_ast_build_has_stride(build, depth))
587 return graft;
589 dom = isl_ast_build_get_stride_constraint(build);
590 dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
591 dom = isl_ast_build_compute_gist(build, dom);
593 graft = isl_ast_graft_add_guard(graft, dom, build);
595 return graft;
598 /* Update "graft" based on "bounds" for the eliminated case.
600 * In the eliminated case, no for node is created, so we only need
601 * to check if "bounds" imply any guards that need to be inserted.
603 static __isl_give isl_ast_graft *refine_eliminated(
604 __isl_take isl_ast_graft *graft, __isl_keep isl_basic_set *bounds,
605 __isl_keep isl_ast_build *build)
607 return add_degenerate_guard(graft, bounds, build);
610 /* Update "graft" based on "bounds" and "sub_build" for the degenerate case.
612 * "build" is the build in which graft->node was created
613 * "sub_build" contains information about the current level itself,
614 * including the single value attained.
616 * We first set the initialization part of the for loop to the single
617 * value attained by the current dimension.
618 * The increment and condition are not strictly needed as the are known
619 * to be "1" and "iterator <= value" respectively.
620 * Then we check if "bounds" imply any guards that need to be inserted.
622 static __isl_give isl_ast_graft *refine_degenerate(
623 __isl_take isl_ast_graft *graft, __isl_keep isl_basic_set *bounds,
624 __isl_keep isl_ast_build *build,
625 __isl_keep isl_ast_build *sub_build)
627 isl_pw_aff *value;
629 if (!graft || !sub_build)
630 return isl_ast_graft_free(graft);
632 value = isl_pw_aff_copy(sub_build->value);
634 graft->node->u.f.init = isl_ast_build_expr_from_pw_aff_internal(build,
635 value);
636 if (!graft->node->u.f.init)
637 return isl_ast_graft_free(graft);
639 graft = add_degenerate_guard(graft, bounds, build);
640 graft = add_stride_guard(graft, build);
642 return graft;
645 /* Return the intersection of constraints in "list" as a set.
647 static __isl_give isl_set *intersect_constraints(
648 __isl_keep isl_constraint_list *list)
650 int i, n;
651 isl_basic_set *bset;
653 n = isl_constraint_list_n_constraint(list);
654 if (n < 1)
655 isl_die(isl_constraint_list_get_ctx(list), isl_error_internal,
656 "expecting at least one constraint", return NULL);
658 bset = isl_basic_set_from_constraint(
659 isl_constraint_list_get_constraint(list, 0));
660 for (i = 1; i < n; ++i) {
661 isl_basic_set *bset_i;
663 bset_i = isl_basic_set_from_constraint(
664 isl_constraint_list_get_constraint(list, i));
665 bset = isl_basic_set_intersect(bset, bset_i);
668 return isl_set_from_basic_set(bset);
671 /* Compute the constraints on the outer dimensions enforced by
672 * graft->node and add those constraints to graft->enforced,
673 * in case the upper bound is expressed as a set "upper".
675 * In particular, if l(...) is a lower bound in "lower", and
677 * -a i + f(...) >= 0 or a i <= f(...)
679 * is an upper bound ocnstraint on the current dimension i,
680 * then the for loop enforces the constraint
682 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
684 * We therefore simply take each lower bound in turn, plug it into
685 * the upper bounds and compute the intersection over all lower bounds.
687 * If a lower bound is a rational expression, then
688 * isl_basic_set_preimage_multi_aff will force this rational
689 * expression to have only integer values. However, the loop
690 * itself does not enforce this integrality constraint. We therefore
691 * use the ceil of the lower bounds instead of the lower bounds themselves.
692 * Other constraints will make sure that the for loop is only executed
693 * when each of the lower bounds attains an integral value.
694 * In particular, potentially rational values only occur in
695 * lower_bound if the offset is a (seemingly) rational expression,
696 * but then outer conditions will make sure that this rational expression
697 * only attains integer values.
699 static __isl_give isl_ast_graft *set_enforced_from_set(
700 __isl_take isl_ast_graft *graft,
701 __isl_keep isl_pw_aff_list *lower, int pos, __isl_keep isl_set *upper)
703 isl_space *space;
704 isl_basic_set *enforced;
705 isl_pw_multi_aff *pma;
706 int i, n;
708 if (!graft || !lower)
709 return isl_ast_graft_free(graft);
711 space = isl_set_get_space(upper);
712 enforced = isl_basic_set_universe(isl_space_copy(space));
714 space = isl_space_map_from_set(space);
715 pma = isl_pw_multi_aff_identity(space);
717 n = isl_pw_aff_list_n_pw_aff(lower);
718 for (i = 0; i < n; ++i) {
719 isl_pw_aff *pa;
720 isl_set *enforced_i;
721 isl_basic_set *hull;
722 isl_pw_multi_aff *pma_i;
724 pa = isl_pw_aff_list_get_pw_aff(lower, i);
725 pa = isl_pw_aff_ceil(pa);
726 pma_i = isl_pw_multi_aff_copy(pma);
727 pma_i = isl_pw_multi_aff_set_pw_aff(pma_i, pos, pa);
728 enforced_i = isl_set_copy(upper);
729 enforced_i = isl_set_preimage_pw_multi_aff(enforced_i, pma_i);
730 hull = isl_set_simple_hull(enforced_i);
731 enforced = isl_basic_set_intersect(enforced, hull);
734 isl_pw_multi_aff_free(pma);
736 graft = isl_ast_graft_enforce(graft, enforced);
738 return graft;
741 /* Compute the constraints on the outer dimensions enforced by
742 * graft->node and add those constraints to graft->enforced,
743 * in case the upper bound is expressed as
744 * a list of affine expressions "upper".
746 * The enforced condition is that each lower bound expression is less
747 * than or equal to each upper bound expression.
749 static __isl_give isl_ast_graft *set_enforced_from_list(
750 __isl_take isl_ast_graft *graft,
751 __isl_keep isl_pw_aff_list *lower, __isl_keep isl_pw_aff_list *upper)
753 isl_set *cond;
754 isl_basic_set *enforced;
756 lower = isl_pw_aff_list_copy(lower);
757 upper = isl_pw_aff_list_copy(upper);
758 cond = isl_pw_aff_list_le_set(lower, upper);
759 enforced = isl_set_simple_hull(cond);
760 graft = isl_ast_graft_enforce(graft, enforced);
762 return graft;
765 /* Does "aff" have a negative constant term?
767 static int aff_constant_is_negative(__isl_take isl_set *set,
768 __isl_take isl_aff *aff, void *user)
770 int *neg = user;
771 isl_val *v;
773 v = isl_aff_get_constant_val(aff);
774 *neg = isl_val_is_neg(v);
775 isl_val_free(v);
776 isl_set_free(set);
777 isl_aff_free(aff);
779 return *neg ? 0 : -1;
782 /* Does "pa" have a negative constant term over its entire domain?
784 static int pw_aff_constant_is_negative(__isl_take isl_pw_aff *pa, void *user)
786 int r;
787 int *neg = user;
789 r = isl_pw_aff_foreach_piece(pa, &aff_constant_is_negative, user);
790 isl_pw_aff_free(pa);
792 return *neg ? 0 : -1;
795 /* Does each element in "list" have a negative constant term?
797 * The callback terminates the iteration as soon an element has been
798 * found that does not have a negative constant term.
800 static int list_constant_is_negative(__isl_keep isl_pw_aff_list *list)
802 int neg = 1;
804 if (isl_pw_aff_list_foreach(list,
805 &pw_aff_constant_is_negative, &neg) < 0 && neg)
806 return -1;
808 return neg;
811 /* Add 1 to each of the elements in "list", where each of these elements
812 * is defined over the internal schedule space of "build".
814 static __isl_give isl_pw_aff_list *list_add_one(
815 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
817 int i, n;
818 isl_space *space;
819 isl_aff *aff;
820 isl_pw_aff *one;
822 space = isl_ast_build_get_space(build, 1);
823 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
824 aff = isl_aff_add_constant_si(aff, 1);
825 one = isl_pw_aff_from_aff(aff);
827 n = isl_pw_aff_list_n_pw_aff(list);
828 for (i = 0; i < n; ++i) {
829 isl_pw_aff *pa;
830 pa = isl_pw_aff_list_get_pw_aff(list, i);
831 pa = isl_pw_aff_add(pa, isl_pw_aff_copy(one));
832 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
835 isl_pw_aff_free(one);
837 return list;
840 /* Set the condition part of the for node graft->node in case
841 * the upper bound is represented as a list of piecewise affine expressions.
843 * In particular, set the condition to
845 * iterator <= min(list of upper bounds)
847 * If each of the upper bounds has a negative constant term, then
848 * set the condition to
850 * iterator < min(list of (upper bound + 1)s)
853 static __isl_give isl_ast_graft *set_for_cond_from_list(
854 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *list,
855 __isl_keep isl_ast_build *build)
857 int neg;
858 isl_ast_expr *bound, *iterator, *cond;
859 enum isl_ast_op_type type = isl_ast_op_le;
861 if (!graft || !list)
862 return isl_ast_graft_free(graft);
864 neg = list_constant_is_negative(list);
865 if (neg < 0)
866 return isl_ast_graft_free(graft);
867 list = isl_pw_aff_list_copy(list);
868 if (neg) {
869 list = list_add_one(list, build);
870 type = isl_ast_op_lt;
873 bound = reduce_list(isl_ast_op_min, list, build);
874 iterator = isl_ast_expr_copy(graft->node->u.f.iterator);
875 cond = isl_ast_expr_alloc_binary(type, iterator, bound);
876 graft->node->u.f.cond = cond;
878 isl_pw_aff_list_free(list);
879 if (!graft->node->u.f.cond)
880 return isl_ast_graft_free(graft);
881 return graft;
884 /* Set the condition part of the for node graft->node in case
885 * the upper bound is represented as a set.
887 static __isl_give isl_ast_graft *set_for_cond_from_set(
888 __isl_take isl_ast_graft *graft, __isl_keep isl_set *set,
889 __isl_keep isl_ast_build *build)
891 isl_ast_expr *cond;
893 if (!graft)
894 return NULL;
896 cond = isl_ast_build_expr_from_set(build, isl_set_copy(set));
897 graft->node->u.f.cond = cond;
898 if (!graft->node->u.f.cond)
899 return isl_ast_graft_free(graft);
900 return graft;
903 /* Construct an isl_ast_expr for the increment (i.e., stride) of
904 * the current dimension.
906 static __isl_give isl_ast_expr *for_inc(__isl_keep isl_ast_build *build)
908 int depth;
909 isl_val *v;
910 isl_ctx *ctx;
912 if (!build)
913 return NULL;
914 ctx = isl_ast_build_get_ctx(build);
915 depth = isl_ast_build_get_depth(build);
917 if (!isl_ast_build_has_stride(build, depth))
918 return isl_ast_expr_alloc_int_si(ctx, 1);
920 v = isl_ast_build_get_stride(build, depth);
921 return isl_ast_expr_from_val(v);
924 /* Should we express the loop condition as
926 * iterator <= min(list of upper bounds)
928 * or as a conjunction of constraints?
930 * The first is constructed from a list of upper bounds.
931 * The second is constructed from a set.
933 * If there are no upper bounds in "constraints", then this could mean
934 * that "domain" simply doesn't have an upper bound or that we didn't
935 * pick any upper bound. In the first case, we want to generate the
936 * loop condition as a(n empty) conjunction of constraints
937 * In the second case, we will compute
938 * a single upper bound from "domain" and so we use the list form.
940 * If there are upper bounds in "constraints",
941 * then we use the list form iff the atomic_upper_bound option is set.
943 static int use_upper_bound_list(isl_ctx *ctx, int n_upper,
944 __isl_keep isl_set *domain, int depth)
946 if (n_upper > 0)
947 return isl_options_get_ast_build_atomic_upper_bound(ctx);
948 else
949 return isl_set_dim_has_upper_bound(domain, isl_dim_set, depth);
952 /* Fill in the expressions of the for node in graft->node.
954 * In particular,
955 * - set the initialization part of the loop to the maximum of the lower bounds
956 * - extract the increment from the stride of the current dimension
957 * - construct the for condition either based on a list of upper bounds
958 * or on a set of upper bound constraints.
960 static __isl_give isl_ast_graft *set_for_node_expressions(
961 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *lower,
962 int use_list, __isl_keep isl_pw_aff_list *upper_list,
963 __isl_keep isl_set *upper_set, __isl_keep isl_ast_build *build)
965 isl_ast_node *node;
967 if (!graft)
968 return NULL;
970 build = isl_ast_build_copy(build);
971 build = isl_ast_build_set_enforced(build,
972 isl_ast_graft_get_enforced(graft));
974 node = graft->node;
975 node->u.f.init = reduce_list(isl_ast_op_max, lower, build);
976 node->u.f.inc = for_inc(build);
978 if (use_list)
979 graft = set_for_cond_from_list(graft, upper_list, build);
980 else
981 graft = set_for_cond_from_set(graft, upper_set, build);
983 isl_ast_build_free(build);
985 if (!node->u.f.iterator || !node->u.f.init ||
986 !node->u.f.cond || !node->u.f.inc)
987 return isl_ast_graft_free(graft);
989 return graft;
992 /* Update "graft" based on "bounds" and "domain" for the generic,
993 * non-degenerate, case.
995 * "c_lower" and "c_upper" contain the lower and upper bounds
996 * that the loop node should express.
997 * "domain" is the subset of the intersection of the constraints
998 * for which some code is executed.
1000 * There may be zero lower bounds or zero upper bounds in "constraints"
1001 * in case the list of constraints was created
1002 * based on the atomic option or based on separation with explicit bounds.
1003 * In that case, we use "domain" to derive lower and/or upper bounds.
1005 * We first compute a list of one or more lower bounds.
1007 * Then we decide if we want to express the condition as
1009 * iterator <= min(list of upper bounds)
1011 * or as a conjunction of constraints.
1013 * The set of enforced constraints is then computed either based on
1014 * a list of upper bounds or on a set of upper bound constraints.
1015 * We do not compute any enforced constraints if we were forced
1016 * to compute a lower or upper bound using exact_bound. The domains
1017 * of the resulting expressions may imply some bounds on outer dimensions
1018 * that we do not want to appear in the enforced constraints since
1019 * they are not actually enforced by the corresponding code.
1021 * Finally, we fill in the expressions of the for node.
1023 static __isl_give isl_ast_graft *refine_generic_bounds(
1024 __isl_take isl_ast_graft *graft,
1025 __isl_take isl_constraint_list *c_lower,
1026 __isl_take isl_constraint_list *c_upper,
1027 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1029 int depth;
1030 isl_ctx *ctx;
1031 isl_pw_aff_list *lower;
1032 int use_list;
1033 isl_set *upper_set = NULL;
1034 isl_pw_aff_list *upper_list = NULL;
1035 int n_lower, n_upper;
1037 if (!graft || !c_lower || !c_upper || !build)
1038 goto error;
1040 depth = isl_ast_build_get_depth(build);
1041 ctx = isl_ast_graft_get_ctx(graft);
1043 n_lower = isl_constraint_list_n_constraint(c_lower);
1044 n_upper = isl_constraint_list_n_constraint(c_upper);
1046 use_list = use_upper_bound_list(ctx, n_upper, domain, depth);
1048 lower = lower_bounds(c_lower, depth, domain, build);
1050 if (use_list)
1051 upper_list = upper_bounds(c_upper, depth, domain, build);
1052 else if (n_upper > 0)
1053 upper_set = intersect_constraints(c_upper);
1054 else
1055 upper_set = isl_set_universe(isl_set_get_space(domain));
1057 if (n_lower == 0 || n_upper == 0)
1059 else if (use_list)
1060 graft = set_enforced_from_list(graft, lower, upper_list);
1061 else
1062 graft = set_enforced_from_set(graft, lower, depth, upper_set);
1064 graft = set_for_node_expressions(graft, lower, use_list, upper_list,
1065 upper_set, build);
1067 isl_pw_aff_list_free(lower);
1068 isl_pw_aff_list_free(upper_list);
1069 isl_set_free(upper_set);
1070 isl_constraint_list_free(c_lower);
1071 isl_constraint_list_free(c_upper);
1073 return graft;
1074 error:
1075 isl_constraint_list_free(c_lower);
1076 isl_constraint_list_free(c_upper);
1077 return isl_ast_graft_free(graft);
1080 /* Internal data structure used inside count_constraints to keep
1081 * track of the number of constraints that are independent of dimension "pos",
1082 * the lower bounds in "pos" and the upper bounds in "pos".
1084 struct isl_ast_count_constraints_data {
1085 int pos;
1087 int n_indep;
1088 int n_lower;
1089 int n_upper;
1092 /* Increment data->n_indep, data->lower or data->upper depending
1093 * on whether "c" is independenct of dimensions data->pos,
1094 * a lower bound or an upper bound.
1096 static int count_constraints(__isl_take isl_constraint *c, void *user)
1098 struct isl_ast_count_constraints_data *data = user;
1100 if (isl_constraint_is_lower_bound(c, isl_dim_set, data->pos))
1101 data->n_lower++;
1102 else if (isl_constraint_is_upper_bound(c, isl_dim_set, data->pos))
1103 data->n_upper++;
1104 else
1105 data->n_indep++;
1107 isl_constraint_free(c);
1109 return 0;
1112 /* Update "graft" based on "bounds" and "domain" for the generic,
1113 * non-degenerate, case.
1115 * "list" respresent the list of bounds that need to be encoded by
1116 * the for loop (or a guard around the for loop).
1117 * "domain" is the subset of the intersection of the constraints
1118 * for which some code is executed.
1119 * "build" is the build in which graft->node was created.
1121 * We separate lower bounds, upper bounds and constraints that
1122 * are independent of the loop iterator.
1124 * The actual for loop bounds are generated in refine_generic_bounds.
1125 * If there are any constraints that are independent of the loop iterator,
1126 * we need to put a guard around the for loop (which may get hoisted up
1127 * to higher levels) and we call refine_generic_bounds in a build
1128 * where this guard is enforced.
1130 static __isl_give isl_ast_graft *refine_generic_split(
1131 __isl_take isl_ast_graft *graft, __isl_take isl_constraint_list *list,
1132 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1134 isl_ast_build *for_build;
1135 isl_set *guard;
1136 struct isl_ast_count_constraints_data data;
1137 isl_constraint_list *lower;
1138 isl_constraint_list *upper;
1140 if (!list)
1141 return isl_ast_graft_free(graft);
1143 data.pos = isl_ast_build_get_depth(build);
1145 list = isl_constraint_list_sort(list, &cmp_constraint, &data.pos);
1146 if (!list)
1147 return isl_ast_graft_free(graft);
1149 data.n_indep = data.n_lower = data.n_upper = 0;
1150 if (isl_constraint_list_foreach(list, &count_constraints, &data) < 0) {
1151 isl_constraint_list_free(list);
1152 return isl_ast_graft_free(graft);
1155 lower = isl_constraint_list_copy(list);
1156 lower = isl_constraint_list_drop(lower, 0, data.n_indep);
1157 upper = isl_constraint_list_copy(lower);
1158 lower = isl_constraint_list_drop(lower, data.n_lower, data.n_upper);
1159 upper = isl_constraint_list_drop(upper, 0, data.n_lower);
1161 if (data.n_indep == 0) {
1162 isl_constraint_list_free(list);
1163 return refine_generic_bounds(graft, lower, upper,
1164 domain, build);
1167 list = isl_constraint_list_drop(list, data.n_indep,
1168 data.n_lower + data.n_upper);
1169 guard = intersect_constraints(list);
1170 isl_constraint_list_free(list);
1172 for_build = isl_ast_build_copy(build);
1173 for_build = isl_ast_build_restrict_pending(for_build,
1174 isl_set_copy(guard));
1175 graft = refine_generic_bounds(graft, lower, upper, domain, for_build);
1176 isl_ast_build_free(for_build);
1178 graft = isl_ast_graft_add_guard(graft, guard, build);
1180 return graft;
1183 /* Update "graft" based on "bounds" and "domain" for the generic,
1184 * non-degenerate, case.
1186 * "bounds" respresent the bounds that need to be encoded by
1187 * the for loop (or a guard around the for loop).
1188 * "domain" is the subset of "bounds" for which some code is executed.
1189 * "build" is the build in which graft->node was created.
1191 * We break up "bounds" into a list of constraints and continue with
1192 * refine_generic_split.
1194 static __isl_give isl_ast_graft *refine_generic(
1195 __isl_take isl_ast_graft *graft,
1196 __isl_keep isl_basic_set *bounds, __isl_keep isl_set *domain,
1197 __isl_keep isl_ast_build *build)
1199 isl_constraint_list *list;
1201 if (!build || !graft)
1202 return isl_ast_graft_free(graft);
1204 bounds = isl_basic_set_copy(bounds);
1205 bounds = isl_ast_build_compute_gist_basic_set(build, bounds);
1206 list = isl_basic_set_get_constraint_list(bounds);
1207 isl_basic_set_free(bounds);
1209 graft = refine_generic_split(graft, list, domain, build);
1210 graft = add_stride_guard(graft, build);
1212 return graft;
1215 /* Create a for node for the current level.
1217 * Mark the for node degenerate if "degenerate" is set.
1219 static __isl_give isl_ast_node *create_for(__isl_keep isl_ast_build *build,
1220 int degenerate)
1222 int depth;
1223 isl_id *id;
1224 isl_ast_node *node;
1226 if (!build)
1227 return NULL;
1229 depth = isl_ast_build_get_depth(build);
1230 id = isl_ast_build_get_iterator_id(build, depth);
1231 node = isl_ast_node_alloc_for(id);
1232 if (degenerate)
1233 node = isl_ast_node_for_mark_degenerate(node);
1235 return node;
1238 /* Create an AST node for the current dimension based on
1239 * the schedule domain "bounds" and return the node encapsulated
1240 * in an isl_ast_graft.
1242 * "executed" is the current inverse schedule, taking into account
1243 * the bounds in "bounds"
1244 * "domain" is the domain of "executed", with inner dimensions projected out.
1245 * It may be a strict subset of "bounds" in case "bounds" was created
1246 * based on the atomic option or based on separation with explicit bounds.
1248 * "domain" may satisfy additional equalities that result
1249 * from intersecting "executed" with "bounds" in add_node.
1250 * It may also satisfy some global constraints that were dropped out because
1251 * we performed separation with explicit bounds.
1252 * The very first step is then to copy these constraints to "bounds".
1254 * Since we may be calling before_each_for and after_each_for
1255 * callbacks, we record the current inverse schedule in the build.
1257 * We consider three builds,
1258 * "build" is the one in which the current level is created,
1259 * "body_build" is the build in which the next level is created,
1260 * "sub_build" is essentially the same as "body_build", except that
1261 * the depth has not been increased yet.
1263 * "build" already contains information (in strides and offsets)
1264 * about the strides at the current level, but this information is not
1265 * reflected in the build->domain.
1266 * We first add this information and the "bounds" to the sub_build->domain.
1267 * isl_ast_build_set_loop_bounds adds the stride information and
1268 * checks whether the current dimension attains
1269 * only a single value and whether this single value can be represented using
1270 * a single affine expression.
1271 * In the first case, the current level is considered "degenerate".
1272 * In the second, sub-case, the current level is considered "eliminated".
1273 * Eliminated levels don't need to be reflected in the AST since we can
1274 * simply plug in the affine expression. For degenerate, but non-eliminated,
1275 * levels, we do introduce a for node, but mark is as degenerate so that
1276 * it can be printed as an assignment of the single value to the loop
1277 * "iterator".
1279 * If the current level is eliminated, we explicitly plug in the value
1280 * for the current level found by isl_ast_build_set_loop_bounds in the
1281 * inverse schedule. This ensures that if we are working on a slice
1282 * of the domain based on information available in the inverse schedule
1283 * and the build domain, that then this information is also reflected
1284 * in the inverse schedule. This operation also eliminates the current
1285 * dimension from the inverse schedule making sure no inner dimensions depend
1286 * on the current dimension. Otherwise, we create a for node, marking
1287 * it degenerate if appropriate. The initial for node is still incomplete
1288 * and will be completed in either refine_degenerate or refine_generic.
1290 * We then generate a sequence of grafts for the next level,
1291 * create a surrounding graft for the current level and insert
1292 * the for node we created (if the current level is not eliminated).
1294 * Finally, we set the bounds of the for loop and insert guards
1295 * (either in the AST or in the graft) in one of
1296 * refine_eliminated, refine_degenerate or refine_generic.
1298 static __isl_give isl_ast_graft *create_node_scaled(
1299 __isl_take isl_union_map *executed,
1300 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1301 __isl_take isl_ast_build *build)
1303 int depth;
1304 int degenerate, eliminated;
1305 isl_basic_set *hull;
1306 isl_ast_node *node = NULL;
1307 isl_ast_graft *graft;
1308 isl_ast_graft_list *children;
1309 isl_ast_build *sub_build;
1310 isl_ast_build *body_build;
1312 domain = isl_ast_build_eliminate_divs(build, domain);
1313 domain = isl_set_detect_equalities(domain);
1314 hull = isl_set_unshifted_simple_hull(isl_set_copy(domain));
1315 bounds = isl_basic_set_intersect(bounds, hull);
1316 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
1318 depth = isl_ast_build_get_depth(build);
1319 sub_build = isl_ast_build_copy(build);
1320 sub_build = isl_ast_build_set_loop_bounds(sub_build,
1321 isl_basic_set_copy(bounds));
1322 degenerate = isl_ast_build_has_value(sub_build);
1323 eliminated = isl_ast_build_has_affine_value(sub_build, depth);
1324 if (degenerate < 0 || eliminated < 0)
1325 executed = isl_union_map_free(executed);
1326 if (eliminated)
1327 executed = plug_in_values(executed, sub_build);
1328 else
1329 node = create_for(build, degenerate);
1331 body_build = isl_ast_build_copy(sub_build);
1332 body_build = isl_ast_build_increase_depth(body_build);
1333 if (!eliminated)
1334 node = before_each_for(node, body_build);
1335 children = generate_next_level(executed,
1336 isl_ast_build_copy(body_build));
1338 graft = isl_ast_graft_alloc_level(children, build, sub_build);
1339 if (!eliminated)
1340 graft = isl_ast_graft_insert_for(graft, node);
1341 if (eliminated)
1342 graft = refine_eliminated(graft, bounds, build);
1343 else if (degenerate)
1344 graft = refine_degenerate(graft, bounds, build, sub_build);
1345 else
1346 graft = refine_generic(graft, bounds, domain, build);
1347 if (!eliminated)
1348 graft = after_each_for(graft, body_build);
1350 isl_ast_build_free(body_build);
1351 isl_ast_build_free(sub_build);
1352 isl_ast_build_free(build);
1353 isl_basic_set_free(bounds);
1354 isl_set_free(domain);
1356 return graft;
1359 /* Internal data structure for checking if all constraints involving
1360 * the input dimension "depth" are such that the other coefficients
1361 * are multiples of "m", reducing "m" if they are not.
1362 * If "m" is reduced all the way down to "1", then the check has failed
1363 * and we break out of the iteration.
1365 struct isl_check_scaled_data {
1366 int depth;
1367 isl_val *m;
1370 /* If constraint "c" involves the input dimension data->depth,
1371 * then make sure that all the other coefficients are multiples of data->m,
1372 * reducing data->m if needed.
1373 * Break out of the iteration if data->m has become equal to "1".
1375 static int constraint_check_scaled(__isl_take isl_constraint *c, void *user)
1377 struct isl_check_scaled_data *data = user;
1378 int i, j, n;
1379 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_out,
1380 isl_dim_div };
1382 if (!isl_constraint_involves_dims(c, isl_dim_in, data->depth, 1)) {
1383 isl_constraint_free(c);
1384 return 0;
1387 for (i = 0; i < 4; ++i) {
1388 n = isl_constraint_dim(c, t[i]);
1389 for (j = 0; j < n; ++j) {
1390 isl_val *d;
1392 if (t[i] == isl_dim_in && j == data->depth)
1393 continue;
1394 if (!isl_constraint_involves_dims(c, t[i], j, 1))
1395 continue;
1396 d = isl_constraint_get_coefficient_val(c, t[i], j);
1397 data->m = isl_val_gcd(data->m, d);
1398 if (isl_val_is_one(data->m))
1399 break;
1401 if (j < n)
1402 break;
1405 isl_constraint_free(c);
1407 return i < 4 ? -1 : 0;
1410 /* For each constraint of "bmap" that involves the input dimension data->depth,
1411 * make sure that all the other coefficients are multiples of data->m,
1412 * reducing data->m if needed.
1413 * Break out of the iteration if data->m has become equal to "1".
1415 static int basic_map_check_scaled(__isl_take isl_basic_map *bmap, void *user)
1417 int r;
1419 r = isl_basic_map_foreach_constraint(bmap,
1420 &constraint_check_scaled, user);
1421 isl_basic_map_free(bmap);
1423 return r;
1426 /* For each constraint of "map" that involves the input dimension data->depth,
1427 * make sure that all the other coefficients are multiples of data->m,
1428 * reducing data->m if needed.
1429 * Break out of the iteration if data->m has become equal to "1".
1431 static int map_check_scaled(__isl_take isl_map *map, void *user)
1433 int r;
1435 r = isl_map_foreach_basic_map(map, &basic_map_check_scaled, user);
1436 isl_map_free(map);
1438 return r;
1441 /* Create an AST node for the current dimension based on
1442 * the schedule domain "bounds" and return the node encapsulated
1443 * in an isl_ast_graft.
1445 * "executed" is the current inverse schedule, taking into account
1446 * the bounds in "bounds"
1447 * "domain" is the domain of "executed", with inner dimensions projected out.
1450 * Before moving on to the actual AST node construction in create_node_scaled,
1451 * we first check if the current dimension is strided and if we can scale
1452 * down this stride. Note that we only do this if the ast_build_scale_strides
1453 * option is set.
1455 * In particular, let the current dimension take on values
1457 * f + s a
1459 * with a an integer. We check if we can find an integer m that (obviously)
1460 * divides both f and s.
1462 * If so, we check if the current dimension only appears in constraints
1463 * where the coefficients of the other variables are multiples of m.
1464 * We perform this extra check to avoid the risk of introducing
1465 * divisions by scaling down the current dimension.
1467 * If so, we scale the current dimension down by a factor of m.
1468 * That is, we plug in
1470 * i = m i' (1)
1472 * Note that in principle we could always scale down strided loops
1473 * by plugging in
1475 * i = f + s i'
1477 * but this may result in i' taking on larger values than the original i,
1478 * due to the shift by "f".
1479 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1481 static __isl_give isl_ast_graft *create_node(__isl_take isl_union_map *executed,
1482 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1483 __isl_take isl_ast_build *build)
1485 struct isl_check_scaled_data data;
1486 isl_ctx *ctx;
1487 isl_aff *offset;
1488 isl_val *d;
1490 ctx = isl_ast_build_get_ctx(build);
1491 if (!isl_options_get_ast_build_scale_strides(ctx))
1492 return create_node_scaled(executed, bounds, domain, build);
1494 data.depth = isl_ast_build_get_depth(build);
1495 if (!isl_ast_build_has_stride(build, data.depth))
1496 return create_node_scaled(executed, bounds, domain, build);
1498 offset = isl_ast_build_get_offset(build, data.depth);
1499 data.m = isl_ast_build_get_stride(build, data.depth);
1500 if (!data.m)
1501 offset = isl_aff_free(offset);
1502 offset = isl_aff_scale_down_val(offset, isl_val_copy(data.m));
1503 d = isl_aff_get_denominator_val(offset);
1504 if (!d)
1505 executed = isl_union_map_free(executed);
1507 if (executed && isl_val_is_divisible_by(data.m, d))
1508 data.m = isl_val_div(data.m, d);
1509 else {
1510 data.m = isl_val_set_si(data.m, 1);
1511 isl_val_free(d);
1514 if (!isl_val_is_one(data.m)) {
1515 if (isl_union_map_foreach_map(executed, &map_check_scaled,
1516 &data) < 0 &&
1517 !isl_val_is_one(data.m))
1518 executed = isl_union_map_free(executed);
1521 if (!isl_val_is_one(data.m)) {
1522 isl_space *space;
1523 isl_multi_aff *ma;
1524 isl_aff *aff;
1525 isl_map *map;
1526 isl_union_map *umap;
1528 space = isl_ast_build_get_space(build, 1);
1529 space = isl_space_map_from_set(space);
1530 ma = isl_multi_aff_identity(space);
1531 aff = isl_multi_aff_get_aff(ma, data.depth);
1532 aff = isl_aff_scale_val(aff, isl_val_copy(data.m));
1533 ma = isl_multi_aff_set_aff(ma, data.depth, aff);
1535 bounds = isl_basic_set_preimage_multi_aff(bounds,
1536 isl_multi_aff_copy(ma));
1537 domain = isl_set_preimage_multi_aff(domain,
1538 isl_multi_aff_copy(ma));
1539 map = isl_map_reverse(isl_map_from_multi_aff(ma));
1540 umap = isl_union_map_from_map(map);
1541 executed = isl_union_map_apply_domain(executed,
1542 isl_union_map_copy(umap));
1543 build = isl_ast_build_scale_down(build, isl_val_copy(data.m),
1544 umap);
1546 isl_aff_free(offset);
1547 isl_val_free(data.m);
1549 return create_node_scaled(executed, bounds, domain, build);
1552 /* Add the basic set to the list that "user" points to.
1554 static int collect_basic_set(__isl_take isl_basic_set *bset, void *user)
1556 isl_basic_set_list **list = user;
1558 *list = isl_basic_set_list_add(*list, bset);
1560 return 0;
1563 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1565 static __isl_give isl_basic_set_list *isl_basic_set_list_from_set(
1566 __isl_take isl_set *set)
1568 int n;
1569 isl_ctx *ctx;
1570 isl_basic_set_list *list;
1572 if (!set)
1573 return NULL;
1575 ctx = isl_set_get_ctx(set);
1577 n = isl_set_n_basic_set(set);
1578 list = isl_basic_set_list_alloc(ctx, n);
1579 if (isl_set_foreach_basic_set(set, &collect_basic_set, &list) < 0)
1580 list = isl_basic_set_list_free(list);
1582 isl_set_free(set);
1583 return list;
1586 /* Generate code for the schedule domain "bounds"
1587 * and add the result to "list".
1589 * We mainly detect strides here and check if the bounds do not
1590 * conflict with the current build domain
1591 * and then pass over control to create_node.
1593 * "bounds" reflects the bounds on the current dimension and possibly
1594 * some extra conditions on outer dimensions.
1595 * It does not, however, include any divs involving the current dimension,
1596 * so it does not capture any stride constraints.
1597 * We therefore need to compute that part of the schedule domain that
1598 * intersects with "bounds" and derive the strides from the result.
1600 static __isl_give isl_ast_graft_list *add_node(
1601 __isl_take isl_ast_graft_list *list, __isl_take isl_union_map *executed,
1602 __isl_take isl_basic_set *bounds, __isl_take isl_ast_build *build)
1604 isl_ast_graft *graft;
1605 isl_set *domain = NULL;
1606 isl_union_set *uset;
1607 int empty, disjoint;
1609 uset = isl_union_set_from_basic_set(isl_basic_set_copy(bounds));
1610 executed = isl_union_map_intersect_domain(executed, uset);
1611 empty = isl_union_map_is_empty(executed);
1612 if (empty < 0)
1613 goto error;
1614 if (empty)
1615 goto done;
1617 uset = isl_union_map_domain(isl_union_map_copy(executed));
1618 domain = isl_set_from_union_set(uset);
1619 domain = isl_ast_build_specialize(build, domain);
1621 domain = isl_set_compute_divs(domain);
1622 domain = isl_ast_build_eliminate_inner(build, domain);
1623 disjoint = isl_set_is_disjoint(domain, build->domain);
1624 if (disjoint < 0)
1625 goto error;
1626 if (disjoint)
1627 goto done;
1629 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
1631 graft = create_node(executed, bounds, domain,
1632 isl_ast_build_copy(build));
1633 list = isl_ast_graft_list_add(list, graft);
1634 isl_ast_build_free(build);
1635 return list;
1636 error:
1637 list = isl_ast_graft_list_free(list);
1638 done:
1639 isl_set_free(domain);
1640 isl_basic_set_free(bounds);
1641 isl_union_map_free(executed);
1642 isl_ast_build_free(build);
1643 return list;
1646 /* Does any element of i follow or coincide with any element of j
1647 * at the current depth for equal values of the outer dimensions?
1649 static int domain_follows_at_depth(__isl_keep isl_basic_set *i,
1650 __isl_keep isl_basic_set *j, void *user)
1652 int depth = *(int *) user;
1653 isl_basic_map *test;
1654 int empty;
1655 int l;
1657 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1658 isl_basic_set_copy(j));
1659 for (l = 0; l < depth; ++l)
1660 test = isl_basic_map_equate(test, isl_dim_in, l,
1661 isl_dim_out, l);
1662 test = isl_basic_map_order_ge(test, isl_dim_in, depth,
1663 isl_dim_out, depth);
1664 empty = isl_basic_map_is_empty(test);
1665 isl_basic_map_free(test);
1667 return empty < 0 ? -1 : !empty;
1670 /* Split up each element of "list" into a part that is related to "bset"
1671 * according to "gt" and a part that is not.
1672 * Return a list that consist of "bset" and all the pieces.
1674 static __isl_give isl_basic_set_list *add_split_on(
1675 __isl_take isl_basic_set_list *list, __isl_take isl_basic_set *bset,
1676 __isl_keep isl_basic_map *gt)
1678 int i, n;
1679 isl_basic_set_list *res;
1681 if (!list)
1682 bset = isl_basic_set_free(bset);
1684 gt = isl_basic_map_copy(gt);
1685 gt = isl_basic_map_intersect_domain(gt, isl_basic_set_copy(bset));
1686 n = isl_basic_set_list_n_basic_set(list);
1687 res = isl_basic_set_list_from_basic_set(bset);
1688 for (i = 0; res && i < n; ++i) {
1689 isl_basic_set *bset;
1690 isl_set *set1, *set2;
1691 isl_basic_map *bmap;
1692 int empty;
1694 bset = isl_basic_set_list_get_basic_set(list, i);
1695 bmap = isl_basic_map_copy(gt);
1696 bmap = isl_basic_map_intersect_range(bmap, bset);
1697 bset = isl_basic_map_range(bmap);
1698 empty = isl_basic_set_is_empty(bset);
1699 if (empty < 0)
1700 res = isl_basic_set_list_free(res);
1701 if (empty) {
1702 isl_basic_set_free(bset);
1703 bset = isl_basic_set_list_get_basic_set(list, i);
1704 res = isl_basic_set_list_add(res, bset);
1705 continue;
1708 res = isl_basic_set_list_add(res, isl_basic_set_copy(bset));
1709 set1 = isl_set_from_basic_set(bset);
1710 bset = isl_basic_set_list_get_basic_set(list, i);
1711 set2 = isl_set_from_basic_set(bset);
1712 set1 = isl_set_subtract(set2, set1);
1713 set1 = isl_set_make_disjoint(set1);
1715 res = isl_basic_set_list_concat(res,
1716 isl_basic_set_list_from_set(set1));
1718 isl_basic_map_free(gt);
1719 isl_basic_set_list_free(list);
1720 return res;
1723 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1724 __isl_keep isl_basic_set_list *domain_list,
1725 __isl_keep isl_union_map *executed,
1726 __isl_keep isl_ast_build *build);
1728 /* Internal data structure for add_nodes.
1730 * "executed" and "build" are extra arguments to be passed to add_node.
1731 * "list" collects the results.
1733 struct isl_add_nodes_data {
1734 isl_union_map *executed;
1735 isl_ast_build *build;
1737 isl_ast_graft_list *list;
1740 /* Generate code for the schedule domains in "scc"
1741 * and add the results to "list".
1743 * The domains in "scc" form a strongly connected component in the ordering.
1744 * If the number of domains in "scc" is larger than 1, then this means
1745 * that we cannot determine a valid ordering for the domains in the component.
1746 * This should be fairly rare because the individual domains
1747 * have been made disjoint first.
1748 * The problem is that the domains may be integrally disjoint but not
1749 * rationally disjoint. For example, we may have domains
1751 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1753 * These two domains have an empty intersection, but their rational
1754 * relaxations do intersect. It is impossible to order these domains
1755 * in the second dimension because the first should be ordered before
1756 * the second for outer dimension equal to 0, while it should be ordered
1757 * after for outer dimension equal to 1.
1759 * This may happen in particular in case of unrolling since the domain
1760 * of each slice is replaced by its simple hull.
1762 * For each basic set i in "scc" and for each of the following basic sets j,
1763 * we split off that part of the basic set i that shares the outer dimensions
1764 * with j and lies before j in the current dimension.
1765 * We collect all the pieces in a new list that replaces "scc".
1767 * While the elements in "scc" should be disjoint, we double-check
1768 * this property to avoid running into an infinite recursion in case
1769 * they intersect due to some internal error.
1771 static int add_nodes(__isl_take isl_basic_set_list *scc, void *user)
1773 struct isl_add_nodes_data *data = user;
1774 int i, n, depth;
1775 isl_basic_set *bset, *first;
1776 isl_basic_set_list *list;
1777 isl_space *space;
1778 isl_basic_map *gt;
1780 n = isl_basic_set_list_n_basic_set(scc);
1781 bset = isl_basic_set_list_get_basic_set(scc, 0);
1782 if (n == 1) {
1783 isl_basic_set_list_free(scc);
1784 data->list = add_node(data->list,
1785 isl_union_map_copy(data->executed), bset,
1786 isl_ast_build_copy(data->build));
1787 return data->list ? 0 : -1;
1790 depth = isl_ast_build_get_depth(data->build);
1791 space = isl_basic_set_get_space(bset);
1792 space = isl_space_map_from_set(space);
1793 gt = isl_basic_map_universe(space);
1794 for (i = 0; i < depth; ++i)
1795 gt = isl_basic_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
1796 gt = isl_basic_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
1798 first = isl_basic_set_copy(bset);
1799 list = isl_basic_set_list_from_basic_set(bset);
1800 for (i = 1; i < n; ++i) {
1801 int disjoint;
1803 bset = isl_basic_set_list_get_basic_set(scc, i);
1805 disjoint = isl_basic_set_is_disjoint(bset, first);
1806 if (disjoint < 0)
1807 list = isl_basic_set_list_free(list);
1808 else if (!disjoint)
1809 isl_die(isl_basic_set_list_get_ctx(scc),
1810 isl_error_internal,
1811 "basic sets in scc are assumed to be disjoint",
1812 list = isl_basic_set_list_free(list));
1814 list = add_split_on(list, bset, gt);
1816 isl_basic_set_free(first);
1817 isl_basic_map_free(gt);
1818 isl_basic_set_list_free(scc);
1819 scc = list;
1820 data->list = isl_ast_graft_list_concat(data->list,
1821 generate_sorted_domains(scc, data->executed, data->build));
1822 isl_basic_set_list_free(scc);
1824 return data->list ? 0 : -1;
1827 /* Sort the domains in "domain_list" according to the execution order
1828 * at the current depth (for equal values of the outer dimensions),
1829 * generate code for each of them, collecting the results in a list.
1830 * If no code is generated (because the intersection of the inverse schedule
1831 * with the domains turns out to be empty), then an empty list is returned.
1833 * The caller is responsible for ensuring that the basic sets in "domain_list"
1834 * are pair-wise disjoint. It can, however, in principle happen that
1835 * two basic sets should be ordered one way for one value of the outer
1836 * dimensions and the other way for some other value of the outer dimensions.
1837 * We therefore play safe and look for strongly connected components.
1838 * The function add_nodes takes care of handling non-trivial components.
1840 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1841 __isl_keep isl_basic_set_list *domain_list,
1842 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
1844 isl_ctx *ctx;
1845 struct isl_add_nodes_data data;
1846 int depth;
1847 int n;
1849 if (!domain_list)
1850 return NULL;
1852 ctx = isl_basic_set_list_get_ctx(domain_list);
1853 n = isl_basic_set_list_n_basic_set(domain_list);
1854 data.list = isl_ast_graft_list_alloc(ctx, n);
1855 if (n == 0)
1856 return data.list;
1857 if (n == 1)
1858 return add_node(data.list, isl_union_map_copy(executed),
1859 isl_basic_set_list_get_basic_set(domain_list, 0),
1860 isl_ast_build_copy(build));
1862 depth = isl_ast_build_get_depth(build);
1863 data.executed = executed;
1864 data.build = build;
1865 if (isl_basic_set_list_foreach_scc(domain_list,
1866 &domain_follows_at_depth, &depth,
1867 &add_nodes, &data) < 0)
1868 data.list = isl_ast_graft_list_free(data.list);
1870 return data.list;
1873 /* Do i and j share any values for the outer dimensions?
1875 static int shared_outer(__isl_keep isl_basic_set *i,
1876 __isl_keep isl_basic_set *j, void *user)
1878 int depth = *(int *) user;
1879 isl_basic_map *test;
1880 int empty;
1881 int l;
1883 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1884 isl_basic_set_copy(j));
1885 for (l = 0; l < depth; ++l)
1886 test = isl_basic_map_equate(test, isl_dim_in, l,
1887 isl_dim_out, l);
1888 empty = isl_basic_map_is_empty(test);
1889 isl_basic_map_free(test);
1891 return empty < 0 ? -1 : !empty;
1894 /* Internal data structure for generate_sorted_domains_wrap.
1896 * "n" is the total number of basic sets
1897 * "executed" and "build" are extra arguments to be passed
1898 * to generate_sorted_domains.
1900 * "single" is set to 1 by generate_sorted_domains_wrap if there
1901 * is only a single component.
1902 * "list" collects the results.
1904 struct isl_ast_generate_parallel_domains_data {
1905 int n;
1906 isl_union_map *executed;
1907 isl_ast_build *build;
1909 int single;
1910 isl_ast_graft_list *list;
1913 /* Call generate_sorted_domains on "scc", fuse the result into a list
1914 * with either zero or one graft and collect the these single element
1915 * lists into data->list.
1917 * If there is only one component, i.e., if the number of basic sets
1918 * in the current component is equal to the total number of basic sets,
1919 * then data->single is set to 1 and the result of generate_sorted_domains
1920 * is not fused.
1922 static int generate_sorted_domains_wrap(__isl_take isl_basic_set_list *scc,
1923 void *user)
1925 struct isl_ast_generate_parallel_domains_data *data = user;
1926 isl_ast_graft_list *list;
1928 list = generate_sorted_domains(scc, data->executed, data->build);
1929 data->single = isl_basic_set_list_n_basic_set(scc) == data->n;
1930 if (!data->single)
1931 list = isl_ast_graft_list_fuse(list, data->build);
1932 if (!data->list)
1933 data->list = list;
1934 else
1935 data->list = isl_ast_graft_list_concat(data->list, list);
1937 isl_basic_set_list_free(scc);
1938 if (!data->list)
1939 return -1;
1941 return 0;
1944 /* Look for any (weakly connected) components in the "domain_list"
1945 * of domains that share some values of the outer dimensions.
1946 * That is, domains in different components do not share any values
1947 * of the outer dimensions. This means that these components
1948 * can be freely reordered.
1949 * Within each of the components, we sort the domains according
1950 * to the execution order at the current depth.
1952 * If there is more than one component, then generate_sorted_domains_wrap
1953 * fuses the result of each call to generate_sorted_domains
1954 * into a list with either zero or one graft and collects these (at most)
1955 * single element lists into a bigger list. This means that the elements of the
1956 * final list can be freely reordered. In particular, we sort them
1957 * according to an arbitrary but fixed ordering to ease merging of
1958 * graft lists from different components.
1960 static __isl_give isl_ast_graft_list *generate_parallel_domains(
1961 __isl_keep isl_basic_set_list *domain_list,
1962 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
1964 int depth;
1965 struct isl_ast_generate_parallel_domains_data data;
1967 if (!domain_list)
1968 return NULL;
1970 data.n = isl_basic_set_list_n_basic_set(domain_list);
1971 if (data.n <= 1)
1972 return generate_sorted_domains(domain_list, executed, build);
1974 depth = isl_ast_build_get_depth(build);
1975 data.list = NULL;
1976 data.executed = executed;
1977 data.build = build;
1978 data.single = 0;
1979 if (isl_basic_set_list_foreach_scc(domain_list, &shared_outer, &depth,
1980 &generate_sorted_domains_wrap,
1981 &data) < 0)
1982 data.list = isl_ast_graft_list_free(data.list);
1984 if (!data.single)
1985 data.list = isl_ast_graft_list_sort_guard(data.list);
1987 return data.list;
1990 /* Internal data for separate_domain.
1992 * "explicit" is set if we only want to use explicit bounds.
1994 * "domain" collects the separated domains.
1996 struct isl_separate_domain_data {
1997 isl_ast_build *build;
1998 int explicit;
1999 isl_set *domain;
2002 /* Extract implicit bounds on the current dimension for the executed "map".
2004 * The domain of "map" may involve inner dimensions, so we
2005 * need to eliminate them.
2007 static __isl_give isl_set *implicit_bounds(__isl_take isl_map *map,
2008 __isl_keep isl_ast_build *build)
2010 isl_set *domain;
2012 domain = isl_map_domain(map);
2013 domain = isl_ast_build_eliminate(build, domain);
2015 return domain;
2018 /* Extract explicit bounds on the current dimension for the executed "map".
2020 * Rather than eliminating the inner dimensions as in implicit_bounds,
2021 * we simply drop any constraints involving those inner dimensions.
2022 * The idea is that most bounds that are implied by constraints on the
2023 * inner dimensions will be enforced by for loops and not by explicit guards.
2024 * There is then no need to separate along those bounds.
2026 static __isl_give isl_set *explicit_bounds(__isl_take isl_map *map,
2027 __isl_keep isl_ast_build *build)
2029 isl_set *domain;
2030 int depth, dim;
2032 dim = isl_map_dim(map, isl_dim_out);
2033 map = isl_map_drop_constraints_involving_dims(map, isl_dim_out, 0, dim);
2035 domain = isl_map_domain(map);
2036 depth = isl_ast_build_get_depth(build);
2037 dim = isl_set_dim(domain, isl_dim_set);
2038 domain = isl_set_detect_equalities(domain);
2039 domain = isl_set_drop_constraints_involving_dims(domain,
2040 isl_dim_set, depth + 1, dim - (depth + 1));
2041 domain = isl_set_remove_divs_involving_dims(domain,
2042 isl_dim_set, depth, 1);
2043 domain = isl_set_remove_unknown_divs(domain);
2045 return domain;
2048 /* Split data->domain into pieces that intersect with the range of "map"
2049 * and pieces that do not intersect with the range of "map"
2050 * and then add that part of the range of "map" that does not intersect
2051 * with data->domain.
2053 static int separate_domain(__isl_take isl_map *map, void *user)
2055 struct isl_separate_domain_data *data = user;
2056 isl_set *domain;
2057 isl_set *d1, *d2;
2059 if (data->explicit)
2060 domain = explicit_bounds(map, data->build);
2061 else
2062 domain = implicit_bounds(map, data->build);
2064 domain = isl_set_coalesce(domain);
2065 domain = isl_set_make_disjoint(domain);
2066 d1 = isl_set_subtract(isl_set_copy(domain), isl_set_copy(data->domain));
2067 d2 = isl_set_subtract(isl_set_copy(data->domain), isl_set_copy(domain));
2068 data->domain = isl_set_intersect(data->domain, domain);
2069 data->domain = isl_set_union(data->domain, d1);
2070 data->domain = isl_set_union(data->domain, d2);
2072 return 0;
2075 /* Separate the schedule domains of "executed".
2077 * That is, break up the domain of "executed" into basic sets,
2078 * such that for each basic set S, every element in S is associated with
2079 * the same domain spaces.
2081 * "space" is the (single) domain space of "executed".
2083 static __isl_give isl_set *separate_schedule_domains(
2084 __isl_take isl_space *space, __isl_take isl_union_map *executed,
2085 __isl_keep isl_ast_build *build)
2087 struct isl_separate_domain_data data = { build };
2088 isl_ctx *ctx;
2090 ctx = isl_ast_build_get_ctx(build);
2091 data.explicit = isl_options_get_ast_build_separation_bounds(ctx) ==
2092 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT;
2093 data.domain = isl_set_empty(space);
2094 if (isl_union_map_foreach_map(executed, &separate_domain, &data) < 0)
2095 data.domain = isl_set_free(data.domain);
2097 isl_union_map_free(executed);
2098 return data.domain;
2101 /* Temporary data used during the search for a lower bound for unrolling.
2103 * "domain" is the original set for which to find a lower bound
2104 * "depth" is the dimension for which to find a lower boudn
2106 * "lower" is the best lower bound found so far. It is NULL if we have not
2107 * found any yet.
2108 * "n" is the corresponding size. If lower is NULL, then the value of n
2109 * is undefined.
2111 struct isl_find_unroll_data {
2112 isl_set *domain;
2113 int depth;
2115 isl_aff *lower;
2116 int *n;
2119 /* Check if we can use "c" as a lower bound and if it is better than
2120 * any previously found lower bound.
2122 * If "c" does not involve the dimension at the current depth,
2123 * then we cannot use it.
2124 * Otherwise, let "c" be of the form
2126 * i >= f(j)/a
2128 * We compute the maximal value of
2130 * -ceil(f(j)/a)) + i + 1
2132 * over the domain. If there is such a value "n", then we know
2134 * -ceil(f(j)/a)) + i + 1 <= n
2136 * or
2138 * i < ceil(f(j)/a)) + n
2140 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2141 * We just need to check if we have found any lower bound before and
2142 * if the new lower bound is better (smaller n) than the previously found
2143 * lower bounds.
2145 static int update_unrolling_lower_bound(struct isl_find_unroll_data *data,
2146 __isl_keep isl_constraint *c)
2148 isl_aff *aff, *lower;
2149 isl_val *max;
2151 if (!isl_constraint_is_lower_bound(c, isl_dim_set, data->depth))
2152 return 0;
2154 lower = isl_constraint_get_bound(c, isl_dim_set, data->depth);
2155 lower = isl_aff_ceil(lower);
2156 aff = isl_aff_copy(lower);
2157 aff = isl_aff_neg(aff);
2158 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, data->depth, 1);
2159 aff = isl_aff_add_constant_si(aff, 1);
2160 max = isl_set_max_val(data->domain, aff);
2161 isl_aff_free(aff);
2163 if (!max)
2164 goto error;
2165 if (isl_val_is_infty(max)) {
2166 isl_val_free(max);
2167 isl_aff_free(lower);
2168 return 0;
2171 if (isl_val_cmp_si(max, INT_MAX) <= 0 &&
2172 (!data->lower || isl_val_cmp_si(max, *data->n) < 0)) {
2173 isl_aff_free(data->lower);
2174 data->lower = lower;
2175 *data->n = isl_val_get_num_si(max);
2176 } else
2177 isl_aff_free(lower);
2178 isl_val_free(max);
2180 return 1;
2181 error:
2182 isl_aff_free(lower);
2183 return -1;
2186 /* Check if we can use "c" as a lower bound and if it is better than
2187 * any previously found lower bound.
2189 static int constraint_find_unroll(__isl_take isl_constraint *c, void *user)
2191 struct isl_find_unroll_data *data;
2192 int r;
2194 data = (struct isl_find_unroll_data *) user;
2195 r = update_unrolling_lower_bound(data, c);
2196 isl_constraint_free(c);
2198 return r;
2201 /* Look for a lower bound l(i) on the dimension at "depth"
2202 * and a size n such that "domain" is a subset of
2204 * { [i] : l(i) <= i_d < l(i) + n }
2206 * where d is "depth" and l(i) depends only on earlier dimensions.
2207 * Furthermore, try and find a lower bound such that n is as small as possible.
2208 * In particular, "n" needs to be finite.
2210 * Inner dimensions have been eliminated from "domain" by the caller.
2212 * We first construct a collection of lower bounds on the input set
2213 * by computing its simple hull. We then iterate through them,
2214 * discarding those that we cannot use (either because they do not
2215 * involve the dimension at "depth" or because they have no corresponding
2216 * upper bound, meaning that "n" would be unbounded) and pick out the
2217 * best from the remaining ones.
2219 * If we cannot find a suitable lower bound, then we consider that
2220 * to be an error.
2222 static __isl_give isl_aff *find_unroll_lower_bound(__isl_keep isl_set *domain,
2223 int depth, int *n)
2225 struct isl_find_unroll_data data = { domain, depth, NULL, n };
2226 isl_basic_set *hull;
2228 hull = isl_set_simple_hull(isl_set_copy(domain));
2230 if (isl_basic_set_foreach_constraint(hull,
2231 &constraint_find_unroll, &data) < 0)
2232 goto error;
2234 isl_basic_set_free(hull);
2236 if (!data.lower)
2237 isl_die(isl_set_get_ctx(domain), isl_error_invalid,
2238 "cannot find lower bound for unrolling", return NULL);
2240 return data.lower;
2241 error:
2242 isl_basic_set_free(hull);
2243 return isl_aff_free(data.lower);
2246 /* Return the constraint
2248 * i_"depth" = aff + offset
2250 static __isl_give isl_constraint *at_offset(int depth, __isl_keep isl_aff *aff,
2251 int offset)
2253 aff = isl_aff_copy(aff);
2254 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, depth, -1);
2255 aff = isl_aff_add_constant_si(aff, offset);
2256 return isl_equality_from_aff(aff);
2259 /* Data structure for storing the results and the intermediate objects
2260 * of compute_domains.
2262 * "list" is the main result of the function and contains a list
2263 * of disjoint basic sets for which code should be generated.
2265 * "executed" and "build" are inputs to compute_domains.
2266 * "schedule_domain" is the domain of "executed".
2268 * "option" constains the domains at the current depth that should by
2269 * atomic, separated or unrolled. These domains are as specified by
2270 * the user, except that inner dimensions have been eliminated and
2271 * that they have been made pair-wise disjoint.
2273 * "sep_class" contains the user-specified split into separation classes
2274 * specialized to the current depth.
2275 * "done" contains the union of the separation domains that have already
2276 * been handled.
2278 struct isl_codegen_domains {
2279 isl_basic_set_list *list;
2281 isl_union_map *executed;
2282 isl_ast_build *build;
2283 isl_set *schedule_domain;
2285 isl_set *option[3];
2287 isl_map *sep_class;
2288 isl_set *done;
2291 /* Extend domains->list with a list of basic sets, one for each value
2292 * of the current dimension in "domain" and remove the corresponding
2293 * sets from the class domain. Return the updated class domain.
2294 * The divs that involve the current dimension have not been projected out
2295 * from this domain.
2297 * Since we are going to be iterating over the individual values,
2298 * we first check if there are any strides on the current dimension.
2299 * If there is, we rewrite the current dimension i as
2301 * i = stride i' + offset
2303 * and then iterate over individual values of i' instead.
2305 * We then look for a lower bound on i' and a size such that the domain
2306 * is a subset of
2308 * { [j,i'] : l(j) <= i' < l(j) + n }
2310 * and then take slices of the domain at values of i'
2311 * between l(j) and l(j) + n - 1.
2313 * We compute the unshifted simple hull of each slice to ensure that
2314 * we have a single basic set per offset. The slicing constraint
2315 * may get simplified away before the unshifted simple hull is taken
2316 * and may therefore in some rare cases disappear from the result.
2317 * We therefore explicitly add the constraint back after computing
2318 * the unshifted simple hull to ensure that the basic sets
2319 * remain disjoint. The constraints that are dropped by taking the hull
2320 * will be taken into account at the next level, as in the case of the
2321 * atomic option.
2323 * Finally, we map i' back to i and add each basic set to the list.
2324 * Since we may have dropped some constraints, we intersect with
2325 * the class domain again to ensure that each element in the list
2326 * is disjoint from the other class domains.
2328 static __isl_give isl_set *do_unroll(struct isl_codegen_domains *domains,
2329 __isl_take isl_set *domain, __isl_take isl_set *class_domain)
2331 int i, n;
2332 int depth;
2333 isl_ctx *ctx;
2334 isl_aff *lower;
2335 isl_multi_aff *expansion;
2336 isl_basic_map *bmap;
2337 isl_set *unroll_domain;
2338 isl_ast_build *build;
2340 if (!domain)
2341 return isl_set_free(class_domain);
2343 ctx = isl_set_get_ctx(domain);
2344 depth = isl_ast_build_get_depth(domains->build);
2345 build = isl_ast_build_copy(domains->build);
2346 domain = isl_ast_build_eliminate_inner(build, domain);
2347 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
2348 expansion = isl_ast_build_get_stride_expansion(build);
2350 domain = isl_set_preimage_multi_aff(domain,
2351 isl_multi_aff_copy(expansion));
2352 domain = isl_ast_build_eliminate_divs(build, domain);
2354 isl_ast_build_free(build);
2356 lower = find_unroll_lower_bound(domain, depth, &n);
2357 if (!lower)
2358 class_domain = isl_set_free(class_domain);
2360 bmap = isl_basic_map_from_multi_aff(expansion);
2362 unroll_domain = isl_set_empty(isl_set_get_space(domain));
2364 for (i = 0; class_domain && i < n; ++i) {
2365 isl_set *set;
2366 isl_basic_set *bset;
2367 isl_constraint *slice;
2368 isl_basic_set_list *list;
2370 slice = at_offset(depth, lower, i);
2371 set = isl_set_copy(domain);
2372 set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2373 bset = isl_set_unshifted_simple_hull(set);
2374 bset = isl_basic_set_add_constraint(bset, slice);
2375 bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2376 set = isl_set_from_basic_set(bset);
2377 unroll_domain = isl_set_union(unroll_domain, isl_set_copy(set));
2378 set = isl_set_intersect(set, isl_set_copy(class_domain));
2379 set = isl_set_make_disjoint(set);
2380 list = isl_basic_set_list_from_set(set);
2381 domains->list = isl_basic_set_list_concat(domains->list, list);
2384 class_domain = isl_set_subtract(class_domain, unroll_domain);
2386 isl_aff_free(lower);
2387 isl_set_free(domain);
2388 isl_basic_map_free(bmap);
2390 return class_domain;
2393 /* Add domains to domains->list for each individual value of the current
2394 * dimension, for that part of the schedule domain that lies in the
2395 * intersection of the option domain and the class domain.
2396 * Remove the corresponding sets from the class domain and
2397 * return the updated class domain.
2399 * We first break up the unroll option domain into individual pieces
2400 * and then handle each of them separately. The unroll option domain
2401 * has been made disjoint in compute_domains_init_options,
2403 * Note that we actively want to combine different pieces of the
2404 * schedule domain that have the same value at the current dimension.
2405 * We therefore need to break up the unroll option domain before
2406 * intersecting with class and schedule domain, hoping that the
2407 * unroll option domain specified by the user is relatively simple.
2409 static __isl_give isl_set *compute_unroll_domains(
2410 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2412 isl_set *unroll_domain;
2413 isl_basic_set_list *unroll_list;
2414 int i, n;
2415 int empty;
2417 empty = isl_set_is_empty(domains->option[unroll]);
2418 if (empty < 0)
2419 return isl_set_free(class_domain);
2420 if (empty)
2421 return class_domain;
2423 unroll_domain = isl_set_copy(domains->option[unroll]);
2424 unroll_list = isl_basic_set_list_from_set(unroll_domain);
2426 n = isl_basic_set_list_n_basic_set(unroll_list);
2427 for (i = 0; i < n; ++i) {
2428 isl_basic_set *bset;
2430 bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2431 unroll_domain = isl_set_from_basic_set(bset);
2432 unroll_domain = isl_set_intersect(unroll_domain,
2433 isl_set_copy(class_domain));
2434 unroll_domain = isl_set_intersect(unroll_domain,
2435 isl_set_copy(domains->schedule_domain));
2437 empty = isl_set_is_empty(unroll_domain);
2438 if (empty >= 0 && empty) {
2439 isl_set_free(unroll_domain);
2440 continue;
2443 class_domain = do_unroll(domains, unroll_domain, class_domain);
2446 isl_basic_set_list_free(unroll_list);
2448 return class_domain;
2451 /* Try and construct a single basic set that includes the intersection of
2452 * the schedule domain, the atomic option domain and the class domain.
2453 * Add the resulting basic set(s) to domains->list and remove them
2454 * from class_domain. Return the updated class domain.
2456 * We construct a single domain rather than trying to combine
2457 * the schedule domains of individual domains because we are working
2458 * within a single component so that non-overlapping schedule domains
2459 * should already have been separated.
2460 * We do however need to make sure that this single domains is a subset
2461 * of the class domain so that it would not intersect with any other
2462 * class domains. This means that we may end up splitting up the atomic
2463 * domain in case separation classes are being used.
2465 * "domain" is the intersection of the schedule domain and the class domain,
2466 * with inner dimensions projected out.
2468 static __isl_give isl_set *compute_atomic_domain(
2469 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2471 isl_basic_set *bset;
2472 isl_basic_set_list *list;
2473 isl_set *domain, *atomic_domain;
2474 int empty;
2476 domain = isl_set_copy(domains->option[atomic]);
2477 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2478 domain = isl_set_intersect(domain,
2479 isl_set_copy(domains->schedule_domain));
2480 empty = isl_set_is_empty(domain);
2481 if (empty < 0)
2482 class_domain = isl_set_free(class_domain);
2483 if (empty) {
2484 isl_set_free(domain);
2485 return class_domain;
2488 domain = isl_ast_build_eliminate(domains->build, domain);
2489 domain = isl_set_coalesce(domain);
2490 bset = isl_set_unshifted_simple_hull(domain);
2491 domain = isl_set_from_basic_set(bset);
2492 atomic_domain = isl_set_copy(domain);
2493 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2494 class_domain = isl_set_subtract(class_domain, atomic_domain);
2495 domain = isl_set_make_disjoint(domain);
2496 list = isl_basic_set_list_from_set(domain);
2497 domains->list = isl_basic_set_list_concat(domains->list, list);
2499 return class_domain;
2502 /* Split up the schedule domain into uniform basic sets,
2503 * in the sense that each element in a basic set is associated to
2504 * elements of the same domains, and add the result to domains->list.
2505 * Do this for that part of the schedule domain that lies in the
2506 * intersection of "class_domain" and the separate option domain.
2508 * "class_domain" may or may not include the constraints
2509 * of the schedule domain, but this does not make a difference
2510 * since we are going to intersect it with the domain of the inverse schedule.
2511 * If it includes schedule domain constraints, then they may involve
2512 * inner dimensions, but we will eliminate them in separation_domain.
2514 static int compute_separate_domain(struct isl_codegen_domains *domains,
2515 __isl_keep isl_set *class_domain)
2517 isl_space *space;
2518 isl_set *domain;
2519 isl_union_map *executed;
2520 isl_basic_set_list *list;
2521 int empty;
2523 domain = isl_set_copy(domains->option[separate]);
2524 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2525 executed = isl_union_map_copy(domains->executed);
2526 executed = isl_union_map_intersect_domain(executed,
2527 isl_union_set_from_set(domain));
2528 empty = isl_union_map_is_empty(executed);
2529 if (empty < 0 || empty) {
2530 isl_union_map_free(executed);
2531 return empty < 0 ? -1 : 0;
2534 space = isl_set_get_space(class_domain);
2535 domain = separate_schedule_domains(space, executed, domains->build);
2537 list = isl_basic_set_list_from_set(domain);
2538 domains->list = isl_basic_set_list_concat(domains->list, list);
2540 return 0;
2543 /* Split up the domain at the current depth into disjoint
2544 * basic sets for which code should be generated separately
2545 * for the given separation class domain.
2547 * If any separation classes have been defined, then "class_domain"
2548 * is the domain of the current class and does not refer to inner dimensions.
2549 * Otherwise, "class_domain" is the universe domain.
2551 * We first make sure that the class domain is disjoint from
2552 * previously considered class domains.
2554 * The separate domains can be computed directly from the "class_domain".
2556 * The unroll, atomic and remainder domains need the constraints
2557 * from the schedule domain.
2559 * For unrolling, the actual schedule domain is needed (with divs that
2560 * may refer to the current dimension) so that stride detection can be
2561 * performed.
2563 * For atomic and remainder domains, inner dimensions and divs involving
2564 * the current dimensions should be eliminated.
2565 * In case we are working within a separation class, we need to intersect
2566 * the result with the current "class_domain" to ensure that the domains
2567 * are disjoint from those generated from other class domains.
2569 * The domain that has been made atomic may be larger than specified
2570 * by the user since it needs to be representable as a single basic set.
2571 * This possibly larger domain is removed from class_domain by
2572 * compute_atomic_domain. It is computed first so that the extended domain
2573 * would not overlap with any domains computed before.
2574 * Similary, the unrolled domains may have some constraints removed and
2575 * may therefore also be larger than specified by the user.
2577 * If anything is left after handling separate, unroll and atomic,
2578 * we split it up into basic sets and append the basic sets to domains->list.
2580 static int compute_partial_domains(struct isl_codegen_domains *domains,
2581 __isl_take isl_set *class_domain)
2583 isl_basic_set_list *list;
2584 isl_set *domain;
2586 class_domain = isl_set_subtract(class_domain,
2587 isl_set_copy(domains->done));
2588 domains->done = isl_set_union(domains->done,
2589 isl_set_copy(class_domain));
2591 class_domain = compute_atomic_domain(domains, class_domain);
2592 class_domain = compute_unroll_domains(domains, class_domain);
2594 domain = isl_set_copy(class_domain);
2596 if (compute_separate_domain(domains, domain) < 0)
2597 goto error;
2598 domain = isl_set_subtract(domain,
2599 isl_set_copy(domains->option[separate]));
2601 domain = isl_set_intersect(domain,
2602 isl_set_copy(domains->schedule_domain));
2604 domain = isl_ast_build_eliminate(domains->build, domain);
2605 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2607 domain = isl_set_coalesce(domain);
2608 domain = isl_set_make_disjoint(domain);
2610 list = isl_basic_set_list_from_set(domain);
2611 domains->list = isl_basic_set_list_concat(domains->list, list);
2613 isl_set_free(class_domain);
2615 return 0;
2616 error:
2617 isl_set_free(domain);
2618 isl_set_free(class_domain);
2619 return -1;
2622 /* Split up the domain at the current depth into disjoint
2623 * basic sets for which code should be generated separately
2624 * for the separation class identified by "pnt".
2626 * We extract the corresponding class domain from domains->sep_class,
2627 * eliminate inner dimensions and pass control to compute_partial_domains.
2629 static int compute_class_domains(__isl_take isl_point *pnt, void *user)
2631 struct isl_codegen_domains *domains = user;
2632 isl_set *class_set;
2633 isl_set *domain;
2634 int disjoint;
2636 class_set = isl_set_from_point(pnt);
2637 domain = isl_map_domain(isl_map_intersect_range(
2638 isl_map_copy(domains->sep_class), class_set));
2639 domain = isl_ast_build_compute_gist(domains->build, domain);
2640 domain = isl_ast_build_eliminate(domains->build, domain);
2642 disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
2643 if (disjoint < 0)
2644 return -1;
2645 if (disjoint) {
2646 isl_set_free(domain);
2647 return 0;
2650 return compute_partial_domains(domains, domain);
2653 /* Extract the domains at the current depth that should be atomic,
2654 * separated or unrolled and store them in option.
2656 * The domains specified by the user might overlap, so we make
2657 * them disjoint by subtracting earlier domains from later domains.
2659 static void compute_domains_init_options(isl_set *option[3],
2660 __isl_keep isl_ast_build *build)
2662 enum isl_ast_build_domain_type type, type2;
2664 for (type = atomic; type <= separate; ++type) {
2665 option[type] = isl_ast_build_get_option_domain(build, type);
2666 for (type2 = atomic; type2 < type; ++type2)
2667 option[type] = isl_set_subtract(option[type],
2668 isl_set_copy(option[type2]));
2671 option[unroll] = isl_set_coalesce(option[unroll]);
2672 option[unroll] = isl_set_make_disjoint(option[unroll]);
2675 /* Split up the domain at the current depth into disjoint
2676 * basic sets for which code should be generated separately,
2677 * based on the user-specified options.
2678 * Return the list of disjoint basic sets.
2680 * There are three kinds of domains that we need to keep track of.
2681 * - the "schedule domain" is the domain of "executed"
2682 * - the "class domain" is the domain corresponding to the currrent
2683 * separation class
2684 * - the "option domain" is the domain corresponding to one of the options
2685 * atomic, unroll or separate
2687 * We first consider the individial values of the separation classes
2688 * and split up the domain for each of them separately.
2689 * Finally, we consider the remainder. If no separation classes were
2690 * specified, then we call compute_partial_domains with the universe
2691 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
2692 * with inner dimensions removed. We do this because we want to
2693 * avoid computing the complement of the class domains (i.e., the difference
2694 * between the universe and domains->done).
2696 static __isl_give isl_basic_set_list *compute_domains(
2697 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2699 struct isl_codegen_domains domains;
2700 isl_ctx *ctx;
2701 isl_set *domain;
2702 isl_union_set *schedule_domain;
2703 isl_set *classes;
2704 isl_space *space;
2705 int n_param;
2706 enum isl_ast_build_domain_type type;
2707 int empty;
2709 if (!executed)
2710 return NULL;
2712 ctx = isl_union_map_get_ctx(executed);
2713 domains.list = isl_basic_set_list_alloc(ctx, 0);
2715 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
2716 domain = isl_set_from_union_set(schedule_domain);
2718 compute_domains_init_options(domains.option, build);
2720 domains.sep_class = isl_ast_build_get_separation_class(build);
2721 classes = isl_map_range(isl_map_copy(domains.sep_class));
2722 n_param = isl_set_dim(classes, isl_dim_param);
2723 classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
2725 space = isl_set_get_space(domain);
2726 domains.build = build;
2727 domains.schedule_domain = isl_set_copy(domain);
2728 domains.executed = executed;
2729 domains.done = isl_set_empty(space);
2731 if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
2732 domains.list = isl_basic_set_list_free(domains.list);
2733 isl_set_free(classes);
2735 empty = isl_set_is_empty(domains.done);
2736 if (empty < 0) {
2737 domains.list = isl_basic_set_list_free(domains.list);
2738 domain = isl_set_free(domain);
2739 } else if (empty) {
2740 isl_set_free(domain);
2741 domain = isl_set_universe(isl_set_get_space(domains.done));
2742 } else {
2743 domain = isl_ast_build_eliminate(build, domain);
2745 if (compute_partial_domains(&domains, domain) < 0)
2746 domains.list = isl_basic_set_list_free(domains.list);
2748 isl_set_free(domains.schedule_domain);
2749 isl_set_free(domains.done);
2750 isl_map_free(domains.sep_class);
2751 for (type = atomic; type <= separate; ++type)
2752 isl_set_free(domains.option[type]);
2754 return domains.list;
2757 /* Generate code for a single component, after shifting (if any)
2758 * has been applied.
2760 * We first split up the domain at the current depth into disjoint
2761 * basic sets based on the user-specified options.
2762 * Then we generated code for each of them and concatenate the results.
2764 static __isl_give isl_ast_graft_list *generate_shifted_component(
2765 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
2767 isl_basic_set_list *domain_list;
2768 isl_ast_graft_list *list = NULL;
2770 domain_list = compute_domains(executed, build);
2771 list = generate_parallel_domains(domain_list, executed, build);
2773 isl_basic_set_list_free(domain_list);
2774 isl_union_map_free(executed);
2775 isl_ast_build_free(build);
2777 return list;
2780 struct isl_set_map_pair {
2781 isl_set *set;
2782 isl_map *map;
2785 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2786 * of indices into the "domain" array,
2787 * return the union of the "map" fields of the elements
2788 * indexed by the first "n" elements of "order".
2790 static __isl_give isl_union_map *construct_component_executed(
2791 struct isl_set_map_pair *domain, int *order, int n)
2793 int i;
2794 isl_map *map;
2795 isl_union_map *executed;
2797 map = isl_map_copy(domain[order[0]].map);
2798 executed = isl_union_map_from_map(map);
2799 for (i = 1; i < n; ++i) {
2800 map = isl_map_copy(domain[order[i]].map);
2801 executed = isl_union_map_add_map(executed, map);
2804 return executed;
2807 /* Generate code for a single component, after shifting (if any)
2808 * has been applied.
2810 * The component inverse schedule is specified as the "map" fields
2811 * of the elements of "domain" indexed by the first "n" elements of "order".
2813 static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
2814 struct isl_set_map_pair *domain, int *order, int n,
2815 __isl_take isl_ast_build *build)
2817 isl_union_map *executed;
2819 executed = construct_component_executed(domain, order, n);
2820 return generate_shifted_component(executed, build);
2823 /* Does set dimension "pos" of "set" have an obviously fixed value?
2825 static int dim_is_fixed(__isl_keep isl_set *set, int pos)
2827 int fixed;
2828 isl_val *v;
2830 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, pos);
2831 if (!v)
2832 return -1;
2833 fixed = !isl_val_is_nan(v);
2834 isl_val_free(v);
2836 return fixed;
2839 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2840 * of indices into the "domain" array,
2841 * do all (except for at most one) of the "set" field of the elements
2842 * indexed by the first "n" elements of "order" have a fixed value
2843 * at position "depth"?
2845 static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
2846 int *order, int n, int depth)
2848 int i;
2849 int non_fixed = -1;
2851 for (i = 0; i < n; ++i) {
2852 int f;
2854 f = dim_is_fixed(domain[order[i]].set, depth);
2855 if (f < 0)
2856 return -1;
2857 if (f)
2858 continue;
2859 if (non_fixed >= 0)
2860 return 0;
2861 non_fixed = i;
2864 return 1;
2867 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2868 * of indices into the "domain" array,
2869 * eliminate the inner dimensions from the "set" field of the elements
2870 * indexed by the first "n" elements of "order", provided the current
2871 * dimension does not have a fixed value.
2873 * Return the index of the first element in "order" with a corresponding
2874 * "set" field that does not have an (obviously) fixed value.
2876 static int eliminate_non_fixed(struct isl_set_map_pair *domain,
2877 int *order, int n, int depth, __isl_keep isl_ast_build *build)
2879 int i;
2880 int base = -1;
2882 for (i = n - 1; i >= 0; --i) {
2883 int f;
2884 f = dim_is_fixed(domain[order[i]].set, depth);
2885 if (f < 0)
2886 return -1;
2887 if (f)
2888 continue;
2889 domain[order[i]].set = isl_ast_build_eliminate_inner(build,
2890 domain[order[i]].set);
2891 base = i;
2894 return base;
2897 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2898 * of indices into the "domain" array,
2899 * find the element of "domain" (amongst those indexed by the first "n"
2900 * elements of "order") with the "set" field that has the smallest
2901 * value for the current iterator.
2903 * Note that the domain with the smallest value may depend on the parameters
2904 * and/or outer loop dimension. Since the result of this function is only
2905 * used as heuristic, we only make a reasonable attempt at finding the best
2906 * domain, one that should work in case a single domain provides the smallest
2907 * value for the current dimension over all values of the parameters
2908 * and outer dimensions.
2910 * In particular, we compute the smallest value of the first domain
2911 * and replace it by that of any later domain if that later domain
2912 * has a smallest value that is smaller for at least some value
2913 * of the parameters and outer dimensions.
2915 static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
2916 __isl_keep isl_ast_build *build)
2918 int i;
2919 isl_map *min_first;
2920 int first = 0;
2922 min_first = isl_ast_build_map_to_iterator(build,
2923 isl_set_copy(domain[order[0]].set));
2924 min_first = isl_map_lexmin(min_first);
2926 for (i = 1; i < n; ++i) {
2927 isl_map *min, *test;
2928 int empty;
2930 min = isl_ast_build_map_to_iterator(build,
2931 isl_set_copy(domain[order[i]].set));
2932 min = isl_map_lexmin(min);
2933 test = isl_map_copy(min);
2934 test = isl_map_apply_domain(isl_map_copy(min_first), test);
2935 test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
2936 empty = isl_map_is_empty(test);
2937 isl_map_free(test);
2938 if (empty >= 0 && !empty) {
2939 isl_map_free(min_first);
2940 first = i;
2941 min_first = min;
2942 } else
2943 isl_map_free(min);
2945 if (empty < 0)
2946 break;
2949 isl_map_free(min_first);
2951 return i < n ? -1 : first;
2954 /* Construct a shifted inverse schedule based on the original inverse schedule,
2955 * the stride and the offset.
2957 * The original inverse schedule is specified as the "map" fields
2958 * of the elements of "domain" indexed by the first "n" elements of "order".
2960 * "stride" and "offset" are such that the difference
2961 * between the values of the current dimension of domain "i"
2962 * and the values of the current dimension for some reference domain are
2963 * equal to
2965 * stride * integer + offset[i]
2967 * Moreover, 0 <= offset[i] < stride.
2969 * For each domain, we create a map
2971 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
2973 * where j refers to the current dimension and the other dimensions are
2974 * unchanged, and apply this map to the original schedule domain.
2976 * For example, for the original schedule
2978 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
2980 * and assuming the offset is 0 for the A domain and 1 for the B domain,
2981 * we apply the mapping
2983 * { [j] -> [j, 0] }
2985 * to the schedule of the "A" domain and the mapping
2987 * { [j - 1] -> [j, 1] }
2989 * to the schedule of the "B" domain.
2992 * Note that after the transformation, the differences between pairs
2993 * of values of the current dimension over all domains are multiples
2994 * of stride and that we have therefore exposed the stride.
2997 * To see that the mapping preserves the lexicographic order,
2998 * first note that each of the individual maps above preserves the order.
2999 * If the value of the current iterator is j1 in one domain and j2 in another,
3000 * then if j1 = j2, we know that the same map is applied to both domains
3001 * and the order is preserved.
3002 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3003 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3005 * j1 - c1 < j2 - c2
3007 * and the order is preserved.
3008 * If c1 < c2, then we know
3010 * 0 <= c2 - c1 < s
3012 * We also have
3014 * j2 - j1 = n * s + r
3016 * with n >= 0 and 0 <= r < s.
3017 * In other words, r = c2 - c1.
3018 * If n > 0, then
3020 * j1 - c1 < j2 - c2
3022 * If n = 0, then
3024 * j1 - c1 = j2 - c2
3026 * and so
3028 * (j1 - c1, c1) << (j2 - c2, c2)
3030 * with "<<" the lexicographic order, proving that the order is preserved
3031 * in all cases.
3033 static __isl_give isl_union_map *contruct_shifted_executed(
3034 struct isl_set_map_pair *domain, int *order, int n,
3035 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3036 __isl_take isl_ast_build *build)
3038 int i;
3039 isl_union_map *executed;
3040 isl_space *space;
3041 isl_map *map;
3042 int depth;
3043 isl_constraint *c;
3045 depth = isl_ast_build_get_depth(build);
3046 space = isl_ast_build_get_space(build, 1);
3047 executed = isl_union_map_empty(isl_space_copy(space));
3048 space = isl_space_map_from_set(space);
3049 map = isl_map_identity(isl_space_copy(space));
3050 map = isl_map_eliminate(map, isl_dim_out, depth, 1);
3051 map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
3052 space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
3054 c = isl_equality_alloc(isl_local_space_from_space(space));
3055 c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
3056 c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
3058 for (i = 0; i < n; ++i) {
3059 isl_map *map_i;
3060 isl_val *v;
3062 v = isl_multi_val_get_val(offset, i);
3063 if (!v)
3064 break;
3065 map_i = isl_map_copy(map);
3066 map_i = isl_map_fix_val(map_i, isl_dim_out, depth + 1,
3067 isl_val_copy(v));
3068 v = isl_val_neg(v);
3069 c = isl_constraint_set_constant_val(c, v);
3070 map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
3072 map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
3073 map_i);
3074 executed = isl_union_map_add_map(executed, map_i);
3077 isl_constraint_free(c);
3078 isl_map_free(map);
3080 if (i < n)
3081 executed = isl_union_map_free(executed);
3083 return executed;
3086 /* Generate code for a single component, after exposing the stride,
3087 * given that the schedule domain is "shifted strided".
3089 * The component inverse schedule is specified as the "map" fields
3090 * of the elements of "domain" indexed by the first "n" elements of "order".
3092 * The schedule domain being "shifted strided" means that the differences
3093 * between the values of the current dimension of domain "i"
3094 * and the values of the current dimension for some reference domain are
3095 * equal to
3097 * stride * integer + offset[i]
3099 * We first look for the domain with the "smallest" value for the current
3100 * dimension and adjust the offsets such that the offset of the "smallest"
3101 * domain is equal to zero. The other offsets are reduced modulo stride.
3103 * Based on this information, we construct a new inverse schedule in
3104 * contruct_shifted_executed that exposes the stride.
3105 * Since this involves the introduction of a new schedule dimension,
3106 * the build needs to be changed accodingly.
3107 * After computing the AST, the newly introduced dimension needs
3108 * to be removed again from the list of grafts. We do this by plugging
3109 * in a mapping that represents the new schedule domain in terms of the
3110 * old schedule domain.
3112 static __isl_give isl_ast_graft_list *generate_shift_component(
3113 struct isl_set_map_pair *domain, int *order, int n,
3114 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3115 __isl_take isl_ast_build *build)
3117 isl_ast_graft_list *list;
3118 int first;
3119 int depth;
3120 isl_ctx *ctx;
3121 isl_val *val;
3122 isl_multi_val *mv;
3123 isl_space *space;
3124 isl_multi_aff *ma, *zero;
3125 isl_union_map *executed;
3127 ctx = isl_ast_build_get_ctx(build);
3128 depth = isl_ast_build_get_depth(build);
3130 first = first_offset(domain, order, n, build);
3131 if (first < 0)
3132 goto error;
3134 mv = isl_multi_val_copy(offset);
3135 val = isl_multi_val_get_val(offset, first);
3136 val = isl_val_neg(val);
3137 mv = isl_multi_val_add_val(mv, val);
3138 mv = isl_multi_val_mod_val(mv, isl_val_copy(stride));
3140 executed = contruct_shifted_executed(domain, order, n, stride, mv,
3141 build);
3142 space = isl_ast_build_get_space(build, 1);
3143 space = isl_space_map_from_set(space);
3144 ma = isl_multi_aff_identity(isl_space_copy(space));
3145 space = isl_space_from_domain(isl_space_domain(space));
3146 space = isl_space_add_dims(space, isl_dim_out, 1);
3147 zero = isl_multi_aff_zero(space);
3148 ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
3149 build = isl_ast_build_insert_dim(build, depth + 1);
3150 list = generate_shifted_component(executed, build);
3152 list = isl_ast_graft_list_preimage_multi_aff(list, ma);
3154 isl_multi_val_free(mv);
3156 return list;
3157 error:
3158 isl_ast_build_free(build);
3159 return NULL;
3162 /* Generate code for a single component.
3164 * The component inverse schedule is specified as the "map" fields
3165 * of the elements of "domain" indexed by the first "n" elements of "order".
3167 * This function may modify the "set" fields of "domain".
3169 * Before proceeding with the actual code generation for the component,
3170 * we first check if there are any "shifted" strides, meaning that
3171 * the schedule domains of the individual domains are all strided,
3172 * but that they have different offsets, resulting in the union
3173 * of schedule domains not being strided anymore.
3175 * The simplest example is the schedule
3177 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3179 * Both schedule domains are strided, but their union is not.
3180 * This function detects such cases and then rewrites the schedule to
3182 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
3184 * In the new schedule, the schedule domains have the same offset (modulo
3185 * the stride), ensuring that the union of schedule domains is also strided.
3188 * If there is only a single domain in the component, then there is
3189 * nothing to do. Similarly, if the current schedule dimension has
3190 * a fixed value for almost all domains then there is nothing to be done.
3191 * In particular, we need at least two domains where the current schedule
3192 * dimension does not have a fixed value.
3193 * Finally, if any of the options refer to the current schedule dimension,
3194 * then we bail out as well. It would be possible to reformulate the options
3195 * in terms of the new schedule domain, but that would introduce constraints
3196 * that separate the domains in the options and that is something we would
3197 * like to avoid.
3200 * To see if there is any shifted stride, we look at the differences
3201 * between the values of the current dimension in pairs of domains
3202 * for equal values of outer dimensions. These differences should be
3203 * of the form
3205 * m x + r
3207 * with "m" the stride and "r" a constant. Note that we cannot perform
3208 * this analysis on individual domains as the lower bound in each domain
3209 * may depend on parameters or outer dimensions and so the current dimension
3210 * itself may not have a fixed remainder on division by the stride.
3212 * In particular, we compare the first domain that does not have an
3213 * obviously fixed value for the current dimension to itself and all
3214 * other domains and collect the offsets and the gcd of the strides.
3215 * If the gcd becomes one, then we failed to find shifted strides.
3216 * If the gcd is zero, then the differences were all fixed, meaning
3217 * that some domains had non-obviously fixed values for the current dimension.
3218 * If all the offsets are the same (for those domains that do not have
3219 * an obviously fixed value for the current dimension), then we do not
3220 * apply the transformation.
3221 * If none of the domains were skipped, then there is nothing to do.
3222 * If some of them were skipped, then if we apply separation, the schedule
3223 * domain should get split in pieces with a (non-shifted) stride.
3225 * Otherwise, we apply a shift to expose the stride in
3226 * generate_shift_component.
3228 static __isl_give isl_ast_graft_list *generate_component(
3229 struct isl_set_map_pair *domain, int *order, int n,
3230 __isl_take isl_ast_build *build)
3232 int i, d;
3233 int depth;
3234 isl_ctx *ctx;
3235 isl_map *map;
3236 isl_set *deltas;
3237 isl_val *gcd = NULL;
3238 isl_multi_val *mv;
3239 int fixed, skip;
3240 int base;
3241 isl_ast_graft_list *list;
3242 int res = 0;
3244 depth = isl_ast_build_get_depth(build);
3246 skip = n == 1;
3247 if (skip >= 0 && !skip)
3248 skip = at_most_one_non_fixed(domain, order, n, depth);
3249 if (skip >= 0 && !skip)
3250 skip = isl_ast_build_options_involve_depth(build);
3251 if (skip < 0)
3252 goto error;
3253 if (skip)
3254 return generate_shifted_component_from_list(domain,
3255 order, n, build);
3257 base = eliminate_non_fixed(domain, order, n, depth, build);
3258 if (base < 0)
3259 goto error;
3261 ctx = isl_ast_build_get_ctx(build);
3263 mv = isl_multi_val_zero(isl_space_set_alloc(ctx, 0, n));
3265 fixed = 1;
3266 for (i = 0; i < n; ++i) {
3267 isl_val *r, *m;
3269 map = isl_map_from_domain_and_range(
3270 isl_set_copy(domain[order[base]].set),
3271 isl_set_copy(domain[order[i]].set));
3272 for (d = 0; d < depth; ++d)
3273 map = isl_map_equate(map, isl_dim_in, d,
3274 isl_dim_out, d);
3275 deltas = isl_map_deltas(map);
3276 res = isl_set_dim_residue_class_val(deltas, depth, &m, &r);
3277 isl_set_free(deltas);
3278 if (res < 0)
3279 break;
3281 if (i == 0)
3282 gcd = m;
3283 else
3284 gcd = isl_val_gcd(gcd, m);
3285 if (isl_val_is_one(gcd)) {
3286 isl_val_free(r);
3287 break;
3289 mv = isl_multi_val_set_val(mv, i, r);
3291 res = dim_is_fixed(domain[order[i]].set, depth);
3292 if (res < 0)
3293 break;
3294 if (res)
3295 continue;
3297 if (fixed && i > base) {
3298 isl_val *a, *b;
3299 a = isl_multi_val_get_val(mv, i);
3300 b = isl_multi_val_get_val(mv, base);
3301 if (isl_val_ne(a, b))
3302 fixed = 0;
3303 isl_val_free(a);
3304 isl_val_free(b);
3308 if (res < 0 || !gcd) {
3309 isl_ast_build_free(build);
3310 list = NULL;
3311 } else if (i < n || fixed || isl_val_is_zero(gcd)) {
3312 list = generate_shifted_component_from_list(domain,
3313 order, n, build);
3314 } else {
3315 list = generate_shift_component(domain, order, n, gcd, mv,
3316 build);
3319 isl_val_free(gcd);
3320 isl_multi_val_free(mv);
3322 return list;
3323 error:
3324 isl_ast_build_free(build);
3325 return NULL;
3328 /* Store both "map" itself and its domain in the
3329 * structure pointed to by *next and advance to the next array element.
3331 static int extract_domain(__isl_take isl_map *map, void *user)
3333 struct isl_set_map_pair **next = user;
3335 (*next)->map = isl_map_copy(map);
3336 (*next)->set = isl_map_domain(map);
3337 (*next)++;
3339 return 0;
3342 /* Internal data for any_scheduled_after.
3344 * "depth" is the number of loops that have already been generated
3345 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
3346 * "domain" is an array of set-map pairs corresponding to the different
3347 * iteration domains. The set is the schedule domain, i.e., the domain
3348 * of the inverse schedule, while the map is the inverse schedule itself.
3350 struct isl_any_scheduled_after_data {
3351 int depth;
3352 int group_coscheduled;
3353 struct isl_set_map_pair *domain;
3356 /* Is any element of domain "i" scheduled after any element of domain "j"
3357 * (for a common iteration of the first data->depth loops)?
3359 * data->domain[i].set contains the domain of the inverse schedule
3360 * for domain "i", i.e., elements in the schedule domain.
3362 * If data->group_coscheduled is set, then we also return 1 if there
3363 * is any pair of elements in the two domains that are scheduled together.
3365 static int any_scheduled_after(int i, int j, void *user)
3367 struct isl_any_scheduled_after_data *data = user;
3368 int dim = isl_set_dim(data->domain[i].set, isl_dim_set);
3369 int pos;
3371 for (pos = data->depth; pos < dim; ++pos) {
3372 int follows;
3374 follows = isl_set_follows_at(data->domain[i].set,
3375 data->domain[j].set, pos);
3377 if (follows < -1)
3378 return -1;
3379 if (follows > 0)
3380 return 1;
3381 if (follows < 0)
3382 return 0;
3385 return data->group_coscheduled;
3388 /* Look for independent components at the current depth and generate code
3389 * for each component separately. The resulting lists of grafts are
3390 * merged in an attempt to combine grafts with identical guards.
3392 * Code for two domains can be generated separately if all the elements
3393 * of one domain are scheduled before (or together with) all the elements
3394 * of the other domain. We therefore consider the graph with as nodes
3395 * the domains and an edge between two nodes if any element of the first
3396 * node is scheduled after any element of the second node.
3397 * If the ast_build_group_coscheduled is set, then we also add an edge if
3398 * there is any pair of elements in the two domains that are scheduled
3399 * together.
3400 * Code is then generated (by generate_component)
3401 * for each of the strongly connected components in this graph
3402 * in their topological order.
3404 * Since the test is performed on the domain of the inverse schedules of
3405 * the different domains, we precompute these domains and store
3406 * them in data.domain.
3408 static __isl_give isl_ast_graft_list *generate_components(
3409 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3411 int i;
3412 isl_ctx *ctx = isl_ast_build_get_ctx(build);
3413 int n = isl_union_map_n_map(executed);
3414 struct isl_any_scheduled_after_data data;
3415 struct isl_set_map_pair *next;
3416 struct isl_tarjan_graph *g = NULL;
3417 isl_ast_graft_list *list = NULL;
3418 int n_domain = 0;
3420 data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
3421 if (!data.domain)
3422 goto error;
3423 n_domain = n;
3425 next = data.domain;
3426 if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
3427 goto error;
3429 if (!build)
3430 goto error;
3431 data.depth = isl_ast_build_get_depth(build);
3432 data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
3433 g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
3434 if (!g)
3435 goto error;
3437 list = isl_ast_graft_list_alloc(ctx, 0);
3439 i = 0;
3440 while (list && n) {
3441 isl_ast_graft_list *list_c;
3442 int first = i;
3444 if (g->order[i] == -1)
3445 isl_die(ctx, isl_error_internal, "cannot happen",
3446 goto error);
3447 ++i; --n;
3448 while (g->order[i] != -1) {
3449 ++i; --n;
3452 list_c = generate_component(data.domain,
3453 g->order + first, i - first,
3454 isl_ast_build_copy(build));
3455 list = isl_ast_graft_list_merge(list, list_c, build);
3457 ++i;
3460 if (0)
3461 error: list = isl_ast_graft_list_free(list);
3462 isl_tarjan_graph_free(g);
3463 for (i = 0; i < n_domain; ++i) {
3464 isl_map_free(data.domain[i].map);
3465 isl_set_free(data.domain[i].set);
3467 free(data.domain);
3468 isl_union_map_free(executed);
3469 isl_ast_build_free(build);
3471 return list;
3474 /* Generate code for the next level (and all inner levels).
3476 * If "executed" is empty, i.e., no code needs to be generated,
3477 * then we return an empty list.
3479 * If we have already generated code for all loop levels, then we pass
3480 * control to generate_inner_level.
3482 * If "executed" lives in a single space, i.e., if code needs to be
3483 * generated for a single domain, then there can only be a single
3484 * component and we go directly to generate_shifted_component.
3485 * Otherwise, we call generate_components to detect the components
3486 * and to call generate_component on each of them separately.
3488 static __isl_give isl_ast_graft_list *generate_next_level(
3489 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3491 int depth;
3493 if (!build || !executed)
3494 goto error;
3496 if (isl_union_map_is_empty(executed)) {
3497 isl_ctx *ctx = isl_ast_build_get_ctx(build);
3498 isl_union_map_free(executed);
3499 isl_ast_build_free(build);
3500 return isl_ast_graft_list_alloc(ctx, 0);
3503 depth = isl_ast_build_get_depth(build);
3504 if (depth >= isl_ast_build_dim(build, isl_dim_set))
3505 return generate_inner_level(executed, build);
3507 if (isl_union_map_n_map(executed) == 1)
3508 return generate_shifted_component(executed, build);
3510 return generate_components(executed, build);
3511 error:
3512 isl_union_map_free(executed);
3513 isl_ast_build_free(build);
3514 return NULL;
3517 /* Internal data structure used by isl_ast_build_ast_from_schedule.
3518 * internal, executed and build are the inputs to generate_code.
3519 * list collects the output.
3521 struct isl_generate_code_data {
3522 int internal;
3523 isl_union_map *executed;
3524 isl_ast_build *build;
3526 isl_ast_graft_list *list;
3529 /* Given an inverse schedule in terms of the external build schedule, i.e.,
3531 * [E -> S] -> D
3533 * with E the external build schedule and S the additional schedule "space",
3534 * reformulate the inverse schedule in terms of the internal schedule domain,
3535 * i.e., return
3537 * [I -> S] -> D
3539 * We first obtain a mapping
3541 * I -> E
3543 * take the inverse and the product with S -> S, resulting in
3545 * [I -> S] -> [E -> S]
3547 * Applying the map to the input produces the desired result.
3549 static __isl_give isl_union_map *internal_executed(
3550 __isl_take isl_union_map *executed, __isl_keep isl_space *space,
3551 __isl_keep isl_ast_build *build)
3553 isl_map *id, *proj;
3555 proj = isl_ast_build_get_schedule_map(build);
3556 proj = isl_map_reverse(proj);
3557 space = isl_space_map_from_set(isl_space_copy(space));
3558 id = isl_map_identity(space);
3559 proj = isl_map_product(proj, id);
3560 executed = isl_union_map_apply_domain(executed,
3561 isl_union_map_from_map(proj));
3562 return executed;
3565 /* Generate an AST that visits the elements in the range of data->executed
3566 * in the relative order specified by the corresponding domain element(s)
3567 * for those domain elements that belong to "set".
3568 * Add the result to data->list.
3570 * The caller ensures that "set" is a universe domain.
3571 * "space" is the space of the additional part of the schedule.
3572 * It is equal to the space of "set" if build->domain is parametric.
3573 * Otherwise, it is equal to the range of the wrapped space of "set".
3575 * If the build space is not parametric and if isl_ast_build_ast_from_schedule
3576 * was called from an outside user (data->internal not set), then
3577 * the (inverse) schedule refers to the external build domain and needs to
3578 * be transformed to refer to the internal build domain.
3580 * If the build space is parametric, then we add some of the parameter
3581 * constraints to the executed relation. Adding these constraints
3582 * allows for an earlier detection of conflicts in some cases.
3583 * However, we do not want to divide the executed relation into
3584 * more disjuncts than necessary. We therefore approximate
3585 * the constraints on the parameters by a single disjunct set.
3587 * The build is extended to include the additional part of the schedule.
3588 * If the original build space was not parametric, then the options
3589 * in data->build refer only to the additional part of the schedule
3590 * and they need to be adjusted to refer to the complete AST build
3591 * domain.
3593 * After having adjusted inverse schedule and build, we start generating
3594 * code with the outer loop of the current code generation
3595 * in generate_next_level.
3597 * If the original build space was not parametric, we undo the embedding
3598 * on the resulting isl_ast_node_list so that it can be used within
3599 * the outer AST build.
3601 static int generate_code_in_space(struct isl_generate_code_data *data,
3602 __isl_take isl_set *set, __isl_take isl_space *space)
3604 isl_union_map *executed;
3605 isl_ast_build *build;
3606 isl_ast_graft_list *list;
3607 int embed;
3609 executed = isl_union_map_copy(data->executed);
3610 executed = isl_union_map_intersect_domain(executed,
3611 isl_union_set_from_set(set));
3613 embed = !isl_set_is_params(data->build->domain);
3614 if (embed && !data->internal)
3615 executed = internal_executed(executed, space, data->build);
3616 if (!embed) {
3617 isl_set *domain;
3618 domain = isl_ast_build_get_domain(data->build);
3619 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
3620 executed = isl_union_map_intersect_params(executed, domain);
3623 build = isl_ast_build_copy(data->build);
3624 build = isl_ast_build_product(build, space);
3626 list = generate_next_level(executed, build);
3628 list = isl_ast_graft_list_unembed(list, embed);
3630 data->list = isl_ast_graft_list_concat(data->list, list);
3632 return 0;
3635 /* Generate an AST that visits the elements in the range of data->executed
3636 * in the relative order specified by the corresponding domain element(s)
3637 * for those domain elements that belong to "set".
3638 * Add the result to data->list.
3640 * The caller ensures that "set" is a universe domain.
3642 * If the build space S is not parametric, then the space of "set"
3643 * need to be a wrapped relation with S as domain. That is, it needs
3644 * to be of the form
3646 * [S -> T]
3648 * Check this property and pass control to generate_code_in_space
3649 * passing along T.
3650 * If the build space is not parametric, then T is the space of "set".
3652 static int generate_code_set(__isl_take isl_set *set, void *user)
3654 struct isl_generate_code_data *data = user;
3655 isl_space *space, *build_space;
3656 int is_domain;
3658 space = isl_set_get_space(set);
3660 if (isl_set_is_params(data->build->domain))
3661 return generate_code_in_space(data, set, space);
3663 build_space = isl_ast_build_get_space(data->build, data->internal);
3664 space = isl_space_unwrap(space);
3665 is_domain = isl_space_is_domain(build_space, space);
3666 isl_space_free(build_space);
3667 space = isl_space_range(space);
3669 if (is_domain < 0)
3670 goto error;
3671 if (!is_domain)
3672 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3673 "invalid nested schedule space", goto error);
3675 return generate_code_in_space(data, set, space);
3676 error:
3677 isl_set_free(set);
3678 isl_space_free(space);
3679 return -1;
3682 /* Generate an AST that visits the elements in the range of "executed"
3683 * in the relative order specified by the corresponding domain element(s).
3685 * "build" is an isl_ast_build that has either been constructed by
3686 * isl_ast_build_from_context or passed to a callback set by
3687 * isl_ast_build_set_create_leaf.
3688 * In the first case, the space of the isl_ast_build is typically
3689 * a parametric space, although this is currently not enforced.
3690 * In the second case, the space is never a parametric space.
3691 * If the space S is not parametric, then the domain space(s) of "executed"
3692 * need to be wrapped relations with S as domain.
3694 * If the domain of "executed" consists of several spaces, then an AST
3695 * is generated for each of them (in arbitrary order) and the results
3696 * are concatenated.
3698 * If "internal" is set, then the domain "S" above refers to the internal
3699 * schedule domain representation. Otherwise, it refers to the external
3700 * representation, as returned by isl_ast_build_get_schedule_space.
3702 * We essentially run over all the spaces in the domain of "executed"
3703 * and call generate_code_set on each of them.
3705 static __isl_give isl_ast_graft_list *generate_code(
3706 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3707 int internal)
3709 isl_ctx *ctx;
3710 struct isl_generate_code_data data = { 0 };
3711 isl_space *space;
3712 isl_union_set *schedule_domain;
3713 isl_union_map *universe;
3715 if (!build)
3716 goto error;
3717 space = isl_ast_build_get_space(build, 1);
3718 space = isl_space_align_params(space,
3719 isl_union_map_get_space(executed));
3720 space = isl_space_align_params(space,
3721 isl_union_map_get_space(build->options));
3722 build = isl_ast_build_align_params(build, isl_space_copy(space));
3723 executed = isl_union_map_align_params(executed, space);
3724 if (!executed || !build)
3725 goto error;
3727 ctx = isl_ast_build_get_ctx(build);
3729 data.internal = internal;
3730 data.executed = executed;
3731 data.build = build;
3732 data.list = isl_ast_graft_list_alloc(ctx, 0);
3734 universe = isl_union_map_universe(isl_union_map_copy(executed));
3735 schedule_domain = isl_union_map_domain(universe);
3736 if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
3737 &data) < 0)
3738 data.list = isl_ast_graft_list_free(data.list);
3740 isl_union_set_free(schedule_domain);
3741 isl_union_map_free(executed);
3743 isl_ast_build_free(build);
3744 return data.list;
3745 error:
3746 isl_union_map_free(executed);
3747 isl_ast_build_free(build);
3748 return NULL;
3751 /* Generate an AST that visits the elements in the domain of "schedule"
3752 * in the relative order specified by the corresponding image element(s).
3754 * "build" is an isl_ast_build that has either been constructed by
3755 * isl_ast_build_from_context or passed to a callback set by
3756 * isl_ast_build_set_create_leaf.
3757 * In the first case, the space of the isl_ast_build is typically
3758 * a parametric space, although this is currently not enforced.
3759 * In the second case, the space is never a parametric space.
3760 * If the space S is not parametric, then the range space(s) of "schedule"
3761 * need to be wrapped relations with S as domain.
3763 * If the range of "schedule" consists of several spaces, then an AST
3764 * is generated for each of them (in arbitrary order) and the results
3765 * are concatenated.
3767 * We first initialize the local copies of the relevant options.
3768 * We do this here rather than when the isl_ast_build is created
3769 * because the options may have changed between the construction
3770 * of the isl_ast_build and the call to isl_generate_code.
3772 * The main computation is performed on an inverse schedule (with
3773 * the schedule domain in the domain and the elements to be executed
3774 * in the range) called "executed".
3776 __isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
3777 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
3779 isl_ast_graft_list *list;
3780 isl_ast_node *node;
3781 isl_union_map *executed;
3783 build = isl_ast_build_copy(build);
3784 build = isl_ast_build_set_single_valued(build, 0);
3785 schedule = isl_union_map_coalesce(schedule);
3786 executed = isl_union_map_reverse(schedule);
3787 list = generate_code(executed, isl_ast_build_copy(build), 0);
3788 node = isl_ast_node_from_graft_list(list, build);
3789 isl_ast_build_free(build);
3791 return node;