isl_mat_variable_compression: use isl_mat_get_ctx
[isl.git] / isl_ast_codegen.c
blob350a8402165f206fcfd255677074c434ca0b54e4
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/constraint.h>
16 #include <isl/set.h>
17 #include <isl/ilp.h>
18 #include <isl/union_set.h>
19 #include <isl/union_map.h>
20 #include <isl/schedule_node.h>
21 #include <isl_sort.h>
22 #include <isl_tarjan.h>
23 #include <isl_ast_private.h>
24 #include <isl_ast_build_expr.h>
25 #include <isl_ast_build_private.h>
26 #include <isl_ast_graft_private.h>
28 /* Data used in generate_domain.
30 * "build" is the input build.
31 * "list" collects the results.
33 struct isl_generate_domain_data {
34 isl_ast_build *build;
36 isl_ast_graft_list *list;
39 static __isl_give isl_ast_graft_list *generate_next_level(
40 __isl_take isl_union_map *executed,
41 __isl_take isl_ast_build *build);
42 static __isl_give isl_ast_graft_list *generate_code(
43 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
44 int internal);
46 /* Generate an AST for a single domain based on
47 * the (non single valued) inverse schedule "executed".
49 * We extend the schedule with the iteration domain
50 * and continue generating through a call to generate_code.
52 * In particular, if executed has the form
54 * S -> D
56 * then we continue generating code on
58 * [S -> D] -> D
60 * The extended inverse schedule is clearly single valued
61 * ensuring that the nested generate_code will not reach this function,
62 * but will instead create calls to all elements of D that need
63 * to be executed from the current schedule domain.
65 static isl_stat generate_non_single_valued(__isl_take isl_map *executed,
66 struct isl_generate_domain_data *data)
68 isl_map *identity;
69 isl_ast_build *build;
70 isl_ast_graft_list *list;
72 build = isl_ast_build_copy(data->build);
74 identity = isl_set_identity(isl_map_range(isl_map_copy(executed)));
75 executed = isl_map_domain_product(executed, identity);
76 build = isl_ast_build_set_single_valued(build, 1);
78 list = generate_code(isl_union_map_from_map(executed), build, 1);
80 data->list = isl_ast_graft_list_concat(data->list, list);
82 return isl_stat_ok;
85 /* Call the at_each_domain callback, if requested by the user,
86 * after recording the current inverse schedule in the build.
88 static __isl_give isl_ast_graft *at_each_domain(__isl_take isl_ast_graft *graft,
89 __isl_keep isl_map *executed, __isl_keep isl_ast_build *build)
91 if (!graft || !build)
92 return isl_ast_graft_free(graft);
93 if (!build->at_each_domain)
94 return graft;
96 build = isl_ast_build_copy(build);
97 build = isl_ast_build_set_executed(build,
98 isl_union_map_from_map(isl_map_copy(executed)));
99 if (!build)
100 return isl_ast_graft_free(graft);
102 graft->node = build->at_each_domain(graft->node,
103 build, build->at_each_domain_user);
104 isl_ast_build_free(build);
106 if (!graft->node)
107 graft = isl_ast_graft_free(graft);
109 return graft;
112 /* Generate a call expression for the single executed
113 * domain element "map" and put a guard around it based its (simplified)
114 * domain. "executed" is the original inverse schedule from which "map"
115 * has been derived. In particular, "map" is either identical to "executed"
116 * or it is the result of gisting "executed" with respect to the build domain.
117 * "executed" is only used if there is an at_each_domain callback.
119 * At this stage, any pending constraints in the build can no longer
120 * be simplified with respect to any enforced constraints since
121 * the call node does not have any enforced constraints.
122 * Since all pending constraints not covered by any enforced constraints
123 * will be added as a guard to the graft in create_node_scaled,
124 * even in the eliminated case, the pending constraints
125 * can be considered to have been generated by outer constructs.
127 * If the user has set an at_each_domain callback, it is called
128 * on the constructed call expression node.
130 static isl_stat add_domain(__isl_take isl_map *executed,
131 __isl_take isl_map *map, struct isl_generate_domain_data *data)
133 isl_ast_build *build;
134 isl_ast_graft *graft;
135 isl_ast_graft_list *list;
136 isl_set *guard, *pending;
138 build = isl_ast_build_copy(data->build);
139 pending = isl_ast_build_get_pending(build);
140 build = isl_ast_build_replace_pending_by_guard(build, pending);
142 guard = isl_map_domain(isl_map_copy(map));
143 guard = isl_set_compute_divs(guard);
144 guard = isl_set_coalesce(guard);
145 guard = isl_set_gist(guard, isl_ast_build_get_generated(build));
146 guard = isl_ast_build_specialize(build, guard);
148 graft = isl_ast_graft_alloc_domain(map, build);
149 graft = at_each_domain(graft, executed, build);
150 isl_ast_build_free(build);
151 isl_map_free(executed);
152 graft = isl_ast_graft_add_guard(graft, guard, data->build);
154 list = isl_ast_graft_list_from_ast_graft(graft);
155 data->list = isl_ast_graft_list_concat(data->list, list);
157 return isl_stat_ok;
160 /* Generate an AST for a single domain based on
161 * the inverse schedule "executed" and add it to data->list.
163 * If there is more than one domain element associated to the current
164 * schedule "time", then we need to continue the generation process
165 * in generate_non_single_valued.
166 * Note that the inverse schedule being single-valued may depend
167 * on constraints that are only available in the original context
168 * domain specified by the user. We therefore first introduce
169 * some of the constraints of data->build->domain. In particular,
170 * we intersect with a single-disjunct approximation of this set.
171 * We perform this approximation to avoid further splitting up
172 * the executed relation, possibly introducing a disjunctive guard
173 * on the statement.
175 * On the other hand, we only perform the test after having taken the gist
176 * of the domain as the resulting map is the one from which the call
177 * expression is constructed. Using this map to construct the call
178 * expression usually yields simpler results in cases where the original
179 * map is not obviously single-valued.
180 * If the original map is obviously single-valued, then the gist
181 * operation is skipped.
183 * Because we perform the single-valuedness test on the gisted map,
184 * we may in rare cases fail to recognize that the inverse schedule
185 * is single-valued. This becomes problematic if this happens
186 * from the recursive call through generate_non_single_valued
187 * as we would then end up in an infinite recursion.
188 * We therefore check if we are inside a call to generate_non_single_valued
189 * and revert to the ungisted map if the gisted map turns out not to be
190 * single-valued.
192 * Otherwise, call add_domain to generate a call expression (with guard) and
193 * to call the at_each_domain callback, if any.
195 static isl_stat generate_domain(__isl_take isl_map *executed, void *user)
197 struct isl_generate_domain_data *data = user;
198 isl_set *domain;
199 isl_map *map = NULL;
200 int empty, sv;
202 domain = isl_ast_build_get_domain(data->build);
203 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
204 executed = isl_map_intersect_domain(executed, domain);
205 empty = isl_map_is_empty(executed);
206 if (empty < 0)
207 goto error;
208 if (empty) {
209 isl_map_free(executed);
210 return isl_stat_ok;
213 sv = isl_map_plain_is_single_valued(executed);
214 if (sv < 0)
215 goto error;
216 if (sv)
217 return add_domain(executed, isl_map_copy(executed), data);
219 executed = isl_map_coalesce(executed);
220 map = isl_map_copy(executed);
221 map = isl_ast_build_compute_gist_map_domain(data->build, map);
222 sv = isl_map_is_single_valued(map);
223 if (sv < 0)
224 goto error;
225 if (!sv) {
226 isl_map_free(map);
227 if (data->build->single_valued)
228 map = isl_map_copy(executed);
229 else
230 return generate_non_single_valued(executed, data);
233 return add_domain(executed, map, data);
234 error:
235 isl_map_free(map);
236 isl_map_free(executed);
237 return isl_stat_error;
240 /* Call build->create_leaf to a create "leaf" node in the AST,
241 * encapsulate the result in an isl_ast_graft and return the result
242 * as a 1-element list.
244 * Note that the node returned by the user may be an entire tree.
246 * Since the node itself cannot enforce any constraints, we turn
247 * all pending constraints into guards and add them to the resulting
248 * graft to ensure that they will be generated.
250 * Before we pass control to the user, we first clear some information
251 * from the build that is (presumbably) only meaningful
252 * for the current code generation.
253 * This includes the create_leaf callback itself, so we make a copy
254 * of the build first.
256 static __isl_give isl_ast_graft_list *call_create_leaf(
257 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
259 isl_set *guard;
260 isl_ast_node *node;
261 isl_ast_graft *graft;
262 isl_ast_build *user_build;
264 guard = isl_ast_build_get_pending(build);
265 user_build = isl_ast_build_copy(build);
266 user_build = isl_ast_build_replace_pending_by_guard(user_build,
267 isl_set_copy(guard));
268 user_build = isl_ast_build_set_executed(user_build, executed);
269 user_build = isl_ast_build_clear_local_info(user_build);
270 if (!user_build)
271 node = NULL;
272 else
273 node = build->create_leaf(user_build, build->create_leaf_user);
274 graft = isl_ast_graft_alloc(node, build);
275 graft = isl_ast_graft_add_guard(graft, guard, build);
276 isl_ast_build_free(build);
277 return isl_ast_graft_list_from_ast_graft(graft);
280 static __isl_give isl_ast_graft_list *build_ast_from_child(
281 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
282 __isl_take isl_union_map *executed);
284 /* Generate an AST after having handled the complete schedule
285 * of this call to the code generator or the complete band
286 * if we are generating an AST from a schedule tree.
288 * If we are inside a band node, then move on to the child of the band.
290 * If the user has specified a create_leaf callback, control
291 * is passed to the user in call_create_leaf.
293 * Otherwise, we generate one or more calls for each individual
294 * domain in generate_domain.
296 static __isl_give isl_ast_graft_list *generate_inner_level(
297 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
299 isl_ctx *ctx;
300 struct isl_generate_domain_data data = { build };
302 if (!build || !executed)
303 goto error;
305 if (isl_ast_build_has_schedule_node(build)) {
306 isl_schedule_node *node;
307 node = isl_ast_build_get_schedule_node(build);
308 build = isl_ast_build_reset_schedule_node(build);
309 return build_ast_from_child(build, node, executed);
312 if (build->create_leaf)
313 return call_create_leaf(executed, build);
315 ctx = isl_union_map_get_ctx(executed);
316 data.list = isl_ast_graft_list_alloc(ctx, 0);
317 if (isl_union_map_foreach_map(executed, &generate_domain, &data) < 0)
318 data.list = isl_ast_graft_list_free(data.list);
320 if (0)
321 error: data.list = NULL;
322 isl_ast_build_free(build);
323 isl_union_map_free(executed);
324 return data.list;
327 /* Call the before_each_for callback, if requested by the user.
329 static __isl_give isl_ast_node *before_each_for(__isl_take isl_ast_node *node,
330 __isl_keep isl_ast_build *build)
332 isl_id *id;
334 if (!node || !build)
335 return isl_ast_node_free(node);
336 if (!build->before_each_for)
337 return node;
338 id = build->before_each_for(build, build->before_each_for_user);
339 node = isl_ast_node_set_annotation(node, id);
340 return node;
343 /* Call the after_each_for callback, if requested by the user.
345 static __isl_give isl_ast_graft *after_each_for(__isl_take isl_ast_graft *graft,
346 __isl_keep isl_ast_build *build)
348 if (!graft || !build)
349 return isl_ast_graft_free(graft);
350 if (!build->after_each_for)
351 return graft;
352 graft->node = build->after_each_for(graft->node, build,
353 build->after_each_for_user);
354 if (!graft->node)
355 return isl_ast_graft_free(graft);
356 return graft;
359 /* Plug in all the know values of the current and outer dimensions
360 * in the domain of "executed". In principle, we only need to plug
361 * in the known value of the current dimension since the values of
362 * outer dimensions have been plugged in already.
363 * However, it turns out to be easier to just plug in all known values.
365 static __isl_give isl_union_map *plug_in_values(
366 __isl_take isl_union_map *executed, __isl_keep isl_ast_build *build)
368 return isl_ast_build_substitute_values_union_map_domain(build,
369 executed);
372 /* Check if the constraint "c" is a lower bound on dimension "pos",
373 * an upper bound, or independent of dimension "pos".
375 static int constraint_type(isl_constraint *c, int pos)
377 if (isl_constraint_is_lower_bound(c, isl_dim_set, pos))
378 return 1;
379 if (isl_constraint_is_upper_bound(c, isl_dim_set, pos))
380 return 2;
381 return 0;
384 /* Compare the types of the constraints "a" and "b",
385 * resulting in constraints that are independent of "depth"
386 * to be sorted before the lower bounds on "depth", which in
387 * turn are sorted before the upper bounds on "depth".
389 static int cmp_constraint(__isl_keep isl_constraint *a,
390 __isl_keep isl_constraint *b, void *user)
392 int *depth = user;
393 int t1 = constraint_type(a, *depth);
394 int t2 = constraint_type(b, *depth);
396 return t1 - t2;
399 /* Extract a lower bound on dimension "pos" from constraint "c".
401 * If the constraint is of the form
403 * a x + f(...) >= 0
405 * then we essentially return
407 * l = ceil(-f(...)/a)
409 * However, if the current dimension is strided, then we need to make
410 * sure that the lower bound we construct is of the form
412 * f + s a
414 * with f the offset and s the stride.
415 * We therefore compute
417 * f + s * ceil((l - f)/s)
419 static __isl_give isl_aff *lower_bound(__isl_keep isl_constraint *c,
420 int pos, __isl_keep isl_ast_build *build)
422 isl_aff *aff;
424 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
425 aff = isl_aff_ceil(aff);
427 if (isl_ast_build_has_stride(build, pos)) {
428 isl_aff *offset;
429 isl_val *stride;
431 offset = isl_ast_build_get_offset(build, pos);
432 stride = isl_ast_build_get_stride(build, pos);
434 aff = isl_aff_sub(aff, isl_aff_copy(offset));
435 aff = isl_aff_scale_down_val(aff, isl_val_copy(stride));
436 aff = isl_aff_ceil(aff);
437 aff = isl_aff_scale_val(aff, stride);
438 aff = isl_aff_add(aff, offset);
441 aff = isl_ast_build_compute_gist_aff(build, aff);
443 return aff;
446 /* Return the exact lower bound (or upper bound if "upper" is set)
447 * of "domain" as a piecewise affine expression.
449 * If we are computing a lower bound (of a strided dimension), then
450 * we need to make sure it is of the form
452 * f + s a
454 * where f is the offset and s is the stride.
455 * We therefore need to include the stride constraint before computing
456 * the minimum.
458 static __isl_give isl_pw_aff *exact_bound(__isl_keep isl_set *domain,
459 __isl_keep isl_ast_build *build, int upper)
461 isl_set *stride;
462 isl_map *it_map;
463 isl_pw_aff *pa;
464 isl_pw_multi_aff *pma;
466 domain = isl_set_copy(domain);
467 if (!upper) {
468 stride = isl_ast_build_get_stride_constraint(build);
469 domain = isl_set_intersect(domain, stride);
471 it_map = isl_ast_build_map_to_iterator(build, domain);
472 if (upper)
473 pma = isl_map_lexmax_pw_multi_aff(it_map);
474 else
475 pma = isl_map_lexmin_pw_multi_aff(it_map);
476 pa = isl_pw_multi_aff_get_pw_aff(pma, 0);
477 isl_pw_multi_aff_free(pma);
478 pa = isl_ast_build_compute_gist_pw_aff(build, pa);
479 pa = isl_pw_aff_coalesce(pa);
481 return pa;
484 /* Callback for sorting the isl_pw_aff_list passed to reduce_list and
485 * remove_redundant_lower_bounds.
487 static int reduce_list_cmp(__isl_keep isl_pw_aff *a, __isl_keep isl_pw_aff *b,
488 void *user)
490 return isl_pw_aff_plain_cmp(a, b);
493 /* Given a list of lower bounds "list", remove those that are redundant
494 * with respect to the other bounds in "list" and the domain of "build".
496 * We first sort the bounds in the same way as they would be sorted
497 * by set_for_node_expressions so that we can try and remove the last
498 * bounds first.
500 * For a lower bound to be effective, there needs to be at least
501 * one domain element for which it is larger than all other lower bounds.
502 * For each lower bound we therefore intersect the domain with
503 * the conditions that it is larger than all other bounds and
504 * check whether the result is empty. If so, the bound can be removed.
506 static __isl_give isl_pw_aff_list *remove_redundant_lower_bounds(
507 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
509 int i, j, n;
510 isl_set *domain;
512 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
513 if (!list)
514 return NULL;
516 n = isl_pw_aff_list_n_pw_aff(list);
517 if (n <= 1)
518 return list;
520 domain = isl_ast_build_get_domain(build);
522 for (i = n - 1; i >= 0; --i) {
523 isl_pw_aff *pa_i;
524 isl_set *domain_i;
525 int empty;
527 domain_i = isl_set_copy(domain);
528 pa_i = isl_pw_aff_list_get_pw_aff(list, i);
530 for (j = 0; j < n; ++j) {
531 isl_pw_aff *pa_j;
532 isl_set *better;
534 if (j == i)
535 continue;
537 pa_j = isl_pw_aff_list_get_pw_aff(list, j);
538 better = isl_pw_aff_gt_set(isl_pw_aff_copy(pa_i), pa_j);
539 domain_i = isl_set_intersect(domain_i, better);
542 empty = isl_set_is_empty(domain_i);
544 isl_set_free(domain_i);
545 isl_pw_aff_free(pa_i);
547 if (empty < 0)
548 goto error;
549 if (!empty)
550 continue;
551 list = isl_pw_aff_list_drop(list, i, 1);
552 n--;
555 isl_set_free(domain);
557 return list;
558 error:
559 isl_set_free(domain);
560 return isl_pw_aff_list_free(list);
563 /* Extract a lower bound on dimension "pos" from each constraint
564 * in "constraints" and return the list of lower bounds.
565 * If "constraints" has zero elements, then we extract a lower bound
566 * from "domain" instead.
568 * If the current dimension is strided, then the lower bound
569 * is adjusted by lower_bound to match the stride information.
570 * This modification may make one or more lower bounds redundant
571 * with respect to the other lower bounds. We therefore check
572 * for this condition and remove the redundant lower bounds.
574 static __isl_give isl_pw_aff_list *lower_bounds(
575 __isl_keep isl_constraint_list *constraints, int pos,
576 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
578 isl_ctx *ctx;
579 isl_pw_aff_list *list;
580 int i, n;
582 if (!build)
583 return NULL;
585 n = isl_constraint_list_n_constraint(constraints);
586 if (n == 0) {
587 isl_pw_aff *pa;
588 pa = exact_bound(domain, build, 0);
589 return isl_pw_aff_list_from_pw_aff(pa);
592 ctx = isl_ast_build_get_ctx(build);
593 list = isl_pw_aff_list_alloc(ctx,n);
595 for (i = 0; i < n; ++i) {
596 isl_aff *aff;
597 isl_constraint *c;
599 c = isl_constraint_list_get_constraint(constraints, i);
600 aff = lower_bound(c, pos, build);
601 isl_constraint_free(c);
602 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
605 if (isl_ast_build_has_stride(build, pos))
606 list = remove_redundant_lower_bounds(list, build);
608 return list;
611 /* Extract an upper bound on dimension "pos" from each constraint
612 * in "constraints" and return the list of upper bounds.
613 * If "constraints" has zero elements, then we extract an upper bound
614 * from "domain" instead.
616 static __isl_give isl_pw_aff_list *upper_bounds(
617 __isl_keep isl_constraint_list *constraints, int pos,
618 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
620 isl_ctx *ctx;
621 isl_pw_aff_list *list;
622 int i, n;
624 n = isl_constraint_list_n_constraint(constraints);
625 if (n == 0) {
626 isl_pw_aff *pa;
627 pa = exact_bound(domain, build, 1);
628 return isl_pw_aff_list_from_pw_aff(pa);
631 ctx = isl_ast_build_get_ctx(build);
632 list = isl_pw_aff_list_alloc(ctx,n);
634 for (i = 0; i < n; ++i) {
635 isl_aff *aff;
636 isl_constraint *c;
638 c = isl_constraint_list_get_constraint(constraints, i);
639 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
640 isl_constraint_free(c);
641 aff = isl_aff_floor(aff);
642 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
645 return list;
648 /* Return an isl_ast_expr that performs the reduction of type "type"
649 * on AST expressions corresponding to the elements in "list".
651 * The list is assumed to contain at least one element.
652 * If the list contains exactly one element, then the returned isl_ast_expr
653 * simply computes that affine expression.
654 * If the list contains more than one element, then we sort it
655 * using a fairly abitrary but hopefully reasonably stable order.
657 static __isl_give isl_ast_expr *reduce_list(enum isl_ast_op_type type,
658 __isl_keep isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
660 int i, n;
661 isl_ctx *ctx;
662 isl_ast_expr *expr;
664 if (!list)
665 return NULL;
667 n = isl_pw_aff_list_n_pw_aff(list);
669 if (n == 1)
670 return isl_ast_build_expr_from_pw_aff_internal(build,
671 isl_pw_aff_list_get_pw_aff(list, 0));
673 ctx = isl_pw_aff_list_get_ctx(list);
674 expr = isl_ast_expr_alloc_op(ctx, type, n);
675 if (!expr)
676 return NULL;
678 list = isl_pw_aff_list_copy(list);
679 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
680 if (!list)
681 return isl_ast_expr_free(expr);
683 for (i = 0; i < n; ++i) {
684 isl_ast_expr *expr_i;
686 expr_i = isl_ast_build_expr_from_pw_aff_internal(build,
687 isl_pw_aff_list_get_pw_aff(list, i));
688 if (!expr_i)
689 goto error;
690 expr->u.op.args[i] = expr_i;
693 isl_pw_aff_list_free(list);
694 return expr;
695 error:
696 isl_pw_aff_list_free(list);
697 isl_ast_expr_free(expr);
698 return NULL;
701 /* Add guards implied by the "generated constraints",
702 * but not (necessarily) enforced by the generated AST to "guard".
703 * In particular, if there is any stride constraints,
704 * then add the guard implied by those constraints.
705 * If we have generated a degenerate loop, then add the guard
706 * implied by "bounds" on the outer dimensions, i.e., the guard
707 * that ensures that the single value actually exists.
708 * Since there may also be guards implied by a combination
709 * of these constraints, we first combine them before
710 * deriving the implied constraints.
712 static __isl_give isl_set *add_implied_guards(__isl_take isl_set *guard,
713 int degenerate, __isl_keep isl_basic_set *bounds,
714 __isl_keep isl_ast_build *build)
716 int depth, has_stride;
717 isl_space *space;
718 isl_set *dom, *set;
720 depth = isl_ast_build_get_depth(build);
721 has_stride = isl_ast_build_has_stride(build, depth);
722 if (!has_stride && !degenerate)
723 return guard;
725 space = isl_basic_set_get_space(bounds);
726 dom = isl_set_universe(space);
728 if (degenerate) {
729 bounds = isl_basic_set_copy(bounds);
730 bounds = isl_basic_set_drop_constraints_not_involving_dims(
731 bounds, isl_dim_set, depth, 1);
732 set = isl_set_from_basic_set(bounds);
733 dom = isl_set_intersect(dom, set);
736 if (has_stride) {
737 set = isl_ast_build_get_stride_constraint(build);
738 dom = isl_set_intersect(dom, set);
741 dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
742 dom = isl_ast_build_compute_gist(build, dom);
743 guard = isl_set_intersect(guard, dom);
745 return guard;
748 /* Update "graft" based on "sub_build" for the degenerate case.
750 * "build" is the build in which graft->node was created
751 * "sub_build" contains information about the current level itself,
752 * including the single value attained.
754 * We set the initialization part of the for loop to the single
755 * value attained by the current dimension.
756 * The increment and condition are not strictly needed as the are known
757 * to be "1" and "iterator <= value" respectively.
759 static __isl_give isl_ast_graft *refine_degenerate(
760 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build,
761 __isl_keep isl_ast_build *sub_build)
763 isl_pw_aff *value;
765 if (!graft || !sub_build)
766 return isl_ast_graft_free(graft);
768 value = isl_pw_aff_copy(sub_build->value);
770 graft->node->u.f.init = isl_ast_build_expr_from_pw_aff_internal(build,
771 value);
772 if (!graft->node->u.f.init)
773 return isl_ast_graft_free(graft);
775 return graft;
778 /* Return the intersection of constraints in "list" as a set.
780 static __isl_give isl_set *intersect_constraints(
781 __isl_keep isl_constraint_list *list)
783 int i, n;
784 isl_basic_set *bset;
786 n = isl_constraint_list_n_constraint(list);
787 if (n < 1)
788 isl_die(isl_constraint_list_get_ctx(list), isl_error_internal,
789 "expecting at least one constraint", return NULL);
791 bset = isl_basic_set_from_constraint(
792 isl_constraint_list_get_constraint(list, 0));
793 for (i = 1; i < n; ++i) {
794 isl_basic_set *bset_i;
796 bset_i = isl_basic_set_from_constraint(
797 isl_constraint_list_get_constraint(list, i));
798 bset = isl_basic_set_intersect(bset, bset_i);
801 return isl_set_from_basic_set(bset);
804 /* Compute the constraints on the outer dimensions enforced by
805 * graft->node and add those constraints to graft->enforced,
806 * in case the upper bound is expressed as a set "upper".
808 * In particular, if l(...) is a lower bound in "lower", and
810 * -a i + f(...) >= 0 or a i <= f(...)
812 * is an upper bound ocnstraint on the current dimension i,
813 * then the for loop enforces the constraint
815 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
817 * We therefore simply take each lower bound in turn, plug it into
818 * the upper bounds and compute the intersection over all lower bounds.
820 * If a lower bound is a rational expression, then
821 * isl_basic_set_preimage_multi_aff will force this rational
822 * expression to have only integer values. However, the loop
823 * itself does not enforce this integrality constraint. We therefore
824 * use the ceil of the lower bounds instead of the lower bounds themselves.
825 * Other constraints will make sure that the for loop is only executed
826 * when each of the lower bounds attains an integral value.
827 * In particular, potentially rational values only occur in
828 * lower_bound if the offset is a (seemingly) rational expression,
829 * but then outer conditions will make sure that this rational expression
830 * only attains integer values.
832 static __isl_give isl_ast_graft *set_enforced_from_set(
833 __isl_take isl_ast_graft *graft,
834 __isl_keep isl_pw_aff_list *lower, int pos, __isl_keep isl_set *upper)
836 isl_space *space;
837 isl_basic_set *enforced;
838 isl_pw_multi_aff *pma;
839 int i, n;
841 if (!graft || !lower)
842 return isl_ast_graft_free(graft);
844 space = isl_set_get_space(upper);
845 enforced = isl_basic_set_universe(isl_space_copy(space));
847 space = isl_space_map_from_set(space);
848 pma = isl_pw_multi_aff_identity(space);
850 n = isl_pw_aff_list_n_pw_aff(lower);
851 for (i = 0; i < n; ++i) {
852 isl_pw_aff *pa;
853 isl_set *enforced_i;
854 isl_basic_set *hull;
855 isl_pw_multi_aff *pma_i;
857 pa = isl_pw_aff_list_get_pw_aff(lower, i);
858 pa = isl_pw_aff_ceil(pa);
859 pma_i = isl_pw_multi_aff_copy(pma);
860 pma_i = isl_pw_multi_aff_set_pw_aff(pma_i, pos, pa);
861 enforced_i = isl_set_copy(upper);
862 enforced_i = isl_set_preimage_pw_multi_aff(enforced_i, pma_i);
863 hull = isl_set_simple_hull(enforced_i);
864 enforced = isl_basic_set_intersect(enforced, hull);
867 isl_pw_multi_aff_free(pma);
869 graft = isl_ast_graft_enforce(graft, enforced);
871 return graft;
874 /* Compute the constraints on the outer dimensions enforced by
875 * graft->node and add those constraints to graft->enforced,
876 * in case the upper bound is expressed as
877 * a list of affine expressions "upper".
879 * The enforced condition is that each lower bound expression is less
880 * than or equal to each upper bound expression.
882 static __isl_give isl_ast_graft *set_enforced_from_list(
883 __isl_take isl_ast_graft *graft,
884 __isl_keep isl_pw_aff_list *lower, __isl_keep isl_pw_aff_list *upper)
886 isl_set *cond;
887 isl_basic_set *enforced;
889 lower = isl_pw_aff_list_copy(lower);
890 upper = isl_pw_aff_list_copy(upper);
891 cond = isl_pw_aff_list_le_set(lower, upper);
892 enforced = isl_set_simple_hull(cond);
893 graft = isl_ast_graft_enforce(graft, enforced);
895 return graft;
898 /* Does "aff" have a negative constant term?
900 static isl_stat aff_constant_is_negative(__isl_take isl_set *set,
901 __isl_take isl_aff *aff, void *user)
903 int *neg = user;
904 isl_val *v;
906 v = isl_aff_get_constant_val(aff);
907 *neg = isl_val_is_neg(v);
908 isl_val_free(v);
909 isl_set_free(set);
910 isl_aff_free(aff);
912 return *neg ? isl_stat_ok : isl_stat_error;
915 /* Does "pa" have a negative constant term over its entire domain?
917 static isl_stat pw_aff_constant_is_negative(__isl_take isl_pw_aff *pa,
918 void *user)
920 isl_stat r;
921 int *neg = user;
923 r = isl_pw_aff_foreach_piece(pa, &aff_constant_is_negative, user);
924 isl_pw_aff_free(pa);
926 return (*neg && r >= 0) ? isl_stat_ok : isl_stat_error;
929 /* Does each element in "list" have a negative constant term?
931 * The callback terminates the iteration as soon an element has been
932 * found that does not have a negative constant term.
934 static int list_constant_is_negative(__isl_keep isl_pw_aff_list *list)
936 int neg = 1;
938 if (isl_pw_aff_list_foreach(list,
939 &pw_aff_constant_is_negative, &neg) < 0 && neg)
940 return -1;
942 return neg;
945 /* Add 1 to each of the elements in "list", where each of these elements
946 * is defined over the internal schedule space of "build".
948 static __isl_give isl_pw_aff_list *list_add_one(
949 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
951 int i, n;
952 isl_space *space;
953 isl_aff *aff;
954 isl_pw_aff *one;
956 space = isl_ast_build_get_space(build, 1);
957 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
958 aff = isl_aff_add_constant_si(aff, 1);
959 one = isl_pw_aff_from_aff(aff);
961 n = isl_pw_aff_list_n_pw_aff(list);
962 for (i = 0; i < n; ++i) {
963 isl_pw_aff *pa;
964 pa = isl_pw_aff_list_get_pw_aff(list, i);
965 pa = isl_pw_aff_add(pa, isl_pw_aff_copy(one));
966 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
969 isl_pw_aff_free(one);
971 return list;
974 /* Set the condition part of the for node graft->node in case
975 * the upper bound is represented as a list of piecewise affine expressions.
977 * In particular, set the condition to
979 * iterator <= min(list of upper bounds)
981 * If each of the upper bounds has a negative constant term, then
982 * set the condition to
984 * iterator < min(list of (upper bound + 1)s)
987 static __isl_give isl_ast_graft *set_for_cond_from_list(
988 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *list,
989 __isl_keep isl_ast_build *build)
991 int neg;
992 isl_ast_expr *bound, *iterator, *cond;
993 enum isl_ast_op_type type = isl_ast_op_le;
995 if (!graft || !list)
996 return isl_ast_graft_free(graft);
998 neg = list_constant_is_negative(list);
999 if (neg < 0)
1000 return isl_ast_graft_free(graft);
1001 list = isl_pw_aff_list_copy(list);
1002 if (neg) {
1003 list = list_add_one(list, build);
1004 type = isl_ast_op_lt;
1007 bound = reduce_list(isl_ast_op_min, list, build);
1008 iterator = isl_ast_expr_copy(graft->node->u.f.iterator);
1009 cond = isl_ast_expr_alloc_binary(type, iterator, bound);
1010 graft->node->u.f.cond = cond;
1012 isl_pw_aff_list_free(list);
1013 if (!graft->node->u.f.cond)
1014 return isl_ast_graft_free(graft);
1015 return graft;
1018 /* Set the condition part of the for node graft->node in case
1019 * the upper bound is represented as a set.
1021 static __isl_give isl_ast_graft *set_for_cond_from_set(
1022 __isl_take isl_ast_graft *graft, __isl_keep isl_set *set,
1023 __isl_keep isl_ast_build *build)
1025 isl_ast_expr *cond;
1027 if (!graft)
1028 return NULL;
1030 cond = isl_ast_build_expr_from_set_internal(build, isl_set_copy(set));
1031 graft->node->u.f.cond = cond;
1032 if (!graft->node->u.f.cond)
1033 return isl_ast_graft_free(graft);
1034 return graft;
1037 /* Construct an isl_ast_expr for the increment (i.e., stride) of
1038 * the current dimension.
1040 static __isl_give isl_ast_expr *for_inc(__isl_keep isl_ast_build *build)
1042 int depth;
1043 isl_val *v;
1044 isl_ctx *ctx;
1046 if (!build)
1047 return NULL;
1048 ctx = isl_ast_build_get_ctx(build);
1049 depth = isl_ast_build_get_depth(build);
1051 if (!isl_ast_build_has_stride(build, depth))
1052 return isl_ast_expr_alloc_int_si(ctx, 1);
1054 v = isl_ast_build_get_stride(build, depth);
1055 return isl_ast_expr_from_val(v);
1058 /* Should we express the loop condition as
1060 * iterator <= min(list of upper bounds)
1062 * or as a conjunction of constraints?
1064 * The first is constructed from a list of upper bounds.
1065 * The second is constructed from a set.
1067 * If there are no upper bounds in "constraints", then this could mean
1068 * that "domain" simply doesn't have an upper bound or that we didn't
1069 * pick any upper bound. In the first case, we want to generate the
1070 * loop condition as a(n empty) conjunction of constraints
1071 * In the second case, we will compute
1072 * a single upper bound from "domain" and so we use the list form.
1074 * If there are upper bounds in "constraints",
1075 * then we use the list form iff the atomic_upper_bound option is set.
1077 static int use_upper_bound_list(isl_ctx *ctx, int n_upper,
1078 __isl_keep isl_set *domain, int depth)
1080 if (n_upper > 0)
1081 return isl_options_get_ast_build_atomic_upper_bound(ctx);
1082 else
1083 return isl_set_dim_has_upper_bound(domain, isl_dim_set, depth);
1086 /* Fill in the expressions of the for node in graft->node.
1088 * In particular,
1089 * - set the initialization part of the loop to the maximum of the lower bounds
1090 * - extract the increment from the stride of the current dimension
1091 * - construct the for condition either based on a list of upper bounds
1092 * or on a set of upper bound constraints.
1094 static __isl_give isl_ast_graft *set_for_node_expressions(
1095 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *lower,
1096 int use_list, __isl_keep isl_pw_aff_list *upper_list,
1097 __isl_keep isl_set *upper_set, __isl_keep isl_ast_build *build)
1099 isl_ast_node *node;
1101 if (!graft)
1102 return NULL;
1104 build = isl_ast_build_copy(build);
1106 node = graft->node;
1107 node->u.f.init = reduce_list(isl_ast_op_max, lower, build);
1108 node->u.f.inc = for_inc(build);
1110 if (use_list)
1111 graft = set_for_cond_from_list(graft, upper_list, build);
1112 else
1113 graft = set_for_cond_from_set(graft, upper_set, build);
1115 isl_ast_build_free(build);
1117 if (!node->u.f.iterator || !node->u.f.init ||
1118 !node->u.f.cond || !node->u.f.inc)
1119 return isl_ast_graft_free(graft);
1121 return graft;
1124 /* Update "graft" based on "bounds" and "domain" for the generic,
1125 * non-degenerate, case.
1127 * "c_lower" and "c_upper" contain the lower and upper bounds
1128 * that the loop node should express.
1129 * "domain" is the subset of the intersection of the constraints
1130 * for which some code is executed.
1132 * There may be zero lower bounds or zero upper bounds in "constraints"
1133 * in case the list of constraints was created
1134 * based on the atomic option or based on separation with explicit bounds.
1135 * In that case, we use "domain" to derive lower and/or upper bounds.
1137 * We first compute a list of one or more lower bounds.
1139 * Then we decide if we want to express the condition as
1141 * iterator <= min(list of upper bounds)
1143 * or as a conjunction of constraints.
1145 * The set of enforced constraints is then computed either based on
1146 * a list of upper bounds or on a set of upper bound constraints.
1147 * We do not compute any enforced constraints if we were forced
1148 * to compute a lower or upper bound using exact_bound. The domains
1149 * of the resulting expressions may imply some bounds on outer dimensions
1150 * that we do not want to appear in the enforced constraints since
1151 * they are not actually enforced by the corresponding code.
1153 * Finally, we fill in the expressions of the for node.
1155 static __isl_give isl_ast_graft *refine_generic_bounds(
1156 __isl_take isl_ast_graft *graft,
1157 __isl_take isl_constraint_list *c_lower,
1158 __isl_take isl_constraint_list *c_upper,
1159 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1161 int depth;
1162 isl_ctx *ctx;
1163 isl_pw_aff_list *lower;
1164 int use_list;
1165 isl_set *upper_set = NULL;
1166 isl_pw_aff_list *upper_list = NULL;
1167 int n_lower, n_upper;
1169 if (!graft || !c_lower || !c_upper || !build)
1170 goto error;
1172 depth = isl_ast_build_get_depth(build);
1173 ctx = isl_ast_graft_get_ctx(graft);
1175 n_lower = isl_constraint_list_n_constraint(c_lower);
1176 n_upper = isl_constraint_list_n_constraint(c_upper);
1178 use_list = use_upper_bound_list(ctx, n_upper, domain, depth);
1180 lower = lower_bounds(c_lower, depth, domain, build);
1182 if (use_list)
1183 upper_list = upper_bounds(c_upper, depth, domain, build);
1184 else if (n_upper > 0)
1185 upper_set = intersect_constraints(c_upper);
1186 else
1187 upper_set = isl_set_universe(isl_set_get_space(domain));
1189 if (n_lower == 0 || n_upper == 0)
1191 else if (use_list)
1192 graft = set_enforced_from_list(graft, lower, upper_list);
1193 else
1194 graft = set_enforced_from_set(graft, lower, depth, upper_set);
1196 graft = set_for_node_expressions(graft, lower, use_list, upper_list,
1197 upper_set, build);
1199 isl_pw_aff_list_free(lower);
1200 isl_pw_aff_list_free(upper_list);
1201 isl_set_free(upper_set);
1202 isl_constraint_list_free(c_lower);
1203 isl_constraint_list_free(c_upper);
1205 return graft;
1206 error:
1207 isl_constraint_list_free(c_lower);
1208 isl_constraint_list_free(c_upper);
1209 return isl_ast_graft_free(graft);
1212 /* Internal data structure used inside count_constraints to keep
1213 * track of the number of constraints that are independent of dimension "pos",
1214 * the lower bounds in "pos" and the upper bounds in "pos".
1216 struct isl_ast_count_constraints_data {
1217 int pos;
1219 int n_indep;
1220 int n_lower;
1221 int n_upper;
1224 /* Increment data->n_indep, data->lower or data->upper depending
1225 * on whether "c" is independenct of dimensions data->pos,
1226 * a lower bound or an upper bound.
1228 static isl_stat count_constraints(__isl_take isl_constraint *c, void *user)
1230 struct isl_ast_count_constraints_data *data = user;
1232 if (isl_constraint_is_lower_bound(c, isl_dim_set, data->pos))
1233 data->n_lower++;
1234 else if (isl_constraint_is_upper_bound(c, isl_dim_set, data->pos))
1235 data->n_upper++;
1236 else
1237 data->n_indep++;
1239 isl_constraint_free(c);
1241 return isl_stat_ok;
1244 /* Update "graft" based on "bounds" and "domain" for the generic,
1245 * non-degenerate, case.
1247 * "list" respresent the list of bounds that need to be encoded by
1248 * the for loop. Only the constraints that involve the iterator
1249 * are relevant here. The other constraints are taken care of by
1250 * the caller and are included in the generated constraints of "build".
1251 * "domain" is the subset of the intersection of the constraints
1252 * for which some code is executed.
1253 * "build" is the build in which graft->node was created.
1255 * We separate lower bounds, upper bounds and constraints that
1256 * are independent of the loop iterator.
1258 * The actual for loop bounds are generated in refine_generic_bounds.
1260 static __isl_give isl_ast_graft *refine_generic_split(
1261 __isl_take isl_ast_graft *graft, __isl_take isl_constraint_list *list,
1262 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1264 struct isl_ast_count_constraints_data data;
1265 isl_constraint_list *lower;
1266 isl_constraint_list *upper;
1268 if (!list)
1269 return isl_ast_graft_free(graft);
1271 data.pos = isl_ast_build_get_depth(build);
1273 list = isl_constraint_list_sort(list, &cmp_constraint, &data.pos);
1274 if (!list)
1275 return isl_ast_graft_free(graft);
1277 data.n_indep = data.n_lower = data.n_upper = 0;
1278 if (isl_constraint_list_foreach(list, &count_constraints, &data) < 0) {
1279 isl_constraint_list_free(list);
1280 return isl_ast_graft_free(graft);
1283 lower = isl_constraint_list_drop(list, 0, data.n_indep);
1284 upper = isl_constraint_list_copy(lower);
1285 lower = isl_constraint_list_drop(lower, data.n_lower, data.n_upper);
1286 upper = isl_constraint_list_drop(upper, 0, data.n_lower);
1288 return refine_generic_bounds(graft, lower, upper, domain, build);
1291 /* Update "graft" based on "bounds" and "domain" for the generic,
1292 * non-degenerate, case.
1294 * "bounds" respresent the bounds that need to be encoded by
1295 * the for loop (or a guard around the for loop).
1296 * "domain" is the subset of "bounds" for which some code is executed.
1297 * "build" is the build in which graft->node was created.
1299 * We break up "bounds" into a list of constraints and continue with
1300 * refine_generic_split.
1302 static __isl_give isl_ast_graft *refine_generic(
1303 __isl_take isl_ast_graft *graft,
1304 __isl_keep isl_basic_set *bounds, __isl_keep isl_set *domain,
1305 __isl_keep isl_ast_build *build)
1307 isl_constraint_list *list;
1309 if (!build || !graft)
1310 return isl_ast_graft_free(graft);
1312 list = isl_basic_set_get_constraint_list(bounds);
1314 graft = refine_generic_split(graft, list, domain, build);
1316 return graft;
1319 /* Create a for node for the current level.
1321 * Mark the for node degenerate if "degenerate" is set.
1323 static __isl_give isl_ast_node *create_for(__isl_keep isl_ast_build *build,
1324 int degenerate)
1326 int depth;
1327 isl_id *id;
1328 isl_ast_node *node;
1330 if (!build)
1331 return NULL;
1333 depth = isl_ast_build_get_depth(build);
1334 id = isl_ast_build_get_iterator_id(build, depth);
1335 node = isl_ast_node_alloc_for(id);
1336 if (degenerate)
1337 node = isl_ast_node_for_mark_degenerate(node);
1339 return node;
1342 /* If the ast_build_exploit_nested_bounds option is set, then return
1343 * the constraints enforced by all elements in "list".
1344 * Otherwise, return the universe.
1346 static __isl_give isl_basic_set *extract_shared_enforced(
1347 __isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
1349 isl_ctx *ctx;
1350 isl_space *space;
1352 if (!list)
1353 return NULL;
1355 ctx = isl_ast_graft_list_get_ctx(list);
1356 if (isl_options_get_ast_build_exploit_nested_bounds(ctx))
1357 return isl_ast_graft_list_extract_shared_enforced(list, build);
1359 space = isl_ast_build_get_space(build, 1);
1360 return isl_basic_set_universe(space);
1363 /* Return the pending constraints of "build" that are not already taken
1364 * care of (by a combination of "enforced" and the generated constraints
1365 * of "build").
1367 static __isl_give isl_set *extract_pending(__isl_keep isl_ast_build *build,
1368 __isl_keep isl_basic_set *enforced)
1370 isl_set *guard, *context;
1372 guard = isl_ast_build_get_pending(build);
1373 context = isl_set_from_basic_set(isl_basic_set_copy(enforced));
1374 context = isl_set_intersect(context,
1375 isl_ast_build_get_generated(build));
1376 return isl_set_gist(guard, context);
1379 /* Create an AST node for the current dimension based on
1380 * the schedule domain "bounds" and return the node encapsulated
1381 * in an isl_ast_graft.
1383 * "executed" is the current inverse schedule, taking into account
1384 * the bounds in "bounds"
1385 * "domain" is the domain of "executed", with inner dimensions projected out.
1386 * It may be a strict subset of "bounds" in case "bounds" was created
1387 * based on the atomic option or based on separation with explicit bounds.
1389 * "domain" may satisfy additional equalities that result
1390 * from intersecting "executed" with "bounds" in add_node.
1391 * It may also satisfy some global constraints that were dropped out because
1392 * we performed separation with explicit bounds.
1393 * The very first step is then to copy these constraints to "bounds".
1395 * Since we may be calling before_each_for and after_each_for
1396 * callbacks, we record the current inverse schedule in the build.
1398 * We consider three builds,
1399 * "build" is the one in which the current level is created,
1400 * "body_build" is the build in which the next level is created,
1401 * "sub_build" is essentially the same as "body_build", except that
1402 * the depth has not been increased yet.
1404 * "build" already contains information (in strides and offsets)
1405 * about the strides at the current level, but this information is not
1406 * reflected in the build->domain.
1407 * We first add this information and the "bounds" to the sub_build->domain.
1408 * isl_ast_build_set_loop_bounds adds the stride information and
1409 * checks whether the current dimension attains
1410 * only a single value and whether this single value can be represented using
1411 * a single affine expression.
1412 * In the first case, the current level is considered "degenerate".
1413 * In the second, sub-case, the current level is considered "eliminated".
1414 * Eliminated levels don't need to be reflected in the AST since we can
1415 * simply plug in the affine expression. For degenerate, but non-eliminated,
1416 * levels, we do introduce a for node, but mark is as degenerate so that
1417 * it can be printed as an assignment of the single value to the loop
1418 * "iterator".
1420 * If the current level is eliminated, we explicitly plug in the value
1421 * for the current level found by isl_ast_build_set_loop_bounds in the
1422 * inverse schedule. This ensures that if we are working on a slice
1423 * of the domain based on information available in the inverse schedule
1424 * and the build domain, that then this information is also reflected
1425 * in the inverse schedule. This operation also eliminates the current
1426 * dimension from the inverse schedule making sure no inner dimensions depend
1427 * on the current dimension. Otherwise, we create a for node, marking
1428 * it degenerate if appropriate. The initial for node is still incomplete
1429 * and will be completed in either refine_degenerate or refine_generic.
1431 * We then generate a sequence of grafts for the next level,
1432 * create a surrounding graft for the current level and insert
1433 * the for node we created (if the current level is not eliminated).
1434 * Before creating a graft for the current level, we first extract
1435 * hoistable constraints from the child guards and combine them
1436 * with the pending constraints in the build. These constraints
1437 * are used to simplify the child guards and then added to the guard
1438 * of the current graft to ensure that they will be generated.
1439 * If the hoisted guard is a disjunction, then we use it directly
1440 * to gist the guards on the children before intersect it with the
1441 * pending constraints. We do so because this disjunction is typically
1442 * identical to the guards on the children such that these guards
1443 * can be effectively removed completely. After the intersection,
1444 * the gist operation would have a harder time figuring this out.
1446 * Finally, we set the bounds of the for loop in either
1447 * refine_degenerate or refine_generic.
1448 * We do so in a context where the pending constraints of the build
1449 * have been replaced by the guard of the current graft.
1451 static __isl_give isl_ast_graft *create_node_scaled(
1452 __isl_take isl_union_map *executed,
1453 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1454 __isl_take isl_ast_build *build)
1456 int depth;
1457 int degenerate, eliminated;
1458 isl_basic_set *hull;
1459 isl_basic_set *enforced;
1460 isl_set *guard, *hoisted;
1461 isl_ast_node *node = NULL;
1462 isl_ast_graft *graft;
1463 isl_ast_graft_list *children;
1464 isl_ast_build *sub_build;
1465 isl_ast_build *body_build;
1467 domain = isl_ast_build_eliminate_divs(build, domain);
1468 domain = isl_set_detect_equalities(domain);
1469 hull = isl_set_unshifted_simple_hull(isl_set_copy(domain));
1470 bounds = isl_basic_set_intersect(bounds, hull);
1471 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
1473 depth = isl_ast_build_get_depth(build);
1474 sub_build = isl_ast_build_copy(build);
1475 bounds = isl_basic_set_remove_redundancies(bounds);
1476 bounds = isl_ast_build_specialize_basic_set(sub_build, bounds);
1477 sub_build = isl_ast_build_set_loop_bounds(sub_build,
1478 isl_basic_set_copy(bounds));
1479 degenerate = isl_ast_build_has_value(sub_build);
1480 eliminated = isl_ast_build_has_affine_value(sub_build, depth);
1481 if (degenerate < 0 || eliminated < 0)
1482 executed = isl_union_map_free(executed);
1483 if (!degenerate)
1484 bounds = isl_ast_build_compute_gist_basic_set(build, bounds);
1485 sub_build = isl_ast_build_set_pending_generated(sub_build,
1486 isl_basic_set_copy(bounds));
1487 if (eliminated)
1488 executed = plug_in_values(executed, sub_build);
1489 else
1490 node = create_for(build, degenerate);
1492 body_build = isl_ast_build_copy(sub_build);
1493 body_build = isl_ast_build_increase_depth(body_build);
1494 if (!eliminated)
1495 node = before_each_for(node, body_build);
1496 children = generate_next_level(executed,
1497 isl_ast_build_copy(body_build));
1499 enforced = extract_shared_enforced(children, build);
1500 guard = extract_pending(sub_build, enforced);
1501 hoisted = isl_ast_graft_list_extract_hoistable_guard(children, build);
1502 if (isl_set_n_basic_set(hoisted) > 1)
1503 children = isl_ast_graft_list_gist_guards(children,
1504 isl_set_copy(hoisted));
1505 guard = isl_set_intersect(guard, hoisted);
1506 if (!eliminated)
1507 guard = add_implied_guards(guard, degenerate, bounds, build);
1509 graft = isl_ast_graft_alloc_from_children(children,
1510 isl_set_copy(guard), enforced, build, sub_build);
1512 if (!eliminated) {
1513 isl_ast_build *for_build;
1515 graft = isl_ast_graft_insert_for(graft, node);
1516 for_build = isl_ast_build_copy(build);
1517 for_build = isl_ast_build_replace_pending_by_guard(for_build,
1518 isl_set_copy(guard));
1519 if (degenerate)
1520 graft = refine_degenerate(graft, for_build, sub_build);
1521 else
1522 graft = refine_generic(graft, bounds,
1523 domain, for_build);
1524 isl_ast_build_free(for_build);
1526 isl_set_free(guard);
1527 if (!eliminated)
1528 graft = after_each_for(graft, body_build);
1530 isl_ast_build_free(body_build);
1531 isl_ast_build_free(sub_build);
1532 isl_ast_build_free(build);
1533 isl_basic_set_free(bounds);
1534 isl_set_free(domain);
1536 return graft;
1539 /* Internal data structure for checking if all constraints involving
1540 * the input dimension "depth" are such that the other coefficients
1541 * are multiples of "m", reducing "m" if they are not.
1542 * If "m" is reduced all the way down to "1", then the check has failed
1543 * and we break out of the iteration.
1545 struct isl_check_scaled_data {
1546 int depth;
1547 isl_val *m;
1550 /* If constraint "c" involves the input dimension data->depth,
1551 * then make sure that all the other coefficients are multiples of data->m,
1552 * reducing data->m if needed.
1553 * Break out of the iteration if data->m has become equal to "1".
1555 static isl_stat constraint_check_scaled(__isl_take isl_constraint *c,
1556 void *user)
1558 struct isl_check_scaled_data *data = user;
1559 int i, j, n;
1560 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_out,
1561 isl_dim_div };
1563 if (!isl_constraint_involves_dims(c, isl_dim_in, data->depth, 1)) {
1564 isl_constraint_free(c);
1565 return isl_stat_ok;
1568 for (i = 0; i < 4; ++i) {
1569 n = isl_constraint_dim(c, t[i]);
1570 for (j = 0; j < n; ++j) {
1571 isl_val *d;
1573 if (t[i] == isl_dim_in && j == data->depth)
1574 continue;
1575 if (!isl_constraint_involves_dims(c, t[i], j, 1))
1576 continue;
1577 d = isl_constraint_get_coefficient_val(c, t[i], j);
1578 data->m = isl_val_gcd(data->m, d);
1579 if (isl_val_is_one(data->m))
1580 break;
1582 if (j < n)
1583 break;
1586 isl_constraint_free(c);
1588 return i < 4 ? isl_stat_error : isl_stat_ok;
1591 /* For each constraint of "bmap" that involves the input dimension data->depth,
1592 * make sure that all the other coefficients are multiples of data->m,
1593 * reducing data->m if needed.
1594 * Break out of the iteration if data->m has become equal to "1".
1596 static isl_stat basic_map_check_scaled(__isl_take isl_basic_map *bmap,
1597 void *user)
1599 isl_stat r;
1601 r = isl_basic_map_foreach_constraint(bmap,
1602 &constraint_check_scaled, user);
1603 isl_basic_map_free(bmap);
1605 return r;
1608 /* For each constraint of "map" that involves the input dimension data->depth,
1609 * make sure that all the other coefficients are multiples of data->m,
1610 * reducing data->m if needed.
1611 * Break out of the iteration if data->m has become equal to "1".
1613 static isl_stat map_check_scaled(__isl_take isl_map *map, void *user)
1615 isl_stat r;
1617 r = isl_map_foreach_basic_map(map, &basic_map_check_scaled, user);
1618 isl_map_free(map);
1620 return r;
1623 /* Create an AST node for the current dimension based on
1624 * the schedule domain "bounds" and return the node encapsulated
1625 * in an isl_ast_graft.
1627 * "executed" is the current inverse schedule, taking into account
1628 * the bounds in "bounds"
1629 * "domain" is the domain of "executed", with inner dimensions projected out.
1632 * Before moving on to the actual AST node construction in create_node_scaled,
1633 * we first check if the current dimension is strided and if we can scale
1634 * down this stride. Note that we only do this if the ast_build_scale_strides
1635 * option is set.
1637 * In particular, let the current dimension take on values
1639 * f + s a
1641 * with a an integer. We check if we can find an integer m that (obviously)
1642 * divides both f and s.
1644 * If so, we check if the current dimension only appears in constraints
1645 * where the coefficients of the other variables are multiples of m.
1646 * We perform this extra check to avoid the risk of introducing
1647 * divisions by scaling down the current dimension.
1649 * If so, we scale the current dimension down by a factor of m.
1650 * That is, we plug in
1652 * i = m i' (1)
1654 * Note that in principle we could always scale down strided loops
1655 * by plugging in
1657 * i = f + s i'
1659 * but this may result in i' taking on larger values than the original i,
1660 * due to the shift by "f".
1661 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1663 static __isl_give isl_ast_graft *create_node(__isl_take isl_union_map *executed,
1664 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1665 __isl_take isl_ast_build *build)
1667 struct isl_check_scaled_data data;
1668 isl_ctx *ctx;
1669 isl_aff *offset;
1670 isl_val *d;
1672 ctx = isl_ast_build_get_ctx(build);
1673 if (!isl_options_get_ast_build_scale_strides(ctx))
1674 return create_node_scaled(executed, bounds, domain, build);
1676 data.depth = isl_ast_build_get_depth(build);
1677 if (!isl_ast_build_has_stride(build, data.depth))
1678 return create_node_scaled(executed, bounds, domain, build);
1680 offset = isl_ast_build_get_offset(build, data.depth);
1681 data.m = isl_ast_build_get_stride(build, data.depth);
1682 if (!data.m)
1683 offset = isl_aff_free(offset);
1684 offset = isl_aff_scale_down_val(offset, isl_val_copy(data.m));
1685 d = isl_aff_get_denominator_val(offset);
1686 if (!d)
1687 executed = isl_union_map_free(executed);
1689 if (executed && isl_val_is_divisible_by(data.m, d))
1690 data.m = isl_val_div(data.m, d);
1691 else {
1692 data.m = isl_val_set_si(data.m, 1);
1693 isl_val_free(d);
1696 if (!isl_val_is_one(data.m)) {
1697 if (isl_union_map_foreach_map(executed, &map_check_scaled,
1698 &data) < 0 &&
1699 !isl_val_is_one(data.m))
1700 executed = isl_union_map_free(executed);
1703 if (!isl_val_is_one(data.m)) {
1704 isl_space *space;
1705 isl_multi_aff *ma;
1706 isl_aff *aff;
1707 isl_map *map;
1708 isl_union_map *umap;
1710 space = isl_ast_build_get_space(build, 1);
1711 space = isl_space_map_from_set(space);
1712 ma = isl_multi_aff_identity(space);
1713 aff = isl_multi_aff_get_aff(ma, data.depth);
1714 aff = isl_aff_scale_val(aff, isl_val_copy(data.m));
1715 ma = isl_multi_aff_set_aff(ma, data.depth, aff);
1717 bounds = isl_basic_set_preimage_multi_aff(bounds,
1718 isl_multi_aff_copy(ma));
1719 domain = isl_set_preimage_multi_aff(domain,
1720 isl_multi_aff_copy(ma));
1721 map = isl_map_reverse(isl_map_from_multi_aff(ma));
1722 umap = isl_union_map_from_map(map);
1723 executed = isl_union_map_apply_domain(executed,
1724 isl_union_map_copy(umap));
1725 build = isl_ast_build_scale_down(build, isl_val_copy(data.m),
1726 umap);
1728 isl_aff_free(offset);
1729 isl_val_free(data.m);
1731 return create_node_scaled(executed, bounds, domain, build);
1734 /* Add the basic set to the list that "user" points to.
1736 static isl_stat collect_basic_set(__isl_take isl_basic_set *bset, void *user)
1738 isl_basic_set_list **list = user;
1740 *list = isl_basic_set_list_add(*list, bset);
1742 return isl_stat_ok;
1745 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1747 static __isl_give isl_basic_set_list *isl_basic_set_list_from_set(
1748 __isl_take isl_set *set)
1750 int n;
1751 isl_ctx *ctx;
1752 isl_basic_set_list *list;
1754 if (!set)
1755 return NULL;
1757 ctx = isl_set_get_ctx(set);
1759 n = isl_set_n_basic_set(set);
1760 list = isl_basic_set_list_alloc(ctx, n);
1761 if (isl_set_foreach_basic_set(set, &collect_basic_set, &list) < 0)
1762 list = isl_basic_set_list_free(list);
1764 isl_set_free(set);
1765 return list;
1768 /* Generate code for the schedule domain "bounds"
1769 * and add the result to "list".
1771 * We mainly detect strides here and check if the bounds do not
1772 * conflict with the current build domain
1773 * and then pass over control to create_node.
1775 * "bounds" reflects the bounds on the current dimension and possibly
1776 * some extra conditions on outer dimensions.
1777 * It does not, however, include any divs involving the current dimension,
1778 * so it does not capture any stride constraints.
1779 * We therefore need to compute that part of the schedule domain that
1780 * intersects with "bounds" and derive the strides from the result.
1782 static __isl_give isl_ast_graft_list *add_node(
1783 __isl_take isl_ast_graft_list *list, __isl_take isl_union_map *executed,
1784 __isl_take isl_basic_set *bounds, __isl_take isl_ast_build *build)
1786 isl_ast_graft *graft;
1787 isl_set *domain = NULL;
1788 isl_union_set *uset;
1789 int empty, disjoint;
1791 uset = isl_union_set_from_basic_set(isl_basic_set_copy(bounds));
1792 executed = isl_union_map_intersect_domain(executed, uset);
1793 empty = isl_union_map_is_empty(executed);
1794 if (empty < 0)
1795 goto error;
1796 if (empty)
1797 goto done;
1799 uset = isl_union_map_domain(isl_union_map_copy(executed));
1800 domain = isl_set_from_union_set(uset);
1801 domain = isl_ast_build_specialize(build, domain);
1803 domain = isl_set_compute_divs(domain);
1804 domain = isl_ast_build_eliminate_inner(build, domain);
1805 disjoint = isl_set_is_disjoint(domain, build->domain);
1806 if (disjoint < 0)
1807 goto error;
1808 if (disjoint)
1809 goto done;
1811 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
1813 graft = create_node(executed, bounds, domain,
1814 isl_ast_build_copy(build));
1815 list = isl_ast_graft_list_add(list, graft);
1816 isl_ast_build_free(build);
1817 return list;
1818 error:
1819 list = isl_ast_graft_list_free(list);
1820 done:
1821 isl_set_free(domain);
1822 isl_basic_set_free(bounds);
1823 isl_union_map_free(executed);
1824 isl_ast_build_free(build);
1825 return list;
1828 /* Does any element of i follow or coincide with any element of j
1829 * at the current depth for equal values of the outer dimensions?
1831 static isl_bool domain_follows_at_depth(__isl_keep isl_basic_set *i,
1832 __isl_keep isl_basic_set *j, void *user)
1834 int depth = *(int *) user;
1835 isl_basic_map *test;
1836 isl_bool empty;
1837 int l;
1839 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1840 isl_basic_set_copy(j));
1841 for (l = 0; l < depth; ++l)
1842 test = isl_basic_map_equate(test, isl_dim_in, l,
1843 isl_dim_out, l);
1844 test = isl_basic_map_order_ge(test, isl_dim_in, depth,
1845 isl_dim_out, depth);
1846 empty = isl_basic_map_is_empty(test);
1847 isl_basic_map_free(test);
1849 return empty < 0 ? isl_bool_error : !empty;
1852 /* Split up each element of "list" into a part that is related to "bset"
1853 * according to "gt" and a part that is not.
1854 * Return a list that consist of "bset" and all the pieces.
1856 static __isl_give isl_basic_set_list *add_split_on(
1857 __isl_take isl_basic_set_list *list, __isl_take isl_basic_set *bset,
1858 __isl_keep isl_basic_map *gt)
1860 int i, n;
1861 isl_basic_set_list *res;
1863 if (!list)
1864 bset = isl_basic_set_free(bset);
1866 gt = isl_basic_map_copy(gt);
1867 gt = isl_basic_map_intersect_domain(gt, isl_basic_set_copy(bset));
1868 n = isl_basic_set_list_n_basic_set(list);
1869 res = isl_basic_set_list_from_basic_set(bset);
1870 for (i = 0; res && i < n; ++i) {
1871 isl_basic_set *bset;
1872 isl_set *set1, *set2;
1873 isl_basic_map *bmap;
1874 int empty;
1876 bset = isl_basic_set_list_get_basic_set(list, i);
1877 bmap = isl_basic_map_copy(gt);
1878 bmap = isl_basic_map_intersect_range(bmap, bset);
1879 bset = isl_basic_map_range(bmap);
1880 empty = isl_basic_set_is_empty(bset);
1881 if (empty < 0)
1882 res = isl_basic_set_list_free(res);
1883 if (empty) {
1884 isl_basic_set_free(bset);
1885 bset = isl_basic_set_list_get_basic_set(list, i);
1886 res = isl_basic_set_list_add(res, bset);
1887 continue;
1890 res = isl_basic_set_list_add(res, isl_basic_set_copy(bset));
1891 set1 = isl_set_from_basic_set(bset);
1892 bset = isl_basic_set_list_get_basic_set(list, i);
1893 set2 = isl_set_from_basic_set(bset);
1894 set1 = isl_set_subtract(set2, set1);
1895 set1 = isl_set_make_disjoint(set1);
1897 res = isl_basic_set_list_concat(res,
1898 isl_basic_set_list_from_set(set1));
1900 isl_basic_map_free(gt);
1901 isl_basic_set_list_free(list);
1902 return res;
1905 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1906 __isl_keep isl_basic_set_list *domain_list,
1907 __isl_keep isl_union_map *executed,
1908 __isl_keep isl_ast_build *build);
1910 /* Internal data structure for add_nodes.
1912 * "executed" and "build" are extra arguments to be passed to add_node.
1913 * "list" collects the results.
1915 struct isl_add_nodes_data {
1916 isl_union_map *executed;
1917 isl_ast_build *build;
1919 isl_ast_graft_list *list;
1922 /* Generate code for the schedule domains in "scc"
1923 * and add the results to "list".
1925 * The domains in "scc" form a strongly connected component in the ordering.
1926 * If the number of domains in "scc" is larger than 1, then this means
1927 * that we cannot determine a valid ordering for the domains in the component.
1928 * This should be fairly rare because the individual domains
1929 * have been made disjoint first.
1930 * The problem is that the domains may be integrally disjoint but not
1931 * rationally disjoint. For example, we may have domains
1933 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1935 * These two domains have an empty intersection, but their rational
1936 * relaxations do intersect. It is impossible to order these domains
1937 * in the second dimension because the first should be ordered before
1938 * the second for outer dimension equal to 0, while it should be ordered
1939 * after for outer dimension equal to 1.
1941 * This may happen in particular in case of unrolling since the domain
1942 * of each slice is replaced by its simple hull.
1944 * For each basic set i in "scc" and for each of the following basic sets j,
1945 * we split off that part of the basic set i that shares the outer dimensions
1946 * with j and lies before j in the current dimension.
1947 * We collect all the pieces in a new list that replaces "scc".
1949 * While the elements in "scc" should be disjoint, we double-check
1950 * this property to avoid running into an infinite recursion in case
1951 * they intersect due to some internal error.
1953 static isl_stat add_nodes(__isl_take isl_basic_set_list *scc, void *user)
1955 struct isl_add_nodes_data *data = user;
1956 int i, n, depth;
1957 isl_basic_set *bset, *first;
1958 isl_basic_set_list *list;
1959 isl_space *space;
1960 isl_basic_map *gt;
1962 n = isl_basic_set_list_n_basic_set(scc);
1963 bset = isl_basic_set_list_get_basic_set(scc, 0);
1964 if (n == 1) {
1965 isl_basic_set_list_free(scc);
1966 data->list = add_node(data->list,
1967 isl_union_map_copy(data->executed), bset,
1968 isl_ast_build_copy(data->build));
1969 return data->list ? isl_stat_ok : isl_stat_error;
1972 depth = isl_ast_build_get_depth(data->build);
1973 space = isl_basic_set_get_space(bset);
1974 space = isl_space_map_from_set(space);
1975 gt = isl_basic_map_universe(space);
1976 for (i = 0; i < depth; ++i)
1977 gt = isl_basic_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
1978 gt = isl_basic_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
1980 first = isl_basic_set_copy(bset);
1981 list = isl_basic_set_list_from_basic_set(bset);
1982 for (i = 1; i < n; ++i) {
1983 int disjoint;
1985 bset = isl_basic_set_list_get_basic_set(scc, i);
1987 disjoint = isl_basic_set_is_disjoint(bset, first);
1988 if (disjoint < 0)
1989 list = isl_basic_set_list_free(list);
1990 else if (!disjoint)
1991 isl_die(isl_basic_set_list_get_ctx(scc),
1992 isl_error_internal,
1993 "basic sets in scc are assumed to be disjoint",
1994 list = isl_basic_set_list_free(list));
1996 list = add_split_on(list, bset, gt);
1998 isl_basic_set_free(first);
1999 isl_basic_map_free(gt);
2000 isl_basic_set_list_free(scc);
2001 scc = list;
2002 data->list = isl_ast_graft_list_concat(data->list,
2003 generate_sorted_domains(scc, data->executed, data->build));
2004 isl_basic_set_list_free(scc);
2006 return data->list ? isl_stat_ok : isl_stat_error;
2009 /* Sort the domains in "domain_list" according to the execution order
2010 * at the current depth (for equal values of the outer dimensions),
2011 * generate code for each of them, collecting the results in a list.
2012 * If no code is generated (because the intersection of the inverse schedule
2013 * with the domains turns out to be empty), then an empty list is returned.
2015 * The caller is responsible for ensuring that the basic sets in "domain_list"
2016 * are pair-wise disjoint. It can, however, in principle happen that
2017 * two basic sets should be ordered one way for one value of the outer
2018 * dimensions and the other way for some other value of the outer dimensions.
2019 * We therefore play safe and look for strongly connected components.
2020 * The function add_nodes takes care of handling non-trivial components.
2022 static __isl_give isl_ast_graft_list *generate_sorted_domains(
2023 __isl_keep isl_basic_set_list *domain_list,
2024 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2026 isl_ctx *ctx;
2027 struct isl_add_nodes_data data;
2028 int depth;
2029 int n;
2031 if (!domain_list)
2032 return NULL;
2034 ctx = isl_basic_set_list_get_ctx(domain_list);
2035 n = isl_basic_set_list_n_basic_set(domain_list);
2036 data.list = isl_ast_graft_list_alloc(ctx, n);
2037 if (n == 0)
2038 return data.list;
2039 if (n == 1)
2040 return add_node(data.list, isl_union_map_copy(executed),
2041 isl_basic_set_list_get_basic_set(domain_list, 0),
2042 isl_ast_build_copy(build));
2044 depth = isl_ast_build_get_depth(build);
2045 data.executed = executed;
2046 data.build = build;
2047 if (isl_basic_set_list_foreach_scc(domain_list,
2048 &domain_follows_at_depth, &depth,
2049 &add_nodes, &data) < 0)
2050 data.list = isl_ast_graft_list_free(data.list);
2052 return data.list;
2055 /* Do i and j share any values for the outer dimensions?
2057 static isl_bool shared_outer(__isl_keep isl_basic_set *i,
2058 __isl_keep isl_basic_set *j, void *user)
2060 int depth = *(int *) user;
2061 isl_basic_map *test;
2062 isl_bool empty;
2063 int l;
2065 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
2066 isl_basic_set_copy(j));
2067 for (l = 0; l < depth; ++l)
2068 test = isl_basic_map_equate(test, isl_dim_in, l,
2069 isl_dim_out, l);
2070 empty = isl_basic_map_is_empty(test);
2071 isl_basic_map_free(test);
2073 return empty < 0 ? isl_bool_error : !empty;
2076 /* Internal data structure for generate_sorted_domains_wrap.
2078 * "n" is the total number of basic sets
2079 * "executed" and "build" are extra arguments to be passed
2080 * to generate_sorted_domains.
2082 * "single" is set to 1 by generate_sorted_domains_wrap if there
2083 * is only a single component.
2084 * "list" collects the results.
2086 struct isl_ast_generate_parallel_domains_data {
2087 int n;
2088 isl_union_map *executed;
2089 isl_ast_build *build;
2091 int single;
2092 isl_ast_graft_list *list;
2095 /* Call generate_sorted_domains on "scc", fuse the result into a list
2096 * with either zero or one graft and collect the these single element
2097 * lists into data->list.
2099 * If there is only one component, i.e., if the number of basic sets
2100 * in the current component is equal to the total number of basic sets,
2101 * then data->single is set to 1 and the result of generate_sorted_domains
2102 * is not fused.
2104 static isl_stat generate_sorted_domains_wrap(__isl_take isl_basic_set_list *scc,
2105 void *user)
2107 struct isl_ast_generate_parallel_domains_data *data = user;
2108 isl_ast_graft_list *list;
2110 list = generate_sorted_domains(scc, data->executed, data->build);
2111 data->single = isl_basic_set_list_n_basic_set(scc) == data->n;
2112 if (!data->single)
2113 list = isl_ast_graft_list_fuse(list, data->build);
2114 if (!data->list)
2115 data->list = list;
2116 else
2117 data->list = isl_ast_graft_list_concat(data->list, list);
2119 isl_basic_set_list_free(scc);
2120 if (!data->list)
2121 return isl_stat_error;
2123 return isl_stat_ok;
2126 /* Look for any (weakly connected) components in the "domain_list"
2127 * of domains that share some values of the outer dimensions.
2128 * That is, domains in different components do not share any values
2129 * of the outer dimensions. This means that these components
2130 * can be freely reordered.
2131 * Within each of the components, we sort the domains according
2132 * to the execution order at the current depth.
2134 * If there is more than one component, then generate_sorted_domains_wrap
2135 * fuses the result of each call to generate_sorted_domains
2136 * into a list with either zero or one graft and collects these (at most)
2137 * single element lists into a bigger list. This means that the elements of the
2138 * final list can be freely reordered. In particular, we sort them
2139 * according to an arbitrary but fixed ordering to ease merging of
2140 * graft lists from different components.
2142 static __isl_give isl_ast_graft_list *generate_parallel_domains(
2143 __isl_keep isl_basic_set_list *domain_list,
2144 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2146 int depth;
2147 struct isl_ast_generate_parallel_domains_data data;
2149 if (!domain_list)
2150 return NULL;
2152 data.n = isl_basic_set_list_n_basic_set(domain_list);
2153 if (data.n <= 1)
2154 return generate_sorted_domains(domain_list, executed, build);
2156 depth = isl_ast_build_get_depth(build);
2157 data.list = NULL;
2158 data.executed = executed;
2159 data.build = build;
2160 data.single = 0;
2161 if (isl_basic_set_list_foreach_scc(domain_list, &shared_outer, &depth,
2162 &generate_sorted_domains_wrap,
2163 &data) < 0)
2164 data.list = isl_ast_graft_list_free(data.list);
2166 if (!data.single)
2167 data.list = isl_ast_graft_list_sort_guard(data.list);
2169 return data.list;
2172 /* Internal data for separate_domain.
2174 * "explicit" is set if we only want to use explicit bounds.
2176 * "domain" collects the separated domains.
2178 struct isl_separate_domain_data {
2179 isl_ast_build *build;
2180 int explicit;
2181 isl_set *domain;
2184 /* Extract implicit bounds on the current dimension for the executed "map".
2186 * The domain of "map" may involve inner dimensions, so we
2187 * need to eliminate them.
2189 static __isl_give isl_set *implicit_bounds(__isl_take isl_map *map,
2190 __isl_keep isl_ast_build *build)
2192 isl_set *domain;
2194 domain = isl_map_domain(map);
2195 domain = isl_ast_build_eliminate(build, domain);
2197 return domain;
2200 /* Extract explicit bounds on the current dimension for the executed "map".
2202 * Rather than eliminating the inner dimensions as in implicit_bounds,
2203 * we simply drop any constraints involving those inner dimensions.
2204 * The idea is that most bounds that are implied by constraints on the
2205 * inner dimensions will be enforced by for loops and not by explicit guards.
2206 * There is then no need to separate along those bounds.
2208 static __isl_give isl_set *explicit_bounds(__isl_take isl_map *map,
2209 __isl_keep isl_ast_build *build)
2211 isl_set *domain;
2212 int depth, dim;
2214 dim = isl_map_dim(map, isl_dim_out);
2215 map = isl_map_drop_constraints_involving_dims(map, isl_dim_out, 0, dim);
2217 domain = isl_map_domain(map);
2218 depth = isl_ast_build_get_depth(build);
2219 dim = isl_set_dim(domain, isl_dim_set);
2220 domain = isl_set_detect_equalities(domain);
2221 domain = isl_set_drop_constraints_involving_dims(domain,
2222 isl_dim_set, depth + 1, dim - (depth + 1));
2223 domain = isl_set_remove_divs_involving_dims(domain,
2224 isl_dim_set, depth, 1);
2225 domain = isl_set_remove_unknown_divs(domain);
2227 return domain;
2230 /* Split data->domain into pieces that intersect with the range of "map"
2231 * and pieces that do not intersect with the range of "map"
2232 * and then add that part of the range of "map" that does not intersect
2233 * with data->domain.
2235 static isl_stat separate_domain(__isl_take isl_map *map, void *user)
2237 struct isl_separate_domain_data *data = user;
2238 isl_set *domain;
2239 isl_set *d1, *d2;
2241 if (data->explicit)
2242 domain = explicit_bounds(map, data->build);
2243 else
2244 domain = implicit_bounds(map, data->build);
2246 domain = isl_set_coalesce(domain);
2247 domain = isl_set_make_disjoint(domain);
2248 d1 = isl_set_subtract(isl_set_copy(domain), isl_set_copy(data->domain));
2249 d2 = isl_set_subtract(isl_set_copy(data->domain), isl_set_copy(domain));
2250 data->domain = isl_set_intersect(data->domain, domain);
2251 data->domain = isl_set_union(data->domain, d1);
2252 data->domain = isl_set_union(data->domain, d2);
2254 return isl_stat_ok;
2257 /* Separate the schedule domains of "executed".
2259 * That is, break up the domain of "executed" into basic sets,
2260 * such that for each basic set S, every element in S is associated with
2261 * the same domain spaces.
2263 * "space" is the (single) domain space of "executed".
2265 static __isl_give isl_set *separate_schedule_domains(
2266 __isl_take isl_space *space, __isl_take isl_union_map *executed,
2267 __isl_keep isl_ast_build *build)
2269 struct isl_separate_domain_data data = { build };
2270 isl_ctx *ctx;
2272 ctx = isl_ast_build_get_ctx(build);
2273 data.explicit = isl_options_get_ast_build_separation_bounds(ctx) ==
2274 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT;
2275 data.domain = isl_set_empty(space);
2276 if (isl_union_map_foreach_map(executed, &separate_domain, &data) < 0)
2277 data.domain = isl_set_free(data.domain);
2279 isl_union_map_free(executed);
2280 return data.domain;
2283 /* Temporary data used during the search for a lower bound for unrolling.
2285 * "build" is the build in which the unrolling will be performed
2286 * "domain" is the original set for which to find a lower bound
2287 * "depth" is the dimension for which to find a lower boudn
2288 * "expansion" is the expansion that needs to be applied to "domain"
2289 * in the unrolling that will be performed
2291 * "lower" is the best lower bound found so far. It is NULL if we have not
2292 * found any yet.
2293 * "n" is the corresponding size. If lower is NULL, then the value of n
2294 * is undefined.
2295 * "n_div" is the maximal number of integer divisions in the first
2296 * unrolled iteration (after expansion). It is set to -1 if it hasn't
2297 * been computed yet.
2299 struct isl_find_unroll_data {
2300 isl_ast_build *build;
2301 isl_set *domain;
2302 int depth;
2303 isl_basic_map *expansion;
2305 isl_aff *lower;
2306 int *n;
2307 int n_div;
2310 /* Return the constraint
2312 * i_"depth" = aff + offset
2314 static __isl_give isl_constraint *at_offset(int depth, __isl_keep isl_aff *aff,
2315 int offset)
2317 aff = isl_aff_copy(aff);
2318 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, depth, -1);
2319 aff = isl_aff_add_constant_si(aff, offset);
2320 return isl_equality_from_aff(aff);
2323 /* Update *user to the number of integer divsions in the first element
2324 * of "ma", if it is larger than the current value.
2326 static isl_stat update_n_div(__isl_take isl_set *set,
2327 __isl_take isl_multi_aff *ma, void *user)
2329 isl_aff *aff;
2330 int *n = user;
2331 int n_div;
2333 aff = isl_multi_aff_get_aff(ma, 0);
2334 n_div = isl_aff_dim(aff, isl_dim_div);
2335 isl_aff_free(aff);
2336 isl_multi_aff_free(ma);
2337 isl_set_free(set);
2339 if (n_div > *n)
2340 *n = n_div;
2342 return aff ? isl_stat_ok : isl_stat_error;
2345 /* Get the number of integer divisions in the expression for the iterator
2346 * value at the first slice in the unrolling based on lower bound "lower",
2347 * taking into account the expansion that needs to be performed on this slice.
2349 static int get_expanded_n_div(struct isl_find_unroll_data *data,
2350 __isl_keep isl_aff *lower)
2352 isl_constraint *c;
2353 isl_set *set;
2354 isl_map *it_map, *expansion;
2355 isl_pw_multi_aff *pma;
2356 int n;
2358 c = at_offset(data->depth, lower, 0);
2359 set = isl_set_copy(data->domain);
2360 set = isl_set_add_constraint(set, c);
2361 expansion = isl_map_from_basic_map(isl_basic_map_copy(data->expansion));
2362 set = isl_set_apply(set, expansion);
2363 it_map = isl_ast_build_map_to_iterator(data->build, set);
2364 pma = isl_pw_multi_aff_from_map(it_map);
2365 n = 0;
2366 if (isl_pw_multi_aff_foreach_piece(pma, &update_n_div, &n) < 0)
2367 n = -1;
2368 isl_pw_multi_aff_free(pma);
2370 return n;
2373 /* Is the lower bound "lower" with corresponding iteration count "n"
2374 * better than the one stored in "data"?
2375 * If there is no upper bound on the iteration count ("n" is infinity) or
2376 * if the count is too large, then we cannot use this lower bound.
2377 * Otherwise, if there was no previous lower bound or
2378 * if the iteration count of the new lower bound is smaller than
2379 * the iteration count of the previous lower bound, then we consider
2380 * the new lower bound to be better.
2381 * If the iteration count is the same, then compare the number
2382 * of integer divisions that would be needed to express
2383 * the iterator value at the first slice in the unrolling
2384 * according to the lower bound. If we end up computing this
2385 * number, then store the lowest value in data->n_div.
2387 static int is_better_lower_bound(struct isl_find_unroll_data *data,
2388 __isl_keep isl_aff *lower, __isl_keep isl_val *n)
2390 int cmp;
2391 int n_div;
2393 if (!n)
2394 return -1;
2395 if (isl_val_is_infty(n))
2396 return 0;
2397 if (isl_val_cmp_si(n, INT_MAX) > 0)
2398 return 0;
2399 if (!data->lower)
2400 return 1;
2401 cmp = isl_val_cmp_si(n, *data->n);
2402 if (cmp < 0)
2403 return 1;
2404 if (cmp > 0)
2405 return 0;
2406 if (data->n_div < 0)
2407 data->n_div = get_expanded_n_div(data, data->lower);
2408 if (data->n_div < 0)
2409 return -1;
2410 if (data->n_div == 0)
2411 return 0;
2412 n_div = get_expanded_n_div(data, lower);
2413 if (n_div < 0)
2414 return -1;
2415 if (n_div >= data->n_div)
2416 return 0;
2417 data->n_div = n_div;
2419 return 1;
2422 /* Check if we can use "c" as a lower bound and if it is better than
2423 * any previously found lower bound.
2425 * If "c" does not involve the dimension at the current depth,
2426 * then we cannot use it.
2427 * Otherwise, let "c" be of the form
2429 * i >= f(j)/a
2431 * We compute the maximal value of
2433 * -ceil(f(j)/a)) + i + 1
2435 * over the domain. If there is such a value "n", then we know
2437 * -ceil(f(j)/a)) + i + 1 <= n
2439 * or
2441 * i < ceil(f(j)/a)) + n
2443 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2444 * We just need to check if we have found any lower bound before and
2445 * if the new lower bound is better (smaller n or fewer integer divisions)
2446 * than the previously found lower bounds.
2448 static isl_stat update_unrolling_lower_bound(struct isl_find_unroll_data *data,
2449 __isl_keep isl_constraint *c)
2451 isl_aff *aff, *lower;
2452 isl_val *max;
2453 int better;
2455 if (!isl_constraint_is_lower_bound(c, isl_dim_set, data->depth))
2456 return isl_stat_ok;
2458 lower = isl_constraint_get_bound(c, isl_dim_set, data->depth);
2459 lower = isl_aff_ceil(lower);
2460 aff = isl_aff_copy(lower);
2461 aff = isl_aff_neg(aff);
2462 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, data->depth, 1);
2463 aff = isl_aff_add_constant_si(aff, 1);
2464 max = isl_set_max_val(data->domain, aff);
2465 isl_aff_free(aff);
2467 better = is_better_lower_bound(data, lower, max);
2468 if (better < 0 || !better) {
2469 isl_val_free(max);
2470 isl_aff_free(lower);
2471 return better < 0 ? isl_stat_error : isl_stat_ok;
2474 isl_aff_free(data->lower);
2475 data->lower = lower;
2476 *data->n = isl_val_get_num_si(max);
2477 isl_val_free(max);
2479 return isl_stat_ok;
2482 /* Check if we can use "c" as a lower bound and if it is better than
2483 * any previously found lower bound.
2485 static isl_stat constraint_find_unroll(__isl_take isl_constraint *c, void *user)
2487 struct isl_find_unroll_data *data;
2488 isl_stat r;
2490 data = (struct isl_find_unroll_data *) user;
2491 r = update_unrolling_lower_bound(data, c);
2492 isl_constraint_free(c);
2494 return r;
2497 /* Look for a lower bound l(i) on the dimension at "depth"
2498 * and a size n such that "domain" is a subset of
2500 * { [i] : l(i) <= i_d < l(i) + n }
2502 * where d is "depth" and l(i) depends only on earlier dimensions.
2503 * Furthermore, try and find a lower bound such that n is as small as possible.
2504 * In particular, "n" needs to be finite.
2505 * "build" is the build in which the unrolling will be performed.
2506 * "expansion" is the expansion that needs to be applied to "domain"
2507 * in the unrolling that will be performed.
2509 * Inner dimensions have been eliminated from "domain" by the caller.
2511 * We first construct a collection of lower bounds on the input set
2512 * by computing its simple hull. We then iterate through them,
2513 * discarding those that we cannot use (either because they do not
2514 * involve the dimension at "depth" or because they have no corresponding
2515 * upper bound, meaning that "n" would be unbounded) and pick out the
2516 * best from the remaining ones.
2518 * If we cannot find a suitable lower bound, then we consider that
2519 * to be an error.
2521 static __isl_give isl_aff *find_unroll_lower_bound(
2522 __isl_keep isl_ast_build *build, __isl_keep isl_set *domain,
2523 int depth, __isl_keep isl_basic_map *expansion, int *n)
2525 struct isl_find_unroll_data data =
2526 { build, domain, depth, expansion, NULL, n, -1 };
2527 isl_basic_set *hull;
2529 hull = isl_set_simple_hull(isl_set_copy(domain));
2531 if (isl_basic_set_foreach_constraint(hull,
2532 &constraint_find_unroll, &data) < 0)
2533 goto error;
2535 isl_basic_set_free(hull);
2537 if (!data.lower)
2538 isl_die(isl_set_get_ctx(domain), isl_error_invalid,
2539 "cannot find lower bound for unrolling", return NULL);
2541 return data.lower;
2542 error:
2543 isl_basic_set_free(hull);
2544 return isl_aff_free(data.lower);
2547 /* Call "fn" on each iteration of the current dimension of "domain".
2548 * If "init" is not NULL, then it is called with the number of
2549 * iterations before any call to "fn".
2550 * Return -1 on failure.
2552 * Since we are going to be iterating over the individual values,
2553 * we first check if there are any strides on the current dimension.
2554 * If there is, we rewrite the current dimension i as
2556 * i = stride i' + offset
2558 * and then iterate over individual values of i' instead.
2560 * We then look for a lower bound on i' and a size such that the domain
2561 * is a subset of
2563 * { [j,i'] : l(j) <= i' < l(j) + n }
2565 * and then take slices of the domain at values of i'
2566 * between l(j) and l(j) + n - 1.
2568 * We compute the unshifted simple hull of each slice to ensure that
2569 * we have a single basic set per offset. The slicing constraint
2570 * may get simplified away before the unshifted simple hull is taken
2571 * and may therefore in some rare cases disappear from the result.
2572 * We therefore explicitly add the constraint back after computing
2573 * the unshifted simple hull to ensure that the basic sets
2574 * remain disjoint. The constraints that are dropped by taking the hull
2575 * will be taken into account at the next level, as in the case of the
2576 * atomic option.
2578 * Finally, we map i' back to i and call "fn".
2580 static int foreach_iteration(__isl_take isl_set *domain,
2581 __isl_keep isl_ast_build *build, int (*init)(int n, void *user),
2582 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
2584 int i, n;
2585 int empty;
2586 int depth;
2587 isl_multi_aff *expansion;
2588 isl_basic_map *bmap;
2589 isl_aff *lower = NULL;
2590 isl_ast_build *stride_build;
2592 depth = isl_ast_build_get_depth(build);
2594 domain = isl_ast_build_eliminate_inner(build, domain);
2595 domain = isl_set_intersect(domain, isl_ast_build_get_domain(build));
2596 stride_build = isl_ast_build_copy(build);
2597 stride_build = isl_ast_build_detect_strides(stride_build,
2598 isl_set_copy(domain));
2599 expansion = isl_ast_build_get_stride_expansion(stride_build);
2601 domain = isl_set_preimage_multi_aff(domain,
2602 isl_multi_aff_copy(expansion));
2603 domain = isl_ast_build_eliminate_divs(stride_build, domain);
2604 isl_ast_build_free(stride_build);
2606 bmap = isl_basic_map_from_multi_aff(expansion);
2608 empty = isl_set_is_empty(domain);
2609 if (empty < 0) {
2610 n = -1;
2611 } else if (empty) {
2612 n = 0;
2613 } else {
2614 lower = find_unroll_lower_bound(build, domain, depth, bmap, &n);
2615 if (!lower)
2616 n = -1;
2618 if (n >= 0 && init && init(n, user) < 0)
2619 n = -1;
2620 for (i = 0; i < n; ++i) {
2621 isl_set *set;
2622 isl_basic_set *bset;
2623 isl_constraint *slice;
2625 slice = at_offset(depth, lower, i);
2626 set = isl_set_copy(domain);
2627 set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2628 bset = isl_set_unshifted_simple_hull(set);
2629 bset = isl_basic_set_add_constraint(bset, slice);
2630 bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2632 if (fn(bset, user) < 0)
2633 break;
2636 isl_aff_free(lower);
2637 isl_set_free(domain);
2638 isl_basic_map_free(bmap);
2640 return n < 0 || i < n ? -1 : 0;
2643 /* Data structure for storing the results and the intermediate objects
2644 * of compute_domains.
2646 * "list" is the main result of the function and contains a list
2647 * of disjoint basic sets for which code should be generated.
2649 * "executed" and "build" are inputs to compute_domains.
2650 * "schedule_domain" is the domain of "executed".
2652 * "option" constains the domains at the current depth that should by
2653 * atomic, separated or unrolled. These domains are as specified by
2654 * the user, except that inner dimensions have been eliminated and
2655 * that they have been made pair-wise disjoint.
2657 * "sep_class" contains the user-specified split into separation classes
2658 * specialized to the current depth.
2659 * "done" contains the union of the separation domains that have already
2660 * been handled.
2662 struct isl_codegen_domains {
2663 isl_basic_set_list *list;
2665 isl_union_map *executed;
2666 isl_ast_build *build;
2667 isl_set *schedule_domain;
2669 isl_set *option[4];
2671 isl_map *sep_class;
2672 isl_set *done;
2675 /* Internal data structure for do_unroll.
2677 * "domains" stores the results of compute_domains.
2678 * "class_domain" is the original class domain passed to do_unroll.
2679 * "unroll_domain" collects the unrolled iterations.
2681 struct isl_ast_unroll_data {
2682 struct isl_codegen_domains *domains;
2683 isl_set *class_domain;
2684 isl_set *unroll_domain;
2687 /* Given an iteration of an unrolled domain represented by "bset",
2688 * add it to data->domains->list.
2689 * Since we may have dropped some constraints, we intersect with
2690 * the class domain again to ensure that each element in the list
2691 * is disjoint from the other class domains.
2693 static int do_unroll_iteration(__isl_take isl_basic_set *bset, void *user)
2695 struct isl_ast_unroll_data *data = user;
2696 isl_set *set;
2697 isl_basic_set_list *list;
2699 set = isl_set_from_basic_set(bset);
2700 data->unroll_domain = isl_set_union(data->unroll_domain,
2701 isl_set_copy(set));
2702 set = isl_set_intersect(set, isl_set_copy(data->class_domain));
2703 set = isl_set_make_disjoint(set);
2704 list = isl_basic_set_list_from_set(set);
2705 data->domains->list = isl_basic_set_list_concat(data->domains->list,
2706 list);
2708 return 0;
2711 /* Extend domains->list with a list of basic sets, one for each value
2712 * of the current dimension in "domain" and remove the corresponding
2713 * sets from the class domain. Return the updated class domain.
2714 * The divs that involve the current dimension have not been projected out
2715 * from this domain.
2717 * We call foreach_iteration to iterate over the individual values and
2718 * in do_unroll_iteration we collect the individual basic sets in
2719 * domains->list and their union in data->unroll_domain, which is then
2720 * used to update the class domain.
2722 static __isl_give isl_set *do_unroll(struct isl_codegen_domains *domains,
2723 __isl_take isl_set *domain, __isl_take isl_set *class_domain)
2725 struct isl_ast_unroll_data data;
2727 if (!domain)
2728 return isl_set_free(class_domain);
2729 if (!class_domain)
2730 return isl_set_free(domain);
2732 data.domains = domains;
2733 data.class_domain = class_domain;
2734 data.unroll_domain = isl_set_empty(isl_set_get_space(domain));
2736 if (foreach_iteration(domain, domains->build, NULL,
2737 &do_unroll_iteration, &data) < 0)
2738 data.unroll_domain = isl_set_free(data.unroll_domain);
2740 class_domain = isl_set_subtract(class_domain, data.unroll_domain);
2742 return class_domain;
2745 /* Add domains to domains->list for each individual value of the current
2746 * dimension, for that part of the schedule domain that lies in the
2747 * intersection of the option domain and the class domain.
2748 * Remove the corresponding sets from the class domain and
2749 * return the updated class domain.
2751 * We first break up the unroll option domain into individual pieces
2752 * and then handle each of them separately. The unroll option domain
2753 * has been made disjoint in compute_domains_init_options,
2755 * Note that we actively want to combine different pieces of the
2756 * schedule domain that have the same value at the current dimension.
2757 * We therefore need to break up the unroll option domain before
2758 * intersecting with class and schedule domain, hoping that the
2759 * unroll option domain specified by the user is relatively simple.
2761 static __isl_give isl_set *compute_unroll_domains(
2762 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2764 isl_set *unroll_domain;
2765 isl_basic_set_list *unroll_list;
2766 int i, n;
2767 int empty;
2769 empty = isl_set_is_empty(domains->option[isl_ast_loop_unroll]);
2770 if (empty < 0)
2771 return isl_set_free(class_domain);
2772 if (empty)
2773 return class_domain;
2775 unroll_domain = isl_set_copy(domains->option[isl_ast_loop_unroll]);
2776 unroll_list = isl_basic_set_list_from_set(unroll_domain);
2778 n = isl_basic_set_list_n_basic_set(unroll_list);
2779 for (i = 0; i < n; ++i) {
2780 isl_basic_set *bset;
2782 bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2783 unroll_domain = isl_set_from_basic_set(bset);
2784 unroll_domain = isl_set_intersect(unroll_domain,
2785 isl_set_copy(class_domain));
2786 unroll_domain = isl_set_intersect(unroll_domain,
2787 isl_set_copy(domains->schedule_domain));
2789 empty = isl_set_is_empty(unroll_domain);
2790 if (empty >= 0 && empty) {
2791 isl_set_free(unroll_domain);
2792 continue;
2795 class_domain = do_unroll(domains, unroll_domain, class_domain);
2798 isl_basic_set_list_free(unroll_list);
2800 return class_domain;
2803 /* Try and construct a single basic set that includes the intersection of
2804 * the schedule domain, the atomic option domain and the class domain.
2805 * Add the resulting basic set(s) to domains->list and remove them
2806 * from class_domain. Return the updated class domain.
2808 * We construct a single domain rather than trying to combine
2809 * the schedule domains of individual domains because we are working
2810 * within a single component so that non-overlapping schedule domains
2811 * should already have been separated.
2812 * We do however need to make sure that this single domains is a subset
2813 * of the class domain so that it would not intersect with any other
2814 * class domains. This means that we may end up splitting up the atomic
2815 * domain in case separation classes are being used.
2817 * "domain" is the intersection of the schedule domain and the class domain,
2818 * with inner dimensions projected out.
2820 static __isl_give isl_set *compute_atomic_domain(
2821 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2823 isl_basic_set *bset;
2824 isl_basic_set_list *list;
2825 isl_set *domain, *atomic_domain;
2826 int empty;
2828 domain = isl_set_copy(domains->option[isl_ast_loop_atomic]);
2829 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2830 domain = isl_set_intersect(domain,
2831 isl_set_copy(domains->schedule_domain));
2832 empty = isl_set_is_empty(domain);
2833 if (empty < 0)
2834 class_domain = isl_set_free(class_domain);
2835 if (empty) {
2836 isl_set_free(domain);
2837 return class_domain;
2840 domain = isl_ast_build_eliminate(domains->build, domain);
2841 domain = isl_set_coalesce(domain);
2842 bset = isl_set_unshifted_simple_hull(domain);
2843 domain = isl_set_from_basic_set(bset);
2844 atomic_domain = isl_set_copy(domain);
2845 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2846 class_domain = isl_set_subtract(class_domain, atomic_domain);
2847 domain = isl_set_make_disjoint(domain);
2848 list = isl_basic_set_list_from_set(domain);
2849 domains->list = isl_basic_set_list_concat(domains->list, list);
2851 return class_domain;
2854 /* Split up the schedule domain into uniform basic sets,
2855 * in the sense that each element in a basic set is associated to
2856 * elements of the same domains, and add the result to domains->list.
2857 * Do this for that part of the schedule domain that lies in the
2858 * intersection of "class_domain" and the separate option domain.
2860 * "class_domain" may or may not include the constraints
2861 * of the schedule domain, but this does not make a difference
2862 * since we are going to intersect it with the domain of the inverse schedule.
2863 * If it includes schedule domain constraints, then they may involve
2864 * inner dimensions, but we will eliminate them in separation_domain.
2866 static int compute_separate_domain(struct isl_codegen_domains *domains,
2867 __isl_keep isl_set *class_domain)
2869 isl_space *space;
2870 isl_set *domain;
2871 isl_union_map *executed;
2872 isl_basic_set_list *list;
2873 int empty;
2875 domain = isl_set_copy(domains->option[isl_ast_loop_separate]);
2876 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2877 executed = isl_union_map_copy(domains->executed);
2878 executed = isl_union_map_intersect_domain(executed,
2879 isl_union_set_from_set(domain));
2880 empty = isl_union_map_is_empty(executed);
2881 if (empty < 0 || empty) {
2882 isl_union_map_free(executed);
2883 return empty < 0 ? -1 : 0;
2886 space = isl_set_get_space(class_domain);
2887 domain = separate_schedule_domains(space, executed, domains->build);
2889 list = isl_basic_set_list_from_set(domain);
2890 domains->list = isl_basic_set_list_concat(domains->list, list);
2892 return 0;
2895 /* Split up the domain at the current depth into disjoint
2896 * basic sets for which code should be generated separately
2897 * for the given separation class domain.
2899 * If any separation classes have been defined, then "class_domain"
2900 * is the domain of the current class and does not refer to inner dimensions.
2901 * Otherwise, "class_domain" is the universe domain.
2903 * We first make sure that the class domain is disjoint from
2904 * previously considered class domains.
2906 * The separate domains can be computed directly from the "class_domain".
2908 * The unroll, atomic and remainder domains need the constraints
2909 * from the schedule domain.
2911 * For unrolling, the actual schedule domain is needed (with divs that
2912 * may refer to the current dimension) so that stride detection can be
2913 * performed.
2915 * For atomic and remainder domains, inner dimensions and divs involving
2916 * the current dimensions should be eliminated.
2917 * In case we are working within a separation class, we need to intersect
2918 * the result with the current "class_domain" to ensure that the domains
2919 * are disjoint from those generated from other class domains.
2921 * The domain that has been made atomic may be larger than specified
2922 * by the user since it needs to be representable as a single basic set.
2923 * This possibly larger domain is removed from class_domain by
2924 * compute_atomic_domain. It is computed first so that the extended domain
2925 * would not overlap with any domains computed before.
2926 * Similary, the unrolled domains may have some constraints removed and
2927 * may therefore also be larger than specified by the user.
2929 * If anything is left after handling separate, unroll and atomic,
2930 * we split it up into basic sets and append the basic sets to domains->list.
2932 static isl_stat compute_partial_domains(struct isl_codegen_domains *domains,
2933 __isl_take isl_set *class_domain)
2935 isl_basic_set_list *list;
2936 isl_set *domain;
2938 class_domain = isl_set_subtract(class_domain,
2939 isl_set_copy(domains->done));
2940 domains->done = isl_set_union(domains->done,
2941 isl_set_copy(class_domain));
2943 class_domain = compute_atomic_domain(domains, class_domain);
2944 class_domain = compute_unroll_domains(domains, class_domain);
2946 domain = isl_set_copy(class_domain);
2948 if (compute_separate_domain(domains, domain) < 0)
2949 goto error;
2950 domain = isl_set_subtract(domain,
2951 isl_set_copy(domains->option[isl_ast_loop_separate]));
2953 domain = isl_set_intersect(domain,
2954 isl_set_copy(domains->schedule_domain));
2956 domain = isl_ast_build_eliminate(domains->build, domain);
2957 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2959 domain = isl_set_coalesce(domain);
2960 domain = isl_set_make_disjoint(domain);
2962 list = isl_basic_set_list_from_set(domain);
2963 domains->list = isl_basic_set_list_concat(domains->list, list);
2965 isl_set_free(class_domain);
2967 return isl_stat_ok;
2968 error:
2969 isl_set_free(domain);
2970 isl_set_free(class_domain);
2971 return isl_stat_error;
2974 /* Split up the domain at the current depth into disjoint
2975 * basic sets for which code should be generated separately
2976 * for the separation class identified by "pnt".
2978 * We extract the corresponding class domain from domains->sep_class,
2979 * eliminate inner dimensions and pass control to compute_partial_domains.
2981 static isl_stat compute_class_domains(__isl_take isl_point *pnt, void *user)
2983 struct isl_codegen_domains *domains = user;
2984 isl_set *class_set;
2985 isl_set *domain;
2986 int disjoint;
2988 class_set = isl_set_from_point(pnt);
2989 domain = isl_map_domain(isl_map_intersect_range(
2990 isl_map_copy(domains->sep_class), class_set));
2991 domain = isl_ast_build_compute_gist(domains->build, domain);
2992 domain = isl_ast_build_eliminate(domains->build, domain);
2994 disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
2995 if (disjoint < 0)
2996 return isl_stat_error;
2997 if (disjoint) {
2998 isl_set_free(domain);
2999 return isl_stat_ok;
3002 return compute_partial_domains(domains, domain);
3005 /* Extract the domains at the current depth that should be atomic,
3006 * separated or unrolled and store them in option.
3008 * The domains specified by the user might overlap, so we make
3009 * them disjoint by subtracting earlier domains from later domains.
3011 static void compute_domains_init_options(isl_set *option[4],
3012 __isl_keep isl_ast_build *build)
3014 enum isl_ast_loop_type type, type2;
3015 isl_set *unroll;
3017 for (type = isl_ast_loop_atomic;
3018 type <= isl_ast_loop_separate; ++type) {
3019 option[type] = isl_ast_build_get_option_domain(build, type);
3020 for (type2 = isl_ast_loop_atomic; type2 < type; ++type2)
3021 option[type] = isl_set_subtract(option[type],
3022 isl_set_copy(option[type2]));
3025 unroll = option[isl_ast_loop_unroll];
3026 unroll = isl_set_coalesce(unroll);
3027 unroll = isl_set_make_disjoint(unroll);
3028 option[isl_ast_loop_unroll] = unroll;
3031 /* Split up the domain at the current depth into disjoint
3032 * basic sets for which code should be generated separately,
3033 * based on the user-specified options.
3034 * Return the list of disjoint basic sets.
3036 * There are three kinds of domains that we need to keep track of.
3037 * - the "schedule domain" is the domain of "executed"
3038 * - the "class domain" is the domain corresponding to the currrent
3039 * separation class
3040 * - the "option domain" is the domain corresponding to one of the options
3041 * atomic, unroll or separate
3043 * We first consider the individial values of the separation classes
3044 * and split up the domain for each of them separately.
3045 * Finally, we consider the remainder. If no separation classes were
3046 * specified, then we call compute_partial_domains with the universe
3047 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
3048 * with inner dimensions removed. We do this because we want to
3049 * avoid computing the complement of the class domains (i.e., the difference
3050 * between the universe and domains->done).
3052 static __isl_give isl_basic_set_list *compute_domains(
3053 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
3055 struct isl_codegen_domains domains;
3056 isl_ctx *ctx;
3057 isl_set *domain;
3058 isl_union_set *schedule_domain;
3059 isl_set *classes;
3060 isl_space *space;
3061 int n_param;
3062 enum isl_ast_loop_type type;
3063 int empty;
3065 if (!executed)
3066 return NULL;
3068 ctx = isl_union_map_get_ctx(executed);
3069 domains.list = isl_basic_set_list_alloc(ctx, 0);
3071 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3072 domain = isl_set_from_union_set(schedule_domain);
3074 compute_domains_init_options(domains.option, build);
3076 domains.sep_class = isl_ast_build_get_separation_class(build);
3077 classes = isl_map_range(isl_map_copy(domains.sep_class));
3078 n_param = isl_set_dim(classes, isl_dim_param);
3079 classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
3081 space = isl_set_get_space(domain);
3082 domains.build = build;
3083 domains.schedule_domain = isl_set_copy(domain);
3084 domains.executed = executed;
3085 domains.done = isl_set_empty(space);
3087 if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
3088 domains.list = isl_basic_set_list_free(domains.list);
3089 isl_set_free(classes);
3091 empty = isl_set_is_empty(domains.done);
3092 if (empty < 0) {
3093 domains.list = isl_basic_set_list_free(domains.list);
3094 domain = isl_set_free(domain);
3095 } else if (empty) {
3096 isl_set_free(domain);
3097 domain = isl_set_universe(isl_set_get_space(domains.done));
3098 } else {
3099 domain = isl_ast_build_eliminate(build, domain);
3101 if (compute_partial_domains(&domains, domain) < 0)
3102 domains.list = isl_basic_set_list_free(domains.list);
3104 isl_set_free(domains.schedule_domain);
3105 isl_set_free(domains.done);
3106 isl_map_free(domains.sep_class);
3107 for (type = isl_ast_loop_atomic; type <= isl_ast_loop_separate; ++type)
3108 isl_set_free(domains.option[type]);
3110 return domains.list;
3113 /* Generate code for a single component, after shifting (if any)
3114 * has been applied, in case the schedule was specified as a union map.
3116 * We first split up the domain at the current depth into disjoint
3117 * basic sets based on the user-specified options.
3118 * Then we generated code for each of them and concatenate the results.
3120 static __isl_give isl_ast_graft_list *generate_shifted_component_flat(
3121 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3123 isl_basic_set_list *domain_list;
3124 isl_ast_graft_list *list = NULL;
3126 domain_list = compute_domains(executed, build);
3127 list = generate_parallel_domains(domain_list, executed, build);
3129 isl_basic_set_list_free(domain_list);
3130 isl_union_map_free(executed);
3131 isl_ast_build_free(build);
3133 return list;
3136 /* Generate code for a single component, after shifting (if any)
3137 * has been applied, in case the schedule was specified as a schedule tree
3138 * and the separate option was specified.
3140 * We perform separation on the domain of "executed" and then generate
3141 * an AST for each of the resulting disjoint basic sets.
3143 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_separate(
3144 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3146 isl_space *space;
3147 isl_set *domain;
3148 isl_basic_set_list *domain_list;
3149 isl_ast_graft_list *list;
3151 space = isl_ast_build_get_space(build, 1);
3152 domain = separate_schedule_domains(space,
3153 isl_union_map_copy(executed), build);
3154 domain_list = isl_basic_set_list_from_set(domain);
3156 list = generate_parallel_domains(domain_list, executed, build);
3158 isl_basic_set_list_free(domain_list);
3159 isl_union_map_free(executed);
3160 isl_ast_build_free(build);
3162 return list;
3165 /* Internal data structure for generate_shifted_component_tree_unroll.
3167 * "executed" and "build" are inputs to generate_shifted_component_tree_unroll.
3168 * "list" collects the constructs grafts.
3170 struct isl_ast_unroll_tree_data {
3171 isl_union_map *executed;
3172 isl_ast_build *build;
3173 isl_ast_graft_list *list;
3176 /* Initialize data->list to a list of "n" elements.
3178 static int init_unroll_tree(int n, void *user)
3180 struct isl_ast_unroll_tree_data *data = user;
3181 isl_ctx *ctx;
3183 ctx = isl_ast_build_get_ctx(data->build);
3184 data->list = isl_ast_graft_list_alloc(ctx, n);
3186 return 0;
3189 /* Given an iteration of an unrolled domain represented by "bset",
3190 * generate the corresponding AST and add the result to data->list.
3192 static int do_unroll_tree_iteration(__isl_take isl_basic_set *bset, void *user)
3194 struct isl_ast_unroll_tree_data *data = user;
3196 data->list = add_node(data->list, isl_union_map_copy(data->executed),
3197 bset, isl_ast_build_copy(data->build));
3199 return 0;
3202 /* Generate code for a single component, after shifting (if any)
3203 * has been applied, in case the schedule was specified as a schedule tree
3204 * and the unroll option was specified.
3206 * We call foreach_iteration to iterate over the individual values and
3207 * construct and collect the corresponding grafts in do_unroll_tree_iteration.
3209 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_unroll(
3210 __isl_take isl_union_map *executed, __isl_take isl_set *domain,
3211 __isl_take isl_ast_build *build)
3213 struct isl_ast_unroll_tree_data data = { executed, build, NULL };
3215 if (foreach_iteration(domain, build, &init_unroll_tree,
3216 &do_unroll_tree_iteration, &data) < 0)
3217 data.list = isl_ast_graft_list_free(data.list);
3219 isl_union_map_free(executed);
3220 isl_ast_build_free(build);
3222 return data.list;
3225 /* Generate code for a single component, after shifting (if any)
3226 * has been applied, in case the schedule was specified as a schedule tree.
3227 * In particular, handle the base case where there is either no isolated
3228 * set or we are within the isolated set (in which case "isolated" is set)
3229 * or the iterations that precede or follow the isolated set.
3231 * The schedule domain is broken up or combined into basic sets
3232 * according to the AST generation option specified in the current
3233 * schedule node, which may be either atomic, separate, unroll or
3234 * unspecified. If the option is unspecified, then we currently simply
3235 * split the schedule domain into disjoint basic sets.
3237 * In case the separate option is specified, the AST generation is
3238 * handled by generate_shifted_component_tree_separate.
3239 * In the other cases, we need the global schedule domain.
3240 * In the unroll case, the AST generation is then handled by
3241 * generate_shifted_component_tree_unroll which needs the actual
3242 * schedule domain (with divs that may refer to the current dimension)
3243 * so that stride detection can be performed.
3244 * In the atomic or unspecified case, inner dimensions and divs involving
3245 * the current dimensions should be eliminated.
3246 * The result is then either combined into a single basic set or
3247 * split up into disjoint basic sets.
3248 * Finally an AST is generated for each basic set and the results are
3249 * concatenated.
3251 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_base(
3252 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3253 int isolated)
3255 isl_union_set *schedule_domain;
3256 isl_set *domain;
3257 isl_basic_set_list *domain_list;
3258 isl_ast_graft_list *list;
3259 enum isl_ast_loop_type type;
3261 type = isl_ast_build_get_loop_type(build, isolated);
3262 if (type < 0)
3263 goto error;
3265 if (type == isl_ast_loop_separate)
3266 return generate_shifted_component_tree_separate(executed,
3267 build);
3269 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3270 domain = isl_set_from_union_set(schedule_domain);
3272 if (type == isl_ast_loop_unroll)
3273 return generate_shifted_component_tree_unroll(executed, domain,
3274 build);
3276 domain = isl_ast_build_eliminate(build, domain);
3277 domain = isl_set_coalesce(domain);
3279 if (type == isl_ast_loop_atomic) {
3280 isl_basic_set *hull;
3281 hull = isl_set_unshifted_simple_hull(domain);
3282 domain_list = isl_basic_set_list_from_basic_set(hull);
3283 } else {
3284 domain = isl_set_make_disjoint(domain);
3285 domain_list = isl_basic_set_list_from_set(domain);
3288 list = generate_parallel_domains(domain_list, executed, build);
3290 isl_basic_set_list_free(domain_list);
3291 isl_union_map_free(executed);
3292 isl_ast_build_free(build);
3294 return list;
3295 error:
3296 isl_union_map_free(executed);
3297 isl_ast_build_free(build);
3298 return NULL;
3301 /* Extract out the disjunction imposed by "domain" on the outer
3302 * schedule dimensions.
3304 * In particular, remove all inner dimensions from "domain" (including
3305 * the current dimension) and then remove the constraints that are shared
3306 * by all disjuncts in the result.
3308 static __isl_give isl_set *extract_disjunction(__isl_take isl_set *domain,
3309 __isl_keep isl_ast_build *build)
3311 isl_set *hull;
3312 int depth, dim;
3314 domain = isl_ast_build_specialize(build, domain);
3315 depth = isl_ast_build_get_depth(build);
3316 dim = isl_set_dim(domain, isl_dim_set);
3317 domain = isl_set_eliminate(domain, isl_dim_set, depth, dim - depth);
3318 domain = isl_set_remove_unknown_divs(domain);
3319 hull = isl_set_copy(domain);
3320 hull = isl_set_from_basic_set(isl_set_unshifted_simple_hull(hull));
3321 domain = isl_set_gist(domain, hull);
3323 return domain;
3326 /* Add "guard" to the grafts in "list".
3327 * "build" is the outer AST build, while "sub_build" includes "guard"
3328 * in its generated domain.
3330 * First combine the grafts into a single graft and then add the guard.
3331 * If the list is empty, or if some error occurred, then simply return
3332 * the list.
3334 static __isl_give isl_ast_graft_list *list_add_guard(
3335 __isl_take isl_ast_graft_list *list, __isl_keep isl_set *guard,
3336 __isl_keep isl_ast_build *build, __isl_keep isl_ast_build *sub_build)
3338 isl_ast_graft *graft;
3340 list = isl_ast_graft_list_fuse(list, sub_build);
3342 if (isl_ast_graft_list_n_ast_graft(list) != 1)
3343 return list;
3345 graft = isl_ast_graft_list_get_ast_graft(list, 0);
3346 graft = isl_ast_graft_add_guard(graft, isl_set_copy(guard), build);
3347 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
3349 return list;
3352 /* Generate code for a single component, after shifting (if any)
3353 * has been applied, in case the schedule was specified as a schedule tree.
3354 * In particular, do so for the specified subset of the schedule domain.
3356 * If we are outside of the isolated part, then "domain" may include
3357 * a disjunction. Explicitly generate this disjunction at this point
3358 * instead of relying on the disjunction getting hoisted back up
3359 * to this level.
3361 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_part(
3362 __isl_keep isl_union_map *executed, __isl_take isl_set *domain,
3363 __isl_keep isl_ast_build *build, int isolated)
3365 isl_union_set *uset;
3366 isl_ast_graft_list *list;
3367 isl_ast_build *sub_build;
3368 int empty;
3370 uset = isl_union_set_from_set(isl_set_copy(domain));
3371 executed = isl_union_map_copy(executed);
3372 executed = isl_union_map_intersect_domain(executed, uset);
3373 empty = isl_union_map_is_empty(executed);
3374 if (empty < 0)
3375 goto error;
3376 if (empty) {
3377 isl_ctx *ctx;
3378 isl_union_map_free(executed);
3379 isl_set_free(domain);
3380 ctx = isl_ast_build_get_ctx(build);
3381 return isl_ast_graft_list_alloc(ctx, 0);
3384 sub_build = isl_ast_build_copy(build);
3385 if (!isolated) {
3386 domain = extract_disjunction(domain, build);
3387 sub_build = isl_ast_build_restrict_generated(sub_build,
3388 isl_set_copy(domain));
3390 list = generate_shifted_component_tree_base(executed,
3391 isl_ast_build_copy(sub_build), isolated);
3392 if (!isolated)
3393 list = list_add_guard(list, domain, build, sub_build);
3394 isl_ast_build_free(sub_build);
3395 isl_set_free(domain);
3396 return list;
3397 error:
3398 isl_union_map_free(executed);
3399 isl_set_free(domain);
3400 return NULL;
3403 /* Generate code for a single component, after shifting (if any)
3404 * has been applied, in case the schedule was specified as a schedule tree.
3405 * In particular, do so for the specified sequence of subsets
3406 * of the schedule domain, "before", "isolated", "after" and "other",
3407 * where only the "isolated" part is considered to be isolated.
3409 static __isl_give isl_ast_graft_list *generate_shifted_component_parts(
3410 __isl_take isl_union_map *executed, __isl_take isl_set *before,
3411 __isl_take isl_set *isolated, __isl_take isl_set *after,
3412 __isl_take isl_set *other, __isl_take isl_ast_build *build)
3414 isl_ast_graft_list *list, *res;
3416 res = generate_shifted_component_tree_part(executed, before, build, 0);
3417 list = generate_shifted_component_tree_part(executed, isolated,
3418 build, 1);
3419 res = isl_ast_graft_list_concat(res, list);
3420 list = generate_shifted_component_tree_part(executed, after, build, 0);
3421 res = isl_ast_graft_list_concat(res, list);
3422 list = generate_shifted_component_tree_part(executed, other, build, 0);
3423 res = isl_ast_graft_list_concat(res, list);
3425 isl_union_map_free(executed);
3426 isl_ast_build_free(build);
3428 return res;
3431 /* Does "set" intersect "first", but not "second"?
3433 static isl_bool only_intersects_first(__isl_keep isl_set *set,
3434 __isl_keep isl_set *first, __isl_keep isl_set *second)
3436 isl_bool disjoint;
3438 disjoint = isl_set_is_disjoint(set, first);
3439 if (disjoint < 0)
3440 return isl_bool_error;
3441 if (disjoint)
3442 return isl_bool_false;
3444 return isl_set_is_disjoint(set, second);
3447 /* Generate code for a single component, after shifting (if any)
3448 * has been applied, in case the schedule was specified as a schedule tree.
3449 * In particular, do so in case of isolation where there is
3450 * only an "isolated" part and an "after" part.
3451 * "dead1" and "dead2" are freed by this function in order to simplify
3452 * the caller.
3454 * The "before" and "other" parts are set to empty sets.
3456 static __isl_give isl_ast_graft_list *generate_shifted_component_only_after(
3457 __isl_take isl_union_map *executed, __isl_take isl_set *isolated,
3458 __isl_take isl_set *after, __isl_take isl_ast_build *build,
3459 __isl_take isl_set *dead1, __isl_take isl_set *dead2)
3461 isl_set *empty;
3463 empty = isl_set_empty(isl_set_get_space(after));
3464 isl_set_free(dead1);
3465 isl_set_free(dead2);
3466 return generate_shifted_component_parts(executed, isl_set_copy(empty),
3467 isolated, after, empty, build);
3470 /* Generate code for a single component, after shifting (if any)
3471 * has been applied, in case the schedule was specified as a schedule tree.
3473 * We first check if the user has specified an isolated schedule domain
3474 * and that we are not already outside of this isolated schedule domain.
3475 * If so, we break up the schedule domain into iterations that
3476 * precede the isolated domain, the isolated domain itself,
3477 * the iterations that follow the isolated domain and
3478 * the remaining iterations (those that are incomparable
3479 * to the isolated domain).
3480 * We generate an AST for each piece and concatenate the results.
3482 * In the special case where at least one element of the schedule
3483 * domain that does not belong to the isolated domain needs
3484 * to be scheduled after this isolated domain, but none of those
3485 * elements need to be scheduled before, break up the schedule domain
3486 * in only two parts, the isolated domain, and a part that will be
3487 * scheduled after the isolated domain.
3489 * If no isolated set has been specified, then we generate an
3490 * AST for the entire inverse schedule.
3492 static __isl_give isl_ast_graft_list *generate_shifted_component_tree(
3493 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3495 int i, depth;
3496 int empty, has_isolate;
3497 isl_space *space;
3498 isl_union_set *schedule_domain;
3499 isl_set *domain;
3500 isl_basic_set *hull;
3501 isl_set *isolated, *before, *after, *test;
3502 isl_map *gt, *lt;
3503 isl_bool pure;
3505 build = isl_ast_build_extract_isolated(build);
3506 has_isolate = isl_ast_build_has_isolated(build);
3507 if (has_isolate < 0)
3508 executed = isl_union_map_free(executed);
3509 else if (!has_isolate)
3510 return generate_shifted_component_tree_base(executed, build, 0);
3512 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3513 domain = isl_set_from_union_set(schedule_domain);
3515 isolated = isl_ast_build_get_isolated(build);
3516 isolated = isl_set_intersect(isolated, isl_set_copy(domain));
3517 test = isl_ast_build_specialize(build, isl_set_copy(isolated));
3518 empty = isl_set_is_empty(test);
3519 isl_set_free(test);
3520 if (empty < 0)
3521 goto error;
3522 if (empty) {
3523 isl_set_free(isolated);
3524 isl_set_free(domain);
3525 return generate_shifted_component_tree_base(executed, build, 0);
3527 isolated = isl_ast_build_eliminate(build, isolated);
3528 hull = isl_set_unshifted_simple_hull(isolated);
3529 isolated = isl_set_from_basic_set(hull);
3531 depth = isl_ast_build_get_depth(build);
3532 space = isl_space_map_from_set(isl_set_get_space(isolated));
3533 gt = isl_map_universe(space);
3534 for (i = 0; i < depth; ++i)
3535 gt = isl_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
3536 gt = isl_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
3537 lt = isl_map_reverse(isl_map_copy(gt));
3538 before = isl_set_apply(isl_set_copy(isolated), gt);
3539 after = isl_set_apply(isl_set_copy(isolated), lt);
3541 domain = isl_set_subtract(domain, isl_set_copy(isolated));
3542 pure = only_intersects_first(domain, after, before);
3543 if (pure < 0)
3544 executed = isl_union_map_free(executed);
3545 else if (pure)
3546 return generate_shifted_component_only_after(executed, isolated,
3547 domain, build, before, after);
3548 domain = isl_set_subtract(domain, isl_set_copy(before));
3549 domain = isl_set_subtract(domain, isl_set_copy(after));
3550 after = isl_set_subtract(after, isl_set_copy(isolated));
3551 after = isl_set_subtract(after, isl_set_copy(before));
3552 before = isl_set_subtract(before, isl_set_copy(isolated));
3554 return generate_shifted_component_parts(executed, before, isolated,
3555 after, domain, build);
3556 error:
3557 isl_set_free(domain);
3558 isl_set_free(isolated);
3559 isl_union_map_free(executed);
3560 isl_ast_build_free(build);
3561 return NULL;
3564 /* Generate code for a single component, after shifting (if any)
3565 * has been applied.
3567 * Call generate_shifted_component_tree or generate_shifted_component_flat
3568 * depending on whether the schedule was specified as a schedule tree.
3570 static __isl_give isl_ast_graft_list *generate_shifted_component(
3571 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3573 if (isl_ast_build_has_schedule_node(build))
3574 return generate_shifted_component_tree(executed, build);
3575 else
3576 return generate_shifted_component_flat(executed, build);
3579 struct isl_set_map_pair {
3580 isl_set *set;
3581 isl_map *map;
3584 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3585 * of indices into the "domain" array,
3586 * return the union of the "map" fields of the elements
3587 * indexed by the first "n" elements of "order".
3589 static __isl_give isl_union_map *construct_component_executed(
3590 struct isl_set_map_pair *domain, int *order, int n)
3592 int i;
3593 isl_map *map;
3594 isl_union_map *executed;
3596 map = isl_map_copy(domain[order[0]].map);
3597 executed = isl_union_map_from_map(map);
3598 for (i = 1; i < n; ++i) {
3599 map = isl_map_copy(domain[order[i]].map);
3600 executed = isl_union_map_add_map(executed, map);
3603 return executed;
3606 /* Generate code for a single component, after shifting (if any)
3607 * has been applied.
3609 * The component inverse schedule is specified as the "map" fields
3610 * of the elements of "domain" indexed by the first "n" elements of "order".
3612 static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
3613 struct isl_set_map_pair *domain, int *order, int n,
3614 __isl_take isl_ast_build *build)
3616 isl_union_map *executed;
3618 executed = construct_component_executed(domain, order, n);
3619 return generate_shifted_component(executed, build);
3622 /* Does set dimension "pos" of "set" have an obviously fixed value?
3624 static int dim_is_fixed(__isl_keep isl_set *set, int pos)
3626 int fixed;
3627 isl_val *v;
3629 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, pos);
3630 if (!v)
3631 return -1;
3632 fixed = !isl_val_is_nan(v);
3633 isl_val_free(v);
3635 return fixed;
3638 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3639 * of indices into the "domain" array,
3640 * do all (except for at most one) of the "set" field of the elements
3641 * indexed by the first "n" elements of "order" have a fixed value
3642 * at position "depth"?
3644 static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
3645 int *order, int n, int depth)
3647 int i;
3648 int non_fixed = -1;
3650 for (i = 0; i < n; ++i) {
3651 int f;
3653 f = dim_is_fixed(domain[order[i]].set, depth);
3654 if (f < 0)
3655 return -1;
3656 if (f)
3657 continue;
3658 if (non_fixed >= 0)
3659 return 0;
3660 non_fixed = i;
3663 return 1;
3666 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3667 * of indices into the "domain" array,
3668 * eliminate the inner dimensions from the "set" field of the elements
3669 * indexed by the first "n" elements of "order", provided the current
3670 * dimension does not have a fixed value.
3672 * Return the index of the first element in "order" with a corresponding
3673 * "set" field that does not have an (obviously) fixed value.
3675 static int eliminate_non_fixed(struct isl_set_map_pair *domain,
3676 int *order, int n, int depth, __isl_keep isl_ast_build *build)
3678 int i;
3679 int base = -1;
3681 for (i = n - 1; i >= 0; --i) {
3682 int f;
3683 f = dim_is_fixed(domain[order[i]].set, depth);
3684 if (f < 0)
3685 return -1;
3686 if (f)
3687 continue;
3688 domain[order[i]].set = isl_ast_build_eliminate_inner(build,
3689 domain[order[i]].set);
3690 base = i;
3693 return base;
3696 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3697 * of indices into the "domain" array,
3698 * find the element of "domain" (amongst those indexed by the first "n"
3699 * elements of "order") with the "set" field that has the smallest
3700 * value for the current iterator.
3702 * Note that the domain with the smallest value may depend on the parameters
3703 * and/or outer loop dimension. Since the result of this function is only
3704 * used as heuristic, we only make a reasonable attempt at finding the best
3705 * domain, one that should work in case a single domain provides the smallest
3706 * value for the current dimension over all values of the parameters
3707 * and outer dimensions.
3709 * In particular, we compute the smallest value of the first domain
3710 * and replace it by that of any later domain if that later domain
3711 * has a smallest value that is smaller for at least some value
3712 * of the parameters and outer dimensions.
3714 static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
3715 __isl_keep isl_ast_build *build)
3717 int i;
3718 isl_map *min_first;
3719 int first = 0;
3721 min_first = isl_ast_build_map_to_iterator(build,
3722 isl_set_copy(domain[order[0]].set));
3723 min_first = isl_map_lexmin(min_first);
3725 for (i = 1; i < n; ++i) {
3726 isl_map *min, *test;
3727 int empty;
3729 min = isl_ast_build_map_to_iterator(build,
3730 isl_set_copy(domain[order[i]].set));
3731 min = isl_map_lexmin(min);
3732 test = isl_map_copy(min);
3733 test = isl_map_apply_domain(isl_map_copy(min_first), test);
3734 test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
3735 empty = isl_map_is_empty(test);
3736 isl_map_free(test);
3737 if (empty >= 0 && !empty) {
3738 isl_map_free(min_first);
3739 first = i;
3740 min_first = min;
3741 } else
3742 isl_map_free(min);
3744 if (empty < 0)
3745 break;
3748 isl_map_free(min_first);
3750 return i < n ? -1 : first;
3753 /* Construct a shifted inverse schedule based on the original inverse schedule,
3754 * the stride and the offset.
3756 * The original inverse schedule is specified as the "map" fields
3757 * of the elements of "domain" indexed by the first "n" elements of "order".
3759 * "stride" and "offset" are such that the difference
3760 * between the values of the current dimension of domain "i"
3761 * and the values of the current dimension for some reference domain are
3762 * equal to
3764 * stride * integer + offset[i]
3766 * Moreover, 0 <= offset[i] < stride.
3768 * For each domain, we create a map
3770 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3772 * where j refers to the current dimension and the other dimensions are
3773 * unchanged, and apply this map to the original schedule domain.
3775 * For example, for the original schedule
3777 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3779 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3780 * we apply the mapping
3782 * { [j] -> [j, 0] }
3784 * to the schedule of the "A" domain and the mapping
3786 * { [j - 1] -> [j, 1] }
3788 * to the schedule of the "B" domain.
3791 * Note that after the transformation, the differences between pairs
3792 * of values of the current dimension over all domains are multiples
3793 * of stride and that we have therefore exposed the stride.
3796 * To see that the mapping preserves the lexicographic order,
3797 * first note that each of the individual maps above preserves the order.
3798 * If the value of the current iterator is j1 in one domain and j2 in another,
3799 * then if j1 = j2, we know that the same map is applied to both domains
3800 * and the order is preserved.
3801 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3802 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3804 * j1 - c1 < j2 - c2
3806 * and the order is preserved.
3807 * If c1 < c2, then we know
3809 * 0 <= c2 - c1 < s
3811 * We also have
3813 * j2 - j1 = n * s + r
3815 * with n >= 0 and 0 <= r < s.
3816 * In other words, r = c2 - c1.
3817 * If n > 0, then
3819 * j1 - c1 < j2 - c2
3821 * If n = 0, then
3823 * j1 - c1 = j2 - c2
3825 * and so
3827 * (j1 - c1, c1) << (j2 - c2, c2)
3829 * with "<<" the lexicographic order, proving that the order is preserved
3830 * in all cases.
3832 static __isl_give isl_union_map *contruct_shifted_executed(
3833 struct isl_set_map_pair *domain, int *order, int n,
3834 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3835 __isl_take isl_ast_build *build)
3837 int i;
3838 isl_union_map *executed;
3839 isl_space *space;
3840 isl_map *map;
3841 int depth;
3842 isl_constraint *c;
3844 depth = isl_ast_build_get_depth(build);
3845 space = isl_ast_build_get_space(build, 1);
3846 executed = isl_union_map_empty(isl_space_copy(space));
3847 space = isl_space_map_from_set(space);
3848 map = isl_map_identity(isl_space_copy(space));
3849 map = isl_map_eliminate(map, isl_dim_out, depth, 1);
3850 map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
3851 space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
3853 c = isl_constraint_alloc_equality(isl_local_space_from_space(space));
3854 c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
3855 c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
3857 for (i = 0; i < n; ++i) {
3858 isl_map *map_i;
3859 isl_val *v;
3861 v = isl_multi_val_get_val(offset, i);
3862 if (!v)
3863 break;
3864 map_i = isl_map_copy(map);
3865 map_i = isl_map_fix_val(map_i, isl_dim_out, depth + 1,
3866 isl_val_copy(v));
3867 v = isl_val_neg(v);
3868 c = isl_constraint_set_constant_val(c, v);
3869 map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
3871 map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
3872 map_i);
3873 executed = isl_union_map_add_map(executed, map_i);
3876 isl_constraint_free(c);
3877 isl_map_free(map);
3879 if (i < n)
3880 executed = isl_union_map_free(executed);
3882 return executed;
3885 /* Generate code for a single component, after exposing the stride,
3886 * given that the schedule domain is "shifted strided".
3888 * The component inverse schedule is specified as the "map" fields
3889 * of the elements of "domain" indexed by the first "n" elements of "order".
3891 * The schedule domain being "shifted strided" means that the differences
3892 * between the values of the current dimension of domain "i"
3893 * and the values of the current dimension for some reference domain are
3894 * equal to
3896 * stride * integer + offset[i]
3898 * We first look for the domain with the "smallest" value for the current
3899 * dimension and adjust the offsets such that the offset of the "smallest"
3900 * domain is equal to zero. The other offsets are reduced modulo stride.
3902 * Based on this information, we construct a new inverse schedule in
3903 * contruct_shifted_executed that exposes the stride.
3904 * Since this involves the introduction of a new schedule dimension,
3905 * the build needs to be changed accodingly.
3906 * After computing the AST, the newly introduced dimension needs
3907 * to be removed again from the list of grafts. We do this by plugging
3908 * in a mapping that represents the new schedule domain in terms of the
3909 * old schedule domain.
3911 static __isl_give isl_ast_graft_list *generate_shift_component(
3912 struct isl_set_map_pair *domain, int *order, int n,
3913 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3914 __isl_take isl_ast_build *build)
3916 isl_ast_graft_list *list;
3917 int first;
3918 int depth;
3919 isl_val *val;
3920 isl_multi_val *mv;
3921 isl_space *space;
3922 isl_multi_aff *ma, *zero;
3923 isl_union_map *executed;
3925 depth = isl_ast_build_get_depth(build);
3927 first = first_offset(domain, order, n, build);
3928 if (first < 0)
3929 goto error;
3931 mv = isl_multi_val_copy(offset);
3932 val = isl_multi_val_get_val(offset, first);
3933 val = isl_val_neg(val);
3934 mv = isl_multi_val_add_val(mv, val);
3935 mv = isl_multi_val_mod_val(mv, isl_val_copy(stride));
3937 executed = contruct_shifted_executed(domain, order, n, stride, mv,
3938 build);
3939 space = isl_ast_build_get_space(build, 1);
3940 space = isl_space_map_from_set(space);
3941 ma = isl_multi_aff_identity(isl_space_copy(space));
3942 space = isl_space_from_domain(isl_space_domain(space));
3943 space = isl_space_add_dims(space, isl_dim_out, 1);
3944 zero = isl_multi_aff_zero(space);
3945 ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
3946 build = isl_ast_build_insert_dim(build, depth + 1);
3947 list = generate_shifted_component(executed, build);
3949 list = isl_ast_graft_list_preimage_multi_aff(list, ma);
3951 isl_multi_val_free(mv);
3953 return list;
3954 error:
3955 isl_ast_build_free(build);
3956 return NULL;
3959 /* Does any node in the schedule tree rooted at the current schedule node
3960 * of "build" depend on outer schedule nodes?
3962 static int has_anchored_subtree(__isl_keep isl_ast_build *build)
3964 isl_schedule_node *node;
3965 int dependent = 0;
3967 node = isl_ast_build_get_schedule_node(build);
3968 dependent = isl_schedule_node_is_subtree_anchored(node);
3969 isl_schedule_node_free(node);
3971 return dependent;
3974 /* Generate code for a single component.
3976 * The component inverse schedule is specified as the "map" fields
3977 * of the elements of "domain" indexed by the first "n" elements of "order".
3979 * This function may modify the "set" fields of "domain".
3981 * Before proceeding with the actual code generation for the component,
3982 * we first check if there are any "shifted" strides, meaning that
3983 * the schedule domains of the individual domains are all strided,
3984 * but that they have different offsets, resulting in the union
3985 * of schedule domains not being strided anymore.
3987 * The simplest example is the schedule
3989 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3991 * Both schedule domains are strided, but their union is not.
3992 * This function detects such cases and then rewrites the schedule to
3994 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
3996 * In the new schedule, the schedule domains have the same offset (modulo
3997 * the stride), ensuring that the union of schedule domains is also strided.
4000 * If there is only a single domain in the component, then there is
4001 * nothing to do. Similarly, if the current schedule dimension has
4002 * a fixed value for almost all domains then there is nothing to be done.
4003 * In particular, we need at least two domains where the current schedule
4004 * dimension does not have a fixed value.
4005 * Finally, in case of a schedule map input,
4006 * if any of the options refer to the current schedule dimension,
4007 * then we bail out as well. It would be possible to reformulate the options
4008 * in terms of the new schedule domain, but that would introduce constraints
4009 * that separate the domains in the options and that is something we would
4010 * like to avoid.
4011 * In the case of a schedule tree input, we bail out if any of
4012 * the descendants of the current schedule node refer to outer
4013 * schedule nodes in any way.
4016 * To see if there is any shifted stride, we look at the differences
4017 * between the values of the current dimension in pairs of domains
4018 * for equal values of outer dimensions. These differences should be
4019 * of the form
4021 * m x + r
4023 * with "m" the stride and "r" a constant. Note that we cannot perform
4024 * this analysis on individual domains as the lower bound in each domain
4025 * may depend on parameters or outer dimensions and so the current dimension
4026 * itself may not have a fixed remainder on division by the stride.
4028 * In particular, we compare the first domain that does not have an
4029 * obviously fixed value for the current dimension to itself and all
4030 * other domains and collect the offsets and the gcd of the strides.
4031 * If the gcd becomes one, then we failed to find shifted strides.
4032 * If the gcd is zero, then the differences were all fixed, meaning
4033 * that some domains had non-obviously fixed values for the current dimension.
4034 * If all the offsets are the same (for those domains that do not have
4035 * an obviously fixed value for the current dimension), then we do not
4036 * apply the transformation.
4037 * If none of the domains were skipped, then there is nothing to do.
4038 * If some of them were skipped, then if we apply separation, the schedule
4039 * domain should get split in pieces with a (non-shifted) stride.
4041 * Otherwise, we apply a shift to expose the stride in
4042 * generate_shift_component.
4044 static __isl_give isl_ast_graft_list *generate_component(
4045 struct isl_set_map_pair *domain, int *order, int n,
4046 __isl_take isl_ast_build *build)
4048 int i, d;
4049 int depth;
4050 isl_ctx *ctx;
4051 isl_map *map;
4052 isl_set *deltas;
4053 isl_val *gcd = NULL;
4054 isl_multi_val *mv;
4055 int fixed, skip;
4056 int base;
4057 isl_ast_graft_list *list;
4058 int res = 0;
4060 depth = isl_ast_build_get_depth(build);
4062 skip = n == 1;
4063 if (skip >= 0 && !skip)
4064 skip = at_most_one_non_fixed(domain, order, n, depth);
4065 if (skip >= 0 && !skip) {
4066 if (isl_ast_build_has_schedule_node(build))
4067 skip = has_anchored_subtree(build);
4068 else
4069 skip = isl_ast_build_options_involve_depth(build);
4071 if (skip < 0)
4072 goto error;
4073 if (skip)
4074 return generate_shifted_component_from_list(domain,
4075 order, n, build);
4077 base = eliminate_non_fixed(domain, order, n, depth, build);
4078 if (base < 0)
4079 goto error;
4081 ctx = isl_ast_build_get_ctx(build);
4083 mv = isl_multi_val_zero(isl_space_set_alloc(ctx, 0, n));
4085 fixed = 1;
4086 for (i = 0; i < n; ++i) {
4087 isl_val *r, *m;
4089 map = isl_map_from_domain_and_range(
4090 isl_set_copy(domain[order[base]].set),
4091 isl_set_copy(domain[order[i]].set));
4092 for (d = 0; d < depth; ++d)
4093 map = isl_map_equate(map, isl_dim_in, d,
4094 isl_dim_out, d);
4095 deltas = isl_map_deltas(map);
4096 res = isl_set_dim_residue_class_val(deltas, depth, &m, &r);
4097 isl_set_free(deltas);
4098 if (res < 0)
4099 break;
4101 if (i == 0)
4102 gcd = m;
4103 else
4104 gcd = isl_val_gcd(gcd, m);
4105 if (isl_val_is_one(gcd)) {
4106 isl_val_free(r);
4107 break;
4109 mv = isl_multi_val_set_val(mv, i, r);
4111 res = dim_is_fixed(domain[order[i]].set, depth);
4112 if (res < 0)
4113 break;
4114 if (res)
4115 continue;
4117 if (fixed && i > base) {
4118 isl_val *a, *b;
4119 a = isl_multi_val_get_val(mv, i);
4120 b = isl_multi_val_get_val(mv, base);
4121 if (isl_val_ne(a, b))
4122 fixed = 0;
4123 isl_val_free(a);
4124 isl_val_free(b);
4128 if (res < 0 || !gcd) {
4129 isl_ast_build_free(build);
4130 list = NULL;
4131 } else if (i < n || fixed || isl_val_is_zero(gcd)) {
4132 list = generate_shifted_component_from_list(domain,
4133 order, n, build);
4134 } else {
4135 list = generate_shift_component(domain, order, n, gcd, mv,
4136 build);
4139 isl_val_free(gcd);
4140 isl_multi_val_free(mv);
4142 return list;
4143 error:
4144 isl_ast_build_free(build);
4145 return NULL;
4148 /* Store both "map" itself and its domain in the
4149 * structure pointed to by *next and advance to the next array element.
4151 static isl_stat extract_domain(__isl_take isl_map *map, void *user)
4153 struct isl_set_map_pair **next = user;
4155 (*next)->map = isl_map_copy(map);
4156 (*next)->set = isl_map_domain(map);
4157 (*next)++;
4159 return isl_stat_ok;
4162 static int after_in_tree(__isl_keep isl_union_map *umap,
4163 __isl_keep isl_schedule_node *node);
4165 /* Is any domain element of "umap" scheduled after any of
4166 * the corresponding image elements by the tree rooted at
4167 * the child of "node"?
4169 static int after_in_child(__isl_keep isl_union_map *umap,
4170 __isl_keep isl_schedule_node *node)
4172 isl_schedule_node *child;
4173 int after;
4175 child = isl_schedule_node_get_child(node, 0);
4176 after = after_in_tree(umap, child);
4177 isl_schedule_node_free(child);
4179 return after;
4182 /* Is any domain element of "umap" scheduled after any of
4183 * the corresponding image elements by the tree rooted at
4184 * the band node "node"?
4186 * We first check if any domain element is scheduled after any
4187 * of the corresponding image elements by the band node itself.
4188 * If not, we restrict "map" to those pairs of element that
4189 * are scheduled together by the band node and continue with
4190 * the child of the band node.
4191 * If there are no such pairs then the map passed to after_in_child
4192 * will be empty causing it to return 0.
4194 static int after_in_band(__isl_keep isl_union_map *umap,
4195 __isl_keep isl_schedule_node *node)
4197 isl_multi_union_pw_aff *mupa;
4198 isl_union_map *partial, *test, *gt, *universe, *umap1, *umap2;
4199 isl_union_set *domain, *range;
4200 isl_space *space;
4201 int empty;
4202 int after;
4204 if (isl_schedule_node_band_n_member(node) == 0)
4205 return after_in_child(umap, node);
4207 mupa = isl_schedule_node_band_get_partial_schedule(node);
4208 space = isl_multi_union_pw_aff_get_space(mupa);
4209 partial = isl_union_map_from_multi_union_pw_aff(mupa);
4210 test = isl_union_map_copy(umap);
4211 test = isl_union_map_apply_domain(test, isl_union_map_copy(partial));
4212 test = isl_union_map_apply_range(test, isl_union_map_copy(partial));
4213 gt = isl_union_map_from_map(isl_map_lex_gt(space));
4214 test = isl_union_map_intersect(test, gt);
4215 empty = isl_union_map_is_empty(test);
4216 isl_union_map_free(test);
4218 if (empty < 0 || !empty) {
4219 isl_union_map_free(partial);
4220 return empty < 0 ? -1 : 1;
4223 universe = isl_union_map_universe(isl_union_map_copy(umap));
4224 domain = isl_union_map_domain(isl_union_map_copy(universe));
4225 range = isl_union_map_range(universe);
4226 umap1 = isl_union_map_copy(partial);
4227 umap1 = isl_union_map_intersect_domain(umap1, domain);
4228 umap2 = isl_union_map_intersect_domain(partial, range);
4229 test = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4230 test = isl_union_map_intersect(test, isl_union_map_copy(umap));
4231 after = after_in_child(test, node);
4232 isl_union_map_free(test);
4233 return after;
4236 /* Is any domain element of "umap" scheduled after any of
4237 * the corresponding image elements by the tree rooted at
4238 * the context node "node"?
4240 * The context constraints apply to the schedule domain,
4241 * so we cannot apply them directly to "umap", which contains
4242 * pairs of statement instances. Instead, we add them
4243 * to the range of the prefix schedule for both domain and
4244 * range of "umap".
4246 static int after_in_context(__isl_keep isl_union_map *umap,
4247 __isl_keep isl_schedule_node *node)
4249 isl_union_map *prefix, *universe, *umap1, *umap2;
4250 isl_union_set *domain, *range;
4251 isl_set *context;
4252 int after;
4254 umap = isl_union_map_copy(umap);
4255 context = isl_schedule_node_context_get_context(node);
4256 prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
4257 universe = isl_union_map_universe(isl_union_map_copy(umap));
4258 domain = isl_union_map_domain(isl_union_map_copy(universe));
4259 range = isl_union_map_range(universe);
4260 umap1 = isl_union_map_copy(prefix);
4261 umap1 = isl_union_map_intersect_domain(umap1, domain);
4262 umap2 = isl_union_map_intersect_domain(prefix, range);
4263 umap1 = isl_union_map_intersect_range(umap1,
4264 isl_union_set_from_set(context));
4265 umap1 = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4266 umap = isl_union_map_intersect(umap, umap1);
4268 after = after_in_child(umap, node);
4270 isl_union_map_free(umap);
4272 return after;
4275 /* Is any domain element of "umap" scheduled after any of
4276 * the corresponding image elements by the tree rooted at
4277 * the expansion node "node"?
4279 * We apply the expansion to domain and range of "umap" and
4280 * continue with its child.
4282 static int after_in_expansion(__isl_keep isl_union_map *umap,
4283 __isl_keep isl_schedule_node *node)
4285 isl_union_map *expansion;
4286 int after;
4288 expansion = isl_schedule_node_expansion_get_expansion(node);
4289 umap = isl_union_map_copy(umap);
4290 umap = isl_union_map_apply_domain(umap, isl_union_map_copy(expansion));
4291 umap = isl_union_map_apply_range(umap, expansion);
4293 after = after_in_child(umap, node);
4295 isl_union_map_free(umap);
4297 return after;
4300 /* Is any domain element of "umap" scheduled after any of
4301 * the corresponding image elements by the tree rooted at
4302 * the extension node "node"?
4304 * Since the extension node may add statement instances before or
4305 * after the pairs of statement instances in "umap", we return 1
4306 * to ensure that these pairs are not broken up.
4308 static int after_in_extension(__isl_keep isl_union_map *umap,
4309 __isl_keep isl_schedule_node *node)
4311 return 1;
4314 /* Is any domain element of "umap" scheduled after any of
4315 * the corresponding image elements by the tree rooted at
4316 * the filter node "node"?
4318 * We intersect domain and range of "umap" with the filter and
4319 * continue with its child.
4321 static int after_in_filter(__isl_keep isl_union_map *umap,
4322 __isl_keep isl_schedule_node *node)
4324 isl_union_set *filter;
4325 int after;
4327 umap = isl_union_map_copy(umap);
4328 filter = isl_schedule_node_filter_get_filter(node);
4329 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(filter));
4330 umap = isl_union_map_intersect_range(umap, filter);
4332 after = after_in_child(umap, node);
4334 isl_union_map_free(umap);
4336 return after;
4339 /* Is any domain element of "umap" scheduled after any of
4340 * the corresponding image elements by the tree rooted at
4341 * the set node "node"?
4343 * This is only the case if this condition holds in any
4344 * of the (filter) children of the set node.
4345 * In particular, if the domain and the range of "umap"
4346 * are contained in different children, then the condition
4347 * does not hold.
4349 static int after_in_set(__isl_keep isl_union_map *umap,
4350 __isl_keep isl_schedule_node *node)
4352 int i, n;
4354 n = isl_schedule_node_n_children(node);
4355 for (i = 0; i < n; ++i) {
4356 isl_schedule_node *child;
4357 int after;
4359 child = isl_schedule_node_get_child(node, i);
4360 after = after_in_tree(umap, child);
4361 isl_schedule_node_free(child);
4363 if (after < 0 || after)
4364 return after;
4367 return 0;
4370 /* Return the filter of child "i" of "node".
4372 static __isl_give isl_union_set *child_filter(
4373 __isl_keep isl_schedule_node *node, int i)
4375 isl_schedule_node *child;
4376 isl_union_set *filter;
4378 child = isl_schedule_node_get_child(node, i);
4379 filter = isl_schedule_node_filter_get_filter(child);
4380 isl_schedule_node_free(child);
4382 return filter;
4385 /* Is any domain element of "umap" scheduled after any of
4386 * the corresponding image elements by the tree rooted at
4387 * the sequence node "node"?
4389 * This happens in particular if any domain element is
4390 * contained in a later child than one containing a range element or
4391 * if the condition holds within a given child in the sequence.
4392 * The later part of the condition is checked by after_in_set.
4394 static int after_in_sequence(__isl_keep isl_union_map *umap,
4395 __isl_keep isl_schedule_node *node)
4397 int i, j, n;
4398 isl_union_map *umap_i;
4399 int empty, after = 0;
4401 n = isl_schedule_node_n_children(node);
4402 for (i = 1; i < n; ++i) {
4403 isl_union_set *filter_i;
4405 umap_i = isl_union_map_copy(umap);
4406 filter_i = child_filter(node, i);
4407 umap_i = isl_union_map_intersect_domain(umap_i, filter_i);
4408 empty = isl_union_map_is_empty(umap_i);
4409 if (empty < 0)
4410 goto error;
4411 if (empty) {
4412 isl_union_map_free(umap_i);
4413 continue;
4416 for (j = 0; j < i; ++j) {
4417 isl_union_set *filter_j;
4418 isl_union_map *umap_ij;
4420 umap_ij = isl_union_map_copy(umap_i);
4421 filter_j = child_filter(node, j);
4422 umap_ij = isl_union_map_intersect_range(umap_ij,
4423 filter_j);
4424 empty = isl_union_map_is_empty(umap_ij);
4425 isl_union_map_free(umap_ij);
4427 if (empty < 0)
4428 goto error;
4429 if (!empty)
4430 after = 1;
4431 if (after)
4432 break;
4435 isl_union_map_free(umap_i);
4436 if (after)
4437 break;
4440 if (after < 0 || after)
4441 return after;
4443 return after_in_set(umap, node);
4444 error:
4445 isl_union_map_free(umap_i);
4446 return -1;
4449 /* Is any domain element of "umap" scheduled after any of
4450 * the corresponding image elements by the tree rooted at "node"?
4452 * If "umap" is empty, then clearly there is no such element.
4453 * Otherwise, consider the different types of nodes separately.
4455 static int after_in_tree(__isl_keep isl_union_map *umap,
4456 __isl_keep isl_schedule_node *node)
4458 int empty;
4459 enum isl_schedule_node_type type;
4461 empty = isl_union_map_is_empty(umap);
4462 if (empty < 0)
4463 return -1;
4464 if (empty)
4465 return 0;
4466 if (!node)
4467 return -1;
4469 type = isl_schedule_node_get_type(node);
4470 switch (type) {
4471 case isl_schedule_node_error:
4472 return -1;
4473 case isl_schedule_node_leaf:
4474 return 0;
4475 case isl_schedule_node_band:
4476 return after_in_band(umap, node);
4477 case isl_schedule_node_domain:
4478 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
4479 "unexpected internal domain node", return -1);
4480 case isl_schedule_node_context:
4481 return after_in_context(umap, node);
4482 case isl_schedule_node_expansion:
4483 return after_in_expansion(umap, node);
4484 case isl_schedule_node_extension:
4485 return after_in_extension(umap, node);
4486 case isl_schedule_node_filter:
4487 return after_in_filter(umap, node);
4488 case isl_schedule_node_guard:
4489 case isl_schedule_node_mark:
4490 return after_in_child(umap, node);
4491 case isl_schedule_node_set:
4492 return after_in_set(umap, node);
4493 case isl_schedule_node_sequence:
4494 return after_in_sequence(umap, node);
4497 return 1;
4500 /* Is any domain element of "map1" scheduled after any domain
4501 * element of "map2" by the subtree underneath the current band node,
4502 * while at the same time being scheduled together by the current
4503 * band node, i.e., by "map1" and "map2?
4505 * If the child of the current band node is a leaf, then
4506 * no element can be scheduled after any other element.
4508 * Otherwise, we construct a relation between domain elements
4509 * of "map1" and domain elements of "map2" that are scheduled
4510 * together and then check if the subtree underneath the current
4511 * band node determines their relative order.
4513 static int after_in_subtree(__isl_keep isl_ast_build *build,
4514 __isl_keep isl_map *map1, __isl_keep isl_map *map2)
4516 isl_schedule_node *node;
4517 isl_map *map;
4518 isl_union_map *umap;
4519 int after;
4521 node = isl_ast_build_get_schedule_node(build);
4522 if (!node)
4523 return -1;
4524 node = isl_schedule_node_child(node, 0);
4525 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
4526 isl_schedule_node_free(node);
4527 return 0;
4529 map = isl_map_copy(map2);
4530 map = isl_map_apply_domain(map, isl_map_copy(map1));
4531 umap = isl_union_map_from_map(map);
4532 after = after_in_tree(umap, node);
4533 isl_union_map_free(umap);
4534 isl_schedule_node_free(node);
4535 return after;
4538 /* Internal data for any_scheduled_after.
4540 * "build" is the build in which the AST is constructed.
4541 * "depth" is the number of loops that have already been generated
4542 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
4543 * "domain" is an array of set-map pairs corresponding to the different
4544 * iteration domains. The set is the schedule domain, i.e., the domain
4545 * of the inverse schedule, while the map is the inverse schedule itself.
4547 struct isl_any_scheduled_after_data {
4548 isl_ast_build *build;
4549 int depth;
4550 int group_coscheduled;
4551 struct isl_set_map_pair *domain;
4554 /* Is any element of domain "i" scheduled after any element of domain "j"
4555 * (for a common iteration of the first data->depth loops)?
4557 * data->domain[i].set contains the domain of the inverse schedule
4558 * for domain "i", i.e., elements in the schedule domain.
4560 * If we are inside a band of a schedule tree and there is a pair
4561 * of elements in the two domains that is schedule together by
4562 * the current band, then we check if any element of "i" may be schedule
4563 * after element of "j" by the descendants of the band node.
4565 * If data->group_coscheduled is set, then we also return 1 if there
4566 * is any pair of elements in the two domains that are scheduled together.
4568 static isl_bool any_scheduled_after(int i, int j, void *user)
4570 struct isl_any_scheduled_after_data *data = user;
4571 int dim = isl_set_dim(data->domain[i].set, isl_dim_set);
4572 int pos;
4574 for (pos = data->depth; pos < dim; ++pos) {
4575 int follows;
4577 follows = isl_set_follows_at(data->domain[i].set,
4578 data->domain[j].set, pos);
4580 if (follows < -1)
4581 return isl_bool_error;
4582 if (follows > 0)
4583 return isl_bool_true;
4584 if (follows < 0)
4585 return isl_bool_false;
4588 if (isl_ast_build_has_schedule_node(data->build)) {
4589 int after;
4591 after = after_in_subtree(data->build, data->domain[i].map,
4592 data->domain[j].map);
4593 if (after < 0 || after)
4594 return after;
4597 return data->group_coscheduled;
4600 /* Look for independent components at the current depth and generate code
4601 * for each component separately. The resulting lists of grafts are
4602 * merged in an attempt to combine grafts with identical guards.
4604 * Code for two domains can be generated separately if all the elements
4605 * of one domain are scheduled before (or together with) all the elements
4606 * of the other domain. We therefore consider the graph with as nodes
4607 * the domains and an edge between two nodes if any element of the first
4608 * node is scheduled after any element of the second node.
4609 * If the ast_build_group_coscheduled is set, then we also add an edge if
4610 * there is any pair of elements in the two domains that are scheduled
4611 * together.
4612 * Code is then generated (by generate_component)
4613 * for each of the strongly connected components in this graph
4614 * in their topological order.
4616 * Since the test is performed on the domain of the inverse schedules of
4617 * the different domains, we precompute these domains and store
4618 * them in data.domain.
4620 static __isl_give isl_ast_graft_list *generate_components(
4621 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4623 int i;
4624 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4625 int n = isl_union_map_n_map(executed);
4626 struct isl_any_scheduled_after_data data;
4627 struct isl_set_map_pair *next;
4628 struct isl_tarjan_graph *g = NULL;
4629 isl_ast_graft_list *list = NULL;
4630 int n_domain = 0;
4632 data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
4633 if (!data.domain)
4634 goto error;
4635 n_domain = n;
4637 next = data.domain;
4638 if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
4639 goto error;
4641 if (!build)
4642 goto error;
4643 data.build = build;
4644 data.depth = isl_ast_build_get_depth(build);
4645 data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
4646 g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
4647 if (!g)
4648 goto error;
4650 list = isl_ast_graft_list_alloc(ctx, 0);
4652 i = 0;
4653 while (list && n) {
4654 isl_ast_graft_list *list_c;
4655 int first = i;
4657 if (g->order[i] == -1)
4658 isl_die(ctx, isl_error_internal, "cannot happen",
4659 goto error);
4660 ++i; --n;
4661 while (g->order[i] != -1) {
4662 ++i; --n;
4665 list_c = generate_component(data.domain,
4666 g->order + first, i - first,
4667 isl_ast_build_copy(build));
4668 list = isl_ast_graft_list_merge(list, list_c, build);
4670 ++i;
4673 if (0)
4674 error: list = isl_ast_graft_list_free(list);
4675 isl_tarjan_graph_free(g);
4676 for (i = 0; i < n_domain; ++i) {
4677 isl_map_free(data.domain[i].map);
4678 isl_set_free(data.domain[i].set);
4680 free(data.domain);
4681 isl_union_map_free(executed);
4682 isl_ast_build_free(build);
4684 return list;
4687 /* Generate code for the next level (and all inner levels).
4689 * If "executed" is empty, i.e., no code needs to be generated,
4690 * then we return an empty list.
4692 * If we have already generated code for all loop levels, then we pass
4693 * control to generate_inner_level.
4695 * If "executed" lives in a single space, i.e., if code needs to be
4696 * generated for a single domain, then there can only be a single
4697 * component and we go directly to generate_shifted_component.
4698 * Otherwise, we call generate_components to detect the components
4699 * and to call generate_component on each of them separately.
4701 static __isl_give isl_ast_graft_list *generate_next_level(
4702 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4704 int depth;
4706 if (!build || !executed)
4707 goto error;
4709 if (isl_union_map_is_empty(executed)) {
4710 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4711 isl_union_map_free(executed);
4712 isl_ast_build_free(build);
4713 return isl_ast_graft_list_alloc(ctx, 0);
4716 depth = isl_ast_build_get_depth(build);
4717 if (depth >= isl_ast_build_dim(build, isl_dim_set))
4718 return generate_inner_level(executed, build);
4720 if (isl_union_map_n_map(executed) == 1)
4721 return generate_shifted_component(executed, build);
4723 return generate_components(executed, build);
4724 error:
4725 isl_union_map_free(executed);
4726 isl_ast_build_free(build);
4727 return NULL;
4730 /* Internal data structure used by isl_ast_build_node_from_schedule_map.
4731 * internal, executed and build are the inputs to generate_code.
4732 * list collects the output.
4734 struct isl_generate_code_data {
4735 int internal;
4736 isl_union_map *executed;
4737 isl_ast_build *build;
4739 isl_ast_graft_list *list;
4742 /* Given an inverse schedule in terms of the external build schedule, i.e.,
4744 * [E -> S] -> D
4746 * with E the external build schedule and S the additional schedule "space",
4747 * reformulate the inverse schedule in terms of the internal schedule domain,
4748 * i.e., return
4750 * [I -> S] -> D
4752 * We first obtain a mapping
4754 * I -> E
4756 * take the inverse and the product with S -> S, resulting in
4758 * [I -> S] -> [E -> S]
4760 * Applying the map to the input produces the desired result.
4762 static __isl_give isl_union_map *internal_executed(
4763 __isl_take isl_union_map *executed, __isl_keep isl_space *space,
4764 __isl_keep isl_ast_build *build)
4766 isl_map *id, *proj;
4768 proj = isl_ast_build_get_schedule_map(build);
4769 proj = isl_map_reverse(proj);
4770 space = isl_space_map_from_set(isl_space_copy(space));
4771 id = isl_map_identity(space);
4772 proj = isl_map_product(proj, id);
4773 executed = isl_union_map_apply_domain(executed,
4774 isl_union_map_from_map(proj));
4775 return executed;
4778 /* Generate an AST that visits the elements in the range of data->executed
4779 * in the relative order specified by the corresponding domain element(s)
4780 * for those domain elements that belong to "set".
4781 * Add the result to data->list.
4783 * The caller ensures that "set" is a universe domain.
4784 * "space" is the space of the additional part of the schedule.
4785 * It is equal to the space of "set" if build->domain is parametric.
4786 * Otherwise, it is equal to the range of the wrapped space of "set".
4788 * If the build space is not parametric and
4789 * if isl_ast_build_node_from_schedule_map
4790 * was called from an outside user (data->internal not set), then
4791 * the (inverse) schedule refers to the external build domain and needs to
4792 * be transformed to refer to the internal build domain.
4794 * If the build space is parametric, then we add some of the parameter
4795 * constraints to the executed relation. Adding these constraints
4796 * allows for an earlier detection of conflicts in some cases.
4797 * However, we do not want to divide the executed relation into
4798 * more disjuncts than necessary. We therefore approximate
4799 * the constraints on the parameters by a single disjunct set.
4801 * The build is extended to include the additional part of the schedule.
4802 * If the original build space was not parametric, then the options
4803 * in data->build refer only to the additional part of the schedule
4804 * and they need to be adjusted to refer to the complete AST build
4805 * domain.
4807 * After having adjusted inverse schedule and build, we start generating
4808 * code with the outer loop of the current code generation
4809 * in generate_next_level.
4811 * If the original build space was not parametric, we undo the embedding
4812 * on the resulting isl_ast_node_list so that it can be used within
4813 * the outer AST build.
4815 static isl_stat generate_code_in_space(struct isl_generate_code_data *data,
4816 __isl_take isl_set *set, __isl_take isl_space *space)
4818 isl_union_map *executed;
4819 isl_ast_build *build;
4820 isl_ast_graft_list *list;
4821 int embed;
4823 executed = isl_union_map_copy(data->executed);
4824 executed = isl_union_map_intersect_domain(executed,
4825 isl_union_set_from_set(set));
4827 embed = !isl_set_is_params(data->build->domain);
4828 if (embed && !data->internal)
4829 executed = internal_executed(executed, space, data->build);
4830 if (!embed) {
4831 isl_set *domain;
4832 domain = isl_ast_build_get_domain(data->build);
4833 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
4834 executed = isl_union_map_intersect_params(executed, domain);
4837 build = isl_ast_build_copy(data->build);
4838 build = isl_ast_build_product(build, space);
4840 list = generate_next_level(executed, build);
4842 list = isl_ast_graft_list_unembed(list, embed);
4844 data->list = isl_ast_graft_list_concat(data->list, list);
4846 return isl_stat_ok;
4849 /* Generate an AST that visits the elements in the range of data->executed
4850 * in the relative order specified by the corresponding domain element(s)
4851 * for those domain elements that belong to "set".
4852 * Add the result to data->list.
4854 * The caller ensures that "set" is a universe domain.
4856 * If the build space S is not parametric, then the space of "set"
4857 * need to be a wrapped relation with S as domain. That is, it needs
4858 * to be of the form
4860 * [S -> T]
4862 * Check this property and pass control to generate_code_in_space
4863 * passing along T.
4864 * If the build space is not parametric, then T is the space of "set".
4866 static isl_stat generate_code_set(__isl_take isl_set *set, void *user)
4868 struct isl_generate_code_data *data = user;
4869 isl_space *space, *build_space;
4870 int is_domain;
4872 space = isl_set_get_space(set);
4874 if (isl_set_is_params(data->build->domain))
4875 return generate_code_in_space(data, set, space);
4877 build_space = isl_ast_build_get_space(data->build, data->internal);
4878 space = isl_space_unwrap(space);
4879 is_domain = isl_space_is_domain(build_space, space);
4880 isl_space_free(build_space);
4881 space = isl_space_range(space);
4883 if (is_domain < 0)
4884 goto error;
4885 if (!is_domain)
4886 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4887 "invalid nested schedule space", goto error);
4889 return generate_code_in_space(data, set, space);
4890 error:
4891 isl_set_free(set);
4892 isl_space_free(space);
4893 return isl_stat_error;
4896 /* Generate an AST that visits the elements in the range of "executed"
4897 * in the relative order specified by the corresponding domain element(s).
4899 * "build" is an isl_ast_build that has either been constructed by
4900 * isl_ast_build_from_context or passed to a callback set by
4901 * isl_ast_build_set_create_leaf.
4902 * In the first case, the space of the isl_ast_build is typically
4903 * a parametric space, although this is currently not enforced.
4904 * In the second case, the space is never a parametric space.
4905 * If the space S is not parametric, then the domain space(s) of "executed"
4906 * need to be wrapped relations with S as domain.
4908 * If the domain of "executed" consists of several spaces, then an AST
4909 * is generated for each of them (in arbitrary order) and the results
4910 * are concatenated.
4912 * If "internal" is set, then the domain "S" above refers to the internal
4913 * schedule domain representation. Otherwise, it refers to the external
4914 * representation, as returned by isl_ast_build_get_schedule_space.
4916 * We essentially run over all the spaces in the domain of "executed"
4917 * and call generate_code_set on each of them.
4919 static __isl_give isl_ast_graft_list *generate_code(
4920 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
4921 int internal)
4923 isl_ctx *ctx;
4924 struct isl_generate_code_data data = { 0 };
4925 isl_space *space;
4926 isl_union_set *schedule_domain;
4927 isl_union_map *universe;
4929 if (!build)
4930 goto error;
4931 space = isl_ast_build_get_space(build, 1);
4932 space = isl_space_align_params(space,
4933 isl_union_map_get_space(executed));
4934 space = isl_space_align_params(space,
4935 isl_union_map_get_space(build->options));
4936 build = isl_ast_build_align_params(build, isl_space_copy(space));
4937 executed = isl_union_map_align_params(executed, space);
4938 if (!executed || !build)
4939 goto error;
4941 ctx = isl_ast_build_get_ctx(build);
4943 data.internal = internal;
4944 data.executed = executed;
4945 data.build = build;
4946 data.list = isl_ast_graft_list_alloc(ctx, 0);
4948 universe = isl_union_map_universe(isl_union_map_copy(executed));
4949 schedule_domain = isl_union_map_domain(universe);
4950 if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
4951 &data) < 0)
4952 data.list = isl_ast_graft_list_free(data.list);
4954 isl_union_set_free(schedule_domain);
4955 isl_union_map_free(executed);
4957 isl_ast_build_free(build);
4958 return data.list;
4959 error:
4960 isl_union_map_free(executed);
4961 isl_ast_build_free(build);
4962 return NULL;
4965 /* Generate an AST that visits the elements in the domain of "schedule"
4966 * in the relative order specified by the corresponding image element(s).
4968 * "build" is an isl_ast_build that has either been constructed by
4969 * isl_ast_build_from_context or passed to a callback set by
4970 * isl_ast_build_set_create_leaf.
4971 * In the first case, the space of the isl_ast_build is typically
4972 * a parametric space, although this is currently not enforced.
4973 * In the second case, the space is never a parametric space.
4974 * If the space S is not parametric, then the range space(s) of "schedule"
4975 * need to be wrapped relations with S as domain.
4977 * If the range of "schedule" consists of several spaces, then an AST
4978 * is generated for each of them (in arbitrary order) and the results
4979 * are concatenated.
4981 * We first initialize the local copies of the relevant options.
4982 * We do this here rather than when the isl_ast_build is created
4983 * because the options may have changed between the construction
4984 * of the isl_ast_build and the call to isl_generate_code.
4986 * The main computation is performed on an inverse schedule (with
4987 * the schedule domain in the domain and the elements to be executed
4988 * in the range) called "executed".
4990 __isl_give isl_ast_node *isl_ast_build_node_from_schedule_map(
4991 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
4993 isl_ast_graft_list *list;
4994 isl_ast_node *node;
4995 isl_union_map *executed;
4997 build = isl_ast_build_copy(build);
4998 build = isl_ast_build_set_single_valued(build, 0);
4999 schedule = isl_union_map_coalesce(schedule);
5000 schedule = isl_union_map_remove_redundancies(schedule);
5001 executed = isl_union_map_reverse(schedule);
5002 list = generate_code(executed, isl_ast_build_copy(build), 0);
5003 node = isl_ast_node_from_graft_list(list, build);
5004 isl_ast_build_free(build);
5006 return node;
5009 /* The old name for isl_ast_build_node_from_schedule_map.
5010 * It is being kept for backward compatibility, but
5011 * it will be removed in the future.
5013 __isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
5014 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5016 return isl_ast_build_node_from_schedule_map(build, schedule);
5019 /* Generate an AST that visits the elements in the domain of "executed"
5020 * in the relative order specified by the band node "node" and its descendants.
5022 * The relation "executed" maps the outer generated loop iterators
5023 * to the domain elements executed by those iterations.
5025 * If the band is empty, we continue with its descendants.
5026 * Otherwise, we extend the build and the inverse schedule with
5027 * the additional space/partial schedule and continue generating
5028 * an AST in generate_next_level.
5029 * As soon as we have extended the inverse schedule with the additional
5030 * partial schedule, we look for equalities that may exists between
5031 * the old and the new part.
5033 static __isl_give isl_ast_graft_list *build_ast_from_band(
5034 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5035 __isl_take isl_union_map *executed)
5037 isl_space *space;
5038 isl_multi_union_pw_aff *extra;
5039 isl_union_map *extra_umap;
5040 isl_ast_graft_list *list;
5041 unsigned n1, n2;
5043 if (!build || !node || !executed)
5044 goto error;
5046 if (isl_schedule_node_band_n_member(node) == 0)
5047 return build_ast_from_child(build, node, executed);
5049 extra = isl_schedule_node_band_get_partial_schedule(node);
5050 extra = isl_multi_union_pw_aff_align_params(extra,
5051 isl_ast_build_get_space(build, 1));
5052 space = isl_multi_union_pw_aff_get_space(extra);
5054 extra_umap = isl_union_map_from_multi_union_pw_aff(extra);
5055 extra_umap = isl_union_map_reverse(extra_umap);
5057 executed = isl_union_map_domain_product(executed, extra_umap);
5058 executed = isl_union_map_detect_equalities(executed);
5060 n1 = isl_ast_build_dim(build, isl_dim_param);
5061 build = isl_ast_build_product(build, space);
5062 n2 = isl_ast_build_dim(build, isl_dim_param);
5063 if (n2 > n1)
5064 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5065 "band node is not allowed to introduce new parameters",
5066 build = isl_ast_build_free(build));
5067 build = isl_ast_build_set_schedule_node(build, node);
5069 list = generate_next_level(executed, build);
5071 list = isl_ast_graft_list_unembed(list, 1);
5073 return list;
5074 error:
5075 isl_schedule_node_free(node);
5076 isl_union_map_free(executed);
5077 isl_ast_build_free(build);
5078 return NULL;
5081 /* Hoist a list of grafts (in practice containing a single graft)
5082 * from "sub_build" (which includes extra context information)
5083 * to "build".
5085 * In particular, project out all additional parameters introduced
5086 * by the context node from the enforced constraints and the guard
5087 * of the single graft.
5089 static __isl_give isl_ast_graft_list *hoist_out_of_context(
5090 __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build,
5091 __isl_keep isl_ast_build *sub_build)
5093 isl_ast_graft *graft;
5094 isl_basic_set *enforced;
5095 isl_set *guard;
5096 unsigned n_param, extra_param;
5098 if (!build || !sub_build)
5099 return isl_ast_graft_list_free(list);
5101 n_param = isl_ast_build_dim(build, isl_dim_param);
5102 extra_param = isl_ast_build_dim(sub_build, isl_dim_param);
5104 if (extra_param == n_param)
5105 return list;
5107 extra_param -= n_param;
5108 enforced = isl_ast_graft_list_extract_shared_enforced(list, sub_build);
5109 enforced = isl_basic_set_project_out(enforced, isl_dim_param,
5110 n_param, extra_param);
5111 enforced = isl_basic_set_remove_unknown_divs(enforced);
5112 guard = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5113 guard = isl_set_remove_divs_involving_dims(guard, isl_dim_param,
5114 n_param, extra_param);
5115 guard = isl_set_project_out(guard, isl_dim_param, n_param, extra_param);
5116 guard = isl_set_compute_divs(guard);
5117 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5118 build, sub_build);
5119 list = isl_ast_graft_list_from_ast_graft(graft);
5121 return list;
5124 /* Generate an AST that visits the elements in the domain of "executed"
5125 * in the relative order specified by the context node "node"
5126 * and its descendants.
5128 * The relation "executed" maps the outer generated loop iterators
5129 * to the domain elements executed by those iterations.
5131 * The context node may introduce additional parameters as well as
5132 * constraints on the outer schedule dimenions or original parameters.
5134 * We add the extra parameters to a new build and the context
5135 * constraints to both the build and (as a single disjunct)
5136 * to the domain of "executed". Since the context constraints
5137 * are specified in terms of the input schedule, we first need
5138 * to map them to the internal schedule domain.
5140 * After constructing the AST from the descendants of "node",
5141 * we combine the list of grafts into a single graft within
5142 * the new build, in order to be able to exploit the additional
5143 * context constraints during this combination.
5145 * Additionally, if the current node is the outermost node in
5146 * the schedule tree (apart from the root domain node), we generate
5147 * all pending guards, again to be able to exploit the additional
5148 * context constraints. We currently do not do this for internal
5149 * context nodes since we may still want to hoist conditions
5150 * to outer AST nodes.
5152 * If the context node introduced any new parameters, then they
5153 * are removed from the set of enforced constraints and guard
5154 * in hoist_out_of_context.
5156 static __isl_give isl_ast_graft_list *build_ast_from_context(
5157 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5158 __isl_take isl_union_map *executed)
5160 isl_set *context;
5161 isl_space *space;
5162 isl_multi_aff *internal2input;
5163 isl_ast_build *sub_build;
5164 isl_ast_graft_list *list;
5165 int n, depth;
5167 depth = isl_schedule_node_get_tree_depth(node);
5168 space = isl_ast_build_get_space(build, 1);
5169 context = isl_schedule_node_context_get_context(node);
5170 context = isl_set_align_params(context, space);
5171 sub_build = isl_ast_build_copy(build);
5172 space = isl_set_get_space(context);
5173 sub_build = isl_ast_build_align_params(sub_build, space);
5174 internal2input = isl_ast_build_get_internal2input(sub_build);
5175 context = isl_set_preimage_multi_aff(context, internal2input);
5176 sub_build = isl_ast_build_restrict_generated(sub_build,
5177 isl_set_copy(context));
5178 context = isl_set_from_basic_set(isl_set_simple_hull(context));
5179 executed = isl_union_map_intersect_domain(executed,
5180 isl_union_set_from_set(context));
5182 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5183 node, executed);
5184 n = isl_ast_graft_list_n_ast_graft(list);
5185 if (n < 0)
5186 list = isl_ast_graft_list_free(list);
5188 list = isl_ast_graft_list_fuse(list, sub_build);
5189 if (depth == 1)
5190 list = isl_ast_graft_list_insert_pending_guard_nodes(list,
5191 sub_build);
5192 if (n >= 1)
5193 list = hoist_out_of_context(list, build, sub_build);
5195 isl_ast_build_free(build);
5196 isl_ast_build_free(sub_build);
5198 return list;
5201 /* Generate an AST that visits the elements in the domain of "executed"
5202 * in the relative order specified by the expansion node "node" and
5203 * its descendants.
5205 * The relation "executed" maps the outer generated loop iterators
5206 * to the domain elements executed by those iterations.
5208 * We expand the domain elements by the expansion and
5209 * continue with the descendants of the node.
5211 static __isl_give isl_ast_graft_list *build_ast_from_expansion(
5212 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5213 __isl_take isl_union_map *executed)
5215 isl_union_map *expansion;
5216 unsigned n1, n2;
5218 expansion = isl_schedule_node_expansion_get_expansion(node);
5219 expansion = isl_union_map_align_params(expansion,
5220 isl_union_map_get_space(executed));
5222 n1 = isl_union_map_dim(executed, isl_dim_param);
5223 executed = isl_union_map_apply_range(executed, expansion);
5224 n2 = isl_union_map_dim(executed, isl_dim_param);
5225 if (n2 > n1)
5226 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5227 "expansion node is not allowed to introduce "
5228 "new parameters", goto error);
5230 return build_ast_from_child(build, node, executed);
5231 error:
5232 isl_ast_build_free(build);
5233 isl_schedule_node_free(node);
5234 isl_union_map_free(executed);
5235 return NULL;
5238 /* Generate an AST that visits the elements in the domain of "executed"
5239 * in the relative order specified by the extension node "node" and
5240 * its descendants.
5242 * The relation "executed" maps the outer generated loop iterators
5243 * to the domain elements executed by those iterations.
5245 * Extend the inverse schedule with the extension applied to current
5246 * set of generated constraints. Since the extension if formulated
5247 * in terms of the input schedule, it first needs to be transformed
5248 * to refer to the internal schedule.
5250 static __isl_give isl_ast_graft_list *build_ast_from_extension(
5251 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5252 __isl_take isl_union_map *executed)
5254 isl_union_set *schedule_domain;
5255 isl_union_map *extension;
5256 isl_set *set;
5258 set = isl_ast_build_get_generated(build);
5259 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5260 schedule_domain = isl_union_set_from_set(set);
5262 extension = isl_schedule_node_extension_get_extension(node);
5264 extension = isl_union_map_preimage_domain_multi_aff(extension,
5265 isl_multi_aff_copy(build->internal2input));
5266 extension = isl_union_map_intersect_domain(extension, schedule_domain);
5267 extension = isl_ast_build_substitute_values_union_map_domain(build,
5268 extension);
5269 executed = isl_union_map_union(executed, extension);
5271 return build_ast_from_child(build, node, executed);
5274 /* Generate an AST that visits the elements in the domain of "executed"
5275 * in the relative order specified by the filter node "node" and
5276 * its descendants.
5278 * The relation "executed" maps the outer generated loop iterators
5279 * to the domain elements executed by those iterations.
5281 * We simply intersect the iteration domain (i.e., the range of "executed")
5282 * with the filter and continue with the descendants of the node,
5283 * unless the resulting inverse schedule is empty, in which
5284 * case we return an empty list.
5286 * If the result of the intersection is equal to the original "executed"
5287 * relation, then keep the original representation since the intersection
5288 * may have unnecessarily broken up the relation into a greater number
5289 * of disjuncts.
5291 static __isl_give isl_ast_graft_list *build_ast_from_filter(
5292 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5293 __isl_take isl_union_map *executed)
5295 isl_ctx *ctx;
5296 isl_union_set *filter;
5297 isl_union_map *orig;
5298 isl_ast_graft_list *list;
5299 int empty;
5300 isl_bool unchanged;
5301 unsigned n1, n2;
5303 orig = isl_union_map_copy(executed);
5304 if (!build || !node || !executed)
5305 goto error;
5307 filter = isl_schedule_node_filter_get_filter(node);
5308 filter = isl_union_set_align_params(filter,
5309 isl_union_map_get_space(executed));
5310 n1 = isl_union_map_dim(executed, isl_dim_param);
5311 executed = isl_union_map_intersect_range(executed, filter);
5312 n2 = isl_union_map_dim(executed, isl_dim_param);
5313 if (n2 > n1)
5314 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5315 "filter node is not allowed to introduce "
5316 "new parameters", goto error);
5318 unchanged = isl_union_map_is_subset(orig, executed);
5319 empty = isl_union_map_is_empty(executed);
5320 if (unchanged < 0 || empty < 0)
5321 goto error;
5322 if (unchanged) {
5323 isl_union_map_free(executed);
5324 return build_ast_from_child(build, node, orig);
5326 isl_union_map_free(orig);
5327 if (!empty)
5328 return build_ast_from_child(build, node, executed);
5330 ctx = isl_ast_build_get_ctx(build);
5331 list = isl_ast_graft_list_alloc(ctx, 0);
5332 isl_ast_build_free(build);
5333 isl_schedule_node_free(node);
5334 isl_union_map_free(executed);
5335 return list;
5336 error:
5337 isl_ast_build_free(build);
5338 isl_schedule_node_free(node);
5339 isl_union_map_free(executed);
5340 isl_union_map_free(orig);
5341 return NULL;
5344 /* Generate an AST that visits the elements in the domain of "executed"
5345 * in the relative order specified by the guard node "node" and
5346 * its descendants.
5348 * The relation "executed" maps the outer generated loop iterators
5349 * to the domain elements executed by those iterations.
5351 * Ensure that the associated guard is enforced by the outer AST
5352 * constructs by adding it to the guard of the graft.
5353 * Since we know that we will enforce the guard, we can also include it
5354 * in the generated constraints used to construct an AST for
5355 * the descendant nodes.
5357 static __isl_give isl_ast_graft_list *build_ast_from_guard(
5358 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5359 __isl_take isl_union_map *executed)
5361 isl_space *space;
5362 isl_set *guard, *hoisted;
5363 isl_basic_set *enforced;
5364 isl_ast_build *sub_build;
5365 isl_ast_graft *graft;
5366 isl_ast_graft_list *list;
5367 unsigned n1, n2;
5369 space = isl_ast_build_get_space(build, 1);
5370 guard = isl_schedule_node_guard_get_guard(node);
5371 n1 = isl_space_dim(space, isl_dim_param);
5372 guard = isl_set_align_params(guard, space);
5373 n2 = isl_set_dim(guard, isl_dim_param);
5374 if (n2 > n1)
5375 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5376 "guard node is not allowed to introduce "
5377 "new parameters", guard = isl_set_free(guard));
5378 guard = isl_set_preimage_multi_aff(guard,
5379 isl_multi_aff_copy(build->internal2input));
5380 guard = isl_ast_build_specialize(build, guard);
5381 guard = isl_set_gist(guard, isl_set_copy(build->generated));
5383 sub_build = isl_ast_build_copy(build);
5384 sub_build = isl_ast_build_restrict_generated(sub_build,
5385 isl_set_copy(guard));
5387 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5388 node, executed);
5390 hoisted = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5391 if (isl_set_n_basic_set(hoisted) > 1)
5392 list = isl_ast_graft_list_gist_guards(list,
5393 isl_set_copy(hoisted));
5394 guard = isl_set_intersect(guard, hoisted);
5395 enforced = extract_shared_enforced(list, build);
5396 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5397 build, sub_build);
5399 isl_ast_build_free(sub_build);
5400 isl_ast_build_free(build);
5401 return isl_ast_graft_list_from_ast_graft(graft);
5404 /* Call the before_each_mark callback, if requested by the user.
5406 * Return 0 on success and -1 on error.
5408 * The caller is responsible for recording the current inverse schedule
5409 * in "build".
5411 static isl_stat before_each_mark(__isl_keep isl_id *mark,
5412 __isl_keep isl_ast_build *build)
5414 if (!build)
5415 return isl_stat_error;
5416 if (!build->before_each_mark)
5417 return isl_stat_ok;
5418 return build->before_each_mark(mark, build,
5419 build->before_each_mark_user);
5422 /* Call the after_each_mark callback, if requested by the user.
5424 * The caller is responsible for recording the current inverse schedule
5425 * in "build".
5427 static __isl_give isl_ast_graft *after_each_mark(
5428 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
5430 if (!graft || !build)
5431 return isl_ast_graft_free(graft);
5432 if (!build->after_each_mark)
5433 return graft;
5434 graft->node = build->after_each_mark(graft->node, build,
5435 build->after_each_mark_user);
5436 if (!graft->node)
5437 return isl_ast_graft_free(graft);
5438 return graft;
5442 /* Generate an AST that visits the elements in the domain of "executed"
5443 * in the relative order specified by the mark node "node" and
5444 * its descendants.
5446 * The relation "executed" maps the outer generated loop iterators
5447 * to the domain elements executed by those iterations.
5449 * Since we may be calling before_each_mark and after_each_mark
5450 * callbacks, we record the current inverse schedule in the build.
5452 * We generate an AST for the child of the mark node, combine
5453 * the graft list into a single graft and then insert the mark
5454 * in the AST of that single graft.
5456 static __isl_give isl_ast_graft_list *build_ast_from_mark(
5457 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5458 __isl_take isl_union_map *executed)
5460 isl_id *mark;
5461 isl_ast_graft *graft;
5462 isl_ast_graft_list *list;
5463 int n;
5465 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
5467 mark = isl_schedule_node_mark_get_id(node);
5468 if (before_each_mark(mark, build) < 0)
5469 node = isl_schedule_node_free(node);
5471 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5472 list = isl_ast_graft_list_fuse(list, build);
5473 n = isl_ast_graft_list_n_ast_graft(list);
5474 if (n < 0)
5475 list = isl_ast_graft_list_free(list);
5476 if (n == 0) {
5477 isl_id_free(mark);
5478 } else {
5479 graft = isl_ast_graft_list_get_ast_graft(list, 0);
5480 graft = isl_ast_graft_insert_mark(graft, mark);
5481 graft = after_each_mark(graft, build);
5482 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
5484 isl_ast_build_free(build);
5486 return list;
5489 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5490 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5491 __isl_take isl_union_map *executed);
5493 /* Generate an AST that visits the elements in the domain of "executed"
5494 * in the relative order specified by the sequence (or set) node "node" and
5495 * its descendants.
5497 * The relation "executed" maps the outer generated loop iterators
5498 * to the domain elements executed by those iterations.
5500 * We simply generate an AST for each of the children and concatenate
5501 * the results.
5503 static __isl_give isl_ast_graft_list *build_ast_from_sequence(
5504 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5505 __isl_take isl_union_map *executed)
5507 int i, n;
5508 isl_ctx *ctx;
5509 isl_ast_graft_list *list;
5511 ctx = isl_ast_build_get_ctx(build);
5512 list = isl_ast_graft_list_alloc(ctx, 0);
5514 n = isl_schedule_node_n_children(node);
5515 for (i = 0; i < n; ++i) {
5516 isl_schedule_node *child;
5517 isl_ast_graft_list *list_i;
5519 child = isl_schedule_node_get_child(node, i);
5520 list_i = build_ast_from_schedule_node(isl_ast_build_copy(build),
5521 child, isl_union_map_copy(executed));
5522 list = isl_ast_graft_list_concat(list, list_i);
5524 isl_ast_build_free(build);
5525 isl_schedule_node_free(node);
5526 isl_union_map_free(executed);
5528 return list;
5531 /* Generate an AST that visits the elements in the domain of "executed"
5532 * in the relative order specified by the node "node" and its descendants.
5534 * The relation "executed" maps the outer generated loop iterators
5535 * to the domain elements executed by those iterations.
5537 * If the node is a leaf, then we pass control to generate_inner_level.
5538 * Note that the current build does not refer to any band node, so
5539 * that generate_inner_level will not try to visit the child of
5540 * the leaf node.
5542 * The other node types are handled in separate functions.
5543 * Set nodes are currently treated in the same way as sequence nodes.
5544 * The children of a set node may be executed in any order,
5545 * including the order of the children.
5547 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5548 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5549 __isl_take isl_union_map *executed)
5551 enum isl_schedule_node_type type;
5553 type = isl_schedule_node_get_type(node);
5555 switch (type) {
5556 case isl_schedule_node_error:
5557 goto error;
5558 case isl_schedule_node_leaf:
5559 isl_schedule_node_free(node);
5560 return generate_inner_level(executed, build);
5561 case isl_schedule_node_band:
5562 return build_ast_from_band(build, node, executed);
5563 case isl_schedule_node_context:
5564 return build_ast_from_context(build, node, executed);
5565 case isl_schedule_node_domain:
5566 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
5567 "unexpected internal domain node", goto error);
5568 case isl_schedule_node_expansion:
5569 return build_ast_from_expansion(build, node, executed);
5570 case isl_schedule_node_extension:
5571 return build_ast_from_extension(build, node, executed);
5572 case isl_schedule_node_filter:
5573 return build_ast_from_filter(build, node, executed);
5574 case isl_schedule_node_guard:
5575 return build_ast_from_guard(build, node, executed);
5576 case isl_schedule_node_mark:
5577 return build_ast_from_mark(build, node, executed);
5578 case isl_schedule_node_sequence:
5579 case isl_schedule_node_set:
5580 return build_ast_from_sequence(build, node, executed);
5583 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
5584 "unhandled type", goto error);
5585 error:
5586 isl_union_map_free(executed);
5587 isl_schedule_node_free(node);
5588 isl_ast_build_free(build);
5590 return NULL;
5593 /* Generate an AST that visits the elements in the domain of "executed"
5594 * in the relative order specified by the (single) child of "node" and
5595 * its descendants.
5597 * The relation "executed" maps the outer generated loop iterators
5598 * to the domain elements executed by those iterations.
5600 * This function is never called on a leaf, set or sequence node,
5601 * so the node always has exactly one child.
5603 static __isl_give isl_ast_graft_list *build_ast_from_child(
5604 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5605 __isl_take isl_union_map *executed)
5607 node = isl_schedule_node_child(node, 0);
5608 return build_ast_from_schedule_node(build, node, executed);
5611 /* Generate an AST that visits the elements in the domain of the domain
5612 * node "node" in the relative order specified by its descendants.
5614 * An initial inverse schedule is created that maps a zero-dimensional
5615 * schedule space to the node domain.
5616 * The input "build" is assumed to have a parametric domain and
5617 * is replaced by the same zero-dimensional schedule space.
5619 * We also add some of the parameter constraints in the build domain
5620 * to the executed relation. Adding these constraints
5621 * allows for an earlier detection of conflicts in some cases.
5622 * However, we do not want to divide the executed relation into
5623 * more disjuncts than necessary. We therefore approximate
5624 * the constraints on the parameters by a single disjunct set.
5626 static __isl_give isl_ast_node *build_ast_from_domain(
5627 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node)
5629 isl_ctx *ctx;
5630 isl_union_set *domain, *schedule_domain;
5631 isl_union_map *executed;
5632 isl_space *space;
5633 isl_set *set;
5634 isl_ast_graft_list *list;
5635 isl_ast_node *ast;
5636 int is_params;
5638 if (!build)
5639 goto error;
5641 ctx = isl_ast_build_get_ctx(build);
5642 space = isl_ast_build_get_space(build, 1);
5643 is_params = isl_space_is_params(space);
5644 isl_space_free(space);
5645 if (is_params < 0)
5646 goto error;
5647 if (!is_params)
5648 isl_die(ctx, isl_error_unsupported,
5649 "expecting parametric initial context", goto error);
5651 domain = isl_schedule_node_domain_get_domain(node);
5652 domain = isl_union_set_coalesce(domain);
5654 space = isl_union_set_get_space(domain);
5655 space = isl_space_set_from_params(space);
5656 build = isl_ast_build_product(build, space);
5658 set = isl_ast_build_get_domain(build);
5659 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5660 schedule_domain = isl_union_set_from_set(set);
5662 executed = isl_union_map_from_domain_and_range(schedule_domain, domain);
5663 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5664 ast = isl_ast_node_from_graft_list(list, build);
5665 isl_ast_build_free(build);
5667 return ast;
5668 error:
5669 isl_schedule_node_free(node);
5670 isl_ast_build_free(build);
5671 return NULL;
5674 /* Generate an AST that visits the elements in the domain of "schedule"
5675 * in the relative order specified by the schedule tree.
5677 * "build" is an isl_ast_build that has been created using
5678 * isl_ast_build_alloc or isl_ast_build_from_context based
5679 * on a parametric set.
5681 * The construction starts at the root node of the schedule,
5682 * which is assumed to be a domain node.
5684 __isl_give isl_ast_node *isl_ast_build_node_from_schedule(
5685 __isl_keep isl_ast_build *build, __isl_take isl_schedule *schedule)
5687 isl_ctx *ctx;
5688 isl_schedule_node *node;
5690 if (!build || !schedule)
5691 goto error;
5693 ctx = isl_ast_build_get_ctx(build);
5695 node = isl_schedule_get_root(schedule);
5696 isl_schedule_free(schedule);
5698 build = isl_ast_build_copy(build);
5699 build = isl_ast_build_set_single_valued(build, 0);
5700 if (isl_schedule_node_get_type(node) != isl_schedule_node_domain)
5701 isl_die(ctx, isl_error_unsupported,
5702 "expecting root domain node",
5703 build = isl_ast_build_free(build));
5704 return build_ast_from_domain(build, node);
5705 error:
5706 isl_schedule_free(schedule);
5707 return NULL;