update for change in clang's DiagnosticOptions
[isl.git] / isl_schedule.c
blobf2c3c892c166d7dd36bf5326e9bd70800b034a20
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));
798 total = isl_basic_set_total_dim(graph->lp);
799 dim_map = isl_dim_map_alloc(ctx, total);
800 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
801 isl_space_dim(dim, isl_dim_set), 1,
802 node->nvar, -1);
803 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
804 isl_space_dim(dim, isl_dim_set), 1,
805 node->nvar, 1);
806 graph->lp = isl_basic_set_extend_constraints(graph->lp,
807 coef->n_eq, coef->n_ineq);
808 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
809 coef, dim_map);
810 isl_space_free(dim);
812 return 0;
815 /* Add constraints to graph->lp that force validity for the given
816 * dependence from node i to node j.
817 * That is, add constraints that enforce
819 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
821 * for each (x,y) in R.
822 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
823 * of valid constraints for R and then plug in
824 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
825 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
826 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
827 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
829 * Actually, we do not construct constraints for the c_*_x themselves,
830 * but for the coefficients of c_*_x written as a linear combination
831 * of the columns in node->cmap.
833 static int add_inter_validity_constraints(struct isl_sched_graph *graph,
834 struct isl_sched_edge *edge)
836 unsigned total;
837 isl_map *map = isl_map_copy(edge->map);
838 isl_ctx *ctx = isl_map_get_ctx(map);
839 isl_space *dim;
840 isl_dim_map *dim_map;
841 isl_basic_set *coef;
842 struct isl_sched_node *src = edge->src;
843 struct isl_sched_node *dst = edge->dst;
845 coef = inter_coefficients(graph, map);
847 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
849 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
850 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
851 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
852 isl_space_dim(dim, isl_dim_set) + src->nvar,
853 isl_mat_copy(dst->cmap));
855 total = isl_basic_set_total_dim(graph->lp);
856 dim_map = isl_dim_map_alloc(ctx, total);
858 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
859 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
860 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
861 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
862 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
863 dst->nvar, -1);
864 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
865 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
866 dst->nvar, 1);
868 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
869 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
870 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
871 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
872 isl_space_dim(dim, isl_dim_set), 1,
873 src->nvar, 1);
874 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
875 isl_space_dim(dim, isl_dim_set), 1,
876 src->nvar, -1);
878 edge->start = graph->lp->n_ineq;
879 graph->lp = isl_basic_set_extend_constraints(graph->lp,
880 coef->n_eq, coef->n_ineq);
881 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
882 coef, dim_map);
883 isl_space_free(dim);
884 edge->end = graph->lp->n_ineq;
886 return 0;
889 /* Add constraints to graph->lp that bound the dependence distance for the given
890 * dependence from a node i to itself.
891 * If s = 1, we add the constraint
893 * c_i_x (y - x) <= m_0 + m_n n
895 * or
897 * -c_i_x (y - x) + m_0 + m_n n >= 0
899 * for each (x,y) in R.
900 * If s = -1, we add the constraint
902 * -c_i_x (y - x) <= m_0 + m_n n
904 * or
906 * c_i_x (y - x) + m_0 + m_n n >= 0
908 * for each (x,y) in R.
909 * We obtain general constraints on coefficients (c_0, c_n, c_x)
910 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
911 * with each coefficient (except m_0) represented as a pair of non-negative
912 * coefficients.
914 * Actually, we do not construct constraints for the c_i_x themselves,
915 * but for the coefficients of c_i_x written as a linear combination
916 * of the columns in node->cmap.
918 static int add_intra_proximity_constraints(struct isl_sched_graph *graph,
919 struct isl_sched_edge *edge, int s)
921 unsigned total;
922 unsigned nparam;
923 isl_map *map = isl_map_copy(edge->map);
924 isl_ctx *ctx = isl_map_get_ctx(map);
925 isl_space *dim;
926 isl_dim_map *dim_map;
927 isl_basic_set *coef;
928 struct isl_sched_node *node = edge->src;
930 coef = intra_coefficients(graph, map);
932 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
934 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
935 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
937 nparam = isl_space_dim(node->dim, isl_dim_param);
938 total = isl_basic_set_total_dim(graph->lp);
939 dim_map = isl_dim_map_alloc(ctx, total);
940 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
941 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
942 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
943 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
944 isl_space_dim(dim, isl_dim_set), 1,
945 node->nvar, s);
946 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
947 isl_space_dim(dim, isl_dim_set), 1,
948 node->nvar, -s);
949 graph->lp = isl_basic_set_extend_constraints(graph->lp,
950 coef->n_eq, coef->n_ineq);
951 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
952 coef, dim_map);
953 isl_space_free(dim);
955 return 0;
958 /* Add constraints to graph->lp that bound the dependence distance for the given
959 * dependence from node i to node j.
960 * If s = 1, we add the constraint
962 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
963 * <= m_0 + m_n n
965 * or
967 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
968 * m_0 + m_n n >= 0
970 * for each (x,y) in R.
971 * If s = -1, we add the constraint
973 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
974 * <= m_0 + m_n n
976 * or
978 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
979 * m_0 + m_n n >= 0
981 * for each (x,y) in R.
982 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
983 * of valid constraints for R and then plug in
984 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
985 * -s*c_j_x+s*c_i_x)
986 * with each coefficient (except m_0, c_j_0 and c_i_0)
987 * represented as a pair of non-negative coefficients.
989 * Actually, we do not construct constraints for the c_*_x themselves,
990 * but for the coefficients of c_*_x written as a linear combination
991 * of the columns in node->cmap.
993 static int add_inter_proximity_constraints(struct isl_sched_graph *graph,
994 struct isl_sched_edge *edge, int s)
996 unsigned total;
997 unsigned nparam;
998 isl_map *map = isl_map_copy(edge->map);
999 isl_ctx *ctx = isl_map_get_ctx(map);
1000 isl_space *dim;
1001 isl_dim_map *dim_map;
1002 isl_basic_set *coef;
1003 struct isl_sched_node *src = edge->src;
1004 struct isl_sched_node *dst = edge->dst;
1006 coef = inter_coefficients(graph, map);
1008 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1010 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1011 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1012 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1013 isl_space_dim(dim, isl_dim_set) + src->nvar,
1014 isl_mat_copy(dst->cmap));
1016 nparam = isl_space_dim(src->dim, isl_dim_param);
1017 total = isl_basic_set_total_dim(graph->lp);
1018 dim_map = isl_dim_map_alloc(ctx, total);
1020 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1021 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1022 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1024 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, -s);
1025 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, s);
1026 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, -s);
1027 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1028 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1029 dst->nvar, s);
1030 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1031 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1032 dst->nvar, -s);
1034 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, s);
1035 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, -s);
1036 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, s);
1037 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1038 isl_space_dim(dim, isl_dim_set), 1,
1039 src->nvar, -s);
1040 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1041 isl_space_dim(dim, isl_dim_set), 1,
1042 src->nvar, s);
1044 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1045 coef->n_eq, coef->n_ineq);
1046 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1047 coef, dim_map);
1048 isl_space_free(dim);
1050 return 0;
1053 static int add_all_validity_constraints(struct isl_sched_graph *graph)
1055 int i;
1057 for (i = 0; i < graph->n_edge; ++i) {
1058 struct isl_sched_edge *edge= &graph->edge[i];
1059 if (!edge->validity)
1060 continue;
1061 if (edge->src != edge->dst)
1062 continue;
1063 if (add_intra_validity_constraints(graph, edge) < 0)
1064 return -1;
1067 for (i = 0; i < graph->n_edge; ++i) {
1068 struct isl_sched_edge *edge = &graph->edge[i];
1069 if (!edge->validity)
1070 continue;
1071 if (edge->src == edge->dst)
1072 continue;
1073 if (add_inter_validity_constraints(graph, edge) < 0)
1074 return -1;
1077 return 0;
1080 /* Add constraints to graph->lp that bound the dependence distance
1081 * for all dependence relations.
1082 * If a given proximity dependence is identical to a validity
1083 * dependence, then the dependence distance is already bounded
1084 * from below (by zero), so we only need to bound the distance
1085 * from above.
1086 * Otherwise, we need to bound the distance both from above and from below.
1088 static int add_all_proximity_constraints(struct isl_sched_graph *graph)
1090 int i;
1092 for (i = 0; i < graph->n_edge; ++i) {
1093 struct isl_sched_edge *edge= &graph->edge[i];
1094 if (!edge->proximity)
1095 continue;
1096 if (edge->src == edge->dst &&
1097 add_intra_proximity_constraints(graph, edge, 1) < 0)
1098 return -1;
1099 if (edge->src != edge->dst &&
1100 add_inter_proximity_constraints(graph, edge, 1) < 0)
1101 return -1;
1102 if (edge->validity)
1103 continue;
1104 if (edge->src == edge->dst &&
1105 add_intra_proximity_constraints(graph, edge, -1) < 0)
1106 return -1;
1107 if (edge->src != edge->dst &&
1108 add_inter_proximity_constraints(graph, edge, -1) < 0)
1109 return -1;
1112 return 0;
1115 /* Compute a basis for the rows in the linear part of the schedule
1116 * and extend this basis to a full basis. The remaining rows
1117 * can then be used to force linear independence from the rows
1118 * in the schedule.
1120 * In particular, given the schedule rows S, we compute
1122 * S = H Q
1124 * with H the Hermite normal form of S. That is, all but the
1125 * first rank columns of Q are zero and so each row in S is
1126 * a linear combination of the first rank rows of Q.
1127 * The matrix Q is then transposed because we will write the
1128 * coefficients of the next schedule row as a column vector s
1129 * and express this s as a linear combination s = Q c of the
1130 * computed basis.
1132 static int node_update_cmap(struct isl_sched_node *node)
1134 isl_mat *H, *Q;
1135 int n_row = isl_mat_rows(node->sched);
1137 H = isl_mat_sub_alloc(node->sched, 0, n_row,
1138 1 + node->nparam, node->nvar);
1140 H = isl_mat_left_hermite(H, 0, NULL, &Q);
1141 isl_mat_free(node->cmap);
1142 node->cmap = isl_mat_transpose(Q);
1143 node->rank = isl_mat_initial_non_zero_cols(H);
1144 isl_mat_free(H);
1146 if (!node->cmap || node->rank < 0)
1147 return -1;
1148 return 0;
1151 /* Count the number of equality and inequality constraints
1152 * that will be added for the given map.
1153 * If carry is set, then we are counting the number of (validity)
1154 * constraints that will be added in setup_carry_lp and we count
1155 * each edge exactly once. Otherwise, we count as follows
1156 * validity -> 1 (>= 0)
1157 * validity+proximity -> 2 (>= 0 and upper bound)
1158 * proximity -> 2 (lower and upper bound)
1160 static int count_map_constraints(struct isl_sched_graph *graph,
1161 struct isl_sched_edge *edge, __isl_take isl_map *map,
1162 int *n_eq, int *n_ineq, int carry)
1164 isl_basic_set *coef;
1165 int f = carry ? 1 : edge->proximity ? 2 : 1;
1167 if (carry && !edge->validity) {
1168 isl_map_free(map);
1169 return 0;
1172 if (edge->src == edge->dst)
1173 coef = intra_coefficients(graph, map);
1174 else
1175 coef = inter_coefficients(graph, map);
1176 if (!coef)
1177 return -1;
1178 *n_eq += f * coef->n_eq;
1179 *n_ineq += f * coef->n_ineq;
1180 isl_basic_set_free(coef);
1182 return 0;
1185 /* Count the number of equality and inequality constraints
1186 * that will be added to the main lp problem.
1187 * We count as follows
1188 * validity -> 1 (>= 0)
1189 * validity+proximity -> 2 (>= 0 and upper bound)
1190 * proximity -> 2 (lower and upper bound)
1192 static int count_constraints(struct isl_sched_graph *graph,
1193 int *n_eq, int *n_ineq)
1195 int i;
1197 *n_eq = *n_ineq = 0;
1198 for (i = 0; i < graph->n_edge; ++i) {
1199 struct isl_sched_edge *edge= &graph->edge[i];
1200 isl_map *map = isl_map_copy(edge->map);
1202 if (count_map_constraints(graph, edge, map,
1203 n_eq, n_ineq, 0) < 0)
1204 return -1;
1207 return 0;
1210 /* Add constraints that bound the values of the variable and parameter
1211 * coefficients of the schedule.
1213 * The maximal value of the coefficients is defined by the option
1214 * 'schedule_max_coefficient'.
1216 static int add_bound_coefficient_constraints(isl_ctx *ctx,
1217 struct isl_sched_graph *graph)
1219 int i, j, k;
1220 int max_coefficient;
1221 int total;
1223 max_coefficient = ctx->opt->schedule_max_coefficient;
1225 if (max_coefficient == -1)
1226 return 0;
1228 total = isl_basic_set_total_dim(graph->lp);
1230 for (i = 0; i < graph->n; ++i) {
1231 struct isl_sched_node *node = &graph->node[i];
1232 for (j = 0; j < 2 * node->nparam + 2 * node->nvar; ++j) {
1233 int dim;
1234 k = isl_basic_set_alloc_inequality(graph->lp);
1235 if (k < 0)
1236 return -1;
1237 dim = 1 + node->start + 1 + j;
1238 isl_seq_clr(graph->lp->ineq[k], 1 + total);
1239 isl_int_set_si(graph->lp->ineq[k][dim], -1);
1240 isl_int_set_si(graph->lp->ineq[k][0], max_coefficient);
1244 return 0;
1247 /* Construct an ILP problem for finding schedule coefficients
1248 * that result in non-negative, but small dependence distances
1249 * over all dependences.
1250 * In particular, the dependence distances over proximity edges
1251 * are bounded by m_0 + m_n n and we compute schedule coefficients
1252 * with small values (preferably zero) of m_n and m_0.
1254 * All variables of the ILP are non-negative. The actual coefficients
1255 * may be negative, so each coefficient is represented as the difference
1256 * of two non-negative variables. The negative part always appears
1257 * immediately before the positive part.
1258 * Other than that, the variables have the following order
1260 * - sum of positive and negative parts of m_n coefficients
1261 * - m_0
1262 * - sum of positive and negative parts of all c_n coefficients
1263 * (unconstrained when computing non-parametric schedules)
1264 * - sum of positive and negative parts of all c_x coefficients
1265 * - positive and negative parts of m_n coefficients
1266 * - for each node
1267 * - c_i_0
1268 * - positive and negative parts of c_i_n (if parametric)
1269 * - positive and negative parts of c_i_x
1271 * The c_i_x are not represented directly, but through the columns of
1272 * node->cmap. That is, the computed values are for variable t_i_x
1273 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
1275 * The constraints are those from the edges plus two or three equalities
1276 * to express the sums.
1278 * If force_zero is set, then we add equalities to ensure that
1279 * the sum of the m_n coefficients and m_0 are both zero.
1281 static int setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
1282 int force_zero)
1284 int i, j;
1285 int k;
1286 unsigned nparam;
1287 unsigned total;
1288 isl_space *dim;
1289 int parametric;
1290 int param_pos;
1291 int n_eq, n_ineq;
1292 int max_constant_term;
1293 int max_coefficient;
1295 max_constant_term = ctx->opt->schedule_max_constant_term;
1296 max_coefficient = ctx->opt->schedule_max_coefficient;
1298 parametric = ctx->opt->schedule_parametric;
1299 nparam = isl_space_dim(graph->node[0].dim, isl_dim_param);
1300 param_pos = 4;
1301 total = param_pos + 2 * nparam;
1302 for (i = 0; i < graph->n; ++i) {
1303 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
1304 if (node_update_cmap(node) < 0)
1305 return -1;
1306 node->start = total;
1307 total += 1 + 2 * (node->nparam + node->nvar);
1310 if (count_constraints(graph, &n_eq, &n_ineq) < 0)
1311 return -1;
1313 dim = isl_space_set_alloc(ctx, 0, total);
1314 isl_basic_set_free(graph->lp);
1315 n_eq += 2 + parametric + force_zero;
1316 if (max_constant_term != -1)
1317 n_ineq += graph->n;
1318 if (max_coefficient != -1)
1319 for (i = 0; i < graph->n; ++i)
1320 n_ineq += 2 * graph->node[i].nparam +
1321 2 * graph->node[i].nvar;
1323 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
1325 k = isl_basic_set_alloc_equality(graph->lp);
1326 if (k < 0)
1327 return -1;
1328 isl_seq_clr(graph->lp->eq[k], 1 + total);
1329 if (!force_zero)
1330 isl_int_set_si(graph->lp->eq[k][1], -1);
1331 for (i = 0; i < 2 * nparam; ++i)
1332 isl_int_set_si(graph->lp->eq[k][1 + param_pos + i], 1);
1334 if (force_zero) {
1335 k = isl_basic_set_alloc_equality(graph->lp);
1336 if (k < 0)
1337 return -1;
1338 isl_seq_clr(graph->lp->eq[k], 1 + total);
1339 isl_int_set_si(graph->lp->eq[k][2], -1);
1342 if (parametric) {
1343 k = isl_basic_set_alloc_equality(graph->lp);
1344 if (k < 0)
1345 return -1;
1346 isl_seq_clr(graph->lp->eq[k], 1 + total);
1347 isl_int_set_si(graph->lp->eq[k][3], -1);
1348 for (i = 0; i < graph->n; ++i) {
1349 int pos = 1 + graph->node[i].start + 1;
1351 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
1352 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
1356 k = isl_basic_set_alloc_equality(graph->lp);
1357 if (k < 0)
1358 return -1;
1359 isl_seq_clr(graph->lp->eq[k], 1 + total);
1360 isl_int_set_si(graph->lp->eq[k][4], -1);
1361 for (i = 0; i < graph->n; ++i) {
1362 struct isl_sched_node *node = &graph->node[i];
1363 int pos = 1 + node->start + 1 + 2 * node->nparam;
1365 for (j = 0; j < 2 * node->nvar; ++j)
1366 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
1369 if (max_constant_term != -1)
1370 for (i = 0; i < graph->n; ++i) {
1371 struct isl_sched_node *node = &graph->node[i];
1372 k = isl_basic_set_alloc_inequality(graph->lp);
1373 if (k < 0)
1374 return -1;
1375 isl_seq_clr(graph->lp->ineq[k], 1 + total);
1376 isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
1377 isl_int_set_si(graph->lp->ineq[k][0], max_constant_term);
1380 if (add_bound_coefficient_constraints(ctx, graph) < 0)
1381 return -1;
1382 if (add_all_validity_constraints(graph) < 0)
1383 return -1;
1384 if (add_all_proximity_constraints(graph) < 0)
1385 return -1;
1387 return 0;
1390 /* Analyze the conflicting constraint found by
1391 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
1392 * constraint of one of the edges between distinct nodes, living, moreover
1393 * in distinct SCCs, then record the source and sink SCC as this may
1394 * be a good place to cut between SCCs.
1396 static int check_conflict(int con, void *user)
1398 int i;
1399 struct isl_sched_graph *graph = user;
1401 if (graph->src_scc >= 0)
1402 return 0;
1404 con -= graph->lp->n_eq;
1406 if (con >= graph->lp->n_ineq)
1407 return 0;
1409 for (i = 0; i < graph->n_edge; ++i) {
1410 if (!graph->edge[i].validity)
1411 continue;
1412 if (graph->edge[i].src == graph->edge[i].dst)
1413 continue;
1414 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
1415 continue;
1416 if (graph->edge[i].start > con)
1417 continue;
1418 if (graph->edge[i].end <= con)
1419 continue;
1420 graph->src_scc = graph->edge[i].src->scc;
1421 graph->dst_scc = graph->edge[i].dst->scc;
1424 return 0;
1427 /* Check whether the next schedule row of the given node needs to be
1428 * non-trivial. Lower-dimensional domains may have some trivial rows,
1429 * but as soon as the number of remaining required non-trivial rows
1430 * is as large as the number or remaining rows to be computed,
1431 * all remaining rows need to be non-trivial.
1433 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
1435 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
1438 /* Solve the ILP problem constructed in setup_lp.
1439 * For each node such that all the remaining rows of its schedule
1440 * need to be non-trivial, we construct a non-triviality region.
1441 * This region imposes that the next row is independent of previous rows.
1442 * In particular the coefficients c_i_x are represented by t_i_x
1443 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
1444 * its first columns span the rows of the previously computed part
1445 * of the schedule. The non-triviality region enforces that at least
1446 * one of the remaining components of t_i_x is non-zero, i.e.,
1447 * that the new schedule row depends on at least one of the remaining
1448 * columns of Q.
1450 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
1452 int i;
1453 isl_vec *sol;
1454 isl_basic_set *lp;
1456 for (i = 0; i < graph->n; ++i) {
1457 struct isl_sched_node *node = &graph->node[i];
1458 int skip = node->rank;
1459 graph->region[i].pos = node->start + 1 + 2*(node->nparam+skip);
1460 if (needs_row(graph, node))
1461 graph->region[i].len = 2 * (node->nvar - skip);
1462 else
1463 graph->region[i].len = 0;
1465 lp = isl_basic_set_copy(graph->lp);
1466 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
1467 graph->region, &check_conflict, graph);
1468 return sol;
1471 /* Update the schedules of all nodes based on the given solution
1472 * of the LP problem.
1473 * The new row is added to the current band.
1474 * All possibly negative coefficients are encoded as a difference
1475 * of two non-negative variables, so we need to perform the subtraction
1476 * here. Moreover, if use_cmap is set, then the solution does
1477 * not refer to the actual coefficients c_i_x, but instead to variables
1478 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
1479 * In this case, we then also need to perform this multiplication
1480 * to obtain the values of c_i_x.
1482 * If check_zero is set, then the first two coordinates of sol are
1483 * assumed to correspond to the dependence distance. If these two
1484 * coordinates are zero, then the corresponding scheduling dimension
1485 * is marked as being zero distance.
1487 static int update_schedule(struct isl_sched_graph *graph,
1488 __isl_take isl_vec *sol, int use_cmap, int check_zero)
1490 int i, j;
1491 int zero = 0;
1492 isl_vec *csol = NULL;
1494 if (!sol)
1495 goto error;
1496 if (sol->size == 0)
1497 isl_die(sol->ctx, isl_error_internal,
1498 "no solution found", goto error);
1500 if (check_zero)
1501 zero = isl_int_is_zero(sol->el[1]) &&
1502 isl_int_is_zero(sol->el[2]);
1504 for (i = 0; i < graph->n; ++i) {
1505 struct isl_sched_node *node = &graph->node[i];
1506 int pos = node->start;
1507 int row = isl_mat_rows(node->sched);
1509 isl_vec_free(csol);
1510 csol = isl_vec_alloc(sol->ctx, node->nvar);
1511 if (!csol)
1512 goto error;
1514 isl_map_free(node->sched_map);
1515 node->sched_map = NULL;
1516 node->sched = isl_mat_add_rows(node->sched, 1);
1517 if (!node->sched)
1518 goto error;
1519 node->sched = isl_mat_set_element(node->sched, row, 0,
1520 sol->el[1 + pos]);
1521 for (j = 0; j < node->nparam + node->nvar; ++j)
1522 isl_int_sub(sol->el[1 + pos + 1 + 2 * j + 1],
1523 sol->el[1 + pos + 1 + 2 * j + 1],
1524 sol->el[1 + pos + 1 + 2 * j]);
1525 for (j = 0; j < node->nparam; ++j)
1526 node->sched = isl_mat_set_element(node->sched,
1527 row, 1 + j, sol->el[1+pos+1+2*j+1]);
1528 for (j = 0; j < node->nvar; ++j)
1529 isl_int_set(csol->el[j],
1530 sol->el[1+pos+1+2*(node->nparam+j)+1]);
1531 if (use_cmap)
1532 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
1533 csol);
1534 if (!csol)
1535 goto error;
1536 for (j = 0; j < node->nvar; ++j)
1537 node->sched = isl_mat_set_element(node->sched,
1538 row, 1 + node->nparam + j, csol->el[j]);
1539 node->band[graph->n_total_row] = graph->n_band;
1540 node->zero[graph->n_total_row] = zero;
1542 isl_vec_free(sol);
1543 isl_vec_free(csol);
1545 graph->n_row++;
1546 graph->n_total_row++;
1548 return 0;
1549 error:
1550 isl_vec_free(sol);
1551 isl_vec_free(csol);
1552 return -1;
1555 /* Convert node->sched into a multi_aff and return this multi_aff.
1557 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
1558 struct isl_sched_node *node)
1560 int i, j;
1561 isl_space *space;
1562 isl_local_space *ls;
1563 isl_aff *aff;
1564 isl_multi_aff *ma;
1565 int nrow, ncol;
1566 isl_int v;
1568 nrow = isl_mat_rows(node->sched);
1569 ncol = isl_mat_cols(node->sched) - 1;
1570 space = isl_space_from_domain(isl_space_copy(node->dim));
1571 space = isl_space_add_dims(space, isl_dim_out, nrow);
1572 ma = isl_multi_aff_zero(space);
1573 ls = isl_local_space_from_space(isl_space_copy(node->dim));
1575 isl_int_init(v);
1577 for (i = 0; i < nrow; ++i) {
1578 aff = isl_aff_zero_on_domain(isl_local_space_copy(ls));
1579 isl_mat_get_element(node->sched, i, 0, &v);
1580 aff = isl_aff_set_constant(aff, v);
1581 for (j = 0; j < node->nparam; ++j) {
1582 isl_mat_get_element(node->sched, i, 1 + j, &v);
1583 aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
1585 for (j = 0; j < node->nvar; ++j) {
1586 isl_mat_get_element(node->sched,
1587 i, 1 + node->nparam + j, &v);
1588 aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
1590 ma = isl_multi_aff_set_aff(ma, i, aff);
1593 isl_int_clear(v);
1595 isl_local_space_free(ls);
1597 return ma;
1600 /* Convert node->sched into a map and return this map.
1602 * The result is cached in node->sched_map, which needs to be released
1603 * whenever node->sched is updated.
1605 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
1607 if (!node->sched_map) {
1608 isl_multi_aff *ma;
1610 ma = node_extract_schedule_multi_aff(node);
1611 node->sched_map = isl_map_from_multi_aff(ma);
1614 return isl_map_copy(node->sched_map);
1617 /* Update the given dependence relation based on the current schedule.
1618 * That is, intersect the dependence relation with a map expressing
1619 * that source and sink are executed within the same iteration of
1620 * the current schedule.
1621 * This is not the most efficient way, but this shouldn't be a critical
1622 * operation.
1624 static __isl_give isl_map *specialize(__isl_take isl_map *map,
1625 struct isl_sched_node *src, struct isl_sched_node *dst)
1627 isl_map *src_sched, *dst_sched, *id;
1629 src_sched = node_extract_schedule(src);
1630 dst_sched = node_extract_schedule(dst);
1631 id = isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
1632 return isl_map_intersect(map, id);
1635 /* Update the dependence relations of all edges based on the current schedule.
1636 * If a dependence is carried completely by the current schedule, then
1637 * it is removed from the edge_tables. It is kept in the list of edges
1638 * as otherwise all edge_tables would have to be recomputed.
1640 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
1642 int i;
1644 for (i = graph->n_edge - 1; i >= 0; --i) {
1645 struct isl_sched_edge *edge = &graph->edge[i];
1646 edge->map = specialize(edge->map, edge->src, edge->dst);
1647 if (!edge->map)
1648 return -1;
1650 if (isl_map_plain_is_empty(edge->map))
1651 graph_remove_edge(graph, edge);
1654 return 0;
1657 static void next_band(struct isl_sched_graph *graph)
1659 graph->band_start = graph->n_total_row;
1660 graph->n_band++;
1663 /* Topologically sort statements mapped to the same schedule iteration
1664 * and add a row to the schedule corresponding to this order.
1666 static int sort_statements(isl_ctx *ctx, struct isl_sched_graph *graph)
1668 int i, j;
1670 if (graph->n <= 1)
1671 return 0;
1673 if (update_edges(ctx, graph) < 0)
1674 return -1;
1676 if (graph->n_edge == 0)
1677 return 0;
1679 if (detect_sccs(ctx, graph) < 0)
1680 return -1;
1682 for (i = 0; i < graph->n; ++i) {
1683 struct isl_sched_node *node = &graph->node[i];
1684 int row = isl_mat_rows(node->sched);
1685 int cols = isl_mat_cols(node->sched);
1687 isl_map_free(node->sched_map);
1688 node->sched_map = NULL;
1689 node->sched = isl_mat_add_rows(node->sched, 1);
1690 if (!node->sched)
1691 return -1;
1692 node->sched = isl_mat_set_element_si(node->sched, row, 0,
1693 node->scc);
1694 for (j = 1; j < cols; ++j)
1695 node->sched = isl_mat_set_element_si(node->sched,
1696 row, j, 0);
1697 node->band[graph->n_total_row] = graph->n_band;
1700 graph->n_total_row++;
1701 next_band(graph);
1703 return 0;
1706 /* Construct an isl_schedule based on the computed schedule stored
1707 * in graph and with parameters specified by dim.
1709 static __isl_give isl_schedule *extract_schedule(struct isl_sched_graph *graph,
1710 __isl_take isl_space *dim)
1712 int i;
1713 isl_ctx *ctx;
1714 isl_schedule *sched = NULL;
1716 if (!dim)
1717 return NULL;
1719 ctx = isl_space_get_ctx(dim);
1720 sched = isl_calloc(ctx, struct isl_schedule,
1721 sizeof(struct isl_schedule) +
1722 (graph->n - 1) * sizeof(struct isl_schedule_node));
1723 if (!sched)
1724 goto error;
1726 sched->ref = 1;
1727 sched->n = graph->n;
1728 sched->n_band = graph->n_band;
1729 sched->n_total_row = graph->n_total_row;
1731 for (i = 0; i < sched->n; ++i) {
1732 int r, b;
1733 int *band_end, *band_id, *zero;
1735 sched->node[i].sched =
1736 node_extract_schedule_multi_aff(&graph->node[i]);
1737 if (!sched->node[i].sched)
1738 goto error;
1740 sched->node[i].n_band = graph->n_band;
1741 if (graph->n_band == 0)
1742 continue;
1744 band_end = isl_alloc_array(ctx, int, graph->n_band);
1745 band_id = isl_alloc_array(ctx, int, graph->n_band);
1746 zero = isl_alloc_array(ctx, int, graph->n_total_row);
1747 sched->node[i].band_end = band_end;
1748 sched->node[i].band_id = band_id;
1749 sched->node[i].zero = zero;
1750 if (!band_end || !band_id || !zero)
1751 goto error;
1753 for (r = 0; r < graph->n_total_row; ++r)
1754 zero[r] = graph->node[i].zero[r];
1755 for (r = b = 0; r < graph->n_total_row; ++r) {
1756 if (graph->node[i].band[r] == b)
1757 continue;
1758 band_end[b++] = r;
1759 if (graph->node[i].band[r] == -1)
1760 break;
1762 if (r == graph->n_total_row)
1763 band_end[b++] = r;
1764 sched->node[i].n_band = b;
1765 for (--b; b >= 0; --b)
1766 band_id[b] = graph->node[i].band_id[b];
1769 sched->dim = dim;
1771 return sched;
1772 error:
1773 isl_space_free(dim);
1774 isl_schedule_free(sched);
1775 return NULL;
1778 /* Copy nodes that satisfy node_pred from the src dependence graph
1779 * to the dst dependence graph.
1781 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
1782 int (*node_pred)(struct isl_sched_node *node, int data), int data)
1784 int i;
1786 dst->n = 0;
1787 for (i = 0; i < src->n; ++i) {
1788 if (!node_pred(&src->node[i], data))
1789 continue;
1790 dst->node[dst->n].dim = isl_space_copy(src->node[i].dim);
1791 dst->node[dst->n].nvar = src->node[i].nvar;
1792 dst->node[dst->n].nparam = src->node[i].nparam;
1793 dst->node[dst->n].sched = isl_mat_copy(src->node[i].sched);
1794 dst->node[dst->n].sched_map =
1795 isl_map_copy(src->node[i].sched_map);
1796 dst->node[dst->n].band = src->node[i].band;
1797 dst->node[dst->n].band_id = src->node[i].band_id;
1798 dst->node[dst->n].zero = src->node[i].zero;
1799 dst->n++;
1802 return 0;
1805 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
1806 * to the dst dependence graph.
1807 * If the source or destination node of the edge is not in the destination
1808 * graph, then it must be a backward proximity edge and it should simply
1809 * be ignored.
1811 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
1812 struct isl_sched_graph *src,
1813 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
1815 int i;
1816 enum isl_edge_type t;
1818 dst->n_edge = 0;
1819 for (i = 0; i < src->n_edge; ++i) {
1820 struct isl_sched_edge *edge = &src->edge[i];
1821 isl_map *map;
1822 struct isl_sched_node *dst_src, *dst_dst;
1824 if (!edge_pred(edge, data))
1825 continue;
1827 if (isl_map_plain_is_empty(edge->map))
1828 continue;
1830 dst_src = graph_find_node(ctx, dst, edge->src->dim);
1831 dst_dst = graph_find_node(ctx, dst, edge->dst->dim);
1832 if (!dst_src || !dst_dst) {
1833 if (edge->validity)
1834 isl_die(ctx, isl_error_internal,
1835 "backward validity edge", return -1);
1836 continue;
1839 map = isl_map_copy(edge->map);
1841 dst->edge[dst->n_edge].src = dst_src;
1842 dst->edge[dst->n_edge].dst = dst_dst;
1843 dst->edge[dst->n_edge].map = map;
1844 dst->edge[dst->n_edge].validity = edge->validity;
1845 dst->edge[dst->n_edge].proximity = edge->proximity;
1846 dst->n_edge++;
1848 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
1849 if (edge !=
1850 graph_find_edge(src, t, edge->src, edge->dst))
1851 continue;
1852 if (graph_edge_table_add(ctx, dst, t,
1853 &dst->edge[dst->n_edge - 1]) < 0)
1854 return -1;
1858 return 0;
1861 /* Given a "src" dependence graph that contains the nodes from "dst"
1862 * that satisfy node_pred, copy the schedule computed in "src"
1863 * for those nodes back to "dst".
1865 static int copy_schedule(struct isl_sched_graph *dst,
1866 struct isl_sched_graph *src,
1867 int (*node_pred)(struct isl_sched_node *node, int data), int data)
1869 int i;
1871 src->n = 0;
1872 for (i = 0; i < dst->n; ++i) {
1873 if (!node_pred(&dst->node[i], data))
1874 continue;
1875 isl_mat_free(dst->node[i].sched);
1876 isl_map_free(dst->node[i].sched_map);
1877 dst->node[i].sched = isl_mat_copy(src->node[src->n].sched);
1878 dst->node[i].sched_map =
1879 isl_map_copy(src->node[src->n].sched_map);
1880 src->n++;
1883 dst->n_total_row = src->n_total_row;
1884 dst->n_band = src->n_band;
1886 return 0;
1889 /* Compute the maximal number of variables over all nodes.
1890 * This is the maximal number of linearly independent schedule
1891 * rows that we need to compute.
1892 * Just in case we end up in a part of the dependence graph
1893 * with only lower-dimensional domains, we make sure we will
1894 * compute the required amount of extra linearly independent rows.
1896 static int compute_maxvar(struct isl_sched_graph *graph)
1898 int i;
1900 graph->maxvar = 0;
1901 for (i = 0; i < graph->n; ++i) {
1902 struct isl_sched_node *node = &graph->node[i];
1903 int nvar;
1905 if (node_update_cmap(node) < 0)
1906 return -1;
1907 nvar = node->nvar + graph->n_row - node->rank;
1908 if (nvar > graph->maxvar)
1909 graph->maxvar = nvar;
1912 return 0;
1915 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph);
1916 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph);
1918 /* Compute a schedule for a subgraph of "graph". In particular, for
1919 * the graph composed of nodes that satisfy node_pred and edges that
1920 * that satisfy edge_pred. The caller should precompute the number
1921 * of nodes and edges that satisfy these predicates and pass them along
1922 * as "n" and "n_edge".
1923 * If the subgraph is known to consist of a single component, then wcc should
1924 * be set and then we call compute_schedule_wcc on the constructed subgraph.
1925 * Otherwise, we call compute_schedule, which will check whether the subgraph
1926 * is connected.
1928 static int compute_sub_schedule(isl_ctx *ctx,
1929 struct isl_sched_graph *graph, int n, int n_edge,
1930 int (*node_pred)(struct isl_sched_node *node, int data),
1931 int (*edge_pred)(struct isl_sched_edge *edge, int data),
1932 int data, int wcc)
1934 struct isl_sched_graph split = { 0 };
1935 int t;
1937 if (graph_alloc(ctx, &split, n, n_edge) < 0)
1938 goto error;
1939 if (copy_nodes(&split, graph, node_pred, data) < 0)
1940 goto error;
1941 if (graph_init_table(ctx, &split) < 0)
1942 goto error;
1943 for (t = 0; t <= isl_edge_last; ++t)
1944 split.max_edge[t] = graph->max_edge[t];
1945 if (graph_init_edge_tables(ctx, &split) < 0)
1946 goto error;
1947 if (copy_edges(ctx, &split, graph, edge_pred, data) < 0)
1948 goto error;
1949 split.n_row = graph->n_row;
1950 split.n_total_row = graph->n_total_row;
1951 split.n_band = graph->n_band;
1952 split.band_start = graph->band_start;
1954 if (wcc && compute_schedule_wcc(ctx, &split) < 0)
1955 goto error;
1956 if (!wcc && compute_schedule(ctx, &split) < 0)
1957 goto error;
1959 copy_schedule(graph, &split, node_pred, data);
1961 graph_free(ctx, &split);
1962 return 0;
1963 error:
1964 graph_free(ctx, &split);
1965 return -1;
1968 static int node_scc_exactly(struct isl_sched_node *node, int scc)
1970 return node->scc == scc;
1973 static int node_scc_at_most(struct isl_sched_node *node, int scc)
1975 return node->scc <= scc;
1978 static int node_scc_at_least(struct isl_sched_node *node, int scc)
1980 return node->scc >= scc;
1983 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
1985 return edge->src->scc == scc && edge->dst->scc == scc;
1988 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
1990 return edge->dst->scc <= scc;
1993 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
1995 return edge->src->scc >= scc;
1998 /* Pad the schedules of all nodes with zero rows such that in the end
1999 * they all have graph->n_total_row rows.
2000 * The extra rows don't belong to any band, so they get assigned band number -1.
2002 static int pad_schedule(struct isl_sched_graph *graph)
2004 int i, j;
2006 for (i = 0; i < graph->n; ++i) {
2007 struct isl_sched_node *node = &graph->node[i];
2008 int row = isl_mat_rows(node->sched);
2009 if (graph->n_total_row > row) {
2010 isl_map_free(node->sched_map);
2011 node->sched_map = NULL;
2013 node->sched = isl_mat_add_zero_rows(node->sched,
2014 graph->n_total_row - row);
2015 if (!node->sched)
2016 return -1;
2017 for (j = row; j < graph->n_total_row; ++j)
2018 node->band[j] = -1;
2021 return 0;
2024 /* Split the current graph into two parts and compute a schedule for each
2025 * part individually. In particular, one part consists of all SCCs up
2026 * to and including graph->src_scc, while the other part contains the other
2027 * SCCS.
2029 * The split is enforced in the schedule by constant rows with two different
2030 * values (0 and 1). These constant rows replace the previously computed rows
2031 * in the current band.
2032 * It would be possible to reuse them as the first rows in the next
2033 * band, but recomputing them may result in better rows as we are looking
2034 * at a smaller part of the dependence graph.
2035 * compute_split_schedule is only called when no zero-distance schedule row
2036 * could be found on the entire graph, so we wark the splitting row as
2037 * non zero-distance.
2039 * The band_id of the second group is set to n, where n is the number
2040 * of nodes in the first group. This ensures that the band_ids over
2041 * the two groups remain disjoint, even if either or both of the two
2042 * groups contain independent components.
2044 static int compute_split_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
2046 int i, j, n, e1, e2;
2047 int n_total_row, orig_total_row;
2048 int n_band, orig_band;
2049 int drop;
2051 drop = graph->n_total_row - graph->band_start;
2052 graph->n_total_row -= drop;
2053 graph->n_row -= drop;
2055 n = 0;
2056 for (i = 0; i < graph->n; ++i) {
2057 struct isl_sched_node *node = &graph->node[i];
2058 int row = isl_mat_rows(node->sched) - drop;
2059 int cols = isl_mat_cols(node->sched);
2060 int before = node->scc <= graph->src_scc;
2062 if (before)
2063 n++;
2065 isl_map_free(node->sched_map);
2066 node->sched_map = NULL;
2067 node->sched = isl_mat_drop_rows(node->sched,
2068 graph->band_start, drop);
2069 node->sched = isl_mat_add_rows(node->sched, 1);
2070 if (!node->sched)
2071 return -1;
2072 node->sched = isl_mat_set_element_si(node->sched, row, 0,
2073 !before);
2074 for (j = 1; j < cols; ++j)
2075 node->sched = isl_mat_set_element_si(node->sched,
2076 row, j, 0);
2077 node->band[graph->n_total_row] = graph->n_band;
2078 node->zero[graph->n_total_row] = 0;
2081 e1 = e2 = 0;
2082 for (i = 0; i < graph->n_edge; ++i) {
2083 if (graph->edge[i].dst->scc <= graph->src_scc)
2084 e1++;
2085 if (graph->edge[i].src->scc > graph->src_scc)
2086 e2++;
2089 graph->n_total_row++;
2090 next_band(graph);
2092 for (i = 0; i < graph->n; ++i) {
2093 struct isl_sched_node *node = &graph->node[i];
2094 if (node->scc > graph->src_scc)
2095 node->band_id[graph->n_band] = n;
2098 orig_total_row = graph->n_total_row;
2099 orig_band = graph->n_band;
2100 if (compute_sub_schedule(ctx, graph, n, e1,
2101 &node_scc_at_most, &edge_dst_scc_at_most,
2102 graph->src_scc, 0) < 0)
2103 return -1;
2104 n_total_row = graph->n_total_row;
2105 graph->n_total_row = orig_total_row;
2106 n_band = graph->n_band;
2107 graph->n_band = orig_band;
2108 if (compute_sub_schedule(ctx, graph, graph->n - n, e2,
2109 &node_scc_at_least, &edge_src_scc_at_least,
2110 graph->src_scc + 1, 0) < 0)
2111 return -1;
2112 if (n_total_row > graph->n_total_row)
2113 graph->n_total_row = n_total_row;
2114 if (n_band > graph->n_band)
2115 graph->n_band = n_band;
2117 return pad_schedule(graph);
2120 /* Compute the next band of the schedule after updating the dependence
2121 * relations based on the the current schedule.
2123 static int compute_next_band(isl_ctx *ctx, struct isl_sched_graph *graph)
2125 if (update_edges(ctx, graph) < 0)
2126 return -1;
2127 next_band(graph);
2129 return compute_schedule(ctx, graph);
2132 /* Add constraints to graph->lp that force the dependence "map" (which
2133 * is part of the dependence relation of "edge")
2134 * to be respected and attempt to carry it, where the edge is one from
2135 * a node j to itself. "pos" is the sequence number of the given map.
2136 * That is, add constraints that enforce
2138 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
2139 * = c_j_x (y - x) >= e_i
2141 * for each (x,y) in R.
2142 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2143 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
2144 * with each coefficient in c_j_x represented as a pair of non-negative
2145 * coefficients.
2147 static int add_intra_constraints(struct isl_sched_graph *graph,
2148 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
2150 unsigned total;
2151 isl_ctx *ctx = isl_map_get_ctx(map);
2152 isl_space *dim;
2153 isl_dim_map *dim_map;
2154 isl_basic_set *coef;
2155 struct isl_sched_node *node = edge->src;
2157 coef = intra_coefficients(graph, map);
2159 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
2161 total = isl_basic_set_total_dim(graph->lp);
2162 dim_map = isl_dim_map_alloc(ctx, total);
2163 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
2164 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
2165 isl_space_dim(dim, isl_dim_set), 1,
2166 node->nvar, -1);
2167 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
2168 isl_space_dim(dim, isl_dim_set), 1,
2169 node->nvar, 1);
2170 graph->lp = isl_basic_set_extend_constraints(graph->lp,
2171 coef->n_eq, coef->n_ineq);
2172 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
2173 coef, dim_map);
2174 isl_space_free(dim);
2176 return 0;
2179 /* Add constraints to graph->lp that force the dependence "map" (which
2180 * is part of the dependence relation of "edge")
2181 * to be respected and attempt to carry it, where the edge is one from
2182 * node j to node k. "pos" is the sequence number of the given map.
2183 * That is, add constraints that enforce
2185 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
2187 * for each (x,y) in R.
2188 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2189 * of valid constraints for R and then plug in
2190 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
2191 * with each coefficient (except e_i, c_k_0 and c_j_0)
2192 * represented as a pair of non-negative coefficients.
2194 static int add_inter_constraints(struct isl_sched_graph *graph,
2195 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
2197 unsigned total;
2198 isl_ctx *ctx = isl_map_get_ctx(map);
2199 isl_space *dim;
2200 isl_dim_map *dim_map;
2201 isl_basic_set *coef;
2202 struct isl_sched_node *src = edge->src;
2203 struct isl_sched_node *dst = edge->dst;
2205 coef = inter_coefficients(graph, map);
2207 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
2209 total = isl_basic_set_total_dim(graph->lp);
2210 dim_map = isl_dim_map_alloc(ctx, total);
2212 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
2214 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
2215 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
2216 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
2217 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
2218 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
2219 dst->nvar, -1);
2220 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
2221 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
2222 dst->nvar, 1);
2224 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
2225 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
2226 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
2227 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
2228 isl_space_dim(dim, isl_dim_set), 1,
2229 src->nvar, 1);
2230 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
2231 isl_space_dim(dim, isl_dim_set), 1,
2232 src->nvar, -1);
2234 graph->lp = isl_basic_set_extend_constraints(graph->lp,
2235 coef->n_eq, coef->n_ineq);
2236 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
2237 coef, dim_map);
2238 isl_space_free(dim);
2240 return 0;
2243 /* Add constraints to graph->lp that force all validity dependences
2244 * to be respected and attempt to carry them.
2246 static int add_all_constraints(struct isl_sched_graph *graph)
2248 int i, j;
2249 int pos;
2251 pos = 0;
2252 for (i = 0; i < graph->n_edge; ++i) {
2253 struct isl_sched_edge *edge= &graph->edge[i];
2255 if (!edge->validity)
2256 continue;
2258 for (j = 0; j < edge->map->n; ++j) {
2259 isl_basic_map *bmap;
2260 isl_map *map;
2262 bmap = isl_basic_map_copy(edge->map->p[j]);
2263 map = isl_map_from_basic_map(bmap);
2265 if (edge->src == edge->dst &&
2266 add_intra_constraints(graph, edge, map, pos) < 0)
2267 return -1;
2268 if (edge->src != edge->dst &&
2269 add_inter_constraints(graph, edge, map, pos) < 0)
2270 return -1;
2271 ++pos;
2275 return 0;
2278 /* Count the number of equality and inequality constraints
2279 * that will be added to the carry_lp problem.
2280 * We count each edge exactly once.
2282 static int count_all_constraints(struct isl_sched_graph *graph,
2283 int *n_eq, int *n_ineq)
2285 int i, j;
2287 *n_eq = *n_ineq = 0;
2288 for (i = 0; i < graph->n_edge; ++i) {
2289 struct isl_sched_edge *edge= &graph->edge[i];
2290 for (j = 0; j < edge->map->n; ++j) {
2291 isl_basic_map *bmap;
2292 isl_map *map;
2294 bmap = isl_basic_map_copy(edge->map->p[j]);
2295 map = isl_map_from_basic_map(bmap);
2297 if (count_map_constraints(graph, edge, map,
2298 n_eq, n_ineq, 1) < 0)
2299 return -1;
2303 return 0;
2306 /* Construct an LP problem for finding schedule coefficients
2307 * such that the schedule carries as many dependences as possible.
2308 * In particular, for each dependence i, we bound the dependence distance
2309 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
2310 * of all e_i's. Dependence with e_i = 0 in the solution are simply
2311 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
2312 * Note that if the dependence relation is a union of basic maps,
2313 * then we have to consider each basic map individually as it may only
2314 * be possible to carry the dependences expressed by some of those
2315 * basic maps and not all off them.
2316 * Below, we consider each of those basic maps as a separate "edge".
2318 * All variables of the LP are non-negative. The actual coefficients
2319 * may be negative, so each coefficient is represented as the difference
2320 * of two non-negative variables. The negative part always appears
2321 * immediately before the positive part.
2322 * Other than that, the variables have the following order
2324 * - sum of (1 - e_i) over all edges
2325 * - sum of positive and negative parts of all c_n coefficients
2326 * (unconstrained when computing non-parametric schedules)
2327 * - sum of positive and negative parts of all c_x coefficients
2328 * - for each edge
2329 * - e_i
2330 * - for each node
2331 * - c_i_0
2332 * - positive and negative parts of c_i_n (if parametric)
2333 * - positive and negative parts of c_i_x
2335 * The constraints are those from the (validity) edges plus three equalities
2336 * to express the sums and n_edge inequalities to express e_i <= 1.
2338 static int setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
2340 int i, j;
2341 int k;
2342 isl_space *dim;
2343 unsigned total;
2344 int n_eq, n_ineq;
2345 int n_edge;
2347 n_edge = 0;
2348 for (i = 0; i < graph->n_edge; ++i)
2349 n_edge += graph->edge[i].map->n;
2351 total = 3 + n_edge;
2352 for (i = 0; i < graph->n; ++i) {
2353 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2354 node->start = total;
2355 total += 1 + 2 * (node->nparam + node->nvar);
2358 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
2359 return -1;
2361 dim = isl_space_set_alloc(ctx, 0, total);
2362 isl_basic_set_free(graph->lp);
2363 n_eq += 3;
2364 n_ineq += n_edge;
2365 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
2366 graph->lp = isl_basic_set_set_rational(graph->lp);
2368 k = isl_basic_set_alloc_equality(graph->lp);
2369 if (k < 0)
2370 return -1;
2371 isl_seq_clr(graph->lp->eq[k], 1 + total);
2372 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
2373 isl_int_set_si(graph->lp->eq[k][1], 1);
2374 for (i = 0; i < n_edge; ++i)
2375 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
2377 k = isl_basic_set_alloc_equality(graph->lp);
2378 if (k < 0)
2379 return -1;
2380 isl_seq_clr(graph->lp->eq[k], 1 + total);
2381 isl_int_set_si(graph->lp->eq[k][2], -1);
2382 for (i = 0; i < graph->n; ++i) {
2383 int pos = 1 + graph->node[i].start + 1;
2385 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
2386 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2389 k = isl_basic_set_alloc_equality(graph->lp);
2390 if (k < 0)
2391 return -1;
2392 isl_seq_clr(graph->lp->eq[k], 1 + total);
2393 isl_int_set_si(graph->lp->eq[k][3], -1);
2394 for (i = 0; i < graph->n; ++i) {
2395 struct isl_sched_node *node = &graph->node[i];
2396 int pos = 1 + node->start + 1 + 2 * node->nparam;
2398 for (j = 0; j < 2 * node->nvar; ++j)
2399 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2402 for (i = 0; i < n_edge; ++i) {
2403 k = isl_basic_set_alloc_inequality(graph->lp);
2404 if (k < 0)
2405 return -1;
2406 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2407 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
2408 isl_int_set_si(graph->lp->ineq[k][0], 1);
2411 if (add_all_constraints(graph) < 0)
2412 return -1;
2414 return 0;
2417 /* If the schedule_split_scaled option is set and if the linear
2418 * parts of the scheduling rows for all nodes in the graphs have
2419 * non-trivial common divisor, then split off the constant term
2420 * from the linear part.
2421 * The constant term is then placed in a separate band and
2422 * the linear part is reduced.
2424 static int split_scaled(isl_ctx *ctx, struct isl_sched_graph *graph)
2426 int i;
2427 int row;
2428 isl_int gcd, gcd_i;
2430 if (!ctx->opt->schedule_split_scaled)
2431 return 0;
2432 if (graph->n <= 1)
2433 return 0;
2435 isl_int_init(gcd);
2436 isl_int_init(gcd_i);
2438 isl_int_set_si(gcd, 0);
2440 row = isl_mat_rows(graph->node[0].sched) - 1;
2442 for (i = 0; i < graph->n; ++i) {
2443 struct isl_sched_node *node = &graph->node[i];
2444 int cols = isl_mat_cols(node->sched);
2446 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
2447 isl_int_gcd(gcd, gcd, gcd_i);
2450 isl_int_clear(gcd_i);
2452 if (isl_int_cmp_si(gcd, 1) <= 0) {
2453 isl_int_clear(gcd);
2454 return 0;
2457 next_band(graph);
2459 for (i = 0; i < graph->n; ++i) {
2460 struct isl_sched_node *node = &graph->node[i];
2462 isl_map_free(node->sched_map);
2463 node->sched_map = NULL;
2464 node->sched = isl_mat_add_zero_rows(node->sched, 1);
2465 if (!node->sched)
2466 goto error;
2467 isl_int_fdiv_r(node->sched->row[row + 1][0],
2468 node->sched->row[row][0], gcd);
2469 isl_int_fdiv_q(node->sched->row[row][0],
2470 node->sched->row[row][0], gcd);
2471 isl_int_mul(node->sched->row[row][0],
2472 node->sched->row[row][0], gcd);
2473 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
2474 if (!node->sched)
2475 goto error;
2476 node->band[graph->n_total_row] = graph->n_band;
2479 graph->n_total_row++;
2481 isl_int_clear(gcd);
2482 return 0;
2483 error:
2484 isl_int_clear(gcd);
2485 return -1;
2488 /* Construct a schedule row for each node such that as many dependences
2489 * as possible are carried and then continue with the next band.
2491 static int carry_dependences(isl_ctx *ctx, struct isl_sched_graph *graph)
2493 int i;
2494 int n_edge;
2495 isl_vec *sol;
2496 isl_basic_set *lp;
2498 n_edge = 0;
2499 for (i = 0; i < graph->n_edge; ++i)
2500 n_edge += graph->edge[i].map->n;
2502 if (setup_carry_lp(ctx, graph) < 0)
2503 return -1;
2505 lp = isl_basic_set_copy(graph->lp);
2506 sol = isl_tab_basic_set_non_neg_lexmin(lp);
2507 if (!sol)
2508 return -1;
2510 if (sol->size == 0) {
2511 isl_vec_free(sol);
2512 isl_die(ctx, isl_error_internal,
2513 "error in schedule construction", return -1);
2516 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
2517 isl_vec_free(sol);
2518 isl_die(ctx, isl_error_unknown,
2519 "unable to carry dependences", return -1);
2522 if (update_schedule(graph, sol, 0, 0) < 0)
2523 return -1;
2525 if (split_scaled(ctx, graph) < 0)
2526 return -1;
2528 return compute_next_band(ctx, graph);
2531 /* Are there any (non-empty) validity edges in the graph?
2533 static int has_validity_edges(struct isl_sched_graph *graph)
2535 int i;
2537 for (i = 0; i < graph->n_edge; ++i) {
2538 int empty;
2540 empty = isl_map_plain_is_empty(graph->edge[i].map);
2541 if (empty < 0)
2542 return -1;
2543 if (empty)
2544 continue;
2545 if (graph->edge[i].validity)
2546 return 1;
2549 return 0;
2552 /* Should we apply a Feautrier step?
2553 * That is, did the user request the Feautrier algorithm and are
2554 * there any validity dependences (left)?
2556 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
2558 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
2559 return 0;
2561 return has_validity_edges(graph);
2564 /* Compute a schedule for a connected dependence graph using Feautrier's
2565 * multi-dimensional scheduling algorithm.
2566 * The original algorithm is described in [1].
2567 * The main idea is to minimize the number of scheduling dimensions, by
2568 * trying to satisfy as many dependences as possible per scheduling dimension.
2570 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
2571 * Problem, Part II: Multi-Dimensional Time.
2572 * In Intl. Journal of Parallel Programming, 1992.
2574 static int compute_schedule_wcc_feautrier(isl_ctx *ctx,
2575 struct isl_sched_graph *graph)
2577 return carry_dependences(ctx, graph);
2580 /* Compute a schedule for a connected dependence graph.
2581 * We try to find a sequence of as many schedule rows as possible that result
2582 * in non-negative dependence distances (independent of the previous rows
2583 * in the sequence, i.e., such that the sequence is tilable).
2584 * If we can't find any more rows we either
2585 * - split between SCCs and start over (assuming we found an interesting
2586 * pair of SCCs between which to split)
2587 * - continue with the next band (assuming the current band has at least
2588 * one row)
2589 * - try to carry as many dependences as possible and continue with the next
2590 * band
2592 * If Feautrier's algorithm is selected, we first recursively try to satisfy
2593 * as many validity dependences as possible. When all validity dependences
2594 * are satisfied we extend the schedule to a full-dimensional schedule.
2596 * If we manage to complete the schedule, we finish off by topologically
2597 * sorting the statements based on the remaining dependences.
2599 * If ctx->opt->schedule_outer_zero_distance is set, then we force the
2600 * outermost dimension in the current band to be zero distance. If this
2601 * turns out to be impossible, we fall back on the general scheme above
2602 * and try to carry as many dependences as possible.
2604 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph)
2606 int force_zero = 0;
2608 if (detect_sccs(ctx, graph) < 0)
2609 return -1;
2610 if (sort_sccs(graph) < 0)
2611 return -1;
2613 if (compute_maxvar(graph) < 0)
2614 return -1;
2616 if (need_feautrier_step(ctx, graph))
2617 return compute_schedule_wcc_feautrier(ctx, graph);
2619 if (ctx->opt->schedule_outer_zero_distance)
2620 force_zero = 1;
2622 while (graph->n_row < graph->maxvar) {
2623 isl_vec *sol;
2625 graph->src_scc = -1;
2626 graph->dst_scc = -1;
2628 if (setup_lp(ctx, graph, force_zero) < 0)
2629 return -1;
2630 sol = solve_lp(graph);
2631 if (!sol)
2632 return -1;
2633 if (sol->size == 0) {
2634 isl_vec_free(sol);
2635 if (!ctx->opt->schedule_maximize_band_depth &&
2636 graph->n_total_row > graph->band_start)
2637 return compute_next_band(ctx, graph);
2638 if (graph->src_scc >= 0)
2639 return compute_split_schedule(ctx, graph);
2640 if (graph->n_total_row > graph->band_start)
2641 return compute_next_band(ctx, graph);
2642 return carry_dependences(ctx, graph);
2644 if (update_schedule(graph, sol, 1, 1) < 0)
2645 return -1;
2646 force_zero = 0;
2649 if (graph->n_total_row > graph->band_start)
2650 next_band(graph);
2651 return sort_statements(ctx, graph);
2654 /* Add a row to the schedules that separates the SCCs and move
2655 * to the next band.
2657 static int split_on_scc(struct isl_sched_graph *graph)
2659 int i;
2661 for (i = 0; i < graph->n; ++i) {
2662 struct isl_sched_node *node = &graph->node[i];
2663 int row = isl_mat_rows(node->sched);
2665 isl_map_free(node->sched_map);
2666 node->sched_map = NULL;
2667 node->sched = isl_mat_add_zero_rows(node->sched, 1);
2668 node->sched = isl_mat_set_element_si(node->sched, row, 0,
2669 node->scc);
2670 if (!node->sched)
2671 return -1;
2672 node->band[graph->n_total_row] = graph->n_band;
2675 graph->n_total_row++;
2676 next_band(graph);
2678 return 0;
2681 /* Compute a schedule for each component (identified by node->scc)
2682 * of the dependence graph separately and then combine the results.
2683 * Depending on the setting of schedule_fuse, a component may be
2684 * either weakly or strongly connected.
2686 * The band_id is adjusted such that each component has a separate id.
2687 * Note that the band_id may have already been set to a value different
2688 * from zero by compute_split_schedule.
2690 static int compute_component_schedule(isl_ctx *ctx,
2691 struct isl_sched_graph *graph)
2693 int wcc, i;
2694 int n, n_edge;
2695 int n_total_row, orig_total_row;
2696 int n_band, orig_band;
2698 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN ||
2699 ctx->opt->schedule_separate_components)
2700 split_on_scc(graph);
2702 n_total_row = 0;
2703 orig_total_row = graph->n_total_row;
2704 n_band = 0;
2705 orig_band = graph->n_band;
2706 for (i = 0; i < graph->n; ++i)
2707 graph->node[i].band_id[graph->n_band] += graph->node[i].scc;
2708 for (wcc = 0; wcc < graph->scc; ++wcc) {
2709 n = 0;
2710 for (i = 0; i < graph->n; ++i)
2711 if (graph->node[i].scc == wcc)
2712 n++;
2713 n_edge = 0;
2714 for (i = 0; i < graph->n_edge; ++i)
2715 if (graph->edge[i].src->scc == wcc &&
2716 graph->edge[i].dst->scc == wcc)
2717 n_edge++;
2719 if (compute_sub_schedule(ctx, graph, n, n_edge,
2720 &node_scc_exactly,
2721 &edge_scc_exactly, wcc, 1) < 0)
2722 return -1;
2723 if (graph->n_total_row > n_total_row)
2724 n_total_row = graph->n_total_row;
2725 graph->n_total_row = orig_total_row;
2726 if (graph->n_band > n_band)
2727 n_band = graph->n_band;
2728 graph->n_band = orig_band;
2731 graph->n_total_row = n_total_row;
2732 graph->n_band = n_band;
2734 return pad_schedule(graph);
2737 /* Compute a schedule for the given dependence graph.
2738 * We first check if the graph is connected (through validity dependences)
2739 * and, if not, compute a schedule for each component separately.
2740 * If schedule_fuse is set to minimal fusion, then we check for strongly
2741 * connected components instead and compute a separate schedule for
2742 * each such strongly connected component.
2744 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
2746 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN) {
2747 if (detect_sccs(ctx, graph) < 0)
2748 return -1;
2749 } else {
2750 if (detect_wccs(ctx, graph) < 0)
2751 return -1;
2754 if (graph->scc > 1)
2755 return compute_component_schedule(ctx, graph);
2757 return compute_schedule_wcc(ctx, graph);
2760 /* Compute a schedule for the given union of domains that respects
2761 * all the validity dependences.
2762 * If the default isl scheduling algorithm is used, it tries to minimize
2763 * the dependence distances over the proximity dependences.
2764 * If Feautrier's scheduling algorithm is used, the proximity dependence
2765 * distances are only minimized during the extension to a full-dimensional
2766 * schedule.
2768 __isl_give isl_schedule *isl_union_set_compute_schedule(
2769 __isl_take isl_union_set *domain,
2770 __isl_take isl_union_map *validity,
2771 __isl_take isl_union_map *proximity)
2773 isl_ctx *ctx = isl_union_set_get_ctx(domain);
2774 isl_space *dim;
2775 struct isl_sched_graph graph = { 0 };
2776 isl_schedule *sched;
2777 struct isl_extract_edge_data data;
2779 domain = isl_union_set_align_params(domain,
2780 isl_union_map_get_space(validity));
2781 domain = isl_union_set_align_params(domain,
2782 isl_union_map_get_space(proximity));
2783 dim = isl_union_set_get_space(domain);
2784 validity = isl_union_map_align_params(validity, isl_space_copy(dim));
2785 proximity = isl_union_map_align_params(proximity, dim);
2787 if (!domain)
2788 goto error;
2790 graph.n = isl_union_set_n_set(domain);
2791 if (graph.n == 0)
2792 goto empty;
2793 if (graph_alloc(ctx, &graph, graph.n,
2794 isl_union_map_n_map(validity) + isl_union_map_n_map(proximity)) < 0)
2795 goto error;
2796 if (compute_max_row(&graph, domain) < 0)
2797 goto error;
2798 graph.root = 1;
2799 graph.n = 0;
2800 if (isl_union_set_foreach_set(domain, &extract_node, &graph) < 0)
2801 goto error;
2802 if (graph_init_table(ctx, &graph) < 0)
2803 goto error;
2804 graph.max_edge[isl_edge_validity] = isl_union_map_n_map(validity);
2805 graph.max_edge[isl_edge_proximity] = isl_union_map_n_map(proximity);
2806 if (graph_init_edge_tables(ctx, &graph) < 0)
2807 goto error;
2808 graph.n_edge = 0;
2809 data.graph = &graph;
2810 data.type = isl_edge_validity;
2811 if (isl_union_map_foreach_map(validity, &extract_edge, &data) < 0)
2812 goto error;
2813 data.type = isl_edge_proximity;
2814 if (isl_union_map_foreach_map(proximity, &extract_edge, &data) < 0)
2815 goto error;
2817 if (compute_schedule(ctx, &graph) < 0)
2818 goto error;
2820 empty:
2821 sched = extract_schedule(&graph, isl_union_set_get_space(domain));
2823 graph_free(ctx, &graph);
2824 isl_union_set_free(domain);
2825 isl_union_map_free(validity);
2826 isl_union_map_free(proximity);
2828 return sched;
2829 error:
2830 graph_free(ctx, &graph);
2831 isl_union_set_free(domain);
2832 isl_union_map_free(validity);
2833 isl_union_map_free(proximity);
2834 return NULL;
2837 void *isl_schedule_free(__isl_take isl_schedule *sched)
2839 int i;
2840 if (!sched)
2841 return NULL;
2843 if (--sched->ref > 0)
2844 return NULL;
2846 for (i = 0; i < sched->n; ++i) {
2847 isl_multi_aff_free(sched->node[i].sched);
2848 free(sched->node[i].band_end);
2849 free(sched->node[i].band_id);
2850 free(sched->node[i].zero);
2852 isl_space_free(sched->dim);
2853 isl_band_list_free(sched->band_forest);
2854 free(sched);
2855 return NULL;
2858 isl_ctx *isl_schedule_get_ctx(__isl_keep isl_schedule *schedule)
2860 return schedule ? isl_space_get_ctx(schedule->dim) : NULL;
2863 /* Return an isl_union_map of the schedule. If we have already constructed
2864 * a band forest, then this band forest may have been modified so we need
2865 * to extract the isl_union_map from the forest rather than from
2866 * the originally computed schedule.
2868 __isl_give isl_union_map *isl_schedule_get_map(__isl_keep isl_schedule *sched)
2870 int i;
2871 isl_union_map *umap;
2873 if (!sched)
2874 return NULL;
2876 if (sched->band_forest)
2877 return isl_band_list_get_suffix_schedule(sched->band_forest);
2879 umap = isl_union_map_empty(isl_space_copy(sched->dim));
2880 for (i = 0; i < sched->n; ++i) {
2881 isl_multi_aff *ma;
2883 ma = isl_multi_aff_copy(sched->node[i].sched);
2884 umap = isl_union_map_add_map(umap, isl_map_from_multi_aff(ma));
2887 return umap;
2890 static __isl_give isl_band_list *construct_band_list(
2891 __isl_keep isl_schedule *schedule, __isl_keep isl_band *parent,
2892 int band_nr, int *parent_active, int n_active);
2894 /* Construct an isl_band structure for the band in the given schedule
2895 * with sequence number band_nr for the n_active nodes marked by active.
2896 * If the nodes don't have a band with the given sequence number,
2897 * then a band without members is created.
2899 * Because of the way the schedule is constructed, we know that
2900 * the position of the band inside the schedule of a node is the same
2901 * for all active nodes.
2903 static __isl_give isl_band *construct_band(__isl_keep isl_schedule *schedule,
2904 __isl_keep isl_band *parent,
2905 int band_nr, int *active, int n_active)
2907 int i, j;
2908 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
2909 isl_band *band;
2910 unsigned start, end;
2912 band = isl_band_alloc(ctx);
2913 if (!band)
2914 return NULL;
2916 band->schedule = schedule;
2917 band->parent = parent;
2919 for (i = 0; i < schedule->n; ++i)
2920 if (active[i] && schedule->node[i].n_band > band_nr + 1)
2921 break;
2923 if (i < schedule->n) {
2924 band->children = construct_band_list(schedule, band,
2925 band_nr + 1, active, n_active);
2926 if (!band->children)
2927 goto error;
2930 for (i = 0; i < schedule->n; ++i)
2931 if (active[i])
2932 break;
2934 if (i >= schedule->n)
2935 isl_die(ctx, isl_error_internal,
2936 "band without active statements", goto error);
2938 start = band_nr ? schedule->node[i].band_end[band_nr - 1] : 0;
2939 end = band_nr < schedule->node[i].n_band ?
2940 schedule->node[i].band_end[band_nr] : start;
2941 band->n = end - start;
2943 band->zero = isl_alloc_array(ctx, int, band->n);
2944 if (!band->zero)
2945 goto error;
2947 for (j = 0; j < band->n; ++j)
2948 band->zero[j] = schedule->node[i].zero[start + j];
2950 band->pma = isl_union_pw_multi_aff_empty(isl_space_copy(schedule->dim));
2951 for (i = 0; i < schedule->n; ++i) {
2952 isl_multi_aff *ma;
2953 isl_pw_multi_aff *pma;
2954 unsigned n_out;
2956 if (!active[i])
2957 continue;
2959 ma = isl_multi_aff_copy(schedule->node[i].sched);
2960 n_out = isl_multi_aff_dim(ma, isl_dim_out);
2961 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, end, n_out - end);
2962 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, 0, start);
2963 pma = isl_pw_multi_aff_from_multi_aff(ma);
2964 band->pma = isl_union_pw_multi_aff_add_pw_multi_aff(band->pma,
2965 pma);
2967 if (!band->pma)
2968 goto error;
2970 return band;
2971 error:
2972 isl_band_free(band);
2973 return NULL;
2976 /* Construct a list of bands that start at the same position (with
2977 * sequence number band_nr) in the schedules of the nodes that
2978 * were active in the parent band.
2980 * A separate isl_band structure is created for each band_id
2981 * and for each node that does not have a band with sequence
2982 * number band_nr. In the latter case, a band without members
2983 * is created.
2984 * This ensures that if a band has any children, then each node
2985 * that was active in the band is active in exactly one of the children.
2987 static __isl_give isl_band_list *construct_band_list(
2988 __isl_keep isl_schedule *schedule, __isl_keep isl_band *parent,
2989 int band_nr, int *parent_active, int n_active)
2991 int i, j;
2992 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
2993 int *active;
2994 int n_band;
2995 isl_band_list *list;
2997 n_band = 0;
2998 for (i = 0; i < n_active; ++i) {
2999 for (j = 0; j < schedule->n; ++j) {
3000 if (!parent_active[j])
3001 continue;
3002 if (schedule->node[j].n_band <= band_nr)
3003 continue;
3004 if (schedule->node[j].band_id[band_nr] == i) {
3005 n_band++;
3006 break;
3010 for (j = 0; j < schedule->n; ++j)
3011 if (schedule->node[j].n_band <= band_nr)
3012 n_band++;
3014 if (n_band == 1) {
3015 isl_band *band;
3016 list = isl_band_list_alloc(ctx, n_band);
3017 band = construct_band(schedule, parent, band_nr,
3018 parent_active, n_active);
3019 return isl_band_list_add(list, band);
3022 active = isl_alloc_array(ctx, int, schedule->n);
3023 if (!active)
3024 return NULL;
3026 list = isl_band_list_alloc(ctx, n_band);
3028 for (i = 0; i < n_active; ++i) {
3029 int n = 0;
3030 isl_band *band;
3032 for (j = 0; j < schedule->n; ++j) {
3033 active[j] = parent_active[j] &&
3034 schedule->node[j].n_band > band_nr &&
3035 schedule->node[j].band_id[band_nr] == i;
3036 if (active[j])
3037 n++;
3039 if (n == 0)
3040 continue;
3042 band = construct_band(schedule, parent, band_nr, active, n);
3044 list = isl_band_list_add(list, band);
3046 for (i = 0; i < schedule->n; ++i) {
3047 isl_band *band;
3048 if (!parent_active[i])
3049 continue;
3050 if (schedule->node[i].n_band > band_nr)
3051 continue;
3052 for (j = 0; j < schedule->n; ++j)
3053 active[j] = j == i;
3054 band = construct_band(schedule, parent, band_nr, active, 1);
3055 list = isl_band_list_add(list, band);
3058 free(active);
3060 return list;
3063 /* Construct a band forest representation of the schedule and
3064 * return the list of roots.
3066 static __isl_give isl_band_list *construct_forest(
3067 __isl_keep isl_schedule *schedule)
3069 int i;
3070 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
3071 isl_band_list *forest;
3072 int *active;
3074 active = isl_alloc_array(ctx, int, schedule->n);
3075 if (!active)
3076 return NULL;
3078 for (i = 0; i < schedule->n; ++i)
3079 active[i] = 1;
3081 forest = construct_band_list(schedule, NULL, 0, active, schedule->n);
3083 free(active);
3085 return forest;
3088 /* Return the roots of a band forest representation of the schedule.
3090 __isl_give isl_band_list *isl_schedule_get_band_forest(
3091 __isl_keep isl_schedule *schedule)
3093 if (!schedule)
3094 return NULL;
3095 if (!schedule->band_forest)
3096 schedule->band_forest = construct_forest(schedule);
3097 return isl_band_list_dup(schedule->band_forest);
3100 /* Call "fn" on each band in the schedule in depth-first post-order.
3102 int isl_schedule_foreach_band(__isl_keep isl_schedule *sched,
3103 int (*fn)(__isl_keep isl_band *band, void *user), void *user)
3105 int r;
3106 isl_band_list *forest;
3108 if (!sched)
3109 return -1;
3111 forest = isl_schedule_get_band_forest(sched);
3112 r = isl_band_list_foreach_band(forest, fn, user);
3113 isl_band_list_free(forest);
3115 return r;
3118 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
3119 __isl_keep isl_band_list *list);
3121 static __isl_give isl_printer *print_band(__isl_take isl_printer *p,
3122 __isl_keep isl_band *band)
3124 isl_band_list *children;
3126 p = isl_printer_start_line(p);
3127 p = isl_printer_print_union_pw_multi_aff(p, band->pma);
3128 p = isl_printer_end_line(p);
3130 if (!isl_band_has_children(band))
3131 return p;
3133 children = isl_band_get_children(band);
3135 p = isl_printer_indent(p, 4);
3136 p = print_band_list(p, children);
3137 p = isl_printer_indent(p, -4);
3139 isl_band_list_free(children);
3141 return p;
3144 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
3145 __isl_keep isl_band_list *list)
3147 int i, n;
3149 n = isl_band_list_n_band(list);
3150 for (i = 0; i < n; ++i) {
3151 isl_band *band;
3152 band = isl_band_list_get_band(list, i);
3153 p = print_band(p, band);
3154 isl_band_free(band);
3157 return p;
3160 __isl_give isl_printer *isl_printer_print_schedule(__isl_take isl_printer *p,
3161 __isl_keep isl_schedule *schedule)
3163 isl_band_list *forest;
3165 forest = isl_schedule_get_band_forest(schedule);
3167 p = print_band_list(p, forest);
3169 isl_band_list_free(forest);
3171 return p;
3174 void isl_schedule_dump(__isl_keep isl_schedule *schedule)
3176 isl_printer *printer;
3178 if (!schedule)
3179 return;
3181 printer = isl_printer_to_file(isl_schedule_get_ctx(schedule), stderr);
3182 printer = isl_printer_print_schedule(printer, schedule);
3184 isl_printer_free(printer);