isl_tab_basic_set_non_trivial_lexmin: handle NULL input
[isl.git] / isl_schedule.c
blobb86329bd768856a6b7e9055334f0ca70e88d4f5c
1 /*
2 * Copyright 2011 INRIA Saclay
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8 * 91893 Orsay, France
9 */
11 #include <isl_ctx_private.h>
12 #include <isl_map_private.h>
13 #include <isl_space_private.h>
14 #include <isl/aff.h>
15 #include <isl/hash.h>
16 #include <isl/constraint.h>
17 #include <isl/schedule.h>
18 #include <isl_mat_private.h>
19 #include <isl/set.h>
20 #include <isl/seq.h>
21 #include <isl_tab.h>
22 #include <isl_dim_map.h>
23 #include <isl_hmap_map_basic_set.h>
24 #include <isl_sort.h>
25 #include <isl_schedule_private.h>
26 #include <isl_band_private.h>
27 #include <isl_list_private.h>
28 #include <isl_options_private.h>
29 #include <isl_tarjan.h>
32 * The scheduling algorithm implemented in this file was inspired by
33 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
34 * Parallelization and Locality Optimization in the Polyhedral Model".
38 /* Internal information about a node that is used during the construction
39 * of a schedule.
40 * dim represents the space in which the domain lives
41 * sched is a matrix representation of the schedule being constructed
42 * for this node
43 * sched_map is an isl_map representation of the same (partial) schedule
44 * sched_map may be NULL
45 * rank is the number of linearly independent rows in the linear part
46 * of sched
47 * the columns of cmap represent a change of basis for the schedule
48 * coefficients; the first rank columns span the linear part of
49 * the schedule rows
50 * start is the first variable in the LP problem in the sequences that
51 * represents the schedule coefficients of this node
52 * nvar is the dimension of the domain
53 * nparam is the number of parameters or 0 if we are not constructing
54 * a parametric schedule
56 * scc is the index of SCC (or WCC) this node belongs to
58 * band contains the band index for each of the rows of the schedule.
59 * band_id is used to differentiate between separate bands at the same
60 * level within the same parent band, i.e., bands that are separated
61 * by the parent band or bands that are independent of each other.
62 * zero contains a boolean for each of the rows of the schedule,
63 * indicating whether the corresponding scheduling dimension results
64 * in zero dependence distances within its band and with respect
65 * to the proximity edges.
67 struct isl_sched_node {
68 isl_space *dim;
69 isl_mat *sched;
70 isl_map *sched_map;
71 int rank;
72 isl_mat *cmap;
73 int start;
74 int nvar;
75 int nparam;
77 int scc;
79 int *band;
80 int *band_id;
81 int *zero;
84 static int node_has_dim(const void *entry, const void *val)
86 struct isl_sched_node *node = (struct isl_sched_node *)entry;
87 isl_space *dim = (isl_space *)val;
89 return isl_space_is_equal(node->dim, dim);
92 /* An edge in the dependence graph. An edge may be used to
93 * ensure validity of the generated schedule, to minimize the dependence
94 * distance or both
96 * map is the dependence relation
97 * src is the source node
98 * dst is the sink node
99 * validity is set if the edge is used to ensure correctness
100 * proximity is set if the edge is used to minimize dependence distances
102 * For validity edges, start and end mark the sequence of inequality
103 * constraints in the LP problem that encode the validity constraint
104 * corresponding to this edge.
106 struct isl_sched_edge {
107 isl_map *map;
109 struct isl_sched_node *src;
110 struct isl_sched_node *dst;
112 int validity;
113 int proximity;
115 int start;
116 int end;
119 enum isl_edge_type {
120 isl_edge_validity = 0,
121 isl_edge_first = isl_edge_validity,
122 isl_edge_proximity,
123 isl_edge_last = isl_edge_proximity
126 /* Internal information about the dependence graph used during
127 * the construction of the schedule.
129 * intra_hmap is a cache, mapping dependence relations to their dual,
130 * for dependences from a node to itself
131 * inter_hmap is a cache, mapping dependence relations to their dual,
132 * for dependences between distinct nodes
134 * n is the number of nodes
135 * node is the list of nodes
136 * maxvar is the maximal number of variables over all nodes
137 * max_row is the allocated number of rows in the schedule
138 * n_row is the current (maximal) number of linearly independent
139 * rows in the node schedules
140 * n_total_row is the current number of rows in the node schedules
141 * n_band is the current number of completed bands
142 * band_start is the starting row in the node schedules of the current band
143 * root is set if this graph is the original dependence graph,
144 * without any splitting
146 * sorted contains a list of node indices sorted according to the
147 * SCC to which a node belongs
149 * n_edge is the number of edges
150 * edge is the list of edges
151 * max_edge contains the maximal number of edges of each type;
152 * in particular, it contains the number of edges in the inital graph.
153 * edge_table contains pointers into the edge array, hashed on the source
154 * and sink spaces; there is one such table for each type;
155 * a given edge may be referenced from more than one table
156 * if the corresponding relation appears in more than of the
157 * sets of dependences
159 * node_table contains pointers into the node array, hashed on the space
161 * region contains a list of variable sequences that should be non-trivial
163 * lp contains the (I)LP problem used to obtain new schedule rows
165 * src_scc and dst_scc are the source and sink SCCs of an edge with
166 * conflicting constraints
168 * scc represents the number of components
170 struct isl_sched_graph {
171 isl_hmap_map_basic_set *intra_hmap;
172 isl_hmap_map_basic_set *inter_hmap;
174 struct isl_sched_node *node;
175 int n;
176 int maxvar;
177 int max_row;
178 int n_row;
180 int *sorted;
182 int n_band;
183 int n_total_row;
184 int band_start;
186 int root;
188 struct isl_sched_edge *edge;
189 int n_edge;
190 int max_edge[isl_edge_last + 1];
191 struct isl_hash_table *edge_table[isl_edge_last + 1];
193 struct isl_hash_table *node_table;
194 struct isl_region *region;
196 isl_basic_set *lp;
198 int src_scc;
199 int dst_scc;
201 int scc;
204 /* Initialize node_table based on the list of nodes.
206 static int graph_init_table(isl_ctx *ctx, struct isl_sched_graph *graph)
208 int i;
210 graph->node_table = isl_hash_table_alloc(ctx, graph->n);
211 if (!graph->node_table)
212 return -1;
214 for (i = 0; i < graph->n; ++i) {
215 struct isl_hash_table_entry *entry;
216 uint32_t hash;
218 hash = isl_space_get_hash(graph->node[i].dim);
219 entry = isl_hash_table_find(ctx, graph->node_table, hash,
220 &node_has_dim,
221 graph->node[i].dim, 1);
222 if (!entry)
223 return -1;
224 entry->data = &graph->node[i];
227 return 0;
230 /* Return a pointer to the node that lives within the given space,
231 * or NULL if there is no such node.
233 static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
234 struct isl_sched_graph *graph, __isl_keep isl_space *dim)
236 struct isl_hash_table_entry *entry;
237 uint32_t hash;
239 hash = isl_space_get_hash(dim);
240 entry = isl_hash_table_find(ctx, graph->node_table, hash,
241 &node_has_dim, dim, 0);
243 return entry ? entry->data : NULL;
246 static int edge_has_src_and_dst(const void *entry, const void *val)
248 const struct isl_sched_edge *edge = entry;
249 const struct isl_sched_edge *temp = val;
251 return edge->src == temp->src && edge->dst == temp->dst;
254 /* Add the given edge to graph->edge_table[type].
256 static int graph_edge_table_add(isl_ctx *ctx, struct isl_sched_graph *graph,
257 enum isl_edge_type type, struct isl_sched_edge *edge)
259 struct isl_hash_table_entry *entry;
260 uint32_t hash;
262 hash = isl_hash_init();
263 hash = isl_hash_builtin(hash, edge->src);
264 hash = isl_hash_builtin(hash, edge->dst);
265 entry = isl_hash_table_find(ctx, graph->edge_table[type], hash,
266 &edge_has_src_and_dst, edge, 1);
267 if (!entry)
268 return -1;
269 entry->data = edge;
271 return 0;
274 /* Allocate the edge_tables based on the maximal number of edges of
275 * each type.
277 static int graph_init_edge_tables(isl_ctx *ctx, struct isl_sched_graph *graph)
279 int i;
281 for (i = 0; i <= isl_edge_last; ++i) {
282 graph->edge_table[i] = isl_hash_table_alloc(ctx,
283 graph->max_edge[i]);
284 if (!graph->edge_table[i])
285 return -1;
288 return 0;
291 /* If graph->edge_table[type] contains an edge from the given source
292 * to the given destination, then return the hash table entry of this edge.
293 * Otherwise, return NULL.
295 static struct isl_hash_table_entry *graph_find_edge_entry(
296 struct isl_sched_graph *graph,
297 enum isl_edge_type type,
298 struct isl_sched_node *src, struct isl_sched_node *dst)
300 isl_ctx *ctx = isl_space_get_ctx(src->dim);
301 uint32_t hash;
302 struct isl_sched_edge temp = { .src = src, .dst = dst };
304 hash = isl_hash_init();
305 hash = isl_hash_builtin(hash, temp.src);
306 hash = isl_hash_builtin(hash, temp.dst);
307 return isl_hash_table_find(ctx, graph->edge_table[type], hash,
308 &edge_has_src_and_dst, &temp, 0);
312 /* If graph->edge_table[type] contains an edge from the given source
313 * to the given destination, then return this edge.
314 * Otherwise, return NULL.
316 static struct isl_sched_edge *graph_find_edge(struct isl_sched_graph *graph,
317 enum isl_edge_type type,
318 struct isl_sched_node *src, struct isl_sched_node *dst)
320 struct isl_hash_table_entry *entry;
322 entry = graph_find_edge_entry(graph, type, src, dst);
323 if (!entry)
324 return NULL;
326 return entry->data;
329 /* Check whether the dependence graph has an edge of the give type
330 * between the given two nodes.
332 static int graph_has_edge(struct isl_sched_graph *graph,
333 enum isl_edge_type type,
334 struct isl_sched_node *src, struct isl_sched_node *dst)
336 struct isl_sched_edge *edge;
337 int empty;
339 edge = graph_find_edge(graph, type, src, dst);
340 if (!edge)
341 return 0;
343 empty = isl_map_plain_is_empty(edge->map);
344 if (empty < 0)
345 return -1;
347 return !empty;
350 /* If there is an edge from the given source to the given destination
351 * of any type then return this edge.
352 * Otherwise, return NULL.
354 static struct isl_sched_edge *graph_find_any_edge(struct isl_sched_graph *graph,
355 struct isl_sched_node *src, struct isl_sched_node *dst)
357 enum isl_edge_type i;
358 struct isl_sched_edge *edge;
360 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
361 edge = graph_find_edge(graph, i, src, dst);
362 if (edge)
363 return edge;
366 return NULL;
369 /* Remove the given edge from all the edge_tables that refer to it.
371 static void graph_remove_edge(struct isl_sched_graph *graph,
372 struct isl_sched_edge *edge)
374 isl_ctx *ctx = isl_map_get_ctx(edge->map);
375 enum isl_edge_type i;
377 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
378 struct isl_hash_table_entry *entry;
380 entry = graph_find_edge_entry(graph, i, edge->src, edge->dst);
381 if (!entry)
382 continue;
383 if (entry->data != edge)
384 continue;
385 isl_hash_table_remove(ctx, graph->edge_table[i], entry);
389 /* Check whether the dependence graph has any edge
390 * between the given two nodes.
392 static int graph_has_any_edge(struct isl_sched_graph *graph,
393 struct isl_sched_node *src, struct isl_sched_node *dst)
395 enum isl_edge_type i;
396 int r;
398 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
399 r = graph_has_edge(graph, i, src, dst);
400 if (r < 0 || r)
401 return r;
404 return r;
407 /* Check whether the dependence graph has a validity edge
408 * between the given two nodes.
410 static int graph_has_validity_edge(struct isl_sched_graph *graph,
411 struct isl_sched_node *src, struct isl_sched_node *dst)
413 return graph_has_edge(graph, isl_edge_validity, src, dst);
416 static int graph_alloc(isl_ctx *ctx, struct isl_sched_graph *graph,
417 int n_node, int n_edge)
419 int i;
421 graph->n = n_node;
422 graph->n_edge = n_edge;
423 graph->node = isl_calloc_array(ctx, struct isl_sched_node, graph->n);
424 graph->sorted = isl_calloc_array(ctx, int, graph->n);
425 graph->region = isl_alloc_array(ctx, struct isl_region, graph->n);
426 graph->edge = isl_calloc_array(ctx,
427 struct isl_sched_edge, graph->n_edge);
429 graph->intra_hmap = isl_hmap_map_basic_set_alloc(ctx, 2 * n_edge);
430 graph->inter_hmap = isl_hmap_map_basic_set_alloc(ctx, 2 * n_edge);
432 if (!graph->node || !graph->region || !graph->edge || !graph->sorted)
433 return -1;
435 for(i = 0; i < graph->n; ++i)
436 graph->sorted[i] = i;
438 return 0;
441 static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
443 int i;
445 isl_hmap_map_basic_set_free(ctx, graph->intra_hmap);
446 isl_hmap_map_basic_set_free(ctx, graph->inter_hmap);
448 for (i = 0; i < graph->n; ++i) {
449 isl_space_free(graph->node[i].dim);
450 isl_mat_free(graph->node[i].sched);
451 isl_map_free(graph->node[i].sched_map);
452 isl_mat_free(graph->node[i].cmap);
453 if (graph->root) {
454 free(graph->node[i].band);
455 free(graph->node[i].band_id);
456 free(graph->node[i].zero);
459 free(graph->node);
460 free(graph->sorted);
461 for (i = 0; i < graph->n_edge; ++i)
462 isl_map_free(graph->edge[i].map);
463 free(graph->edge);
464 free(graph->region);
465 for (i = 0; i <= isl_edge_last; ++i)
466 isl_hash_table_free(ctx, graph->edge_table[i]);
467 isl_hash_table_free(ctx, graph->node_table);
468 isl_basic_set_free(graph->lp);
471 /* For each "set" on which this function is called, increment
472 * graph->n by one and update graph->maxvar.
474 static int init_n_maxvar(__isl_take isl_set *set, void *user)
476 struct isl_sched_graph *graph = user;
477 int nvar = isl_set_dim(set, isl_dim_set);
479 graph->n++;
480 if (nvar > graph->maxvar)
481 graph->maxvar = nvar;
483 isl_set_free(set);
485 return 0;
488 /* Compute the number of rows that should be allocated for the schedule.
489 * The graph can be split at most "n - 1" times, there can be at most
490 * two rows for each dimension in the iteration domains (in particular,
491 * we usually have one row, but it may be split by split_scaled),
492 * and there can be one extra row for ordering the statements.
493 * Note that if we have actually split "n - 1" times, then no ordering
494 * is needed, so in principle we could use "graph->n + 2 * graph->maxvar - 1".
496 static int compute_max_row(struct isl_sched_graph *graph,
497 __isl_keep isl_union_set *domain)
499 graph->n = 0;
500 graph->maxvar = 0;
501 if (isl_union_set_foreach_set(domain, &init_n_maxvar, graph) < 0)
502 return -1;
503 graph->max_row = graph->n + 2 * graph->maxvar;
505 return 0;
508 /* Add a new node to the graph representing the given set.
510 static int extract_node(__isl_take isl_set *set, void *user)
512 int nvar, nparam;
513 isl_ctx *ctx;
514 isl_space *dim;
515 isl_mat *sched;
516 struct isl_sched_graph *graph = user;
517 int *band, *band_id, *zero;
519 ctx = isl_set_get_ctx(set);
520 dim = isl_set_get_space(set);
521 isl_set_free(set);
522 nvar = isl_space_dim(dim, isl_dim_set);
523 nparam = isl_space_dim(dim, isl_dim_param);
524 if (!ctx->opt->schedule_parametric)
525 nparam = 0;
526 sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
527 graph->node[graph->n].dim = dim;
528 graph->node[graph->n].nvar = nvar;
529 graph->node[graph->n].nparam = nparam;
530 graph->node[graph->n].sched = sched;
531 graph->node[graph->n].sched_map = NULL;
532 band = isl_alloc_array(ctx, int, graph->max_row);
533 graph->node[graph->n].band = band;
534 band_id = isl_calloc_array(ctx, int, graph->max_row);
535 graph->node[graph->n].band_id = band_id;
536 zero = isl_calloc_array(ctx, int, graph->max_row);
537 graph->node[graph->n].zero = zero;
538 graph->n++;
540 if (!sched || !band || !band_id || !zero)
541 return -1;
543 return 0;
546 struct isl_extract_edge_data {
547 enum isl_edge_type type;
548 struct isl_sched_graph *graph;
551 /* Add a new edge to the graph based on the given map
552 * and add it to data->graph->edge_table[data->type].
553 * If a dependence relation of a given type happens to be identical
554 * to one of the dependence relations of a type that was added before,
555 * then we don't create a new edge, but instead mark the original edge
556 * as also representing a dependence of the current type.
558 static int extract_edge(__isl_take isl_map *map, void *user)
560 isl_ctx *ctx = isl_map_get_ctx(map);
561 struct isl_extract_edge_data *data = user;
562 struct isl_sched_graph *graph = data->graph;
563 struct isl_sched_node *src, *dst;
564 isl_space *dim;
565 struct isl_sched_edge *edge;
566 int is_equal;
568 dim = isl_space_domain(isl_map_get_space(map));
569 src = graph_find_node(ctx, graph, dim);
570 isl_space_free(dim);
571 dim = isl_space_range(isl_map_get_space(map));
572 dst = graph_find_node(ctx, graph, dim);
573 isl_space_free(dim);
575 if (!src || !dst) {
576 isl_map_free(map);
577 return 0;
580 graph->edge[graph->n_edge].src = src;
581 graph->edge[graph->n_edge].dst = dst;
582 graph->edge[graph->n_edge].map = map;
583 if (data->type == isl_edge_validity) {
584 graph->edge[graph->n_edge].validity = 1;
585 graph->edge[graph->n_edge].proximity = 0;
587 if (data->type == isl_edge_proximity) {
588 graph->edge[graph->n_edge].validity = 0;
589 graph->edge[graph->n_edge].proximity = 1;
591 graph->n_edge++;
593 edge = graph_find_any_edge(graph, src, dst);
594 if (!edge)
595 return graph_edge_table_add(ctx, graph, data->type,
596 &graph->edge[graph->n_edge - 1]);
597 is_equal = isl_map_plain_is_equal(map, edge->map);
598 if (is_equal < 0)
599 return -1;
600 if (!is_equal)
601 return graph_edge_table_add(ctx, graph, data->type,
602 &graph->edge[graph->n_edge - 1]);
604 graph->n_edge--;
605 edge->validity |= graph->edge[graph->n_edge].validity;
606 edge->proximity |= graph->edge[graph->n_edge].proximity;
607 isl_map_free(map);
609 return graph_edge_table_add(ctx, graph, data->type, edge);
612 /* Check whether there is any dependence from node[j] to node[i]
613 * or from node[i] to node[j].
615 static int node_follows_weak(int i, int j, void *user)
617 int f;
618 struct isl_sched_graph *graph = user;
620 f = graph_has_any_edge(graph, &graph->node[j], &graph->node[i]);
621 if (f < 0 || f)
622 return f;
623 return graph_has_any_edge(graph, &graph->node[i], &graph->node[j]);
626 /* Check whether there is a validity dependence from node[j] to node[i],
627 * forcing node[i] to follow node[j].
629 static int node_follows_strong(int i, int j, void *user)
631 struct isl_sched_graph *graph = user;
633 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
636 /* Use Tarjan's algorithm for computing the strongly connected components
637 * in the dependence graph (only validity edges).
638 * If weak is set, we consider the graph to be undirected and
639 * we effectively compute the (weakly) connected components.
640 * Additionally, we also consider other edges when weak is set.
642 static int detect_ccs(isl_ctx *ctx, struct isl_sched_graph *graph, int weak)
644 int i, n;
645 struct isl_tarjan_graph *g = NULL;
647 g = isl_tarjan_graph_init(ctx, graph->n,
648 weak ? &node_follows_weak : &node_follows_strong, graph);
649 if (!g)
650 return -1;
652 graph->scc = 0;
653 i = 0;
654 n = graph->n;
655 while (n) {
656 while (g->order[i] != -1) {
657 graph->node[g->order[i]].scc = graph->scc;
658 --n;
659 ++i;
661 ++i;
662 graph->scc++;
665 isl_tarjan_graph_free(g);
667 return 0;
670 /* Apply Tarjan's algorithm to detect the strongly connected components
671 * in the dependence graph.
673 static int detect_sccs(isl_ctx *ctx, struct isl_sched_graph *graph)
675 return detect_ccs(ctx, graph, 0);
678 /* Apply Tarjan's algorithm to detect the (weakly) connected components
679 * in the dependence graph.
681 static int detect_wccs(isl_ctx *ctx, struct isl_sched_graph *graph)
683 return detect_ccs(ctx, graph, 1);
686 static int cmp_scc(const void *a, const void *b, void *data)
688 struct isl_sched_graph *graph = data;
689 const int *i1 = a;
690 const int *i2 = b;
692 return graph->node[*i1].scc - graph->node[*i2].scc;
695 /* Sort the elements of graph->sorted according to the corresponding SCCs.
697 static int sort_sccs(struct isl_sched_graph *graph)
699 return isl_sort(graph->sorted, graph->n, sizeof(int), &cmp_scc, graph);
702 /* Given a dependence relation R from a node to itself,
703 * construct the set of coefficients of valid constraints for elements
704 * in that dependence relation.
705 * In particular, the result contains tuples of coefficients
706 * c_0, c_n, c_x such that
708 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
710 * or, equivalently,
712 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
714 * We choose here to compute the dual of delta R.
715 * Alternatively, we could have computed the dual of R, resulting
716 * in a set of tuples c_0, c_n, c_x, c_y, and then
717 * plugged in (c_0, c_n, c_x, -c_x).
719 static __isl_give isl_basic_set *intra_coefficients(
720 struct isl_sched_graph *graph, __isl_take isl_map *map)
722 isl_ctx *ctx = isl_map_get_ctx(map);
723 isl_set *delta;
724 isl_basic_set *coef;
726 if (isl_hmap_map_basic_set_has(ctx, graph->intra_hmap, map))
727 return isl_hmap_map_basic_set_get(ctx, graph->intra_hmap, map);
729 delta = isl_set_remove_divs(isl_map_deltas(isl_map_copy(map)));
730 coef = isl_set_coefficients(delta);
731 isl_hmap_map_basic_set_set(ctx, graph->intra_hmap, map,
732 isl_basic_set_copy(coef));
734 return coef;
737 /* Given a dependence relation R, * construct the set of coefficients
738 * of valid constraints for elements in that dependence relation.
739 * In particular, the result contains tuples of coefficients
740 * c_0, c_n, c_x, c_y such that
742 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
745 static __isl_give isl_basic_set *inter_coefficients(
746 struct isl_sched_graph *graph, __isl_take isl_map *map)
748 isl_ctx *ctx = isl_map_get_ctx(map);
749 isl_set *set;
750 isl_basic_set *coef;
752 if (isl_hmap_map_basic_set_has(ctx, graph->inter_hmap, map))
753 return isl_hmap_map_basic_set_get(ctx, graph->inter_hmap, map);
755 set = isl_map_wrap(isl_map_remove_divs(isl_map_copy(map)));
756 coef = isl_set_coefficients(set);
757 isl_hmap_map_basic_set_set(ctx, graph->inter_hmap, map,
758 isl_basic_set_copy(coef));
760 return coef;
763 /* Add constraints to graph->lp that force validity for the given
764 * dependence from a node i to itself.
765 * That is, add constraints that enforce
767 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
768 * = c_i_x (y - x) >= 0
770 * for each (x,y) in R.
771 * We obtain general constraints on coefficients (c_0, c_n, c_x)
772 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
773 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
774 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
776 * Actually, we do not construct constraints for the c_i_x themselves,
777 * but for the coefficients of c_i_x written as a linear combination
778 * of the columns in node->cmap.
780 static int add_intra_validity_constraints(struct isl_sched_graph *graph,
781 struct isl_sched_edge *edge)
783 unsigned total;
784 isl_map *map = isl_map_copy(edge->map);
785 isl_ctx *ctx = isl_map_get_ctx(map);
786 isl_space *dim;
787 isl_dim_map *dim_map;
788 isl_basic_set *coef;
789 struct isl_sched_node *node = edge->src;
791 coef = intra_coefficients(graph, map);
793 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
795 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
796 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
797 if (!coef)
798 goto error;
800 total = isl_basic_set_total_dim(graph->lp);
801 dim_map = isl_dim_map_alloc(ctx, total);
802 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
803 isl_space_dim(dim, isl_dim_set), 1,
804 node->nvar, -1);
805 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
806 isl_space_dim(dim, isl_dim_set), 1,
807 node->nvar, 1);
808 graph->lp = isl_basic_set_extend_constraints(graph->lp,
809 coef->n_eq, coef->n_ineq);
810 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
811 coef, dim_map);
812 isl_space_free(dim);
814 return 0;
815 error:
816 isl_space_free(dim);
817 return -1;
820 /* Add constraints to graph->lp that force validity for the given
821 * dependence from node i to node j.
822 * That is, add constraints that enforce
824 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
826 * for each (x,y) in R.
827 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
828 * of valid constraints for R and then plug in
829 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
830 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
831 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
832 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
834 * Actually, we do not construct constraints for the c_*_x themselves,
835 * but for the coefficients of c_*_x written as a linear combination
836 * of the columns in node->cmap.
838 static int add_inter_validity_constraints(struct isl_sched_graph *graph,
839 struct isl_sched_edge *edge)
841 unsigned total;
842 isl_map *map = isl_map_copy(edge->map);
843 isl_ctx *ctx = isl_map_get_ctx(map);
844 isl_space *dim;
845 isl_dim_map *dim_map;
846 isl_basic_set *coef;
847 struct isl_sched_node *src = edge->src;
848 struct isl_sched_node *dst = edge->dst;
850 coef = inter_coefficients(graph, map);
852 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
854 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
855 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
856 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
857 isl_space_dim(dim, isl_dim_set) + src->nvar,
858 isl_mat_copy(dst->cmap));
859 if (!coef)
860 goto error;
862 total = isl_basic_set_total_dim(graph->lp);
863 dim_map = isl_dim_map_alloc(ctx, total);
865 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
866 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
867 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
868 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
869 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
870 dst->nvar, -1);
871 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
872 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
873 dst->nvar, 1);
875 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
876 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
877 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
878 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
879 isl_space_dim(dim, isl_dim_set), 1,
880 src->nvar, 1);
881 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
882 isl_space_dim(dim, isl_dim_set), 1,
883 src->nvar, -1);
885 edge->start = graph->lp->n_ineq;
886 graph->lp = isl_basic_set_extend_constraints(graph->lp,
887 coef->n_eq, coef->n_ineq);
888 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
889 coef, dim_map);
890 isl_space_free(dim);
891 edge->end = graph->lp->n_ineq;
893 return 0;
894 error:
895 isl_space_free(dim);
896 return -1;
899 /* Add constraints to graph->lp that bound the dependence distance for the given
900 * dependence from a node i to itself.
901 * If s = 1, we add the constraint
903 * c_i_x (y - x) <= m_0 + m_n n
905 * or
907 * -c_i_x (y - x) + m_0 + m_n n >= 0
909 * for each (x,y) in R.
910 * If s = -1, we add the constraint
912 * -c_i_x (y - x) <= m_0 + m_n n
914 * or
916 * c_i_x (y - x) + m_0 + m_n n >= 0
918 * for each (x,y) in R.
919 * We obtain general constraints on coefficients (c_0, c_n, c_x)
920 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
921 * with each coefficient (except m_0) represented as a pair of non-negative
922 * coefficients.
924 * Actually, we do not construct constraints for the c_i_x themselves,
925 * but for the coefficients of c_i_x written as a linear combination
926 * of the columns in node->cmap.
928 static int add_intra_proximity_constraints(struct isl_sched_graph *graph,
929 struct isl_sched_edge *edge, int s)
931 unsigned total;
932 unsigned nparam;
933 isl_map *map = isl_map_copy(edge->map);
934 isl_ctx *ctx = isl_map_get_ctx(map);
935 isl_space *dim;
936 isl_dim_map *dim_map;
937 isl_basic_set *coef;
938 struct isl_sched_node *node = edge->src;
940 coef = intra_coefficients(graph, map);
942 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
944 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
945 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
946 if (!coef)
947 goto error;
949 nparam = isl_space_dim(node->dim, isl_dim_param);
950 total = isl_basic_set_total_dim(graph->lp);
951 dim_map = isl_dim_map_alloc(ctx, total);
952 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
953 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
954 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
955 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
956 isl_space_dim(dim, isl_dim_set), 1,
957 node->nvar, s);
958 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
959 isl_space_dim(dim, isl_dim_set), 1,
960 node->nvar, -s);
961 graph->lp = isl_basic_set_extend_constraints(graph->lp,
962 coef->n_eq, coef->n_ineq);
963 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
964 coef, dim_map);
965 isl_space_free(dim);
967 return 0;
968 error:
969 isl_space_free(dim);
970 return -1;
973 /* Add constraints to graph->lp that bound the dependence distance for the given
974 * dependence from node i to node j.
975 * If s = 1, we add the constraint
977 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
978 * <= m_0 + m_n n
980 * or
982 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
983 * m_0 + m_n n >= 0
985 * for each (x,y) in R.
986 * If s = -1, we add the constraint
988 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
989 * <= m_0 + m_n n
991 * or
993 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
994 * m_0 + m_n n >= 0
996 * for each (x,y) in R.
997 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
998 * of valid constraints for R and then plug in
999 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1000 * -s*c_j_x+s*c_i_x)
1001 * with each coefficient (except m_0, c_j_0 and c_i_0)
1002 * represented as a pair of non-negative coefficients.
1004 * Actually, we do not construct constraints for the c_*_x themselves,
1005 * but for the coefficients of c_*_x written as a linear combination
1006 * of the columns in node->cmap.
1008 static int add_inter_proximity_constraints(struct isl_sched_graph *graph,
1009 struct isl_sched_edge *edge, int s)
1011 unsigned total;
1012 unsigned nparam;
1013 isl_map *map = isl_map_copy(edge->map);
1014 isl_ctx *ctx = isl_map_get_ctx(map);
1015 isl_space *dim;
1016 isl_dim_map *dim_map;
1017 isl_basic_set *coef;
1018 struct isl_sched_node *src = edge->src;
1019 struct isl_sched_node *dst = edge->dst;
1021 coef = inter_coefficients(graph, map);
1023 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1025 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1026 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1027 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1028 isl_space_dim(dim, isl_dim_set) + src->nvar,
1029 isl_mat_copy(dst->cmap));
1030 if (!coef)
1031 goto error;
1033 nparam = isl_space_dim(src->dim, isl_dim_param);
1034 total = isl_basic_set_total_dim(graph->lp);
1035 dim_map = isl_dim_map_alloc(ctx, total);
1037 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1038 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1039 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1041 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, -s);
1042 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, s);
1043 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, -s);
1044 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1045 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1046 dst->nvar, s);
1047 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1048 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1049 dst->nvar, -s);
1051 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, s);
1052 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, -s);
1053 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, s);
1054 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1055 isl_space_dim(dim, isl_dim_set), 1,
1056 src->nvar, -s);
1057 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1058 isl_space_dim(dim, isl_dim_set), 1,
1059 src->nvar, s);
1061 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1062 coef->n_eq, coef->n_ineq);
1063 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1064 coef, dim_map);
1065 isl_space_free(dim);
1067 return 0;
1068 error:
1069 isl_space_free(dim);
1070 return -1;
1073 static int add_all_validity_constraints(struct isl_sched_graph *graph)
1075 int i;
1077 for (i = 0; i < graph->n_edge; ++i) {
1078 struct isl_sched_edge *edge= &graph->edge[i];
1079 if (!edge->validity)
1080 continue;
1081 if (edge->src != edge->dst)
1082 continue;
1083 if (add_intra_validity_constraints(graph, edge) < 0)
1084 return -1;
1087 for (i = 0; i < graph->n_edge; ++i) {
1088 struct isl_sched_edge *edge = &graph->edge[i];
1089 if (!edge->validity)
1090 continue;
1091 if (edge->src == edge->dst)
1092 continue;
1093 if (add_inter_validity_constraints(graph, edge) < 0)
1094 return -1;
1097 return 0;
1100 /* Add constraints to graph->lp that bound the dependence distance
1101 * for all dependence relations.
1102 * If a given proximity dependence is identical to a validity
1103 * dependence, then the dependence distance is already bounded
1104 * from below (by zero), so we only need to bound the distance
1105 * from above.
1106 * Otherwise, we need to bound the distance both from above and from below.
1108 static int add_all_proximity_constraints(struct isl_sched_graph *graph)
1110 int i;
1112 for (i = 0; i < graph->n_edge; ++i) {
1113 struct isl_sched_edge *edge= &graph->edge[i];
1114 if (!edge->proximity)
1115 continue;
1116 if (edge->src == edge->dst &&
1117 add_intra_proximity_constraints(graph, edge, 1) < 0)
1118 return -1;
1119 if (edge->src != edge->dst &&
1120 add_inter_proximity_constraints(graph, edge, 1) < 0)
1121 return -1;
1122 if (edge->validity)
1123 continue;
1124 if (edge->src == edge->dst &&
1125 add_intra_proximity_constraints(graph, edge, -1) < 0)
1126 return -1;
1127 if (edge->src != edge->dst &&
1128 add_inter_proximity_constraints(graph, edge, -1) < 0)
1129 return -1;
1132 return 0;
1135 /* Compute a basis for the rows in the linear part of the schedule
1136 * and extend this basis to a full basis. The remaining rows
1137 * can then be used to force linear independence from the rows
1138 * in the schedule.
1140 * In particular, given the schedule rows S, we compute
1142 * S = H Q
1144 * with H the Hermite normal form of S. That is, all but the
1145 * first rank columns of Q are zero and so each row in S is
1146 * a linear combination of the first rank rows of Q.
1147 * The matrix Q is then transposed because we will write the
1148 * coefficients of the next schedule row as a column vector s
1149 * and express this s as a linear combination s = Q c of the
1150 * computed basis.
1152 static int node_update_cmap(struct isl_sched_node *node)
1154 isl_mat *H, *Q;
1155 int n_row = isl_mat_rows(node->sched);
1157 H = isl_mat_sub_alloc(node->sched, 0, n_row,
1158 1 + node->nparam, node->nvar);
1160 H = isl_mat_left_hermite(H, 0, NULL, &Q);
1161 isl_mat_free(node->cmap);
1162 node->cmap = isl_mat_transpose(Q);
1163 node->rank = isl_mat_initial_non_zero_cols(H);
1164 isl_mat_free(H);
1166 if (!node->cmap || node->rank < 0)
1167 return -1;
1168 return 0;
1171 /* Count the number of equality and inequality constraints
1172 * that will be added for the given map.
1173 * If carry is set, then we are counting the number of (validity)
1174 * constraints that will be added in setup_carry_lp and we count
1175 * each edge exactly once. Otherwise, we count as follows
1176 * validity -> 1 (>= 0)
1177 * validity+proximity -> 2 (>= 0 and upper bound)
1178 * proximity -> 2 (lower and upper bound)
1180 static int count_map_constraints(struct isl_sched_graph *graph,
1181 struct isl_sched_edge *edge, __isl_take isl_map *map,
1182 int *n_eq, int *n_ineq, int carry)
1184 isl_basic_set *coef;
1185 int f = carry ? 1 : edge->proximity ? 2 : 1;
1187 if (carry && !edge->validity) {
1188 isl_map_free(map);
1189 return 0;
1192 if (edge->src == edge->dst)
1193 coef = intra_coefficients(graph, map);
1194 else
1195 coef = inter_coefficients(graph, map);
1196 if (!coef)
1197 return -1;
1198 *n_eq += f * coef->n_eq;
1199 *n_ineq += f * coef->n_ineq;
1200 isl_basic_set_free(coef);
1202 return 0;
1205 /* Count the number of equality and inequality constraints
1206 * that will be added to the main lp problem.
1207 * We count as follows
1208 * validity -> 1 (>= 0)
1209 * validity+proximity -> 2 (>= 0 and upper bound)
1210 * proximity -> 2 (lower and upper bound)
1212 static int count_constraints(struct isl_sched_graph *graph,
1213 int *n_eq, int *n_ineq)
1215 int i;
1217 *n_eq = *n_ineq = 0;
1218 for (i = 0; i < graph->n_edge; ++i) {
1219 struct isl_sched_edge *edge= &graph->edge[i];
1220 isl_map *map = isl_map_copy(edge->map);
1222 if (count_map_constraints(graph, edge, map,
1223 n_eq, n_ineq, 0) < 0)
1224 return -1;
1227 return 0;
1230 /* Add constraints that bound the values of the variable and parameter
1231 * coefficients of the schedule.
1233 * The maximal value of the coefficients is defined by the option
1234 * 'schedule_max_coefficient'.
1236 static int add_bound_coefficient_constraints(isl_ctx *ctx,
1237 struct isl_sched_graph *graph)
1239 int i, j, k;
1240 int max_coefficient;
1241 int total;
1243 max_coefficient = ctx->opt->schedule_max_coefficient;
1245 if (max_coefficient == -1)
1246 return 0;
1248 total = isl_basic_set_total_dim(graph->lp);
1250 for (i = 0; i < graph->n; ++i) {
1251 struct isl_sched_node *node = &graph->node[i];
1252 for (j = 0; j < 2 * node->nparam + 2 * node->nvar; ++j) {
1253 int dim;
1254 k = isl_basic_set_alloc_inequality(graph->lp);
1255 if (k < 0)
1256 return -1;
1257 dim = 1 + node->start + 1 + j;
1258 isl_seq_clr(graph->lp->ineq[k], 1 + total);
1259 isl_int_set_si(graph->lp->ineq[k][dim], -1);
1260 isl_int_set_si(graph->lp->ineq[k][0], max_coefficient);
1264 return 0;
1267 /* Construct an ILP problem for finding schedule coefficients
1268 * that result in non-negative, but small dependence distances
1269 * over all dependences.
1270 * In particular, the dependence distances over proximity edges
1271 * are bounded by m_0 + m_n n and we compute schedule coefficients
1272 * with small values (preferably zero) of m_n and m_0.
1274 * All variables of the ILP are non-negative. The actual coefficients
1275 * may be negative, so each coefficient is represented as the difference
1276 * of two non-negative variables. The negative part always appears
1277 * immediately before the positive part.
1278 * Other than that, the variables have the following order
1280 * - sum of positive and negative parts of m_n coefficients
1281 * - m_0
1282 * - sum of positive and negative parts of all c_n coefficients
1283 * (unconstrained when computing non-parametric schedules)
1284 * - sum of positive and negative parts of all c_x coefficients
1285 * - positive and negative parts of m_n coefficients
1286 * - for each node
1287 * - c_i_0
1288 * - positive and negative parts of c_i_n (if parametric)
1289 * - positive and negative parts of c_i_x
1291 * The c_i_x are not represented directly, but through the columns of
1292 * node->cmap. That is, the computed values are for variable t_i_x
1293 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
1295 * The constraints are those from the edges plus two or three equalities
1296 * to express the sums.
1298 * If force_zero is set, then we add equalities to ensure that
1299 * the sum of the m_n coefficients and m_0 are both zero.
1301 static int setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
1302 int force_zero)
1304 int i, j;
1305 int k;
1306 unsigned nparam;
1307 unsigned total;
1308 isl_space *dim;
1309 int parametric;
1310 int param_pos;
1311 int n_eq, n_ineq;
1312 int max_constant_term;
1313 int max_coefficient;
1315 max_constant_term = ctx->opt->schedule_max_constant_term;
1316 max_coefficient = ctx->opt->schedule_max_coefficient;
1318 parametric = ctx->opt->schedule_parametric;
1319 nparam = isl_space_dim(graph->node[0].dim, isl_dim_param);
1320 param_pos = 4;
1321 total = param_pos + 2 * nparam;
1322 for (i = 0; i < graph->n; ++i) {
1323 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
1324 if (node_update_cmap(node) < 0)
1325 return -1;
1326 node->start = total;
1327 total += 1 + 2 * (node->nparam + node->nvar);
1330 if (count_constraints(graph, &n_eq, &n_ineq) < 0)
1331 return -1;
1333 dim = isl_space_set_alloc(ctx, 0, total);
1334 isl_basic_set_free(graph->lp);
1335 n_eq += 2 + parametric + force_zero;
1336 if (max_constant_term != -1)
1337 n_ineq += graph->n;
1338 if (max_coefficient != -1)
1339 for (i = 0; i < graph->n; ++i)
1340 n_ineq += 2 * graph->node[i].nparam +
1341 2 * graph->node[i].nvar;
1343 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
1345 k = isl_basic_set_alloc_equality(graph->lp);
1346 if (k < 0)
1347 return -1;
1348 isl_seq_clr(graph->lp->eq[k], 1 + total);
1349 if (!force_zero)
1350 isl_int_set_si(graph->lp->eq[k][1], -1);
1351 for (i = 0; i < 2 * nparam; ++i)
1352 isl_int_set_si(graph->lp->eq[k][1 + param_pos + i], 1);
1354 if (force_zero) {
1355 k = isl_basic_set_alloc_equality(graph->lp);
1356 if (k < 0)
1357 return -1;
1358 isl_seq_clr(graph->lp->eq[k], 1 + total);
1359 isl_int_set_si(graph->lp->eq[k][2], -1);
1362 if (parametric) {
1363 k = isl_basic_set_alloc_equality(graph->lp);
1364 if (k < 0)
1365 return -1;
1366 isl_seq_clr(graph->lp->eq[k], 1 + total);
1367 isl_int_set_si(graph->lp->eq[k][3], -1);
1368 for (i = 0; i < graph->n; ++i) {
1369 int pos = 1 + graph->node[i].start + 1;
1371 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
1372 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
1376 k = isl_basic_set_alloc_equality(graph->lp);
1377 if (k < 0)
1378 return -1;
1379 isl_seq_clr(graph->lp->eq[k], 1 + total);
1380 isl_int_set_si(graph->lp->eq[k][4], -1);
1381 for (i = 0; i < graph->n; ++i) {
1382 struct isl_sched_node *node = &graph->node[i];
1383 int pos = 1 + node->start + 1 + 2 * node->nparam;
1385 for (j = 0; j < 2 * node->nvar; ++j)
1386 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
1389 if (max_constant_term != -1)
1390 for (i = 0; i < graph->n; ++i) {
1391 struct isl_sched_node *node = &graph->node[i];
1392 k = isl_basic_set_alloc_inequality(graph->lp);
1393 if (k < 0)
1394 return -1;
1395 isl_seq_clr(graph->lp->ineq[k], 1 + total);
1396 isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
1397 isl_int_set_si(graph->lp->ineq[k][0], max_constant_term);
1400 if (add_bound_coefficient_constraints(ctx, graph) < 0)
1401 return -1;
1402 if (add_all_validity_constraints(graph) < 0)
1403 return -1;
1404 if (add_all_proximity_constraints(graph) < 0)
1405 return -1;
1407 return 0;
1410 /* Analyze the conflicting constraint found by
1411 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
1412 * constraint of one of the edges between distinct nodes, living, moreover
1413 * in distinct SCCs, then record the source and sink SCC as this may
1414 * be a good place to cut between SCCs.
1416 static int check_conflict(int con, void *user)
1418 int i;
1419 struct isl_sched_graph *graph = user;
1421 if (graph->src_scc >= 0)
1422 return 0;
1424 con -= graph->lp->n_eq;
1426 if (con >= graph->lp->n_ineq)
1427 return 0;
1429 for (i = 0; i < graph->n_edge; ++i) {
1430 if (!graph->edge[i].validity)
1431 continue;
1432 if (graph->edge[i].src == graph->edge[i].dst)
1433 continue;
1434 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
1435 continue;
1436 if (graph->edge[i].start > con)
1437 continue;
1438 if (graph->edge[i].end <= con)
1439 continue;
1440 graph->src_scc = graph->edge[i].src->scc;
1441 graph->dst_scc = graph->edge[i].dst->scc;
1444 return 0;
1447 /* Check whether the next schedule row of the given node needs to be
1448 * non-trivial. Lower-dimensional domains may have some trivial rows,
1449 * but as soon as the number of remaining required non-trivial rows
1450 * is as large as the number or remaining rows to be computed,
1451 * all remaining rows need to be non-trivial.
1453 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
1455 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
1458 /* Solve the ILP problem constructed in setup_lp.
1459 * For each node such that all the remaining rows of its schedule
1460 * need to be non-trivial, we construct a non-triviality region.
1461 * This region imposes that the next row is independent of previous rows.
1462 * In particular the coefficients c_i_x are represented by t_i_x
1463 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
1464 * its first columns span the rows of the previously computed part
1465 * of the schedule. The non-triviality region enforces that at least
1466 * one of the remaining components of t_i_x is non-zero, i.e.,
1467 * that the new schedule row depends on at least one of the remaining
1468 * columns of Q.
1470 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
1472 int i;
1473 isl_vec *sol;
1474 isl_basic_set *lp;
1476 for (i = 0; i < graph->n; ++i) {
1477 struct isl_sched_node *node = &graph->node[i];
1478 int skip = node->rank;
1479 graph->region[i].pos = node->start + 1 + 2*(node->nparam+skip);
1480 if (needs_row(graph, node))
1481 graph->region[i].len = 2 * (node->nvar - skip);
1482 else
1483 graph->region[i].len = 0;
1485 lp = isl_basic_set_copy(graph->lp);
1486 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
1487 graph->region, &check_conflict, graph);
1488 return sol;
1491 /* Update the schedules of all nodes based on the given solution
1492 * of the LP problem.
1493 * The new row is added to the current band.
1494 * All possibly negative coefficients are encoded as a difference
1495 * of two non-negative variables, so we need to perform the subtraction
1496 * here. Moreover, if use_cmap is set, then the solution does
1497 * not refer to the actual coefficients c_i_x, but instead to variables
1498 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
1499 * In this case, we then also need to perform this multiplication
1500 * to obtain the values of c_i_x.
1502 * If check_zero is set, then the first two coordinates of sol are
1503 * assumed to correspond to the dependence distance. If these two
1504 * coordinates are zero, then the corresponding scheduling dimension
1505 * is marked as being zero distance.
1507 static int update_schedule(struct isl_sched_graph *graph,
1508 __isl_take isl_vec *sol, int use_cmap, int check_zero)
1510 int i, j;
1511 int zero = 0;
1512 isl_vec *csol = NULL;
1514 if (!sol)
1515 goto error;
1516 if (sol->size == 0)
1517 isl_die(sol->ctx, isl_error_internal,
1518 "no solution found", goto error);
1519 if (graph->n_total_row >= graph->max_row)
1520 isl_die(sol->ctx, isl_error_internal,
1521 "too many schedule rows", goto error);
1523 if (check_zero)
1524 zero = isl_int_is_zero(sol->el[1]) &&
1525 isl_int_is_zero(sol->el[2]);
1527 for (i = 0; i < graph->n; ++i) {
1528 struct isl_sched_node *node = &graph->node[i];
1529 int pos = node->start;
1530 int row = isl_mat_rows(node->sched);
1532 isl_vec_free(csol);
1533 csol = isl_vec_alloc(sol->ctx, node->nvar);
1534 if (!csol)
1535 goto error;
1537 isl_map_free(node->sched_map);
1538 node->sched_map = NULL;
1539 node->sched = isl_mat_add_rows(node->sched, 1);
1540 if (!node->sched)
1541 goto error;
1542 node->sched = isl_mat_set_element(node->sched, row, 0,
1543 sol->el[1 + pos]);
1544 for (j = 0; j < node->nparam + node->nvar; ++j)
1545 isl_int_sub(sol->el[1 + pos + 1 + 2 * j + 1],
1546 sol->el[1 + pos + 1 + 2 * j + 1],
1547 sol->el[1 + pos + 1 + 2 * j]);
1548 for (j = 0; j < node->nparam; ++j)
1549 node->sched = isl_mat_set_element(node->sched,
1550 row, 1 + j, sol->el[1+pos+1+2*j+1]);
1551 for (j = 0; j < node->nvar; ++j)
1552 isl_int_set(csol->el[j],
1553 sol->el[1+pos+1+2*(node->nparam+j)+1]);
1554 if (use_cmap)
1555 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
1556 csol);
1557 if (!csol)
1558 goto error;
1559 for (j = 0; j < node->nvar; ++j)
1560 node->sched = isl_mat_set_element(node->sched,
1561 row, 1 + node->nparam + j, csol->el[j]);
1562 node->band[graph->n_total_row] = graph->n_band;
1563 node->zero[graph->n_total_row] = zero;
1565 isl_vec_free(sol);
1566 isl_vec_free(csol);
1568 graph->n_row++;
1569 graph->n_total_row++;
1571 return 0;
1572 error:
1573 isl_vec_free(sol);
1574 isl_vec_free(csol);
1575 return -1;
1578 /* Convert node->sched into a multi_aff and return this multi_aff.
1580 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
1581 struct isl_sched_node *node)
1583 int i, j;
1584 isl_space *space;
1585 isl_local_space *ls;
1586 isl_aff *aff;
1587 isl_multi_aff *ma;
1588 int nrow, ncol;
1589 isl_int v;
1591 nrow = isl_mat_rows(node->sched);
1592 ncol = isl_mat_cols(node->sched) - 1;
1593 space = isl_space_from_domain(isl_space_copy(node->dim));
1594 space = isl_space_add_dims(space, isl_dim_out, nrow);
1595 ma = isl_multi_aff_zero(space);
1596 ls = isl_local_space_from_space(isl_space_copy(node->dim));
1598 isl_int_init(v);
1600 for (i = 0; i < nrow; ++i) {
1601 aff = isl_aff_zero_on_domain(isl_local_space_copy(ls));
1602 isl_mat_get_element(node->sched, i, 0, &v);
1603 aff = isl_aff_set_constant(aff, v);
1604 for (j = 0; j < node->nparam; ++j) {
1605 isl_mat_get_element(node->sched, i, 1 + j, &v);
1606 aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
1608 for (j = 0; j < node->nvar; ++j) {
1609 isl_mat_get_element(node->sched,
1610 i, 1 + node->nparam + j, &v);
1611 aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
1613 ma = isl_multi_aff_set_aff(ma, i, aff);
1616 isl_int_clear(v);
1618 isl_local_space_free(ls);
1620 return ma;
1623 /* Convert node->sched into a map and return this map.
1625 * The result is cached in node->sched_map, which needs to be released
1626 * whenever node->sched is updated.
1628 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
1630 if (!node->sched_map) {
1631 isl_multi_aff *ma;
1633 ma = node_extract_schedule_multi_aff(node);
1634 node->sched_map = isl_map_from_multi_aff(ma);
1637 return isl_map_copy(node->sched_map);
1640 /* Update the given dependence relation based on the current schedule.
1641 * That is, intersect the dependence relation with a map expressing
1642 * that source and sink are executed within the same iteration of
1643 * the current schedule.
1644 * This is not the most efficient way, but this shouldn't be a critical
1645 * operation.
1647 static __isl_give isl_map *specialize(__isl_take isl_map *map,
1648 struct isl_sched_node *src, struct isl_sched_node *dst)
1650 isl_map *src_sched, *dst_sched, *id;
1652 src_sched = node_extract_schedule(src);
1653 dst_sched = node_extract_schedule(dst);
1654 id = isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
1655 return isl_map_intersect(map, id);
1658 /* Update the dependence relations of all edges based on the current schedule.
1659 * If a dependence is carried completely by the current schedule, then
1660 * it is removed from the edge_tables. It is kept in the list of edges
1661 * as otherwise all edge_tables would have to be recomputed.
1663 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
1665 int i;
1667 for (i = graph->n_edge - 1; i >= 0; --i) {
1668 struct isl_sched_edge *edge = &graph->edge[i];
1669 edge->map = specialize(edge->map, edge->src, edge->dst);
1670 if (!edge->map)
1671 return -1;
1673 if (isl_map_plain_is_empty(edge->map))
1674 graph_remove_edge(graph, edge);
1677 return 0;
1680 static void next_band(struct isl_sched_graph *graph)
1682 graph->band_start = graph->n_total_row;
1683 graph->n_band++;
1686 /* Topologically sort statements mapped to the same schedule iteration
1687 * and add a row to the schedule corresponding to this order.
1689 static int sort_statements(isl_ctx *ctx, struct isl_sched_graph *graph)
1691 int i, j;
1693 if (graph->n <= 1)
1694 return 0;
1696 if (update_edges(ctx, graph) < 0)
1697 return -1;
1699 if (graph->n_edge == 0)
1700 return 0;
1702 if (detect_sccs(ctx, graph) < 0)
1703 return -1;
1705 if (graph->n_total_row >= graph->max_row)
1706 isl_die(ctx, isl_error_internal,
1707 "too many schedule rows", return -1);
1709 for (i = 0; i < graph->n; ++i) {
1710 struct isl_sched_node *node = &graph->node[i];
1711 int row = isl_mat_rows(node->sched);
1712 int cols = isl_mat_cols(node->sched);
1714 isl_map_free(node->sched_map);
1715 node->sched_map = NULL;
1716 node->sched = isl_mat_add_rows(node->sched, 1);
1717 if (!node->sched)
1718 return -1;
1719 node->sched = isl_mat_set_element_si(node->sched, row, 0,
1720 node->scc);
1721 for (j = 1; j < cols; ++j)
1722 node->sched = isl_mat_set_element_si(node->sched,
1723 row, j, 0);
1724 node->band[graph->n_total_row] = graph->n_band;
1727 graph->n_total_row++;
1728 next_band(graph);
1730 return 0;
1733 /* Construct an isl_schedule based on the computed schedule stored
1734 * in graph and with parameters specified by dim.
1736 static __isl_give isl_schedule *extract_schedule(struct isl_sched_graph *graph,
1737 __isl_take isl_space *dim)
1739 int i;
1740 isl_ctx *ctx;
1741 isl_schedule *sched = NULL;
1743 if (!dim)
1744 return NULL;
1746 ctx = isl_space_get_ctx(dim);
1747 sched = isl_calloc(ctx, struct isl_schedule,
1748 sizeof(struct isl_schedule) +
1749 (graph->n - 1) * sizeof(struct isl_schedule_node));
1750 if (!sched)
1751 goto error;
1753 sched->ref = 1;
1754 sched->n = graph->n;
1755 sched->n_band = graph->n_band;
1756 sched->n_total_row = graph->n_total_row;
1758 for (i = 0; i < sched->n; ++i) {
1759 int r, b;
1760 int *band_end, *band_id, *zero;
1762 sched->node[i].sched =
1763 node_extract_schedule_multi_aff(&graph->node[i]);
1764 if (!sched->node[i].sched)
1765 goto error;
1767 sched->node[i].n_band = graph->n_band;
1768 if (graph->n_band == 0)
1769 continue;
1771 band_end = isl_alloc_array(ctx, int, graph->n_band);
1772 band_id = isl_alloc_array(ctx, int, graph->n_band);
1773 zero = isl_alloc_array(ctx, int, graph->n_total_row);
1774 sched->node[i].band_end = band_end;
1775 sched->node[i].band_id = band_id;
1776 sched->node[i].zero = zero;
1777 if (!band_end || !band_id || !zero)
1778 goto error;
1780 for (r = 0; r < graph->n_total_row; ++r)
1781 zero[r] = graph->node[i].zero[r];
1782 for (r = b = 0; r < graph->n_total_row; ++r) {
1783 if (graph->node[i].band[r] == b)
1784 continue;
1785 band_end[b++] = r;
1786 if (graph->node[i].band[r] == -1)
1787 break;
1789 if (r == graph->n_total_row)
1790 band_end[b++] = r;
1791 sched->node[i].n_band = b;
1792 for (--b; b >= 0; --b)
1793 band_id[b] = graph->node[i].band_id[b];
1796 sched->dim = dim;
1798 return sched;
1799 error:
1800 isl_space_free(dim);
1801 isl_schedule_free(sched);
1802 return NULL;
1805 /* Copy nodes that satisfy node_pred from the src dependence graph
1806 * to the dst dependence graph.
1808 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
1809 int (*node_pred)(struct isl_sched_node *node, int data), int data)
1811 int i;
1813 dst->n = 0;
1814 for (i = 0; i < src->n; ++i) {
1815 if (!node_pred(&src->node[i], data))
1816 continue;
1817 dst->node[dst->n].dim = isl_space_copy(src->node[i].dim);
1818 dst->node[dst->n].nvar = src->node[i].nvar;
1819 dst->node[dst->n].nparam = src->node[i].nparam;
1820 dst->node[dst->n].sched = isl_mat_copy(src->node[i].sched);
1821 dst->node[dst->n].sched_map =
1822 isl_map_copy(src->node[i].sched_map);
1823 dst->node[dst->n].band = src->node[i].band;
1824 dst->node[dst->n].band_id = src->node[i].band_id;
1825 dst->node[dst->n].zero = src->node[i].zero;
1826 dst->n++;
1829 return 0;
1832 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
1833 * to the dst dependence graph.
1834 * If the source or destination node of the edge is not in the destination
1835 * graph, then it must be a backward proximity edge and it should simply
1836 * be ignored.
1838 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
1839 struct isl_sched_graph *src,
1840 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
1842 int i;
1843 enum isl_edge_type t;
1845 dst->n_edge = 0;
1846 for (i = 0; i < src->n_edge; ++i) {
1847 struct isl_sched_edge *edge = &src->edge[i];
1848 isl_map *map;
1849 struct isl_sched_node *dst_src, *dst_dst;
1851 if (!edge_pred(edge, data))
1852 continue;
1854 if (isl_map_plain_is_empty(edge->map))
1855 continue;
1857 dst_src = graph_find_node(ctx, dst, edge->src->dim);
1858 dst_dst = graph_find_node(ctx, dst, edge->dst->dim);
1859 if (!dst_src || !dst_dst) {
1860 if (edge->validity)
1861 isl_die(ctx, isl_error_internal,
1862 "backward validity edge", return -1);
1863 continue;
1866 map = isl_map_copy(edge->map);
1868 dst->edge[dst->n_edge].src = dst_src;
1869 dst->edge[dst->n_edge].dst = dst_dst;
1870 dst->edge[dst->n_edge].map = map;
1871 dst->edge[dst->n_edge].validity = edge->validity;
1872 dst->edge[dst->n_edge].proximity = edge->proximity;
1873 dst->n_edge++;
1875 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
1876 if (edge !=
1877 graph_find_edge(src, t, edge->src, edge->dst))
1878 continue;
1879 if (graph_edge_table_add(ctx, dst, t,
1880 &dst->edge[dst->n_edge - 1]) < 0)
1881 return -1;
1885 return 0;
1888 /* Given a "src" dependence graph that contains the nodes from "dst"
1889 * that satisfy node_pred, copy the schedule computed in "src"
1890 * for those nodes back to "dst".
1892 static int copy_schedule(struct isl_sched_graph *dst,
1893 struct isl_sched_graph *src,
1894 int (*node_pred)(struct isl_sched_node *node, int data), int data)
1896 int i;
1898 src->n = 0;
1899 for (i = 0; i < dst->n; ++i) {
1900 if (!node_pred(&dst->node[i], data))
1901 continue;
1902 isl_mat_free(dst->node[i].sched);
1903 isl_map_free(dst->node[i].sched_map);
1904 dst->node[i].sched = isl_mat_copy(src->node[src->n].sched);
1905 dst->node[i].sched_map =
1906 isl_map_copy(src->node[src->n].sched_map);
1907 src->n++;
1910 dst->max_row = src->max_row;
1911 dst->n_total_row = src->n_total_row;
1912 dst->n_band = src->n_band;
1914 return 0;
1917 /* Compute the maximal number of variables over all nodes.
1918 * This is the maximal number of linearly independent schedule
1919 * rows that we need to compute.
1920 * Just in case we end up in a part of the dependence graph
1921 * with only lower-dimensional domains, we make sure we will
1922 * compute the required amount of extra linearly independent rows.
1924 static int compute_maxvar(struct isl_sched_graph *graph)
1926 int i;
1928 graph->maxvar = 0;
1929 for (i = 0; i < graph->n; ++i) {
1930 struct isl_sched_node *node = &graph->node[i];
1931 int nvar;
1933 if (node_update_cmap(node) < 0)
1934 return -1;
1935 nvar = node->nvar + graph->n_row - node->rank;
1936 if (nvar > graph->maxvar)
1937 graph->maxvar = nvar;
1940 return 0;
1943 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph);
1944 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph);
1946 /* Compute a schedule for a subgraph of "graph". In particular, for
1947 * the graph composed of nodes that satisfy node_pred and edges that
1948 * that satisfy edge_pred. The caller should precompute the number
1949 * of nodes and edges that satisfy these predicates and pass them along
1950 * as "n" and "n_edge".
1951 * If the subgraph is known to consist of a single component, then wcc should
1952 * be set and then we call compute_schedule_wcc on the constructed subgraph.
1953 * Otherwise, we call compute_schedule, which will check whether the subgraph
1954 * is connected.
1956 static int compute_sub_schedule(isl_ctx *ctx,
1957 struct isl_sched_graph *graph, int n, int n_edge,
1958 int (*node_pred)(struct isl_sched_node *node, int data),
1959 int (*edge_pred)(struct isl_sched_edge *edge, int data),
1960 int data, int wcc)
1962 struct isl_sched_graph split = { 0 };
1963 int t;
1965 if (graph_alloc(ctx, &split, n, n_edge) < 0)
1966 goto error;
1967 if (copy_nodes(&split, graph, node_pred, data) < 0)
1968 goto error;
1969 if (graph_init_table(ctx, &split) < 0)
1970 goto error;
1971 for (t = 0; t <= isl_edge_last; ++t)
1972 split.max_edge[t] = graph->max_edge[t];
1973 if (graph_init_edge_tables(ctx, &split) < 0)
1974 goto error;
1975 if (copy_edges(ctx, &split, graph, edge_pred, data) < 0)
1976 goto error;
1977 split.n_row = graph->n_row;
1978 split.max_row = graph->max_row;
1979 split.n_total_row = graph->n_total_row;
1980 split.n_band = graph->n_band;
1981 split.band_start = graph->band_start;
1983 if (wcc && compute_schedule_wcc(ctx, &split) < 0)
1984 goto error;
1985 if (!wcc && compute_schedule(ctx, &split) < 0)
1986 goto error;
1988 copy_schedule(graph, &split, node_pred, data);
1990 graph_free(ctx, &split);
1991 return 0;
1992 error:
1993 graph_free(ctx, &split);
1994 return -1;
1997 static int node_scc_exactly(struct isl_sched_node *node, int scc)
1999 return node->scc == scc;
2002 static int node_scc_at_most(struct isl_sched_node *node, int scc)
2004 return node->scc <= scc;
2007 static int node_scc_at_least(struct isl_sched_node *node, int scc)
2009 return node->scc >= scc;
2012 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
2014 return edge->src->scc == scc && edge->dst->scc == scc;
2017 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
2019 return edge->dst->scc <= scc;
2022 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
2024 return edge->src->scc >= scc;
2027 /* Pad the schedules of all nodes with zero rows such that in the end
2028 * they all have graph->n_total_row rows.
2029 * The extra rows don't belong to any band, so they get assigned band number -1.
2031 static int pad_schedule(struct isl_sched_graph *graph)
2033 int i, j;
2035 for (i = 0; i < graph->n; ++i) {
2036 struct isl_sched_node *node = &graph->node[i];
2037 int row = isl_mat_rows(node->sched);
2038 if (graph->n_total_row > row) {
2039 isl_map_free(node->sched_map);
2040 node->sched_map = NULL;
2042 node->sched = isl_mat_add_zero_rows(node->sched,
2043 graph->n_total_row - row);
2044 if (!node->sched)
2045 return -1;
2046 for (j = row; j < graph->n_total_row; ++j)
2047 node->band[j] = -1;
2050 return 0;
2053 /* Split the current graph into two parts and compute a schedule for each
2054 * part individually. In particular, one part consists of all SCCs up
2055 * to and including graph->src_scc, while the other part contains the other
2056 * SCCS.
2058 * The split is enforced in the schedule by constant rows with two different
2059 * values (0 and 1). These constant rows replace the previously computed rows
2060 * in the current band.
2061 * It would be possible to reuse them as the first rows in the next
2062 * band, but recomputing them may result in better rows as we are looking
2063 * at a smaller part of the dependence graph.
2064 * compute_split_schedule is only called when no zero-distance schedule row
2065 * could be found on the entire graph, so we wark the splitting row as
2066 * non zero-distance.
2068 * The band_id of the second group is set to n, where n is the number
2069 * of nodes in the first group. This ensures that the band_ids over
2070 * the two groups remain disjoint, even if either or both of the two
2071 * groups contain independent components.
2073 static int compute_split_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
2075 int i, j, n, e1, e2;
2076 int n_total_row, orig_total_row;
2077 int n_band, orig_band;
2078 int drop;
2080 if (graph->n_total_row >= graph->max_row)
2081 isl_die(ctx, isl_error_internal,
2082 "too many schedule rows", return -1);
2084 drop = graph->n_total_row - graph->band_start;
2085 graph->n_total_row -= drop;
2086 graph->n_row -= drop;
2088 n = 0;
2089 for (i = 0; i < graph->n; ++i) {
2090 struct isl_sched_node *node = &graph->node[i];
2091 int row = isl_mat_rows(node->sched) - drop;
2092 int cols = isl_mat_cols(node->sched);
2093 int before = node->scc <= graph->src_scc;
2095 if (before)
2096 n++;
2098 isl_map_free(node->sched_map);
2099 node->sched_map = NULL;
2100 node->sched = isl_mat_drop_rows(node->sched,
2101 graph->band_start, drop);
2102 node->sched = isl_mat_add_rows(node->sched, 1);
2103 if (!node->sched)
2104 return -1;
2105 node->sched = isl_mat_set_element_si(node->sched, row, 0,
2106 !before);
2107 for (j = 1; j < cols; ++j)
2108 node->sched = isl_mat_set_element_si(node->sched,
2109 row, j, 0);
2110 node->band[graph->n_total_row] = graph->n_band;
2111 node->zero[graph->n_total_row] = 0;
2114 e1 = e2 = 0;
2115 for (i = 0; i < graph->n_edge; ++i) {
2116 if (graph->edge[i].dst->scc <= graph->src_scc)
2117 e1++;
2118 if (graph->edge[i].src->scc > graph->src_scc)
2119 e2++;
2122 graph->n_total_row++;
2123 next_band(graph);
2125 for (i = 0; i < graph->n; ++i) {
2126 struct isl_sched_node *node = &graph->node[i];
2127 if (node->scc > graph->src_scc)
2128 node->band_id[graph->n_band] = n;
2131 orig_total_row = graph->n_total_row;
2132 orig_band = graph->n_band;
2133 if (compute_sub_schedule(ctx, graph, n, e1,
2134 &node_scc_at_most, &edge_dst_scc_at_most,
2135 graph->src_scc, 0) < 0)
2136 return -1;
2137 n_total_row = graph->n_total_row;
2138 graph->n_total_row = orig_total_row;
2139 n_band = graph->n_band;
2140 graph->n_band = orig_band;
2141 if (compute_sub_schedule(ctx, graph, graph->n - n, e2,
2142 &node_scc_at_least, &edge_src_scc_at_least,
2143 graph->src_scc + 1, 0) < 0)
2144 return -1;
2145 if (n_total_row > graph->n_total_row)
2146 graph->n_total_row = n_total_row;
2147 if (n_band > graph->n_band)
2148 graph->n_band = n_band;
2150 return pad_schedule(graph);
2153 /* Compute the next band of the schedule after updating the dependence
2154 * relations based on the the current schedule.
2156 static int compute_next_band(isl_ctx *ctx, struct isl_sched_graph *graph)
2158 if (update_edges(ctx, graph) < 0)
2159 return -1;
2160 next_band(graph);
2162 return compute_schedule(ctx, graph);
2165 /* Add constraints to graph->lp that force the dependence "map" (which
2166 * is part of the dependence relation of "edge")
2167 * to be respected and attempt to carry it, where the edge is one from
2168 * a node j to itself. "pos" is the sequence number of the given map.
2169 * That is, add constraints that enforce
2171 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
2172 * = c_j_x (y - x) >= e_i
2174 * for each (x,y) in R.
2175 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2176 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
2177 * with each coefficient in c_j_x represented as a pair of non-negative
2178 * coefficients.
2180 static int add_intra_constraints(struct isl_sched_graph *graph,
2181 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
2183 unsigned total;
2184 isl_ctx *ctx = isl_map_get_ctx(map);
2185 isl_space *dim;
2186 isl_dim_map *dim_map;
2187 isl_basic_set *coef;
2188 struct isl_sched_node *node = edge->src;
2190 coef = intra_coefficients(graph, map);
2192 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
2194 total = isl_basic_set_total_dim(graph->lp);
2195 dim_map = isl_dim_map_alloc(ctx, total);
2196 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
2197 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
2198 isl_space_dim(dim, isl_dim_set), 1,
2199 node->nvar, -1);
2200 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
2201 isl_space_dim(dim, isl_dim_set), 1,
2202 node->nvar, 1);
2203 graph->lp = isl_basic_set_extend_constraints(graph->lp,
2204 coef->n_eq, coef->n_ineq);
2205 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
2206 coef, dim_map);
2207 isl_space_free(dim);
2209 return 0;
2212 /* Add constraints to graph->lp that force the dependence "map" (which
2213 * is part of the dependence relation of "edge")
2214 * to be respected and attempt to carry it, where the edge is one from
2215 * node j to node k. "pos" is the sequence number of the given map.
2216 * That is, add constraints that enforce
2218 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
2220 * for each (x,y) in R.
2221 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2222 * of valid constraints for R and then plug in
2223 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
2224 * with each coefficient (except e_i, c_k_0 and c_j_0)
2225 * represented as a pair of non-negative coefficients.
2227 static int add_inter_constraints(struct isl_sched_graph *graph,
2228 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
2230 unsigned total;
2231 isl_ctx *ctx = isl_map_get_ctx(map);
2232 isl_space *dim;
2233 isl_dim_map *dim_map;
2234 isl_basic_set *coef;
2235 struct isl_sched_node *src = edge->src;
2236 struct isl_sched_node *dst = edge->dst;
2238 coef = inter_coefficients(graph, map);
2240 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
2242 total = isl_basic_set_total_dim(graph->lp);
2243 dim_map = isl_dim_map_alloc(ctx, total);
2245 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
2247 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
2248 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
2249 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
2250 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
2251 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
2252 dst->nvar, -1);
2253 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
2254 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
2255 dst->nvar, 1);
2257 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
2258 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
2259 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
2260 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
2261 isl_space_dim(dim, isl_dim_set), 1,
2262 src->nvar, 1);
2263 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
2264 isl_space_dim(dim, isl_dim_set), 1,
2265 src->nvar, -1);
2267 graph->lp = isl_basic_set_extend_constraints(graph->lp,
2268 coef->n_eq, coef->n_ineq);
2269 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
2270 coef, dim_map);
2271 isl_space_free(dim);
2273 return 0;
2276 /* Add constraints to graph->lp that force all validity dependences
2277 * to be respected and attempt to carry them.
2279 static int add_all_constraints(struct isl_sched_graph *graph)
2281 int i, j;
2282 int pos;
2284 pos = 0;
2285 for (i = 0; i < graph->n_edge; ++i) {
2286 struct isl_sched_edge *edge= &graph->edge[i];
2288 if (!edge->validity)
2289 continue;
2291 for (j = 0; j < edge->map->n; ++j) {
2292 isl_basic_map *bmap;
2293 isl_map *map;
2295 bmap = isl_basic_map_copy(edge->map->p[j]);
2296 map = isl_map_from_basic_map(bmap);
2298 if (edge->src == edge->dst &&
2299 add_intra_constraints(graph, edge, map, pos) < 0)
2300 return -1;
2301 if (edge->src != edge->dst &&
2302 add_inter_constraints(graph, edge, map, pos) < 0)
2303 return -1;
2304 ++pos;
2308 return 0;
2311 /* Count the number of equality and inequality constraints
2312 * that will be added to the carry_lp problem.
2313 * We count each edge exactly once.
2315 static int count_all_constraints(struct isl_sched_graph *graph,
2316 int *n_eq, int *n_ineq)
2318 int i, j;
2320 *n_eq = *n_ineq = 0;
2321 for (i = 0; i < graph->n_edge; ++i) {
2322 struct isl_sched_edge *edge= &graph->edge[i];
2323 for (j = 0; j < edge->map->n; ++j) {
2324 isl_basic_map *bmap;
2325 isl_map *map;
2327 bmap = isl_basic_map_copy(edge->map->p[j]);
2328 map = isl_map_from_basic_map(bmap);
2330 if (count_map_constraints(graph, edge, map,
2331 n_eq, n_ineq, 1) < 0)
2332 return -1;
2336 return 0;
2339 /* Construct an LP problem for finding schedule coefficients
2340 * such that the schedule carries as many dependences as possible.
2341 * In particular, for each dependence i, we bound the dependence distance
2342 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
2343 * of all e_i's. Dependence with e_i = 0 in the solution are simply
2344 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
2345 * Note that if the dependence relation is a union of basic maps,
2346 * then we have to consider each basic map individually as it may only
2347 * be possible to carry the dependences expressed by some of those
2348 * basic maps and not all off them.
2349 * Below, we consider each of those basic maps as a separate "edge".
2351 * All variables of the LP are non-negative. The actual coefficients
2352 * may be negative, so each coefficient is represented as the difference
2353 * of two non-negative variables. The negative part always appears
2354 * immediately before the positive part.
2355 * Other than that, the variables have the following order
2357 * - sum of (1 - e_i) over all edges
2358 * - sum of positive and negative parts of all c_n coefficients
2359 * (unconstrained when computing non-parametric schedules)
2360 * - sum of positive and negative parts of all c_x coefficients
2361 * - for each edge
2362 * - e_i
2363 * - for each node
2364 * - c_i_0
2365 * - positive and negative parts of c_i_n (if parametric)
2366 * - positive and negative parts of c_i_x
2368 * The constraints are those from the (validity) edges plus three equalities
2369 * to express the sums and n_edge inequalities to express e_i <= 1.
2371 static int setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
2373 int i, j;
2374 int k;
2375 isl_space *dim;
2376 unsigned total;
2377 int n_eq, n_ineq;
2378 int n_edge;
2380 n_edge = 0;
2381 for (i = 0; i < graph->n_edge; ++i)
2382 n_edge += graph->edge[i].map->n;
2384 total = 3 + n_edge;
2385 for (i = 0; i < graph->n; ++i) {
2386 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2387 node->start = total;
2388 total += 1 + 2 * (node->nparam + node->nvar);
2391 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
2392 return -1;
2394 dim = isl_space_set_alloc(ctx, 0, total);
2395 isl_basic_set_free(graph->lp);
2396 n_eq += 3;
2397 n_ineq += n_edge;
2398 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
2399 graph->lp = isl_basic_set_set_rational(graph->lp);
2401 k = isl_basic_set_alloc_equality(graph->lp);
2402 if (k < 0)
2403 return -1;
2404 isl_seq_clr(graph->lp->eq[k], 1 + total);
2405 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
2406 isl_int_set_si(graph->lp->eq[k][1], 1);
2407 for (i = 0; i < n_edge; ++i)
2408 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
2410 k = isl_basic_set_alloc_equality(graph->lp);
2411 if (k < 0)
2412 return -1;
2413 isl_seq_clr(graph->lp->eq[k], 1 + total);
2414 isl_int_set_si(graph->lp->eq[k][2], -1);
2415 for (i = 0; i < graph->n; ++i) {
2416 int pos = 1 + graph->node[i].start + 1;
2418 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
2419 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2422 k = isl_basic_set_alloc_equality(graph->lp);
2423 if (k < 0)
2424 return -1;
2425 isl_seq_clr(graph->lp->eq[k], 1 + total);
2426 isl_int_set_si(graph->lp->eq[k][3], -1);
2427 for (i = 0; i < graph->n; ++i) {
2428 struct isl_sched_node *node = &graph->node[i];
2429 int pos = 1 + node->start + 1 + 2 * node->nparam;
2431 for (j = 0; j < 2 * node->nvar; ++j)
2432 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2435 for (i = 0; i < n_edge; ++i) {
2436 k = isl_basic_set_alloc_inequality(graph->lp);
2437 if (k < 0)
2438 return -1;
2439 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2440 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
2441 isl_int_set_si(graph->lp->ineq[k][0], 1);
2444 if (add_all_constraints(graph) < 0)
2445 return -1;
2447 return 0;
2450 /* If the schedule_split_scaled option is set and if the linear
2451 * parts of the scheduling rows for all nodes in the graphs have
2452 * non-trivial common divisor, then split off the constant term
2453 * from the linear part.
2454 * The constant term is then placed in a separate band and
2455 * the linear part is reduced.
2457 static int split_scaled(isl_ctx *ctx, struct isl_sched_graph *graph)
2459 int i;
2460 int row;
2461 isl_int gcd, gcd_i;
2463 if (!ctx->opt->schedule_split_scaled)
2464 return 0;
2465 if (graph->n <= 1)
2466 return 0;
2468 if (graph->n_total_row >= graph->max_row)
2469 isl_die(ctx, isl_error_internal,
2470 "too many schedule rows", return -1);
2472 isl_int_init(gcd);
2473 isl_int_init(gcd_i);
2475 isl_int_set_si(gcd, 0);
2477 row = isl_mat_rows(graph->node[0].sched) - 1;
2479 for (i = 0; i < graph->n; ++i) {
2480 struct isl_sched_node *node = &graph->node[i];
2481 int cols = isl_mat_cols(node->sched);
2483 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
2484 isl_int_gcd(gcd, gcd, gcd_i);
2487 isl_int_clear(gcd_i);
2489 if (isl_int_cmp_si(gcd, 1) <= 0) {
2490 isl_int_clear(gcd);
2491 return 0;
2494 next_band(graph);
2496 for (i = 0; i < graph->n; ++i) {
2497 struct isl_sched_node *node = &graph->node[i];
2499 isl_map_free(node->sched_map);
2500 node->sched_map = NULL;
2501 node->sched = isl_mat_add_zero_rows(node->sched, 1);
2502 if (!node->sched)
2503 goto error;
2504 isl_int_fdiv_r(node->sched->row[row + 1][0],
2505 node->sched->row[row][0], gcd);
2506 isl_int_fdiv_q(node->sched->row[row][0],
2507 node->sched->row[row][0], gcd);
2508 isl_int_mul(node->sched->row[row][0],
2509 node->sched->row[row][0], gcd);
2510 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
2511 if (!node->sched)
2512 goto error;
2513 node->band[graph->n_total_row] = graph->n_band;
2516 graph->n_total_row++;
2518 isl_int_clear(gcd);
2519 return 0;
2520 error:
2521 isl_int_clear(gcd);
2522 return -1;
2525 static int compute_component_schedule(isl_ctx *ctx,
2526 struct isl_sched_graph *graph);
2528 /* Is the schedule row "sol" trivial on node "node"?
2529 * That is, is the solution zero on the dimensions orthogonal to
2530 * the previously found solutions?
2531 * Each coefficient is represented as the difference between
2532 * two non-negative values in "sol". The coefficient is then
2533 * zero if those two values are equal to each other.
2535 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
2537 int i;
2538 int pos;
2539 int len;
2541 pos = 1 + node->start + 1 + 2 * (node->nparam + node->rank);
2542 len = 2 * (node->nvar - node->rank);
2544 if (len == 0)
2545 return 0;
2547 for (i = 0; i < len; i += 2)
2548 if (isl_int_ne(sol->el[pos + i], sol->el[pos + i + 1]))
2549 return 0;
2551 return 1;
2554 /* Is the schedule row "sol" trivial on any node where it should
2555 * not be trivial?
2557 static int is_any_trivial(struct isl_sched_graph *graph,
2558 __isl_keep isl_vec *sol)
2560 int i;
2562 for (i = 0; i < graph->n; ++i) {
2563 struct isl_sched_node *node = &graph->node[i];
2565 if (!needs_row(graph, node))
2566 continue;
2567 if (is_trivial(node, sol))
2568 return 1;
2571 return 0;
2574 /* Construct a schedule row for each node such that as many dependences
2575 * as possible are carried and then continue with the next band.
2577 * If the computed schedule row turns out to be trivial on one or
2578 * more nodes where it should not be trivial, then we throw it away
2579 * and try again on each component separately.
2581 static int carry_dependences(isl_ctx *ctx, struct isl_sched_graph *graph)
2583 int i;
2584 int n_edge;
2585 isl_vec *sol;
2586 isl_basic_set *lp;
2588 n_edge = 0;
2589 for (i = 0; i < graph->n_edge; ++i)
2590 n_edge += graph->edge[i].map->n;
2592 if (setup_carry_lp(ctx, graph) < 0)
2593 return -1;
2595 lp = isl_basic_set_copy(graph->lp);
2596 sol = isl_tab_basic_set_non_neg_lexmin(lp);
2597 if (!sol)
2598 return -1;
2600 if (sol->size == 0) {
2601 isl_vec_free(sol);
2602 isl_die(ctx, isl_error_internal,
2603 "error in schedule construction", return -1);
2606 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
2607 isl_vec_free(sol);
2608 isl_die(ctx, isl_error_unknown,
2609 "unable to carry dependences", return -1);
2612 if (is_any_trivial(graph, sol)) {
2613 isl_vec_free(sol);
2614 if (graph->scc > 1)
2615 return compute_component_schedule(ctx, graph);
2616 isl_die(ctx, isl_error_unknown,
2617 "unable to construct non-trivial solution", return -1);
2620 if (update_schedule(graph, sol, 0, 0) < 0)
2621 return -1;
2623 if (split_scaled(ctx, graph) < 0)
2624 return -1;
2626 return compute_next_band(ctx, graph);
2629 /* Are there any (non-empty) validity edges in the graph?
2631 static int has_validity_edges(struct isl_sched_graph *graph)
2633 int i;
2635 for (i = 0; i < graph->n_edge; ++i) {
2636 int empty;
2638 empty = isl_map_plain_is_empty(graph->edge[i].map);
2639 if (empty < 0)
2640 return -1;
2641 if (empty)
2642 continue;
2643 if (graph->edge[i].validity)
2644 return 1;
2647 return 0;
2650 /* Should we apply a Feautrier step?
2651 * That is, did the user request the Feautrier algorithm and are
2652 * there any validity dependences (left)?
2654 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
2656 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
2657 return 0;
2659 return has_validity_edges(graph);
2662 /* Compute a schedule for a connected dependence graph using Feautrier's
2663 * multi-dimensional scheduling algorithm.
2664 * The original algorithm is described in [1].
2665 * The main idea is to minimize the number of scheduling dimensions, by
2666 * trying to satisfy as many dependences as possible per scheduling dimension.
2668 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
2669 * Problem, Part II: Multi-Dimensional Time.
2670 * In Intl. Journal of Parallel Programming, 1992.
2672 static int compute_schedule_wcc_feautrier(isl_ctx *ctx,
2673 struct isl_sched_graph *graph)
2675 return carry_dependences(ctx, graph);
2678 /* Compute a schedule for a connected dependence graph.
2679 * We try to find a sequence of as many schedule rows as possible that result
2680 * in non-negative dependence distances (independent of the previous rows
2681 * in the sequence, i.e., such that the sequence is tilable).
2682 * If we can't find any more rows we either
2683 * - split between SCCs and start over (assuming we found an interesting
2684 * pair of SCCs between which to split)
2685 * - continue with the next band (assuming the current band has at least
2686 * one row)
2687 * - try to carry as many dependences as possible and continue with the next
2688 * band
2690 * If Feautrier's algorithm is selected, we first recursively try to satisfy
2691 * as many validity dependences as possible. When all validity dependences
2692 * are satisfied we extend the schedule to a full-dimensional schedule.
2694 * If we manage to complete the schedule, we finish off by topologically
2695 * sorting the statements based on the remaining dependences.
2697 * If ctx->opt->schedule_outer_zero_distance is set, then we force the
2698 * outermost dimension in the current band to be zero distance. If this
2699 * turns out to be impossible, we fall back on the general scheme above
2700 * and try to carry as many dependences as possible.
2702 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph)
2704 int force_zero = 0;
2706 if (detect_sccs(ctx, graph) < 0)
2707 return -1;
2708 if (sort_sccs(graph) < 0)
2709 return -1;
2711 if (compute_maxvar(graph) < 0)
2712 return -1;
2714 if (need_feautrier_step(ctx, graph))
2715 return compute_schedule_wcc_feautrier(ctx, graph);
2717 if (ctx->opt->schedule_outer_zero_distance)
2718 force_zero = 1;
2720 while (graph->n_row < graph->maxvar) {
2721 isl_vec *sol;
2723 graph->src_scc = -1;
2724 graph->dst_scc = -1;
2726 if (setup_lp(ctx, graph, force_zero) < 0)
2727 return -1;
2728 sol = solve_lp(graph);
2729 if (!sol)
2730 return -1;
2731 if (sol->size == 0) {
2732 isl_vec_free(sol);
2733 if (!ctx->opt->schedule_maximize_band_depth &&
2734 graph->n_total_row > graph->band_start)
2735 return compute_next_band(ctx, graph);
2736 if (graph->src_scc >= 0)
2737 return compute_split_schedule(ctx, graph);
2738 if (graph->n_total_row > graph->band_start)
2739 return compute_next_band(ctx, graph);
2740 return carry_dependences(ctx, graph);
2742 if (update_schedule(graph, sol, 1, 1) < 0)
2743 return -1;
2744 force_zero = 0;
2747 if (graph->n_total_row > graph->band_start)
2748 next_band(graph);
2749 return sort_statements(ctx, graph);
2752 /* Add a row to the schedules that separates the SCCs and move
2753 * to the next band.
2755 static int split_on_scc(isl_ctx *ctx, struct isl_sched_graph *graph)
2757 int i;
2759 if (graph->n_total_row >= graph->max_row)
2760 isl_die(ctx, isl_error_internal,
2761 "too many schedule rows", return -1);
2763 for (i = 0; i < graph->n; ++i) {
2764 struct isl_sched_node *node = &graph->node[i];
2765 int row = isl_mat_rows(node->sched);
2767 isl_map_free(node->sched_map);
2768 node->sched_map = NULL;
2769 node->sched = isl_mat_add_zero_rows(node->sched, 1);
2770 node->sched = isl_mat_set_element_si(node->sched, row, 0,
2771 node->scc);
2772 if (!node->sched)
2773 return -1;
2774 node->band[graph->n_total_row] = graph->n_band;
2777 graph->n_total_row++;
2778 next_band(graph);
2780 return 0;
2783 /* Compute a schedule for each component (identified by node->scc)
2784 * of the dependence graph separately and then combine the results.
2785 * Depending on the setting of schedule_fuse, a component may be
2786 * either weakly or strongly connected.
2788 * The band_id is adjusted such that each component has a separate id.
2789 * Note that the band_id may have already been set to a value different
2790 * from zero by compute_split_schedule.
2792 static int compute_component_schedule(isl_ctx *ctx,
2793 struct isl_sched_graph *graph)
2795 int wcc, i;
2796 int n, n_edge;
2797 int n_total_row, orig_total_row;
2798 int n_band, orig_band;
2800 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN ||
2801 ctx->opt->schedule_separate_components)
2802 if (split_on_scc(ctx, graph) < 0)
2803 return -1;
2805 n_total_row = 0;
2806 orig_total_row = graph->n_total_row;
2807 n_band = 0;
2808 orig_band = graph->n_band;
2809 for (i = 0; i < graph->n; ++i)
2810 graph->node[i].band_id[graph->n_band] += graph->node[i].scc;
2811 for (wcc = 0; wcc < graph->scc; ++wcc) {
2812 n = 0;
2813 for (i = 0; i < graph->n; ++i)
2814 if (graph->node[i].scc == wcc)
2815 n++;
2816 n_edge = 0;
2817 for (i = 0; i < graph->n_edge; ++i)
2818 if (graph->edge[i].src->scc == wcc &&
2819 graph->edge[i].dst->scc == wcc)
2820 n_edge++;
2822 if (compute_sub_schedule(ctx, graph, n, n_edge,
2823 &node_scc_exactly,
2824 &edge_scc_exactly, wcc, 1) < 0)
2825 return -1;
2826 if (graph->n_total_row > n_total_row)
2827 n_total_row = graph->n_total_row;
2828 graph->n_total_row = orig_total_row;
2829 if (graph->n_band > n_band)
2830 n_band = graph->n_band;
2831 graph->n_band = orig_band;
2834 graph->n_total_row = n_total_row;
2835 graph->n_band = n_band;
2837 return pad_schedule(graph);
2840 /* Compute a schedule for the given dependence graph.
2841 * We first check if the graph is connected (through validity dependences)
2842 * and, if not, compute a schedule for each component separately.
2843 * If schedule_fuse is set to minimal fusion, then we check for strongly
2844 * connected components instead and compute a separate schedule for
2845 * each such strongly connected component.
2847 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
2849 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN) {
2850 if (detect_sccs(ctx, graph) < 0)
2851 return -1;
2852 } else {
2853 if (detect_wccs(ctx, graph) < 0)
2854 return -1;
2857 if (graph->scc > 1)
2858 return compute_component_schedule(ctx, graph);
2860 return compute_schedule_wcc(ctx, graph);
2863 /* Compute a schedule for the given union of domains that respects
2864 * all the validity dependences.
2865 * If the default isl scheduling algorithm is used, it tries to minimize
2866 * the dependence distances over the proximity dependences.
2867 * If Feautrier's scheduling algorithm is used, the proximity dependence
2868 * distances are only minimized during the extension to a full-dimensional
2869 * schedule.
2871 __isl_give isl_schedule *isl_union_set_compute_schedule(
2872 __isl_take isl_union_set *domain,
2873 __isl_take isl_union_map *validity,
2874 __isl_take isl_union_map *proximity)
2876 isl_ctx *ctx = isl_union_set_get_ctx(domain);
2877 isl_space *dim;
2878 struct isl_sched_graph graph = { 0 };
2879 isl_schedule *sched;
2880 struct isl_extract_edge_data data;
2882 domain = isl_union_set_align_params(domain,
2883 isl_union_map_get_space(validity));
2884 domain = isl_union_set_align_params(domain,
2885 isl_union_map_get_space(proximity));
2886 dim = isl_union_set_get_space(domain);
2887 validity = isl_union_map_align_params(validity, isl_space_copy(dim));
2888 proximity = isl_union_map_align_params(proximity, dim);
2890 if (!domain)
2891 goto error;
2893 graph.n = isl_union_set_n_set(domain);
2894 if (graph.n == 0)
2895 goto empty;
2896 if (graph_alloc(ctx, &graph, graph.n,
2897 isl_union_map_n_map(validity) + isl_union_map_n_map(proximity)) < 0)
2898 goto error;
2899 if (compute_max_row(&graph, domain) < 0)
2900 goto error;
2901 graph.root = 1;
2902 graph.n = 0;
2903 if (isl_union_set_foreach_set(domain, &extract_node, &graph) < 0)
2904 goto error;
2905 if (graph_init_table(ctx, &graph) < 0)
2906 goto error;
2907 graph.max_edge[isl_edge_validity] = isl_union_map_n_map(validity);
2908 graph.max_edge[isl_edge_proximity] = isl_union_map_n_map(proximity);
2909 if (graph_init_edge_tables(ctx, &graph) < 0)
2910 goto error;
2911 graph.n_edge = 0;
2912 data.graph = &graph;
2913 data.type = isl_edge_validity;
2914 if (isl_union_map_foreach_map(validity, &extract_edge, &data) < 0)
2915 goto error;
2916 data.type = isl_edge_proximity;
2917 if (isl_union_map_foreach_map(proximity, &extract_edge, &data) < 0)
2918 goto error;
2920 if (compute_schedule(ctx, &graph) < 0)
2921 goto error;
2923 empty:
2924 sched = extract_schedule(&graph, isl_union_set_get_space(domain));
2926 graph_free(ctx, &graph);
2927 isl_union_set_free(domain);
2928 isl_union_map_free(validity);
2929 isl_union_map_free(proximity);
2931 return sched;
2932 error:
2933 graph_free(ctx, &graph);
2934 isl_union_set_free(domain);
2935 isl_union_map_free(validity);
2936 isl_union_map_free(proximity);
2937 return NULL;
2940 void *isl_schedule_free(__isl_take isl_schedule *sched)
2942 int i;
2943 if (!sched)
2944 return NULL;
2946 if (--sched->ref > 0)
2947 return NULL;
2949 for (i = 0; i < sched->n; ++i) {
2950 isl_multi_aff_free(sched->node[i].sched);
2951 free(sched->node[i].band_end);
2952 free(sched->node[i].band_id);
2953 free(sched->node[i].zero);
2955 isl_space_free(sched->dim);
2956 isl_band_list_free(sched->band_forest);
2957 free(sched);
2958 return NULL;
2961 isl_ctx *isl_schedule_get_ctx(__isl_keep isl_schedule *schedule)
2963 return schedule ? isl_space_get_ctx(schedule->dim) : NULL;
2966 /* Return an isl_union_map of the schedule. If we have already constructed
2967 * a band forest, then this band forest may have been modified so we need
2968 * to extract the isl_union_map from the forest rather than from
2969 * the originally computed schedule.
2971 __isl_give isl_union_map *isl_schedule_get_map(__isl_keep isl_schedule *sched)
2973 int i;
2974 isl_union_map *umap;
2976 if (!sched)
2977 return NULL;
2979 if (sched->band_forest)
2980 return isl_band_list_get_suffix_schedule(sched->band_forest);
2982 umap = isl_union_map_empty(isl_space_copy(sched->dim));
2983 for (i = 0; i < sched->n; ++i) {
2984 isl_multi_aff *ma;
2986 ma = isl_multi_aff_copy(sched->node[i].sched);
2987 umap = isl_union_map_add_map(umap, isl_map_from_multi_aff(ma));
2990 return umap;
2993 static __isl_give isl_band_list *construct_band_list(
2994 __isl_keep isl_schedule *schedule, __isl_keep isl_band *parent,
2995 int band_nr, int *parent_active, int n_active);
2997 /* Construct an isl_band structure for the band in the given schedule
2998 * with sequence number band_nr for the n_active nodes marked by active.
2999 * If the nodes don't have a band with the given sequence number,
3000 * then a band without members is created.
3002 * Because of the way the schedule is constructed, we know that
3003 * the position of the band inside the schedule of a node is the same
3004 * for all active nodes.
3006 static __isl_give isl_band *construct_band(__isl_keep isl_schedule *schedule,
3007 __isl_keep isl_band *parent,
3008 int band_nr, int *active, int n_active)
3010 int i, j;
3011 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
3012 isl_band *band;
3013 unsigned start, end;
3015 band = isl_band_alloc(ctx);
3016 if (!band)
3017 return NULL;
3019 band->schedule = schedule;
3020 band->parent = parent;
3022 for (i = 0; i < schedule->n; ++i)
3023 if (active[i] && schedule->node[i].n_band > band_nr + 1)
3024 break;
3026 if (i < schedule->n) {
3027 band->children = construct_band_list(schedule, band,
3028 band_nr + 1, active, n_active);
3029 if (!band->children)
3030 goto error;
3033 for (i = 0; i < schedule->n; ++i)
3034 if (active[i])
3035 break;
3037 if (i >= schedule->n)
3038 isl_die(ctx, isl_error_internal,
3039 "band without active statements", goto error);
3041 start = band_nr ? schedule->node[i].band_end[band_nr - 1] : 0;
3042 end = band_nr < schedule->node[i].n_band ?
3043 schedule->node[i].band_end[band_nr] : start;
3044 band->n = end - start;
3046 band->zero = isl_alloc_array(ctx, int, band->n);
3047 if (!band->zero)
3048 goto error;
3050 for (j = 0; j < band->n; ++j)
3051 band->zero[j] = schedule->node[i].zero[start + j];
3053 band->pma = isl_union_pw_multi_aff_empty(isl_space_copy(schedule->dim));
3054 for (i = 0; i < schedule->n; ++i) {
3055 isl_multi_aff *ma;
3056 isl_pw_multi_aff *pma;
3057 unsigned n_out;
3059 if (!active[i])
3060 continue;
3062 ma = isl_multi_aff_copy(schedule->node[i].sched);
3063 n_out = isl_multi_aff_dim(ma, isl_dim_out);
3064 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, end, n_out - end);
3065 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, 0, start);
3066 pma = isl_pw_multi_aff_from_multi_aff(ma);
3067 band->pma = isl_union_pw_multi_aff_add_pw_multi_aff(band->pma,
3068 pma);
3070 if (!band->pma)
3071 goto error;
3073 return band;
3074 error:
3075 isl_band_free(band);
3076 return NULL;
3079 /* Construct a list of bands that start at the same position (with
3080 * sequence number band_nr) in the schedules of the nodes that
3081 * were active in the parent band.
3083 * A separate isl_band structure is created for each band_id
3084 * and for each node that does not have a band with sequence
3085 * number band_nr. In the latter case, a band without members
3086 * is created.
3087 * This ensures that if a band has any children, then each node
3088 * that was active in the band is active in exactly one of the children.
3090 static __isl_give isl_band_list *construct_band_list(
3091 __isl_keep isl_schedule *schedule, __isl_keep isl_band *parent,
3092 int band_nr, int *parent_active, int n_active)
3094 int i, j;
3095 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
3096 int *active;
3097 int n_band;
3098 isl_band_list *list;
3100 n_band = 0;
3101 for (i = 0; i < n_active; ++i) {
3102 for (j = 0; j < schedule->n; ++j) {
3103 if (!parent_active[j])
3104 continue;
3105 if (schedule->node[j].n_band <= band_nr)
3106 continue;
3107 if (schedule->node[j].band_id[band_nr] == i) {
3108 n_band++;
3109 break;
3113 for (j = 0; j < schedule->n; ++j)
3114 if (schedule->node[j].n_band <= band_nr)
3115 n_band++;
3117 if (n_band == 1) {
3118 isl_band *band;
3119 list = isl_band_list_alloc(ctx, n_band);
3120 band = construct_band(schedule, parent, band_nr,
3121 parent_active, n_active);
3122 return isl_band_list_add(list, band);
3125 active = isl_alloc_array(ctx, int, schedule->n);
3126 if (!active)
3127 return NULL;
3129 list = isl_band_list_alloc(ctx, n_band);
3131 for (i = 0; i < n_active; ++i) {
3132 int n = 0;
3133 isl_band *band;
3135 for (j = 0; j < schedule->n; ++j) {
3136 active[j] = parent_active[j] &&
3137 schedule->node[j].n_band > band_nr &&
3138 schedule->node[j].band_id[band_nr] == i;
3139 if (active[j])
3140 n++;
3142 if (n == 0)
3143 continue;
3145 band = construct_band(schedule, parent, band_nr, active, n);
3147 list = isl_band_list_add(list, band);
3149 for (i = 0; i < schedule->n; ++i) {
3150 isl_band *band;
3151 if (!parent_active[i])
3152 continue;
3153 if (schedule->node[i].n_band > band_nr)
3154 continue;
3155 for (j = 0; j < schedule->n; ++j)
3156 active[j] = j == i;
3157 band = construct_band(schedule, parent, band_nr, active, 1);
3158 list = isl_band_list_add(list, band);
3161 free(active);
3163 return list;
3166 /* Construct a band forest representation of the schedule and
3167 * return the list of roots.
3169 static __isl_give isl_band_list *construct_forest(
3170 __isl_keep isl_schedule *schedule)
3172 int i;
3173 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
3174 isl_band_list *forest;
3175 int *active;
3177 active = isl_alloc_array(ctx, int, schedule->n);
3178 if (!active)
3179 return NULL;
3181 for (i = 0; i < schedule->n; ++i)
3182 active[i] = 1;
3184 forest = construct_band_list(schedule, NULL, 0, active, schedule->n);
3186 free(active);
3188 return forest;
3191 /* Return the roots of a band forest representation of the schedule.
3193 __isl_give isl_band_list *isl_schedule_get_band_forest(
3194 __isl_keep isl_schedule *schedule)
3196 if (!schedule)
3197 return NULL;
3198 if (!schedule->band_forest)
3199 schedule->band_forest = construct_forest(schedule);
3200 return isl_band_list_dup(schedule->band_forest);
3203 /* Call "fn" on each band in the schedule in depth-first post-order.
3205 int isl_schedule_foreach_band(__isl_keep isl_schedule *sched,
3206 int (*fn)(__isl_keep isl_band *band, void *user), void *user)
3208 int r;
3209 isl_band_list *forest;
3211 if (!sched)
3212 return -1;
3214 forest = isl_schedule_get_band_forest(sched);
3215 r = isl_band_list_foreach_band(forest, fn, user);
3216 isl_band_list_free(forest);
3218 return r;
3221 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
3222 __isl_keep isl_band_list *list);
3224 static __isl_give isl_printer *print_band(__isl_take isl_printer *p,
3225 __isl_keep isl_band *band)
3227 isl_band_list *children;
3229 p = isl_printer_start_line(p);
3230 p = isl_printer_print_union_pw_multi_aff(p, band->pma);
3231 p = isl_printer_end_line(p);
3233 if (!isl_band_has_children(band))
3234 return p;
3236 children = isl_band_get_children(band);
3238 p = isl_printer_indent(p, 4);
3239 p = print_band_list(p, children);
3240 p = isl_printer_indent(p, -4);
3242 isl_band_list_free(children);
3244 return p;
3247 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
3248 __isl_keep isl_band_list *list)
3250 int i, n;
3252 n = isl_band_list_n_band(list);
3253 for (i = 0; i < n; ++i) {
3254 isl_band *band;
3255 band = isl_band_list_get_band(list, i);
3256 p = print_band(p, band);
3257 isl_band_free(band);
3260 return p;
3263 __isl_give isl_printer *isl_printer_print_schedule(__isl_take isl_printer *p,
3264 __isl_keep isl_schedule *schedule)
3266 isl_band_list *forest;
3268 forest = isl_schedule_get_band_forest(schedule);
3270 p = print_band_list(p, forest);
3272 isl_band_list_free(forest);
3274 return p;
3277 void isl_schedule_dump(__isl_keep isl_schedule *schedule)
3279 isl_printer *printer;
3281 if (!schedule)
3282 return;
3284 printer = isl_printer_to_file(isl_schedule_get_ctx(schedule), stderr);
3285 printer = isl_printer_print_schedule(printer, schedule);
3287 isl_printer_free(printer);