MinGW-w64 build fix (lacks ffs declaration)
[isl.git] / isl_schedule.c
blob7403692ba970aebb944e40d94f7e26ff195175d6
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 * start is the first variable in the LP problem in the sequences that
52 * represents the schedule coefficients of this node
53 * nvar is the dimension of the domain
54 * nparam is the number of parameters or 0 if we are not constructing
55 * a parametric schedule
57 * scc is the index of SCC (or WCC) this node belongs to
59 * band contains the band index for each of the rows of the schedule.
60 * band_id is used to differentiate between separate bands at the same
61 * level within the same parent band, i.e., bands that are separated
62 * by the parent band or bands that are independent of each other.
63 * zero contains a boolean for each of the rows of the schedule,
64 * indicating whether the corresponding scheduling dimension results
65 * in zero dependence distances within its band and with respect
66 * to the proximity edges.
68 struct isl_sched_node {
69 isl_space *dim;
70 isl_mat *sched;
71 isl_map *sched_map;
72 int rank;
73 isl_mat *cmap;
74 int start;
75 int nvar;
76 int nparam;
78 int scc;
80 int *band;
81 int *band_id;
82 int *zero;
85 static int node_has_dim(const void *entry, const void *val)
87 struct isl_sched_node *node = (struct isl_sched_node *)entry;
88 isl_space *dim = (isl_space *)val;
90 return isl_space_is_equal(node->dim, dim);
93 /* An edge in the dependence graph. An edge may be used to
94 * ensure validity of the generated schedule, to minimize the dependence
95 * distance or both
97 * map is the dependence relation
98 * src is the source node
99 * dst is the sink node
100 * validity is set if the edge is used to ensure correctness
101 * proximity is set if the edge is used to minimize dependence distances
103 * For validity edges, start and end mark the sequence of inequality
104 * constraints in the LP problem that encode the validity constraint
105 * corresponding to this edge.
107 struct isl_sched_edge {
108 isl_map *map;
110 struct isl_sched_node *src;
111 struct isl_sched_node *dst;
113 int validity;
114 int proximity;
116 int start;
117 int end;
120 enum isl_edge_type {
121 isl_edge_validity = 0,
122 isl_edge_first = isl_edge_validity,
123 isl_edge_proximity,
124 isl_edge_last = isl_edge_proximity
127 /* Internal information about the dependence graph used during
128 * the construction of the schedule.
130 * intra_hmap is a cache, mapping dependence relations to their dual,
131 * for dependences from a node to itself
132 * inter_hmap is a cache, mapping dependence relations to their dual,
133 * for dependences between distinct nodes
135 * n is the number of nodes
136 * node is the list of nodes
137 * maxvar is the maximal number of variables over all nodes
138 * max_row is the allocated number of rows in the schedule
139 * n_row is the current (maximal) number of linearly independent
140 * rows in the node schedules
141 * n_total_row is the current number of rows in the node schedules
142 * n_band is the current number of completed bands
143 * band_start is the starting row in the node schedules of the current band
144 * root is set if this graph is the original dependence graph,
145 * without any splitting
147 * sorted contains a list of node indices sorted according to the
148 * SCC to which a node belongs
150 * n_edge is the number of edges
151 * edge is the list of edges
152 * max_edge contains the maximal number of edges of each type;
153 * in particular, it contains the number of edges in the inital graph.
154 * edge_table contains pointers into the edge array, hashed on the source
155 * and sink spaces; there is one such table for each type;
156 * a given edge may be referenced from more than one table
157 * if the corresponding relation appears in more than of the
158 * sets of dependences
160 * node_table contains pointers into the node array, hashed on the space
162 * region contains a list of variable sequences that should be non-trivial
164 * lp contains the (I)LP problem used to obtain new schedule rows
166 * src_scc and dst_scc are the source and sink SCCs of an edge with
167 * conflicting constraints
169 * scc represents the number of components
171 struct isl_sched_graph {
172 isl_hmap_map_basic_set *intra_hmap;
173 isl_hmap_map_basic_set *inter_hmap;
175 struct isl_sched_node *node;
176 int n;
177 int maxvar;
178 int max_row;
179 int n_row;
181 int *sorted;
183 int n_band;
184 int n_total_row;
185 int band_start;
187 int root;
189 struct isl_sched_edge *edge;
190 int n_edge;
191 int max_edge[isl_edge_last + 1];
192 struct isl_hash_table *edge_table[isl_edge_last + 1];
194 struct isl_hash_table *node_table;
195 struct isl_region *region;
197 isl_basic_set *lp;
199 int src_scc;
200 int dst_scc;
202 int scc;
205 /* Initialize node_table based on the list of nodes.
207 static int graph_init_table(isl_ctx *ctx, struct isl_sched_graph *graph)
209 int i;
211 graph->node_table = isl_hash_table_alloc(ctx, graph->n);
212 if (!graph->node_table)
213 return -1;
215 for (i = 0; i < graph->n; ++i) {
216 struct isl_hash_table_entry *entry;
217 uint32_t hash;
219 hash = isl_space_get_hash(graph->node[i].dim);
220 entry = isl_hash_table_find(ctx, graph->node_table, hash,
221 &node_has_dim,
222 graph->node[i].dim, 1);
223 if (!entry)
224 return -1;
225 entry->data = &graph->node[i];
228 return 0;
231 /* Return a pointer to the node that lives within the given space,
232 * or NULL if there is no such node.
234 static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
235 struct isl_sched_graph *graph, __isl_keep isl_space *dim)
237 struct isl_hash_table_entry *entry;
238 uint32_t hash;
240 hash = isl_space_get_hash(dim);
241 entry = isl_hash_table_find(ctx, graph->node_table, hash,
242 &node_has_dim, dim, 0);
244 return entry ? entry->data : NULL;
247 static int edge_has_src_and_dst(const void *entry, const void *val)
249 const struct isl_sched_edge *edge = entry;
250 const struct isl_sched_edge *temp = val;
252 return edge->src == temp->src && edge->dst == temp->dst;
255 /* Add the given edge to graph->edge_table[type].
257 static int graph_edge_table_add(isl_ctx *ctx, struct isl_sched_graph *graph,
258 enum isl_edge_type type, struct isl_sched_edge *edge)
260 struct isl_hash_table_entry *entry;
261 uint32_t hash;
263 hash = isl_hash_init();
264 hash = isl_hash_builtin(hash, edge->src);
265 hash = isl_hash_builtin(hash, edge->dst);
266 entry = isl_hash_table_find(ctx, graph->edge_table[type], hash,
267 &edge_has_src_and_dst, edge, 1);
268 if (!entry)
269 return -1;
270 entry->data = edge;
272 return 0;
275 /* Allocate the edge_tables based on the maximal number of edges of
276 * each type.
278 static int graph_init_edge_tables(isl_ctx *ctx, struct isl_sched_graph *graph)
280 int i;
282 for (i = 0; i <= isl_edge_last; ++i) {
283 graph->edge_table[i] = isl_hash_table_alloc(ctx,
284 graph->max_edge[i]);
285 if (!graph->edge_table[i])
286 return -1;
289 return 0;
292 /* If graph->edge_table[type] contains an edge from the given source
293 * to the given destination, then return the hash table entry of this edge.
294 * Otherwise, return NULL.
296 static struct isl_hash_table_entry *graph_find_edge_entry(
297 struct isl_sched_graph *graph,
298 enum isl_edge_type type,
299 struct isl_sched_node *src, struct isl_sched_node *dst)
301 isl_ctx *ctx = isl_space_get_ctx(src->dim);
302 uint32_t hash;
303 struct isl_sched_edge temp = { .src = src, .dst = dst };
305 hash = isl_hash_init();
306 hash = isl_hash_builtin(hash, temp.src);
307 hash = isl_hash_builtin(hash, temp.dst);
308 return isl_hash_table_find(ctx, graph->edge_table[type], hash,
309 &edge_has_src_and_dst, &temp, 0);
313 /* If graph->edge_table[type] contains an edge from the given source
314 * to the given destination, then return this edge.
315 * Otherwise, return NULL.
317 static struct isl_sched_edge *graph_find_edge(struct isl_sched_graph *graph,
318 enum isl_edge_type type,
319 struct isl_sched_node *src, struct isl_sched_node *dst)
321 struct isl_hash_table_entry *entry;
323 entry = graph_find_edge_entry(graph, type, src, dst);
324 if (!entry)
325 return NULL;
327 return entry->data;
330 /* Check whether the dependence graph has an edge of the given type
331 * between the given two nodes.
333 static int graph_has_edge(struct isl_sched_graph *graph,
334 enum isl_edge_type type,
335 struct isl_sched_node *src, struct isl_sched_node *dst)
337 struct isl_sched_edge *edge;
338 int empty;
340 edge = graph_find_edge(graph, type, src, dst);
341 if (!edge)
342 return 0;
344 empty = isl_map_plain_is_empty(edge->map);
345 if (empty < 0)
346 return -1;
348 return !empty;
351 /* If there is an edge from the given source to the given destination
352 * of any type then return this edge.
353 * Otherwise, return NULL.
355 static struct isl_sched_edge *graph_find_any_edge(struct isl_sched_graph *graph,
356 struct isl_sched_node *src, struct isl_sched_node *dst)
358 enum isl_edge_type i;
359 struct isl_sched_edge *edge;
361 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
362 edge = graph_find_edge(graph, i, src, dst);
363 if (edge)
364 return edge;
367 return NULL;
370 /* Remove the given edge from all the edge_tables that refer to it.
372 static void graph_remove_edge(struct isl_sched_graph *graph,
373 struct isl_sched_edge *edge)
375 isl_ctx *ctx = isl_map_get_ctx(edge->map);
376 enum isl_edge_type i;
378 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
379 struct isl_hash_table_entry *entry;
381 entry = graph_find_edge_entry(graph, i, edge->src, edge->dst);
382 if (!entry)
383 continue;
384 if (entry->data != edge)
385 continue;
386 isl_hash_table_remove(ctx, graph->edge_table[i], entry);
390 /* Check whether the dependence graph has any edge
391 * between the given two nodes.
393 static int graph_has_any_edge(struct isl_sched_graph *graph,
394 struct isl_sched_node *src, struct isl_sched_node *dst)
396 enum isl_edge_type i;
397 int r;
399 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
400 r = graph_has_edge(graph, i, src, dst);
401 if (r < 0 || r)
402 return r;
405 return r;
408 /* Check whether the dependence graph has a validity edge
409 * between the given two nodes.
411 static int graph_has_validity_edge(struct isl_sched_graph *graph,
412 struct isl_sched_node *src, struct isl_sched_node *dst)
414 return graph_has_edge(graph, isl_edge_validity, src, dst);
417 static int graph_alloc(isl_ctx *ctx, struct isl_sched_graph *graph,
418 int n_node, int n_edge)
420 int i;
422 graph->n = n_node;
423 graph->n_edge = n_edge;
424 graph->node = isl_calloc_array(ctx, struct isl_sched_node, graph->n);
425 graph->sorted = isl_calloc_array(ctx, int, graph->n);
426 graph->region = isl_alloc_array(ctx, struct isl_region, graph->n);
427 graph->edge = isl_calloc_array(ctx,
428 struct isl_sched_edge, graph->n_edge);
430 graph->intra_hmap = isl_hmap_map_basic_set_alloc(ctx, 2 * n_edge);
431 graph->inter_hmap = isl_hmap_map_basic_set_alloc(ctx, 2 * n_edge);
433 if (!graph->node || !graph->region || (graph->n_edge && !graph->edge) ||
434 !graph->sorted)
435 return -1;
437 for(i = 0; i < graph->n; ++i)
438 graph->sorted[i] = i;
440 return 0;
443 static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
445 int i;
447 isl_hmap_map_basic_set_free(ctx, graph->intra_hmap);
448 isl_hmap_map_basic_set_free(ctx, graph->inter_hmap);
450 for (i = 0; i < graph->n; ++i) {
451 isl_space_free(graph->node[i].dim);
452 isl_mat_free(graph->node[i].sched);
453 isl_map_free(graph->node[i].sched_map);
454 isl_mat_free(graph->node[i].cmap);
455 if (graph->root) {
456 free(graph->node[i].band);
457 free(graph->node[i].band_id);
458 free(graph->node[i].zero);
461 free(graph->node);
462 free(graph->sorted);
463 for (i = 0; i < graph->n_edge; ++i)
464 isl_map_free(graph->edge[i].map);
465 free(graph->edge);
466 free(graph->region);
467 for (i = 0; i <= isl_edge_last; ++i)
468 isl_hash_table_free(ctx, graph->edge_table[i]);
469 isl_hash_table_free(ctx, graph->node_table);
470 isl_basic_set_free(graph->lp);
473 /* For each "set" on which this function is called, increment
474 * graph->n by one and update graph->maxvar.
476 static int init_n_maxvar(__isl_take isl_set *set, void *user)
478 struct isl_sched_graph *graph = user;
479 int nvar = isl_set_dim(set, isl_dim_set);
481 graph->n++;
482 if (nvar > graph->maxvar)
483 graph->maxvar = nvar;
485 isl_set_free(set);
487 return 0;
490 /* Compute the number of rows that should be allocated for the schedule.
491 * The graph can be split at most "n - 1" times, there can be at most
492 * two rows for each dimension in the iteration domains (in particular,
493 * we usually have one row, but it may be split by split_scaled),
494 * and there can be one extra row for ordering the statements.
495 * Note that if we have actually split "n - 1" times, then no ordering
496 * is needed, so in principle we could use "graph->n + 2 * graph->maxvar - 1".
498 static int compute_max_row(struct isl_sched_graph *graph,
499 __isl_keep isl_union_set *domain)
501 graph->n = 0;
502 graph->maxvar = 0;
503 if (isl_union_set_foreach_set(domain, &init_n_maxvar, graph) < 0)
504 return -1;
505 graph->max_row = graph->n + 2 * graph->maxvar;
507 return 0;
510 /* Add a new node to the graph representing the given set.
512 static int extract_node(__isl_take isl_set *set, void *user)
514 int nvar, nparam;
515 isl_ctx *ctx;
516 isl_space *dim;
517 isl_mat *sched;
518 struct isl_sched_graph *graph = user;
519 int *band, *band_id, *zero;
521 ctx = isl_set_get_ctx(set);
522 dim = isl_set_get_space(set);
523 isl_set_free(set);
524 nvar = isl_space_dim(dim, isl_dim_set);
525 nparam = isl_space_dim(dim, isl_dim_param);
526 if (!ctx->opt->schedule_parametric)
527 nparam = 0;
528 sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
529 graph->node[graph->n].dim = dim;
530 graph->node[graph->n].nvar = nvar;
531 graph->node[graph->n].nparam = nparam;
532 graph->node[graph->n].sched = sched;
533 graph->node[graph->n].sched_map = NULL;
534 band = isl_alloc_array(ctx, int, graph->max_row);
535 graph->node[graph->n].band = band;
536 band_id = isl_calloc_array(ctx, int, graph->max_row);
537 graph->node[graph->n].band_id = band_id;
538 zero = isl_calloc_array(ctx, int, graph->max_row);
539 graph->node[graph->n].zero = zero;
540 graph->n++;
542 if (!sched || (graph->max_row && (!band || !band_id || !zero)))
543 return -1;
545 return 0;
548 struct isl_extract_edge_data {
549 enum isl_edge_type type;
550 struct isl_sched_graph *graph;
553 /* Add a new edge to the graph based on the given map
554 * and add it to data->graph->edge_table[data->type].
555 * If a dependence relation of a given type happens to be identical
556 * to one of the dependence relations of a type that was added before,
557 * then we don't create a new edge, but instead mark the original edge
558 * as also representing a dependence of the current type.
560 static int extract_edge(__isl_take isl_map *map, void *user)
562 isl_ctx *ctx = isl_map_get_ctx(map);
563 struct isl_extract_edge_data *data = user;
564 struct isl_sched_graph *graph = data->graph;
565 struct isl_sched_node *src, *dst;
566 isl_space *dim;
567 struct isl_sched_edge *edge;
568 int is_equal;
570 dim = isl_space_domain(isl_map_get_space(map));
571 src = graph_find_node(ctx, graph, dim);
572 isl_space_free(dim);
573 dim = isl_space_range(isl_map_get_space(map));
574 dst = graph_find_node(ctx, graph, dim);
575 isl_space_free(dim);
577 if (!src || !dst) {
578 isl_map_free(map);
579 return 0;
582 graph->edge[graph->n_edge].src = src;
583 graph->edge[graph->n_edge].dst = dst;
584 graph->edge[graph->n_edge].map = map;
585 if (data->type == isl_edge_validity) {
586 graph->edge[graph->n_edge].validity = 1;
587 graph->edge[graph->n_edge].proximity = 0;
589 if (data->type == isl_edge_proximity) {
590 graph->edge[graph->n_edge].validity = 0;
591 graph->edge[graph->n_edge].proximity = 1;
593 graph->n_edge++;
595 edge = graph_find_any_edge(graph, src, dst);
596 if (!edge)
597 return graph_edge_table_add(ctx, graph, data->type,
598 &graph->edge[graph->n_edge - 1]);
599 is_equal = isl_map_plain_is_equal(map, edge->map);
600 if (is_equal < 0)
601 return -1;
602 if (!is_equal)
603 return graph_edge_table_add(ctx, graph, data->type,
604 &graph->edge[graph->n_edge - 1]);
606 graph->n_edge--;
607 edge->validity |= graph->edge[graph->n_edge].validity;
608 edge->proximity |= graph->edge[graph->n_edge].proximity;
609 isl_map_free(map);
611 return graph_edge_table_add(ctx, graph, data->type, edge);
614 /* Check whether there is any dependence from node[j] to node[i]
615 * or from node[i] to node[j].
617 static int node_follows_weak(int i, int j, void *user)
619 int f;
620 struct isl_sched_graph *graph = user;
622 f = graph_has_any_edge(graph, &graph->node[j], &graph->node[i]);
623 if (f < 0 || f)
624 return f;
625 return graph_has_any_edge(graph, &graph->node[i], &graph->node[j]);
628 /* Check whether there is a validity dependence from node[j] to node[i],
629 * forcing node[i] to follow node[j].
631 static int node_follows_strong(int i, int j, void *user)
633 struct isl_sched_graph *graph = user;
635 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
638 /* Use Tarjan's algorithm for computing the strongly connected components
639 * in the dependence graph (only validity edges).
640 * If weak is set, we consider the graph to be undirected and
641 * we effectively compute the (weakly) connected components.
642 * Additionally, we also consider other edges when weak is set.
644 static int detect_ccs(isl_ctx *ctx, struct isl_sched_graph *graph, int weak)
646 int i, n;
647 struct isl_tarjan_graph *g = NULL;
649 g = isl_tarjan_graph_init(ctx, graph->n,
650 weak ? &node_follows_weak : &node_follows_strong, graph);
651 if (!g)
652 return -1;
654 graph->scc = 0;
655 i = 0;
656 n = graph->n;
657 while (n) {
658 while (g->order[i] != -1) {
659 graph->node[g->order[i]].scc = graph->scc;
660 --n;
661 ++i;
663 ++i;
664 graph->scc++;
667 isl_tarjan_graph_free(g);
669 return 0;
672 /* Apply Tarjan's algorithm to detect the strongly connected components
673 * in the dependence graph.
675 static int detect_sccs(isl_ctx *ctx, struct isl_sched_graph *graph)
677 return detect_ccs(ctx, graph, 0);
680 /* Apply Tarjan's algorithm to detect the (weakly) connected components
681 * in the dependence graph.
683 static int detect_wccs(isl_ctx *ctx, struct isl_sched_graph *graph)
685 return detect_ccs(ctx, graph, 1);
688 static int cmp_scc(const void *a, const void *b, void *data)
690 struct isl_sched_graph *graph = data;
691 const int *i1 = a;
692 const int *i2 = b;
694 return graph->node[*i1].scc - graph->node[*i2].scc;
697 /* Sort the elements of graph->sorted according to the corresponding SCCs.
699 static int sort_sccs(struct isl_sched_graph *graph)
701 return isl_sort(graph->sorted, graph->n, sizeof(int), &cmp_scc, graph);
704 /* Given a dependence relation R from a node to itself,
705 * construct the set of coefficients of valid constraints for elements
706 * in that dependence relation.
707 * In particular, the result contains tuples of coefficients
708 * c_0, c_n, c_x such that
710 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
712 * or, equivalently,
714 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
716 * We choose here to compute the dual of delta R.
717 * Alternatively, we could have computed the dual of R, resulting
718 * in a set of tuples c_0, c_n, c_x, c_y, and then
719 * plugged in (c_0, c_n, c_x, -c_x).
721 static __isl_give isl_basic_set *intra_coefficients(
722 struct isl_sched_graph *graph, __isl_take isl_map *map)
724 isl_ctx *ctx = isl_map_get_ctx(map);
725 isl_set *delta;
726 isl_basic_set *coef;
728 if (isl_hmap_map_basic_set_has(ctx, graph->intra_hmap, map))
729 return isl_hmap_map_basic_set_get(ctx, graph->intra_hmap, map);
731 delta = isl_set_remove_divs(isl_map_deltas(isl_map_copy(map)));
732 coef = isl_set_coefficients(delta);
733 isl_hmap_map_basic_set_set(ctx, graph->intra_hmap, map,
734 isl_basic_set_copy(coef));
736 return coef;
739 /* Given a dependence relation R, * construct the set of coefficients
740 * of valid constraints for elements in that dependence relation.
741 * In particular, the result contains tuples of coefficients
742 * c_0, c_n, c_x, c_y such that
744 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
747 static __isl_give isl_basic_set *inter_coefficients(
748 struct isl_sched_graph *graph, __isl_take isl_map *map)
750 isl_ctx *ctx = isl_map_get_ctx(map);
751 isl_set *set;
752 isl_basic_set *coef;
754 if (isl_hmap_map_basic_set_has(ctx, graph->inter_hmap, map))
755 return isl_hmap_map_basic_set_get(ctx, graph->inter_hmap, map);
757 set = isl_map_wrap(isl_map_remove_divs(isl_map_copy(map)));
758 coef = isl_set_coefficients(set);
759 isl_hmap_map_basic_set_set(ctx, graph->inter_hmap, map,
760 isl_basic_set_copy(coef));
762 return coef;
765 /* Add constraints to graph->lp that force validity for the given
766 * dependence from a node i to itself.
767 * That is, add constraints that enforce
769 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
770 * = c_i_x (y - x) >= 0
772 * for each (x,y) in R.
773 * We obtain general constraints on coefficients (c_0, c_n, c_x)
774 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
775 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
776 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
778 * Actually, we do not construct constraints for the c_i_x themselves,
779 * but for the coefficients of c_i_x written as a linear combination
780 * of the columns in node->cmap.
782 static int add_intra_validity_constraints(struct isl_sched_graph *graph,
783 struct isl_sched_edge *edge)
785 unsigned total;
786 isl_map *map = isl_map_copy(edge->map);
787 isl_ctx *ctx = isl_map_get_ctx(map);
788 isl_space *dim;
789 isl_dim_map *dim_map;
790 isl_basic_set *coef;
791 struct isl_sched_node *node = edge->src;
793 coef = intra_coefficients(graph, map);
795 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
797 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
798 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
799 if (!coef)
800 goto error;
802 total = isl_basic_set_total_dim(graph->lp);
803 dim_map = isl_dim_map_alloc(ctx, total);
804 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
805 isl_space_dim(dim, isl_dim_set), 1,
806 node->nvar, -1);
807 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
808 isl_space_dim(dim, isl_dim_set), 1,
809 node->nvar, 1);
810 graph->lp = isl_basic_set_extend_constraints(graph->lp,
811 coef->n_eq, coef->n_ineq);
812 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
813 coef, dim_map);
814 isl_space_free(dim);
816 return 0;
817 error:
818 isl_space_free(dim);
819 return -1;
822 /* Add constraints to graph->lp that force validity for the given
823 * dependence from node i to node j.
824 * That is, add constraints that enforce
826 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
828 * for each (x,y) in R.
829 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
830 * of valid constraints for R and then plug in
831 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
832 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
833 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
834 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
836 * Actually, we do not construct constraints for the c_*_x themselves,
837 * but for the coefficients of c_*_x written as a linear combination
838 * of the columns in node->cmap.
840 static int add_inter_validity_constraints(struct isl_sched_graph *graph,
841 struct isl_sched_edge *edge)
843 unsigned total;
844 isl_map *map = isl_map_copy(edge->map);
845 isl_ctx *ctx = isl_map_get_ctx(map);
846 isl_space *dim;
847 isl_dim_map *dim_map;
848 isl_basic_set *coef;
849 struct isl_sched_node *src = edge->src;
850 struct isl_sched_node *dst = edge->dst;
852 coef = inter_coefficients(graph, map);
854 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
856 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
857 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
858 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
859 isl_space_dim(dim, isl_dim_set) + src->nvar,
860 isl_mat_copy(dst->cmap));
861 if (!coef)
862 goto error;
864 total = isl_basic_set_total_dim(graph->lp);
865 dim_map = isl_dim_map_alloc(ctx, total);
867 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
868 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
869 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
870 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
871 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
872 dst->nvar, -1);
873 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
874 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
875 dst->nvar, 1);
877 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
878 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
879 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
880 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
881 isl_space_dim(dim, isl_dim_set), 1,
882 src->nvar, 1);
883 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
884 isl_space_dim(dim, isl_dim_set), 1,
885 src->nvar, -1);
887 edge->start = graph->lp->n_ineq;
888 graph->lp = isl_basic_set_extend_constraints(graph->lp,
889 coef->n_eq, coef->n_ineq);
890 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
891 coef, dim_map);
892 if (!graph->lp)
893 goto error;
894 isl_space_free(dim);
895 edge->end = graph->lp->n_ineq;
897 return 0;
898 error:
899 isl_space_free(dim);
900 return -1;
903 /* Add constraints to graph->lp that bound the dependence distance for the given
904 * dependence from a node i to itself.
905 * If s = 1, we add the constraint
907 * c_i_x (y - x) <= m_0 + m_n n
909 * or
911 * -c_i_x (y - x) + m_0 + m_n n >= 0
913 * for each (x,y) in R.
914 * If s = -1, we add the constraint
916 * -c_i_x (y - x) <= m_0 + m_n n
918 * or
920 * c_i_x (y - x) + m_0 + m_n n >= 0
922 * for each (x,y) in R.
923 * We obtain general constraints on coefficients (c_0, c_n, c_x)
924 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
925 * with each coefficient (except m_0) represented as a pair of non-negative
926 * coefficients.
928 * Actually, we do not construct constraints for the c_i_x themselves,
929 * but for the coefficients of c_i_x written as a linear combination
930 * of the columns in node->cmap.
932 static int add_intra_proximity_constraints(struct isl_sched_graph *graph,
933 struct isl_sched_edge *edge, int s)
935 unsigned total;
936 unsigned nparam;
937 isl_map *map = isl_map_copy(edge->map);
938 isl_ctx *ctx = isl_map_get_ctx(map);
939 isl_space *dim;
940 isl_dim_map *dim_map;
941 isl_basic_set *coef;
942 struct isl_sched_node *node = edge->src;
944 coef = intra_coefficients(graph, map);
946 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
948 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
949 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
950 if (!coef)
951 goto error;
953 nparam = isl_space_dim(node->dim, isl_dim_param);
954 total = isl_basic_set_total_dim(graph->lp);
955 dim_map = isl_dim_map_alloc(ctx, total);
956 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
957 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
958 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
959 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
960 isl_space_dim(dim, isl_dim_set), 1,
961 node->nvar, s);
962 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
963 isl_space_dim(dim, isl_dim_set), 1,
964 node->nvar, -s);
965 graph->lp = isl_basic_set_extend_constraints(graph->lp,
966 coef->n_eq, coef->n_ineq);
967 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
968 coef, dim_map);
969 isl_space_free(dim);
971 return 0;
972 error:
973 isl_space_free(dim);
974 return -1;
977 /* Add constraints to graph->lp that bound the dependence distance for the given
978 * dependence from node i to node j.
979 * If s = 1, we add the constraint
981 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
982 * <= m_0 + m_n n
984 * or
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 >= 0
989 * for each (x,y) in R.
990 * If s = -1, we add the constraint
992 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
993 * <= m_0 + m_n n
995 * or
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 >= 0
1000 * for each (x,y) in R.
1001 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1002 * of valid constraints for R and then plug in
1003 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1004 * -s*c_j_x+s*c_i_x)
1005 * with each coefficient (except m_0, c_j_0 and c_i_0)
1006 * represented as a pair of non-negative coefficients.
1008 * Actually, we do not construct constraints for the c_*_x themselves,
1009 * but for the coefficients of c_*_x written as a linear combination
1010 * of the columns in node->cmap.
1012 static int add_inter_proximity_constraints(struct isl_sched_graph *graph,
1013 struct isl_sched_edge *edge, int s)
1015 unsigned total;
1016 unsigned nparam;
1017 isl_map *map = isl_map_copy(edge->map);
1018 isl_ctx *ctx = isl_map_get_ctx(map);
1019 isl_space *dim;
1020 isl_dim_map *dim_map;
1021 isl_basic_set *coef;
1022 struct isl_sched_node *src = edge->src;
1023 struct isl_sched_node *dst = edge->dst;
1025 coef = inter_coefficients(graph, map);
1027 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1029 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1030 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1031 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1032 isl_space_dim(dim, isl_dim_set) + src->nvar,
1033 isl_mat_copy(dst->cmap));
1034 if (!coef)
1035 goto error;
1037 nparam = isl_space_dim(src->dim, isl_dim_param);
1038 total = isl_basic_set_total_dim(graph->lp);
1039 dim_map = isl_dim_map_alloc(ctx, total);
1041 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1042 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1043 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1045 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, -s);
1046 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, s);
1047 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, -s);
1048 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1049 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1050 dst->nvar, s);
1051 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1052 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1053 dst->nvar, -s);
1055 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, s);
1056 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, -s);
1057 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, s);
1058 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1059 isl_space_dim(dim, isl_dim_set), 1,
1060 src->nvar, -s);
1061 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1062 isl_space_dim(dim, isl_dim_set), 1,
1063 src->nvar, s);
1065 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1066 coef->n_eq, coef->n_ineq);
1067 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1068 coef, dim_map);
1069 isl_space_free(dim);
1071 return 0;
1072 error:
1073 isl_space_free(dim);
1074 return -1;
1077 static int add_all_validity_constraints(struct isl_sched_graph *graph)
1079 int i;
1081 for (i = 0; i < graph->n_edge; ++i) {
1082 struct isl_sched_edge *edge= &graph->edge[i];
1083 if (!edge->validity)
1084 continue;
1085 if (edge->src != edge->dst)
1086 continue;
1087 if (add_intra_validity_constraints(graph, edge) < 0)
1088 return -1;
1091 for (i = 0; i < graph->n_edge; ++i) {
1092 struct isl_sched_edge *edge = &graph->edge[i];
1093 if (!edge->validity)
1094 continue;
1095 if (edge->src == edge->dst)
1096 continue;
1097 if (add_inter_validity_constraints(graph, edge) < 0)
1098 return -1;
1101 return 0;
1104 /* Add constraints to graph->lp that bound the dependence distance
1105 * for all dependence relations.
1106 * If a given proximity dependence is identical to a validity
1107 * dependence, then the dependence distance is already bounded
1108 * from below (by zero), so we only need to bound the distance
1109 * from above.
1110 * Otherwise, we need to bound the distance both from above and from below.
1112 static int add_all_proximity_constraints(struct isl_sched_graph *graph)
1114 int i;
1116 for (i = 0; i < graph->n_edge; ++i) {
1117 struct isl_sched_edge *edge= &graph->edge[i];
1118 if (!edge->proximity)
1119 continue;
1120 if (edge->src == edge->dst &&
1121 add_intra_proximity_constraints(graph, edge, 1) < 0)
1122 return -1;
1123 if (edge->src != edge->dst &&
1124 add_inter_proximity_constraints(graph, edge, 1) < 0)
1125 return -1;
1126 if (edge->validity)
1127 continue;
1128 if (edge->src == edge->dst &&
1129 add_intra_proximity_constraints(graph, edge, -1) < 0)
1130 return -1;
1131 if (edge->src != edge->dst &&
1132 add_inter_proximity_constraints(graph, edge, -1) < 0)
1133 return -1;
1136 return 0;
1139 /* Compute a basis for the rows in the linear part of the schedule
1140 * and extend this basis to a full basis. The remaining rows
1141 * can then be used to force linear independence from the rows
1142 * in the schedule.
1144 * In particular, given the schedule rows S, we compute
1146 * S = H Q
1148 * with H the Hermite normal form of S. That is, all but the
1149 * first rank columns of Q are zero and so each row in S is
1150 * a linear combination of the first rank rows of Q.
1151 * The matrix Q is then transposed because we will write the
1152 * coefficients of the next schedule row as a column vector s
1153 * and express this s as a linear combination s = Q c of the
1154 * computed basis.
1156 static int node_update_cmap(struct isl_sched_node *node)
1158 isl_mat *H, *Q;
1159 int n_row = isl_mat_rows(node->sched);
1161 H = isl_mat_sub_alloc(node->sched, 0, n_row,
1162 1 + node->nparam, node->nvar);
1164 H = isl_mat_left_hermite(H, 0, NULL, &Q);
1165 isl_mat_free(node->cmap);
1166 node->cmap = isl_mat_transpose(Q);
1167 node->rank = isl_mat_initial_non_zero_cols(H);
1168 isl_mat_free(H);
1170 if (!node->cmap || node->rank < 0)
1171 return -1;
1172 return 0;
1175 /* Count the number of equality and inequality constraints
1176 * that will be added for the given map.
1177 * If carry is set, then we are counting the number of (validity)
1178 * constraints that will be added in setup_carry_lp and we count
1179 * each edge exactly once. Otherwise, we count as follows
1180 * validity -> 1 (>= 0)
1181 * validity+proximity -> 2 (>= 0 and upper bound)
1182 * proximity -> 2 (lower and upper bound)
1184 static int count_map_constraints(struct isl_sched_graph *graph,
1185 struct isl_sched_edge *edge, __isl_take isl_map *map,
1186 int *n_eq, int *n_ineq, int carry)
1188 isl_basic_set *coef;
1189 int f = carry ? 1 : edge->proximity ? 2 : 1;
1191 if (carry && !edge->validity) {
1192 isl_map_free(map);
1193 return 0;
1196 if (edge->src == edge->dst)
1197 coef = intra_coefficients(graph, map);
1198 else
1199 coef = inter_coefficients(graph, map);
1200 if (!coef)
1201 return -1;
1202 *n_eq += f * coef->n_eq;
1203 *n_ineq += f * coef->n_ineq;
1204 isl_basic_set_free(coef);
1206 return 0;
1209 /* Count the number of equality and inequality constraints
1210 * that will be added to the main lp problem.
1211 * We count as follows
1212 * validity -> 1 (>= 0)
1213 * validity+proximity -> 2 (>= 0 and upper bound)
1214 * proximity -> 2 (lower and upper bound)
1216 static int count_constraints(struct isl_sched_graph *graph,
1217 int *n_eq, int *n_ineq)
1219 int i;
1221 *n_eq = *n_ineq = 0;
1222 for (i = 0; i < graph->n_edge; ++i) {
1223 struct isl_sched_edge *edge= &graph->edge[i];
1224 isl_map *map = isl_map_copy(edge->map);
1226 if (count_map_constraints(graph, edge, map,
1227 n_eq, n_ineq, 0) < 0)
1228 return -1;
1231 return 0;
1234 /* Add constraints that bound the values of the variable and parameter
1235 * coefficients of the schedule.
1237 * The maximal value of the coefficients is defined by the option
1238 * 'schedule_max_coefficient'.
1240 static int add_bound_coefficient_constraints(isl_ctx *ctx,
1241 struct isl_sched_graph *graph)
1243 int i, j, k;
1244 int max_coefficient;
1245 int total;
1247 max_coefficient = ctx->opt->schedule_max_coefficient;
1249 if (max_coefficient == -1)
1250 return 0;
1252 total = isl_basic_set_total_dim(graph->lp);
1254 for (i = 0; i < graph->n; ++i) {
1255 struct isl_sched_node *node = &graph->node[i];
1256 for (j = 0; j < 2 * node->nparam + 2 * node->nvar; ++j) {
1257 int dim;
1258 k = isl_basic_set_alloc_inequality(graph->lp);
1259 if (k < 0)
1260 return -1;
1261 dim = 1 + node->start + 1 + j;
1262 isl_seq_clr(graph->lp->ineq[k], 1 + total);
1263 isl_int_set_si(graph->lp->ineq[k][dim], -1);
1264 isl_int_set_si(graph->lp->ineq[k][0], max_coefficient);
1268 return 0;
1271 /* Construct an ILP problem for finding schedule coefficients
1272 * that result in non-negative, but small dependence distances
1273 * over all dependences.
1274 * In particular, the dependence distances over proximity edges
1275 * are bounded by m_0 + m_n n and we compute schedule coefficients
1276 * with small values (preferably zero) of m_n and m_0.
1278 * All variables of the ILP are non-negative. The actual coefficients
1279 * may be negative, so each coefficient is represented as the difference
1280 * of two non-negative variables. The negative part always appears
1281 * immediately before the positive part.
1282 * Other than that, the variables have the following order
1284 * - sum of positive and negative parts of m_n coefficients
1285 * - m_0
1286 * - sum of positive and negative parts of all c_n coefficients
1287 * (unconstrained when computing non-parametric schedules)
1288 * - sum of positive and negative parts of all c_x coefficients
1289 * - positive and negative parts of m_n coefficients
1290 * - for each node
1291 * - c_i_0
1292 * - positive and negative parts of c_i_n (if parametric)
1293 * - positive and negative parts of c_i_x
1295 * The c_i_x are not represented directly, but through the columns of
1296 * node->cmap. That is, the computed values are for variable t_i_x
1297 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
1299 * The constraints are those from the edges plus two or three equalities
1300 * to express the sums.
1302 * If force_zero is set, then we add equalities to ensure that
1303 * the sum of the m_n coefficients and m_0 are both zero.
1305 static int setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
1306 int force_zero)
1308 int i, j;
1309 int k;
1310 unsigned nparam;
1311 unsigned total;
1312 isl_space *dim;
1313 int parametric;
1314 int param_pos;
1315 int n_eq, n_ineq;
1316 int max_constant_term;
1317 int max_coefficient;
1319 max_constant_term = ctx->opt->schedule_max_constant_term;
1320 max_coefficient = ctx->opt->schedule_max_coefficient;
1322 parametric = ctx->opt->schedule_parametric;
1323 nparam = isl_space_dim(graph->node[0].dim, isl_dim_param);
1324 param_pos = 4;
1325 total = param_pos + 2 * nparam;
1326 for (i = 0; i < graph->n; ++i) {
1327 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
1328 if (node_update_cmap(node) < 0)
1329 return -1;
1330 node->start = total;
1331 total += 1 + 2 * (node->nparam + node->nvar);
1334 if (count_constraints(graph, &n_eq, &n_ineq) < 0)
1335 return -1;
1337 dim = isl_space_set_alloc(ctx, 0, total);
1338 isl_basic_set_free(graph->lp);
1339 n_eq += 2 + parametric + force_zero;
1340 if (max_constant_term != -1)
1341 n_ineq += graph->n;
1342 if (max_coefficient != -1)
1343 for (i = 0; i < graph->n; ++i)
1344 n_ineq += 2 * graph->node[i].nparam +
1345 2 * graph->node[i].nvar;
1347 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
1349 k = isl_basic_set_alloc_equality(graph->lp);
1350 if (k < 0)
1351 return -1;
1352 isl_seq_clr(graph->lp->eq[k], 1 + total);
1353 if (!force_zero)
1354 isl_int_set_si(graph->lp->eq[k][1], -1);
1355 for (i = 0; i < 2 * nparam; ++i)
1356 isl_int_set_si(graph->lp->eq[k][1 + param_pos + i], 1);
1358 if (force_zero) {
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 isl_int_set_si(graph->lp->eq[k][2], -1);
1366 if (parametric) {
1367 k = isl_basic_set_alloc_equality(graph->lp);
1368 if (k < 0)
1369 return -1;
1370 isl_seq_clr(graph->lp->eq[k], 1 + total);
1371 isl_int_set_si(graph->lp->eq[k][3], -1);
1372 for (i = 0; i < graph->n; ++i) {
1373 int pos = 1 + graph->node[i].start + 1;
1375 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
1376 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
1380 k = isl_basic_set_alloc_equality(graph->lp);
1381 if (k < 0)
1382 return -1;
1383 isl_seq_clr(graph->lp->eq[k], 1 + total);
1384 isl_int_set_si(graph->lp->eq[k][4], -1);
1385 for (i = 0; i < graph->n; ++i) {
1386 struct isl_sched_node *node = &graph->node[i];
1387 int pos = 1 + node->start + 1 + 2 * node->nparam;
1389 for (j = 0; j < 2 * node->nvar; ++j)
1390 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
1393 if (max_constant_term != -1)
1394 for (i = 0; i < graph->n; ++i) {
1395 struct isl_sched_node *node = &graph->node[i];
1396 k = isl_basic_set_alloc_inequality(graph->lp);
1397 if (k < 0)
1398 return -1;
1399 isl_seq_clr(graph->lp->ineq[k], 1 + total);
1400 isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
1401 isl_int_set_si(graph->lp->ineq[k][0], max_constant_term);
1404 if (add_bound_coefficient_constraints(ctx, graph) < 0)
1405 return -1;
1406 if (add_all_validity_constraints(graph) < 0)
1407 return -1;
1408 if (add_all_proximity_constraints(graph) < 0)
1409 return -1;
1411 return 0;
1414 /* Analyze the conflicting constraint found by
1415 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
1416 * constraint of one of the edges between distinct nodes, living, moreover
1417 * in distinct SCCs, then record the source and sink SCC as this may
1418 * be a good place to cut between SCCs.
1420 static int check_conflict(int con, void *user)
1422 int i;
1423 struct isl_sched_graph *graph = user;
1425 if (graph->src_scc >= 0)
1426 return 0;
1428 con -= graph->lp->n_eq;
1430 if (con >= graph->lp->n_ineq)
1431 return 0;
1433 for (i = 0; i < graph->n_edge; ++i) {
1434 if (!graph->edge[i].validity)
1435 continue;
1436 if (graph->edge[i].src == graph->edge[i].dst)
1437 continue;
1438 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
1439 continue;
1440 if (graph->edge[i].start > con)
1441 continue;
1442 if (graph->edge[i].end <= con)
1443 continue;
1444 graph->src_scc = graph->edge[i].src->scc;
1445 graph->dst_scc = graph->edge[i].dst->scc;
1448 return 0;
1451 /* Check whether the next schedule row of the given node needs to be
1452 * non-trivial. Lower-dimensional domains may have some trivial rows,
1453 * but as soon as the number of remaining required non-trivial rows
1454 * is as large as the number or remaining rows to be computed,
1455 * all remaining rows need to be non-trivial.
1457 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
1459 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
1462 /* Solve the ILP problem constructed in setup_lp.
1463 * For each node such that all the remaining rows of its schedule
1464 * need to be non-trivial, we construct a non-triviality region.
1465 * This region imposes that the next row is independent of previous rows.
1466 * In particular the coefficients c_i_x are represented by t_i_x
1467 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
1468 * its first columns span the rows of the previously computed part
1469 * of the schedule. The non-triviality region enforces that at least
1470 * one of the remaining components of t_i_x is non-zero, i.e.,
1471 * that the new schedule row depends on at least one of the remaining
1472 * columns of Q.
1474 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
1476 int i;
1477 isl_vec *sol;
1478 isl_basic_set *lp;
1480 for (i = 0; i < graph->n; ++i) {
1481 struct isl_sched_node *node = &graph->node[i];
1482 int skip = node->rank;
1483 graph->region[i].pos = node->start + 1 + 2*(node->nparam+skip);
1484 if (needs_row(graph, node))
1485 graph->region[i].len = 2 * (node->nvar - skip);
1486 else
1487 graph->region[i].len = 0;
1489 lp = isl_basic_set_copy(graph->lp);
1490 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
1491 graph->region, &check_conflict, graph);
1492 return sol;
1495 /* Update the schedules of all nodes based on the given solution
1496 * of the LP problem.
1497 * The new row is added to the current band.
1498 * All possibly negative coefficients are encoded as a difference
1499 * of two non-negative variables, so we need to perform the subtraction
1500 * here. Moreover, if use_cmap is set, then the solution does
1501 * not refer to the actual coefficients c_i_x, but instead to variables
1502 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
1503 * In this case, we then also need to perform this multiplication
1504 * to obtain the values of c_i_x.
1506 * If check_zero is set, then the first two coordinates of sol are
1507 * assumed to correspond to the dependence distance. If these two
1508 * coordinates are zero, then the corresponding scheduling dimension
1509 * is marked as being zero distance.
1511 static int update_schedule(struct isl_sched_graph *graph,
1512 __isl_take isl_vec *sol, int use_cmap, int check_zero)
1514 int i, j;
1515 int zero = 0;
1516 isl_vec *csol = NULL;
1518 if (!sol)
1519 goto error;
1520 if (sol->size == 0)
1521 isl_die(sol->ctx, isl_error_internal,
1522 "no solution found", goto error);
1523 if (graph->n_total_row >= graph->max_row)
1524 isl_die(sol->ctx, isl_error_internal,
1525 "too many schedule rows", goto error);
1527 if (check_zero)
1528 zero = isl_int_is_zero(sol->el[1]) &&
1529 isl_int_is_zero(sol->el[2]);
1531 for (i = 0; i < graph->n; ++i) {
1532 struct isl_sched_node *node = &graph->node[i];
1533 int pos = node->start;
1534 int row = isl_mat_rows(node->sched);
1536 isl_vec_free(csol);
1537 csol = isl_vec_alloc(sol->ctx, node->nvar);
1538 if (!csol)
1539 goto error;
1541 isl_map_free(node->sched_map);
1542 node->sched_map = NULL;
1543 node->sched = isl_mat_add_rows(node->sched, 1);
1544 if (!node->sched)
1545 goto error;
1546 node->sched = isl_mat_set_element(node->sched, row, 0,
1547 sol->el[1 + pos]);
1548 for (j = 0; j < node->nparam + node->nvar; ++j)
1549 isl_int_sub(sol->el[1 + pos + 1 + 2 * j + 1],
1550 sol->el[1 + pos + 1 + 2 * j + 1],
1551 sol->el[1 + pos + 1 + 2 * j]);
1552 for (j = 0; j < node->nparam; ++j)
1553 node->sched = isl_mat_set_element(node->sched,
1554 row, 1 + j, sol->el[1+pos+1+2*j+1]);
1555 for (j = 0; j < node->nvar; ++j)
1556 isl_int_set(csol->el[j],
1557 sol->el[1+pos+1+2*(node->nparam+j)+1]);
1558 if (use_cmap)
1559 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
1560 csol);
1561 if (!csol)
1562 goto error;
1563 for (j = 0; j < node->nvar; ++j)
1564 node->sched = isl_mat_set_element(node->sched,
1565 row, 1 + node->nparam + j, csol->el[j]);
1566 node->band[graph->n_total_row] = graph->n_band;
1567 node->zero[graph->n_total_row] = zero;
1569 isl_vec_free(sol);
1570 isl_vec_free(csol);
1572 graph->n_row++;
1573 graph->n_total_row++;
1575 return 0;
1576 error:
1577 isl_vec_free(sol);
1578 isl_vec_free(csol);
1579 return -1;
1582 /* Convert node->sched into a multi_aff and return this multi_aff.
1584 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
1585 struct isl_sched_node *node)
1587 int i, j;
1588 isl_space *space;
1589 isl_local_space *ls;
1590 isl_aff *aff;
1591 isl_multi_aff *ma;
1592 int nrow, ncol;
1593 isl_int v;
1595 nrow = isl_mat_rows(node->sched);
1596 ncol = isl_mat_cols(node->sched) - 1;
1597 space = isl_space_from_domain(isl_space_copy(node->dim));
1598 space = isl_space_add_dims(space, isl_dim_out, nrow);
1599 ma = isl_multi_aff_zero(space);
1600 ls = isl_local_space_from_space(isl_space_copy(node->dim));
1602 isl_int_init(v);
1604 for (i = 0; i < nrow; ++i) {
1605 aff = isl_aff_zero_on_domain(isl_local_space_copy(ls));
1606 isl_mat_get_element(node->sched, i, 0, &v);
1607 aff = isl_aff_set_constant(aff, v);
1608 for (j = 0; j < node->nparam; ++j) {
1609 isl_mat_get_element(node->sched, i, 1 + j, &v);
1610 aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
1612 for (j = 0; j < node->nvar; ++j) {
1613 isl_mat_get_element(node->sched,
1614 i, 1 + node->nparam + j, &v);
1615 aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
1617 ma = isl_multi_aff_set_aff(ma, i, aff);
1620 isl_int_clear(v);
1622 isl_local_space_free(ls);
1624 return ma;
1627 /* Convert node->sched into a map and return this map.
1629 * The result is cached in node->sched_map, which needs to be released
1630 * whenever node->sched is updated.
1632 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
1634 if (!node->sched_map) {
1635 isl_multi_aff *ma;
1637 ma = node_extract_schedule_multi_aff(node);
1638 node->sched_map = isl_map_from_multi_aff(ma);
1641 return isl_map_copy(node->sched_map);
1644 /* Update the given dependence relation based on the current schedule.
1645 * That is, intersect the dependence relation with a map expressing
1646 * that source and sink are executed within the same iteration of
1647 * the current schedule.
1648 * This is not the most efficient way, but this shouldn't be a critical
1649 * operation.
1651 static __isl_give isl_map *specialize(__isl_take isl_map *map,
1652 struct isl_sched_node *src, struct isl_sched_node *dst)
1654 isl_map *src_sched, *dst_sched, *id;
1656 src_sched = node_extract_schedule(src);
1657 dst_sched = node_extract_schedule(dst);
1658 id = isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
1659 return isl_map_intersect(map, id);
1662 /* Update the dependence relations of all edges based on the current schedule.
1663 * If a dependence is carried completely by the current schedule, then
1664 * it is removed from the edge_tables. It is kept in the list of edges
1665 * as otherwise all edge_tables would have to be recomputed.
1667 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
1669 int i;
1671 for (i = graph->n_edge - 1; i >= 0; --i) {
1672 struct isl_sched_edge *edge = &graph->edge[i];
1673 edge->map = specialize(edge->map, edge->src, edge->dst);
1674 if (!edge->map)
1675 return -1;
1677 if (isl_map_plain_is_empty(edge->map))
1678 graph_remove_edge(graph, edge);
1681 return 0;
1684 static void next_band(struct isl_sched_graph *graph)
1686 graph->band_start = graph->n_total_row;
1687 graph->n_band++;
1690 /* Topologically sort statements mapped to the same schedule iteration
1691 * and add a row to the schedule corresponding to this order.
1693 static int sort_statements(isl_ctx *ctx, struct isl_sched_graph *graph)
1695 int i, j;
1697 if (graph->n <= 1)
1698 return 0;
1700 if (update_edges(ctx, graph) < 0)
1701 return -1;
1703 if (graph->n_edge == 0)
1704 return 0;
1706 if (detect_sccs(ctx, graph) < 0)
1707 return -1;
1709 if (graph->n_total_row >= graph->max_row)
1710 isl_die(ctx, isl_error_internal,
1711 "too many schedule rows", return -1);
1713 for (i = 0; i < graph->n; ++i) {
1714 struct isl_sched_node *node = &graph->node[i];
1715 int row = isl_mat_rows(node->sched);
1716 int cols = isl_mat_cols(node->sched);
1718 isl_map_free(node->sched_map);
1719 node->sched_map = NULL;
1720 node->sched = isl_mat_add_rows(node->sched, 1);
1721 if (!node->sched)
1722 return -1;
1723 node->sched = isl_mat_set_element_si(node->sched, row, 0,
1724 node->scc);
1725 for (j = 1; j < cols; ++j)
1726 node->sched = isl_mat_set_element_si(node->sched,
1727 row, j, 0);
1728 node->band[graph->n_total_row] = graph->n_band;
1731 graph->n_total_row++;
1732 next_band(graph);
1734 return 0;
1737 /* Construct an isl_schedule based on the computed schedule stored
1738 * in graph and with parameters specified by dim.
1740 static __isl_give isl_schedule *extract_schedule(struct isl_sched_graph *graph,
1741 __isl_take isl_space *dim)
1743 int i;
1744 isl_ctx *ctx;
1745 isl_schedule *sched = NULL;
1747 if (!dim)
1748 return NULL;
1750 ctx = isl_space_get_ctx(dim);
1751 sched = isl_calloc(ctx, struct isl_schedule,
1752 sizeof(struct isl_schedule) +
1753 (graph->n - 1) * sizeof(struct isl_schedule_node));
1754 if (!sched)
1755 goto error;
1757 sched->ref = 1;
1758 sched->n = graph->n;
1759 sched->n_band = graph->n_band;
1760 sched->n_total_row = graph->n_total_row;
1762 for (i = 0; i < sched->n; ++i) {
1763 int r, b;
1764 int *band_end, *band_id, *zero;
1766 sched->node[i].sched =
1767 node_extract_schedule_multi_aff(&graph->node[i]);
1768 if (!sched->node[i].sched)
1769 goto error;
1771 sched->node[i].n_band = graph->n_band;
1772 if (graph->n_band == 0)
1773 continue;
1775 band_end = isl_alloc_array(ctx, int, graph->n_band);
1776 band_id = isl_alloc_array(ctx, int, graph->n_band);
1777 zero = isl_alloc_array(ctx, int, graph->n_total_row);
1778 sched->node[i].band_end = band_end;
1779 sched->node[i].band_id = band_id;
1780 sched->node[i].zero = zero;
1781 if (!band_end || !band_id || !zero)
1782 goto error;
1784 for (r = 0; r < graph->n_total_row; ++r)
1785 zero[r] = graph->node[i].zero[r];
1786 for (r = b = 0; r < graph->n_total_row; ++r) {
1787 if (graph->node[i].band[r] == b)
1788 continue;
1789 band_end[b++] = r;
1790 if (graph->node[i].band[r] == -1)
1791 break;
1793 if (r == graph->n_total_row)
1794 band_end[b++] = r;
1795 sched->node[i].n_band = b;
1796 for (--b; b >= 0; --b)
1797 band_id[b] = graph->node[i].band_id[b];
1800 sched->dim = dim;
1802 return sched;
1803 error:
1804 isl_space_free(dim);
1805 isl_schedule_free(sched);
1806 return NULL;
1809 /* Copy nodes that satisfy node_pred from the src dependence graph
1810 * to the dst dependence graph.
1812 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
1813 int (*node_pred)(struct isl_sched_node *node, int data), int data)
1815 int i;
1817 dst->n = 0;
1818 for (i = 0; i < src->n; ++i) {
1819 if (!node_pred(&src->node[i], data))
1820 continue;
1821 dst->node[dst->n].dim = isl_space_copy(src->node[i].dim);
1822 dst->node[dst->n].nvar = src->node[i].nvar;
1823 dst->node[dst->n].nparam = src->node[i].nparam;
1824 dst->node[dst->n].sched = isl_mat_copy(src->node[i].sched);
1825 dst->node[dst->n].sched_map =
1826 isl_map_copy(src->node[i].sched_map);
1827 dst->node[dst->n].band = src->node[i].band;
1828 dst->node[dst->n].band_id = src->node[i].band_id;
1829 dst->node[dst->n].zero = src->node[i].zero;
1830 dst->n++;
1833 return 0;
1836 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
1837 * to the dst dependence graph.
1838 * If the source or destination node of the edge is not in the destination
1839 * graph, then it must be a backward proximity edge and it should simply
1840 * be ignored.
1842 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
1843 struct isl_sched_graph *src,
1844 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
1846 int i;
1847 enum isl_edge_type t;
1849 dst->n_edge = 0;
1850 for (i = 0; i < src->n_edge; ++i) {
1851 struct isl_sched_edge *edge = &src->edge[i];
1852 isl_map *map;
1853 struct isl_sched_node *dst_src, *dst_dst;
1855 if (!edge_pred(edge, data))
1856 continue;
1858 if (isl_map_plain_is_empty(edge->map))
1859 continue;
1861 dst_src = graph_find_node(ctx, dst, edge->src->dim);
1862 dst_dst = graph_find_node(ctx, dst, edge->dst->dim);
1863 if (!dst_src || !dst_dst) {
1864 if (edge->validity)
1865 isl_die(ctx, isl_error_internal,
1866 "backward validity edge", return -1);
1867 continue;
1870 map = isl_map_copy(edge->map);
1872 dst->edge[dst->n_edge].src = dst_src;
1873 dst->edge[dst->n_edge].dst = dst_dst;
1874 dst->edge[dst->n_edge].map = map;
1875 dst->edge[dst->n_edge].validity = edge->validity;
1876 dst->edge[dst->n_edge].proximity = edge->proximity;
1877 dst->n_edge++;
1879 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
1880 if (edge !=
1881 graph_find_edge(src, t, edge->src, edge->dst))
1882 continue;
1883 if (graph_edge_table_add(ctx, dst, t,
1884 &dst->edge[dst->n_edge - 1]) < 0)
1885 return -1;
1889 return 0;
1892 /* Given a "src" dependence graph that contains the nodes from "dst"
1893 * that satisfy node_pred, copy the schedule computed in "src"
1894 * for those nodes back to "dst".
1896 static int copy_schedule(struct isl_sched_graph *dst,
1897 struct isl_sched_graph *src,
1898 int (*node_pred)(struct isl_sched_node *node, int data), int data)
1900 int i;
1902 src->n = 0;
1903 for (i = 0; i < dst->n; ++i) {
1904 if (!node_pred(&dst->node[i], data))
1905 continue;
1906 isl_mat_free(dst->node[i].sched);
1907 isl_map_free(dst->node[i].sched_map);
1908 dst->node[i].sched = isl_mat_copy(src->node[src->n].sched);
1909 dst->node[i].sched_map =
1910 isl_map_copy(src->node[src->n].sched_map);
1911 src->n++;
1914 dst->max_row = src->max_row;
1915 dst->n_total_row = src->n_total_row;
1916 dst->n_band = src->n_band;
1918 return 0;
1921 /* Compute the maximal number of variables over all nodes.
1922 * This is the maximal number of linearly independent schedule
1923 * rows that we need to compute.
1924 * Just in case we end up in a part of the dependence graph
1925 * with only lower-dimensional domains, we make sure we will
1926 * compute the required amount of extra linearly independent rows.
1928 static int compute_maxvar(struct isl_sched_graph *graph)
1930 int i;
1932 graph->maxvar = 0;
1933 for (i = 0; i < graph->n; ++i) {
1934 struct isl_sched_node *node = &graph->node[i];
1935 int nvar;
1937 if (node_update_cmap(node) < 0)
1938 return -1;
1939 nvar = node->nvar + graph->n_row - node->rank;
1940 if (nvar > graph->maxvar)
1941 graph->maxvar = nvar;
1944 return 0;
1947 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph);
1948 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph);
1950 /* Compute a schedule for a subgraph of "graph". In particular, for
1951 * the graph composed of nodes that satisfy node_pred and edges that
1952 * that satisfy edge_pred. The caller should precompute the number
1953 * of nodes and edges that satisfy these predicates and pass them along
1954 * as "n" and "n_edge".
1955 * If the subgraph is known to consist of a single component, then wcc should
1956 * be set and then we call compute_schedule_wcc on the constructed subgraph.
1957 * Otherwise, we call compute_schedule, which will check whether the subgraph
1958 * is connected.
1960 static int compute_sub_schedule(isl_ctx *ctx,
1961 struct isl_sched_graph *graph, int n, int n_edge,
1962 int (*node_pred)(struct isl_sched_node *node, int data),
1963 int (*edge_pred)(struct isl_sched_edge *edge, int data),
1964 int data, int wcc)
1966 struct isl_sched_graph split = { 0 };
1967 int t;
1969 if (graph_alloc(ctx, &split, n, n_edge) < 0)
1970 goto error;
1971 if (copy_nodes(&split, graph, node_pred, data) < 0)
1972 goto error;
1973 if (graph_init_table(ctx, &split) < 0)
1974 goto error;
1975 for (t = 0; t <= isl_edge_last; ++t)
1976 split.max_edge[t] = graph->max_edge[t];
1977 if (graph_init_edge_tables(ctx, &split) < 0)
1978 goto error;
1979 if (copy_edges(ctx, &split, graph, edge_pred, data) < 0)
1980 goto error;
1981 split.n_row = graph->n_row;
1982 split.max_row = graph->max_row;
1983 split.n_total_row = graph->n_total_row;
1984 split.n_band = graph->n_band;
1985 split.band_start = graph->band_start;
1987 if (wcc && compute_schedule_wcc(ctx, &split) < 0)
1988 goto error;
1989 if (!wcc && compute_schedule(ctx, &split) < 0)
1990 goto error;
1992 copy_schedule(graph, &split, node_pred, data);
1994 graph_free(ctx, &split);
1995 return 0;
1996 error:
1997 graph_free(ctx, &split);
1998 return -1;
2001 static int node_scc_exactly(struct isl_sched_node *node, int scc)
2003 return node->scc == scc;
2006 static int node_scc_at_most(struct isl_sched_node *node, int scc)
2008 return node->scc <= scc;
2011 static int node_scc_at_least(struct isl_sched_node *node, int scc)
2013 return node->scc >= scc;
2016 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
2018 return edge->src->scc == scc && edge->dst->scc == scc;
2021 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
2023 return edge->dst->scc <= scc;
2026 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
2028 return edge->src->scc >= scc;
2031 /* Pad the schedules of all nodes with zero rows such that in the end
2032 * they all have graph->n_total_row rows.
2033 * The extra rows don't belong to any band, so they get assigned band number -1.
2035 static int pad_schedule(struct isl_sched_graph *graph)
2037 int i, j;
2039 for (i = 0; i < graph->n; ++i) {
2040 struct isl_sched_node *node = &graph->node[i];
2041 int row = isl_mat_rows(node->sched);
2042 if (graph->n_total_row > row) {
2043 isl_map_free(node->sched_map);
2044 node->sched_map = NULL;
2046 node->sched = isl_mat_add_zero_rows(node->sched,
2047 graph->n_total_row - row);
2048 if (!node->sched)
2049 return -1;
2050 for (j = row; j < graph->n_total_row; ++j)
2051 node->band[j] = -1;
2054 return 0;
2057 /* Split the current graph into two parts and compute a schedule for each
2058 * part individually. In particular, one part consists of all SCCs up
2059 * to and including graph->src_scc, while the other part contains the other
2060 * SCCS.
2062 * The split is enforced in the schedule by constant rows with two different
2063 * values (0 and 1). These constant rows replace the previously computed rows
2064 * in the current band.
2065 * It would be possible to reuse them as the first rows in the next
2066 * band, but recomputing them may result in better rows as we are looking
2067 * at a smaller part of the dependence graph.
2068 * compute_split_schedule is only called when no zero-distance schedule row
2069 * could be found on the entire graph, so we wark the splitting row as
2070 * non zero-distance.
2072 * The band_id of the second group is set to n, where n is the number
2073 * of nodes in the first group. This ensures that the band_ids over
2074 * the two groups remain disjoint, even if either or both of the two
2075 * groups contain independent components.
2077 static int compute_split_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
2079 int i, j, n, e1, e2;
2080 int n_total_row, orig_total_row;
2081 int n_band, orig_band;
2082 int drop;
2084 if (graph->n_total_row >= graph->max_row)
2085 isl_die(ctx, isl_error_internal,
2086 "too many schedule rows", return -1);
2088 drop = graph->n_total_row - graph->band_start;
2089 graph->n_total_row -= drop;
2090 graph->n_row -= drop;
2092 n = 0;
2093 for (i = 0; i < graph->n; ++i) {
2094 struct isl_sched_node *node = &graph->node[i];
2095 int row = isl_mat_rows(node->sched) - drop;
2096 int cols = isl_mat_cols(node->sched);
2097 int before = node->scc <= graph->src_scc;
2099 if (before)
2100 n++;
2102 isl_map_free(node->sched_map);
2103 node->sched_map = NULL;
2104 node->sched = isl_mat_drop_rows(node->sched,
2105 graph->band_start, drop);
2106 node->sched = isl_mat_add_rows(node->sched, 1);
2107 if (!node->sched)
2108 return -1;
2109 node->sched = isl_mat_set_element_si(node->sched, row, 0,
2110 !before);
2111 for (j = 1; j < cols; ++j)
2112 node->sched = isl_mat_set_element_si(node->sched,
2113 row, j, 0);
2114 node->band[graph->n_total_row] = graph->n_band;
2115 node->zero[graph->n_total_row] = 0;
2118 e1 = e2 = 0;
2119 for (i = 0; i < graph->n_edge; ++i) {
2120 if (graph->edge[i].dst->scc <= graph->src_scc)
2121 e1++;
2122 if (graph->edge[i].src->scc > graph->src_scc)
2123 e2++;
2126 graph->n_total_row++;
2127 next_band(graph);
2129 for (i = 0; i < graph->n; ++i) {
2130 struct isl_sched_node *node = &graph->node[i];
2131 if (node->scc > graph->src_scc)
2132 node->band_id[graph->n_band] = n;
2135 orig_total_row = graph->n_total_row;
2136 orig_band = graph->n_band;
2137 if (compute_sub_schedule(ctx, graph, n, e1,
2138 &node_scc_at_most, &edge_dst_scc_at_most,
2139 graph->src_scc, 0) < 0)
2140 return -1;
2141 n_total_row = graph->n_total_row;
2142 graph->n_total_row = orig_total_row;
2143 n_band = graph->n_band;
2144 graph->n_band = orig_band;
2145 if (compute_sub_schedule(ctx, graph, graph->n - n, e2,
2146 &node_scc_at_least, &edge_src_scc_at_least,
2147 graph->src_scc + 1, 0) < 0)
2148 return -1;
2149 if (n_total_row > graph->n_total_row)
2150 graph->n_total_row = n_total_row;
2151 if (n_band > graph->n_band)
2152 graph->n_band = n_band;
2154 return pad_schedule(graph);
2157 /* Compute the next band of the schedule after updating the dependence
2158 * relations based on the the current schedule.
2160 static int compute_next_band(isl_ctx *ctx, struct isl_sched_graph *graph)
2162 if (update_edges(ctx, graph) < 0)
2163 return -1;
2164 next_band(graph);
2166 return compute_schedule(ctx, graph);
2169 /* Add constraints to graph->lp that force the dependence "map" (which
2170 * is part of the dependence relation of "edge")
2171 * to be respected and attempt to carry it, where the edge is one from
2172 * a node j to itself. "pos" is the sequence number of the given map.
2173 * That is, add constraints that enforce
2175 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
2176 * = c_j_x (y - x) >= e_i
2178 * for each (x,y) in R.
2179 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2180 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
2181 * with each coefficient in c_j_x represented as a pair of non-negative
2182 * coefficients.
2184 static int add_intra_constraints(struct isl_sched_graph *graph,
2185 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
2187 unsigned total;
2188 isl_ctx *ctx = isl_map_get_ctx(map);
2189 isl_space *dim;
2190 isl_dim_map *dim_map;
2191 isl_basic_set *coef;
2192 struct isl_sched_node *node = edge->src;
2194 coef = intra_coefficients(graph, map);
2195 if (!coef)
2196 return -1;
2198 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
2200 total = isl_basic_set_total_dim(graph->lp);
2201 dim_map = isl_dim_map_alloc(ctx, total);
2202 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
2203 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
2204 isl_space_dim(dim, isl_dim_set), 1,
2205 node->nvar, -1);
2206 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
2207 isl_space_dim(dim, isl_dim_set), 1,
2208 node->nvar, 1);
2209 graph->lp = isl_basic_set_extend_constraints(graph->lp,
2210 coef->n_eq, coef->n_ineq);
2211 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
2212 coef, dim_map);
2213 isl_space_free(dim);
2215 return 0;
2218 /* Add constraints to graph->lp that force the dependence "map" (which
2219 * is part of the dependence relation of "edge")
2220 * to be respected and attempt to carry it, where the edge is one from
2221 * node j to node k. "pos" is the sequence number of the given map.
2222 * That is, add constraints that enforce
2224 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
2226 * for each (x,y) in R.
2227 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2228 * of valid constraints for R and then plug in
2229 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
2230 * with each coefficient (except e_i, c_k_0 and c_j_0)
2231 * represented as a pair of non-negative coefficients.
2233 static int add_inter_constraints(struct isl_sched_graph *graph,
2234 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
2236 unsigned total;
2237 isl_ctx *ctx = isl_map_get_ctx(map);
2238 isl_space *dim;
2239 isl_dim_map *dim_map;
2240 isl_basic_set *coef;
2241 struct isl_sched_node *src = edge->src;
2242 struct isl_sched_node *dst = edge->dst;
2244 coef = inter_coefficients(graph, map);
2245 if (!coef)
2246 return -1;
2248 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
2250 total = isl_basic_set_total_dim(graph->lp);
2251 dim_map = isl_dim_map_alloc(ctx, total);
2253 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
2255 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
2256 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
2257 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
2258 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
2259 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
2260 dst->nvar, -1);
2261 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
2262 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
2263 dst->nvar, 1);
2265 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
2266 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
2267 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
2268 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
2269 isl_space_dim(dim, isl_dim_set), 1,
2270 src->nvar, 1);
2271 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
2272 isl_space_dim(dim, isl_dim_set), 1,
2273 src->nvar, -1);
2275 graph->lp = isl_basic_set_extend_constraints(graph->lp,
2276 coef->n_eq, coef->n_ineq);
2277 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
2278 coef, dim_map);
2279 isl_space_free(dim);
2281 return 0;
2284 /* Add constraints to graph->lp that force all validity dependences
2285 * to be respected and attempt to carry them.
2287 static int add_all_constraints(struct isl_sched_graph *graph)
2289 int i, j;
2290 int pos;
2292 pos = 0;
2293 for (i = 0; i < graph->n_edge; ++i) {
2294 struct isl_sched_edge *edge= &graph->edge[i];
2296 if (!edge->validity)
2297 continue;
2299 for (j = 0; j < edge->map->n; ++j) {
2300 isl_basic_map *bmap;
2301 isl_map *map;
2303 bmap = isl_basic_map_copy(edge->map->p[j]);
2304 map = isl_map_from_basic_map(bmap);
2306 if (edge->src == edge->dst &&
2307 add_intra_constraints(graph, edge, map, pos) < 0)
2308 return -1;
2309 if (edge->src != edge->dst &&
2310 add_inter_constraints(graph, edge, map, pos) < 0)
2311 return -1;
2312 ++pos;
2316 return 0;
2319 /* Count the number of equality and inequality constraints
2320 * that will be added to the carry_lp problem.
2321 * We count each edge exactly once.
2323 static int count_all_constraints(struct isl_sched_graph *graph,
2324 int *n_eq, int *n_ineq)
2326 int i, j;
2328 *n_eq = *n_ineq = 0;
2329 for (i = 0; i < graph->n_edge; ++i) {
2330 struct isl_sched_edge *edge= &graph->edge[i];
2331 for (j = 0; j < edge->map->n; ++j) {
2332 isl_basic_map *bmap;
2333 isl_map *map;
2335 bmap = isl_basic_map_copy(edge->map->p[j]);
2336 map = isl_map_from_basic_map(bmap);
2338 if (count_map_constraints(graph, edge, map,
2339 n_eq, n_ineq, 1) < 0)
2340 return -1;
2344 return 0;
2347 /* Construct an LP problem for finding schedule coefficients
2348 * such that the schedule carries as many dependences as possible.
2349 * In particular, for each dependence i, we bound the dependence distance
2350 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
2351 * of all e_i's. Dependence with e_i = 0 in the solution are simply
2352 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
2353 * Note that if the dependence relation is a union of basic maps,
2354 * then we have to consider each basic map individually as it may only
2355 * be possible to carry the dependences expressed by some of those
2356 * basic maps and not all off them.
2357 * Below, we consider each of those basic maps as a separate "edge".
2359 * All variables of the LP are non-negative. The actual coefficients
2360 * may be negative, so each coefficient is represented as the difference
2361 * of two non-negative variables. The negative part always appears
2362 * immediately before the positive part.
2363 * Other than that, the variables have the following order
2365 * - sum of (1 - e_i) over all edges
2366 * - sum of positive and negative parts of all c_n coefficients
2367 * (unconstrained when computing non-parametric schedules)
2368 * - sum of positive and negative parts of all c_x coefficients
2369 * - for each edge
2370 * - e_i
2371 * - for each node
2372 * - c_i_0
2373 * - positive and negative parts of c_i_n (if parametric)
2374 * - positive and negative parts of c_i_x
2376 * The constraints are those from the (validity) edges plus three equalities
2377 * to express the sums and n_edge inequalities to express e_i <= 1.
2379 static int setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
2381 int i, j;
2382 int k;
2383 isl_space *dim;
2384 unsigned total;
2385 int n_eq, n_ineq;
2386 int n_edge;
2388 n_edge = 0;
2389 for (i = 0; i < graph->n_edge; ++i)
2390 n_edge += graph->edge[i].map->n;
2392 total = 3 + n_edge;
2393 for (i = 0; i < graph->n; ++i) {
2394 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2395 node->start = total;
2396 total += 1 + 2 * (node->nparam + node->nvar);
2399 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
2400 return -1;
2402 dim = isl_space_set_alloc(ctx, 0, total);
2403 isl_basic_set_free(graph->lp);
2404 n_eq += 3;
2405 n_ineq += n_edge;
2406 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
2407 graph->lp = isl_basic_set_set_rational(graph->lp);
2409 k = isl_basic_set_alloc_equality(graph->lp);
2410 if (k < 0)
2411 return -1;
2412 isl_seq_clr(graph->lp->eq[k], 1 + total);
2413 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
2414 isl_int_set_si(graph->lp->eq[k][1], 1);
2415 for (i = 0; i < n_edge; ++i)
2416 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
2418 k = isl_basic_set_alloc_equality(graph->lp);
2419 if (k < 0)
2420 return -1;
2421 isl_seq_clr(graph->lp->eq[k], 1 + total);
2422 isl_int_set_si(graph->lp->eq[k][2], -1);
2423 for (i = 0; i < graph->n; ++i) {
2424 int pos = 1 + graph->node[i].start + 1;
2426 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
2427 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2430 k = isl_basic_set_alloc_equality(graph->lp);
2431 if (k < 0)
2432 return -1;
2433 isl_seq_clr(graph->lp->eq[k], 1 + total);
2434 isl_int_set_si(graph->lp->eq[k][3], -1);
2435 for (i = 0; i < graph->n; ++i) {
2436 struct isl_sched_node *node = &graph->node[i];
2437 int pos = 1 + node->start + 1 + 2 * node->nparam;
2439 for (j = 0; j < 2 * node->nvar; ++j)
2440 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2443 for (i = 0; i < n_edge; ++i) {
2444 k = isl_basic_set_alloc_inequality(graph->lp);
2445 if (k < 0)
2446 return -1;
2447 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2448 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
2449 isl_int_set_si(graph->lp->ineq[k][0], 1);
2452 if (add_all_constraints(graph) < 0)
2453 return -1;
2455 return 0;
2458 /* If the schedule_split_scaled option is set and if the linear
2459 * parts of the scheduling rows for all nodes in the graphs have
2460 * non-trivial common divisor, then split off the constant term
2461 * from the linear part.
2462 * The constant term is then placed in a separate band and
2463 * the linear part is reduced.
2465 static int split_scaled(isl_ctx *ctx, struct isl_sched_graph *graph)
2467 int i;
2468 int row;
2469 isl_int gcd, gcd_i;
2471 if (!ctx->opt->schedule_split_scaled)
2472 return 0;
2473 if (graph->n <= 1)
2474 return 0;
2476 if (graph->n_total_row >= graph->max_row)
2477 isl_die(ctx, isl_error_internal,
2478 "too many schedule rows", return -1);
2480 isl_int_init(gcd);
2481 isl_int_init(gcd_i);
2483 isl_int_set_si(gcd, 0);
2485 row = isl_mat_rows(graph->node[0].sched) - 1;
2487 for (i = 0; i < graph->n; ++i) {
2488 struct isl_sched_node *node = &graph->node[i];
2489 int cols = isl_mat_cols(node->sched);
2491 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
2492 isl_int_gcd(gcd, gcd, gcd_i);
2495 isl_int_clear(gcd_i);
2497 if (isl_int_cmp_si(gcd, 1) <= 0) {
2498 isl_int_clear(gcd);
2499 return 0;
2502 next_band(graph);
2504 for (i = 0; i < graph->n; ++i) {
2505 struct isl_sched_node *node = &graph->node[i];
2507 isl_map_free(node->sched_map);
2508 node->sched_map = NULL;
2509 node->sched = isl_mat_add_zero_rows(node->sched, 1);
2510 if (!node->sched)
2511 goto error;
2512 isl_int_fdiv_r(node->sched->row[row + 1][0],
2513 node->sched->row[row][0], gcd);
2514 isl_int_fdiv_q(node->sched->row[row][0],
2515 node->sched->row[row][0], gcd);
2516 isl_int_mul(node->sched->row[row][0],
2517 node->sched->row[row][0], gcd);
2518 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
2519 if (!node->sched)
2520 goto error;
2521 node->band[graph->n_total_row] = graph->n_band;
2524 graph->n_total_row++;
2526 isl_int_clear(gcd);
2527 return 0;
2528 error:
2529 isl_int_clear(gcd);
2530 return -1;
2533 static int compute_component_schedule(isl_ctx *ctx,
2534 struct isl_sched_graph *graph);
2536 /* Is the schedule row "sol" trivial on node "node"?
2537 * That is, is the solution zero on the dimensions orthogonal to
2538 * the previously found solutions?
2539 * Each coefficient is represented as the difference between
2540 * two non-negative values in "sol". The coefficient is then
2541 * zero if those two values are equal to each other.
2543 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
2545 int i;
2546 int pos;
2547 int len;
2549 pos = 1 + node->start + 1 + 2 * (node->nparam + node->rank);
2550 len = 2 * (node->nvar - node->rank);
2552 if (len == 0)
2553 return 0;
2555 for (i = 0; i < len; i += 2)
2556 if (isl_int_ne(sol->el[pos + i], sol->el[pos + i + 1]))
2557 return 0;
2559 return 1;
2562 /* Is the schedule row "sol" trivial on any node where it should
2563 * not be trivial?
2565 static int is_any_trivial(struct isl_sched_graph *graph,
2566 __isl_keep isl_vec *sol)
2568 int i;
2570 for (i = 0; i < graph->n; ++i) {
2571 struct isl_sched_node *node = &graph->node[i];
2573 if (!needs_row(graph, node))
2574 continue;
2575 if (is_trivial(node, sol))
2576 return 1;
2579 return 0;
2582 /* Construct a schedule row for each node such that as many dependences
2583 * as possible are carried and then continue with the next band.
2585 * If the computed schedule row turns out to be trivial on one or
2586 * more nodes where it should not be trivial, then we throw it away
2587 * and try again on each component separately.
2589 static int carry_dependences(isl_ctx *ctx, struct isl_sched_graph *graph)
2591 int i;
2592 int n_edge;
2593 isl_vec *sol;
2594 isl_basic_set *lp;
2596 n_edge = 0;
2597 for (i = 0; i < graph->n_edge; ++i)
2598 n_edge += graph->edge[i].map->n;
2600 if (setup_carry_lp(ctx, graph) < 0)
2601 return -1;
2603 lp = isl_basic_set_copy(graph->lp);
2604 sol = isl_tab_basic_set_non_neg_lexmin(lp);
2605 if (!sol)
2606 return -1;
2608 if (sol->size == 0) {
2609 isl_vec_free(sol);
2610 isl_die(ctx, isl_error_internal,
2611 "error in schedule construction", return -1);
2614 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
2615 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
2616 isl_vec_free(sol);
2617 isl_die(ctx, isl_error_unknown,
2618 "unable to carry dependences", return -1);
2621 if (is_any_trivial(graph, sol)) {
2622 isl_vec_free(sol);
2623 if (graph->scc > 1)
2624 return compute_component_schedule(ctx, graph);
2625 isl_die(ctx, isl_error_unknown,
2626 "unable to construct non-trivial solution", return -1);
2629 if (update_schedule(graph, sol, 0, 0) < 0)
2630 return -1;
2632 if (split_scaled(ctx, graph) < 0)
2633 return -1;
2635 return compute_next_band(ctx, graph);
2638 /* Are there any (non-empty) validity edges in the graph?
2640 static int has_validity_edges(struct isl_sched_graph *graph)
2642 int i;
2644 for (i = 0; i < graph->n_edge; ++i) {
2645 int empty;
2647 empty = isl_map_plain_is_empty(graph->edge[i].map);
2648 if (empty < 0)
2649 return -1;
2650 if (empty)
2651 continue;
2652 if (graph->edge[i].validity)
2653 return 1;
2656 return 0;
2659 /* Should we apply a Feautrier step?
2660 * That is, did the user request the Feautrier algorithm and are
2661 * there any validity dependences (left)?
2663 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
2665 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
2666 return 0;
2668 return has_validity_edges(graph);
2671 /* Compute a schedule for a connected dependence graph using Feautrier's
2672 * multi-dimensional scheduling algorithm.
2673 * The original algorithm is described in [1].
2674 * The main idea is to minimize the number of scheduling dimensions, by
2675 * trying to satisfy as many dependences as possible per scheduling dimension.
2677 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
2678 * Problem, Part II: Multi-Dimensional Time.
2679 * In Intl. Journal of Parallel Programming, 1992.
2681 static int compute_schedule_wcc_feautrier(isl_ctx *ctx,
2682 struct isl_sched_graph *graph)
2684 return carry_dependences(ctx, graph);
2687 /* Compute a schedule for a connected dependence graph.
2688 * We try to find a sequence of as many schedule rows as possible that result
2689 * in non-negative dependence distances (independent of the previous rows
2690 * in the sequence, i.e., such that the sequence is tilable).
2691 * If we can't find any more rows we either
2692 * - split between SCCs and start over (assuming we found an interesting
2693 * pair of SCCs between which to split)
2694 * - continue with the next band (assuming the current band has at least
2695 * one row)
2696 * - try to carry as many dependences as possible and continue with the next
2697 * band
2699 * If Feautrier's algorithm is selected, we first recursively try to satisfy
2700 * as many validity dependences as possible. When all validity dependences
2701 * are satisfied we extend the schedule to a full-dimensional schedule.
2703 * If we manage to complete the schedule, we finish off by topologically
2704 * sorting the statements based on the remaining dependences.
2706 * If ctx->opt->schedule_outer_zero_distance is set, then we force the
2707 * outermost dimension in the current band to be zero distance. If this
2708 * turns out to be impossible, we fall back on the general scheme above
2709 * and try to carry as many dependences as possible.
2711 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph)
2713 int force_zero = 0;
2715 if (detect_sccs(ctx, graph) < 0)
2716 return -1;
2717 if (sort_sccs(graph) < 0)
2718 return -1;
2720 if (compute_maxvar(graph) < 0)
2721 return -1;
2723 if (need_feautrier_step(ctx, graph))
2724 return compute_schedule_wcc_feautrier(ctx, graph);
2726 if (ctx->opt->schedule_outer_zero_distance)
2727 force_zero = 1;
2729 while (graph->n_row < graph->maxvar) {
2730 isl_vec *sol;
2732 graph->src_scc = -1;
2733 graph->dst_scc = -1;
2735 if (setup_lp(ctx, graph, force_zero) < 0)
2736 return -1;
2737 sol = solve_lp(graph);
2738 if (!sol)
2739 return -1;
2740 if (sol->size == 0) {
2741 isl_vec_free(sol);
2742 if (!ctx->opt->schedule_maximize_band_depth &&
2743 graph->n_total_row > graph->band_start)
2744 return compute_next_band(ctx, graph);
2745 if (graph->src_scc >= 0)
2746 return compute_split_schedule(ctx, graph);
2747 if (graph->n_total_row > graph->band_start)
2748 return compute_next_band(ctx, graph);
2749 return carry_dependences(ctx, graph);
2751 if (update_schedule(graph, sol, 1, 1) < 0)
2752 return -1;
2753 force_zero = 0;
2756 if (graph->n_total_row > graph->band_start)
2757 next_band(graph);
2758 return sort_statements(ctx, graph);
2761 /* Add a row to the schedules that separates the SCCs and move
2762 * to the next band.
2764 static int split_on_scc(isl_ctx *ctx, struct isl_sched_graph *graph)
2766 int i;
2768 if (graph->n_total_row >= graph->max_row)
2769 isl_die(ctx, isl_error_internal,
2770 "too many schedule rows", return -1);
2772 for (i = 0; i < graph->n; ++i) {
2773 struct isl_sched_node *node = &graph->node[i];
2774 int row = isl_mat_rows(node->sched);
2776 isl_map_free(node->sched_map);
2777 node->sched_map = NULL;
2778 node->sched = isl_mat_add_zero_rows(node->sched, 1);
2779 node->sched = isl_mat_set_element_si(node->sched, row, 0,
2780 node->scc);
2781 if (!node->sched)
2782 return -1;
2783 node->band[graph->n_total_row] = graph->n_band;
2786 graph->n_total_row++;
2787 next_band(graph);
2789 return 0;
2792 /* Compute a schedule for each component (identified by node->scc)
2793 * of the dependence graph separately and then combine the results.
2794 * Depending on the setting of schedule_fuse, a component may be
2795 * either weakly or strongly connected.
2797 * The band_id is adjusted such that each component has a separate id.
2798 * Note that the band_id may have already been set to a value different
2799 * from zero by compute_split_schedule.
2801 static int compute_component_schedule(isl_ctx *ctx,
2802 struct isl_sched_graph *graph)
2804 int wcc, i;
2805 int n, n_edge;
2806 int n_total_row, orig_total_row;
2807 int n_band, orig_band;
2809 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN ||
2810 ctx->opt->schedule_separate_components)
2811 if (split_on_scc(ctx, graph) < 0)
2812 return -1;
2814 n_total_row = 0;
2815 orig_total_row = graph->n_total_row;
2816 n_band = 0;
2817 orig_band = graph->n_band;
2818 for (i = 0; i < graph->n; ++i)
2819 graph->node[i].band_id[graph->n_band] += graph->node[i].scc;
2820 for (wcc = 0; wcc < graph->scc; ++wcc) {
2821 n = 0;
2822 for (i = 0; i < graph->n; ++i)
2823 if (graph->node[i].scc == wcc)
2824 n++;
2825 n_edge = 0;
2826 for (i = 0; i < graph->n_edge; ++i)
2827 if (graph->edge[i].src->scc == wcc &&
2828 graph->edge[i].dst->scc == wcc)
2829 n_edge++;
2831 if (compute_sub_schedule(ctx, graph, n, n_edge,
2832 &node_scc_exactly,
2833 &edge_scc_exactly, wcc, 1) < 0)
2834 return -1;
2835 if (graph->n_total_row > n_total_row)
2836 n_total_row = graph->n_total_row;
2837 graph->n_total_row = orig_total_row;
2838 if (graph->n_band > n_band)
2839 n_band = graph->n_band;
2840 graph->n_band = orig_band;
2843 graph->n_total_row = n_total_row;
2844 graph->n_band = n_band;
2846 return pad_schedule(graph);
2849 /* Compute a schedule for the given dependence graph.
2850 * We first check if the graph is connected (through validity dependences)
2851 * and, if not, compute a schedule for each component separately.
2852 * If schedule_fuse is set to minimal fusion, then we check for strongly
2853 * connected components instead and compute a separate schedule for
2854 * each such strongly connected component.
2856 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
2858 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN) {
2859 if (detect_sccs(ctx, graph) < 0)
2860 return -1;
2861 } else {
2862 if (detect_wccs(ctx, graph) < 0)
2863 return -1;
2866 if (graph->scc > 1)
2867 return compute_component_schedule(ctx, graph);
2869 return compute_schedule_wcc(ctx, graph);
2872 /* Compute a schedule for the given union of domains that respects
2873 * all the validity dependences.
2874 * If the default isl scheduling algorithm is used, it tries to minimize
2875 * the dependence distances over the proximity dependences.
2876 * If Feautrier's scheduling algorithm is used, the proximity dependence
2877 * distances are only minimized during the extension to a full-dimensional
2878 * schedule.
2880 __isl_give isl_schedule *isl_union_set_compute_schedule(
2881 __isl_take isl_union_set *domain,
2882 __isl_take isl_union_map *validity,
2883 __isl_take isl_union_map *proximity)
2885 isl_ctx *ctx = isl_union_set_get_ctx(domain);
2886 isl_space *dim;
2887 struct isl_sched_graph graph = { 0 };
2888 isl_schedule *sched;
2889 struct isl_extract_edge_data data;
2891 domain = isl_union_set_align_params(domain,
2892 isl_union_map_get_space(validity));
2893 domain = isl_union_set_align_params(domain,
2894 isl_union_map_get_space(proximity));
2895 dim = isl_union_set_get_space(domain);
2896 validity = isl_union_map_align_params(validity, isl_space_copy(dim));
2897 proximity = isl_union_map_align_params(proximity, dim);
2899 if (!domain)
2900 goto error;
2902 graph.n = isl_union_set_n_set(domain);
2903 if (graph.n == 0)
2904 goto empty;
2905 if (graph_alloc(ctx, &graph, graph.n,
2906 isl_union_map_n_map(validity) + isl_union_map_n_map(proximity)) < 0)
2907 goto error;
2908 if (compute_max_row(&graph, domain) < 0)
2909 goto error;
2910 graph.root = 1;
2911 graph.n = 0;
2912 if (isl_union_set_foreach_set(domain, &extract_node, &graph) < 0)
2913 goto error;
2914 if (graph_init_table(ctx, &graph) < 0)
2915 goto error;
2916 graph.max_edge[isl_edge_validity] = isl_union_map_n_map(validity);
2917 graph.max_edge[isl_edge_proximity] = isl_union_map_n_map(proximity);
2918 if (graph_init_edge_tables(ctx, &graph) < 0)
2919 goto error;
2920 graph.n_edge = 0;
2921 data.graph = &graph;
2922 data.type = isl_edge_validity;
2923 if (isl_union_map_foreach_map(validity, &extract_edge, &data) < 0)
2924 goto error;
2925 data.type = isl_edge_proximity;
2926 if (isl_union_map_foreach_map(proximity, &extract_edge, &data) < 0)
2927 goto error;
2929 if (compute_schedule(ctx, &graph) < 0)
2930 goto error;
2932 empty:
2933 sched = extract_schedule(&graph, isl_union_set_get_space(domain));
2935 graph_free(ctx, &graph);
2936 isl_union_set_free(domain);
2937 isl_union_map_free(validity);
2938 isl_union_map_free(proximity);
2940 return sched;
2941 error:
2942 graph_free(ctx, &graph);
2943 isl_union_set_free(domain);
2944 isl_union_map_free(validity);
2945 isl_union_map_free(proximity);
2946 return NULL;
2949 void *isl_schedule_free(__isl_take isl_schedule *sched)
2951 int i;
2952 if (!sched)
2953 return NULL;
2955 if (--sched->ref > 0)
2956 return NULL;
2958 for (i = 0; i < sched->n; ++i) {
2959 isl_multi_aff_free(sched->node[i].sched);
2960 free(sched->node[i].band_end);
2961 free(sched->node[i].band_id);
2962 free(sched->node[i].zero);
2964 isl_space_free(sched->dim);
2965 isl_band_list_free(sched->band_forest);
2966 free(sched);
2967 return NULL;
2970 isl_ctx *isl_schedule_get_ctx(__isl_keep isl_schedule *schedule)
2972 return schedule ? isl_space_get_ctx(schedule->dim) : NULL;
2975 /* Set max_out to the maximal number of output dimensions over
2976 * all maps.
2978 static int update_max_out(__isl_take isl_map *map, void *user)
2980 int *max_out = user;
2981 int n_out = isl_map_dim(map, isl_dim_out);
2983 if (n_out > *max_out)
2984 *max_out = n_out;
2986 isl_map_free(map);
2987 return 0;
2990 /* Internal data structure for map_pad_range.
2992 * "max_out" is the maximal schedule dimension.
2993 * "res" collects the results.
2995 struct isl_pad_schedule_map_data {
2996 int max_out;
2997 isl_union_map *res;
3000 /* Pad the range of the given map with zeros to data->max_out and
3001 * then add the result to data->res.
3003 static int map_pad_range(__isl_take isl_map *map, void *user)
3005 struct isl_pad_schedule_map_data *data = user;
3006 int i;
3007 int n_out = isl_map_dim(map, isl_dim_out);
3009 map = isl_map_add_dims(map, isl_dim_out, data->max_out - n_out);
3010 for (i = n_out; i < data->max_out; ++i)
3011 map = isl_map_fix_si(map, isl_dim_out, i, 0);
3013 data->res = isl_union_map_add_map(data->res, map);
3014 if (!data->res)
3015 return -1;
3017 return 0;
3020 /* Pad the ranges of the maps in the union map with zeros such they all have
3021 * the same dimension.
3023 static __isl_give isl_union_map *pad_schedule_map(
3024 __isl_take isl_union_map *umap)
3026 struct isl_pad_schedule_map_data data;
3028 if (!umap)
3029 return NULL;
3030 if (isl_union_map_n_map(umap) <= 1)
3031 return umap;
3033 data.max_out = 0;
3034 if (isl_union_map_foreach_map(umap, &update_max_out, &data.max_out) < 0)
3035 return isl_union_map_free(umap);
3037 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3038 if (isl_union_map_foreach_map(umap, &map_pad_range, &data) < 0)
3039 data.res = isl_union_map_free(data.res);
3041 isl_union_map_free(umap);
3042 return data.res;
3045 /* Return an isl_union_map of the schedule. If we have already constructed
3046 * a band forest, then this band forest may have been modified so we need
3047 * to extract the isl_union_map from the forest rather than from
3048 * the originally computed schedule. This reconstructed schedule map
3049 * then needs to be padded with zeros to unify the schedule space
3050 * since the result of isl_band_list_get_suffix_schedule may not have
3051 * a unified schedule space.
3053 __isl_give isl_union_map *isl_schedule_get_map(__isl_keep isl_schedule *sched)
3055 int i;
3056 isl_union_map *umap;
3058 if (!sched)
3059 return NULL;
3061 if (sched->band_forest) {
3062 umap = isl_band_list_get_suffix_schedule(sched->band_forest);
3063 return pad_schedule_map(umap);
3066 umap = isl_union_map_empty(isl_space_copy(sched->dim));
3067 for (i = 0; i < sched->n; ++i) {
3068 isl_multi_aff *ma;
3070 ma = isl_multi_aff_copy(sched->node[i].sched);
3071 umap = isl_union_map_add_map(umap, isl_map_from_multi_aff(ma));
3074 return umap;
3077 static __isl_give isl_band_list *construct_band_list(
3078 __isl_keep isl_schedule *schedule, __isl_keep isl_band *parent,
3079 int band_nr, int *parent_active, int n_active);
3081 /* Construct an isl_band structure for the band in the given schedule
3082 * with sequence number band_nr for the n_active nodes marked by active.
3083 * If the nodes don't have a band with the given sequence number,
3084 * then a band without members is created.
3086 * Because of the way the schedule is constructed, we know that
3087 * the position of the band inside the schedule of a node is the same
3088 * for all active nodes.
3090 * The partial schedule for the band is created before the children
3091 * are created to that construct_band_list can refer to the partial
3092 * schedule of the parent.
3094 static __isl_give isl_band *construct_band(__isl_keep isl_schedule *schedule,
3095 __isl_keep isl_band *parent,
3096 int band_nr, int *active, int n_active)
3098 int i, j;
3099 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
3100 isl_band *band;
3101 unsigned start, end;
3103 band = isl_band_alloc(ctx);
3104 if (!band)
3105 return NULL;
3107 band->schedule = schedule;
3108 band->parent = parent;
3110 for (i = 0; i < schedule->n; ++i)
3111 if (active[i])
3112 break;
3114 if (i >= schedule->n)
3115 isl_die(ctx, isl_error_internal,
3116 "band without active statements", goto error);
3118 start = band_nr ? schedule->node[i].band_end[band_nr - 1] : 0;
3119 end = band_nr < schedule->node[i].n_band ?
3120 schedule->node[i].band_end[band_nr] : start;
3121 band->n = end - start;
3123 band->zero = isl_alloc_array(ctx, int, band->n);
3124 if (band->n && !band->zero)
3125 goto error;
3127 for (j = 0; j < band->n; ++j)
3128 band->zero[j] = schedule->node[i].zero[start + j];
3130 band->pma = isl_union_pw_multi_aff_empty(isl_space_copy(schedule->dim));
3131 for (i = 0; i < schedule->n; ++i) {
3132 isl_multi_aff *ma;
3133 isl_pw_multi_aff *pma;
3134 unsigned n_out;
3136 if (!active[i])
3137 continue;
3139 ma = isl_multi_aff_copy(schedule->node[i].sched);
3140 n_out = isl_multi_aff_dim(ma, isl_dim_out);
3141 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, end, n_out - end);
3142 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, 0, start);
3143 pma = isl_pw_multi_aff_from_multi_aff(ma);
3144 band->pma = isl_union_pw_multi_aff_add_pw_multi_aff(band->pma,
3145 pma);
3147 if (!band->pma)
3148 goto error;
3150 for (i = 0; i < schedule->n; ++i)
3151 if (active[i] && schedule->node[i].n_band > band_nr + 1)
3152 break;
3154 if (i < schedule->n) {
3155 band->children = construct_band_list(schedule, band,
3156 band_nr + 1, active, n_active);
3157 if (!band->children)
3158 goto error;
3161 return band;
3162 error:
3163 isl_band_free(band);
3164 return NULL;
3167 /* Internal data structure used inside cmp_band and pw_multi_aff_extract_int.
3169 * r is set to a negative value if anything goes wrong.
3171 * c1 stores the result of extract_int.
3172 * c2 is a temporary value used inside cmp_band_in_ancestor.
3173 * t is a temporary value used inside extract_int.
3175 * first and equal are used inside extract_int.
3176 * first is set if we are looking at the first isl_multi_aff inside
3177 * the isl_union_pw_multi_aff.
3178 * equal is set if all the isl_multi_affs have been equal so far.
3180 struct isl_cmp_band_data {
3181 int r;
3183 int first;
3184 int equal;
3186 isl_int t;
3187 isl_int c1;
3188 isl_int c2;
3191 /* Check if "ma" assigns a constant value.
3192 * Note that this function is only called on isl_multi_affs
3193 * with a single output dimension.
3195 * If "ma" assigns a constant value then we compare it to data->c1
3196 * or assign it to data->c1 if this is the first isl_multi_aff we consider.
3197 * If "ma" does not assign a constant value or if it assigns a value
3198 * that is different from data->c1, then we set data->equal to zero
3199 * and terminate the check.
3201 static int multi_aff_extract_int(__isl_take isl_set *set,
3202 __isl_take isl_multi_aff *ma, void *user)
3204 isl_aff *aff;
3205 struct isl_cmp_band_data *data = user;
3207 aff = isl_multi_aff_get_aff(ma, 0);
3208 data->r = isl_aff_is_cst(aff);
3209 if (data->r >= 0 && data->r) {
3210 isl_aff_get_constant(aff, &data->t);
3211 if (data->first) {
3212 isl_int_set(data->c1, data->t);
3213 data->first = 0;
3214 } else if (!isl_int_eq(data->c1, data->t))
3215 data->equal = 0;
3216 } else if (data->r >= 0 && !data->r)
3217 data->equal = 0;
3219 isl_aff_free(aff);
3220 isl_set_free(set);
3221 isl_multi_aff_free(ma);
3223 if (data->r < 0)
3224 return -1;
3225 if (!data->equal)
3226 return -1;
3227 return 0;
3230 /* This function is called for each isl_pw_multi_aff in
3231 * the isl_union_pw_multi_aff checked by extract_int.
3232 * Check all the isl_multi_affs inside "pma".
3234 static int pw_multi_aff_extract_int(__isl_take isl_pw_multi_aff *pma,
3235 void *user)
3237 int r;
3239 r = isl_pw_multi_aff_foreach_piece(pma, &multi_aff_extract_int, user);
3240 isl_pw_multi_aff_free(pma);
3242 return r;
3245 /* Check if "upma" assigns a single constant value to its domain.
3246 * If so, return 1 and store the result in data->c1.
3247 * If not, return 0.
3249 * A negative return value from isl_union_pw_multi_aff_foreach_pw_multi_aff
3250 * means that either an error occurred or that we have broken off the check
3251 * because we already know the result is going to be negative.
3252 * In the latter case, data->equal is set to zero.
3254 static int extract_int(__isl_keep isl_union_pw_multi_aff *upma,
3255 struct isl_cmp_band_data *data)
3257 data->first = 1;
3258 data->equal = 1;
3260 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3261 &pw_multi_aff_extract_int, data) < 0) {
3262 if (!data->equal)
3263 return 0;
3264 return -1;
3267 return !data->first && data->equal;
3270 /* Compare "b1" and "b2" based on the parent schedule of their ancestor
3271 * "ancestor".
3273 * If the parent of "ancestor" also has a single member, then we
3274 * first try to compare the two band based on the partial schedule
3275 * of this parent.
3277 * Otherwise, or if the result is inconclusive, we look at the partial schedule
3278 * of "ancestor" itself.
3279 * In particular, we specialize the parent schedule based
3280 * on the domains of the child schedules, check if both assign
3281 * a single constant value and, if so, compare the two constant values.
3282 * If the specialized parent schedules do not assign a constant value,
3283 * then they cannot be used to order the two bands and so in this case
3284 * we return 0.
3286 static int cmp_band_in_ancestor(__isl_keep isl_band *b1,
3287 __isl_keep isl_band *b2, struct isl_cmp_band_data *data,
3288 __isl_keep isl_band *ancestor)
3290 isl_union_pw_multi_aff *upma;
3291 isl_union_set *domain;
3292 int r;
3294 if (data->r < 0)
3295 return 0;
3297 if (ancestor->parent && ancestor->parent->n == 1) {
3298 r = cmp_band_in_ancestor(b1, b2, data, ancestor->parent);
3299 if (data->r < 0)
3300 return 0;
3301 if (r)
3302 return r;
3305 upma = isl_union_pw_multi_aff_copy(b1->pma);
3306 domain = isl_union_pw_multi_aff_domain(upma);
3307 upma = isl_union_pw_multi_aff_copy(ancestor->pma);
3308 upma = isl_union_pw_multi_aff_intersect_domain(upma, domain);
3309 r = extract_int(upma, data);
3310 isl_union_pw_multi_aff_free(upma);
3312 if (r < 0)
3313 data->r = -1;
3314 if (r < 0 || !r)
3315 return 0;
3317 isl_int_set(data->c2, data->c1);
3319 upma = isl_union_pw_multi_aff_copy(b2->pma);
3320 domain = isl_union_pw_multi_aff_domain(upma);
3321 upma = isl_union_pw_multi_aff_copy(ancestor->pma);
3322 upma = isl_union_pw_multi_aff_intersect_domain(upma, domain);
3323 r = extract_int(upma, data);
3324 isl_union_pw_multi_aff_free(upma);
3326 if (r < 0)
3327 data->r = -1;
3328 if (r < 0 || !r)
3329 return 0;
3331 return isl_int_cmp(data->c2, data->c1);
3334 /* Compare "a" and "b" based on the parent schedule of their parent.
3336 static int cmp_band(const void *a, const void *b, void *user)
3338 isl_band *b1 = *(isl_band * const *) a;
3339 isl_band *b2 = *(isl_band * const *) b;
3340 struct isl_cmp_band_data *data = user;
3342 return cmp_band_in_ancestor(b1, b2, data, b1->parent);
3345 /* Sort the elements in "list" based on the partial schedules of its parent
3346 * (and ancestors). In particular if the parent assigns constant values
3347 * to the domains of the bands in "list", then the elements are sorted
3348 * according to that order.
3349 * This order should be a more "natural" order for the user, but otherwise
3350 * shouldn't have any effect.
3351 * If we would be constructing an isl_band forest directly in
3352 * isl_union_set_compute_schedule then there wouldn't be any need
3353 * for a reordering, since the children would be added to the list
3354 * in their natural order automatically.
3356 * If there is only one element in the list, then there is no need to sort
3357 * anything.
3358 * If the partial schedule of the parent has more than one member
3359 * (or if there is no parent), then it's
3360 * defnitely not assigning constant values to the different children in
3361 * the list and so we wouldn't be able to use it to sort the list.
3363 static __isl_give isl_band_list *sort_band_list(__isl_take isl_band_list *list,
3364 __isl_keep isl_band *parent)
3366 struct isl_cmp_band_data data;
3368 if (!list)
3369 return NULL;
3370 if (list->n <= 1)
3371 return list;
3372 if (!parent || parent->n != 1)
3373 return list;
3375 data.r = 0;
3376 isl_int_init(data.c1);
3377 isl_int_init(data.c2);
3378 isl_int_init(data.t);
3379 isl_sort(list->p, list->n, sizeof(list->p[0]), &cmp_band, &data);
3380 if (data.r < 0)
3381 list = isl_band_list_free(list);
3382 isl_int_clear(data.c1);
3383 isl_int_clear(data.c2);
3384 isl_int_clear(data.t);
3386 return list;
3389 /* Construct a list of bands that start at the same position (with
3390 * sequence number band_nr) in the schedules of the nodes that
3391 * were active in the parent band.
3393 * A separate isl_band structure is created for each band_id
3394 * and for each node that does not have a band with sequence
3395 * number band_nr. In the latter case, a band without members
3396 * is created.
3397 * This ensures that if a band has any children, then each node
3398 * that was active in the band is active in exactly one of the children.
3400 static __isl_give isl_band_list *construct_band_list(
3401 __isl_keep isl_schedule *schedule, __isl_keep isl_band *parent,
3402 int band_nr, int *parent_active, int n_active)
3404 int i, j;
3405 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
3406 int *active;
3407 int n_band;
3408 isl_band_list *list;
3410 n_band = 0;
3411 for (i = 0; i < n_active; ++i) {
3412 for (j = 0; j < schedule->n; ++j) {
3413 if (!parent_active[j])
3414 continue;
3415 if (schedule->node[j].n_band <= band_nr)
3416 continue;
3417 if (schedule->node[j].band_id[band_nr] == i) {
3418 n_band++;
3419 break;
3423 for (j = 0; j < schedule->n; ++j)
3424 if (schedule->node[j].n_band <= band_nr)
3425 n_band++;
3427 if (n_band == 1) {
3428 isl_band *band;
3429 list = isl_band_list_alloc(ctx, n_band);
3430 band = construct_band(schedule, parent, band_nr,
3431 parent_active, n_active);
3432 return isl_band_list_add(list, band);
3435 active = isl_alloc_array(ctx, int, schedule->n);
3436 if (schedule->n && !active)
3437 return NULL;
3439 list = isl_band_list_alloc(ctx, n_band);
3441 for (i = 0; i < n_active; ++i) {
3442 int n = 0;
3443 isl_band *band;
3445 for (j = 0; j < schedule->n; ++j) {
3446 active[j] = parent_active[j] &&
3447 schedule->node[j].n_band > band_nr &&
3448 schedule->node[j].band_id[band_nr] == i;
3449 if (active[j])
3450 n++;
3452 if (n == 0)
3453 continue;
3455 band = construct_band(schedule, parent, band_nr, active, n);
3457 list = isl_band_list_add(list, band);
3459 for (i = 0; i < schedule->n; ++i) {
3460 isl_band *band;
3461 if (!parent_active[i])
3462 continue;
3463 if (schedule->node[i].n_band > band_nr)
3464 continue;
3465 for (j = 0; j < schedule->n; ++j)
3466 active[j] = j == i;
3467 band = construct_band(schedule, parent, band_nr, active, 1);
3468 list = isl_band_list_add(list, band);
3471 free(active);
3473 list = sort_band_list(list, parent);
3475 return list;
3478 /* Construct a band forest representation of the schedule and
3479 * return the list of roots.
3481 static __isl_give isl_band_list *construct_forest(
3482 __isl_keep isl_schedule *schedule)
3484 int i;
3485 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
3486 isl_band_list *forest;
3487 int *active;
3489 active = isl_alloc_array(ctx, int, schedule->n);
3490 if (schedule->n && !active)
3491 return NULL;
3493 for (i = 0; i < schedule->n; ++i)
3494 active[i] = 1;
3496 forest = construct_band_list(schedule, NULL, 0, active, schedule->n);
3498 free(active);
3500 return forest;
3503 /* Return the roots of a band forest representation of the schedule.
3505 __isl_give isl_band_list *isl_schedule_get_band_forest(
3506 __isl_keep isl_schedule *schedule)
3508 if (!schedule)
3509 return NULL;
3510 if (!schedule->band_forest)
3511 schedule->band_forest = construct_forest(schedule);
3512 return isl_band_list_dup(schedule->band_forest);
3515 /* Call "fn" on each band in the schedule in depth-first post-order.
3517 int isl_schedule_foreach_band(__isl_keep isl_schedule *sched,
3518 int (*fn)(__isl_keep isl_band *band, void *user), void *user)
3520 int r;
3521 isl_band_list *forest;
3523 if (!sched)
3524 return -1;
3526 forest = isl_schedule_get_band_forest(sched);
3527 r = isl_band_list_foreach_band(forest, fn, user);
3528 isl_band_list_free(forest);
3530 return r;
3533 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
3534 __isl_keep isl_band_list *list);
3536 static __isl_give isl_printer *print_band(__isl_take isl_printer *p,
3537 __isl_keep isl_band *band)
3539 isl_band_list *children;
3541 p = isl_printer_start_line(p);
3542 p = isl_printer_print_union_pw_multi_aff(p, band->pma);
3543 p = isl_printer_end_line(p);
3545 if (!isl_band_has_children(band))
3546 return p;
3548 children = isl_band_get_children(band);
3550 p = isl_printer_indent(p, 4);
3551 p = print_band_list(p, children);
3552 p = isl_printer_indent(p, -4);
3554 isl_band_list_free(children);
3556 return p;
3559 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
3560 __isl_keep isl_band_list *list)
3562 int i, n;
3564 n = isl_band_list_n_band(list);
3565 for (i = 0; i < n; ++i) {
3566 isl_band *band;
3567 band = isl_band_list_get_band(list, i);
3568 p = print_band(p, band);
3569 isl_band_free(band);
3572 return p;
3575 __isl_give isl_printer *isl_printer_print_schedule(__isl_take isl_printer *p,
3576 __isl_keep isl_schedule *schedule)
3578 isl_band_list *forest;
3580 forest = isl_schedule_get_band_forest(schedule);
3582 p = print_band_list(p, forest);
3584 isl_band_list_free(forest);
3586 return p;
3589 void isl_schedule_dump(__isl_keep isl_schedule *schedule)
3591 isl_printer *printer;
3593 if (!schedule)
3594 return;
3596 printer = isl_printer_to_file(isl_schedule_get_ctx(schedule), stderr);
3597 printer = isl_printer_print_schedule(printer, schedule);
3599 isl_printer_free(printer);