isl_basic_map_simplify: fix elimination of unit coefficient known divs
[isl.git] / isl_schedule.c
blobd7b8b93531ea1479232131f24e627ba72eb750c4
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9 * 91893 Orsay, France
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_space_private.h>
16 #include <isl/aff.h>
17 #include <isl/hash.h>
18 #include <isl/constraint.h>
19 #include <isl/schedule.h>
20 #include <isl_mat_private.h>
21 #include <isl/set.h>
22 #include <isl/seq.h>
23 #include <isl_tab.h>
24 #include <isl_dim_map.h>
25 #include <isl_hmap_map_basic_set.h>
26 #include <isl_sort.h>
27 #include <isl_schedule_private.h>
28 #include <isl_band_private.h>
29 #include <isl_options_private.h>
30 #include <isl_tarjan.h>
33 * The scheduling algorithm implemented in this file was inspired by
34 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
35 * Parallelization and Locality Optimization in the Polyhedral Model".
39 /* Internal information about a node that is used during the construction
40 * of a schedule.
41 * dim represents the space in which the domain lives
42 * sched is a matrix representation of the schedule being constructed
43 * for this node
44 * sched_map is an isl_map representation of the same (partial) schedule
45 * sched_map may be NULL
46 * rank is the number of linearly independent rows in the linear part
47 * of sched
48 * the columns of cmap represent a change of basis for the schedule
49 * coefficients; the first rank columns span the linear part of
50 * the schedule rows
51 * cinv is the inverse of cmap.
52 * start is the first variable in the LP problem in the sequences that
53 * represents the schedule coefficients of this node
54 * nvar is the dimension of the domain
55 * nparam is the number of parameters or 0 if we are not constructing
56 * a parametric schedule
58 * scc is the index of SCC (or WCC) this node belongs to
60 * band contains the band index for each of the rows of the schedule.
61 * band_id is used to differentiate between separate bands at the same
62 * level within the same parent band, i.e., bands that are separated
63 * by the parent band or bands that are independent of each other.
64 * zero contains a boolean for each of the rows of the schedule,
65 * indicating whether the corresponding scheduling dimension results
66 * in zero dependence distances within its band and with respect
67 * to the proximity edges.
69 struct isl_sched_node {
70 isl_space *dim;
71 isl_mat *sched;
72 isl_map *sched_map;
73 int rank;
74 isl_mat *cmap;
75 isl_mat *cinv;
76 int start;
77 int nvar;
78 int nparam;
80 int scc;
82 int *band;
83 int *band_id;
84 int *zero;
87 static int node_has_dim(const void *entry, const void *val)
89 struct isl_sched_node *node = (struct isl_sched_node *)entry;
90 isl_space *dim = (isl_space *)val;
92 return isl_space_is_equal(node->dim, dim);
95 /* An edge in the dependence graph. An edge may be used to
96 * ensure validity of the generated schedule, to minimize the dependence
97 * distance or both
99 * map is the dependence relation
100 * src is the source node
101 * dst is the sink node
102 * validity is set if the edge is used to ensure correctness
103 * proximity is set if the edge is used to minimize dependence distances
105 * For validity edges, start and end mark the sequence of inequality
106 * constraints in the LP problem that encode the validity constraint
107 * corresponding to this edge.
109 struct isl_sched_edge {
110 isl_map *map;
112 struct isl_sched_node *src;
113 struct isl_sched_node *dst;
115 int validity;
116 int proximity;
118 int start;
119 int end;
122 enum isl_edge_type {
123 isl_edge_validity = 0,
124 isl_edge_first = isl_edge_validity,
125 isl_edge_proximity,
126 isl_edge_last = isl_edge_proximity
129 /* Internal information about the dependence graph used during
130 * the construction of the schedule.
132 * intra_hmap is a cache, mapping dependence relations to their dual,
133 * for dependences from a node to itself
134 * inter_hmap is a cache, mapping dependence relations to their dual,
135 * for dependences between distinct nodes
137 * n is the number of nodes
138 * node is the list of nodes
139 * maxvar is the maximal number of variables over all nodes
140 * max_row is the allocated number of rows in the schedule
141 * n_row is the current (maximal) number of linearly independent
142 * rows in the node schedules
143 * n_total_row is the current number of rows in the node schedules
144 * n_band is the current number of completed bands
145 * band_start is the starting row in the node schedules of the current band
146 * root is set if this graph is the original dependence graph,
147 * without any splitting
149 * sorted contains a list of node indices sorted according to the
150 * SCC to which a node belongs
152 * n_edge is the number of edges
153 * edge is the list of edges
154 * max_edge contains the maximal number of edges of each type;
155 * in particular, it contains the number of edges in the inital graph.
156 * edge_table contains pointers into the edge array, hashed on the source
157 * and sink spaces; there is one such table for each type;
158 * a given edge may be referenced from more than one table
159 * if the corresponding relation appears in more than of the
160 * sets of dependences
162 * node_table contains pointers into the node array, hashed on the space
164 * region contains a list of variable sequences that should be non-trivial
166 * lp contains the (I)LP problem used to obtain new schedule rows
168 * src_scc and dst_scc are the source and sink SCCs of an edge with
169 * conflicting constraints
171 * scc represents the number of components
173 struct isl_sched_graph {
174 isl_hmap_map_basic_set *intra_hmap;
175 isl_hmap_map_basic_set *inter_hmap;
177 struct isl_sched_node *node;
178 int n;
179 int maxvar;
180 int max_row;
181 int n_row;
183 int *sorted;
185 int n_band;
186 int n_total_row;
187 int band_start;
189 int root;
191 struct isl_sched_edge *edge;
192 int n_edge;
193 int max_edge[isl_edge_last + 1];
194 struct isl_hash_table *edge_table[isl_edge_last + 1];
196 struct isl_hash_table *node_table;
197 struct isl_region *region;
199 isl_basic_set *lp;
201 int src_scc;
202 int dst_scc;
204 int scc;
207 /* Initialize node_table based on the list of nodes.
209 static int graph_init_table(isl_ctx *ctx, struct isl_sched_graph *graph)
211 int i;
213 graph->node_table = isl_hash_table_alloc(ctx, graph->n);
214 if (!graph->node_table)
215 return -1;
217 for (i = 0; i < graph->n; ++i) {
218 struct isl_hash_table_entry *entry;
219 uint32_t hash;
221 hash = isl_space_get_hash(graph->node[i].dim);
222 entry = isl_hash_table_find(ctx, graph->node_table, hash,
223 &node_has_dim,
224 graph->node[i].dim, 1);
225 if (!entry)
226 return -1;
227 entry->data = &graph->node[i];
230 return 0;
233 /* Return a pointer to the node that lives within the given space,
234 * or NULL if there is no such node.
236 static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
237 struct isl_sched_graph *graph, __isl_keep isl_space *dim)
239 struct isl_hash_table_entry *entry;
240 uint32_t hash;
242 hash = isl_space_get_hash(dim);
243 entry = isl_hash_table_find(ctx, graph->node_table, hash,
244 &node_has_dim, dim, 0);
246 return entry ? entry->data : NULL;
249 static int edge_has_src_and_dst(const void *entry, const void *val)
251 const struct isl_sched_edge *edge = entry;
252 const struct isl_sched_edge *temp = val;
254 return edge->src == temp->src && edge->dst == temp->dst;
257 /* Add the given edge to graph->edge_table[type].
259 static int graph_edge_table_add(isl_ctx *ctx, struct isl_sched_graph *graph,
260 enum isl_edge_type type, struct isl_sched_edge *edge)
262 struct isl_hash_table_entry *entry;
263 uint32_t hash;
265 hash = isl_hash_init();
266 hash = isl_hash_builtin(hash, edge->src);
267 hash = isl_hash_builtin(hash, edge->dst);
268 entry = isl_hash_table_find(ctx, graph->edge_table[type], hash,
269 &edge_has_src_and_dst, edge, 1);
270 if (!entry)
271 return -1;
272 entry->data = edge;
274 return 0;
277 /* Allocate the edge_tables based on the maximal number of edges of
278 * each type.
280 static int graph_init_edge_tables(isl_ctx *ctx, struct isl_sched_graph *graph)
282 int i;
284 for (i = 0; i <= isl_edge_last; ++i) {
285 graph->edge_table[i] = isl_hash_table_alloc(ctx,
286 graph->max_edge[i]);
287 if (!graph->edge_table[i])
288 return -1;
291 return 0;
294 /* If graph->edge_table[type] contains an edge from the given source
295 * to the given destination, then return the hash table entry of this edge.
296 * Otherwise, return NULL.
298 static struct isl_hash_table_entry *graph_find_edge_entry(
299 struct isl_sched_graph *graph,
300 enum isl_edge_type type,
301 struct isl_sched_node *src, struct isl_sched_node *dst)
303 isl_ctx *ctx = isl_space_get_ctx(src->dim);
304 uint32_t hash;
305 struct isl_sched_edge temp = { .src = src, .dst = dst };
307 hash = isl_hash_init();
308 hash = isl_hash_builtin(hash, temp.src);
309 hash = isl_hash_builtin(hash, temp.dst);
310 return isl_hash_table_find(ctx, graph->edge_table[type], hash,
311 &edge_has_src_and_dst, &temp, 0);
315 /* If graph->edge_table[type] contains an edge from the given source
316 * to the given destination, then return this edge.
317 * Otherwise, return NULL.
319 static struct isl_sched_edge *graph_find_edge(struct isl_sched_graph *graph,
320 enum isl_edge_type type,
321 struct isl_sched_node *src, struct isl_sched_node *dst)
323 struct isl_hash_table_entry *entry;
325 entry = graph_find_edge_entry(graph, type, src, dst);
326 if (!entry)
327 return NULL;
329 return entry->data;
332 /* Check whether the dependence graph has an edge of the given type
333 * between the given two nodes.
335 static int graph_has_edge(struct isl_sched_graph *graph,
336 enum isl_edge_type type,
337 struct isl_sched_node *src, struct isl_sched_node *dst)
339 struct isl_sched_edge *edge;
340 int empty;
342 edge = graph_find_edge(graph, type, src, dst);
343 if (!edge)
344 return 0;
346 empty = isl_map_plain_is_empty(edge->map);
347 if (empty < 0)
348 return -1;
350 return !empty;
353 /* If there is an edge from the given source to the given destination
354 * of any type then return this edge.
355 * Otherwise, return NULL.
357 static struct isl_sched_edge *graph_find_any_edge(struct isl_sched_graph *graph,
358 struct isl_sched_node *src, struct isl_sched_node *dst)
360 enum isl_edge_type i;
361 struct isl_sched_edge *edge;
363 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
364 edge = graph_find_edge(graph, i, src, dst);
365 if (edge)
366 return edge;
369 return NULL;
372 /* Remove the given edge from all the edge_tables that refer to it.
374 static void graph_remove_edge(struct isl_sched_graph *graph,
375 struct isl_sched_edge *edge)
377 isl_ctx *ctx = isl_map_get_ctx(edge->map);
378 enum isl_edge_type i;
380 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
381 struct isl_hash_table_entry *entry;
383 entry = graph_find_edge_entry(graph, i, edge->src, edge->dst);
384 if (!entry)
385 continue;
386 if (entry->data != edge)
387 continue;
388 isl_hash_table_remove(ctx, graph->edge_table[i], entry);
392 /* Check whether the dependence graph has any edge
393 * between the given two nodes.
395 static int graph_has_any_edge(struct isl_sched_graph *graph,
396 struct isl_sched_node *src, struct isl_sched_node *dst)
398 enum isl_edge_type i;
399 int r;
401 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
402 r = graph_has_edge(graph, i, src, dst);
403 if (r < 0 || r)
404 return r;
407 return r;
410 /* Check whether the dependence graph has a validity edge
411 * between the given two nodes.
413 static int graph_has_validity_edge(struct isl_sched_graph *graph,
414 struct isl_sched_node *src, struct isl_sched_node *dst)
416 return graph_has_edge(graph, isl_edge_validity, src, dst);
419 static int graph_alloc(isl_ctx *ctx, struct isl_sched_graph *graph,
420 int n_node, int n_edge)
422 int i;
424 graph->n = n_node;
425 graph->n_edge = n_edge;
426 graph->node = isl_calloc_array(ctx, struct isl_sched_node, graph->n);
427 graph->sorted = isl_calloc_array(ctx, int, graph->n);
428 graph->region = isl_alloc_array(ctx, struct isl_region, graph->n);
429 graph->edge = isl_calloc_array(ctx,
430 struct isl_sched_edge, graph->n_edge);
432 graph->intra_hmap = isl_hmap_map_basic_set_alloc(ctx, 2 * n_edge);
433 graph->inter_hmap = isl_hmap_map_basic_set_alloc(ctx, 2 * n_edge);
435 if (!graph->node || !graph->region || (graph->n_edge && !graph->edge) ||
436 !graph->sorted)
437 return -1;
439 for(i = 0; i < graph->n; ++i)
440 graph->sorted[i] = i;
442 return 0;
445 static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
447 int i;
449 isl_hmap_map_basic_set_free(ctx, graph->intra_hmap);
450 isl_hmap_map_basic_set_free(ctx, graph->inter_hmap);
452 for (i = 0; i < graph->n; ++i) {
453 isl_space_free(graph->node[i].dim);
454 isl_mat_free(graph->node[i].sched);
455 isl_map_free(graph->node[i].sched_map);
456 isl_mat_free(graph->node[i].cmap);
457 isl_mat_free(graph->node[i].cinv);
458 if (graph->root) {
459 free(graph->node[i].band);
460 free(graph->node[i].band_id);
461 free(graph->node[i].zero);
464 free(graph->node);
465 free(graph->sorted);
466 for (i = 0; i < graph->n_edge; ++i)
467 isl_map_free(graph->edge[i].map);
468 free(graph->edge);
469 free(graph->region);
470 for (i = 0; i <= isl_edge_last; ++i)
471 isl_hash_table_free(ctx, graph->edge_table[i]);
472 isl_hash_table_free(ctx, graph->node_table);
473 isl_basic_set_free(graph->lp);
476 /* For each "set" on which this function is called, increment
477 * graph->n by one and update graph->maxvar.
479 static int init_n_maxvar(__isl_take isl_set *set, void *user)
481 struct isl_sched_graph *graph = user;
482 int nvar = isl_set_dim(set, isl_dim_set);
484 graph->n++;
485 if (nvar > graph->maxvar)
486 graph->maxvar = nvar;
488 isl_set_free(set);
490 return 0;
493 /* Compute the number of rows that should be allocated for the schedule.
494 * The graph can be split at most "n - 1" times, there can be at most
495 * two rows for each dimension in the iteration domains (in particular,
496 * we usually have one row, but it may be split by split_scaled),
497 * and there can be one extra row for ordering the statements.
498 * Note that if we have actually split "n - 1" times, then no ordering
499 * is needed, so in principle we could use "graph->n + 2 * graph->maxvar - 1".
501 static int compute_max_row(struct isl_sched_graph *graph,
502 __isl_keep isl_union_set *domain)
504 graph->n = 0;
505 graph->maxvar = 0;
506 if (isl_union_set_foreach_set(domain, &init_n_maxvar, graph) < 0)
507 return -1;
508 graph->max_row = graph->n + 2 * graph->maxvar;
510 return 0;
513 /* Add a new node to the graph representing the given set.
515 static int extract_node(__isl_take isl_set *set, void *user)
517 int nvar, nparam;
518 isl_ctx *ctx;
519 isl_space *dim;
520 isl_mat *sched;
521 struct isl_sched_graph *graph = user;
522 int *band, *band_id, *zero;
524 ctx = isl_set_get_ctx(set);
525 dim = isl_set_get_space(set);
526 isl_set_free(set);
527 nvar = isl_space_dim(dim, isl_dim_set);
528 nparam = isl_space_dim(dim, isl_dim_param);
529 if (!ctx->opt->schedule_parametric)
530 nparam = 0;
531 sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
532 graph->node[graph->n].dim = dim;
533 graph->node[graph->n].nvar = nvar;
534 graph->node[graph->n].nparam = nparam;
535 graph->node[graph->n].sched = sched;
536 graph->node[graph->n].sched_map = NULL;
537 band = isl_alloc_array(ctx, int, graph->max_row);
538 graph->node[graph->n].band = band;
539 band_id = isl_calloc_array(ctx, int, graph->max_row);
540 graph->node[graph->n].band_id = band_id;
541 zero = isl_calloc_array(ctx, int, graph->max_row);
542 graph->node[graph->n].zero = zero;
543 graph->n++;
545 if (!sched || (graph->max_row && (!band || !band_id || !zero)))
546 return -1;
548 return 0;
551 struct isl_extract_edge_data {
552 enum isl_edge_type type;
553 struct isl_sched_graph *graph;
556 /* Add a new edge to the graph based on the given map
557 * and add it to data->graph->edge_table[data->type].
558 * If a dependence relation of a given type happens to be identical
559 * to one of the dependence relations of a type that was added before,
560 * then we don't create a new edge, but instead mark the original edge
561 * as also representing a dependence of the current type.
563 static int extract_edge(__isl_take isl_map *map, void *user)
565 isl_ctx *ctx = isl_map_get_ctx(map);
566 struct isl_extract_edge_data *data = user;
567 struct isl_sched_graph *graph = data->graph;
568 struct isl_sched_node *src, *dst;
569 isl_space *dim;
570 struct isl_sched_edge *edge;
571 int is_equal;
573 dim = isl_space_domain(isl_map_get_space(map));
574 src = graph_find_node(ctx, graph, dim);
575 isl_space_free(dim);
576 dim = isl_space_range(isl_map_get_space(map));
577 dst = graph_find_node(ctx, graph, dim);
578 isl_space_free(dim);
580 if (!src || !dst) {
581 isl_map_free(map);
582 return 0;
585 graph->edge[graph->n_edge].src = src;
586 graph->edge[graph->n_edge].dst = dst;
587 graph->edge[graph->n_edge].map = map;
588 if (data->type == isl_edge_validity) {
589 graph->edge[graph->n_edge].validity = 1;
590 graph->edge[graph->n_edge].proximity = 0;
592 if (data->type == isl_edge_proximity) {
593 graph->edge[graph->n_edge].validity = 0;
594 graph->edge[graph->n_edge].proximity = 1;
596 graph->n_edge++;
598 edge = graph_find_any_edge(graph, src, dst);
599 if (!edge)
600 return graph_edge_table_add(ctx, graph, data->type,
601 &graph->edge[graph->n_edge - 1]);
602 is_equal = isl_map_plain_is_equal(map, edge->map);
603 if (is_equal < 0)
604 return -1;
605 if (!is_equal)
606 return graph_edge_table_add(ctx, graph, data->type,
607 &graph->edge[graph->n_edge - 1]);
609 graph->n_edge--;
610 edge->validity |= graph->edge[graph->n_edge].validity;
611 edge->proximity |= graph->edge[graph->n_edge].proximity;
612 isl_map_free(map);
614 return graph_edge_table_add(ctx, graph, data->type, edge);
617 /* Check whether there is any dependence from node[j] to node[i]
618 * or from node[i] to node[j].
620 static int node_follows_weak(int i, int j, void *user)
622 int f;
623 struct isl_sched_graph *graph = user;
625 f = graph_has_any_edge(graph, &graph->node[j], &graph->node[i]);
626 if (f < 0 || f)
627 return f;
628 return graph_has_any_edge(graph, &graph->node[i], &graph->node[j]);
631 /* Check whether there is a validity dependence from node[j] to node[i],
632 * forcing node[i] to follow node[j].
634 static int node_follows_strong(int i, int j, void *user)
636 struct isl_sched_graph *graph = user;
638 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
641 /* Use Tarjan's algorithm for computing the strongly connected components
642 * in the dependence graph (only validity edges).
643 * If weak is set, we consider the graph to be undirected and
644 * we effectively compute the (weakly) connected components.
645 * Additionally, we also consider other edges when weak is set.
647 static int detect_ccs(isl_ctx *ctx, struct isl_sched_graph *graph, int weak)
649 int i, n;
650 struct isl_tarjan_graph *g = NULL;
652 g = isl_tarjan_graph_init(ctx, graph->n,
653 weak ? &node_follows_weak : &node_follows_strong, graph);
654 if (!g)
655 return -1;
657 graph->scc = 0;
658 i = 0;
659 n = graph->n;
660 while (n) {
661 while (g->order[i] != -1) {
662 graph->node[g->order[i]].scc = graph->scc;
663 --n;
664 ++i;
666 ++i;
667 graph->scc++;
670 isl_tarjan_graph_free(g);
672 return 0;
675 /* Apply Tarjan's algorithm to detect the strongly connected components
676 * in the dependence graph.
678 static int detect_sccs(isl_ctx *ctx, struct isl_sched_graph *graph)
680 return detect_ccs(ctx, graph, 0);
683 /* Apply Tarjan's algorithm to detect the (weakly) connected components
684 * in the dependence graph.
686 static int detect_wccs(isl_ctx *ctx, struct isl_sched_graph *graph)
688 return detect_ccs(ctx, graph, 1);
691 static int cmp_scc(const void *a, const void *b, void *data)
693 struct isl_sched_graph *graph = data;
694 const int *i1 = a;
695 const int *i2 = b;
697 return graph->node[*i1].scc - graph->node[*i2].scc;
700 /* Sort the elements of graph->sorted according to the corresponding SCCs.
702 static int sort_sccs(struct isl_sched_graph *graph)
704 return isl_sort(graph->sorted, graph->n, sizeof(int), &cmp_scc, graph);
707 /* Given a dependence relation R from a node to itself,
708 * construct the set of coefficients of valid constraints for elements
709 * in that dependence relation.
710 * In particular, the result contains tuples of coefficients
711 * c_0, c_n, c_x such that
713 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
715 * or, equivalently,
717 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
719 * We choose here to compute the dual of delta R.
720 * Alternatively, we could have computed the dual of R, resulting
721 * in a set of tuples c_0, c_n, c_x, c_y, and then
722 * plugged in (c_0, c_n, c_x, -c_x).
724 static __isl_give isl_basic_set *intra_coefficients(
725 struct isl_sched_graph *graph, __isl_take isl_map *map)
727 isl_ctx *ctx = isl_map_get_ctx(map);
728 isl_set *delta;
729 isl_basic_set *coef;
731 if (isl_hmap_map_basic_set_has(ctx, graph->intra_hmap, map))
732 return isl_hmap_map_basic_set_get(ctx, graph->intra_hmap, map);
734 delta = isl_set_remove_divs(isl_map_deltas(isl_map_copy(map)));
735 coef = isl_set_coefficients(delta);
736 isl_hmap_map_basic_set_set(ctx, graph->intra_hmap, map,
737 isl_basic_set_copy(coef));
739 return coef;
742 /* Given a dependence relation R, * construct the set of coefficients
743 * of valid constraints for elements in that dependence relation.
744 * In particular, the result contains tuples of coefficients
745 * c_0, c_n, c_x, c_y such that
747 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
750 static __isl_give isl_basic_set *inter_coefficients(
751 struct isl_sched_graph *graph, __isl_take isl_map *map)
753 isl_ctx *ctx = isl_map_get_ctx(map);
754 isl_set *set;
755 isl_basic_set *coef;
757 if (isl_hmap_map_basic_set_has(ctx, graph->inter_hmap, map))
758 return isl_hmap_map_basic_set_get(ctx, graph->inter_hmap, map);
760 set = isl_map_wrap(isl_map_remove_divs(isl_map_copy(map)));
761 coef = isl_set_coefficients(set);
762 isl_hmap_map_basic_set_set(ctx, graph->inter_hmap, map,
763 isl_basic_set_copy(coef));
765 return coef;
768 /* Add constraints to graph->lp that force validity for the given
769 * dependence from a node i to itself.
770 * That is, add constraints that enforce
772 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
773 * = c_i_x (y - x) >= 0
775 * for each (x,y) in R.
776 * We obtain general constraints on coefficients (c_0, c_n, c_x)
777 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
778 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
779 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
781 * Actually, we do not construct constraints for the c_i_x themselves,
782 * but for the coefficients of c_i_x written as a linear combination
783 * of the columns in node->cmap.
785 static int add_intra_validity_constraints(struct isl_sched_graph *graph,
786 struct isl_sched_edge *edge)
788 unsigned total;
789 isl_map *map = isl_map_copy(edge->map);
790 isl_ctx *ctx = isl_map_get_ctx(map);
791 isl_space *dim;
792 isl_dim_map *dim_map;
793 isl_basic_set *coef;
794 struct isl_sched_node *node = edge->src;
796 coef = intra_coefficients(graph, map);
798 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
800 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
801 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
802 if (!coef)
803 goto error;
805 total = isl_basic_set_total_dim(graph->lp);
806 dim_map = isl_dim_map_alloc(ctx, total);
807 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
808 isl_space_dim(dim, isl_dim_set), 1,
809 node->nvar, -1);
810 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
811 isl_space_dim(dim, isl_dim_set), 1,
812 node->nvar, 1);
813 graph->lp = isl_basic_set_extend_constraints(graph->lp,
814 coef->n_eq, coef->n_ineq);
815 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
816 coef, dim_map);
817 isl_space_free(dim);
819 return 0;
820 error:
821 isl_space_free(dim);
822 return -1;
825 /* Add constraints to graph->lp that force validity for the given
826 * dependence from node i to node j.
827 * That is, add constraints that enforce
829 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
831 * for each (x,y) in R.
832 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
833 * of valid constraints for R and then plug in
834 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
835 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
836 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
837 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
839 * Actually, we do not construct constraints for the c_*_x themselves,
840 * but for the coefficients of c_*_x written as a linear combination
841 * of the columns in node->cmap.
843 static int add_inter_validity_constraints(struct isl_sched_graph *graph,
844 struct isl_sched_edge *edge)
846 unsigned total;
847 isl_map *map = isl_map_copy(edge->map);
848 isl_ctx *ctx = isl_map_get_ctx(map);
849 isl_space *dim;
850 isl_dim_map *dim_map;
851 isl_basic_set *coef;
852 struct isl_sched_node *src = edge->src;
853 struct isl_sched_node *dst = edge->dst;
855 coef = inter_coefficients(graph, map);
857 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
859 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
860 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
861 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
862 isl_space_dim(dim, isl_dim_set) + src->nvar,
863 isl_mat_copy(dst->cmap));
864 if (!coef)
865 goto error;
867 total = isl_basic_set_total_dim(graph->lp);
868 dim_map = isl_dim_map_alloc(ctx, total);
870 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
871 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
872 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
873 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
874 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
875 dst->nvar, -1);
876 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
877 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
878 dst->nvar, 1);
880 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
881 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
882 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
883 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
884 isl_space_dim(dim, isl_dim_set), 1,
885 src->nvar, 1);
886 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
887 isl_space_dim(dim, isl_dim_set), 1,
888 src->nvar, -1);
890 edge->start = graph->lp->n_ineq;
891 graph->lp = isl_basic_set_extend_constraints(graph->lp,
892 coef->n_eq, coef->n_ineq);
893 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
894 coef, dim_map);
895 if (!graph->lp)
896 goto error;
897 isl_space_free(dim);
898 edge->end = graph->lp->n_ineq;
900 return 0;
901 error:
902 isl_space_free(dim);
903 return -1;
906 /* Add constraints to graph->lp that bound the dependence distance for the given
907 * dependence from a node i to itself.
908 * If s = 1, we add the constraint
910 * c_i_x (y - x) <= m_0 + m_n n
912 * or
914 * -c_i_x (y - x) + m_0 + m_n n >= 0
916 * for each (x,y) in R.
917 * If s = -1, we add the constraint
919 * -c_i_x (y - x) <= m_0 + m_n n
921 * or
923 * c_i_x (y - x) + m_0 + m_n n >= 0
925 * for each (x,y) in R.
926 * We obtain general constraints on coefficients (c_0, c_n, c_x)
927 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
928 * with each coefficient (except m_0) represented as a pair of non-negative
929 * coefficients.
931 * Actually, we do not construct constraints for the c_i_x themselves,
932 * but for the coefficients of c_i_x written as a linear combination
933 * of the columns in node->cmap.
935 static int add_intra_proximity_constraints(struct isl_sched_graph *graph,
936 struct isl_sched_edge *edge, int s)
938 unsigned total;
939 unsigned nparam;
940 isl_map *map = isl_map_copy(edge->map);
941 isl_ctx *ctx = isl_map_get_ctx(map);
942 isl_space *dim;
943 isl_dim_map *dim_map;
944 isl_basic_set *coef;
945 struct isl_sched_node *node = edge->src;
947 coef = intra_coefficients(graph, map);
949 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
951 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
952 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
953 if (!coef)
954 goto error;
956 nparam = isl_space_dim(node->dim, isl_dim_param);
957 total = isl_basic_set_total_dim(graph->lp);
958 dim_map = isl_dim_map_alloc(ctx, total);
959 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
960 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
961 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
962 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
963 isl_space_dim(dim, isl_dim_set), 1,
964 node->nvar, s);
965 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
966 isl_space_dim(dim, isl_dim_set), 1,
967 node->nvar, -s);
968 graph->lp = isl_basic_set_extend_constraints(graph->lp,
969 coef->n_eq, coef->n_ineq);
970 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
971 coef, dim_map);
972 isl_space_free(dim);
974 return 0;
975 error:
976 isl_space_free(dim);
977 return -1;
980 /* Add constraints to graph->lp that bound the dependence distance for the given
981 * dependence from node i to node j.
982 * If s = 1, we add the constraint
984 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
985 * <= m_0 + m_n n
987 * or
989 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
990 * m_0 + m_n n >= 0
992 * for each (x,y) in R.
993 * If s = -1, we add the constraint
995 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
996 * <= m_0 + m_n n
998 * or
1000 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1001 * m_0 + m_n n >= 0
1003 * for each (x,y) in R.
1004 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1005 * of valid constraints for R and then plug in
1006 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1007 * -s*c_j_x+s*c_i_x)
1008 * with each coefficient (except m_0, c_j_0 and c_i_0)
1009 * represented as a pair of non-negative coefficients.
1011 * Actually, we do not construct constraints for the c_*_x themselves,
1012 * but for the coefficients of c_*_x written as a linear combination
1013 * of the columns in node->cmap.
1015 static int add_inter_proximity_constraints(struct isl_sched_graph *graph,
1016 struct isl_sched_edge *edge, int s)
1018 unsigned total;
1019 unsigned nparam;
1020 isl_map *map = isl_map_copy(edge->map);
1021 isl_ctx *ctx = isl_map_get_ctx(map);
1022 isl_space *dim;
1023 isl_dim_map *dim_map;
1024 isl_basic_set *coef;
1025 struct isl_sched_node *src = edge->src;
1026 struct isl_sched_node *dst = edge->dst;
1028 coef = inter_coefficients(graph, map);
1030 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1032 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1033 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1034 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1035 isl_space_dim(dim, isl_dim_set) + src->nvar,
1036 isl_mat_copy(dst->cmap));
1037 if (!coef)
1038 goto error;
1040 nparam = isl_space_dim(src->dim, isl_dim_param);
1041 total = isl_basic_set_total_dim(graph->lp);
1042 dim_map = isl_dim_map_alloc(ctx, total);
1044 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1045 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1046 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1048 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, -s);
1049 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, s);
1050 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, -s);
1051 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1052 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1053 dst->nvar, s);
1054 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1055 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1056 dst->nvar, -s);
1058 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, s);
1059 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, -s);
1060 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, s);
1061 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1062 isl_space_dim(dim, isl_dim_set), 1,
1063 src->nvar, -s);
1064 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1065 isl_space_dim(dim, isl_dim_set), 1,
1066 src->nvar, s);
1068 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1069 coef->n_eq, coef->n_ineq);
1070 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1071 coef, dim_map);
1072 isl_space_free(dim);
1074 return 0;
1075 error:
1076 isl_space_free(dim);
1077 return -1;
1080 static int add_all_validity_constraints(struct isl_sched_graph *graph)
1082 int i;
1084 for (i = 0; i < graph->n_edge; ++i) {
1085 struct isl_sched_edge *edge= &graph->edge[i];
1086 if (!edge->validity)
1087 continue;
1088 if (edge->src != edge->dst)
1089 continue;
1090 if (add_intra_validity_constraints(graph, edge) < 0)
1091 return -1;
1094 for (i = 0; i < graph->n_edge; ++i) {
1095 struct isl_sched_edge *edge = &graph->edge[i];
1096 if (!edge->validity)
1097 continue;
1098 if (edge->src == edge->dst)
1099 continue;
1100 if (add_inter_validity_constraints(graph, edge) < 0)
1101 return -1;
1104 return 0;
1107 /* Add constraints to graph->lp that bound the dependence distance
1108 * for all dependence relations.
1109 * If a given proximity dependence is identical to a validity
1110 * dependence, then the dependence distance is already bounded
1111 * from below (by zero), so we only need to bound the distance
1112 * from above.
1113 * Otherwise, we need to bound the distance both from above and from below.
1115 static int add_all_proximity_constraints(struct isl_sched_graph *graph)
1117 int i;
1119 for (i = 0; i < graph->n_edge; ++i) {
1120 struct isl_sched_edge *edge= &graph->edge[i];
1121 if (!edge->proximity)
1122 continue;
1123 if (edge->src == edge->dst &&
1124 add_intra_proximity_constraints(graph, edge, 1) < 0)
1125 return -1;
1126 if (edge->src != edge->dst &&
1127 add_inter_proximity_constraints(graph, edge, 1) < 0)
1128 return -1;
1129 if (edge->validity)
1130 continue;
1131 if (edge->src == edge->dst &&
1132 add_intra_proximity_constraints(graph, edge, -1) < 0)
1133 return -1;
1134 if (edge->src != edge->dst &&
1135 add_inter_proximity_constraints(graph, edge, -1) < 0)
1136 return -1;
1139 return 0;
1142 /* Compute a basis for the rows in the linear part of the schedule
1143 * and extend this basis to a full basis. The remaining rows
1144 * can then be used to force linear independence from the rows
1145 * in the schedule.
1147 * In particular, given the schedule rows S, we compute
1149 * S = H Q
1150 * S U = H
1152 * with H the Hermite normal form of S. That is, all but the
1153 * first rank columns of Q are zero and so each row in S is
1154 * a linear combination of the first rank rows of Q.
1155 * The matrix Q is then transposed because we will write the
1156 * coefficients of the next schedule row as a column vector s
1157 * and express this s as a linear combination s = Q c of the
1158 * computed basis.
1159 * Similarly, the matrix U is transposed such that we can
1160 * compute the coefficients c = U s from a schedule row s.
1162 static int node_update_cmap(struct isl_sched_node *node)
1164 isl_mat *H, *U, *Q;
1165 int n_row = isl_mat_rows(node->sched);
1167 H = isl_mat_sub_alloc(node->sched, 0, n_row,
1168 1 + node->nparam, node->nvar);
1170 H = isl_mat_left_hermite(H, 0, &U, &Q);
1171 isl_mat_free(node->cmap);
1172 isl_mat_free(node->cinv);
1173 node->cmap = isl_mat_transpose(Q);
1174 node->cinv = isl_mat_transpose(U);
1175 node->rank = isl_mat_initial_non_zero_cols(H);
1176 isl_mat_free(H);
1178 if (!node->cmap || !node->cinv || node->rank < 0)
1179 return -1;
1180 return 0;
1183 /* Count the number of equality and inequality constraints
1184 * that will be added for the given map.
1185 * If carry is set, then we are counting the number of (validity)
1186 * constraints that will be added in setup_carry_lp and we count
1187 * each edge exactly once. Otherwise, we count as follows
1188 * validity -> 1 (>= 0)
1189 * validity+proximity -> 2 (>= 0 and upper bound)
1190 * proximity -> 2 (lower and upper bound)
1192 static int count_map_constraints(struct isl_sched_graph *graph,
1193 struct isl_sched_edge *edge, __isl_take isl_map *map,
1194 int *n_eq, int *n_ineq, int carry)
1196 isl_basic_set *coef;
1197 int f = carry ? 1 : edge->proximity ? 2 : 1;
1199 if (carry && !edge->validity) {
1200 isl_map_free(map);
1201 return 0;
1204 if (edge->src == edge->dst)
1205 coef = intra_coefficients(graph, map);
1206 else
1207 coef = inter_coefficients(graph, map);
1208 if (!coef)
1209 return -1;
1210 *n_eq += f * coef->n_eq;
1211 *n_ineq += f * coef->n_ineq;
1212 isl_basic_set_free(coef);
1214 return 0;
1217 /* Count the number of equality and inequality constraints
1218 * that will be added to the main lp problem.
1219 * We count as follows
1220 * validity -> 1 (>= 0)
1221 * validity+proximity -> 2 (>= 0 and upper bound)
1222 * proximity -> 2 (lower and upper bound)
1224 static int count_constraints(struct isl_sched_graph *graph,
1225 int *n_eq, int *n_ineq)
1227 int i;
1229 *n_eq = *n_ineq = 0;
1230 for (i = 0; i < graph->n_edge; ++i) {
1231 struct isl_sched_edge *edge= &graph->edge[i];
1232 isl_map *map = isl_map_copy(edge->map);
1234 if (count_map_constraints(graph, edge, map,
1235 n_eq, n_ineq, 0) < 0)
1236 return -1;
1239 return 0;
1242 /* Add constraints that bound the values of the variable and parameter
1243 * coefficients of the schedule.
1245 * The maximal value of the coefficients is defined by the option
1246 * 'schedule_max_coefficient'.
1248 static int add_bound_coefficient_constraints(isl_ctx *ctx,
1249 struct isl_sched_graph *graph)
1251 int i, j, k;
1252 int max_coefficient;
1253 int total;
1255 max_coefficient = ctx->opt->schedule_max_coefficient;
1257 if (max_coefficient == -1)
1258 return 0;
1260 total = isl_basic_set_total_dim(graph->lp);
1262 for (i = 0; i < graph->n; ++i) {
1263 struct isl_sched_node *node = &graph->node[i];
1264 for (j = 0; j < 2 * node->nparam + 2 * node->nvar; ++j) {
1265 int dim;
1266 k = isl_basic_set_alloc_inequality(graph->lp);
1267 if (k < 0)
1268 return -1;
1269 dim = 1 + node->start + 1 + j;
1270 isl_seq_clr(graph->lp->ineq[k], 1 + total);
1271 isl_int_set_si(graph->lp->ineq[k][dim], -1);
1272 isl_int_set_si(graph->lp->ineq[k][0], max_coefficient);
1276 return 0;
1279 /* Construct an ILP problem for finding schedule coefficients
1280 * that result in non-negative, but small dependence distances
1281 * over all dependences.
1282 * In particular, the dependence distances over proximity edges
1283 * are bounded by m_0 + m_n n and we compute schedule coefficients
1284 * with small values (preferably zero) of m_n and m_0.
1286 * All variables of the ILP are non-negative. The actual coefficients
1287 * may be negative, so each coefficient is represented as the difference
1288 * of two non-negative variables. The negative part always appears
1289 * immediately before the positive part.
1290 * Other than that, the variables have the following order
1292 * - sum of positive and negative parts of m_n coefficients
1293 * - m_0
1294 * - sum of positive and negative parts of all c_n coefficients
1295 * (unconstrained when computing non-parametric schedules)
1296 * - sum of positive and negative parts of all c_x coefficients
1297 * - positive and negative parts of m_n coefficients
1298 * - for each node
1299 * - c_i_0
1300 * - positive and negative parts of c_i_n (if parametric)
1301 * - positive and negative parts of c_i_x
1303 * The c_i_x are not represented directly, but through the columns of
1304 * node->cmap. That is, the computed values are for variable t_i_x
1305 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
1307 * The constraints are those from the edges plus two or three equalities
1308 * to express the sums.
1310 * If force_zero is set, then we add equalities to ensure that
1311 * the sum of the m_n coefficients and m_0 are both zero.
1313 static int setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
1314 int force_zero)
1316 int i, j;
1317 int k;
1318 unsigned nparam;
1319 unsigned total;
1320 isl_space *dim;
1321 int parametric;
1322 int param_pos;
1323 int n_eq, n_ineq;
1324 int max_constant_term;
1325 int max_coefficient;
1327 max_constant_term = ctx->opt->schedule_max_constant_term;
1328 max_coefficient = ctx->opt->schedule_max_coefficient;
1330 parametric = ctx->opt->schedule_parametric;
1331 nparam = isl_space_dim(graph->node[0].dim, isl_dim_param);
1332 param_pos = 4;
1333 total = param_pos + 2 * nparam;
1334 for (i = 0; i < graph->n; ++i) {
1335 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
1336 if (node_update_cmap(node) < 0)
1337 return -1;
1338 node->start = total;
1339 total += 1 + 2 * (node->nparam + node->nvar);
1342 if (count_constraints(graph, &n_eq, &n_ineq) < 0)
1343 return -1;
1345 dim = isl_space_set_alloc(ctx, 0, total);
1346 isl_basic_set_free(graph->lp);
1347 n_eq += 2 + parametric + force_zero;
1348 if (max_constant_term != -1)
1349 n_ineq += graph->n;
1350 if (max_coefficient != -1)
1351 for (i = 0; i < graph->n; ++i)
1352 n_ineq += 2 * graph->node[i].nparam +
1353 2 * graph->node[i].nvar;
1355 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
1357 k = isl_basic_set_alloc_equality(graph->lp);
1358 if (k < 0)
1359 return -1;
1360 isl_seq_clr(graph->lp->eq[k], 1 + total);
1361 if (!force_zero)
1362 isl_int_set_si(graph->lp->eq[k][1], -1);
1363 for (i = 0; i < 2 * nparam; ++i)
1364 isl_int_set_si(graph->lp->eq[k][1 + param_pos + i], 1);
1366 if (force_zero) {
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][2], -1);
1374 if (parametric) {
1375 k = isl_basic_set_alloc_equality(graph->lp);
1376 if (k < 0)
1377 return -1;
1378 isl_seq_clr(graph->lp->eq[k], 1 + total);
1379 isl_int_set_si(graph->lp->eq[k][3], -1);
1380 for (i = 0; i < graph->n; ++i) {
1381 int pos = 1 + graph->node[i].start + 1;
1383 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
1384 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
1388 k = isl_basic_set_alloc_equality(graph->lp);
1389 if (k < 0)
1390 return -1;
1391 isl_seq_clr(graph->lp->eq[k], 1 + total);
1392 isl_int_set_si(graph->lp->eq[k][4], -1);
1393 for (i = 0; i < graph->n; ++i) {
1394 struct isl_sched_node *node = &graph->node[i];
1395 int pos = 1 + node->start + 1 + 2 * node->nparam;
1397 for (j = 0; j < 2 * node->nvar; ++j)
1398 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
1401 if (max_constant_term != -1)
1402 for (i = 0; i < graph->n; ++i) {
1403 struct isl_sched_node *node = &graph->node[i];
1404 k = isl_basic_set_alloc_inequality(graph->lp);
1405 if (k < 0)
1406 return -1;
1407 isl_seq_clr(graph->lp->ineq[k], 1 + total);
1408 isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
1409 isl_int_set_si(graph->lp->ineq[k][0], max_constant_term);
1412 if (add_bound_coefficient_constraints(ctx, graph) < 0)
1413 return -1;
1414 if (add_all_validity_constraints(graph) < 0)
1415 return -1;
1416 if (add_all_proximity_constraints(graph) < 0)
1417 return -1;
1419 return 0;
1422 /* Analyze the conflicting constraint found by
1423 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
1424 * constraint of one of the edges between distinct nodes, living, moreover
1425 * in distinct SCCs, then record the source and sink SCC as this may
1426 * be a good place to cut between SCCs.
1428 static int check_conflict(int con, void *user)
1430 int i;
1431 struct isl_sched_graph *graph = user;
1433 if (graph->src_scc >= 0)
1434 return 0;
1436 con -= graph->lp->n_eq;
1438 if (con >= graph->lp->n_ineq)
1439 return 0;
1441 for (i = 0; i < graph->n_edge; ++i) {
1442 if (!graph->edge[i].validity)
1443 continue;
1444 if (graph->edge[i].src == graph->edge[i].dst)
1445 continue;
1446 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
1447 continue;
1448 if (graph->edge[i].start > con)
1449 continue;
1450 if (graph->edge[i].end <= con)
1451 continue;
1452 graph->src_scc = graph->edge[i].src->scc;
1453 graph->dst_scc = graph->edge[i].dst->scc;
1456 return 0;
1459 /* Check whether the next schedule row of the given node needs to be
1460 * non-trivial. Lower-dimensional domains may have some trivial rows,
1461 * but as soon as the number of remaining required non-trivial rows
1462 * is as large as the number or remaining rows to be computed,
1463 * all remaining rows need to be non-trivial.
1465 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
1467 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
1470 /* Solve the ILP problem constructed in setup_lp.
1471 * For each node such that all the remaining rows of its schedule
1472 * need to be non-trivial, we construct a non-triviality region.
1473 * This region imposes that the next row is independent of previous rows.
1474 * In particular the coefficients c_i_x are represented by t_i_x
1475 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
1476 * its first columns span the rows of the previously computed part
1477 * of the schedule. The non-triviality region enforces that at least
1478 * one of the remaining components of t_i_x is non-zero, i.e.,
1479 * that the new schedule row depends on at least one of the remaining
1480 * columns of Q.
1482 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
1484 int i;
1485 isl_vec *sol;
1486 isl_basic_set *lp;
1488 for (i = 0; i < graph->n; ++i) {
1489 struct isl_sched_node *node = &graph->node[i];
1490 int skip = node->rank;
1491 graph->region[i].pos = node->start + 1 + 2*(node->nparam+skip);
1492 if (needs_row(graph, node))
1493 graph->region[i].len = 2 * (node->nvar - skip);
1494 else
1495 graph->region[i].len = 0;
1497 lp = isl_basic_set_copy(graph->lp);
1498 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
1499 graph->region, &check_conflict, graph);
1500 return sol;
1503 /* Update the schedules of all nodes based on the given solution
1504 * of the LP problem.
1505 * The new row is added to the current band.
1506 * All possibly negative coefficients are encoded as a difference
1507 * of two non-negative variables, so we need to perform the subtraction
1508 * here. Moreover, if use_cmap is set, then the solution does
1509 * not refer to the actual coefficients c_i_x, but instead to variables
1510 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
1511 * In this case, we then also need to perform this multiplication
1512 * to obtain the values of c_i_x.
1514 * If check_zero is set, then the first two coordinates of sol are
1515 * assumed to correspond to the dependence distance. If these two
1516 * coordinates are zero, then the corresponding scheduling dimension
1517 * is marked as being zero distance.
1519 static int update_schedule(struct isl_sched_graph *graph,
1520 __isl_take isl_vec *sol, int use_cmap, int check_zero)
1522 int i, j;
1523 int zero = 0;
1524 isl_vec *csol = NULL;
1526 if (!sol)
1527 goto error;
1528 if (sol->size == 0)
1529 isl_die(sol->ctx, isl_error_internal,
1530 "no solution found", goto error);
1531 if (graph->n_total_row >= graph->max_row)
1532 isl_die(sol->ctx, isl_error_internal,
1533 "too many schedule rows", goto error);
1535 if (check_zero)
1536 zero = isl_int_is_zero(sol->el[1]) &&
1537 isl_int_is_zero(sol->el[2]);
1539 for (i = 0; i < graph->n; ++i) {
1540 struct isl_sched_node *node = &graph->node[i];
1541 int pos = node->start;
1542 int row = isl_mat_rows(node->sched);
1544 isl_vec_free(csol);
1545 csol = isl_vec_alloc(sol->ctx, node->nvar);
1546 if (!csol)
1547 goto error;
1549 isl_map_free(node->sched_map);
1550 node->sched_map = NULL;
1551 node->sched = isl_mat_add_rows(node->sched, 1);
1552 if (!node->sched)
1553 goto error;
1554 node->sched = isl_mat_set_element(node->sched, row, 0,
1555 sol->el[1 + pos]);
1556 for (j = 0; j < node->nparam + node->nvar; ++j)
1557 isl_int_sub(sol->el[1 + pos + 1 + 2 * j + 1],
1558 sol->el[1 + pos + 1 + 2 * j + 1],
1559 sol->el[1 + pos + 1 + 2 * j]);
1560 for (j = 0; j < node->nparam; ++j)
1561 node->sched = isl_mat_set_element(node->sched,
1562 row, 1 + j, sol->el[1+pos+1+2*j+1]);
1563 for (j = 0; j < node->nvar; ++j)
1564 isl_int_set(csol->el[j],
1565 sol->el[1+pos+1+2*(node->nparam+j)+1]);
1566 if (use_cmap)
1567 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
1568 csol);
1569 if (!csol)
1570 goto error;
1571 for (j = 0; j < node->nvar; ++j)
1572 node->sched = isl_mat_set_element(node->sched,
1573 row, 1 + node->nparam + j, csol->el[j]);
1574 node->band[graph->n_total_row] = graph->n_band;
1575 node->zero[graph->n_total_row] = zero;
1577 isl_vec_free(sol);
1578 isl_vec_free(csol);
1580 graph->n_row++;
1581 graph->n_total_row++;
1583 return 0;
1584 error:
1585 isl_vec_free(sol);
1586 isl_vec_free(csol);
1587 return -1;
1590 /* Convert node->sched into a multi_aff and return this multi_aff.
1592 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
1593 struct isl_sched_node *node)
1595 int i, j;
1596 isl_space *space;
1597 isl_local_space *ls;
1598 isl_aff *aff;
1599 isl_multi_aff *ma;
1600 int nrow, ncol;
1601 isl_int v;
1603 nrow = isl_mat_rows(node->sched);
1604 ncol = isl_mat_cols(node->sched) - 1;
1605 space = isl_space_from_domain(isl_space_copy(node->dim));
1606 space = isl_space_add_dims(space, isl_dim_out, nrow);
1607 ma = isl_multi_aff_zero(space);
1608 ls = isl_local_space_from_space(isl_space_copy(node->dim));
1610 isl_int_init(v);
1612 for (i = 0; i < nrow; ++i) {
1613 aff = isl_aff_zero_on_domain(isl_local_space_copy(ls));
1614 isl_mat_get_element(node->sched, i, 0, &v);
1615 aff = isl_aff_set_constant(aff, v);
1616 for (j = 0; j < node->nparam; ++j) {
1617 isl_mat_get_element(node->sched, i, 1 + j, &v);
1618 aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
1620 for (j = 0; j < node->nvar; ++j) {
1621 isl_mat_get_element(node->sched,
1622 i, 1 + node->nparam + j, &v);
1623 aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
1625 ma = isl_multi_aff_set_aff(ma, i, aff);
1628 isl_int_clear(v);
1630 isl_local_space_free(ls);
1632 return ma;
1635 /* Convert node->sched into a map and return this map.
1637 * The result is cached in node->sched_map, which needs to be released
1638 * whenever node->sched is updated.
1640 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
1642 if (!node->sched_map) {
1643 isl_multi_aff *ma;
1645 ma = node_extract_schedule_multi_aff(node);
1646 node->sched_map = isl_map_from_multi_aff(ma);
1649 return isl_map_copy(node->sched_map);
1652 /* Update the given dependence relation based on the current schedule.
1653 * That is, intersect the dependence relation with a map expressing
1654 * that source and sink are executed within the same iteration of
1655 * the current schedule.
1656 * This is not the most efficient way, but this shouldn't be a critical
1657 * operation.
1659 static __isl_give isl_map *specialize(__isl_take isl_map *map,
1660 struct isl_sched_node *src, struct isl_sched_node *dst)
1662 isl_map *src_sched, *dst_sched, *id;
1664 src_sched = node_extract_schedule(src);
1665 dst_sched = node_extract_schedule(dst);
1666 id = isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
1667 return isl_map_intersect(map, id);
1670 /* Update the dependence relations of all edges based on the current schedule.
1671 * If a dependence is carried completely by the current schedule, then
1672 * it is removed from the edge_tables. It is kept in the list of edges
1673 * as otherwise all edge_tables would have to be recomputed.
1675 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
1677 int i;
1679 for (i = graph->n_edge - 1; i >= 0; --i) {
1680 struct isl_sched_edge *edge = &graph->edge[i];
1681 edge->map = specialize(edge->map, edge->src, edge->dst);
1682 if (!edge->map)
1683 return -1;
1685 if (isl_map_plain_is_empty(edge->map))
1686 graph_remove_edge(graph, edge);
1689 return 0;
1692 static void next_band(struct isl_sched_graph *graph)
1694 graph->band_start = graph->n_total_row;
1695 graph->n_band++;
1698 /* Topologically sort statements mapped to the same schedule iteration
1699 * and add a row to the schedule corresponding to this order.
1701 static int sort_statements(isl_ctx *ctx, struct isl_sched_graph *graph)
1703 int i, j;
1705 if (graph->n <= 1)
1706 return 0;
1708 if (update_edges(ctx, graph) < 0)
1709 return -1;
1711 if (graph->n_edge == 0)
1712 return 0;
1714 if (detect_sccs(ctx, graph) < 0)
1715 return -1;
1717 if (graph->n_total_row >= graph->max_row)
1718 isl_die(ctx, isl_error_internal,
1719 "too many schedule rows", return -1);
1721 for (i = 0; i < graph->n; ++i) {
1722 struct isl_sched_node *node = &graph->node[i];
1723 int row = isl_mat_rows(node->sched);
1724 int cols = isl_mat_cols(node->sched);
1726 isl_map_free(node->sched_map);
1727 node->sched_map = NULL;
1728 node->sched = isl_mat_add_rows(node->sched, 1);
1729 if (!node->sched)
1730 return -1;
1731 node->sched = isl_mat_set_element_si(node->sched, row, 0,
1732 node->scc);
1733 for (j = 1; j < cols; ++j)
1734 node->sched = isl_mat_set_element_si(node->sched,
1735 row, j, 0);
1736 node->band[graph->n_total_row] = graph->n_band;
1739 graph->n_total_row++;
1740 next_band(graph);
1742 return 0;
1745 /* Construct an isl_schedule based on the computed schedule stored
1746 * in graph and with parameters specified by dim.
1748 static __isl_give isl_schedule *extract_schedule(struct isl_sched_graph *graph,
1749 __isl_take isl_space *dim)
1751 int i;
1752 isl_ctx *ctx;
1753 isl_schedule *sched = NULL;
1755 if (!dim)
1756 return NULL;
1758 ctx = isl_space_get_ctx(dim);
1759 sched = isl_calloc(ctx, struct isl_schedule,
1760 sizeof(struct isl_schedule) +
1761 (graph->n - 1) * sizeof(struct isl_schedule_node));
1762 if (!sched)
1763 goto error;
1765 sched->ref = 1;
1766 sched->n = graph->n;
1767 sched->n_band = graph->n_band;
1768 sched->n_total_row = graph->n_total_row;
1770 for (i = 0; i < sched->n; ++i) {
1771 int r, b;
1772 int *band_end, *band_id, *zero;
1774 sched->node[i].sched =
1775 node_extract_schedule_multi_aff(&graph->node[i]);
1776 if (!sched->node[i].sched)
1777 goto error;
1779 sched->node[i].n_band = graph->n_band;
1780 if (graph->n_band == 0)
1781 continue;
1783 band_end = isl_alloc_array(ctx, int, graph->n_band);
1784 band_id = isl_alloc_array(ctx, int, graph->n_band);
1785 zero = isl_alloc_array(ctx, int, graph->n_total_row);
1786 sched->node[i].band_end = band_end;
1787 sched->node[i].band_id = band_id;
1788 sched->node[i].zero = zero;
1789 if (!band_end || !band_id || !zero)
1790 goto error;
1792 for (r = 0; r < graph->n_total_row; ++r)
1793 zero[r] = graph->node[i].zero[r];
1794 for (r = b = 0; r < graph->n_total_row; ++r) {
1795 if (graph->node[i].band[r] == b)
1796 continue;
1797 band_end[b++] = r;
1798 if (graph->node[i].band[r] == -1)
1799 break;
1801 if (r == graph->n_total_row)
1802 band_end[b++] = r;
1803 sched->node[i].n_band = b;
1804 for (--b; b >= 0; --b)
1805 band_id[b] = graph->node[i].band_id[b];
1808 sched->dim = dim;
1810 return sched;
1811 error:
1812 isl_space_free(dim);
1813 isl_schedule_free(sched);
1814 return NULL;
1817 /* Copy nodes that satisfy node_pred from the src dependence graph
1818 * to the dst dependence graph.
1820 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
1821 int (*node_pred)(struct isl_sched_node *node, int data), int data)
1823 int i;
1825 dst->n = 0;
1826 for (i = 0; i < src->n; ++i) {
1827 if (!node_pred(&src->node[i], data))
1828 continue;
1829 dst->node[dst->n].dim = isl_space_copy(src->node[i].dim);
1830 dst->node[dst->n].nvar = src->node[i].nvar;
1831 dst->node[dst->n].nparam = src->node[i].nparam;
1832 dst->node[dst->n].sched = isl_mat_copy(src->node[i].sched);
1833 dst->node[dst->n].sched_map =
1834 isl_map_copy(src->node[i].sched_map);
1835 dst->node[dst->n].band = src->node[i].band;
1836 dst->node[dst->n].band_id = src->node[i].band_id;
1837 dst->node[dst->n].zero = src->node[i].zero;
1838 dst->n++;
1841 return 0;
1844 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
1845 * to the dst dependence graph.
1846 * If the source or destination node of the edge is not in the destination
1847 * graph, then it must be a backward proximity edge and it should simply
1848 * be ignored.
1850 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
1851 struct isl_sched_graph *src,
1852 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
1854 int i;
1855 enum isl_edge_type t;
1857 dst->n_edge = 0;
1858 for (i = 0; i < src->n_edge; ++i) {
1859 struct isl_sched_edge *edge = &src->edge[i];
1860 isl_map *map;
1861 struct isl_sched_node *dst_src, *dst_dst;
1863 if (!edge_pred(edge, data))
1864 continue;
1866 if (isl_map_plain_is_empty(edge->map))
1867 continue;
1869 dst_src = graph_find_node(ctx, dst, edge->src->dim);
1870 dst_dst = graph_find_node(ctx, dst, edge->dst->dim);
1871 if (!dst_src || !dst_dst) {
1872 if (edge->validity)
1873 isl_die(ctx, isl_error_internal,
1874 "backward validity edge", return -1);
1875 continue;
1878 map = isl_map_copy(edge->map);
1880 dst->edge[dst->n_edge].src = dst_src;
1881 dst->edge[dst->n_edge].dst = dst_dst;
1882 dst->edge[dst->n_edge].map = map;
1883 dst->edge[dst->n_edge].validity = edge->validity;
1884 dst->edge[dst->n_edge].proximity = edge->proximity;
1885 dst->n_edge++;
1887 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
1888 if (edge !=
1889 graph_find_edge(src, t, edge->src, edge->dst))
1890 continue;
1891 if (graph_edge_table_add(ctx, dst, t,
1892 &dst->edge[dst->n_edge - 1]) < 0)
1893 return -1;
1897 return 0;
1900 /* Given a "src" dependence graph that contains the nodes from "dst"
1901 * that satisfy node_pred, copy the schedule computed in "src"
1902 * for those nodes back to "dst".
1904 static int copy_schedule(struct isl_sched_graph *dst,
1905 struct isl_sched_graph *src,
1906 int (*node_pred)(struct isl_sched_node *node, int data), int data)
1908 int i;
1910 src->n = 0;
1911 for (i = 0; i < dst->n; ++i) {
1912 if (!node_pred(&dst->node[i], data))
1913 continue;
1914 isl_mat_free(dst->node[i].sched);
1915 isl_map_free(dst->node[i].sched_map);
1916 dst->node[i].sched = isl_mat_copy(src->node[src->n].sched);
1917 dst->node[i].sched_map =
1918 isl_map_copy(src->node[src->n].sched_map);
1919 src->n++;
1922 dst->max_row = src->max_row;
1923 dst->n_total_row = src->n_total_row;
1924 dst->n_band = src->n_band;
1926 return 0;
1929 /* Compute the maximal number of variables over all nodes.
1930 * This is the maximal number of linearly independent schedule
1931 * rows that we need to compute.
1932 * Just in case we end up in a part of the dependence graph
1933 * with only lower-dimensional domains, we make sure we will
1934 * compute the required amount of extra linearly independent rows.
1936 static int compute_maxvar(struct isl_sched_graph *graph)
1938 int i;
1940 graph->maxvar = 0;
1941 for (i = 0; i < graph->n; ++i) {
1942 struct isl_sched_node *node = &graph->node[i];
1943 int nvar;
1945 if (node_update_cmap(node) < 0)
1946 return -1;
1947 nvar = node->nvar + graph->n_row - node->rank;
1948 if (nvar > graph->maxvar)
1949 graph->maxvar = nvar;
1952 return 0;
1955 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph);
1956 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph);
1958 /* Compute a schedule for a subgraph of "graph". In particular, for
1959 * the graph composed of nodes that satisfy node_pred and edges that
1960 * that satisfy edge_pred. The caller should precompute the number
1961 * of nodes and edges that satisfy these predicates and pass them along
1962 * as "n" and "n_edge".
1963 * If the subgraph is known to consist of a single component, then wcc should
1964 * be set and then we call compute_schedule_wcc on the constructed subgraph.
1965 * Otherwise, we call compute_schedule, which will check whether the subgraph
1966 * is connected.
1968 static int compute_sub_schedule(isl_ctx *ctx,
1969 struct isl_sched_graph *graph, int n, int n_edge,
1970 int (*node_pred)(struct isl_sched_node *node, int data),
1971 int (*edge_pred)(struct isl_sched_edge *edge, int data),
1972 int data, int wcc)
1974 struct isl_sched_graph split = { 0 };
1975 int t;
1977 if (graph_alloc(ctx, &split, n, n_edge) < 0)
1978 goto error;
1979 if (copy_nodes(&split, graph, node_pred, data) < 0)
1980 goto error;
1981 if (graph_init_table(ctx, &split) < 0)
1982 goto error;
1983 for (t = 0; t <= isl_edge_last; ++t)
1984 split.max_edge[t] = graph->max_edge[t];
1985 if (graph_init_edge_tables(ctx, &split) < 0)
1986 goto error;
1987 if (copy_edges(ctx, &split, graph, edge_pred, data) < 0)
1988 goto error;
1989 split.n_row = graph->n_row;
1990 split.max_row = graph->max_row;
1991 split.n_total_row = graph->n_total_row;
1992 split.n_band = graph->n_band;
1993 split.band_start = graph->band_start;
1995 if (wcc && compute_schedule_wcc(ctx, &split) < 0)
1996 goto error;
1997 if (!wcc && compute_schedule(ctx, &split) < 0)
1998 goto error;
2000 copy_schedule(graph, &split, node_pred, data);
2002 graph_free(ctx, &split);
2003 return 0;
2004 error:
2005 graph_free(ctx, &split);
2006 return -1;
2009 static int node_scc_exactly(struct isl_sched_node *node, int scc)
2011 return node->scc == scc;
2014 static int node_scc_at_most(struct isl_sched_node *node, int scc)
2016 return node->scc <= scc;
2019 static int node_scc_at_least(struct isl_sched_node *node, int scc)
2021 return node->scc >= scc;
2024 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
2026 return edge->src->scc == scc && edge->dst->scc == scc;
2029 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
2031 return edge->dst->scc <= scc;
2034 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
2036 return edge->src->scc >= scc;
2039 /* Pad the schedules of all nodes with zero rows such that in the end
2040 * they all have graph->n_total_row rows.
2041 * The extra rows don't belong to any band, so they get assigned band number -1.
2043 static int pad_schedule(struct isl_sched_graph *graph)
2045 int i, j;
2047 for (i = 0; i < graph->n; ++i) {
2048 struct isl_sched_node *node = &graph->node[i];
2049 int row = isl_mat_rows(node->sched);
2050 if (graph->n_total_row > row) {
2051 isl_map_free(node->sched_map);
2052 node->sched_map = NULL;
2054 node->sched = isl_mat_add_zero_rows(node->sched,
2055 graph->n_total_row - row);
2056 if (!node->sched)
2057 return -1;
2058 for (j = row; j < graph->n_total_row; ++j)
2059 node->band[j] = -1;
2062 return 0;
2065 /* Split the current graph into two parts and compute a schedule for each
2066 * part individually. In particular, one part consists of all SCCs up
2067 * to and including graph->src_scc, while the other part contains the other
2068 * SCCS.
2070 * The split is enforced in the schedule by constant rows with two different
2071 * values (0 and 1). These constant rows replace the previously computed rows
2072 * in the current band.
2073 * It would be possible to reuse them as the first rows in the next
2074 * band, but recomputing them may result in better rows as we are looking
2075 * at a smaller part of the dependence graph.
2076 * compute_split_schedule is only called when no zero-distance schedule row
2077 * could be found on the entire graph, so we wark the splitting row as
2078 * non zero-distance.
2080 * The band_id of the second group is set to n, where n is the number
2081 * of nodes in the first group. This ensures that the band_ids over
2082 * the two groups remain disjoint, even if either or both of the two
2083 * groups contain independent components.
2085 static int compute_split_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
2087 int i, j, n, e1, e2;
2088 int n_total_row, orig_total_row;
2089 int n_band, orig_band;
2090 int drop;
2092 if (graph->n_total_row >= graph->max_row)
2093 isl_die(ctx, isl_error_internal,
2094 "too many schedule rows", return -1);
2096 drop = graph->n_total_row - graph->band_start;
2097 graph->n_total_row -= drop;
2098 graph->n_row -= drop;
2100 n = 0;
2101 for (i = 0; i < graph->n; ++i) {
2102 struct isl_sched_node *node = &graph->node[i];
2103 int row = isl_mat_rows(node->sched) - drop;
2104 int cols = isl_mat_cols(node->sched);
2105 int before = node->scc <= graph->src_scc;
2107 if (before)
2108 n++;
2110 isl_map_free(node->sched_map);
2111 node->sched_map = NULL;
2112 node->sched = isl_mat_drop_rows(node->sched,
2113 graph->band_start, drop);
2114 node->sched = isl_mat_add_rows(node->sched, 1);
2115 if (!node->sched)
2116 return -1;
2117 node->sched = isl_mat_set_element_si(node->sched, row, 0,
2118 !before);
2119 for (j = 1; j < cols; ++j)
2120 node->sched = isl_mat_set_element_si(node->sched,
2121 row, j, 0);
2122 node->band[graph->n_total_row] = graph->n_band;
2123 node->zero[graph->n_total_row] = 0;
2126 e1 = e2 = 0;
2127 for (i = 0; i < graph->n_edge; ++i) {
2128 if (graph->edge[i].dst->scc <= graph->src_scc)
2129 e1++;
2130 if (graph->edge[i].src->scc > graph->src_scc)
2131 e2++;
2134 graph->n_total_row++;
2135 next_band(graph);
2137 for (i = 0; i < graph->n; ++i) {
2138 struct isl_sched_node *node = &graph->node[i];
2139 if (node->scc > graph->src_scc)
2140 node->band_id[graph->n_band] = n;
2143 orig_total_row = graph->n_total_row;
2144 orig_band = graph->n_band;
2145 if (compute_sub_schedule(ctx, graph, n, e1,
2146 &node_scc_at_most, &edge_dst_scc_at_most,
2147 graph->src_scc, 0) < 0)
2148 return -1;
2149 n_total_row = graph->n_total_row;
2150 graph->n_total_row = orig_total_row;
2151 n_band = graph->n_band;
2152 graph->n_band = orig_band;
2153 if (compute_sub_schedule(ctx, graph, graph->n - n, e2,
2154 &node_scc_at_least, &edge_src_scc_at_least,
2155 graph->src_scc + 1, 0) < 0)
2156 return -1;
2157 if (n_total_row > graph->n_total_row)
2158 graph->n_total_row = n_total_row;
2159 if (n_band > graph->n_band)
2160 graph->n_band = n_band;
2162 return pad_schedule(graph);
2165 /* Compute the next band of the schedule after updating the dependence
2166 * relations based on the the current schedule.
2168 static int compute_next_band(isl_ctx *ctx, struct isl_sched_graph *graph)
2170 if (update_edges(ctx, graph) < 0)
2171 return -1;
2172 next_band(graph);
2174 return compute_schedule(ctx, graph);
2177 /* Add constraints to graph->lp that force the dependence "map" (which
2178 * is part of the dependence relation of "edge")
2179 * to be respected and attempt to carry it, where the edge is one from
2180 * a node j to itself. "pos" is the sequence number of the given map.
2181 * That is, add constraints that enforce
2183 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
2184 * = c_j_x (y - x) >= e_i
2186 * for each (x,y) in R.
2187 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2188 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
2189 * with each coefficient in c_j_x represented as a pair of non-negative
2190 * coefficients.
2192 static int add_intra_constraints(struct isl_sched_graph *graph,
2193 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
2195 unsigned total;
2196 isl_ctx *ctx = isl_map_get_ctx(map);
2197 isl_space *dim;
2198 isl_dim_map *dim_map;
2199 isl_basic_set *coef;
2200 struct isl_sched_node *node = edge->src;
2202 coef = intra_coefficients(graph, map);
2203 if (!coef)
2204 return -1;
2206 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
2208 total = isl_basic_set_total_dim(graph->lp);
2209 dim_map = isl_dim_map_alloc(ctx, total);
2210 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
2211 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
2212 isl_space_dim(dim, isl_dim_set), 1,
2213 node->nvar, -1);
2214 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
2215 isl_space_dim(dim, isl_dim_set), 1,
2216 node->nvar, 1);
2217 graph->lp = isl_basic_set_extend_constraints(graph->lp,
2218 coef->n_eq, coef->n_ineq);
2219 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
2220 coef, dim_map);
2221 isl_space_free(dim);
2223 return 0;
2226 /* Add constraints to graph->lp that force the dependence "map" (which
2227 * is part of the dependence relation of "edge")
2228 * to be respected and attempt to carry it, where the edge is one from
2229 * node j to node k. "pos" is the sequence number of the given map.
2230 * That is, add constraints that enforce
2232 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
2234 * for each (x,y) in R.
2235 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2236 * of valid constraints for R and then plug in
2237 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
2238 * with each coefficient (except e_i, c_k_0 and c_j_0)
2239 * represented as a pair of non-negative coefficients.
2241 static int add_inter_constraints(struct isl_sched_graph *graph,
2242 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
2244 unsigned total;
2245 isl_ctx *ctx = isl_map_get_ctx(map);
2246 isl_space *dim;
2247 isl_dim_map *dim_map;
2248 isl_basic_set *coef;
2249 struct isl_sched_node *src = edge->src;
2250 struct isl_sched_node *dst = edge->dst;
2252 coef = inter_coefficients(graph, map);
2253 if (!coef)
2254 return -1;
2256 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
2258 total = isl_basic_set_total_dim(graph->lp);
2259 dim_map = isl_dim_map_alloc(ctx, total);
2261 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
2263 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
2264 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
2265 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
2266 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
2267 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
2268 dst->nvar, -1);
2269 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
2270 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
2271 dst->nvar, 1);
2273 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
2274 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
2275 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
2276 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
2277 isl_space_dim(dim, isl_dim_set), 1,
2278 src->nvar, 1);
2279 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
2280 isl_space_dim(dim, isl_dim_set), 1,
2281 src->nvar, -1);
2283 graph->lp = isl_basic_set_extend_constraints(graph->lp,
2284 coef->n_eq, coef->n_ineq);
2285 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
2286 coef, dim_map);
2287 isl_space_free(dim);
2289 return 0;
2292 /* Add constraints to graph->lp that force all validity dependences
2293 * to be respected and attempt to carry them.
2295 static int add_all_constraints(struct isl_sched_graph *graph)
2297 int i, j;
2298 int pos;
2300 pos = 0;
2301 for (i = 0; i < graph->n_edge; ++i) {
2302 struct isl_sched_edge *edge= &graph->edge[i];
2304 if (!edge->validity)
2305 continue;
2307 for (j = 0; j < edge->map->n; ++j) {
2308 isl_basic_map *bmap;
2309 isl_map *map;
2311 bmap = isl_basic_map_copy(edge->map->p[j]);
2312 map = isl_map_from_basic_map(bmap);
2314 if (edge->src == edge->dst &&
2315 add_intra_constraints(graph, edge, map, pos) < 0)
2316 return -1;
2317 if (edge->src != edge->dst &&
2318 add_inter_constraints(graph, edge, map, pos) < 0)
2319 return -1;
2320 ++pos;
2324 return 0;
2327 /* Count the number of equality and inequality constraints
2328 * that will be added to the carry_lp problem.
2329 * We count each edge exactly once.
2331 static int count_all_constraints(struct isl_sched_graph *graph,
2332 int *n_eq, int *n_ineq)
2334 int i, j;
2336 *n_eq = *n_ineq = 0;
2337 for (i = 0; i < graph->n_edge; ++i) {
2338 struct isl_sched_edge *edge= &graph->edge[i];
2339 for (j = 0; j < edge->map->n; ++j) {
2340 isl_basic_map *bmap;
2341 isl_map *map;
2343 bmap = isl_basic_map_copy(edge->map->p[j]);
2344 map = isl_map_from_basic_map(bmap);
2346 if (count_map_constraints(graph, edge, map,
2347 n_eq, n_ineq, 1) < 0)
2348 return -1;
2352 return 0;
2355 /* Construct an LP problem for finding schedule coefficients
2356 * such that the schedule carries as many dependences as possible.
2357 * In particular, for each dependence i, we bound the dependence distance
2358 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
2359 * of all e_i's. Dependence with e_i = 0 in the solution are simply
2360 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
2361 * Note that if the dependence relation is a union of basic maps,
2362 * then we have to consider each basic map individually as it may only
2363 * be possible to carry the dependences expressed by some of those
2364 * basic maps and not all off them.
2365 * Below, we consider each of those basic maps as a separate "edge".
2367 * All variables of the LP are non-negative. The actual coefficients
2368 * may be negative, so each coefficient is represented as the difference
2369 * of two non-negative variables. The negative part always appears
2370 * immediately before the positive part.
2371 * Other than that, the variables have the following order
2373 * - sum of (1 - e_i) over all edges
2374 * - sum of positive and negative parts of all c_n coefficients
2375 * (unconstrained when computing non-parametric schedules)
2376 * - sum of positive and negative parts of all c_x coefficients
2377 * - for each edge
2378 * - e_i
2379 * - for each node
2380 * - c_i_0
2381 * - positive and negative parts of c_i_n (if parametric)
2382 * - positive and negative parts of c_i_x
2384 * The constraints are those from the (validity) edges plus three equalities
2385 * to express the sums and n_edge inequalities to express e_i <= 1.
2387 static int setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
2389 int i, j;
2390 int k;
2391 isl_space *dim;
2392 unsigned total;
2393 int n_eq, n_ineq;
2394 int n_edge;
2396 n_edge = 0;
2397 for (i = 0; i < graph->n_edge; ++i)
2398 n_edge += graph->edge[i].map->n;
2400 total = 3 + n_edge;
2401 for (i = 0; i < graph->n; ++i) {
2402 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2403 node->start = total;
2404 total += 1 + 2 * (node->nparam + node->nvar);
2407 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
2408 return -1;
2410 dim = isl_space_set_alloc(ctx, 0, total);
2411 isl_basic_set_free(graph->lp);
2412 n_eq += 3;
2413 n_ineq += n_edge;
2414 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
2415 graph->lp = isl_basic_set_set_rational(graph->lp);
2417 k = isl_basic_set_alloc_equality(graph->lp);
2418 if (k < 0)
2419 return -1;
2420 isl_seq_clr(graph->lp->eq[k], 1 + total);
2421 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
2422 isl_int_set_si(graph->lp->eq[k][1], 1);
2423 for (i = 0; i < n_edge; ++i)
2424 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
2426 k = isl_basic_set_alloc_equality(graph->lp);
2427 if (k < 0)
2428 return -1;
2429 isl_seq_clr(graph->lp->eq[k], 1 + total);
2430 isl_int_set_si(graph->lp->eq[k][2], -1);
2431 for (i = 0; i < graph->n; ++i) {
2432 int pos = 1 + graph->node[i].start + 1;
2434 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
2435 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2438 k = isl_basic_set_alloc_equality(graph->lp);
2439 if (k < 0)
2440 return -1;
2441 isl_seq_clr(graph->lp->eq[k], 1 + total);
2442 isl_int_set_si(graph->lp->eq[k][3], -1);
2443 for (i = 0; i < graph->n; ++i) {
2444 struct isl_sched_node *node = &graph->node[i];
2445 int pos = 1 + node->start + 1 + 2 * node->nparam;
2447 for (j = 0; j < 2 * node->nvar; ++j)
2448 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2451 for (i = 0; i < n_edge; ++i) {
2452 k = isl_basic_set_alloc_inequality(graph->lp);
2453 if (k < 0)
2454 return -1;
2455 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2456 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
2457 isl_int_set_si(graph->lp->ineq[k][0], 1);
2460 if (add_all_constraints(graph) < 0)
2461 return -1;
2463 return 0;
2466 /* If the schedule_split_scaled option is set and if the linear
2467 * parts of the scheduling rows for all nodes in the graphs have
2468 * non-trivial common divisor, then split off the constant term
2469 * from the linear part.
2470 * The constant term is then placed in a separate band and
2471 * the linear part is reduced.
2473 static int split_scaled(isl_ctx *ctx, struct isl_sched_graph *graph)
2475 int i;
2476 int row;
2477 isl_int gcd, gcd_i;
2479 if (!ctx->opt->schedule_split_scaled)
2480 return 0;
2481 if (graph->n <= 1)
2482 return 0;
2484 if (graph->n_total_row >= graph->max_row)
2485 isl_die(ctx, isl_error_internal,
2486 "too many schedule rows", return -1);
2488 isl_int_init(gcd);
2489 isl_int_init(gcd_i);
2491 isl_int_set_si(gcd, 0);
2493 row = isl_mat_rows(graph->node[0].sched) - 1;
2495 for (i = 0; i < graph->n; ++i) {
2496 struct isl_sched_node *node = &graph->node[i];
2497 int cols = isl_mat_cols(node->sched);
2499 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
2500 isl_int_gcd(gcd, gcd, gcd_i);
2503 isl_int_clear(gcd_i);
2505 if (isl_int_cmp_si(gcd, 1) <= 0) {
2506 isl_int_clear(gcd);
2507 return 0;
2510 next_band(graph);
2512 for (i = 0; i < graph->n; ++i) {
2513 struct isl_sched_node *node = &graph->node[i];
2515 isl_map_free(node->sched_map);
2516 node->sched_map = NULL;
2517 node->sched = isl_mat_add_zero_rows(node->sched, 1);
2518 if (!node->sched)
2519 goto error;
2520 isl_int_fdiv_r(node->sched->row[row + 1][0],
2521 node->sched->row[row][0], gcd);
2522 isl_int_fdiv_q(node->sched->row[row][0],
2523 node->sched->row[row][0], gcd);
2524 isl_int_mul(node->sched->row[row][0],
2525 node->sched->row[row][0], gcd);
2526 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
2527 if (!node->sched)
2528 goto error;
2529 node->band[graph->n_total_row] = graph->n_band;
2532 graph->n_total_row++;
2534 isl_int_clear(gcd);
2535 return 0;
2536 error:
2537 isl_int_clear(gcd);
2538 return -1;
2541 static int compute_component_schedule(isl_ctx *ctx,
2542 struct isl_sched_graph *graph);
2544 /* Is the schedule row "sol" trivial on node "node"?
2545 * That is, is the solution zero on the dimensions orthogonal to
2546 * the previously found solutions?
2547 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
2549 * Each coefficient is represented as the difference between
2550 * two non-negative values in "sol". "sol" has been computed
2551 * in terms of the original iterators (i.e., without use of cmap).
2552 * We construct the schedule row s and write it as a linear
2553 * combination of (linear combinations of) previously computed schedule rows.
2554 * s = Q c or c = U s.
2555 * If the final entries of c are all zero, then the solution is trivial.
2557 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
2559 int i;
2560 int pos;
2561 int trivial;
2562 isl_ctx *ctx;
2563 isl_vec *node_sol;
2565 if (!sol)
2566 return -1;
2567 if (node->nvar == node->rank)
2568 return 0;
2570 ctx = isl_vec_get_ctx(sol);
2571 node_sol = isl_vec_alloc(ctx, node->nvar);
2572 if (!node_sol)
2573 return -1;
2575 pos = 1 + node->start + 1 + 2 * node->nparam;
2577 for (i = 0; i < node->nvar; ++i)
2578 isl_int_sub(node_sol->el[i],
2579 sol->el[pos + 2 * i + 1], sol->el[pos + 2 * i]);
2581 node_sol = isl_mat_vec_product(isl_mat_copy(node->cinv), node_sol);
2583 if (!node_sol)
2584 return -1;
2586 trivial = isl_seq_first_non_zero(node_sol->el + node->rank,
2587 node->nvar - node->rank) == -1;
2589 isl_vec_free(node_sol);
2591 return trivial;
2594 /* Is the schedule row "sol" trivial on any node where it should
2595 * not be trivial?
2596 * "sol" has been computed in terms of the original iterators
2597 * (i.e., without use of cmap).
2598 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
2600 static int is_any_trivial(struct isl_sched_graph *graph,
2601 __isl_keep isl_vec *sol)
2603 int i;
2605 for (i = 0; i < graph->n; ++i) {
2606 struct isl_sched_node *node = &graph->node[i];
2607 int trivial;
2609 if (!needs_row(graph, node))
2610 continue;
2611 trivial = is_trivial(node, sol);
2612 if (trivial < 0 || trivial)
2613 return trivial;
2616 return 0;
2619 /* Construct a schedule row for each node such that as many dependences
2620 * as possible are carried and then continue with the next band.
2622 * If the computed schedule row turns out to be trivial on one or
2623 * more nodes where it should not be trivial, then we throw it away
2624 * and try again on each component separately.
2626 static int carry_dependences(isl_ctx *ctx, struct isl_sched_graph *graph)
2628 int i;
2629 int n_edge;
2630 int trivial;
2631 isl_vec *sol;
2632 isl_basic_set *lp;
2634 n_edge = 0;
2635 for (i = 0; i < graph->n_edge; ++i)
2636 n_edge += graph->edge[i].map->n;
2638 if (setup_carry_lp(ctx, graph) < 0)
2639 return -1;
2641 lp = isl_basic_set_copy(graph->lp);
2642 sol = isl_tab_basic_set_non_neg_lexmin(lp);
2643 if (!sol)
2644 return -1;
2646 if (sol->size == 0) {
2647 isl_vec_free(sol);
2648 isl_die(ctx, isl_error_internal,
2649 "error in schedule construction", return -1);
2652 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
2653 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
2654 isl_vec_free(sol);
2655 isl_die(ctx, isl_error_unknown,
2656 "unable to carry dependences", return -1);
2659 trivial = is_any_trivial(graph, sol);
2660 if (trivial < 0) {
2661 sol = isl_vec_free(sol);
2662 } else if (trivial) {
2663 isl_vec_free(sol);
2664 if (graph->scc > 1)
2665 return compute_component_schedule(ctx, graph);
2666 isl_die(ctx, isl_error_unknown,
2667 "unable to construct non-trivial solution", return -1);
2670 if (update_schedule(graph, sol, 0, 0) < 0)
2671 return -1;
2673 if (split_scaled(ctx, graph) < 0)
2674 return -1;
2676 return compute_next_band(ctx, graph);
2679 /* Are there any (non-empty) validity edges in the graph?
2681 static int has_validity_edges(struct isl_sched_graph *graph)
2683 int i;
2685 for (i = 0; i < graph->n_edge; ++i) {
2686 int empty;
2688 empty = isl_map_plain_is_empty(graph->edge[i].map);
2689 if (empty < 0)
2690 return -1;
2691 if (empty)
2692 continue;
2693 if (graph->edge[i].validity)
2694 return 1;
2697 return 0;
2700 /* Should we apply a Feautrier step?
2701 * That is, did the user request the Feautrier algorithm and are
2702 * there any validity dependences (left)?
2704 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
2706 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
2707 return 0;
2709 return has_validity_edges(graph);
2712 /* Compute a schedule for a connected dependence graph using Feautrier's
2713 * multi-dimensional scheduling algorithm.
2714 * The original algorithm is described in [1].
2715 * The main idea is to minimize the number of scheduling dimensions, by
2716 * trying to satisfy as many dependences as possible per scheduling dimension.
2718 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
2719 * Problem, Part II: Multi-Dimensional Time.
2720 * In Intl. Journal of Parallel Programming, 1992.
2722 static int compute_schedule_wcc_feautrier(isl_ctx *ctx,
2723 struct isl_sched_graph *graph)
2725 return carry_dependences(ctx, graph);
2728 /* Compute a schedule for a connected dependence graph.
2729 * We try to find a sequence of as many schedule rows as possible that result
2730 * in non-negative dependence distances (independent of the previous rows
2731 * in the sequence, i.e., such that the sequence is tilable).
2732 * If we can't find any more rows we either
2733 * - split between SCCs and start over (assuming we found an interesting
2734 * pair of SCCs between which to split)
2735 * - continue with the next band (assuming the current band has at least
2736 * one row)
2737 * - try to carry as many dependences as possible and continue with the next
2738 * band
2740 * If Feautrier's algorithm is selected, we first recursively try to satisfy
2741 * as many validity dependences as possible. When all validity dependences
2742 * are satisfied we extend the schedule to a full-dimensional schedule.
2744 * If we manage to complete the schedule, we finish off by topologically
2745 * sorting the statements based on the remaining dependences.
2747 * If ctx->opt->schedule_outer_zero_distance is set, then we force the
2748 * outermost dimension in the current band to be zero distance. If this
2749 * turns out to be impossible, we fall back on the general scheme above
2750 * and try to carry as many dependences as possible.
2752 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph)
2754 int force_zero = 0;
2756 if (detect_sccs(ctx, graph) < 0)
2757 return -1;
2758 if (sort_sccs(graph) < 0)
2759 return -1;
2761 if (compute_maxvar(graph) < 0)
2762 return -1;
2764 if (need_feautrier_step(ctx, graph))
2765 return compute_schedule_wcc_feautrier(ctx, graph);
2767 if (ctx->opt->schedule_outer_zero_distance)
2768 force_zero = 1;
2770 while (graph->n_row < graph->maxvar) {
2771 isl_vec *sol;
2773 graph->src_scc = -1;
2774 graph->dst_scc = -1;
2776 if (setup_lp(ctx, graph, force_zero) < 0)
2777 return -1;
2778 sol = solve_lp(graph);
2779 if (!sol)
2780 return -1;
2781 if (sol->size == 0) {
2782 isl_vec_free(sol);
2783 if (!ctx->opt->schedule_maximize_band_depth &&
2784 graph->n_total_row > graph->band_start)
2785 return compute_next_band(ctx, graph);
2786 if (graph->src_scc >= 0)
2787 return compute_split_schedule(ctx, graph);
2788 if (graph->n_total_row > graph->band_start)
2789 return compute_next_band(ctx, graph);
2790 return carry_dependences(ctx, graph);
2792 if (update_schedule(graph, sol, 1, 1) < 0)
2793 return -1;
2794 force_zero = 0;
2797 if (graph->n_total_row > graph->band_start)
2798 next_band(graph);
2799 return sort_statements(ctx, graph);
2802 /* Add a row to the schedules that separates the SCCs and move
2803 * to the next band.
2805 static int split_on_scc(isl_ctx *ctx, struct isl_sched_graph *graph)
2807 int i;
2809 if (graph->n_total_row >= graph->max_row)
2810 isl_die(ctx, isl_error_internal,
2811 "too many schedule rows", return -1);
2813 for (i = 0; i < graph->n; ++i) {
2814 struct isl_sched_node *node = &graph->node[i];
2815 int row = isl_mat_rows(node->sched);
2817 isl_map_free(node->sched_map);
2818 node->sched_map = NULL;
2819 node->sched = isl_mat_add_zero_rows(node->sched, 1);
2820 node->sched = isl_mat_set_element_si(node->sched, row, 0,
2821 node->scc);
2822 if (!node->sched)
2823 return -1;
2824 node->band[graph->n_total_row] = graph->n_band;
2827 graph->n_total_row++;
2828 next_band(graph);
2830 return 0;
2833 /* Compute a schedule for each component (identified by node->scc)
2834 * of the dependence graph separately and then combine the results.
2835 * Depending on the setting of schedule_fuse, a component may be
2836 * either weakly or strongly connected.
2838 * The band_id is adjusted such that each component has a separate id.
2839 * Note that the band_id may have already been set to a value different
2840 * from zero by compute_split_schedule.
2842 static int compute_component_schedule(isl_ctx *ctx,
2843 struct isl_sched_graph *graph)
2845 int wcc, i;
2846 int n, n_edge;
2847 int n_total_row, orig_total_row;
2848 int n_band, orig_band;
2850 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN ||
2851 ctx->opt->schedule_separate_components)
2852 if (split_on_scc(ctx, graph) < 0)
2853 return -1;
2855 n_total_row = 0;
2856 orig_total_row = graph->n_total_row;
2857 n_band = 0;
2858 orig_band = graph->n_band;
2859 for (i = 0; i < graph->n; ++i)
2860 graph->node[i].band_id[graph->n_band] += graph->node[i].scc;
2861 for (wcc = 0; wcc < graph->scc; ++wcc) {
2862 n = 0;
2863 for (i = 0; i < graph->n; ++i)
2864 if (graph->node[i].scc == wcc)
2865 n++;
2866 n_edge = 0;
2867 for (i = 0; i < graph->n_edge; ++i)
2868 if (graph->edge[i].src->scc == wcc &&
2869 graph->edge[i].dst->scc == wcc)
2870 n_edge++;
2872 if (compute_sub_schedule(ctx, graph, n, n_edge,
2873 &node_scc_exactly,
2874 &edge_scc_exactly, wcc, 1) < 0)
2875 return -1;
2876 if (graph->n_total_row > n_total_row)
2877 n_total_row = graph->n_total_row;
2878 graph->n_total_row = orig_total_row;
2879 if (graph->n_band > n_band)
2880 n_band = graph->n_band;
2881 graph->n_band = orig_band;
2884 graph->n_total_row = n_total_row;
2885 graph->n_band = n_band;
2887 return pad_schedule(graph);
2890 /* Compute a schedule for the given dependence graph.
2891 * We first check if the graph is connected (through validity dependences)
2892 * and, if not, compute a schedule for each component separately.
2893 * If schedule_fuse is set to minimal fusion, then we check for strongly
2894 * connected components instead and compute a separate schedule for
2895 * each such strongly connected component.
2897 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
2899 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN) {
2900 if (detect_sccs(ctx, graph) < 0)
2901 return -1;
2902 } else {
2903 if (detect_wccs(ctx, graph) < 0)
2904 return -1;
2907 if (graph->scc > 1)
2908 return compute_component_schedule(ctx, graph);
2910 return compute_schedule_wcc(ctx, graph);
2913 /* Compute a schedule for the given union of domains that respects
2914 * all the validity dependences.
2915 * If the default isl scheduling algorithm is used, it tries to minimize
2916 * the dependence distances over the proximity dependences.
2917 * If Feautrier's scheduling algorithm is used, the proximity dependence
2918 * distances are only minimized during the extension to a full-dimensional
2919 * schedule.
2921 __isl_give isl_schedule *isl_union_set_compute_schedule(
2922 __isl_take isl_union_set *domain,
2923 __isl_take isl_union_map *validity,
2924 __isl_take isl_union_map *proximity)
2926 isl_ctx *ctx = isl_union_set_get_ctx(domain);
2927 isl_space *dim;
2928 struct isl_sched_graph graph = { 0 };
2929 isl_schedule *sched;
2930 struct isl_extract_edge_data data;
2932 domain = isl_union_set_align_params(domain,
2933 isl_union_map_get_space(validity));
2934 domain = isl_union_set_align_params(domain,
2935 isl_union_map_get_space(proximity));
2936 dim = isl_union_set_get_space(domain);
2937 validity = isl_union_map_align_params(validity, isl_space_copy(dim));
2938 proximity = isl_union_map_align_params(proximity, dim);
2940 if (!domain)
2941 goto error;
2943 graph.n = isl_union_set_n_set(domain);
2944 if (graph.n == 0)
2945 goto empty;
2946 if (graph_alloc(ctx, &graph, graph.n,
2947 isl_union_map_n_map(validity) + isl_union_map_n_map(proximity)) < 0)
2948 goto error;
2949 if (compute_max_row(&graph, domain) < 0)
2950 goto error;
2951 graph.root = 1;
2952 graph.n = 0;
2953 if (isl_union_set_foreach_set(domain, &extract_node, &graph) < 0)
2954 goto error;
2955 if (graph_init_table(ctx, &graph) < 0)
2956 goto error;
2957 graph.max_edge[isl_edge_validity] = isl_union_map_n_map(validity);
2958 graph.max_edge[isl_edge_proximity] = isl_union_map_n_map(proximity);
2959 if (graph_init_edge_tables(ctx, &graph) < 0)
2960 goto error;
2961 graph.n_edge = 0;
2962 data.graph = &graph;
2963 data.type = isl_edge_validity;
2964 if (isl_union_map_foreach_map(validity, &extract_edge, &data) < 0)
2965 goto error;
2966 data.type = isl_edge_proximity;
2967 if (isl_union_map_foreach_map(proximity, &extract_edge, &data) < 0)
2968 goto error;
2970 if (compute_schedule(ctx, &graph) < 0)
2971 goto error;
2973 empty:
2974 sched = extract_schedule(&graph, isl_union_set_get_space(domain));
2976 graph_free(ctx, &graph);
2977 isl_union_set_free(domain);
2978 isl_union_map_free(validity);
2979 isl_union_map_free(proximity);
2981 return sched;
2982 error:
2983 graph_free(ctx, &graph);
2984 isl_union_set_free(domain);
2985 isl_union_map_free(validity);
2986 isl_union_map_free(proximity);
2987 return NULL;
2990 void *isl_schedule_free(__isl_take isl_schedule *sched)
2992 int i;
2993 if (!sched)
2994 return NULL;
2996 if (--sched->ref > 0)
2997 return NULL;
2999 for (i = 0; i < sched->n; ++i) {
3000 isl_multi_aff_free(sched->node[i].sched);
3001 free(sched->node[i].band_end);
3002 free(sched->node[i].band_id);
3003 free(sched->node[i].zero);
3005 isl_space_free(sched->dim);
3006 isl_band_list_free(sched->band_forest);
3007 free(sched);
3008 return NULL;
3011 isl_ctx *isl_schedule_get_ctx(__isl_keep isl_schedule *schedule)
3013 return schedule ? isl_space_get_ctx(schedule->dim) : NULL;
3016 /* Set max_out to the maximal number of output dimensions over
3017 * all maps.
3019 static int update_max_out(__isl_take isl_map *map, void *user)
3021 int *max_out = user;
3022 int n_out = isl_map_dim(map, isl_dim_out);
3024 if (n_out > *max_out)
3025 *max_out = n_out;
3027 isl_map_free(map);
3028 return 0;
3031 /* Internal data structure for map_pad_range.
3033 * "max_out" is the maximal schedule dimension.
3034 * "res" collects the results.
3036 struct isl_pad_schedule_map_data {
3037 int max_out;
3038 isl_union_map *res;
3041 /* Pad the range of the given map with zeros to data->max_out and
3042 * then add the result to data->res.
3044 static int map_pad_range(__isl_take isl_map *map, void *user)
3046 struct isl_pad_schedule_map_data *data = user;
3047 int i;
3048 int n_out = isl_map_dim(map, isl_dim_out);
3050 map = isl_map_add_dims(map, isl_dim_out, data->max_out - n_out);
3051 for (i = n_out; i < data->max_out; ++i)
3052 map = isl_map_fix_si(map, isl_dim_out, i, 0);
3054 data->res = isl_union_map_add_map(data->res, map);
3055 if (!data->res)
3056 return -1;
3058 return 0;
3061 /* Pad the ranges of the maps in the union map with zeros such they all have
3062 * the same dimension.
3064 static __isl_give isl_union_map *pad_schedule_map(
3065 __isl_take isl_union_map *umap)
3067 struct isl_pad_schedule_map_data data;
3069 if (!umap)
3070 return NULL;
3071 if (isl_union_map_n_map(umap) <= 1)
3072 return umap;
3074 data.max_out = 0;
3075 if (isl_union_map_foreach_map(umap, &update_max_out, &data.max_out) < 0)
3076 return isl_union_map_free(umap);
3078 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3079 if (isl_union_map_foreach_map(umap, &map_pad_range, &data) < 0)
3080 data.res = isl_union_map_free(data.res);
3082 isl_union_map_free(umap);
3083 return data.res;
3086 /* Return an isl_union_map of the schedule. If we have already constructed
3087 * a band forest, then this band forest may have been modified so we need
3088 * to extract the isl_union_map from the forest rather than from
3089 * the originally computed schedule. This reconstructed schedule map
3090 * then needs to be padded with zeros to unify the schedule space
3091 * since the result of isl_band_list_get_suffix_schedule may not have
3092 * a unified schedule space.
3094 __isl_give isl_union_map *isl_schedule_get_map(__isl_keep isl_schedule *sched)
3096 int i;
3097 isl_union_map *umap;
3099 if (!sched)
3100 return NULL;
3102 if (sched->band_forest) {
3103 umap = isl_band_list_get_suffix_schedule(sched->band_forest);
3104 return pad_schedule_map(umap);
3107 umap = isl_union_map_empty(isl_space_copy(sched->dim));
3108 for (i = 0; i < sched->n; ++i) {
3109 isl_multi_aff *ma;
3111 ma = isl_multi_aff_copy(sched->node[i].sched);
3112 umap = isl_union_map_add_map(umap, isl_map_from_multi_aff(ma));
3115 return umap;
3118 static __isl_give isl_band_list *construct_band_list(
3119 __isl_keep isl_schedule *schedule, __isl_keep isl_band *parent,
3120 int band_nr, int *parent_active, int n_active);
3122 /* Construct an isl_band structure for the band in the given schedule
3123 * with sequence number band_nr for the n_active nodes marked by active.
3124 * If the nodes don't have a band with the given sequence number,
3125 * then a band without members is created.
3127 * Because of the way the schedule is constructed, we know that
3128 * the position of the band inside the schedule of a node is the same
3129 * for all active nodes.
3131 * The partial schedule for the band is created before the children
3132 * are created to that construct_band_list can refer to the partial
3133 * schedule of the parent.
3135 static __isl_give isl_band *construct_band(__isl_keep isl_schedule *schedule,
3136 __isl_keep isl_band *parent,
3137 int band_nr, int *active, int n_active)
3139 int i, j;
3140 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
3141 isl_band *band;
3142 unsigned start, end;
3144 band = isl_band_alloc(ctx);
3145 if (!band)
3146 return NULL;
3148 band->schedule = schedule;
3149 band->parent = parent;
3151 for (i = 0; i < schedule->n; ++i)
3152 if (active[i])
3153 break;
3155 if (i >= schedule->n)
3156 isl_die(ctx, isl_error_internal,
3157 "band without active statements", goto error);
3159 start = band_nr ? schedule->node[i].band_end[band_nr - 1] : 0;
3160 end = band_nr < schedule->node[i].n_band ?
3161 schedule->node[i].band_end[band_nr] : start;
3162 band->n = end - start;
3164 band->zero = isl_alloc_array(ctx, int, band->n);
3165 if (band->n && !band->zero)
3166 goto error;
3168 for (j = 0; j < band->n; ++j)
3169 band->zero[j] = schedule->node[i].zero[start + j];
3171 band->pma = isl_union_pw_multi_aff_empty(isl_space_copy(schedule->dim));
3172 for (i = 0; i < schedule->n; ++i) {
3173 isl_multi_aff *ma;
3174 isl_pw_multi_aff *pma;
3175 unsigned n_out;
3177 if (!active[i])
3178 continue;
3180 ma = isl_multi_aff_copy(schedule->node[i].sched);
3181 n_out = isl_multi_aff_dim(ma, isl_dim_out);
3182 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, end, n_out - end);
3183 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, 0, start);
3184 pma = isl_pw_multi_aff_from_multi_aff(ma);
3185 band->pma = isl_union_pw_multi_aff_add_pw_multi_aff(band->pma,
3186 pma);
3188 if (!band->pma)
3189 goto error;
3191 for (i = 0; i < schedule->n; ++i)
3192 if (active[i] && schedule->node[i].n_band > band_nr + 1)
3193 break;
3195 if (i < schedule->n) {
3196 band->children = construct_band_list(schedule, band,
3197 band_nr + 1, active, n_active);
3198 if (!band->children)
3199 goto error;
3202 return band;
3203 error:
3204 isl_band_free(band);
3205 return NULL;
3208 /* Internal data structure used inside cmp_band and pw_multi_aff_extract_int.
3210 * r is set to a negative value if anything goes wrong.
3212 * c1 stores the result of extract_int.
3213 * c2 is a temporary value used inside cmp_band_in_ancestor.
3214 * t is a temporary value used inside extract_int.
3216 * first and equal are used inside extract_int.
3217 * first is set if we are looking at the first isl_multi_aff inside
3218 * the isl_union_pw_multi_aff.
3219 * equal is set if all the isl_multi_affs have been equal so far.
3221 struct isl_cmp_band_data {
3222 int r;
3224 int first;
3225 int equal;
3227 isl_int t;
3228 isl_int c1;
3229 isl_int c2;
3232 /* Check if "ma" assigns a constant value.
3233 * Note that this function is only called on isl_multi_affs
3234 * with a single output dimension.
3236 * If "ma" assigns a constant value then we compare it to data->c1
3237 * or assign it to data->c1 if this is the first isl_multi_aff we consider.
3238 * If "ma" does not assign a constant value or if it assigns a value
3239 * that is different from data->c1, then we set data->equal to zero
3240 * and terminate the check.
3242 static int multi_aff_extract_int(__isl_take isl_set *set,
3243 __isl_take isl_multi_aff *ma, void *user)
3245 isl_aff *aff;
3246 struct isl_cmp_band_data *data = user;
3248 aff = isl_multi_aff_get_aff(ma, 0);
3249 data->r = isl_aff_is_cst(aff);
3250 if (data->r >= 0 && data->r) {
3251 isl_aff_get_constant(aff, &data->t);
3252 if (data->first) {
3253 isl_int_set(data->c1, data->t);
3254 data->first = 0;
3255 } else if (!isl_int_eq(data->c1, data->t))
3256 data->equal = 0;
3257 } else if (data->r >= 0 && !data->r)
3258 data->equal = 0;
3260 isl_aff_free(aff);
3261 isl_set_free(set);
3262 isl_multi_aff_free(ma);
3264 if (data->r < 0)
3265 return -1;
3266 if (!data->equal)
3267 return -1;
3268 return 0;
3271 /* This function is called for each isl_pw_multi_aff in
3272 * the isl_union_pw_multi_aff checked by extract_int.
3273 * Check all the isl_multi_affs inside "pma".
3275 static int pw_multi_aff_extract_int(__isl_take isl_pw_multi_aff *pma,
3276 void *user)
3278 int r;
3280 r = isl_pw_multi_aff_foreach_piece(pma, &multi_aff_extract_int, user);
3281 isl_pw_multi_aff_free(pma);
3283 return r;
3286 /* Check if "upma" assigns a single constant value to its domain.
3287 * If so, return 1 and store the result in data->c1.
3288 * If not, return 0.
3290 * A negative return value from isl_union_pw_multi_aff_foreach_pw_multi_aff
3291 * means that either an error occurred or that we have broken off the check
3292 * because we already know the result is going to be negative.
3293 * In the latter case, data->equal is set to zero.
3295 static int extract_int(__isl_keep isl_union_pw_multi_aff *upma,
3296 struct isl_cmp_band_data *data)
3298 data->first = 1;
3299 data->equal = 1;
3301 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3302 &pw_multi_aff_extract_int, data) < 0) {
3303 if (!data->equal)
3304 return 0;
3305 return -1;
3308 return !data->first && data->equal;
3311 /* Compare "b1" and "b2" based on the parent schedule of their ancestor
3312 * "ancestor".
3314 * If the parent of "ancestor" also has a single member, then we
3315 * first try to compare the two band based on the partial schedule
3316 * of this parent.
3318 * Otherwise, or if the result is inconclusive, we look at the partial schedule
3319 * of "ancestor" itself.
3320 * In particular, we specialize the parent schedule based
3321 * on the domains of the child schedules, check if both assign
3322 * a single constant value and, if so, compare the two constant values.
3323 * If the specialized parent schedules do not assign a constant value,
3324 * then they cannot be used to order the two bands and so in this case
3325 * we return 0.
3327 static int cmp_band_in_ancestor(__isl_keep isl_band *b1,
3328 __isl_keep isl_band *b2, struct isl_cmp_band_data *data,
3329 __isl_keep isl_band *ancestor)
3331 isl_union_pw_multi_aff *upma;
3332 isl_union_set *domain;
3333 int r;
3335 if (data->r < 0)
3336 return 0;
3338 if (ancestor->parent && ancestor->parent->n == 1) {
3339 r = cmp_band_in_ancestor(b1, b2, data, ancestor->parent);
3340 if (data->r < 0)
3341 return 0;
3342 if (r)
3343 return r;
3346 upma = isl_union_pw_multi_aff_copy(b1->pma);
3347 domain = isl_union_pw_multi_aff_domain(upma);
3348 upma = isl_union_pw_multi_aff_copy(ancestor->pma);
3349 upma = isl_union_pw_multi_aff_intersect_domain(upma, domain);
3350 r = extract_int(upma, data);
3351 isl_union_pw_multi_aff_free(upma);
3353 if (r < 0)
3354 data->r = -1;
3355 if (r < 0 || !r)
3356 return 0;
3358 isl_int_set(data->c2, data->c1);
3360 upma = isl_union_pw_multi_aff_copy(b2->pma);
3361 domain = isl_union_pw_multi_aff_domain(upma);
3362 upma = isl_union_pw_multi_aff_copy(ancestor->pma);
3363 upma = isl_union_pw_multi_aff_intersect_domain(upma, domain);
3364 r = extract_int(upma, data);
3365 isl_union_pw_multi_aff_free(upma);
3367 if (r < 0)
3368 data->r = -1;
3369 if (r < 0 || !r)
3370 return 0;
3372 return isl_int_cmp(data->c2, data->c1);
3375 /* Compare "a" and "b" based on the parent schedule of their parent.
3377 static int cmp_band(const void *a, const void *b, void *user)
3379 isl_band *b1 = *(isl_band * const *) a;
3380 isl_band *b2 = *(isl_band * const *) b;
3381 struct isl_cmp_band_data *data = user;
3383 return cmp_band_in_ancestor(b1, b2, data, b1->parent);
3386 /* Sort the elements in "list" based on the partial schedules of its parent
3387 * (and ancestors). In particular if the parent assigns constant values
3388 * to the domains of the bands in "list", then the elements are sorted
3389 * according to that order.
3390 * This order should be a more "natural" order for the user, but otherwise
3391 * shouldn't have any effect.
3392 * If we would be constructing an isl_band forest directly in
3393 * isl_union_set_compute_schedule then there wouldn't be any need
3394 * for a reordering, since the children would be added to the list
3395 * in their natural order automatically.
3397 * If there is only one element in the list, then there is no need to sort
3398 * anything.
3399 * If the partial schedule of the parent has more than one member
3400 * (or if there is no parent), then it's
3401 * defnitely not assigning constant values to the different children in
3402 * the list and so we wouldn't be able to use it to sort the list.
3404 static __isl_give isl_band_list *sort_band_list(__isl_take isl_band_list *list,
3405 __isl_keep isl_band *parent)
3407 struct isl_cmp_band_data data;
3409 if (!list)
3410 return NULL;
3411 if (list->n <= 1)
3412 return list;
3413 if (!parent || parent->n != 1)
3414 return list;
3416 data.r = 0;
3417 isl_int_init(data.c1);
3418 isl_int_init(data.c2);
3419 isl_int_init(data.t);
3420 isl_sort(list->p, list->n, sizeof(list->p[0]), &cmp_band, &data);
3421 if (data.r < 0)
3422 list = isl_band_list_free(list);
3423 isl_int_clear(data.c1);
3424 isl_int_clear(data.c2);
3425 isl_int_clear(data.t);
3427 return list;
3430 /* Construct a list of bands that start at the same position (with
3431 * sequence number band_nr) in the schedules of the nodes that
3432 * were active in the parent band.
3434 * A separate isl_band structure is created for each band_id
3435 * and for each node that does not have a band with sequence
3436 * number band_nr. In the latter case, a band without members
3437 * is created.
3438 * This ensures that if a band has any children, then each node
3439 * that was active in the band is active in exactly one of the children.
3441 static __isl_give isl_band_list *construct_band_list(
3442 __isl_keep isl_schedule *schedule, __isl_keep isl_band *parent,
3443 int band_nr, int *parent_active, int n_active)
3445 int i, j;
3446 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
3447 int *active;
3448 int n_band;
3449 isl_band_list *list;
3451 n_band = 0;
3452 for (i = 0; i < n_active; ++i) {
3453 for (j = 0; j < schedule->n; ++j) {
3454 if (!parent_active[j])
3455 continue;
3456 if (schedule->node[j].n_band <= band_nr)
3457 continue;
3458 if (schedule->node[j].band_id[band_nr] == i) {
3459 n_band++;
3460 break;
3464 for (j = 0; j < schedule->n; ++j)
3465 if (schedule->node[j].n_band <= band_nr)
3466 n_band++;
3468 if (n_band == 1) {
3469 isl_band *band;
3470 list = isl_band_list_alloc(ctx, n_band);
3471 band = construct_band(schedule, parent, band_nr,
3472 parent_active, n_active);
3473 return isl_band_list_add(list, band);
3476 active = isl_alloc_array(ctx, int, schedule->n);
3477 if (schedule->n && !active)
3478 return NULL;
3480 list = isl_band_list_alloc(ctx, n_band);
3482 for (i = 0; i < n_active; ++i) {
3483 int n = 0;
3484 isl_band *band;
3486 for (j = 0; j < schedule->n; ++j) {
3487 active[j] = parent_active[j] &&
3488 schedule->node[j].n_band > band_nr &&
3489 schedule->node[j].band_id[band_nr] == i;
3490 if (active[j])
3491 n++;
3493 if (n == 0)
3494 continue;
3496 band = construct_band(schedule, parent, band_nr, active, n);
3498 list = isl_band_list_add(list, band);
3500 for (i = 0; i < schedule->n; ++i) {
3501 isl_band *band;
3502 if (!parent_active[i])
3503 continue;
3504 if (schedule->node[i].n_band > band_nr)
3505 continue;
3506 for (j = 0; j < schedule->n; ++j)
3507 active[j] = j == i;
3508 band = construct_band(schedule, parent, band_nr, active, 1);
3509 list = isl_band_list_add(list, band);
3512 free(active);
3514 list = sort_band_list(list, parent);
3516 return list;
3519 /* Construct a band forest representation of the schedule and
3520 * return the list of roots.
3522 static __isl_give isl_band_list *construct_forest(
3523 __isl_keep isl_schedule *schedule)
3525 int i;
3526 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
3527 isl_band_list *forest;
3528 int *active;
3530 active = isl_alloc_array(ctx, int, schedule->n);
3531 if (schedule->n && !active)
3532 return NULL;
3534 for (i = 0; i < schedule->n; ++i)
3535 active[i] = 1;
3537 forest = construct_band_list(schedule, NULL, 0, active, schedule->n);
3539 free(active);
3541 return forest;
3544 /* Return the roots of a band forest representation of the schedule.
3546 __isl_give isl_band_list *isl_schedule_get_band_forest(
3547 __isl_keep isl_schedule *schedule)
3549 if (!schedule)
3550 return NULL;
3551 if (!schedule->band_forest)
3552 schedule->band_forest = construct_forest(schedule);
3553 return isl_band_list_dup(schedule->band_forest);
3556 /* Call "fn" on each band in the schedule in depth-first post-order.
3558 int isl_schedule_foreach_band(__isl_keep isl_schedule *sched,
3559 int (*fn)(__isl_keep isl_band *band, void *user), void *user)
3561 int r;
3562 isl_band_list *forest;
3564 if (!sched)
3565 return -1;
3567 forest = isl_schedule_get_band_forest(sched);
3568 r = isl_band_list_foreach_band(forest, fn, user);
3569 isl_band_list_free(forest);
3571 return r;
3574 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
3575 __isl_keep isl_band_list *list);
3577 static __isl_give isl_printer *print_band(__isl_take isl_printer *p,
3578 __isl_keep isl_band *band)
3580 isl_band_list *children;
3582 p = isl_printer_start_line(p);
3583 p = isl_printer_print_union_pw_multi_aff(p, band->pma);
3584 p = isl_printer_end_line(p);
3586 if (!isl_band_has_children(band))
3587 return p;
3589 children = isl_band_get_children(band);
3591 p = isl_printer_indent(p, 4);
3592 p = print_band_list(p, children);
3593 p = isl_printer_indent(p, -4);
3595 isl_band_list_free(children);
3597 return p;
3600 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
3601 __isl_keep isl_band_list *list)
3603 int i, n;
3605 n = isl_band_list_n_band(list);
3606 for (i = 0; i < n; ++i) {
3607 isl_band *band;
3608 band = isl_band_list_get_band(list, i);
3609 p = print_band(p, band);
3610 isl_band_free(band);
3613 return p;
3616 __isl_give isl_printer *isl_printer_print_schedule(__isl_take isl_printer *p,
3617 __isl_keep isl_schedule *schedule)
3619 isl_band_list *forest;
3621 forest = isl_schedule_get_band_forest(schedule);
3623 p = print_band_list(p, forest);
3625 isl_band_list_free(forest);
3627 return p;
3630 void isl_schedule_dump(__isl_keep isl_schedule *schedule)
3632 isl_printer *printer;
3634 if (!schedule)
3635 return;
3637 printer = isl_printer_to_file(isl_schedule_get_ctx(schedule), stderr);
3638 printer = isl_printer_print_schedule(printer, schedule);
3640 isl_printer_free(printer);