isl_ast_codegen.c: add_node: replace gist by pure specialization
[isl.git] / isl_ast_codegen.c
blobff8716d3cc1a15386c22cee7ffc01b1e0a40f05c
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 if (isl_ast_build_has_stride(build, depth)) {
569 isl_set *stride;
571 stride = isl_ast_build_get_stride_constraint(build);
572 dom = isl_set_intersect(dom, stride);
574 dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
575 dom = isl_ast_build_compute_gist(build, dom);
577 graft = isl_ast_graft_add_guard(graft, dom, build);
579 return graft;
582 /* Update "graft" based on "bounds" for the eliminated case.
584 * In the eliminated case, no for node is created, so we only need
585 * to check if "bounds" imply any guards that need to be inserted.
587 static __isl_give isl_ast_graft *refine_eliminated(
588 __isl_take isl_ast_graft *graft, __isl_keep isl_basic_set *bounds,
589 __isl_keep isl_ast_build *build)
591 return add_degenerate_guard(graft, bounds, build);
594 /* Update "graft" based on "bounds" and "sub_build" for the degenerate case.
596 * "build" is the build in which graft->node was created
597 * "sub_build" contains information about the current level itself,
598 * including the single value attained.
600 * We first set the initialization part of the for loop to the single
601 * value attained by the current dimension.
602 * The increment and condition are not strictly needed as the are known
603 * to be "1" and "iterator <= value" respectively.
604 * Then we check if "bounds" imply any guards that need to be inserted.
606 static __isl_give isl_ast_graft *refine_degenerate(
607 __isl_take isl_ast_graft *graft, __isl_keep isl_basic_set *bounds,
608 __isl_keep isl_ast_build *build,
609 __isl_keep isl_ast_build *sub_build)
611 isl_pw_aff *value;
613 if (!graft || !sub_build)
614 return isl_ast_graft_free(graft);
616 value = isl_pw_aff_copy(sub_build->value);
618 graft->node->u.f.init = isl_ast_build_expr_from_pw_aff_internal(build,
619 value);
620 if (!graft->node->u.f.init)
621 return isl_ast_graft_free(graft);
623 graft = add_degenerate_guard(graft, bounds, build);
625 return graft;
628 /* Return the intersection of constraints in "list" as a set.
630 static __isl_give isl_set *intersect_constraints(
631 __isl_keep isl_constraint_list *list)
633 int i, n;
634 isl_basic_set *bset;
636 n = isl_constraint_list_n_constraint(list);
637 if (n < 1)
638 isl_die(isl_constraint_list_get_ctx(list), isl_error_internal,
639 "expecting at least one constraint", return NULL);
641 bset = isl_basic_set_from_constraint(
642 isl_constraint_list_get_constraint(list, 0));
643 for (i = 1; i < n; ++i) {
644 isl_basic_set *bset_i;
646 bset_i = isl_basic_set_from_constraint(
647 isl_constraint_list_get_constraint(list, i));
648 bset = isl_basic_set_intersect(bset, bset_i);
651 return isl_set_from_basic_set(bset);
654 /* Compute the constraints on the outer dimensions enforced by
655 * graft->node and add those constraints to graft->enforced,
656 * in case the upper bound is expressed as a set "upper".
658 * In particular, if l(...) is a lower bound in "lower", and
660 * -a i + f(...) >= 0 or a i <= f(...)
662 * is an upper bound ocnstraint on the current dimension i,
663 * then the for loop enforces the constraint
665 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
667 * We therefore simply take each lower bound in turn, plug it into
668 * the upper bounds and compute the intersection over all lower bounds.
670 * If a lower bound is a rational expression, then
671 * isl_basic_set_preimage_multi_aff will force this rational
672 * expression to have only integer values. However, the loop
673 * itself does not enforce this integrality constraint. We therefore
674 * use the ceil of the lower bounds instead of the lower bounds themselves.
675 * Other constraints will make sure that the for loop is only executed
676 * when each of the lower bounds attains an integral value.
677 * In particular, potentially rational values only occur in
678 * lower_bound if the offset is a (seemingly) rational expression,
679 * but then outer conditions will make sure that this rational expression
680 * only attains integer values.
682 static __isl_give isl_ast_graft *set_enforced_from_set(
683 __isl_take isl_ast_graft *graft,
684 __isl_keep isl_pw_aff_list *lower, int pos, __isl_keep isl_set *upper)
686 isl_space *space;
687 isl_basic_set *enforced;
688 isl_pw_multi_aff *pma;
689 int i, n;
691 if (!graft || !lower)
692 return isl_ast_graft_free(graft);
694 space = isl_set_get_space(upper);
695 enforced = isl_basic_set_universe(isl_space_copy(space));
697 space = isl_space_map_from_set(space);
698 pma = isl_pw_multi_aff_identity(space);
700 n = isl_pw_aff_list_n_pw_aff(lower);
701 for (i = 0; i < n; ++i) {
702 isl_pw_aff *pa;
703 isl_set *enforced_i;
704 isl_basic_set *hull;
705 isl_pw_multi_aff *pma_i;
707 pa = isl_pw_aff_list_get_pw_aff(lower, i);
708 pa = isl_pw_aff_ceil(pa);
709 pma_i = isl_pw_multi_aff_copy(pma);
710 pma_i = isl_pw_multi_aff_set_pw_aff(pma_i, pos, pa);
711 enforced_i = isl_set_copy(upper);
712 enforced_i = isl_set_preimage_pw_multi_aff(enforced_i, pma_i);
713 hull = isl_set_simple_hull(enforced_i);
714 enforced = isl_basic_set_intersect(enforced, hull);
717 isl_pw_multi_aff_free(pma);
719 graft = isl_ast_graft_enforce(graft, enforced);
721 return graft;
724 /* Compute the constraints on the outer dimensions enforced by
725 * graft->node and add those constraints to graft->enforced,
726 * in case the upper bound is expressed as
727 * a list of affine expressions "upper".
729 * The enforced condition is that each lower bound expression is less
730 * than or equal to each upper bound expression.
732 static __isl_give isl_ast_graft *set_enforced_from_list(
733 __isl_take isl_ast_graft *graft,
734 __isl_keep isl_pw_aff_list *lower, __isl_keep isl_pw_aff_list *upper)
736 isl_set *cond;
737 isl_basic_set *enforced;
739 lower = isl_pw_aff_list_copy(lower);
740 upper = isl_pw_aff_list_copy(upper);
741 cond = isl_pw_aff_list_le_set(lower, upper);
742 enforced = isl_set_simple_hull(cond);
743 graft = isl_ast_graft_enforce(graft, enforced);
745 return graft;
748 /* Does "aff" have a negative constant term?
750 static int aff_constant_is_negative(__isl_take isl_set *set,
751 __isl_take isl_aff *aff, void *user)
753 int *neg = user;
754 isl_val *v;
756 v = isl_aff_get_constant_val(aff);
757 *neg = isl_val_is_neg(v);
758 isl_val_free(v);
759 isl_set_free(set);
760 isl_aff_free(aff);
762 return *neg ? 0 : -1;
765 /* Does "pa" have a negative constant term over its entire domain?
767 static int pw_aff_constant_is_negative(__isl_take isl_pw_aff *pa, void *user)
769 int r;
770 int *neg = user;
772 r = isl_pw_aff_foreach_piece(pa, &aff_constant_is_negative, user);
773 isl_pw_aff_free(pa);
775 return *neg ? 0 : -1;
778 /* Does each element in "list" have a negative constant term?
780 * The callback terminates the iteration as soon an element has been
781 * found that does not have a negative constant term.
783 static int list_constant_is_negative(__isl_keep isl_pw_aff_list *list)
785 int neg = 1;
787 if (isl_pw_aff_list_foreach(list,
788 &pw_aff_constant_is_negative, &neg) < 0 && neg)
789 return -1;
791 return neg;
794 /* Add 1 to each of the elements in "list", where each of these elements
795 * is defined over the internal schedule space of "build".
797 static __isl_give isl_pw_aff_list *list_add_one(
798 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
800 int i, n;
801 isl_space *space;
802 isl_aff *aff;
803 isl_pw_aff *one;
805 space = isl_ast_build_get_space(build, 1);
806 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
807 aff = isl_aff_add_constant_si(aff, 1);
808 one = isl_pw_aff_from_aff(aff);
810 n = isl_pw_aff_list_n_pw_aff(list);
811 for (i = 0; i < n; ++i) {
812 isl_pw_aff *pa;
813 pa = isl_pw_aff_list_get_pw_aff(list, i);
814 pa = isl_pw_aff_add(pa, isl_pw_aff_copy(one));
815 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
818 isl_pw_aff_free(one);
820 return list;
823 /* Set the condition part of the for node graft->node in case
824 * the upper bound is represented as a list of piecewise affine expressions.
826 * In particular, set the condition to
828 * iterator <= min(list of upper bounds)
830 * If each of the upper bounds has a negative constant term, then
831 * set the condition to
833 * iterator < min(list of (upper bound + 1)s)
836 static __isl_give isl_ast_graft *set_for_cond_from_list(
837 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *list,
838 __isl_keep isl_ast_build *build)
840 int neg;
841 isl_ast_expr *bound, *iterator, *cond;
842 enum isl_ast_op_type type = isl_ast_op_le;
844 if (!graft || !list)
845 return isl_ast_graft_free(graft);
847 neg = list_constant_is_negative(list);
848 if (neg < 0)
849 return isl_ast_graft_free(graft);
850 list = isl_pw_aff_list_copy(list);
851 if (neg) {
852 list = list_add_one(list, build);
853 type = isl_ast_op_lt;
856 bound = reduce_list(isl_ast_op_min, list, build);
857 iterator = isl_ast_expr_copy(graft->node->u.f.iterator);
858 cond = isl_ast_expr_alloc_binary(type, iterator, bound);
859 graft->node->u.f.cond = cond;
861 isl_pw_aff_list_free(list);
862 if (!graft->node->u.f.cond)
863 return isl_ast_graft_free(graft);
864 return graft;
867 /* Set the condition part of the for node graft->node in case
868 * the upper bound is represented as a set.
870 static __isl_give isl_ast_graft *set_for_cond_from_set(
871 __isl_take isl_ast_graft *graft, __isl_keep isl_set *set,
872 __isl_keep isl_ast_build *build)
874 isl_ast_expr *cond;
876 if (!graft)
877 return NULL;
879 cond = isl_ast_build_expr_from_set(build, isl_set_copy(set));
880 graft->node->u.f.cond = cond;
881 if (!graft->node->u.f.cond)
882 return isl_ast_graft_free(graft);
883 return graft;
886 /* Construct an isl_ast_expr for the increment (i.e., stride) of
887 * the current dimension.
889 static __isl_give isl_ast_expr *for_inc(__isl_keep isl_ast_build *build)
891 int depth;
892 isl_val *v;
893 isl_ctx *ctx;
895 if (!build)
896 return NULL;
897 ctx = isl_ast_build_get_ctx(build);
898 depth = isl_ast_build_get_depth(build);
900 if (!isl_ast_build_has_stride(build, depth))
901 return isl_ast_expr_alloc_int_si(ctx, 1);
903 v = isl_ast_build_get_stride(build, depth);
904 return isl_ast_expr_from_val(v);
907 /* Should we express the loop condition as
909 * iterator <= min(list of upper bounds)
911 * or as a conjunction of constraints?
913 * The first is constructed from a list of upper bounds.
914 * The second is constructed from a set.
916 * If there are no upper bounds in "constraints", then this could mean
917 * that "domain" simply doesn't have an upper bound or that we didn't
918 * pick any upper bound. In the first case, we want to generate the
919 * loop condition as a(n empty) conjunction of constraints
920 * In the second case, we will compute
921 * a single upper bound from "domain" and so we use the list form.
923 * If there are upper bounds in "constraints",
924 * then we use the list form iff the atomic_upper_bound option is set.
926 static int use_upper_bound_list(isl_ctx *ctx, int n_upper,
927 __isl_keep isl_set *domain, int depth)
929 if (n_upper > 0)
930 return isl_options_get_ast_build_atomic_upper_bound(ctx);
931 else
932 return isl_set_dim_has_upper_bound(domain, isl_dim_set, depth);
935 /* Fill in the expressions of the for node in graft->node.
937 * In particular,
938 * - set the initialization part of the loop to the maximum of the lower bounds
939 * - extract the increment from the stride of the current dimension
940 * - construct the for condition either based on a list of upper bounds
941 * or on a set of upper bound constraints.
943 static __isl_give isl_ast_graft *set_for_node_expressions(
944 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *lower,
945 int use_list, __isl_keep isl_pw_aff_list *upper_list,
946 __isl_keep isl_set *upper_set, __isl_keep isl_ast_build *build)
948 isl_ast_node *node;
950 if (!graft)
951 return NULL;
953 build = isl_ast_build_copy(build);
954 build = isl_ast_build_set_enforced(build,
955 isl_ast_graft_get_enforced(graft));
957 node = graft->node;
958 node->u.f.init = reduce_list(isl_ast_op_max, lower, build);
959 node->u.f.inc = for_inc(build);
961 if (use_list)
962 graft = set_for_cond_from_list(graft, upper_list, build);
963 else
964 graft = set_for_cond_from_set(graft, upper_set, build);
966 isl_ast_build_free(build);
968 if (!node->u.f.iterator || !node->u.f.init ||
969 !node->u.f.cond || !node->u.f.inc)
970 return isl_ast_graft_free(graft);
972 return graft;
975 /* Update "graft" based on "bounds" and "domain" for the generic,
976 * non-degenerate, case.
978 * "c_lower" and "c_upper" contain the lower and upper bounds
979 * that the loop node should express.
980 * "domain" is the subset of the intersection of the constraints
981 * for which some code is executed.
983 * There may be zero lower bounds or zero upper bounds in "constraints"
984 * in case the list of constraints was created
985 * based on the atomic option or based on separation with explicit bounds.
986 * In that case, we use "domain" to derive lower and/or upper bounds.
988 * We first compute a list of one or more lower bounds.
990 * Then we decide if we want to express the condition as
992 * iterator <= min(list of upper bounds)
994 * or as a conjunction of constraints.
996 * The set of enforced constraints is then computed either based on
997 * a list of upper bounds or on a set of upper bound constraints.
998 * We do not compute any enforced constraints if we were forced
999 * to compute a lower or upper bound using exact_bound. The domains
1000 * of the resulting expressions may imply some bounds on outer dimensions
1001 * that we do not want to appear in the enforced constraints since
1002 * they are not actually enforced by the corresponding code.
1004 * Finally, we fill in the expressions of the for node.
1006 static __isl_give isl_ast_graft *refine_generic_bounds(
1007 __isl_take isl_ast_graft *graft,
1008 __isl_take isl_constraint_list *c_lower,
1009 __isl_take isl_constraint_list *c_upper,
1010 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1012 int depth;
1013 isl_ctx *ctx;
1014 isl_pw_aff_list *lower;
1015 int use_list;
1016 isl_set *upper_set = NULL;
1017 isl_pw_aff_list *upper_list = NULL;
1018 int n_lower, n_upper;
1020 if (!graft || !c_lower || !c_upper || !build)
1021 goto error;
1023 depth = isl_ast_build_get_depth(build);
1024 ctx = isl_ast_graft_get_ctx(graft);
1026 n_lower = isl_constraint_list_n_constraint(c_lower);
1027 n_upper = isl_constraint_list_n_constraint(c_upper);
1029 use_list = use_upper_bound_list(ctx, n_upper, domain, depth);
1031 lower = lower_bounds(c_lower, depth, domain, build);
1033 if (use_list)
1034 upper_list = upper_bounds(c_upper, depth, domain, build);
1035 else if (n_upper > 0)
1036 upper_set = intersect_constraints(c_upper);
1037 else
1038 upper_set = isl_set_universe(isl_set_get_space(domain));
1040 if (n_lower == 0 || n_upper == 0)
1042 else if (use_list)
1043 graft = set_enforced_from_list(graft, lower, upper_list);
1044 else
1045 graft = set_enforced_from_set(graft, lower, depth, upper_set);
1047 graft = set_for_node_expressions(graft, lower, use_list, upper_list,
1048 upper_set, build);
1050 isl_pw_aff_list_free(lower);
1051 isl_pw_aff_list_free(upper_list);
1052 isl_set_free(upper_set);
1053 isl_constraint_list_free(c_lower);
1054 isl_constraint_list_free(c_upper);
1056 return graft;
1057 error:
1058 isl_constraint_list_free(c_lower);
1059 isl_constraint_list_free(c_upper);
1060 return isl_ast_graft_free(graft);
1063 /* Internal data structure used inside count_constraints to keep
1064 * track of the number of constraints that are independent of dimension "pos",
1065 * the lower bounds in "pos" and the upper bounds in "pos".
1067 struct isl_ast_count_constraints_data {
1068 int pos;
1070 int n_indep;
1071 int n_lower;
1072 int n_upper;
1075 /* Increment data->n_indep, data->lower or data->upper depending
1076 * on whether "c" is independenct of dimensions data->pos,
1077 * a lower bound or an upper bound.
1079 static int count_constraints(__isl_take isl_constraint *c, void *user)
1081 struct isl_ast_count_constraints_data *data = user;
1083 if (isl_constraint_is_lower_bound(c, isl_dim_set, data->pos))
1084 data->n_lower++;
1085 else if (isl_constraint_is_upper_bound(c, isl_dim_set, data->pos))
1086 data->n_upper++;
1087 else
1088 data->n_indep++;
1090 isl_constraint_free(c);
1092 return 0;
1095 /* Update "graft" based on "bounds" and "domain" for the generic,
1096 * non-degenerate, case.
1098 * "list" respresent the list of bounds that need to be encoded by
1099 * the for loop (or a guard around the for loop).
1100 * "domain" is the subset of the intersection of the constraints
1101 * for which some code is executed.
1102 * "build" is the build in which graft->node was created.
1104 * We separate lower bounds, upper bounds and constraints that
1105 * are independent of the loop iterator.
1107 * The actual for loop bounds are generated in refine_generic_bounds.
1108 * If there are any constraints that are independent of the loop iterator,
1109 * we need to put a guard around the for loop (which may get hoisted up
1110 * to higher levels) and we call refine_generic_bounds in a build
1111 * where this guard is enforced.
1113 static __isl_give isl_ast_graft *refine_generic_split(
1114 __isl_take isl_ast_graft *graft, __isl_take isl_constraint_list *list,
1115 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1117 isl_ast_build *for_build;
1118 isl_set *guard;
1119 struct isl_ast_count_constraints_data data;
1120 isl_constraint_list *lower;
1121 isl_constraint_list *upper;
1123 if (!list)
1124 return isl_ast_graft_free(graft);
1126 data.pos = isl_ast_build_get_depth(build);
1128 list = isl_constraint_list_sort(list, &cmp_constraint, &data.pos);
1129 if (!list)
1130 return isl_ast_graft_free(graft);
1132 data.n_indep = data.n_lower = data.n_upper = 0;
1133 if (isl_constraint_list_foreach(list, &count_constraints, &data) < 0) {
1134 isl_constraint_list_free(list);
1135 return isl_ast_graft_free(graft);
1138 lower = isl_constraint_list_copy(list);
1139 lower = isl_constraint_list_drop(lower, 0, data.n_indep);
1140 upper = isl_constraint_list_copy(lower);
1141 lower = isl_constraint_list_drop(lower, data.n_lower, data.n_upper);
1142 upper = isl_constraint_list_drop(upper, 0, data.n_lower);
1144 if (data.n_indep == 0) {
1145 isl_constraint_list_free(list);
1146 return refine_generic_bounds(graft, lower, upper,
1147 domain, build);
1150 list = isl_constraint_list_drop(list, data.n_indep,
1151 data.n_lower + data.n_upper);
1152 guard = intersect_constraints(list);
1153 isl_constraint_list_free(list);
1155 for_build = isl_ast_build_copy(build);
1156 for_build = isl_ast_build_restrict_pending(for_build,
1157 isl_set_copy(guard));
1158 graft = refine_generic_bounds(graft, lower, upper, domain, for_build);
1159 isl_ast_build_free(for_build);
1161 graft = isl_ast_graft_add_guard(graft, guard, build);
1163 return graft;
1166 /* Add the guard implied by the current stride constraint (if any),
1167 * but not (necessarily) enforced by the generated AST to "graft".
1169 static __isl_give isl_ast_graft *add_stride_guard(
1170 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
1172 int depth;
1173 isl_set *dom;
1175 depth = isl_ast_build_get_depth(build);
1176 if (!isl_ast_build_has_stride(build, depth))
1177 return graft;
1179 dom = isl_ast_build_get_stride_constraint(build);
1180 dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
1181 dom = isl_ast_build_compute_gist(build, dom);
1183 graft = isl_ast_graft_add_guard(graft, dom, build);
1185 return graft;
1188 /* Update "graft" based on "bounds" and "domain" for the generic,
1189 * non-degenerate, case.
1191 * "bounds" respresent the bounds that need to be encoded by
1192 * the for loop (or a guard around the for loop).
1193 * "domain" is the subset of "bounds" for which some code is executed.
1194 * "build" is the build in which graft->node was created.
1196 * We break up "bounds" into a list of constraints and continue with
1197 * refine_generic_split.
1199 static __isl_give isl_ast_graft *refine_generic(
1200 __isl_take isl_ast_graft *graft,
1201 __isl_keep isl_basic_set *bounds, __isl_keep isl_set *domain,
1202 __isl_keep isl_ast_build *build)
1204 isl_constraint_list *list;
1206 if (!build || !graft)
1207 return isl_ast_graft_free(graft);
1209 bounds = isl_basic_set_copy(bounds);
1210 bounds = isl_ast_build_compute_gist_basic_set(build, bounds);
1211 list = isl_basic_set_get_constraint_list(bounds);
1212 isl_basic_set_free(bounds);
1214 graft = refine_generic_split(graft, list, domain, build);
1215 graft = add_stride_guard(graft, build);
1217 return graft;
1220 /* Create a for node for the current level.
1222 * Mark the for node degenerate if "degenerate" is set.
1224 static __isl_give isl_ast_node *create_for(__isl_keep isl_ast_build *build,
1225 int degenerate)
1227 int depth;
1228 isl_id *id;
1229 isl_ast_node *node;
1231 if (!build)
1232 return NULL;
1234 depth = isl_ast_build_get_depth(build);
1235 id = isl_ast_build_get_iterator_id(build, depth);
1236 node = isl_ast_node_alloc_for(id);
1237 if (degenerate)
1238 node = isl_ast_node_for_mark_degenerate(node);
1240 return node;
1243 /* Create an AST node for the current dimension based on
1244 * the schedule domain "bounds" and return the node encapsulated
1245 * in an isl_ast_graft.
1247 * "executed" is the current inverse schedule, taking into account
1248 * the bounds in "bounds"
1249 * "domain" is the domain of "executed", with inner dimensions projected out.
1250 * It may be a strict subset of "bounds" in case "bounds" was created
1251 * based on the atomic option or based on separation with explicit bounds.
1253 * "domain" may satisfy additional equalities that result
1254 * from intersecting "executed" with "bounds" in add_node.
1255 * It may also satisfy some global constraints that were dropped out because
1256 * we performed separation with explicit bounds.
1257 * The very first step is then to copy these constraints to "bounds".
1259 * Since we may be calling before_each_for and after_each_for
1260 * callbacks, we record the current inverse schedule in the build.
1262 * We consider three builds,
1263 * "build" is the one in which the current level is created,
1264 * "body_build" is the build in which the next level is created,
1265 * "sub_build" is essentially the same as "body_build", except that
1266 * the depth has not been increased yet.
1268 * "build" already contains information (in strides and offsets)
1269 * about the strides at the current level, but this information is not
1270 * reflected in the build->domain.
1271 * We first add this information and the "bounds" to the sub_build->domain.
1272 * isl_ast_build_set_loop_bounds checks whether the current dimension attains
1273 * only a single value and whether this single value can be represented using
1274 * a single affine expression.
1275 * In the first case, the current level is considered "degenerate".
1276 * In the second, sub-case, the current level is considered "eliminated".
1277 * Eliminated levels don't need to be reflected in the AST since we can
1278 * simply plug in the affine expression. For degenerate, but non-eliminated,
1279 * levels, we do introduce a for node, but mark is as degenerate so that
1280 * it can be printed as an assignment of the single value to the loop
1281 * "iterator".
1283 * If the current level is eliminated, we explicitly plug in the value
1284 * for the current level found by isl_ast_build_set_loop_bounds in the
1285 * inverse schedule. This ensures that if we are working on a slice
1286 * of the domain based on information available in the inverse schedule
1287 * and the build domain, that then this information is also reflected
1288 * in the inverse schedule. This operation also eliminates the current
1289 * dimension from the inverse schedule making sure no inner dimensions depend
1290 * on the current dimension. Otherwise, we create a for node, marking
1291 * it degenerate if appropriate. The initial for node is still incomplete
1292 * and will be completed in either refine_degenerate or refine_generic.
1294 * We then generate a sequence of grafts for the next level,
1295 * create a surrounding graft for the current level and insert
1296 * the for node we created (if the current level is not eliminated).
1298 * Finally, we set the bounds of the for loop and insert guards
1299 * (either in the AST or in the graft) in one of
1300 * refine_eliminated, refine_degenerate or refine_generic.
1302 static __isl_give isl_ast_graft *create_node_scaled(
1303 __isl_take isl_union_map *executed,
1304 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1305 __isl_take isl_ast_build *build)
1307 int depth;
1308 int degenerate, eliminated;
1309 isl_basic_set *hull;
1310 isl_ast_node *node = NULL;
1311 isl_ast_graft *graft;
1312 isl_ast_graft_list *children;
1313 isl_ast_build *sub_build;
1314 isl_ast_build *body_build;
1316 domain = isl_ast_build_eliminate_divs(build, domain);
1317 domain = isl_set_detect_equalities(domain);
1318 hull = isl_set_unshifted_simple_hull(isl_set_copy(domain));
1319 bounds = isl_basic_set_intersect(bounds, hull);
1320 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
1322 depth = isl_ast_build_get_depth(build);
1323 sub_build = isl_ast_build_copy(build);
1324 sub_build = isl_ast_build_include_stride(sub_build);
1325 sub_build = isl_ast_build_set_loop_bounds(sub_build,
1326 isl_basic_set_copy(bounds));
1327 degenerate = isl_ast_build_has_value(sub_build);
1328 eliminated = isl_ast_build_has_affine_value(sub_build, depth);
1329 if (degenerate < 0 || eliminated < 0)
1330 executed = isl_union_map_free(executed);
1331 if (eliminated)
1332 executed = plug_in_values(executed, sub_build);
1333 else
1334 node = create_for(build, degenerate);
1336 body_build = isl_ast_build_copy(sub_build);
1337 body_build = isl_ast_build_increase_depth(body_build);
1338 if (!eliminated)
1339 node = before_each_for(node, body_build);
1340 children = generate_next_level(executed,
1341 isl_ast_build_copy(body_build));
1343 graft = isl_ast_graft_alloc_level(children, build, sub_build);
1344 if (!eliminated)
1345 graft = isl_ast_graft_insert_for(graft, node);
1346 if (eliminated)
1347 graft = refine_eliminated(graft, bounds, build);
1348 else if (degenerate)
1349 graft = refine_degenerate(graft, bounds, build, sub_build);
1350 else
1351 graft = refine_generic(graft, bounds, domain, build);
1352 if (!eliminated)
1353 graft = after_each_for(graft, body_build);
1355 isl_ast_build_free(body_build);
1356 isl_ast_build_free(sub_build);
1357 isl_ast_build_free(build);
1358 isl_basic_set_free(bounds);
1359 isl_set_free(domain);
1361 return graft;
1364 /* Internal data structure for checking if all constraints involving
1365 * the input dimension "depth" are such that the other coefficients
1366 * are multiples of "m", reducing "m" if they are not.
1367 * If "m" is reduced all the way down to "1", then the check has failed
1368 * and we break out of the iteration.
1370 struct isl_check_scaled_data {
1371 int depth;
1372 isl_val *m;
1375 /* If constraint "c" involves the input dimension data->depth,
1376 * then make sure that all the other coefficients are multiples of data->m,
1377 * reducing data->m if needed.
1378 * Break out of the iteration if data->m has become equal to "1".
1380 static int constraint_check_scaled(__isl_take isl_constraint *c, void *user)
1382 struct isl_check_scaled_data *data = user;
1383 int i, j, n;
1384 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_out,
1385 isl_dim_div };
1387 if (!isl_constraint_involves_dims(c, isl_dim_in, data->depth, 1)) {
1388 isl_constraint_free(c);
1389 return 0;
1392 for (i = 0; i < 4; ++i) {
1393 n = isl_constraint_dim(c, t[i]);
1394 for (j = 0; j < n; ++j) {
1395 isl_val *d;
1397 if (t[i] == isl_dim_in && j == data->depth)
1398 continue;
1399 if (!isl_constraint_involves_dims(c, t[i], j, 1))
1400 continue;
1401 d = isl_constraint_get_coefficient_val(c, t[i], j);
1402 data->m = isl_val_gcd(data->m, d);
1403 if (isl_val_is_one(data->m))
1404 break;
1406 if (j < n)
1407 break;
1410 isl_constraint_free(c);
1412 return i < 4 ? -1 : 0;
1415 /* For each constraint of "bmap" that involves the input dimension data->depth,
1416 * make sure that all the other coefficients are multiples of data->m,
1417 * reducing data->m if needed.
1418 * Break out of the iteration if data->m has become equal to "1".
1420 static int basic_map_check_scaled(__isl_take isl_basic_map *bmap, void *user)
1422 int r;
1424 r = isl_basic_map_foreach_constraint(bmap,
1425 &constraint_check_scaled, user);
1426 isl_basic_map_free(bmap);
1428 return r;
1431 /* For each constraint of "map" that involves the input dimension data->depth,
1432 * make sure that all the other coefficients are multiples of data->m,
1433 * reducing data->m if needed.
1434 * Break out of the iteration if data->m has become equal to "1".
1436 static int map_check_scaled(__isl_take isl_map *map, void *user)
1438 int r;
1440 r = isl_map_foreach_basic_map(map, &basic_map_check_scaled, user);
1441 isl_map_free(map);
1443 return r;
1446 /* Create an AST node for the current dimension based on
1447 * the schedule domain "bounds" and return the node encapsulated
1448 * in an isl_ast_graft.
1450 * "executed" is the current inverse schedule, taking into account
1451 * the bounds in "bounds"
1452 * "domain" is the domain of "executed", with inner dimensions projected out.
1455 * Before moving on to the actual AST node construction in create_node_scaled,
1456 * we first check if the current dimension is strided and if we can scale
1457 * down this stride. Note that we only do this if the ast_build_scale_strides
1458 * option is set.
1460 * In particular, let the current dimension take on values
1462 * f + s a
1464 * with a an integer. We check if we can find an integer m that (obviously)
1465 * divides both f and s.
1467 * If so, we check if the current dimension only appears in constraints
1468 * where the coefficients of the other variables are multiples of m.
1469 * We perform this extra check to avoid the risk of introducing
1470 * divisions by scaling down the current dimension.
1472 * If so, we scale the current dimension down by a factor of m.
1473 * That is, we plug in
1475 * i = m i' (1)
1477 * Note that in principle we could always scale down strided loops
1478 * by plugging in
1480 * i = f + s i'
1482 * but this may result in i' taking on larger values than the original i,
1483 * due to the shift by "f".
1484 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1486 static __isl_give isl_ast_graft *create_node(__isl_take isl_union_map *executed,
1487 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1488 __isl_take isl_ast_build *build)
1490 struct isl_check_scaled_data data;
1491 isl_ctx *ctx;
1492 isl_aff *offset;
1493 isl_val *d;
1495 ctx = isl_ast_build_get_ctx(build);
1496 if (!isl_options_get_ast_build_scale_strides(ctx))
1497 return create_node_scaled(executed, bounds, domain, build);
1499 data.depth = isl_ast_build_get_depth(build);
1500 if (!isl_ast_build_has_stride(build, data.depth))
1501 return create_node_scaled(executed, bounds, domain, build);
1503 offset = isl_ast_build_get_offset(build, data.depth);
1504 data.m = isl_ast_build_get_stride(build, data.depth);
1505 if (!data.m)
1506 offset = isl_aff_free(offset);
1507 offset = isl_aff_scale_down_val(offset, isl_val_copy(data.m));
1508 d = isl_aff_get_denominator_val(offset);
1509 if (!d)
1510 executed = isl_union_map_free(executed);
1512 if (executed && isl_val_is_divisible_by(data.m, d))
1513 data.m = isl_val_div(data.m, d);
1514 else {
1515 data.m = isl_val_set_si(data.m, 1);
1516 isl_val_free(d);
1519 if (!isl_val_is_one(data.m)) {
1520 if (isl_union_map_foreach_map(executed, &map_check_scaled,
1521 &data) < 0 &&
1522 !isl_val_is_one(data.m))
1523 executed = isl_union_map_free(executed);
1526 if (!isl_val_is_one(data.m)) {
1527 isl_space *space;
1528 isl_multi_aff *ma;
1529 isl_aff *aff;
1530 isl_map *map;
1531 isl_union_map *umap;
1533 space = isl_ast_build_get_space(build, 1);
1534 space = isl_space_map_from_set(space);
1535 ma = isl_multi_aff_identity(space);
1536 aff = isl_multi_aff_get_aff(ma, data.depth);
1537 aff = isl_aff_scale_val(aff, isl_val_copy(data.m));
1538 ma = isl_multi_aff_set_aff(ma, data.depth, aff);
1540 bounds = isl_basic_set_preimage_multi_aff(bounds,
1541 isl_multi_aff_copy(ma));
1542 domain = isl_set_preimage_multi_aff(domain,
1543 isl_multi_aff_copy(ma));
1544 map = isl_map_reverse(isl_map_from_multi_aff(ma));
1545 umap = isl_union_map_from_map(map);
1546 executed = isl_union_map_apply_domain(executed,
1547 isl_union_map_copy(umap));
1548 build = isl_ast_build_scale_down(build, isl_val_copy(data.m),
1549 umap);
1551 isl_aff_free(offset);
1552 isl_val_free(data.m);
1554 return create_node_scaled(executed, bounds, domain, build);
1557 /* Add the basic set to the list that "user" points to.
1559 static int collect_basic_set(__isl_take isl_basic_set *bset, void *user)
1561 isl_basic_set_list **list = user;
1563 *list = isl_basic_set_list_add(*list, bset);
1565 return 0;
1568 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1570 static __isl_give isl_basic_set_list *isl_basic_set_list_from_set(
1571 __isl_take isl_set *set)
1573 int n;
1574 isl_ctx *ctx;
1575 isl_basic_set_list *list;
1577 if (!set)
1578 return NULL;
1580 ctx = isl_set_get_ctx(set);
1582 n = isl_set_n_basic_set(set);
1583 list = isl_basic_set_list_alloc(ctx, n);
1584 if (isl_set_foreach_basic_set(set, &collect_basic_set, &list) < 0)
1585 list = isl_basic_set_list_free(list);
1587 isl_set_free(set);
1588 return list;
1591 /* Generate code for the schedule domain "bounds"
1592 * and add the result to "list".
1594 * We mainly detect strides here and check if the bounds do not
1595 * conflict with the current build domain
1596 * and then pass over control to create_node.
1598 * "bounds" reflects the bounds on the current dimension and possibly
1599 * some extra conditions on outer dimensions.
1600 * It does not, however, include any divs involving the current dimension,
1601 * so it does not capture any stride constraints.
1602 * We therefore need to compute that part of the schedule domain that
1603 * intersects with "bounds" and derive the strides from the result.
1605 static __isl_give isl_ast_graft_list *add_node(
1606 __isl_take isl_ast_graft_list *list, __isl_take isl_union_map *executed,
1607 __isl_take isl_basic_set *bounds, __isl_take isl_ast_build *build)
1609 isl_ast_graft *graft;
1610 isl_set *domain = NULL;
1611 isl_union_set *uset;
1612 int empty, disjoint;
1614 uset = isl_union_set_from_basic_set(isl_basic_set_copy(bounds));
1615 executed = isl_union_map_intersect_domain(executed, uset);
1616 empty = isl_union_map_is_empty(executed);
1617 if (empty < 0)
1618 goto error;
1619 if (empty)
1620 goto done;
1622 uset = isl_union_map_domain(isl_union_map_copy(executed));
1623 domain = isl_set_from_union_set(uset);
1624 domain = isl_ast_build_specialize(build, domain);
1626 domain = isl_set_compute_divs(domain);
1627 domain = isl_ast_build_eliminate_inner(build, domain);
1628 disjoint = isl_set_is_disjoint(domain, build->domain);
1629 if (disjoint < 0)
1630 goto error;
1631 if (disjoint)
1632 goto done;
1634 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
1636 graft = create_node(executed, bounds, domain,
1637 isl_ast_build_copy(build));
1638 list = isl_ast_graft_list_add(list, graft);
1639 isl_ast_build_free(build);
1640 return list;
1641 error:
1642 list = isl_ast_graft_list_free(list);
1643 done:
1644 isl_set_free(domain);
1645 isl_basic_set_free(bounds);
1646 isl_union_map_free(executed);
1647 isl_ast_build_free(build);
1648 return list;
1651 /* Does any element of i follow or coincide with any element of j
1652 * at the current depth for equal values of the outer dimensions?
1654 static int domain_follows_at_depth(__isl_keep isl_basic_set *i,
1655 __isl_keep isl_basic_set *j, void *user)
1657 int depth = *(int *) user;
1658 isl_basic_map *test;
1659 int empty;
1660 int l;
1662 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1663 isl_basic_set_copy(j));
1664 for (l = 0; l < depth; ++l)
1665 test = isl_basic_map_equate(test, isl_dim_in, l,
1666 isl_dim_out, l);
1667 test = isl_basic_map_order_ge(test, isl_dim_in, depth,
1668 isl_dim_out, depth);
1669 empty = isl_basic_map_is_empty(test);
1670 isl_basic_map_free(test);
1672 return empty < 0 ? -1 : !empty;
1675 /* Split up each element of "list" into a part that is related to "bset"
1676 * according to "gt" and a part that is not.
1677 * Return a list that consist of "bset" and all the pieces.
1679 static __isl_give isl_basic_set_list *add_split_on(
1680 __isl_take isl_basic_set_list *list, __isl_take isl_basic_set *bset,
1681 __isl_keep isl_basic_map *gt)
1683 int i, n;
1684 isl_basic_set_list *res;
1686 if (!list)
1687 bset = isl_basic_set_free(bset);
1689 gt = isl_basic_map_copy(gt);
1690 gt = isl_basic_map_intersect_domain(gt, isl_basic_set_copy(bset));
1691 n = isl_basic_set_list_n_basic_set(list);
1692 res = isl_basic_set_list_from_basic_set(bset);
1693 for (i = 0; res && i < n; ++i) {
1694 isl_basic_set *bset;
1695 isl_set *set1, *set2;
1696 isl_basic_map *bmap;
1697 int empty;
1699 bset = isl_basic_set_list_get_basic_set(list, i);
1700 bmap = isl_basic_map_copy(gt);
1701 bmap = isl_basic_map_intersect_range(bmap, bset);
1702 bset = isl_basic_map_range(bmap);
1703 empty = isl_basic_set_is_empty(bset);
1704 if (empty < 0)
1705 res = isl_basic_set_list_free(res);
1706 if (empty) {
1707 isl_basic_set_free(bset);
1708 bset = isl_basic_set_list_get_basic_set(list, i);
1709 res = isl_basic_set_list_add(res, bset);
1710 continue;
1713 res = isl_basic_set_list_add(res, isl_basic_set_copy(bset));
1714 set1 = isl_set_from_basic_set(bset);
1715 bset = isl_basic_set_list_get_basic_set(list, i);
1716 set2 = isl_set_from_basic_set(bset);
1717 set1 = isl_set_subtract(set2, set1);
1718 set1 = isl_set_make_disjoint(set1);
1720 res = isl_basic_set_list_concat(res,
1721 isl_basic_set_list_from_set(set1));
1723 isl_basic_map_free(gt);
1724 isl_basic_set_list_free(list);
1725 return res;
1728 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1729 __isl_keep isl_basic_set_list *domain_list,
1730 __isl_keep isl_union_map *executed,
1731 __isl_keep isl_ast_build *build);
1733 /* Internal data structure for add_nodes.
1735 * "executed" and "build" are extra arguments to be passed to add_node.
1736 * "list" collects the results.
1738 struct isl_add_nodes_data {
1739 isl_union_map *executed;
1740 isl_ast_build *build;
1742 isl_ast_graft_list *list;
1745 /* Generate code for the schedule domains in "scc"
1746 * and add the results to "list".
1748 * The domains in "scc" form a strongly connected component in the ordering.
1749 * If the number of domains in "scc" is larger than 1, then this means
1750 * that we cannot determine a valid ordering for the domains in the component.
1751 * This should be fairly rare because the individual domains
1752 * have been made disjoint first.
1753 * The problem is that the domains may be integrally disjoint but not
1754 * rationally disjoint. For example, we may have domains
1756 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1758 * These two domains have an empty intersection, but their rational
1759 * relaxations do intersect. It is impossible to order these domains
1760 * in the second dimension because the first should be ordered before
1761 * the second for outer dimension equal to 0, while it should be ordered
1762 * after for outer dimension equal to 1.
1764 * This may happen in particular in case of unrolling since the domain
1765 * of each slice is replaced by its simple hull.
1767 * For each basic set i in "scc" and for each of the following basic sets j,
1768 * we split off that part of the basic set i that shares the outer dimensions
1769 * with j and lies before j in the current dimension.
1770 * We collect all the pieces in a new list that replaces "scc".
1772 * While the elements in "scc" should be disjoint, we double-check
1773 * this property to avoid running into an infinite recursion in case
1774 * they intersect due to some internal error.
1776 static int add_nodes(__isl_take isl_basic_set_list *scc, void *user)
1778 struct isl_add_nodes_data *data = user;
1779 int i, n, depth;
1780 isl_basic_set *bset, *first;
1781 isl_basic_set_list *list;
1782 isl_space *space;
1783 isl_basic_map *gt;
1785 n = isl_basic_set_list_n_basic_set(scc);
1786 bset = isl_basic_set_list_get_basic_set(scc, 0);
1787 if (n == 1) {
1788 isl_basic_set_list_free(scc);
1789 data->list = add_node(data->list,
1790 isl_union_map_copy(data->executed), bset,
1791 isl_ast_build_copy(data->build));
1792 return data->list ? 0 : -1;
1795 depth = isl_ast_build_get_depth(data->build);
1796 space = isl_basic_set_get_space(bset);
1797 space = isl_space_map_from_set(space);
1798 gt = isl_basic_map_universe(space);
1799 for (i = 0; i < depth; ++i)
1800 gt = isl_basic_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
1801 gt = isl_basic_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
1803 first = isl_basic_set_copy(bset);
1804 list = isl_basic_set_list_from_basic_set(bset);
1805 for (i = 1; i < n; ++i) {
1806 int disjoint;
1808 bset = isl_basic_set_list_get_basic_set(scc, i);
1810 disjoint = isl_basic_set_is_disjoint(bset, first);
1811 if (disjoint < 0)
1812 list = isl_basic_set_list_free(list);
1813 else if (!disjoint)
1814 isl_die(isl_basic_set_list_get_ctx(scc),
1815 isl_error_internal,
1816 "basic sets in scc are assumed to be disjoint",
1817 list = isl_basic_set_list_free(list));
1819 list = add_split_on(list, bset, gt);
1821 isl_basic_set_free(first);
1822 isl_basic_map_free(gt);
1823 isl_basic_set_list_free(scc);
1824 scc = list;
1825 data->list = isl_ast_graft_list_concat(data->list,
1826 generate_sorted_domains(scc, data->executed, data->build));
1827 isl_basic_set_list_free(scc);
1829 return data->list ? 0 : -1;
1832 /* Sort the domains in "domain_list" according to the execution order
1833 * at the current depth (for equal values of the outer dimensions),
1834 * generate code for each of them, collecting the results in a list.
1835 * If no code is generated (because the intersection of the inverse schedule
1836 * with the domains turns out to be empty), then an empty list is returned.
1838 * The caller is responsible for ensuring that the basic sets in "domain_list"
1839 * are pair-wise disjoint. It can, however, in principle happen that
1840 * two basic sets should be ordered one way for one value of the outer
1841 * dimensions and the other way for some other value of the outer dimensions.
1842 * We therefore play safe and look for strongly connected components.
1843 * The function add_nodes takes care of handling non-trivial components.
1845 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1846 __isl_keep isl_basic_set_list *domain_list,
1847 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
1849 isl_ctx *ctx;
1850 struct isl_add_nodes_data data;
1851 int depth;
1852 int n;
1854 if (!domain_list)
1855 return NULL;
1857 ctx = isl_basic_set_list_get_ctx(domain_list);
1858 n = isl_basic_set_list_n_basic_set(domain_list);
1859 data.list = isl_ast_graft_list_alloc(ctx, n);
1860 if (n == 0)
1861 return data.list;
1862 if (n == 1)
1863 return add_node(data.list, isl_union_map_copy(executed),
1864 isl_basic_set_list_get_basic_set(domain_list, 0),
1865 isl_ast_build_copy(build));
1867 depth = isl_ast_build_get_depth(build);
1868 data.executed = executed;
1869 data.build = build;
1870 if (isl_basic_set_list_foreach_scc(domain_list,
1871 &domain_follows_at_depth, &depth,
1872 &add_nodes, &data) < 0)
1873 data.list = isl_ast_graft_list_free(data.list);
1875 return data.list;
1878 /* Do i and j share any values for the outer dimensions?
1880 static int shared_outer(__isl_keep isl_basic_set *i,
1881 __isl_keep isl_basic_set *j, void *user)
1883 int depth = *(int *) user;
1884 isl_basic_map *test;
1885 int empty;
1886 int l;
1888 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1889 isl_basic_set_copy(j));
1890 for (l = 0; l < depth; ++l)
1891 test = isl_basic_map_equate(test, isl_dim_in, l,
1892 isl_dim_out, l);
1893 empty = isl_basic_map_is_empty(test);
1894 isl_basic_map_free(test);
1896 return empty < 0 ? -1 : !empty;
1899 /* Internal data structure for generate_sorted_domains_wrap.
1901 * "n" is the total number of basic sets
1902 * "executed" and "build" are extra arguments to be passed
1903 * to generate_sorted_domains.
1905 * "single" is set to 1 by generate_sorted_domains_wrap if there
1906 * is only a single component.
1907 * "list" collects the results.
1909 struct isl_ast_generate_parallel_domains_data {
1910 int n;
1911 isl_union_map *executed;
1912 isl_ast_build *build;
1914 int single;
1915 isl_ast_graft_list *list;
1918 /* Call generate_sorted_domains on "scc", fuse the result into a list
1919 * with either zero or one graft and collect the these single element
1920 * lists into data->list.
1922 * If there is only one component, i.e., if the number of basic sets
1923 * in the current component is equal to the total number of basic sets,
1924 * then data->single is set to 1 and the result of generate_sorted_domains
1925 * is not fused.
1927 static int generate_sorted_domains_wrap(__isl_take isl_basic_set_list *scc,
1928 void *user)
1930 struct isl_ast_generate_parallel_domains_data *data = user;
1931 isl_ast_graft_list *list;
1933 list = generate_sorted_domains(scc, data->executed, data->build);
1934 data->single = isl_basic_set_list_n_basic_set(scc) == data->n;
1935 if (!data->single)
1936 list = isl_ast_graft_list_fuse(list, data->build);
1937 if (!data->list)
1938 data->list = list;
1939 else
1940 data->list = isl_ast_graft_list_concat(data->list, list);
1942 isl_basic_set_list_free(scc);
1943 if (!data->list)
1944 return -1;
1946 return 0;
1949 /* Look for any (weakly connected) components in the "domain_list"
1950 * of domains that share some values of the outer dimensions.
1951 * That is, domains in different components do not share any values
1952 * of the outer dimensions. This means that these components
1953 * can be freely reordered.
1954 * Within each of the components, we sort the domains according
1955 * to the execution order at the current depth.
1957 * If there is more than one component, then generate_sorted_domains_wrap
1958 * fuses the result of each call to generate_sorted_domains
1959 * into a list with either zero or one graft and collects these (at most)
1960 * single element lists into a bigger list. This means that the elements of the
1961 * final list can be freely reordered. In particular, we sort them
1962 * according to an arbitrary but fixed ordering to ease merging of
1963 * graft lists from different components.
1965 static __isl_give isl_ast_graft_list *generate_parallel_domains(
1966 __isl_keep isl_basic_set_list *domain_list,
1967 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
1969 int depth;
1970 struct isl_ast_generate_parallel_domains_data data;
1972 if (!domain_list)
1973 return NULL;
1975 data.n = isl_basic_set_list_n_basic_set(domain_list);
1976 if (data.n <= 1)
1977 return generate_sorted_domains(domain_list, executed, build);
1979 depth = isl_ast_build_get_depth(build);
1980 data.list = NULL;
1981 data.executed = executed;
1982 data.build = build;
1983 data.single = 0;
1984 if (isl_basic_set_list_foreach_scc(domain_list, &shared_outer, &depth,
1985 &generate_sorted_domains_wrap,
1986 &data) < 0)
1987 data.list = isl_ast_graft_list_free(data.list);
1989 if (!data.single)
1990 data.list = isl_ast_graft_list_sort_guard(data.list);
1992 return data.list;
1995 /* Internal data for separate_domain.
1997 * "explicit" is set if we only want to use explicit bounds.
1999 * "domain" collects the separated domains.
2001 struct isl_separate_domain_data {
2002 isl_ast_build *build;
2003 int explicit;
2004 isl_set *domain;
2007 /* Extract implicit bounds on the current dimension for the executed "map".
2009 * The domain of "map" may involve inner dimensions, so we
2010 * need to eliminate them.
2012 static __isl_give isl_set *implicit_bounds(__isl_take isl_map *map,
2013 __isl_keep isl_ast_build *build)
2015 isl_set *domain;
2017 domain = isl_map_domain(map);
2018 domain = isl_ast_build_eliminate(build, domain);
2020 return domain;
2023 /* Extract explicit bounds on the current dimension for the executed "map".
2025 * Rather than eliminating the inner dimensions as in implicit_bounds,
2026 * we simply drop any constraints involving those inner dimensions.
2027 * The idea is that most bounds that are implied by constraints on the
2028 * inner dimensions will be enforced by for loops and not by explicit guards.
2029 * There is then no need to separate along those bounds.
2031 static __isl_give isl_set *explicit_bounds(__isl_take isl_map *map,
2032 __isl_keep isl_ast_build *build)
2034 isl_set *domain;
2035 int depth, dim;
2037 dim = isl_map_dim(map, isl_dim_out);
2038 map = isl_map_drop_constraints_involving_dims(map, isl_dim_out, 0, dim);
2040 domain = isl_map_domain(map);
2041 depth = isl_ast_build_get_depth(build);
2042 dim = isl_set_dim(domain, isl_dim_set);
2043 domain = isl_set_detect_equalities(domain);
2044 domain = isl_set_drop_constraints_involving_dims(domain,
2045 isl_dim_set, depth + 1, dim - (depth + 1));
2046 domain = isl_set_remove_divs_involving_dims(domain,
2047 isl_dim_set, depth, 1);
2048 domain = isl_set_remove_unknown_divs(domain);
2050 return domain;
2053 /* Split data->domain into pieces that intersect with the range of "map"
2054 * and pieces that do not intersect with the range of "map"
2055 * and then add that part of the range of "map" that does not intersect
2056 * with data->domain.
2058 static int separate_domain(__isl_take isl_map *map, void *user)
2060 struct isl_separate_domain_data *data = user;
2061 isl_set *domain;
2062 isl_set *d1, *d2;
2064 if (data->explicit)
2065 domain = explicit_bounds(map, data->build);
2066 else
2067 domain = implicit_bounds(map, data->build);
2069 domain = isl_set_coalesce(domain);
2070 domain = isl_set_make_disjoint(domain);
2071 d1 = isl_set_subtract(isl_set_copy(domain), isl_set_copy(data->domain));
2072 d2 = isl_set_subtract(isl_set_copy(data->domain), isl_set_copy(domain));
2073 data->domain = isl_set_intersect(data->domain, domain);
2074 data->domain = isl_set_union(data->domain, d1);
2075 data->domain = isl_set_union(data->domain, d2);
2077 return 0;
2080 /* Separate the schedule domains of "executed".
2082 * That is, break up the domain of "executed" into basic sets,
2083 * such that for each basic set S, every element in S is associated with
2084 * the same domain spaces.
2086 * "space" is the (single) domain space of "executed".
2088 static __isl_give isl_set *separate_schedule_domains(
2089 __isl_take isl_space *space, __isl_take isl_union_map *executed,
2090 __isl_keep isl_ast_build *build)
2092 struct isl_separate_domain_data data = { build };
2093 isl_ctx *ctx;
2095 ctx = isl_ast_build_get_ctx(build);
2096 data.explicit = isl_options_get_ast_build_separation_bounds(ctx) ==
2097 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT;
2098 data.domain = isl_set_empty(space);
2099 if (isl_union_map_foreach_map(executed, &separate_domain, &data) < 0)
2100 data.domain = isl_set_free(data.domain);
2102 isl_union_map_free(executed);
2103 return data.domain;
2106 /* Temporary data used during the search for a lower bound for unrolling.
2108 * "domain" is the original set for which to find a lower bound
2109 * "depth" is the dimension for which to find a lower boudn
2111 * "lower" is the best lower bound found so far. It is NULL if we have not
2112 * found any yet.
2113 * "n" is the corresponding size. If lower is NULL, then the value of n
2114 * is undefined.
2116 struct isl_find_unroll_data {
2117 isl_set *domain;
2118 int depth;
2120 isl_aff *lower;
2121 int *n;
2124 /* Check if we can use "c" as a lower bound and if it is better than
2125 * any previously found lower bound.
2127 * If "c" does not involve the dimension at the current depth,
2128 * then we cannot use it.
2129 * Otherwise, let "c" be of the form
2131 * i >= f(j)/a
2133 * We compute the maximal value of
2135 * -ceil(f(j)/a)) + i + 1
2137 * over the domain. If there is such a value "n", then we know
2139 * -ceil(f(j)/a)) + i + 1 <= n
2141 * or
2143 * i < ceil(f(j)/a)) + n
2145 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2146 * We just need to check if we have found any lower bound before and
2147 * if the new lower bound is better (smaller n) than the previously found
2148 * lower bounds.
2150 static int update_unrolling_lower_bound(struct isl_find_unroll_data *data,
2151 __isl_keep isl_constraint *c)
2153 isl_aff *aff, *lower;
2154 isl_val *max;
2156 if (!isl_constraint_is_lower_bound(c, isl_dim_set, data->depth))
2157 return 0;
2159 lower = isl_constraint_get_bound(c, isl_dim_set, data->depth);
2160 lower = isl_aff_ceil(lower);
2161 aff = isl_aff_copy(lower);
2162 aff = isl_aff_neg(aff);
2163 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, data->depth, 1);
2164 aff = isl_aff_add_constant_si(aff, 1);
2165 max = isl_set_max_val(data->domain, aff);
2166 isl_aff_free(aff);
2168 if (!max)
2169 goto error;
2170 if (isl_val_is_infty(max)) {
2171 isl_val_free(max);
2172 isl_aff_free(lower);
2173 return 0;
2176 if (isl_val_cmp_si(max, INT_MAX) <= 0 &&
2177 (!data->lower || isl_val_cmp_si(max, *data->n) < 0)) {
2178 isl_aff_free(data->lower);
2179 data->lower = lower;
2180 *data->n = isl_val_get_num_si(max);
2181 } else
2182 isl_aff_free(lower);
2183 isl_val_free(max);
2185 return 1;
2186 error:
2187 isl_aff_free(lower);
2188 return -1;
2191 /* Check if we can use "c" as a lower bound and if it is better than
2192 * any previously found lower bound.
2194 static int constraint_find_unroll(__isl_take isl_constraint *c, void *user)
2196 struct isl_find_unroll_data *data;
2197 int r;
2199 data = (struct isl_find_unroll_data *) user;
2200 r = update_unrolling_lower_bound(data, c);
2201 isl_constraint_free(c);
2203 return r;
2206 /* Look for a lower bound l(i) on the dimension at "depth"
2207 * and a size n such that "domain" is a subset of
2209 * { [i] : l(i) <= i_d < l(i) + n }
2211 * where d is "depth" and l(i) depends only on earlier dimensions.
2212 * Furthermore, try and find a lower bound such that n is as small as possible.
2213 * In particular, "n" needs to be finite.
2215 * Inner dimensions have been eliminated from "domain" by the caller.
2217 * We first construct a collection of lower bounds on the input set
2218 * by computing its simple hull. We then iterate through them,
2219 * discarding those that we cannot use (either because they do not
2220 * involve the dimension at "depth" or because they have no corresponding
2221 * upper bound, meaning that "n" would be unbounded) and pick out the
2222 * best from the remaining ones.
2224 * If we cannot find a suitable lower bound, then we consider that
2225 * to be an error.
2227 static __isl_give isl_aff *find_unroll_lower_bound(__isl_keep isl_set *domain,
2228 int depth, int *n)
2230 struct isl_find_unroll_data data = { domain, depth, NULL, n };
2231 isl_basic_set *hull;
2233 hull = isl_set_simple_hull(isl_set_copy(domain));
2235 if (isl_basic_set_foreach_constraint(hull,
2236 &constraint_find_unroll, &data) < 0)
2237 goto error;
2239 isl_basic_set_free(hull);
2241 if (!data.lower)
2242 isl_die(isl_set_get_ctx(domain), isl_error_invalid,
2243 "cannot find lower bound for unrolling", return NULL);
2245 return data.lower;
2246 error:
2247 isl_basic_set_free(hull);
2248 return isl_aff_free(data.lower);
2251 /* Return the constraint
2253 * i_"depth" = aff + offset
2255 static __isl_give isl_constraint *at_offset(int depth, __isl_keep isl_aff *aff,
2256 int offset)
2258 aff = isl_aff_copy(aff);
2259 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, depth, -1);
2260 aff = isl_aff_add_constant_si(aff, offset);
2261 return isl_equality_from_aff(aff);
2264 /* Data structure for storing the results and the intermediate objects
2265 * of compute_domains.
2267 * "list" is the main result of the function and contains a list
2268 * of disjoint basic sets for which code should be generated.
2270 * "executed" and "build" are inputs to compute_domains.
2271 * "schedule_domain" is the domain of "executed".
2273 * "option" constains the domains at the current depth that should by
2274 * atomic, separated or unrolled. These domains are as specified by
2275 * the user, except that inner dimensions have been eliminated and
2276 * that they have been made pair-wise disjoint.
2278 * "sep_class" contains the user-specified split into separation classes
2279 * specialized to the current depth.
2280 * "done" contains the union of the separation domains that have already
2281 * been handled.
2283 struct isl_codegen_domains {
2284 isl_basic_set_list *list;
2286 isl_union_map *executed;
2287 isl_ast_build *build;
2288 isl_set *schedule_domain;
2290 isl_set *option[3];
2292 isl_map *sep_class;
2293 isl_set *done;
2296 /* Extend domains->list with a list of basic sets, one for each value
2297 * of the current dimension in "domain" and remove the corresponding
2298 * sets from the class domain. Return the updated class domain.
2299 * The divs that involve the current dimension have not been projected out
2300 * from this domain.
2302 * Since we are going to be iterating over the individual values,
2303 * we first check if there are any strides on the current dimension.
2304 * If there is, we rewrite the current dimension i as
2306 * i = stride i' + offset
2308 * and then iterate over individual values of i' instead.
2310 * We then look for a lower bound on i' and a size such that the domain
2311 * is a subset of
2313 * { [j,i'] : l(j) <= i' < l(j) + n }
2315 * and then take slices of the domain at values of i'
2316 * between l(j) and l(j) + n - 1.
2318 * We compute the unshifted simple hull of each slice to ensure that
2319 * we have a single basic set per offset. The slicing constraint
2320 * may get simplified away before the unshifted simple hull is taken
2321 * and may therefore in some rare cases disappear from the result.
2322 * We therefore explicitly add the constraint back after computing
2323 * the unshifted simple hull to ensure that the basic sets
2324 * remain disjoint. The constraints that are dropped by taking the hull
2325 * will be taken into account at the next level, as in the case of the
2326 * atomic option.
2328 * Finally, we map i' back to i and add each basic set to the list.
2329 * Since we may have dropped some constraints, we intersect with
2330 * the class domain again to ensure that each element in the list
2331 * is disjoint from the other class domains.
2333 static __isl_give isl_set *do_unroll(struct isl_codegen_domains *domains,
2334 __isl_take isl_set *domain, __isl_take isl_set *class_domain)
2336 int i, n;
2337 int depth;
2338 isl_ctx *ctx;
2339 isl_aff *lower;
2340 isl_multi_aff *expansion;
2341 isl_basic_map *bmap;
2342 isl_set *unroll_domain;
2343 isl_ast_build *build;
2345 if (!domain)
2346 return isl_set_free(class_domain);
2348 ctx = isl_set_get_ctx(domain);
2349 depth = isl_ast_build_get_depth(domains->build);
2350 build = isl_ast_build_copy(domains->build);
2351 domain = isl_ast_build_eliminate_inner(build, domain);
2352 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
2353 expansion = isl_ast_build_get_stride_expansion(build);
2355 domain = isl_set_preimage_multi_aff(domain,
2356 isl_multi_aff_copy(expansion));
2357 domain = isl_ast_build_eliminate_divs(build, domain);
2359 isl_ast_build_free(build);
2361 lower = find_unroll_lower_bound(domain, depth, &n);
2362 if (!lower)
2363 class_domain = isl_set_free(class_domain);
2365 bmap = isl_basic_map_from_multi_aff(expansion);
2367 unroll_domain = isl_set_empty(isl_set_get_space(domain));
2369 for (i = 0; class_domain && i < n; ++i) {
2370 isl_set *set;
2371 isl_basic_set *bset;
2372 isl_constraint *slice;
2373 isl_basic_set_list *list;
2375 slice = at_offset(depth, lower, i);
2376 set = isl_set_copy(domain);
2377 set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2378 bset = isl_set_unshifted_simple_hull(set);
2379 bset = isl_basic_set_add_constraint(bset, slice);
2380 bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2381 set = isl_set_from_basic_set(bset);
2382 unroll_domain = isl_set_union(unroll_domain, isl_set_copy(set));
2383 set = isl_set_intersect(set, isl_set_copy(class_domain));
2384 set = isl_set_make_disjoint(set);
2385 list = isl_basic_set_list_from_set(set);
2386 domains->list = isl_basic_set_list_concat(domains->list, list);
2389 class_domain = isl_set_subtract(class_domain, unroll_domain);
2391 isl_aff_free(lower);
2392 isl_set_free(domain);
2393 isl_basic_map_free(bmap);
2395 return class_domain;
2398 /* Add domains to domains->list for each individual value of the current
2399 * dimension, for that part of the schedule domain that lies in the
2400 * intersection of the option domain and the class domain.
2401 * Remove the corresponding sets from the class domain and
2402 * return the updated class domain.
2404 * We first break up the unroll option domain into individual pieces
2405 * and then handle each of them separately. The unroll option domain
2406 * has been made disjoint in compute_domains_init_options,
2408 * Note that we actively want to combine different pieces of the
2409 * schedule domain that have the same value at the current dimension.
2410 * We therefore need to break up the unroll option domain before
2411 * intersecting with class and schedule domain, hoping that the
2412 * unroll option domain specified by the user is relatively simple.
2414 static __isl_give isl_set *compute_unroll_domains(
2415 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2417 isl_set *unroll_domain;
2418 isl_basic_set_list *unroll_list;
2419 int i, n;
2420 int empty;
2422 empty = isl_set_is_empty(domains->option[unroll]);
2423 if (empty < 0)
2424 return isl_set_free(class_domain);
2425 if (empty)
2426 return class_domain;
2428 unroll_domain = isl_set_copy(domains->option[unroll]);
2429 unroll_list = isl_basic_set_list_from_set(unroll_domain);
2431 n = isl_basic_set_list_n_basic_set(unroll_list);
2432 for (i = 0; i < n; ++i) {
2433 isl_basic_set *bset;
2435 bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2436 unroll_domain = isl_set_from_basic_set(bset);
2437 unroll_domain = isl_set_intersect(unroll_domain,
2438 isl_set_copy(class_domain));
2439 unroll_domain = isl_set_intersect(unroll_domain,
2440 isl_set_copy(domains->schedule_domain));
2442 empty = isl_set_is_empty(unroll_domain);
2443 if (empty >= 0 && empty) {
2444 isl_set_free(unroll_domain);
2445 continue;
2448 class_domain = do_unroll(domains, unroll_domain, class_domain);
2451 isl_basic_set_list_free(unroll_list);
2453 return class_domain;
2456 /* Try and construct a single basic set that includes the intersection of
2457 * the schedule domain, the atomic option domain and the class domain.
2458 * Add the resulting basic set(s) to domains->list and remove them
2459 * from class_domain. Return the updated class domain.
2461 * We construct a single domain rather than trying to combine
2462 * the schedule domains of individual domains because we are working
2463 * within a single component so that non-overlapping schedule domains
2464 * should already have been separated.
2465 * We do however need to make sure that this single domains is a subset
2466 * of the class domain so that it would not intersect with any other
2467 * class domains. This means that we may end up splitting up the atomic
2468 * domain in case separation classes are being used.
2470 * "domain" is the intersection of the schedule domain and the class domain,
2471 * with inner dimensions projected out.
2473 static __isl_give isl_set *compute_atomic_domain(
2474 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2476 isl_basic_set *bset;
2477 isl_basic_set_list *list;
2478 isl_set *domain, *atomic_domain;
2479 int empty;
2481 domain = isl_set_copy(domains->option[atomic]);
2482 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2483 domain = isl_set_intersect(domain,
2484 isl_set_copy(domains->schedule_domain));
2485 empty = isl_set_is_empty(domain);
2486 if (empty < 0)
2487 class_domain = isl_set_free(class_domain);
2488 if (empty) {
2489 isl_set_free(domain);
2490 return class_domain;
2493 domain = isl_ast_build_eliminate(domains->build, domain);
2494 domain = isl_set_coalesce(domain);
2495 bset = isl_set_unshifted_simple_hull(domain);
2496 domain = isl_set_from_basic_set(bset);
2497 atomic_domain = isl_set_copy(domain);
2498 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2499 class_domain = isl_set_subtract(class_domain, atomic_domain);
2500 domain = isl_set_make_disjoint(domain);
2501 list = isl_basic_set_list_from_set(domain);
2502 domains->list = isl_basic_set_list_concat(domains->list, list);
2504 return class_domain;
2507 /* Split up the schedule domain into uniform basic sets,
2508 * in the sense that each element in a basic set is associated to
2509 * elements of the same domains, and add the result to domains->list.
2510 * Do this for that part of the schedule domain that lies in the
2511 * intersection of "class_domain" and the separate option domain.
2513 * "class_domain" may or may not include the constraints
2514 * of the schedule domain, but this does not make a difference
2515 * since we are going to intersect it with the domain of the inverse schedule.
2516 * If it includes schedule domain constraints, then they may involve
2517 * inner dimensions, but we will eliminate them in separation_domain.
2519 static int compute_separate_domain(struct isl_codegen_domains *domains,
2520 __isl_keep isl_set *class_domain)
2522 isl_space *space;
2523 isl_set *domain;
2524 isl_union_map *executed;
2525 isl_basic_set_list *list;
2526 int empty;
2528 domain = isl_set_copy(domains->option[separate]);
2529 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2530 executed = isl_union_map_copy(domains->executed);
2531 executed = isl_union_map_intersect_domain(executed,
2532 isl_union_set_from_set(domain));
2533 empty = isl_union_map_is_empty(executed);
2534 if (empty < 0 || empty) {
2535 isl_union_map_free(executed);
2536 return empty < 0 ? -1 : 0;
2539 space = isl_set_get_space(class_domain);
2540 domain = separate_schedule_domains(space, executed, domains->build);
2542 list = isl_basic_set_list_from_set(domain);
2543 domains->list = isl_basic_set_list_concat(domains->list, list);
2545 return 0;
2548 /* Split up the domain at the current depth into disjoint
2549 * basic sets for which code should be generated separately
2550 * for the given separation class domain.
2552 * If any separation classes have been defined, then "class_domain"
2553 * is the domain of the current class and does not refer to inner dimensions.
2554 * Otherwise, "class_domain" is the universe domain.
2556 * We first make sure that the class domain is disjoint from
2557 * previously considered class domains.
2559 * The separate domains can be computed directly from the "class_domain".
2561 * The unroll, atomic and remainder domains need the constraints
2562 * from the schedule domain.
2564 * For unrolling, the actual schedule domain is needed (with divs that
2565 * may refer to the current dimension) so that stride detection can be
2566 * performed.
2568 * For atomic and remainder domains, inner dimensions and divs involving
2569 * the current dimensions should be eliminated.
2570 * In case we are working within a separation class, we need to intersect
2571 * the result with the current "class_domain" to ensure that the domains
2572 * are disjoint from those generated from other class domains.
2574 * The domain that has been made atomic may be larger than specified
2575 * by the user since it needs to be representable as a single basic set.
2576 * This possibly larger domain is removed from class_domain by
2577 * compute_atomic_domain. It is computed first so that the extended domain
2578 * would not overlap with any domains computed before.
2579 * Similary, the unrolled domains may have some constraints removed and
2580 * may therefore also be larger than specified by the user.
2582 * If anything is left after handling separate, unroll and atomic,
2583 * we split it up into basic sets and append the basic sets to domains->list.
2585 static int compute_partial_domains(struct isl_codegen_domains *domains,
2586 __isl_take isl_set *class_domain)
2588 isl_basic_set_list *list;
2589 isl_set *domain;
2591 class_domain = isl_set_subtract(class_domain,
2592 isl_set_copy(domains->done));
2593 domains->done = isl_set_union(domains->done,
2594 isl_set_copy(class_domain));
2596 class_domain = compute_atomic_domain(domains, class_domain);
2597 class_domain = compute_unroll_domains(domains, class_domain);
2599 domain = isl_set_copy(class_domain);
2601 if (compute_separate_domain(domains, domain) < 0)
2602 goto error;
2603 domain = isl_set_subtract(domain,
2604 isl_set_copy(domains->option[separate]));
2606 domain = isl_set_intersect(domain,
2607 isl_set_copy(domains->schedule_domain));
2609 domain = isl_ast_build_eliminate(domains->build, domain);
2610 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2612 domain = isl_set_coalesce(domain);
2613 domain = isl_set_make_disjoint(domain);
2615 list = isl_basic_set_list_from_set(domain);
2616 domains->list = isl_basic_set_list_concat(domains->list, list);
2618 isl_set_free(class_domain);
2620 return 0;
2621 error:
2622 isl_set_free(domain);
2623 isl_set_free(class_domain);
2624 return -1;
2627 /* Split up the domain at the current depth into disjoint
2628 * basic sets for which code should be generated separately
2629 * for the separation class identified by "pnt".
2631 * We extract the corresponding class domain from domains->sep_class,
2632 * eliminate inner dimensions and pass control to compute_partial_domains.
2634 static int compute_class_domains(__isl_take isl_point *pnt, void *user)
2636 struct isl_codegen_domains *domains = user;
2637 isl_set *class_set;
2638 isl_set *domain;
2639 int disjoint;
2641 class_set = isl_set_from_point(pnt);
2642 domain = isl_map_domain(isl_map_intersect_range(
2643 isl_map_copy(domains->sep_class), class_set));
2644 domain = isl_ast_build_compute_gist(domains->build, domain);
2645 domain = isl_ast_build_eliminate(domains->build, domain);
2647 disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
2648 if (disjoint < 0)
2649 return -1;
2650 if (disjoint) {
2651 isl_set_free(domain);
2652 return 0;
2655 return compute_partial_domains(domains, domain);
2658 /* Extract the domains at the current depth that should be atomic,
2659 * separated or unrolled and store them in option.
2661 * The domains specified by the user might overlap, so we make
2662 * them disjoint by subtracting earlier domains from later domains.
2664 static void compute_domains_init_options(isl_set *option[3],
2665 __isl_keep isl_ast_build *build)
2667 enum isl_ast_build_domain_type type, type2;
2669 for (type = atomic; type <= separate; ++type) {
2670 option[type] = isl_ast_build_get_option_domain(build, type);
2671 for (type2 = atomic; type2 < type; ++type2)
2672 option[type] = isl_set_subtract(option[type],
2673 isl_set_copy(option[type2]));
2676 option[unroll] = isl_set_coalesce(option[unroll]);
2677 option[unroll] = isl_set_make_disjoint(option[unroll]);
2680 /* Split up the domain at the current depth into disjoint
2681 * basic sets for which code should be generated separately,
2682 * based on the user-specified options.
2683 * Return the list of disjoint basic sets.
2685 * There are three kinds of domains that we need to keep track of.
2686 * - the "schedule domain" is the domain of "executed"
2687 * - the "class domain" is the domain corresponding to the currrent
2688 * separation class
2689 * - the "option domain" is the domain corresponding to one of the options
2690 * atomic, unroll or separate
2692 * We first consider the individial values of the separation classes
2693 * and split up the domain for each of them separately.
2694 * Finally, we consider the remainder. If no separation classes were
2695 * specified, then we call compute_partial_domains with the universe
2696 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
2697 * with inner dimensions removed. We do this because we want to
2698 * avoid computing the complement of the class domains (i.e., the difference
2699 * between the universe and domains->done).
2701 static __isl_give isl_basic_set_list *compute_domains(
2702 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2704 struct isl_codegen_domains domains;
2705 isl_ctx *ctx;
2706 isl_set *domain;
2707 isl_union_set *schedule_domain;
2708 isl_set *classes;
2709 isl_space *space;
2710 int n_param;
2711 enum isl_ast_build_domain_type type;
2712 int empty;
2714 if (!executed)
2715 return NULL;
2717 ctx = isl_union_map_get_ctx(executed);
2718 domains.list = isl_basic_set_list_alloc(ctx, 0);
2720 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
2721 domain = isl_set_from_union_set(schedule_domain);
2723 compute_domains_init_options(domains.option, build);
2725 domains.sep_class = isl_ast_build_get_separation_class(build);
2726 classes = isl_map_range(isl_map_copy(domains.sep_class));
2727 n_param = isl_set_dim(classes, isl_dim_param);
2728 classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
2730 space = isl_set_get_space(domain);
2731 domains.build = build;
2732 domains.schedule_domain = isl_set_copy(domain);
2733 domains.executed = executed;
2734 domains.done = isl_set_empty(space);
2736 if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
2737 domains.list = isl_basic_set_list_free(domains.list);
2738 isl_set_free(classes);
2740 empty = isl_set_is_empty(domains.done);
2741 if (empty < 0) {
2742 domains.list = isl_basic_set_list_free(domains.list);
2743 domain = isl_set_free(domain);
2744 } else if (empty) {
2745 isl_set_free(domain);
2746 domain = isl_set_universe(isl_set_get_space(domains.done));
2747 } else {
2748 domain = isl_ast_build_eliminate(build, domain);
2750 if (compute_partial_domains(&domains, domain) < 0)
2751 domains.list = isl_basic_set_list_free(domains.list);
2753 isl_set_free(domains.schedule_domain);
2754 isl_set_free(domains.done);
2755 isl_map_free(domains.sep_class);
2756 for (type = atomic; type <= separate; ++type)
2757 isl_set_free(domains.option[type]);
2759 return domains.list;
2762 /* Generate code for a single component, after shifting (if any)
2763 * has been applied.
2765 * We first split up the domain at the current depth into disjoint
2766 * basic sets based on the user-specified options.
2767 * Then we generated code for each of them and concatenate the results.
2769 static __isl_give isl_ast_graft_list *generate_shifted_component(
2770 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
2772 isl_basic_set_list *domain_list;
2773 isl_ast_graft_list *list = NULL;
2775 domain_list = compute_domains(executed, build);
2776 list = generate_parallel_domains(domain_list, executed, build);
2778 isl_basic_set_list_free(domain_list);
2779 isl_union_map_free(executed);
2780 isl_ast_build_free(build);
2782 return list;
2785 struct isl_set_map_pair {
2786 isl_set *set;
2787 isl_map *map;
2790 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2791 * of indices into the "domain" array,
2792 * return the union of the "map" fields of the elements
2793 * indexed by the first "n" elements of "order".
2795 static __isl_give isl_union_map *construct_component_executed(
2796 struct isl_set_map_pair *domain, int *order, int n)
2798 int i;
2799 isl_map *map;
2800 isl_union_map *executed;
2802 map = isl_map_copy(domain[order[0]].map);
2803 executed = isl_union_map_from_map(map);
2804 for (i = 1; i < n; ++i) {
2805 map = isl_map_copy(domain[order[i]].map);
2806 executed = isl_union_map_add_map(executed, map);
2809 return executed;
2812 /* Generate code for a single component, after shifting (if any)
2813 * has been applied.
2815 * The component inverse schedule is specified as the "map" fields
2816 * of the elements of "domain" indexed by the first "n" elements of "order".
2818 static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
2819 struct isl_set_map_pair *domain, int *order, int n,
2820 __isl_take isl_ast_build *build)
2822 isl_union_map *executed;
2824 executed = construct_component_executed(domain, order, n);
2825 return generate_shifted_component(executed, build);
2828 /* Does set dimension "pos" of "set" have an obviously fixed value?
2830 static int dim_is_fixed(__isl_keep isl_set *set, int pos)
2832 int fixed;
2833 isl_val *v;
2835 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, pos);
2836 if (!v)
2837 return -1;
2838 fixed = !isl_val_is_nan(v);
2839 isl_val_free(v);
2841 return fixed;
2844 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2845 * of indices into the "domain" array,
2846 * do all (except for at most one) of the "set" field of the elements
2847 * indexed by the first "n" elements of "order" have a fixed value
2848 * at position "depth"?
2850 static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
2851 int *order, int n, int depth)
2853 int i;
2854 int non_fixed = -1;
2856 for (i = 0; i < n; ++i) {
2857 int f;
2859 f = dim_is_fixed(domain[order[i]].set, depth);
2860 if (f < 0)
2861 return -1;
2862 if (f)
2863 continue;
2864 if (non_fixed >= 0)
2865 return 0;
2866 non_fixed = i;
2869 return 1;
2872 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2873 * of indices into the "domain" array,
2874 * eliminate the inner dimensions from the "set" field of the elements
2875 * indexed by the first "n" elements of "order", provided the current
2876 * dimension does not have a fixed value.
2878 * Return the index of the first element in "order" with a corresponding
2879 * "set" field that does not have an (obviously) fixed value.
2881 static int eliminate_non_fixed(struct isl_set_map_pair *domain,
2882 int *order, int n, int depth, __isl_keep isl_ast_build *build)
2884 int i;
2885 int base = -1;
2887 for (i = n - 1; i >= 0; --i) {
2888 int f;
2889 f = dim_is_fixed(domain[order[i]].set, depth);
2890 if (f < 0)
2891 return -1;
2892 if (f)
2893 continue;
2894 domain[order[i]].set = isl_ast_build_eliminate_inner(build,
2895 domain[order[i]].set);
2896 base = i;
2899 return base;
2902 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2903 * of indices into the "domain" array,
2904 * find the element of "domain" (amongst those indexed by the first "n"
2905 * elements of "order") with the "set" field that has the smallest
2906 * value for the current iterator.
2908 * Note that the domain with the smallest value may depend on the parameters
2909 * and/or outer loop dimension. Since the result of this function is only
2910 * used as heuristic, we only make a reasonable attempt at finding the best
2911 * domain, one that should work in case a single domain provides the smallest
2912 * value for the current dimension over all values of the parameters
2913 * and outer dimensions.
2915 * In particular, we compute the smallest value of the first domain
2916 * and replace it by that of any later domain if that later domain
2917 * has a smallest value that is smaller for at least some value
2918 * of the parameters and outer dimensions.
2920 static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
2921 __isl_keep isl_ast_build *build)
2923 int i;
2924 isl_map *min_first;
2925 int first = 0;
2927 min_first = isl_ast_build_map_to_iterator(build,
2928 isl_set_copy(domain[order[0]].set));
2929 min_first = isl_map_lexmin(min_first);
2931 for (i = 1; i < n; ++i) {
2932 isl_map *min, *test;
2933 int empty;
2935 min = isl_ast_build_map_to_iterator(build,
2936 isl_set_copy(domain[order[i]].set));
2937 min = isl_map_lexmin(min);
2938 test = isl_map_copy(min);
2939 test = isl_map_apply_domain(isl_map_copy(min_first), test);
2940 test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
2941 empty = isl_map_is_empty(test);
2942 isl_map_free(test);
2943 if (empty >= 0 && !empty) {
2944 isl_map_free(min_first);
2945 first = i;
2946 min_first = min;
2947 } else
2948 isl_map_free(min);
2950 if (empty < 0)
2951 break;
2954 isl_map_free(min_first);
2956 return i < n ? -1 : first;
2959 /* Construct a shifted inverse schedule based on the original inverse schedule,
2960 * the stride and the offset.
2962 * The original inverse schedule is specified as the "map" fields
2963 * of the elements of "domain" indexed by the first "n" elements of "order".
2965 * "stride" and "offset" are such that the difference
2966 * between the values of the current dimension of domain "i"
2967 * and the values of the current dimension for some reference domain are
2968 * equal to
2970 * stride * integer + offset[i]
2972 * Moreover, 0 <= offset[i] < stride.
2974 * For each domain, we create a map
2976 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
2978 * where j refers to the current dimension and the other dimensions are
2979 * unchanged, and apply this map to the original schedule domain.
2981 * For example, for the original schedule
2983 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
2985 * and assuming the offset is 0 for the A domain and 1 for the B domain,
2986 * we apply the mapping
2988 * { [j] -> [j, 0] }
2990 * to the schedule of the "A" domain and the mapping
2992 * { [j - 1] -> [j, 1] }
2994 * to the schedule of the "B" domain.
2997 * Note that after the transformation, the differences between pairs
2998 * of values of the current dimension over all domains are multiples
2999 * of stride and that we have therefore exposed the stride.
3002 * To see that the mapping preserves the lexicographic order,
3003 * first note that each of the individual maps above preserves the order.
3004 * If the value of the current iterator is j1 in one domain and j2 in another,
3005 * then if j1 = j2, we know that the same map is applied to both domains
3006 * and the order is preserved.
3007 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3008 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3010 * j1 - c1 < j2 - c2
3012 * and the order is preserved.
3013 * If c1 < c2, then we know
3015 * 0 <= c2 - c1 < s
3017 * We also have
3019 * j2 - j1 = n * s + r
3021 * with n >= 0 and 0 <= r < s.
3022 * In other words, r = c2 - c1.
3023 * If n > 0, then
3025 * j1 - c1 < j2 - c2
3027 * If n = 0, then
3029 * j1 - c1 = j2 - c2
3031 * and so
3033 * (j1 - c1, c1) << (j2 - c2, c2)
3035 * with "<<" the lexicographic order, proving that the order is preserved
3036 * in all cases.
3038 static __isl_give isl_union_map *contruct_shifted_executed(
3039 struct isl_set_map_pair *domain, int *order, int n,
3040 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3041 __isl_take isl_ast_build *build)
3043 int i;
3044 isl_union_map *executed;
3045 isl_space *space;
3046 isl_map *map;
3047 int depth;
3048 isl_constraint *c;
3050 depth = isl_ast_build_get_depth(build);
3051 space = isl_ast_build_get_space(build, 1);
3052 executed = isl_union_map_empty(isl_space_copy(space));
3053 space = isl_space_map_from_set(space);
3054 map = isl_map_identity(isl_space_copy(space));
3055 map = isl_map_eliminate(map, isl_dim_out, depth, 1);
3056 map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
3057 space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
3059 c = isl_equality_alloc(isl_local_space_from_space(space));
3060 c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
3061 c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
3063 for (i = 0; i < n; ++i) {
3064 isl_map *map_i;
3065 isl_val *v;
3067 v = isl_multi_val_get_val(offset, i);
3068 if (!v)
3069 break;
3070 map_i = isl_map_copy(map);
3071 map_i = isl_map_fix_val(map_i, isl_dim_out, depth + 1,
3072 isl_val_copy(v));
3073 v = isl_val_neg(v);
3074 c = isl_constraint_set_constant_val(c, v);
3075 map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
3077 map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
3078 map_i);
3079 executed = isl_union_map_add_map(executed, map_i);
3082 isl_constraint_free(c);
3083 isl_map_free(map);
3085 if (i < n)
3086 executed = isl_union_map_free(executed);
3088 return executed;
3091 /* Generate code for a single component, after exposing the stride,
3092 * given that the schedule domain is "shifted strided".
3094 * The component inverse schedule is specified as the "map" fields
3095 * of the elements of "domain" indexed by the first "n" elements of "order".
3097 * The schedule domain being "shifted strided" means that the differences
3098 * between the values of the current dimension of domain "i"
3099 * and the values of the current dimension for some reference domain are
3100 * equal to
3102 * stride * integer + offset[i]
3104 * We first look for the domain with the "smallest" value for the current
3105 * dimension and adjust the offsets such that the offset of the "smallest"
3106 * domain is equal to zero. The other offsets are reduced modulo stride.
3108 * Based on this information, we construct a new inverse schedule in
3109 * contruct_shifted_executed that exposes the stride.
3110 * Since this involves the introduction of a new schedule dimension,
3111 * the build needs to be changed accodingly.
3112 * After computing the AST, the newly introduced dimension needs
3113 * to be removed again from the list of grafts. We do this by plugging
3114 * in a mapping that represents the new schedule domain in terms of the
3115 * old schedule domain.
3117 static __isl_give isl_ast_graft_list *generate_shift_component(
3118 struct isl_set_map_pair *domain, int *order, int n,
3119 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3120 __isl_take isl_ast_build *build)
3122 isl_ast_graft_list *list;
3123 int first;
3124 int depth;
3125 isl_ctx *ctx;
3126 isl_val *val;
3127 isl_multi_val *mv;
3128 isl_space *space;
3129 isl_multi_aff *ma, *zero;
3130 isl_union_map *executed;
3132 ctx = isl_ast_build_get_ctx(build);
3133 depth = isl_ast_build_get_depth(build);
3135 first = first_offset(domain, order, n, build);
3136 if (first < 0)
3137 goto error;
3139 mv = isl_multi_val_copy(offset);
3140 val = isl_multi_val_get_val(offset, first);
3141 val = isl_val_neg(val);
3142 mv = isl_multi_val_add_val(mv, val);
3143 mv = isl_multi_val_mod_val(mv, isl_val_copy(stride));
3145 executed = contruct_shifted_executed(domain, order, n, stride, mv,
3146 build);
3147 space = isl_ast_build_get_space(build, 1);
3148 space = isl_space_map_from_set(space);
3149 ma = isl_multi_aff_identity(isl_space_copy(space));
3150 space = isl_space_from_domain(isl_space_domain(space));
3151 space = isl_space_add_dims(space, isl_dim_out, 1);
3152 zero = isl_multi_aff_zero(space);
3153 ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
3154 build = isl_ast_build_insert_dim(build, depth + 1);
3155 list = generate_shifted_component(executed, build);
3157 list = isl_ast_graft_list_preimage_multi_aff(list, ma);
3159 isl_multi_val_free(mv);
3161 return list;
3162 error:
3163 isl_ast_build_free(build);
3164 return NULL;
3167 /* Generate code for a single component.
3169 * The component inverse schedule is specified as the "map" fields
3170 * of the elements of "domain" indexed by the first "n" elements of "order".
3172 * This function may modify the "set" fields of "domain".
3174 * Before proceeding with the actual code generation for the component,
3175 * we first check if there are any "shifted" strides, meaning that
3176 * the schedule domains of the individual domains are all strided,
3177 * but that they have different offsets, resulting in the union
3178 * of schedule domains not being strided anymore.
3180 * The simplest example is the schedule
3182 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3184 * Both schedule domains are strided, but their union is not.
3185 * This function detects such cases and then rewrites the schedule to
3187 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
3189 * In the new schedule, the schedule domains have the same offset (modulo
3190 * the stride), ensuring that the union of schedule domains is also strided.
3193 * If there is only a single domain in the component, then there is
3194 * nothing to do. Similarly, if the current schedule dimension has
3195 * a fixed value for almost all domains then there is nothing to be done.
3196 * In particular, we need at least two domains where the current schedule
3197 * dimension does not have a fixed value.
3198 * Finally, if any of the options refer to the current schedule dimension,
3199 * then we bail out as well. It would be possible to reformulate the options
3200 * in terms of the new schedule domain, but that would introduce constraints
3201 * that separate the domains in the options and that is something we would
3202 * like to avoid.
3205 * To see if there is any shifted stride, we look at the differences
3206 * between the values of the current dimension in pairs of domains
3207 * for equal values of outer dimensions. These differences should be
3208 * of the form
3210 * m x + r
3212 * with "m" the stride and "r" a constant. Note that we cannot perform
3213 * this analysis on individual domains as the lower bound in each domain
3214 * may depend on parameters or outer dimensions and so the current dimension
3215 * itself may not have a fixed remainder on division by the stride.
3217 * In particular, we compare the first domain that does not have an
3218 * obviously fixed value for the current dimension to itself and all
3219 * other domains and collect the offsets and the gcd of the strides.
3220 * If the gcd becomes one, then we failed to find shifted strides.
3221 * If the gcd is zero, then the differences were all fixed, meaning
3222 * that some domains had non-obviously fixed values for the current dimension.
3223 * If all the offsets are the same (for those domains that do not have
3224 * an obviously fixed value for the current dimension), then we do not
3225 * apply the transformation.
3226 * If none of the domains were skipped, then there is nothing to do.
3227 * If some of them were skipped, then if we apply separation, the schedule
3228 * domain should get split in pieces with a (non-shifted) stride.
3230 * Otherwise, we apply a shift to expose the stride in
3231 * generate_shift_component.
3233 static __isl_give isl_ast_graft_list *generate_component(
3234 struct isl_set_map_pair *domain, int *order, int n,
3235 __isl_take isl_ast_build *build)
3237 int i, d;
3238 int depth;
3239 isl_ctx *ctx;
3240 isl_map *map;
3241 isl_set *deltas;
3242 isl_val *gcd = NULL;
3243 isl_multi_val *mv;
3244 int fixed, skip;
3245 int base;
3246 isl_ast_graft_list *list;
3247 int res = 0;
3249 depth = isl_ast_build_get_depth(build);
3251 skip = n == 1;
3252 if (skip >= 0 && !skip)
3253 skip = at_most_one_non_fixed(domain, order, n, depth);
3254 if (skip >= 0 && !skip)
3255 skip = isl_ast_build_options_involve_depth(build);
3256 if (skip < 0)
3257 goto error;
3258 if (skip)
3259 return generate_shifted_component_from_list(domain,
3260 order, n, build);
3262 base = eliminate_non_fixed(domain, order, n, depth, build);
3263 if (base < 0)
3264 goto error;
3266 ctx = isl_ast_build_get_ctx(build);
3268 mv = isl_multi_val_zero(isl_space_set_alloc(ctx, 0, n));
3270 fixed = 1;
3271 for (i = 0; i < n; ++i) {
3272 isl_val *r, *m;
3274 map = isl_map_from_domain_and_range(
3275 isl_set_copy(domain[order[base]].set),
3276 isl_set_copy(domain[order[i]].set));
3277 for (d = 0; d < depth; ++d)
3278 map = isl_map_equate(map, isl_dim_in, d,
3279 isl_dim_out, d);
3280 deltas = isl_map_deltas(map);
3281 res = isl_set_dim_residue_class_val(deltas, depth, &m, &r);
3282 isl_set_free(deltas);
3283 if (res < 0)
3284 break;
3286 if (i == 0)
3287 gcd = m;
3288 else
3289 gcd = isl_val_gcd(gcd, m);
3290 if (isl_val_is_one(gcd)) {
3291 isl_val_free(r);
3292 break;
3294 mv = isl_multi_val_set_val(mv, i, r);
3296 res = dim_is_fixed(domain[order[i]].set, depth);
3297 if (res < 0)
3298 break;
3299 if (res)
3300 continue;
3302 if (fixed && i > base) {
3303 isl_val *a, *b;
3304 a = isl_multi_val_get_val(mv, i);
3305 b = isl_multi_val_get_val(mv, base);
3306 if (isl_val_ne(a, b))
3307 fixed = 0;
3308 isl_val_free(a);
3309 isl_val_free(b);
3313 if (res < 0 || !gcd) {
3314 isl_ast_build_free(build);
3315 list = NULL;
3316 } else if (i < n || fixed || isl_val_is_zero(gcd)) {
3317 list = generate_shifted_component_from_list(domain,
3318 order, n, build);
3319 } else {
3320 list = generate_shift_component(domain, order, n, gcd, mv,
3321 build);
3324 isl_val_free(gcd);
3325 isl_multi_val_free(mv);
3327 return list;
3328 error:
3329 isl_ast_build_free(build);
3330 return NULL;
3333 /* Store both "map" itself and its domain in the
3334 * structure pointed to by *next and advance to the next array element.
3336 static int extract_domain(__isl_take isl_map *map, void *user)
3338 struct isl_set_map_pair **next = user;
3340 (*next)->map = isl_map_copy(map);
3341 (*next)->set = isl_map_domain(map);
3342 (*next)++;
3344 return 0;
3347 /* Internal data for any_scheduled_after.
3349 * "depth" is the number of loops that have already been generated
3350 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
3351 * "domain" is an array of set-map pairs corresponding to the different
3352 * iteration domains. The set is the schedule domain, i.e., the domain
3353 * of the inverse schedule, while the map is the inverse schedule itself.
3355 struct isl_any_scheduled_after_data {
3356 int depth;
3357 int group_coscheduled;
3358 struct isl_set_map_pair *domain;
3361 /* Is any element of domain "i" scheduled after any element of domain "j"
3362 * (for a common iteration of the first data->depth loops)?
3364 * data->domain[i].set contains the domain of the inverse schedule
3365 * for domain "i", i.e., elements in the schedule domain.
3367 * If data->group_coscheduled is set, then we also return 1 if there
3368 * is any pair of elements in the two domains that are scheduled together.
3370 static int any_scheduled_after(int i, int j, void *user)
3372 struct isl_any_scheduled_after_data *data = user;
3373 int dim = isl_set_dim(data->domain[i].set, isl_dim_set);
3374 int pos;
3376 for (pos = data->depth; pos < dim; ++pos) {
3377 int follows;
3379 follows = isl_set_follows_at(data->domain[i].set,
3380 data->domain[j].set, pos);
3382 if (follows < -1)
3383 return -1;
3384 if (follows > 0)
3385 return 1;
3386 if (follows < 0)
3387 return 0;
3390 return data->group_coscheduled;
3393 /* Look for independent components at the current depth and generate code
3394 * for each component separately. The resulting lists of grafts are
3395 * merged in an attempt to combine grafts with identical guards.
3397 * Code for two domains can be generated separately if all the elements
3398 * of one domain are scheduled before (or together with) all the elements
3399 * of the other domain. We therefore consider the graph with as nodes
3400 * the domains and an edge between two nodes if any element of the first
3401 * node is scheduled after any element of the second node.
3402 * If the ast_build_group_coscheduled is set, then we also add an edge if
3403 * there is any pair of elements in the two domains that are scheduled
3404 * together.
3405 * Code is then generated (by generate_component)
3406 * for each of the strongly connected components in this graph
3407 * in their topological order.
3409 * Since the test is performed on the domain of the inverse schedules of
3410 * the different domains, we precompute these domains and store
3411 * them in data.domain.
3413 static __isl_give isl_ast_graft_list *generate_components(
3414 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3416 int i;
3417 isl_ctx *ctx = isl_ast_build_get_ctx(build);
3418 int n = isl_union_map_n_map(executed);
3419 struct isl_any_scheduled_after_data data;
3420 struct isl_set_map_pair *next;
3421 struct isl_tarjan_graph *g = NULL;
3422 isl_ast_graft_list *list = NULL;
3423 int n_domain = 0;
3425 data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
3426 if (!data.domain)
3427 goto error;
3428 n_domain = n;
3430 next = data.domain;
3431 if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
3432 goto error;
3434 if (!build)
3435 goto error;
3436 data.depth = isl_ast_build_get_depth(build);
3437 data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
3438 g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
3439 if (!g)
3440 goto error;
3442 list = isl_ast_graft_list_alloc(ctx, 0);
3444 i = 0;
3445 while (list && n) {
3446 isl_ast_graft_list *list_c;
3447 int first = i;
3449 if (g->order[i] == -1)
3450 isl_die(ctx, isl_error_internal, "cannot happen",
3451 goto error);
3452 ++i; --n;
3453 while (g->order[i] != -1) {
3454 ++i; --n;
3457 list_c = generate_component(data.domain,
3458 g->order + first, i - first,
3459 isl_ast_build_copy(build));
3460 list = isl_ast_graft_list_merge(list, list_c, build);
3462 ++i;
3465 if (0)
3466 error: list = isl_ast_graft_list_free(list);
3467 isl_tarjan_graph_free(g);
3468 for (i = 0; i < n_domain; ++i) {
3469 isl_map_free(data.domain[i].map);
3470 isl_set_free(data.domain[i].set);
3472 free(data.domain);
3473 isl_union_map_free(executed);
3474 isl_ast_build_free(build);
3476 return list;
3479 /* Generate code for the next level (and all inner levels).
3481 * If "executed" is empty, i.e., no code needs to be generated,
3482 * then we return an empty list.
3484 * If we have already generated code for all loop levels, then we pass
3485 * control to generate_inner_level.
3487 * If "executed" lives in a single space, i.e., if code needs to be
3488 * generated for a single domain, then there can only be a single
3489 * component and we go directly to generate_shifted_component.
3490 * Otherwise, we call generate_components to detect the components
3491 * and to call generate_component on each of them separately.
3493 static __isl_give isl_ast_graft_list *generate_next_level(
3494 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3496 int depth;
3498 if (!build || !executed)
3499 goto error;
3501 if (isl_union_map_is_empty(executed)) {
3502 isl_ctx *ctx = isl_ast_build_get_ctx(build);
3503 isl_union_map_free(executed);
3504 isl_ast_build_free(build);
3505 return isl_ast_graft_list_alloc(ctx, 0);
3508 depth = isl_ast_build_get_depth(build);
3509 if (depth >= isl_set_dim(build->domain, isl_dim_set))
3510 return generate_inner_level(executed, build);
3512 if (isl_union_map_n_map(executed) == 1)
3513 return generate_shifted_component(executed, build);
3515 return generate_components(executed, build);
3516 error:
3517 isl_union_map_free(executed);
3518 isl_ast_build_free(build);
3519 return NULL;
3522 /* Internal data structure used by isl_ast_build_ast_from_schedule.
3523 * internal, executed and build are the inputs to generate_code.
3524 * list collects the output.
3526 struct isl_generate_code_data {
3527 int internal;
3528 isl_union_map *executed;
3529 isl_ast_build *build;
3531 isl_ast_graft_list *list;
3534 /* Given an inverse schedule in terms of the external build schedule, i.e.,
3536 * [E -> S] -> D
3538 * with E the external build schedule and S the additional schedule "space",
3539 * reformulate the inverse schedule in terms of the internal schedule domain,
3540 * i.e., return
3542 * [I -> S] -> D
3544 * We first obtain a mapping
3546 * I -> E
3548 * take the inverse and the product with S -> S, resulting in
3550 * [I -> S] -> [E -> S]
3552 * Applying the map to the input produces the desired result.
3554 static __isl_give isl_union_map *internal_executed(
3555 __isl_take isl_union_map *executed, __isl_keep isl_space *space,
3556 __isl_keep isl_ast_build *build)
3558 isl_map *id, *proj;
3560 proj = isl_ast_build_get_schedule_map(build);
3561 proj = isl_map_reverse(proj);
3562 space = isl_space_map_from_set(isl_space_copy(space));
3563 id = isl_map_identity(space);
3564 proj = isl_map_product(proj, id);
3565 executed = isl_union_map_apply_domain(executed,
3566 isl_union_map_from_map(proj));
3567 return executed;
3570 /* Generate an AST that visits the elements in the range of data->executed
3571 * in the relative order specified by the corresponding domain element(s)
3572 * for those domain elements that belong to "set".
3573 * Add the result to data->list.
3575 * The caller ensures that "set" is a universe domain.
3576 * "space" is the space of the additional part of the schedule.
3577 * It is equal to the space of "set" if build->domain is parametric.
3578 * Otherwise, it is equal to the range of the wrapped space of "set".
3580 * If the build space is not parametric and if isl_ast_build_ast_from_schedule
3581 * was called from an outside user (data->internal not set), then
3582 * the (inverse) schedule refers to the external build domain and needs to
3583 * be transformed to refer to the internal build domain.
3585 * The build is extended to include the additional part of the schedule.
3586 * If the original build space was not parametric, then the options
3587 * in data->build refer only to the additional part of the schedule
3588 * and they need to be adjusted to refer to the complete AST build
3589 * domain.
3591 * After having adjusted inverse schedule and build, we start generating
3592 * code with the outer loop of the current code generation
3593 * in generate_next_level.
3595 * If the original build space was not parametric, we undo the embedding
3596 * on the resulting isl_ast_node_list so that it can be used within
3597 * the outer AST build.
3599 static int generate_code_in_space(struct isl_generate_code_data *data,
3600 __isl_take isl_set *set, __isl_take isl_space *space)
3602 isl_union_map *executed;
3603 isl_ast_build *build;
3604 isl_ast_graft_list *list;
3605 int embed;
3607 executed = isl_union_map_copy(data->executed);
3608 executed = isl_union_map_intersect_domain(executed,
3609 isl_union_set_from_set(set));
3611 embed = !isl_set_is_params(data->build->domain);
3612 if (embed && !data->internal)
3613 executed = internal_executed(executed, space, data->build);
3615 build = isl_ast_build_copy(data->build);
3616 build = isl_ast_build_product(build, space);
3618 list = generate_next_level(executed, build);
3620 list = isl_ast_graft_list_unembed(list, embed);
3622 data->list = isl_ast_graft_list_concat(data->list, list);
3624 return 0;
3627 /* Generate an AST that visits the elements in the range of data->executed
3628 * in the relative order specified by the corresponding domain element(s)
3629 * for those domain elements that belong to "set".
3630 * Add the result to data->list.
3632 * The caller ensures that "set" is a universe domain.
3634 * If the build space S is not parametric, then the space of "set"
3635 * need to be a wrapped relation with S as domain. That is, it needs
3636 * to be of the form
3638 * [S -> T]
3640 * Check this property and pass control to generate_code_in_space
3641 * passing along T.
3642 * If the build space is not parametric, then T is the space of "set".
3644 static int generate_code_set(__isl_take isl_set *set, void *user)
3646 struct isl_generate_code_data *data = user;
3647 isl_space *space, *build_space;
3648 int is_domain;
3650 space = isl_set_get_space(set);
3652 if (isl_set_is_params(data->build->domain))
3653 return generate_code_in_space(data, set, space);
3655 build_space = isl_ast_build_get_space(data->build, data->internal);
3656 space = isl_space_unwrap(space);
3657 is_domain = isl_space_is_domain(build_space, space);
3658 isl_space_free(build_space);
3659 space = isl_space_range(space);
3661 if (is_domain < 0)
3662 goto error;
3663 if (!is_domain)
3664 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3665 "invalid nested schedule space", goto error);
3667 return generate_code_in_space(data, set, space);
3668 error:
3669 isl_set_free(set);
3670 isl_space_free(space);
3671 return -1;
3674 /* Generate an AST that visits the elements in the range of "executed"
3675 * in the relative order specified by the corresponding domain element(s).
3677 * "build" is an isl_ast_build that has either been constructed by
3678 * isl_ast_build_from_context or passed to a callback set by
3679 * isl_ast_build_set_create_leaf.
3680 * In the first case, the space of the isl_ast_build is typically
3681 * a parametric space, although this is currently not enforced.
3682 * In the second case, the space is never a parametric space.
3683 * If the space S is not parametric, then the domain space(s) of "executed"
3684 * need to be wrapped relations with S as domain.
3686 * If the domain of "executed" consists of several spaces, then an AST
3687 * is generated for each of them (in arbitrary order) and the results
3688 * are concatenated.
3690 * If "internal" is set, then the domain "S" above refers to the internal
3691 * schedule domain representation. Otherwise, it refers to the external
3692 * representation, as returned by isl_ast_build_get_schedule_space.
3694 * We essentially run over all the spaces in the domain of "executed"
3695 * and call generate_code_set on each of them.
3697 static __isl_give isl_ast_graft_list *generate_code(
3698 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3699 int internal)
3701 isl_ctx *ctx;
3702 struct isl_generate_code_data data = { 0 };
3703 isl_space *space;
3704 isl_union_set *schedule_domain;
3705 isl_union_map *universe;
3707 if (!build)
3708 goto error;
3709 space = isl_ast_build_get_space(build, 1);
3710 space = isl_space_align_params(space,
3711 isl_union_map_get_space(executed));
3712 space = isl_space_align_params(space,
3713 isl_union_map_get_space(build->options));
3714 build = isl_ast_build_align_params(build, isl_space_copy(space));
3715 executed = isl_union_map_align_params(executed, space);
3716 if (!executed || !build)
3717 goto error;
3719 ctx = isl_ast_build_get_ctx(build);
3721 data.internal = internal;
3722 data.executed = executed;
3723 data.build = build;
3724 data.list = isl_ast_graft_list_alloc(ctx, 0);
3726 universe = isl_union_map_universe(isl_union_map_copy(executed));
3727 schedule_domain = isl_union_map_domain(universe);
3728 if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
3729 &data) < 0)
3730 data.list = isl_ast_graft_list_free(data.list);
3732 isl_union_set_free(schedule_domain);
3733 isl_union_map_free(executed);
3735 isl_ast_build_free(build);
3736 return data.list;
3737 error:
3738 isl_union_map_free(executed);
3739 isl_ast_build_free(build);
3740 return NULL;
3743 /* Generate an AST that visits the elements in the domain of "schedule"
3744 * in the relative order specified by the corresponding image element(s).
3746 * "build" is an isl_ast_build that has either been constructed by
3747 * isl_ast_build_from_context or passed to a callback set by
3748 * isl_ast_build_set_create_leaf.
3749 * In the first case, the space of the isl_ast_build is typically
3750 * a parametric space, although this is currently not enforced.
3751 * In the second case, the space is never a parametric space.
3752 * If the space S is not parametric, then the range space(s) of "schedule"
3753 * need to be wrapped relations with S as domain.
3755 * If the range of "schedule" consists of several spaces, then an AST
3756 * is generated for each of them (in arbitrary order) and the results
3757 * are concatenated.
3759 * We first initialize the local copies of the relevant options.
3760 * We do this here rather than when the isl_ast_build is created
3761 * because the options may have changed between the construction
3762 * of the isl_ast_build and the call to isl_generate_code.
3764 * The main computation is performed on an inverse schedule (with
3765 * the schedule domain in the domain and the elements to be executed
3766 * in the range) called "executed".
3768 __isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
3769 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
3771 isl_ast_graft_list *list;
3772 isl_ast_node *node;
3773 isl_union_map *executed;
3775 build = isl_ast_build_copy(build);
3776 build = isl_ast_build_set_single_valued(build, 0);
3777 schedule = isl_union_map_coalesce(schedule);
3778 executed = isl_union_map_reverse(schedule);
3779 list = generate_code(executed, isl_ast_build_copy(build), 0);
3780 node = isl_ast_node_from_graft_list(list, build);
3781 isl_ast_build_free(build);
3783 return node;