isl_ast_codegen.c: create_node_scaled: hoist inclusion of stride guard
[isl.git] / isl_ast_codegen.c
blob4dc89152f67dd9013990ae772099e3703d4033e5
1 /*
2 * Copyright 2012-2014 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
8 */
10 #include <limits.h>
11 #include <isl/aff.h>
12 #include <isl/set.h>
13 #include <isl/ilp.h>
14 #include <isl/union_map.h>
15 #include <isl_sort.h>
16 #include <isl_tarjan.h>
17 #include <isl_ast_private.h>
18 #include <isl_ast_build_expr.h>
19 #include <isl_ast_build_private.h>
20 #include <isl_ast_graft_private.h>
22 /* Data used in generate_domain.
24 * "build" is the input build.
25 * "list" collects the results.
27 struct isl_generate_domain_data {
28 isl_ast_build *build;
30 isl_ast_graft_list *list;
33 static __isl_give isl_ast_graft_list *generate_next_level(
34 __isl_take isl_union_map *executed,
35 __isl_take isl_ast_build *build);
36 static __isl_give isl_ast_graft_list *generate_code(
37 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
38 int internal);
40 /* Generate an AST for a single domain based on
41 * the (non single valued) inverse schedule "executed".
43 * We extend the schedule with the iteration domain
44 * and continue generating through a call to generate_code.
46 * In particular, if executed has the form
48 * S -> D
50 * then we continue generating code on
52 * [S -> D] -> D
54 * The extended inverse schedule is clearly single valued
55 * ensuring that the nested generate_code will not reach this function,
56 * but will instead create calls to all elements of D that need
57 * to be executed from the current schedule domain.
59 static int generate_non_single_valued(__isl_take isl_map *executed,
60 struct isl_generate_domain_data *data)
62 isl_map *identity;
63 isl_ast_build *build;
64 isl_ast_graft_list *list;
66 build = isl_ast_build_copy(data->build);
68 identity = isl_set_identity(isl_map_range(isl_map_copy(executed)));
69 executed = isl_map_domain_product(executed, identity);
70 build = isl_ast_build_set_single_valued(build, 1);
72 list = generate_code(isl_union_map_from_map(executed), build, 1);
74 data->list = isl_ast_graft_list_concat(data->list, list);
76 return 0;
79 /* Call the at_each_domain callback, if requested by the user,
80 * after recording the current inverse schedule in the build.
82 static __isl_give isl_ast_graft *at_each_domain(__isl_take isl_ast_graft *graft,
83 __isl_keep isl_map *executed, __isl_keep isl_ast_build *build)
85 if (!graft || !build)
86 return isl_ast_graft_free(graft);
87 if (!build->at_each_domain)
88 return graft;
90 build = isl_ast_build_copy(build);
91 build = isl_ast_build_set_executed(build,
92 isl_union_map_from_map(isl_map_copy(executed)));
93 if (!build)
94 return isl_ast_graft_free(graft);
96 graft->node = build->at_each_domain(graft->node,
97 build, build->at_each_domain_user);
98 isl_ast_build_free(build);
100 if (!graft->node)
101 graft = isl_ast_graft_free(graft);
103 return graft;
106 /* Generate an AST for a single domain based on
107 * the inverse schedule "executed" and add it to data->list.
109 * If there is more than one domain element associated to the current
110 * schedule "time", then we need to continue the generation process
111 * in generate_non_single_valued.
112 * Note that the inverse schedule being single-valued may depend
113 * on constraints that are only available in the original context
114 * domain specified by the user. We therefore first introduce
115 * the constraints from data->build->domain.
116 * On the other hand, we only perform the test after having taken the gist
117 * of the domain as the resulting map is the one from which the call
118 * expression is constructed. Using this map to construct the call
119 * expression usually yields simpler results.
120 * Because we perform the single-valuedness test on the gisted map,
121 * we may in rare cases fail to recognize that the inverse schedule
122 * is single-valued. This becomes problematic if this happens
123 * from the recursive call through generate_non_single_valued
124 * as we would then end up in an infinite recursion.
125 * We therefore check if we are inside a call to generate_non_single_valued
126 * and revert to the ungisted map if the gisted map turns out not to be
127 * single-valued.
129 * Otherwise, we generate a call expression for the single executed
130 * domain element and put a guard around it based on the (simplified)
131 * domain of "executed".
133 * If the user has set an at_each_domain callback, it is called
134 * on the constructed call expression node.
136 static int generate_domain(__isl_take isl_map *executed, void *user)
138 struct isl_generate_domain_data *data = user;
139 isl_ast_graft *graft;
140 isl_ast_graft_list *list;
141 isl_set *guard;
142 isl_map *map = NULL;
143 int empty, sv;
145 executed = isl_map_intersect_domain(executed,
146 isl_set_copy(data->build->domain));
147 empty = isl_map_is_empty(executed);
148 if (empty < 0)
149 goto error;
150 if (empty) {
151 isl_map_free(executed);
152 return 0;
155 executed = isl_map_coalesce(executed);
156 map = isl_map_copy(executed);
157 map = isl_ast_build_compute_gist_map_domain(data->build, map);
158 sv = isl_map_is_single_valued(map);
159 if (sv < 0)
160 goto error;
161 if (!sv) {
162 isl_map_free(map);
163 if (data->build->single_valued)
164 map = isl_map_copy(executed);
165 else
166 return generate_non_single_valued(executed, data);
168 guard = isl_map_domain(isl_map_copy(map));
169 guard = isl_set_compute_divs(guard);
170 guard = isl_set_coalesce(guard);
171 guard = isl_ast_build_compute_gist(data->build, guard);
172 graft = isl_ast_graft_alloc_domain(map, data->build);
173 graft = at_each_domain(graft, executed, data->build);
175 isl_map_free(executed);
176 graft = isl_ast_graft_add_guard(graft, guard, data->build);
178 list = isl_ast_graft_list_from_ast_graft(graft);
179 data->list = isl_ast_graft_list_concat(data->list, list);
181 return 0;
182 error:
183 isl_map_free(map);
184 isl_map_free(executed);
185 return -1;
188 /* Call build->create_leaf to a create "leaf" node in the AST,
189 * encapsulate the result in an isl_ast_graft and return the result
190 * as a 1-element list.
192 * Note that the node returned by the user may be an entire tree.
194 * Before we pass control to the user, we first clear some information
195 * from the build that is (presumbably) only meaningful
196 * for the current code generation.
197 * This includes the create_leaf callback itself, so we make a copy
198 * of the build first.
200 static __isl_give isl_ast_graft_list *call_create_leaf(
201 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
203 isl_ast_node *node;
204 isl_ast_graft *graft;
205 isl_ast_build *user_build;
207 user_build = isl_ast_build_copy(build);
208 user_build = isl_ast_build_set_executed(user_build, executed);
209 user_build = isl_ast_build_clear_local_info(user_build);
210 if (!user_build)
211 node = NULL;
212 else
213 node = build->create_leaf(user_build, build->create_leaf_user);
214 graft = isl_ast_graft_alloc(node, build);
215 isl_ast_build_free(build);
216 return isl_ast_graft_list_from_ast_graft(graft);
219 /* Generate an AST after having handled the complete schedule
220 * of this call to the code generator.
222 * If the user has specified a create_leaf callback, control
223 * is passed to the user in call_create_leaf.
225 * Otherwise, we generate one or more calls for each individual
226 * domain in generate_domain.
228 static __isl_give isl_ast_graft_list *generate_inner_level(
229 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
231 isl_ctx *ctx;
232 struct isl_generate_domain_data data = { build };
234 if (!build || !executed)
235 goto error;
237 if (build->create_leaf)
238 return call_create_leaf(executed, build);
240 ctx = isl_union_map_get_ctx(executed);
241 data.list = isl_ast_graft_list_alloc(ctx, 0);
242 if (isl_union_map_foreach_map(executed, &generate_domain, &data) < 0)
243 data.list = isl_ast_graft_list_free(data.list);
245 if (0)
246 error: data.list = NULL;
247 isl_ast_build_free(build);
248 isl_union_map_free(executed);
249 return data.list;
252 /* Call the before_each_for callback, if requested by the user.
254 static __isl_give isl_ast_node *before_each_for(__isl_take isl_ast_node *node,
255 __isl_keep isl_ast_build *build)
257 isl_id *id;
259 if (!node || !build)
260 return isl_ast_node_free(node);
261 if (!build->before_each_for)
262 return node;
263 id = build->before_each_for(build, build->before_each_for_user);
264 node = isl_ast_node_set_annotation(node, id);
265 return node;
268 /* Call the after_each_for callback, if requested by the user.
270 static __isl_give isl_ast_graft *after_each_for(__isl_take isl_ast_graft *graft,
271 __isl_keep isl_ast_build *build)
273 if (!graft || !build)
274 return isl_ast_graft_free(graft);
275 if (!build->after_each_for)
276 return graft;
277 graft->node = build->after_each_for(graft->node, build,
278 build->after_each_for_user);
279 if (!graft->node)
280 return isl_ast_graft_free(graft);
281 return graft;
284 /* Plug in all the know values of the current and outer dimensions
285 * in the domain of "executed". In principle, we only need to plug
286 * in the known value of the current dimension since the values of
287 * outer dimensions have been plugged in already.
288 * However, it turns out to be easier to just plug in all known values.
290 static __isl_give isl_union_map *plug_in_values(
291 __isl_take isl_union_map *executed, __isl_keep isl_ast_build *build)
293 return isl_ast_build_substitute_values_union_map_domain(build,
294 executed);
297 /* Check if the constraint "c" is a lower bound on dimension "pos",
298 * an upper bound, or independent of dimension "pos".
300 static int constraint_type(isl_constraint *c, int pos)
302 if (isl_constraint_is_lower_bound(c, isl_dim_set, pos))
303 return 1;
304 if (isl_constraint_is_upper_bound(c, isl_dim_set, pos))
305 return 2;
306 return 0;
309 /* Compare the types of the constraints "a" and "b",
310 * resulting in constraints that are independent of "depth"
311 * to be sorted before the lower bounds on "depth", which in
312 * turn are sorted before the upper bounds on "depth".
314 static int cmp_constraint(__isl_keep isl_constraint *a,
315 __isl_keep isl_constraint *b, void *user)
317 int *depth = user;
318 int t1 = constraint_type(a, *depth);
319 int t2 = constraint_type(b, *depth);
321 return t1 - t2;
324 /* Extract a lower bound on dimension "pos" from constraint "c".
326 * If the constraint is of the form
328 * a x + f(...) >= 0
330 * then we essentially return
332 * l = ceil(-f(...)/a)
334 * However, if the current dimension is strided, then we need to make
335 * sure that the lower bound we construct is of the form
337 * f + s a
339 * with f the offset and s the stride.
340 * We therefore compute
342 * f + s * ceil((l - f)/s)
344 static __isl_give isl_aff *lower_bound(__isl_keep isl_constraint *c,
345 int pos, __isl_keep isl_ast_build *build)
347 isl_aff *aff;
349 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
350 aff = isl_aff_ceil(aff);
352 if (isl_ast_build_has_stride(build, pos)) {
353 isl_aff *offset;
354 isl_val *stride;
356 offset = isl_ast_build_get_offset(build, pos);
357 stride = isl_ast_build_get_stride(build, pos);
359 aff = isl_aff_sub(aff, isl_aff_copy(offset));
360 aff = isl_aff_scale_down_val(aff, isl_val_copy(stride));
361 aff = isl_aff_ceil(aff);
362 aff = isl_aff_scale_val(aff, stride);
363 aff = isl_aff_add(aff, offset);
366 aff = isl_ast_build_compute_gist_aff(build, aff);
368 return aff;
371 /* Return the exact lower bound (or upper bound if "upper" is set)
372 * of "domain" as a piecewise affine expression.
374 * If we are computing a lower bound (of a strided dimension), then
375 * we need to make sure it is of the form
377 * f + s a
379 * where f is the offset and s is the stride.
380 * We therefore need to include the stride constraint before computing
381 * the minimum.
383 static __isl_give isl_pw_aff *exact_bound(__isl_keep isl_set *domain,
384 __isl_keep isl_ast_build *build, int upper)
386 isl_set *stride;
387 isl_map *it_map;
388 isl_pw_aff *pa;
389 isl_pw_multi_aff *pma;
391 domain = isl_set_copy(domain);
392 if (!upper) {
393 stride = isl_ast_build_get_stride_constraint(build);
394 domain = isl_set_intersect(domain, stride);
396 it_map = isl_ast_build_map_to_iterator(build, domain);
397 if (upper)
398 pma = isl_map_lexmax_pw_multi_aff(it_map);
399 else
400 pma = isl_map_lexmin_pw_multi_aff(it_map);
401 pa = isl_pw_multi_aff_get_pw_aff(pma, 0);
402 isl_pw_multi_aff_free(pma);
403 pa = isl_ast_build_compute_gist_pw_aff(build, pa);
404 pa = isl_pw_aff_coalesce(pa);
406 return pa;
409 /* Extract a lower bound on dimension "pos" from each constraint
410 * in "constraints" and return the list of lower bounds.
411 * If "constraints" has zero elements, then we extract a lower bound
412 * from "domain" instead.
414 static __isl_give isl_pw_aff_list *lower_bounds(
415 __isl_keep isl_constraint_list *constraints, int pos,
416 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
418 isl_ctx *ctx;
419 isl_pw_aff_list *list;
420 int i, n;
422 if (!build)
423 return NULL;
425 n = isl_constraint_list_n_constraint(constraints);
426 if (n == 0) {
427 isl_pw_aff *pa;
428 pa = exact_bound(domain, build, 0);
429 return isl_pw_aff_list_from_pw_aff(pa);
432 ctx = isl_ast_build_get_ctx(build);
433 list = isl_pw_aff_list_alloc(ctx,n);
435 for (i = 0; i < n; ++i) {
436 isl_aff *aff;
437 isl_constraint *c;
439 c = isl_constraint_list_get_constraint(constraints, i);
440 aff = lower_bound(c, pos, build);
441 isl_constraint_free(c);
442 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
445 return list;
448 /* Extract an upper bound on dimension "pos" from each constraint
449 * in "constraints" and return the list of upper bounds.
450 * If "constraints" has zero elements, then we extract an upper bound
451 * from "domain" instead.
453 static __isl_give isl_pw_aff_list *upper_bounds(
454 __isl_keep isl_constraint_list *constraints, int pos,
455 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
457 isl_ctx *ctx;
458 isl_pw_aff_list *list;
459 int i, n;
461 n = isl_constraint_list_n_constraint(constraints);
462 if (n == 0) {
463 isl_pw_aff *pa;
464 pa = exact_bound(domain, build, 1);
465 return isl_pw_aff_list_from_pw_aff(pa);
468 ctx = isl_ast_build_get_ctx(build);
469 list = isl_pw_aff_list_alloc(ctx,n);
471 for (i = 0; i < n; ++i) {
472 isl_aff *aff;
473 isl_constraint *c;
475 c = isl_constraint_list_get_constraint(constraints, i);
476 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
477 isl_constraint_free(c);
478 aff = isl_aff_floor(aff);
479 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
482 return list;
485 /* Callback for sorting the isl_pw_aff_list passed to reduce_list.
487 static int reduce_list_cmp(__isl_keep isl_pw_aff *a, __isl_keep isl_pw_aff *b,
488 void *user)
490 return isl_pw_aff_plain_cmp(a, b);
493 /* Return an isl_ast_expr that performs the reduction of type "type"
494 * on AST expressions corresponding to the elements in "list".
496 * The list is assumed to contain at least one element.
497 * If the list contains exactly one element, then the returned isl_ast_expr
498 * simply computes that affine expression.
499 * If the list contains more than one element, then we sort it
500 * using a fairly abitrary but hopefully reasonably stable order.
502 static __isl_give isl_ast_expr *reduce_list(enum isl_ast_op_type type,
503 __isl_keep isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
505 int i, n;
506 isl_ctx *ctx;
507 isl_ast_expr *expr;
509 if (!list)
510 return NULL;
512 n = isl_pw_aff_list_n_pw_aff(list);
514 if (n == 1)
515 return isl_ast_build_expr_from_pw_aff_internal(build,
516 isl_pw_aff_list_get_pw_aff(list, 0));
518 ctx = isl_pw_aff_list_get_ctx(list);
519 expr = isl_ast_expr_alloc_op(ctx, type, n);
520 if (!expr)
521 return NULL;
523 list = isl_pw_aff_list_copy(list);
524 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
525 if (!list)
526 return isl_ast_expr_free(expr);
528 for (i = 0; i < n; ++i) {
529 isl_ast_expr *expr_i;
531 expr_i = isl_ast_build_expr_from_pw_aff_internal(build,
532 isl_pw_aff_list_get_pw_aff(list, i));
533 if (!expr_i)
534 goto error;
535 expr->u.op.args[i] = expr_i;
538 isl_pw_aff_list_free(list);
539 return expr;
540 error:
541 isl_pw_aff_list_free(list);
542 isl_ast_expr_free(expr);
543 return NULL;
546 /* Add a guard to "graft" based on "bound" in the case of a degenerate
547 * level (including the special case of an eliminated level).
549 * We eliminate the current dimension, simplify the result in the current
550 * build and add the result as guards to the graft.
552 * Note that we cannot simply drop the constraints on the current dimension
553 * even in the eliminated case, because the single affine expression may
554 * not be explicitly available in "bounds". Moreover, the single affine
555 * expression may only be defined on a subset of the build domain,
556 * so we do in some cases need to insert a guard even in the eliminated case.
558 static __isl_give isl_ast_graft *add_degenerate_guard(
559 __isl_take isl_ast_graft *graft, __isl_keep isl_basic_set *bounds,
560 __isl_keep isl_ast_build *build)
562 int depth;
563 isl_set *dom;
565 depth = isl_ast_build_get_depth(build);
567 dom = isl_set_from_basic_set(isl_basic_set_copy(bounds));
568 dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
569 dom = isl_ast_build_compute_gist(build, dom);
571 graft = isl_ast_graft_add_guard(graft, dom, build);
573 return graft;
576 /* Add the guard implied by the current stride constraint (if any),
577 * but not (necessarily) enforced by the generated AST to "graft".
579 static __isl_give isl_ast_graft *add_stride_guard(
580 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
582 int depth;
583 isl_set *dom;
585 depth = isl_ast_build_get_depth(build);
586 if (!isl_ast_build_has_stride(build, depth))
587 return graft;
589 dom = isl_ast_build_get_stride_constraint(build);
590 dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
591 dom = isl_ast_build_compute_gist(build, dom);
593 graft = isl_ast_graft_add_guard(graft, dom, build);
595 return graft;
598 /* Update "graft" based on "bounds" for the eliminated case.
600 * In the eliminated case, no for node is created, so we only need
601 * to check if "bounds" imply any guards that need to be inserted.
603 static __isl_give isl_ast_graft *refine_eliminated(
604 __isl_take isl_ast_graft *graft, __isl_keep isl_basic_set *bounds,
605 __isl_keep isl_ast_build *build)
607 return add_degenerate_guard(graft, bounds, build);
610 /* Update "graft" based on "bounds" and "sub_build" for the degenerate case.
612 * "build" is the build in which graft->node was created
613 * "sub_build" contains information about the current level itself,
614 * including the single value attained.
616 * We first set the initialization part of the for loop to the single
617 * value attained by the current dimension.
618 * The increment and condition are not strictly needed as the are known
619 * to be "1" and "iterator <= value" respectively.
620 * Then we check if "bounds" imply any guards that need to be inserted.
622 static __isl_give isl_ast_graft *refine_degenerate(
623 __isl_take isl_ast_graft *graft, __isl_keep isl_basic_set *bounds,
624 __isl_keep isl_ast_build *build,
625 __isl_keep isl_ast_build *sub_build)
627 isl_pw_aff *value;
629 if (!graft || !sub_build)
630 return isl_ast_graft_free(graft);
632 value = isl_pw_aff_copy(sub_build->value);
634 graft->node->u.f.init = isl_ast_build_expr_from_pw_aff_internal(build,
635 value);
636 if (!graft->node->u.f.init)
637 return isl_ast_graft_free(graft);
639 graft = add_degenerate_guard(graft, bounds, build);
641 return graft;
644 /* Return the intersection of constraints in "list" as a set.
646 static __isl_give isl_set *intersect_constraints(
647 __isl_keep isl_constraint_list *list)
649 int i, n;
650 isl_basic_set *bset;
652 n = isl_constraint_list_n_constraint(list);
653 if (n < 1)
654 isl_die(isl_constraint_list_get_ctx(list), isl_error_internal,
655 "expecting at least one constraint", return NULL);
657 bset = isl_basic_set_from_constraint(
658 isl_constraint_list_get_constraint(list, 0));
659 for (i = 1; i < n; ++i) {
660 isl_basic_set *bset_i;
662 bset_i = isl_basic_set_from_constraint(
663 isl_constraint_list_get_constraint(list, i));
664 bset = isl_basic_set_intersect(bset, bset_i);
667 return isl_set_from_basic_set(bset);
670 /* Compute the constraints on the outer dimensions enforced by
671 * graft->node and add those constraints to graft->enforced,
672 * in case the upper bound is expressed as a set "upper".
674 * In particular, if l(...) is a lower bound in "lower", and
676 * -a i + f(...) >= 0 or a i <= f(...)
678 * is an upper bound ocnstraint on the current dimension i,
679 * then the for loop enforces the constraint
681 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
683 * We therefore simply take each lower bound in turn, plug it into
684 * the upper bounds and compute the intersection over all lower bounds.
686 * If a lower bound is a rational expression, then
687 * isl_basic_set_preimage_multi_aff will force this rational
688 * expression to have only integer values. However, the loop
689 * itself does not enforce this integrality constraint. We therefore
690 * use the ceil of the lower bounds instead of the lower bounds themselves.
691 * Other constraints will make sure that the for loop is only executed
692 * when each of the lower bounds attains an integral value.
693 * In particular, potentially rational values only occur in
694 * lower_bound if the offset is a (seemingly) rational expression,
695 * but then outer conditions will make sure that this rational expression
696 * only attains integer values.
698 static __isl_give isl_ast_graft *set_enforced_from_set(
699 __isl_take isl_ast_graft *graft,
700 __isl_keep isl_pw_aff_list *lower, int pos, __isl_keep isl_set *upper)
702 isl_space *space;
703 isl_basic_set *enforced;
704 isl_pw_multi_aff *pma;
705 int i, n;
707 if (!graft || !lower)
708 return isl_ast_graft_free(graft);
710 space = isl_set_get_space(upper);
711 enforced = isl_basic_set_universe(isl_space_copy(space));
713 space = isl_space_map_from_set(space);
714 pma = isl_pw_multi_aff_identity(space);
716 n = isl_pw_aff_list_n_pw_aff(lower);
717 for (i = 0; i < n; ++i) {
718 isl_pw_aff *pa;
719 isl_set *enforced_i;
720 isl_basic_set *hull;
721 isl_pw_multi_aff *pma_i;
723 pa = isl_pw_aff_list_get_pw_aff(lower, i);
724 pa = isl_pw_aff_ceil(pa);
725 pma_i = isl_pw_multi_aff_copy(pma);
726 pma_i = isl_pw_multi_aff_set_pw_aff(pma_i, pos, pa);
727 enforced_i = isl_set_copy(upper);
728 enforced_i = isl_set_preimage_pw_multi_aff(enforced_i, pma_i);
729 hull = isl_set_simple_hull(enforced_i);
730 enforced = isl_basic_set_intersect(enforced, hull);
733 isl_pw_multi_aff_free(pma);
735 graft = isl_ast_graft_enforce(graft, enforced);
737 return graft;
740 /* Compute the constraints on the outer dimensions enforced by
741 * graft->node and add those constraints to graft->enforced,
742 * in case the upper bound is expressed as
743 * a list of affine expressions "upper".
745 * The enforced condition is that each lower bound expression is less
746 * than or equal to each upper bound expression.
748 static __isl_give isl_ast_graft *set_enforced_from_list(
749 __isl_take isl_ast_graft *graft,
750 __isl_keep isl_pw_aff_list *lower, __isl_keep isl_pw_aff_list *upper)
752 isl_set *cond;
753 isl_basic_set *enforced;
755 lower = isl_pw_aff_list_copy(lower);
756 upper = isl_pw_aff_list_copy(upper);
757 cond = isl_pw_aff_list_le_set(lower, upper);
758 enforced = isl_set_simple_hull(cond);
759 graft = isl_ast_graft_enforce(graft, enforced);
761 return graft;
764 /* Does "aff" have a negative constant term?
766 static int aff_constant_is_negative(__isl_take isl_set *set,
767 __isl_take isl_aff *aff, void *user)
769 int *neg = user;
770 isl_val *v;
772 v = isl_aff_get_constant_val(aff);
773 *neg = isl_val_is_neg(v);
774 isl_val_free(v);
775 isl_set_free(set);
776 isl_aff_free(aff);
778 return *neg ? 0 : -1;
781 /* Does "pa" have a negative constant term over its entire domain?
783 static int pw_aff_constant_is_negative(__isl_take isl_pw_aff *pa, void *user)
785 int r;
786 int *neg = user;
788 r = isl_pw_aff_foreach_piece(pa, &aff_constant_is_negative, user);
789 isl_pw_aff_free(pa);
791 return *neg ? 0 : -1;
794 /* Does each element in "list" have a negative constant term?
796 * The callback terminates the iteration as soon an element has been
797 * found that does not have a negative constant term.
799 static int list_constant_is_negative(__isl_keep isl_pw_aff_list *list)
801 int neg = 1;
803 if (isl_pw_aff_list_foreach(list,
804 &pw_aff_constant_is_negative, &neg) < 0 && neg)
805 return -1;
807 return neg;
810 /* Add 1 to each of the elements in "list", where each of these elements
811 * is defined over the internal schedule space of "build".
813 static __isl_give isl_pw_aff_list *list_add_one(
814 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
816 int i, n;
817 isl_space *space;
818 isl_aff *aff;
819 isl_pw_aff *one;
821 space = isl_ast_build_get_space(build, 1);
822 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
823 aff = isl_aff_add_constant_si(aff, 1);
824 one = isl_pw_aff_from_aff(aff);
826 n = isl_pw_aff_list_n_pw_aff(list);
827 for (i = 0; i < n; ++i) {
828 isl_pw_aff *pa;
829 pa = isl_pw_aff_list_get_pw_aff(list, i);
830 pa = isl_pw_aff_add(pa, isl_pw_aff_copy(one));
831 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
834 isl_pw_aff_free(one);
836 return list;
839 /* Set the condition part of the for node graft->node in case
840 * the upper bound is represented as a list of piecewise affine expressions.
842 * In particular, set the condition to
844 * iterator <= min(list of upper bounds)
846 * If each of the upper bounds has a negative constant term, then
847 * set the condition to
849 * iterator < min(list of (upper bound + 1)s)
852 static __isl_give isl_ast_graft *set_for_cond_from_list(
853 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *list,
854 __isl_keep isl_ast_build *build)
856 int neg;
857 isl_ast_expr *bound, *iterator, *cond;
858 enum isl_ast_op_type type = isl_ast_op_le;
860 if (!graft || !list)
861 return isl_ast_graft_free(graft);
863 neg = list_constant_is_negative(list);
864 if (neg < 0)
865 return isl_ast_graft_free(graft);
866 list = isl_pw_aff_list_copy(list);
867 if (neg) {
868 list = list_add_one(list, build);
869 type = isl_ast_op_lt;
872 bound = reduce_list(isl_ast_op_min, list, build);
873 iterator = isl_ast_expr_copy(graft->node->u.f.iterator);
874 cond = isl_ast_expr_alloc_binary(type, iterator, bound);
875 graft->node->u.f.cond = cond;
877 isl_pw_aff_list_free(list);
878 if (!graft->node->u.f.cond)
879 return isl_ast_graft_free(graft);
880 return graft;
883 /* Set the condition part of the for node graft->node in case
884 * the upper bound is represented as a set.
886 static __isl_give isl_ast_graft *set_for_cond_from_set(
887 __isl_take isl_ast_graft *graft, __isl_keep isl_set *set,
888 __isl_keep isl_ast_build *build)
890 isl_ast_expr *cond;
892 if (!graft)
893 return NULL;
895 cond = isl_ast_build_expr_from_set(build, isl_set_copy(set));
896 graft->node->u.f.cond = cond;
897 if (!graft->node->u.f.cond)
898 return isl_ast_graft_free(graft);
899 return graft;
902 /* Construct an isl_ast_expr for the increment (i.e., stride) of
903 * the current dimension.
905 static __isl_give isl_ast_expr *for_inc(__isl_keep isl_ast_build *build)
907 int depth;
908 isl_val *v;
909 isl_ctx *ctx;
911 if (!build)
912 return NULL;
913 ctx = isl_ast_build_get_ctx(build);
914 depth = isl_ast_build_get_depth(build);
916 if (!isl_ast_build_has_stride(build, depth))
917 return isl_ast_expr_alloc_int_si(ctx, 1);
919 v = isl_ast_build_get_stride(build, depth);
920 return isl_ast_expr_from_val(v);
923 /* Should we express the loop condition as
925 * iterator <= min(list of upper bounds)
927 * or as a conjunction of constraints?
929 * The first is constructed from a list of upper bounds.
930 * The second is constructed from a set.
932 * If there are no upper bounds in "constraints", then this could mean
933 * that "domain" simply doesn't have an upper bound or that we didn't
934 * pick any upper bound. In the first case, we want to generate the
935 * loop condition as a(n empty) conjunction of constraints
936 * In the second case, we will compute
937 * a single upper bound from "domain" and so we use the list form.
939 * If there are upper bounds in "constraints",
940 * then we use the list form iff the atomic_upper_bound option is set.
942 static int use_upper_bound_list(isl_ctx *ctx, int n_upper,
943 __isl_keep isl_set *domain, int depth)
945 if (n_upper > 0)
946 return isl_options_get_ast_build_atomic_upper_bound(ctx);
947 else
948 return isl_set_dim_has_upper_bound(domain, isl_dim_set, depth);
951 /* Fill in the expressions of the for node in graft->node.
953 * In particular,
954 * - set the initialization part of the loop to the maximum of the lower bounds
955 * - extract the increment from the stride of the current dimension
956 * - construct the for condition either based on a list of upper bounds
957 * or on a set of upper bound constraints.
959 static __isl_give isl_ast_graft *set_for_node_expressions(
960 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *lower,
961 int use_list, __isl_keep isl_pw_aff_list *upper_list,
962 __isl_keep isl_set *upper_set, __isl_keep isl_ast_build *build)
964 isl_ast_node *node;
966 if (!graft)
967 return NULL;
969 build = isl_ast_build_copy(build);
970 build = isl_ast_build_set_enforced(build,
971 isl_ast_graft_get_enforced(graft));
973 node = graft->node;
974 node->u.f.init = reduce_list(isl_ast_op_max, lower, build);
975 node->u.f.inc = for_inc(build);
977 if (use_list)
978 graft = set_for_cond_from_list(graft, upper_list, build);
979 else
980 graft = set_for_cond_from_set(graft, upper_set, build);
982 isl_ast_build_free(build);
984 if (!node->u.f.iterator || !node->u.f.init ||
985 !node->u.f.cond || !node->u.f.inc)
986 return isl_ast_graft_free(graft);
988 return graft;
991 /* Update "graft" based on "bounds" and "domain" for the generic,
992 * non-degenerate, case.
994 * "c_lower" and "c_upper" contain the lower and upper bounds
995 * that the loop node should express.
996 * "domain" is the subset of the intersection of the constraints
997 * for which some code is executed.
999 * There may be zero lower bounds or zero upper bounds in "constraints"
1000 * in case the list of constraints was created
1001 * based on the atomic option or based on separation with explicit bounds.
1002 * In that case, we use "domain" to derive lower and/or upper bounds.
1004 * We first compute a list of one or more lower bounds.
1006 * Then we decide if we want to express the condition as
1008 * iterator <= min(list of upper bounds)
1010 * or as a conjunction of constraints.
1012 * The set of enforced constraints is then computed either based on
1013 * a list of upper bounds or on a set of upper bound constraints.
1014 * We do not compute any enforced constraints if we were forced
1015 * to compute a lower or upper bound using exact_bound. The domains
1016 * of the resulting expressions may imply some bounds on outer dimensions
1017 * that we do not want to appear in the enforced constraints since
1018 * they are not actually enforced by the corresponding code.
1020 * Finally, we fill in the expressions of the for node.
1022 static __isl_give isl_ast_graft *refine_generic_bounds(
1023 __isl_take isl_ast_graft *graft,
1024 __isl_take isl_constraint_list *c_lower,
1025 __isl_take isl_constraint_list *c_upper,
1026 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1028 int depth;
1029 isl_ctx *ctx;
1030 isl_pw_aff_list *lower;
1031 int use_list;
1032 isl_set *upper_set = NULL;
1033 isl_pw_aff_list *upper_list = NULL;
1034 int n_lower, n_upper;
1036 if (!graft || !c_lower || !c_upper || !build)
1037 goto error;
1039 depth = isl_ast_build_get_depth(build);
1040 ctx = isl_ast_graft_get_ctx(graft);
1042 n_lower = isl_constraint_list_n_constraint(c_lower);
1043 n_upper = isl_constraint_list_n_constraint(c_upper);
1045 use_list = use_upper_bound_list(ctx, n_upper, domain, depth);
1047 lower = lower_bounds(c_lower, depth, domain, build);
1049 if (use_list)
1050 upper_list = upper_bounds(c_upper, depth, domain, build);
1051 else if (n_upper > 0)
1052 upper_set = intersect_constraints(c_upper);
1053 else
1054 upper_set = isl_set_universe(isl_set_get_space(domain));
1056 if (n_lower == 0 || n_upper == 0)
1058 else if (use_list)
1059 graft = set_enforced_from_list(graft, lower, upper_list);
1060 else
1061 graft = set_enforced_from_set(graft, lower, depth, upper_set);
1063 graft = set_for_node_expressions(graft, lower, use_list, upper_list,
1064 upper_set, build);
1066 isl_pw_aff_list_free(lower);
1067 isl_pw_aff_list_free(upper_list);
1068 isl_set_free(upper_set);
1069 isl_constraint_list_free(c_lower);
1070 isl_constraint_list_free(c_upper);
1072 return graft;
1073 error:
1074 isl_constraint_list_free(c_lower);
1075 isl_constraint_list_free(c_upper);
1076 return isl_ast_graft_free(graft);
1079 /* Internal data structure used inside count_constraints to keep
1080 * track of the number of constraints that are independent of dimension "pos",
1081 * the lower bounds in "pos" and the upper bounds in "pos".
1083 struct isl_ast_count_constraints_data {
1084 int pos;
1086 int n_indep;
1087 int n_lower;
1088 int n_upper;
1091 /* Increment data->n_indep, data->lower or data->upper depending
1092 * on whether "c" is independenct of dimensions data->pos,
1093 * a lower bound or an upper bound.
1095 static int count_constraints(__isl_take isl_constraint *c, void *user)
1097 struct isl_ast_count_constraints_data *data = user;
1099 if (isl_constraint_is_lower_bound(c, isl_dim_set, data->pos))
1100 data->n_lower++;
1101 else if (isl_constraint_is_upper_bound(c, isl_dim_set, data->pos))
1102 data->n_upper++;
1103 else
1104 data->n_indep++;
1106 isl_constraint_free(c);
1108 return 0;
1111 /* Update "graft" based on "bounds" and "domain" for the generic,
1112 * non-degenerate, case.
1114 * "list" respresent the list of bounds that need to be encoded by
1115 * the for loop (or a guard around the for loop).
1116 * "domain" is the subset of the intersection of the constraints
1117 * for which some code is executed.
1118 * "build" is the build in which graft->node was created.
1120 * We separate lower bounds, upper bounds and constraints that
1121 * are independent of the loop iterator.
1123 * The actual for loop bounds are generated in refine_generic_bounds.
1124 * If there are any constraints that are independent of the loop iterator,
1125 * we need to put a guard around the for loop (which may get hoisted up
1126 * to higher levels) and we call refine_generic_bounds in a build
1127 * where this guard is enforced.
1129 static __isl_give isl_ast_graft *refine_generic_split(
1130 __isl_take isl_ast_graft *graft, __isl_take isl_constraint_list *list,
1131 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1133 isl_ast_build *for_build;
1134 isl_set *guard;
1135 struct isl_ast_count_constraints_data data;
1136 isl_constraint_list *lower;
1137 isl_constraint_list *upper;
1139 if (!list)
1140 return isl_ast_graft_free(graft);
1142 data.pos = isl_ast_build_get_depth(build);
1144 list = isl_constraint_list_sort(list, &cmp_constraint, &data.pos);
1145 if (!list)
1146 return isl_ast_graft_free(graft);
1148 data.n_indep = data.n_lower = data.n_upper = 0;
1149 if (isl_constraint_list_foreach(list, &count_constraints, &data) < 0) {
1150 isl_constraint_list_free(list);
1151 return isl_ast_graft_free(graft);
1154 lower = isl_constraint_list_copy(list);
1155 lower = isl_constraint_list_drop(lower, 0, data.n_indep);
1156 upper = isl_constraint_list_copy(lower);
1157 lower = isl_constraint_list_drop(lower, data.n_lower, data.n_upper);
1158 upper = isl_constraint_list_drop(upper, 0, data.n_lower);
1160 if (data.n_indep == 0) {
1161 isl_constraint_list_free(list);
1162 return refine_generic_bounds(graft, lower, upper,
1163 domain, build);
1166 list = isl_constraint_list_drop(list, data.n_indep,
1167 data.n_lower + data.n_upper);
1168 guard = intersect_constraints(list);
1169 isl_constraint_list_free(list);
1171 for_build = isl_ast_build_copy(build);
1172 for_build = isl_ast_build_restrict_pending(for_build,
1173 isl_set_copy(guard));
1174 graft = refine_generic_bounds(graft, lower, upper, domain, for_build);
1175 isl_ast_build_free(for_build);
1177 graft = isl_ast_graft_add_guard(graft, guard, build);
1179 return graft;
1182 /* Update "graft" based on "bounds" and "domain" for the generic,
1183 * non-degenerate, case.
1185 * "bounds" respresent the bounds that need to be encoded by
1186 * the for loop (or a guard around the for loop).
1187 * "domain" is the subset of "bounds" for which some code is executed.
1188 * "build" is the build in which graft->node was created.
1190 * We break up "bounds" into a list of constraints and continue with
1191 * refine_generic_split.
1193 static __isl_give isl_ast_graft *refine_generic(
1194 __isl_take isl_ast_graft *graft,
1195 __isl_keep isl_basic_set *bounds, __isl_keep isl_set *domain,
1196 __isl_keep isl_ast_build *build)
1198 isl_constraint_list *list;
1200 if (!build || !graft)
1201 return isl_ast_graft_free(graft);
1203 bounds = isl_basic_set_copy(bounds);
1204 bounds = isl_ast_build_compute_gist_basic_set(build, bounds);
1205 list = isl_basic_set_get_constraint_list(bounds);
1206 isl_basic_set_free(bounds);
1208 graft = refine_generic_split(graft, list, domain, build);
1210 return graft;
1213 /* Create a for node for the current level.
1215 * Mark the for node degenerate if "degenerate" is set.
1217 static __isl_give isl_ast_node *create_for(__isl_keep isl_ast_build *build,
1218 int degenerate)
1220 int depth;
1221 isl_id *id;
1222 isl_ast_node *node;
1224 if (!build)
1225 return NULL;
1227 depth = isl_ast_build_get_depth(build);
1228 id = isl_ast_build_get_iterator_id(build, depth);
1229 node = isl_ast_node_alloc_for(id);
1230 if (degenerate)
1231 node = isl_ast_node_for_mark_degenerate(node);
1233 return node;
1236 /* If the ast_build_exploit_nested_bounds option is set, then return
1237 * the constraints enforced by all elements in "list".
1238 * Otherwise, return the universe.
1240 static __isl_give isl_basic_set *extract_shared_enforced(
1241 __isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
1243 isl_ctx *ctx;
1244 isl_space *space;
1246 if (!list)
1247 return NULL;
1249 ctx = isl_ast_graft_list_get_ctx(list);
1250 if (isl_options_get_ast_build_exploit_nested_bounds(ctx))
1251 return isl_ast_graft_list_extract_shared_enforced(list, build);
1253 space = isl_ast_build_get_space(build, 1);
1254 return isl_basic_set_universe(space);
1257 /* Create an AST node for the current dimension based on
1258 * the schedule domain "bounds" and return the node encapsulated
1259 * in an isl_ast_graft.
1261 * "executed" is the current inverse schedule, taking into account
1262 * the bounds in "bounds"
1263 * "domain" is the domain of "executed", with inner dimensions projected out.
1264 * It may be a strict subset of "bounds" in case "bounds" was created
1265 * based on the atomic option or based on separation with explicit bounds.
1267 * "domain" may satisfy additional equalities that result
1268 * from intersecting "executed" with "bounds" in add_node.
1269 * It may also satisfy some global constraints that were dropped out because
1270 * we performed separation with explicit bounds.
1271 * The very first step is then to copy these constraints to "bounds".
1273 * Since we may be calling before_each_for and after_each_for
1274 * callbacks, we record the current inverse schedule in the build.
1276 * We consider three builds,
1277 * "build" is the one in which the current level is created,
1278 * "body_build" is the build in which the next level is created,
1279 * "sub_build" is essentially the same as "body_build", except that
1280 * the depth has not been increased yet.
1282 * "build" already contains information (in strides and offsets)
1283 * about the strides at the current level, but this information is not
1284 * reflected in the build->domain.
1285 * We first add this information and the "bounds" to the sub_build->domain.
1286 * isl_ast_build_set_loop_bounds adds the stride information and
1287 * checks whether the current dimension attains
1288 * only a single value and whether this single value can be represented using
1289 * a single affine expression.
1290 * In the first case, the current level is considered "degenerate".
1291 * In the second, sub-case, the current level is considered "eliminated".
1292 * Eliminated levels don't need to be reflected in the AST since we can
1293 * simply plug in the affine expression. For degenerate, but non-eliminated,
1294 * levels, we do introduce a for node, but mark is as degenerate so that
1295 * it can be printed as an assignment of the single value to the loop
1296 * "iterator".
1298 * If the current level is eliminated, we explicitly plug in the value
1299 * for the current level found by isl_ast_build_set_loop_bounds in the
1300 * inverse schedule. This ensures that if we are working on a slice
1301 * of the domain based on information available in the inverse schedule
1302 * and the build domain, that then this information is also reflected
1303 * in the inverse schedule. This operation also eliminates the current
1304 * dimension from the inverse schedule making sure no inner dimensions depend
1305 * on the current dimension. Otherwise, we create a for node, marking
1306 * it degenerate if appropriate. The initial for node is still incomplete
1307 * and will be completed in either refine_degenerate or refine_generic.
1309 * We then generate a sequence of grafts for the next level,
1310 * create a surrounding graft for the current level and insert
1311 * the for node we created (if the current level is not eliminated).
1312 * Before creating a graft for the current level, we first extract
1313 * hoistable constraints from the child guards. These constraints
1314 * are used to simplify the child guards and then added to the guard
1315 * of the current graft.
1317 * Finally, we set the bounds of the for loop and insert guards
1318 * (either in the AST or in the graft) in one of
1319 * refine_eliminated, refine_degenerate or refine_generic.
1321 static __isl_give isl_ast_graft *create_node_scaled(
1322 __isl_take isl_union_map *executed,
1323 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1324 __isl_take isl_ast_build *build)
1326 int depth;
1327 int degenerate, eliminated;
1328 isl_basic_set *hull;
1329 isl_basic_set *enforced;
1330 isl_set *hoisted;
1331 isl_ast_node *node = NULL;
1332 isl_ast_graft *graft;
1333 isl_ast_graft_list *children;
1334 isl_ast_build *sub_build;
1335 isl_ast_build *body_build;
1337 domain = isl_ast_build_eliminate_divs(build, domain);
1338 domain = isl_set_detect_equalities(domain);
1339 hull = isl_set_unshifted_simple_hull(isl_set_copy(domain));
1340 bounds = isl_basic_set_intersect(bounds, hull);
1341 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
1343 depth = isl_ast_build_get_depth(build);
1344 sub_build = isl_ast_build_copy(build);
1345 sub_build = isl_ast_build_set_loop_bounds(sub_build,
1346 isl_basic_set_copy(bounds));
1347 degenerate = isl_ast_build_has_value(sub_build);
1348 eliminated = isl_ast_build_has_affine_value(sub_build, depth);
1349 if (degenerate < 0 || eliminated < 0)
1350 executed = isl_union_map_free(executed);
1351 if (eliminated)
1352 executed = plug_in_values(executed, sub_build);
1353 else
1354 node = create_for(build, degenerate);
1356 body_build = isl_ast_build_copy(sub_build);
1357 body_build = isl_ast_build_increase_depth(body_build);
1358 if (!eliminated)
1359 node = before_each_for(node, body_build);
1360 children = generate_next_level(executed,
1361 isl_ast_build_copy(body_build));
1363 enforced = extract_shared_enforced(children, build);
1364 hoisted = isl_ast_graft_list_extract_hoistable_guard(children, build);
1365 graft = isl_ast_graft_alloc_from_children(children, hoisted, enforced,
1366 build, sub_build);
1367 if (!eliminated)
1368 graft = isl_ast_graft_insert_for(graft, node);
1369 if (eliminated)
1370 graft = refine_eliminated(graft, bounds, build);
1371 else if (degenerate)
1372 graft = refine_degenerate(graft, bounds, build, sub_build);
1373 else
1374 graft = refine_generic(graft, bounds, domain, build);
1375 if (!eliminated) {
1376 graft = add_stride_guard(graft, build);
1377 graft = after_each_for(graft, body_build);
1380 isl_ast_build_free(body_build);
1381 isl_ast_build_free(sub_build);
1382 isl_ast_build_free(build);
1383 isl_basic_set_free(bounds);
1384 isl_set_free(domain);
1386 return graft;
1389 /* Internal data structure for checking if all constraints involving
1390 * the input dimension "depth" are such that the other coefficients
1391 * are multiples of "m", reducing "m" if they are not.
1392 * If "m" is reduced all the way down to "1", then the check has failed
1393 * and we break out of the iteration.
1395 struct isl_check_scaled_data {
1396 int depth;
1397 isl_val *m;
1400 /* If constraint "c" involves the input dimension data->depth,
1401 * then make sure that all the other coefficients are multiples of data->m,
1402 * reducing data->m if needed.
1403 * Break out of the iteration if data->m has become equal to "1".
1405 static int constraint_check_scaled(__isl_take isl_constraint *c, void *user)
1407 struct isl_check_scaled_data *data = user;
1408 int i, j, n;
1409 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_out,
1410 isl_dim_div };
1412 if (!isl_constraint_involves_dims(c, isl_dim_in, data->depth, 1)) {
1413 isl_constraint_free(c);
1414 return 0;
1417 for (i = 0; i < 4; ++i) {
1418 n = isl_constraint_dim(c, t[i]);
1419 for (j = 0; j < n; ++j) {
1420 isl_val *d;
1422 if (t[i] == isl_dim_in && j == data->depth)
1423 continue;
1424 if (!isl_constraint_involves_dims(c, t[i], j, 1))
1425 continue;
1426 d = isl_constraint_get_coefficient_val(c, t[i], j);
1427 data->m = isl_val_gcd(data->m, d);
1428 if (isl_val_is_one(data->m))
1429 break;
1431 if (j < n)
1432 break;
1435 isl_constraint_free(c);
1437 return i < 4 ? -1 : 0;
1440 /* For each constraint of "bmap" that involves the input dimension data->depth,
1441 * make sure that all the other coefficients are multiples of data->m,
1442 * reducing data->m if needed.
1443 * Break out of the iteration if data->m has become equal to "1".
1445 static int basic_map_check_scaled(__isl_take isl_basic_map *bmap, void *user)
1447 int r;
1449 r = isl_basic_map_foreach_constraint(bmap,
1450 &constraint_check_scaled, user);
1451 isl_basic_map_free(bmap);
1453 return r;
1456 /* For each constraint of "map" that involves the input dimension data->depth,
1457 * make sure that all the other coefficients are multiples of data->m,
1458 * reducing data->m if needed.
1459 * Break out of the iteration if data->m has become equal to "1".
1461 static int map_check_scaled(__isl_take isl_map *map, void *user)
1463 int r;
1465 r = isl_map_foreach_basic_map(map, &basic_map_check_scaled, user);
1466 isl_map_free(map);
1468 return r;
1471 /* Create an AST node for the current dimension based on
1472 * the schedule domain "bounds" and return the node encapsulated
1473 * in an isl_ast_graft.
1475 * "executed" is the current inverse schedule, taking into account
1476 * the bounds in "bounds"
1477 * "domain" is the domain of "executed", with inner dimensions projected out.
1480 * Before moving on to the actual AST node construction in create_node_scaled,
1481 * we first check if the current dimension is strided and if we can scale
1482 * down this stride. Note that we only do this if the ast_build_scale_strides
1483 * option is set.
1485 * In particular, let the current dimension take on values
1487 * f + s a
1489 * with a an integer. We check if we can find an integer m that (obviously)
1490 * divides both f and s.
1492 * If so, we check if the current dimension only appears in constraints
1493 * where the coefficients of the other variables are multiples of m.
1494 * We perform this extra check to avoid the risk of introducing
1495 * divisions by scaling down the current dimension.
1497 * If so, we scale the current dimension down by a factor of m.
1498 * That is, we plug in
1500 * i = m i' (1)
1502 * Note that in principle we could always scale down strided loops
1503 * by plugging in
1505 * i = f + s i'
1507 * but this may result in i' taking on larger values than the original i,
1508 * due to the shift by "f".
1509 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1511 static __isl_give isl_ast_graft *create_node(__isl_take isl_union_map *executed,
1512 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1513 __isl_take isl_ast_build *build)
1515 struct isl_check_scaled_data data;
1516 isl_ctx *ctx;
1517 isl_aff *offset;
1518 isl_val *d;
1520 ctx = isl_ast_build_get_ctx(build);
1521 if (!isl_options_get_ast_build_scale_strides(ctx))
1522 return create_node_scaled(executed, bounds, domain, build);
1524 data.depth = isl_ast_build_get_depth(build);
1525 if (!isl_ast_build_has_stride(build, data.depth))
1526 return create_node_scaled(executed, bounds, domain, build);
1528 offset = isl_ast_build_get_offset(build, data.depth);
1529 data.m = isl_ast_build_get_stride(build, data.depth);
1530 if (!data.m)
1531 offset = isl_aff_free(offset);
1532 offset = isl_aff_scale_down_val(offset, isl_val_copy(data.m));
1533 d = isl_aff_get_denominator_val(offset);
1534 if (!d)
1535 executed = isl_union_map_free(executed);
1537 if (executed && isl_val_is_divisible_by(data.m, d))
1538 data.m = isl_val_div(data.m, d);
1539 else {
1540 data.m = isl_val_set_si(data.m, 1);
1541 isl_val_free(d);
1544 if (!isl_val_is_one(data.m)) {
1545 if (isl_union_map_foreach_map(executed, &map_check_scaled,
1546 &data) < 0 &&
1547 !isl_val_is_one(data.m))
1548 executed = isl_union_map_free(executed);
1551 if (!isl_val_is_one(data.m)) {
1552 isl_space *space;
1553 isl_multi_aff *ma;
1554 isl_aff *aff;
1555 isl_map *map;
1556 isl_union_map *umap;
1558 space = isl_ast_build_get_space(build, 1);
1559 space = isl_space_map_from_set(space);
1560 ma = isl_multi_aff_identity(space);
1561 aff = isl_multi_aff_get_aff(ma, data.depth);
1562 aff = isl_aff_scale_val(aff, isl_val_copy(data.m));
1563 ma = isl_multi_aff_set_aff(ma, data.depth, aff);
1565 bounds = isl_basic_set_preimage_multi_aff(bounds,
1566 isl_multi_aff_copy(ma));
1567 domain = isl_set_preimage_multi_aff(domain,
1568 isl_multi_aff_copy(ma));
1569 map = isl_map_reverse(isl_map_from_multi_aff(ma));
1570 umap = isl_union_map_from_map(map);
1571 executed = isl_union_map_apply_domain(executed,
1572 isl_union_map_copy(umap));
1573 build = isl_ast_build_scale_down(build, isl_val_copy(data.m),
1574 umap);
1576 isl_aff_free(offset);
1577 isl_val_free(data.m);
1579 return create_node_scaled(executed, bounds, domain, build);
1582 /* Add the basic set to the list that "user" points to.
1584 static int collect_basic_set(__isl_take isl_basic_set *bset, void *user)
1586 isl_basic_set_list **list = user;
1588 *list = isl_basic_set_list_add(*list, bset);
1590 return 0;
1593 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1595 static __isl_give isl_basic_set_list *isl_basic_set_list_from_set(
1596 __isl_take isl_set *set)
1598 int n;
1599 isl_ctx *ctx;
1600 isl_basic_set_list *list;
1602 if (!set)
1603 return NULL;
1605 ctx = isl_set_get_ctx(set);
1607 n = isl_set_n_basic_set(set);
1608 list = isl_basic_set_list_alloc(ctx, n);
1609 if (isl_set_foreach_basic_set(set, &collect_basic_set, &list) < 0)
1610 list = isl_basic_set_list_free(list);
1612 isl_set_free(set);
1613 return list;
1616 /* Generate code for the schedule domain "bounds"
1617 * and add the result to "list".
1619 * We mainly detect strides here and check if the bounds do not
1620 * conflict with the current build domain
1621 * and then pass over control to create_node.
1623 * "bounds" reflects the bounds on the current dimension and possibly
1624 * some extra conditions on outer dimensions.
1625 * It does not, however, include any divs involving the current dimension,
1626 * so it does not capture any stride constraints.
1627 * We therefore need to compute that part of the schedule domain that
1628 * intersects with "bounds" and derive the strides from the result.
1630 static __isl_give isl_ast_graft_list *add_node(
1631 __isl_take isl_ast_graft_list *list, __isl_take isl_union_map *executed,
1632 __isl_take isl_basic_set *bounds, __isl_take isl_ast_build *build)
1634 isl_ast_graft *graft;
1635 isl_set *domain = NULL;
1636 isl_union_set *uset;
1637 int empty, disjoint;
1639 uset = isl_union_set_from_basic_set(isl_basic_set_copy(bounds));
1640 executed = isl_union_map_intersect_domain(executed, uset);
1641 empty = isl_union_map_is_empty(executed);
1642 if (empty < 0)
1643 goto error;
1644 if (empty)
1645 goto done;
1647 uset = isl_union_map_domain(isl_union_map_copy(executed));
1648 domain = isl_set_from_union_set(uset);
1649 domain = isl_ast_build_specialize(build, domain);
1651 domain = isl_set_compute_divs(domain);
1652 domain = isl_ast_build_eliminate_inner(build, domain);
1653 disjoint = isl_set_is_disjoint(domain, build->domain);
1654 if (disjoint < 0)
1655 goto error;
1656 if (disjoint)
1657 goto done;
1659 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
1661 graft = create_node(executed, bounds, domain,
1662 isl_ast_build_copy(build));
1663 list = isl_ast_graft_list_add(list, graft);
1664 isl_ast_build_free(build);
1665 return list;
1666 error:
1667 list = isl_ast_graft_list_free(list);
1668 done:
1669 isl_set_free(domain);
1670 isl_basic_set_free(bounds);
1671 isl_union_map_free(executed);
1672 isl_ast_build_free(build);
1673 return list;
1676 /* Does any element of i follow or coincide with any element of j
1677 * at the current depth for equal values of the outer dimensions?
1679 static int domain_follows_at_depth(__isl_keep isl_basic_set *i,
1680 __isl_keep isl_basic_set *j, void *user)
1682 int depth = *(int *) user;
1683 isl_basic_map *test;
1684 int empty;
1685 int l;
1687 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1688 isl_basic_set_copy(j));
1689 for (l = 0; l < depth; ++l)
1690 test = isl_basic_map_equate(test, isl_dim_in, l,
1691 isl_dim_out, l);
1692 test = isl_basic_map_order_ge(test, isl_dim_in, depth,
1693 isl_dim_out, depth);
1694 empty = isl_basic_map_is_empty(test);
1695 isl_basic_map_free(test);
1697 return empty < 0 ? -1 : !empty;
1700 /* Split up each element of "list" into a part that is related to "bset"
1701 * according to "gt" and a part that is not.
1702 * Return a list that consist of "bset" and all the pieces.
1704 static __isl_give isl_basic_set_list *add_split_on(
1705 __isl_take isl_basic_set_list *list, __isl_take isl_basic_set *bset,
1706 __isl_keep isl_basic_map *gt)
1708 int i, n;
1709 isl_basic_set_list *res;
1711 if (!list)
1712 bset = isl_basic_set_free(bset);
1714 gt = isl_basic_map_copy(gt);
1715 gt = isl_basic_map_intersect_domain(gt, isl_basic_set_copy(bset));
1716 n = isl_basic_set_list_n_basic_set(list);
1717 res = isl_basic_set_list_from_basic_set(bset);
1718 for (i = 0; res && i < n; ++i) {
1719 isl_basic_set *bset;
1720 isl_set *set1, *set2;
1721 isl_basic_map *bmap;
1722 int empty;
1724 bset = isl_basic_set_list_get_basic_set(list, i);
1725 bmap = isl_basic_map_copy(gt);
1726 bmap = isl_basic_map_intersect_range(bmap, bset);
1727 bset = isl_basic_map_range(bmap);
1728 empty = isl_basic_set_is_empty(bset);
1729 if (empty < 0)
1730 res = isl_basic_set_list_free(res);
1731 if (empty) {
1732 isl_basic_set_free(bset);
1733 bset = isl_basic_set_list_get_basic_set(list, i);
1734 res = isl_basic_set_list_add(res, bset);
1735 continue;
1738 res = isl_basic_set_list_add(res, isl_basic_set_copy(bset));
1739 set1 = isl_set_from_basic_set(bset);
1740 bset = isl_basic_set_list_get_basic_set(list, i);
1741 set2 = isl_set_from_basic_set(bset);
1742 set1 = isl_set_subtract(set2, set1);
1743 set1 = isl_set_make_disjoint(set1);
1745 res = isl_basic_set_list_concat(res,
1746 isl_basic_set_list_from_set(set1));
1748 isl_basic_map_free(gt);
1749 isl_basic_set_list_free(list);
1750 return res;
1753 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1754 __isl_keep isl_basic_set_list *domain_list,
1755 __isl_keep isl_union_map *executed,
1756 __isl_keep isl_ast_build *build);
1758 /* Internal data structure for add_nodes.
1760 * "executed" and "build" are extra arguments to be passed to add_node.
1761 * "list" collects the results.
1763 struct isl_add_nodes_data {
1764 isl_union_map *executed;
1765 isl_ast_build *build;
1767 isl_ast_graft_list *list;
1770 /* Generate code for the schedule domains in "scc"
1771 * and add the results to "list".
1773 * The domains in "scc" form a strongly connected component in the ordering.
1774 * If the number of domains in "scc" is larger than 1, then this means
1775 * that we cannot determine a valid ordering for the domains in the component.
1776 * This should be fairly rare because the individual domains
1777 * have been made disjoint first.
1778 * The problem is that the domains may be integrally disjoint but not
1779 * rationally disjoint. For example, we may have domains
1781 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1783 * These two domains have an empty intersection, but their rational
1784 * relaxations do intersect. It is impossible to order these domains
1785 * in the second dimension because the first should be ordered before
1786 * the second for outer dimension equal to 0, while it should be ordered
1787 * after for outer dimension equal to 1.
1789 * This may happen in particular in case of unrolling since the domain
1790 * of each slice is replaced by its simple hull.
1792 * For each basic set i in "scc" and for each of the following basic sets j,
1793 * we split off that part of the basic set i that shares the outer dimensions
1794 * with j and lies before j in the current dimension.
1795 * We collect all the pieces in a new list that replaces "scc".
1797 * While the elements in "scc" should be disjoint, we double-check
1798 * this property to avoid running into an infinite recursion in case
1799 * they intersect due to some internal error.
1801 static int add_nodes(__isl_take isl_basic_set_list *scc, void *user)
1803 struct isl_add_nodes_data *data = user;
1804 int i, n, depth;
1805 isl_basic_set *bset, *first;
1806 isl_basic_set_list *list;
1807 isl_space *space;
1808 isl_basic_map *gt;
1810 n = isl_basic_set_list_n_basic_set(scc);
1811 bset = isl_basic_set_list_get_basic_set(scc, 0);
1812 if (n == 1) {
1813 isl_basic_set_list_free(scc);
1814 data->list = add_node(data->list,
1815 isl_union_map_copy(data->executed), bset,
1816 isl_ast_build_copy(data->build));
1817 return data->list ? 0 : -1;
1820 depth = isl_ast_build_get_depth(data->build);
1821 space = isl_basic_set_get_space(bset);
1822 space = isl_space_map_from_set(space);
1823 gt = isl_basic_map_universe(space);
1824 for (i = 0; i < depth; ++i)
1825 gt = isl_basic_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
1826 gt = isl_basic_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
1828 first = isl_basic_set_copy(bset);
1829 list = isl_basic_set_list_from_basic_set(bset);
1830 for (i = 1; i < n; ++i) {
1831 int disjoint;
1833 bset = isl_basic_set_list_get_basic_set(scc, i);
1835 disjoint = isl_basic_set_is_disjoint(bset, first);
1836 if (disjoint < 0)
1837 list = isl_basic_set_list_free(list);
1838 else if (!disjoint)
1839 isl_die(isl_basic_set_list_get_ctx(scc),
1840 isl_error_internal,
1841 "basic sets in scc are assumed to be disjoint",
1842 list = isl_basic_set_list_free(list));
1844 list = add_split_on(list, bset, gt);
1846 isl_basic_set_free(first);
1847 isl_basic_map_free(gt);
1848 isl_basic_set_list_free(scc);
1849 scc = list;
1850 data->list = isl_ast_graft_list_concat(data->list,
1851 generate_sorted_domains(scc, data->executed, data->build));
1852 isl_basic_set_list_free(scc);
1854 return data->list ? 0 : -1;
1857 /* Sort the domains in "domain_list" according to the execution order
1858 * at the current depth (for equal values of the outer dimensions),
1859 * generate code for each of them, collecting the results in a list.
1860 * If no code is generated (because the intersection of the inverse schedule
1861 * with the domains turns out to be empty), then an empty list is returned.
1863 * The caller is responsible for ensuring that the basic sets in "domain_list"
1864 * are pair-wise disjoint. It can, however, in principle happen that
1865 * two basic sets should be ordered one way for one value of the outer
1866 * dimensions and the other way for some other value of the outer dimensions.
1867 * We therefore play safe and look for strongly connected components.
1868 * The function add_nodes takes care of handling non-trivial components.
1870 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1871 __isl_keep isl_basic_set_list *domain_list,
1872 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
1874 isl_ctx *ctx;
1875 struct isl_add_nodes_data data;
1876 int depth;
1877 int n;
1879 if (!domain_list)
1880 return NULL;
1882 ctx = isl_basic_set_list_get_ctx(domain_list);
1883 n = isl_basic_set_list_n_basic_set(domain_list);
1884 data.list = isl_ast_graft_list_alloc(ctx, n);
1885 if (n == 0)
1886 return data.list;
1887 if (n == 1)
1888 return add_node(data.list, isl_union_map_copy(executed),
1889 isl_basic_set_list_get_basic_set(domain_list, 0),
1890 isl_ast_build_copy(build));
1892 depth = isl_ast_build_get_depth(build);
1893 data.executed = executed;
1894 data.build = build;
1895 if (isl_basic_set_list_foreach_scc(domain_list,
1896 &domain_follows_at_depth, &depth,
1897 &add_nodes, &data) < 0)
1898 data.list = isl_ast_graft_list_free(data.list);
1900 return data.list;
1903 /* Do i and j share any values for the outer dimensions?
1905 static int shared_outer(__isl_keep isl_basic_set *i,
1906 __isl_keep isl_basic_set *j, void *user)
1908 int depth = *(int *) user;
1909 isl_basic_map *test;
1910 int empty;
1911 int l;
1913 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1914 isl_basic_set_copy(j));
1915 for (l = 0; l < depth; ++l)
1916 test = isl_basic_map_equate(test, isl_dim_in, l,
1917 isl_dim_out, l);
1918 empty = isl_basic_map_is_empty(test);
1919 isl_basic_map_free(test);
1921 return empty < 0 ? -1 : !empty;
1924 /* Internal data structure for generate_sorted_domains_wrap.
1926 * "n" is the total number of basic sets
1927 * "executed" and "build" are extra arguments to be passed
1928 * to generate_sorted_domains.
1930 * "single" is set to 1 by generate_sorted_domains_wrap if there
1931 * is only a single component.
1932 * "list" collects the results.
1934 struct isl_ast_generate_parallel_domains_data {
1935 int n;
1936 isl_union_map *executed;
1937 isl_ast_build *build;
1939 int single;
1940 isl_ast_graft_list *list;
1943 /* Call generate_sorted_domains on "scc", fuse the result into a list
1944 * with either zero or one graft and collect the these single element
1945 * lists into data->list.
1947 * If there is only one component, i.e., if the number of basic sets
1948 * in the current component is equal to the total number of basic sets,
1949 * then data->single is set to 1 and the result of generate_sorted_domains
1950 * is not fused.
1952 static int generate_sorted_domains_wrap(__isl_take isl_basic_set_list *scc,
1953 void *user)
1955 struct isl_ast_generate_parallel_domains_data *data = user;
1956 isl_ast_graft_list *list;
1958 list = generate_sorted_domains(scc, data->executed, data->build);
1959 data->single = isl_basic_set_list_n_basic_set(scc) == data->n;
1960 if (!data->single)
1961 list = isl_ast_graft_list_fuse(list, data->build);
1962 if (!data->list)
1963 data->list = list;
1964 else
1965 data->list = isl_ast_graft_list_concat(data->list, list);
1967 isl_basic_set_list_free(scc);
1968 if (!data->list)
1969 return -1;
1971 return 0;
1974 /* Look for any (weakly connected) components in the "domain_list"
1975 * of domains that share some values of the outer dimensions.
1976 * That is, domains in different components do not share any values
1977 * of the outer dimensions. This means that these components
1978 * can be freely reordered.
1979 * Within each of the components, we sort the domains according
1980 * to the execution order at the current depth.
1982 * If there is more than one component, then generate_sorted_domains_wrap
1983 * fuses the result of each call to generate_sorted_domains
1984 * into a list with either zero or one graft and collects these (at most)
1985 * single element lists into a bigger list. This means that the elements of the
1986 * final list can be freely reordered. In particular, we sort them
1987 * according to an arbitrary but fixed ordering to ease merging of
1988 * graft lists from different components.
1990 static __isl_give isl_ast_graft_list *generate_parallel_domains(
1991 __isl_keep isl_basic_set_list *domain_list,
1992 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
1994 int depth;
1995 struct isl_ast_generate_parallel_domains_data data;
1997 if (!domain_list)
1998 return NULL;
2000 data.n = isl_basic_set_list_n_basic_set(domain_list);
2001 if (data.n <= 1)
2002 return generate_sorted_domains(domain_list, executed, build);
2004 depth = isl_ast_build_get_depth(build);
2005 data.list = NULL;
2006 data.executed = executed;
2007 data.build = build;
2008 data.single = 0;
2009 if (isl_basic_set_list_foreach_scc(domain_list, &shared_outer, &depth,
2010 &generate_sorted_domains_wrap,
2011 &data) < 0)
2012 data.list = isl_ast_graft_list_free(data.list);
2014 if (!data.single)
2015 data.list = isl_ast_graft_list_sort_guard(data.list);
2017 return data.list;
2020 /* Internal data for separate_domain.
2022 * "explicit" is set if we only want to use explicit bounds.
2024 * "domain" collects the separated domains.
2026 struct isl_separate_domain_data {
2027 isl_ast_build *build;
2028 int explicit;
2029 isl_set *domain;
2032 /* Extract implicit bounds on the current dimension for the executed "map".
2034 * The domain of "map" may involve inner dimensions, so we
2035 * need to eliminate them.
2037 static __isl_give isl_set *implicit_bounds(__isl_take isl_map *map,
2038 __isl_keep isl_ast_build *build)
2040 isl_set *domain;
2042 domain = isl_map_domain(map);
2043 domain = isl_ast_build_eliminate(build, domain);
2045 return domain;
2048 /* Extract explicit bounds on the current dimension for the executed "map".
2050 * Rather than eliminating the inner dimensions as in implicit_bounds,
2051 * we simply drop any constraints involving those inner dimensions.
2052 * The idea is that most bounds that are implied by constraints on the
2053 * inner dimensions will be enforced by for loops and not by explicit guards.
2054 * There is then no need to separate along those bounds.
2056 static __isl_give isl_set *explicit_bounds(__isl_take isl_map *map,
2057 __isl_keep isl_ast_build *build)
2059 isl_set *domain;
2060 int depth, dim;
2062 dim = isl_map_dim(map, isl_dim_out);
2063 map = isl_map_drop_constraints_involving_dims(map, isl_dim_out, 0, dim);
2065 domain = isl_map_domain(map);
2066 depth = isl_ast_build_get_depth(build);
2067 dim = isl_set_dim(domain, isl_dim_set);
2068 domain = isl_set_detect_equalities(domain);
2069 domain = isl_set_drop_constraints_involving_dims(domain,
2070 isl_dim_set, depth + 1, dim - (depth + 1));
2071 domain = isl_set_remove_divs_involving_dims(domain,
2072 isl_dim_set, depth, 1);
2073 domain = isl_set_remove_unknown_divs(domain);
2075 return domain;
2078 /* Split data->domain into pieces that intersect with the range of "map"
2079 * and pieces that do not intersect with the range of "map"
2080 * and then add that part of the range of "map" that does not intersect
2081 * with data->domain.
2083 static int separate_domain(__isl_take isl_map *map, void *user)
2085 struct isl_separate_domain_data *data = user;
2086 isl_set *domain;
2087 isl_set *d1, *d2;
2089 if (data->explicit)
2090 domain = explicit_bounds(map, data->build);
2091 else
2092 domain = implicit_bounds(map, data->build);
2094 domain = isl_set_coalesce(domain);
2095 domain = isl_set_make_disjoint(domain);
2096 d1 = isl_set_subtract(isl_set_copy(domain), isl_set_copy(data->domain));
2097 d2 = isl_set_subtract(isl_set_copy(data->domain), isl_set_copy(domain));
2098 data->domain = isl_set_intersect(data->domain, domain);
2099 data->domain = isl_set_union(data->domain, d1);
2100 data->domain = isl_set_union(data->domain, d2);
2102 return 0;
2105 /* Separate the schedule domains of "executed".
2107 * That is, break up the domain of "executed" into basic sets,
2108 * such that for each basic set S, every element in S is associated with
2109 * the same domain spaces.
2111 * "space" is the (single) domain space of "executed".
2113 static __isl_give isl_set *separate_schedule_domains(
2114 __isl_take isl_space *space, __isl_take isl_union_map *executed,
2115 __isl_keep isl_ast_build *build)
2117 struct isl_separate_domain_data data = { build };
2118 isl_ctx *ctx;
2120 ctx = isl_ast_build_get_ctx(build);
2121 data.explicit = isl_options_get_ast_build_separation_bounds(ctx) ==
2122 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT;
2123 data.domain = isl_set_empty(space);
2124 if (isl_union_map_foreach_map(executed, &separate_domain, &data) < 0)
2125 data.domain = isl_set_free(data.domain);
2127 isl_union_map_free(executed);
2128 return data.domain;
2131 /* Temporary data used during the search for a lower bound for unrolling.
2133 * "domain" is the original set for which to find a lower bound
2134 * "depth" is the dimension for which to find a lower boudn
2136 * "lower" is the best lower bound found so far. It is NULL if we have not
2137 * found any yet.
2138 * "n" is the corresponding size. If lower is NULL, then the value of n
2139 * is undefined.
2141 struct isl_find_unroll_data {
2142 isl_set *domain;
2143 int depth;
2145 isl_aff *lower;
2146 int *n;
2149 /* Check if we can use "c" as a lower bound and if it is better than
2150 * any previously found lower bound.
2152 * If "c" does not involve the dimension at the current depth,
2153 * then we cannot use it.
2154 * Otherwise, let "c" be of the form
2156 * i >= f(j)/a
2158 * We compute the maximal value of
2160 * -ceil(f(j)/a)) + i + 1
2162 * over the domain. If there is such a value "n", then we know
2164 * -ceil(f(j)/a)) + i + 1 <= n
2166 * or
2168 * i < ceil(f(j)/a)) + n
2170 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2171 * We just need to check if we have found any lower bound before and
2172 * if the new lower bound is better (smaller n) than the previously found
2173 * lower bounds.
2175 static int update_unrolling_lower_bound(struct isl_find_unroll_data *data,
2176 __isl_keep isl_constraint *c)
2178 isl_aff *aff, *lower;
2179 isl_val *max;
2181 if (!isl_constraint_is_lower_bound(c, isl_dim_set, data->depth))
2182 return 0;
2184 lower = isl_constraint_get_bound(c, isl_dim_set, data->depth);
2185 lower = isl_aff_ceil(lower);
2186 aff = isl_aff_copy(lower);
2187 aff = isl_aff_neg(aff);
2188 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, data->depth, 1);
2189 aff = isl_aff_add_constant_si(aff, 1);
2190 max = isl_set_max_val(data->domain, aff);
2191 isl_aff_free(aff);
2193 if (!max)
2194 goto error;
2195 if (isl_val_is_infty(max)) {
2196 isl_val_free(max);
2197 isl_aff_free(lower);
2198 return 0;
2201 if (isl_val_cmp_si(max, INT_MAX) <= 0 &&
2202 (!data->lower || isl_val_cmp_si(max, *data->n) < 0)) {
2203 isl_aff_free(data->lower);
2204 data->lower = lower;
2205 *data->n = isl_val_get_num_si(max);
2206 } else
2207 isl_aff_free(lower);
2208 isl_val_free(max);
2210 return 1;
2211 error:
2212 isl_aff_free(lower);
2213 return -1;
2216 /* Check if we can use "c" as a lower bound and if it is better than
2217 * any previously found lower bound.
2219 static int constraint_find_unroll(__isl_take isl_constraint *c, void *user)
2221 struct isl_find_unroll_data *data;
2222 int r;
2224 data = (struct isl_find_unroll_data *) user;
2225 r = update_unrolling_lower_bound(data, c);
2226 isl_constraint_free(c);
2228 return r;
2231 /* Look for a lower bound l(i) on the dimension at "depth"
2232 * and a size n such that "domain" is a subset of
2234 * { [i] : l(i) <= i_d < l(i) + n }
2236 * where d is "depth" and l(i) depends only on earlier dimensions.
2237 * Furthermore, try and find a lower bound such that n is as small as possible.
2238 * In particular, "n" needs to be finite.
2240 * Inner dimensions have been eliminated from "domain" by the caller.
2242 * We first construct a collection of lower bounds on the input set
2243 * by computing its simple hull. We then iterate through them,
2244 * discarding those that we cannot use (either because they do not
2245 * involve the dimension at "depth" or because they have no corresponding
2246 * upper bound, meaning that "n" would be unbounded) and pick out the
2247 * best from the remaining ones.
2249 * If we cannot find a suitable lower bound, then we consider that
2250 * to be an error.
2252 static __isl_give isl_aff *find_unroll_lower_bound(__isl_keep isl_set *domain,
2253 int depth, int *n)
2255 struct isl_find_unroll_data data = { domain, depth, NULL, n };
2256 isl_basic_set *hull;
2258 hull = isl_set_simple_hull(isl_set_copy(domain));
2260 if (isl_basic_set_foreach_constraint(hull,
2261 &constraint_find_unroll, &data) < 0)
2262 goto error;
2264 isl_basic_set_free(hull);
2266 if (!data.lower)
2267 isl_die(isl_set_get_ctx(domain), isl_error_invalid,
2268 "cannot find lower bound for unrolling", return NULL);
2270 return data.lower;
2271 error:
2272 isl_basic_set_free(hull);
2273 return isl_aff_free(data.lower);
2276 /* Return the constraint
2278 * i_"depth" = aff + offset
2280 static __isl_give isl_constraint *at_offset(int depth, __isl_keep isl_aff *aff,
2281 int offset)
2283 aff = isl_aff_copy(aff);
2284 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, depth, -1);
2285 aff = isl_aff_add_constant_si(aff, offset);
2286 return isl_equality_from_aff(aff);
2289 /* Data structure for storing the results and the intermediate objects
2290 * of compute_domains.
2292 * "list" is the main result of the function and contains a list
2293 * of disjoint basic sets for which code should be generated.
2295 * "executed" and "build" are inputs to compute_domains.
2296 * "schedule_domain" is the domain of "executed".
2298 * "option" constains the domains at the current depth that should by
2299 * atomic, separated or unrolled. These domains are as specified by
2300 * the user, except that inner dimensions have been eliminated and
2301 * that they have been made pair-wise disjoint.
2303 * "sep_class" contains the user-specified split into separation classes
2304 * specialized to the current depth.
2305 * "done" contains the union of the separation domains that have already
2306 * been handled.
2308 struct isl_codegen_domains {
2309 isl_basic_set_list *list;
2311 isl_union_map *executed;
2312 isl_ast_build *build;
2313 isl_set *schedule_domain;
2315 isl_set *option[3];
2317 isl_map *sep_class;
2318 isl_set *done;
2321 /* Extend domains->list with a list of basic sets, one for each value
2322 * of the current dimension in "domain" and remove the corresponding
2323 * sets from the class domain. Return the updated class domain.
2324 * The divs that involve the current dimension have not been projected out
2325 * from this domain.
2327 * Since we are going to be iterating over the individual values,
2328 * we first check if there are any strides on the current dimension.
2329 * If there is, we rewrite the current dimension i as
2331 * i = stride i' + offset
2333 * and then iterate over individual values of i' instead.
2335 * We then look for a lower bound on i' and a size such that the domain
2336 * is a subset of
2338 * { [j,i'] : l(j) <= i' < l(j) + n }
2340 * and then take slices of the domain at values of i'
2341 * between l(j) and l(j) + n - 1.
2343 * We compute the unshifted simple hull of each slice to ensure that
2344 * we have a single basic set per offset. The slicing constraint
2345 * may get simplified away before the unshifted simple hull is taken
2346 * and may therefore in some rare cases disappear from the result.
2347 * We therefore explicitly add the constraint back after computing
2348 * the unshifted simple hull to ensure that the basic sets
2349 * remain disjoint. The constraints that are dropped by taking the hull
2350 * will be taken into account at the next level, as in the case of the
2351 * atomic option.
2353 * Finally, we map i' back to i and add each basic set to the list.
2354 * Since we may have dropped some constraints, we intersect with
2355 * the class domain again to ensure that each element in the list
2356 * is disjoint from the other class domains.
2358 static __isl_give isl_set *do_unroll(struct isl_codegen_domains *domains,
2359 __isl_take isl_set *domain, __isl_take isl_set *class_domain)
2361 int i, n;
2362 int depth;
2363 isl_ctx *ctx;
2364 isl_aff *lower;
2365 isl_multi_aff *expansion;
2366 isl_basic_map *bmap;
2367 isl_set *unroll_domain;
2368 isl_ast_build *build;
2370 if (!domain)
2371 return isl_set_free(class_domain);
2373 ctx = isl_set_get_ctx(domain);
2374 depth = isl_ast_build_get_depth(domains->build);
2375 build = isl_ast_build_copy(domains->build);
2376 domain = isl_ast_build_eliminate_inner(build, domain);
2377 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
2378 expansion = isl_ast_build_get_stride_expansion(build);
2380 domain = isl_set_preimage_multi_aff(domain,
2381 isl_multi_aff_copy(expansion));
2382 domain = isl_ast_build_eliminate_divs(build, domain);
2384 isl_ast_build_free(build);
2386 lower = find_unroll_lower_bound(domain, depth, &n);
2387 if (!lower)
2388 class_domain = isl_set_free(class_domain);
2390 bmap = isl_basic_map_from_multi_aff(expansion);
2392 unroll_domain = isl_set_empty(isl_set_get_space(domain));
2394 for (i = 0; class_domain && i < n; ++i) {
2395 isl_set *set;
2396 isl_basic_set *bset;
2397 isl_constraint *slice;
2398 isl_basic_set_list *list;
2400 slice = at_offset(depth, lower, i);
2401 set = isl_set_copy(domain);
2402 set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2403 bset = isl_set_unshifted_simple_hull(set);
2404 bset = isl_basic_set_add_constraint(bset, slice);
2405 bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2406 set = isl_set_from_basic_set(bset);
2407 unroll_domain = isl_set_union(unroll_domain, isl_set_copy(set));
2408 set = isl_set_intersect(set, isl_set_copy(class_domain));
2409 set = isl_set_make_disjoint(set);
2410 list = isl_basic_set_list_from_set(set);
2411 domains->list = isl_basic_set_list_concat(domains->list, list);
2414 class_domain = isl_set_subtract(class_domain, unroll_domain);
2416 isl_aff_free(lower);
2417 isl_set_free(domain);
2418 isl_basic_map_free(bmap);
2420 return class_domain;
2423 /* Add domains to domains->list for each individual value of the current
2424 * dimension, for that part of the schedule domain that lies in the
2425 * intersection of the option domain and the class domain.
2426 * Remove the corresponding sets from the class domain and
2427 * return the updated class domain.
2429 * We first break up the unroll option domain into individual pieces
2430 * and then handle each of them separately. The unroll option domain
2431 * has been made disjoint in compute_domains_init_options,
2433 * Note that we actively want to combine different pieces of the
2434 * schedule domain that have the same value at the current dimension.
2435 * We therefore need to break up the unroll option domain before
2436 * intersecting with class and schedule domain, hoping that the
2437 * unroll option domain specified by the user is relatively simple.
2439 static __isl_give isl_set *compute_unroll_domains(
2440 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2442 isl_set *unroll_domain;
2443 isl_basic_set_list *unroll_list;
2444 int i, n;
2445 int empty;
2447 empty = isl_set_is_empty(domains->option[unroll]);
2448 if (empty < 0)
2449 return isl_set_free(class_domain);
2450 if (empty)
2451 return class_domain;
2453 unroll_domain = isl_set_copy(domains->option[unroll]);
2454 unroll_list = isl_basic_set_list_from_set(unroll_domain);
2456 n = isl_basic_set_list_n_basic_set(unroll_list);
2457 for (i = 0; i < n; ++i) {
2458 isl_basic_set *bset;
2460 bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2461 unroll_domain = isl_set_from_basic_set(bset);
2462 unroll_domain = isl_set_intersect(unroll_domain,
2463 isl_set_copy(class_domain));
2464 unroll_domain = isl_set_intersect(unroll_domain,
2465 isl_set_copy(domains->schedule_domain));
2467 empty = isl_set_is_empty(unroll_domain);
2468 if (empty >= 0 && empty) {
2469 isl_set_free(unroll_domain);
2470 continue;
2473 class_domain = do_unroll(domains, unroll_domain, class_domain);
2476 isl_basic_set_list_free(unroll_list);
2478 return class_domain;
2481 /* Try and construct a single basic set that includes the intersection of
2482 * the schedule domain, the atomic option domain and the class domain.
2483 * Add the resulting basic set(s) to domains->list and remove them
2484 * from class_domain. Return the updated class domain.
2486 * We construct a single domain rather than trying to combine
2487 * the schedule domains of individual domains because we are working
2488 * within a single component so that non-overlapping schedule domains
2489 * should already have been separated.
2490 * We do however need to make sure that this single domains is a subset
2491 * of the class domain so that it would not intersect with any other
2492 * class domains. This means that we may end up splitting up the atomic
2493 * domain in case separation classes are being used.
2495 * "domain" is the intersection of the schedule domain and the class domain,
2496 * with inner dimensions projected out.
2498 static __isl_give isl_set *compute_atomic_domain(
2499 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2501 isl_basic_set *bset;
2502 isl_basic_set_list *list;
2503 isl_set *domain, *atomic_domain;
2504 int empty;
2506 domain = isl_set_copy(domains->option[atomic]);
2507 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2508 domain = isl_set_intersect(domain,
2509 isl_set_copy(domains->schedule_domain));
2510 empty = isl_set_is_empty(domain);
2511 if (empty < 0)
2512 class_domain = isl_set_free(class_domain);
2513 if (empty) {
2514 isl_set_free(domain);
2515 return class_domain;
2518 domain = isl_ast_build_eliminate(domains->build, domain);
2519 domain = isl_set_coalesce(domain);
2520 bset = isl_set_unshifted_simple_hull(domain);
2521 domain = isl_set_from_basic_set(bset);
2522 atomic_domain = isl_set_copy(domain);
2523 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2524 class_domain = isl_set_subtract(class_domain, atomic_domain);
2525 domain = isl_set_make_disjoint(domain);
2526 list = isl_basic_set_list_from_set(domain);
2527 domains->list = isl_basic_set_list_concat(domains->list, list);
2529 return class_domain;
2532 /* Split up the schedule domain into uniform basic sets,
2533 * in the sense that each element in a basic set is associated to
2534 * elements of the same domains, and add the result to domains->list.
2535 * Do this for that part of the schedule domain that lies in the
2536 * intersection of "class_domain" and the separate option domain.
2538 * "class_domain" may or may not include the constraints
2539 * of the schedule domain, but this does not make a difference
2540 * since we are going to intersect it with the domain of the inverse schedule.
2541 * If it includes schedule domain constraints, then they may involve
2542 * inner dimensions, but we will eliminate them in separation_domain.
2544 static int compute_separate_domain(struct isl_codegen_domains *domains,
2545 __isl_keep isl_set *class_domain)
2547 isl_space *space;
2548 isl_set *domain;
2549 isl_union_map *executed;
2550 isl_basic_set_list *list;
2551 int empty;
2553 domain = isl_set_copy(domains->option[separate]);
2554 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2555 executed = isl_union_map_copy(domains->executed);
2556 executed = isl_union_map_intersect_domain(executed,
2557 isl_union_set_from_set(domain));
2558 empty = isl_union_map_is_empty(executed);
2559 if (empty < 0 || empty) {
2560 isl_union_map_free(executed);
2561 return empty < 0 ? -1 : 0;
2564 space = isl_set_get_space(class_domain);
2565 domain = separate_schedule_domains(space, executed, domains->build);
2567 list = isl_basic_set_list_from_set(domain);
2568 domains->list = isl_basic_set_list_concat(domains->list, list);
2570 return 0;
2573 /* Split up the domain at the current depth into disjoint
2574 * basic sets for which code should be generated separately
2575 * for the given separation class domain.
2577 * If any separation classes have been defined, then "class_domain"
2578 * is the domain of the current class and does not refer to inner dimensions.
2579 * Otherwise, "class_domain" is the universe domain.
2581 * We first make sure that the class domain is disjoint from
2582 * previously considered class domains.
2584 * The separate domains can be computed directly from the "class_domain".
2586 * The unroll, atomic and remainder domains need the constraints
2587 * from the schedule domain.
2589 * For unrolling, the actual schedule domain is needed (with divs that
2590 * may refer to the current dimension) so that stride detection can be
2591 * performed.
2593 * For atomic and remainder domains, inner dimensions and divs involving
2594 * the current dimensions should be eliminated.
2595 * In case we are working within a separation class, we need to intersect
2596 * the result with the current "class_domain" to ensure that the domains
2597 * are disjoint from those generated from other class domains.
2599 * The domain that has been made atomic may be larger than specified
2600 * by the user since it needs to be representable as a single basic set.
2601 * This possibly larger domain is removed from class_domain by
2602 * compute_atomic_domain. It is computed first so that the extended domain
2603 * would not overlap with any domains computed before.
2604 * Similary, the unrolled domains may have some constraints removed and
2605 * may therefore also be larger than specified by the user.
2607 * If anything is left after handling separate, unroll and atomic,
2608 * we split it up into basic sets and append the basic sets to domains->list.
2610 static int compute_partial_domains(struct isl_codegen_domains *domains,
2611 __isl_take isl_set *class_domain)
2613 isl_basic_set_list *list;
2614 isl_set *domain;
2616 class_domain = isl_set_subtract(class_domain,
2617 isl_set_copy(domains->done));
2618 domains->done = isl_set_union(domains->done,
2619 isl_set_copy(class_domain));
2621 class_domain = compute_atomic_domain(domains, class_domain);
2622 class_domain = compute_unroll_domains(domains, class_domain);
2624 domain = isl_set_copy(class_domain);
2626 if (compute_separate_domain(domains, domain) < 0)
2627 goto error;
2628 domain = isl_set_subtract(domain,
2629 isl_set_copy(domains->option[separate]));
2631 domain = isl_set_intersect(domain,
2632 isl_set_copy(domains->schedule_domain));
2634 domain = isl_ast_build_eliminate(domains->build, domain);
2635 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2637 domain = isl_set_coalesce(domain);
2638 domain = isl_set_make_disjoint(domain);
2640 list = isl_basic_set_list_from_set(domain);
2641 domains->list = isl_basic_set_list_concat(domains->list, list);
2643 isl_set_free(class_domain);
2645 return 0;
2646 error:
2647 isl_set_free(domain);
2648 isl_set_free(class_domain);
2649 return -1;
2652 /* Split up the domain at the current depth into disjoint
2653 * basic sets for which code should be generated separately
2654 * for the separation class identified by "pnt".
2656 * We extract the corresponding class domain from domains->sep_class,
2657 * eliminate inner dimensions and pass control to compute_partial_domains.
2659 static int compute_class_domains(__isl_take isl_point *pnt, void *user)
2661 struct isl_codegen_domains *domains = user;
2662 isl_set *class_set;
2663 isl_set *domain;
2664 int disjoint;
2666 class_set = isl_set_from_point(pnt);
2667 domain = isl_map_domain(isl_map_intersect_range(
2668 isl_map_copy(domains->sep_class), class_set));
2669 domain = isl_ast_build_compute_gist(domains->build, domain);
2670 domain = isl_ast_build_eliminate(domains->build, domain);
2672 disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
2673 if (disjoint < 0)
2674 return -1;
2675 if (disjoint) {
2676 isl_set_free(domain);
2677 return 0;
2680 return compute_partial_domains(domains, domain);
2683 /* Extract the domains at the current depth that should be atomic,
2684 * separated or unrolled and store them in option.
2686 * The domains specified by the user might overlap, so we make
2687 * them disjoint by subtracting earlier domains from later domains.
2689 static void compute_domains_init_options(isl_set *option[3],
2690 __isl_keep isl_ast_build *build)
2692 enum isl_ast_build_domain_type type, type2;
2694 for (type = atomic; type <= separate; ++type) {
2695 option[type] = isl_ast_build_get_option_domain(build, type);
2696 for (type2 = atomic; type2 < type; ++type2)
2697 option[type] = isl_set_subtract(option[type],
2698 isl_set_copy(option[type2]));
2701 option[unroll] = isl_set_coalesce(option[unroll]);
2702 option[unroll] = isl_set_make_disjoint(option[unroll]);
2705 /* Split up the domain at the current depth into disjoint
2706 * basic sets for which code should be generated separately,
2707 * based on the user-specified options.
2708 * Return the list of disjoint basic sets.
2710 * There are three kinds of domains that we need to keep track of.
2711 * - the "schedule domain" is the domain of "executed"
2712 * - the "class domain" is the domain corresponding to the currrent
2713 * separation class
2714 * - the "option domain" is the domain corresponding to one of the options
2715 * atomic, unroll or separate
2717 * We first consider the individial values of the separation classes
2718 * and split up the domain for each of them separately.
2719 * Finally, we consider the remainder. If no separation classes were
2720 * specified, then we call compute_partial_domains with the universe
2721 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
2722 * with inner dimensions removed. We do this because we want to
2723 * avoid computing the complement of the class domains (i.e., the difference
2724 * between the universe and domains->done).
2726 static __isl_give isl_basic_set_list *compute_domains(
2727 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2729 struct isl_codegen_domains domains;
2730 isl_ctx *ctx;
2731 isl_set *domain;
2732 isl_union_set *schedule_domain;
2733 isl_set *classes;
2734 isl_space *space;
2735 int n_param;
2736 enum isl_ast_build_domain_type type;
2737 int empty;
2739 if (!executed)
2740 return NULL;
2742 ctx = isl_union_map_get_ctx(executed);
2743 domains.list = isl_basic_set_list_alloc(ctx, 0);
2745 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
2746 domain = isl_set_from_union_set(schedule_domain);
2748 compute_domains_init_options(domains.option, build);
2750 domains.sep_class = isl_ast_build_get_separation_class(build);
2751 classes = isl_map_range(isl_map_copy(domains.sep_class));
2752 n_param = isl_set_dim(classes, isl_dim_param);
2753 classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
2755 space = isl_set_get_space(domain);
2756 domains.build = build;
2757 domains.schedule_domain = isl_set_copy(domain);
2758 domains.executed = executed;
2759 domains.done = isl_set_empty(space);
2761 if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
2762 domains.list = isl_basic_set_list_free(domains.list);
2763 isl_set_free(classes);
2765 empty = isl_set_is_empty(domains.done);
2766 if (empty < 0) {
2767 domains.list = isl_basic_set_list_free(domains.list);
2768 domain = isl_set_free(domain);
2769 } else if (empty) {
2770 isl_set_free(domain);
2771 domain = isl_set_universe(isl_set_get_space(domains.done));
2772 } else {
2773 domain = isl_ast_build_eliminate(build, domain);
2775 if (compute_partial_domains(&domains, domain) < 0)
2776 domains.list = isl_basic_set_list_free(domains.list);
2778 isl_set_free(domains.schedule_domain);
2779 isl_set_free(domains.done);
2780 isl_map_free(domains.sep_class);
2781 for (type = atomic; type <= separate; ++type)
2782 isl_set_free(domains.option[type]);
2784 return domains.list;
2787 /* Generate code for a single component, after shifting (if any)
2788 * has been applied.
2790 * We first split up the domain at the current depth into disjoint
2791 * basic sets based on the user-specified options.
2792 * Then we generated code for each of them and concatenate the results.
2794 static __isl_give isl_ast_graft_list *generate_shifted_component(
2795 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
2797 isl_basic_set_list *domain_list;
2798 isl_ast_graft_list *list = NULL;
2800 domain_list = compute_domains(executed, build);
2801 list = generate_parallel_domains(domain_list, executed, build);
2803 isl_basic_set_list_free(domain_list);
2804 isl_union_map_free(executed);
2805 isl_ast_build_free(build);
2807 return list;
2810 struct isl_set_map_pair {
2811 isl_set *set;
2812 isl_map *map;
2815 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2816 * of indices into the "domain" array,
2817 * return the union of the "map" fields of the elements
2818 * indexed by the first "n" elements of "order".
2820 static __isl_give isl_union_map *construct_component_executed(
2821 struct isl_set_map_pair *domain, int *order, int n)
2823 int i;
2824 isl_map *map;
2825 isl_union_map *executed;
2827 map = isl_map_copy(domain[order[0]].map);
2828 executed = isl_union_map_from_map(map);
2829 for (i = 1; i < n; ++i) {
2830 map = isl_map_copy(domain[order[i]].map);
2831 executed = isl_union_map_add_map(executed, map);
2834 return executed;
2837 /* Generate code for a single component, after shifting (if any)
2838 * has been applied.
2840 * The component inverse schedule is specified as the "map" fields
2841 * of the elements of "domain" indexed by the first "n" elements of "order".
2843 static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
2844 struct isl_set_map_pair *domain, int *order, int n,
2845 __isl_take isl_ast_build *build)
2847 isl_union_map *executed;
2849 executed = construct_component_executed(domain, order, n);
2850 return generate_shifted_component(executed, build);
2853 /* Does set dimension "pos" of "set" have an obviously fixed value?
2855 static int dim_is_fixed(__isl_keep isl_set *set, int pos)
2857 int fixed;
2858 isl_val *v;
2860 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, pos);
2861 if (!v)
2862 return -1;
2863 fixed = !isl_val_is_nan(v);
2864 isl_val_free(v);
2866 return fixed;
2869 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2870 * of indices into the "domain" array,
2871 * do all (except for at most one) of the "set" field of the elements
2872 * indexed by the first "n" elements of "order" have a fixed value
2873 * at position "depth"?
2875 static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
2876 int *order, int n, int depth)
2878 int i;
2879 int non_fixed = -1;
2881 for (i = 0; i < n; ++i) {
2882 int f;
2884 f = dim_is_fixed(domain[order[i]].set, depth);
2885 if (f < 0)
2886 return -1;
2887 if (f)
2888 continue;
2889 if (non_fixed >= 0)
2890 return 0;
2891 non_fixed = i;
2894 return 1;
2897 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2898 * of indices into the "domain" array,
2899 * eliminate the inner dimensions from the "set" field of the elements
2900 * indexed by the first "n" elements of "order", provided the current
2901 * dimension does not have a fixed value.
2903 * Return the index of the first element in "order" with a corresponding
2904 * "set" field that does not have an (obviously) fixed value.
2906 static int eliminate_non_fixed(struct isl_set_map_pair *domain,
2907 int *order, int n, int depth, __isl_keep isl_ast_build *build)
2909 int i;
2910 int base = -1;
2912 for (i = n - 1; i >= 0; --i) {
2913 int f;
2914 f = dim_is_fixed(domain[order[i]].set, depth);
2915 if (f < 0)
2916 return -1;
2917 if (f)
2918 continue;
2919 domain[order[i]].set = isl_ast_build_eliminate_inner(build,
2920 domain[order[i]].set);
2921 base = i;
2924 return base;
2927 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2928 * of indices into the "domain" array,
2929 * find the element of "domain" (amongst those indexed by the first "n"
2930 * elements of "order") with the "set" field that has the smallest
2931 * value for the current iterator.
2933 * Note that the domain with the smallest value may depend on the parameters
2934 * and/or outer loop dimension. Since the result of this function is only
2935 * used as heuristic, we only make a reasonable attempt at finding the best
2936 * domain, one that should work in case a single domain provides the smallest
2937 * value for the current dimension over all values of the parameters
2938 * and outer dimensions.
2940 * In particular, we compute the smallest value of the first domain
2941 * and replace it by that of any later domain if that later domain
2942 * has a smallest value that is smaller for at least some value
2943 * of the parameters and outer dimensions.
2945 static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
2946 __isl_keep isl_ast_build *build)
2948 int i;
2949 isl_map *min_first;
2950 int first = 0;
2952 min_first = isl_ast_build_map_to_iterator(build,
2953 isl_set_copy(domain[order[0]].set));
2954 min_first = isl_map_lexmin(min_first);
2956 for (i = 1; i < n; ++i) {
2957 isl_map *min, *test;
2958 int empty;
2960 min = isl_ast_build_map_to_iterator(build,
2961 isl_set_copy(domain[order[i]].set));
2962 min = isl_map_lexmin(min);
2963 test = isl_map_copy(min);
2964 test = isl_map_apply_domain(isl_map_copy(min_first), test);
2965 test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
2966 empty = isl_map_is_empty(test);
2967 isl_map_free(test);
2968 if (empty >= 0 && !empty) {
2969 isl_map_free(min_first);
2970 first = i;
2971 min_first = min;
2972 } else
2973 isl_map_free(min);
2975 if (empty < 0)
2976 break;
2979 isl_map_free(min_first);
2981 return i < n ? -1 : first;
2984 /* Construct a shifted inverse schedule based on the original inverse schedule,
2985 * the stride and the offset.
2987 * The original inverse schedule is specified as the "map" fields
2988 * of the elements of "domain" indexed by the first "n" elements of "order".
2990 * "stride" and "offset" are such that the difference
2991 * between the values of the current dimension of domain "i"
2992 * and the values of the current dimension for some reference domain are
2993 * equal to
2995 * stride * integer + offset[i]
2997 * Moreover, 0 <= offset[i] < stride.
2999 * For each domain, we create a map
3001 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3003 * where j refers to the current dimension and the other dimensions are
3004 * unchanged, and apply this map to the original schedule domain.
3006 * For example, for the original schedule
3008 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3010 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3011 * we apply the mapping
3013 * { [j] -> [j, 0] }
3015 * to the schedule of the "A" domain and the mapping
3017 * { [j - 1] -> [j, 1] }
3019 * to the schedule of the "B" domain.
3022 * Note that after the transformation, the differences between pairs
3023 * of values of the current dimension over all domains are multiples
3024 * of stride and that we have therefore exposed the stride.
3027 * To see that the mapping preserves the lexicographic order,
3028 * first note that each of the individual maps above preserves the order.
3029 * If the value of the current iterator is j1 in one domain and j2 in another,
3030 * then if j1 = j2, we know that the same map is applied to both domains
3031 * and the order is preserved.
3032 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3033 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3035 * j1 - c1 < j2 - c2
3037 * and the order is preserved.
3038 * If c1 < c2, then we know
3040 * 0 <= c2 - c1 < s
3042 * We also have
3044 * j2 - j1 = n * s + r
3046 * with n >= 0 and 0 <= r < s.
3047 * In other words, r = c2 - c1.
3048 * If n > 0, then
3050 * j1 - c1 < j2 - c2
3052 * If n = 0, then
3054 * j1 - c1 = j2 - c2
3056 * and so
3058 * (j1 - c1, c1) << (j2 - c2, c2)
3060 * with "<<" the lexicographic order, proving that the order is preserved
3061 * in all cases.
3063 static __isl_give isl_union_map *contruct_shifted_executed(
3064 struct isl_set_map_pair *domain, int *order, int n,
3065 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3066 __isl_take isl_ast_build *build)
3068 int i;
3069 isl_union_map *executed;
3070 isl_space *space;
3071 isl_map *map;
3072 int depth;
3073 isl_constraint *c;
3075 depth = isl_ast_build_get_depth(build);
3076 space = isl_ast_build_get_space(build, 1);
3077 executed = isl_union_map_empty(isl_space_copy(space));
3078 space = isl_space_map_from_set(space);
3079 map = isl_map_identity(isl_space_copy(space));
3080 map = isl_map_eliminate(map, isl_dim_out, depth, 1);
3081 map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
3082 space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
3084 c = isl_equality_alloc(isl_local_space_from_space(space));
3085 c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
3086 c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
3088 for (i = 0; i < n; ++i) {
3089 isl_map *map_i;
3090 isl_val *v;
3092 v = isl_multi_val_get_val(offset, i);
3093 if (!v)
3094 break;
3095 map_i = isl_map_copy(map);
3096 map_i = isl_map_fix_val(map_i, isl_dim_out, depth + 1,
3097 isl_val_copy(v));
3098 v = isl_val_neg(v);
3099 c = isl_constraint_set_constant_val(c, v);
3100 map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
3102 map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
3103 map_i);
3104 executed = isl_union_map_add_map(executed, map_i);
3107 isl_constraint_free(c);
3108 isl_map_free(map);
3110 if (i < n)
3111 executed = isl_union_map_free(executed);
3113 return executed;
3116 /* Generate code for a single component, after exposing the stride,
3117 * given that the schedule domain is "shifted strided".
3119 * The component inverse schedule is specified as the "map" fields
3120 * of the elements of "domain" indexed by the first "n" elements of "order".
3122 * The schedule domain being "shifted strided" means that the differences
3123 * between the values of the current dimension of domain "i"
3124 * and the values of the current dimension for some reference domain are
3125 * equal to
3127 * stride * integer + offset[i]
3129 * We first look for the domain with the "smallest" value for the current
3130 * dimension and adjust the offsets such that the offset of the "smallest"
3131 * domain is equal to zero. The other offsets are reduced modulo stride.
3133 * Based on this information, we construct a new inverse schedule in
3134 * contruct_shifted_executed that exposes the stride.
3135 * Since this involves the introduction of a new schedule dimension,
3136 * the build needs to be changed accodingly.
3137 * After computing the AST, the newly introduced dimension needs
3138 * to be removed again from the list of grafts. We do this by plugging
3139 * in a mapping that represents the new schedule domain in terms of the
3140 * old schedule domain.
3142 static __isl_give isl_ast_graft_list *generate_shift_component(
3143 struct isl_set_map_pair *domain, int *order, int n,
3144 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3145 __isl_take isl_ast_build *build)
3147 isl_ast_graft_list *list;
3148 int first;
3149 int depth;
3150 isl_ctx *ctx;
3151 isl_val *val;
3152 isl_multi_val *mv;
3153 isl_space *space;
3154 isl_multi_aff *ma, *zero;
3155 isl_union_map *executed;
3157 ctx = isl_ast_build_get_ctx(build);
3158 depth = isl_ast_build_get_depth(build);
3160 first = first_offset(domain, order, n, build);
3161 if (first < 0)
3162 goto error;
3164 mv = isl_multi_val_copy(offset);
3165 val = isl_multi_val_get_val(offset, first);
3166 val = isl_val_neg(val);
3167 mv = isl_multi_val_add_val(mv, val);
3168 mv = isl_multi_val_mod_val(mv, isl_val_copy(stride));
3170 executed = contruct_shifted_executed(domain, order, n, stride, mv,
3171 build);
3172 space = isl_ast_build_get_space(build, 1);
3173 space = isl_space_map_from_set(space);
3174 ma = isl_multi_aff_identity(isl_space_copy(space));
3175 space = isl_space_from_domain(isl_space_domain(space));
3176 space = isl_space_add_dims(space, isl_dim_out, 1);
3177 zero = isl_multi_aff_zero(space);
3178 ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
3179 build = isl_ast_build_insert_dim(build, depth + 1);
3180 list = generate_shifted_component(executed, build);
3182 list = isl_ast_graft_list_preimage_multi_aff(list, ma);
3184 isl_multi_val_free(mv);
3186 return list;
3187 error:
3188 isl_ast_build_free(build);
3189 return NULL;
3192 /* Generate code for a single component.
3194 * The component inverse schedule is specified as the "map" fields
3195 * of the elements of "domain" indexed by the first "n" elements of "order".
3197 * This function may modify the "set" fields of "domain".
3199 * Before proceeding with the actual code generation for the component,
3200 * we first check if there are any "shifted" strides, meaning that
3201 * the schedule domains of the individual domains are all strided,
3202 * but that they have different offsets, resulting in the union
3203 * of schedule domains not being strided anymore.
3205 * The simplest example is the schedule
3207 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3209 * Both schedule domains are strided, but their union is not.
3210 * This function detects such cases and then rewrites the schedule to
3212 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
3214 * In the new schedule, the schedule domains have the same offset (modulo
3215 * the stride), ensuring that the union of schedule domains is also strided.
3218 * If there is only a single domain in the component, then there is
3219 * nothing to do. Similarly, if the current schedule dimension has
3220 * a fixed value for almost all domains then there is nothing to be done.
3221 * In particular, we need at least two domains where the current schedule
3222 * dimension does not have a fixed value.
3223 * Finally, if any of the options refer to the current schedule dimension,
3224 * then we bail out as well. It would be possible to reformulate the options
3225 * in terms of the new schedule domain, but that would introduce constraints
3226 * that separate the domains in the options and that is something we would
3227 * like to avoid.
3230 * To see if there is any shifted stride, we look at the differences
3231 * between the values of the current dimension in pairs of domains
3232 * for equal values of outer dimensions. These differences should be
3233 * of the form
3235 * m x + r
3237 * with "m" the stride and "r" a constant. Note that we cannot perform
3238 * this analysis on individual domains as the lower bound in each domain
3239 * may depend on parameters or outer dimensions and so the current dimension
3240 * itself may not have a fixed remainder on division by the stride.
3242 * In particular, we compare the first domain that does not have an
3243 * obviously fixed value for the current dimension to itself and all
3244 * other domains and collect the offsets and the gcd of the strides.
3245 * If the gcd becomes one, then we failed to find shifted strides.
3246 * If the gcd is zero, then the differences were all fixed, meaning
3247 * that some domains had non-obviously fixed values for the current dimension.
3248 * If all the offsets are the same (for those domains that do not have
3249 * an obviously fixed value for the current dimension), then we do not
3250 * apply the transformation.
3251 * If none of the domains were skipped, then there is nothing to do.
3252 * If some of them were skipped, then if we apply separation, the schedule
3253 * domain should get split in pieces with a (non-shifted) stride.
3255 * Otherwise, we apply a shift to expose the stride in
3256 * generate_shift_component.
3258 static __isl_give isl_ast_graft_list *generate_component(
3259 struct isl_set_map_pair *domain, int *order, int n,
3260 __isl_take isl_ast_build *build)
3262 int i, d;
3263 int depth;
3264 isl_ctx *ctx;
3265 isl_map *map;
3266 isl_set *deltas;
3267 isl_val *gcd = NULL;
3268 isl_multi_val *mv;
3269 int fixed, skip;
3270 int base;
3271 isl_ast_graft_list *list;
3272 int res = 0;
3274 depth = isl_ast_build_get_depth(build);
3276 skip = n == 1;
3277 if (skip >= 0 && !skip)
3278 skip = at_most_one_non_fixed(domain, order, n, depth);
3279 if (skip >= 0 && !skip)
3280 skip = isl_ast_build_options_involve_depth(build);
3281 if (skip < 0)
3282 goto error;
3283 if (skip)
3284 return generate_shifted_component_from_list(domain,
3285 order, n, build);
3287 base = eliminate_non_fixed(domain, order, n, depth, build);
3288 if (base < 0)
3289 goto error;
3291 ctx = isl_ast_build_get_ctx(build);
3293 mv = isl_multi_val_zero(isl_space_set_alloc(ctx, 0, n));
3295 fixed = 1;
3296 for (i = 0; i < n; ++i) {
3297 isl_val *r, *m;
3299 map = isl_map_from_domain_and_range(
3300 isl_set_copy(domain[order[base]].set),
3301 isl_set_copy(domain[order[i]].set));
3302 for (d = 0; d < depth; ++d)
3303 map = isl_map_equate(map, isl_dim_in, d,
3304 isl_dim_out, d);
3305 deltas = isl_map_deltas(map);
3306 res = isl_set_dim_residue_class_val(deltas, depth, &m, &r);
3307 isl_set_free(deltas);
3308 if (res < 0)
3309 break;
3311 if (i == 0)
3312 gcd = m;
3313 else
3314 gcd = isl_val_gcd(gcd, m);
3315 if (isl_val_is_one(gcd)) {
3316 isl_val_free(r);
3317 break;
3319 mv = isl_multi_val_set_val(mv, i, r);
3321 res = dim_is_fixed(domain[order[i]].set, depth);
3322 if (res < 0)
3323 break;
3324 if (res)
3325 continue;
3327 if (fixed && i > base) {
3328 isl_val *a, *b;
3329 a = isl_multi_val_get_val(mv, i);
3330 b = isl_multi_val_get_val(mv, base);
3331 if (isl_val_ne(a, b))
3332 fixed = 0;
3333 isl_val_free(a);
3334 isl_val_free(b);
3338 if (res < 0 || !gcd) {
3339 isl_ast_build_free(build);
3340 list = NULL;
3341 } else if (i < n || fixed || isl_val_is_zero(gcd)) {
3342 list = generate_shifted_component_from_list(domain,
3343 order, n, build);
3344 } else {
3345 list = generate_shift_component(domain, order, n, gcd, mv,
3346 build);
3349 isl_val_free(gcd);
3350 isl_multi_val_free(mv);
3352 return list;
3353 error:
3354 isl_ast_build_free(build);
3355 return NULL;
3358 /* Store both "map" itself and its domain in the
3359 * structure pointed to by *next and advance to the next array element.
3361 static int extract_domain(__isl_take isl_map *map, void *user)
3363 struct isl_set_map_pair **next = user;
3365 (*next)->map = isl_map_copy(map);
3366 (*next)->set = isl_map_domain(map);
3367 (*next)++;
3369 return 0;
3372 /* Internal data for any_scheduled_after.
3374 * "depth" is the number of loops that have already been generated
3375 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
3376 * "domain" is an array of set-map pairs corresponding to the different
3377 * iteration domains. The set is the schedule domain, i.e., the domain
3378 * of the inverse schedule, while the map is the inverse schedule itself.
3380 struct isl_any_scheduled_after_data {
3381 int depth;
3382 int group_coscheduled;
3383 struct isl_set_map_pair *domain;
3386 /* Is any element of domain "i" scheduled after any element of domain "j"
3387 * (for a common iteration of the first data->depth loops)?
3389 * data->domain[i].set contains the domain of the inverse schedule
3390 * for domain "i", i.e., elements in the schedule domain.
3392 * If data->group_coscheduled is set, then we also return 1 if there
3393 * is any pair of elements in the two domains that are scheduled together.
3395 static int any_scheduled_after(int i, int j, void *user)
3397 struct isl_any_scheduled_after_data *data = user;
3398 int dim = isl_set_dim(data->domain[i].set, isl_dim_set);
3399 int pos;
3401 for (pos = data->depth; pos < dim; ++pos) {
3402 int follows;
3404 follows = isl_set_follows_at(data->domain[i].set,
3405 data->domain[j].set, pos);
3407 if (follows < -1)
3408 return -1;
3409 if (follows > 0)
3410 return 1;
3411 if (follows < 0)
3412 return 0;
3415 return data->group_coscheduled;
3418 /* Look for independent components at the current depth and generate code
3419 * for each component separately. The resulting lists of grafts are
3420 * merged in an attempt to combine grafts with identical guards.
3422 * Code for two domains can be generated separately if all the elements
3423 * of one domain are scheduled before (or together with) all the elements
3424 * of the other domain. We therefore consider the graph with as nodes
3425 * the domains and an edge between two nodes if any element of the first
3426 * node is scheduled after any element of the second node.
3427 * If the ast_build_group_coscheduled is set, then we also add an edge if
3428 * there is any pair of elements in the two domains that are scheduled
3429 * together.
3430 * Code is then generated (by generate_component)
3431 * for each of the strongly connected components in this graph
3432 * in their topological order.
3434 * Since the test is performed on the domain of the inverse schedules of
3435 * the different domains, we precompute these domains and store
3436 * them in data.domain.
3438 static __isl_give isl_ast_graft_list *generate_components(
3439 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3441 int i;
3442 isl_ctx *ctx = isl_ast_build_get_ctx(build);
3443 int n = isl_union_map_n_map(executed);
3444 struct isl_any_scheduled_after_data data;
3445 struct isl_set_map_pair *next;
3446 struct isl_tarjan_graph *g = NULL;
3447 isl_ast_graft_list *list = NULL;
3448 int n_domain = 0;
3450 data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
3451 if (!data.domain)
3452 goto error;
3453 n_domain = n;
3455 next = data.domain;
3456 if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
3457 goto error;
3459 if (!build)
3460 goto error;
3461 data.depth = isl_ast_build_get_depth(build);
3462 data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
3463 g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
3464 if (!g)
3465 goto error;
3467 list = isl_ast_graft_list_alloc(ctx, 0);
3469 i = 0;
3470 while (list && n) {
3471 isl_ast_graft_list *list_c;
3472 int first = i;
3474 if (g->order[i] == -1)
3475 isl_die(ctx, isl_error_internal, "cannot happen",
3476 goto error);
3477 ++i; --n;
3478 while (g->order[i] != -1) {
3479 ++i; --n;
3482 list_c = generate_component(data.domain,
3483 g->order + first, i - first,
3484 isl_ast_build_copy(build));
3485 list = isl_ast_graft_list_merge(list, list_c, build);
3487 ++i;
3490 if (0)
3491 error: list = isl_ast_graft_list_free(list);
3492 isl_tarjan_graph_free(g);
3493 for (i = 0; i < n_domain; ++i) {
3494 isl_map_free(data.domain[i].map);
3495 isl_set_free(data.domain[i].set);
3497 free(data.domain);
3498 isl_union_map_free(executed);
3499 isl_ast_build_free(build);
3501 return list;
3504 /* Generate code for the next level (and all inner levels).
3506 * If "executed" is empty, i.e., no code needs to be generated,
3507 * then we return an empty list.
3509 * If we have already generated code for all loop levels, then we pass
3510 * control to generate_inner_level.
3512 * If "executed" lives in a single space, i.e., if code needs to be
3513 * generated for a single domain, then there can only be a single
3514 * component and we go directly to generate_shifted_component.
3515 * Otherwise, we call generate_components to detect the components
3516 * and to call generate_component on each of them separately.
3518 static __isl_give isl_ast_graft_list *generate_next_level(
3519 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3521 int depth;
3523 if (!build || !executed)
3524 goto error;
3526 if (isl_union_map_is_empty(executed)) {
3527 isl_ctx *ctx = isl_ast_build_get_ctx(build);
3528 isl_union_map_free(executed);
3529 isl_ast_build_free(build);
3530 return isl_ast_graft_list_alloc(ctx, 0);
3533 depth = isl_ast_build_get_depth(build);
3534 if (depth >= isl_ast_build_dim(build, isl_dim_set))
3535 return generate_inner_level(executed, build);
3537 if (isl_union_map_n_map(executed) == 1)
3538 return generate_shifted_component(executed, build);
3540 return generate_components(executed, build);
3541 error:
3542 isl_union_map_free(executed);
3543 isl_ast_build_free(build);
3544 return NULL;
3547 /* Internal data structure used by isl_ast_build_ast_from_schedule.
3548 * internal, executed and build are the inputs to generate_code.
3549 * list collects the output.
3551 struct isl_generate_code_data {
3552 int internal;
3553 isl_union_map *executed;
3554 isl_ast_build *build;
3556 isl_ast_graft_list *list;
3559 /* Given an inverse schedule in terms of the external build schedule, i.e.,
3561 * [E -> S] -> D
3563 * with E the external build schedule and S the additional schedule "space",
3564 * reformulate the inverse schedule in terms of the internal schedule domain,
3565 * i.e., return
3567 * [I -> S] -> D
3569 * We first obtain a mapping
3571 * I -> E
3573 * take the inverse and the product with S -> S, resulting in
3575 * [I -> S] -> [E -> S]
3577 * Applying the map to the input produces the desired result.
3579 static __isl_give isl_union_map *internal_executed(
3580 __isl_take isl_union_map *executed, __isl_keep isl_space *space,
3581 __isl_keep isl_ast_build *build)
3583 isl_map *id, *proj;
3585 proj = isl_ast_build_get_schedule_map(build);
3586 proj = isl_map_reverse(proj);
3587 space = isl_space_map_from_set(isl_space_copy(space));
3588 id = isl_map_identity(space);
3589 proj = isl_map_product(proj, id);
3590 executed = isl_union_map_apply_domain(executed,
3591 isl_union_map_from_map(proj));
3592 return executed;
3595 /* Generate an AST that visits the elements in the range of data->executed
3596 * in the relative order specified by the corresponding domain element(s)
3597 * for those domain elements that belong to "set".
3598 * Add the result to data->list.
3600 * The caller ensures that "set" is a universe domain.
3601 * "space" is the space of the additional part of the schedule.
3602 * It is equal to the space of "set" if build->domain is parametric.
3603 * Otherwise, it is equal to the range of the wrapped space of "set".
3605 * If the build space is not parametric and if isl_ast_build_ast_from_schedule
3606 * was called from an outside user (data->internal not set), then
3607 * the (inverse) schedule refers to the external build domain and needs to
3608 * be transformed to refer to the internal build domain.
3610 * If the build space is parametric, then we add some of the parameter
3611 * constraints to the executed relation. Adding these constraints
3612 * allows for an earlier detection of conflicts in some cases.
3613 * However, we do not want to divide the executed relation into
3614 * more disjuncts than necessary. We therefore approximate
3615 * the constraints on the parameters by a single disjunct set.
3617 * The build is extended to include the additional part of the schedule.
3618 * If the original build space was not parametric, then the options
3619 * in data->build refer only to the additional part of the schedule
3620 * and they need to be adjusted to refer to the complete AST build
3621 * domain.
3623 * After having adjusted inverse schedule and build, we start generating
3624 * code with the outer loop of the current code generation
3625 * in generate_next_level.
3627 * If the original build space was not parametric, we undo the embedding
3628 * on the resulting isl_ast_node_list so that it can be used within
3629 * the outer AST build.
3631 static int generate_code_in_space(struct isl_generate_code_data *data,
3632 __isl_take isl_set *set, __isl_take isl_space *space)
3634 isl_union_map *executed;
3635 isl_ast_build *build;
3636 isl_ast_graft_list *list;
3637 int embed;
3639 executed = isl_union_map_copy(data->executed);
3640 executed = isl_union_map_intersect_domain(executed,
3641 isl_union_set_from_set(set));
3643 embed = !isl_set_is_params(data->build->domain);
3644 if (embed && !data->internal)
3645 executed = internal_executed(executed, space, data->build);
3646 if (!embed) {
3647 isl_set *domain;
3648 domain = isl_ast_build_get_domain(data->build);
3649 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
3650 executed = isl_union_map_intersect_params(executed, domain);
3653 build = isl_ast_build_copy(data->build);
3654 build = isl_ast_build_product(build, space);
3656 list = generate_next_level(executed, build);
3658 list = isl_ast_graft_list_unembed(list, embed);
3660 data->list = isl_ast_graft_list_concat(data->list, list);
3662 return 0;
3665 /* Generate an AST that visits the elements in the range of data->executed
3666 * in the relative order specified by the corresponding domain element(s)
3667 * for those domain elements that belong to "set".
3668 * Add the result to data->list.
3670 * The caller ensures that "set" is a universe domain.
3672 * If the build space S is not parametric, then the space of "set"
3673 * need to be a wrapped relation with S as domain. That is, it needs
3674 * to be of the form
3676 * [S -> T]
3678 * Check this property and pass control to generate_code_in_space
3679 * passing along T.
3680 * If the build space is not parametric, then T is the space of "set".
3682 static int generate_code_set(__isl_take isl_set *set, void *user)
3684 struct isl_generate_code_data *data = user;
3685 isl_space *space, *build_space;
3686 int is_domain;
3688 space = isl_set_get_space(set);
3690 if (isl_set_is_params(data->build->domain))
3691 return generate_code_in_space(data, set, space);
3693 build_space = isl_ast_build_get_space(data->build, data->internal);
3694 space = isl_space_unwrap(space);
3695 is_domain = isl_space_is_domain(build_space, space);
3696 isl_space_free(build_space);
3697 space = isl_space_range(space);
3699 if (is_domain < 0)
3700 goto error;
3701 if (!is_domain)
3702 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3703 "invalid nested schedule space", goto error);
3705 return generate_code_in_space(data, set, space);
3706 error:
3707 isl_set_free(set);
3708 isl_space_free(space);
3709 return -1;
3712 /* Generate an AST that visits the elements in the range of "executed"
3713 * in the relative order specified by the corresponding domain element(s).
3715 * "build" is an isl_ast_build that has either been constructed by
3716 * isl_ast_build_from_context or passed to a callback set by
3717 * isl_ast_build_set_create_leaf.
3718 * In the first case, the space of the isl_ast_build is typically
3719 * a parametric space, although this is currently not enforced.
3720 * In the second case, the space is never a parametric space.
3721 * If the space S is not parametric, then the domain space(s) of "executed"
3722 * need to be wrapped relations with S as domain.
3724 * If the domain of "executed" consists of several spaces, then an AST
3725 * is generated for each of them (in arbitrary order) and the results
3726 * are concatenated.
3728 * If "internal" is set, then the domain "S" above refers to the internal
3729 * schedule domain representation. Otherwise, it refers to the external
3730 * representation, as returned by isl_ast_build_get_schedule_space.
3732 * We essentially run over all the spaces in the domain of "executed"
3733 * and call generate_code_set on each of them.
3735 static __isl_give isl_ast_graft_list *generate_code(
3736 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3737 int internal)
3739 isl_ctx *ctx;
3740 struct isl_generate_code_data data = { 0 };
3741 isl_space *space;
3742 isl_union_set *schedule_domain;
3743 isl_union_map *universe;
3745 if (!build)
3746 goto error;
3747 space = isl_ast_build_get_space(build, 1);
3748 space = isl_space_align_params(space,
3749 isl_union_map_get_space(executed));
3750 space = isl_space_align_params(space,
3751 isl_union_map_get_space(build->options));
3752 build = isl_ast_build_align_params(build, isl_space_copy(space));
3753 executed = isl_union_map_align_params(executed, space);
3754 if (!executed || !build)
3755 goto error;
3757 ctx = isl_ast_build_get_ctx(build);
3759 data.internal = internal;
3760 data.executed = executed;
3761 data.build = build;
3762 data.list = isl_ast_graft_list_alloc(ctx, 0);
3764 universe = isl_union_map_universe(isl_union_map_copy(executed));
3765 schedule_domain = isl_union_map_domain(universe);
3766 if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
3767 &data) < 0)
3768 data.list = isl_ast_graft_list_free(data.list);
3770 isl_union_set_free(schedule_domain);
3771 isl_union_map_free(executed);
3773 isl_ast_build_free(build);
3774 return data.list;
3775 error:
3776 isl_union_map_free(executed);
3777 isl_ast_build_free(build);
3778 return NULL;
3781 /* Generate an AST that visits the elements in the domain of "schedule"
3782 * in the relative order specified by the corresponding image element(s).
3784 * "build" is an isl_ast_build that has either been constructed by
3785 * isl_ast_build_from_context or passed to a callback set by
3786 * isl_ast_build_set_create_leaf.
3787 * In the first case, the space of the isl_ast_build is typically
3788 * a parametric space, although this is currently not enforced.
3789 * In the second case, the space is never a parametric space.
3790 * If the space S is not parametric, then the range space(s) of "schedule"
3791 * need to be wrapped relations with S as domain.
3793 * If the range of "schedule" consists of several spaces, then an AST
3794 * is generated for each of them (in arbitrary order) and the results
3795 * are concatenated.
3797 * We first initialize the local copies of the relevant options.
3798 * We do this here rather than when the isl_ast_build is created
3799 * because the options may have changed between the construction
3800 * of the isl_ast_build and the call to isl_generate_code.
3802 * The main computation is performed on an inverse schedule (with
3803 * the schedule domain in the domain and the elements to be executed
3804 * in the range) called "executed".
3806 __isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
3807 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
3809 isl_ast_graft_list *list;
3810 isl_ast_node *node;
3811 isl_union_map *executed;
3813 build = isl_ast_build_copy(build);
3814 build = isl_ast_build_set_single_valued(build, 0);
3815 schedule = isl_union_map_coalesce(schedule);
3816 executed = isl_union_map_reverse(schedule);
3817 list = generate_code(executed, isl_ast_build_copy(build), 0);
3818 node = isl_ast_node_from_graft_list(list, build);
3819 isl_ast_build_free(build);
3821 return node;