add isl_schedule_foreach_schedule_node
[isl.git] / isl_schedule.c
blob2abf101dd033cea641272493e4c4a7abb35019ec
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2014 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9 * 91893 Orsay, France
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 #include <isl/ctx.h>
14 #include <isl_aff_private.h>
15 #include <isl/map.h>
16 #include <isl/set.h>
17 #include <isl/schedule.h>
18 #include <isl/schedule_node.h>
19 #include <isl_sort.h>
20 #include <isl_schedule_private.h>
21 #include <isl_schedule_tree.h>
22 #include <isl_schedule_node_private.h>
23 #include <isl_band_private.h>
25 /* Return a schedule encapsulating the given schedule tree.
27 * We currently only allow schedule trees with a domain as root.
29 * The leaf field is initialized as a leaf node so that it can be
30 * used to represent leaves in the constructed schedule.
31 * The reference count is set to -1 since the isl_schedule_tree
32 * should never be freed. It is up to the (internal) users of
33 * these leaves to ensure that they are only used while the schedule
34 * is still alive.
36 __isl_give isl_schedule *isl_schedule_from_schedule_tree(isl_ctx *ctx,
37 __isl_take isl_schedule_tree *tree)
39 isl_schedule *schedule;
41 if (!tree)
42 return NULL;
43 if (isl_schedule_tree_get_type(tree) != isl_schedule_node_domain)
44 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_unsupported,
45 "root of schedule tree should be a domain",
46 goto error);
48 schedule = isl_calloc_type(ctx, isl_schedule);
49 if (!schedule)
50 goto error;
52 schedule->leaf.ctx = ctx;
53 isl_ctx_ref(ctx);
54 schedule->ref = 1;
55 schedule->root = tree;
56 schedule->leaf.ref = -1;
57 schedule->leaf.type = isl_schedule_node_leaf;
59 return schedule;
60 error:
61 isl_schedule_tree_free(tree);
62 return NULL;
65 /* Return a pointer to a schedule with as single node
66 * a domain node with the given domain.
68 __isl_give isl_schedule *isl_schedule_from_domain(
69 __isl_take isl_union_set *domain)
71 isl_ctx *ctx;
72 isl_schedule_tree *tree;
74 ctx = isl_union_set_get_ctx(domain);
75 tree = isl_schedule_tree_from_domain(domain);
76 return isl_schedule_from_schedule_tree(ctx, tree);
79 /* Return a pointer to a schedule with as single node
80 * a domain node with an empty domain.
82 __isl_give isl_schedule *isl_schedule_empty(__isl_take isl_space *space)
84 return isl_schedule_from_domain(isl_union_set_empty(space));
87 /* Return a new reference to "sched".
89 __isl_give isl_schedule *isl_schedule_copy(__isl_keep isl_schedule *sched)
91 if (!sched)
92 return NULL;
94 sched->ref++;
95 return sched;
98 /* Return an isl_schedule that is equal to "schedule" and that has only
99 * a single reference.
101 * We only need and support this function when the schedule is represented
102 * as a schedule tree.
104 __isl_give isl_schedule *isl_schedule_cow(__isl_take isl_schedule *schedule)
106 isl_ctx *ctx;
107 isl_schedule_tree *tree;
109 if (!schedule)
110 return NULL;
111 if (schedule->ref == 1)
112 return schedule;
114 ctx = isl_schedule_get_ctx(schedule);
115 if (!schedule->root)
116 isl_die(ctx, isl_error_internal,
117 "only for schedule tree based schedules",
118 return isl_schedule_free(schedule));
119 schedule->ref--;
120 tree = isl_schedule_tree_copy(schedule->root);
121 return isl_schedule_from_schedule_tree(ctx, tree);
124 __isl_null isl_schedule *isl_schedule_free(__isl_take isl_schedule *sched)
126 if (!sched)
127 return NULL;
129 if (--sched->ref > 0)
130 return NULL;
132 isl_band_list_free(sched->band_forest);
133 isl_schedule_tree_free(sched->root);
134 isl_ctx_deref(sched->leaf.ctx);
135 free(sched);
136 return NULL;
139 /* Replace the root of "schedule" by "tree".
141 __isl_give isl_schedule *isl_schedule_set_root(
142 __isl_take isl_schedule *schedule, __isl_take isl_schedule_tree *tree)
144 if (!schedule || !tree)
145 goto error;
146 if (schedule->root == tree) {
147 isl_schedule_tree_free(tree);
148 return schedule;
151 schedule = isl_schedule_cow(schedule);
152 if (!schedule)
153 goto error;
154 isl_schedule_tree_free(schedule->root);
155 schedule->root = tree;
157 return schedule;
158 error:
159 isl_schedule_free(schedule);
160 isl_schedule_tree_free(tree);
161 return NULL;
164 isl_ctx *isl_schedule_get_ctx(__isl_keep isl_schedule *schedule)
166 return schedule ? schedule->leaf.ctx : NULL;
169 /* Return a pointer to the leaf of "schedule".
171 * Even though these leaves are not reference counted, we still
172 * indicate that this function does not return a copy.
174 __isl_keep isl_schedule_tree *isl_schedule_peek_leaf(
175 __isl_keep isl_schedule *schedule)
177 return schedule ? &schedule->leaf : NULL;
180 /* Return the (parameter) space of the schedule, i.e., the space
181 * of the root domain.
183 __isl_give isl_space *isl_schedule_get_space(
184 __isl_keep isl_schedule *schedule)
186 enum isl_schedule_node_type type;
187 isl_space *space;
188 isl_union_set *domain;
190 if (!schedule)
191 return NULL;
192 if (!schedule->root)
193 isl_die(isl_schedule_get_ctx(schedule), isl_error_invalid,
194 "schedule tree representation not available",
195 return NULL);
196 type = isl_schedule_tree_get_type(schedule->root);
197 if (type != isl_schedule_node_domain)
198 isl_die(isl_schedule_get_ctx(schedule), isl_error_internal,
199 "root node not a domain node", return NULL);
201 domain = isl_schedule_tree_domain_get_domain(schedule->root);
202 space = isl_union_set_get_space(domain);
203 isl_union_set_free(domain);
205 return space;
208 /* Return a pointer to the root of "schedule".
210 __isl_give isl_schedule_node *isl_schedule_get_root(
211 __isl_keep isl_schedule *schedule)
213 isl_ctx *ctx;
214 isl_schedule_tree *tree;
215 isl_schedule_tree_list *ancestors;
217 if (!schedule)
218 return NULL;
220 if (!schedule->root)
221 isl_die(isl_schedule_get_ctx(schedule), isl_error_invalid,
222 "schedule tree representation not available",
223 return NULL);
225 ctx = isl_schedule_get_ctx(schedule);
226 tree = isl_schedule_tree_copy(schedule->root);
227 schedule = isl_schedule_copy(schedule);
228 ancestors = isl_schedule_tree_list_alloc(ctx, 0);
229 return isl_schedule_node_alloc(schedule, tree, ancestors, NULL);
232 /* Set max_out to the maximal number of output dimensions over
233 * all maps.
235 static int update_max_out(__isl_take isl_map *map, void *user)
237 int *max_out = user;
238 int n_out = isl_map_dim(map, isl_dim_out);
240 if (n_out > *max_out)
241 *max_out = n_out;
243 isl_map_free(map);
244 return 0;
247 /* Internal data structure for map_pad_range.
249 * "max_out" is the maximal schedule dimension.
250 * "res" collects the results.
252 struct isl_pad_schedule_map_data {
253 int max_out;
254 isl_union_map *res;
257 /* Pad the range of the given map with zeros to data->max_out and
258 * then add the result to data->res.
260 static int map_pad_range(__isl_take isl_map *map, void *user)
262 struct isl_pad_schedule_map_data *data = user;
263 int i;
264 int n_out = isl_map_dim(map, isl_dim_out);
266 map = isl_map_add_dims(map, isl_dim_out, data->max_out - n_out);
267 for (i = n_out; i < data->max_out; ++i)
268 map = isl_map_fix_si(map, isl_dim_out, i, 0);
270 data->res = isl_union_map_add_map(data->res, map);
271 if (!data->res)
272 return -1;
274 return 0;
277 /* Pad the ranges of the maps in the union map with zeros such they all have
278 * the same dimension.
280 static __isl_give isl_union_map *pad_schedule_map(
281 __isl_take isl_union_map *umap)
283 struct isl_pad_schedule_map_data data;
285 if (!umap)
286 return NULL;
287 if (isl_union_map_n_map(umap) <= 1)
288 return umap;
290 data.max_out = 0;
291 if (isl_union_map_foreach_map(umap, &update_max_out, &data.max_out) < 0)
292 return isl_union_map_free(umap);
294 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
295 if (isl_union_map_foreach_map(umap, &map_pad_range, &data) < 0)
296 data.res = isl_union_map_free(data.res);
298 isl_union_map_free(umap);
299 return data.res;
302 /* Return the domain of the root domain node of "schedule".
304 __isl_give isl_union_set *isl_schedule_get_domain(
305 __isl_keep isl_schedule *schedule)
307 if (!schedule)
308 return NULL;
309 if (!schedule->root)
310 isl_die(isl_schedule_get_ctx(schedule), isl_error_invalid,
311 "schedule tree representation not available",
312 return NULL);
313 return isl_schedule_tree_domain_get_domain(schedule->root);
316 /* Traverse all nodes of "sched" in depth first preorder.
318 * If "fn" returns -1 on any of the nodes, then the traversal is aborted.
319 * If "fn" returns 0 on any of the nodes, then the subtree rooted
320 * at that node is skipped.
322 * Return 0 on success and -1 on failure.
324 int isl_schedule_foreach_schedule_node(__isl_keep isl_schedule *sched,
325 int (*fn)(__isl_keep isl_schedule_node *node, void *user), void *user)
327 isl_schedule_node *node;
328 int r;
330 if (!sched)
331 return -1;
333 node = isl_schedule_get_root(sched);
334 r = isl_schedule_node_foreach_descendant(node, fn, user);
335 isl_schedule_node_free(node);
337 return r;
340 /* Return an isl_union_map representation of the schedule.
341 * If we still have access to the schedule tree, then we return
342 * an isl_union_map corresponding to the subtree schedule of the child
343 * of the root domain node. That is, we do not intersect the domain
344 * of the returned isl_union_map with the domain constraints.
345 * Otherwise, we must have removed it because we created a band forest.
346 * If so, we extract the isl_union_map from the forest.
347 * This reconstructed schedule map
348 * then needs to be padded with zeros to unify the schedule space
349 * since the result of isl_band_list_get_suffix_schedule may not have
350 * a unified schedule space.
352 __isl_give isl_union_map *isl_schedule_get_map(__isl_keep isl_schedule *sched)
354 enum isl_schedule_node_type type;
355 isl_schedule_node *node;
356 isl_union_map *umap;
358 if (!sched)
359 return NULL;
361 if (sched->root) {
362 type = isl_schedule_tree_get_type(sched->root);
363 if (type != isl_schedule_node_domain)
364 isl_die(isl_schedule_get_ctx(sched), isl_error_internal,
365 "root node not a domain node", return NULL);
367 node = isl_schedule_get_root(sched);
368 node = isl_schedule_node_child(node, 0);
369 umap = isl_schedule_node_get_subtree_schedule_union_map(node);
370 isl_schedule_node_free(node);
372 return umap;
375 umap = isl_band_list_get_suffix_schedule(sched->band_forest);
376 return pad_schedule_map(umap);
379 static __isl_give isl_band_list *construct_band_list(
380 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain,
381 __isl_keep isl_band *parent);
383 /* Construct an isl_band structure from the given schedule tree node,
384 * which may be either a band node or a leaf node.
385 * In the latter case, construct a zero-dimensional band.
386 * "domain" is the universe set of the domain elements that reach "node".
387 * "parent" is the parent isl_band of the isl_band constructed
388 * by this function.
390 * In case of a band node, we copy the properties (except tilability,
391 * which is implicit in an isl_band) to the isl_band.
392 * We assume that the band node is not zero-dimensional.
393 * If the child of the band node is not a leaf node,
394 * then we extract the children of the isl_band from this child.
396 static __isl_give isl_band *construct_band(__isl_take isl_schedule_node *node,
397 __isl_take isl_union_set *domain, __isl_keep isl_band *parent)
399 int i;
400 isl_ctx *ctx;
401 isl_band *band = NULL;
402 isl_multi_union_pw_aff *mupa;
404 if (!node || !domain)
405 goto error;
407 ctx = isl_schedule_node_get_ctx(node);
408 band = isl_band_alloc(ctx);
409 if (!band)
410 goto error;
412 band->schedule = node->schedule;
413 band->parent = parent;
415 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
416 band->n = 0;
417 band->pma = isl_union_pw_multi_aff_from_domain(domain);
418 isl_schedule_node_free(node);
419 return band;
422 band->n = isl_schedule_node_band_n_member(node);
423 if (band->n == 0)
424 isl_die(ctx, isl_error_unsupported,
425 "zero-dimensional band nodes not supported",
426 goto error);
427 band->coincident = isl_alloc_array(ctx, int, band->n);
428 if (band->n && !band->coincident)
429 goto error;
430 for (i = 0; i < band->n; ++i)
431 band->coincident[i] =
432 isl_schedule_node_band_member_get_coincident(node, i);
433 mupa = isl_schedule_node_band_get_partial_schedule(node);
434 band->pma = isl_union_pw_multi_aff_from_multi_union_pw_aff(mupa);
435 if (!band->pma)
436 goto error;
438 node = isl_schedule_node_child(node, 0);
439 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
440 isl_schedule_node_free(node);
441 isl_union_set_free(domain);
442 return band;
445 band->children = construct_band_list(node, domain, band);
446 if (!band->children)
447 return isl_band_free(band);
449 return band;
450 error:
451 isl_union_set_free(domain);
452 isl_schedule_node_free(node);
453 isl_band_free(band);
454 return NULL;
457 /* Construct a list of isl_band structures from the children of "node".
458 * "node" itself is a sequence or set node, so that each of the child nodes
459 * is a filter node and the list returned by node_construct_band_list
460 * consists of a single element.
461 * "domain" is the universe set of the domain elements that reach "node".
462 * "parent" is the parent isl_band of the isl_band structures constructed
463 * by this function.
465 static __isl_give isl_band_list *construct_band_list_from_children(
466 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain,
467 __isl_keep isl_band *parent)
469 int i, n;
470 isl_ctx *ctx;
471 isl_band_list *list;
473 n = isl_schedule_node_n_children(node);
475 ctx = isl_schedule_node_get_ctx(node);
476 list = isl_band_list_alloc(ctx, 0);
477 for (i = 0; i < n; ++i) {
478 isl_schedule_node *child;
479 isl_band_list *list_i;
481 child = isl_schedule_node_get_child(node, i);
482 list_i = construct_band_list(child, isl_union_set_copy(domain),
483 parent);
484 list = isl_band_list_concat(list, list_i);
487 isl_union_set_free(domain);
488 isl_schedule_node_free(node);
490 return list;
493 /* Construct an isl_band structure from the given sequence node
494 * (or set node that is treated as a sequence node).
495 * A single-dimensional band is created with as schedule for each of
496 * filters of the children, the corresponding child position.
497 * "domain" is the universe set of the domain elements that reach "node".
498 * "parent" is the parent isl_band of the isl_band constructed
499 * by this function.
501 static __isl_give isl_band_list *construct_band_list_sequence(
502 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain,
503 __isl_keep isl_band *parent)
505 int i, n;
506 isl_ctx *ctx;
507 isl_band *band = NULL;
508 isl_space *space;
509 isl_union_pw_multi_aff *upma;
511 if (!node || !domain)
512 goto error;
514 ctx = isl_schedule_node_get_ctx(node);
515 band = isl_band_alloc(ctx);
516 if (!band)
517 goto error;
519 band->schedule = node->schedule;
520 band->parent = parent;
521 band->n = 1;
522 band->coincident = isl_calloc_array(ctx, int, band->n);
523 if (!band->coincident)
524 goto error;
526 n = isl_schedule_node_n_children(node);
527 space = isl_union_set_get_space(domain);
528 upma = isl_union_pw_multi_aff_empty(isl_space_copy(space));
530 space = isl_space_set_from_params(space);
531 space = isl_space_add_dims(space, isl_dim_set, 1);
533 for (i = 0; i < n; ++i) {
534 isl_schedule_node *child;
535 isl_union_set *filter;
536 isl_val *v;
537 isl_val_list *vl;
538 isl_multi_val *mv;
539 isl_union_pw_multi_aff *upma_i;
541 child = isl_schedule_node_get_child(node, i);
542 filter = isl_schedule_node_filter_get_filter(child);
543 isl_schedule_node_free(child);
544 filter = isl_union_set_intersect(filter,
545 isl_union_set_copy(domain));
546 v = isl_val_int_from_si(ctx, i);
547 vl = isl_val_list_from_val(v);
548 mv = isl_multi_val_from_val_list(isl_space_copy(space), vl);
549 upma_i = isl_union_pw_multi_aff_multi_val_on_domain(filter, mv);
550 upma = isl_union_pw_multi_aff_union_add(upma, upma_i);
553 isl_space_free(space);
555 band->pma = upma;
556 if (!band->pma)
557 goto error;
559 band->children = construct_band_list_from_children(node, domain, band);
560 if (!band->children)
561 band = isl_band_free(band);
562 return isl_band_list_from_band(band);
563 error:
564 isl_union_set_free(domain);
565 isl_schedule_node_free(node);
566 isl_band_free(band);
567 return NULL;
570 /* Construct a list of isl_band structures from "node" depending
571 * on the type of "node".
572 * "domain" is the universe set of the domain elements that reach "node".
573 * "parent" is the parent isl_band of the isl_band structures constructed
574 * by this function.
576 * If schedule_separate_components is set then set nodes are treated
577 * as sequence nodes. Otherwise, we directly extract an (implicitly
578 * parallel) list of isl_band structures.
580 * If "node" is a filter, then "domain" is updated by the filter.
582 static __isl_give isl_band_list *construct_band_list(
583 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain,
584 __isl_keep isl_band *parent)
586 enum isl_schedule_node_type type;
587 isl_ctx *ctx;
588 isl_band *band;
589 isl_band_list *list;
590 isl_union_set *filter;
592 if (!node || !domain)
593 goto error;
595 type = isl_schedule_node_get_type(node);
596 switch (type) {
597 case isl_schedule_node_error:
598 goto error;
599 case isl_schedule_node_domain:
600 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
601 "internal domain nodes not allowed", goto error);
602 case isl_schedule_node_filter:
603 filter = isl_schedule_node_filter_get_filter(node);
604 domain = isl_union_set_intersect(domain, filter);
605 node = isl_schedule_node_child(node, 0);
606 return construct_band_list(node, domain, parent);
607 case isl_schedule_node_set:
608 ctx = isl_schedule_node_get_ctx(node);
609 if (isl_options_get_schedule_separate_components(ctx))
610 return construct_band_list_sequence(node, domain,
611 parent);
612 else
613 return construct_band_list_from_children(node, domain,
614 parent);
615 case isl_schedule_node_sequence:
616 return construct_band_list_sequence(node, domain, parent);
617 case isl_schedule_node_leaf:
618 case isl_schedule_node_band:
619 band = construct_band(node, domain, parent);
620 list = isl_band_list_from_band(band);
621 break;
624 return list;
625 error:
626 isl_union_set_free(domain);
627 isl_schedule_node_free(node);
628 return NULL;
631 /* Return the roots of a band forest representation of the schedule.
632 * The band forest is constructed from the schedule tree,
633 * but once such a band forest is
634 * constructed, we forget about the original schedule tree since
635 * the user may modify the schedule through the band forest.
637 __isl_give isl_band_list *isl_schedule_get_band_forest(
638 __isl_keep isl_schedule *schedule)
640 isl_schedule_node *node;
641 isl_union_set *domain;
643 if (!schedule)
644 return NULL;
645 if (schedule->root) {
646 node = isl_schedule_get_root(schedule);
647 domain = isl_schedule_node_domain_get_domain(node);
648 domain = isl_union_set_universe(domain);
649 node = isl_schedule_node_child(node, 0);
651 schedule->band_forest = construct_band_list(node, domain, NULL);
652 schedule->root = isl_schedule_tree_free(schedule->root);
654 return isl_band_list_dup(schedule->band_forest);
657 /* Call "fn" on each band in the schedule in depth-first post-order.
659 int isl_schedule_foreach_band(__isl_keep isl_schedule *sched,
660 int (*fn)(__isl_keep isl_band *band, void *user), void *user)
662 int r;
663 isl_band_list *forest;
665 if (!sched)
666 return -1;
668 forest = isl_schedule_get_band_forest(sched);
669 r = isl_band_list_foreach_band(forest, fn, user);
670 isl_band_list_free(forest);
672 return r;
675 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
676 __isl_keep isl_band_list *list);
678 static __isl_give isl_printer *print_band(__isl_take isl_printer *p,
679 __isl_keep isl_band *band)
681 isl_band_list *children;
683 p = isl_printer_start_line(p);
684 p = isl_printer_print_union_pw_multi_aff(p, band->pma);
685 p = isl_printer_end_line(p);
687 if (!isl_band_has_children(band))
688 return p;
690 children = isl_band_get_children(band);
692 p = isl_printer_indent(p, 4);
693 p = print_band_list(p, children);
694 p = isl_printer_indent(p, -4);
696 isl_band_list_free(children);
698 return p;
701 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
702 __isl_keep isl_band_list *list)
704 int i, n;
706 n = isl_band_list_n_band(list);
707 for (i = 0; i < n; ++i) {
708 isl_band *band;
709 band = isl_band_list_get_band(list, i);
710 p = print_band(p, band);
711 isl_band_free(band);
714 return p;
717 /* Print "schedule" to "p".
719 * If "schedule" was created from a schedule tree, then we print
720 * the schedule tree representation. Otherwise, we print
721 * the band forest representation.
723 __isl_give isl_printer *isl_printer_print_schedule(__isl_take isl_printer *p,
724 __isl_keep isl_schedule *schedule)
726 isl_band_list *forest;
728 if (!schedule)
729 return isl_printer_free(p);
731 if (schedule->root)
732 return isl_printer_print_schedule_tree(p, schedule->root);
734 forest = isl_schedule_get_band_forest(schedule);
736 p = print_band_list(p, forest);
738 isl_band_list_free(forest);
740 return p;
743 void isl_schedule_dump(__isl_keep isl_schedule *schedule)
745 isl_printer *printer;
747 if (!schedule)
748 return;
750 printer = isl_printer_to_file(isl_schedule_get_ctx(schedule), stderr);
751 printer = isl_printer_set_yaml_style(printer, ISL_YAML_STYLE_BLOCK);
752 printer = isl_printer_print_schedule(printer, schedule);
754 isl_printer_free(printer);