isl_union_*_alloc: plug memory leak on error path
[isl.git] / isl_schedule.c
blobbc5ed8571c3ea30fec58324760921fa29f0a22ab
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2013 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_private.h>
14 #include <isl_map_private.h>
15 #include <isl_space_private.h>
16 #include <isl/aff.h>
17 #include <isl/hash.h>
18 #include <isl/constraint.h>
19 #include <isl/schedule.h>
20 #include <isl_mat_private.h>
21 #include <isl/set.h>
22 #include <isl/seq.h>
23 #include <isl_tab.h>
24 #include <isl_dim_map.h>
25 #include <isl_hmap_map_basic_set.h>
26 #include <isl_sort.h>
27 #include <isl_schedule_private.h>
28 #include <isl_band_private.h>
29 #include <isl_options_private.h>
30 #include <isl_tarjan.h>
33 * The scheduling algorithm implemented in this file was inspired by
34 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
35 * Parallelization and Locality Optimization in the Polyhedral Model".
39 /* Internal information about a node that is used during the construction
40 * of a schedule.
41 * dim represents the space in which the domain lives
42 * sched is a matrix representation of the schedule being constructed
43 * for this node
44 * sched_map is an isl_map representation of the same (partial) schedule
45 * sched_map may be NULL
46 * rank is the number of linearly independent rows in the linear part
47 * of sched
48 * the columns of cmap represent a change of basis for the schedule
49 * coefficients; the first rank columns span the linear part of
50 * the schedule rows
51 * cinv is the inverse of cmap.
52 * start is the first variable in the LP problem in the sequences that
53 * represents the schedule coefficients of this node
54 * nvar is the dimension of the domain
55 * nparam is the number of parameters or 0 if we are not constructing
56 * a parametric schedule
58 * scc is the index of SCC (or WCC) this node belongs to
60 * band contains the band index for each of the rows of the schedule.
61 * band_id is used to differentiate between separate bands at the same
62 * level within the same parent band, i.e., bands that are separated
63 * by the parent band or bands that are independent of each other.
64 * zero contains a boolean for each of the rows of the schedule,
65 * indicating whether the corresponding scheduling dimension results
66 * in zero dependence distances within its band and with respect
67 * to the proximity edges.
69 struct isl_sched_node {
70 isl_space *dim;
71 isl_mat *sched;
72 isl_map *sched_map;
73 int rank;
74 isl_mat *cmap;
75 isl_mat *cinv;
76 int start;
77 int nvar;
78 int nparam;
80 int scc;
82 int *band;
83 int *band_id;
84 int *zero;
87 static int node_has_dim(const void *entry, const void *val)
89 struct isl_sched_node *node = (struct isl_sched_node *)entry;
90 isl_space *dim = (isl_space *)val;
92 return isl_space_is_equal(node->dim, dim);
95 /* An edge in the dependence graph. An edge may be used to
96 * ensure validity of the generated schedule, to minimize the dependence
97 * distance or both
99 * map is the dependence relation
100 * src is the source node
101 * dst is the sink node
102 * validity is set if the edge is used to ensure correctness
103 * proximity is set if the edge is used to minimize dependence distances
105 * For validity edges, start and end mark the sequence of inequality
106 * constraints in the LP problem that encode the validity constraint
107 * corresponding to this edge.
109 struct isl_sched_edge {
110 isl_map *map;
112 struct isl_sched_node *src;
113 struct isl_sched_node *dst;
115 int validity;
116 int proximity;
118 int start;
119 int end;
122 enum isl_edge_type {
123 isl_edge_validity = 0,
124 isl_edge_first = isl_edge_validity,
125 isl_edge_proximity,
126 isl_edge_last = isl_edge_proximity
129 /* Internal information about the dependence graph used during
130 * the construction of the schedule.
132 * intra_hmap is a cache, mapping dependence relations to their dual,
133 * for dependences from a node to itself
134 * inter_hmap is a cache, mapping dependence relations to their dual,
135 * for dependences between distinct nodes
137 * n is the number of nodes
138 * node is the list of nodes
139 * maxvar is the maximal number of variables over all nodes
140 * max_row is the allocated number of rows in the schedule
141 * n_row is the current (maximal) number of linearly independent
142 * rows in the node schedules
143 * n_total_row is the current number of rows in the node schedules
144 * n_band is the current number of completed bands
145 * band_start is the starting row in the node schedules of the current band
146 * root is set if this graph is the original dependence graph,
147 * without any splitting
149 * sorted contains a list of node indices sorted according to the
150 * SCC to which a node belongs
152 * n_edge is the number of edges
153 * edge is the list of edges
154 * max_edge contains the maximal number of edges of each type;
155 * in particular, it contains the number of edges in the inital graph.
156 * edge_table contains pointers into the edge array, hashed on the source
157 * and sink spaces; there is one such table for each type;
158 * a given edge may be referenced from more than one table
159 * if the corresponding relation appears in more than of the
160 * sets of dependences
162 * node_table contains pointers into the node array, hashed on the space
164 * region contains a list of variable sequences that should be non-trivial
166 * lp contains the (I)LP problem used to obtain new schedule rows
168 * src_scc and dst_scc are the source and sink SCCs of an edge with
169 * conflicting constraints
171 * scc represents the number of components
173 struct isl_sched_graph {
174 isl_hmap_map_basic_set *intra_hmap;
175 isl_hmap_map_basic_set *inter_hmap;
177 struct isl_sched_node *node;
178 int n;
179 int maxvar;
180 int max_row;
181 int n_row;
183 int *sorted;
185 int n_band;
186 int n_total_row;
187 int band_start;
189 int root;
191 struct isl_sched_edge *edge;
192 int n_edge;
193 int max_edge[isl_edge_last + 1];
194 struct isl_hash_table *edge_table[isl_edge_last + 1];
196 struct isl_hash_table *node_table;
197 struct isl_region *region;
199 isl_basic_set *lp;
201 int src_scc;
202 int dst_scc;
204 int scc;
207 /* Initialize node_table based on the list of nodes.
209 static int graph_init_table(isl_ctx *ctx, struct isl_sched_graph *graph)
211 int i;
213 graph->node_table = isl_hash_table_alloc(ctx, graph->n);
214 if (!graph->node_table)
215 return -1;
217 for (i = 0; i < graph->n; ++i) {
218 struct isl_hash_table_entry *entry;
219 uint32_t hash;
221 hash = isl_space_get_hash(graph->node[i].dim);
222 entry = isl_hash_table_find(ctx, graph->node_table, hash,
223 &node_has_dim,
224 graph->node[i].dim, 1);
225 if (!entry)
226 return -1;
227 entry->data = &graph->node[i];
230 return 0;
233 /* Return a pointer to the node that lives within the given space,
234 * or NULL if there is no such node.
236 static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
237 struct isl_sched_graph *graph, __isl_keep isl_space *dim)
239 struct isl_hash_table_entry *entry;
240 uint32_t hash;
242 hash = isl_space_get_hash(dim);
243 entry = isl_hash_table_find(ctx, graph->node_table, hash,
244 &node_has_dim, dim, 0);
246 return entry ? entry->data : NULL;
249 static int edge_has_src_and_dst(const void *entry, const void *val)
251 const struct isl_sched_edge *edge = entry;
252 const struct isl_sched_edge *temp = val;
254 return edge->src == temp->src && edge->dst == temp->dst;
257 /* Add the given edge to graph->edge_table[type].
259 static int graph_edge_table_add(isl_ctx *ctx, struct isl_sched_graph *graph,
260 enum isl_edge_type type, struct isl_sched_edge *edge)
262 struct isl_hash_table_entry *entry;
263 uint32_t hash;
265 hash = isl_hash_init();
266 hash = isl_hash_builtin(hash, edge->src);
267 hash = isl_hash_builtin(hash, edge->dst);
268 entry = isl_hash_table_find(ctx, graph->edge_table[type], hash,
269 &edge_has_src_and_dst, edge, 1);
270 if (!entry)
271 return -1;
272 entry->data = edge;
274 return 0;
277 /* Allocate the edge_tables based on the maximal number of edges of
278 * each type.
280 static int graph_init_edge_tables(isl_ctx *ctx, struct isl_sched_graph *graph)
282 int i;
284 for (i = 0; i <= isl_edge_last; ++i) {
285 graph->edge_table[i] = isl_hash_table_alloc(ctx,
286 graph->max_edge[i]);
287 if (!graph->edge_table[i])
288 return -1;
291 return 0;
294 /* If graph->edge_table[type] contains an edge from the given source
295 * to the given destination, then return the hash table entry of this edge.
296 * Otherwise, return NULL.
298 static struct isl_hash_table_entry *graph_find_edge_entry(
299 struct isl_sched_graph *graph,
300 enum isl_edge_type type,
301 struct isl_sched_node *src, struct isl_sched_node *dst)
303 isl_ctx *ctx = isl_space_get_ctx(src->dim);
304 uint32_t hash;
305 struct isl_sched_edge temp = { .src = src, .dst = dst };
307 hash = isl_hash_init();
308 hash = isl_hash_builtin(hash, temp.src);
309 hash = isl_hash_builtin(hash, temp.dst);
310 return isl_hash_table_find(ctx, graph->edge_table[type], hash,
311 &edge_has_src_and_dst, &temp, 0);
315 /* If graph->edge_table[type] contains an edge from the given source
316 * to the given destination, then return this edge.
317 * Otherwise, return NULL.
319 static struct isl_sched_edge *graph_find_edge(struct isl_sched_graph *graph,
320 enum isl_edge_type type,
321 struct isl_sched_node *src, struct isl_sched_node *dst)
323 struct isl_hash_table_entry *entry;
325 entry = graph_find_edge_entry(graph, type, src, dst);
326 if (!entry)
327 return NULL;
329 return entry->data;
332 /* Check whether the dependence graph has an edge of the given type
333 * between the given two nodes.
335 static int graph_has_edge(struct isl_sched_graph *graph,
336 enum isl_edge_type type,
337 struct isl_sched_node *src, struct isl_sched_node *dst)
339 struct isl_sched_edge *edge;
340 int empty;
342 edge = graph_find_edge(graph, type, src, dst);
343 if (!edge)
344 return 0;
346 empty = isl_map_plain_is_empty(edge->map);
347 if (empty < 0)
348 return -1;
350 return !empty;
353 /* If there is an edge from the given source to the given destination
354 * of any type then return this edge.
355 * Otherwise, return NULL.
357 static struct isl_sched_edge *graph_find_any_edge(struct isl_sched_graph *graph,
358 struct isl_sched_node *src, struct isl_sched_node *dst)
360 enum isl_edge_type i;
361 struct isl_sched_edge *edge;
363 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
364 edge = graph_find_edge(graph, i, src, dst);
365 if (edge)
366 return edge;
369 return NULL;
372 /* Remove the given edge from all the edge_tables that refer to it.
374 static void graph_remove_edge(struct isl_sched_graph *graph,
375 struct isl_sched_edge *edge)
377 isl_ctx *ctx = isl_map_get_ctx(edge->map);
378 enum isl_edge_type i;
380 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
381 struct isl_hash_table_entry *entry;
383 entry = graph_find_edge_entry(graph, i, edge->src, edge->dst);
384 if (!entry)
385 continue;
386 if (entry->data != edge)
387 continue;
388 isl_hash_table_remove(ctx, graph->edge_table[i], entry);
392 /* Check whether the dependence graph has any edge
393 * between the given two nodes.
395 static int graph_has_any_edge(struct isl_sched_graph *graph,
396 struct isl_sched_node *src, struct isl_sched_node *dst)
398 enum isl_edge_type i;
399 int r;
401 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
402 r = graph_has_edge(graph, i, src, dst);
403 if (r < 0 || r)
404 return r;
407 return r;
410 /* Check whether the dependence graph has a validity edge
411 * between the given two nodes.
413 static int graph_has_validity_edge(struct isl_sched_graph *graph,
414 struct isl_sched_node *src, struct isl_sched_node *dst)
416 return graph_has_edge(graph, isl_edge_validity, src, dst);
419 static int graph_alloc(isl_ctx *ctx, struct isl_sched_graph *graph,
420 int n_node, int n_edge)
422 int i;
424 graph->n = n_node;
425 graph->n_edge = n_edge;
426 graph->node = isl_calloc_array(ctx, struct isl_sched_node, graph->n);
427 graph->sorted = isl_calloc_array(ctx, int, graph->n);
428 graph->region = isl_alloc_array(ctx, struct isl_region, graph->n);
429 graph->edge = isl_calloc_array(ctx,
430 struct isl_sched_edge, graph->n_edge);
432 graph->intra_hmap = isl_hmap_map_basic_set_alloc(ctx, 2 * n_edge);
433 graph->inter_hmap = isl_hmap_map_basic_set_alloc(ctx, 2 * n_edge);
435 if (!graph->node || !graph->region || (graph->n_edge && !graph->edge) ||
436 !graph->sorted)
437 return -1;
439 for(i = 0; i < graph->n; ++i)
440 graph->sorted[i] = i;
442 return 0;
445 static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
447 int i;
449 isl_hmap_map_basic_set_free(ctx, graph->intra_hmap);
450 isl_hmap_map_basic_set_free(ctx, graph->inter_hmap);
452 if (graph->node)
453 for (i = 0; i < graph->n; ++i) {
454 isl_space_free(graph->node[i].dim);
455 isl_mat_free(graph->node[i].sched);
456 isl_map_free(graph->node[i].sched_map);
457 isl_mat_free(graph->node[i].cmap);
458 isl_mat_free(graph->node[i].cinv);
459 if (graph->root) {
460 free(graph->node[i].band);
461 free(graph->node[i].band_id);
462 free(graph->node[i].zero);
465 free(graph->node);
466 free(graph->sorted);
467 if (graph->edge)
468 for (i = 0; i < graph->n_edge; ++i)
469 isl_map_free(graph->edge[i].map);
470 free(graph->edge);
471 free(graph->region);
472 for (i = 0; i <= isl_edge_last; ++i)
473 isl_hash_table_free(ctx, graph->edge_table[i]);
474 isl_hash_table_free(ctx, graph->node_table);
475 isl_basic_set_free(graph->lp);
478 /* For each "set" on which this function is called, increment
479 * graph->n by one and update graph->maxvar.
481 static int init_n_maxvar(__isl_take isl_set *set, void *user)
483 struct isl_sched_graph *graph = user;
484 int nvar = isl_set_dim(set, isl_dim_set);
486 graph->n++;
487 if (nvar > graph->maxvar)
488 graph->maxvar = nvar;
490 isl_set_free(set);
492 return 0;
495 /* Compute the number of rows that should be allocated for the schedule.
496 * The graph can be split at most "n - 1" times, there can be at most
497 * two rows for each dimension in the iteration domains (in particular,
498 * we usually have one row, but it may be split by split_scaled),
499 * and there can be one extra row for ordering the statements.
500 * Note that if we have actually split "n - 1" times, then no ordering
501 * is needed, so in principle we could use "graph->n + 2 * graph->maxvar - 1".
503 static int compute_max_row(struct isl_sched_graph *graph,
504 __isl_keep isl_union_set *domain)
506 graph->n = 0;
507 graph->maxvar = 0;
508 if (isl_union_set_foreach_set(domain, &init_n_maxvar, graph) < 0)
509 return -1;
510 graph->max_row = graph->n + 2 * graph->maxvar;
512 return 0;
515 /* Add a new node to the graph representing the given set.
517 static int extract_node(__isl_take isl_set *set, void *user)
519 int nvar, nparam;
520 isl_ctx *ctx;
521 isl_space *dim;
522 isl_mat *sched;
523 struct isl_sched_graph *graph = user;
524 int *band, *band_id, *zero;
526 ctx = isl_set_get_ctx(set);
527 dim = isl_set_get_space(set);
528 isl_set_free(set);
529 nvar = isl_space_dim(dim, isl_dim_set);
530 nparam = isl_space_dim(dim, isl_dim_param);
531 if (!ctx->opt->schedule_parametric)
532 nparam = 0;
533 sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
534 graph->node[graph->n].dim = dim;
535 graph->node[graph->n].nvar = nvar;
536 graph->node[graph->n].nparam = nparam;
537 graph->node[graph->n].sched = sched;
538 graph->node[graph->n].sched_map = NULL;
539 band = isl_alloc_array(ctx, int, graph->max_row);
540 graph->node[graph->n].band = band;
541 band_id = isl_calloc_array(ctx, int, graph->max_row);
542 graph->node[graph->n].band_id = band_id;
543 zero = isl_calloc_array(ctx, int, graph->max_row);
544 graph->node[graph->n].zero = zero;
545 graph->n++;
547 if (!sched || (graph->max_row && (!band || !band_id || !zero)))
548 return -1;
550 return 0;
553 struct isl_extract_edge_data {
554 enum isl_edge_type type;
555 struct isl_sched_graph *graph;
558 /* Add a new edge to the graph based on the given map
559 * and add it to data->graph->edge_table[data->type].
560 * If a dependence relation of a given type happens to be identical
561 * to one of the dependence relations of a type that was added before,
562 * then we don't create a new edge, but instead mark the original edge
563 * as also representing a dependence of the current type.
565 static int extract_edge(__isl_take isl_map *map, void *user)
567 isl_ctx *ctx = isl_map_get_ctx(map);
568 struct isl_extract_edge_data *data = user;
569 struct isl_sched_graph *graph = data->graph;
570 struct isl_sched_node *src, *dst;
571 isl_space *dim;
572 struct isl_sched_edge *edge;
573 int is_equal;
575 dim = isl_space_domain(isl_map_get_space(map));
576 src = graph_find_node(ctx, graph, dim);
577 isl_space_free(dim);
578 dim = isl_space_range(isl_map_get_space(map));
579 dst = graph_find_node(ctx, graph, dim);
580 isl_space_free(dim);
582 if (!src || !dst) {
583 isl_map_free(map);
584 return 0;
587 graph->edge[graph->n_edge].src = src;
588 graph->edge[graph->n_edge].dst = dst;
589 graph->edge[graph->n_edge].map = map;
590 if (data->type == isl_edge_validity) {
591 graph->edge[graph->n_edge].validity = 1;
592 graph->edge[graph->n_edge].proximity = 0;
594 if (data->type == isl_edge_proximity) {
595 graph->edge[graph->n_edge].validity = 0;
596 graph->edge[graph->n_edge].proximity = 1;
598 graph->n_edge++;
600 edge = graph_find_any_edge(graph, src, dst);
601 if (!edge)
602 return graph_edge_table_add(ctx, graph, data->type,
603 &graph->edge[graph->n_edge - 1]);
604 is_equal = isl_map_plain_is_equal(map, edge->map);
605 if (is_equal < 0)
606 return -1;
607 if (!is_equal)
608 return graph_edge_table_add(ctx, graph, data->type,
609 &graph->edge[graph->n_edge - 1]);
611 graph->n_edge--;
612 edge->validity |= graph->edge[graph->n_edge].validity;
613 edge->proximity |= graph->edge[graph->n_edge].proximity;
614 isl_map_free(map);
616 return graph_edge_table_add(ctx, graph, data->type, edge);
619 /* Check whether there is any dependence from node[j] to node[i]
620 * or from node[i] to node[j].
622 static int node_follows_weak(int i, int j, void *user)
624 int f;
625 struct isl_sched_graph *graph = user;
627 f = graph_has_any_edge(graph, &graph->node[j], &graph->node[i]);
628 if (f < 0 || f)
629 return f;
630 return graph_has_any_edge(graph, &graph->node[i], &graph->node[j]);
633 /* Check whether there is a validity dependence from node[j] to node[i],
634 * forcing node[i] to follow node[j].
636 static int node_follows_strong(int i, int j, void *user)
638 struct isl_sched_graph *graph = user;
640 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
643 /* Use Tarjan's algorithm for computing the strongly connected components
644 * in the dependence graph (only validity edges).
645 * If weak is set, we consider the graph to be undirected and
646 * we effectively compute the (weakly) connected components.
647 * Additionally, we also consider other edges when weak is set.
649 static int detect_ccs(isl_ctx *ctx, struct isl_sched_graph *graph, int weak)
651 int i, n;
652 struct isl_tarjan_graph *g = NULL;
654 g = isl_tarjan_graph_init(ctx, graph->n,
655 weak ? &node_follows_weak : &node_follows_strong, graph);
656 if (!g)
657 return -1;
659 graph->scc = 0;
660 i = 0;
661 n = graph->n;
662 while (n) {
663 while (g->order[i] != -1) {
664 graph->node[g->order[i]].scc = graph->scc;
665 --n;
666 ++i;
668 ++i;
669 graph->scc++;
672 isl_tarjan_graph_free(g);
674 return 0;
677 /* Apply Tarjan's algorithm to detect the strongly connected components
678 * in the dependence graph.
680 static int detect_sccs(isl_ctx *ctx, struct isl_sched_graph *graph)
682 return detect_ccs(ctx, graph, 0);
685 /* Apply Tarjan's algorithm to detect the (weakly) connected components
686 * in the dependence graph.
688 static int detect_wccs(isl_ctx *ctx, struct isl_sched_graph *graph)
690 return detect_ccs(ctx, graph, 1);
693 static int cmp_scc(const void *a, const void *b, void *data)
695 struct isl_sched_graph *graph = data;
696 const int *i1 = a;
697 const int *i2 = b;
699 return graph->node[*i1].scc - graph->node[*i2].scc;
702 /* Sort the elements of graph->sorted according to the corresponding SCCs.
704 static int sort_sccs(struct isl_sched_graph *graph)
706 return isl_sort(graph->sorted, graph->n, sizeof(int), &cmp_scc, graph);
709 /* Given a dependence relation R from a node to itself,
710 * construct the set of coefficients of valid constraints for elements
711 * in that dependence relation.
712 * In particular, the result contains tuples of coefficients
713 * c_0, c_n, c_x such that
715 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
717 * or, equivalently,
719 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
721 * We choose here to compute the dual of delta R.
722 * Alternatively, we could have computed the dual of R, resulting
723 * in a set of tuples c_0, c_n, c_x, c_y, and then
724 * plugged in (c_0, c_n, c_x, -c_x).
726 static __isl_give isl_basic_set *intra_coefficients(
727 struct isl_sched_graph *graph, __isl_take isl_map *map)
729 isl_ctx *ctx = isl_map_get_ctx(map);
730 isl_set *delta;
731 isl_basic_set *coef;
733 if (isl_hmap_map_basic_set_has(ctx, graph->intra_hmap, map))
734 return isl_hmap_map_basic_set_get(ctx, graph->intra_hmap, map);
736 delta = isl_set_remove_divs(isl_map_deltas(isl_map_copy(map)));
737 coef = isl_set_coefficients(delta);
738 isl_hmap_map_basic_set_set(ctx, graph->intra_hmap, map,
739 isl_basic_set_copy(coef));
741 return coef;
744 /* Given a dependence relation R, * construct the set of coefficients
745 * of valid constraints for elements in that dependence relation.
746 * In particular, the result contains tuples of coefficients
747 * c_0, c_n, c_x, c_y such that
749 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
752 static __isl_give isl_basic_set *inter_coefficients(
753 struct isl_sched_graph *graph, __isl_take isl_map *map)
755 isl_ctx *ctx = isl_map_get_ctx(map);
756 isl_set *set;
757 isl_basic_set *coef;
759 if (isl_hmap_map_basic_set_has(ctx, graph->inter_hmap, map))
760 return isl_hmap_map_basic_set_get(ctx, graph->inter_hmap, map);
762 set = isl_map_wrap(isl_map_remove_divs(isl_map_copy(map)));
763 coef = isl_set_coefficients(set);
764 isl_hmap_map_basic_set_set(ctx, graph->inter_hmap, map,
765 isl_basic_set_copy(coef));
767 return coef;
770 /* Add constraints to graph->lp that force validity for the given
771 * dependence from a node i to itself.
772 * That is, add constraints that enforce
774 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
775 * = c_i_x (y - x) >= 0
777 * for each (x,y) in R.
778 * We obtain general constraints on coefficients (c_0, c_n, c_x)
779 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
780 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
781 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
783 * Actually, we do not construct constraints for the c_i_x themselves,
784 * but for the coefficients of c_i_x written as a linear combination
785 * of the columns in node->cmap.
787 static int add_intra_validity_constraints(struct isl_sched_graph *graph,
788 struct isl_sched_edge *edge)
790 unsigned total;
791 isl_map *map = isl_map_copy(edge->map);
792 isl_ctx *ctx = isl_map_get_ctx(map);
793 isl_space *dim;
794 isl_dim_map *dim_map;
795 isl_basic_set *coef;
796 struct isl_sched_node *node = edge->src;
798 coef = intra_coefficients(graph, map);
800 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
802 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
803 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
804 if (!coef)
805 goto error;
807 total = isl_basic_set_total_dim(graph->lp);
808 dim_map = isl_dim_map_alloc(ctx, total);
809 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
810 isl_space_dim(dim, isl_dim_set), 1,
811 node->nvar, -1);
812 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
813 isl_space_dim(dim, isl_dim_set), 1,
814 node->nvar, 1);
815 graph->lp = isl_basic_set_extend_constraints(graph->lp,
816 coef->n_eq, coef->n_ineq);
817 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
818 coef, dim_map);
819 isl_space_free(dim);
821 return 0;
822 error:
823 isl_space_free(dim);
824 return -1;
827 /* Add constraints to graph->lp that force validity for the given
828 * dependence from node i to node j.
829 * That is, add constraints that enforce
831 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
833 * for each (x,y) in R.
834 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
835 * of valid constraints for R and then plug in
836 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
837 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
838 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
839 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
841 * Actually, we do not construct constraints for the c_*_x themselves,
842 * but for the coefficients of c_*_x written as a linear combination
843 * of the columns in node->cmap.
845 static int add_inter_validity_constraints(struct isl_sched_graph *graph,
846 struct isl_sched_edge *edge)
848 unsigned total;
849 isl_map *map = isl_map_copy(edge->map);
850 isl_ctx *ctx = isl_map_get_ctx(map);
851 isl_space *dim;
852 isl_dim_map *dim_map;
853 isl_basic_set *coef;
854 struct isl_sched_node *src = edge->src;
855 struct isl_sched_node *dst = edge->dst;
857 coef = inter_coefficients(graph, map);
859 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
861 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
862 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
863 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
864 isl_space_dim(dim, isl_dim_set) + src->nvar,
865 isl_mat_copy(dst->cmap));
866 if (!coef)
867 goto error;
869 total = isl_basic_set_total_dim(graph->lp);
870 dim_map = isl_dim_map_alloc(ctx, total);
872 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
873 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
874 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
875 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
876 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
877 dst->nvar, -1);
878 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
879 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
880 dst->nvar, 1);
882 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
883 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
884 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
885 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
886 isl_space_dim(dim, isl_dim_set), 1,
887 src->nvar, 1);
888 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
889 isl_space_dim(dim, isl_dim_set), 1,
890 src->nvar, -1);
892 edge->start = graph->lp->n_ineq;
893 graph->lp = isl_basic_set_extend_constraints(graph->lp,
894 coef->n_eq, coef->n_ineq);
895 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
896 coef, dim_map);
897 if (!graph->lp)
898 goto error;
899 isl_space_free(dim);
900 edge->end = graph->lp->n_ineq;
902 return 0;
903 error:
904 isl_space_free(dim);
905 return -1;
908 /* Add constraints to graph->lp that bound the dependence distance for the given
909 * dependence from a node i to itself.
910 * If s = 1, we add the constraint
912 * c_i_x (y - x) <= m_0 + m_n n
914 * or
916 * -c_i_x (y - x) + m_0 + m_n n >= 0
918 * for each (x,y) in R.
919 * If s = -1, we add the constraint
921 * -c_i_x (y - x) <= m_0 + m_n n
923 * or
925 * c_i_x (y - x) + m_0 + m_n n >= 0
927 * for each (x,y) in R.
928 * We obtain general constraints on coefficients (c_0, c_n, c_x)
929 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
930 * with each coefficient (except m_0) represented as a pair of non-negative
931 * coefficients.
933 * Actually, we do not construct constraints for the c_i_x themselves,
934 * but for the coefficients of c_i_x written as a linear combination
935 * of the columns in node->cmap.
937 static int add_intra_proximity_constraints(struct isl_sched_graph *graph,
938 struct isl_sched_edge *edge, int s)
940 unsigned total;
941 unsigned nparam;
942 isl_map *map = isl_map_copy(edge->map);
943 isl_ctx *ctx = isl_map_get_ctx(map);
944 isl_space *dim;
945 isl_dim_map *dim_map;
946 isl_basic_set *coef;
947 struct isl_sched_node *node = edge->src;
949 coef = intra_coefficients(graph, map);
951 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
953 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
954 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
955 if (!coef)
956 goto error;
958 nparam = isl_space_dim(node->dim, isl_dim_param);
959 total = isl_basic_set_total_dim(graph->lp);
960 dim_map = isl_dim_map_alloc(ctx, total);
961 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
962 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
963 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
964 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
965 isl_space_dim(dim, isl_dim_set), 1,
966 node->nvar, s);
967 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
968 isl_space_dim(dim, isl_dim_set), 1,
969 node->nvar, -s);
970 graph->lp = isl_basic_set_extend_constraints(graph->lp,
971 coef->n_eq, coef->n_ineq);
972 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
973 coef, dim_map);
974 isl_space_free(dim);
976 return 0;
977 error:
978 isl_space_free(dim);
979 return -1;
982 /* Add constraints to graph->lp that bound the dependence distance for the given
983 * dependence from node i to node j.
984 * If s = 1, we add the constraint
986 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
987 * <= m_0 + m_n n
989 * or
991 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
992 * m_0 + m_n n >= 0
994 * for each (x,y) in R.
995 * If s = -1, we add the constraint
997 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
998 * <= m_0 + m_n n
1000 * or
1002 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1003 * m_0 + m_n n >= 0
1005 * for each (x,y) in R.
1006 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1007 * of valid constraints for R and then plug in
1008 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1009 * -s*c_j_x+s*c_i_x)
1010 * with each coefficient (except m_0, c_j_0 and c_i_0)
1011 * represented as a pair of non-negative coefficients.
1013 * Actually, we do not construct constraints for the c_*_x themselves,
1014 * but for the coefficients of c_*_x written as a linear combination
1015 * of the columns in node->cmap.
1017 static int add_inter_proximity_constraints(struct isl_sched_graph *graph,
1018 struct isl_sched_edge *edge, int s)
1020 unsigned total;
1021 unsigned nparam;
1022 isl_map *map = isl_map_copy(edge->map);
1023 isl_ctx *ctx = isl_map_get_ctx(map);
1024 isl_space *dim;
1025 isl_dim_map *dim_map;
1026 isl_basic_set *coef;
1027 struct isl_sched_node *src = edge->src;
1028 struct isl_sched_node *dst = edge->dst;
1030 coef = inter_coefficients(graph, map);
1032 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1034 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1035 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1036 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1037 isl_space_dim(dim, isl_dim_set) + src->nvar,
1038 isl_mat_copy(dst->cmap));
1039 if (!coef)
1040 goto error;
1042 nparam = isl_space_dim(src->dim, isl_dim_param);
1043 total = isl_basic_set_total_dim(graph->lp);
1044 dim_map = isl_dim_map_alloc(ctx, total);
1046 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1047 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1048 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1050 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, -s);
1051 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, s);
1052 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, -s);
1053 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1054 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1055 dst->nvar, s);
1056 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1057 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1058 dst->nvar, -s);
1060 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, s);
1061 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, -s);
1062 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, s);
1063 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1064 isl_space_dim(dim, isl_dim_set), 1,
1065 src->nvar, -s);
1066 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1067 isl_space_dim(dim, isl_dim_set), 1,
1068 src->nvar, s);
1070 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1071 coef->n_eq, coef->n_ineq);
1072 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1073 coef, dim_map);
1074 isl_space_free(dim);
1076 return 0;
1077 error:
1078 isl_space_free(dim);
1079 return -1;
1082 static int add_all_validity_constraints(struct isl_sched_graph *graph)
1084 int i;
1086 for (i = 0; i < graph->n_edge; ++i) {
1087 struct isl_sched_edge *edge= &graph->edge[i];
1088 if (!edge->validity)
1089 continue;
1090 if (edge->src != edge->dst)
1091 continue;
1092 if (add_intra_validity_constraints(graph, edge) < 0)
1093 return -1;
1096 for (i = 0; i < graph->n_edge; ++i) {
1097 struct isl_sched_edge *edge = &graph->edge[i];
1098 if (!edge->validity)
1099 continue;
1100 if (edge->src == edge->dst)
1101 continue;
1102 if (add_inter_validity_constraints(graph, edge) < 0)
1103 return -1;
1106 return 0;
1109 /* Add constraints to graph->lp that bound the dependence distance
1110 * for all dependence relations.
1111 * If a given proximity dependence is identical to a validity
1112 * dependence, then the dependence distance is already bounded
1113 * from below (by zero), so we only need to bound the distance
1114 * from above.
1115 * Otherwise, we need to bound the distance both from above and from below.
1117 static int add_all_proximity_constraints(struct isl_sched_graph *graph)
1119 int i;
1121 for (i = 0; i < graph->n_edge; ++i) {
1122 struct isl_sched_edge *edge= &graph->edge[i];
1123 if (!edge->proximity)
1124 continue;
1125 if (edge->src == edge->dst &&
1126 add_intra_proximity_constraints(graph, edge, 1) < 0)
1127 return -1;
1128 if (edge->src != edge->dst &&
1129 add_inter_proximity_constraints(graph, edge, 1) < 0)
1130 return -1;
1131 if (edge->validity)
1132 continue;
1133 if (edge->src == edge->dst &&
1134 add_intra_proximity_constraints(graph, edge, -1) < 0)
1135 return -1;
1136 if (edge->src != edge->dst &&
1137 add_inter_proximity_constraints(graph, edge, -1) < 0)
1138 return -1;
1141 return 0;
1144 /* Compute a basis for the rows in the linear part of the schedule
1145 * and extend this basis to a full basis. The remaining rows
1146 * can then be used to force linear independence from the rows
1147 * in the schedule.
1149 * In particular, given the schedule rows S, we compute
1151 * S = H Q
1152 * S U = H
1154 * with H the Hermite normal form of S. That is, all but the
1155 * first rank columns of Q are zero and so each row in S is
1156 * a linear combination of the first rank rows of Q.
1157 * The matrix Q is then transposed because we will write the
1158 * coefficients of the next schedule row as a column vector s
1159 * and express this s as a linear combination s = Q c of the
1160 * computed basis.
1161 * Similarly, the matrix U is transposed such that we can
1162 * compute the coefficients c = U s from a schedule row s.
1164 static int node_update_cmap(struct isl_sched_node *node)
1166 isl_mat *H, *U, *Q;
1167 int n_row = isl_mat_rows(node->sched);
1169 H = isl_mat_sub_alloc(node->sched, 0, n_row,
1170 1 + node->nparam, node->nvar);
1172 H = isl_mat_left_hermite(H, 0, &U, &Q);
1173 isl_mat_free(node->cmap);
1174 isl_mat_free(node->cinv);
1175 node->cmap = isl_mat_transpose(Q);
1176 node->cinv = isl_mat_transpose(U);
1177 node->rank = isl_mat_initial_non_zero_cols(H);
1178 isl_mat_free(H);
1180 if (!node->cmap || !node->cinv || node->rank < 0)
1181 return -1;
1182 return 0;
1185 /* Count the number of equality and inequality constraints
1186 * that will be added for the given map.
1187 * If carry is set, then we are counting the number of (validity)
1188 * constraints that will be added in setup_carry_lp and we count
1189 * each edge exactly once. Otherwise, we count as follows
1190 * validity -> 1 (>= 0)
1191 * validity+proximity -> 2 (>= 0 and upper bound)
1192 * proximity -> 2 (lower and upper bound)
1194 static int count_map_constraints(struct isl_sched_graph *graph,
1195 struct isl_sched_edge *edge, __isl_take isl_map *map,
1196 int *n_eq, int *n_ineq, int carry)
1198 isl_basic_set *coef;
1199 int f = carry ? 1 : edge->proximity ? 2 : 1;
1201 if (carry && !edge->validity) {
1202 isl_map_free(map);
1203 return 0;
1206 if (edge->src == edge->dst)
1207 coef = intra_coefficients(graph, map);
1208 else
1209 coef = inter_coefficients(graph, map);
1210 if (!coef)
1211 return -1;
1212 *n_eq += f * coef->n_eq;
1213 *n_ineq += f * coef->n_ineq;
1214 isl_basic_set_free(coef);
1216 return 0;
1219 /* Count the number of equality and inequality constraints
1220 * that will be added to the main lp problem.
1221 * We count as follows
1222 * validity -> 1 (>= 0)
1223 * validity+proximity -> 2 (>= 0 and upper bound)
1224 * proximity -> 2 (lower and upper bound)
1226 static int count_constraints(struct isl_sched_graph *graph,
1227 int *n_eq, int *n_ineq)
1229 int i;
1231 *n_eq = *n_ineq = 0;
1232 for (i = 0; i < graph->n_edge; ++i) {
1233 struct isl_sched_edge *edge= &graph->edge[i];
1234 isl_map *map = isl_map_copy(edge->map);
1236 if (count_map_constraints(graph, edge, map,
1237 n_eq, n_ineq, 0) < 0)
1238 return -1;
1241 return 0;
1244 /* Add constraints that bound the values of the variable and parameter
1245 * coefficients of the schedule.
1247 * The maximal value of the coefficients is defined by the option
1248 * 'schedule_max_coefficient'.
1250 static int add_bound_coefficient_constraints(isl_ctx *ctx,
1251 struct isl_sched_graph *graph)
1253 int i, j, k;
1254 int max_coefficient;
1255 int total;
1257 max_coefficient = ctx->opt->schedule_max_coefficient;
1259 if (max_coefficient == -1)
1260 return 0;
1262 total = isl_basic_set_total_dim(graph->lp);
1264 for (i = 0; i < graph->n; ++i) {
1265 struct isl_sched_node *node = &graph->node[i];
1266 for (j = 0; j < 2 * node->nparam + 2 * node->nvar; ++j) {
1267 int dim;
1268 k = isl_basic_set_alloc_inequality(graph->lp);
1269 if (k < 0)
1270 return -1;
1271 dim = 1 + node->start + 1 + j;
1272 isl_seq_clr(graph->lp->ineq[k], 1 + total);
1273 isl_int_set_si(graph->lp->ineq[k][dim], -1);
1274 isl_int_set_si(graph->lp->ineq[k][0], max_coefficient);
1278 return 0;
1281 /* Construct an ILP problem for finding schedule coefficients
1282 * that result in non-negative, but small dependence distances
1283 * over all dependences.
1284 * In particular, the dependence distances over proximity edges
1285 * are bounded by m_0 + m_n n and we compute schedule coefficients
1286 * with small values (preferably zero) of m_n and m_0.
1288 * All variables of the ILP are non-negative. The actual coefficients
1289 * may be negative, so each coefficient is represented as the difference
1290 * of two non-negative variables. The negative part always appears
1291 * immediately before the positive part.
1292 * Other than that, the variables have the following order
1294 * - sum of positive and negative parts of m_n coefficients
1295 * - m_0
1296 * - sum of positive and negative parts of all c_n coefficients
1297 * (unconstrained when computing non-parametric schedules)
1298 * - sum of positive and negative parts of all c_x coefficients
1299 * - positive and negative parts of m_n coefficients
1300 * - for each node
1301 * - c_i_0
1302 * - positive and negative parts of c_i_n (if parametric)
1303 * - positive and negative parts of c_i_x
1305 * The c_i_x are not represented directly, but through the columns of
1306 * node->cmap. That is, the computed values are for variable t_i_x
1307 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
1309 * The constraints are those from the edges plus two or three equalities
1310 * to express the sums.
1312 * If force_zero is set, then we add equalities to ensure that
1313 * the sum of the m_n coefficients and m_0 are both zero.
1315 static int setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
1316 int force_zero)
1318 int i, j;
1319 int k;
1320 unsigned nparam;
1321 unsigned total;
1322 isl_space *dim;
1323 int parametric;
1324 int param_pos;
1325 int n_eq, n_ineq;
1326 int max_constant_term;
1327 int max_coefficient;
1329 max_constant_term = ctx->opt->schedule_max_constant_term;
1330 max_coefficient = ctx->opt->schedule_max_coefficient;
1332 parametric = ctx->opt->schedule_parametric;
1333 nparam = isl_space_dim(graph->node[0].dim, isl_dim_param);
1334 param_pos = 4;
1335 total = param_pos + 2 * nparam;
1336 for (i = 0; i < graph->n; ++i) {
1337 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
1338 if (node_update_cmap(node) < 0)
1339 return -1;
1340 node->start = total;
1341 total += 1 + 2 * (node->nparam + node->nvar);
1344 if (count_constraints(graph, &n_eq, &n_ineq) < 0)
1345 return -1;
1347 dim = isl_space_set_alloc(ctx, 0, total);
1348 isl_basic_set_free(graph->lp);
1349 n_eq += 2 + parametric + force_zero;
1350 if (max_constant_term != -1)
1351 n_ineq += graph->n;
1352 if (max_coefficient != -1)
1353 for (i = 0; i < graph->n; ++i)
1354 n_ineq += 2 * graph->node[i].nparam +
1355 2 * graph->node[i].nvar;
1357 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
1359 k = isl_basic_set_alloc_equality(graph->lp);
1360 if (k < 0)
1361 return -1;
1362 isl_seq_clr(graph->lp->eq[k], 1 + total);
1363 if (!force_zero)
1364 isl_int_set_si(graph->lp->eq[k][1], -1);
1365 for (i = 0; i < 2 * nparam; ++i)
1366 isl_int_set_si(graph->lp->eq[k][1 + param_pos + i], 1);
1368 if (force_zero) {
1369 k = isl_basic_set_alloc_equality(graph->lp);
1370 if (k < 0)
1371 return -1;
1372 isl_seq_clr(graph->lp->eq[k], 1 + total);
1373 isl_int_set_si(graph->lp->eq[k][2], -1);
1376 if (parametric) {
1377 k = isl_basic_set_alloc_equality(graph->lp);
1378 if (k < 0)
1379 return -1;
1380 isl_seq_clr(graph->lp->eq[k], 1 + total);
1381 isl_int_set_si(graph->lp->eq[k][3], -1);
1382 for (i = 0; i < graph->n; ++i) {
1383 int pos = 1 + graph->node[i].start + 1;
1385 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
1386 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
1390 k = isl_basic_set_alloc_equality(graph->lp);
1391 if (k < 0)
1392 return -1;
1393 isl_seq_clr(graph->lp->eq[k], 1 + total);
1394 isl_int_set_si(graph->lp->eq[k][4], -1);
1395 for (i = 0; i < graph->n; ++i) {
1396 struct isl_sched_node *node = &graph->node[i];
1397 int pos = 1 + node->start + 1 + 2 * node->nparam;
1399 for (j = 0; j < 2 * node->nvar; ++j)
1400 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
1403 if (max_constant_term != -1)
1404 for (i = 0; i < graph->n; ++i) {
1405 struct isl_sched_node *node = &graph->node[i];
1406 k = isl_basic_set_alloc_inequality(graph->lp);
1407 if (k < 0)
1408 return -1;
1409 isl_seq_clr(graph->lp->ineq[k], 1 + total);
1410 isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
1411 isl_int_set_si(graph->lp->ineq[k][0], max_constant_term);
1414 if (add_bound_coefficient_constraints(ctx, graph) < 0)
1415 return -1;
1416 if (add_all_validity_constraints(graph) < 0)
1417 return -1;
1418 if (add_all_proximity_constraints(graph) < 0)
1419 return -1;
1421 return 0;
1424 /* Analyze the conflicting constraint found by
1425 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
1426 * constraint of one of the edges between distinct nodes, living, moreover
1427 * in distinct SCCs, then record the source and sink SCC as this may
1428 * be a good place to cut between SCCs.
1430 static int check_conflict(int con, void *user)
1432 int i;
1433 struct isl_sched_graph *graph = user;
1435 if (graph->src_scc >= 0)
1436 return 0;
1438 con -= graph->lp->n_eq;
1440 if (con >= graph->lp->n_ineq)
1441 return 0;
1443 for (i = 0; i < graph->n_edge; ++i) {
1444 if (!graph->edge[i].validity)
1445 continue;
1446 if (graph->edge[i].src == graph->edge[i].dst)
1447 continue;
1448 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
1449 continue;
1450 if (graph->edge[i].start > con)
1451 continue;
1452 if (graph->edge[i].end <= con)
1453 continue;
1454 graph->src_scc = graph->edge[i].src->scc;
1455 graph->dst_scc = graph->edge[i].dst->scc;
1458 return 0;
1461 /* Check whether the next schedule row of the given node needs to be
1462 * non-trivial. Lower-dimensional domains may have some trivial rows,
1463 * but as soon as the number of remaining required non-trivial rows
1464 * is as large as the number or remaining rows to be computed,
1465 * all remaining rows need to be non-trivial.
1467 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
1469 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
1472 /* Solve the ILP problem constructed in setup_lp.
1473 * For each node such that all the remaining rows of its schedule
1474 * need to be non-trivial, we construct a non-triviality region.
1475 * This region imposes that the next row is independent of previous rows.
1476 * In particular the coefficients c_i_x are represented by t_i_x
1477 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
1478 * its first columns span the rows of the previously computed part
1479 * of the schedule. The non-triviality region enforces that at least
1480 * one of the remaining components of t_i_x is non-zero, i.e.,
1481 * that the new schedule row depends on at least one of the remaining
1482 * columns of Q.
1484 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
1486 int i;
1487 isl_vec *sol;
1488 isl_basic_set *lp;
1490 for (i = 0; i < graph->n; ++i) {
1491 struct isl_sched_node *node = &graph->node[i];
1492 int skip = node->rank;
1493 graph->region[i].pos = node->start + 1 + 2*(node->nparam+skip);
1494 if (needs_row(graph, node))
1495 graph->region[i].len = 2 * (node->nvar - skip);
1496 else
1497 graph->region[i].len = 0;
1499 lp = isl_basic_set_copy(graph->lp);
1500 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
1501 graph->region, &check_conflict, graph);
1502 return sol;
1505 /* Update the schedules of all nodes based on the given solution
1506 * of the LP problem.
1507 * The new row is added to the current band.
1508 * All possibly negative coefficients are encoded as a difference
1509 * of two non-negative variables, so we need to perform the subtraction
1510 * here. Moreover, if use_cmap is set, then the solution does
1511 * not refer to the actual coefficients c_i_x, but instead to variables
1512 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
1513 * In this case, we then also need to perform this multiplication
1514 * to obtain the values of c_i_x.
1516 * If check_zero is set, then the first two coordinates of sol are
1517 * assumed to correspond to the dependence distance. If these two
1518 * coordinates are zero, then the corresponding scheduling dimension
1519 * is marked as being zero distance.
1521 static int update_schedule(struct isl_sched_graph *graph,
1522 __isl_take isl_vec *sol, int use_cmap, int check_zero)
1524 int i, j;
1525 int zero = 0;
1526 isl_vec *csol = NULL;
1528 if (!sol)
1529 goto error;
1530 if (sol->size == 0)
1531 isl_die(sol->ctx, isl_error_internal,
1532 "no solution found", goto error);
1533 if (graph->n_total_row >= graph->max_row)
1534 isl_die(sol->ctx, isl_error_internal,
1535 "too many schedule rows", goto error);
1537 if (check_zero)
1538 zero = isl_int_is_zero(sol->el[1]) &&
1539 isl_int_is_zero(sol->el[2]);
1541 for (i = 0; i < graph->n; ++i) {
1542 struct isl_sched_node *node = &graph->node[i];
1543 int pos = node->start;
1544 int row = isl_mat_rows(node->sched);
1546 isl_vec_free(csol);
1547 csol = isl_vec_alloc(sol->ctx, node->nvar);
1548 if (!csol)
1549 goto error;
1551 isl_map_free(node->sched_map);
1552 node->sched_map = NULL;
1553 node->sched = isl_mat_add_rows(node->sched, 1);
1554 if (!node->sched)
1555 goto error;
1556 node->sched = isl_mat_set_element(node->sched, row, 0,
1557 sol->el[1 + pos]);
1558 for (j = 0; j < node->nparam + node->nvar; ++j)
1559 isl_int_sub(sol->el[1 + pos + 1 + 2 * j + 1],
1560 sol->el[1 + pos + 1 + 2 * j + 1],
1561 sol->el[1 + pos + 1 + 2 * j]);
1562 for (j = 0; j < node->nparam; ++j)
1563 node->sched = isl_mat_set_element(node->sched,
1564 row, 1 + j, sol->el[1+pos+1+2*j+1]);
1565 for (j = 0; j < node->nvar; ++j)
1566 isl_int_set(csol->el[j],
1567 sol->el[1+pos+1+2*(node->nparam+j)+1]);
1568 if (use_cmap)
1569 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
1570 csol);
1571 if (!csol)
1572 goto error;
1573 for (j = 0; j < node->nvar; ++j)
1574 node->sched = isl_mat_set_element(node->sched,
1575 row, 1 + node->nparam + j, csol->el[j]);
1576 node->band[graph->n_total_row] = graph->n_band;
1577 node->zero[graph->n_total_row] = zero;
1579 isl_vec_free(sol);
1580 isl_vec_free(csol);
1582 graph->n_row++;
1583 graph->n_total_row++;
1585 return 0;
1586 error:
1587 isl_vec_free(sol);
1588 isl_vec_free(csol);
1589 return -1;
1592 /* Convert node->sched into a multi_aff and return this multi_aff.
1594 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
1595 struct isl_sched_node *node)
1597 int i, j;
1598 isl_space *space;
1599 isl_local_space *ls;
1600 isl_aff *aff;
1601 isl_multi_aff *ma;
1602 int nrow, ncol;
1603 isl_int v;
1605 nrow = isl_mat_rows(node->sched);
1606 ncol = isl_mat_cols(node->sched) - 1;
1607 space = isl_space_from_domain(isl_space_copy(node->dim));
1608 space = isl_space_add_dims(space, isl_dim_out, nrow);
1609 ma = isl_multi_aff_zero(space);
1610 ls = isl_local_space_from_space(isl_space_copy(node->dim));
1612 isl_int_init(v);
1614 for (i = 0; i < nrow; ++i) {
1615 aff = isl_aff_zero_on_domain(isl_local_space_copy(ls));
1616 isl_mat_get_element(node->sched, i, 0, &v);
1617 aff = isl_aff_set_constant(aff, v);
1618 for (j = 0; j < node->nparam; ++j) {
1619 isl_mat_get_element(node->sched, i, 1 + j, &v);
1620 aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
1622 for (j = 0; j < node->nvar; ++j) {
1623 isl_mat_get_element(node->sched,
1624 i, 1 + node->nparam + j, &v);
1625 aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
1627 ma = isl_multi_aff_set_aff(ma, i, aff);
1630 isl_int_clear(v);
1632 isl_local_space_free(ls);
1634 return ma;
1637 /* Convert node->sched into a map and return this map.
1639 * The result is cached in node->sched_map, which needs to be released
1640 * whenever node->sched is updated.
1642 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
1644 if (!node->sched_map) {
1645 isl_multi_aff *ma;
1647 ma = node_extract_schedule_multi_aff(node);
1648 node->sched_map = isl_map_from_multi_aff(ma);
1651 return isl_map_copy(node->sched_map);
1654 /* Update the given dependence relation based on the current schedule.
1655 * That is, intersect the dependence relation with a map expressing
1656 * that source and sink are executed within the same iteration of
1657 * the current schedule.
1658 * This is not the most efficient way, but this shouldn't be a critical
1659 * operation.
1661 static __isl_give isl_map *specialize(__isl_take isl_map *map,
1662 struct isl_sched_node *src, struct isl_sched_node *dst)
1664 isl_map *src_sched, *dst_sched, *id;
1666 src_sched = node_extract_schedule(src);
1667 dst_sched = node_extract_schedule(dst);
1668 id = isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
1669 return isl_map_intersect(map, id);
1672 /* Update the dependence relations of all edges based on the current schedule.
1673 * If a dependence is carried completely by the current schedule, then
1674 * it is removed from the edge_tables. It is kept in the list of edges
1675 * as otherwise all edge_tables would have to be recomputed.
1677 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
1679 int i;
1681 for (i = graph->n_edge - 1; i >= 0; --i) {
1682 struct isl_sched_edge *edge = &graph->edge[i];
1683 edge->map = specialize(edge->map, edge->src, edge->dst);
1684 if (!edge->map)
1685 return -1;
1687 if (isl_map_plain_is_empty(edge->map))
1688 graph_remove_edge(graph, edge);
1691 return 0;
1694 static void next_band(struct isl_sched_graph *graph)
1696 graph->band_start = graph->n_total_row;
1697 graph->n_band++;
1700 /* Topologically sort statements mapped to the same schedule iteration
1701 * and add a row to the schedule corresponding to this order.
1703 static int sort_statements(isl_ctx *ctx, struct isl_sched_graph *graph)
1705 int i, j;
1707 if (graph->n <= 1)
1708 return 0;
1710 if (update_edges(ctx, graph) < 0)
1711 return -1;
1713 if (graph->n_edge == 0)
1714 return 0;
1716 if (detect_sccs(ctx, graph) < 0)
1717 return -1;
1719 if (graph->n_total_row >= graph->max_row)
1720 isl_die(ctx, isl_error_internal,
1721 "too many schedule rows", return -1);
1723 for (i = 0; i < graph->n; ++i) {
1724 struct isl_sched_node *node = &graph->node[i];
1725 int row = isl_mat_rows(node->sched);
1726 int cols = isl_mat_cols(node->sched);
1728 isl_map_free(node->sched_map);
1729 node->sched_map = NULL;
1730 node->sched = isl_mat_add_rows(node->sched, 1);
1731 if (!node->sched)
1732 return -1;
1733 node->sched = isl_mat_set_element_si(node->sched, row, 0,
1734 node->scc);
1735 for (j = 1; j < cols; ++j)
1736 node->sched = isl_mat_set_element_si(node->sched,
1737 row, j, 0);
1738 node->band[graph->n_total_row] = graph->n_band;
1741 graph->n_total_row++;
1742 next_band(graph);
1744 return 0;
1747 /* Construct an isl_schedule based on the computed schedule stored
1748 * in graph and with parameters specified by dim.
1750 static __isl_give isl_schedule *extract_schedule(struct isl_sched_graph *graph,
1751 __isl_take isl_space *dim)
1753 int i;
1754 isl_ctx *ctx;
1755 isl_schedule *sched = NULL;
1757 if (!dim)
1758 return NULL;
1760 ctx = isl_space_get_ctx(dim);
1761 sched = isl_calloc(ctx, struct isl_schedule,
1762 sizeof(struct isl_schedule) +
1763 (graph->n - 1) * sizeof(struct isl_schedule_node));
1764 if (!sched)
1765 goto error;
1767 sched->ref = 1;
1768 sched->n = graph->n;
1769 sched->n_band = graph->n_band;
1770 sched->n_total_row = graph->n_total_row;
1772 for (i = 0; i < sched->n; ++i) {
1773 int r, b;
1774 int *band_end, *band_id, *zero;
1776 sched->node[i].sched =
1777 node_extract_schedule_multi_aff(&graph->node[i]);
1778 if (!sched->node[i].sched)
1779 goto error;
1781 sched->node[i].n_band = graph->n_band;
1782 if (graph->n_band == 0)
1783 continue;
1785 band_end = isl_alloc_array(ctx, int, graph->n_band);
1786 band_id = isl_alloc_array(ctx, int, graph->n_band);
1787 zero = isl_alloc_array(ctx, int, graph->n_total_row);
1788 sched->node[i].band_end = band_end;
1789 sched->node[i].band_id = band_id;
1790 sched->node[i].zero = zero;
1791 if (!band_end || !band_id || !zero)
1792 goto error;
1794 for (r = 0; r < graph->n_total_row; ++r)
1795 zero[r] = graph->node[i].zero[r];
1796 for (r = b = 0; r < graph->n_total_row; ++r) {
1797 if (graph->node[i].band[r] == b)
1798 continue;
1799 band_end[b++] = r;
1800 if (graph->node[i].band[r] == -1)
1801 break;
1803 if (r == graph->n_total_row)
1804 band_end[b++] = r;
1805 sched->node[i].n_band = b;
1806 for (--b; b >= 0; --b)
1807 band_id[b] = graph->node[i].band_id[b];
1810 sched->dim = dim;
1812 return sched;
1813 error:
1814 isl_space_free(dim);
1815 isl_schedule_free(sched);
1816 return NULL;
1819 /* Copy nodes that satisfy node_pred from the src dependence graph
1820 * to the dst dependence graph.
1822 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
1823 int (*node_pred)(struct isl_sched_node *node, int data), int data)
1825 int i;
1827 dst->n = 0;
1828 for (i = 0; i < src->n; ++i) {
1829 if (!node_pred(&src->node[i], data))
1830 continue;
1831 dst->node[dst->n].dim = isl_space_copy(src->node[i].dim);
1832 dst->node[dst->n].nvar = src->node[i].nvar;
1833 dst->node[dst->n].nparam = src->node[i].nparam;
1834 dst->node[dst->n].sched = isl_mat_copy(src->node[i].sched);
1835 dst->node[dst->n].sched_map =
1836 isl_map_copy(src->node[i].sched_map);
1837 dst->node[dst->n].band = src->node[i].band;
1838 dst->node[dst->n].band_id = src->node[i].band_id;
1839 dst->node[dst->n].zero = src->node[i].zero;
1840 dst->n++;
1843 return 0;
1846 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
1847 * to the dst dependence graph.
1848 * If the source or destination node of the edge is not in the destination
1849 * graph, then it must be a backward proximity edge and it should simply
1850 * be ignored.
1852 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
1853 struct isl_sched_graph *src,
1854 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
1856 int i;
1857 enum isl_edge_type t;
1859 dst->n_edge = 0;
1860 for (i = 0; i < src->n_edge; ++i) {
1861 struct isl_sched_edge *edge = &src->edge[i];
1862 isl_map *map;
1863 struct isl_sched_node *dst_src, *dst_dst;
1865 if (!edge_pred(edge, data))
1866 continue;
1868 if (isl_map_plain_is_empty(edge->map))
1869 continue;
1871 dst_src = graph_find_node(ctx, dst, edge->src->dim);
1872 dst_dst = graph_find_node(ctx, dst, edge->dst->dim);
1873 if (!dst_src || !dst_dst) {
1874 if (edge->validity)
1875 isl_die(ctx, isl_error_internal,
1876 "backward validity edge", return -1);
1877 continue;
1880 map = isl_map_copy(edge->map);
1882 dst->edge[dst->n_edge].src = dst_src;
1883 dst->edge[dst->n_edge].dst = dst_dst;
1884 dst->edge[dst->n_edge].map = map;
1885 dst->edge[dst->n_edge].validity = edge->validity;
1886 dst->edge[dst->n_edge].proximity = edge->proximity;
1887 dst->n_edge++;
1889 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
1890 if (edge !=
1891 graph_find_edge(src, t, edge->src, edge->dst))
1892 continue;
1893 if (graph_edge_table_add(ctx, dst, t,
1894 &dst->edge[dst->n_edge - 1]) < 0)
1895 return -1;
1899 return 0;
1902 /* Given a "src" dependence graph that contains the nodes from "dst"
1903 * that satisfy node_pred, copy the schedule computed in "src"
1904 * for those nodes back to "dst".
1906 static int copy_schedule(struct isl_sched_graph *dst,
1907 struct isl_sched_graph *src,
1908 int (*node_pred)(struct isl_sched_node *node, int data), int data)
1910 int i;
1912 src->n = 0;
1913 for (i = 0; i < dst->n; ++i) {
1914 if (!node_pred(&dst->node[i], data))
1915 continue;
1916 isl_mat_free(dst->node[i].sched);
1917 isl_map_free(dst->node[i].sched_map);
1918 dst->node[i].sched = isl_mat_copy(src->node[src->n].sched);
1919 dst->node[i].sched_map =
1920 isl_map_copy(src->node[src->n].sched_map);
1921 src->n++;
1924 dst->max_row = src->max_row;
1925 dst->n_total_row = src->n_total_row;
1926 dst->n_band = src->n_band;
1928 return 0;
1931 /* Compute the maximal number of variables over all nodes.
1932 * This is the maximal number of linearly independent schedule
1933 * rows that we need to compute.
1934 * Just in case we end up in a part of the dependence graph
1935 * with only lower-dimensional domains, we make sure we will
1936 * compute the required amount of extra linearly independent rows.
1938 static int compute_maxvar(struct isl_sched_graph *graph)
1940 int i;
1942 graph->maxvar = 0;
1943 for (i = 0; i < graph->n; ++i) {
1944 struct isl_sched_node *node = &graph->node[i];
1945 int nvar;
1947 if (node_update_cmap(node) < 0)
1948 return -1;
1949 nvar = node->nvar + graph->n_row - node->rank;
1950 if (nvar > graph->maxvar)
1951 graph->maxvar = nvar;
1954 return 0;
1957 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph);
1958 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph);
1960 /* Compute a schedule for a subgraph of "graph". In particular, for
1961 * the graph composed of nodes that satisfy node_pred and edges that
1962 * that satisfy edge_pred. The caller should precompute the number
1963 * of nodes and edges that satisfy these predicates and pass them along
1964 * as "n" and "n_edge".
1965 * If the subgraph is known to consist of a single component, then wcc should
1966 * be set and then we call compute_schedule_wcc on the constructed subgraph.
1967 * Otherwise, we call compute_schedule, which will check whether the subgraph
1968 * is connected.
1970 static int compute_sub_schedule(isl_ctx *ctx,
1971 struct isl_sched_graph *graph, int n, int n_edge,
1972 int (*node_pred)(struct isl_sched_node *node, int data),
1973 int (*edge_pred)(struct isl_sched_edge *edge, int data),
1974 int data, int wcc)
1976 struct isl_sched_graph split = { 0 };
1977 int t;
1979 if (graph_alloc(ctx, &split, n, n_edge) < 0)
1980 goto error;
1981 if (copy_nodes(&split, graph, node_pred, data) < 0)
1982 goto error;
1983 if (graph_init_table(ctx, &split) < 0)
1984 goto error;
1985 for (t = 0; t <= isl_edge_last; ++t)
1986 split.max_edge[t] = graph->max_edge[t];
1987 if (graph_init_edge_tables(ctx, &split) < 0)
1988 goto error;
1989 if (copy_edges(ctx, &split, graph, edge_pred, data) < 0)
1990 goto error;
1991 split.n_row = graph->n_row;
1992 split.max_row = graph->max_row;
1993 split.n_total_row = graph->n_total_row;
1994 split.n_band = graph->n_band;
1995 split.band_start = graph->band_start;
1997 if (wcc && compute_schedule_wcc(ctx, &split) < 0)
1998 goto error;
1999 if (!wcc && compute_schedule(ctx, &split) < 0)
2000 goto error;
2002 copy_schedule(graph, &split, node_pred, data);
2004 graph_free(ctx, &split);
2005 return 0;
2006 error:
2007 graph_free(ctx, &split);
2008 return -1;
2011 static int node_scc_exactly(struct isl_sched_node *node, int scc)
2013 return node->scc == scc;
2016 static int node_scc_at_most(struct isl_sched_node *node, int scc)
2018 return node->scc <= scc;
2021 static int node_scc_at_least(struct isl_sched_node *node, int scc)
2023 return node->scc >= scc;
2026 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
2028 return edge->src->scc == scc && edge->dst->scc == scc;
2031 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
2033 return edge->dst->scc <= scc;
2036 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
2038 return edge->src->scc >= scc;
2041 /* Pad the schedules of all nodes with zero rows such that in the end
2042 * they all have graph->n_total_row rows.
2043 * The extra rows don't belong to any band, so they get assigned band number -1.
2045 static int pad_schedule(struct isl_sched_graph *graph)
2047 int i, j;
2049 for (i = 0; i < graph->n; ++i) {
2050 struct isl_sched_node *node = &graph->node[i];
2051 int row = isl_mat_rows(node->sched);
2052 if (graph->n_total_row > row) {
2053 isl_map_free(node->sched_map);
2054 node->sched_map = NULL;
2056 node->sched = isl_mat_add_zero_rows(node->sched,
2057 graph->n_total_row - row);
2058 if (!node->sched)
2059 return -1;
2060 for (j = row; j < graph->n_total_row; ++j)
2061 node->band[j] = -1;
2064 return 0;
2067 /* Split the current graph into two parts and compute a schedule for each
2068 * part individually. In particular, one part consists of all SCCs up
2069 * to and including graph->src_scc, while the other part contains the other
2070 * SCCS.
2072 * The split is enforced in the schedule by constant rows with two different
2073 * values (0 and 1). These constant rows replace the previously computed rows
2074 * in the current band.
2075 * It would be possible to reuse them as the first rows in the next
2076 * band, but recomputing them may result in better rows as we are looking
2077 * at a smaller part of the dependence graph.
2078 * compute_split_schedule is only called when no zero-distance schedule row
2079 * could be found on the entire graph, so we wark the splitting row as
2080 * non zero-distance.
2082 * The band_id of the second group is set to n, where n is the number
2083 * of nodes in the first group. This ensures that the band_ids over
2084 * the two groups remain disjoint, even if either or both of the two
2085 * groups contain independent components.
2087 static int compute_split_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
2089 int i, j, n, e1, e2;
2090 int n_total_row, orig_total_row;
2091 int n_band, orig_band;
2092 int drop;
2094 if (graph->n_total_row >= graph->max_row)
2095 isl_die(ctx, isl_error_internal,
2096 "too many schedule rows", return -1);
2098 drop = graph->n_total_row - graph->band_start;
2099 graph->n_total_row -= drop;
2100 graph->n_row -= drop;
2102 n = 0;
2103 for (i = 0; i < graph->n; ++i) {
2104 struct isl_sched_node *node = &graph->node[i];
2105 int row = isl_mat_rows(node->sched) - drop;
2106 int cols = isl_mat_cols(node->sched);
2107 int before = node->scc <= graph->src_scc;
2109 if (before)
2110 n++;
2112 isl_map_free(node->sched_map);
2113 node->sched_map = NULL;
2114 node->sched = isl_mat_drop_rows(node->sched,
2115 graph->band_start, drop);
2116 node->sched = isl_mat_add_rows(node->sched, 1);
2117 if (!node->sched)
2118 return -1;
2119 node->sched = isl_mat_set_element_si(node->sched, row, 0,
2120 !before);
2121 for (j = 1; j < cols; ++j)
2122 node->sched = isl_mat_set_element_si(node->sched,
2123 row, j, 0);
2124 node->band[graph->n_total_row] = graph->n_band;
2125 node->zero[graph->n_total_row] = 0;
2128 e1 = e2 = 0;
2129 for (i = 0; i < graph->n_edge; ++i) {
2130 if (graph->edge[i].dst->scc <= graph->src_scc)
2131 e1++;
2132 if (graph->edge[i].src->scc > graph->src_scc)
2133 e2++;
2136 graph->n_total_row++;
2137 next_band(graph);
2139 for (i = 0; i < graph->n; ++i) {
2140 struct isl_sched_node *node = &graph->node[i];
2141 if (node->scc > graph->src_scc)
2142 node->band_id[graph->n_band] = n;
2145 orig_total_row = graph->n_total_row;
2146 orig_band = graph->n_band;
2147 if (compute_sub_schedule(ctx, graph, n, e1,
2148 &node_scc_at_most, &edge_dst_scc_at_most,
2149 graph->src_scc, 0) < 0)
2150 return -1;
2151 n_total_row = graph->n_total_row;
2152 graph->n_total_row = orig_total_row;
2153 n_band = graph->n_band;
2154 graph->n_band = orig_band;
2155 if (compute_sub_schedule(ctx, graph, graph->n - n, e2,
2156 &node_scc_at_least, &edge_src_scc_at_least,
2157 graph->src_scc + 1, 0) < 0)
2158 return -1;
2159 if (n_total_row > graph->n_total_row)
2160 graph->n_total_row = n_total_row;
2161 if (n_band > graph->n_band)
2162 graph->n_band = n_band;
2164 return pad_schedule(graph);
2167 /* Compute the next band of the schedule after updating the dependence
2168 * relations based on the the current schedule.
2170 static int compute_next_band(isl_ctx *ctx, struct isl_sched_graph *graph)
2172 if (update_edges(ctx, graph) < 0)
2173 return -1;
2174 next_band(graph);
2176 return compute_schedule(ctx, graph);
2179 /* Add constraints to graph->lp that force the dependence "map" (which
2180 * is part of the dependence relation of "edge")
2181 * to be respected and attempt to carry it, where the edge is one from
2182 * a node j to itself. "pos" is the sequence number of the given map.
2183 * That is, add constraints that enforce
2185 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
2186 * = c_j_x (y - x) >= e_i
2188 * for each (x,y) in R.
2189 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2190 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
2191 * with each coefficient in c_j_x represented as a pair of non-negative
2192 * coefficients.
2194 static int add_intra_constraints(struct isl_sched_graph *graph,
2195 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
2197 unsigned total;
2198 isl_ctx *ctx = isl_map_get_ctx(map);
2199 isl_space *dim;
2200 isl_dim_map *dim_map;
2201 isl_basic_set *coef;
2202 struct isl_sched_node *node = edge->src;
2204 coef = intra_coefficients(graph, map);
2205 if (!coef)
2206 return -1;
2208 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
2210 total = isl_basic_set_total_dim(graph->lp);
2211 dim_map = isl_dim_map_alloc(ctx, total);
2212 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
2213 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
2214 isl_space_dim(dim, isl_dim_set), 1,
2215 node->nvar, -1);
2216 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
2217 isl_space_dim(dim, isl_dim_set), 1,
2218 node->nvar, 1);
2219 graph->lp = isl_basic_set_extend_constraints(graph->lp,
2220 coef->n_eq, coef->n_ineq);
2221 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
2222 coef, dim_map);
2223 isl_space_free(dim);
2225 return 0;
2228 /* Add constraints to graph->lp that force the dependence "map" (which
2229 * is part of the dependence relation of "edge")
2230 * to be respected and attempt to carry it, where the edge is one from
2231 * node j to node k. "pos" is the sequence number of the given map.
2232 * That is, add constraints that enforce
2234 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
2236 * for each (x,y) in R.
2237 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2238 * of valid constraints for R and then plug in
2239 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
2240 * with each coefficient (except e_i, c_k_0 and c_j_0)
2241 * represented as a pair of non-negative coefficients.
2243 static int add_inter_constraints(struct isl_sched_graph *graph,
2244 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
2246 unsigned total;
2247 isl_ctx *ctx = isl_map_get_ctx(map);
2248 isl_space *dim;
2249 isl_dim_map *dim_map;
2250 isl_basic_set *coef;
2251 struct isl_sched_node *src = edge->src;
2252 struct isl_sched_node *dst = edge->dst;
2254 coef = inter_coefficients(graph, map);
2255 if (!coef)
2256 return -1;
2258 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
2260 total = isl_basic_set_total_dim(graph->lp);
2261 dim_map = isl_dim_map_alloc(ctx, total);
2263 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
2265 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
2266 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
2267 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
2268 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
2269 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
2270 dst->nvar, -1);
2271 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
2272 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
2273 dst->nvar, 1);
2275 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
2276 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
2277 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
2278 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
2279 isl_space_dim(dim, isl_dim_set), 1,
2280 src->nvar, 1);
2281 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
2282 isl_space_dim(dim, isl_dim_set), 1,
2283 src->nvar, -1);
2285 graph->lp = isl_basic_set_extend_constraints(graph->lp,
2286 coef->n_eq, coef->n_ineq);
2287 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
2288 coef, dim_map);
2289 isl_space_free(dim);
2291 return 0;
2294 /* Add constraints to graph->lp that force all validity dependences
2295 * to be respected and attempt to carry them.
2297 static int add_all_constraints(struct isl_sched_graph *graph)
2299 int i, j;
2300 int pos;
2302 pos = 0;
2303 for (i = 0; i < graph->n_edge; ++i) {
2304 struct isl_sched_edge *edge= &graph->edge[i];
2306 if (!edge->validity)
2307 continue;
2309 for (j = 0; j < edge->map->n; ++j) {
2310 isl_basic_map *bmap;
2311 isl_map *map;
2313 bmap = isl_basic_map_copy(edge->map->p[j]);
2314 map = isl_map_from_basic_map(bmap);
2316 if (edge->src == edge->dst &&
2317 add_intra_constraints(graph, edge, map, pos) < 0)
2318 return -1;
2319 if (edge->src != edge->dst &&
2320 add_inter_constraints(graph, edge, map, pos) < 0)
2321 return -1;
2322 ++pos;
2326 return 0;
2329 /* Count the number of equality and inequality constraints
2330 * that will be added to the carry_lp problem.
2331 * We count each edge exactly once.
2333 static int count_all_constraints(struct isl_sched_graph *graph,
2334 int *n_eq, int *n_ineq)
2336 int i, j;
2338 *n_eq = *n_ineq = 0;
2339 for (i = 0; i < graph->n_edge; ++i) {
2340 struct isl_sched_edge *edge= &graph->edge[i];
2341 for (j = 0; j < edge->map->n; ++j) {
2342 isl_basic_map *bmap;
2343 isl_map *map;
2345 bmap = isl_basic_map_copy(edge->map->p[j]);
2346 map = isl_map_from_basic_map(bmap);
2348 if (count_map_constraints(graph, edge, map,
2349 n_eq, n_ineq, 1) < 0)
2350 return -1;
2354 return 0;
2357 /* Construct an LP problem for finding schedule coefficients
2358 * such that the schedule carries as many dependences as possible.
2359 * In particular, for each dependence i, we bound the dependence distance
2360 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
2361 * of all e_i's. Dependence with e_i = 0 in the solution are simply
2362 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
2363 * Note that if the dependence relation is a union of basic maps,
2364 * then we have to consider each basic map individually as it may only
2365 * be possible to carry the dependences expressed by some of those
2366 * basic maps and not all off them.
2367 * Below, we consider each of those basic maps as a separate "edge".
2369 * All variables of the LP are non-negative. The actual coefficients
2370 * may be negative, so each coefficient is represented as the difference
2371 * of two non-negative variables. The negative part always appears
2372 * immediately before the positive part.
2373 * Other than that, the variables have the following order
2375 * - sum of (1 - e_i) over all edges
2376 * - sum of positive and negative parts of all c_n coefficients
2377 * (unconstrained when computing non-parametric schedules)
2378 * - sum of positive and negative parts of all c_x coefficients
2379 * - for each edge
2380 * - e_i
2381 * - for each node
2382 * - c_i_0
2383 * - positive and negative parts of c_i_n (if parametric)
2384 * - positive and negative parts of c_i_x
2386 * The constraints are those from the (validity) edges plus three equalities
2387 * to express the sums and n_edge inequalities to express e_i <= 1.
2389 static int setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
2391 int i, j;
2392 int k;
2393 isl_space *dim;
2394 unsigned total;
2395 int n_eq, n_ineq;
2396 int n_edge;
2398 n_edge = 0;
2399 for (i = 0; i < graph->n_edge; ++i)
2400 n_edge += graph->edge[i].map->n;
2402 total = 3 + n_edge;
2403 for (i = 0; i < graph->n; ++i) {
2404 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2405 node->start = total;
2406 total += 1 + 2 * (node->nparam + node->nvar);
2409 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
2410 return -1;
2412 dim = isl_space_set_alloc(ctx, 0, total);
2413 isl_basic_set_free(graph->lp);
2414 n_eq += 3;
2415 n_ineq += n_edge;
2416 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
2417 graph->lp = isl_basic_set_set_rational(graph->lp);
2419 k = isl_basic_set_alloc_equality(graph->lp);
2420 if (k < 0)
2421 return -1;
2422 isl_seq_clr(graph->lp->eq[k], 1 + total);
2423 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
2424 isl_int_set_si(graph->lp->eq[k][1], 1);
2425 for (i = 0; i < n_edge; ++i)
2426 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
2428 k = isl_basic_set_alloc_equality(graph->lp);
2429 if (k < 0)
2430 return -1;
2431 isl_seq_clr(graph->lp->eq[k], 1 + total);
2432 isl_int_set_si(graph->lp->eq[k][2], -1);
2433 for (i = 0; i < graph->n; ++i) {
2434 int pos = 1 + graph->node[i].start + 1;
2436 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
2437 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2440 k = isl_basic_set_alloc_equality(graph->lp);
2441 if (k < 0)
2442 return -1;
2443 isl_seq_clr(graph->lp->eq[k], 1 + total);
2444 isl_int_set_si(graph->lp->eq[k][3], -1);
2445 for (i = 0; i < graph->n; ++i) {
2446 struct isl_sched_node *node = &graph->node[i];
2447 int pos = 1 + node->start + 1 + 2 * node->nparam;
2449 for (j = 0; j < 2 * node->nvar; ++j)
2450 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2453 for (i = 0; i < n_edge; ++i) {
2454 k = isl_basic_set_alloc_inequality(graph->lp);
2455 if (k < 0)
2456 return -1;
2457 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2458 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
2459 isl_int_set_si(graph->lp->ineq[k][0], 1);
2462 if (add_all_constraints(graph) < 0)
2463 return -1;
2465 return 0;
2468 /* If the schedule_split_scaled option is set and if the linear
2469 * parts of the scheduling rows for all nodes in the graphs have
2470 * non-trivial common divisor, then split off the constant term
2471 * from the linear part.
2472 * The constant term is then placed in a separate band and
2473 * the linear part is reduced.
2475 static int split_scaled(isl_ctx *ctx, struct isl_sched_graph *graph)
2477 int i;
2478 int row;
2479 isl_int gcd, gcd_i;
2481 if (!ctx->opt->schedule_split_scaled)
2482 return 0;
2483 if (graph->n <= 1)
2484 return 0;
2486 if (graph->n_total_row >= graph->max_row)
2487 isl_die(ctx, isl_error_internal,
2488 "too many schedule rows", return -1);
2490 isl_int_init(gcd);
2491 isl_int_init(gcd_i);
2493 isl_int_set_si(gcd, 0);
2495 row = isl_mat_rows(graph->node[0].sched) - 1;
2497 for (i = 0; i < graph->n; ++i) {
2498 struct isl_sched_node *node = &graph->node[i];
2499 int cols = isl_mat_cols(node->sched);
2501 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
2502 isl_int_gcd(gcd, gcd, gcd_i);
2505 isl_int_clear(gcd_i);
2507 if (isl_int_cmp_si(gcd, 1) <= 0) {
2508 isl_int_clear(gcd);
2509 return 0;
2512 next_band(graph);
2514 for (i = 0; i < graph->n; ++i) {
2515 struct isl_sched_node *node = &graph->node[i];
2517 isl_map_free(node->sched_map);
2518 node->sched_map = NULL;
2519 node->sched = isl_mat_add_zero_rows(node->sched, 1);
2520 if (!node->sched)
2521 goto error;
2522 isl_int_fdiv_r(node->sched->row[row + 1][0],
2523 node->sched->row[row][0], gcd);
2524 isl_int_fdiv_q(node->sched->row[row][0],
2525 node->sched->row[row][0], gcd);
2526 isl_int_mul(node->sched->row[row][0],
2527 node->sched->row[row][0], gcd);
2528 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
2529 if (!node->sched)
2530 goto error;
2531 node->band[graph->n_total_row] = graph->n_band;
2534 graph->n_total_row++;
2536 isl_int_clear(gcd);
2537 return 0;
2538 error:
2539 isl_int_clear(gcd);
2540 return -1;
2543 static int compute_component_schedule(isl_ctx *ctx,
2544 struct isl_sched_graph *graph);
2546 /* Is the schedule row "sol" trivial on node "node"?
2547 * That is, is the solution zero on the dimensions orthogonal to
2548 * the previously found solutions?
2549 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
2551 * Each coefficient is represented as the difference between
2552 * two non-negative values in "sol". "sol" has been computed
2553 * in terms of the original iterators (i.e., without use of cmap).
2554 * We construct the schedule row s and write it as a linear
2555 * combination of (linear combinations of) previously computed schedule rows.
2556 * s = Q c or c = U s.
2557 * If the final entries of c are all zero, then the solution is trivial.
2559 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
2561 int i;
2562 int pos;
2563 int trivial;
2564 isl_ctx *ctx;
2565 isl_vec *node_sol;
2567 if (!sol)
2568 return -1;
2569 if (node->nvar == node->rank)
2570 return 0;
2572 ctx = isl_vec_get_ctx(sol);
2573 node_sol = isl_vec_alloc(ctx, node->nvar);
2574 if (!node_sol)
2575 return -1;
2577 pos = 1 + node->start + 1 + 2 * node->nparam;
2579 for (i = 0; i < node->nvar; ++i)
2580 isl_int_sub(node_sol->el[i],
2581 sol->el[pos + 2 * i + 1], sol->el[pos + 2 * i]);
2583 node_sol = isl_mat_vec_product(isl_mat_copy(node->cinv), node_sol);
2585 if (!node_sol)
2586 return -1;
2588 trivial = isl_seq_first_non_zero(node_sol->el + node->rank,
2589 node->nvar - node->rank) == -1;
2591 isl_vec_free(node_sol);
2593 return trivial;
2596 /* Is the schedule row "sol" trivial on any node where it should
2597 * not be trivial?
2598 * "sol" has been computed in terms of the original iterators
2599 * (i.e., without use of cmap).
2600 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
2602 static int is_any_trivial(struct isl_sched_graph *graph,
2603 __isl_keep isl_vec *sol)
2605 int i;
2607 for (i = 0; i < graph->n; ++i) {
2608 struct isl_sched_node *node = &graph->node[i];
2609 int trivial;
2611 if (!needs_row(graph, node))
2612 continue;
2613 trivial = is_trivial(node, sol);
2614 if (trivial < 0 || trivial)
2615 return trivial;
2618 return 0;
2621 /* Construct a schedule row for each node such that as many dependences
2622 * as possible are carried and then continue with the next band.
2624 * If the computed schedule row turns out to be trivial on one or
2625 * more nodes where it should not be trivial, then we throw it away
2626 * and try again on each component separately.
2628 static int carry_dependences(isl_ctx *ctx, struct isl_sched_graph *graph)
2630 int i;
2631 int n_edge;
2632 int trivial;
2633 isl_vec *sol;
2634 isl_basic_set *lp;
2636 n_edge = 0;
2637 for (i = 0; i < graph->n_edge; ++i)
2638 n_edge += graph->edge[i].map->n;
2640 if (setup_carry_lp(ctx, graph) < 0)
2641 return -1;
2643 lp = isl_basic_set_copy(graph->lp);
2644 sol = isl_tab_basic_set_non_neg_lexmin(lp);
2645 if (!sol)
2646 return -1;
2648 if (sol->size == 0) {
2649 isl_vec_free(sol);
2650 isl_die(ctx, isl_error_internal,
2651 "error in schedule construction", return -1);
2654 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
2655 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
2656 isl_vec_free(sol);
2657 isl_die(ctx, isl_error_unknown,
2658 "unable to carry dependences", return -1);
2661 trivial = is_any_trivial(graph, sol);
2662 if (trivial < 0) {
2663 sol = isl_vec_free(sol);
2664 } else if (trivial) {
2665 isl_vec_free(sol);
2666 if (graph->scc > 1)
2667 return compute_component_schedule(ctx, graph);
2668 isl_die(ctx, isl_error_unknown,
2669 "unable to construct non-trivial solution", return -1);
2672 if (update_schedule(graph, sol, 0, 0) < 0)
2673 return -1;
2675 if (split_scaled(ctx, graph) < 0)
2676 return -1;
2678 return compute_next_band(ctx, graph);
2681 /* Are there any (non-empty) validity edges in the graph?
2683 static int has_validity_edges(struct isl_sched_graph *graph)
2685 int i;
2687 for (i = 0; i < graph->n_edge; ++i) {
2688 int empty;
2690 empty = isl_map_plain_is_empty(graph->edge[i].map);
2691 if (empty < 0)
2692 return -1;
2693 if (empty)
2694 continue;
2695 if (graph->edge[i].validity)
2696 return 1;
2699 return 0;
2702 /* Should we apply a Feautrier step?
2703 * That is, did the user request the Feautrier algorithm and are
2704 * there any validity dependences (left)?
2706 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
2708 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
2709 return 0;
2711 return has_validity_edges(graph);
2714 /* Compute a schedule for a connected dependence graph using Feautrier's
2715 * multi-dimensional scheduling algorithm.
2716 * The original algorithm is described in [1].
2717 * The main idea is to minimize the number of scheduling dimensions, by
2718 * trying to satisfy as many dependences as possible per scheduling dimension.
2720 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
2721 * Problem, Part II: Multi-Dimensional Time.
2722 * In Intl. Journal of Parallel Programming, 1992.
2724 static int compute_schedule_wcc_feautrier(isl_ctx *ctx,
2725 struct isl_sched_graph *graph)
2727 return carry_dependences(ctx, graph);
2730 /* Compute a schedule for a connected dependence graph.
2731 * We try to find a sequence of as many schedule rows as possible that result
2732 * in non-negative dependence distances (independent of the previous rows
2733 * in the sequence, i.e., such that the sequence is tilable).
2734 * If we can't find any more rows we either
2735 * - split between SCCs and start over (assuming we found an interesting
2736 * pair of SCCs between which to split)
2737 * - continue with the next band (assuming the current band has at least
2738 * one row)
2739 * - try to carry as many dependences as possible and continue with the next
2740 * band
2742 * If Feautrier's algorithm is selected, we first recursively try to satisfy
2743 * as many validity dependences as possible. When all validity dependences
2744 * are satisfied we extend the schedule to a full-dimensional schedule.
2746 * If we manage to complete the schedule, we finish off by topologically
2747 * sorting the statements based on the remaining dependences.
2749 * If ctx->opt->schedule_outer_zero_distance is set, then we force the
2750 * outermost dimension in the current band to be zero distance. If this
2751 * turns out to be impossible, we fall back on the general scheme above
2752 * and try to carry as many dependences as possible.
2754 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph)
2756 int force_zero = 0;
2758 if (detect_sccs(ctx, graph) < 0)
2759 return -1;
2760 if (sort_sccs(graph) < 0)
2761 return -1;
2763 if (compute_maxvar(graph) < 0)
2764 return -1;
2766 if (need_feautrier_step(ctx, graph))
2767 return compute_schedule_wcc_feautrier(ctx, graph);
2769 if (ctx->opt->schedule_outer_zero_distance)
2770 force_zero = 1;
2772 while (graph->n_row < graph->maxvar) {
2773 isl_vec *sol;
2775 graph->src_scc = -1;
2776 graph->dst_scc = -1;
2778 if (setup_lp(ctx, graph, force_zero) < 0)
2779 return -1;
2780 sol = solve_lp(graph);
2781 if (!sol)
2782 return -1;
2783 if (sol->size == 0) {
2784 isl_vec_free(sol);
2785 if (!ctx->opt->schedule_maximize_band_depth &&
2786 graph->n_total_row > graph->band_start)
2787 return compute_next_band(ctx, graph);
2788 if (graph->src_scc >= 0)
2789 return compute_split_schedule(ctx, graph);
2790 if (graph->n_total_row > graph->band_start)
2791 return compute_next_band(ctx, graph);
2792 return carry_dependences(ctx, graph);
2794 if (update_schedule(graph, sol, 1, 1) < 0)
2795 return -1;
2796 force_zero = 0;
2799 if (graph->n_total_row > graph->band_start)
2800 next_band(graph);
2801 return sort_statements(ctx, graph);
2804 /* Add a row to the schedules that separates the SCCs and move
2805 * to the next band.
2807 static int split_on_scc(isl_ctx *ctx, struct isl_sched_graph *graph)
2809 int i;
2811 if (graph->n_total_row >= graph->max_row)
2812 isl_die(ctx, isl_error_internal,
2813 "too many schedule rows", return -1);
2815 for (i = 0; i < graph->n; ++i) {
2816 struct isl_sched_node *node = &graph->node[i];
2817 int row = isl_mat_rows(node->sched);
2819 isl_map_free(node->sched_map);
2820 node->sched_map = NULL;
2821 node->sched = isl_mat_add_zero_rows(node->sched, 1);
2822 node->sched = isl_mat_set_element_si(node->sched, row, 0,
2823 node->scc);
2824 if (!node->sched)
2825 return -1;
2826 node->band[graph->n_total_row] = graph->n_band;
2829 graph->n_total_row++;
2830 next_band(graph);
2832 return 0;
2835 /* Compute a schedule for each component (identified by node->scc)
2836 * of the dependence graph separately and then combine the results.
2837 * Depending on the setting of schedule_fuse, a component may be
2838 * either weakly or strongly connected.
2840 * The band_id is adjusted such that each component has a separate id.
2841 * Note that the band_id may have already been set to a value different
2842 * from zero by compute_split_schedule.
2844 static int compute_component_schedule(isl_ctx *ctx,
2845 struct isl_sched_graph *graph)
2847 int wcc, i;
2848 int n, n_edge;
2849 int n_total_row, orig_total_row;
2850 int n_band, orig_band;
2852 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN ||
2853 ctx->opt->schedule_separate_components)
2854 if (split_on_scc(ctx, graph) < 0)
2855 return -1;
2857 n_total_row = 0;
2858 orig_total_row = graph->n_total_row;
2859 n_band = 0;
2860 orig_band = graph->n_band;
2861 for (i = 0; i < graph->n; ++i)
2862 graph->node[i].band_id[graph->n_band] += graph->node[i].scc;
2863 for (wcc = 0; wcc < graph->scc; ++wcc) {
2864 n = 0;
2865 for (i = 0; i < graph->n; ++i)
2866 if (graph->node[i].scc == wcc)
2867 n++;
2868 n_edge = 0;
2869 for (i = 0; i < graph->n_edge; ++i)
2870 if (graph->edge[i].src->scc == wcc &&
2871 graph->edge[i].dst->scc == wcc)
2872 n_edge++;
2874 if (compute_sub_schedule(ctx, graph, n, n_edge,
2875 &node_scc_exactly,
2876 &edge_scc_exactly, wcc, 1) < 0)
2877 return -1;
2878 if (graph->n_total_row > n_total_row)
2879 n_total_row = graph->n_total_row;
2880 graph->n_total_row = orig_total_row;
2881 if (graph->n_band > n_band)
2882 n_band = graph->n_band;
2883 graph->n_band = orig_band;
2886 graph->n_total_row = n_total_row;
2887 graph->n_band = n_band;
2889 return pad_schedule(graph);
2892 /* Compute a schedule for the given dependence graph.
2893 * We first check if the graph is connected (through validity dependences)
2894 * and, if not, compute a schedule for each component separately.
2895 * If schedule_fuse is set to minimal fusion, then we check for strongly
2896 * connected components instead and compute a separate schedule for
2897 * each such strongly connected component.
2899 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
2901 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN) {
2902 if (detect_sccs(ctx, graph) < 0)
2903 return -1;
2904 } else {
2905 if (detect_wccs(ctx, graph) < 0)
2906 return -1;
2909 if (graph->scc > 1)
2910 return compute_component_schedule(ctx, graph);
2912 return compute_schedule_wcc(ctx, graph);
2915 /* Compute a schedule for the given union of domains that respects
2916 * all the validity dependences.
2917 * If the default isl scheduling algorithm is used, it tries to minimize
2918 * the dependence distances over the proximity dependences.
2919 * If Feautrier's scheduling algorithm is used, the proximity dependence
2920 * distances are only minimized during the extension to a full-dimensional
2921 * schedule.
2923 __isl_give isl_schedule *isl_union_set_compute_schedule(
2924 __isl_take isl_union_set *domain,
2925 __isl_take isl_union_map *validity,
2926 __isl_take isl_union_map *proximity)
2928 isl_ctx *ctx = isl_union_set_get_ctx(domain);
2929 isl_space *dim;
2930 struct isl_sched_graph graph = { 0 };
2931 isl_schedule *sched;
2932 struct isl_extract_edge_data data;
2934 domain = isl_union_set_align_params(domain,
2935 isl_union_map_get_space(validity));
2936 domain = isl_union_set_align_params(domain,
2937 isl_union_map_get_space(proximity));
2938 dim = isl_union_set_get_space(domain);
2939 validity = isl_union_map_align_params(validity, isl_space_copy(dim));
2940 proximity = isl_union_map_align_params(proximity, dim);
2942 if (!domain)
2943 goto error;
2945 graph.n = isl_union_set_n_set(domain);
2946 if (graph.n == 0)
2947 goto empty;
2948 if (graph_alloc(ctx, &graph, graph.n,
2949 isl_union_map_n_map(validity) + isl_union_map_n_map(proximity)) < 0)
2950 goto error;
2951 if (compute_max_row(&graph, domain) < 0)
2952 goto error;
2953 graph.root = 1;
2954 graph.n = 0;
2955 if (isl_union_set_foreach_set(domain, &extract_node, &graph) < 0)
2956 goto error;
2957 if (graph_init_table(ctx, &graph) < 0)
2958 goto error;
2959 graph.max_edge[isl_edge_validity] = isl_union_map_n_map(validity);
2960 graph.max_edge[isl_edge_proximity] = isl_union_map_n_map(proximity);
2961 if (graph_init_edge_tables(ctx, &graph) < 0)
2962 goto error;
2963 graph.n_edge = 0;
2964 data.graph = &graph;
2965 data.type = isl_edge_validity;
2966 if (isl_union_map_foreach_map(validity, &extract_edge, &data) < 0)
2967 goto error;
2968 data.type = isl_edge_proximity;
2969 if (isl_union_map_foreach_map(proximity, &extract_edge, &data) < 0)
2970 goto error;
2972 if (compute_schedule(ctx, &graph) < 0)
2973 goto error;
2975 empty:
2976 sched = extract_schedule(&graph, isl_union_set_get_space(domain));
2978 graph_free(ctx, &graph);
2979 isl_union_set_free(domain);
2980 isl_union_map_free(validity);
2981 isl_union_map_free(proximity);
2983 return sched;
2984 error:
2985 graph_free(ctx, &graph);
2986 isl_union_set_free(domain);
2987 isl_union_map_free(validity);
2988 isl_union_map_free(proximity);
2989 return NULL;
2992 void *isl_schedule_free(__isl_take isl_schedule *sched)
2994 int i;
2995 if (!sched)
2996 return NULL;
2998 if (--sched->ref > 0)
2999 return NULL;
3001 for (i = 0; i < sched->n; ++i) {
3002 isl_multi_aff_free(sched->node[i].sched);
3003 free(sched->node[i].band_end);
3004 free(sched->node[i].band_id);
3005 free(sched->node[i].zero);
3007 isl_space_free(sched->dim);
3008 isl_band_list_free(sched->band_forest);
3009 free(sched);
3010 return NULL;
3013 isl_ctx *isl_schedule_get_ctx(__isl_keep isl_schedule *schedule)
3015 return schedule ? isl_space_get_ctx(schedule->dim) : NULL;
3018 /* Set max_out to the maximal number of output dimensions over
3019 * all maps.
3021 static int update_max_out(__isl_take isl_map *map, void *user)
3023 int *max_out = user;
3024 int n_out = isl_map_dim(map, isl_dim_out);
3026 if (n_out > *max_out)
3027 *max_out = n_out;
3029 isl_map_free(map);
3030 return 0;
3033 /* Internal data structure for map_pad_range.
3035 * "max_out" is the maximal schedule dimension.
3036 * "res" collects the results.
3038 struct isl_pad_schedule_map_data {
3039 int max_out;
3040 isl_union_map *res;
3043 /* Pad the range of the given map with zeros to data->max_out and
3044 * then add the result to data->res.
3046 static int map_pad_range(__isl_take isl_map *map, void *user)
3048 struct isl_pad_schedule_map_data *data = user;
3049 int i;
3050 int n_out = isl_map_dim(map, isl_dim_out);
3052 map = isl_map_add_dims(map, isl_dim_out, data->max_out - n_out);
3053 for (i = n_out; i < data->max_out; ++i)
3054 map = isl_map_fix_si(map, isl_dim_out, i, 0);
3056 data->res = isl_union_map_add_map(data->res, map);
3057 if (!data->res)
3058 return -1;
3060 return 0;
3063 /* Pad the ranges of the maps in the union map with zeros such they all have
3064 * the same dimension.
3066 static __isl_give isl_union_map *pad_schedule_map(
3067 __isl_take isl_union_map *umap)
3069 struct isl_pad_schedule_map_data data;
3071 if (!umap)
3072 return NULL;
3073 if (isl_union_map_n_map(umap) <= 1)
3074 return umap;
3076 data.max_out = 0;
3077 if (isl_union_map_foreach_map(umap, &update_max_out, &data.max_out) < 0)
3078 return isl_union_map_free(umap);
3080 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3081 if (isl_union_map_foreach_map(umap, &map_pad_range, &data) < 0)
3082 data.res = isl_union_map_free(data.res);
3084 isl_union_map_free(umap);
3085 return data.res;
3088 /* Return an isl_union_map of the schedule. If we have already constructed
3089 * a band forest, then this band forest may have been modified so we need
3090 * to extract the isl_union_map from the forest rather than from
3091 * the originally computed schedule. This reconstructed schedule map
3092 * then needs to be padded with zeros to unify the schedule space
3093 * since the result of isl_band_list_get_suffix_schedule may not have
3094 * a unified schedule space.
3096 __isl_give isl_union_map *isl_schedule_get_map(__isl_keep isl_schedule *sched)
3098 int i;
3099 isl_union_map *umap;
3101 if (!sched)
3102 return NULL;
3104 if (sched->band_forest) {
3105 umap = isl_band_list_get_suffix_schedule(sched->band_forest);
3106 return pad_schedule_map(umap);
3109 umap = isl_union_map_empty(isl_space_copy(sched->dim));
3110 for (i = 0; i < sched->n; ++i) {
3111 isl_multi_aff *ma;
3113 ma = isl_multi_aff_copy(sched->node[i].sched);
3114 umap = isl_union_map_add_map(umap, isl_map_from_multi_aff(ma));
3117 return umap;
3120 static __isl_give isl_band_list *construct_band_list(
3121 __isl_keep isl_schedule *schedule, __isl_keep isl_band *parent,
3122 int band_nr, int *parent_active, int n_active);
3124 /* Construct an isl_band structure for the band in the given schedule
3125 * with sequence number band_nr for the n_active nodes marked by active.
3126 * If the nodes don't have a band with the given sequence number,
3127 * then a band without members is created.
3129 * Because of the way the schedule is constructed, we know that
3130 * the position of the band inside the schedule of a node is the same
3131 * for all active nodes.
3133 * The partial schedule for the band is created before the children
3134 * are created to that construct_band_list can refer to the partial
3135 * schedule of the parent.
3137 static __isl_give isl_band *construct_band(__isl_keep isl_schedule *schedule,
3138 __isl_keep isl_band *parent,
3139 int band_nr, int *active, int n_active)
3141 int i, j;
3142 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
3143 isl_band *band;
3144 unsigned start, end;
3146 band = isl_band_alloc(ctx);
3147 if (!band)
3148 return NULL;
3150 band->schedule = schedule;
3151 band->parent = parent;
3153 for (i = 0; i < schedule->n; ++i)
3154 if (active[i])
3155 break;
3157 if (i >= schedule->n)
3158 isl_die(ctx, isl_error_internal,
3159 "band without active statements", goto error);
3161 start = band_nr ? schedule->node[i].band_end[band_nr - 1] : 0;
3162 end = band_nr < schedule->node[i].n_band ?
3163 schedule->node[i].band_end[band_nr] : start;
3164 band->n = end - start;
3166 band->zero = isl_alloc_array(ctx, int, band->n);
3167 if (band->n && !band->zero)
3168 goto error;
3170 for (j = 0; j < band->n; ++j)
3171 band->zero[j] = schedule->node[i].zero[start + j];
3173 band->pma = isl_union_pw_multi_aff_empty(isl_space_copy(schedule->dim));
3174 for (i = 0; i < schedule->n; ++i) {
3175 isl_multi_aff *ma;
3176 isl_pw_multi_aff *pma;
3177 unsigned n_out;
3179 if (!active[i])
3180 continue;
3182 ma = isl_multi_aff_copy(schedule->node[i].sched);
3183 n_out = isl_multi_aff_dim(ma, isl_dim_out);
3184 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, end, n_out - end);
3185 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, 0, start);
3186 pma = isl_pw_multi_aff_from_multi_aff(ma);
3187 band->pma = isl_union_pw_multi_aff_add_pw_multi_aff(band->pma,
3188 pma);
3190 if (!band->pma)
3191 goto error;
3193 for (i = 0; i < schedule->n; ++i)
3194 if (active[i] && schedule->node[i].n_band > band_nr + 1)
3195 break;
3197 if (i < schedule->n) {
3198 band->children = construct_band_list(schedule, band,
3199 band_nr + 1, active, n_active);
3200 if (!band->children)
3201 goto error;
3204 return band;
3205 error:
3206 isl_band_free(band);
3207 return NULL;
3210 /* Internal data structure used inside cmp_band and pw_multi_aff_extract_int.
3212 * r is set to a negative value if anything goes wrong.
3214 * c1 stores the result of extract_int.
3215 * c2 is a temporary value used inside cmp_band_in_ancestor.
3216 * t is a temporary value used inside extract_int.
3218 * first and equal are used inside extract_int.
3219 * first is set if we are looking at the first isl_multi_aff inside
3220 * the isl_union_pw_multi_aff.
3221 * equal is set if all the isl_multi_affs have been equal so far.
3223 struct isl_cmp_band_data {
3224 int r;
3226 int first;
3227 int equal;
3229 isl_int t;
3230 isl_int c1;
3231 isl_int c2;
3234 /* Check if "ma" assigns a constant value.
3235 * Note that this function is only called on isl_multi_affs
3236 * with a single output dimension.
3238 * If "ma" assigns a constant value then we compare it to data->c1
3239 * or assign it to data->c1 if this is the first isl_multi_aff we consider.
3240 * If "ma" does not assign a constant value or if it assigns a value
3241 * that is different from data->c1, then we set data->equal to zero
3242 * and terminate the check.
3244 static int multi_aff_extract_int(__isl_take isl_set *set,
3245 __isl_take isl_multi_aff *ma, void *user)
3247 isl_aff *aff;
3248 struct isl_cmp_band_data *data = user;
3250 aff = isl_multi_aff_get_aff(ma, 0);
3251 data->r = isl_aff_is_cst(aff);
3252 if (data->r >= 0 && data->r) {
3253 isl_aff_get_constant(aff, &data->t);
3254 if (data->first) {
3255 isl_int_set(data->c1, data->t);
3256 data->first = 0;
3257 } else if (!isl_int_eq(data->c1, data->t))
3258 data->equal = 0;
3259 } else if (data->r >= 0 && !data->r)
3260 data->equal = 0;
3262 isl_aff_free(aff);
3263 isl_set_free(set);
3264 isl_multi_aff_free(ma);
3266 if (data->r < 0)
3267 return -1;
3268 if (!data->equal)
3269 return -1;
3270 return 0;
3273 /* This function is called for each isl_pw_multi_aff in
3274 * the isl_union_pw_multi_aff checked by extract_int.
3275 * Check all the isl_multi_affs inside "pma".
3277 static int pw_multi_aff_extract_int(__isl_take isl_pw_multi_aff *pma,
3278 void *user)
3280 int r;
3282 r = isl_pw_multi_aff_foreach_piece(pma, &multi_aff_extract_int, user);
3283 isl_pw_multi_aff_free(pma);
3285 return r;
3288 /* Check if "upma" assigns a single constant value to its domain.
3289 * If so, return 1 and store the result in data->c1.
3290 * If not, return 0.
3292 * A negative return value from isl_union_pw_multi_aff_foreach_pw_multi_aff
3293 * means that either an error occurred or that we have broken off the check
3294 * because we already know the result is going to be negative.
3295 * In the latter case, data->equal is set to zero.
3297 static int extract_int(__isl_keep isl_union_pw_multi_aff *upma,
3298 struct isl_cmp_band_data *data)
3300 data->first = 1;
3301 data->equal = 1;
3303 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3304 &pw_multi_aff_extract_int, data) < 0) {
3305 if (!data->equal)
3306 return 0;
3307 return -1;
3310 return !data->first && data->equal;
3313 /* Compare "b1" and "b2" based on the parent schedule of their ancestor
3314 * "ancestor".
3316 * If the parent of "ancestor" also has a single member, then we
3317 * first try to compare the two band based on the partial schedule
3318 * of this parent.
3320 * Otherwise, or if the result is inconclusive, we look at the partial schedule
3321 * of "ancestor" itself.
3322 * In particular, we specialize the parent schedule based
3323 * on the domains of the child schedules, check if both assign
3324 * a single constant value and, if so, compare the two constant values.
3325 * If the specialized parent schedules do not assign a constant value,
3326 * then they cannot be used to order the two bands and so in this case
3327 * we return 0.
3329 static int cmp_band_in_ancestor(__isl_keep isl_band *b1,
3330 __isl_keep isl_band *b2, struct isl_cmp_band_data *data,
3331 __isl_keep isl_band *ancestor)
3333 isl_union_pw_multi_aff *upma;
3334 isl_union_set *domain;
3335 int r;
3337 if (data->r < 0)
3338 return 0;
3340 if (ancestor->parent && ancestor->parent->n == 1) {
3341 r = cmp_band_in_ancestor(b1, b2, data, ancestor->parent);
3342 if (data->r < 0)
3343 return 0;
3344 if (r)
3345 return r;
3348 upma = isl_union_pw_multi_aff_copy(b1->pma);
3349 domain = isl_union_pw_multi_aff_domain(upma);
3350 upma = isl_union_pw_multi_aff_copy(ancestor->pma);
3351 upma = isl_union_pw_multi_aff_intersect_domain(upma, domain);
3352 r = extract_int(upma, data);
3353 isl_union_pw_multi_aff_free(upma);
3355 if (r < 0)
3356 data->r = -1;
3357 if (r < 0 || !r)
3358 return 0;
3360 isl_int_set(data->c2, data->c1);
3362 upma = isl_union_pw_multi_aff_copy(b2->pma);
3363 domain = isl_union_pw_multi_aff_domain(upma);
3364 upma = isl_union_pw_multi_aff_copy(ancestor->pma);
3365 upma = isl_union_pw_multi_aff_intersect_domain(upma, domain);
3366 r = extract_int(upma, data);
3367 isl_union_pw_multi_aff_free(upma);
3369 if (r < 0)
3370 data->r = -1;
3371 if (r < 0 || !r)
3372 return 0;
3374 return isl_int_cmp(data->c2, data->c1);
3377 /* Compare "a" and "b" based on the parent schedule of their parent.
3379 static int cmp_band(const void *a, const void *b, void *user)
3381 isl_band *b1 = *(isl_band * const *) a;
3382 isl_band *b2 = *(isl_band * const *) b;
3383 struct isl_cmp_band_data *data = user;
3385 return cmp_band_in_ancestor(b1, b2, data, b1->parent);
3388 /* Sort the elements in "list" based on the partial schedules of its parent
3389 * (and ancestors). In particular if the parent assigns constant values
3390 * to the domains of the bands in "list", then the elements are sorted
3391 * according to that order.
3392 * This order should be a more "natural" order for the user, but otherwise
3393 * shouldn't have any effect.
3394 * If we would be constructing an isl_band forest directly in
3395 * isl_union_set_compute_schedule then there wouldn't be any need
3396 * for a reordering, since the children would be added to the list
3397 * in their natural order automatically.
3399 * If there is only one element in the list, then there is no need to sort
3400 * anything.
3401 * If the partial schedule of the parent has more than one member
3402 * (or if there is no parent), then it's
3403 * defnitely not assigning constant values to the different children in
3404 * the list and so we wouldn't be able to use it to sort the list.
3406 static __isl_give isl_band_list *sort_band_list(__isl_take isl_band_list *list,
3407 __isl_keep isl_band *parent)
3409 struct isl_cmp_band_data data;
3411 if (!list)
3412 return NULL;
3413 if (list->n <= 1)
3414 return list;
3415 if (!parent || parent->n != 1)
3416 return list;
3418 data.r = 0;
3419 isl_int_init(data.c1);
3420 isl_int_init(data.c2);
3421 isl_int_init(data.t);
3422 isl_sort(list->p, list->n, sizeof(list->p[0]), &cmp_band, &data);
3423 if (data.r < 0)
3424 list = isl_band_list_free(list);
3425 isl_int_clear(data.c1);
3426 isl_int_clear(data.c2);
3427 isl_int_clear(data.t);
3429 return list;
3432 /* Construct a list of bands that start at the same position (with
3433 * sequence number band_nr) in the schedules of the nodes that
3434 * were active in the parent band.
3436 * A separate isl_band structure is created for each band_id
3437 * and for each node that does not have a band with sequence
3438 * number band_nr. In the latter case, a band without members
3439 * is created.
3440 * This ensures that if a band has any children, then each node
3441 * that was active in the band is active in exactly one of the children.
3443 static __isl_give isl_band_list *construct_band_list(
3444 __isl_keep isl_schedule *schedule, __isl_keep isl_band *parent,
3445 int band_nr, int *parent_active, int n_active)
3447 int i, j;
3448 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
3449 int *active;
3450 int n_band;
3451 isl_band_list *list;
3453 n_band = 0;
3454 for (i = 0; i < n_active; ++i) {
3455 for (j = 0; j < schedule->n; ++j) {
3456 if (!parent_active[j])
3457 continue;
3458 if (schedule->node[j].n_band <= band_nr)
3459 continue;
3460 if (schedule->node[j].band_id[band_nr] == i) {
3461 n_band++;
3462 break;
3466 for (j = 0; j < schedule->n; ++j)
3467 if (schedule->node[j].n_band <= band_nr)
3468 n_band++;
3470 if (n_band == 1) {
3471 isl_band *band;
3472 list = isl_band_list_alloc(ctx, n_band);
3473 band = construct_band(schedule, parent, band_nr,
3474 parent_active, n_active);
3475 return isl_band_list_add(list, band);
3478 active = isl_alloc_array(ctx, int, schedule->n);
3479 if (schedule->n && !active)
3480 return NULL;
3482 list = isl_band_list_alloc(ctx, n_band);
3484 for (i = 0; i < n_active; ++i) {
3485 int n = 0;
3486 isl_band *band;
3488 for (j = 0; j < schedule->n; ++j) {
3489 active[j] = parent_active[j] &&
3490 schedule->node[j].n_band > band_nr &&
3491 schedule->node[j].band_id[band_nr] == i;
3492 if (active[j])
3493 n++;
3495 if (n == 0)
3496 continue;
3498 band = construct_band(schedule, parent, band_nr, active, n);
3500 list = isl_band_list_add(list, band);
3502 for (i = 0; i < schedule->n; ++i) {
3503 isl_band *band;
3504 if (!parent_active[i])
3505 continue;
3506 if (schedule->node[i].n_band > band_nr)
3507 continue;
3508 for (j = 0; j < schedule->n; ++j)
3509 active[j] = j == i;
3510 band = construct_band(schedule, parent, band_nr, active, 1);
3511 list = isl_band_list_add(list, band);
3514 free(active);
3516 list = sort_band_list(list, parent);
3518 return list;
3521 /* Construct a band forest representation of the schedule and
3522 * return the list of roots.
3524 static __isl_give isl_band_list *construct_forest(
3525 __isl_keep isl_schedule *schedule)
3527 int i;
3528 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
3529 isl_band_list *forest;
3530 int *active;
3532 active = isl_alloc_array(ctx, int, schedule->n);
3533 if (schedule->n && !active)
3534 return NULL;
3536 for (i = 0; i < schedule->n; ++i)
3537 active[i] = 1;
3539 forest = construct_band_list(schedule, NULL, 0, active, schedule->n);
3541 free(active);
3543 return forest;
3546 /* Return the roots of a band forest representation of the schedule.
3548 __isl_give isl_band_list *isl_schedule_get_band_forest(
3549 __isl_keep isl_schedule *schedule)
3551 if (!schedule)
3552 return NULL;
3553 if (!schedule->band_forest)
3554 schedule->band_forest = construct_forest(schedule);
3555 return isl_band_list_dup(schedule->band_forest);
3558 /* Call "fn" on each band in the schedule in depth-first post-order.
3560 int isl_schedule_foreach_band(__isl_keep isl_schedule *sched,
3561 int (*fn)(__isl_keep isl_band *band, void *user), void *user)
3563 int r;
3564 isl_band_list *forest;
3566 if (!sched)
3567 return -1;
3569 forest = isl_schedule_get_band_forest(sched);
3570 r = isl_band_list_foreach_band(forest, fn, user);
3571 isl_band_list_free(forest);
3573 return r;
3576 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
3577 __isl_keep isl_band_list *list);
3579 static __isl_give isl_printer *print_band(__isl_take isl_printer *p,
3580 __isl_keep isl_band *band)
3582 isl_band_list *children;
3584 p = isl_printer_start_line(p);
3585 p = isl_printer_print_union_pw_multi_aff(p, band->pma);
3586 p = isl_printer_end_line(p);
3588 if (!isl_band_has_children(band))
3589 return p;
3591 children = isl_band_get_children(band);
3593 p = isl_printer_indent(p, 4);
3594 p = print_band_list(p, children);
3595 p = isl_printer_indent(p, -4);
3597 isl_band_list_free(children);
3599 return p;
3602 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
3603 __isl_keep isl_band_list *list)
3605 int i, n;
3607 n = isl_band_list_n_band(list);
3608 for (i = 0; i < n; ++i) {
3609 isl_band *band;
3610 band = isl_band_list_get_band(list, i);
3611 p = print_band(p, band);
3612 isl_band_free(band);
3615 return p;
3618 __isl_give isl_printer *isl_printer_print_schedule(__isl_take isl_printer *p,
3619 __isl_keep isl_schedule *schedule)
3621 isl_band_list *forest;
3623 forest = isl_schedule_get_band_forest(schedule);
3625 p = print_band_list(p, forest);
3627 isl_band_list_free(forest);
3629 return p;
3632 void isl_schedule_dump(__isl_keep isl_schedule *schedule)
3634 isl_printer *printer;
3636 if (!schedule)
3637 return;
3639 printer = isl_printer_to_file(isl_schedule_get_ctx(schedule), stderr);
3640 printer = isl_printer_print_schedule(printer, schedule);
3642 isl_printer_free(printer);