isl_ast_build_expr.c: extract out isl_ast_build_with_arguments
[isl.git] / isl_schedule.c
blob362596db90a77174a7a9815bf559f61da01913a7
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9 * 91893 Orsay, France
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_space_private.h>
16 #include <isl_aff_private.h>
17 #include <isl/hash.h>
18 #include <isl/constraint.h>
19 #include <isl/schedule.h>
20 #include <isl_mat_private.h>
21 #include <isl_vec_private.h>
22 #include <isl/set.h>
23 #include <isl_seq.h>
24 #include <isl_tab.h>
25 #include <isl_dim_map.h>
26 #include <isl/map_to_basic_set.h>
27 #include <isl_sort.h>
28 #include <isl_schedule_private.h>
29 #include <isl_band_private.h>
30 #include <isl_options_private.h>
31 #include <isl_tarjan.h>
34 * The scheduling algorithm implemented in this file was inspired by
35 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
36 * Parallelization and Locality Optimization in the Polyhedral Model".
40 /* Internal information about a node that is used during the construction
41 * of a schedule.
42 * dim represents the space in which the domain lives
43 * sched is a matrix representation of the schedule being constructed
44 * for this node
45 * sched_map is an isl_map representation of the same (partial) schedule
46 * sched_map may be NULL
47 * rank is the number of linearly independent rows in the linear part
48 * of sched
49 * the columns of cmap represent a change of basis for the schedule
50 * coefficients; the first rank columns span the linear part of
51 * the schedule rows
52 * cinv is the inverse of cmap.
53 * start is the first variable in the LP problem in the sequences that
54 * represents the schedule coefficients of this node
55 * nvar is the dimension of the domain
56 * nparam is the number of parameters or 0 if we are not constructing
57 * a parametric schedule
59 * scc is the index of SCC (or WCC) this node belongs to
61 * band contains the band index for each of the rows of the schedule.
62 * band_id is used to differentiate between separate bands at the same
63 * level within the same parent band, i.e., bands that are separated
64 * by the parent band or bands that are independent of each other.
65 * zero contains a boolean for each of the rows of the schedule,
66 * indicating whether the corresponding scheduling dimension results
67 * in zero dependence distances within its band and with respect
68 * to the proximity edges.
70 struct isl_sched_node {
71 isl_space *dim;
72 isl_mat *sched;
73 isl_map *sched_map;
74 int rank;
75 isl_mat *cmap;
76 isl_mat *cinv;
77 int start;
78 int nvar;
79 int nparam;
81 int scc;
83 int *band;
84 int *band_id;
85 int *zero;
88 static int node_has_dim(const void *entry, const void *val)
90 struct isl_sched_node *node = (struct isl_sched_node *)entry;
91 isl_space *dim = (isl_space *)val;
93 return isl_space_is_equal(node->dim, dim);
96 /* An edge in the dependence graph. An edge may be used to
97 * ensure validity of the generated schedule, to minimize the dependence
98 * distance or both
100 * map is the dependence relation
101 * src is the source node
102 * dst is the sink node
103 * validity is set if the edge is used to ensure correctness
104 * proximity is set if the edge is used to minimize dependence distances
106 * For validity edges, start and end mark the sequence of inequality
107 * constraints in the LP problem that encode the validity constraint
108 * corresponding to this edge.
110 struct isl_sched_edge {
111 isl_map *map;
113 struct isl_sched_node *src;
114 struct isl_sched_node *dst;
116 int validity;
117 int proximity;
119 int start;
120 int end;
123 enum isl_edge_type {
124 isl_edge_validity = 0,
125 isl_edge_first = isl_edge_validity,
126 isl_edge_proximity,
127 isl_edge_last = isl_edge_proximity
130 /* Internal information about the dependence graph used during
131 * the construction of the schedule.
133 * intra_hmap is a cache, mapping dependence relations to their dual,
134 * for dependences from a node to itself
135 * inter_hmap is a cache, mapping dependence relations to their dual,
136 * for dependences between distinct nodes
138 * n is the number of nodes
139 * node is the list of nodes
140 * maxvar is the maximal number of variables over all nodes
141 * max_row is the allocated number of rows in the schedule
142 * n_row is the current (maximal) number of linearly independent
143 * rows in the node schedules
144 * n_total_row is the current number of rows in the node schedules
145 * n_band is the current number of completed bands
146 * band_start is the starting row in the node schedules of the current band
147 * root is set if this graph is the original dependence graph,
148 * without any splitting
150 * sorted contains a list of node indices sorted according to the
151 * SCC to which a node belongs
153 * n_edge is the number of edges
154 * edge is the list of edges
155 * max_edge contains the maximal number of edges of each type;
156 * in particular, it contains the number of edges in the inital graph.
157 * edge_table contains pointers into the edge array, hashed on the source
158 * and sink spaces; there is one such table for each type;
159 * a given edge may be referenced from more than one table
160 * if the corresponding relation appears in more than of the
161 * sets of dependences
163 * node_table contains pointers into the node array, hashed on the space
165 * region contains a list of variable sequences that should be non-trivial
167 * lp contains the (I)LP problem used to obtain new schedule rows
169 * src_scc and dst_scc are the source and sink SCCs of an edge with
170 * conflicting constraints
172 * scc represents the number of components
174 struct isl_sched_graph {
175 isl_map_to_basic_set *intra_hmap;
176 isl_map_to_basic_set *inter_hmap;
178 struct isl_sched_node *node;
179 int n;
180 int maxvar;
181 int max_row;
182 int n_row;
184 int *sorted;
186 int n_band;
187 int n_total_row;
188 int band_start;
190 int root;
192 struct isl_sched_edge *edge;
193 int n_edge;
194 int max_edge[isl_edge_last + 1];
195 struct isl_hash_table *edge_table[isl_edge_last + 1];
197 struct isl_hash_table *node_table;
198 struct isl_region *region;
200 isl_basic_set *lp;
202 int src_scc;
203 int dst_scc;
205 int scc;
208 /* Initialize node_table based on the list of nodes.
210 static int graph_init_table(isl_ctx *ctx, struct isl_sched_graph *graph)
212 int i;
214 graph->node_table = isl_hash_table_alloc(ctx, graph->n);
215 if (!graph->node_table)
216 return -1;
218 for (i = 0; i < graph->n; ++i) {
219 struct isl_hash_table_entry *entry;
220 uint32_t hash;
222 hash = isl_space_get_hash(graph->node[i].dim);
223 entry = isl_hash_table_find(ctx, graph->node_table, hash,
224 &node_has_dim,
225 graph->node[i].dim, 1);
226 if (!entry)
227 return -1;
228 entry->data = &graph->node[i];
231 return 0;
234 /* Return a pointer to the node that lives within the given space,
235 * or NULL if there is no such node.
237 static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
238 struct isl_sched_graph *graph, __isl_keep isl_space *dim)
240 struct isl_hash_table_entry *entry;
241 uint32_t hash;
243 hash = isl_space_get_hash(dim);
244 entry = isl_hash_table_find(ctx, graph->node_table, hash,
245 &node_has_dim, dim, 0);
247 return entry ? entry->data : NULL;
250 static int edge_has_src_and_dst(const void *entry, const void *val)
252 const struct isl_sched_edge *edge = entry;
253 const struct isl_sched_edge *temp = val;
255 return edge->src == temp->src && edge->dst == temp->dst;
258 /* Add the given edge to graph->edge_table[type].
260 static int graph_edge_table_add(isl_ctx *ctx, struct isl_sched_graph *graph,
261 enum isl_edge_type type, struct isl_sched_edge *edge)
263 struct isl_hash_table_entry *entry;
264 uint32_t hash;
266 hash = isl_hash_init();
267 hash = isl_hash_builtin(hash, edge->src);
268 hash = isl_hash_builtin(hash, edge->dst);
269 entry = isl_hash_table_find(ctx, graph->edge_table[type], hash,
270 &edge_has_src_and_dst, edge, 1);
271 if (!entry)
272 return -1;
273 entry->data = edge;
275 return 0;
278 /* Allocate the edge_tables based on the maximal number of edges of
279 * each type.
281 static int graph_init_edge_tables(isl_ctx *ctx, struct isl_sched_graph *graph)
283 int i;
285 for (i = 0; i <= isl_edge_last; ++i) {
286 graph->edge_table[i] = isl_hash_table_alloc(ctx,
287 graph->max_edge[i]);
288 if (!graph->edge_table[i])
289 return -1;
292 return 0;
295 /* If graph->edge_table[type] contains an edge from the given source
296 * to the given destination, then return the hash table entry of this edge.
297 * Otherwise, return NULL.
299 static struct isl_hash_table_entry *graph_find_edge_entry(
300 struct isl_sched_graph *graph,
301 enum isl_edge_type type,
302 struct isl_sched_node *src, struct isl_sched_node *dst)
304 isl_ctx *ctx = isl_space_get_ctx(src->dim);
305 uint32_t hash;
306 struct isl_sched_edge temp = { .src = src, .dst = dst };
308 hash = isl_hash_init();
309 hash = isl_hash_builtin(hash, temp.src);
310 hash = isl_hash_builtin(hash, temp.dst);
311 return isl_hash_table_find(ctx, graph->edge_table[type], hash,
312 &edge_has_src_and_dst, &temp, 0);
316 /* If graph->edge_table[type] contains an edge from the given source
317 * to the given destination, then return this edge.
318 * Otherwise, return NULL.
320 static struct isl_sched_edge *graph_find_edge(struct isl_sched_graph *graph,
321 enum isl_edge_type type,
322 struct isl_sched_node *src, struct isl_sched_node *dst)
324 struct isl_hash_table_entry *entry;
326 entry = graph_find_edge_entry(graph, type, src, dst);
327 if (!entry)
328 return NULL;
330 return entry->data;
333 /* Check whether the dependence graph has an edge of the given type
334 * between the given two nodes.
336 static int graph_has_edge(struct isl_sched_graph *graph,
337 enum isl_edge_type type,
338 struct isl_sched_node *src, struct isl_sched_node *dst)
340 struct isl_sched_edge *edge;
341 int empty;
343 edge = graph_find_edge(graph, type, src, dst);
344 if (!edge)
345 return 0;
347 empty = isl_map_plain_is_empty(edge->map);
348 if (empty < 0)
349 return -1;
351 return !empty;
354 /* If there is an edge from the given source to the given destination
355 * of any type then return this edge.
356 * Otherwise, return NULL.
358 static struct isl_sched_edge *graph_find_any_edge(struct isl_sched_graph *graph,
359 struct isl_sched_node *src, struct isl_sched_node *dst)
361 enum isl_edge_type i;
362 struct isl_sched_edge *edge;
364 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
365 edge = graph_find_edge(graph, i, src, dst);
366 if (edge)
367 return edge;
370 return NULL;
373 /* Remove the given edge from all the edge_tables that refer to it.
375 static void graph_remove_edge(struct isl_sched_graph *graph,
376 struct isl_sched_edge *edge)
378 isl_ctx *ctx = isl_map_get_ctx(edge->map);
379 enum isl_edge_type i;
381 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
382 struct isl_hash_table_entry *entry;
384 entry = graph_find_edge_entry(graph, i, edge->src, edge->dst);
385 if (!entry)
386 continue;
387 if (entry->data != edge)
388 continue;
389 isl_hash_table_remove(ctx, graph->edge_table[i], entry);
393 /* Check whether the dependence graph has any edge
394 * between the given two nodes.
396 static int graph_has_any_edge(struct isl_sched_graph *graph,
397 struct isl_sched_node *src, struct isl_sched_node *dst)
399 enum isl_edge_type i;
400 int r;
402 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
403 r = graph_has_edge(graph, i, src, dst);
404 if (r < 0 || r)
405 return r;
408 return r;
411 /* Check whether the dependence graph has a validity edge
412 * between the given two nodes.
414 static int graph_has_validity_edge(struct isl_sched_graph *graph,
415 struct isl_sched_node *src, struct isl_sched_node *dst)
417 return graph_has_edge(graph, isl_edge_validity, src, dst);
420 static int graph_alloc(isl_ctx *ctx, struct isl_sched_graph *graph,
421 int n_node, int n_edge)
423 int i;
425 graph->n = n_node;
426 graph->n_edge = n_edge;
427 graph->node = isl_calloc_array(ctx, struct isl_sched_node, graph->n);
428 graph->sorted = isl_calloc_array(ctx, int, graph->n);
429 graph->region = isl_alloc_array(ctx, struct isl_region, graph->n);
430 graph->edge = isl_calloc_array(ctx,
431 struct isl_sched_edge, graph->n_edge);
433 graph->intra_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
434 graph->inter_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
436 if (!graph->node || !graph->region || (graph->n_edge && !graph->edge) ||
437 !graph->sorted)
438 return -1;
440 for(i = 0; i < graph->n; ++i)
441 graph->sorted[i] = i;
443 return 0;
446 static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
448 int i;
450 isl_map_to_basic_set_free(graph->intra_hmap);
451 isl_map_to_basic_set_free(graph->inter_hmap);
453 for (i = 0; i < graph->n; ++i) {
454 isl_space_free(graph->node[i].dim);
455 isl_mat_free(graph->node[i].sched);
456 isl_map_free(graph->node[i].sched_map);
457 isl_mat_free(graph->node[i].cmap);
458 isl_mat_free(graph->node[i].cinv);
459 if (graph->root) {
460 free(graph->node[i].band);
461 free(graph->node[i].band_id);
462 free(graph->node[i].zero);
465 free(graph->node);
466 free(graph->sorted);
467 for (i = 0; i < graph->n_edge; ++i)
468 isl_map_free(graph->edge[i].map);
469 free(graph->edge);
470 free(graph->region);
471 for (i = 0; i <= isl_edge_last; ++i)
472 isl_hash_table_free(ctx, graph->edge_table[i]);
473 isl_hash_table_free(ctx, graph->node_table);
474 isl_basic_set_free(graph->lp);
477 /* For each "set" on which this function is called, increment
478 * graph->n by one and update graph->maxvar.
480 static int init_n_maxvar(__isl_take isl_set *set, void *user)
482 struct isl_sched_graph *graph = user;
483 int nvar = isl_set_dim(set, isl_dim_set);
485 graph->n++;
486 if (nvar > graph->maxvar)
487 graph->maxvar = nvar;
489 isl_set_free(set);
491 return 0;
494 /* Compute the number of rows that should be allocated for the schedule.
495 * The graph can be split at most "n - 1" times, there can be at most
496 * two rows for each dimension in the iteration domains (in particular,
497 * we usually have one row, but it may be split by split_scaled),
498 * and there can be one extra row for ordering the statements.
499 * Note that if we have actually split "n - 1" times, then no ordering
500 * is needed, so in principle we could use "graph->n + 2 * graph->maxvar - 1".
502 static int compute_max_row(struct isl_sched_graph *graph,
503 __isl_keep isl_union_set *domain)
505 graph->n = 0;
506 graph->maxvar = 0;
507 if (isl_union_set_foreach_set(domain, &init_n_maxvar, graph) < 0)
508 return -1;
509 graph->max_row = graph->n + 2 * graph->maxvar;
511 return 0;
514 /* Add a new node to the graph representing the given set.
516 static int extract_node(__isl_take isl_set *set, void *user)
518 int nvar, nparam;
519 isl_ctx *ctx;
520 isl_space *dim;
521 isl_mat *sched;
522 struct isl_sched_graph *graph = user;
523 int *band, *band_id, *zero;
525 ctx = isl_set_get_ctx(set);
526 dim = isl_set_get_space(set);
527 isl_set_free(set);
528 nvar = isl_space_dim(dim, isl_dim_set);
529 nparam = isl_space_dim(dim, isl_dim_param);
530 if (!ctx->opt->schedule_parametric)
531 nparam = 0;
532 sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
533 graph->node[graph->n].dim = dim;
534 graph->node[graph->n].nvar = nvar;
535 graph->node[graph->n].nparam = nparam;
536 graph->node[graph->n].sched = sched;
537 graph->node[graph->n].sched_map = NULL;
538 band = isl_alloc_array(ctx, int, graph->max_row);
539 graph->node[graph->n].band = band;
540 band_id = isl_calloc_array(ctx, int, graph->max_row);
541 graph->node[graph->n].band_id = band_id;
542 zero = isl_calloc_array(ctx, int, graph->max_row);
543 graph->node[graph->n].zero = zero;
544 graph->n++;
546 if (!sched || (graph->max_row && (!band || !band_id || !zero)))
547 return -1;
549 return 0;
552 struct isl_extract_edge_data {
553 enum isl_edge_type type;
554 struct isl_sched_graph *graph;
557 /* Add a new edge to the graph based on the given map
558 * and add it to data->graph->edge_table[data->type].
559 * If a dependence relation of a given type happens to be identical
560 * to one of the dependence relations of a type that was added before,
561 * then we don't create a new edge, but instead mark the original edge
562 * as also representing a dependence of the current type.
564 static int extract_edge(__isl_take isl_map *map, void *user)
566 isl_ctx *ctx = isl_map_get_ctx(map);
567 struct isl_extract_edge_data *data = user;
568 struct isl_sched_graph *graph = data->graph;
569 struct isl_sched_node *src, *dst;
570 isl_space *dim;
571 struct isl_sched_edge *edge;
572 int is_equal;
574 dim = isl_space_domain(isl_map_get_space(map));
575 src = graph_find_node(ctx, graph, dim);
576 isl_space_free(dim);
577 dim = isl_space_range(isl_map_get_space(map));
578 dst = graph_find_node(ctx, graph, dim);
579 isl_space_free(dim);
581 if (!src || !dst) {
582 isl_map_free(map);
583 return 0;
586 graph->edge[graph->n_edge].src = src;
587 graph->edge[graph->n_edge].dst = dst;
588 graph->edge[graph->n_edge].map = map;
589 if (data->type == isl_edge_validity) {
590 graph->edge[graph->n_edge].validity = 1;
591 graph->edge[graph->n_edge].proximity = 0;
593 if (data->type == isl_edge_proximity) {
594 graph->edge[graph->n_edge].validity = 0;
595 graph->edge[graph->n_edge].proximity = 1;
597 graph->n_edge++;
599 edge = graph_find_any_edge(graph, src, dst);
600 if (!edge)
601 return graph_edge_table_add(ctx, graph, data->type,
602 &graph->edge[graph->n_edge - 1]);
603 is_equal = isl_map_plain_is_equal(map, edge->map);
604 if (is_equal < 0)
605 return -1;
606 if (!is_equal)
607 return graph_edge_table_add(ctx, graph, data->type,
608 &graph->edge[graph->n_edge - 1]);
610 graph->n_edge--;
611 edge->validity |= graph->edge[graph->n_edge].validity;
612 edge->proximity |= graph->edge[graph->n_edge].proximity;
613 isl_map_free(map);
615 return graph_edge_table_add(ctx, graph, data->type, edge);
618 /* Check whether there is any dependence from node[j] to node[i]
619 * or from node[i] to node[j].
621 static int node_follows_weak(int i, int j, void *user)
623 int f;
624 struct isl_sched_graph *graph = user;
626 f = graph_has_any_edge(graph, &graph->node[j], &graph->node[i]);
627 if (f < 0 || f)
628 return f;
629 return graph_has_any_edge(graph, &graph->node[i], &graph->node[j]);
632 /* Check whether there is a validity dependence from node[j] to node[i],
633 * forcing node[i] to follow node[j].
635 static int node_follows_strong(int i, int j, void *user)
637 struct isl_sched_graph *graph = user;
639 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
642 /* Use Tarjan's algorithm for computing the strongly connected components
643 * in the dependence graph (only validity edges).
644 * If weak is set, we consider the graph to be undirected and
645 * we effectively compute the (weakly) connected components.
646 * Additionally, we also consider other edges when weak is set.
648 static int detect_ccs(isl_ctx *ctx, struct isl_sched_graph *graph, int weak)
650 int i, n;
651 struct isl_tarjan_graph *g = NULL;
653 g = isl_tarjan_graph_init(ctx, graph->n,
654 weak ? &node_follows_weak : &node_follows_strong, graph);
655 if (!g)
656 return -1;
658 graph->scc = 0;
659 i = 0;
660 n = graph->n;
661 while (n) {
662 while (g->order[i] != -1) {
663 graph->node[g->order[i]].scc = graph->scc;
664 --n;
665 ++i;
667 ++i;
668 graph->scc++;
671 isl_tarjan_graph_free(g);
673 return 0;
676 /* Apply Tarjan's algorithm to detect the strongly connected components
677 * in the dependence graph.
679 static int detect_sccs(isl_ctx *ctx, struct isl_sched_graph *graph)
681 return detect_ccs(ctx, graph, 0);
684 /* Apply Tarjan's algorithm to detect the (weakly) connected components
685 * in the dependence graph.
687 static int detect_wccs(isl_ctx *ctx, struct isl_sched_graph *graph)
689 return detect_ccs(ctx, graph, 1);
692 static int cmp_scc(const void *a, const void *b, void *data)
694 struct isl_sched_graph *graph = data;
695 const int *i1 = a;
696 const int *i2 = b;
698 return graph->node[*i1].scc - graph->node[*i2].scc;
701 /* Sort the elements of graph->sorted according to the corresponding SCCs.
703 static int sort_sccs(struct isl_sched_graph *graph)
705 return isl_sort(graph->sorted, graph->n, sizeof(int), &cmp_scc, graph);
708 /* Given a dependence relation R from a node to itself,
709 * construct the set of coefficients of valid constraints for elements
710 * in that dependence relation.
711 * In particular, the result contains tuples of coefficients
712 * c_0, c_n, c_x such that
714 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
716 * or, equivalently,
718 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
720 * We choose here to compute the dual of delta R.
721 * Alternatively, we could have computed the dual of R, resulting
722 * in a set of tuples c_0, c_n, c_x, c_y, and then
723 * plugged in (c_0, c_n, c_x, -c_x).
725 static __isl_give isl_basic_set *intra_coefficients(
726 struct isl_sched_graph *graph, __isl_take isl_map *map)
728 isl_set *delta;
729 isl_basic_set *coef;
731 if (isl_map_to_basic_set_has(graph->intra_hmap, map))
732 return isl_map_to_basic_set_get(graph->intra_hmap, map);
734 delta = isl_set_remove_divs(isl_map_deltas(isl_map_copy(map)));
735 coef = isl_set_coefficients(delta);
736 graph->intra_hmap = isl_map_to_basic_set_set(graph->intra_hmap, map,
737 isl_basic_set_copy(coef));
739 return coef;
742 /* Given a dependence relation R, * construct the set of coefficients
743 * of valid constraints for elements in that dependence relation.
744 * In particular, the result contains tuples of coefficients
745 * c_0, c_n, c_x, c_y such that
747 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
750 static __isl_give isl_basic_set *inter_coefficients(
751 struct isl_sched_graph *graph, __isl_take isl_map *map)
753 isl_set *set;
754 isl_basic_set *coef;
756 if (isl_map_to_basic_set_has(graph->inter_hmap, map))
757 return isl_map_to_basic_set_get(graph->inter_hmap, map);
759 set = isl_map_wrap(isl_map_remove_divs(isl_map_copy(map)));
760 coef = isl_set_coefficients(set);
761 graph->inter_hmap = isl_map_to_basic_set_set(graph->inter_hmap, map,
762 isl_basic_set_copy(coef));
764 return coef;
767 /* Add constraints to graph->lp that force validity for the given
768 * dependence from a node i to itself.
769 * That is, add constraints that enforce
771 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
772 * = c_i_x (y - x) >= 0
774 * for each (x,y) in R.
775 * We obtain general constraints on coefficients (c_0, c_n, c_x)
776 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
777 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
778 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
780 * Actually, we do not construct constraints for the c_i_x themselves,
781 * but for the coefficients of c_i_x written as a linear combination
782 * of the columns in node->cmap.
784 static int add_intra_validity_constraints(struct isl_sched_graph *graph,
785 struct isl_sched_edge *edge)
787 unsigned total;
788 isl_map *map = isl_map_copy(edge->map);
789 isl_ctx *ctx = isl_map_get_ctx(map);
790 isl_space *dim;
791 isl_dim_map *dim_map;
792 isl_basic_set *coef;
793 struct isl_sched_node *node = edge->src;
795 coef = intra_coefficients(graph, map);
797 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
799 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
800 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
801 if (!coef)
802 goto error;
804 total = isl_basic_set_total_dim(graph->lp);
805 dim_map = isl_dim_map_alloc(ctx, total);
806 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
807 isl_space_dim(dim, isl_dim_set), 1,
808 node->nvar, -1);
809 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
810 isl_space_dim(dim, isl_dim_set), 1,
811 node->nvar, 1);
812 graph->lp = isl_basic_set_extend_constraints(graph->lp,
813 coef->n_eq, coef->n_ineq);
814 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
815 coef, dim_map);
816 isl_space_free(dim);
818 return 0;
819 error:
820 isl_space_free(dim);
821 return -1;
824 /* Add constraints to graph->lp that force validity for the given
825 * dependence from node i to node j.
826 * That is, add constraints that enforce
828 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
830 * for each (x,y) in R.
831 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
832 * of valid constraints for R and then plug in
833 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
834 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
835 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
836 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
838 * Actually, we do not construct constraints for the c_*_x themselves,
839 * but for the coefficients of c_*_x written as a linear combination
840 * of the columns in node->cmap.
842 static int add_inter_validity_constraints(struct isl_sched_graph *graph,
843 struct isl_sched_edge *edge)
845 unsigned total;
846 isl_map *map = isl_map_copy(edge->map);
847 isl_ctx *ctx = isl_map_get_ctx(map);
848 isl_space *dim;
849 isl_dim_map *dim_map;
850 isl_basic_set *coef;
851 struct isl_sched_node *src = edge->src;
852 struct isl_sched_node *dst = edge->dst;
854 coef = inter_coefficients(graph, map);
856 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
858 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
859 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
860 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
861 isl_space_dim(dim, isl_dim_set) + src->nvar,
862 isl_mat_copy(dst->cmap));
863 if (!coef)
864 goto error;
866 total = isl_basic_set_total_dim(graph->lp);
867 dim_map = isl_dim_map_alloc(ctx, total);
869 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
870 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
871 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
872 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
873 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
874 dst->nvar, -1);
875 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
876 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
877 dst->nvar, 1);
879 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
880 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
881 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
882 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
883 isl_space_dim(dim, isl_dim_set), 1,
884 src->nvar, 1);
885 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
886 isl_space_dim(dim, isl_dim_set), 1,
887 src->nvar, -1);
889 edge->start = graph->lp->n_ineq;
890 graph->lp = isl_basic_set_extend_constraints(graph->lp,
891 coef->n_eq, coef->n_ineq);
892 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
893 coef, dim_map);
894 if (!graph->lp)
895 goto error;
896 isl_space_free(dim);
897 edge->end = graph->lp->n_ineq;
899 return 0;
900 error:
901 isl_space_free(dim);
902 return -1;
905 /* Add constraints to graph->lp that bound the dependence distance for the given
906 * dependence from a node i to itself.
907 * If s = 1, we add the constraint
909 * c_i_x (y - x) <= m_0 + m_n n
911 * or
913 * -c_i_x (y - x) + m_0 + m_n n >= 0
915 * for each (x,y) in R.
916 * If s = -1, we add the constraint
918 * -c_i_x (y - x) <= m_0 + m_n n
920 * or
922 * c_i_x (y - x) + m_0 + m_n n >= 0
924 * for each (x,y) in R.
925 * We obtain general constraints on coefficients (c_0, c_n, c_x)
926 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
927 * with each coefficient (except m_0) represented as a pair of non-negative
928 * coefficients.
930 * Actually, we do not construct constraints for the c_i_x themselves,
931 * but for the coefficients of c_i_x written as a linear combination
932 * of the columns in node->cmap.
934 static int add_intra_proximity_constraints(struct isl_sched_graph *graph,
935 struct isl_sched_edge *edge, int s)
937 unsigned total;
938 unsigned nparam;
939 isl_map *map = isl_map_copy(edge->map);
940 isl_ctx *ctx = isl_map_get_ctx(map);
941 isl_space *dim;
942 isl_dim_map *dim_map;
943 isl_basic_set *coef;
944 struct isl_sched_node *node = edge->src;
946 coef = intra_coefficients(graph, map);
948 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
950 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
951 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
952 if (!coef)
953 goto error;
955 nparam = isl_space_dim(node->dim, isl_dim_param);
956 total = isl_basic_set_total_dim(graph->lp);
957 dim_map = isl_dim_map_alloc(ctx, total);
958 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
959 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
960 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
961 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
962 isl_space_dim(dim, isl_dim_set), 1,
963 node->nvar, s);
964 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
965 isl_space_dim(dim, isl_dim_set), 1,
966 node->nvar, -s);
967 graph->lp = isl_basic_set_extend_constraints(graph->lp,
968 coef->n_eq, coef->n_ineq);
969 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
970 coef, dim_map);
971 isl_space_free(dim);
973 return 0;
974 error:
975 isl_space_free(dim);
976 return -1;
979 /* Add constraints to graph->lp that bound the dependence distance for the given
980 * dependence from node i to node j.
981 * If s = 1, we add the constraint
983 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
984 * <= m_0 + m_n n
986 * or
988 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
989 * m_0 + m_n n >= 0
991 * for each (x,y) in R.
992 * If s = -1, we add the constraint
994 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
995 * <= m_0 + m_n n
997 * or
999 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1000 * m_0 + m_n n >= 0
1002 * for each (x,y) in R.
1003 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1004 * of valid constraints for R and then plug in
1005 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1006 * -s*c_j_x+s*c_i_x)
1007 * with each coefficient (except m_0, c_j_0 and c_i_0)
1008 * represented as a pair of non-negative coefficients.
1010 * Actually, we do not construct constraints for the c_*_x themselves,
1011 * but for the coefficients of c_*_x written as a linear combination
1012 * of the columns in node->cmap.
1014 static int add_inter_proximity_constraints(struct isl_sched_graph *graph,
1015 struct isl_sched_edge *edge, int s)
1017 unsigned total;
1018 unsigned nparam;
1019 isl_map *map = isl_map_copy(edge->map);
1020 isl_ctx *ctx = isl_map_get_ctx(map);
1021 isl_space *dim;
1022 isl_dim_map *dim_map;
1023 isl_basic_set *coef;
1024 struct isl_sched_node *src = edge->src;
1025 struct isl_sched_node *dst = edge->dst;
1027 coef = inter_coefficients(graph, map);
1029 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1031 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1032 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1033 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1034 isl_space_dim(dim, isl_dim_set) + src->nvar,
1035 isl_mat_copy(dst->cmap));
1036 if (!coef)
1037 goto error;
1039 nparam = isl_space_dim(src->dim, isl_dim_param);
1040 total = isl_basic_set_total_dim(graph->lp);
1041 dim_map = isl_dim_map_alloc(ctx, total);
1043 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1044 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1045 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1047 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, -s);
1048 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, s);
1049 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, -s);
1050 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1051 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1052 dst->nvar, s);
1053 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1054 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1055 dst->nvar, -s);
1057 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, s);
1058 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, -s);
1059 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, s);
1060 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1061 isl_space_dim(dim, isl_dim_set), 1,
1062 src->nvar, -s);
1063 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1064 isl_space_dim(dim, isl_dim_set), 1,
1065 src->nvar, s);
1067 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1068 coef->n_eq, coef->n_ineq);
1069 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1070 coef, dim_map);
1071 isl_space_free(dim);
1073 return 0;
1074 error:
1075 isl_space_free(dim);
1076 return -1;
1079 static int add_all_validity_constraints(struct isl_sched_graph *graph)
1081 int i;
1083 for (i = 0; i < graph->n_edge; ++i) {
1084 struct isl_sched_edge *edge= &graph->edge[i];
1085 if (!edge->validity)
1086 continue;
1087 if (edge->src != edge->dst)
1088 continue;
1089 if (add_intra_validity_constraints(graph, edge) < 0)
1090 return -1;
1093 for (i = 0; i < graph->n_edge; ++i) {
1094 struct isl_sched_edge *edge = &graph->edge[i];
1095 if (!edge->validity)
1096 continue;
1097 if (edge->src == edge->dst)
1098 continue;
1099 if (add_inter_validity_constraints(graph, edge) < 0)
1100 return -1;
1103 return 0;
1106 /* Add constraints to graph->lp that bound the dependence distance
1107 * for all dependence relations.
1108 * If a given proximity dependence is identical to a validity
1109 * dependence, then the dependence distance is already bounded
1110 * from below (by zero), so we only need to bound the distance
1111 * from above.
1112 * Otherwise, we need to bound the distance both from above and from below.
1114 static int add_all_proximity_constraints(struct isl_sched_graph *graph)
1116 int i;
1118 for (i = 0; i < graph->n_edge; ++i) {
1119 struct isl_sched_edge *edge= &graph->edge[i];
1120 if (!edge->proximity)
1121 continue;
1122 if (edge->src == edge->dst &&
1123 add_intra_proximity_constraints(graph, edge, 1) < 0)
1124 return -1;
1125 if (edge->src != edge->dst &&
1126 add_inter_proximity_constraints(graph, edge, 1) < 0)
1127 return -1;
1128 if (edge->validity)
1129 continue;
1130 if (edge->src == edge->dst &&
1131 add_intra_proximity_constraints(graph, edge, -1) < 0)
1132 return -1;
1133 if (edge->src != edge->dst &&
1134 add_inter_proximity_constraints(graph, edge, -1) < 0)
1135 return -1;
1138 return 0;
1141 /* Compute a basis for the rows in the linear part of the schedule
1142 * and extend this basis to a full basis. The remaining rows
1143 * can then be used to force linear independence from the rows
1144 * in the schedule.
1146 * In particular, given the schedule rows S, we compute
1148 * S = H Q
1149 * S U = H
1151 * with H the Hermite normal form of S. That is, all but the
1152 * first rank columns of H are zero and so each row in S is
1153 * a linear combination of the first rank rows of Q.
1154 * The matrix Q is then transposed because we will write the
1155 * coefficients of the next schedule row as a column vector s
1156 * and express this s as a linear combination s = Q c of the
1157 * computed basis.
1158 * Similarly, the matrix U is transposed such that we can
1159 * compute the coefficients c = U s from a schedule row s.
1161 static int node_update_cmap(struct isl_sched_node *node)
1163 isl_mat *H, *U, *Q;
1164 int n_row = isl_mat_rows(node->sched);
1166 H = isl_mat_sub_alloc(node->sched, 0, n_row,
1167 1 + node->nparam, node->nvar);
1169 H = isl_mat_left_hermite(H, 0, &U, &Q);
1170 isl_mat_free(node->cmap);
1171 isl_mat_free(node->cinv);
1172 node->cmap = isl_mat_transpose(Q);
1173 node->cinv = isl_mat_transpose(U);
1174 node->rank = isl_mat_initial_non_zero_cols(H);
1175 isl_mat_free(H);
1177 if (!node->cmap || !node->cinv || node->rank < 0)
1178 return -1;
1179 return 0;
1182 /* Count the number of equality and inequality constraints
1183 * that will be added for the given map.
1184 * If carry is set, then we are counting the number of (validity)
1185 * constraints that will be added in setup_carry_lp and we count
1186 * each edge exactly once. Otherwise, we count as follows
1187 * validity -> 1 (>= 0)
1188 * validity+proximity -> 2 (>= 0 and upper bound)
1189 * proximity -> 2 (lower and upper bound)
1191 static int count_map_constraints(struct isl_sched_graph *graph,
1192 struct isl_sched_edge *edge, __isl_take isl_map *map,
1193 int *n_eq, int *n_ineq, int carry)
1195 isl_basic_set *coef;
1196 int f = carry ? 1 : edge->proximity ? 2 : 1;
1198 if (carry && !edge->validity) {
1199 isl_map_free(map);
1200 return 0;
1203 if (edge->src == edge->dst)
1204 coef = intra_coefficients(graph, map);
1205 else
1206 coef = inter_coefficients(graph, map);
1207 if (!coef)
1208 return -1;
1209 *n_eq += f * coef->n_eq;
1210 *n_ineq += f * coef->n_ineq;
1211 isl_basic_set_free(coef);
1213 return 0;
1216 /* Count the number of equality and inequality constraints
1217 * that will be added to the main lp problem.
1218 * We count as follows
1219 * validity -> 1 (>= 0)
1220 * validity+proximity -> 2 (>= 0 and upper bound)
1221 * proximity -> 2 (lower and upper bound)
1223 static int count_constraints(struct isl_sched_graph *graph,
1224 int *n_eq, int *n_ineq)
1226 int i;
1228 *n_eq = *n_ineq = 0;
1229 for (i = 0; i < graph->n_edge; ++i) {
1230 struct isl_sched_edge *edge= &graph->edge[i];
1231 isl_map *map = isl_map_copy(edge->map);
1233 if (count_map_constraints(graph, edge, map,
1234 n_eq, n_ineq, 0) < 0)
1235 return -1;
1238 return 0;
1241 /* Count the number of constraints that will be added by
1242 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
1243 * accordingly.
1245 * In practice, add_bound_coefficient_constraints only adds inequalities.
1247 static int count_bound_coefficient_constraints(isl_ctx *ctx,
1248 struct isl_sched_graph *graph, int *n_eq, int *n_ineq)
1250 int i;
1252 if (ctx->opt->schedule_max_coefficient == -1)
1253 return 0;
1255 for (i = 0; i < graph->n; ++i)
1256 *n_ineq += 2 * graph->node[i].nparam + 2 * graph->node[i].nvar;
1258 return 0;
1261 /* Add constraints that bound the values of the variable and parameter
1262 * coefficients of the schedule.
1264 * The maximal value of the coefficients is defined by the option
1265 * 'schedule_max_coefficient'.
1267 static int add_bound_coefficient_constraints(isl_ctx *ctx,
1268 struct isl_sched_graph *graph)
1270 int i, j, k;
1271 int max_coefficient;
1272 int total;
1274 max_coefficient = ctx->opt->schedule_max_coefficient;
1276 if (max_coefficient == -1)
1277 return 0;
1279 total = isl_basic_set_total_dim(graph->lp);
1281 for (i = 0; i < graph->n; ++i) {
1282 struct isl_sched_node *node = &graph->node[i];
1283 for (j = 0; j < 2 * node->nparam + 2 * node->nvar; ++j) {
1284 int dim;
1285 k = isl_basic_set_alloc_inequality(graph->lp);
1286 if (k < 0)
1287 return -1;
1288 dim = 1 + node->start + 1 + j;
1289 isl_seq_clr(graph->lp->ineq[k], 1 + total);
1290 isl_int_set_si(graph->lp->ineq[k][dim], -1);
1291 isl_int_set_si(graph->lp->ineq[k][0], max_coefficient);
1295 return 0;
1298 /* Construct an ILP problem for finding schedule coefficients
1299 * that result in non-negative, but small dependence distances
1300 * over all dependences.
1301 * In particular, the dependence distances over proximity edges
1302 * are bounded by m_0 + m_n n and we compute schedule coefficients
1303 * with small values (preferably zero) of m_n and m_0.
1305 * All variables of the ILP are non-negative. The actual coefficients
1306 * may be negative, so each coefficient is represented as the difference
1307 * of two non-negative variables. The negative part always appears
1308 * immediately before the positive part.
1309 * Other than that, the variables have the following order
1311 * - sum of positive and negative parts of m_n coefficients
1312 * - m_0
1313 * - sum of positive and negative parts of all c_n coefficients
1314 * (unconstrained when computing non-parametric schedules)
1315 * - sum of positive and negative parts of all c_x coefficients
1316 * - positive and negative parts of m_n coefficients
1317 * - for each node
1318 * - c_i_0
1319 * - positive and negative parts of c_i_n (if parametric)
1320 * - positive and negative parts of c_i_x
1322 * The c_i_x are not represented directly, but through the columns of
1323 * node->cmap. That is, the computed values are for variable t_i_x
1324 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
1326 * The constraints are those from the edges plus two or three equalities
1327 * to express the sums.
1329 * If force_zero is set, then we add equalities to ensure that
1330 * the sum of the m_n coefficients and m_0 are both zero.
1332 static int setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
1333 int force_zero)
1335 int i, j;
1336 int k;
1337 unsigned nparam;
1338 unsigned total;
1339 isl_space *dim;
1340 int parametric;
1341 int param_pos;
1342 int n_eq, n_ineq;
1343 int max_constant_term;
1345 max_constant_term = ctx->opt->schedule_max_constant_term;
1347 parametric = ctx->opt->schedule_parametric;
1348 nparam = isl_space_dim(graph->node[0].dim, isl_dim_param);
1349 param_pos = 4;
1350 total = param_pos + 2 * nparam;
1351 for (i = 0; i < graph->n; ++i) {
1352 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
1353 if (node_update_cmap(node) < 0)
1354 return -1;
1355 node->start = total;
1356 total += 1 + 2 * (node->nparam + node->nvar);
1359 if (count_constraints(graph, &n_eq, &n_ineq) < 0)
1360 return -1;
1361 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
1362 return -1;
1364 dim = isl_space_set_alloc(ctx, 0, total);
1365 isl_basic_set_free(graph->lp);
1366 n_eq += 2 + parametric + force_zero;
1367 if (max_constant_term != -1)
1368 n_ineq += graph->n;
1370 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
1372 k = isl_basic_set_alloc_equality(graph->lp);
1373 if (k < 0)
1374 return -1;
1375 isl_seq_clr(graph->lp->eq[k], 1 + total);
1376 if (!force_zero)
1377 isl_int_set_si(graph->lp->eq[k][1], -1);
1378 for (i = 0; i < 2 * nparam; ++i)
1379 isl_int_set_si(graph->lp->eq[k][1 + param_pos + i], 1);
1381 if (force_zero) {
1382 k = isl_basic_set_alloc_equality(graph->lp);
1383 if (k < 0)
1384 return -1;
1385 isl_seq_clr(graph->lp->eq[k], 1 + total);
1386 isl_int_set_si(graph->lp->eq[k][2], -1);
1389 if (parametric) {
1390 k = isl_basic_set_alloc_equality(graph->lp);
1391 if (k < 0)
1392 return -1;
1393 isl_seq_clr(graph->lp->eq[k], 1 + total);
1394 isl_int_set_si(graph->lp->eq[k][3], -1);
1395 for (i = 0; i < graph->n; ++i) {
1396 int pos = 1 + graph->node[i].start + 1;
1398 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
1399 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
1403 k = isl_basic_set_alloc_equality(graph->lp);
1404 if (k < 0)
1405 return -1;
1406 isl_seq_clr(graph->lp->eq[k], 1 + total);
1407 isl_int_set_si(graph->lp->eq[k][4], -1);
1408 for (i = 0; i < graph->n; ++i) {
1409 struct isl_sched_node *node = &graph->node[i];
1410 int pos = 1 + node->start + 1 + 2 * node->nparam;
1412 for (j = 0; j < 2 * node->nvar; ++j)
1413 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
1416 if (max_constant_term != -1)
1417 for (i = 0; i < graph->n; ++i) {
1418 struct isl_sched_node *node = &graph->node[i];
1419 k = isl_basic_set_alloc_inequality(graph->lp);
1420 if (k < 0)
1421 return -1;
1422 isl_seq_clr(graph->lp->ineq[k], 1 + total);
1423 isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
1424 isl_int_set_si(graph->lp->ineq[k][0], max_constant_term);
1427 if (add_bound_coefficient_constraints(ctx, graph) < 0)
1428 return -1;
1429 if (add_all_validity_constraints(graph) < 0)
1430 return -1;
1431 if (add_all_proximity_constraints(graph) < 0)
1432 return -1;
1434 return 0;
1437 /* Analyze the conflicting constraint found by
1438 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
1439 * constraint of one of the edges between distinct nodes, living, moreover
1440 * in distinct SCCs, then record the source and sink SCC as this may
1441 * be a good place to cut between SCCs.
1443 static int check_conflict(int con, void *user)
1445 int i;
1446 struct isl_sched_graph *graph = user;
1448 if (graph->src_scc >= 0)
1449 return 0;
1451 con -= graph->lp->n_eq;
1453 if (con >= graph->lp->n_ineq)
1454 return 0;
1456 for (i = 0; i < graph->n_edge; ++i) {
1457 if (!graph->edge[i].validity)
1458 continue;
1459 if (graph->edge[i].src == graph->edge[i].dst)
1460 continue;
1461 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
1462 continue;
1463 if (graph->edge[i].start > con)
1464 continue;
1465 if (graph->edge[i].end <= con)
1466 continue;
1467 graph->src_scc = graph->edge[i].src->scc;
1468 graph->dst_scc = graph->edge[i].dst->scc;
1471 return 0;
1474 /* Check whether the next schedule row of the given node needs to be
1475 * non-trivial. Lower-dimensional domains may have some trivial rows,
1476 * but as soon as the number of remaining required non-trivial rows
1477 * is as large as the number or remaining rows to be computed,
1478 * all remaining rows need to be non-trivial.
1480 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
1482 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
1485 /* Solve the ILP problem constructed in setup_lp.
1486 * For each node such that all the remaining rows of its schedule
1487 * need to be non-trivial, we construct a non-triviality region.
1488 * This region imposes that the next row is independent of previous rows.
1489 * In particular the coefficients c_i_x are represented by t_i_x
1490 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
1491 * its first columns span the rows of the previously computed part
1492 * of the schedule. The non-triviality region enforces that at least
1493 * one of the remaining components of t_i_x is non-zero, i.e.,
1494 * that the new schedule row depends on at least one of the remaining
1495 * columns of Q.
1497 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
1499 int i;
1500 isl_vec *sol;
1501 isl_basic_set *lp;
1503 for (i = 0; i < graph->n; ++i) {
1504 struct isl_sched_node *node = &graph->node[i];
1505 int skip = node->rank;
1506 graph->region[i].pos = node->start + 1 + 2*(node->nparam+skip);
1507 if (needs_row(graph, node))
1508 graph->region[i].len = 2 * (node->nvar - skip);
1509 else
1510 graph->region[i].len = 0;
1512 lp = isl_basic_set_copy(graph->lp);
1513 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
1514 graph->region, &check_conflict, graph);
1515 return sol;
1518 /* Update the schedules of all nodes based on the given solution
1519 * of the LP problem.
1520 * The new row is added to the current band.
1521 * All possibly negative coefficients are encoded as a difference
1522 * of two non-negative variables, so we need to perform the subtraction
1523 * here. Moreover, if use_cmap is set, then the solution does
1524 * not refer to the actual coefficients c_i_x, but instead to variables
1525 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
1526 * In this case, we then also need to perform this multiplication
1527 * to obtain the values of c_i_x.
1529 * If check_zero is set, then the first two coordinates of sol are
1530 * assumed to correspond to the dependence distance. If these two
1531 * coordinates are zero, then the corresponding scheduling dimension
1532 * is marked as being zero distance.
1534 static int update_schedule(struct isl_sched_graph *graph,
1535 __isl_take isl_vec *sol, int use_cmap, int check_zero)
1537 int i, j;
1538 int zero = 0;
1539 isl_vec *csol = NULL;
1541 if (!sol)
1542 goto error;
1543 if (sol->size == 0)
1544 isl_die(sol->ctx, isl_error_internal,
1545 "no solution found", goto error);
1546 if (graph->n_total_row >= graph->max_row)
1547 isl_die(sol->ctx, isl_error_internal,
1548 "too many schedule rows", goto error);
1550 if (check_zero)
1551 zero = isl_int_is_zero(sol->el[1]) &&
1552 isl_int_is_zero(sol->el[2]);
1554 for (i = 0; i < graph->n; ++i) {
1555 struct isl_sched_node *node = &graph->node[i];
1556 int pos = node->start;
1557 int row = isl_mat_rows(node->sched);
1559 isl_vec_free(csol);
1560 csol = isl_vec_alloc(sol->ctx, node->nvar);
1561 if (!csol)
1562 goto error;
1564 isl_map_free(node->sched_map);
1565 node->sched_map = NULL;
1566 node->sched = isl_mat_add_rows(node->sched, 1);
1567 if (!node->sched)
1568 goto error;
1569 node->sched = isl_mat_set_element(node->sched, row, 0,
1570 sol->el[1 + pos]);
1571 for (j = 0; j < node->nparam + node->nvar; ++j)
1572 isl_int_sub(sol->el[1 + pos + 1 + 2 * j + 1],
1573 sol->el[1 + pos + 1 + 2 * j + 1],
1574 sol->el[1 + pos + 1 + 2 * j]);
1575 for (j = 0; j < node->nparam; ++j)
1576 node->sched = isl_mat_set_element(node->sched,
1577 row, 1 + j, sol->el[1+pos+1+2*j+1]);
1578 for (j = 0; j < node->nvar; ++j)
1579 isl_int_set(csol->el[j],
1580 sol->el[1+pos+1+2*(node->nparam+j)+1]);
1581 if (use_cmap)
1582 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
1583 csol);
1584 if (!csol)
1585 goto error;
1586 for (j = 0; j < node->nvar; ++j)
1587 node->sched = isl_mat_set_element(node->sched,
1588 row, 1 + node->nparam + j, csol->el[j]);
1589 node->band[graph->n_total_row] = graph->n_band;
1590 node->zero[graph->n_total_row] = zero;
1592 isl_vec_free(sol);
1593 isl_vec_free(csol);
1595 graph->n_row++;
1596 graph->n_total_row++;
1598 return 0;
1599 error:
1600 isl_vec_free(sol);
1601 isl_vec_free(csol);
1602 return -1;
1605 /* Convert node->sched into a multi_aff and return this multi_aff.
1607 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
1608 struct isl_sched_node *node)
1610 int i, j;
1611 isl_space *space;
1612 isl_local_space *ls;
1613 isl_aff *aff;
1614 isl_multi_aff *ma;
1615 int nrow, ncol;
1616 isl_int v;
1618 nrow = isl_mat_rows(node->sched);
1619 ncol = isl_mat_cols(node->sched) - 1;
1620 space = isl_space_from_domain(isl_space_copy(node->dim));
1621 space = isl_space_add_dims(space, isl_dim_out, nrow);
1622 ma = isl_multi_aff_zero(space);
1623 ls = isl_local_space_from_space(isl_space_copy(node->dim));
1625 isl_int_init(v);
1627 for (i = 0; i < nrow; ++i) {
1628 aff = isl_aff_zero_on_domain(isl_local_space_copy(ls));
1629 isl_mat_get_element(node->sched, i, 0, &v);
1630 aff = isl_aff_set_constant(aff, v);
1631 for (j = 0; j < node->nparam; ++j) {
1632 isl_mat_get_element(node->sched, i, 1 + j, &v);
1633 aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
1635 for (j = 0; j < node->nvar; ++j) {
1636 isl_mat_get_element(node->sched,
1637 i, 1 + node->nparam + j, &v);
1638 aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
1640 ma = isl_multi_aff_set_aff(ma, i, aff);
1643 isl_int_clear(v);
1645 isl_local_space_free(ls);
1647 return ma;
1650 /* Convert node->sched into a map and return this map.
1652 * The result is cached in node->sched_map, which needs to be released
1653 * whenever node->sched is updated.
1655 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
1657 if (!node->sched_map) {
1658 isl_multi_aff *ma;
1660 ma = node_extract_schedule_multi_aff(node);
1661 node->sched_map = isl_map_from_multi_aff(ma);
1664 return isl_map_copy(node->sched_map);
1667 /* Update the given dependence relation based on the current schedule.
1668 * That is, intersect the dependence relation with a map expressing
1669 * that source and sink are executed within the same iteration of
1670 * the current schedule.
1671 * This is not the most efficient way, but this shouldn't be a critical
1672 * operation.
1674 static __isl_give isl_map *specialize(__isl_take isl_map *map,
1675 struct isl_sched_node *src, struct isl_sched_node *dst)
1677 isl_map *src_sched, *dst_sched, *id;
1679 src_sched = node_extract_schedule(src);
1680 dst_sched = node_extract_schedule(dst);
1681 id = isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
1682 return isl_map_intersect(map, id);
1685 /* Update the dependence relations of all edges based on the current schedule.
1686 * If a dependence is carried completely by the current schedule, then
1687 * it is removed from the edge_tables. It is kept in the list of edges
1688 * as otherwise all edge_tables would have to be recomputed.
1690 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
1692 int i;
1694 for (i = graph->n_edge - 1; i >= 0; --i) {
1695 struct isl_sched_edge *edge = &graph->edge[i];
1696 edge->map = specialize(edge->map, edge->src, edge->dst);
1697 if (!edge->map)
1698 return -1;
1700 if (isl_map_plain_is_empty(edge->map))
1701 graph_remove_edge(graph, edge);
1704 return 0;
1707 static void next_band(struct isl_sched_graph *graph)
1709 graph->band_start = graph->n_total_row;
1710 graph->n_band++;
1713 /* Topologically sort statements mapped to the same schedule iteration
1714 * and add a row to the schedule corresponding to this order.
1716 static int sort_statements(isl_ctx *ctx, struct isl_sched_graph *graph)
1718 int i, j;
1720 if (graph->n <= 1)
1721 return 0;
1723 if (update_edges(ctx, graph) < 0)
1724 return -1;
1726 if (graph->n_edge == 0)
1727 return 0;
1729 if (detect_sccs(ctx, graph) < 0)
1730 return -1;
1732 if (graph->n_total_row >= graph->max_row)
1733 isl_die(ctx, isl_error_internal,
1734 "too many schedule rows", return -1);
1736 for (i = 0; i < graph->n; ++i) {
1737 struct isl_sched_node *node = &graph->node[i];
1738 int row = isl_mat_rows(node->sched);
1739 int cols = isl_mat_cols(node->sched);
1741 isl_map_free(node->sched_map);
1742 node->sched_map = NULL;
1743 node->sched = isl_mat_add_rows(node->sched, 1);
1744 if (!node->sched)
1745 return -1;
1746 node->sched = isl_mat_set_element_si(node->sched, row, 0,
1747 node->scc);
1748 for (j = 1; j < cols; ++j)
1749 node->sched = isl_mat_set_element_si(node->sched,
1750 row, j, 0);
1751 node->band[graph->n_total_row] = graph->n_band;
1754 graph->n_total_row++;
1755 next_band(graph);
1757 return 0;
1760 /* Construct an isl_schedule based on the computed schedule stored
1761 * in graph and with parameters specified by dim.
1763 static __isl_give isl_schedule *extract_schedule(struct isl_sched_graph *graph,
1764 __isl_take isl_space *dim)
1766 int i;
1767 isl_ctx *ctx;
1768 isl_schedule *sched = NULL;
1770 if (!dim)
1771 return NULL;
1773 ctx = isl_space_get_ctx(dim);
1774 sched = isl_calloc(ctx, struct isl_schedule,
1775 sizeof(struct isl_schedule) +
1776 (graph->n - 1) * sizeof(struct isl_schedule_node));
1777 if (!sched)
1778 goto error;
1780 sched->ref = 1;
1781 sched->n = graph->n;
1782 sched->n_band = graph->n_band;
1783 sched->n_total_row = graph->n_total_row;
1785 for (i = 0; i < sched->n; ++i) {
1786 int r, b;
1787 int *band_end, *band_id, *zero;
1789 sched->node[i].sched =
1790 node_extract_schedule_multi_aff(&graph->node[i]);
1791 if (!sched->node[i].sched)
1792 goto error;
1794 sched->node[i].n_band = graph->n_band;
1795 if (graph->n_band == 0)
1796 continue;
1798 band_end = isl_alloc_array(ctx, int, graph->n_band);
1799 band_id = isl_alloc_array(ctx, int, graph->n_band);
1800 zero = isl_alloc_array(ctx, int, graph->n_total_row);
1801 sched->node[i].band_end = band_end;
1802 sched->node[i].band_id = band_id;
1803 sched->node[i].zero = zero;
1804 if (!band_end || !band_id || !zero)
1805 goto error;
1807 for (r = 0; r < graph->n_total_row; ++r)
1808 zero[r] = graph->node[i].zero[r];
1809 for (r = b = 0; r < graph->n_total_row; ++r) {
1810 if (graph->node[i].band[r] == b)
1811 continue;
1812 band_end[b++] = r;
1813 if (graph->node[i].band[r] == -1)
1814 break;
1816 if (r == graph->n_total_row)
1817 band_end[b++] = r;
1818 sched->node[i].n_band = b;
1819 for (--b; b >= 0; --b)
1820 band_id[b] = graph->node[i].band_id[b];
1823 sched->dim = dim;
1825 return sched;
1826 error:
1827 isl_space_free(dim);
1828 isl_schedule_free(sched);
1829 return NULL;
1832 /* Copy nodes that satisfy node_pred from the src dependence graph
1833 * to the dst dependence graph.
1835 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
1836 int (*node_pred)(struct isl_sched_node *node, int data), int data)
1838 int i;
1840 dst->n = 0;
1841 for (i = 0; i < src->n; ++i) {
1842 if (!node_pred(&src->node[i], data))
1843 continue;
1844 dst->node[dst->n].dim = isl_space_copy(src->node[i].dim);
1845 dst->node[dst->n].nvar = src->node[i].nvar;
1846 dst->node[dst->n].nparam = src->node[i].nparam;
1847 dst->node[dst->n].sched = isl_mat_copy(src->node[i].sched);
1848 dst->node[dst->n].sched_map =
1849 isl_map_copy(src->node[i].sched_map);
1850 dst->node[dst->n].band = src->node[i].band;
1851 dst->node[dst->n].band_id = src->node[i].band_id;
1852 dst->node[dst->n].zero = src->node[i].zero;
1853 dst->n++;
1856 return 0;
1859 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
1860 * to the dst dependence graph.
1861 * If the source or destination node of the edge is not in the destination
1862 * graph, then it must be a backward proximity edge and it should simply
1863 * be ignored.
1865 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
1866 struct isl_sched_graph *src,
1867 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
1869 int i;
1870 enum isl_edge_type t;
1872 dst->n_edge = 0;
1873 for (i = 0; i < src->n_edge; ++i) {
1874 struct isl_sched_edge *edge = &src->edge[i];
1875 isl_map *map;
1876 struct isl_sched_node *dst_src, *dst_dst;
1878 if (!edge_pred(edge, data))
1879 continue;
1881 if (isl_map_plain_is_empty(edge->map))
1882 continue;
1884 dst_src = graph_find_node(ctx, dst, edge->src->dim);
1885 dst_dst = graph_find_node(ctx, dst, edge->dst->dim);
1886 if (!dst_src || !dst_dst) {
1887 if (edge->validity)
1888 isl_die(ctx, isl_error_internal,
1889 "backward validity edge", return -1);
1890 continue;
1893 map = isl_map_copy(edge->map);
1895 dst->edge[dst->n_edge].src = dst_src;
1896 dst->edge[dst->n_edge].dst = dst_dst;
1897 dst->edge[dst->n_edge].map = map;
1898 dst->edge[dst->n_edge].validity = edge->validity;
1899 dst->edge[dst->n_edge].proximity = edge->proximity;
1900 dst->n_edge++;
1902 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
1903 if (edge !=
1904 graph_find_edge(src, t, edge->src, edge->dst))
1905 continue;
1906 if (graph_edge_table_add(ctx, dst, t,
1907 &dst->edge[dst->n_edge - 1]) < 0)
1908 return -1;
1912 return 0;
1915 /* Given a "src" dependence graph that contains the nodes from "dst"
1916 * that satisfy node_pred, copy the schedule computed in "src"
1917 * for those nodes back to "dst".
1919 static int copy_schedule(struct isl_sched_graph *dst,
1920 struct isl_sched_graph *src,
1921 int (*node_pred)(struct isl_sched_node *node, int data), int data)
1923 int i;
1925 src->n = 0;
1926 for (i = 0; i < dst->n; ++i) {
1927 if (!node_pred(&dst->node[i], data))
1928 continue;
1929 isl_mat_free(dst->node[i].sched);
1930 isl_map_free(dst->node[i].sched_map);
1931 dst->node[i].sched = isl_mat_copy(src->node[src->n].sched);
1932 dst->node[i].sched_map =
1933 isl_map_copy(src->node[src->n].sched_map);
1934 src->n++;
1937 dst->max_row = src->max_row;
1938 dst->n_total_row = src->n_total_row;
1939 dst->n_band = src->n_band;
1941 return 0;
1944 /* Compute the maximal number of variables over all nodes.
1945 * This is the maximal number of linearly independent schedule
1946 * rows that we need to compute.
1947 * Just in case we end up in a part of the dependence graph
1948 * with only lower-dimensional domains, we make sure we will
1949 * compute the required amount of extra linearly independent rows.
1951 static int compute_maxvar(struct isl_sched_graph *graph)
1953 int i;
1955 graph->maxvar = 0;
1956 for (i = 0; i < graph->n; ++i) {
1957 struct isl_sched_node *node = &graph->node[i];
1958 int nvar;
1960 if (node_update_cmap(node) < 0)
1961 return -1;
1962 nvar = node->nvar + graph->n_row - node->rank;
1963 if (nvar > graph->maxvar)
1964 graph->maxvar = nvar;
1967 return 0;
1970 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph);
1971 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph);
1973 /* Compute a schedule for a subgraph of "graph". In particular, for
1974 * the graph composed of nodes that satisfy node_pred and edges that
1975 * that satisfy edge_pred. The caller should precompute the number
1976 * of nodes and edges that satisfy these predicates and pass them along
1977 * as "n" and "n_edge".
1978 * If the subgraph is known to consist of a single component, then wcc should
1979 * be set and then we call compute_schedule_wcc on the constructed subgraph.
1980 * Otherwise, we call compute_schedule, which will check whether the subgraph
1981 * is connected.
1983 static int compute_sub_schedule(isl_ctx *ctx,
1984 struct isl_sched_graph *graph, int n, int n_edge,
1985 int (*node_pred)(struct isl_sched_node *node, int data),
1986 int (*edge_pred)(struct isl_sched_edge *edge, int data),
1987 int data, int wcc)
1989 struct isl_sched_graph split = { 0 };
1990 int t;
1992 if (graph_alloc(ctx, &split, n, n_edge) < 0)
1993 goto error;
1994 if (copy_nodes(&split, graph, node_pred, data) < 0)
1995 goto error;
1996 if (graph_init_table(ctx, &split) < 0)
1997 goto error;
1998 for (t = 0; t <= isl_edge_last; ++t)
1999 split.max_edge[t] = graph->max_edge[t];
2000 if (graph_init_edge_tables(ctx, &split) < 0)
2001 goto error;
2002 if (copy_edges(ctx, &split, graph, edge_pred, data) < 0)
2003 goto error;
2004 split.n_row = graph->n_row;
2005 split.max_row = graph->max_row;
2006 split.n_total_row = graph->n_total_row;
2007 split.n_band = graph->n_band;
2008 split.band_start = graph->band_start;
2010 if (wcc && compute_schedule_wcc(ctx, &split) < 0)
2011 goto error;
2012 if (!wcc && compute_schedule(ctx, &split) < 0)
2013 goto error;
2015 copy_schedule(graph, &split, node_pred, data);
2017 graph_free(ctx, &split);
2018 return 0;
2019 error:
2020 graph_free(ctx, &split);
2021 return -1;
2024 static int node_scc_exactly(struct isl_sched_node *node, int scc)
2026 return node->scc == scc;
2029 static int node_scc_at_most(struct isl_sched_node *node, int scc)
2031 return node->scc <= scc;
2034 static int node_scc_at_least(struct isl_sched_node *node, int scc)
2036 return node->scc >= scc;
2039 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
2041 return edge->src->scc == scc && edge->dst->scc == scc;
2044 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
2046 return edge->dst->scc <= scc;
2049 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
2051 return edge->src->scc >= scc;
2054 /* Pad the schedules of all nodes with zero rows such that in the end
2055 * they all have graph->n_total_row rows.
2056 * The extra rows don't belong to any band, so they get assigned band number -1.
2058 static int pad_schedule(struct isl_sched_graph *graph)
2060 int i, j;
2062 for (i = 0; i < graph->n; ++i) {
2063 struct isl_sched_node *node = &graph->node[i];
2064 int row = isl_mat_rows(node->sched);
2065 if (graph->n_total_row > row) {
2066 isl_map_free(node->sched_map);
2067 node->sched_map = NULL;
2069 node->sched = isl_mat_add_zero_rows(node->sched,
2070 graph->n_total_row - row);
2071 if (!node->sched)
2072 return -1;
2073 for (j = row; j < graph->n_total_row; ++j)
2074 node->band[j] = -1;
2077 return 0;
2080 /* Split the current graph into two parts and compute a schedule for each
2081 * part individually. In particular, one part consists of all SCCs up
2082 * to and including graph->src_scc, while the other part contains the other
2083 * SCCS.
2085 * The split is enforced in the schedule by constant rows with two different
2086 * values (0 and 1). These constant rows replace the previously computed rows
2087 * in the current band.
2088 * It would be possible to reuse them as the first rows in the next
2089 * band, but recomputing them may result in better rows as we are looking
2090 * at a smaller part of the dependence graph.
2091 * compute_split_schedule is only called when no zero-distance schedule row
2092 * could be found on the entire graph, so we wark the splitting row as
2093 * non zero-distance.
2095 * The band_id of the second group is set to n, where n is the number
2096 * of nodes in the first group. This ensures that the band_ids over
2097 * the two groups remain disjoint, even if either or both of the two
2098 * groups contain independent components.
2100 static int compute_split_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
2102 int i, j, n, e1, e2;
2103 int n_total_row, orig_total_row;
2104 int n_band, orig_band;
2105 int drop;
2107 if (graph->n_total_row >= graph->max_row)
2108 isl_die(ctx, isl_error_internal,
2109 "too many schedule rows", return -1);
2111 drop = graph->n_total_row - graph->band_start;
2112 graph->n_total_row -= drop;
2113 graph->n_row -= drop;
2115 n = 0;
2116 for (i = 0; i < graph->n; ++i) {
2117 struct isl_sched_node *node = &graph->node[i];
2118 int row = isl_mat_rows(node->sched) - drop;
2119 int cols = isl_mat_cols(node->sched);
2120 int before = node->scc <= graph->src_scc;
2122 if (before)
2123 n++;
2125 isl_map_free(node->sched_map);
2126 node->sched_map = NULL;
2127 node->sched = isl_mat_drop_rows(node->sched,
2128 graph->band_start, drop);
2129 node->sched = isl_mat_add_rows(node->sched, 1);
2130 if (!node->sched)
2131 return -1;
2132 node->sched = isl_mat_set_element_si(node->sched, row, 0,
2133 !before);
2134 for (j = 1; j < cols; ++j)
2135 node->sched = isl_mat_set_element_si(node->sched,
2136 row, j, 0);
2137 node->band[graph->n_total_row] = graph->n_band;
2138 node->zero[graph->n_total_row] = 0;
2141 e1 = e2 = 0;
2142 for (i = 0; i < graph->n_edge; ++i) {
2143 if (graph->edge[i].dst->scc <= graph->src_scc)
2144 e1++;
2145 if (graph->edge[i].src->scc > graph->src_scc)
2146 e2++;
2149 graph->n_total_row++;
2150 next_band(graph);
2152 for (i = 0; i < graph->n; ++i) {
2153 struct isl_sched_node *node = &graph->node[i];
2154 if (node->scc > graph->src_scc)
2155 node->band_id[graph->n_band] = n;
2158 orig_total_row = graph->n_total_row;
2159 orig_band = graph->n_band;
2160 if (compute_sub_schedule(ctx, graph, n, e1,
2161 &node_scc_at_most, &edge_dst_scc_at_most,
2162 graph->src_scc, 0) < 0)
2163 return -1;
2164 n_total_row = graph->n_total_row;
2165 graph->n_total_row = orig_total_row;
2166 n_band = graph->n_band;
2167 graph->n_band = orig_band;
2168 if (compute_sub_schedule(ctx, graph, graph->n - n, e2,
2169 &node_scc_at_least, &edge_src_scc_at_least,
2170 graph->src_scc + 1, 0) < 0)
2171 return -1;
2172 if (n_total_row > graph->n_total_row)
2173 graph->n_total_row = n_total_row;
2174 if (n_band > graph->n_band)
2175 graph->n_band = n_band;
2177 return pad_schedule(graph);
2180 /* Compute the next band of the schedule after updating the dependence
2181 * relations based on the the current schedule.
2183 static int compute_next_band(isl_ctx *ctx, struct isl_sched_graph *graph)
2185 if (update_edges(ctx, graph) < 0)
2186 return -1;
2187 next_band(graph);
2189 return compute_schedule(ctx, graph);
2192 /* Add constraints to graph->lp that force the dependence "map" (which
2193 * is part of the dependence relation of "edge")
2194 * to be respected and attempt to carry it, where the edge is one from
2195 * a node j to itself. "pos" is the sequence number of the given map.
2196 * That is, add constraints that enforce
2198 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
2199 * = c_j_x (y - x) >= e_i
2201 * for each (x,y) in R.
2202 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2203 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
2204 * with each coefficient in c_j_x represented as a pair of non-negative
2205 * coefficients.
2207 static int add_intra_constraints(struct isl_sched_graph *graph,
2208 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
2210 unsigned total;
2211 isl_ctx *ctx = isl_map_get_ctx(map);
2212 isl_space *dim;
2213 isl_dim_map *dim_map;
2214 isl_basic_set *coef;
2215 struct isl_sched_node *node = edge->src;
2217 coef = intra_coefficients(graph, map);
2218 if (!coef)
2219 return -1;
2221 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
2223 total = isl_basic_set_total_dim(graph->lp);
2224 dim_map = isl_dim_map_alloc(ctx, total);
2225 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
2226 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
2227 isl_space_dim(dim, isl_dim_set), 1,
2228 node->nvar, -1);
2229 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
2230 isl_space_dim(dim, isl_dim_set), 1,
2231 node->nvar, 1);
2232 graph->lp = isl_basic_set_extend_constraints(graph->lp,
2233 coef->n_eq, coef->n_ineq);
2234 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
2235 coef, dim_map);
2236 isl_space_free(dim);
2238 return 0;
2241 /* Add constraints to graph->lp that force the dependence "map" (which
2242 * is part of the dependence relation of "edge")
2243 * to be respected and attempt to carry it, where the edge is one from
2244 * node j to node k. "pos" is the sequence number of the given map.
2245 * That is, add constraints that enforce
2247 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
2249 * for each (x,y) in R.
2250 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2251 * of valid constraints for R and then plug in
2252 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
2253 * with each coefficient (except e_i, c_k_0 and c_j_0)
2254 * represented as a pair of non-negative coefficients.
2256 static int add_inter_constraints(struct isl_sched_graph *graph,
2257 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
2259 unsigned total;
2260 isl_ctx *ctx = isl_map_get_ctx(map);
2261 isl_space *dim;
2262 isl_dim_map *dim_map;
2263 isl_basic_set *coef;
2264 struct isl_sched_node *src = edge->src;
2265 struct isl_sched_node *dst = edge->dst;
2267 coef = inter_coefficients(graph, map);
2268 if (!coef)
2269 return -1;
2271 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
2273 total = isl_basic_set_total_dim(graph->lp);
2274 dim_map = isl_dim_map_alloc(ctx, total);
2276 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
2278 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
2279 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
2280 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
2281 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
2282 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
2283 dst->nvar, -1);
2284 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
2285 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
2286 dst->nvar, 1);
2288 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
2289 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
2290 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
2291 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
2292 isl_space_dim(dim, isl_dim_set), 1,
2293 src->nvar, 1);
2294 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
2295 isl_space_dim(dim, isl_dim_set), 1,
2296 src->nvar, -1);
2298 graph->lp = isl_basic_set_extend_constraints(graph->lp,
2299 coef->n_eq, coef->n_ineq);
2300 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
2301 coef, dim_map);
2302 isl_space_free(dim);
2304 return 0;
2307 /* Add constraints to graph->lp that force all validity dependences
2308 * to be respected and attempt to carry them.
2310 static int add_all_constraints(struct isl_sched_graph *graph)
2312 int i, j;
2313 int pos;
2315 pos = 0;
2316 for (i = 0; i < graph->n_edge; ++i) {
2317 struct isl_sched_edge *edge= &graph->edge[i];
2319 if (!edge->validity)
2320 continue;
2322 for (j = 0; j < edge->map->n; ++j) {
2323 isl_basic_map *bmap;
2324 isl_map *map;
2326 bmap = isl_basic_map_copy(edge->map->p[j]);
2327 map = isl_map_from_basic_map(bmap);
2329 if (edge->src == edge->dst &&
2330 add_intra_constraints(graph, edge, map, pos) < 0)
2331 return -1;
2332 if (edge->src != edge->dst &&
2333 add_inter_constraints(graph, edge, map, pos) < 0)
2334 return -1;
2335 ++pos;
2339 return 0;
2342 /* Count the number of equality and inequality constraints
2343 * that will be added to the carry_lp problem.
2344 * We count each edge exactly once.
2346 static int count_all_constraints(struct isl_sched_graph *graph,
2347 int *n_eq, int *n_ineq)
2349 int i, j;
2351 *n_eq = *n_ineq = 0;
2352 for (i = 0; i < graph->n_edge; ++i) {
2353 struct isl_sched_edge *edge= &graph->edge[i];
2354 for (j = 0; j < edge->map->n; ++j) {
2355 isl_basic_map *bmap;
2356 isl_map *map;
2358 bmap = isl_basic_map_copy(edge->map->p[j]);
2359 map = isl_map_from_basic_map(bmap);
2361 if (count_map_constraints(graph, edge, map,
2362 n_eq, n_ineq, 1) < 0)
2363 return -1;
2367 return 0;
2370 /* Construct an LP problem for finding schedule coefficients
2371 * such that the schedule carries as many dependences as possible.
2372 * In particular, for each dependence i, we bound the dependence distance
2373 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
2374 * of all e_i's. Dependence with e_i = 0 in the solution are simply
2375 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
2376 * Note that if the dependence relation is a union of basic maps,
2377 * then we have to consider each basic map individually as it may only
2378 * be possible to carry the dependences expressed by some of those
2379 * basic maps and not all off them.
2380 * Below, we consider each of those basic maps as a separate "edge".
2382 * All variables of the LP are non-negative. The actual coefficients
2383 * may be negative, so each coefficient is represented as the difference
2384 * of two non-negative variables. The negative part always appears
2385 * immediately before the positive part.
2386 * Other than that, the variables have the following order
2388 * - sum of (1 - e_i) over all edges
2389 * - sum of positive and negative parts of all c_n coefficients
2390 * (unconstrained when computing non-parametric schedules)
2391 * - sum of positive and negative parts of all c_x coefficients
2392 * - for each edge
2393 * - e_i
2394 * - for each node
2395 * - c_i_0
2396 * - positive and negative parts of c_i_n (if parametric)
2397 * - positive and negative parts of c_i_x
2399 * The constraints are those from the (validity) edges plus three equalities
2400 * to express the sums and n_edge inequalities to express e_i <= 1.
2402 static int setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
2404 int i, j;
2405 int k;
2406 isl_space *dim;
2407 unsigned total;
2408 int n_eq, n_ineq;
2409 int n_edge;
2411 n_edge = 0;
2412 for (i = 0; i < graph->n_edge; ++i)
2413 n_edge += graph->edge[i].map->n;
2415 total = 3 + n_edge;
2416 for (i = 0; i < graph->n; ++i) {
2417 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2418 node->start = total;
2419 total += 1 + 2 * (node->nparam + node->nvar);
2422 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
2423 return -1;
2424 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
2425 return -1;
2427 dim = isl_space_set_alloc(ctx, 0, total);
2428 isl_basic_set_free(graph->lp);
2429 n_eq += 3;
2430 n_ineq += n_edge;
2431 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
2432 graph->lp = isl_basic_set_set_rational(graph->lp);
2434 k = isl_basic_set_alloc_equality(graph->lp);
2435 if (k < 0)
2436 return -1;
2437 isl_seq_clr(graph->lp->eq[k], 1 + total);
2438 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
2439 isl_int_set_si(graph->lp->eq[k][1], 1);
2440 for (i = 0; i < n_edge; ++i)
2441 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
2443 k = isl_basic_set_alloc_equality(graph->lp);
2444 if (k < 0)
2445 return -1;
2446 isl_seq_clr(graph->lp->eq[k], 1 + total);
2447 isl_int_set_si(graph->lp->eq[k][2], -1);
2448 for (i = 0; i < graph->n; ++i) {
2449 int pos = 1 + graph->node[i].start + 1;
2451 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
2452 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2455 k = isl_basic_set_alloc_equality(graph->lp);
2456 if (k < 0)
2457 return -1;
2458 isl_seq_clr(graph->lp->eq[k], 1 + total);
2459 isl_int_set_si(graph->lp->eq[k][3], -1);
2460 for (i = 0; i < graph->n; ++i) {
2461 struct isl_sched_node *node = &graph->node[i];
2462 int pos = 1 + node->start + 1 + 2 * node->nparam;
2464 for (j = 0; j < 2 * node->nvar; ++j)
2465 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2468 for (i = 0; i < n_edge; ++i) {
2469 k = isl_basic_set_alloc_inequality(graph->lp);
2470 if (k < 0)
2471 return -1;
2472 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2473 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
2474 isl_int_set_si(graph->lp->ineq[k][0], 1);
2477 if (add_bound_coefficient_constraints(ctx, graph) < 0)
2478 return -1;
2479 if (add_all_constraints(graph) < 0)
2480 return -1;
2482 return 0;
2485 /* If the schedule_split_scaled option is set and if the linear
2486 * parts of the scheduling rows for all nodes in the graphs have
2487 * non-trivial common divisor, then split off the constant term
2488 * from the linear part.
2489 * The constant term is then placed in a separate band and
2490 * the linear part is reduced.
2492 static int split_scaled(isl_ctx *ctx, struct isl_sched_graph *graph)
2494 int i;
2495 int row;
2496 isl_int gcd, gcd_i;
2498 if (!ctx->opt->schedule_split_scaled)
2499 return 0;
2500 if (graph->n <= 1)
2501 return 0;
2503 if (graph->n_total_row >= graph->max_row)
2504 isl_die(ctx, isl_error_internal,
2505 "too many schedule rows", return -1);
2507 isl_int_init(gcd);
2508 isl_int_init(gcd_i);
2510 isl_int_set_si(gcd, 0);
2512 row = isl_mat_rows(graph->node[0].sched) - 1;
2514 for (i = 0; i < graph->n; ++i) {
2515 struct isl_sched_node *node = &graph->node[i];
2516 int cols = isl_mat_cols(node->sched);
2518 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
2519 isl_int_gcd(gcd, gcd, gcd_i);
2522 isl_int_clear(gcd_i);
2524 if (isl_int_cmp_si(gcd, 1) <= 0) {
2525 isl_int_clear(gcd);
2526 return 0;
2529 next_band(graph);
2531 for (i = 0; i < graph->n; ++i) {
2532 struct isl_sched_node *node = &graph->node[i];
2534 isl_map_free(node->sched_map);
2535 node->sched_map = NULL;
2536 node->sched = isl_mat_add_zero_rows(node->sched, 1);
2537 if (!node->sched)
2538 goto error;
2539 isl_int_fdiv_r(node->sched->row[row + 1][0],
2540 node->sched->row[row][0], gcd);
2541 isl_int_fdiv_q(node->sched->row[row][0],
2542 node->sched->row[row][0], gcd);
2543 isl_int_mul(node->sched->row[row][0],
2544 node->sched->row[row][0], gcd);
2545 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
2546 if (!node->sched)
2547 goto error;
2548 node->band[graph->n_total_row] = graph->n_band;
2551 graph->n_total_row++;
2553 isl_int_clear(gcd);
2554 return 0;
2555 error:
2556 isl_int_clear(gcd);
2557 return -1;
2560 static int compute_component_schedule(isl_ctx *ctx,
2561 struct isl_sched_graph *graph);
2563 /* Is the schedule row "sol" trivial on node "node"?
2564 * That is, is the solution zero on the dimensions orthogonal to
2565 * the previously found solutions?
2566 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
2568 * Each coefficient is represented as the difference between
2569 * two non-negative values in "sol". "sol" has been computed
2570 * in terms of the original iterators (i.e., without use of cmap).
2571 * We construct the schedule row s and write it as a linear
2572 * combination of (linear combinations of) previously computed schedule rows.
2573 * s = Q c or c = U s.
2574 * If the final entries of c are all zero, then the solution is trivial.
2576 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
2578 int i;
2579 int pos;
2580 int trivial;
2581 isl_ctx *ctx;
2582 isl_vec *node_sol;
2584 if (!sol)
2585 return -1;
2586 if (node->nvar == node->rank)
2587 return 0;
2589 ctx = isl_vec_get_ctx(sol);
2590 node_sol = isl_vec_alloc(ctx, node->nvar);
2591 if (!node_sol)
2592 return -1;
2594 pos = 1 + node->start + 1 + 2 * node->nparam;
2596 for (i = 0; i < node->nvar; ++i)
2597 isl_int_sub(node_sol->el[i],
2598 sol->el[pos + 2 * i + 1], sol->el[pos + 2 * i]);
2600 node_sol = isl_mat_vec_product(isl_mat_copy(node->cinv), node_sol);
2602 if (!node_sol)
2603 return -1;
2605 trivial = isl_seq_first_non_zero(node_sol->el + node->rank,
2606 node->nvar - node->rank) == -1;
2608 isl_vec_free(node_sol);
2610 return trivial;
2613 /* Is the schedule row "sol" trivial on any node where it should
2614 * not be trivial?
2615 * "sol" has been computed in terms of the original iterators
2616 * (i.e., without use of cmap).
2617 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
2619 static int is_any_trivial(struct isl_sched_graph *graph,
2620 __isl_keep isl_vec *sol)
2622 int i;
2624 for (i = 0; i < graph->n; ++i) {
2625 struct isl_sched_node *node = &graph->node[i];
2626 int trivial;
2628 if (!needs_row(graph, node))
2629 continue;
2630 trivial = is_trivial(node, sol);
2631 if (trivial < 0 || trivial)
2632 return trivial;
2635 return 0;
2638 /* Construct a schedule row for each node such that as many dependences
2639 * as possible are carried and then continue with the next band.
2641 * If the computed schedule row turns out to be trivial on one or
2642 * more nodes where it should not be trivial, then we throw it away
2643 * and try again on each component separately.
2645 static int carry_dependences(isl_ctx *ctx, struct isl_sched_graph *graph)
2647 int i;
2648 int n_edge;
2649 int trivial;
2650 isl_vec *sol;
2651 isl_basic_set *lp;
2653 n_edge = 0;
2654 for (i = 0; i < graph->n_edge; ++i)
2655 n_edge += graph->edge[i].map->n;
2657 if (setup_carry_lp(ctx, graph) < 0)
2658 return -1;
2660 lp = isl_basic_set_copy(graph->lp);
2661 sol = isl_tab_basic_set_non_neg_lexmin(lp);
2662 if (!sol)
2663 return -1;
2665 if (sol->size == 0) {
2666 isl_vec_free(sol);
2667 isl_die(ctx, isl_error_internal,
2668 "error in schedule construction", return -1);
2671 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
2672 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
2673 isl_vec_free(sol);
2674 isl_die(ctx, isl_error_unknown,
2675 "unable to carry dependences", return -1);
2678 trivial = is_any_trivial(graph, sol);
2679 if (trivial < 0) {
2680 sol = isl_vec_free(sol);
2681 } else if (trivial) {
2682 isl_vec_free(sol);
2683 if (graph->scc > 1)
2684 return compute_component_schedule(ctx, graph);
2685 isl_die(ctx, isl_error_unknown,
2686 "unable to construct non-trivial solution", return -1);
2689 if (update_schedule(graph, sol, 0, 0) < 0)
2690 return -1;
2692 if (split_scaled(ctx, graph) < 0)
2693 return -1;
2695 return compute_next_band(ctx, graph);
2698 /* Are there any (non-empty) validity edges in the graph?
2700 static int has_validity_edges(struct isl_sched_graph *graph)
2702 int i;
2704 for (i = 0; i < graph->n_edge; ++i) {
2705 int empty;
2707 empty = isl_map_plain_is_empty(graph->edge[i].map);
2708 if (empty < 0)
2709 return -1;
2710 if (empty)
2711 continue;
2712 if (graph->edge[i].validity)
2713 return 1;
2716 return 0;
2719 /* Should we apply a Feautrier step?
2720 * That is, did the user request the Feautrier algorithm and are
2721 * there any validity dependences (left)?
2723 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
2725 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
2726 return 0;
2728 return has_validity_edges(graph);
2731 /* Compute a schedule for a connected dependence graph using Feautrier's
2732 * multi-dimensional scheduling algorithm.
2733 * The original algorithm is described in [1].
2734 * The main idea is to minimize the number of scheduling dimensions, by
2735 * trying to satisfy as many dependences as possible per scheduling dimension.
2737 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
2738 * Problem, Part II: Multi-Dimensional Time.
2739 * In Intl. Journal of Parallel Programming, 1992.
2741 static int compute_schedule_wcc_feautrier(isl_ctx *ctx,
2742 struct isl_sched_graph *graph)
2744 return carry_dependences(ctx, graph);
2747 /* Compute a schedule for a connected dependence graph.
2748 * We try to find a sequence of as many schedule rows as possible that result
2749 * in non-negative dependence distances (independent of the previous rows
2750 * in the sequence, i.e., such that the sequence is tilable).
2751 * If we can't find any more rows we either
2752 * - split between SCCs and start over (assuming we found an interesting
2753 * pair of SCCs between which to split)
2754 * - continue with the next band (assuming the current band has at least
2755 * one row)
2756 * - try to carry as many dependences as possible and continue with the next
2757 * band
2759 * If Feautrier's algorithm is selected, we first recursively try to satisfy
2760 * as many validity dependences as possible. When all validity dependences
2761 * are satisfied we extend the schedule to a full-dimensional schedule.
2763 * If we manage to complete the schedule, we finish off by topologically
2764 * sorting the statements based on the remaining dependences.
2766 * If ctx->opt->schedule_outer_zero_distance is set, then we force the
2767 * outermost dimension in the current band to be zero distance. If this
2768 * turns out to be impossible, we fall back on the general scheme above
2769 * and try to carry as many dependences as possible.
2771 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph)
2773 int force_zero = 0;
2775 if (detect_sccs(ctx, graph) < 0)
2776 return -1;
2777 if (sort_sccs(graph) < 0)
2778 return -1;
2780 if (compute_maxvar(graph) < 0)
2781 return -1;
2783 if (need_feautrier_step(ctx, graph))
2784 return compute_schedule_wcc_feautrier(ctx, graph);
2786 if (ctx->opt->schedule_outer_zero_distance)
2787 force_zero = 1;
2789 while (graph->n_row < graph->maxvar) {
2790 isl_vec *sol;
2792 graph->src_scc = -1;
2793 graph->dst_scc = -1;
2795 if (setup_lp(ctx, graph, force_zero) < 0)
2796 return -1;
2797 sol = solve_lp(graph);
2798 if (!sol)
2799 return -1;
2800 if (sol->size == 0) {
2801 isl_vec_free(sol);
2802 if (!ctx->opt->schedule_maximize_band_depth &&
2803 graph->n_total_row > graph->band_start)
2804 return compute_next_band(ctx, graph);
2805 if (graph->src_scc >= 0)
2806 return compute_split_schedule(ctx, graph);
2807 if (graph->n_total_row > graph->band_start)
2808 return compute_next_band(ctx, graph);
2809 return carry_dependences(ctx, graph);
2811 if (update_schedule(graph, sol, 1, 1) < 0)
2812 return -1;
2813 force_zero = 0;
2816 if (graph->n_total_row > graph->band_start)
2817 next_band(graph);
2818 return sort_statements(ctx, graph);
2821 /* Add a row to the schedules that separates the SCCs and move
2822 * to the next band.
2824 static int split_on_scc(isl_ctx *ctx, struct isl_sched_graph *graph)
2826 int i;
2828 if (graph->n_total_row >= graph->max_row)
2829 isl_die(ctx, isl_error_internal,
2830 "too many schedule rows", return -1);
2832 for (i = 0; i < graph->n; ++i) {
2833 struct isl_sched_node *node = &graph->node[i];
2834 int row = isl_mat_rows(node->sched);
2836 isl_map_free(node->sched_map);
2837 node->sched_map = NULL;
2838 node->sched = isl_mat_add_zero_rows(node->sched, 1);
2839 node->sched = isl_mat_set_element_si(node->sched, row, 0,
2840 node->scc);
2841 if (!node->sched)
2842 return -1;
2843 node->band[graph->n_total_row] = graph->n_band;
2846 graph->n_total_row++;
2847 next_band(graph);
2849 return 0;
2852 /* Compute a schedule for each component (identified by node->scc)
2853 * of the dependence graph separately and then combine the results.
2854 * Depending on the setting of schedule_fuse, a component may be
2855 * either weakly or strongly connected.
2857 * The band_id is adjusted such that each component has a separate id.
2858 * Note that the band_id may have already been set to a value different
2859 * from zero by compute_split_schedule.
2861 static int compute_component_schedule(isl_ctx *ctx,
2862 struct isl_sched_graph *graph)
2864 int wcc, i;
2865 int n, n_edge;
2866 int n_total_row, orig_total_row;
2867 int n_band, orig_band;
2869 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN ||
2870 ctx->opt->schedule_separate_components)
2871 if (split_on_scc(ctx, graph) < 0)
2872 return -1;
2874 n_total_row = 0;
2875 orig_total_row = graph->n_total_row;
2876 n_band = 0;
2877 orig_band = graph->n_band;
2878 for (i = 0; i < graph->n; ++i)
2879 graph->node[i].band_id[graph->n_band] += graph->node[i].scc;
2880 for (wcc = 0; wcc < graph->scc; ++wcc) {
2881 n = 0;
2882 for (i = 0; i < graph->n; ++i)
2883 if (graph->node[i].scc == wcc)
2884 n++;
2885 n_edge = 0;
2886 for (i = 0; i < graph->n_edge; ++i)
2887 if (graph->edge[i].src->scc == wcc &&
2888 graph->edge[i].dst->scc == wcc)
2889 n_edge++;
2891 if (compute_sub_schedule(ctx, graph, n, n_edge,
2892 &node_scc_exactly,
2893 &edge_scc_exactly, wcc, 1) < 0)
2894 return -1;
2895 if (graph->n_total_row > n_total_row)
2896 n_total_row = graph->n_total_row;
2897 graph->n_total_row = orig_total_row;
2898 if (graph->n_band > n_band)
2899 n_band = graph->n_band;
2900 graph->n_band = orig_band;
2903 graph->n_total_row = n_total_row;
2904 graph->n_band = n_band;
2906 return pad_schedule(graph);
2909 /* Compute a schedule for the given dependence graph.
2910 * We first check if the graph is connected (through validity dependences)
2911 * and, if not, compute a schedule for each component separately.
2912 * If schedule_fuse is set to minimal fusion, then we check for strongly
2913 * connected components instead and compute a separate schedule for
2914 * each such strongly connected component.
2916 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
2918 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN) {
2919 if (detect_sccs(ctx, graph) < 0)
2920 return -1;
2921 } else {
2922 if (detect_wccs(ctx, graph) < 0)
2923 return -1;
2926 if (graph->scc > 1)
2927 return compute_component_schedule(ctx, graph);
2929 return compute_schedule_wcc(ctx, graph);
2932 /* Compute a schedule for the given union of domains that respects
2933 * all the validity dependences.
2934 * If the default isl scheduling algorithm is used, it tries to minimize
2935 * the dependence distances over the proximity dependences.
2936 * If Feautrier's scheduling algorithm is used, the proximity dependence
2937 * distances are only minimized during the extension to a full-dimensional
2938 * schedule.
2940 __isl_give isl_schedule *isl_union_set_compute_schedule(
2941 __isl_take isl_union_set *domain,
2942 __isl_take isl_union_map *validity,
2943 __isl_take isl_union_map *proximity)
2945 isl_ctx *ctx = isl_union_set_get_ctx(domain);
2946 isl_space *dim;
2947 struct isl_sched_graph graph = { 0 };
2948 isl_schedule *sched;
2949 struct isl_extract_edge_data data;
2951 domain = isl_union_set_align_params(domain,
2952 isl_union_map_get_space(validity));
2953 domain = isl_union_set_align_params(domain,
2954 isl_union_map_get_space(proximity));
2955 dim = isl_union_set_get_space(domain);
2956 validity = isl_union_map_align_params(validity, isl_space_copy(dim));
2957 proximity = isl_union_map_align_params(proximity, dim);
2959 if (!domain)
2960 goto error;
2962 graph.n = isl_union_set_n_set(domain);
2963 if (graph.n == 0)
2964 goto empty;
2965 if (graph_alloc(ctx, &graph, graph.n,
2966 isl_union_map_n_map(validity) + isl_union_map_n_map(proximity)) < 0)
2967 goto error;
2968 if (compute_max_row(&graph, domain) < 0)
2969 goto error;
2970 graph.root = 1;
2971 graph.n = 0;
2972 if (isl_union_set_foreach_set(domain, &extract_node, &graph) < 0)
2973 goto error;
2974 if (graph_init_table(ctx, &graph) < 0)
2975 goto error;
2976 graph.max_edge[isl_edge_validity] = isl_union_map_n_map(validity);
2977 graph.max_edge[isl_edge_proximity] = isl_union_map_n_map(proximity);
2978 if (graph_init_edge_tables(ctx, &graph) < 0)
2979 goto error;
2980 graph.n_edge = 0;
2981 data.graph = &graph;
2982 data.type = isl_edge_validity;
2983 if (isl_union_map_foreach_map(validity, &extract_edge, &data) < 0)
2984 goto error;
2985 data.type = isl_edge_proximity;
2986 if (isl_union_map_foreach_map(proximity, &extract_edge, &data) < 0)
2987 goto error;
2989 if (compute_schedule(ctx, &graph) < 0)
2990 goto error;
2992 empty:
2993 sched = extract_schedule(&graph, isl_union_set_get_space(domain));
2995 graph_free(ctx, &graph);
2996 isl_union_set_free(domain);
2997 isl_union_map_free(validity);
2998 isl_union_map_free(proximity);
3000 return sched;
3001 error:
3002 graph_free(ctx, &graph);
3003 isl_union_set_free(domain);
3004 isl_union_map_free(validity);
3005 isl_union_map_free(proximity);
3006 return NULL;
3009 void *isl_schedule_free(__isl_take isl_schedule *sched)
3011 int i;
3012 if (!sched)
3013 return NULL;
3015 if (--sched->ref > 0)
3016 return NULL;
3018 for (i = 0; i < sched->n; ++i) {
3019 isl_multi_aff_free(sched->node[i].sched);
3020 free(sched->node[i].band_end);
3021 free(sched->node[i].band_id);
3022 free(sched->node[i].zero);
3024 isl_space_free(sched->dim);
3025 isl_band_list_free(sched->band_forest);
3026 free(sched);
3027 return NULL;
3030 isl_ctx *isl_schedule_get_ctx(__isl_keep isl_schedule *schedule)
3032 return schedule ? isl_space_get_ctx(schedule->dim) : NULL;
3035 /* Set max_out to the maximal number of output dimensions over
3036 * all maps.
3038 static int update_max_out(__isl_take isl_map *map, void *user)
3040 int *max_out = user;
3041 int n_out = isl_map_dim(map, isl_dim_out);
3043 if (n_out > *max_out)
3044 *max_out = n_out;
3046 isl_map_free(map);
3047 return 0;
3050 /* Internal data structure for map_pad_range.
3052 * "max_out" is the maximal schedule dimension.
3053 * "res" collects the results.
3055 struct isl_pad_schedule_map_data {
3056 int max_out;
3057 isl_union_map *res;
3060 /* Pad the range of the given map with zeros to data->max_out and
3061 * then add the result to data->res.
3063 static int map_pad_range(__isl_take isl_map *map, void *user)
3065 struct isl_pad_schedule_map_data *data = user;
3066 int i;
3067 int n_out = isl_map_dim(map, isl_dim_out);
3069 map = isl_map_add_dims(map, isl_dim_out, data->max_out - n_out);
3070 for (i = n_out; i < data->max_out; ++i)
3071 map = isl_map_fix_si(map, isl_dim_out, i, 0);
3073 data->res = isl_union_map_add_map(data->res, map);
3074 if (!data->res)
3075 return -1;
3077 return 0;
3080 /* Pad the ranges of the maps in the union map with zeros such they all have
3081 * the same dimension.
3083 static __isl_give isl_union_map *pad_schedule_map(
3084 __isl_take isl_union_map *umap)
3086 struct isl_pad_schedule_map_data data;
3088 if (!umap)
3089 return NULL;
3090 if (isl_union_map_n_map(umap) <= 1)
3091 return umap;
3093 data.max_out = 0;
3094 if (isl_union_map_foreach_map(umap, &update_max_out, &data.max_out) < 0)
3095 return isl_union_map_free(umap);
3097 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3098 if (isl_union_map_foreach_map(umap, &map_pad_range, &data) < 0)
3099 data.res = isl_union_map_free(data.res);
3101 isl_union_map_free(umap);
3102 return data.res;
3105 /* Return an isl_union_map of the schedule. If we have already constructed
3106 * a band forest, then this band forest may have been modified so we need
3107 * to extract the isl_union_map from the forest rather than from
3108 * the originally computed schedule. This reconstructed schedule map
3109 * then needs to be padded with zeros to unify the schedule space
3110 * since the result of isl_band_list_get_suffix_schedule may not have
3111 * a unified schedule space.
3113 __isl_give isl_union_map *isl_schedule_get_map(__isl_keep isl_schedule *sched)
3115 int i;
3116 isl_union_map *umap;
3118 if (!sched)
3119 return NULL;
3121 if (sched->band_forest) {
3122 umap = isl_band_list_get_suffix_schedule(sched->band_forest);
3123 return pad_schedule_map(umap);
3126 umap = isl_union_map_empty(isl_space_copy(sched->dim));
3127 for (i = 0; i < sched->n; ++i) {
3128 isl_multi_aff *ma;
3130 ma = isl_multi_aff_copy(sched->node[i].sched);
3131 umap = isl_union_map_add_map(umap, isl_map_from_multi_aff(ma));
3134 return umap;
3137 static __isl_give isl_band_list *construct_band_list(
3138 __isl_keep isl_schedule *schedule, __isl_keep isl_band *parent,
3139 int band_nr, int *parent_active, int n_active);
3141 /* Construct an isl_band structure for the band in the given schedule
3142 * with sequence number band_nr for the n_active nodes marked by active.
3143 * If the nodes don't have a band with the given sequence number,
3144 * then a band without members is created.
3146 * Because of the way the schedule is constructed, we know that
3147 * the position of the band inside the schedule of a node is the same
3148 * for all active nodes.
3150 * The partial schedule for the band is created before the children
3151 * are created to that construct_band_list can refer to the partial
3152 * schedule of the parent.
3154 static __isl_give isl_band *construct_band(__isl_keep isl_schedule *schedule,
3155 __isl_keep isl_band *parent,
3156 int band_nr, int *active, int n_active)
3158 int i, j;
3159 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
3160 isl_band *band;
3161 unsigned start, end;
3163 band = isl_band_alloc(ctx);
3164 if (!band)
3165 return NULL;
3167 band->schedule = schedule;
3168 band->parent = parent;
3170 for (i = 0; i < schedule->n; ++i)
3171 if (active[i])
3172 break;
3174 if (i >= schedule->n)
3175 isl_die(ctx, isl_error_internal,
3176 "band without active statements", goto error);
3178 start = band_nr ? schedule->node[i].band_end[band_nr - 1] : 0;
3179 end = band_nr < schedule->node[i].n_band ?
3180 schedule->node[i].band_end[band_nr] : start;
3181 band->n = end - start;
3183 band->zero = isl_alloc_array(ctx, int, band->n);
3184 if (band->n && !band->zero)
3185 goto error;
3187 for (j = 0; j < band->n; ++j)
3188 band->zero[j] = schedule->node[i].zero[start + j];
3190 band->pma = isl_union_pw_multi_aff_empty(isl_space_copy(schedule->dim));
3191 for (i = 0; i < schedule->n; ++i) {
3192 isl_multi_aff *ma;
3193 isl_pw_multi_aff *pma;
3194 unsigned n_out;
3196 if (!active[i])
3197 continue;
3199 ma = isl_multi_aff_copy(schedule->node[i].sched);
3200 n_out = isl_multi_aff_dim(ma, isl_dim_out);
3201 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, end, n_out - end);
3202 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, 0, start);
3203 pma = isl_pw_multi_aff_from_multi_aff(ma);
3204 band->pma = isl_union_pw_multi_aff_add_pw_multi_aff(band->pma,
3205 pma);
3207 if (!band->pma)
3208 goto error;
3210 for (i = 0; i < schedule->n; ++i)
3211 if (active[i] && schedule->node[i].n_band > band_nr + 1)
3212 break;
3214 if (i < schedule->n) {
3215 band->children = construct_band_list(schedule, band,
3216 band_nr + 1, active, n_active);
3217 if (!band->children)
3218 goto error;
3221 return band;
3222 error:
3223 isl_band_free(band);
3224 return NULL;
3227 /* Internal data structure used inside cmp_band and pw_multi_aff_extract_int.
3229 * r is set to a negative value if anything goes wrong.
3231 * c1 stores the result of extract_int.
3232 * c2 is a temporary value used inside cmp_band_in_ancestor.
3233 * t is a temporary value used inside extract_int.
3235 * first and equal are used inside extract_int.
3236 * first is set if we are looking at the first isl_multi_aff inside
3237 * the isl_union_pw_multi_aff.
3238 * equal is set if all the isl_multi_affs have been equal so far.
3240 struct isl_cmp_band_data {
3241 int r;
3243 int first;
3244 int equal;
3246 isl_int t;
3247 isl_int c1;
3248 isl_int c2;
3251 /* Check if "ma" assigns a constant value.
3252 * Note that this function is only called on isl_multi_affs
3253 * with a single output dimension.
3255 * If "ma" assigns a constant value then we compare it to data->c1
3256 * or assign it to data->c1 if this is the first isl_multi_aff we consider.
3257 * If "ma" does not assign a constant value or if it assigns a value
3258 * that is different from data->c1, then we set data->equal to zero
3259 * and terminate the check.
3261 static int multi_aff_extract_int(__isl_take isl_set *set,
3262 __isl_take isl_multi_aff *ma, void *user)
3264 isl_aff *aff;
3265 struct isl_cmp_band_data *data = user;
3267 aff = isl_multi_aff_get_aff(ma, 0);
3268 data->r = isl_aff_is_cst(aff);
3269 if (data->r >= 0 && data->r) {
3270 isl_aff_get_constant(aff, &data->t);
3271 if (data->first) {
3272 isl_int_set(data->c1, data->t);
3273 data->first = 0;
3274 } else if (!isl_int_eq(data->c1, data->t))
3275 data->equal = 0;
3276 } else if (data->r >= 0 && !data->r)
3277 data->equal = 0;
3279 isl_aff_free(aff);
3280 isl_set_free(set);
3281 isl_multi_aff_free(ma);
3283 if (data->r < 0)
3284 return -1;
3285 if (!data->equal)
3286 return -1;
3287 return 0;
3290 /* This function is called for each isl_pw_multi_aff in
3291 * the isl_union_pw_multi_aff checked by extract_int.
3292 * Check all the isl_multi_affs inside "pma".
3294 static int pw_multi_aff_extract_int(__isl_take isl_pw_multi_aff *pma,
3295 void *user)
3297 int r;
3299 r = isl_pw_multi_aff_foreach_piece(pma, &multi_aff_extract_int, user);
3300 isl_pw_multi_aff_free(pma);
3302 return r;
3305 /* Check if "upma" assigns a single constant value to its domain.
3306 * If so, return 1 and store the result in data->c1.
3307 * If not, return 0.
3309 * A negative return value from isl_union_pw_multi_aff_foreach_pw_multi_aff
3310 * means that either an error occurred or that we have broken off the check
3311 * because we already know the result is going to be negative.
3312 * In the latter case, data->equal is set to zero.
3314 static int extract_int(__isl_keep isl_union_pw_multi_aff *upma,
3315 struct isl_cmp_band_data *data)
3317 data->first = 1;
3318 data->equal = 1;
3320 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3321 &pw_multi_aff_extract_int, data) < 0) {
3322 if (!data->equal)
3323 return 0;
3324 return -1;
3327 return !data->first && data->equal;
3330 /* Compare "b1" and "b2" based on the parent schedule of their ancestor
3331 * "ancestor".
3333 * If the parent of "ancestor" also has a single member, then we
3334 * first try to compare the two band based on the partial schedule
3335 * of this parent.
3337 * Otherwise, or if the result is inconclusive, we look at the partial schedule
3338 * of "ancestor" itself.
3339 * In particular, we specialize the parent schedule based
3340 * on the domains of the child schedules, check if both assign
3341 * a single constant value and, if so, compare the two constant values.
3342 * If the specialized parent schedules do not assign a constant value,
3343 * then they cannot be used to order the two bands and so in this case
3344 * we return 0.
3346 static int cmp_band_in_ancestor(__isl_keep isl_band *b1,
3347 __isl_keep isl_band *b2, struct isl_cmp_band_data *data,
3348 __isl_keep isl_band *ancestor)
3350 isl_union_pw_multi_aff *upma;
3351 isl_union_set *domain;
3352 int r;
3354 if (data->r < 0)
3355 return 0;
3357 if (ancestor->parent && ancestor->parent->n == 1) {
3358 r = cmp_band_in_ancestor(b1, b2, data, ancestor->parent);
3359 if (data->r < 0)
3360 return 0;
3361 if (r)
3362 return r;
3365 upma = isl_union_pw_multi_aff_copy(b1->pma);
3366 domain = isl_union_pw_multi_aff_domain(upma);
3367 upma = isl_union_pw_multi_aff_copy(ancestor->pma);
3368 upma = isl_union_pw_multi_aff_intersect_domain(upma, domain);
3369 r = extract_int(upma, data);
3370 isl_union_pw_multi_aff_free(upma);
3372 if (r < 0)
3373 data->r = -1;
3374 if (r < 0 || !r)
3375 return 0;
3377 isl_int_set(data->c2, data->c1);
3379 upma = isl_union_pw_multi_aff_copy(b2->pma);
3380 domain = isl_union_pw_multi_aff_domain(upma);
3381 upma = isl_union_pw_multi_aff_copy(ancestor->pma);
3382 upma = isl_union_pw_multi_aff_intersect_domain(upma, domain);
3383 r = extract_int(upma, data);
3384 isl_union_pw_multi_aff_free(upma);
3386 if (r < 0)
3387 data->r = -1;
3388 if (r < 0 || !r)
3389 return 0;
3391 return isl_int_cmp(data->c2, data->c1);
3394 /* Compare "a" and "b" based on the parent schedule of their parent.
3396 static int cmp_band(const void *a, const void *b, void *user)
3398 isl_band *b1 = *(isl_band * const *) a;
3399 isl_band *b2 = *(isl_band * const *) b;
3400 struct isl_cmp_band_data *data = user;
3402 return cmp_band_in_ancestor(b1, b2, data, b1->parent);
3405 /* Sort the elements in "list" based on the partial schedules of its parent
3406 * (and ancestors). In particular if the parent assigns constant values
3407 * to the domains of the bands in "list", then the elements are sorted
3408 * according to that order.
3409 * This order should be a more "natural" order for the user, but otherwise
3410 * shouldn't have any effect.
3411 * If we would be constructing an isl_band forest directly in
3412 * isl_union_set_compute_schedule then there wouldn't be any need
3413 * for a reordering, since the children would be added to the list
3414 * in their natural order automatically.
3416 * If there is only one element in the list, then there is no need to sort
3417 * anything.
3418 * If the partial schedule of the parent has more than one member
3419 * (or if there is no parent), then it's
3420 * defnitely not assigning constant values to the different children in
3421 * the list and so we wouldn't be able to use it to sort the list.
3423 static __isl_give isl_band_list *sort_band_list(__isl_take isl_band_list *list,
3424 __isl_keep isl_band *parent)
3426 struct isl_cmp_band_data data;
3428 if (!list)
3429 return NULL;
3430 if (list->n <= 1)
3431 return list;
3432 if (!parent || parent->n != 1)
3433 return list;
3435 data.r = 0;
3436 isl_int_init(data.c1);
3437 isl_int_init(data.c2);
3438 isl_int_init(data.t);
3439 isl_sort(list->p, list->n, sizeof(list->p[0]), &cmp_band, &data);
3440 if (data.r < 0)
3441 list = isl_band_list_free(list);
3442 isl_int_clear(data.c1);
3443 isl_int_clear(data.c2);
3444 isl_int_clear(data.t);
3446 return list;
3449 /* Construct a list of bands that start at the same position (with
3450 * sequence number band_nr) in the schedules of the nodes that
3451 * were active in the parent band.
3453 * A separate isl_band structure is created for each band_id
3454 * and for each node that does not have a band with sequence
3455 * number band_nr. In the latter case, a band without members
3456 * is created.
3457 * This ensures that if a band has any children, then each node
3458 * that was active in the band is active in exactly one of the children.
3460 static __isl_give isl_band_list *construct_band_list(
3461 __isl_keep isl_schedule *schedule, __isl_keep isl_band *parent,
3462 int band_nr, int *parent_active, int n_active)
3464 int i, j;
3465 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
3466 int *active;
3467 int n_band;
3468 isl_band_list *list;
3470 n_band = 0;
3471 for (i = 0; i < n_active; ++i) {
3472 for (j = 0; j < schedule->n; ++j) {
3473 if (!parent_active[j])
3474 continue;
3475 if (schedule->node[j].n_band <= band_nr)
3476 continue;
3477 if (schedule->node[j].band_id[band_nr] == i) {
3478 n_band++;
3479 break;
3483 for (j = 0; j < schedule->n; ++j)
3484 if (schedule->node[j].n_band <= band_nr)
3485 n_band++;
3487 if (n_band == 1) {
3488 isl_band *band;
3489 list = isl_band_list_alloc(ctx, n_band);
3490 band = construct_band(schedule, parent, band_nr,
3491 parent_active, n_active);
3492 return isl_band_list_add(list, band);
3495 active = isl_alloc_array(ctx, int, schedule->n);
3496 if (schedule->n && !active)
3497 return NULL;
3499 list = isl_band_list_alloc(ctx, n_band);
3501 for (i = 0; i < n_active; ++i) {
3502 int n = 0;
3503 isl_band *band;
3505 for (j = 0; j < schedule->n; ++j) {
3506 active[j] = parent_active[j] &&
3507 schedule->node[j].n_band > band_nr &&
3508 schedule->node[j].band_id[band_nr] == i;
3509 if (active[j])
3510 n++;
3512 if (n == 0)
3513 continue;
3515 band = construct_band(schedule, parent, band_nr, active, n);
3517 list = isl_band_list_add(list, band);
3519 for (i = 0; i < schedule->n; ++i) {
3520 isl_band *band;
3521 if (!parent_active[i])
3522 continue;
3523 if (schedule->node[i].n_band > band_nr)
3524 continue;
3525 for (j = 0; j < schedule->n; ++j)
3526 active[j] = j == i;
3527 band = construct_band(schedule, parent, band_nr, active, 1);
3528 list = isl_band_list_add(list, band);
3531 free(active);
3533 list = sort_band_list(list, parent);
3535 return list;
3538 /* Construct a band forest representation of the schedule and
3539 * return the list of roots.
3541 static __isl_give isl_band_list *construct_forest(
3542 __isl_keep isl_schedule *schedule)
3544 int i;
3545 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
3546 isl_band_list *forest;
3547 int *active;
3549 active = isl_alloc_array(ctx, int, schedule->n);
3550 if (schedule->n && !active)
3551 return NULL;
3553 for (i = 0; i < schedule->n; ++i)
3554 active[i] = 1;
3556 forest = construct_band_list(schedule, NULL, 0, active, schedule->n);
3558 free(active);
3560 return forest;
3563 /* Return the roots of a band forest representation of the schedule.
3565 __isl_give isl_band_list *isl_schedule_get_band_forest(
3566 __isl_keep isl_schedule *schedule)
3568 if (!schedule)
3569 return NULL;
3570 if (!schedule->band_forest)
3571 schedule->band_forest = construct_forest(schedule);
3572 return isl_band_list_dup(schedule->band_forest);
3575 /* Call "fn" on each band in the schedule in depth-first post-order.
3577 int isl_schedule_foreach_band(__isl_keep isl_schedule *sched,
3578 int (*fn)(__isl_keep isl_band *band, void *user), void *user)
3580 int r;
3581 isl_band_list *forest;
3583 if (!sched)
3584 return -1;
3586 forest = isl_schedule_get_band_forest(sched);
3587 r = isl_band_list_foreach_band(forest, fn, user);
3588 isl_band_list_free(forest);
3590 return r;
3593 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
3594 __isl_keep isl_band_list *list);
3596 static __isl_give isl_printer *print_band(__isl_take isl_printer *p,
3597 __isl_keep isl_band *band)
3599 isl_band_list *children;
3601 p = isl_printer_start_line(p);
3602 p = isl_printer_print_union_pw_multi_aff(p, band->pma);
3603 p = isl_printer_end_line(p);
3605 if (!isl_band_has_children(band))
3606 return p;
3608 children = isl_band_get_children(band);
3610 p = isl_printer_indent(p, 4);
3611 p = print_band_list(p, children);
3612 p = isl_printer_indent(p, -4);
3614 isl_band_list_free(children);
3616 return p;
3619 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
3620 __isl_keep isl_band_list *list)
3622 int i, n;
3624 n = isl_band_list_n_band(list);
3625 for (i = 0; i < n; ++i) {
3626 isl_band *band;
3627 band = isl_band_list_get_band(list, i);
3628 p = print_band(p, band);
3629 isl_band_free(band);
3632 return p;
3635 __isl_give isl_printer *isl_printer_print_schedule(__isl_take isl_printer *p,
3636 __isl_keep isl_schedule *schedule)
3638 isl_band_list *forest;
3640 forest = isl_schedule_get_band_forest(schedule);
3642 p = print_band_list(p, forest);
3644 isl_band_list_free(forest);
3646 return p;
3649 void isl_schedule_dump(__isl_keep isl_schedule *schedule)
3651 isl_printer *printer;
3653 if (!schedule)
3654 return;
3656 printer = isl_printer_to_file(isl_schedule_get_ctx(schedule), stderr);
3657 printer = isl_printer_print_schedule(printer, schedule);
3659 isl_printer_free(printer);