extract out shared isl_basic_map_div_expr_involves_vars
[isl.git] / isl_ast_codegen.c
blob8275827a874d69f9e38800e665d9fcd40b6bf89d
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/id.h>
15 #include <isl/val.h>
16 #include <isl/space.h>
17 #include <isl/aff.h>
18 #include <isl/constraint.h>
19 #include <isl/set.h>
20 #include <isl/ilp.h>
21 #include <isl/union_set.h>
22 #include <isl/union_map.h>
23 #include <isl/schedule_node.h>
24 #include <isl/options.h>
25 #include <isl_sort.h>
26 #include <isl_tarjan.h>
27 #include <isl_ast_private.h>
28 #include <isl_ast_build_expr.h>
29 #include <isl_ast_build_private.h>
30 #include <isl_ast_graft_private.h>
32 /* Try and reduce the number of disjuncts in the representation of "set",
33 * without dropping explicit representations of local variables.
35 static __isl_give isl_set *isl_set_coalesce_preserve(__isl_take isl_set *set)
37 isl_ctx *ctx;
38 int save_preserve;
40 if (!set)
41 return NULL;
43 ctx = isl_set_get_ctx(set);
44 save_preserve = isl_options_get_coalesce_preserve_locals(ctx);
45 isl_options_set_coalesce_preserve_locals(ctx, 1);
46 set = isl_set_coalesce(set);
47 isl_options_set_coalesce_preserve_locals(ctx, save_preserve);
48 return set;
51 /* Data used in generate_domain.
53 * "build" is the input build.
54 * "list" collects the results.
56 struct isl_generate_domain_data {
57 isl_ast_build *build;
59 isl_ast_graft_list *list;
62 static __isl_give isl_ast_graft_list *generate_next_level(
63 __isl_take isl_union_map *executed,
64 __isl_take isl_ast_build *build);
65 static __isl_give isl_ast_graft_list *generate_code(
66 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
67 int internal);
69 /* Generate an AST for a single domain based on
70 * the (non single valued) inverse schedule "executed".
72 * We extend the schedule with the iteration domain
73 * and continue generating through a call to generate_code.
75 * In particular, if executed has the form
77 * S -> D
79 * then we continue generating code on
81 * [S -> D] -> D
83 * The extended inverse schedule is clearly single valued
84 * ensuring that the nested generate_code will not reach this function,
85 * but will instead create calls to all elements of D that need
86 * to be executed from the current schedule domain.
88 static isl_stat generate_non_single_valued(__isl_take isl_map *executed,
89 struct isl_generate_domain_data *data)
91 isl_map *identity;
92 isl_ast_build *build;
93 isl_ast_graft_list *list;
95 build = isl_ast_build_copy(data->build);
97 identity = isl_set_identity(isl_map_range(isl_map_copy(executed)));
98 executed = isl_map_domain_product(executed, identity);
100 list = generate_code(isl_union_map_from_map(executed), build, 1);
102 data->list = isl_ast_graft_list_concat(data->list, list);
104 return isl_stat_ok;
107 /* Call the at_each_domain callback, if requested by the user,
108 * after recording the current inverse schedule in the build.
110 static __isl_give isl_ast_graft *at_each_domain(__isl_take isl_ast_graft *graft,
111 __isl_keep isl_map *executed, __isl_keep isl_ast_build *build)
113 if (!graft || !build)
114 return isl_ast_graft_free(graft);
115 if (!build->at_each_domain)
116 return graft;
118 build = isl_ast_build_copy(build);
119 build = isl_ast_build_set_executed(build,
120 isl_union_map_from_map(isl_map_copy(executed)));
121 if (!build)
122 return isl_ast_graft_free(graft);
124 graft->node = build->at_each_domain(graft->node,
125 build, build->at_each_domain_user);
126 isl_ast_build_free(build);
128 if (!graft->node)
129 graft = isl_ast_graft_free(graft);
131 return graft;
134 /* Generate a call expression for the single executed
135 * domain element "executed" and put a guard around it based on its (simplified)
136 * domain.
138 * At this stage, any pending constraints in the build can no longer
139 * be simplified with respect to any enforced constraints since
140 * the call node does not have any enforced constraints.
141 * Since all pending constraints not covered by any enforced constraints
142 * will be added as a guard to the graft in create_node_scaled,
143 * even in the eliminated case, the pending constraints
144 * can be considered to have been generated by outer constructs.
146 * If the user has set an at_each_domain callback, it is called
147 * on the constructed call expression node.
149 static isl_stat add_domain(__isl_take isl_map *executed,
150 struct isl_generate_domain_data *data)
152 isl_ast_build *build;
153 isl_ast_graft *graft;
154 isl_ast_graft_list *list;
155 isl_set *guard, *pending;
157 build = isl_ast_build_copy(data->build);
158 pending = isl_ast_build_get_pending(build);
159 build = isl_ast_build_replace_pending_by_guard(build, pending);
161 guard = isl_map_domain(isl_map_copy(executed));
162 guard = isl_set_compute_divs(guard);
163 guard = isl_set_coalesce_preserve(guard);
164 guard = isl_set_gist(guard, isl_ast_build_get_generated(build));
165 guard = isl_ast_build_specialize(build, guard);
167 graft = isl_ast_graft_alloc_domain(isl_map_copy(executed), build);
168 graft = at_each_domain(graft, executed, build);
169 isl_ast_build_free(build);
170 isl_map_free(executed);
171 graft = isl_ast_graft_add_guard(graft, guard, data->build);
173 list = isl_ast_graft_list_from_ast_graft(graft);
174 data->list = isl_ast_graft_list_concat(data->list, list);
176 return isl_stat_ok;
179 /* Generate an AST for a single domain based on
180 * the inverse schedule "executed" and add it to data->list.
182 * If there is more than one domain element associated to the current
183 * schedule "time", then we need to continue the generation process
184 * in generate_non_single_valued.
185 * Note that the inverse schedule being single-valued may depend
186 * on constraints that are only available in the original context
187 * domain specified by the user. We therefore first introduce
188 * some of the constraints of data->build->domain. In particular,
189 * we intersect with a single-disjunct approximation of this set.
190 * We perform this approximation to avoid further splitting up
191 * the executed relation, possibly introducing a disjunctive guard
192 * on the statement.
194 * Otherwise, call add_domain to generate a call expression (with guard) and
195 * to call the at_each_domain callback, if any.
197 * Coalesce the inverse schedule before checking for single-valuedness.
198 * Skip this if the inverse schedule is obviously single-valued.
200 static isl_stat generate_domain(__isl_take isl_map *executed, void *user)
202 struct isl_generate_domain_data *data = user;
203 isl_set *domain;
204 int empty, sv;
206 domain = isl_ast_build_get_domain(data->build);
207 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
208 executed = isl_map_intersect_domain(executed, domain);
209 empty = isl_map_is_empty(executed);
210 if (empty < 0)
211 goto error;
212 if (empty) {
213 isl_map_free(executed);
214 return isl_stat_ok;
217 sv = isl_map_plain_is_single_valued(executed);
218 if (sv < 0)
219 goto error;
220 if (sv)
221 return add_domain(executed, data);
223 executed = isl_map_coalesce(executed);
224 sv = isl_map_is_single_valued(executed);
225 if (sv < 0)
226 goto error;
227 if (!sv)
228 return generate_non_single_valued(executed, data);
230 return add_domain(executed, data);
231 error:
232 isl_map_free(executed);
233 return isl_stat_error;
236 /* Call build->create_leaf to a create "leaf" node in the AST,
237 * encapsulate the result in an isl_ast_graft and return the result
238 * as a 1-element list.
240 * Note that the node returned by the user may be an entire tree.
242 * Since the node itself cannot enforce any constraints, we turn
243 * all pending constraints into guards and add them to the resulting
244 * graft to ensure that they will be generated.
246 * Before we pass control to the user, we first clear some information
247 * from the build that is (presumbably) only meaningful
248 * for the current code generation.
249 * This includes the create_leaf callback itself, so we make a copy
250 * of the build first.
252 static __isl_give isl_ast_graft_list *call_create_leaf(
253 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
255 isl_set *guard;
256 isl_ast_node *node;
257 isl_ast_graft *graft;
258 isl_ast_build *user_build;
260 guard = isl_ast_build_get_pending(build);
261 user_build = isl_ast_build_copy(build);
262 user_build = isl_ast_build_replace_pending_by_guard(user_build,
263 isl_set_copy(guard));
264 user_build = isl_ast_build_set_executed(user_build, executed);
265 user_build = isl_ast_build_clear_local_info(user_build);
266 if (!user_build)
267 node = NULL;
268 else
269 node = build->create_leaf(user_build, build->create_leaf_user);
270 graft = isl_ast_graft_alloc(node, build);
271 graft = isl_ast_graft_add_guard(graft, guard, build);
272 isl_ast_build_free(build);
273 return isl_ast_graft_list_from_ast_graft(graft);
276 static __isl_give isl_ast_graft_list *build_ast_from_child(
277 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
278 __isl_take isl_union_map *executed);
280 /* Generate an AST after having handled the complete schedule
281 * of this call to the code generator or the complete band
282 * if we are generating an AST from a schedule tree.
284 * If we are inside a band node, then move on to the child of the band.
286 * If the user has specified a create_leaf callback, control
287 * is passed to the user in call_create_leaf.
289 * Otherwise, we generate one or more calls for each individual
290 * domain in generate_domain.
292 static __isl_give isl_ast_graft_list *generate_inner_level(
293 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
295 isl_ctx *ctx;
296 struct isl_generate_domain_data data = { build };
298 if (!build || !executed)
299 goto error;
301 if (isl_ast_build_has_schedule_node(build)) {
302 isl_schedule_node *node;
303 node = isl_ast_build_get_schedule_node(build);
304 build = isl_ast_build_reset_schedule_node(build);
305 return build_ast_from_child(build, node, executed);
308 if (build->create_leaf)
309 return call_create_leaf(executed, build);
311 ctx = isl_union_map_get_ctx(executed);
312 data.list = isl_ast_graft_list_alloc(ctx, 0);
313 if (isl_union_map_foreach_map(executed, &generate_domain, &data) < 0)
314 data.list = isl_ast_graft_list_free(data.list);
316 if (0)
317 error: data.list = NULL;
318 isl_ast_build_free(build);
319 isl_union_map_free(executed);
320 return data.list;
323 /* Call the before_each_for callback, if requested by the user.
325 static __isl_give isl_ast_node *before_each_for(__isl_take isl_ast_node *node,
326 __isl_keep isl_ast_build *build)
328 isl_id *id;
330 if (!node || !build)
331 return isl_ast_node_free(node);
332 if (!build->before_each_for)
333 return node;
334 id = build->before_each_for(build, build->before_each_for_user);
335 node = isl_ast_node_set_annotation(node, id);
336 return node;
339 /* Call the after_each_for callback, if requested by the user.
341 static __isl_give isl_ast_graft *after_each_for(__isl_take isl_ast_graft *graft,
342 __isl_keep isl_ast_build *build)
344 if (!graft || !build)
345 return isl_ast_graft_free(graft);
346 if (!build->after_each_for)
347 return graft;
348 graft->node = build->after_each_for(graft->node, build,
349 build->after_each_for_user);
350 if (!graft->node)
351 return isl_ast_graft_free(graft);
352 return graft;
355 /* Plug in all the know values of the current and outer dimensions
356 * in the domain of "executed". In principle, we only need to plug
357 * in the known value of the current dimension since the values of
358 * outer dimensions have been plugged in already.
359 * However, it turns out to be easier to just plug in all known values.
361 static __isl_give isl_union_map *plug_in_values(
362 __isl_take isl_union_map *executed, __isl_keep isl_ast_build *build)
364 return isl_ast_build_substitute_values_union_map_domain(build,
365 executed);
368 /* Check if the constraint "c" is a lower bound on dimension "pos",
369 * an upper bound, or independent of dimension "pos".
371 static int constraint_type(isl_constraint *c, int pos)
373 if (isl_constraint_is_lower_bound(c, isl_dim_set, pos))
374 return 1;
375 if (isl_constraint_is_upper_bound(c, isl_dim_set, pos))
376 return 2;
377 return 0;
380 /* Compare the types of the constraints "a" and "b",
381 * resulting in constraints that are independent of "depth"
382 * to be sorted before the lower bounds on "depth", which in
383 * turn are sorted before the upper bounds on "depth".
385 static int cmp_constraint(__isl_keep isl_constraint *a,
386 __isl_keep isl_constraint *b, void *user)
388 int *depth = user;
389 int t1 = constraint_type(a, *depth);
390 int t2 = constraint_type(b, *depth);
392 return t1 - t2;
395 /* Extract a lower bound on dimension "pos" from constraint "c".
397 * If the constraint is of the form
399 * a x + f(...) >= 0
401 * then we essentially return
403 * l = ceil(-f(...)/a)
405 * However, if the current dimension is strided, then we need to make
406 * sure that the lower bound we construct is of the form
408 * f + s a
410 * with f the offset and s the stride.
411 * We therefore compute
413 * f + s * ceil((l - f)/s)
415 static __isl_give isl_aff *lower_bound(__isl_keep isl_constraint *c,
416 int pos, __isl_keep isl_ast_build *build)
418 isl_aff *aff;
420 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
421 aff = isl_aff_ceil(aff);
423 if (isl_ast_build_has_stride(build, pos)) {
424 isl_aff *offset;
425 isl_val *stride;
427 offset = isl_ast_build_get_offset(build, pos);
428 stride = isl_ast_build_get_stride(build, pos);
430 aff = isl_aff_sub(aff, isl_aff_copy(offset));
431 aff = isl_aff_scale_down_val(aff, isl_val_copy(stride));
432 aff = isl_aff_ceil(aff);
433 aff = isl_aff_scale_val(aff, stride);
434 aff = isl_aff_add(aff, offset);
437 aff = isl_ast_build_compute_gist_aff(build, aff);
439 return aff;
442 /* Return the exact lower bound (or upper bound if "upper" is set)
443 * of "domain" as a piecewise affine expression.
445 * If we are computing a lower bound (of a strided dimension), then
446 * we need to make sure it is of the form
448 * f + s a
450 * where f is the offset and s is the stride.
451 * We therefore need to include the stride constraint before computing
452 * the minimum.
454 static __isl_give isl_pw_aff *exact_bound(__isl_keep isl_set *domain,
455 __isl_keep isl_ast_build *build, int upper)
457 isl_set *stride;
458 isl_map *it_map;
459 isl_pw_aff *pa;
460 isl_pw_multi_aff *pma;
462 domain = isl_set_copy(domain);
463 if (!upper) {
464 stride = isl_ast_build_get_stride_constraint(build);
465 domain = isl_set_intersect(domain, stride);
467 it_map = isl_ast_build_map_to_iterator(build, domain);
468 if (upper)
469 pma = isl_map_lexmax_pw_multi_aff(it_map);
470 else
471 pma = isl_map_lexmin_pw_multi_aff(it_map);
472 pa = isl_pw_multi_aff_get_pw_aff(pma, 0);
473 isl_pw_multi_aff_free(pma);
474 pa = isl_ast_build_compute_gist_pw_aff(build, pa);
475 pa = isl_pw_aff_coalesce(pa);
477 return pa;
480 /* Callback for sorting the isl_pw_aff_list passed to reduce_list and
481 * remove_redundant_lower_bounds.
483 static int reduce_list_cmp(__isl_keep isl_pw_aff *a, __isl_keep isl_pw_aff *b,
484 void *user)
486 return isl_pw_aff_plain_cmp(a, b);
489 /* Given a list of lower bounds "list", remove those that are redundant
490 * with respect to the other bounds in "list" and the domain of "build".
492 * We first sort the bounds in the same way as they would be sorted
493 * by set_for_node_expressions so that we can try and remove the last
494 * bounds first.
496 * For a lower bound to be effective, there needs to be at least
497 * one domain element for which it is larger than all other lower bounds.
498 * For each lower bound we therefore intersect the domain with
499 * the conditions that it is larger than all other bounds and
500 * check whether the result is empty. If so, the bound can be removed.
502 static __isl_give isl_pw_aff_list *remove_redundant_lower_bounds(
503 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
505 int i, j;
506 isl_size n;
507 isl_set *domain;
509 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
511 n = isl_pw_aff_list_n_pw_aff(list);
512 if (n < 0)
513 return isl_pw_aff_list_free(list);
514 if (n <= 1)
515 return list;
517 domain = isl_ast_build_get_domain(build);
519 for (i = n - 1; i >= 0; --i) {
520 isl_pw_aff *pa_i;
521 isl_set *domain_i;
522 int empty;
524 domain_i = isl_set_copy(domain);
525 pa_i = isl_pw_aff_list_get_pw_aff(list, i);
527 for (j = 0; j < n; ++j) {
528 isl_pw_aff *pa_j;
529 isl_set *better;
531 if (j == i)
532 continue;
534 pa_j = isl_pw_aff_list_get_pw_aff(list, j);
535 better = isl_pw_aff_gt_set(isl_pw_aff_copy(pa_i), pa_j);
536 domain_i = isl_set_intersect(domain_i, better);
539 empty = isl_set_is_empty(domain_i);
541 isl_set_free(domain_i);
542 isl_pw_aff_free(pa_i);
544 if (empty < 0)
545 goto error;
546 if (!empty)
547 continue;
548 list = isl_pw_aff_list_drop(list, i, 1);
549 n--;
552 isl_set_free(domain);
554 return list;
555 error:
556 isl_set_free(domain);
557 return isl_pw_aff_list_free(list);
560 /* Extract a lower bound on dimension "pos" from each constraint
561 * in "constraints" and return the list of lower bounds.
562 * If "constraints" has zero elements, then we extract a lower bound
563 * from "domain" instead.
565 * If the current dimension is strided, then the lower bound
566 * is adjusted by lower_bound to match the stride information.
567 * This modification may make one or more lower bounds redundant
568 * with respect to the other lower bounds. We therefore check
569 * for this condition and remove the redundant lower bounds.
571 static __isl_give isl_pw_aff_list *lower_bounds(
572 __isl_keep isl_constraint_list *constraints, int pos,
573 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
575 isl_ctx *ctx;
576 isl_pw_aff_list *list;
577 int i;
578 isl_size n;
580 if (!build)
581 return NULL;
583 n = isl_constraint_list_n_constraint(constraints);
584 if (n < 0)
585 return NULL;
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;
623 isl_size n;
625 n = isl_constraint_list_n_constraint(constraints);
626 if (n < 0)
627 return NULL;
628 if (n == 0) {
629 isl_pw_aff *pa;
630 pa = exact_bound(domain, build, 1);
631 return isl_pw_aff_list_from_pw_aff(pa);
634 ctx = isl_ast_build_get_ctx(build);
635 list = isl_pw_aff_list_alloc(ctx,n);
637 for (i = 0; i < n; ++i) {
638 isl_aff *aff;
639 isl_constraint *c;
641 c = isl_constraint_list_get_constraint(constraints, i);
642 aff = isl_constraint_get_bound(c, isl_dim_set, pos);
643 isl_constraint_free(c);
644 aff = isl_aff_floor(aff);
645 list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
648 return list;
651 /* Return an isl_ast_expr that performs the reduction of type "type"
652 * on AST expressions corresponding to the elements in "list".
654 * The list is assumed to contain at least one element.
655 * If the list contains exactly one element, then the returned isl_ast_expr
656 * simply computes that affine expression.
657 * If the list contains more than one element, then we sort it
658 * using a fairly arbitrary but hopefully reasonably stable order.
660 static __isl_give isl_ast_expr *reduce_list(enum isl_ast_expr_op_type type,
661 __isl_keep isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
663 int i;
664 isl_size n;
665 isl_ctx *ctx;
666 isl_ast_expr *expr;
668 n = isl_pw_aff_list_n_pw_aff(list);
669 if (n < 0)
670 return NULL;
672 if (n == 1)
673 return isl_ast_build_expr_from_pw_aff_internal(build,
674 isl_pw_aff_list_get_pw_aff(list, 0));
676 ctx = isl_pw_aff_list_get_ctx(list);
677 expr = isl_ast_expr_alloc_op(ctx, type, n);
679 list = isl_pw_aff_list_copy(list);
680 list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
681 if (!list)
682 return isl_ast_expr_free(expr);
684 for (i = 0; i < n; ++i) {
685 isl_ast_expr *expr_i;
687 expr_i = isl_ast_build_expr_from_pw_aff_internal(build,
688 isl_pw_aff_list_get_pw_aff(list, i));
689 expr = isl_ast_expr_op_add_arg(expr, expr_i);
692 isl_pw_aff_list_free(list);
693 return expr;
696 /* Add guards implied by the "generated constraints",
697 * but not (necessarily) enforced by the generated AST to "guard".
698 * In particular, if there is any stride constraints,
699 * then add the guard implied by those constraints.
700 * If we have generated a degenerate loop, then add the guard
701 * implied by "bounds" on the outer dimensions, i.e., the guard
702 * that ensures that the single value actually exists.
703 * Since there may also be guards implied by a combination
704 * of these constraints, we first combine them before
705 * deriving the implied constraints.
707 static __isl_give isl_set *add_implied_guards(__isl_take isl_set *guard,
708 int degenerate, __isl_keep isl_basic_set *bounds,
709 __isl_keep isl_ast_build *build)
711 isl_size depth;
712 isl_bool has_stride;
713 isl_space *space;
714 isl_set *dom, *set;
716 depth = isl_ast_build_get_depth(build);
717 has_stride = isl_ast_build_has_stride(build, depth);
718 if (depth < 0 || has_stride < 0)
719 return isl_set_free(guard);
720 if (!has_stride && !degenerate)
721 return guard;
723 space = isl_basic_set_get_space(bounds);
724 dom = isl_set_universe(space);
726 if (degenerate) {
727 bounds = isl_basic_set_copy(bounds);
728 bounds = isl_basic_set_drop_constraints_not_involving_dims(
729 bounds, isl_dim_set, depth, 1);
730 set = isl_set_from_basic_set(bounds);
731 dom = isl_set_intersect(dom, set);
734 if (has_stride) {
735 set = isl_ast_build_get_stride_constraint(build);
736 dom = isl_set_intersect(dom, set);
739 dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
740 dom = isl_ast_build_compute_gist(build, dom);
741 guard = isl_set_intersect(guard, dom);
743 return guard;
746 /* Update "graft" based on "sub_build" for the degenerate case.
748 * "build" is the build in which graft->node was created
749 * "sub_build" contains information about the current level itself,
750 * including the single value attained.
752 * We set the initialization part of the for loop to the single
753 * value attained by the current dimension.
754 * The increment and condition are not strictly needed as they are known
755 * to be "1" and "iterator <= value" respectively.
757 static __isl_give isl_ast_graft *refine_degenerate(
758 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build,
759 __isl_keep isl_ast_build *sub_build)
761 isl_pw_aff *value;
762 isl_ast_expr *init;
764 if (!graft || !sub_build)
765 return isl_ast_graft_free(graft);
767 value = isl_pw_aff_copy(sub_build->value);
769 init = isl_ast_build_expr_from_pw_aff_internal(build, value);
770 graft->node = isl_ast_node_for_set_init(graft->node, init);
771 if (!graft->node)
772 return isl_ast_graft_free(graft);
774 return graft;
777 /* Return the intersection of constraints in "list" as a set.
779 static __isl_give isl_set *intersect_constraints(
780 __isl_keep isl_constraint_list *list)
782 int i;
783 isl_size n;
784 isl_basic_set *bset;
786 n = isl_constraint_list_n_constraint(list);
787 if (n < 0)
788 return NULL;
789 if (n < 1)
790 isl_die(isl_constraint_list_get_ctx(list), isl_error_internal,
791 "expecting at least one constraint", return NULL);
793 bset = isl_basic_set_from_constraint(
794 isl_constraint_list_get_constraint(list, 0));
795 for (i = 1; i < n; ++i) {
796 isl_basic_set *bset_i;
798 bset_i = isl_basic_set_from_constraint(
799 isl_constraint_list_get_constraint(list, i));
800 bset = isl_basic_set_intersect(bset, bset_i);
803 return isl_set_from_basic_set(bset);
806 /* Compute the constraints on the outer dimensions enforced by
807 * graft->node and add those constraints to graft->enforced,
808 * in case the upper bound is expressed as a set "upper".
810 * In particular, if l(...) is a lower bound in "lower", and
812 * -a i + f(...) >= 0 or a i <= f(...)
814 * is an upper bound ocnstraint on the current dimension i,
815 * then the for loop enforces the constraint
817 * -a l(...) + f(...) >= 0 or a l(...) <= f(...)
819 * We therefore simply take each lower bound in turn, plug it into
820 * the upper bounds and compute the intersection over all lower bounds.
822 * If a lower bound is a rational expression, then
823 * isl_basic_set_preimage_multi_aff will force this rational
824 * expression to have only integer values. However, the loop
825 * itself does not enforce this integrality constraint. We therefore
826 * use the ceil of the lower bounds instead of the lower bounds themselves.
827 * Other constraints will make sure that the for loop is only executed
828 * when each of the lower bounds attains an integral value.
829 * In particular, potentially rational values only occur in
830 * lower_bound if the offset is a (seemingly) rational expression,
831 * but then outer conditions will make sure that this rational expression
832 * only attains integer values.
834 static __isl_give isl_ast_graft *set_enforced_from_set(
835 __isl_take isl_ast_graft *graft,
836 __isl_keep isl_pw_aff_list *lower, int pos, __isl_keep isl_set *upper)
838 isl_space *space;
839 isl_basic_set *enforced;
840 isl_pw_multi_aff *pma;
841 int i;
842 isl_size n;
844 n = isl_pw_aff_list_n_pw_aff(lower);
845 if (!graft || n < 0)
846 return isl_ast_graft_free(graft);
848 space = isl_set_get_space(upper);
849 enforced = isl_basic_set_universe(isl_space_copy(space));
851 space = isl_space_map_from_set(space);
852 pma = isl_pw_multi_aff_identity(space);
854 for (i = 0; i < n; ++i) {
855 isl_pw_aff *pa;
856 isl_set *enforced_i;
857 isl_basic_set *hull;
858 isl_pw_multi_aff *pma_i;
860 pa = isl_pw_aff_list_get_pw_aff(lower, i);
861 pa = isl_pw_aff_ceil(pa);
862 pma_i = isl_pw_multi_aff_copy(pma);
863 pma_i = isl_pw_multi_aff_set_pw_aff(pma_i, pos, pa);
864 enforced_i = isl_set_copy(upper);
865 enforced_i = isl_set_preimage_pw_multi_aff(enforced_i, pma_i);
866 hull = isl_set_simple_hull(enforced_i);
867 enforced = isl_basic_set_intersect(enforced, hull);
870 isl_pw_multi_aff_free(pma);
872 graft = isl_ast_graft_enforce(graft, enforced);
874 return graft;
877 /* Compute the constraints on the outer dimensions enforced by
878 * graft->node and add those constraints to graft->enforced,
879 * in case the upper bound is expressed as
880 * a list of affine expressions "upper".
882 * The enforced condition is that each lower bound expression is less
883 * than or equal to each upper bound expression.
885 static __isl_give isl_ast_graft *set_enforced_from_list(
886 __isl_take isl_ast_graft *graft,
887 __isl_keep isl_pw_aff_list *lower, __isl_keep isl_pw_aff_list *upper)
889 isl_set *cond;
890 isl_basic_set *enforced;
892 lower = isl_pw_aff_list_copy(lower);
893 upper = isl_pw_aff_list_copy(upper);
894 cond = isl_pw_aff_list_le_set(lower, upper);
895 enforced = isl_set_simple_hull(cond);
896 graft = isl_ast_graft_enforce(graft, enforced);
898 return graft;
901 /* Does "aff" have a negative constant term?
903 static isl_bool aff_constant_is_negative(__isl_keep isl_set *set,
904 __isl_keep isl_aff *aff, void *user)
906 isl_bool is_neg;
907 isl_val *v;
909 v = isl_aff_get_constant_val(aff);
910 is_neg = isl_val_is_neg(v);
911 isl_val_free(v);
913 return is_neg;
916 /* Does "pa" have a negative constant term over its entire domain?
918 static isl_bool pw_aff_constant_is_negative(__isl_keep isl_pw_aff *pa,
919 void *user)
921 return isl_pw_aff_every_piece(pa, &aff_constant_is_negative, NULL);
924 /* Does each element in "list" have a negative constant term?
926 static int list_constant_is_negative(__isl_keep isl_pw_aff_list *list)
928 return isl_pw_aff_list_every(list, &pw_aff_constant_is_negative, NULL);
931 /* Add 1 to each of the elements in "list", where each of these elements
932 * is defined over the internal schedule space of "build".
934 static __isl_give isl_pw_aff_list *list_add_one(
935 __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
937 int i;
938 isl_size n;
939 isl_space *space;
940 isl_aff *aff;
941 isl_pw_aff *one;
943 n = isl_pw_aff_list_n_pw_aff(list);
944 if (n < 0)
945 return isl_pw_aff_list_free(list);
947 space = isl_ast_build_get_space(build, 1);
948 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
949 aff = isl_aff_add_constant_si(aff, 1);
950 one = isl_pw_aff_from_aff(aff);
952 for (i = 0; i < n; ++i) {
953 isl_pw_aff *pa;
954 pa = isl_pw_aff_list_get_pw_aff(list, i);
955 pa = isl_pw_aff_add(pa, isl_pw_aff_copy(one));
956 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
959 isl_pw_aff_free(one);
961 return list;
964 /* Set the condition part of the for node graft->node in case
965 * the upper bound is represented as a list of piecewise affine expressions.
967 * In particular, set the condition to
969 * iterator <= min(list of upper bounds)
971 * If each of the upper bounds has a negative constant term, then
972 * set the condition to
974 * iterator < min(list of (upper bound + 1)s)
977 static __isl_give isl_ast_graft *set_for_cond_from_list(
978 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *list,
979 __isl_keep isl_ast_build *build)
981 int neg;
982 isl_ast_expr *bound, *iterator, *cond;
983 enum isl_ast_expr_op_type type = isl_ast_expr_op_le;
985 if (!graft || !list)
986 return isl_ast_graft_free(graft);
988 neg = list_constant_is_negative(list);
989 if (neg < 0)
990 return isl_ast_graft_free(graft);
991 list = isl_pw_aff_list_copy(list);
992 if (neg) {
993 list = list_add_one(list, build);
994 type = isl_ast_expr_op_lt;
997 bound = reduce_list(isl_ast_expr_op_min, list, build);
998 iterator = isl_ast_expr_copy(graft->node->u.f.iterator);
999 cond = isl_ast_expr_alloc_binary(type, iterator, bound);
1000 graft->node = isl_ast_node_for_set_cond(graft->node, cond);
1002 isl_pw_aff_list_free(list);
1003 if (!graft->node)
1004 return isl_ast_graft_free(graft);
1005 return graft;
1008 /* Set the condition part of the for node graft->node in case
1009 * the upper bound is represented as a set.
1011 static __isl_give isl_ast_graft *set_for_cond_from_set(
1012 __isl_take isl_ast_graft *graft, __isl_keep isl_set *set,
1013 __isl_keep isl_ast_build *build)
1015 isl_ast_expr *cond;
1017 if (!graft)
1018 return NULL;
1020 cond = isl_ast_build_expr_from_set_internal(build, isl_set_copy(set));
1021 graft->node = isl_ast_node_for_set_cond(graft->node, cond);
1022 if (!graft->node)
1023 return isl_ast_graft_free(graft);
1024 return graft;
1027 /* Construct an isl_ast_expr for the increment (i.e., stride) of
1028 * the current dimension.
1030 static __isl_give isl_ast_expr *for_inc(__isl_keep isl_ast_build *build)
1032 isl_size depth;
1033 isl_val *v;
1034 isl_ctx *ctx;
1036 depth = isl_ast_build_get_depth(build);
1037 if (depth < 0)
1038 return NULL;
1039 ctx = isl_ast_build_get_ctx(build);
1041 if (!isl_ast_build_has_stride(build, depth))
1042 return isl_ast_expr_alloc_int_si(ctx, 1);
1044 v = isl_ast_build_get_stride(build, depth);
1045 return isl_ast_expr_from_val(v);
1048 /* Should we express the loop condition as
1050 * iterator <= min(list of upper bounds)
1052 * or as a conjunction of constraints?
1054 * The first is constructed from a list of upper bounds.
1055 * The second is constructed from a set.
1057 * If there are no upper bounds in "constraints", then this could mean
1058 * that "domain" simply doesn't have an upper bound or that we didn't
1059 * pick any upper bound. In the first case, we want to generate the
1060 * loop condition as a(n empty) conjunction of constraints
1061 * In the second case, we will compute
1062 * a single upper bound from "domain" and so we use the list form.
1064 * If there are upper bounds in "constraints",
1065 * then we use the list form iff the atomic_upper_bound option is set.
1067 static int use_upper_bound_list(isl_ctx *ctx, int n_upper,
1068 __isl_keep isl_set *domain, int depth)
1070 if (n_upper > 0)
1071 return isl_options_get_ast_build_atomic_upper_bound(ctx);
1072 else
1073 return isl_set_dim_has_upper_bound(domain, isl_dim_set, depth);
1076 /* Fill in the expressions of the for node in graft->node.
1078 * In particular,
1079 * - set the initialization part of the loop to the maximum of the lower bounds
1080 * - extract the increment from the stride of the current dimension
1081 * - construct the for condition either based on a list of upper bounds
1082 * or on a set of upper bound constraints.
1084 static __isl_give isl_ast_graft *set_for_node_expressions(
1085 __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *lower,
1086 int use_list, __isl_keep isl_pw_aff_list *upper_list,
1087 __isl_keep isl_set *upper_set, __isl_keep isl_ast_build *build)
1089 isl_ast_expr *init;
1091 if (!graft)
1092 return NULL;
1094 init = reduce_list(isl_ast_expr_op_max, lower, build);
1095 graft->node = isl_ast_node_for_set_init(graft->node, init);
1096 graft->node = isl_ast_node_for_set_inc(graft->node, for_inc(build));
1098 if (!graft->node)
1099 graft = isl_ast_graft_free(graft);
1101 if (use_list)
1102 graft = set_for_cond_from_list(graft, upper_list, build);
1103 else
1104 graft = set_for_cond_from_set(graft, upper_set, build);
1106 return graft;
1109 /* Update "graft" based on "bounds" and "domain" for the generic,
1110 * non-degenerate, case.
1112 * "c_lower" and "c_upper" contain the lower and upper bounds
1113 * that the loop node should express.
1114 * "domain" is the subset of the intersection of the constraints
1115 * for which some code is executed.
1117 * There may be zero lower bounds or zero upper bounds in "constraints"
1118 * in case the list of constraints was created
1119 * based on the atomic option or based on separation with explicit bounds.
1120 * In that case, we use "domain" to derive lower and/or upper bounds.
1122 * We first compute a list of one or more lower bounds.
1124 * Then we decide if we want to express the condition as
1126 * iterator <= min(list of upper bounds)
1128 * or as a conjunction of constraints.
1130 * The set of enforced constraints is then computed either based on
1131 * a list of upper bounds or on a set of upper bound constraints.
1132 * We do not compute any enforced constraints if we were forced
1133 * to compute a lower or upper bound using exact_bound. The domains
1134 * of the resulting expressions may imply some bounds on outer dimensions
1135 * that we do not want to appear in the enforced constraints since
1136 * they are not actually enforced by the corresponding code.
1138 * Finally, we fill in the expressions of the for node.
1140 static __isl_give isl_ast_graft *refine_generic_bounds(
1141 __isl_take isl_ast_graft *graft,
1142 __isl_take isl_constraint_list *c_lower,
1143 __isl_take isl_constraint_list *c_upper,
1144 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1146 isl_size depth;
1147 isl_ctx *ctx;
1148 isl_pw_aff_list *lower;
1149 int use_list;
1150 isl_set *upper_set = NULL;
1151 isl_pw_aff_list *upper_list = NULL;
1152 isl_size n_lower, n_upper;
1154 depth = isl_ast_build_get_depth(build);
1155 if (!graft || !c_lower || !c_upper || depth < 0)
1156 goto error;
1158 ctx = isl_ast_graft_get_ctx(graft);
1160 n_lower = isl_constraint_list_n_constraint(c_lower);
1161 n_upper = isl_constraint_list_n_constraint(c_upper);
1162 if (n_lower < 0 || n_upper < 0)
1163 goto error;
1165 use_list = use_upper_bound_list(ctx, n_upper, domain, depth);
1167 lower = lower_bounds(c_lower, depth, domain, build);
1169 if (use_list)
1170 upper_list = upper_bounds(c_upper, depth, domain, build);
1171 else if (n_upper > 0)
1172 upper_set = intersect_constraints(c_upper);
1173 else
1174 upper_set = isl_set_universe(isl_set_get_space(domain));
1176 if (n_lower == 0 || n_upper == 0)
1178 else if (use_list)
1179 graft = set_enforced_from_list(graft, lower, upper_list);
1180 else
1181 graft = set_enforced_from_set(graft, lower, depth, upper_set);
1183 graft = set_for_node_expressions(graft, lower, use_list, upper_list,
1184 upper_set, build);
1186 isl_pw_aff_list_free(lower);
1187 isl_pw_aff_list_free(upper_list);
1188 isl_set_free(upper_set);
1189 isl_constraint_list_free(c_lower);
1190 isl_constraint_list_free(c_upper);
1192 return graft;
1193 error:
1194 isl_constraint_list_free(c_lower);
1195 isl_constraint_list_free(c_upper);
1196 return isl_ast_graft_free(graft);
1199 /* Internal data structure used inside count_constraints to keep
1200 * track of the number of constraints that are independent of dimension "pos",
1201 * the lower bounds in "pos" and the upper bounds in "pos".
1203 struct isl_ast_count_constraints_data {
1204 int pos;
1206 int n_indep;
1207 int n_lower;
1208 int n_upper;
1211 /* Increment data->n_indep, data->lower or data->upper depending
1212 * on whether "c" is independent of dimensions data->pos,
1213 * a lower bound or an upper bound.
1215 static isl_stat count_constraints(__isl_take isl_constraint *c, void *user)
1217 struct isl_ast_count_constraints_data *data = user;
1219 if (isl_constraint_is_lower_bound(c, isl_dim_set, data->pos))
1220 data->n_lower++;
1221 else if (isl_constraint_is_upper_bound(c, isl_dim_set, data->pos))
1222 data->n_upper++;
1223 else
1224 data->n_indep++;
1226 isl_constraint_free(c);
1228 return isl_stat_ok;
1231 /* Update "graft" based on "bounds" and "domain" for the generic,
1232 * non-degenerate, case.
1234 * "list" respresent the list of bounds that need to be encoded by
1235 * the for loop. Only the constraints that involve the iterator
1236 * are relevant here. The other constraints are taken care of by
1237 * the caller and are included in the generated constraints of "build".
1238 * "domain" is the subset of the intersection of the constraints
1239 * for which some code is executed.
1240 * "build" is the build in which graft->node was created.
1242 * We separate lower bounds, upper bounds and constraints that
1243 * are independent of the loop iterator.
1245 * The actual for loop bounds are generated in refine_generic_bounds.
1247 static __isl_give isl_ast_graft *refine_generic_split(
1248 __isl_take isl_ast_graft *graft, __isl_take isl_constraint_list *list,
1249 __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1251 struct isl_ast_count_constraints_data data;
1252 isl_size depth;
1253 isl_constraint_list *lower;
1254 isl_constraint_list *upper;
1256 depth = isl_ast_build_get_depth(build);
1257 if (depth < 0)
1258 list = isl_constraint_list_free(list);
1259 if (!list)
1260 return isl_ast_graft_free(graft);
1262 data.pos = depth;
1264 list = isl_constraint_list_sort(list, &cmp_constraint, &data.pos);
1265 if (!list)
1266 return isl_ast_graft_free(graft);
1268 data.n_indep = data.n_lower = data.n_upper = 0;
1269 if (isl_constraint_list_foreach(list, &count_constraints, &data) < 0) {
1270 isl_constraint_list_free(list);
1271 return isl_ast_graft_free(graft);
1274 lower = isl_constraint_list_drop(list, 0, data.n_indep);
1275 upper = isl_constraint_list_copy(lower);
1276 lower = isl_constraint_list_drop(lower, data.n_lower, data.n_upper);
1277 upper = isl_constraint_list_drop(upper, 0, data.n_lower);
1279 return refine_generic_bounds(graft, lower, upper, domain, build);
1282 /* Update "graft" based on "bounds" and "domain" for the generic,
1283 * non-degenerate, case.
1285 * "bounds" respresent the bounds that need to be encoded by
1286 * the for loop (or a guard around the for loop).
1287 * "domain" is the subset of "bounds" for which some code is executed.
1288 * "build" is the build in which graft->node was created.
1290 * We break up "bounds" into a list of constraints and continue with
1291 * refine_generic_split.
1293 static __isl_give isl_ast_graft *refine_generic(
1294 __isl_take isl_ast_graft *graft,
1295 __isl_keep isl_basic_set *bounds, __isl_keep isl_set *domain,
1296 __isl_keep isl_ast_build *build)
1298 isl_constraint_list *list;
1300 if (!build || !graft)
1301 return isl_ast_graft_free(graft);
1303 list = isl_basic_set_get_constraint_list(bounds);
1305 graft = refine_generic_split(graft, list, domain, build);
1307 return graft;
1310 /* Create a for node for the current level.
1312 * Mark the for node degenerate if "degenerate" is set.
1314 static __isl_give isl_ast_node *create_for(__isl_keep isl_ast_build *build,
1315 int degenerate)
1317 isl_size depth;
1318 isl_id *id;
1319 isl_ast_node *node;
1321 depth = isl_ast_build_get_depth(build);
1322 if (depth < 0)
1323 return NULL;
1325 id = isl_ast_build_get_iterator_id(build, depth);
1326 node = isl_ast_node_alloc_for(id);
1327 if (degenerate)
1328 node = isl_ast_node_for_mark_degenerate(node);
1330 return node;
1333 /* If the ast_build_exploit_nested_bounds option is set, then return
1334 * the constraints enforced by all elements in "list".
1335 * Otherwise, return the universe.
1337 static __isl_give isl_basic_set *extract_shared_enforced(
1338 __isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
1340 isl_ctx *ctx;
1341 isl_space *space;
1343 if (!list)
1344 return NULL;
1346 ctx = isl_ast_graft_list_get_ctx(list);
1347 if (isl_options_get_ast_build_exploit_nested_bounds(ctx))
1348 return isl_ast_graft_list_extract_shared_enforced(list, build);
1350 space = isl_ast_build_get_space(build, 1);
1351 return isl_basic_set_universe(space);
1354 /* Return the pending constraints of "build" that are not already taken
1355 * care of (by a combination of "enforced" and the generated constraints
1356 * of "build").
1358 static __isl_give isl_set *extract_pending(__isl_keep isl_ast_build *build,
1359 __isl_keep isl_basic_set *enforced)
1361 isl_set *guard, *context;
1363 guard = isl_ast_build_get_pending(build);
1364 context = isl_set_from_basic_set(isl_basic_set_copy(enforced));
1365 context = isl_set_intersect(context,
1366 isl_ast_build_get_generated(build));
1367 return isl_set_gist(guard, context);
1370 /* Create an AST node for the current dimension based on
1371 * the schedule domain "bounds" and return the node encapsulated
1372 * in an isl_ast_graft.
1374 * "executed" is the current inverse schedule, taking into account
1375 * the bounds in "bounds"
1376 * "domain" is the domain of "executed", with inner dimensions projected out.
1377 * It may be a strict subset of "bounds" in case "bounds" was created
1378 * based on the atomic option or based on separation with explicit bounds.
1380 * "domain" may satisfy additional equalities that result
1381 * from intersecting "executed" with "bounds" in add_node.
1382 * It may also satisfy some global constraints that were dropped out because
1383 * we performed separation with explicit bounds.
1384 * The very first step is then to copy these constraints to "bounds".
1386 * Since we may be calling before_each_for and after_each_for
1387 * callbacks, we record the current inverse schedule in the build.
1389 * We consider three builds,
1390 * "build" is the one in which the current level is created,
1391 * "body_build" is the build in which the next level is created,
1392 * "sub_build" is essentially the same as "body_build", except that
1393 * the depth has not been increased yet.
1395 * "build" already contains information (in strides and offsets)
1396 * about the strides at the current level, but this information is not
1397 * reflected in the build->domain.
1398 * We first add this information and the "bounds" to the sub_build->domain.
1399 * isl_ast_build_set_loop_bounds adds the stride information and
1400 * checks whether the current dimension attains
1401 * only a single value and whether this single value can be represented using
1402 * a single affine expression.
1403 * In the first case, the current level is considered "degenerate".
1404 * In the second, sub-case, the current level is considered "eliminated".
1405 * Eliminated levels don't need to be reflected in the AST since we can
1406 * simply plug in the affine expression. For degenerate, but non-eliminated,
1407 * levels, we do introduce a for node, but mark is as degenerate so that
1408 * it can be printed as an assignment of the single value to the loop
1409 * "iterator".
1411 * If the current level is eliminated, we explicitly plug in the value
1412 * for the current level found by isl_ast_build_set_loop_bounds in the
1413 * inverse schedule. This ensures that if we are working on a slice
1414 * of the domain based on information available in the inverse schedule
1415 * and the build domain, that then this information is also reflected
1416 * in the inverse schedule. This operation also eliminates the current
1417 * dimension from the inverse schedule making sure no inner dimensions depend
1418 * on the current dimension. Otherwise, we create a for node, marking
1419 * it degenerate if appropriate. The initial for node is still incomplete
1420 * and will be completed in either refine_degenerate or refine_generic.
1422 * We then generate a sequence of grafts for the next level,
1423 * create a surrounding graft for the current level and insert
1424 * the for node we created (if the current level is not eliminated).
1425 * Before creating a graft for the current level, we first extract
1426 * hoistable constraints from the child guards and combine them
1427 * with the pending constraints in the build. These constraints
1428 * are used to simplify the child guards and then added to the guard
1429 * of the current graft to ensure that they will be generated.
1430 * If the hoisted guard is a disjunction, then we use it directly
1431 * to gist the guards on the children before intersect it with the
1432 * pending constraints. We do so because this disjunction is typically
1433 * identical to the guards on the children such that these guards
1434 * can be effectively removed completely. After the intersection,
1435 * the gist operation would have a harder time figuring this out.
1437 * Finally, we set the bounds of the for loop in either
1438 * refine_degenerate or refine_generic.
1439 * We do so in a context where the pending constraints of the build
1440 * have been replaced by the guard of the current graft.
1442 static __isl_give isl_ast_graft *create_node_scaled(
1443 __isl_take isl_union_map *executed,
1444 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1445 __isl_take isl_ast_build *build)
1447 isl_size depth;
1448 int degenerate;
1449 isl_bool eliminated;
1450 isl_size n;
1451 isl_basic_set *hull;
1452 isl_basic_set *enforced;
1453 isl_set *guard, *hoisted;
1454 isl_ast_node *node = NULL;
1455 isl_ast_graft *graft;
1456 isl_ast_graft_list *children;
1457 isl_ast_build *sub_build;
1458 isl_ast_build *body_build;
1460 domain = isl_ast_build_eliminate_divs(build, domain);
1461 domain = isl_set_detect_equalities(domain);
1462 hull = isl_set_unshifted_simple_hull(isl_set_copy(domain));
1463 bounds = isl_basic_set_intersect(bounds, hull);
1464 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
1466 depth = isl_ast_build_get_depth(build);
1467 if (depth < 0)
1468 build = isl_ast_build_free(build);
1469 sub_build = isl_ast_build_copy(build);
1470 bounds = isl_basic_set_remove_redundancies(bounds);
1471 bounds = isl_ast_build_specialize_basic_set(sub_build, bounds);
1472 sub_build = isl_ast_build_set_loop_bounds(sub_build,
1473 isl_basic_set_copy(bounds));
1474 degenerate = isl_ast_build_has_value(sub_build);
1475 eliminated = isl_ast_build_has_affine_value(sub_build, depth);
1476 if (degenerate < 0 || eliminated < 0)
1477 executed = isl_union_map_free(executed);
1478 if (!degenerate)
1479 bounds = isl_ast_build_compute_gist_basic_set(build, bounds);
1480 sub_build = isl_ast_build_set_pending_generated(sub_build,
1481 isl_basic_set_copy(bounds));
1482 if (eliminated)
1483 executed = plug_in_values(executed, sub_build);
1484 else
1485 node = create_for(build, degenerate);
1487 body_build = isl_ast_build_copy(sub_build);
1488 body_build = isl_ast_build_increase_depth(body_build);
1489 if (!eliminated)
1490 node = before_each_for(node, body_build);
1491 children = generate_next_level(executed,
1492 isl_ast_build_copy(body_build));
1494 enforced = extract_shared_enforced(children, build);
1495 guard = extract_pending(sub_build, enforced);
1496 hoisted = isl_ast_graft_list_extract_hoistable_guard(children, build);
1497 n = isl_set_n_basic_set(hoisted);
1498 if (n < 0)
1499 children = isl_ast_graft_list_free(children);
1500 if (n > 1)
1501 children = isl_ast_graft_list_gist_guards(children,
1502 isl_set_copy(hoisted));
1503 guard = isl_set_intersect(guard, hoisted);
1504 if (!eliminated)
1505 guard = add_implied_guards(guard, degenerate, bounds, build);
1507 graft = isl_ast_graft_alloc_from_children(children,
1508 isl_set_copy(guard), enforced, build, sub_build);
1510 if (!eliminated) {
1511 isl_ast_build *for_build;
1513 graft = isl_ast_graft_insert_for(graft, node);
1514 for_build = isl_ast_build_copy(build);
1515 for_build = isl_ast_build_replace_pending_by_guard(for_build,
1516 isl_set_copy(guard));
1517 if (degenerate)
1518 graft = refine_degenerate(graft, for_build, sub_build);
1519 else
1520 graft = refine_generic(graft, bounds,
1521 domain, for_build);
1522 isl_ast_build_free(for_build);
1524 isl_set_free(guard);
1525 if (!eliminated)
1526 graft = after_each_for(graft, body_build);
1528 isl_ast_build_free(body_build);
1529 isl_ast_build_free(sub_build);
1530 isl_ast_build_free(build);
1531 isl_basic_set_free(bounds);
1532 isl_set_free(domain);
1534 return graft;
1537 /* Internal data structure for checking if all constraints involving
1538 * the input dimension "depth" are such that the other coefficients
1539 * are multiples of "m", reducing "m" if they are not.
1540 * If "m" is reduced all the way down to "1", then the check has failed
1541 * and we break out of the iteration.
1543 struct isl_check_scaled_data {
1544 int depth;
1545 isl_val *m;
1548 /* If constraint "c" involves the input dimension data->depth,
1549 * then make sure that all the other coefficients are multiples of data->m,
1550 * reducing data->m if needed.
1551 * Break out of the iteration if data->m has become equal to "1".
1553 static isl_stat constraint_check_scaled(__isl_take isl_constraint *c,
1554 void *user)
1556 struct isl_check_scaled_data *data = user;
1557 int i, j;
1558 isl_size n;
1559 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_out,
1560 isl_dim_div };
1562 if (!isl_constraint_involves_dims(c, isl_dim_in, data->depth, 1)) {
1563 isl_constraint_free(c);
1564 return isl_stat_ok;
1567 for (i = 0; i < 4; ++i) {
1568 n = isl_constraint_dim(c, t[i]);
1569 if (n < 0)
1570 break;
1571 for (j = 0; j < n; ++j) {
1572 isl_val *d;
1574 if (t[i] == isl_dim_in && j == data->depth)
1575 continue;
1576 if (!isl_constraint_involves_dims(c, t[i], j, 1))
1577 continue;
1578 d = isl_constraint_get_coefficient_val(c, t[i], j);
1579 data->m = isl_val_gcd(data->m, d);
1580 if (isl_val_is_one(data->m))
1581 break;
1583 if (j < n)
1584 break;
1587 isl_constraint_free(c);
1589 return i < 4 ? isl_stat_error : isl_stat_ok;
1592 /* For each constraint of "bmap" that involves the input dimension data->depth,
1593 * make sure that all the other coefficients are multiples of data->m,
1594 * reducing data->m if needed.
1595 * Break out of the iteration if data->m has become equal to "1".
1597 static isl_stat basic_map_check_scaled(__isl_take isl_basic_map *bmap,
1598 void *user)
1600 isl_stat r;
1602 r = isl_basic_map_foreach_constraint(bmap,
1603 &constraint_check_scaled, user);
1604 isl_basic_map_free(bmap);
1606 return r;
1609 /* For each constraint of "map" that involves the input dimension data->depth,
1610 * make sure that all the other coefficients are multiples of data->m,
1611 * reducing data->m if needed.
1612 * Break out of the iteration if data->m has become equal to "1".
1614 static isl_stat map_check_scaled(__isl_take isl_map *map, void *user)
1616 isl_stat r;
1618 r = isl_map_foreach_basic_map(map, &basic_map_check_scaled, user);
1619 isl_map_free(map);
1621 return r;
1624 /* Create an AST node for the current dimension based on
1625 * the schedule domain "bounds" and return the node encapsulated
1626 * in an isl_ast_graft.
1628 * "executed" is the current inverse schedule, taking into account
1629 * the bounds in "bounds"
1630 * "domain" is the domain of "executed", with inner dimensions projected out.
1633 * Before moving on to the actual AST node construction in create_node_scaled,
1634 * we first check if the current dimension is strided and if we can scale
1635 * down this stride. Note that we only do this if the ast_build_scale_strides
1636 * option is set.
1638 * In particular, let the current dimension take on values
1640 * f + s a
1642 * with a an integer. We check if we can find an integer m that (obviously)
1643 * divides both f and s.
1645 * If so, we check if the current dimension only appears in constraints
1646 * where the coefficients of the other variables are multiples of m.
1647 * We perform this extra check to avoid the risk of introducing
1648 * divisions by scaling down the current dimension.
1650 * If so, we scale the current dimension down by a factor of m.
1651 * That is, we plug in
1653 * i = m i' (1)
1655 * Note that in principle we could always scale down strided loops
1656 * by plugging in
1658 * i = f + s i'
1660 * but this may result in i' taking on larger values than the original i,
1661 * due to the shift by "f".
1662 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1664 static __isl_give isl_ast_graft *create_node(__isl_take isl_union_map *executed,
1665 __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1666 __isl_take isl_ast_build *build)
1668 struct isl_check_scaled_data data;
1669 isl_size depth;
1670 isl_ctx *ctx;
1671 isl_aff *offset;
1672 isl_val *d;
1674 ctx = isl_ast_build_get_ctx(build);
1675 if (!isl_options_get_ast_build_scale_strides(ctx))
1676 return create_node_scaled(executed, bounds, domain, build);
1678 depth = isl_ast_build_get_depth(build);
1679 if (depth < 0)
1680 build = isl_ast_build_free(build);
1681 data.depth = depth;
1682 if (!isl_ast_build_has_stride(build, data.depth))
1683 return create_node_scaled(executed, bounds, domain, build);
1685 offset = isl_ast_build_get_offset(build, data.depth);
1686 data.m = isl_ast_build_get_stride(build, data.depth);
1687 if (!data.m)
1688 offset = isl_aff_free(offset);
1689 offset = isl_aff_scale_down_val(offset, isl_val_copy(data.m));
1690 d = isl_aff_get_denominator_val(offset);
1691 if (!d)
1692 executed = isl_union_map_free(executed);
1694 if (executed && isl_val_is_divisible_by(data.m, d))
1695 data.m = isl_val_div(data.m, d);
1696 else {
1697 data.m = isl_val_set_si(data.m, 1);
1698 isl_val_free(d);
1701 if (!isl_val_is_one(data.m)) {
1702 if (isl_union_map_foreach_map(executed, &map_check_scaled,
1703 &data) < 0 &&
1704 !isl_val_is_one(data.m))
1705 executed = isl_union_map_free(executed);
1708 if (!isl_val_is_one(data.m)) {
1709 isl_space *space;
1710 isl_multi_aff *ma;
1711 isl_aff *aff;
1712 isl_map *map;
1713 isl_union_map *umap;
1715 space = isl_ast_build_get_space(build, 1);
1716 space = isl_space_map_from_set(space);
1717 ma = isl_multi_aff_identity(space);
1718 aff = isl_multi_aff_get_aff(ma, data.depth);
1719 aff = isl_aff_scale_val(aff, isl_val_copy(data.m));
1720 ma = isl_multi_aff_set_aff(ma, data.depth, aff);
1722 bounds = isl_basic_set_preimage_multi_aff(bounds,
1723 isl_multi_aff_copy(ma));
1724 domain = isl_set_preimage_multi_aff(domain,
1725 isl_multi_aff_copy(ma));
1726 map = isl_map_reverse(isl_map_from_multi_aff(ma));
1727 umap = isl_union_map_from_map(map);
1728 executed = isl_union_map_apply_domain(executed,
1729 isl_union_map_copy(umap));
1730 build = isl_ast_build_scale_down(build, isl_val_copy(data.m),
1731 umap);
1733 isl_aff_free(offset);
1734 isl_val_free(data.m);
1736 return create_node_scaled(executed, bounds, domain, build);
1739 /* Add the basic set to the list that "user" points to.
1741 static isl_stat collect_basic_set(__isl_take isl_basic_set *bset, void *user)
1743 isl_basic_set_list **list = user;
1745 *list = isl_basic_set_list_add(*list, bset);
1747 return isl_stat_ok;
1750 /* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1752 static __isl_give isl_basic_set_list *isl_basic_set_list_from_set(
1753 __isl_take isl_set *set)
1755 isl_size n;
1756 isl_ctx *ctx;
1757 isl_basic_set_list *list;
1759 n = isl_set_n_basic_set(set);
1760 if (n < 0)
1761 set = isl_set_free(set);
1762 if (!set)
1763 return NULL;
1765 ctx = isl_set_get_ctx(set);
1767 list = isl_basic_set_list_alloc(ctx, n);
1768 if (isl_set_foreach_basic_set(set, &collect_basic_set, &list) < 0)
1769 list = isl_basic_set_list_free(list);
1771 isl_set_free(set);
1772 return list;
1775 /* Generate code for the schedule domain "bounds"
1776 * and add the result to "list".
1778 * We mainly detect strides here and check if the bounds do not
1779 * conflict with the current build domain
1780 * and then pass over control to create_node.
1782 * "bounds" reflects the bounds on the current dimension and possibly
1783 * some extra conditions on outer dimensions.
1784 * It does not, however, include any divs involving the current dimension,
1785 * so it does not capture any stride constraints.
1786 * We therefore need to compute that part of the schedule domain that
1787 * intersects with "bounds" and derive the strides from the result.
1789 static __isl_give isl_ast_graft_list *add_node(
1790 __isl_take isl_ast_graft_list *list, __isl_take isl_union_map *executed,
1791 __isl_take isl_basic_set *bounds, __isl_take isl_ast_build *build)
1793 isl_ast_graft *graft;
1794 isl_set *domain = NULL;
1795 isl_union_set *uset;
1796 int empty, disjoint;
1798 uset = isl_union_set_from_basic_set(isl_basic_set_copy(bounds));
1799 executed = isl_union_map_intersect_domain(executed, uset);
1800 empty = isl_union_map_is_empty(executed);
1801 if (empty < 0)
1802 goto error;
1803 if (empty)
1804 goto done;
1806 uset = isl_union_map_domain(isl_union_map_copy(executed));
1807 domain = isl_set_from_union_set(uset);
1808 domain = isl_ast_build_specialize(build, domain);
1810 domain = isl_set_compute_divs(domain);
1811 domain = isl_ast_build_eliminate_inner(build, domain);
1812 disjoint = isl_set_is_disjoint(domain, build->domain);
1813 if (disjoint < 0)
1814 goto error;
1815 if (disjoint)
1816 goto done;
1818 build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
1820 graft = create_node(executed, bounds, domain,
1821 isl_ast_build_copy(build));
1822 list = isl_ast_graft_list_add(list, graft);
1823 isl_ast_build_free(build);
1824 return list;
1825 error:
1826 list = isl_ast_graft_list_free(list);
1827 done:
1828 isl_set_free(domain);
1829 isl_basic_set_free(bounds);
1830 isl_union_map_free(executed);
1831 isl_ast_build_free(build);
1832 return list;
1835 /* Does any element of i follow or coincide with any element of j
1836 * at the current depth for equal values of the outer dimensions?
1838 static isl_bool domain_follows_at_depth(__isl_keep isl_basic_set *i,
1839 __isl_keep isl_basic_set *j, void *user)
1841 int depth = *(int *) user;
1842 isl_basic_map *test;
1843 isl_bool empty;
1844 int l;
1846 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1847 isl_basic_set_copy(j));
1848 for (l = 0; l < depth; ++l)
1849 test = isl_basic_map_equate(test, isl_dim_in, l,
1850 isl_dim_out, l);
1851 test = isl_basic_map_order_ge(test, isl_dim_in, depth,
1852 isl_dim_out, depth);
1853 empty = isl_basic_map_is_empty(test);
1854 isl_basic_map_free(test);
1856 return isl_bool_not(empty);
1859 /* Split up each element of "list" into a part that is related to "bset"
1860 * according to "gt" and a part that is not.
1861 * Return a list that consist of "bset" and all the pieces.
1863 static __isl_give isl_basic_set_list *add_split_on(
1864 __isl_take isl_basic_set_list *list, __isl_take isl_basic_set *bset,
1865 __isl_keep isl_basic_map *gt)
1867 int i;
1868 isl_size n;
1869 isl_basic_set_list *res;
1871 n = isl_basic_set_list_n_basic_set(list);
1872 if (n < 0)
1873 bset = isl_basic_set_free(bset);
1875 gt = isl_basic_map_copy(gt);
1876 gt = isl_basic_map_intersect_domain(gt, isl_basic_set_copy(bset));
1877 res = isl_basic_set_list_from_basic_set(bset);
1878 for (i = 0; res && i < n; ++i) {
1879 isl_basic_set *bset;
1880 isl_set *set1, *set2;
1881 isl_basic_map *bmap;
1882 int empty;
1884 bset = isl_basic_set_list_get_basic_set(list, i);
1885 bmap = isl_basic_map_copy(gt);
1886 bmap = isl_basic_map_intersect_range(bmap, bset);
1887 bset = isl_basic_map_range(bmap);
1888 empty = isl_basic_set_is_empty(bset);
1889 if (empty < 0)
1890 res = isl_basic_set_list_free(res);
1891 if (empty) {
1892 isl_basic_set_free(bset);
1893 bset = isl_basic_set_list_get_basic_set(list, i);
1894 res = isl_basic_set_list_add(res, bset);
1895 continue;
1898 res = isl_basic_set_list_add(res, isl_basic_set_copy(bset));
1899 set1 = isl_set_from_basic_set(bset);
1900 bset = isl_basic_set_list_get_basic_set(list, i);
1901 set2 = isl_set_from_basic_set(bset);
1902 set1 = isl_set_subtract(set2, set1);
1903 set1 = isl_set_make_disjoint(set1);
1905 res = isl_basic_set_list_concat(res,
1906 isl_basic_set_list_from_set(set1));
1908 isl_basic_map_free(gt);
1909 isl_basic_set_list_free(list);
1910 return res;
1913 static __isl_give isl_ast_graft_list *generate_sorted_domains(
1914 __isl_keep isl_basic_set_list *domain_list,
1915 __isl_keep isl_union_map *executed,
1916 __isl_keep isl_ast_build *build);
1918 /* Internal data structure for add_nodes.
1920 * "executed" and "build" are extra arguments to be passed to add_node.
1921 * "list" collects the results.
1923 struct isl_add_nodes_data {
1924 isl_union_map *executed;
1925 isl_ast_build *build;
1927 isl_ast_graft_list *list;
1930 /* Generate code for the schedule domains in "scc"
1931 * and add the results to "list".
1933 * The domains in "scc" form a strongly connected component in the ordering.
1934 * If the number of domains in "scc" is larger than 1, then this means
1935 * that we cannot determine a valid ordering for the domains in the component.
1936 * This should be fairly rare because the individual domains
1937 * have been made disjoint first.
1938 * The problem is that the domains may be integrally disjoint but not
1939 * rationally disjoint. For example, we may have domains
1941 * { [i,i] : 0 <= i <= 1 } and { [i,1-i] : 0 <= i <= 1 }
1943 * These two domains have an empty intersection, but their rational
1944 * relaxations do intersect. It is impossible to order these domains
1945 * in the second dimension because the first should be ordered before
1946 * the second for outer dimension equal to 0, while it should be ordered
1947 * after for outer dimension equal to 1.
1949 * This may happen in particular in case of unrolling since the domain
1950 * of each slice is replaced by its simple hull.
1952 * For each basic set i in "scc" and for each of the following basic sets j,
1953 * we split off that part of the basic set i that shares the outer dimensions
1954 * with j and lies before j in the current dimension.
1955 * We collect all the pieces in a new list that replaces "scc".
1957 * While the elements in "scc" should be disjoint, we double-check
1958 * this property to avoid running into an infinite recursion in case
1959 * they intersect due to some internal error.
1961 static isl_stat add_nodes(__isl_take isl_basic_set_list *scc, void *user)
1963 struct isl_add_nodes_data *data = user;
1964 int i;
1965 isl_size depth;
1966 isl_size n;
1967 isl_basic_set *bset, *first;
1968 isl_basic_set_list *list;
1969 isl_space *space;
1970 isl_basic_map *gt;
1972 n = isl_basic_set_list_n_basic_set(scc);
1973 if (n < 0)
1974 goto error;
1975 bset = isl_basic_set_list_get_basic_set(scc, 0);
1976 if (n == 1) {
1977 isl_basic_set_list_free(scc);
1978 data->list = add_node(data->list,
1979 isl_union_map_copy(data->executed), bset,
1980 isl_ast_build_copy(data->build));
1981 return data->list ? isl_stat_ok : isl_stat_error;
1984 depth = isl_ast_build_get_depth(data->build);
1985 if (depth < 0)
1986 bset = isl_basic_set_free(bset);
1987 space = isl_basic_set_get_space(bset);
1988 space = isl_space_map_from_set(space);
1989 gt = isl_basic_map_universe(space);
1990 for (i = 0; i < depth; ++i)
1991 gt = isl_basic_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
1992 gt = isl_basic_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
1994 first = isl_basic_set_copy(bset);
1995 list = isl_basic_set_list_from_basic_set(bset);
1996 for (i = 1; i < n; ++i) {
1997 int disjoint;
1999 bset = isl_basic_set_list_get_basic_set(scc, i);
2001 disjoint = isl_basic_set_is_disjoint(bset, first);
2002 if (disjoint < 0)
2003 list = isl_basic_set_list_free(list);
2004 else if (!disjoint)
2005 isl_die(isl_basic_set_list_get_ctx(scc),
2006 isl_error_internal,
2007 "basic sets in scc are assumed to be disjoint",
2008 list = isl_basic_set_list_free(list));
2010 list = add_split_on(list, bset, gt);
2012 isl_basic_set_free(first);
2013 isl_basic_map_free(gt);
2014 isl_basic_set_list_free(scc);
2015 scc = list;
2016 data->list = isl_ast_graft_list_concat(data->list,
2017 generate_sorted_domains(scc, data->executed, data->build));
2018 isl_basic_set_list_free(scc);
2020 return data->list ? isl_stat_ok : isl_stat_error;
2021 error:
2022 isl_basic_set_list_free(scc);
2023 return isl_stat_error;
2026 /* Sort the domains in "domain_list" according to the execution order
2027 * at the current depth (for equal values of the outer dimensions),
2028 * generate code for each of them, collecting the results in a list.
2029 * If no code is generated (because the intersection of the inverse schedule
2030 * with the domains turns out to be empty), then an empty list is returned.
2032 * The caller is responsible for ensuring that the basic sets in "domain_list"
2033 * are pair-wise disjoint. It can, however, in principle happen that
2034 * two basic sets should be ordered one way for one value of the outer
2035 * dimensions and the other way for some other value of the outer dimensions.
2036 * We therefore play safe and look for strongly connected components.
2037 * The function add_nodes takes care of handling non-trivial components.
2039 static __isl_give isl_ast_graft_list *generate_sorted_domains(
2040 __isl_keep isl_basic_set_list *domain_list,
2041 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2043 isl_ctx *ctx;
2044 struct isl_add_nodes_data data;
2045 isl_size depth;
2046 isl_size n;
2048 n = isl_basic_set_list_n_basic_set(domain_list);
2049 if (n < 0)
2050 return NULL;
2052 ctx = isl_basic_set_list_get_ctx(domain_list);
2053 data.list = isl_ast_graft_list_alloc(ctx, n);
2054 if (n == 0)
2055 return data.list;
2056 if (n == 1)
2057 return add_node(data.list, isl_union_map_copy(executed),
2058 isl_basic_set_list_get_basic_set(domain_list, 0),
2059 isl_ast_build_copy(build));
2061 depth = isl_ast_build_get_depth(build);
2062 data.executed = executed;
2063 data.build = build;
2064 if (depth < 0 || isl_basic_set_list_foreach_scc(domain_list,
2065 &domain_follows_at_depth, &depth,
2066 &add_nodes, &data) < 0)
2067 data.list = isl_ast_graft_list_free(data.list);
2069 return data.list;
2072 /* Do i and j share any values for the outer dimensions?
2074 static isl_bool shared_outer(__isl_keep isl_basic_set *i,
2075 __isl_keep isl_basic_set *j, void *user)
2077 int depth = *(int *) user;
2078 isl_basic_map *test;
2079 isl_bool empty;
2080 int l;
2082 test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
2083 isl_basic_set_copy(j));
2084 for (l = 0; l < depth; ++l)
2085 test = isl_basic_map_equate(test, isl_dim_in, l,
2086 isl_dim_out, l);
2087 empty = isl_basic_map_is_empty(test);
2088 isl_basic_map_free(test);
2090 return isl_bool_not(empty);
2093 /* Internal data structure for generate_sorted_domains_wrap.
2095 * "n" is the total number of basic sets
2096 * "executed" and "build" are extra arguments to be passed
2097 * to generate_sorted_domains.
2099 * "single" is set to 1 by generate_sorted_domains_wrap if there
2100 * is only a single component.
2101 * "list" collects the results.
2103 struct isl_ast_generate_parallel_domains_data {
2104 isl_size n;
2105 isl_union_map *executed;
2106 isl_ast_build *build;
2108 int single;
2109 isl_ast_graft_list *list;
2112 /* Call generate_sorted_domains on "scc", fuse the result into a list
2113 * with either zero or one graft and collect the these single element
2114 * lists into data->list.
2116 * If there is only one component, i.e., if the number of basic sets
2117 * in the current component is equal to the total number of basic sets,
2118 * then data->single is set to 1 and the result of generate_sorted_domains
2119 * is not fused.
2121 static isl_stat generate_sorted_domains_wrap(__isl_take isl_basic_set_list *scc,
2122 void *user)
2124 struct isl_ast_generate_parallel_domains_data *data = user;
2125 isl_ast_graft_list *list;
2126 isl_size n;
2128 n = isl_basic_set_list_n_basic_set(scc);
2129 if (n < 0)
2130 scc = isl_basic_set_list_free(scc);
2131 list = generate_sorted_domains(scc, data->executed, data->build);
2132 data->single = n == data->n;
2133 if (!data->single)
2134 list = isl_ast_graft_list_fuse(list, data->build);
2135 if (!data->list)
2136 data->list = list;
2137 else
2138 data->list = isl_ast_graft_list_concat(data->list, list);
2140 isl_basic_set_list_free(scc);
2141 if (!data->list)
2142 return isl_stat_error;
2144 return isl_stat_ok;
2147 /* Look for any (weakly connected) components in the "domain_list"
2148 * of domains that share some values of the outer dimensions.
2149 * That is, domains in different components do not share any values
2150 * of the outer dimensions. This means that these components
2151 * can be freely reordered.
2152 * Within each of the components, we sort the domains according
2153 * to the execution order at the current depth.
2155 * If there is more than one component, then generate_sorted_domains_wrap
2156 * fuses the result of each call to generate_sorted_domains
2157 * into a list with either zero or one graft and collects these (at most)
2158 * single element lists into a bigger list. This means that the elements of the
2159 * final list can be freely reordered. In particular, we sort them
2160 * according to an arbitrary but fixed ordering to ease merging of
2161 * graft lists from different components.
2163 static __isl_give isl_ast_graft_list *generate_parallel_domains(
2164 __isl_keep isl_basic_set_list *domain_list,
2165 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2167 isl_size depth;
2168 struct isl_ast_generate_parallel_domains_data data;
2170 data.n = isl_basic_set_list_n_basic_set(domain_list);
2171 if (data.n < 0)
2172 return NULL;
2174 if (data.n <= 1)
2175 return generate_sorted_domains(domain_list, executed, build);
2177 depth = isl_ast_build_get_depth(build);
2178 if (depth < 0)
2179 return NULL;
2180 data.list = NULL;
2181 data.executed = executed;
2182 data.build = build;
2183 data.single = 0;
2184 if (isl_basic_set_list_foreach_scc(domain_list, &shared_outer, &depth,
2185 &generate_sorted_domains_wrap,
2186 &data) < 0)
2187 data.list = isl_ast_graft_list_free(data.list);
2189 if (!data.single)
2190 data.list = isl_ast_graft_list_sort_guard(data.list);
2192 return data.list;
2195 /* Internal data for separate_domain.
2197 * "explicit" is set if we only want to use explicit bounds.
2199 * "domain" collects the separated domains.
2201 struct isl_separate_domain_data {
2202 isl_ast_build *build;
2203 int explicit;
2204 isl_set *domain;
2207 /* Extract implicit bounds on the current dimension for the executed "map".
2209 * The domain of "map" may involve inner dimensions, so we
2210 * need to eliminate them.
2212 static __isl_give isl_set *implicit_bounds(__isl_take isl_map *map,
2213 __isl_keep isl_ast_build *build)
2215 isl_set *domain;
2217 domain = isl_map_domain(map);
2218 domain = isl_ast_build_eliminate(build, domain);
2220 return domain;
2223 /* Extract explicit bounds on the current dimension for the executed "map".
2225 * Rather than eliminating the inner dimensions as in implicit_bounds,
2226 * we simply drop any constraints involving those inner dimensions.
2227 * The idea is that most bounds that are implied by constraints on the
2228 * inner dimensions will be enforced by for loops and not by explicit guards.
2229 * There is then no need to separate along those bounds.
2231 static __isl_give isl_set *explicit_bounds(__isl_take isl_map *map,
2232 __isl_keep isl_ast_build *build)
2234 isl_set *domain;
2235 isl_size depth;
2236 isl_size dim;
2238 depth = isl_ast_build_get_depth(build);
2239 dim = isl_map_dim(map, isl_dim_out);
2240 if (depth < 0 || dim < 0)
2241 return isl_map_domain(isl_map_free(map));
2242 map = isl_map_drop_constraints_involving_dims(map, isl_dim_out, 0, dim);
2244 domain = isl_map_domain(map);
2245 dim = isl_set_dim(domain, isl_dim_set);
2246 domain = isl_set_detect_equalities(domain);
2247 domain = isl_set_drop_constraints_involving_dims(domain,
2248 isl_dim_set, depth + 1, dim - (depth + 1));
2249 domain = isl_set_remove_divs_involving_dims(domain,
2250 isl_dim_set, depth, 1);
2251 domain = isl_set_remove_unknown_divs(domain);
2253 return domain;
2256 /* Split data->domain into pieces that intersect with the range of "map"
2257 * and pieces that do not intersect with the range of "map"
2258 * and then add that part of the range of "map" that does not intersect
2259 * with data->domain.
2261 static isl_stat separate_domain(__isl_take isl_map *map, void *user)
2263 struct isl_separate_domain_data *data = user;
2264 isl_set *domain;
2265 isl_set *d1, *d2;
2267 if (data->explicit)
2268 domain = explicit_bounds(map, data->build);
2269 else
2270 domain = implicit_bounds(map, data->build);
2272 domain = isl_set_coalesce(domain);
2273 domain = isl_set_make_disjoint(domain);
2274 d1 = isl_set_subtract(isl_set_copy(domain), isl_set_copy(data->domain));
2275 d2 = isl_set_subtract(isl_set_copy(data->domain), isl_set_copy(domain));
2276 data->domain = isl_set_intersect(data->domain, domain);
2277 data->domain = isl_set_union(data->domain, d1);
2278 data->domain = isl_set_union(data->domain, d2);
2280 return isl_stat_ok;
2283 /* Separate the schedule domains of "executed".
2285 * That is, break up the domain of "executed" into basic sets,
2286 * such that for each basic set S, every element in S is associated with
2287 * the same domain spaces.
2289 * "space" is the (single) domain space of "executed".
2291 static __isl_give isl_set *separate_schedule_domains(
2292 __isl_take isl_space *space, __isl_take isl_union_map *executed,
2293 __isl_keep isl_ast_build *build)
2295 struct isl_separate_domain_data data = { build };
2296 isl_ctx *ctx;
2298 ctx = isl_ast_build_get_ctx(build);
2299 data.explicit = isl_options_get_ast_build_separation_bounds(ctx) ==
2300 ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT;
2301 data.domain = isl_set_empty(space);
2302 if (isl_union_map_foreach_map(executed, &separate_domain, &data) < 0)
2303 data.domain = isl_set_free(data.domain);
2305 isl_union_map_free(executed);
2306 return data.domain;
2309 /* Temporary data used during the search for a lower bound for unrolling.
2311 * "build" is the build in which the unrolling will be performed
2312 * "domain" is the original set for which to find a lower bound
2313 * "depth" is the dimension for which to find a lower boudn
2314 * "expansion" is the expansion that needs to be applied to "domain"
2315 * in the unrolling that will be performed
2317 * "lower" is the best lower bound found so far. It is NULL if we have not
2318 * found any yet.
2319 * "n" is the corresponding size. If lower is NULL, then the value of n
2320 * is undefined.
2321 * "n_div" is the maximal number of integer divisions in the first
2322 * unrolled iteration (after expansion). It is set to -1 if it hasn't
2323 * been computed yet.
2325 struct isl_find_unroll_data {
2326 isl_ast_build *build;
2327 isl_set *domain;
2328 int depth;
2329 isl_basic_map *expansion;
2331 isl_aff *lower;
2332 int *n;
2333 int n_div;
2336 /* Return the constraint
2338 * i_"depth" = aff + offset
2340 static __isl_give isl_constraint *at_offset(int depth, __isl_keep isl_aff *aff,
2341 int offset)
2343 aff = isl_aff_copy(aff);
2344 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, depth, -1);
2345 aff = isl_aff_add_constant_si(aff, offset);
2346 return isl_equality_from_aff(aff);
2349 /* Update *user to the number of integer divisions in the first element
2350 * of "ma", if it is larger than the current value.
2352 static isl_stat update_n_div(__isl_take isl_set *set,
2353 __isl_take isl_multi_aff *ma, void *user)
2355 isl_aff *aff;
2356 int *n = user;
2357 isl_size n_div;
2359 aff = isl_multi_aff_get_aff(ma, 0);
2360 n_div = isl_aff_dim(aff, isl_dim_div);
2361 isl_aff_free(aff);
2362 isl_multi_aff_free(ma);
2363 isl_set_free(set);
2365 if (n_div > *n)
2366 *n = n_div;
2368 return n_div >= 0 ? isl_stat_ok : isl_stat_error;
2371 /* Get the number of integer divisions in the expression for the iterator
2372 * value at the first slice in the unrolling based on lower bound "lower",
2373 * taking into account the expansion that needs to be performed on this slice.
2375 static int get_expanded_n_div(struct isl_find_unroll_data *data,
2376 __isl_keep isl_aff *lower)
2378 isl_constraint *c;
2379 isl_set *set;
2380 isl_map *it_map, *expansion;
2381 isl_pw_multi_aff *pma;
2382 int n;
2384 c = at_offset(data->depth, lower, 0);
2385 set = isl_set_copy(data->domain);
2386 set = isl_set_add_constraint(set, c);
2387 expansion = isl_map_from_basic_map(isl_basic_map_copy(data->expansion));
2388 set = isl_set_apply(set, expansion);
2389 it_map = isl_ast_build_map_to_iterator(data->build, set);
2390 pma = isl_pw_multi_aff_from_map(it_map);
2391 n = 0;
2392 if (isl_pw_multi_aff_foreach_piece(pma, &update_n_div, &n) < 0)
2393 n = -1;
2394 isl_pw_multi_aff_free(pma);
2396 return n;
2399 /* Is the lower bound "lower" with corresponding iteration count "n"
2400 * better than the one stored in "data"?
2401 * If there is no upper bound on the iteration count ("n" is infinity) or
2402 * if the count is too large, then we cannot use this lower bound.
2403 * Otherwise, if there was no previous lower bound or
2404 * if the iteration count of the new lower bound is smaller than
2405 * the iteration count of the previous lower bound, then we consider
2406 * the new lower bound to be better.
2407 * If the iteration count is the same, then compare the number
2408 * of integer divisions that would be needed to express
2409 * the iterator value at the first slice in the unrolling
2410 * according to the lower bound. If we end up computing this
2411 * number, then store the lowest value in data->n_div.
2413 static int is_better_lower_bound(struct isl_find_unroll_data *data,
2414 __isl_keep isl_aff *lower, __isl_keep isl_val *n)
2416 int cmp;
2417 int n_div;
2419 if (!n)
2420 return -1;
2421 if (isl_val_is_infty(n))
2422 return 0;
2423 if (isl_val_cmp_si(n, INT_MAX) > 0)
2424 return 0;
2425 if (!data->lower)
2426 return 1;
2427 cmp = isl_val_cmp_si(n, *data->n);
2428 if (cmp < 0)
2429 return 1;
2430 if (cmp > 0)
2431 return 0;
2432 if (data->n_div < 0)
2433 data->n_div = get_expanded_n_div(data, data->lower);
2434 if (data->n_div < 0)
2435 return -1;
2436 if (data->n_div == 0)
2437 return 0;
2438 n_div = get_expanded_n_div(data, lower);
2439 if (n_div < 0)
2440 return -1;
2441 if (n_div >= data->n_div)
2442 return 0;
2443 data->n_div = n_div;
2445 return 1;
2448 /* Check if we can use "c" as a lower bound and if it is better than
2449 * any previously found lower bound.
2451 * If "c" does not involve the dimension at the current depth,
2452 * then we cannot use it.
2453 * Otherwise, let "c" be of the form
2455 * i >= f(j)/a
2457 * We compute the maximal value of
2459 * -ceil(f(j)/a)) + i + 1
2461 * over the domain. If there is such a value "n", then we know
2463 * -ceil(f(j)/a)) + i + 1 <= n
2465 * or
2467 * i < ceil(f(j)/a)) + n
2469 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2470 * We just need to check if we have found any lower bound before and
2471 * if the new lower bound is better (smaller n or fewer integer divisions)
2472 * than the previously found lower bounds.
2474 static isl_stat update_unrolling_lower_bound(struct isl_find_unroll_data *data,
2475 __isl_keep isl_constraint *c)
2477 isl_aff *aff, *lower;
2478 isl_val *max;
2479 int better;
2481 if (!isl_constraint_is_lower_bound(c, isl_dim_set, data->depth))
2482 return isl_stat_ok;
2484 lower = isl_constraint_get_bound(c, isl_dim_set, data->depth);
2485 lower = isl_aff_ceil(lower);
2486 aff = isl_aff_copy(lower);
2487 aff = isl_aff_neg(aff);
2488 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, data->depth, 1);
2489 aff = isl_aff_add_constant_si(aff, 1);
2490 max = isl_set_max_val(data->domain, aff);
2491 isl_aff_free(aff);
2493 better = is_better_lower_bound(data, lower, max);
2494 if (better < 0 || !better) {
2495 isl_val_free(max);
2496 isl_aff_free(lower);
2497 return better < 0 ? isl_stat_error : isl_stat_ok;
2500 isl_aff_free(data->lower);
2501 data->lower = lower;
2502 *data->n = isl_val_get_num_si(max);
2503 isl_val_free(max);
2505 return isl_stat_ok;
2508 /* Check if we can use "c" as a lower bound and if it is better than
2509 * any previously found lower bound.
2511 static isl_stat constraint_find_unroll(__isl_take isl_constraint *c, void *user)
2513 struct isl_find_unroll_data *data;
2514 isl_stat r;
2516 data = (struct isl_find_unroll_data *) user;
2517 r = update_unrolling_lower_bound(data, c);
2518 isl_constraint_free(c);
2520 return r;
2523 /* Look for a lower bound l(i) on the dimension at "depth"
2524 * and a size n such that "domain" is a subset of
2526 * { [i] : l(i) <= i_d < l(i) + n }
2528 * where d is "depth" and l(i) depends only on earlier dimensions.
2529 * Furthermore, try and find a lower bound such that n is as small as possible.
2530 * In particular, "n" needs to be finite.
2531 * "build" is the build in which the unrolling will be performed.
2532 * "expansion" is the expansion that needs to be applied to "domain"
2533 * in the unrolling that will be performed.
2535 * Inner dimensions have been eliminated from "domain" by the caller.
2537 * We first construct a collection of lower bounds on the input set
2538 * by computing its simple hull. We then iterate through them,
2539 * discarding those that we cannot use (either because they do not
2540 * involve the dimension at "depth" or because they have no corresponding
2541 * upper bound, meaning that "n" would be unbounded) and pick out the
2542 * best from the remaining ones.
2544 * If we cannot find a suitable lower bound, then we consider that
2545 * to be an error.
2547 static __isl_give isl_aff *find_unroll_lower_bound(
2548 __isl_keep isl_ast_build *build, __isl_keep isl_set *domain,
2549 int depth, __isl_keep isl_basic_map *expansion, int *n)
2551 struct isl_find_unroll_data data =
2552 { build, domain, depth, expansion, NULL, n, -1 };
2553 isl_basic_set *hull;
2555 hull = isl_set_simple_hull(isl_set_copy(domain));
2557 if (isl_basic_set_foreach_constraint(hull,
2558 &constraint_find_unroll, &data) < 0)
2559 goto error;
2561 isl_basic_set_free(hull);
2563 if (!data.lower)
2564 isl_die(isl_set_get_ctx(domain), isl_error_invalid,
2565 "cannot find lower bound for unrolling", return NULL);
2567 return data.lower;
2568 error:
2569 isl_basic_set_free(hull);
2570 return isl_aff_free(data.lower);
2573 /* Call "fn" on each iteration of the current dimension of "domain".
2574 * If "init" is not NULL, then it is called with the number of
2575 * iterations before any call to "fn".
2576 * Return -1 on failure.
2578 * Since we are going to be iterating over the individual values,
2579 * we first check if there are any strides on the current dimension.
2580 * If there is, we rewrite the current dimension i as
2582 * i = stride i' + offset
2584 * and then iterate over individual values of i' instead.
2586 * We then look for a lower bound on i' and a size such that the domain
2587 * is a subset of
2589 * { [j,i'] : l(j) <= i' < l(j) + n }
2591 * and then take slices of the domain at values of i'
2592 * between l(j) and l(j) + n - 1.
2594 * We compute the unshifted simple hull of each slice to ensure that
2595 * we have a single basic set per offset. The slicing constraint
2596 * may get simplified away before the unshifted simple hull is taken
2597 * and may therefore in some rare cases disappear from the result.
2598 * We therefore explicitly add the constraint back after computing
2599 * the unshifted simple hull to ensure that the basic sets
2600 * remain disjoint. The constraints that are dropped by taking the hull
2601 * will be taken into account at the next level, as in the case of the
2602 * atomic option.
2604 * Finally, we map i' back to i and call "fn".
2606 static int foreach_iteration(__isl_take isl_set *domain,
2607 __isl_keep isl_ast_build *build, int (*init)(int n, void *user),
2608 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
2610 int i, n;
2611 isl_bool empty;
2612 isl_size depth;
2613 isl_multi_aff *expansion;
2614 isl_basic_map *bmap;
2615 isl_aff *lower = NULL;
2616 isl_ast_build *stride_build;
2618 depth = isl_ast_build_get_depth(build);
2619 if (depth < 0)
2620 domain = isl_set_free(domain);
2622 domain = isl_ast_build_eliminate_inner(build, domain);
2623 domain = isl_set_intersect(domain, isl_ast_build_get_domain(build));
2624 stride_build = isl_ast_build_copy(build);
2625 stride_build = isl_ast_build_detect_strides(stride_build,
2626 isl_set_copy(domain));
2627 expansion = isl_ast_build_get_stride_expansion(stride_build);
2629 domain = isl_set_preimage_multi_aff(domain,
2630 isl_multi_aff_copy(expansion));
2631 domain = isl_ast_build_eliminate_divs(stride_build, domain);
2632 isl_ast_build_free(stride_build);
2634 bmap = isl_basic_map_from_multi_aff(expansion);
2636 empty = isl_set_is_empty(domain);
2637 if (empty < 0) {
2638 n = -1;
2639 } else if (empty) {
2640 n = 0;
2641 } else {
2642 lower = find_unroll_lower_bound(build, domain, depth, bmap, &n);
2643 if (!lower)
2644 n = -1;
2646 if (n >= 0 && init && init(n, user) < 0)
2647 n = -1;
2648 for (i = 0; i < n; ++i) {
2649 isl_set *set;
2650 isl_basic_set *bset;
2651 isl_constraint *slice;
2653 slice = at_offset(depth, lower, i);
2654 set = isl_set_copy(domain);
2655 set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2656 bset = isl_set_unshifted_simple_hull(set);
2657 bset = isl_basic_set_add_constraint(bset, slice);
2658 bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2660 if (fn(bset, user) < 0)
2661 break;
2664 isl_aff_free(lower);
2665 isl_set_free(domain);
2666 isl_basic_map_free(bmap);
2668 return n < 0 || i < n ? -1 : 0;
2671 /* Data structure for storing the results and the intermediate objects
2672 * of compute_domains.
2674 * "list" is the main result of the function and contains a list
2675 * of disjoint basic sets for which code should be generated.
2677 * "executed" and "build" are inputs to compute_domains.
2678 * "schedule_domain" is the domain of "executed".
2680 * "option" contains the domains at the current depth that should by
2681 * atomic, separated or unrolled. These domains are as specified by
2682 * the user, except that inner dimensions have been eliminated and
2683 * that they have been made pair-wise disjoint.
2685 * "sep_class" contains the user-specified split into separation classes
2686 * specialized to the current depth.
2687 * "done" contains the union of the separation domains that have already
2688 * been handled.
2690 struct isl_codegen_domains {
2691 isl_basic_set_list *list;
2693 isl_union_map *executed;
2694 isl_ast_build *build;
2695 isl_set *schedule_domain;
2697 isl_set *option[4];
2699 isl_map *sep_class;
2700 isl_set *done;
2703 /* Internal data structure for do_unroll.
2705 * "domains" stores the results of compute_domains.
2706 * "class_domain" is the original class domain passed to do_unroll.
2707 * "unroll_domain" collects the unrolled iterations.
2709 struct isl_ast_unroll_data {
2710 struct isl_codegen_domains *domains;
2711 isl_set *class_domain;
2712 isl_set *unroll_domain;
2715 /* Given an iteration of an unrolled domain represented by "bset",
2716 * add it to data->domains->list.
2717 * Since we may have dropped some constraints, we intersect with
2718 * the class domain again to ensure that each element in the list
2719 * is disjoint from the other class domains.
2721 static int do_unroll_iteration(__isl_take isl_basic_set *bset, void *user)
2723 struct isl_ast_unroll_data *data = user;
2724 isl_set *set;
2725 isl_basic_set_list *list;
2727 set = isl_set_from_basic_set(bset);
2728 data->unroll_domain = isl_set_union(data->unroll_domain,
2729 isl_set_copy(set));
2730 set = isl_set_intersect(set, isl_set_copy(data->class_domain));
2731 set = isl_set_make_disjoint(set);
2732 list = isl_basic_set_list_from_set(set);
2733 data->domains->list = isl_basic_set_list_concat(data->domains->list,
2734 list);
2736 return 0;
2739 /* Extend domains->list with a list of basic sets, one for each value
2740 * of the current dimension in "domain" and remove the corresponding
2741 * sets from the class domain. Return the updated class domain.
2742 * The divs that involve the current dimension have not been projected out
2743 * from this domain.
2745 * We call foreach_iteration to iterate over the individual values and
2746 * in do_unroll_iteration we collect the individual basic sets in
2747 * domains->list and their union in data->unroll_domain, which is then
2748 * used to update the class domain.
2750 static __isl_give isl_set *do_unroll(struct isl_codegen_domains *domains,
2751 __isl_take isl_set *domain, __isl_take isl_set *class_domain)
2753 struct isl_ast_unroll_data data;
2755 if (!domain)
2756 return isl_set_free(class_domain);
2757 if (!class_domain)
2758 return isl_set_free(domain);
2760 data.domains = domains;
2761 data.class_domain = class_domain;
2762 data.unroll_domain = isl_set_empty(isl_set_get_space(domain));
2764 if (foreach_iteration(domain, domains->build, NULL,
2765 &do_unroll_iteration, &data) < 0)
2766 data.unroll_domain = isl_set_free(data.unroll_domain);
2768 class_domain = isl_set_subtract(class_domain, data.unroll_domain);
2770 return class_domain;
2773 /* Add domains to domains->list for each individual value of the current
2774 * dimension, for that part of the schedule domain that lies in the
2775 * intersection of the option domain and the class domain.
2776 * Remove the corresponding sets from the class domain and
2777 * return the updated class domain.
2779 * We first break up the unroll option domain into individual pieces
2780 * and then handle each of them separately. The unroll option domain
2781 * has been made disjoint in compute_domains_init_options,
2783 * Note that we actively want to combine different pieces of the
2784 * schedule domain that have the same value at the current dimension.
2785 * We therefore need to break up the unroll option domain before
2786 * intersecting with class and schedule domain, hoping that the
2787 * unroll option domain specified by the user is relatively simple.
2789 static __isl_give isl_set *compute_unroll_domains(
2790 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2792 isl_set *unroll_domain;
2793 isl_basic_set_list *unroll_list;
2794 int i;
2795 isl_size n;
2796 isl_bool empty;
2798 empty = isl_set_is_empty(domains->option[isl_ast_loop_unroll]);
2799 if (empty < 0)
2800 return isl_set_free(class_domain);
2801 if (empty)
2802 return class_domain;
2804 unroll_domain = isl_set_copy(domains->option[isl_ast_loop_unroll]);
2805 unroll_list = isl_basic_set_list_from_set(unroll_domain);
2807 n = isl_basic_set_list_n_basic_set(unroll_list);
2808 if (n < 0)
2809 class_domain = isl_set_free(class_domain);
2810 for (i = 0; i < n; ++i) {
2811 isl_basic_set *bset;
2813 bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2814 unroll_domain = isl_set_from_basic_set(bset);
2815 unroll_domain = isl_set_intersect(unroll_domain,
2816 isl_set_copy(class_domain));
2817 unroll_domain = isl_set_intersect(unroll_domain,
2818 isl_set_copy(domains->schedule_domain));
2820 empty = isl_set_is_empty(unroll_domain);
2821 if (empty >= 0 && empty) {
2822 isl_set_free(unroll_domain);
2823 continue;
2826 class_domain = do_unroll(domains, unroll_domain, class_domain);
2829 isl_basic_set_list_free(unroll_list);
2831 return class_domain;
2834 /* Try and construct a single basic set that includes the intersection of
2835 * the schedule domain, the atomic option domain and the class domain.
2836 * Add the resulting basic set(s) to domains->list and remove them
2837 * from class_domain. Return the updated class domain.
2839 * We construct a single domain rather than trying to combine
2840 * the schedule domains of individual domains because we are working
2841 * within a single component so that non-overlapping schedule domains
2842 * should already have been separated.
2843 * We do however need to make sure that this single domains is a subset
2844 * of the class domain so that it would not intersect with any other
2845 * class domains. This means that we may end up splitting up the atomic
2846 * domain in case separation classes are being used.
2848 * "domain" is the intersection of the schedule domain and the class domain,
2849 * with inner dimensions projected out.
2851 static __isl_give isl_set *compute_atomic_domain(
2852 struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2854 isl_basic_set *bset;
2855 isl_basic_set_list *list;
2856 isl_set *domain, *atomic_domain;
2857 int empty;
2859 domain = isl_set_copy(domains->option[isl_ast_loop_atomic]);
2860 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2861 domain = isl_set_intersect(domain,
2862 isl_set_copy(domains->schedule_domain));
2863 empty = isl_set_is_empty(domain);
2864 if (empty < 0)
2865 class_domain = isl_set_free(class_domain);
2866 if (empty) {
2867 isl_set_free(domain);
2868 return class_domain;
2871 domain = isl_ast_build_eliminate(domains->build, domain);
2872 domain = isl_set_coalesce_preserve(domain);
2873 bset = isl_set_unshifted_simple_hull(domain);
2874 domain = isl_set_from_basic_set(bset);
2875 atomic_domain = isl_set_copy(domain);
2876 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2877 class_domain = isl_set_subtract(class_domain, atomic_domain);
2878 domain = isl_set_make_disjoint(domain);
2879 list = isl_basic_set_list_from_set(domain);
2880 domains->list = isl_basic_set_list_concat(domains->list, list);
2882 return class_domain;
2885 /* Split up the schedule domain into uniform basic sets,
2886 * in the sense that each element in a basic set is associated to
2887 * elements of the same domains, and add the result to domains->list.
2888 * Do this for that part of the schedule domain that lies in the
2889 * intersection of "class_domain" and the separate option domain.
2891 * "class_domain" may or may not include the constraints
2892 * of the schedule domain, but this does not make a difference
2893 * since we are going to intersect it with the domain of the inverse schedule.
2894 * If it includes schedule domain constraints, then they may involve
2895 * inner dimensions, but we will eliminate them in separation_domain.
2897 static int compute_separate_domain(struct isl_codegen_domains *domains,
2898 __isl_keep isl_set *class_domain)
2900 isl_space *space;
2901 isl_set *domain;
2902 isl_union_map *executed;
2903 isl_basic_set_list *list;
2904 int empty;
2906 domain = isl_set_copy(domains->option[isl_ast_loop_separate]);
2907 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2908 executed = isl_union_map_copy(domains->executed);
2909 executed = isl_union_map_intersect_domain(executed,
2910 isl_union_set_from_set(domain));
2911 empty = isl_union_map_is_empty(executed);
2912 if (empty < 0 || empty) {
2913 isl_union_map_free(executed);
2914 return empty < 0 ? -1 : 0;
2917 space = isl_set_get_space(class_domain);
2918 domain = separate_schedule_domains(space, executed, domains->build);
2920 list = isl_basic_set_list_from_set(domain);
2921 domains->list = isl_basic_set_list_concat(domains->list, list);
2923 return 0;
2926 /* Split up the domain at the current depth into disjoint
2927 * basic sets for which code should be generated separately
2928 * for the given separation class domain.
2930 * If any separation classes have been defined, then "class_domain"
2931 * is the domain of the current class and does not refer to inner dimensions.
2932 * Otherwise, "class_domain" is the universe domain.
2934 * We first make sure that the class domain is disjoint from
2935 * previously considered class domains.
2937 * The separate domains can be computed directly from the "class_domain".
2939 * The unroll, atomic and remainder domains need the constraints
2940 * from the schedule domain.
2942 * For unrolling, the actual schedule domain is needed (with divs that
2943 * may refer to the current dimension) so that stride detection can be
2944 * performed.
2946 * For atomic and remainder domains, inner dimensions and divs involving
2947 * the current dimensions should be eliminated.
2948 * In case we are working within a separation class, we need to intersect
2949 * the result with the current "class_domain" to ensure that the domains
2950 * are disjoint from those generated from other class domains.
2952 * The domain that has been made atomic may be larger than specified
2953 * by the user since it needs to be representable as a single basic set.
2954 * This possibly larger domain is removed from class_domain by
2955 * compute_atomic_domain. It is computed first so that the extended domain
2956 * would not overlap with any domains computed before.
2957 * Similary, the unrolled domains may have some constraints removed and
2958 * may therefore also be larger than specified by the user.
2960 * If anything is left after handling separate, unroll and atomic,
2961 * we split it up into basic sets and append the basic sets to domains->list.
2963 static isl_stat compute_partial_domains(struct isl_codegen_domains *domains,
2964 __isl_take isl_set *class_domain)
2966 isl_basic_set_list *list;
2967 isl_set *domain;
2969 class_domain = isl_set_subtract(class_domain,
2970 isl_set_copy(domains->done));
2971 domains->done = isl_set_union(domains->done,
2972 isl_set_copy(class_domain));
2974 class_domain = compute_atomic_domain(domains, class_domain);
2975 class_domain = compute_unroll_domains(domains, class_domain);
2977 domain = isl_set_copy(class_domain);
2979 if (compute_separate_domain(domains, domain) < 0)
2980 goto error;
2981 domain = isl_set_subtract(domain,
2982 isl_set_copy(domains->option[isl_ast_loop_separate]));
2984 domain = isl_set_intersect(domain,
2985 isl_set_copy(domains->schedule_domain));
2987 domain = isl_ast_build_eliminate(domains->build, domain);
2988 domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2990 domain = isl_set_coalesce_preserve(domain);
2991 domain = isl_set_make_disjoint(domain);
2993 list = isl_basic_set_list_from_set(domain);
2994 domains->list = isl_basic_set_list_concat(domains->list, list);
2996 isl_set_free(class_domain);
2998 return isl_stat_ok;
2999 error:
3000 isl_set_free(domain);
3001 isl_set_free(class_domain);
3002 return isl_stat_error;
3005 /* Split up the domain at the current depth into disjoint
3006 * basic sets for which code should be generated separately
3007 * for the separation class identified by "pnt".
3009 * We extract the corresponding class domain from domains->sep_class,
3010 * eliminate inner dimensions and pass control to compute_partial_domains.
3012 static isl_stat compute_class_domains(__isl_take isl_point *pnt, void *user)
3014 struct isl_codegen_domains *domains = user;
3015 isl_set *class_set;
3016 isl_set *domain;
3017 int disjoint;
3019 class_set = isl_set_from_point(pnt);
3020 domain = isl_map_domain(isl_map_intersect_range(
3021 isl_map_copy(domains->sep_class), class_set));
3022 domain = isl_ast_build_compute_gist(domains->build, domain);
3023 domain = isl_ast_build_eliminate(domains->build, domain);
3025 disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
3026 if (disjoint < 0)
3027 return isl_stat_error;
3028 if (disjoint) {
3029 isl_set_free(domain);
3030 return isl_stat_ok;
3033 return compute_partial_domains(domains, domain);
3036 /* Extract the domains at the current depth that should be atomic,
3037 * separated or unrolled and store them in option.
3039 * The domains specified by the user might overlap, so we make
3040 * them disjoint by subtracting earlier domains from later domains.
3042 static void compute_domains_init_options(isl_set *option[4],
3043 __isl_keep isl_ast_build *build)
3045 enum isl_ast_loop_type type, type2;
3046 isl_set *unroll;
3048 for (type = isl_ast_loop_atomic;
3049 type <= isl_ast_loop_separate; ++type) {
3050 option[type] = isl_ast_build_get_option_domain(build, type);
3051 for (type2 = isl_ast_loop_atomic; type2 < type; ++type2)
3052 option[type] = isl_set_subtract(option[type],
3053 isl_set_copy(option[type2]));
3056 unroll = option[isl_ast_loop_unroll];
3057 unroll = isl_set_coalesce(unroll);
3058 unroll = isl_set_make_disjoint(unroll);
3059 option[isl_ast_loop_unroll] = unroll;
3062 /* Split up the domain at the current depth into disjoint
3063 * basic sets for which code should be generated separately,
3064 * based on the user-specified options.
3065 * Return the list of disjoint basic sets.
3067 * There are three kinds of domains that we need to keep track of.
3068 * - the "schedule domain" is the domain of "executed"
3069 * - the "class domain" is the domain corresponding to the currrent
3070 * separation class
3071 * - the "option domain" is the domain corresponding to one of the options
3072 * atomic, unroll or separate
3074 * We first consider the individial values of the separation classes
3075 * and split up the domain for each of them separately.
3076 * Finally, we consider the remainder. If no separation classes were
3077 * specified, then we call compute_partial_domains with the universe
3078 * "class_domain". Otherwise, we take the "schedule_domain" as "class_domain",
3079 * with inner dimensions removed. We do this because we want to
3080 * avoid computing the complement of the class domains (i.e., the difference
3081 * between the universe and domains->done).
3083 static __isl_give isl_basic_set_list *compute_domains(
3084 __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
3086 struct isl_codegen_domains domains;
3087 isl_ctx *ctx;
3088 isl_set *domain;
3089 isl_union_set *schedule_domain;
3090 isl_set *classes;
3091 isl_space *space;
3092 int n_param;
3093 enum isl_ast_loop_type type;
3094 isl_bool empty;
3096 if (!executed)
3097 return NULL;
3099 ctx = isl_union_map_get_ctx(executed);
3100 domains.list = isl_basic_set_list_alloc(ctx, 0);
3102 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3103 domain = isl_set_from_union_set(schedule_domain);
3105 compute_domains_init_options(domains.option, build);
3107 domains.sep_class = isl_ast_build_get_separation_class(build);
3108 classes = isl_map_range(isl_map_copy(domains.sep_class));
3109 n_param = isl_set_dim(classes, isl_dim_param);
3110 if (n_param < 0)
3111 classes = isl_set_free(classes);
3112 classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
3114 space = isl_set_get_space(domain);
3115 domains.build = build;
3116 domains.schedule_domain = isl_set_copy(domain);
3117 domains.executed = executed;
3118 domains.done = isl_set_empty(space);
3120 if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
3121 domains.list = isl_basic_set_list_free(domains.list);
3122 isl_set_free(classes);
3124 empty = isl_set_is_empty(domains.done);
3125 if (empty < 0) {
3126 domains.list = isl_basic_set_list_free(domains.list);
3127 domain = isl_set_free(domain);
3128 } else if (empty) {
3129 isl_set_free(domain);
3130 domain = isl_set_universe(isl_set_get_space(domains.done));
3131 } else {
3132 domain = isl_ast_build_eliminate(build, domain);
3134 if (compute_partial_domains(&domains, domain) < 0)
3135 domains.list = isl_basic_set_list_free(domains.list);
3137 isl_set_free(domains.schedule_domain);
3138 isl_set_free(domains.done);
3139 isl_map_free(domains.sep_class);
3140 for (type = isl_ast_loop_atomic; type <= isl_ast_loop_separate; ++type)
3141 isl_set_free(domains.option[type]);
3143 return domains.list;
3146 /* Generate code for a single component, after shifting (if any)
3147 * has been applied, in case the schedule was specified as a union map.
3149 * We first split up the domain at the current depth into disjoint
3150 * basic sets based on the user-specified options.
3151 * Then we generated code for each of them and concatenate the results.
3153 static __isl_give isl_ast_graft_list *generate_shifted_component_flat(
3154 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3156 isl_basic_set_list *domain_list;
3157 isl_ast_graft_list *list = NULL;
3159 domain_list = compute_domains(executed, build);
3160 list = generate_parallel_domains(domain_list, executed, build);
3162 isl_basic_set_list_free(domain_list);
3163 isl_union_map_free(executed);
3164 isl_ast_build_free(build);
3166 return list;
3169 /* Generate code for a single component, after shifting (if any)
3170 * has been applied, in case the schedule was specified as a schedule tree
3171 * and the separate option was specified.
3173 * We perform separation on the domain of "executed" and then generate
3174 * an AST for each of the resulting disjoint basic sets.
3176 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_separate(
3177 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3179 isl_space *space;
3180 isl_set *domain;
3181 isl_basic_set_list *domain_list;
3182 isl_ast_graft_list *list;
3184 space = isl_ast_build_get_space(build, 1);
3185 domain = separate_schedule_domains(space,
3186 isl_union_map_copy(executed), build);
3187 domain_list = isl_basic_set_list_from_set(domain);
3189 list = generate_parallel_domains(domain_list, executed, build);
3191 isl_basic_set_list_free(domain_list);
3192 isl_union_map_free(executed);
3193 isl_ast_build_free(build);
3195 return list;
3198 /* Internal data structure for generate_shifted_component_tree_unroll.
3200 * "executed" and "build" are inputs to generate_shifted_component_tree_unroll.
3201 * "list" collects the constructs grafts.
3203 struct isl_ast_unroll_tree_data {
3204 isl_union_map *executed;
3205 isl_ast_build *build;
3206 isl_ast_graft_list *list;
3209 /* Initialize data->list to a list of "n" elements.
3211 static int init_unroll_tree(int n, void *user)
3213 struct isl_ast_unroll_tree_data *data = user;
3214 isl_ctx *ctx;
3216 ctx = isl_ast_build_get_ctx(data->build);
3217 data->list = isl_ast_graft_list_alloc(ctx, n);
3219 return 0;
3222 /* Given an iteration of an unrolled domain represented by "bset",
3223 * generate the corresponding AST and add the result to data->list.
3225 static int do_unroll_tree_iteration(__isl_take isl_basic_set *bset, void *user)
3227 struct isl_ast_unroll_tree_data *data = user;
3229 data->list = add_node(data->list, isl_union_map_copy(data->executed),
3230 bset, isl_ast_build_copy(data->build));
3232 return 0;
3235 /* Generate code for a single component, after shifting (if any)
3236 * has been applied, in case the schedule was specified as a schedule tree
3237 * and the unroll option was specified.
3239 * We call foreach_iteration to iterate over the individual values and
3240 * construct and collect the corresponding grafts in do_unroll_tree_iteration.
3242 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_unroll(
3243 __isl_take isl_union_map *executed, __isl_take isl_set *domain,
3244 __isl_take isl_ast_build *build)
3246 struct isl_ast_unroll_tree_data data = { executed, build, NULL };
3248 if (foreach_iteration(domain, build, &init_unroll_tree,
3249 &do_unroll_tree_iteration, &data) < 0)
3250 data.list = isl_ast_graft_list_free(data.list);
3252 isl_union_map_free(executed);
3253 isl_ast_build_free(build);
3255 return data.list;
3258 /* Does "domain" involve a disjunction that is purely based on
3259 * constraints involving only outer dimension?
3261 * In particular, is there a disjunction such that the constraints
3262 * involving the current and later dimensions are the same over
3263 * all the disjuncts?
3265 static isl_bool has_pure_outer_disjunction(__isl_keep isl_set *domain,
3266 __isl_keep isl_ast_build *build)
3268 isl_basic_set *hull;
3269 isl_set *shared, *inner;
3270 isl_bool equal;
3271 isl_size depth;
3272 isl_size n;
3273 isl_size dim;
3275 n = isl_set_n_basic_set(domain);
3276 if (n < 0)
3277 return isl_bool_error;
3278 if (n <= 1)
3279 return isl_bool_false;
3280 dim = isl_set_dim(domain, isl_dim_set);
3281 depth = isl_ast_build_get_depth(build);
3282 if (dim < 0 || depth < 0)
3283 return isl_bool_error;
3285 inner = isl_set_copy(domain);
3286 inner = isl_set_drop_constraints_not_involving_dims(inner,
3287 isl_dim_set, depth, dim - depth);
3288 hull = isl_set_plain_unshifted_simple_hull(isl_set_copy(inner));
3289 shared = isl_set_from_basic_set(hull);
3290 equal = isl_set_plain_is_equal(inner, shared);
3291 isl_set_free(inner);
3292 isl_set_free(shared);
3294 return equal;
3297 /* Generate code for a single component, after shifting (if any)
3298 * has been applied, in case the schedule was specified as a schedule tree.
3299 * In particular, handle the base case where there is either no isolated
3300 * set or we are within the isolated set (in which case "isolated" is set)
3301 * or the iterations that precede or follow the isolated set.
3303 * The schedule domain is broken up or combined into basic sets
3304 * according to the AST generation option specified in the current
3305 * schedule node, which may be either atomic, separate, unroll or
3306 * unspecified. If the option is unspecified, then we currently simply
3307 * split the schedule domain into disjoint basic sets.
3309 * In case the separate option is specified, the AST generation is
3310 * handled by generate_shifted_component_tree_separate.
3311 * In the other cases, we need the global schedule domain.
3312 * In the unroll case, the AST generation is then handled by
3313 * generate_shifted_component_tree_unroll which needs the actual
3314 * schedule domain (with divs that may refer to the current dimension)
3315 * so that stride detection can be performed.
3316 * In the atomic or unspecified case, inner dimensions and divs involving
3317 * the current dimensions should be eliminated.
3318 * The result is then either combined into a single basic set or
3319 * split up into disjoint basic sets.
3320 * Finally an AST is generated for each basic set and the results are
3321 * concatenated.
3323 * If the schedule domain involves a disjunction that is purely based on
3324 * constraints involving only outer dimension, then it is treated as
3325 * if atomic was specified. This ensures that only a single loop
3326 * is generated instead of a sequence of identical loops with
3327 * different guards.
3329 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_base(
3330 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3331 int isolated)
3333 isl_bool outer_disjunction;
3334 isl_union_set *schedule_domain;
3335 isl_set *domain;
3336 isl_basic_set_list *domain_list;
3337 isl_ast_graft_list *list;
3338 enum isl_ast_loop_type type;
3340 type = isl_ast_build_get_loop_type(build, isolated);
3341 if (type < 0)
3342 goto error;
3344 if (type == isl_ast_loop_separate)
3345 return generate_shifted_component_tree_separate(executed,
3346 build);
3348 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3349 domain = isl_set_from_union_set(schedule_domain);
3351 if (type == isl_ast_loop_unroll)
3352 return generate_shifted_component_tree_unroll(executed, domain,
3353 build);
3355 domain = isl_ast_build_eliminate(build, domain);
3356 domain = isl_set_coalesce_preserve(domain);
3358 outer_disjunction = has_pure_outer_disjunction(domain, build);
3359 if (outer_disjunction < 0)
3360 domain = isl_set_free(domain);
3362 if (outer_disjunction || type == isl_ast_loop_atomic) {
3363 isl_basic_set *hull;
3364 hull = isl_set_unshifted_simple_hull(domain);
3365 domain_list = isl_basic_set_list_from_basic_set(hull);
3366 } else {
3367 domain = isl_set_make_disjoint(domain);
3368 domain_list = isl_basic_set_list_from_set(domain);
3371 list = generate_parallel_domains(domain_list, executed, build);
3373 isl_basic_set_list_free(domain_list);
3374 isl_union_map_free(executed);
3375 isl_ast_build_free(build);
3377 return list;
3378 error:
3379 isl_union_map_free(executed);
3380 isl_ast_build_free(build);
3381 return NULL;
3384 /* Extract out the disjunction imposed by "domain" on the outer
3385 * schedule dimensions.
3387 * In particular, remove all inner dimensions from "domain" (including
3388 * the current dimension) and then remove the constraints that are shared
3389 * by all disjuncts in the result.
3391 static __isl_give isl_set *extract_disjunction(__isl_take isl_set *domain,
3392 __isl_keep isl_ast_build *build)
3394 isl_set *hull;
3395 isl_size depth;
3396 isl_size dim;
3398 domain = isl_ast_build_specialize(build, domain);
3399 depth = isl_ast_build_get_depth(build);
3400 dim = isl_set_dim(domain, isl_dim_set);
3401 if (depth < 0 || dim < 0)
3402 return isl_set_free(domain);
3403 domain = isl_set_eliminate(domain, isl_dim_set, depth, dim - depth);
3404 domain = isl_set_remove_unknown_divs(domain);
3405 hull = isl_set_copy(domain);
3406 hull = isl_set_from_basic_set(isl_set_unshifted_simple_hull(hull));
3407 domain = isl_set_gist(domain, hull);
3409 return domain;
3412 /* Add "guard" to the grafts in "list".
3413 * "build" is the outer AST build, while "sub_build" includes "guard"
3414 * in its generated domain.
3416 * First combine the grafts into a single graft and then add the guard.
3417 * If the list is empty, or if some error occurred, then simply return
3418 * the list.
3420 static __isl_give isl_ast_graft_list *list_add_guard(
3421 __isl_take isl_ast_graft_list *list, __isl_keep isl_set *guard,
3422 __isl_keep isl_ast_build *build, __isl_keep isl_ast_build *sub_build)
3424 isl_ast_graft *graft;
3425 isl_size n;
3427 list = isl_ast_graft_list_fuse(list, sub_build);
3429 n = isl_ast_graft_list_n_ast_graft(list);
3430 if (n < 0)
3431 return isl_ast_graft_list_free(list);
3432 if (n != 1)
3433 return list;
3435 graft = isl_ast_graft_list_get_ast_graft(list, 0);
3436 graft = isl_ast_graft_add_guard(graft, isl_set_copy(guard), build);
3437 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
3439 return list;
3442 /* Generate code for a single component, after shifting (if any)
3443 * has been applied, in case the schedule was specified as a schedule tree.
3444 * In particular, do so for the specified subset of the schedule domain.
3446 * If we are outside of the isolated part, then "domain" may include
3447 * a disjunction. Explicitly generate this disjunction at this point
3448 * instead of relying on the disjunction getting hoisted back up
3449 * to this level.
3451 static __isl_give isl_ast_graft_list *generate_shifted_component_tree_part(
3452 __isl_keep isl_union_map *executed, __isl_take isl_set *domain,
3453 __isl_keep isl_ast_build *build, int isolated)
3455 isl_union_set *uset;
3456 isl_ast_graft_list *list;
3457 isl_ast_build *sub_build;
3458 int empty;
3460 uset = isl_union_set_from_set(isl_set_copy(domain));
3461 executed = isl_union_map_copy(executed);
3462 executed = isl_union_map_intersect_domain(executed, uset);
3463 empty = isl_union_map_is_empty(executed);
3464 if (empty < 0)
3465 goto error;
3466 if (empty) {
3467 isl_ctx *ctx;
3468 isl_union_map_free(executed);
3469 isl_set_free(domain);
3470 ctx = isl_ast_build_get_ctx(build);
3471 return isl_ast_graft_list_alloc(ctx, 0);
3474 sub_build = isl_ast_build_copy(build);
3475 if (!isolated) {
3476 domain = extract_disjunction(domain, build);
3477 sub_build = isl_ast_build_restrict_generated(sub_build,
3478 isl_set_copy(domain));
3480 list = generate_shifted_component_tree_base(executed,
3481 isl_ast_build_copy(sub_build), isolated);
3482 if (!isolated)
3483 list = list_add_guard(list, domain, build, sub_build);
3484 isl_ast_build_free(sub_build);
3485 isl_set_free(domain);
3486 return list;
3487 error:
3488 isl_union_map_free(executed);
3489 isl_set_free(domain);
3490 return NULL;
3493 /* Generate code for a single component, after shifting (if any)
3494 * has been applied, in case the schedule was specified as a schedule tree.
3495 * In particular, do so for the specified sequence of subsets
3496 * of the schedule domain, "before", "isolated", "after" and "other",
3497 * where only the "isolated" part is considered to be isolated.
3499 static __isl_give isl_ast_graft_list *generate_shifted_component_parts(
3500 __isl_take isl_union_map *executed, __isl_take isl_set *before,
3501 __isl_take isl_set *isolated, __isl_take isl_set *after,
3502 __isl_take isl_set *other, __isl_take isl_ast_build *build)
3504 isl_ast_graft_list *list, *res;
3506 res = generate_shifted_component_tree_part(executed, before, build, 0);
3507 list = generate_shifted_component_tree_part(executed, isolated,
3508 build, 1);
3509 res = isl_ast_graft_list_concat(res, list);
3510 list = generate_shifted_component_tree_part(executed, after, build, 0);
3511 res = isl_ast_graft_list_concat(res, list);
3512 list = generate_shifted_component_tree_part(executed, other, build, 0);
3513 res = isl_ast_graft_list_concat(res, list);
3515 isl_union_map_free(executed);
3516 isl_ast_build_free(build);
3518 return res;
3521 /* Does "set" intersect "first", but not "second"?
3523 static isl_bool only_intersects_first(__isl_keep isl_set *set,
3524 __isl_keep isl_set *first, __isl_keep isl_set *second)
3526 isl_bool disjoint;
3528 disjoint = isl_set_is_disjoint(set, first);
3529 if (disjoint < 0)
3530 return isl_bool_error;
3531 if (disjoint)
3532 return isl_bool_false;
3534 return isl_set_is_disjoint(set, second);
3537 /* Generate code for a single component, after shifting (if any)
3538 * has been applied, in case the schedule was specified as a schedule tree.
3539 * In particular, do so in case of isolation where there is
3540 * only an "isolated" part and an "after" part.
3541 * "dead1" and "dead2" are freed by this function in order to simplify
3542 * the caller.
3544 * The "before" and "other" parts are set to empty sets.
3546 static __isl_give isl_ast_graft_list *generate_shifted_component_only_after(
3547 __isl_take isl_union_map *executed, __isl_take isl_set *isolated,
3548 __isl_take isl_set *after, __isl_take isl_ast_build *build,
3549 __isl_take isl_set *dead1, __isl_take isl_set *dead2)
3551 isl_set *empty;
3553 empty = isl_set_empty(isl_set_get_space(after));
3554 isl_set_free(dead1);
3555 isl_set_free(dead2);
3556 return generate_shifted_component_parts(executed, isl_set_copy(empty),
3557 isolated, after, empty, build);
3560 /* Generate code for a single component, after shifting (if any)
3561 * has been applied, in case the schedule was specified as a schedule tree.
3563 * We first check if the user has specified an isolated schedule domain
3564 * and that we are not already outside of this isolated schedule domain.
3565 * If so, we break up the schedule domain into iterations that
3566 * precede the isolated domain, the isolated domain itself,
3567 * the iterations that follow the isolated domain and
3568 * the remaining iterations (those that are incomparable
3569 * to the isolated domain).
3570 * We generate an AST for each piece and concatenate the results.
3572 * If the isolated domain is not convex, then it is replaced
3573 * by a convex superset to ensure that the sets of preceding and
3574 * following iterations are properly defined and, in particular,
3575 * that there are no intermediate iterations that do not belong
3576 * to the isolated domain.
3578 * In the special case where at least one element of the schedule
3579 * domain that does not belong to the isolated domain needs
3580 * to be scheduled after this isolated domain, but none of those
3581 * elements need to be scheduled before, break up the schedule domain
3582 * in only two parts, the isolated domain, and a part that will be
3583 * scheduled after the isolated domain.
3585 * If no isolated set has been specified, then we generate an
3586 * AST for the entire inverse schedule.
3588 static __isl_give isl_ast_graft_list *generate_shifted_component_tree(
3589 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3591 int i;
3592 isl_size depth;
3593 int empty, has_isolate;
3594 isl_space *space;
3595 isl_union_set *schedule_domain;
3596 isl_set *domain;
3597 isl_basic_set *hull;
3598 isl_set *isolated, *before, *after, *test;
3599 isl_map *gt, *lt;
3600 isl_bool pure;
3602 build = isl_ast_build_extract_isolated(build);
3603 has_isolate = isl_ast_build_has_isolated(build);
3604 if (has_isolate < 0)
3605 executed = isl_union_map_free(executed);
3606 else if (!has_isolate)
3607 return generate_shifted_component_tree_base(executed, build, 0);
3609 schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3610 domain = isl_set_from_union_set(schedule_domain);
3612 isolated = isl_ast_build_get_isolated(build);
3613 isolated = isl_set_intersect(isolated, isl_set_copy(domain));
3614 test = isl_ast_build_specialize(build, isl_set_copy(isolated));
3615 empty = isl_set_is_empty(test);
3616 isl_set_free(test);
3617 if (empty < 0)
3618 goto error;
3619 if (empty) {
3620 isl_set_free(isolated);
3621 isl_set_free(domain);
3622 return generate_shifted_component_tree_base(executed, build, 0);
3624 depth = isl_ast_build_get_depth(build);
3625 if (depth < 0)
3626 goto error;
3628 isolated = isl_ast_build_eliminate(build, isolated);
3629 hull = isl_set_unshifted_simple_hull(isolated);
3630 isolated = isl_set_from_basic_set(hull);
3632 space = isl_space_map_from_set(isl_set_get_space(isolated));
3633 gt = isl_map_universe(space);
3634 for (i = 0; i < depth; ++i)
3635 gt = isl_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
3636 gt = isl_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
3637 lt = isl_map_reverse(isl_map_copy(gt));
3638 before = isl_set_apply(isl_set_copy(isolated), gt);
3639 after = isl_set_apply(isl_set_copy(isolated), lt);
3641 domain = isl_set_subtract(domain, isl_set_copy(isolated));
3642 pure = only_intersects_first(domain, after, before);
3643 if (pure < 0)
3644 executed = isl_union_map_free(executed);
3645 else if (pure)
3646 return generate_shifted_component_only_after(executed, isolated,
3647 domain, build, before, after);
3648 domain = isl_set_subtract(domain, isl_set_copy(before));
3649 domain = isl_set_subtract(domain, isl_set_copy(after));
3650 after = isl_set_subtract(after, isl_set_copy(isolated));
3651 after = isl_set_subtract(after, isl_set_copy(before));
3652 before = isl_set_subtract(before, isl_set_copy(isolated));
3654 return generate_shifted_component_parts(executed, before, isolated,
3655 after, domain, build);
3656 error:
3657 isl_set_free(domain);
3658 isl_set_free(isolated);
3659 isl_union_map_free(executed);
3660 isl_ast_build_free(build);
3661 return NULL;
3664 /* Generate code for a single component, after shifting (if any)
3665 * has been applied.
3667 * Call generate_shifted_component_tree or generate_shifted_component_flat
3668 * depending on whether the schedule was specified as a schedule tree.
3670 static __isl_give isl_ast_graft_list *generate_shifted_component(
3671 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3673 if (isl_ast_build_has_schedule_node(build))
3674 return generate_shifted_component_tree(executed, build);
3675 else
3676 return generate_shifted_component_flat(executed, build);
3679 struct isl_set_map_pair {
3680 isl_set *set;
3681 isl_map *map;
3684 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3685 * of indices into the "domain" array,
3686 * return the union of the "map" fields of the elements
3687 * indexed by the first "n" elements of "order".
3689 static __isl_give isl_union_map *construct_component_executed(
3690 struct isl_set_map_pair *domain, int *order, int n)
3692 int i;
3693 isl_map *map;
3694 isl_union_map *executed;
3696 map = isl_map_copy(domain[order[0]].map);
3697 executed = isl_union_map_from_map(map);
3698 for (i = 1; i < n; ++i) {
3699 map = isl_map_copy(domain[order[i]].map);
3700 executed = isl_union_map_add_map(executed, map);
3703 return executed;
3706 /* Generate code for a single component, after shifting (if any)
3707 * has been applied.
3709 * The component inverse schedule is specified as the "map" fields
3710 * of the elements of "domain" indexed by the first "n" elements of "order".
3712 static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
3713 struct isl_set_map_pair *domain, int *order, int n,
3714 __isl_take isl_ast_build *build)
3716 isl_union_map *executed;
3718 executed = construct_component_executed(domain, order, n);
3719 return generate_shifted_component(executed, build);
3722 /* Does set dimension "pos" of "set" have an obviously fixed value?
3724 static int dim_is_fixed(__isl_keep isl_set *set, int pos)
3726 int fixed;
3727 isl_val *v;
3729 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, pos);
3730 if (!v)
3731 return -1;
3732 fixed = !isl_val_is_nan(v);
3733 isl_val_free(v);
3735 return fixed;
3738 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3739 * of indices into the "domain" array,
3740 * do all (except for at most one) of the "set" field of the elements
3741 * indexed by the first "n" elements of "order" have a fixed value
3742 * at position "depth"?
3744 static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
3745 int *order, int n, int depth)
3747 int i;
3748 int non_fixed = -1;
3750 for (i = 0; i < n; ++i) {
3751 int f;
3753 f = dim_is_fixed(domain[order[i]].set, depth);
3754 if (f < 0)
3755 return -1;
3756 if (f)
3757 continue;
3758 if (non_fixed >= 0)
3759 return 0;
3760 non_fixed = i;
3763 return 1;
3766 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3767 * of indices into the "domain" array,
3768 * eliminate the inner dimensions from the "set" field of the elements
3769 * indexed by the first "n" elements of "order", provided the current
3770 * dimension does not have a fixed value.
3772 * Return the index of the first element in "order" with a corresponding
3773 * "set" field that does not have an (obviously) fixed value.
3775 static int eliminate_non_fixed(struct isl_set_map_pair *domain,
3776 int *order, int n, int depth, __isl_keep isl_ast_build *build)
3778 int i;
3779 int base = -1;
3781 for (i = n - 1; i >= 0; --i) {
3782 int f;
3783 f = dim_is_fixed(domain[order[i]].set, depth);
3784 if (f < 0)
3785 return -1;
3786 if (f)
3787 continue;
3788 domain[order[i]].set = isl_ast_build_eliminate_inner(build,
3789 domain[order[i]].set);
3790 base = i;
3793 return base;
3796 /* Given an array "domain" of isl_set_map_pairs and an array "order"
3797 * of indices into the "domain" array,
3798 * find the element of "domain" (amongst those indexed by the first "n"
3799 * elements of "order") with the "set" field that has the smallest
3800 * value for the current iterator.
3802 * Note that the domain with the smallest value may depend on the parameters
3803 * and/or outer loop dimension. Since the result of this function is only
3804 * used as heuristic, we only make a reasonable attempt at finding the best
3805 * domain, one that should work in case a single domain provides the smallest
3806 * value for the current dimension over all values of the parameters
3807 * and outer dimensions.
3809 * In particular, we compute the smallest value of the first domain
3810 * and replace it by that of any later domain if that later domain
3811 * has a smallest value that is smaller for at least some value
3812 * of the parameters and outer dimensions.
3814 static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
3815 __isl_keep isl_ast_build *build)
3817 int i;
3818 isl_map *min_first;
3819 int first = 0;
3821 min_first = isl_ast_build_map_to_iterator(build,
3822 isl_set_copy(domain[order[0]].set));
3823 min_first = isl_map_lexmin(min_first);
3825 for (i = 1; i < n; ++i) {
3826 isl_map *min, *test;
3827 int empty;
3829 min = isl_ast_build_map_to_iterator(build,
3830 isl_set_copy(domain[order[i]].set));
3831 min = isl_map_lexmin(min);
3832 test = isl_map_copy(min);
3833 test = isl_map_apply_domain(isl_map_copy(min_first), test);
3834 test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
3835 empty = isl_map_is_empty(test);
3836 isl_map_free(test);
3837 if (empty >= 0 && !empty) {
3838 isl_map_free(min_first);
3839 first = i;
3840 min_first = min;
3841 } else
3842 isl_map_free(min);
3844 if (empty < 0)
3845 break;
3848 isl_map_free(min_first);
3850 return i < n ? -1 : first;
3853 /* Construct a shifted inverse schedule based on the original inverse schedule,
3854 * the stride and the offset.
3856 * The original inverse schedule is specified as the "map" fields
3857 * of the elements of "domain" indexed by the first "n" elements of "order".
3859 * "stride" and "offset" are such that the difference
3860 * between the values of the current dimension of domain "i"
3861 * and the values of the current dimension for some reference domain are
3862 * equal to
3864 * stride * integer + offset[i]
3866 * Moreover, 0 <= offset[i] < stride.
3868 * For each domain, we create a map
3870 * { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3872 * where j refers to the current dimension and the other dimensions are
3873 * unchanged, and apply this map to the original schedule domain.
3875 * For example, for the original schedule
3877 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3879 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3880 * we apply the mapping
3882 * { [j] -> [j, 0] }
3884 * to the schedule of the "A" domain and the mapping
3886 * { [j - 1] -> [j, 1] }
3888 * to the schedule of the "B" domain.
3891 * Note that after the transformation, the differences between pairs
3892 * of values of the current dimension over all domains are multiples
3893 * of stride and that we have therefore exposed the stride.
3896 * To see that the mapping preserves the lexicographic order,
3897 * first note that each of the individual maps above preserves the order.
3898 * If the value of the current iterator is j1 in one domain and j2 in another,
3899 * then if j1 = j2, we know that the same map is applied to both domains
3900 * and the order is preserved.
3901 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3902 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3904 * j1 - c1 < j2 - c2
3906 * and the order is preserved.
3907 * If c1 < c2, then we know
3909 * 0 <= c2 - c1 < s
3911 * We also have
3913 * j2 - j1 = n * s + r
3915 * with n >= 0 and 0 <= r < s.
3916 * In other words, r = c2 - c1.
3917 * If n > 0, then
3919 * j1 - c1 < j2 - c2
3921 * If n = 0, then
3923 * j1 - c1 = j2 - c2
3925 * and so
3927 * (j1 - c1, c1) << (j2 - c2, c2)
3929 * with "<<" the lexicographic order, proving that the order is preserved
3930 * in all cases.
3932 static __isl_give isl_union_map *construct_shifted_executed(
3933 struct isl_set_map_pair *domain, int *order, int n,
3934 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3935 __isl_keep isl_ast_build *build)
3937 int i;
3938 isl_union_map *executed;
3939 isl_space *space;
3940 isl_map *map;
3941 isl_size depth;
3942 isl_constraint *c;
3944 depth = isl_ast_build_get_depth(build);
3945 if (depth < 0)
3946 return NULL;
3947 space = isl_ast_build_get_space(build, 1);
3948 executed = isl_union_map_empty(isl_space_copy(space));
3949 space = isl_space_map_from_set(space);
3950 map = isl_map_identity(isl_space_copy(space));
3951 map = isl_map_eliminate(map, isl_dim_out, depth, 1);
3952 map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
3953 space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
3955 c = isl_constraint_alloc_equality(isl_local_space_from_space(space));
3956 c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
3957 c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
3959 for (i = 0; i < n; ++i) {
3960 isl_map *map_i;
3961 isl_val *v;
3963 v = isl_multi_val_get_val(offset, i);
3964 if (!v)
3965 break;
3966 map_i = isl_map_copy(map);
3967 map_i = isl_map_fix_val(map_i, isl_dim_out, depth + 1,
3968 isl_val_copy(v));
3969 v = isl_val_neg(v);
3970 c = isl_constraint_set_constant_val(c, v);
3971 map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
3973 map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
3974 map_i);
3975 executed = isl_union_map_add_map(executed, map_i);
3978 isl_constraint_free(c);
3979 isl_map_free(map);
3981 if (i < n)
3982 executed = isl_union_map_free(executed);
3984 return executed;
3987 /* Generate code for a single component, after exposing the stride,
3988 * given that the schedule domain is "shifted strided".
3990 * The component inverse schedule is specified as the "map" fields
3991 * of the elements of "domain" indexed by the first "n" elements of "order".
3993 * The schedule domain being "shifted strided" means that the differences
3994 * between the values of the current dimension of domain "i"
3995 * and the values of the current dimension for some reference domain are
3996 * equal to
3998 * stride * integer + offset[i]
4000 * We first look for the domain with the "smallest" value for the current
4001 * dimension and adjust the offsets such that the offset of the "smallest"
4002 * domain is equal to zero. The other offsets are reduced modulo stride.
4004 * Based on this information, we construct a new inverse schedule in
4005 * construct_shifted_executed that exposes the stride.
4006 * Since this involves the introduction of a new schedule dimension,
4007 * the build needs to be changed accordingly.
4008 * After computing the AST, the newly introduced dimension needs
4009 * to be removed again from the list of grafts. We do this by plugging
4010 * in a mapping that represents the new schedule domain in terms of the
4011 * old schedule domain.
4013 static __isl_give isl_ast_graft_list *generate_shift_component(
4014 struct isl_set_map_pair *domain, int *order, int n,
4015 __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
4016 __isl_take isl_ast_build *build)
4018 isl_ast_graft_list *list;
4019 int first;
4020 isl_size depth;
4021 isl_val *val;
4022 isl_multi_val *mv;
4023 isl_space *space;
4024 isl_multi_aff *ma, *zero;
4025 isl_union_map *executed;
4027 depth = isl_ast_build_get_depth(build);
4029 first = first_offset(domain, order, n, build);
4030 if (depth < 0 || first < 0)
4031 goto error;
4033 mv = isl_multi_val_copy(offset);
4034 val = isl_multi_val_get_val(offset, first);
4035 val = isl_val_neg(val);
4036 mv = isl_multi_val_add_val(mv, val);
4037 mv = isl_multi_val_mod_val(mv, isl_val_copy(stride));
4039 executed = construct_shifted_executed(domain, order, n, stride, mv,
4040 build);
4041 space = isl_ast_build_get_space(build, 1);
4042 space = isl_space_map_from_set(space);
4043 ma = isl_multi_aff_identity(isl_space_copy(space));
4044 space = isl_space_from_domain(isl_space_domain(space));
4045 space = isl_space_add_dims(space, isl_dim_out, 1);
4046 zero = isl_multi_aff_zero(space);
4047 ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
4048 build = isl_ast_build_insert_dim(build, depth + 1);
4049 list = generate_shifted_component(executed, build);
4051 list = isl_ast_graft_list_preimage_multi_aff(list, ma);
4053 isl_multi_val_free(mv);
4055 return list;
4056 error:
4057 isl_ast_build_free(build);
4058 return NULL;
4061 /* Does any node in the schedule tree rooted at the current schedule node
4062 * of "build" depend on outer schedule nodes?
4064 static int has_anchored_subtree(__isl_keep isl_ast_build *build)
4066 isl_schedule_node *node;
4067 int dependent = 0;
4069 node = isl_ast_build_get_schedule_node(build);
4070 dependent = isl_schedule_node_is_subtree_anchored(node);
4071 isl_schedule_node_free(node);
4073 return dependent;
4076 /* Generate code for a single component.
4078 * The component inverse schedule is specified as the "map" fields
4079 * of the elements of "domain" indexed by the first "n" elements of "order".
4081 * This function may modify the "set" fields of "domain".
4083 * Before proceeding with the actual code generation for the component,
4084 * we first check if there are any "shifted" strides, meaning that
4085 * the schedule domains of the individual domains are all strided,
4086 * but that they have different offsets, resulting in the union
4087 * of schedule domains not being strided anymore.
4089 * The simplest example is the schedule
4091 * { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
4093 * Both schedule domains are strided, but their union is not.
4094 * This function detects such cases and then rewrites the schedule to
4096 * { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
4098 * In the new schedule, the schedule domains have the same offset (modulo
4099 * the stride), ensuring that the union of schedule domains is also strided.
4102 * If there is only a single domain in the component, then there is
4103 * nothing to do. Similarly, if the current schedule dimension has
4104 * a fixed value for almost all domains then there is nothing to be done.
4105 * In particular, we need at least two domains where the current schedule
4106 * dimension does not have a fixed value.
4107 * Finally, in case of a schedule map input,
4108 * if any of the options refer to the current schedule dimension,
4109 * then we bail out as well. It would be possible to reformulate the options
4110 * in terms of the new schedule domain, but that would introduce constraints
4111 * that separate the domains in the options and that is something we would
4112 * like to avoid.
4113 * In the case of a schedule tree input, we bail out if any of
4114 * the descendants of the current schedule node refer to outer
4115 * schedule nodes in any way.
4118 * To see if there is any shifted stride, we look at the differences
4119 * between the values of the current dimension in pairs of domains
4120 * for equal values of outer dimensions. These differences should be
4121 * of the form
4123 * m x + r
4125 * with "m" the stride and "r" a constant. Note that we cannot perform
4126 * this analysis on individual domains as the lower bound in each domain
4127 * may depend on parameters or outer dimensions and so the current dimension
4128 * itself may not have a fixed remainder on division by the stride.
4130 * In particular, we compare the first domain that does not have an
4131 * obviously fixed value for the current dimension to itself and all
4132 * other domains and collect the offsets and the gcd of the strides.
4133 * If the gcd becomes one, then we failed to find shifted strides.
4134 * If the gcd is zero, then the differences were all fixed, meaning
4135 * that some domains had non-obviously fixed values for the current dimension.
4136 * If all the offsets are the same (for those domains that do not have
4137 * an obviously fixed value for the current dimension), then we do not
4138 * apply the transformation.
4139 * If none of the domains were skipped, then there is nothing to do.
4140 * If some of them were skipped, then if we apply separation, the schedule
4141 * domain should get split in pieces with a (non-shifted) stride.
4143 * Otherwise, we apply a shift to expose the stride in
4144 * generate_shift_component.
4146 static __isl_give isl_ast_graft_list *generate_component(
4147 struct isl_set_map_pair *domain, int *order, int n,
4148 __isl_take isl_ast_build *build)
4150 int i, d;
4151 isl_size depth;
4152 isl_ctx *ctx;
4153 isl_map *map;
4154 isl_set *deltas;
4155 isl_val *gcd = NULL;
4156 isl_multi_val *mv;
4157 int fixed, skip;
4158 int base;
4159 isl_ast_graft_list *list;
4160 int res = 0;
4162 depth = isl_ast_build_get_depth(build);
4163 if (depth < 0)
4164 goto error;
4166 skip = n == 1;
4167 if (skip >= 0 && !skip)
4168 skip = at_most_one_non_fixed(domain, order, n, depth);
4169 if (skip >= 0 && !skip) {
4170 if (isl_ast_build_has_schedule_node(build))
4171 skip = has_anchored_subtree(build);
4172 else
4173 skip = isl_ast_build_options_involve_depth(build);
4175 if (skip < 0)
4176 goto error;
4177 if (skip)
4178 return generate_shifted_component_from_list(domain,
4179 order, n, build);
4181 base = eliminate_non_fixed(domain, order, n, depth, build);
4182 if (base < 0)
4183 goto error;
4185 ctx = isl_ast_build_get_ctx(build);
4187 mv = isl_multi_val_zero(isl_space_set_alloc(ctx, 0, n));
4189 fixed = 1;
4190 for (i = 0; i < n; ++i) {
4191 isl_val *r, *m;
4193 map = isl_map_from_domain_and_range(
4194 isl_set_copy(domain[order[base]].set),
4195 isl_set_copy(domain[order[i]].set));
4196 for (d = 0; d < depth; ++d)
4197 map = isl_map_equate(map, isl_dim_in, d,
4198 isl_dim_out, d);
4199 deltas = isl_map_deltas(map);
4200 res = isl_set_dim_residue_class_val(deltas, depth, &m, &r);
4201 isl_set_free(deltas);
4202 if (res < 0)
4203 break;
4205 if (i == 0)
4206 gcd = m;
4207 else
4208 gcd = isl_val_gcd(gcd, m);
4209 if (isl_val_is_one(gcd)) {
4210 isl_val_free(r);
4211 break;
4213 mv = isl_multi_val_set_val(mv, i, r);
4215 res = dim_is_fixed(domain[order[i]].set, depth);
4216 if (res < 0)
4217 break;
4218 if (res)
4219 continue;
4221 if (fixed && i > base) {
4222 isl_val *a, *b;
4223 a = isl_multi_val_get_val(mv, i);
4224 b = isl_multi_val_get_val(mv, base);
4225 if (isl_val_ne(a, b))
4226 fixed = 0;
4227 isl_val_free(a);
4228 isl_val_free(b);
4232 if (res < 0 || !gcd) {
4233 isl_ast_build_free(build);
4234 list = NULL;
4235 } else if (i < n || fixed || isl_val_is_zero(gcd)) {
4236 list = generate_shifted_component_from_list(domain,
4237 order, n, build);
4238 } else {
4239 list = generate_shift_component(domain, order, n, gcd, mv,
4240 build);
4243 isl_val_free(gcd);
4244 isl_multi_val_free(mv);
4246 return list;
4247 error:
4248 isl_ast_build_free(build);
4249 return NULL;
4252 /* Store both "map" itself and its domain in the
4253 * structure pointed to by *next and advance to the next array element.
4255 static isl_stat extract_domain(__isl_take isl_map *map, void *user)
4257 struct isl_set_map_pair **next = user;
4259 (*next)->map = isl_map_copy(map);
4260 (*next)->set = isl_map_domain(map);
4261 (*next)++;
4263 return isl_stat_ok;
4266 static isl_bool after_in_tree(__isl_keep isl_union_map *umap,
4267 __isl_keep isl_schedule_node *node);
4269 /* Is any domain element of "umap" scheduled after any of
4270 * the corresponding image elements by the tree rooted at
4271 * the child of "node"?
4273 static isl_bool after_in_child(__isl_keep isl_union_map *umap,
4274 __isl_keep isl_schedule_node *node)
4276 isl_schedule_node *child;
4277 isl_bool after;
4279 child = isl_schedule_node_get_child(node, 0);
4280 after = after_in_tree(umap, child);
4281 isl_schedule_node_free(child);
4283 return after;
4286 /* Is any domain element of "umap" scheduled after any of
4287 * the corresponding image elements by the tree rooted at
4288 * the band node "node"?
4290 * We first check if any domain element is scheduled after any
4291 * of the corresponding image elements by the band node itself.
4292 * If not, we restrict "map" to those pairs of element that
4293 * are scheduled together by the band node and continue with
4294 * the child of the band node.
4295 * If there are no such pairs then the map passed to after_in_child
4296 * will be empty causing it to return 0.
4298 static isl_bool after_in_band(__isl_keep isl_union_map *umap,
4299 __isl_keep isl_schedule_node *node)
4301 isl_multi_union_pw_aff *mupa;
4302 isl_union_map *partial, *test, *gt, *universe, *umap1, *umap2;
4303 isl_union_set *domain, *range;
4304 isl_space *space;
4305 isl_bool empty;
4306 isl_bool after;
4307 isl_size n;
4309 n = isl_schedule_node_band_n_member(node);
4310 if (n < 0)
4311 return isl_bool_error;
4312 if (n == 0)
4313 return after_in_child(umap, node);
4315 mupa = isl_schedule_node_band_get_partial_schedule(node);
4316 space = isl_multi_union_pw_aff_get_space(mupa);
4317 partial = isl_union_map_from_multi_union_pw_aff(mupa);
4318 test = isl_union_map_copy(umap);
4319 test = isl_union_map_apply_domain(test, isl_union_map_copy(partial));
4320 test = isl_union_map_apply_range(test, isl_union_map_copy(partial));
4321 gt = isl_union_map_from_map(isl_map_lex_gt(space));
4322 test = isl_union_map_intersect(test, gt);
4323 empty = isl_union_map_is_empty(test);
4324 isl_union_map_free(test);
4326 if (empty < 0 || !empty) {
4327 isl_union_map_free(partial);
4328 return isl_bool_not(empty);
4331 universe = isl_union_map_universe(isl_union_map_copy(umap));
4332 domain = isl_union_map_domain(isl_union_map_copy(universe));
4333 range = isl_union_map_range(universe);
4334 umap1 = isl_union_map_copy(partial);
4335 umap1 = isl_union_map_intersect_domain(umap1, domain);
4336 umap2 = isl_union_map_intersect_domain(partial, range);
4337 test = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4338 test = isl_union_map_intersect(test, isl_union_map_copy(umap));
4339 after = after_in_child(test, node);
4340 isl_union_map_free(test);
4341 return after;
4344 /* Is any domain element of "umap" scheduled after any of
4345 * the corresponding image elements by the tree rooted at
4346 * the context node "node"?
4348 * The context constraints apply to the schedule domain,
4349 * so we cannot apply them directly to "umap", which contains
4350 * pairs of statement instances. Instead, we add them
4351 * to the range of the prefix schedule for both domain and
4352 * range of "umap".
4354 static isl_bool after_in_context(__isl_keep isl_union_map *umap,
4355 __isl_keep isl_schedule_node *node)
4357 isl_union_map *prefix, *universe, *umap1, *umap2;
4358 isl_union_set *domain, *range;
4359 isl_set *context;
4360 isl_bool after;
4362 umap = isl_union_map_copy(umap);
4363 context = isl_schedule_node_context_get_context(node);
4364 prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
4365 universe = isl_union_map_universe(isl_union_map_copy(umap));
4366 domain = isl_union_map_domain(isl_union_map_copy(universe));
4367 range = isl_union_map_range(universe);
4368 umap1 = isl_union_map_copy(prefix);
4369 umap1 = isl_union_map_intersect_domain(umap1, domain);
4370 umap2 = isl_union_map_intersect_domain(prefix, range);
4371 umap1 = isl_union_map_intersect_range(umap1,
4372 isl_union_set_from_set(context));
4373 umap1 = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4374 umap = isl_union_map_intersect(umap, umap1);
4376 after = after_in_child(umap, node);
4378 isl_union_map_free(umap);
4380 return after;
4383 /* Is any domain element of "umap" scheduled after any of
4384 * the corresponding image elements by the tree rooted at
4385 * the expansion node "node"?
4387 * We apply the expansion to domain and range of "umap" and
4388 * continue with its child.
4390 static isl_bool after_in_expansion(__isl_keep isl_union_map *umap,
4391 __isl_keep isl_schedule_node *node)
4393 isl_union_map *expansion;
4394 isl_bool after;
4396 expansion = isl_schedule_node_expansion_get_expansion(node);
4397 umap = isl_union_map_copy(umap);
4398 umap = isl_union_map_apply_domain(umap, isl_union_map_copy(expansion));
4399 umap = isl_union_map_apply_range(umap, expansion);
4401 after = after_in_child(umap, node);
4403 isl_union_map_free(umap);
4405 return after;
4408 /* Is any domain element of "umap" scheduled after any of
4409 * the corresponding image elements by the tree rooted at
4410 * the extension node "node"?
4412 * Since the extension node may add statement instances before or
4413 * after the pairs of statement instances in "umap", we return isl_bool_true
4414 * to ensure that these pairs are not broken up.
4416 static isl_bool after_in_extension(__isl_keep isl_union_map *umap,
4417 __isl_keep isl_schedule_node *node)
4419 return isl_bool_true;
4422 /* Is any domain element of "umap" scheduled after any of
4423 * the corresponding image elements by the tree rooted at
4424 * the filter node "node"?
4426 * We intersect domain and range of "umap" with the filter and
4427 * continue with its child.
4429 static isl_bool after_in_filter(__isl_keep isl_union_map *umap,
4430 __isl_keep isl_schedule_node *node)
4432 isl_union_set *filter;
4433 isl_bool after;
4435 umap = isl_union_map_copy(umap);
4436 filter = isl_schedule_node_filter_get_filter(node);
4437 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(filter));
4438 umap = isl_union_map_intersect_range(umap, filter);
4440 after = after_in_child(umap, node);
4442 isl_union_map_free(umap);
4444 return after;
4447 /* Is any domain element of "umap" scheduled after any of
4448 * the corresponding image elements by the tree rooted at
4449 * the set node "node"?
4451 * This is only the case if this condition holds in any
4452 * of the (filter) children of the set node.
4453 * In particular, if the domain and the range of "umap"
4454 * are contained in different children, then the condition
4455 * does not hold.
4457 static isl_bool after_in_set(__isl_keep isl_union_map *umap,
4458 __isl_keep isl_schedule_node *node)
4460 int i;
4461 isl_size n;
4463 n = isl_schedule_node_n_children(node);
4464 if (n < 0)
4465 return isl_bool_error;
4466 for (i = 0; i < n; ++i) {
4467 isl_schedule_node *child;
4468 isl_bool after;
4470 child = isl_schedule_node_get_child(node, i);
4471 after = after_in_tree(umap, child);
4472 isl_schedule_node_free(child);
4474 if (after < 0 || after)
4475 return after;
4478 return isl_bool_false;
4481 /* Return the filter of child "i" of "node".
4483 static __isl_give isl_union_set *child_filter(
4484 __isl_keep isl_schedule_node *node, int i)
4486 isl_schedule_node *child;
4487 isl_union_set *filter;
4489 child = isl_schedule_node_get_child(node, i);
4490 filter = isl_schedule_node_filter_get_filter(child);
4491 isl_schedule_node_free(child);
4493 return filter;
4496 /* Is any domain element of "umap" scheduled after any of
4497 * the corresponding image elements by the tree rooted at
4498 * the sequence node "node"?
4500 * This happens in particular if any domain element is
4501 * contained in a later child than one containing a range element or
4502 * if the condition holds within a given child in the sequence.
4503 * The later part of the condition is checked by after_in_set.
4505 static isl_bool after_in_sequence(__isl_keep isl_union_map *umap,
4506 __isl_keep isl_schedule_node *node)
4508 int i, j;
4509 isl_size n;
4510 isl_union_map *umap_i;
4511 isl_bool empty;
4512 isl_bool after = isl_bool_false;
4514 n = isl_schedule_node_n_children(node);
4515 if (n < 0)
4516 return isl_bool_error;
4517 for (i = 1; i < n; ++i) {
4518 isl_union_set *filter_i;
4520 umap_i = isl_union_map_copy(umap);
4521 filter_i = child_filter(node, i);
4522 umap_i = isl_union_map_intersect_domain(umap_i, filter_i);
4523 empty = isl_union_map_is_empty(umap_i);
4524 if (empty < 0)
4525 goto error;
4526 if (empty) {
4527 isl_union_map_free(umap_i);
4528 continue;
4531 for (j = 0; j < i; ++j) {
4532 isl_union_set *filter_j;
4533 isl_union_map *umap_ij;
4535 umap_ij = isl_union_map_copy(umap_i);
4536 filter_j = child_filter(node, j);
4537 umap_ij = isl_union_map_intersect_range(umap_ij,
4538 filter_j);
4539 empty = isl_union_map_is_empty(umap_ij);
4540 isl_union_map_free(umap_ij);
4542 if (empty < 0)
4543 goto error;
4544 if (!empty)
4545 after = isl_bool_true;
4546 if (after)
4547 break;
4550 isl_union_map_free(umap_i);
4551 if (after)
4552 break;
4555 if (after < 0 || after)
4556 return after;
4558 return after_in_set(umap, node);
4559 error:
4560 isl_union_map_free(umap_i);
4561 return isl_bool_error;
4564 /* Is any domain element of "umap" scheduled after any of
4565 * the corresponding image elements by the tree rooted at "node"?
4567 * If "umap" is empty, then clearly there is no such element.
4568 * Otherwise, consider the different types of nodes separately.
4570 static isl_bool after_in_tree(__isl_keep isl_union_map *umap,
4571 __isl_keep isl_schedule_node *node)
4573 isl_bool empty;
4574 enum isl_schedule_node_type type;
4576 empty = isl_union_map_is_empty(umap);
4577 if (empty < 0)
4578 return isl_bool_error;
4579 if (empty)
4580 return isl_bool_false;
4581 if (!node)
4582 return isl_bool_error;
4584 type = isl_schedule_node_get_type(node);
4585 switch (type) {
4586 case isl_schedule_node_error:
4587 return isl_bool_error;
4588 case isl_schedule_node_leaf:
4589 return isl_bool_false;
4590 case isl_schedule_node_band:
4591 return after_in_band(umap, node);
4592 case isl_schedule_node_domain:
4593 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
4594 "unexpected internal domain node",
4595 return isl_bool_error);
4596 case isl_schedule_node_context:
4597 return after_in_context(umap, node);
4598 case isl_schedule_node_expansion:
4599 return after_in_expansion(umap, node);
4600 case isl_schedule_node_extension:
4601 return after_in_extension(umap, node);
4602 case isl_schedule_node_filter:
4603 return after_in_filter(umap, node);
4604 case isl_schedule_node_guard:
4605 case isl_schedule_node_mark:
4606 return after_in_child(umap, node);
4607 case isl_schedule_node_set:
4608 return after_in_set(umap, node);
4609 case isl_schedule_node_sequence:
4610 return after_in_sequence(umap, node);
4613 return isl_bool_true;
4616 /* Is any domain element of "map1" scheduled after any domain
4617 * element of "map2" by the subtree underneath the current band node,
4618 * while at the same time being scheduled together by the current
4619 * band node, i.e., by "map1" and "map2?
4621 * If the child of the current band node is a leaf, then
4622 * no element can be scheduled after any other element.
4624 * Otherwise, we construct a relation between domain elements
4625 * of "map1" and domain elements of "map2" that are scheduled
4626 * together and then check if the subtree underneath the current
4627 * band node determines their relative order.
4629 static isl_bool after_in_subtree(__isl_keep isl_ast_build *build,
4630 __isl_keep isl_map *map1, __isl_keep isl_map *map2)
4632 isl_schedule_node *node;
4633 isl_map *map;
4634 isl_union_map *umap;
4635 isl_bool after;
4637 node = isl_ast_build_get_schedule_node(build);
4638 if (!node)
4639 return isl_bool_error;
4640 node = isl_schedule_node_child(node, 0);
4641 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
4642 isl_schedule_node_free(node);
4643 return isl_bool_false;
4645 map = isl_map_copy(map2);
4646 map = isl_map_apply_domain(map, isl_map_copy(map1));
4647 umap = isl_union_map_from_map(map);
4648 after = after_in_tree(umap, node);
4649 isl_union_map_free(umap);
4650 isl_schedule_node_free(node);
4651 return after;
4654 /* Internal data for any_scheduled_after.
4656 * "build" is the build in which the AST is constructed.
4657 * "depth" is the number of loops that have already been generated
4658 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
4659 * "domain" is an array of set-map pairs corresponding to the different
4660 * iteration domains. The set is the schedule domain, i.e., the domain
4661 * of the inverse schedule, while the map is the inverse schedule itself.
4663 struct isl_any_scheduled_after_data {
4664 isl_ast_build *build;
4665 int depth;
4666 int group_coscheduled;
4667 struct isl_set_map_pair *domain;
4670 /* Is any element of domain "i" scheduled after any element of domain "j"
4671 * (for a common iteration of the first data->depth loops)?
4673 * data->domain[i].set contains the domain of the inverse schedule
4674 * for domain "i", i.e., elements in the schedule domain.
4676 * If we are inside a band of a schedule tree and there is a pair
4677 * of elements in the two domains that is schedule together by
4678 * the current band, then we check if any element of "i" may be schedule
4679 * after element of "j" by the descendants of the band node.
4681 * If data->group_coscheduled is set, then we also return 1 if there
4682 * is any pair of elements in the two domains that are scheduled together.
4684 static isl_bool any_scheduled_after(int i, int j, void *user)
4686 struct isl_any_scheduled_after_data *data = user;
4687 isl_size dim = isl_set_dim(data->domain[i].set, isl_dim_set);
4688 int pos;
4690 if (dim < 0)
4691 return isl_bool_error;
4693 for (pos = data->depth; pos < dim; ++pos) {
4694 int follows;
4696 follows = isl_set_follows_at(data->domain[i].set,
4697 data->domain[j].set, pos);
4699 if (follows < -1)
4700 return isl_bool_error;
4701 if (follows > 0)
4702 return isl_bool_true;
4703 if (follows < 0)
4704 return isl_bool_false;
4707 if (isl_ast_build_has_schedule_node(data->build)) {
4708 isl_bool after;
4710 after = after_in_subtree(data->build, data->domain[i].map,
4711 data->domain[j].map);
4712 if (after < 0 || after)
4713 return after;
4716 return isl_bool_ok(data->group_coscheduled);
4719 /* Look for independent components at the current depth and generate code
4720 * for each component separately. The resulting lists of grafts are
4721 * merged in an attempt to combine grafts with identical guards.
4723 * Code for two domains can be generated separately if all the elements
4724 * of one domain are scheduled before (or together with) all the elements
4725 * of the other domain. We therefore consider the graph with as nodes
4726 * the domains and an edge between two nodes if any element of the first
4727 * node is scheduled after any element of the second node.
4728 * If the ast_build_group_coscheduled is set, then we also add an edge if
4729 * there is any pair of elements in the two domains that are scheduled
4730 * together.
4731 * Code is then generated (by generate_component)
4732 * for each of the strongly connected components in this graph
4733 * in their topological order.
4735 * Since the test is performed on the domain of the inverse schedules of
4736 * the different domains, we precompute these domains and store
4737 * them in data.domain.
4739 static __isl_give isl_ast_graft_list *generate_components(
4740 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4742 int i;
4743 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4744 isl_size n = isl_union_map_n_map(executed);
4745 isl_size depth;
4746 struct isl_any_scheduled_after_data data;
4747 struct isl_set_map_pair *next;
4748 struct isl_tarjan_graph *g = NULL;
4749 isl_ast_graft_list *list = NULL;
4750 int n_domain = 0;
4752 data.domain = NULL;
4753 if (n < 0)
4754 goto error;
4755 data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
4756 if (!data.domain)
4757 goto error;
4758 n_domain = n;
4760 next = data.domain;
4761 if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
4762 goto error;
4764 depth = isl_ast_build_get_depth(build);
4765 if (depth < 0)
4766 goto error;
4767 data.build = build;
4768 data.depth = depth;
4769 data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
4770 g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
4771 if (!g)
4772 goto error;
4774 list = isl_ast_graft_list_alloc(ctx, 0);
4776 i = 0;
4777 while (list && n) {
4778 isl_ast_graft_list *list_c;
4779 int first = i;
4781 if (g->order[i] == -1)
4782 isl_die(ctx, isl_error_internal, "cannot happen",
4783 goto error);
4784 ++i; --n;
4785 while (g->order[i] != -1) {
4786 ++i; --n;
4789 list_c = generate_component(data.domain,
4790 g->order + first, i - first,
4791 isl_ast_build_copy(build));
4792 list = isl_ast_graft_list_merge(list, list_c, build);
4794 ++i;
4797 if (0)
4798 error: list = isl_ast_graft_list_free(list);
4799 isl_tarjan_graph_free(g);
4800 for (i = 0; i < n_domain; ++i) {
4801 isl_map_free(data.domain[i].map);
4802 isl_set_free(data.domain[i].set);
4804 free(data.domain);
4805 isl_union_map_free(executed);
4806 isl_ast_build_free(build);
4808 return list;
4811 /* Generate code for the next level (and all inner levels).
4813 * If "executed" is empty, i.e., no code needs to be generated,
4814 * then we return an empty list.
4816 * If we have already generated code for all loop levels, then we pass
4817 * control to generate_inner_level.
4819 * If "executed" lives in a single space, i.e., if code needs to be
4820 * generated for a single domain, then there can only be a single
4821 * component and we go directly to generate_shifted_component.
4822 * Otherwise, we call generate_components to detect the components
4823 * and to call generate_component on each of them separately.
4825 static __isl_give isl_ast_graft_list *generate_next_level(
4826 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4828 isl_size depth;
4829 isl_size dim;
4830 isl_size n;
4832 if (!build || !executed)
4833 goto error;
4835 if (isl_union_map_is_empty(executed)) {
4836 isl_ctx *ctx = isl_ast_build_get_ctx(build);
4837 isl_union_map_free(executed);
4838 isl_ast_build_free(build);
4839 return isl_ast_graft_list_alloc(ctx, 0);
4842 depth = isl_ast_build_get_depth(build);
4843 dim = isl_ast_build_dim(build, isl_dim_set);
4844 if (depth < 0 || dim < 0)
4845 goto error;
4846 if (depth >= dim)
4847 return generate_inner_level(executed, build);
4849 n = isl_union_map_n_map(executed);
4850 if (n < 0)
4851 goto error;
4852 if (n == 1)
4853 return generate_shifted_component(executed, build);
4855 return generate_components(executed, build);
4856 error:
4857 isl_union_map_free(executed);
4858 isl_ast_build_free(build);
4859 return NULL;
4862 /* Internal data structure used by isl_ast_build_node_from_schedule_map.
4863 * internal, executed and build are the inputs to generate_code.
4864 * list collects the output.
4866 struct isl_generate_code_data {
4867 int internal;
4868 isl_union_map *executed;
4869 isl_ast_build *build;
4871 isl_ast_graft_list *list;
4874 /* Given an inverse schedule in terms of the external build schedule, i.e.,
4876 * [E -> S] -> D
4878 * with E the external build schedule and S the additional schedule "space",
4879 * reformulate the inverse schedule in terms of the internal schedule domain,
4880 * i.e., return
4882 * [I -> S] -> D
4884 * We first obtain a mapping
4886 * I -> E
4888 * take the inverse and the product with S -> S, resulting in
4890 * [I -> S] -> [E -> S]
4892 * Applying the map to the input produces the desired result.
4894 static __isl_give isl_union_map *internal_executed(
4895 __isl_take isl_union_map *executed, __isl_keep isl_space *space,
4896 __isl_keep isl_ast_build *build)
4898 isl_map *id, *proj;
4900 proj = isl_ast_build_get_schedule_map(build);
4901 proj = isl_map_reverse(proj);
4902 space = isl_space_map_from_set(isl_space_copy(space));
4903 id = isl_map_identity(space);
4904 proj = isl_map_product(proj, id);
4905 executed = isl_union_map_apply_domain(executed,
4906 isl_union_map_from_map(proj));
4907 return executed;
4910 /* Generate an AST that visits the elements in the range of data->executed
4911 * in the relative order specified by the corresponding domain element(s)
4912 * for those domain elements that belong to "set".
4913 * Add the result to data->list.
4915 * The caller ensures that "set" is a universe domain.
4916 * "space" is the space of the additional part of the schedule.
4917 * It is equal to the space of "set" if build->domain is parametric.
4918 * Otherwise, it is equal to the range of the wrapped space of "set".
4920 * If the build space is not parametric and
4921 * if isl_ast_build_node_from_schedule_map
4922 * was called from an outside user (data->internal not set), then
4923 * the (inverse) schedule refers to the external build domain and needs to
4924 * be transformed to refer to the internal build domain.
4926 * If the build space is parametric, then we add some of the parameter
4927 * constraints to the executed relation. Adding these constraints
4928 * allows for an earlier detection of conflicts in some cases.
4929 * However, we do not want to divide the executed relation into
4930 * more disjuncts than necessary. We therefore approximate
4931 * the constraints on the parameters by a single disjunct set.
4933 * The build is extended to include the additional part of the schedule.
4934 * If the original build space was not parametric, then the options
4935 * in data->build refer only to the additional part of the schedule
4936 * and they need to be adjusted to refer to the complete AST build
4937 * domain.
4939 * After having adjusted inverse schedule and build, we start generating
4940 * code with the outer loop of the current code generation
4941 * in generate_next_level.
4943 * If the original build space was not parametric, we undo the embedding
4944 * on the resulting isl_ast_node_list so that it can be used within
4945 * the outer AST build.
4947 static isl_stat generate_code_in_space(struct isl_generate_code_data *data,
4948 __isl_take isl_set *set, __isl_take isl_space *space)
4950 isl_union_map *executed;
4951 isl_ast_build *build;
4952 isl_ast_graft_list *list;
4953 int embed;
4955 executed = isl_union_map_copy(data->executed);
4956 executed = isl_union_map_intersect_domain(executed,
4957 isl_union_set_from_set(set));
4959 embed = !isl_set_is_params(data->build->domain);
4960 if (embed && !data->internal)
4961 executed = internal_executed(executed, space, data->build);
4962 if (!embed) {
4963 isl_set *domain;
4964 domain = isl_ast_build_get_domain(data->build);
4965 domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
4966 executed = isl_union_map_intersect_params(executed, domain);
4969 build = isl_ast_build_copy(data->build);
4970 build = isl_ast_build_product(build, space);
4972 list = generate_next_level(executed, build);
4974 list = isl_ast_graft_list_unembed(list, embed);
4976 data->list = isl_ast_graft_list_concat(data->list, list);
4978 return isl_stat_ok;
4981 /* Generate an AST that visits the elements in the range of data->executed
4982 * in the relative order specified by the corresponding domain element(s)
4983 * for those domain elements that belong to "set".
4984 * Add the result to data->list.
4986 * The caller ensures that "set" is a universe domain.
4988 * If the build space S is not parametric, then the space of "set"
4989 * need to be a wrapped relation with S as domain. That is, it needs
4990 * to be of the form
4992 * [S -> T]
4994 * Check this property and pass control to generate_code_in_space
4995 * passing along T.
4996 * If the build space is not parametric, then T is the space of "set".
4998 static isl_stat generate_code_set(__isl_take isl_set *set, void *user)
5000 struct isl_generate_code_data *data = user;
5001 isl_space *space, *build_space;
5002 int is_domain;
5004 space = isl_set_get_space(set);
5006 if (isl_set_is_params(data->build->domain))
5007 return generate_code_in_space(data, set, space);
5009 build_space = isl_ast_build_get_space(data->build, data->internal);
5010 space = isl_space_unwrap(space);
5011 is_domain = isl_space_is_domain(build_space, space);
5012 isl_space_free(build_space);
5013 space = isl_space_range(space);
5015 if (is_domain < 0)
5016 goto error;
5017 if (!is_domain)
5018 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5019 "invalid nested schedule space", goto error);
5021 return generate_code_in_space(data, set, space);
5022 error:
5023 isl_set_free(set);
5024 isl_space_free(space);
5025 return isl_stat_error;
5028 /* Generate an AST that visits the elements in the range of "executed"
5029 * in the relative order specified by the corresponding domain element(s).
5031 * "build" is an isl_ast_build that has either been constructed by
5032 * isl_ast_build_from_context or passed to a callback set by
5033 * isl_ast_build_set_create_leaf.
5034 * In the first case, the space of the isl_ast_build is typically
5035 * a parametric space, although this is currently not enforced.
5036 * In the second case, the space is never a parametric space.
5037 * If the space S is not parametric, then the domain space(s) of "executed"
5038 * need to be wrapped relations with S as domain.
5040 * If the domain of "executed" consists of several spaces, then an AST
5041 * is generated for each of them (in arbitrary order) and the results
5042 * are concatenated.
5044 * If "internal" is set, then the domain "S" above refers to the internal
5045 * schedule domain representation. Otherwise, it refers to the external
5046 * representation, as returned by isl_ast_build_get_schedule_space.
5048 * We essentially run over all the spaces in the domain of "executed"
5049 * and call generate_code_set on each of them.
5051 static __isl_give isl_ast_graft_list *generate_code(
5052 __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
5053 int internal)
5055 isl_ctx *ctx;
5056 struct isl_generate_code_data data = { 0 };
5057 isl_space *space;
5058 isl_union_set *schedule_domain;
5059 isl_union_map *universe;
5061 if (!build)
5062 goto error;
5063 space = isl_ast_build_get_space(build, 1);
5064 space = isl_space_align_params(space,
5065 isl_union_map_get_space(executed));
5066 space = isl_space_align_params(space,
5067 isl_union_map_get_space(build->options));
5068 build = isl_ast_build_align_params(build, isl_space_copy(space));
5069 executed = isl_union_map_align_params(executed, space);
5070 if (!executed || !build)
5071 goto error;
5073 ctx = isl_ast_build_get_ctx(build);
5075 data.internal = internal;
5076 data.executed = executed;
5077 data.build = build;
5078 data.list = isl_ast_graft_list_alloc(ctx, 0);
5080 universe = isl_union_map_universe(isl_union_map_copy(executed));
5081 schedule_domain = isl_union_map_domain(universe);
5082 if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
5083 &data) < 0)
5084 data.list = isl_ast_graft_list_free(data.list);
5086 isl_union_set_free(schedule_domain);
5087 isl_union_map_free(executed);
5089 isl_ast_build_free(build);
5090 return data.list;
5091 error:
5092 isl_union_map_free(executed);
5093 isl_ast_build_free(build);
5094 return NULL;
5097 /* Generate an AST that visits the elements in the domain of "schedule"
5098 * in the relative order specified by the corresponding image element(s).
5100 * "build" is an isl_ast_build that has either been constructed by
5101 * isl_ast_build_from_context or passed to a callback set by
5102 * isl_ast_build_set_create_leaf.
5103 * In the first case, the space of the isl_ast_build is typically
5104 * a parametric space, although this is currently not enforced.
5105 * In the second case, the space is never a parametric space.
5106 * If the space S is not parametric, then the range space(s) of "schedule"
5107 * need to be wrapped relations with S as domain.
5109 * If the range of "schedule" consists of several spaces, then an AST
5110 * is generated for each of them (in arbitrary order) and the results
5111 * are concatenated.
5113 * We first initialize the local copies of the relevant options.
5114 * We do this here rather than when the isl_ast_build is created
5115 * because the options may have changed between the construction
5116 * of the isl_ast_build and the call to isl_generate_code.
5118 * The main computation is performed on an inverse schedule (with
5119 * the schedule domain in the domain and the elements to be executed
5120 * in the range) called "executed".
5122 __isl_give isl_ast_node *isl_ast_build_node_from_schedule_map(
5123 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5125 isl_ast_graft_list *list;
5126 isl_ast_node *node;
5127 isl_union_map *executed;
5129 schedule = isl_union_map_coalesce(schedule);
5130 schedule = isl_union_map_remove_redundancies(schedule);
5131 executed = isl_union_map_reverse(schedule);
5132 list = generate_code(executed, isl_ast_build_copy(build), 0);
5133 node = isl_ast_node_from_graft_list(list, build);
5135 return node;
5138 /* The old name for isl_ast_build_node_from_schedule_map.
5139 * It is being kept for backward compatibility, but
5140 * it will be removed in the future.
5142 __isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
5143 __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5145 return isl_ast_build_node_from_schedule_map(build, schedule);
5148 /* Generate an AST that visits the elements in the domain of "executed"
5149 * in the relative order specified by the leaf node "node".
5151 * The relation "executed" maps the outer generated loop iterators
5152 * to the domain elements executed by those iterations.
5154 * Simply pass control to generate_inner_level.
5155 * Note that the current build does not refer to any band node, so
5156 * that generate_inner_level will not try to visit the child of
5157 * the leaf node.
5159 * If multiple statement instances reach a leaf,
5160 * then they can be executed in any order.
5161 * Group the list of grafts based on shared guards
5162 * such that identical guards are only generated once
5163 * when the list is eventually passed on to isl_ast_graft_list_fuse.
5165 static __isl_give isl_ast_graft_list *build_ast_from_leaf(
5166 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5167 __isl_take isl_union_map *executed)
5169 isl_ast_graft_list *list;
5171 isl_schedule_node_free(node);
5172 list = generate_inner_level(executed, isl_ast_build_copy(build));
5173 list = isl_ast_graft_list_group_on_guard(list, build);
5174 isl_ast_build_free(build);
5176 return list;
5179 /* Check that the band partial schedule "partial" does not filter out
5180 * any statement instances, as specified by the range of "executed".
5182 static isl_stat check_band_schedule_total_on_instances(
5183 __isl_keep isl_multi_union_pw_aff *partial,
5184 __isl_keep isl_union_map *executed)
5186 isl_bool subset;
5187 isl_union_set *domain, *instances;
5189 instances = isl_union_map_range(isl_union_map_copy(executed));
5190 partial = isl_multi_union_pw_aff_copy(partial);
5191 domain = isl_multi_union_pw_aff_domain(partial);
5192 subset = isl_union_set_is_subset(instances, domain);
5193 isl_union_set_free(domain);
5194 isl_union_set_free(instances);
5196 if (subset < 0)
5197 return isl_stat_error;
5198 if (!subset)
5199 isl_die(isl_union_map_get_ctx(executed), isl_error_invalid,
5200 "band node is not allowed to drop statement instances",
5201 return isl_stat_error);
5202 return isl_stat_ok;
5205 /* Generate an AST that visits the elements in the domain of "executed"
5206 * in the relative order specified by the band node "node" and its descendants.
5208 * The relation "executed" maps the outer generated loop iterators
5209 * to the domain elements executed by those iterations.
5211 * If the band is empty, we continue with its descendants.
5212 * Otherwise, we extend the build and the inverse schedule with
5213 * the additional space/partial schedule and continue generating
5214 * an AST in generate_next_level.
5215 * As soon as we have extended the inverse schedule with the additional
5216 * partial schedule, we look for equalities that may exists between
5217 * the old and the new part.
5219 static __isl_give isl_ast_graft_list *build_ast_from_band(
5220 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5221 __isl_take isl_union_map *executed)
5223 isl_space *space;
5224 isl_multi_union_pw_aff *extra;
5225 isl_union_map *extra_umap;
5226 isl_ast_graft_list *list;
5227 isl_size n1, n2;
5228 isl_size n;
5230 n = isl_schedule_node_band_n_member(node);
5231 if (!build || n < 0 || !executed)
5232 goto error;
5234 if (n == 0)
5235 return build_ast_from_child(build, node, executed);
5237 extra = isl_schedule_node_band_get_partial_schedule(node);
5238 extra = isl_multi_union_pw_aff_align_params(extra,
5239 isl_ast_build_get_space(build, 1));
5240 space = isl_multi_union_pw_aff_get_space(extra);
5242 if (check_band_schedule_total_on_instances(extra, executed) < 0)
5243 executed = isl_union_map_free(executed);
5245 extra_umap = isl_union_map_from_multi_union_pw_aff(extra);
5246 extra_umap = isl_union_map_reverse(extra_umap);
5248 executed = isl_union_map_domain_product(executed, extra_umap);
5249 executed = isl_union_map_detect_equalities(executed);
5251 n1 = isl_ast_build_dim(build, isl_dim_param);
5252 build = isl_ast_build_product(build, space);
5253 n2 = isl_ast_build_dim(build, isl_dim_param);
5254 if (n1 < 0 || n2 < 0)
5255 build = isl_ast_build_free(build);
5256 else if (n2 > n1)
5257 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5258 "band node is not allowed to introduce new parameters",
5259 build = isl_ast_build_free(build));
5260 build = isl_ast_build_set_schedule_node(build, node);
5262 list = generate_next_level(executed, build);
5264 list = isl_ast_graft_list_unembed(list, 1);
5266 return list;
5267 error:
5268 isl_schedule_node_free(node);
5269 isl_union_map_free(executed);
5270 isl_ast_build_free(build);
5271 return NULL;
5274 /* Hoist a list of grafts (in practice containing a single graft)
5275 * from "sub_build" (which includes extra context information)
5276 * to "build".
5278 * In particular, project out all additional parameters introduced
5279 * by the context node from the enforced constraints and the guard
5280 * of the single graft.
5282 static __isl_give isl_ast_graft_list *hoist_out_of_context(
5283 __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build,
5284 __isl_keep isl_ast_build *sub_build)
5286 isl_ast_graft *graft;
5287 isl_basic_set *enforced;
5288 isl_set *guard;
5289 isl_size n_param, extra_param;
5291 n_param = isl_ast_build_dim(build, isl_dim_param);
5292 extra_param = isl_ast_build_dim(sub_build, isl_dim_param);
5293 if (n_param < 0 || extra_param < 0)
5294 return isl_ast_graft_list_free(list);
5296 if (extra_param == n_param)
5297 return list;
5299 extra_param -= n_param;
5300 enforced = isl_ast_graft_list_extract_shared_enforced(list, sub_build);
5301 enforced = isl_basic_set_project_out(enforced, isl_dim_param,
5302 n_param, extra_param);
5303 enforced = isl_basic_set_remove_unknown_divs(enforced);
5304 guard = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5305 guard = isl_set_remove_divs_involving_dims(guard, isl_dim_param,
5306 n_param, extra_param);
5307 guard = isl_set_project_out(guard, isl_dim_param, n_param, extra_param);
5308 guard = isl_set_compute_divs(guard);
5309 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5310 build, sub_build);
5311 list = isl_ast_graft_list_from_ast_graft(graft);
5313 return list;
5316 /* Generate an AST that visits the elements in the domain of "executed"
5317 * in the relative order specified by the context node "node"
5318 * and its descendants.
5320 * The relation "executed" maps the outer generated loop iterators
5321 * to the domain elements executed by those iterations.
5323 * The context node may introduce additional parameters as well as
5324 * constraints on the outer schedule dimensions or original parameters.
5326 * We add the extra parameters to a new build and the context
5327 * constraints to both the build and (as a single disjunct)
5328 * to the domain of "executed". Since the context constraints
5329 * are specified in terms of the input schedule, we first need
5330 * to map them to the internal schedule domain.
5332 * After constructing the AST from the descendants of "node",
5333 * we combine the list of grafts into a single graft within
5334 * the new build, in order to be able to exploit the additional
5335 * context constraints during this combination.
5337 * Additionally, if the current node is the outermost node in
5338 * the schedule tree (apart from the root domain node), we generate
5339 * all pending guards, again to be able to exploit the additional
5340 * context constraints. We currently do not do this for internal
5341 * context nodes since we may still want to hoist conditions
5342 * to outer AST nodes.
5344 * If the context node introduced any new parameters, then they
5345 * are removed from the set of enforced constraints and guard
5346 * in hoist_out_of_context.
5348 static __isl_give isl_ast_graft_list *build_ast_from_context(
5349 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5350 __isl_take isl_union_map *executed)
5352 isl_set *context;
5353 isl_space *space;
5354 isl_multi_aff *internal2input;
5355 isl_ast_build *sub_build;
5356 isl_ast_graft_list *list;
5357 isl_size n;
5358 isl_size depth;
5360 depth = isl_schedule_node_get_tree_depth(node);
5361 if (depth < 0)
5362 build = isl_ast_build_free(build);
5363 space = isl_ast_build_get_space(build, 1);
5364 context = isl_schedule_node_context_get_context(node);
5365 context = isl_set_align_params(context, space);
5366 sub_build = isl_ast_build_copy(build);
5367 space = isl_set_get_space(context);
5368 sub_build = isl_ast_build_align_params(sub_build, space);
5369 internal2input = isl_ast_build_get_internal2input(sub_build);
5370 context = isl_set_preimage_multi_aff(context, internal2input);
5371 sub_build = isl_ast_build_restrict_generated(sub_build,
5372 isl_set_copy(context));
5373 context = isl_set_from_basic_set(isl_set_simple_hull(context));
5374 executed = isl_union_map_intersect_domain(executed,
5375 isl_union_set_from_set(context));
5377 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5378 node, executed);
5379 n = isl_ast_graft_list_n_ast_graft(list);
5380 if (n < 0)
5381 list = isl_ast_graft_list_free(list);
5383 list = isl_ast_graft_list_fuse(list, sub_build);
5384 if (depth == 1)
5385 list = isl_ast_graft_list_insert_pending_guard_nodes(list,
5386 sub_build);
5387 if (n >= 1)
5388 list = hoist_out_of_context(list, build, sub_build);
5390 isl_ast_build_free(build);
5391 isl_ast_build_free(sub_build);
5393 return list;
5396 /* Generate an AST that visits the elements in the domain of "executed"
5397 * in the relative order specified by the expansion node "node" and
5398 * its descendants.
5400 * The relation "executed" maps the outer generated loop iterators
5401 * to the domain elements executed by those iterations.
5403 * We expand the domain elements by the expansion and
5404 * continue with the descendants of the node.
5406 static __isl_give isl_ast_graft_list *build_ast_from_expansion(
5407 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5408 __isl_take isl_union_map *executed)
5410 isl_union_map *expansion;
5411 isl_size n1, n2;
5413 expansion = isl_schedule_node_expansion_get_expansion(node);
5414 expansion = isl_union_map_align_params(expansion,
5415 isl_union_map_get_space(executed));
5417 n1 = isl_union_map_dim(executed, isl_dim_param);
5418 executed = isl_union_map_apply_range(executed, expansion);
5419 n2 = isl_union_map_dim(executed, isl_dim_param);
5420 if (n1 < 0 || n2 < 0)
5421 goto error;
5422 if (n2 > n1)
5423 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5424 "expansion node is not allowed to introduce "
5425 "new parameters", goto error);
5427 return build_ast_from_child(build, node, executed);
5428 error:
5429 isl_ast_build_free(build);
5430 isl_schedule_node_free(node);
5431 isl_union_map_free(executed);
5432 return NULL;
5435 /* Generate an AST that visits the elements in the domain of "executed"
5436 * in the relative order specified by the extension node "node" and
5437 * its descendants.
5439 * The relation "executed" maps the outer generated loop iterators
5440 * to the domain elements executed by those iterations.
5442 * Extend the inverse schedule with the extension applied to current
5443 * set of generated constraints. Since the extension if formulated
5444 * in terms of the input schedule, it first needs to be transformed
5445 * to refer to the internal schedule.
5447 static __isl_give isl_ast_graft_list *build_ast_from_extension(
5448 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5449 __isl_take isl_union_map *executed)
5451 isl_union_set *schedule_domain;
5452 isl_union_map *extension;
5453 isl_set *set;
5455 set = isl_ast_build_get_generated(build);
5456 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5457 schedule_domain = isl_union_set_from_set(set);
5459 extension = isl_schedule_node_extension_get_extension(node);
5461 extension = isl_union_map_preimage_domain_multi_aff(extension,
5462 isl_multi_aff_copy(build->internal2input));
5463 extension = isl_union_map_intersect_domain(extension, schedule_domain);
5464 extension = isl_ast_build_substitute_values_union_map_domain(build,
5465 extension);
5466 executed = isl_union_map_union(executed, extension);
5468 return build_ast_from_child(build, node, executed);
5471 /* Generate an AST that visits the elements in the domain of "executed"
5472 * in the relative order specified by the filter node "node" and
5473 * its descendants.
5475 * The relation "executed" maps the outer generated loop iterators
5476 * to the domain elements executed by those iterations.
5478 * We simply intersect the iteration domain (i.e., the range of "executed")
5479 * with the filter and continue with the descendants of the node,
5480 * unless the resulting inverse schedule is empty, in which
5481 * case we return an empty list.
5483 * If the result of the intersection is equal to the original "executed"
5484 * relation, then keep the original representation since the intersection
5485 * may have unnecessarily broken up the relation into a greater number
5486 * of disjuncts.
5488 static __isl_give isl_ast_graft_list *build_ast_from_filter(
5489 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5490 __isl_take isl_union_map *executed)
5492 isl_ctx *ctx;
5493 isl_union_set *filter;
5494 isl_union_map *orig;
5495 isl_ast_graft_list *list;
5496 int empty;
5497 isl_bool unchanged;
5498 isl_size n1, n2;
5500 orig = isl_union_map_copy(executed);
5501 if (!build || !node || !executed)
5502 goto error;
5504 filter = isl_schedule_node_filter_get_filter(node);
5505 filter = isl_union_set_align_params(filter,
5506 isl_union_map_get_space(executed));
5507 n1 = isl_union_map_dim(executed, isl_dim_param);
5508 executed = isl_union_map_intersect_range(executed, filter);
5509 n2 = isl_union_map_dim(executed, isl_dim_param);
5510 if (n1 < 0 || n2 < 0)
5511 goto error;
5512 if (n2 > n1)
5513 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5514 "filter node is not allowed to introduce "
5515 "new parameters", goto error);
5517 unchanged = isl_union_map_is_subset(orig, executed);
5518 empty = isl_union_map_is_empty(executed);
5519 if (unchanged < 0 || empty < 0)
5520 goto error;
5521 if (unchanged) {
5522 isl_union_map_free(executed);
5523 return build_ast_from_child(build, node, orig);
5525 isl_union_map_free(orig);
5526 if (!empty)
5527 return build_ast_from_child(build, node, executed);
5529 ctx = isl_ast_build_get_ctx(build);
5530 list = isl_ast_graft_list_alloc(ctx, 0);
5531 isl_ast_build_free(build);
5532 isl_schedule_node_free(node);
5533 isl_union_map_free(executed);
5534 return list;
5535 error:
5536 isl_ast_build_free(build);
5537 isl_schedule_node_free(node);
5538 isl_union_map_free(executed);
5539 isl_union_map_free(orig);
5540 return NULL;
5543 /* Generate an AST that visits the elements in the domain of "executed"
5544 * in the relative order specified by the guard node "node" and
5545 * its descendants.
5547 * The relation "executed" maps the outer generated loop iterators
5548 * to the domain elements executed by those iterations.
5550 * Ensure that the associated guard is enforced by the outer AST
5551 * constructs by adding it to the guard of the graft.
5552 * Since we know that we will enforce the guard, we can also include it
5553 * in the generated constraints used to construct an AST for
5554 * the descendant nodes.
5556 static __isl_give isl_ast_graft_list *build_ast_from_guard(
5557 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5558 __isl_take isl_union_map *executed)
5560 isl_space *space;
5561 isl_set *guard, *hoisted;
5562 isl_basic_set *enforced;
5563 isl_ast_build *sub_build;
5564 isl_ast_graft *graft;
5565 isl_ast_graft_list *list;
5566 isl_size n1, n2, n;
5568 space = isl_ast_build_get_space(build, 1);
5569 guard = isl_schedule_node_guard_get_guard(node);
5570 n1 = isl_space_dim(space, isl_dim_param);
5571 guard = isl_set_align_params(guard, space);
5572 n2 = isl_set_dim(guard, isl_dim_param);
5573 if (n1 < 0 || n2 < 0)
5574 guard = isl_set_free(guard);
5575 else if (n2 > n1)
5576 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5577 "guard node is not allowed to introduce "
5578 "new parameters", guard = isl_set_free(guard));
5579 guard = isl_set_preimage_multi_aff(guard,
5580 isl_multi_aff_copy(build->internal2input));
5581 guard = isl_ast_build_specialize(build, guard);
5582 guard = isl_set_gist(guard, isl_set_copy(build->generated));
5584 sub_build = isl_ast_build_copy(build);
5585 sub_build = isl_ast_build_restrict_generated(sub_build,
5586 isl_set_copy(guard));
5588 list = build_ast_from_child(isl_ast_build_copy(sub_build),
5589 node, executed);
5591 hoisted = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5592 n = isl_set_n_basic_set(hoisted);
5593 if (n < 0)
5594 list = isl_ast_graft_list_free(list);
5595 if (n > 1)
5596 list = isl_ast_graft_list_gist_guards(list,
5597 isl_set_copy(hoisted));
5598 guard = isl_set_intersect(guard, hoisted);
5599 enforced = extract_shared_enforced(list, build);
5600 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5601 build, sub_build);
5603 isl_ast_build_free(sub_build);
5604 isl_ast_build_free(build);
5605 return isl_ast_graft_list_from_ast_graft(graft);
5608 /* Call the before_each_mark callback, if requested by the user.
5610 * Return 0 on success and -1 on error.
5612 * The caller is responsible for recording the current inverse schedule
5613 * in "build".
5615 static isl_stat before_each_mark(__isl_keep isl_id *mark,
5616 __isl_keep isl_ast_build *build)
5618 if (!build)
5619 return isl_stat_error;
5620 if (!build->before_each_mark)
5621 return isl_stat_ok;
5622 return build->before_each_mark(mark, build,
5623 build->before_each_mark_user);
5626 /* Call the after_each_mark callback, if requested by the user.
5628 * The caller is responsible for recording the current inverse schedule
5629 * in "build".
5631 static __isl_give isl_ast_graft *after_each_mark(
5632 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
5634 if (!graft || !build)
5635 return isl_ast_graft_free(graft);
5636 if (!build->after_each_mark)
5637 return graft;
5638 graft->node = build->after_each_mark(graft->node, build,
5639 build->after_each_mark_user);
5640 if (!graft->node)
5641 return isl_ast_graft_free(graft);
5642 return graft;
5646 /* Generate an AST that visits the elements in the domain of "executed"
5647 * in the relative order specified by the mark node "node" and
5648 * its descendants.
5650 * The relation "executed" maps the outer generated loop iterators
5651 * to the domain elements executed by those iterations.
5653 * Since we may be calling before_each_mark and after_each_mark
5654 * callbacks, we record the current inverse schedule in the build.
5656 * We generate an AST for the child of the mark node, combine
5657 * the graft list into a single graft and then insert the mark
5658 * in the AST of that single graft.
5660 static __isl_give isl_ast_graft_list *build_ast_from_mark(
5661 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5662 __isl_take isl_union_map *executed)
5664 isl_id *mark;
5665 isl_ast_graft *graft;
5666 isl_ast_graft_list *list;
5667 isl_size n;
5669 build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
5671 mark = isl_schedule_node_mark_get_id(node);
5672 if (before_each_mark(mark, build) < 0)
5673 node = isl_schedule_node_free(node);
5675 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5676 list = isl_ast_graft_list_fuse(list, build);
5677 n = isl_ast_graft_list_n_ast_graft(list);
5678 if (n < 0)
5679 list = isl_ast_graft_list_free(list);
5680 if (n == 0) {
5681 isl_id_free(mark);
5682 } else {
5683 graft = isl_ast_graft_list_get_ast_graft(list, 0);
5684 graft = isl_ast_graft_insert_mark(graft, mark);
5685 graft = after_each_mark(graft, build);
5686 list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
5688 isl_ast_build_free(build);
5690 return list;
5693 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5694 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5695 __isl_take isl_union_map *executed);
5697 /* Generate an AST that visits the elements in the domain of "executed"
5698 * in the relative order specified by the sequence (or set) node "node" and
5699 * its descendants.
5701 * The relation "executed" maps the outer generated loop iterators
5702 * to the domain elements executed by those iterations.
5704 * We simply generate an AST for each of the children and concatenate
5705 * the results.
5707 static __isl_give isl_ast_graft_list *build_ast_from_sequence(
5708 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5709 __isl_take isl_union_map *executed)
5711 int i;
5712 isl_size n;
5713 isl_ctx *ctx;
5714 isl_ast_graft_list *list;
5716 ctx = isl_ast_build_get_ctx(build);
5717 list = isl_ast_graft_list_alloc(ctx, 0);
5719 n = isl_schedule_node_n_children(node);
5720 if (n < 0)
5721 list = isl_ast_graft_list_free(list);
5722 for (i = 0; i < n; ++i) {
5723 isl_schedule_node *child;
5724 isl_ast_graft_list *list_i;
5726 child = isl_schedule_node_get_child(node, i);
5727 list_i = build_ast_from_schedule_node(isl_ast_build_copy(build),
5728 child, isl_union_map_copy(executed));
5729 list = isl_ast_graft_list_concat(list, list_i);
5731 isl_ast_build_free(build);
5732 isl_schedule_node_free(node);
5733 isl_union_map_free(executed);
5735 return list;
5738 /* Generate an AST that visits the elements in the domain of "executed"
5739 * in the relative order specified by the node "node" and its descendants.
5741 * The relation "executed" maps the outer generated loop iterators
5742 * to the domain elements executed by those iterations.
5744 * The node types are handled in separate functions.
5745 * Set nodes are currently treated in the same way as sequence nodes.
5746 * The children of a set node may be executed in any order,
5747 * including the order of the children.
5749 static __isl_give isl_ast_graft_list *build_ast_from_schedule_node(
5750 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5751 __isl_take isl_union_map *executed)
5753 enum isl_schedule_node_type type;
5755 type = isl_schedule_node_get_type(node);
5757 switch (type) {
5758 case isl_schedule_node_error:
5759 goto error;
5760 case isl_schedule_node_leaf:
5761 return build_ast_from_leaf(build, node, executed);
5762 case isl_schedule_node_band:
5763 return build_ast_from_band(build, node, executed);
5764 case isl_schedule_node_context:
5765 return build_ast_from_context(build, node, executed);
5766 case isl_schedule_node_domain:
5767 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
5768 "unexpected internal domain node", goto error);
5769 case isl_schedule_node_expansion:
5770 return build_ast_from_expansion(build, node, executed);
5771 case isl_schedule_node_extension:
5772 return build_ast_from_extension(build, node, executed);
5773 case isl_schedule_node_filter:
5774 return build_ast_from_filter(build, node, executed);
5775 case isl_schedule_node_guard:
5776 return build_ast_from_guard(build, node, executed);
5777 case isl_schedule_node_mark:
5778 return build_ast_from_mark(build, node, executed);
5779 case isl_schedule_node_sequence:
5780 case isl_schedule_node_set:
5781 return build_ast_from_sequence(build, node, executed);
5784 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
5785 "unhandled type", goto error);
5786 error:
5787 isl_union_map_free(executed);
5788 isl_schedule_node_free(node);
5789 isl_ast_build_free(build);
5791 return NULL;
5794 /* Generate an AST that visits the elements in the domain of "executed"
5795 * in the relative order specified by the (single) child of "node" and
5796 * its descendants.
5798 * The relation "executed" maps the outer generated loop iterators
5799 * to the domain elements executed by those iterations.
5801 * This function is never called on a leaf, set or sequence node,
5802 * so the node always has exactly one child.
5804 static __isl_give isl_ast_graft_list *build_ast_from_child(
5805 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5806 __isl_take isl_union_map *executed)
5808 node = isl_schedule_node_child(node, 0);
5809 return build_ast_from_schedule_node(build, node, executed);
5812 /* Generate an AST that visits the elements in the domain of the domain
5813 * node "node" in the relative order specified by its descendants.
5815 * An initial inverse schedule is created that maps a zero-dimensional
5816 * schedule space to the node domain.
5817 * The input "build" is assumed to have a parametric domain and
5818 * is replaced by the same zero-dimensional schedule space.
5820 * We also add some of the parameter constraints in the build domain
5821 * to the executed relation. Adding these constraints
5822 * allows for an earlier detection of conflicts in some cases.
5823 * However, we do not want to divide the executed relation into
5824 * more disjuncts than necessary. We therefore approximate
5825 * the constraints on the parameters by a single disjunct set.
5827 static __isl_give isl_ast_node *build_ast_from_domain(
5828 __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node)
5830 isl_ctx *ctx;
5831 isl_union_set *domain, *schedule_domain;
5832 isl_union_map *executed;
5833 isl_space *space;
5834 isl_set *set;
5835 isl_ast_graft_list *list;
5836 isl_ast_node *ast;
5837 int is_params;
5839 if (!build)
5840 goto error;
5842 ctx = isl_ast_build_get_ctx(build);
5843 space = isl_ast_build_get_space(build, 1);
5844 is_params = isl_space_is_params(space);
5845 isl_space_free(space);
5846 if (is_params < 0)
5847 goto error;
5848 if (!is_params)
5849 isl_die(ctx, isl_error_unsupported,
5850 "expecting parametric initial context", goto error);
5852 domain = isl_schedule_node_domain_get_domain(node);
5853 domain = isl_union_set_coalesce(domain);
5855 space = isl_union_set_get_space(domain);
5856 space = isl_space_set_from_params(space);
5857 build = isl_ast_build_product(build, space);
5859 set = isl_ast_build_get_domain(build);
5860 set = isl_set_from_basic_set(isl_set_simple_hull(set));
5861 schedule_domain = isl_union_set_from_set(set);
5863 executed = isl_union_map_from_domain_and_range(schedule_domain, domain);
5864 list = build_ast_from_child(isl_ast_build_copy(build), node, executed);
5865 ast = isl_ast_node_from_graft_list(list, build);
5866 isl_ast_build_free(build);
5868 return ast;
5869 error:
5870 isl_schedule_node_free(node);
5871 isl_ast_build_free(build);
5872 return NULL;
5875 /* Generate an AST that visits the elements in the domain of "schedule"
5876 * in the relative order specified by the schedule tree.
5878 * "build" is an isl_ast_build that has been created using
5879 * isl_ast_build_alloc or isl_ast_build_from_context based
5880 * on a parametric set.
5882 * The construction starts at the root node of the schedule,
5883 * which is assumed to be a domain node.
5885 __isl_give isl_ast_node *isl_ast_build_node_from_schedule(
5886 __isl_keep isl_ast_build *build, __isl_take isl_schedule *schedule)
5888 isl_ctx *ctx;
5889 isl_schedule_node *node;
5891 if (!build || !schedule)
5892 goto error;
5894 ctx = isl_ast_build_get_ctx(build);
5896 node = isl_schedule_get_root(schedule);
5897 if (!node)
5898 goto error;
5899 isl_schedule_free(schedule);
5901 build = isl_ast_build_copy(build);
5902 if (isl_schedule_node_get_type(node) != isl_schedule_node_domain)
5903 isl_die(ctx, isl_error_unsupported,
5904 "expecting root domain node",
5905 build = isl_ast_build_free(build));
5906 return build_ast_from_domain(build, node);
5907 error:
5908 isl_schedule_free(schedule);
5909 return NULL;