drop isl_ast_build_set_enforced
[isl.git] / isl_ast_codegen.c
blob4546464f5b158bf37c2d9ccf35d92e42a19789bf
1 /*
2 * Copyright 2012-2014 Ecole Normale Superieure
3 * Copyright 2014 INRIA Rocquencourt
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege,
8 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
9 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
10 * B.P. 105 - 78153 Le Chesnay, France
13 #include <limits.h>
14 #include <isl/aff.h>
15 #include <isl/set.h>
16 #include <isl/ilp.h>
17 #include <isl/union_map.h>
18 #include <isl_sort.h>
19 #include <isl_tarjan.h>
20 #include <isl_ast_private.h>
21 #include <isl_ast_build_expr.h>
22 #include <isl_ast_build_private.h>
23 #include <isl_ast_graft_private.h>
25 /* Data used in generate_domain.
27 * "build" is the input build.
28 * "list" collects the results.
30 struct isl_generate_domain_data {
31 isl_ast_build *build;
33 isl_ast_graft_list *list;
36 static __isl_give isl_ast_graft_list *generate_next_level(
37 __isl_take isl_union_map *executed,
38 __isl_take isl_ast_build *build);
39 static __isl_give isl_ast_graft_list *generate_code(
40 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
41 int internal);
43 /* Generate an AST for a single domain based on
44 * the (non single valued) inverse schedule "executed".
46 * We extend the schedule with the iteration domain
47 * and continue generating through a call to generate_code.
49 * In particular, if executed has the form
51 * S -> D
53 * then we continue generating code on
55 * [S -> D] -> D
57 * The extended inverse schedule is clearly single valued
58 * ensuring that the nested generate_code will not reach this function,
59 * but will instead create calls to all elements of D that need
60 * to be executed from the current schedule domain.
62 static int generate_non_single_valued(__isl_take isl_map *executed,
63 struct isl_generate_domain_data *data)
65 isl_map *identity;
66 isl_ast_build *build;
67 isl_ast_graft_list *list;
69 build = isl_ast_build_copy(data->build);
71 identity = isl_set_identity(isl_map_range(isl_map_copy(executed)));
72 executed = isl_map_domain_product(executed, identity);
73 build = isl_ast_build_set_single_valued(build, 1);
75 list = generate_code(isl_union_map_from_map(executed), build, 1);
77 data->list = isl_ast_graft_list_concat(data->list, list);
79 return 0;
82 /* Call the at_each_domain callback, if requested by the user,
83 * after recording the current inverse schedule in the build.
85 static __isl_give isl_ast_graft *at_each_domain(__isl_take isl_ast_graft *graft,
86 __isl_keep isl_map *executed, __isl_keep isl_ast_build *build)
88 if (!graft || !build)
89 return isl_ast_graft_free(graft);
90 if (!build->at_each_domain)
91 return graft;
93 build = isl_ast_build_copy(build);
94 build = isl_ast_build_set_executed(build,
95 isl_union_map_from_map(isl_map_copy(executed)));
96 if (!build)
97 return isl_ast_graft_free(graft);
99 graft->node = build->at_each_domain(graft->node,
100 build, build->at_each_domain_user);
101 isl_ast_build_free(build);
103 if (!graft->node)
104 graft = isl_ast_graft_free(graft);
106 return graft;
109 /* Generate an AST for a single domain based on
110 * the inverse schedule "executed" and add it to data->list.
112 * If there is more than one domain element associated to the current
113 * schedule "time", then we need to continue the generation process
114 * in generate_non_single_valued.
115 * Note that the inverse schedule being single-valued may depend
116 * on constraints that are only available in the original context
117 * domain specified by the user. We therefore first introduce
118 * the constraints from data->build->domain.
119 * On the other hand, we only perform the test after having taken the gist
120 * of the domain as the resulting map is the one from which the call
121 * expression is constructed. Using this map to construct the call
122 * expression usually yields simpler results.
123 * Because we perform the single-valuedness test on the gisted map,
124 * we may in rare cases fail to recognize that the inverse schedule
125 * is single-valued. This becomes problematic if this happens
126 * from the recursive call through generate_non_single_valued
127 * as we would then end up in an infinite recursion.
128 * We therefore check if we are inside a call to generate_non_single_valued
129 * and revert to the ungisted map if the gisted map turns out not to be
130 * single-valued.
132 * Otherwise, we generate a call expression for the single executed
133 * domain element and put a guard around it based on the (simplified)
134 * domain of "executed".
136 * At this stage, any pending constraints in the build can no longer
137 * be simplified with respect to any enforced constraints since
138 * the call node does not have any enforced constraints.
139 * We therefore turn all pending constraints into guards
140 * (after simplifying them with respect to the already generated
141 * constraints) and add them to both the generated constraints
142 * and the guard of the constructed graft. This guard will ensure
143 * that the constraints are effectively generated.
145 * If the user has set an at_each_domain callback, it is called
146 * on the constructed call expression node.
148 static int generate_domain(__isl_take isl_map *executed, void *user)
150 struct isl_generate_domain_data *data = user;
151 isl_ast_build *build;
152 isl_ast_graft *graft;
153 isl_ast_graft_list *list;
154 isl_set *guard;
155 isl_map *map = NULL;
156 int empty, sv;
158 executed = isl_map_intersect_domain(executed,
159 isl_set_copy(data->build->domain));
160 empty = isl_map_is_empty(executed);
161 if (empty < 0)
162 goto error;
163 if (empty) {
164 isl_map_free(executed);
165 return 0;
168 executed = isl_map_coalesce(executed);
169 map = isl_map_copy(executed);
170 map = isl_ast_build_compute_gist_map_domain(data->build, map);
171 sv = isl_map_is_single_valued(map);
172 if (sv < 0)
173 goto error;
174 if (!sv) {
175 isl_map_free(map);
176 if (data->build->single_valued)
177 map = isl_map_copy(executed);
178 else
179 return generate_non_single_valued(executed, data);
181 guard = isl_map_domain(isl_map_copy(map));
182 guard = isl_set_compute_divs(guard);
183 guard = isl_set_intersect(guard,
184 isl_ast_build_get_pending(data->build));
185 guard = isl_set_coalesce(guard);
186 guard = isl_ast_build_specialize(data->build, guard);
187 guard = isl_set_gist(guard, isl_ast_build_get_generated(data->build));
189 build = isl_ast_build_copy(data->build);
190 build = isl_ast_build_replace_pending_by_guard(build,
191 isl_set_copy(guard));
192 graft = isl_ast_graft_alloc_domain(map, build);
193 graft = at_each_domain(graft, executed, build);
194 isl_ast_build_free(build);
195 isl_map_free(executed);
196 graft = isl_ast_graft_add_guard(graft, guard, data->build);
198 list = isl_ast_graft_list_from_ast_graft(graft);
199 data->list = isl_ast_graft_list_concat(data->list, list);
201 return 0;
202 error:
203 isl_map_free(map);
204 isl_map_free(executed);
205 return -1;
208 /* Call build->create_leaf to a create "leaf" node in the AST,
209 * encapsulate the result in an isl_ast_graft and return the result
210 * as a 1-element list.
212 * Note that the node returned by the user may be an entire tree.
214 * Since the node itself cannot enforce any constraints, we turn
215 * all pending constraints into guards and add them to the resulting
216 * graft to ensure that they will be generated.
218 * Before we pass control to the user, we first clear some information
219 * from the build that is (presumbably) only meaningful
220 * for the current code generation.
221 * This includes the create_leaf callback itself, so we make a copy
222 * of the build first.
224 static __isl_give isl_ast_graft_list *call_create_leaf(
225 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
227 isl_set *guard;
228 isl_ast_node *node;
229 isl_ast_graft *graft;
230 isl_ast_build *user_build;
232 guard = isl_ast_build_get_pending(build);
233 user_build = isl_ast_build_copy(build);
234 user_build = isl_ast_build_replace_pending_by_guard(user_build,
235 isl_set_copy(guard));
236 user_build = isl_ast_build_set_executed(user_build, executed);
237 user_build = isl_ast_build_clear_local_info(user_build);
238 if (!user_build)
239 node = NULL;
240 else
241 node = build->create_leaf(user_build, build->create_leaf_user);
242 graft = isl_ast_graft_alloc(node, build);
243 graft = isl_ast_graft_add_guard(graft, guard, build);
244 isl_ast_build_free(build);
245 return isl_ast_graft_list_from_ast_graft(graft);
248 /* Generate an AST after having handled the complete schedule
249 * of this call to the code generator.
251 * If the user has specified a create_leaf callback, control
252 * is passed to the user in call_create_leaf.
254 * Otherwise, we generate one or more calls for each individual
255 * domain in generate_domain.
257 static __isl_give isl_ast_graft_list *generate_inner_level(
258 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
260 isl_ctx *ctx;
261 struct isl_generate_domain_data data = { build };
263 if (!build || !executed)
264 goto error;
266 if (build->create_leaf)
267 return call_create_leaf(executed, build);
269 ctx = isl_union_map_get_ctx(executed);
270 data.list = isl_ast_graft_list_alloc(ctx, 0);
271 if (isl_union_map_foreach_map(executed, &generate_domain, &data) < 0)
272 data.list = isl_ast_graft_list_free(data.list);
274 if (0)
275 error: data.list = NULL;
276 isl_ast_build_free(build);
277 isl_union_map_free(executed);
278 return data.list;
281 /* Call the before_each_for callback, if requested by the user.
283 static __isl_give isl_ast_node *before_each_for(__isl_take isl_ast_node *node,
284 __isl_keep isl_ast_build *build)
286 isl_id *id;
288 if (!node || !build)
289 return isl_ast_node_free(node);
290 if (!build->before_each_for)
291 return node;
292 id = build->before_each_for(build, build->before_each_for_user);
293 node = isl_ast_node_set_annotation(node, id);
294 return node;
297 /* Call the after_each_for callback, if requested by the user.
299 static __isl_give isl_ast_graft *after_each_for(__isl_take isl_ast_graft *graft,
300 __isl_keep isl_ast_build *build)
302 if (!graft || !build)
303 return isl_ast_graft_free(graft);
304 if (!build->after_each_for)
305 return graft;
306 graft->node = build->after_each_for(graft->node, build,
307 build->after_each_for_user);
308 if (!graft->node)
309 return isl_ast_graft_free(graft);
310 return graft;
313 /* Plug in all the know values of the current and outer dimensions
314 * in the domain of "executed". In principle, we only need to plug
315 * in the known value of the current dimension since the values of
316 * outer dimensions have been plugged in already.
317 * However, it turns out to be easier to just plug in all known values.
319 static __isl_give isl_union_map *plug_in_values(
320 __isl_take isl_union_map *executed, __isl_keep isl_ast_build *build)
322 return isl_ast_build_substitute_values_union_map_domain(build,
323 executed);
326 /* Check if the constraint "c" is a lower bound on dimension "pos",
327 * an upper bound, or independent of dimension "pos".
329 static int constraint_type(isl_constraint *c, int pos)
331 if (isl_constraint_is_lower_bound(c, isl_dim_set, pos))
332 return 1;
333 if (isl_constraint_is_upper_bound(c, isl_dim_set, pos))
334 return 2;
335 return 0;
338 /* Compare the types of the constraints "a" and "b",
339 * resulting in constraints that are independent of "depth"
340 * to be sorted before the lower bounds on "depth", which in
341 * turn are sorted before the upper bounds on "depth".
343 static int cmp_constraint(__isl_keep isl_constraint *a,
344 __isl_keep isl_constraint *b, void *user)
346 int *depth = user;
347 int t1 = constraint_type(a, *depth);
348 int t2 = constraint_type(b, *depth);
350 return t1 - t2;
353 /* Extract a lower bound on dimension "pos" from constraint "c".
355 * If the constraint is of the form
357 * a x + f(...) >= 0
359 * then we essentially return
361 * l = ceil(-f(...)/a)
363 * However, if the current dimension is strided, then we need to make
364 * sure that the lower bound we construct is of the form
366 * f + s a
368 * with f the offset and s the stride.
369 * We therefore compute
371 * f + s * ceil((l - f)/s)
373 static __isl_give isl_aff *lower_bound(__isl_keep isl_constraint *c,
374 int pos, __isl_keep isl_ast_build *build)
376 isl_aff *aff;
378 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
379 aff = isl_aff_ceil(aff);
381 if (isl_ast_build_has_stride(build, pos)) {
382 isl_aff *offset;
383 isl_val *stride;
385 offset = isl_ast_build_get_offset(build, pos);
386 stride = isl_ast_build_get_stride(build, pos);
388 aff = isl_aff_sub(aff, isl_aff_copy(offset));
389 aff = isl_aff_scale_down_val(aff, isl_val_copy(stride));
390 aff = isl_aff_ceil(aff);
391 aff = isl_aff_scale_val(aff, stride);
392 aff = isl_aff_add(aff, offset);
395 aff = isl_ast_build_compute_gist_aff(build, aff);
397 return aff;
400 /* Return the exact lower bound (or upper bound if "upper" is set)
401 * of "domain" as a piecewise affine expression.
403 * If we are computing a lower bound (of a strided dimension), then
404 * we need to make sure it is of the form
406 * f + s a
408 * where f is the offset and s is the stride.
409 * We therefore need to include the stride constraint before computing
410 * the minimum.
412 static __isl_give isl_pw_aff *exact_bound(__isl_keep isl_set *domain,
413 __isl_keep isl_ast_build *build, int upper)
415 isl_set *stride;
416 isl_map *it_map;
417 isl_pw_aff *pa;
418 isl_pw_multi_aff *pma;
420 domain = isl_set_copy(domain);
421 if (!upper) {
422 stride = isl_ast_build_get_stride_constraint(build);
423 domain = isl_set_intersect(domain, stride);
425 it_map = isl_ast_build_map_to_iterator(build, domain);
426 if (upper)
427 pma = isl_map_lexmax_pw_multi_aff(it_map);
428 else
429 pma = isl_map_lexmin_pw_multi_aff(it_map);
430 pa = isl_pw_multi_aff_get_pw_aff(pma, 0);
431 isl_pw_multi_aff_free(pma);
432 pa = isl_ast_build_compute_gist_pw_aff(build, pa);
433 pa = isl_pw_aff_coalesce(pa);
435 return pa;
438 /* Extract a lower bound on dimension "pos" from each constraint
439 * in "constraints" and return the list of lower bounds.
440 * If "constraints" has zero elements, then we extract a lower bound
441 * from "domain" instead.
443 static __isl_give isl_pw_aff_list *lower_bounds(
444 __isl_keep isl_constraint_list *constraints, int pos,
445 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
447 isl_ctx *ctx;
448 isl_pw_aff_list *list;
449 int i, n;
451 if (!build)
452 return NULL;
454 n = isl_constraint_list_n_constraint(constraints);
455 if (n == 0) {
456 isl_pw_aff *pa;
457 pa = exact_bound(domain, build, 0);
458 return isl_pw_aff_list_from_pw_aff(pa);
461 ctx = isl_ast_build_get_ctx(build);
462 list = isl_pw_aff_list_alloc(ctx,n);
464 for (i = 0; i < n; ++i) {
465 isl_aff *aff;
466 isl_constraint *c;
468 c = isl_constraint_list_get_constraint(constraints, i);
469 aff = lower_bound(c, pos, build);
470 isl_constraint_free(c);
471 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
474 return list;
477 /* Extract an upper bound on dimension "pos" from each constraint
478 * in "constraints" and return the list of upper bounds.
479 * If "constraints" has zero elements, then we extract an upper bound
480 * from "domain" instead.
482 static __isl_give isl_pw_aff_list *upper_bounds(
483 __isl_keep isl_constraint_list *constraints, int pos,
484 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
486 isl_ctx *ctx;
487 isl_pw_aff_list *list;
488 int i, n;
490 n = isl_constraint_list_n_constraint(constraints);
491 if (n == 0) {
492 isl_pw_aff *pa;
493 pa = exact_bound(domain, build, 1);
494 return isl_pw_aff_list_from_pw_aff(pa);
497 ctx = isl_ast_build_get_ctx(build);
498 list = isl_pw_aff_list_alloc(ctx,n);
500 for (i = 0; i < n; ++i) {
501 isl_aff *aff;
502 isl_constraint *c;
504 c = isl_constraint_list_get_constraint(constraints, i);
505 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
506 isl_constraint_free(c);
507 aff = isl_aff_floor(aff);
508 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
511 return list;
514 /* Callback for sorting the isl_pw_aff_list passed to reduce_list.
516 static int reduce_list_cmp(__isl_keep isl_pw_aff *a, __isl_keep isl_pw_aff *b,
517 void *user)
519 return isl_pw_aff_plain_cmp(a, b);
522 /* Return an isl_ast_expr that performs the reduction of type "type"
523 * on AST expressions corresponding to the elements in "list".
525 * The list is assumed to contain at least one element.
526 * If the list contains exactly one element, then the returned isl_ast_expr
527 * simply computes that affine expression.
528 * If the list contains more than one element, then we sort it
529 * using a fairly abitrary but hopefully reasonably stable order.
531 static __isl_give isl_ast_expr *reduce_list(enum isl_ast_op_type type,
532 __isl_keep isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
534 int i, n;
535 isl_ctx *ctx;
536 isl_ast_expr *expr;
538 if (!list)
539 return NULL;
541 n = isl_pw_aff_list_n_pw_aff(list);
543 if (n == 1)
544 return isl_ast_build_expr_from_pw_aff_internal(build,
545 isl_pw_aff_list_get_pw_aff(list, 0));
547 ctx = isl_pw_aff_list_get_ctx(list);
548 expr = isl_ast_expr_alloc_op(ctx, type, n);
549 if (!expr)
550 return NULL;
552 list = isl_pw_aff_list_copy(list);
553 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
554 if (!list)
555 return isl_ast_expr_free(expr);
557 for (i = 0; i < n; ++i) {
558 isl_ast_expr *expr_i;
560 expr_i = isl_ast_build_expr_from_pw_aff_internal(build,
561 isl_pw_aff_list_get_pw_aff(list, i));
562 if (!expr_i)
563 goto error;
564 expr->u.op.args[i] = expr_i;
567 isl_pw_aff_list_free(list);
568 return expr;
569 error:
570 isl_pw_aff_list_free(list);
571 isl_ast_expr_free(expr);
572 return NULL;
575 /* Add guards implied by the "generated constraints",
576 * but not (necessarily) enforced by the generated AST to "graft".
577 * In particular, if there is any stride constraints,
578 * then add the guard implied by those constraints.
579 * If we have generated a degenerate loop, then add the guard
580 * implied by "bounds" on the outer dimensions, i.e., the guard
581 * that ensures that the single value actually exists.
583 static __isl_give isl_ast_graft *add_implied_guards(
584 __isl_take isl_ast_graft *graft, int degenerate,
585 __isl_keep isl_basic_set *bounds, __isl_keep isl_ast_build *build)
587 int depth, has_stride;
588 isl_set *dom;
590 depth = isl_ast_build_get_depth(build);
591 has_stride = isl_ast_build_has_stride(build, depth);
592 if (!has_stride && !degenerate)
593 return graft;
595 if (degenerate) {
596 bounds = isl_basic_set_copy(bounds);
597 bounds = isl_basic_set_drop_constraints_not_involving_dims(
598 bounds, isl_dim_set, depth, 1);
599 dom = isl_set_from_basic_set(bounds);
600 dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
601 dom = isl_ast_build_compute_gist(build, dom);
602 graft = isl_ast_graft_add_guard(graft, dom, build);
605 if (has_stride) {
606 dom = isl_ast_build_get_stride_constraint(build);
607 dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
608 dom = isl_ast_build_compute_gist(build, dom);
609 graft = isl_ast_graft_add_guard(graft, dom, build);
612 return graft;
615 /* Update "graft" based on "sub_build" for the degenerate case.
617 * "build" is the build in which graft->node was created
618 * "sub_build" contains information about the current level itself,
619 * including the single value attained.
621 * We set the initialization part of the for loop to the single
622 * value attained by the current dimension.
623 * The increment and condition are not strictly needed as the are known
624 * to be "1" and "iterator <= value" respectively.
626 static __isl_give isl_ast_graft *refine_degenerate(
627 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build,
628 __isl_keep isl_ast_build *sub_build)
630 isl_pw_aff *value;
632 if (!graft || !sub_build)
633 return isl_ast_graft_free(graft);
635 value = isl_pw_aff_copy(sub_build->value);
637 graft->node->u.f.init = isl_ast_build_expr_from_pw_aff_internal(build,
638 value);
639 if (!graft->node->u.f.init)
640 return isl_ast_graft_free(graft);
642 return graft;
645 /* Return the intersection of constraints in "list" as a set.
647 static __isl_give isl_set *intersect_constraints(
648 __isl_keep isl_constraint_list *list)
650 int i, n;
651 isl_basic_set *bset;
653 n = isl_constraint_list_n_constraint(list);
654 if (n < 1)
655 isl_die(isl_constraint_list_get_ctx(list), isl_error_internal,
656 "expecting at least one constraint", return NULL);
658 bset = isl_basic_set_from_constraint(
659 isl_constraint_list_get_constraint(list, 0));
660 for (i = 1; i < n; ++i) {
661 isl_basic_set *bset_i;
663 bset_i = isl_basic_set_from_constraint(
664 isl_constraint_list_get_constraint(list, i));
665 bset = isl_basic_set_intersect(bset, bset_i);
668 return isl_set_from_basic_set(bset);
671 /* Compute the constraints on the outer dimensions enforced by
672 * graft->node and add those constraints to graft->enforced,
673 * in case the upper bound is expressed as a set "upper".
675 * In particular, if l(...) is a lower bound in "lower", and
677 * -a i + f(...) >= 0 or a i <= f(...)
679 * is an upper bound ocnstraint on the current dimension i,
680 * then the for loop enforces the constraint
682 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
684 * We therefore simply take each lower bound in turn, plug it into
685 * the upper bounds and compute the intersection over all lower bounds.
687 * If a lower bound is a rational expression, then
688 * isl_basic_set_preimage_multi_aff will force this rational
689 * expression to have only integer values. However, the loop
690 * itself does not enforce this integrality constraint. We therefore
691 * use the ceil of the lower bounds instead of the lower bounds themselves.
692 * Other constraints will make sure that the for loop is only executed
693 * when each of the lower bounds attains an integral value.
694 * In particular, potentially rational values only occur in
695 * lower_bound if the offset is a (seemingly) rational expression,
696 * but then outer conditions will make sure that this rational expression
697 * only attains integer values.
699 static __isl_give isl_ast_graft *set_enforced_from_set(
700 __isl_take isl_ast_graft *graft,
701 __isl_keep isl_pw_aff_list *lower, int pos, __isl_keep isl_set *upper)
703 isl_space *space;
704 isl_basic_set *enforced;
705 isl_pw_multi_aff *pma;
706 int i, n;
708 if (!graft || !lower)
709 return isl_ast_graft_free(graft);
711 space = isl_set_get_space(upper);
712 enforced = isl_basic_set_universe(isl_space_copy(space));
714 space = isl_space_map_from_set(space);
715 pma = isl_pw_multi_aff_identity(space);
717 n = isl_pw_aff_list_n_pw_aff(lower);
718 for (i = 0; i < n; ++i) {
719 isl_pw_aff *pa;
720 isl_set *enforced_i;
721 isl_basic_set *hull;
722 isl_pw_multi_aff *pma_i;
724 pa = isl_pw_aff_list_get_pw_aff(lower, i);
725 pa = isl_pw_aff_ceil(pa);
726 pma_i = isl_pw_multi_aff_copy(pma);
727 pma_i = isl_pw_multi_aff_set_pw_aff(pma_i, pos, pa);
728 enforced_i = isl_set_copy(upper);
729 enforced_i = isl_set_preimage_pw_multi_aff(enforced_i, pma_i);
730 hull = isl_set_simple_hull(enforced_i);
731 enforced = isl_basic_set_intersect(enforced, hull);
734 isl_pw_multi_aff_free(pma);
736 graft = isl_ast_graft_enforce(graft, enforced);
738 return graft;
741 /* Compute the constraints on the outer dimensions enforced by
742 * graft->node and add those constraints to graft->enforced,
743 * in case the upper bound is expressed as
744 * a list of affine expressions "upper".
746 * The enforced condition is that each lower bound expression is less
747 * than or equal to each upper bound expression.
749 static __isl_give isl_ast_graft *set_enforced_from_list(
750 __isl_take isl_ast_graft *graft,
751 __isl_keep isl_pw_aff_list *lower, __isl_keep isl_pw_aff_list *upper)
753 isl_set *cond;
754 isl_basic_set *enforced;
756 lower = isl_pw_aff_list_copy(lower);
757 upper = isl_pw_aff_list_copy(upper);
758 cond = isl_pw_aff_list_le_set(lower, upper);
759 enforced = isl_set_simple_hull(cond);
760 graft = isl_ast_graft_enforce(graft, enforced);
762 return graft;
765 /* Does "aff" have a negative constant term?
767 static int aff_constant_is_negative(__isl_take isl_set *set,
768 __isl_take isl_aff *aff, void *user)
770 int *neg = user;
771 isl_val *v;
773 v = isl_aff_get_constant_val(aff);
774 *neg = isl_val_is_neg(v);
775 isl_val_free(v);
776 isl_set_free(set);
777 isl_aff_free(aff);
779 return *neg ? 0 : -1;
782 /* Does "pa" have a negative constant term over its entire domain?
784 static int pw_aff_constant_is_negative(__isl_take isl_pw_aff *pa, void *user)
786 int r;
787 int *neg = user;
789 r = isl_pw_aff_foreach_piece(pa, &aff_constant_is_negative, user);
790 isl_pw_aff_free(pa);
792 return *neg ? 0 : -1;
795 /* Does each element in "list" have a negative constant term?
797 * The callback terminates the iteration as soon an element has been
798 * found that does not have a negative constant term.
800 static int list_constant_is_negative(__isl_keep isl_pw_aff_list *list)
802 int neg = 1;
804 if (isl_pw_aff_list_foreach(list,
805 &pw_aff_constant_is_negative, &neg) < 0 && neg)
806 return -1;
808 return neg;
811 /* Add 1 to each of the elements in "list", where each of these elements
812 * is defined over the internal schedule space of "build".
814 static __isl_give isl_pw_aff_list *list_add_one(
815 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
817 int i, n;
818 isl_space *space;
819 isl_aff *aff;
820 isl_pw_aff *one;
822 space = isl_ast_build_get_space(build, 1);
823 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
824 aff = isl_aff_add_constant_si(aff, 1);
825 one = isl_pw_aff_from_aff(aff);
827 n = isl_pw_aff_list_n_pw_aff(list);
828 for (i = 0; i < n; ++i) {
829 isl_pw_aff *pa;
830 pa = isl_pw_aff_list_get_pw_aff(list, i);
831 pa = isl_pw_aff_add(pa, isl_pw_aff_copy(one));
832 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
835 isl_pw_aff_free(one);
837 return list;
840 /* Set the condition part of the for node graft->node in case
841 * the upper bound is represented as a list of piecewise affine expressions.
843 * In particular, set the condition to
845 * iterator <= min(list of upper bounds)
847 * If each of the upper bounds has a negative constant term, then
848 * set the condition to
850 * iterator < min(list of (upper bound + 1)s)
853 static __isl_give isl_ast_graft *set_for_cond_from_list(
854 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *list,
855 __isl_keep isl_ast_build *build)
857 int neg;
858 isl_ast_expr *bound, *iterator, *cond;
859 enum isl_ast_op_type type = isl_ast_op_le;
861 if (!graft || !list)
862 return isl_ast_graft_free(graft);
864 neg = list_constant_is_negative(list);
865 if (neg < 0)
866 return isl_ast_graft_free(graft);
867 list = isl_pw_aff_list_copy(list);
868 if (neg) {
869 list = list_add_one(list, build);
870 type = isl_ast_op_lt;
873 bound = reduce_list(isl_ast_op_min, list, build);
874 iterator = isl_ast_expr_copy(graft->node->u.f.iterator);
875 cond = isl_ast_expr_alloc_binary(type, iterator, bound);
876 graft->node->u.f.cond = cond;
878 isl_pw_aff_list_free(list);
879 if (!graft->node->u.f.cond)
880 return isl_ast_graft_free(graft);
881 return graft;
884 /* Set the condition part of the for node graft->node in case
885 * the upper bound is represented as a set.
887 static __isl_give isl_ast_graft *set_for_cond_from_set(
888 __isl_take isl_ast_graft *graft, __isl_keep isl_set *set,
889 __isl_keep isl_ast_build *build)
891 isl_ast_expr *cond;
893 if (!graft)
894 return NULL;
896 cond = isl_ast_build_expr_from_set(build, isl_set_copy(set));
897 graft->node->u.f.cond = cond;
898 if (!graft->node->u.f.cond)
899 return isl_ast_graft_free(graft);
900 return graft;
903 /* Construct an isl_ast_expr for the increment (i.e., stride) of
904 * the current dimension.
906 static __isl_give isl_ast_expr *for_inc(__isl_keep isl_ast_build *build)
908 int depth;
909 isl_val *v;
910 isl_ctx *ctx;
912 if (!build)
913 return NULL;
914 ctx = isl_ast_build_get_ctx(build);
915 depth = isl_ast_build_get_depth(build);
917 if (!isl_ast_build_has_stride(build, depth))
918 return isl_ast_expr_alloc_int_si(ctx, 1);
920 v = isl_ast_build_get_stride(build, depth);
921 return isl_ast_expr_from_val(v);
924 /* Should we express the loop condition as
926 * iterator <= min(list of upper bounds)
928 * or as a conjunction of constraints?
930 * The first is constructed from a list of upper bounds.
931 * The second is constructed from a set.
933 * If there are no upper bounds in "constraints", then this could mean
934 * that "domain" simply doesn't have an upper bound or that we didn't
935 * pick any upper bound. In the first case, we want to generate the
936 * loop condition as a(n empty) conjunction of constraints
937 * In the second case, we will compute
938 * a single upper bound from "domain" and so we use the list form.
940 * If there are upper bounds in "constraints",
941 * then we use the list form iff the atomic_upper_bound option is set.
943 static int use_upper_bound_list(isl_ctx *ctx, int n_upper,
944 __isl_keep isl_set *domain, int depth)
946 if (n_upper > 0)
947 return isl_options_get_ast_build_atomic_upper_bound(ctx);
948 else
949 return isl_set_dim_has_upper_bound(domain, isl_dim_set, depth);
952 /* Fill in the expressions of the for node in graft->node.
954 * In particular,
955 * - set the initialization part of the loop to the maximum of the lower bounds
956 * - extract the increment from the stride of the current dimension
957 * - construct the for condition either based on a list of upper bounds
958 * or on a set of upper bound constraints.
960 static __isl_give isl_ast_graft *set_for_node_expressions(
961 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *lower,
962 int use_list, __isl_keep isl_pw_aff_list *upper_list,
963 __isl_keep isl_set *upper_set, __isl_keep isl_ast_build *build)
965 isl_ast_node *node;
967 if (!graft)
968 return NULL;
970 build = isl_ast_build_copy(build);
972 node = graft->node;
973 node->u.f.init = reduce_list(isl_ast_op_max, lower, build);
974 node->u.f.inc = for_inc(build);
976 if (use_list)
977 graft = set_for_cond_from_list(graft, upper_list, build);
978 else
979 graft = set_for_cond_from_set(graft, upper_set, build);
981 isl_ast_build_free(build);
983 if (!node->u.f.iterator || !node->u.f.init ||
984 !node->u.f.cond || !node->u.f.inc)
985 return isl_ast_graft_free(graft);
987 return graft;
990 /* Update "graft" based on "bounds" and "domain" for the generic,
991 * non-degenerate, case.
993 * "c_lower" and "c_upper" contain the lower and upper bounds
994 * that the loop node should express.
995 * "domain" is the subset of the intersection of the constraints
996 * for which some code is executed.
998 * There may be zero lower bounds or zero upper bounds in "constraints"
999 * in case the list of constraints was created
1000 * based on the atomic option or based on separation with explicit bounds.
1001 * In that case, we use "domain" to derive lower and/or upper bounds.
1003 * We first compute a list of one or more lower bounds.
1005 * Then we decide if we want to express the condition as
1007 * iterator <= min(list of upper bounds)
1009 * or as a conjunction of constraints.
1011 * The set of enforced constraints is then computed either based on
1012 * a list of upper bounds or on a set of upper bound constraints.
1013 * We do not compute any enforced constraints if we were forced
1014 * to compute a lower or upper bound using exact_bound. The domains
1015 * of the resulting expressions may imply some bounds on outer dimensions
1016 * that we do not want to appear in the enforced constraints since
1017 * they are not actually enforced by the corresponding code.
1019 * Finally, we fill in the expressions of the for node.
1021 static __isl_give isl_ast_graft *refine_generic_bounds(
1022 __isl_take isl_ast_graft *graft,
1023 __isl_take isl_constraint_list *c_lower,
1024 __isl_take isl_constraint_list *c_upper,
1025 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1027 int depth;
1028 isl_ctx *ctx;
1029 isl_pw_aff_list *lower;
1030 int use_list;
1031 isl_set *upper_set = NULL;
1032 isl_pw_aff_list *upper_list = NULL;
1033 int n_lower, n_upper;
1035 if (!graft || !c_lower || !c_upper || !build)
1036 goto error;
1038 depth = isl_ast_build_get_depth(build);
1039 ctx = isl_ast_graft_get_ctx(graft);
1041 n_lower = isl_constraint_list_n_constraint(c_lower);
1042 n_upper = isl_constraint_list_n_constraint(c_upper);
1044 use_list = use_upper_bound_list(ctx, n_upper, domain, depth);
1046 lower = lower_bounds(c_lower, depth, domain, build);
1048 if (use_list)
1049 upper_list = upper_bounds(c_upper, depth, domain, build);
1050 else if (n_upper > 0)
1051 upper_set = intersect_constraints(c_upper);
1052 else
1053 upper_set = isl_set_universe(isl_set_get_space(domain));
1055 if (n_lower == 0 || n_upper == 0)
1057 else if (use_list)
1058 graft = set_enforced_from_list(graft, lower, upper_list);
1059 else
1060 graft = set_enforced_from_set(graft, lower, depth, upper_set);
1062 graft = set_for_node_expressions(graft, lower, use_list, upper_list,
1063 upper_set, build);
1065 isl_pw_aff_list_free(lower);
1066 isl_pw_aff_list_free(upper_list);
1067 isl_set_free(upper_set);
1068 isl_constraint_list_free(c_lower);
1069 isl_constraint_list_free(c_upper);
1071 return graft;
1072 error:
1073 isl_constraint_list_free(c_lower);
1074 isl_constraint_list_free(c_upper);
1075 return isl_ast_graft_free(graft);
1078 /* Internal data structure used inside count_constraints to keep
1079 * track of the number of constraints that are independent of dimension "pos",
1080 * the lower bounds in "pos" and the upper bounds in "pos".
1082 struct isl_ast_count_constraints_data {
1083 int pos;
1085 int n_indep;
1086 int n_lower;
1087 int n_upper;
1090 /* Increment data->n_indep, data->lower or data->upper depending
1091 * on whether "c" is independenct of dimensions data->pos,
1092 * a lower bound or an upper bound.
1094 static int count_constraints(__isl_take isl_constraint *c, void *user)
1096 struct isl_ast_count_constraints_data *data = user;
1098 if (isl_constraint_is_lower_bound(c, isl_dim_set, data->pos))
1099 data->n_lower++;
1100 else if (isl_constraint_is_upper_bound(c, isl_dim_set, data->pos))
1101 data->n_upper++;
1102 else
1103 data->n_indep++;
1105 isl_constraint_free(c);
1107 return 0;
1110 /* Update "graft" based on "bounds" and "domain" for the generic,
1111 * non-degenerate, case.
1113 * "list" respresent the list of bounds that need to be encoded by
1114 * the for loop. Only the constraints that involve the iterator
1115 * are relevant here. The other constraints are taken care of by
1116 * the caller and are included in the generated constraints of "build".
1117 * "domain" is the subset of the intersection of the constraints
1118 * for which some code is executed.
1119 * "build" is the build in which graft->node was created.
1121 * We separate lower bounds, upper bounds and constraints that
1122 * are independent of the loop iterator.
1124 * The actual for loop bounds are generated in refine_generic_bounds.
1126 static __isl_give isl_ast_graft *refine_generic_split(
1127 __isl_take isl_ast_graft *graft, __isl_take isl_constraint_list *list,
1128 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1130 struct isl_ast_count_constraints_data data;
1131 isl_constraint_list *lower;
1132 isl_constraint_list *upper;
1134 if (!list)
1135 return isl_ast_graft_free(graft);
1137 data.pos = isl_ast_build_get_depth(build);
1139 list = isl_constraint_list_sort(list, &cmp_constraint, &data.pos);
1140 if (!list)
1141 return isl_ast_graft_free(graft);
1143 data.n_indep = data.n_lower = data.n_upper = 0;
1144 if (isl_constraint_list_foreach(list, &count_constraints, &data) < 0) {
1145 isl_constraint_list_free(list);
1146 return isl_ast_graft_free(graft);
1149 lower = isl_constraint_list_drop(list, 0, data.n_indep);
1150 upper = isl_constraint_list_copy(lower);
1151 lower = isl_constraint_list_drop(lower, data.n_lower, data.n_upper);
1152 upper = isl_constraint_list_drop(upper, 0, data.n_lower);
1154 return refine_generic_bounds(graft, lower, upper, domain, build);
1157 /* Update "graft" based on "bounds" and "domain" for the generic,
1158 * non-degenerate, case.
1160 * "bounds" respresent the bounds that need to be encoded by
1161 * the for loop (or a guard around the for loop).
1162 * "domain" is the subset of "bounds" for which some code is executed.
1163 * "build" is the build in which graft->node was created.
1165 * We break up "bounds" into a list of constraints and continue with
1166 * refine_generic_split.
1168 static __isl_give isl_ast_graft *refine_generic(
1169 __isl_take isl_ast_graft *graft,
1170 __isl_keep isl_basic_set *bounds, __isl_keep isl_set *domain,
1171 __isl_keep isl_ast_build *build)
1173 isl_constraint_list *list;
1175 if (!build || !graft)
1176 return isl_ast_graft_free(graft);
1178 list = isl_basic_set_get_constraint_list(bounds);
1180 graft = refine_generic_split(graft, list, domain, build);
1182 return graft;
1185 /* Create a for node for the current level.
1187 * Mark the for node degenerate if "degenerate" is set.
1189 static __isl_give isl_ast_node *create_for(__isl_keep isl_ast_build *build,
1190 int degenerate)
1192 int depth;
1193 isl_id *id;
1194 isl_ast_node *node;
1196 if (!build)
1197 return NULL;
1199 depth = isl_ast_build_get_depth(build);
1200 id = isl_ast_build_get_iterator_id(build, depth);
1201 node = isl_ast_node_alloc_for(id);
1202 if (degenerate)
1203 node = isl_ast_node_for_mark_degenerate(node);
1205 return node;
1208 /* If the ast_build_exploit_nested_bounds option is set, then return
1209 * the constraints enforced by all elements in "list".
1210 * Otherwise, return the universe.
1212 static __isl_give isl_basic_set *extract_shared_enforced(
1213 __isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
1215 isl_ctx *ctx;
1216 isl_space *space;
1218 if (!list)
1219 return NULL;
1221 ctx = isl_ast_graft_list_get_ctx(list);
1222 if (isl_options_get_ast_build_exploit_nested_bounds(ctx))
1223 return isl_ast_graft_list_extract_shared_enforced(list, build);
1225 space = isl_ast_build_get_space(build, 1);
1226 return isl_basic_set_universe(space);
1229 /* Return the pending constraints of "build" that are not already taken
1230 * care of (by a combination of "enforced" and the generated constraints
1231 * of "build").
1233 static __isl_give isl_set *extract_pending(__isl_keep isl_ast_build *build,
1234 __isl_keep isl_basic_set *enforced)
1236 isl_set *guard, *context;
1238 guard = isl_ast_build_get_pending(build);
1239 context = isl_set_from_basic_set(isl_basic_set_copy(enforced));
1240 context = isl_set_intersect(context,
1241 isl_ast_build_get_generated(build));
1242 return isl_set_gist(guard, context);
1245 /* Create an AST node for the current dimension based on
1246 * the schedule domain "bounds" and return the node encapsulated
1247 * in an isl_ast_graft.
1249 * "executed" is the current inverse schedule, taking into account
1250 * the bounds in "bounds"
1251 * "domain" is the domain of "executed", with inner dimensions projected out.
1252 * It may be a strict subset of "bounds" in case "bounds" was created
1253 * based on the atomic option or based on separation with explicit bounds.
1255 * "domain" may satisfy additional equalities that result
1256 * from intersecting "executed" with "bounds" in add_node.
1257 * It may also satisfy some global constraints that were dropped out because
1258 * we performed separation with explicit bounds.
1259 * The very first step is then to copy these constraints to "bounds".
1261 * Since we may be calling before_each_for and after_each_for
1262 * callbacks, we record the current inverse schedule in the build.
1264 * We consider three builds,
1265 * "build" is the one in which the current level is created,
1266 * "body_build" is the build in which the next level is created,
1267 * "sub_build" is essentially the same as "body_build", except that
1268 * the depth has not been increased yet.
1270 * "build" already contains information (in strides and offsets)
1271 * about the strides at the current level, but this information is not
1272 * reflected in the build->domain.
1273 * We first add this information and the "bounds" to the sub_build->domain.
1274 * isl_ast_build_set_loop_bounds adds the stride information and
1275 * checks whether the current dimension attains
1276 * only a single value and whether this single value can be represented using
1277 * a single affine expression.
1278 * In the first case, the current level is considered "degenerate".
1279 * In the second, sub-case, the current level is considered "eliminated".
1280 * Eliminated levels don't need to be reflected in the AST since we can
1281 * simply plug in the affine expression. For degenerate, but non-eliminated,
1282 * levels, we do introduce a for node, but mark is as degenerate so that
1283 * it can be printed as an assignment of the single value to the loop
1284 * "iterator".
1286 * If the current level is eliminated, we explicitly plug in the value
1287 * for the current level found by isl_ast_build_set_loop_bounds in the
1288 * inverse schedule. This ensures that if we are working on a slice
1289 * of the domain based on information available in the inverse schedule
1290 * and the build domain, that then this information is also reflected
1291 * in the inverse schedule. This operation also eliminates the current
1292 * dimension from the inverse schedule making sure no inner dimensions depend
1293 * on the current dimension. Otherwise, we create a for node, marking
1294 * it degenerate if appropriate. The initial for node is still incomplete
1295 * and will be completed in either refine_degenerate or refine_generic.
1297 * We then generate a sequence of grafts for the next level,
1298 * create a surrounding graft for the current level and insert
1299 * the for node we created (if the current level is not eliminated).
1300 * Before creating a graft for the current level, we first extract
1301 * hoistable constraints from the child guards and combine them
1302 * with the pending constraints in the build. These constraints
1303 * are used to simplify the child guards and then added to the guard
1304 * of the current graft to ensure that they will be generated.
1305 * If the hoisted guard is a disjunction, then we use it directly
1306 * to gist the guards on the children before intersect it with the
1307 * pending constraints. We do so because this disjunction is typically
1308 * identical to the guards on the children such that these guards
1309 * can be effectively removed completely. After the intersection,
1310 * the gist operation would have a harder time figuring this out.
1312 * Finally, we set the bounds of the for loop in either
1313 * refine_degenerate or refine_generic.
1314 * We do so in a context where the pending constraints of the build
1315 * have been replaced by the guard of the current graft.
1317 static __isl_give isl_ast_graft *create_node_scaled(
1318 __isl_take isl_union_map *executed,
1319 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1320 __isl_take isl_ast_build *build)
1322 int depth;
1323 int degenerate, eliminated;
1324 isl_basic_set *hull;
1325 isl_basic_set *enforced;
1326 isl_set *guard, *hoisted;
1327 isl_ast_node *node = NULL;
1328 isl_ast_graft *graft;
1329 isl_ast_graft_list *children;
1330 isl_ast_build *sub_build;
1331 isl_ast_build *body_build;
1333 domain = isl_ast_build_eliminate_divs(build, domain);
1334 domain = isl_set_detect_equalities(domain);
1335 hull = isl_set_unshifted_simple_hull(isl_set_copy(domain));
1336 bounds = isl_basic_set_intersect(bounds, hull);
1337 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
1339 depth = isl_ast_build_get_depth(build);
1340 sub_build = isl_ast_build_copy(build);
1341 sub_build = isl_ast_build_set_loop_bounds(sub_build,
1342 isl_basic_set_copy(bounds));
1343 degenerate = isl_ast_build_has_value(sub_build);
1344 eliminated = isl_ast_build_has_affine_value(sub_build, depth);
1345 if (degenerate < 0 || eliminated < 0)
1346 executed = isl_union_map_free(executed);
1347 if (eliminated)
1348 executed = plug_in_values(executed, sub_build);
1349 else
1350 node = create_for(build, degenerate);
1352 body_build = isl_ast_build_copy(sub_build);
1353 body_build = isl_ast_build_increase_depth(body_build);
1354 if (!eliminated)
1355 node = before_each_for(node, body_build);
1356 children = generate_next_level(executed,
1357 isl_ast_build_copy(body_build));
1359 enforced = extract_shared_enforced(children, build);
1360 guard = extract_pending(sub_build, enforced);
1361 hoisted = isl_ast_graft_list_extract_hoistable_guard(children, build);
1362 if (isl_set_n_basic_set(hoisted) > 1)
1363 children = isl_ast_graft_list_gist_guards(children,
1364 isl_set_copy(hoisted));
1365 guard = isl_set_intersect(guard, hoisted);
1367 graft = isl_ast_graft_alloc_from_children(children,
1368 isl_set_copy(guard), enforced, build, sub_build);
1370 if (!degenerate)
1371 bounds = isl_ast_build_compute_gist_basic_set(build, bounds);
1372 if (!eliminated) {
1373 isl_ast_build *for_build;
1375 graft = isl_ast_graft_insert_for(graft, node);
1376 for_build = isl_ast_build_copy(build);
1377 for_build = isl_ast_build_replace_pending_by_guard(for_build,
1378 isl_set_copy(guard));
1379 if (degenerate)
1380 graft = refine_degenerate(graft, for_build, sub_build);
1381 else
1382 graft = refine_generic(graft, bounds,
1383 domain, for_build);
1384 isl_ast_build_free(for_build);
1386 isl_set_free(guard);
1387 if (!eliminated) {
1388 graft = add_implied_guards(graft, degenerate, bounds, build);
1389 graft = after_each_for(graft, body_build);
1392 isl_ast_build_free(body_build);
1393 isl_ast_build_free(sub_build);
1394 isl_ast_build_free(build);
1395 isl_basic_set_free(bounds);
1396 isl_set_free(domain);
1398 return graft;
1401 /* Internal data structure for checking if all constraints involving
1402 * the input dimension "depth" are such that the other coefficients
1403 * are multiples of "m", reducing "m" if they are not.
1404 * If "m" is reduced all the way down to "1", then the check has failed
1405 * and we break out of the iteration.
1407 struct isl_check_scaled_data {
1408 int depth;
1409 isl_val *m;
1412 /* If constraint "c" involves the input dimension data->depth,
1413 * then make sure that all the other coefficients are multiples of data->m,
1414 * reducing data->m if needed.
1415 * Break out of the iteration if data->m has become equal to "1".
1417 static int constraint_check_scaled(__isl_take isl_constraint *c, void *user)
1419 struct isl_check_scaled_data *data = user;
1420 int i, j, n;
1421 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_out,
1422 isl_dim_div };
1424 if (!isl_constraint_involves_dims(c, isl_dim_in, data->depth, 1)) {
1425 isl_constraint_free(c);
1426 return 0;
1429 for (i = 0; i < 4; ++i) {
1430 n = isl_constraint_dim(c, t[i]);
1431 for (j = 0; j < n; ++j) {
1432 isl_val *d;
1434 if (t[i] == isl_dim_in && j == data->depth)
1435 continue;
1436 if (!isl_constraint_involves_dims(c, t[i], j, 1))
1437 continue;
1438 d = isl_constraint_get_coefficient_val(c, t[i], j);
1439 data->m = isl_val_gcd(data->m, d);
1440 if (isl_val_is_one(data->m))
1441 break;
1443 if (j < n)
1444 break;
1447 isl_constraint_free(c);
1449 return i < 4 ? -1 : 0;
1452 /* For each constraint of "bmap" that involves the input dimension data->depth,
1453 * make sure that all the other coefficients are multiples of data->m,
1454 * reducing data->m if needed.
1455 * Break out of the iteration if data->m has become equal to "1".
1457 static int basic_map_check_scaled(__isl_take isl_basic_map *bmap, void *user)
1459 int r;
1461 r = isl_basic_map_foreach_constraint(bmap,
1462 &constraint_check_scaled, user);
1463 isl_basic_map_free(bmap);
1465 return r;
1468 /* For each constraint of "map" that involves the input dimension data->depth,
1469 * make sure that all the other coefficients are multiples of data->m,
1470 * reducing data->m if needed.
1471 * Break out of the iteration if data->m has become equal to "1".
1473 static int map_check_scaled(__isl_take isl_map *map, void *user)
1475 int r;
1477 r = isl_map_foreach_basic_map(map, &basic_map_check_scaled, user);
1478 isl_map_free(map);
1480 return r;
1483 /* Create an AST node for the current dimension based on
1484 * the schedule domain "bounds" and return the node encapsulated
1485 * in an isl_ast_graft.
1487 * "executed" is the current inverse schedule, taking into account
1488 * the bounds in "bounds"
1489 * "domain" is the domain of "executed", with inner dimensions projected out.
1492 * Before moving on to the actual AST node construction in create_node_scaled,
1493 * we first check if the current dimension is strided and if we can scale
1494 * down this stride. Note that we only do this if the ast_build_scale_strides
1495 * option is set.
1497 * In particular, let the current dimension take on values
1499 * f + s a
1501 * with a an integer. We check if we can find an integer m that (obviously)
1502 * divides both f and s.
1504 * If so, we check if the current dimension only appears in constraints
1505 * where the coefficients of the other variables are multiples of m.
1506 * We perform this extra check to avoid the risk of introducing
1507 * divisions by scaling down the current dimension.
1509 * If so, we scale the current dimension down by a factor of m.
1510 * That is, we plug in
1512 * i = m i' (1)
1514 * Note that in principle we could always scale down strided loops
1515 * by plugging in
1517 * i = f + s i'
1519 * but this may result in i' taking on larger values than the original i,
1520 * due to the shift by "f".
1521 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1523 static __isl_give isl_ast_graft *create_node(__isl_take isl_union_map *executed,
1524 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1525 __isl_take isl_ast_build *build)
1527 struct isl_check_scaled_data data;
1528 isl_ctx *ctx;
1529 isl_aff *offset;
1530 isl_val *d;
1532 ctx = isl_ast_build_get_ctx(build);
1533 if (!isl_options_get_ast_build_scale_strides(ctx))
1534 return create_node_scaled(executed, bounds, domain, build);
1536 data.depth = isl_ast_build_get_depth(build);
1537 if (!isl_ast_build_has_stride(build, data.depth))
1538 return create_node_scaled(executed, bounds, domain, build);
1540 offset = isl_ast_build_get_offset(build, data.depth);
1541 data.m = isl_ast_build_get_stride(build, data.depth);
1542 if (!data.m)
1543 offset = isl_aff_free(offset);
1544 offset = isl_aff_scale_down_val(offset, isl_val_copy(data.m));
1545 d = isl_aff_get_denominator_val(offset);
1546 if (!d)
1547 executed = isl_union_map_free(executed);
1549 if (executed && isl_val_is_divisible_by(data.m, d))
1550 data.m = isl_val_div(data.m, d);
1551 else {
1552 data.m = isl_val_set_si(data.m, 1);
1553 isl_val_free(d);
1556 if (!isl_val_is_one(data.m)) {
1557 if (isl_union_map_foreach_map(executed, &map_check_scaled,
1558 &data) < 0 &&
1559 !isl_val_is_one(data.m))
1560 executed = isl_union_map_free(executed);
1563 if (!isl_val_is_one(data.m)) {
1564 isl_space *space;
1565 isl_multi_aff *ma;
1566 isl_aff *aff;
1567 isl_map *map;
1568 isl_union_map *umap;
1570 space = isl_ast_build_get_space(build, 1);
1571 space = isl_space_map_from_set(space);
1572 ma = isl_multi_aff_identity(space);
1573 aff = isl_multi_aff_get_aff(ma, data.depth);
1574 aff = isl_aff_scale_val(aff, isl_val_copy(data.m));
1575 ma = isl_multi_aff_set_aff(ma, data.depth, aff);
1577 bounds = isl_basic_set_preimage_multi_aff(bounds,
1578 isl_multi_aff_copy(ma));
1579 domain = isl_set_preimage_multi_aff(domain,
1580 isl_multi_aff_copy(ma));
1581 map = isl_map_reverse(isl_map_from_multi_aff(ma));
1582 umap = isl_union_map_from_map(map);
1583 executed = isl_union_map_apply_domain(executed,
1584 isl_union_map_copy(umap));
1585 build = isl_ast_build_scale_down(build, isl_val_copy(data.m),
1586 umap);
1588 isl_aff_free(offset);
1589 isl_val_free(data.m);
1591 return create_node_scaled(executed, bounds, domain, build);
1594 /* Add the basic set to the list that "user" points to.
1596 static int collect_basic_set(__isl_take isl_basic_set *bset, void *user)
1598 isl_basic_set_list **list = user;
1600 *list = isl_basic_set_list_add(*list, bset);
1602 return 0;
1605 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1607 static __isl_give isl_basic_set_list *isl_basic_set_list_from_set(
1608 __isl_take isl_set *set)
1610 int n;
1611 isl_ctx *ctx;
1612 isl_basic_set_list *list;
1614 if (!set)
1615 return NULL;
1617 ctx = isl_set_get_ctx(set);
1619 n = isl_set_n_basic_set(set);
1620 list = isl_basic_set_list_alloc(ctx, n);
1621 if (isl_set_foreach_basic_set(set, &collect_basic_set, &list) < 0)
1622 list = isl_basic_set_list_free(list);
1624 isl_set_free(set);
1625 return list;
1628 /* Generate code for the schedule domain "bounds"
1629 * and add the result to "list".
1631 * We mainly detect strides here and check if the bounds do not
1632 * conflict with the current build domain
1633 * and then pass over control to create_node.
1635 * "bounds" reflects the bounds on the current dimension and possibly
1636 * some extra conditions on outer dimensions.
1637 * It does not, however, include any divs involving the current dimension,
1638 * so it does not capture any stride constraints.
1639 * We therefore need to compute that part of the schedule domain that
1640 * intersects with "bounds" and derive the strides from the result.
1642 static __isl_give isl_ast_graft_list *add_node(
1643 __isl_take isl_ast_graft_list *list, __isl_take isl_union_map *executed,
1644 __isl_take isl_basic_set *bounds, __isl_take isl_ast_build *build)
1646 isl_ast_graft *graft;
1647 isl_set *domain = NULL;
1648 isl_union_set *uset;
1649 int empty, disjoint;
1651 uset = isl_union_set_from_basic_set(isl_basic_set_copy(bounds));
1652 executed = isl_union_map_intersect_domain(executed, uset);
1653 empty = isl_union_map_is_empty(executed);
1654 if (empty < 0)
1655 goto error;
1656 if (empty)
1657 goto done;
1659 uset = isl_union_map_domain(isl_union_map_copy(executed));
1660 domain = isl_set_from_union_set(uset);
1661 domain = isl_ast_build_specialize(build, domain);
1663 domain = isl_set_compute_divs(domain);
1664 domain = isl_ast_build_eliminate_inner(build, domain);
1665 disjoint = isl_set_is_disjoint(domain, build->domain);
1666 if (disjoint < 0)
1667 goto error;
1668 if (disjoint)
1669 goto done;
1671 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
1673 graft = create_node(executed, bounds, domain,
1674 isl_ast_build_copy(build));
1675 list = isl_ast_graft_list_add(list, graft);
1676 isl_ast_build_free(build);
1677 return list;
1678 error:
1679 list = isl_ast_graft_list_free(list);
1680 done:
1681 isl_set_free(domain);
1682 isl_basic_set_free(bounds);
1683 isl_union_map_free(executed);
1684 isl_ast_build_free(build);
1685 return list;
1688 /* Does any element of i follow or coincide with any element of j
1689 * at the current depth for equal values of the outer dimensions?
1691 static int domain_follows_at_depth(__isl_keep isl_basic_set *i,
1692 __isl_keep isl_basic_set *j, void *user)
1694 int depth = *(int *) user;
1695 isl_basic_map *test;
1696 int empty;
1697 int l;
1699 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1700 isl_basic_set_copy(j));
1701 for (l = 0; l < depth; ++l)
1702 test = isl_basic_map_equate(test, isl_dim_in, l,
1703 isl_dim_out, l);
1704 test = isl_basic_map_order_ge(test, isl_dim_in, depth,
1705 isl_dim_out, depth);
1706 empty = isl_basic_map_is_empty(test);
1707 isl_basic_map_free(test);
1709 return empty < 0 ? -1 : !empty;
1712 /* Split up each element of "list" into a part that is related to "bset"
1713 * according to "gt" and a part that is not.
1714 * Return a list that consist of "bset" and all the pieces.
1716 static __isl_give isl_basic_set_list *add_split_on(
1717 __isl_take isl_basic_set_list *list, __isl_take isl_basic_set *bset,
1718 __isl_keep isl_basic_map *gt)
1720 int i, n;
1721 isl_basic_set_list *res;
1723 if (!list)
1724 bset = isl_basic_set_free(bset);
1726 gt = isl_basic_map_copy(gt);
1727 gt = isl_basic_map_intersect_domain(gt, isl_basic_set_copy(bset));
1728 n = isl_basic_set_list_n_basic_set(list);
1729 res = isl_basic_set_list_from_basic_set(bset);
1730 for (i = 0; res && i < n; ++i) {
1731 isl_basic_set *bset;
1732 isl_set *set1, *set2;
1733 isl_basic_map *bmap;
1734 int empty;
1736 bset = isl_basic_set_list_get_basic_set(list, i);
1737 bmap = isl_basic_map_copy(gt);
1738 bmap = isl_basic_map_intersect_range(bmap, bset);
1739 bset = isl_basic_map_range(bmap);
1740 empty = isl_basic_set_is_empty(bset);
1741 if (empty < 0)
1742 res = isl_basic_set_list_free(res);
1743 if (empty) {
1744 isl_basic_set_free(bset);
1745 bset = isl_basic_set_list_get_basic_set(list, i);
1746 res = isl_basic_set_list_add(res, bset);
1747 continue;
1750 res = isl_basic_set_list_add(res, isl_basic_set_copy(bset));
1751 set1 = isl_set_from_basic_set(bset);
1752 bset = isl_basic_set_list_get_basic_set(list, i);
1753 set2 = isl_set_from_basic_set(bset);
1754 set1 = isl_set_subtract(set2, set1);
1755 set1 = isl_set_make_disjoint(set1);
1757 res = isl_basic_set_list_concat(res,
1758 isl_basic_set_list_from_set(set1));
1760 isl_basic_map_free(gt);
1761 isl_basic_set_list_free(list);
1762 return res;
1765 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1766 __isl_keep isl_basic_set_list *domain_list,
1767 __isl_keep isl_union_map *executed,
1768 __isl_keep isl_ast_build *build);
1770 /* Internal data structure for add_nodes.
1772 * "executed" and "build" are extra arguments to be passed to add_node.
1773 * "list" collects the results.
1775 struct isl_add_nodes_data {
1776 isl_union_map *executed;
1777 isl_ast_build *build;
1779 isl_ast_graft_list *list;
1782 /* Generate code for the schedule domains in "scc"
1783 * and add the results to "list".
1785 * The domains in "scc" form a strongly connected component in the ordering.
1786 * If the number of domains in "scc" is larger than 1, then this means
1787 * that we cannot determine a valid ordering for the domains in the component.
1788 * This should be fairly rare because the individual domains
1789 * have been made disjoint first.
1790 * The problem is that the domains may be integrally disjoint but not
1791 * rationally disjoint. For example, we may have domains
1793 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1795 * These two domains have an empty intersection, but their rational
1796 * relaxations do intersect. It is impossible to order these domains
1797 * in the second dimension because the first should be ordered before
1798 * the second for outer dimension equal to 0, while it should be ordered
1799 * after for outer dimension equal to 1.
1801 * This may happen in particular in case of unrolling since the domain
1802 * of each slice is replaced by its simple hull.
1804 * For each basic set i in "scc" and for each of the following basic sets j,
1805 * we split off that part of the basic set i that shares the outer dimensions
1806 * with j and lies before j in the current dimension.
1807 * We collect all the pieces in a new list that replaces "scc".
1809 * While the elements in "scc" should be disjoint, we double-check
1810 * this property to avoid running into an infinite recursion in case
1811 * they intersect due to some internal error.
1813 static int add_nodes(__isl_take isl_basic_set_list *scc, void *user)
1815 struct isl_add_nodes_data *data = user;
1816 int i, n, depth;
1817 isl_basic_set *bset, *first;
1818 isl_basic_set_list *list;
1819 isl_space *space;
1820 isl_basic_map *gt;
1822 n = isl_basic_set_list_n_basic_set(scc);
1823 bset = isl_basic_set_list_get_basic_set(scc, 0);
1824 if (n == 1) {
1825 isl_basic_set_list_free(scc);
1826 data->list = add_node(data->list,
1827 isl_union_map_copy(data->executed), bset,
1828 isl_ast_build_copy(data->build));
1829 return data->list ? 0 : -1;
1832 depth = isl_ast_build_get_depth(data->build);
1833 space = isl_basic_set_get_space(bset);
1834 space = isl_space_map_from_set(space);
1835 gt = isl_basic_map_universe(space);
1836 for (i = 0; i < depth; ++i)
1837 gt = isl_basic_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
1838 gt = isl_basic_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
1840 first = isl_basic_set_copy(bset);
1841 list = isl_basic_set_list_from_basic_set(bset);
1842 for (i = 1; i < n; ++i) {
1843 int disjoint;
1845 bset = isl_basic_set_list_get_basic_set(scc, i);
1847 disjoint = isl_basic_set_is_disjoint(bset, first);
1848 if (disjoint < 0)
1849 list = isl_basic_set_list_free(list);
1850 else if (!disjoint)
1851 isl_die(isl_basic_set_list_get_ctx(scc),
1852 isl_error_internal,
1853 "basic sets in scc are assumed to be disjoint",
1854 list = isl_basic_set_list_free(list));
1856 list = add_split_on(list, bset, gt);
1858 isl_basic_set_free(first);
1859 isl_basic_map_free(gt);
1860 isl_basic_set_list_free(scc);
1861 scc = list;
1862 data->list = isl_ast_graft_list_concat(data->list,
1863 generate_sorted_domains(scc, data->executed, data->build));
1864 isl_basic_set_list_free(scc);
1866 return data->list ? 0 : -1;
1869 /* Sort the domains in "domain_list" according to the execution order
1870 * at the current depth (for equal values of the outer dimensions),
1871 * generate code for each of them, collecting the results in a list.
1872 * If no code is generated (because the intersection of the inverse schedule
1873 * with the domains turns out to be empty), then an empty list is returned.
1875 * The caller is responsible for ensuring that the basic sets in "domain_list"
1876 * are pair-wise disjoint. It can, however, in principle happen that
1877 * two basic sets should be ordered one way for one value of the outer
1878 * dimensions and the other way for some other value of the outer dimensions.
1879 * We therefore play safe and look for strongly connected components.
1880 * The function add_nodes takes care of handling non-trivial components.
1882 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1883 __isl_keep isl_basic_set_list *domain_list,
1884 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
1886 isl_ctx *ctx;
1887 struct isl_add_nodes_data data;
1888 int depth;
1889 int n;
1891 if (!domain_list)
1892 return NULL;
1894 ctx = isl_basic_set_list_get_ctx(domain_list);
1895 n = isl_basic_set_list_n_basic_set(domain_list);
1896 data.list = isl_ast_graft_list_alloc(ctx, n);
1897 if (n == 0)
1898 return data.list;
1899 if (n == 1)
1900 return add_node(data.list, isl_union_map_copy(executed),
1901 isl_basic_set_list_get_basic_set(domain_list, 0),
1902 isl_ast_build_copy(build));
1904 depth = isl_ast_build_get_depth(build);
1905 data.executed = executed;
1906 data.build = build;
1907 if (isl_basic_set_list_foreach_scc(domain_list,
1908 &domain_follows_at_depth, &depth,
1909 &add_nodes, &data) < 0)
1910 data.list = isl_ast_graft_list_free(data.list);
1912 return data.list;
1915 /* Do i and j share any values for the outer dimensions?
1917 static int shared_outer(__isl_keep isl_basic_set *i,
1918 __isl_keep isl_basic_set *j, void *user)
1920 int depth = *(int *) user;
1921 isl_basic_map *test;
1922 int empty;
1923 int l;
1925 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1926 isl_basic_set_copy(j));
1927 for (l = 0; l < depth; ++l)
1928 test = isl_basic_map_equate(test, isl_dim_in, l,
1929 isl_dim_out, l);
1930 empty = isl_basic_map_is_empty(test);
1931 isl_basic_map_free(test);
1933 return empty < 0 ? -1 : !empty;
1936 /* Internal data structure for generate_sorted_domains_wrap.
1938 * "n" is the total number of basic sets
1939 * "executed" and "build" are extra arguments to be passed
1940 * to generate_sorted_domains.
1942 * "single" is set to 1 by generate_sorted_domains_wrap if there
1943 * is only a single component.
1944 * "list" collects the results.
1946 struct isl_ast_generate_parallel_domains_data {
1947 int n;
1948 isl_union_map *executed;
1949 isl_ast_build *build;
1951 int single;
1952 isl_ast_graft_list *list;
1955 /* Call generate_sorted_domains on "scc", fuse the result into a list
1956 * with either zero or one graft and collect the these single element
1957 * lists into data->list.
1959 * If there is only one component, i.e., if the number of basic sets
1960 * in the current component is equal to the total number of basic sets,
1961 * then data->single is set to 1 and the result of generate_sorted_domains
1962 * is not fused.
1964 static int generate_sorted_domains_wrap(__isl_take isl_basic_set_list *scc,
1965 void *user)
1967 struct isl_ast_generate_parallel_domains_data *data = user;
1968 isl_ast_graft_list *list;
1970 list = generate_sorted_domains(scc, data->executed, data->build);
1971 data->single = isl_basic_set_list_n_basic_set(scc) == data->n;
1972 if (!data->single)
1973 list = isl_ast_graft_list_fuse(list, data->build);
1974 if (!data->list)
1975 data->list = list;
1976 else
1977 data->list = isl_ast_graft_list_concat(data->list, list);
1979 isl_basic_set_list_free(scc);
1980 if (!data->list)
1981 return -1;
1983 return 0;
1986 /* Look for any (weakly connected) components in the "domain_list"
1987 * of domains that share some values of the outer dimensions.
1988 * That is, domains in different components do not share any values
1989 * of the outer dimensions. This means that these components
1990 * can be freely reordered.
1991 * Within each of the components, we sort the domains according
1992 * to the execution order at the current depth.
1994 * If there is more than one component, then generate_sorted_domains_wrap
1995 * fuses the result of each call to generate_sorted_domains
1996 * into a list with either zero or one graft and collects these (at most)
1997 * single element lists into a bigger list. This means that the elements of the
1998 * final list can be freely reordered. In particular, we sort them
1999 * according to an arbitrary but fixed ordering to ease merging of
2000 * graft lists from different components.
2002 static __isl_give isl_ast_graft_list *generate_parallel_domains(
2003 __isl_keep isl_basic_set_list *domain_list,
2004 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2006 int depth;
2007 struct isl_ast_generate_parallel_domains_data data;
2009 if (!domain_list)
2010 return NULL;
2012 data.n = isl_basic_set_list_n_basic_set(domain_list);
2013 if (data.n <= 1)
2014 return generate_sorted_domains(domain_list, executed, build);
2016 depth = isl_ast_build_get_depth(build);
2017 data.list = NULL;
2018 data.executed = executed;
2019 data.build = build;
2020 data.single = 0;
2021 if (isl_basic_set_list_foreach_scc(domain_list, &shared_outer, &depth,
2022 &generate_sorted_domains_wrap,
2023 &data) < 0)
2024 data.list = isl_ast_graft_list_free(data.list);
2026 if (!data.single)
2027 data.list = isl_ast_graft_list_sort_guard(data.list);
2029 return data.list;
2032 /* Internal data for separate_domain.
2034 * "explicit" is set if we only want to use explicit bounds.
2036 * "domain" collects the separated domains.
2038 struct isl_separate_domain_data {
2039 isl_ast_build *build;
2040 int explicit;
2041 isl_set *domain;
2044 /* Extract implicit bounds on the current dimension for the executed "map".
2046 * The domain of "map" may involve inner dimensions, so we
2047 * need to eliminate them.
2049 static __isl_give isl_set *implicit_bounds(__isl_take isl_map *map,
2050 __isl_keep isl_ast_build *build)
2052 isl_set *domain;
2054 domain = isl_map_domain(map);
2055 domain = isl_ast_build_eliminate(build, domain);
2057 return domain;
2060 /* Extract explicit bounds on the current dimension for the executed "map".
2062 * Rather than eliminating the inner dimensions as in implicit_bounds,
2063 * we simply drop any constraints involving those inner dimensions.
2064 * The idea is that most bounds that are implied by constraints on the
2065 * inner dimensions will be enforced by for loops and not by explicit guards.
2066 * There is then no need to separate along those bounds.
2068 static __isl_give isl_set *explicit_bounds(__isl_take isl_map *map,
2069 __isl_keep isl_ast_build *build)
2071 isl_set *domain;
2072 int depth, dim;
2074 dim = isl_map_dim(map, isl_dim_out);
2075 map = isl_map_drop_constraints_involving_dims(map, isl_dim_out, 0, dim);
2077 domain = isl_map_domain(map);
2078 depth = isl_ast_build_get_depth(build);
2079 dim = isl_set_dim(domain, isl_dim_set);
2080 domain = isl_set_detect_equalities(domain);
2081 domain = isl_set_drop_constraints_involving_dims(domain,
2082 isl_dim_set, depth + 1, dim - (depth + 1));
2083 domain = isl_set_remove_divs_involving_dims(domain,
2084 isl_dim_set, depth, 1);
2085 domain = isl_set_remove_unknown_divs(domain);
2087 return domain;
2090 /* Split data->domain into pieces that intersect with the range of "map"
2091 * and pieces that do not intersect with the range of "map"
2092 * and then add that part of the range of "map" that does not intersect
2093 * with data->domain.
2095 static int separate_domain(__isl_take isl_map *map, void *user)
2097 struct isl_separate_domain_data *data = user;
2098 isl_set *domain;
2099 isl_set *d1, *d2;
2101 if (data->explicit)
2102 domain = explicit_bounds(map, data->build);
2103 else
2104 domain = implicit_bounds(map, data->build);
2106 domain = isl_set_coalesce(domain);
2107 domain = isl_set_make_disjoint(domain);
2108 d1 = isl_set_subtract(isl_set_copy(domain), isl_set_copy(data->domain));
2109 d2 = isl_set_subtract(isl_set_copy(data->domain), isl_set_copy(domain));
2110 data->domain = isl_set_intersect(data->domain, domain);
2111 data->domain = isl_set_union(data->domain, d1);
2112 data->domain = isl_set_union(data->domain, d2);
2114 return 0;
2117 /* Separate the schedule domains of "executed".
2119 * That is, break up the domain of "executed" into basic sets,
2120 * such that for each basic set S, every element in S is associated with
2121 * the same domain spaces.
2123 * "space" is the (single) domain space of "executed".
2125 static __isl_give isl_set *separate_schedule_domains(
2126 __isl_take isl_space *space, __isl_take isl_union_map *executed,
2127 __isl_keep isl_ast_build *build)
2129 struct isl_separate_domain_data data = { build };
2130 isl_ctx *ctx;
2132 ctx = isl_ast_build_get_ctx(build);
2133 data.explicit = isl_options_get_ast_build_separation_bounds(ctx) ==
2134 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT;
2135 data.domain = isl_set_empty(space);
2136 if (isl_union_map_foreach_map(executed, &separate_domain, &data) < 0)
2137 data.domain = isl_set_free(data.domain);
2139 isl_union_map_free(executed);
2140 return data.domain;
2143 /* Temporary data used during the search for a lower bound for unrolling.
2145 * "domain" is the original set for which to find a lower bound
2146 * "depth" is the dimension for which to find a lower boudn
2148 * "lower" is the best lower bound found so far. It is NULL if we have not
2149 * found any yet.
2150 * "n" is the corresponding size. If lower is NULL, then the value of n
2151 * is undefined.
2153 struct isl_find_unroll_data {
2154 isl_set *domain;
2155 int depth;
2157 isl_aff *lower;
2158 int *n;
2161 /* Check if we can use "c" as a lower bound and if it is better than
2162 * any previously found lower bound.
2164 * If "c" does not involve the dimension at the current depth,
2165 * then we cannot use it.
2166 * Otherwise, let "c" be of the form
2168 * i >= f(j)/a
2170 * We compute the maximal value of
2172 * -ceil(f(j)/a)) + i + 1
2174 * over the domain. If there is such a value "n", then we know
2176 * -ceil(f(j)/a)) + i + 1 <= n
2178 * or
2180 * i < ceil(f(j)/a)) + n
2182 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2183 * We just need to check if we have found any lower bound before and
2184 * if the new lower bound is better (smaller n) than the previously found
2185 * lower bounds.
2187 static int update_unrolling_lower_bound(struct isl_find_unroll_data *data,
2188 __isl_keep isl_constraint *c)
2190 isl_aff *aff, *lower;
2191 isl_val *max;
2193 if (!isl_constraint_is_lower_bound(c, isl_dim_set, data->depth))
2194 return 0;
2196 lower = isl_constraint_get_bound(c, isl_dim_set, data->depth);
2197 lower = isl_aff_ceil(lower);
2198 aff = isl_aff_copy(lower);
2199 aff = isl_aff_neg(aff);
2200 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, data->depth, 1);
2201 aff = isl_aff_add_constant_si(aff, 1);
2202 max = isl_set_max_val(data->domain, aff);
2203 isl_aff_free(aff);
2205 if (!max)
2206 goto error;
2207 if (isl_val_is_infty(max)) {
2208 isl_val_free(max);
2209 isl_aff_free(lower);
2210 return 0;
2213 if (isl_val_cmp_si(max, INT_MAX) <= 0 &&
2214 (!data->lower || isl_val_cmp_si(max, *data->n) < 0)) {
2215 isl_aff_free(data->lower);
2216 data->lower = lower;
2217 *data->n = isl_val_get_num_si(max);
2218 } else
2219 isl_aff_free(lower);
2220 isl_val_free(max);
2222 return 1;
2223 error:
2224 isl_aff_free(lower);
2225 return -1;
2228 /* Check if we can use "c" as a lower bound and if it is better than
2229 * any previously found lower bound.
2231 static int constraint_find_unroll(__isl_take isl_constraint *c, void *user)
2233 struct isl_find_unroll_data *data;
2234 int r;
2236 data = (struct isl_find_unroll_data *) user;
2237 r = update_unrolling_lower_bound(data, c);
2238 isl_constraint_free(c);
2240 return r;
2243 /* Look for a lower bound l(i) on the dimension at "depth"
2244 * and a size n such that "domain" is a subset of
2246 * { [i] : l(i) <= i_d < l(i) + n }
2248 * where d is "depth" and l(i) depends only on earlier dimensions.
2249 * Furthermore, try and find a lower bound such that n is as small as possible.
2250 * In particular, "n" needs to be finite.
2252 * Inner dimensions have been eliminated from "domain" by the caller.
2254 * We first construct a collection of lower bounds on the input set
2255 * by computing its simple hull. We then iterate through them,
2256 * discarding those that we cannot use (either because they do not
2257 * involve the dimension at "depth" or because they have no corresponding
2258 * upper bound, meaning that "n" would be unbounded) and pick out the
2259 * best from the remaining ones.
2261 * If we cannot find a suitable lower bound, then we consider that
2262 * to be an error.
2264 static __isl_give isl_aff *find_unroll_lower_bound(__isl_keep isl_set *domain,
2265 int depth, int *n)
2267 struct isl_find_unroll_data data = { domain, depth, NULL, n };
2268 isl_basic_set *hull;
2270 hull = isl_set_simple_hull(isl_set_copy(domain));
2272 if (isl_basic_set_foreach_constraint(hull,
2273 &constraint_find_unroll, &data) < 0)
2274 goto error;
2276 isl_basic_set_free(hull);
2278 if (!data.lower)
2279 isl_die(isl_set_get_ctx(domain), isl_error_invalid,
2280 "cannot find lower bound for unrolling", return NULL);
2282 return data.lower;
2283 error:
2284 isl_basic_set_free(hull);
2285 return isl_aff_free(data.lower);
2288 /* Return the constraint
2290 * i_"depth" = aff + offset
2292 static __isl_give isl_constraint *at_offset(int depth, __isl_keep isl_aff *aff,
2293 int offset)
2295 aff = isl_aff_copy(aff);
2296 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, depth, -1);
2297 aff = isl_aff_add_constant_si(aff, offset);
2298 return isl_equality_from_aff(aff);
2301 /* Data structure for storing the results and the intermediate objects
2302 * of compute_domains.
2304 * "list" is the main result of the function and contains a list
2305 * of disjoint basic sets for which code should be generated.
2307 * "executed" and "build" are inputs to compute_domains.
2308 * "schedule_domain" is the domain of "executed".
2310 * "option" constains the domains at the current depth that should by
2311 * atomic, separated or unrolled. These domains are as specified by
2312 * the user, except that inner dimensions have been eliminated and
2313 * that they have been made pair-wise disjoint.
2315 * "sep_class" contains the user-specified split into separation classes
2316 * specialized to the current depth.
2317 * "done" contains the union of the separation domains that have already
2318 * been handled.
2320 struct isl_codegen_domains {
2321 isl_basic_set_list *list;
2323 isl_union_map *executed;
2324 isl_ast_build *build;
2325 isl_set *schedule_domain;
2327 isl_set *option[3];
2329 isl_map *sep_class;
2330 isl_set *done;
2333 /* Extend domains->list with a list of basic sets, one for each value
2334 * of the current dimension in "domain" and remove the corresponding
2335 * sets from the class domain. Return the updated class domain.
2336 * The divs that involve the current dimension have not been projected out
2337 * from this domain.
2339 * Since we are going to be iterating over the individual values,
2340 * we first check if there are any strides on the current dimension.
2341 * If there is, we rewrite the current dimension i as
2343 * i = stride i' + offset
2345 * and then iterate over individual values of i' instead.
2347 * We then look for a lower bound on i' and a size such that the domain
2348 * is a subset of
2350 * { [j,i'] : l(j) <= i' < l(j) + n }
2352 * and then take slices of the domain at values of i'
2353 * between l(j) and l(j) + n - 1.
2355 * We compute the unshifted simple hull of each slice to ensure that
2356 * we have a single basic set per offset. The slicing constraint
2357 * may get simplified away before the unshifted simple hull is taken
2358 * and may therefore in some rare cases disappear from the result.
2359 * We therefore explicitly add the constraint back after computing
2360 * the unshifted simple hull to ensure that the basic sets
2361 * remain disjoint. The constraints that are dropped by taking the hull
2362 * will be taken into account at the next level, as in the case of the
2363 * atomic option.
2365 * Finally, we map i' back to i and add each basic set to the list.
2366 * Since we may have dropped some constraints, we intersect with
2367 * the class domain again to ensure that each element in the list
2368 * is disjoint from the other class domains.
2370 static __isl_give isl_set *do_unroll(struct isl_codegen_domains *domains,
2371 __isl_take isl_set *domain, __isl_take isl_set *class_domain)
2373 int i, n;
2374 int depth;
2375 isl_ctx *ctx;
2376 isl_aff *lower;
2377 isl_multi_aff *expansion;
2378 isl_basic_map *bmap;
2379 isl_set *unroll_domain;
2380 isl_ast_build *build;
2382 if (!domain)
2383 return isl_set_free(class_domain);
2385 ctx = isl_set_get_ctx(domain);
2386 depth = isl_ast_build_get_depth(domains->build);
2387 build = isl_ast_build_copy(domains->build);
2388 domain = isl_ast_build_eliminate_inner(build, domain);
2389 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
2390 expansion = isl_ast_build_get_stride_expansion(build);
2392 domain = isl_set_preimage_multi_aff(domain,
2393 isl_multi_aff_copy(expansion));
2394 domain = isl_ast_build_eliminate_divs(build, domain);
2396 isl_ast_build_free(build);
2398 lower = find_unroll_lower_bound(domain, depth, &n);
2399 if (!lower)
2400 class_domain = isl_set_free(class_domain);
2402 bmap = isl_basic_map_from_multi_aff(expansion);
2404 unroll_domain = isl_set_empty(isl_set_get_space(domain));
2406 for (i = 0; class_domain && i < n; ++i) {
2407 isl_set *set;
2408 isl_basic_set *bset;
2409 isl_constraint *slice;
2410 isl_basic_set_list *list;
2412 slice = at_offset(depth, lower, i);
2413 set = isl_set_copy(domain);
2414 set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2415 bset = isl_set_unshifted_simple_hull(set);
2416 bset = isl_basic_set_add_constraint(bset, slice);
2417 bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2418 set = isl_set_from_basic_set(bset);
2419 unroll_domain = isl_set_union(unroll_domain, isl_set_copy(set));
2420 set = isl_set_intersect(set, isl_set_copy(class_domain));
2421 set = isl_set_make_disjoint(set);
2422 list = isl_basic_set_list_from_set(set);
2423 domains->list = isl_basic_set_list_concat(domains->list, list);
2426 class_domain = isl_set_subtract(class_domain, unroll_domain);
2428 isl_aff_free(lower);
2429 isl_set_free(domain);
2430 isl_basic_map_free(bmap);
2432 return class_domain;
2435 /* Add domains to domains->list for each individual value of the current
2436 * dimension, for that part of the schedule domain that lies in the
2437 * intersection of the option domain and the class domain.
2438 * Remove the corresponding sets from the class domain and
2439 * return the updated class domain.
2441 * We first break up the unroll option domain into individual pieces
2442 * and then handle each of them separately. The unroll option domain
2443 * has been made disjoint in compute_domains_init_options,
2445 * Note that we actively want to combine different pieces of the
2446 * schedule domain that have the same value at the current dimension.
2447 * We therefore need to break up the unroll option domain before
2448 * intersecting with class and schedule domain, hoping that the
2449 * unroll option domain specified by the user is relatively simple.
2451 static __isl_give isl_set *compute_unroll_domains(
2452 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2454 isl_set *unroll_domain;
2455 isl_basic_set_list *unroll_list;
2456 int i, n;
2457 int empty;
2459 empty = isl_set_is_empty(domains->option[unroll]);
2460 if (empty < 0)
2461 return isl_set_free(class_domain);
2462 if (empty)
2463 return class_domain;
2465 unroll_domain = isl_set_copy(domains->option[unroll]);
2466 unroll_list = isl_basic_set_list_from_set(unroll_domain);
2468 n = isl_basic_set_list_n_basic_set(unroll_list);
2469 for (i = 0; i < n; ++i) {
2470 isl_basic_set *bset;
2472 bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2473 unroll_domain = isl_set_from_basic_set(bset);
2474 unroll_domain = isl_set_intersect(unroll_domain,
2475 isl_set_copy(class_domain));
2476 unroll_domain = isl_set_intersect(unroll_domain,
2477 isl_set_copy(domains->schedule_domain));
2479 empty = isl_set_is_empty(unroll_domain);
2480 if (empty >= 0 && empty) {
2481 isl_set_free(unroll_domain);
2482 continue;
2485 class_domain = do_unroll(domains, unroll_domain, class_domain);
2488 isl_basic_set_list_free(unroll_list);
2490 return class_domain;
2493 /* Try and construct a single basic set that includes the intersection of
2494 * the schedule domain, the atomic option domain and the class domain.
2495 * Add the resulting basic set(s) to domains->list and remove them
2496 * from class_domain. Return the updated class domain.
2498 * We construct a single domain rather than trying to combine
2499 * the schedule domains of individual domains because we are working
2500 * within a single component so that non-overlapping schedule domains
2501 * should already have been separated.
2502 * We do however need to make sure that this single domains is a subset
2503 * of the class domain so that it would not intersect with any other
2504 * class domains. This means that we may end up splitting up the atomic
2505 * domain in case separation classes are being used.
2507 * "domain" is the intersection of the schedule domain and the class domain,
2508 * with inner dimensions projected out.
2510 static __isl_give isl_set *compute_atomic_domain(
2511 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2513 isl_basic_set *bset;
2514 isl_basic_set_list *list;
2515 isl_set *domain, *atomic_domain;
2516 int empty;
2518 domain = isl_set_copy(domains->option[atomic]);
2519 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2520 domain = isl_set_intersect(domain,
2521 isl_set_copy(domains->schedule_domain));
2522 empty = isl_set_is_empty(domain);
2523 if (empty < 0)
2524 class_domain = isl_set_free(class_domain);
2525 if (empty) {
2526 isl_set_free(domain);
2527 return class_domain;
2530 domain = isl_ast_build_eliminate(domains->build, domain);
2531 domain = isl_set_coalesce(domain);
2532 bset = isl_set_unshifted_simple_hull(domain);
2533 domain = isl_set_from_basic_set(bset);
2534 atomic_domain = isl_set_copy(domain);
2535 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2536 class_domain = isl_set_subtract(class_domain, atomic_domain);
2537 domain = isl_set_make_disjoint(domain);
2538 list = isl_basic_set_list_from_set(domain);
2539 domains->list = isl_basic_set_list_concat(domains->list, list);
2541 return class_domain;
2544 /* Split up the schedule domain into uniform basic sets,
2545 * in the sense that each element in a basic set is associated to
2546 * elements of the same domains, and add the result to domains->list.
2547 * Do this for that part of the schedule domain that lies in the
2548 * intersection of "class_domain" and the separate option domain.
2550 * "class_domain" may or may not include the constraints
2551 * of the schedule domain, but this does not make a difference
2552 * since we are going to intersect it with the domain of the inverse schedule.
2553 * If it includes schedule domain constraints, then they may involve
2554 * inner dimensions, but we will eliminate them in separation_domain.
2556 static int compute_separate_domain(struct isl_codegen_domains *domains,
2557 __isl_keep isl_set *class_domain)
2559 isl_space *space;
2560 isl_set *domain;
2561 isl_union_map *executed;
2562 isl_basic_set_list *list;
2563 int empty;
2565 domain = isl_set_copy(domains->option[separate]);
2566 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2567 executed = isl_union_map_copy(domains->executed);
2568 executed = isl_union_map_intersect_domain(executed,
2569 isl_union_set_from_set(domain));
2570 empty = isl_union_map_is_empty(executed);
2571 if (empty < 0 || empty) {
2572 isl_union_map_free(executed);
2573 return empty < 0 ? -1 : 0;
2576 space = isl_set_get_space(class_domain);
2577 domain = separate_schedule_domains(space, executed, domains->build);
2579 list = isl_basic_set_list_from_set(domain);
2580 domains->list = isl_basic_set_list_concat(domains->list, list);
2582 return 0;
2585 /* Split up the domain at the current depth into disjoint
2586 * basic sets for which code should be generated separately
2587 * for the given separation class domain.
2589 * If any separation classes have been defined, then "class_domain"
2590 * is the domain of the current class and does not refer to inner dimensions.
2591 * Otherwise, "class_domain" is the universe domain.
2593 * We first make sure that the class domain is disjoint from
2594 * previously considered class domains.
2596 * The separate domains can be computed directly from the "class_domain".
2598 * The unroll, atomic and remainder domains need the constraints
2599 * from the schedule domain.
2601 * For unrolling, the actual schedule domain is needed (with divs that
2602 * may refer to the current dimension) so that stride detection can be
2603 * performed.
2605 * For atomic and remainder domains, inner dimensions and divs involving
2606 * the current dimensions should be eliminated.
2607 * In case we are working within a separation class, we need to intersect
2608 * the result with the current "class_domain" to ensure that the domains
2609 * are disjoint from those generated from other class domains.
2611 * The domain that has been made atomic may be larger than specified
2612 * by the user since it needs to be representable as a single basic set.
2613 * This possibly larger domain is removed from class_domain by
2614 * compute_atomic_domain. It is computed first so that the extended domain
2615 * would not overlap with any domains computed before.
2616 * Similary, the unrolled domains may have some constraints removed and
2617 * may therefore also be larger than specified by the user.
2619 * If anything is left after handling separate, unroll and atomic,
2620 * we split it up into basic sets and append the basic sets to domains->list.
2622 static int compute_partial_domains(struct isl_codegen_domains *domains,
2623 __isl_take isl_set *class_domain)
2625 isl_basic_set_list *list;
2626 isl_set *domain;
2628 class_domain = isl_set_subtract(class_domain,
2629 isl_set_copy(domains->done));
2630 domains->done = isl_set_union(domains->done,
2631 isl_set_copy(class_domain));
2633 class_domain = compute_atomic_domain(domains, class_domain);
2634 class_domain = compute_unroll_domains(domains, class_domain);
2636 domain = isl_set_copy(class_domain);
2638 if (compute_separate_domain(domains, domain) < 0)
2639 goto error;
2640 domain = isl_set_subtract(domain,
2641 isl_set_copy(domains->option[separate]));
2643 domain = isl_set_intersect(domain,
2644 isl_set_copy(domains->schedule_domain));
2646 domain = isl_ast_build_eliminate(domains->build, domain);
2647 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2649 domain = isl_set_coalesce(domain);
2650 domain = isl_set_make_disjoint(domain);
2652 list = isl_basic_set_list_from_set(domain);
2653 domains->list = isl_basic_set_list_concat(domains->list, list);
2655 isl_set_free(class_domain);
2657 return 0;
2658 error:
2659 isl_set_free(domain);
2660 isl_set_free(class_domain);
2661 return -1;
2664 /* Split up the domain at the current depth into disjoint
2665 * basic sets for which code should be generated separately
2666 * for the separation class identified by "pnt".
2668 * We extract the corresponding class domain from domains->sep_class,
2669 * eliminate inner dimensions and pass control to compute_partial_domains.
2671 static int compute_class_domains(__isl_take isl_point *pnt, void *user)
2673 struct isl_codegen_domains *domains = user;
2674 isl_set *class_set;
2675 isl_set *domain;
2676 int disjoint;
2678 class_set = isl_set_from_point(pnt);
2679 domain = isl_map_domain(isl_map_intersect_range(
2680 isl_map_copy(domains->sep_class), class_set));
2681 domain = isl_ast_build_compute_gist(domains->build, domain);
2682 domain = isl_ast_build_eliminate(domains->build, domain);
2684 disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
2685 if (disjoint < 0)
2686 return -1;
2687 if (disjoint) {
2688 isl_set_free(domain);
2689 return 0;
2692 return compute_partial_domains(domains, domain);
2695 /* Extract the domains at the current depth that should be atomic,
2696 * separated or unrolled and store them in option.
2698 * The domains specified by the user might overlap, so we make
2699 * them disjoint by subtracting earlier domains from later domains.
2701 static void compute_domains_init_options(isl_set *option[3],
2702 __isl_keep isl_ast_build *build)
2704 enum isl_ast_build_domain_type type, type2;
2706 for (type = atomic; type <= separate; ++type) {
2707 option[type] = isl_ast_build_get_option_domain(build, type);
2708 for (type2 = atomic; type2 < type; ++type2)
2709 option[type] = isl_set_subtract(option[type],
2710 isl_set_copy(option[type2]));
2713 option[unroll] = isl_set_coalesce(option[unroll]);
2714 option[unroll] = isl_set_make_disjoint(option[unroll]);
2717 /* Split up the domain at the current depth into disjoint
2718 * basic sets for which code should be generated separately,
2719 * based on the user-specified options.
2720 * Return the list of disjoint basic sets.
2722 * There are three kinds of domains that we need to keep track of.
2723 * - the "schedule domain" is the domain of "executed"
2724 * - the "class domain" is the domain corresponding to the currrent
2725 * separation class
2726 * - the "option domain" is the domain corresponding to one of the options
2727 * atomic, unroll or separate
2729 * We first consider the individial values of the separation classes
2730 * and split up the domain for each of them separately.
2731 * Finally, we consider the remainder. If no separation classes were
2732 * specified, then we call compute_partial_domains with the universe
2733 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
2734 * with inner dimensions removed. We do this because we want to
2735 * avoid computing the complement of the class domains (i.e., the difference
2736 * between the universe and domains->done).
2738 static __isl_give isl_basic_set_list *compute_domains(
2739 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2741 struct isl_codegen_domains domains;
2742 isl_ctx *ctx;
2743 isl_set *domain;
2744 isl_union_set *schedule_domain;
2745 isl_set *classes;
2746 isl_space *space;
2747 int n_param;
2748 enum isl_ast_build_domain_type type;
2749 int empty;
2751 if (!executed)
2752 return NULL;
2754 ctx = isl_union_map_get_ctx(executed);
2755 domains.list = isl_basic_set_list_alloc(ctx, 0);
2757 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
2758 domain = isl_set_from_union_set(schedule_domain);
2760 compute_domains_init_options(domains.option, build);
2762 domains.sep_class = isl_ast_build_get_separation_class(build);
2763 classes = isl_map_range(isl_map_copy(domains.sep_class));
2764 n_param = isl_set_dim(classes, isl_dim_param);
2765 classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
2767 space = isl_set_get_space(domain);
2768 domains.build = build;
2769 domains.schedule_domain = isl_set_copy(domain);
2770 domains.executed = executed;
2771 domains.done = isl_set_empty(space);
2773 if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
2774 domains.list = isl_basic_set_list_free(domains.list);
2775 isl_set_free(classes);
2777 empty = isl_set_is_empty(domains.done);
2778 if (empty < 0) {
2779 domains.list = isl_basic_set_list_free(domains.list);
2780 domain = isl_set_free(domain);
2781 } else if (empty) {
2782 isl_set_free(domain);
2783 domain = isl_set_universe(isl_set_get_space(domains.done));
2784 } else {
2785 domain = isl_ast_build_eliminate(build, domain);
2787 if (compute_partial_domains(&domains, domain) < 0)
2788 domains.list = isl_basic_set_list_free(domains.list);
2790 isl_set_free(domains.schedule_domain);
2791 isl_set_free(domains.done);
2792 isl_map_free(domains.sep_class);
2793 for (type = atomic; type <= separate; ++type)
2794 isl_set_free(domains.option[type]);
2796 return domains.list;
2799 /* Generate code for a single component, after shifting (if any)
2800 * has been applied.
2802 * We first split up the domain at the current depth into disjoint
2803 * basic sets based on the user-specified options.
2804 * Then we generated code for each of them and concatenate the results.
2806 static __isl_give isl_ast_graft_list *generate_shifted_component(
2807 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
2809 isl_basic_set_list *domain_list;
2810 isl_ast_graft_list *list = NULL;
2812 domain_list = compute_domains(executed, build);
2813 list = generate_parallel_domains(domain_list, executed, build);
2815 isl_basic_set_list_free(domain_list);
2816 isl_union_map_free(executed);
2817 isl_ast_build_free(build);
2819 return list;
2822 struct isl_set_map_pair {
2823 isl_set *set;
2824 isl_map *map;
2827 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2828 * of indices into the "domain" array,
2829 * return the union of the "map" fields of the elements
2830 * indexed by the first "n" elements of "order".
2832 static __isl_give isl_union_map *construct_component_executed(
2833 struct isl_set_map_pair *domain, int *order, int n)
2835 int i;
2836 isl_map *map;
2837 isl_union_map *executed;
2839 map = isl_map_copy(domain[order[0]].map);
2840 executed = isl_union_map_from_map(map);
2841 for (i = 1; i < n; ++i) {
2842 map = isl_map_copy(domain[order[i]].map);
2843 executed = isl_union_map_add_map(executed, map);
2846 return executed;
2849 /* Generate code for a single component, after shifting (if any)
2850 * has been applied.
2852 * The component inverse schedule is specified as the "map" fields
2853 * of the elements of "domain" indexed by the first "n" elements of "order".
2855 static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
2856 struct isl_set_map_pair *domain, int *order, int n,
2857 __isl_take isl_ast_build *build)
2859 isl_union_map *executed;
2861 executed = construct_component_executed(domain, order, n);
2862 return generate_shifted_component(executed, build);
2865 /* Does set dimension "pos" of "set" have an obviously fixed value?
2867 static int dim_is_fixed(__isl_keep isl_set *set, int pos)
2869 int fixed;
2870 isl_val *v;
2872 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, pos);
2873 if (!v)
2874 return -1;
2875 fixed = !isl_val_is_nan(v);
2876 isl_val_free(v);
2878 return fixed;
2881 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2882 * of indices into the "domain" array,
2883 * do all (except for at most one) of the "set" field of the elements
2884 * indexed by the first "n" elements of "order" have a fixed value
2885 * at position "depth"?
2887 static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
2888 int *order, int n, int depth)
2890 int i;
2891 int non_fixed = -1;
2893 for (i = 0; i < n; ++i) {
2894 int f;
2896 f = dim_is_fixed(domain[order[i]].set, depth);
2897 if (f < 0)
2898 return -1;
2899 if (f)
2900 continue;
2901 if (non_fixed >= 0)
2902 return 0;
2903 non_fixed = i;
2906 return 1;
2909 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2910 * of indices into the "domain" array,
2911 * eliminate the inner dimensions from the "set" field of the elements
2912 * indexed by the first "n" elements of "order", provided the current
2913 * dimension does not have a fixed value.
2915 * Return the index of the first element in "order" with a corresponding
2916 * "set" field that does not have an (obviously) fixed value.
2918 static int eliminate_non_fixed(struct isl_set_map_pair *domain,
2919 int *order, int n, int depth, __isl_keep isl_ast_build *build)
2921 int i;
2922 int base = -1;
2924 for (i = n - 1; i >= 0; --i) {
2925 int f;
2926 f = dim_is_fixed(domain[order[i]].set, depth);
2927 if (f < 0)
2928 return -1;
2929 if (f)
2930 continue;
2931 domain[order[i]].set = isl_ast_build_eliminate_inner(build,
2932 domain[order[i]].set);
2933 base = i;
2936 return base;
2939 /* Given an array "domain" of isl_set_map_pairs and an array "order"
2940 * of indices into the "domain" array,
2941 * find the element of "domain" (amongst those indexed by the first "n"
2942 * elements of "order") with the "set" field that has the smallest
2943 * value for the current iterator.
2945 * Note that the domain with the smallest value may depend on the parameters
2946 * and/or outer loop dimension. Since the result of this function is only
2947 * used as heuristic, we only make a reasonable attempt at finding the best
2948 * domain, one that should work in case a single domain provides the smallest
2949 * value for the current dimension over all values of the parameters
2950 * and outer dimensions.
2952 * In particular, we compute the smallest value of the first domain
2953 * and replace it by that of any later domain if that later domain
2954 * has a smallest value that is smaller for at least some value
2955 * of the parameters and outer dimensions.
2957 static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
2958 __isl_keep isl_ast_build *build)
2960 int i;
2961 isl_map *min_first;
2962 int first = 0;
2964 min_first = isl_ast_build_map_to_iterator(build,
2965 isl_set_copy(domain[order[0]].set));
2966 min_first = isl_map_lexmin(min_first);
2968 for (i = 1; i < n; ++i) {
2969 isl_map *min, *test;
2970 int empty;
2972 min = isl_ast_build_map_to_iterator(build,
2973 isl_set_copy(domain[order[i]].set));
2974 min = isl_map_lexmin(min);
2975 test = isl_map_copy(min);
2976 test = isl_map_apply_domain(isl_map_copy(min_first), test);
2977 test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
2978 empty = isl_map_is_empty(test);
2979 isl_map_free(test);
2980 if (empty >= 0 && !empty) {
2981 isl_map_free(min_first);
2982 first = i;
2983 min_first = min;
2984 } else
2985 isl_map_free(min);
2987 if (empty < 0)
2988 break;
2991 isl_map_free(min_first);
2993 return i < n ? -1 : first;
2996 /* Construct a shifted inverse schedule based on the original inverse schedule,
2997 * the stride and the offset.
2999 * The original inverse schedule is specified as the "map" fields
3000 * of the elements of "domain" indexed by the first "n" elements of "order".
3002 * "stride" and "offset" are such that the difference
3003 * between the values of the current dimension of domain "i"
3004 * and the values of the current dimension for some reference domain are
3005 * equal to
3007 * stride * integer + offset[i]
3009 * Moreover, 0 <= offset[i] < stride.
3011 * For each domain, we create a map
3013 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3015 * where j refers to the current dimension and the other dimensions are
3016 * unchanged, and apply this map to the original schedule domain.
3018 * For example, for the original schedule
3020 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3022 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3023 * we apply the mapping
3025 * { [j] -> [j, 0] }
3027 * to the schedule of the "A" domain and the mapping
3029 * { [j - 1] -> [j, 1] }
3031 * to the schedule of the "B" domain.
3034 * Note that after the transformation, the differences between pairs
3035 * of values of the current dimension over all domains are multiples
3036 * of stride and that we have therefore exposed the stride.
3039 * To see that the mapping preserves the lexicographic order,
3040 * first note that each of the individual maps above preserves the order.
3041 * If the value of the current iterator is j1 in one domain and j2 in another,
3042 * then if j1 = j2, we know that the same map is applied to both domains
3043 * and the order is preserved.
3044 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3045 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3047 * j1 - c1 < j2 - c2
3049 * and the order is preserved.
3050 * If c1 < c2, then we know
3052 * 0 <= c2 - c1 < s
3054 * We also have
3056 * j2 - j1 = n * s + r
3058 * with n >= 0 and 0 <= r < s.
3059 * In other words, r = c2 - c1.
3060 * If n > 0, then
3062 * j1 - c1 < j2 - c2
3064 * If n = 0, then
3066 * j1 - c1 = j2 - c2
3068 * and so
3070 * (j1 - c1, c1) << (j2 - c2, c2)
3072 * with "<<" the lexicographic order, proving that the order is preserved
3073 * in all cases.
3075 static __isl_give isl_union_map *contruct_shifted_executed(
3076 struct isl_set_map_pair *domain, int *order, int n,
3077 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3078 __isl_take isl_ast_build *build)
3080 int i;
3081 isl_union_map *executed;
3082 isl_space *space;
3083 isl_map *map;
3084 int depth;
3085 isl_constraint *c;
3087 depth = isl_ast_build_get_depth(build);
3088 space = isl_ast_build_get_space(build, 1);
3089 executed = isl_union_map_empty(isl_space_copy(space));
3090 space = isl_space_map_from_set(space);
3091 map = isl_map_identity(isl_space_copy(space));
3092 map = isl_map_eliminate(map, isl_dim_out, depth, 1);
3093 map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
3094 space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
3096 c = isl_equality_alloc(isl_local_space_from_space(space));
3097 c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
3098 c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
3100 for (i = 0; i < n; ++i) {
3101 isl_map *map_i;
3102 isl_val *v;
3104 v = isl_multi_val_get_val(offset, i);
3105 if (!v)
3106 break;
3107 map_i = isl_map_copy(map);
3108 map_i = isl_map_fix_val(map_i, isl_dim_out, depth + 1,
3109 isl_val_copy(v));
3110 v = isl_val_neg(v);
3111 c = isl_constraint_set_constant_val(c, v);
3112 map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
3114 map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
3115 map_i);
3116 executed = isl_union_map_add_map(executed, map_i);
3119 isl_constraint_free(c);
3120 isl_map_free(map);
3122 if (i < n)
3123 executed = isl_union_map_free(executed);
3125 return executed;
3128 /* Generate code for a single component, after exposing the stride,
3129 * given that the schedule domain is "shifted strided".
3131 * The component inverse schedule is specified as the "map" fields
3132 * of the elements of "domain" indexed by the first "n" elements of "order".
3134 * The schedule domain being "shifted strided" means that the differences
3135 * between the values of the current dimension of domain "i"
3136 * and the values of the current dimension for some reference domain are
3137 * equal to
3139 * stride * integer + offset[i]
3141 * We first look for the domain with the "smallest" value for the current
3142 * dimension and adjust the offsets such that the offset of the "smallest"
3143 * domain is equal to zero. The other offsets are reduced modulo stride.
3145 * Based on this information, we construct a new inverse schedule in
3146 * contruct_shifted_executed that exposes the stride.
3147 * Since this involves the introduction of a new schedule dimension,
3148 * the build needs to be changed accodingly.
3149 * After computing the AST, the newly introduced dimension needs
3150 * to be removed again from the list of grafts. We do this by plugging
3151 * in a mapping that represents the new schedule domain in terms of the
3152 * old schedule domain.
3154 static __isl_give isl_ast_graft_list *generate_shift_component(
3155 struct isl_set_map_pair *domain, int *order, int n,
3156 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3157 __isl_take isl_ast_build *build)
3159 isl_ast_graft_list *list;
3160 int first;
3161 int depth;
3162 isl_ctx *ctx;
3163 isl_val *val;
3164 isl_multi_val *mv;
3165 isl_space *space;
3166 isl_multi_aff *ma, *zero;
3167 isl_union_map *executed;
3169 ctx = isl_ast_build_get_ctx(build);
3170 depth = isl_ast_build_get_depth(build);
3172 first = first_offset(domain, order, n, build);
3173 if (first < 0)
3174 goto error;
3176 mv = isl_multi_val_copy(offset);
3177 val = isl_multi_val_get_val(offset, first);
3178 val = isl_val_neg(val);
3179 mv = isl_multi_val_add_val(mv, val);
3180 mv = isl_multi_val_mod_val(mv, isl_val_copy(stride));
3182 executed = contruct_shifted_executed(domain, order, n, stride, mv,
3183 build);
3184 space = isl_ast_build_get_space(build, 1);
3185 space = isl_space_map_from_set(space);
3186 ma = isl_multi_aff_identity(isl_space_copy(space));
3187 space = isl_space_from_domain(isl_space_domain(space));
3188 space = isl_space_add_dims(space, isl_dim_out, 1);
3189 zero = isl_multi_aff_zero(space);
3190 ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
3191 build = isl_ast_build_insert_dim(build, depth + 1);
3192 list = generate_shifted_component(executed, build);
3194 list = isl_ast_graft_list_preimage_multi_aff(list, ma);
3196 isl_multi_val_free(mv);
3198 return list;
3199 error:
3200 isl_ast_build_free(build);
3201 return NULL;
3204 /* Generate code for a single component.
3206 * The component inverse schedule is specified as the "map" fields
3207 * of the elements of "domain" indexed by the first "n" elements of "order".
3209 * This function may modify the "set" fields of "domain".
3211 * Before proceeding with the actual code generation for the component,
3212 * we first check if there are any "shifted" strides, meaning that
3213 * the schedule domains of the individual domains are all strided,
3214 * but that they have different offsets, resulting in the union
3215 * of schedule domains not being strided anymore.
3217 * The simplest example is the schedule
3219 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3221 * Both schedule domains are strided, but their union is not.
3222 * This function detects such cases and then rewrites the schedule to
3224 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
3226 * In the new schedule, the schedule domains have the same offset (modulo
3227 * the stride), ensuring that the union of schedule domains is also strided.
3230 * If there is only a single domain in the component, then there is
3231 * nothing to do. Similarly, if the current schedule dimension has
3232 * a fixed value for almost all domains then there is nothing to be done.
3233 * In particular, we need at least two domains where the current schedule
3234 * dimension does not have a fixed value.
3235 * Finally, if any of the options refer to the current schedule dimension,
3236 * then we bail out as well. It would be possible to reformulate the options
3237 * in terms of the new schedule domain, but that would introduce constraints
3238 * that separate the domains in the options and that is something we would
3239 * like to avoid.
3242 * To see if there is any shifted stride, we look at the differences
3243 * between the values of the current dimension in pairs of domains
3244 * for equal values of outer dimensions. These differences should be
3245 * of the form
3247 * m x + r
3249 * with "m" the stride and "r" a constant. Note that we cannot perform
3250 * this analysis on individual domains as the lower bound in each domain
3251 * may depend on parameters or outer dimensions and so the current dimension
3252 * itself may not have a fixed remainder on division by the stride.
3254 * In particular, we compare the first domain that does not have an
3255 * obviously fixed value for the current dimension to itself and all
3256 * other domains and collect the offsets and the gcd of the strides.
3257 * If the gcd becomes one, then we failed to find shifted strides.
3258 * If the gcd is zero, then the differences were all fixed, meaning
3259 * that some domains had non-obviously fixed values for the current dimension.
3260 * If all the offsets are the same (for those domains that do not have
3261 * an obviously fixed value for the current dimension), then we do not
3262 * apply the transformation.
3263 * If none of the domains were skipped, then there is nothing to do.
3264 * If some of them were skipped, then if we apply separation, the schedule
3265 * domain should get split in pieces with a (non-shifted) stride.
3267 * Otherwise, we apply a shift to expose the stride in
3268 * generate_shift_component.
3270 static __isl_give isl_ast_graft_list *generate_component(
3271 struct isl_set_map_pair *domain, int *order, int n,
3272 __isl_take isl_ast_build *build)
3274 int i, d;
3275 int depth;
3276 isl_ctx *ctx;
3277 isl_map *map;
3278 isl_set *deltas;
3279 isl_val *gcd = NULL;
3280 isl_multi_val *mv;
3281 int fixed, skip;
3282 int base;
3283 isl_ast_graft_list *list;
3284 int res = 0;
3286 depth = isl_ast_build_get_depth(build);
3288 skip = n == 1;
3289 if (skip >= 0 && !skip)
3290 skip = at_most_one_non_fixed(domain, order, n, depth);
3291 if (skip >= 0 && !skip)
3292 skip = isl_ast_build_options_involve_depth(build);
3293 if (skip < 0)
3294 goto error;
3295 if (skip)
3296 return generate_shifted_component_from_list(domain,
3297 order, n, build);
3299 base = eliminate_non_fixed(domain, order, n, depth, build);
3300 if (base < 0)
3301 goto error;
3303 ctx = isl_ast_build_get_ctx(build);
3305 mv = isl_multi_val_zero(isl_space_set_alloc(ctx, 0, n));
3307 fixed = 1;
3308 for (i = 0; i < n; ++i) {
3309 isl_val *r, *m;
3311 map = isl_map_from_domain_and_range(
3312 isl_set_copy(domain[order[base]].set),
3313 isl_set_copy(domain[order[i]].set));
3314 for (d = 0; d < depth; ++d)
3315 map = isl_map_equate(map, isl_dim_in, d,
3316 isl_dim_out, d);
3317 deltas = isl_map_deltas(map);
3318 res = isl_set_dim_residue_class_val(deltas, depth, &m, &r);
3319 isl_set_free(deltas);
3320 if (res < 0)
3321 break;
3323 if (i == 0)
3324 gcd = m;
3325 else
3326 gcd = isl_val_gcd(gcd, m);
3327 if (isl_val_is_one(gcd)) {
3328 isl_val_free(r);
3329 break;
3331 mv = isl_multi_val_set_val(mv, i, r);
3333 res = dim_is_fixed(domain[order[i]].set, depth);
3334 if (res < 0)
3335 break;
3336 if (res)
3337 continue;
3339 if (fixed && i > base) {
3340 isl_val *a, *b;
3341 a = isl_multi_val_get_val(mv, i);
3342 b = isl_multi_val_get_val(mv, base);
3343 if (isl_val_ne(a, b))
3344 fixed = 0;
3345 isl_val_free(a);
3346 isl_val_free(b);
3350 if (res < 0 || !gcd) {
3351 isl_ast_build_free(build);
3352 list = NULL;
3353 } else if (i < n || fixed || isl_val_is_zero(gcd)) {
3354 list = generate_shifted_component_from_list(domain,
3355 order, n, build);
3356 } else {
3357 list = generate_shift_component(domain, order, n, gcd, mv,
3358 build);
3361 isl_val_free(gcd);
3362 isl_multi_val_free(mv);
3364 return list;
3365 error:
3366 isl_ast_build_free(build);
3367 return NULL;
3370 /* Store both "map" itself and its domain in the
3371 * structure pointed to by *next and advance to the next array element.
3373 static int extract_domain(__isl_take isl_map *map, void *user)
3375 struct isl_set_map_pair **next = user;
3377 (*next)->map = isl_map_copy(map);
3378 (*next)->set = isl_map_domain(map);
3379 (*next)++;
3381 return 0;
3384 /* Internal data for any_scheduled_after.
3386 * "depth" is the number of loops that have already been generated
3387 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
3388 * "domain" is an array of set-map pairs corresponding to the different
3389 * iteration domains. The set is the schedule domain, i.e., the domain
3390 * of the inverse schedule, while the map is the inverse schedule itself.
3392 struct isl_any_scheduled_after_data {
3393 int depth;
3394 int group_coscheduled;
3395 struct isl_set_map_pair *domain;
3398 /* Is any element of domain "i" scheduled after any element of domain "j"
3399 * (for a common iteration of the first data->depth loops)?
3401 * data->domain[i].set contains the domain of the inverse schedule
3402 * for domain "i", i.e., elements in the schedule domain.
3404 * If data->group_coscheduled is set, then we also return 1 if there
3405 * is any pair of elements in the two domains that are scheduled together.
3407 static int any_scheduled_after(int i, int j, void *user)
3409 struct isl_any_scheduled_after_data *data = user;
3410 int dim = isl_set_dim(data->domain[i].set, isl_dim_set);
3411 int pos;
3413 for (pos = data->depth; pos < dim; ++pos) {
3414 int follows;
3416 follows = isl_set_follows_at(data->domain[i].set,
3417 data->domain[j].set, pos);
3419 if (follows < -1)
3420 return -1;
3421 if (follows > 0)
3422 return 1;
3423 if (follows < 0)
3424 return 0;
3427 return data->group_coscheduled;
3430 /* Look for independent components at the current depth and generate code
3431 * for each component separately. The resulting lists of grafts are
3432 * merged in an attempt to combine grafts with identical guards.
3434 * Code for two domains can be generated separately if all the elements
3435 * of one domain are scheduled before (or together with) all the elements
3436 * of the other domain. We therefore consider the graph with as nodes
3437 * the domains and an edge between two nodes if any element of the first
3438 * node is scheduled after any element of the second node.
3439 * If the ast_build_group_coscheduled is set, then we also add an edge if
3440 * there is any pair of elements in the two domains that are scheduled
3441 * together.
3442 * Code is then generated (by generate_component)
3443 * for each of the strongly connected components in this graph
3444 * in their topological order.
3446 * Since the test is performed on the domain of the inverse schedules of
3447 * the different domains, we precompute these domains and store
3448 * them in data.domain.
3450 static __isl_give isl_ast_graft_list *generate_components(
3451 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3453 int i;
3454 isl_ctx *ctx = isl_ast_build_get_ctx(build);
3455 int n = isl_union_map_n_map(executed);
3456 struct isl_any_scheduled_after_data data;
3457 struct isl_set_map_pair *next;
3458 struct isl_tarjan_graph *g = NULL;
3459 isl_ast_graft_list *list = NULL;
3460 int n_domain = 0;
3462 data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
3463 if (!data.domain)
3464 goto error;
3465 n_domain = n;
3467 next = data.domain;
3468 if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
3469 goto error;
3471 if (!build)
3472 goto error;
3473 data.depth = isl_ast_build_get_depth(build);
3474 data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
3475 g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
3476 if (!g)
3477 goto error;
3479 list = isl_ast_graft_list_alloc(ctx, 0);
3481 i = 0;
3482 while (list && n) {
3483 isl_ast_graft_list *list_c;
3484 int first = i;
3486 if (g->order[i] == -1)
3487 isl_die(ctx, isl_error_internal, "cannot happen",
3488 goto error);
3489 ++i; --n;
3490 while (g->order[i] != -1) {
3491 ++i; --n;
3494 list_c = generate_component(data.domain,
3495 g->order + first, i - first,
3496 isl_ast_build_copy(build));
3497 list = isl_ast_graft_list_merge(list, list_c, build);
3499 ++i;
3502 if (0)
3503 error: list = isl_ast_graft_list_free(list);
3504 isl_tarjan_graph_free(g);
3505 for (i = 0; i < n_domain; ++i) {
3506 isl_map_free(data.domain[i].map);
3507 isl_set_free(data.domain[i].set);
3509 free(data.domain);
3510 isl_union_map_free(executed);
3511 isl_ast_build_free(build);
3513 return list;
3516 /* Generate code for the next level (and all inner levels).
3518 * If "executed" is empty, i.e., no code needs to be generated,
3519 * then we return an empty list.
3521 * If we have already generated code for all loop levels, then we pass
3522 * control to generate_inner_level.
3524 * If "executed" lives in a single space, i.e., if code needs to be
3525 * generated for a single domain, then there can only be a single
3526 * component and we go directly to generate_shifted_component.
3527 * Otherwise, we call generate_components to detect the components
3528 * and to call generate_component on each of them separately.
3530 static __isl_give isl_ast_graft_list *generate_next_level(
3531 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3533 int depth;
3535 if (!build || !executed)
3536 goto error;
3538 if (isl_union_map_is_empty(executed)) {
3539 isl_ctx *ctx = isl_ast_build_get_ctx(build);
3540 isl_union_map_free(executed);
3541 isl_ast_build_free(build);
3542 return isl_ast_graft_list_alloc(ctx, 0);
3545 depth = isl_ast_build_get_depth(build);
3546 if (depth >= isl_ast_build_dim(build, isl_dim_set))
3547 return generate_inner_level(executed, build);
3549 if (isl_union_map_n_map(executed) == 1)
3550 return generate_shifted_component(executed, build);
3552 return generate_components(executed, build);
3553 error:
3554 isl_union_map_free(executed);
3555 isl_ast_build_free(build);
3556 return NULL;
3559 /* Internal data structure used by isl_ast_build_ast_from_schedule.
3560 * internal, executed and build are the inputs to generate_code.
3561 * list collects the output.
3563 struct isl_generate_code_data {
3564 int internal;
3565 isl_union_map *executed;
3566 isl_ast_build *build;
3568 isl_ast_graft_list *list;
3571 /* Given an inverse schedule in terms of the external build schedule, i.e.,
3573 * [E -> S] -> D
3575 * with E the external build schedule and S the additional schedule "space",
3576 * reformulate the inverse schedule in terms of the internal schedule domain,
3577 * i.e., return
3579 * [I -> S] -> D
3581 * We first obtain a mapping
3583 * I -> E
3585 * take the inverse and the product with S -> S, resulting in
3587 * [I -> S] -> [E -> S]
3589 * Applying the map to the input produces the desired result.
3591 static __isl_give isl_union_map *internal_executed(
3592 __isl_take isl_union_map *executed, __isl_keep isl_space *space,
3593 __isl_keep isl_ast_build *build)
3595 isl_map *id, *proj;
3597 proj = isl_ast_build_get_schedule_map(build);
3598 proj = isl_map_reverse(proj);
3599 space = isl_space_map_from_set(isl_space_copy(space));
3600 id = isl_map_identity(space);
3601 proj = isl_map_product(proj, id);
3602 executed = isl_union_map_apply_domain(executed,
3603 isl_union_map_from_map(proj));
3604 return executed;
3607 /* Generate an AST that visits the elements in the range of data->executed
3608 * in the relative order specified by the corresponding domain element(s)
3609 * for those domain elements that belong to "set".
3610 * Add the result to data->list.
3612 * The caller ensures that "set" is a universe domain.
3613 * "space" is the space of the additional part of the schedule.
3614 * It is equal to the space of "set" if build->domain is parametric.
3615 * Otherwise, it is equal to the range of the wrapped space of "set".
3617 * If the build space is not parametric and if isl_ast_build_ast_from_schedule
3618 * was called from an outside user (data->internal not set), then
3619 * the (inverse) schedule refers to the external build domain and needs to
3620 * be transformed to refer to the internal build domain.
3622 * If the build space is parametric, then we add some of the parameter
3623 * constraints to the executed relation. Adding these constraints
3624 * allows for an earlier detection of conflicts in some cases.
3625 * However, we do not want to divide the executed relation into
3626 * more disjuncts than necessary. We therefore approximate
3627 * the constraints on the parameters by a single disjunct set.
3629 * The build is extended to include the additional part of the schedule.
3630 * If the original build space was not parametric, then the options
3631 * in data->build refer only to the additional part of the schedule
3632 * and they need to be adjusted to refer to the complete AST build
3633 * domain.
3635 * After having adjusted inverse schedule and build, we start generating
3636 * code with the outer loop of the current code generation
3637 * in generate_next_level.
3639 * If the original build space was not parametric, we undo the embedding
3640 * on the resulting isl_ast_node_list so that it can be used within
3641 * the outer AST build.
3643 static int generate_code_in_space(struct isl_generate_code_data *data,
3644 __isl_take isl_set *set, __isl_take isl_space *space)
3646 isl_union_map *executed;
3647 isl_ast_build *build;
3648 isl_ast_graft_list *list;
3649 int embed;
3651 executed = isl_union_map_copy(data->executed);
3652 executed = isl_union_map_intersect_domain(executed,
3653 isl_union_set_from_set(set));
3655 embed = !isl_set_is_params(data->build->domain);
3656 if (embed && !data->internal)
3657 executed = internal_executed(executed, space, data->build);
3658 if (!embed) {
3659 isl_set *domain;
3660 domain = isl_ast_build_get_domain(data->build);
3661 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
3662 executed = isl_union_map_intersect_params(executed, domain);
3665 build = isl_ast_build_copy(data->build);
3666 build = isl_ast_build_product(build, space);
3668 list = generate_next_level(executed, build);
3670 list = isl_ast_graft_list_unembed(list, embed);
3672 data->list = isl_ast_graft_list_concat(data->list, list);
3674 return 0;
3677 /* Generate an AST that visits the elements in the range of data->executed
3678 * in the relative order specified by the corresponding domain element(s)
3679 * for those domain elements that belong to "set".
3680 * Add the result to data->list.
3682 * The caller ensures that "set" is a universe domain.
3684 * If the build space S is not parametric, then the space of "set"
3685 * need to be a wrapped relation with S as domain. That is, it needs
3686 * to be of the form
3688 * [S -> T]
3690 * Check this property and pass control to generate_code_in_space
3691 * passing along T.
3692 * If the build space is not parametric, then T is the space of "set".
3694 static int generate_code_set(__isl_take isl_set *set, void *user)
3696 struct isl_generate_code_data *data = user;
3697 isl_space *space, *build_space;
3698 int is_domain;
3700 space = isl_set_get_space(set);
3702 if (isl_set_is_params(data->build->domain))
3703 return generate_code_in_space(data, set, space);
3705 build_space = isl_ast_build_get_space(data->build, data->internal);
3706 space = isl_space_unwrap(space);
3707 is_domain = isl_space_is_domain(build_space, space);
3708 isl_space_free(build_space);
3709 space = isl_space_range(space);
3711 if (is_domain < 0)
3712 goto error;
3713 if (!is_domain)
3714 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3715 "invalid nested schedule space", goto error);
3717 return generate_code_in_space(data, set, space);
3718 error:
3719 isl_set_free(set);
3720 isl_space_free(space);
3721 return -1;
3724 /* Generate an AST that visits the elements in the range of "executed"
3725 * in the relative order specified by the corresponding domain element(s).
3727 * "build" is an isl_ast_build that has either been constructed by
3728 * isl_ast_build_from_context or passed to a callback set by
3729 * isl_ast_build_set_create_leaf.
3730 * In the first case, the space of the isl_ast_build is typically
3731 * a parametric space, although this is currently not enforced.
3732 * In the second case, the space is never a parametric space.
3733 * If the space S is not parametric, then the domain space(s) of "executed"
3734 * need to be wrapped relations with S as domain.
3736 * If the domain of "executed" consists of several spaces, then an AST
3737 * is generated for each of them (in arbitrary order) and the results
3738 * are concatenated.
3740 * If "internal" is set, then the domain "S" above refers to the internal
3741 * schedule domain representation. Otherwise, it refers to the external
3742 * representation, as returned by isl_ast_build_get_schedule_space.
3744 * We essentially run over all the spaces in the domain of "executed"
3745 * and call generate_code_set on each of them.
3747 static __isl_give isl_ast_graft_list *generate_code(
3748 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3749 int internal)
3751 isl_ctx *ctx;
3752 struct isl_generate_code_data data = { 0 };
3753 isl_space *space;
3754 isl_union_set *schedule_domain;
3755 isl_union_map *universe;
3757 if (!build)
3758 goto error;
3759 space = isl_ast_build_get_space(build, 1);
3760 space = isl_space_align_params(space,
3761 isl_union_map_get_space(executed));
3762 space = isl_space_align_params(space,
3763 isl_union_map_get_space(build->options));
3764 build = isl_ast_build_align_params(build, isl_space_copy(space));
3765 executed = isl_union_map_align_params(executed, space);
3766 if (!executed || !build)
3767 goto error;
3769 ctx = isl_ast_build_get_ctx(build);
3771 data.internal = internal;
3772 data.executed = executed;
3773 data.build = build;
3774 data.list = isl_ast_graft_list_alloc(ctx, 0);
3776 universe = isl_union_map_universe(isl_union_map_copy(executed));
3777 schedule_domain = isl_union_map_domain(universe);
3778 if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
3779 &data) < 0)
3780 data.list = isl_ast_graft_list_free(data.list);
3782 isl_union_set_free(schedule_domain);
3783 isl_union_map_free(executed);
3785 isl_ast_build_free(build);
3786 return data.list;
3787 error:
3788 isl_union_map_free(executed);
3789 isl_ast_build_free(build);
3790 return NULL;
3793 /* Generate an AST that visits the elements in the domain of "schedule"
3794 * in the relative order specified by the corresponding image element(s).
3796 * "build" is an isl_ast_build that has either been constructed by
3797 * isl_ast_build_from_context or passed to a callback set by
3798 * isl_ast_build_set_create_leaf.
3799 * In the first case, the space of the isl_ast_build is typically
3800 * a parametric space, although this is currently not enforced.
3801 * In the second case, the space is never a parametric space.
3802 * If the space S is not parametric, then the range space(s) of "schedule"
3803 * need to be wrapped relations with S as domain.
3805 * If the range of "schedule" consists of several spaces, then an AST
3806 * is generated for each of them (in arbitrary order) and the results
3807 * are concatenated.
3809 * We first initialize the local copies of the relevant options.
3810 * We do this here rather than when the isl_ast_build is created
3811 * because the options may have changed between the construction
3812 * of the isl_ast_build and the call to isl_generate_code.
3814 * The main computation is performed on an inverse schedule (with
3815 * the schedule domain in the domain and the elements to be executed
3816 * in the range) called "executed".
3818 __isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
3819 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
3821 isl_ast_graft_list *list;
3822 isl_ast_node *node;
3823 isl_union_map *executed;
3825 build = isl_ast_build_copy(build);
3826 build = isl_ast_build_set_single_valued(build, 0);
3827 schedule = isl_union_map_coalesce(schedule);
3828 executed = isl_union_map_reverse(schedule);
3829 list = generate_code(executed, isl_ast_build_copy(build), 0);
3830 node = isl_ast_node_from_graft_list(list, build);
3831 isl_ast_build_free(build);
3833 return node;