add isl_ast_expr_substitute_ids
[isl.git] / isl_schedule.c
blobc3205efc89d16fd77b935a135b15a582c03d01da
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 * start is the first variable in the LP problem in the sequences that
53 * represents the schedule coefficients of this node
54 * nvar is the dimension of the domain
55 * nparam is the number of parameters or 0 if we are not constructing
56 * a parametric schedule
58 * scc is the index of SCC (or WCC) this node belongs to
60 * band contains the band index for each of the rows of the schedule.
61 * band_id is used to differentiate between separate bands at the same
62 * level within the same parent band, i.e., bands that are separated
63 * by the parent band or bands that are independent of each other.
64 * zero contains a boolean for each of the rows of the schedule,
65 * indicating whether the corresponding scheduling dimension results
66 * in zero dependence distances within its band and with respect
67 * to the proximity edges.
69 struct isl_sched_node {
70 isl_space *dim;
71 isl_mat *sched;
72 isl_map *sched_map;
73 int rank;
74 isl_mat *cmap;
75 int start;
76 int nvar;
77 int nparam;
79 int scc;
81 int *band;
82 int *band_id;
83 int *zero;
86 static int node_has_dim(const void *entry, const void *val)
88 struct isl_sched_node *node = (struct isl_sched_node *)entry;
89 isl_space *dim = (isl_space *)val;
91 return isl_space_is_equal(node->dim, dim);
94 /* An edge in the dependence graph. An edge may be used to
95 * ensure validity of the generated schedule, to minimize the dependence
96 * distance or both
98 * map is the dependence relation
99 * src is the source node
100 * dst is the sink node
101 * validity is set if the edge is used to ensure correctness
102 * proximity is set if the edge is used to minimize dependence distances
104 * For validity edges, start and end mark the sequence of inequality
105 * constraints in the LP problem that encode the validity constraint
106 * corresponding to this edge.
108 struct isl_sched_edge {
109 isl_map *map;
111 struct isl_sched_node *src;
112 struct isl_sched_node *dst;
114 int validity;
115 int proximity;
117 int start;
118 int end;
121 enum isl_edge_type {
122 isl_edge_validity = 0,
123 isl_edge_first = isl_edge_validity,
124 isl_edge_proximity,
125 isl_edge_last = isl_edge_proximity
128 /* Internal information about the dependence graph used during
129 * the construction of the schedule.
131 * intra_hmap is a cache, mapping dependence relations to their dual,
132 * for dependences from a node to itself
133 * inter_hmap is a cache, mapping dependence relations to their dual,
134 * for dependences between distinct nodes
136 * n is the number of nodes
137 * node is the list of nodes
138 * maxvar is the maximal number of variables over all nodes
139 * max_row is the allocated number of rows in the schedule
140 * n_row is the current (maximal) number of linearly independent
141 * rows in the node schedules
142 * n_total_row is the current number of rows in the node schedules
143 * n_band is the current number of completed bands
144 * band_start is the starting row in the node schedules of the current band
145 * root is set if this graph is the original dependence graph,
146 * without any splitting
148 * sorted contains a list of node indices sorted according to the
149 * SCC to which a node belongs
151 * n_edge is the number of edges
152 * edge is the list of edges
153 * max_edge contains the maximal number of edges of each type;
154 * in particular, it contains the number of edges in the inital graph.
155 * edge_table contains pointers into the edge array, hashed on the source
156 * and sink spaces; there is one such table for each type;
157 * a given edge may be referenced from more than one table
158 * if the corresponding relation appears in more than of the
159 * sets of dependences
161 * node_table contains pointers into the node array, hashed on the space
163 * region contains a list of variable sequences that should be non-trivial
165 * lp contains the (I)LP problem used to obtain new schedule rows
167 * src_scc and dst_scc are the source and sink SCCs of an edge with
168 * conflicting constraints
170 * scc represents the number of components
172 struct isl_sched_graph {
173 isl_map_to_basic_set *intra_hmap;
174 isl_map_to_basic_set *inter_hmap;
176 struct isl_sched_node *node;
177 int n;
178 int maxvar;
179 int max_row;
180 int n_row;
182 int *sorted;
184 int n_band;
185 int n_total_row;
186 int band_start;
188 int root;
190 struct isl_sched_edge *edge;
191 int n_edge;
192 int max_edge[isl_edge_last + 1];
193 struct isl_hash_table *edge_table[isl_edge_last + 1];
195 struct isl_hash_table *node_table;
196 struct isl_region *region;
198 isl_basic_set *lp;
200 int src_scc;
201 int dst_scc;
203 int scc;
206 /* Initialize node_table based on the list of nodes.
208 static int graph_init_table(isl_ctx *ctx, struct isl_sched_graph *graph)
210 int i;
212 graph->node_table = isl_hash_table_alloc(ctx, graph->n);
213 if (!graph->node_table)
214 return -1;
216 for (i = 0; i < graph->n; ++i) {
217 struct isl_hash_table_entry *entry;
218 uint32_t hash;
220 hash = isl_space_get_hash(graph->node[i].dim);
221 entry = isl_hash_table_find(ctx, graph->node_table, hash,
222 &node_has_dim,
223 graph->node[i].dim, 1);
224 if (!entry)
225 return -1;
226 entry->data = &graph->node[i];
229 return 0;
232 /* Return a pointer to the node that lives within the given space,
233 * or NULL if there is no such node.
235 static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
236 struct isl_sched_graph *graph, __isl_keep isl_space *dim)
238 struct isl_hash_table_entry *entry;
239 uint32_t hash;
241 hash = isl_space_get_hash(dim);
242 entry = isl_hash_table_find(ctx, graph->node_table, hash,
243 &node_has_dim, dim, 0);
245 return entry ? entry->data : NULL;
248 static int edge_has_src_and_dst(const void *entry, const void *val)
250 const struct isl_sched_edge *edge = entry;
251 const struct isl_sched_edge *temp = val;
253 return edge->src == temp->src && edge->dst == temp->dst;
256 /* Add the given edge to graph->edge_table[type].
258 static int graph_edge_table_add(isl_ctx *ctx, struct isl_sched_graph *graph,
259 enum isl_edge_type type, struct isl_sched_edge *edge)
261 struct isl_hash_table_entry *entry;
262 uint32_t hash;
264 hash = isl_hash_init();
265 hash = isl_hash_builtin(hash, edge->src);
266 hash = isl_hash_builtin(hash, edge->dst);
267 entry = isl_hash_table_find(ctx, graph->edge_table[type], hash,
268 &edge_has_src_and_dst, edge, 1);
269 if (!entry)
270 return -1;
271 entry->data = edge;
273 return 0;
276 /* Allocate the edge_tables based on the maximal number of edges of
277 * each type.
279 static int graph_init_edge_tables(isl_ctx *ctx, struct isl_sched_graph *graph)
281 int i;
283 for (i = 0; i <= isl_edge_last; ++i) {
284 graph->edge_table[i] = isl_hash_table_alloc(ctx,
285 graph->max_edge[i]);
286 if (!graph->edge_table[i])
287 return -1;
290 return 0;
293 /* If graph->edge_table[type] contains an edge from the given source
294 * to the given destination, then return the hash table entry of this edge.
295 * Otherwise, return NULL.
297 static struct isl_hash_table_entry *graph_find_edge_entry(
298 struct isl_sched_graph *graph,
299 enum isl_edge_type type,
300 struct isl_sched_node *src, struct isl_sched_node *dst)
302 isl_ctx *ctx = isl_space_get_ctx(src->dim);
303 uint32_t hash;
304 struct isl_sched_edge temp = { .src = src, .dst = dst };
306 hash = isl_hash_init();
307 hash = isl_hash_builtin(hash, temp.src);
308 hash = isl_hash_builtin(hash, temp.dst);
309 return isl_hash_table_find(ctx, graph->edge_table[type], hash,
310 &edge_has_src_and_dst, &temp, 0);
314 /* If graph->edge_table[type] contains an edge from the given source
315 * to the given destination, then return this edge.
316 * Otherwise, return NULL.
318 static struct isl_sched_edge *graph_find_edge(struct isl_sched_graph *graph,
319 enum isl_edge_type type,
320 struct isl_sched_node *src, struct isl_sched_node *dst)
322 struct isl_hash_table_entry *entry;
324 entry = graph_find_edge_entry(graph, type, src, dst);
325 if (!entry)
326 return NULL;
328 return entry->data;
331 /* Check whether the dependence graph has an edge of the given type
332 * between the given two nodes.
334 static int graph_has_edge(struct isl_sched_graph *graph,
335 enum isl_edge_type type,
336 struct isl_sched_node *src, struct isl_sched_node *dst)
338 struct isl_sched_edge *edge;
339 int empty;
341 edge = graph_find_edge(graph, type, src, dst);
342 if (!edge)
343 return 0;
345 empty = isl_map_plain_is_empty(edge->map);
346 if (empty < 0)
347 return -1;
349 return !empty;
352 /* If there is an edge from the given source to the given destination
353 * of any type then return this edge.
354 * Otherwise, return NULL.
356 static struct isl_sched_edge *graph_find_any_edge(struct isl_sched_graph *graph,
357 struct isl_sched_node *src, struct isl_sched_node *dst)
359 enum isl_edge_type i;
360 struct isl_sched_edge *edge;
362 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
363 edge = graph_find_edge(graph, i, src, dst);
364 if (edge)
365 return edge;
368 return NULL;
371 /* Remove the given edge from all the edge_tables that refer to it.
373 static void graph_remove_edge(struct isl_sched_graph *graph,
374 struct isl_sched_edge *edge)
376 isl_ctx *ctx = isl_map_get_ctx(edge->map);
377 enum isl_edge_type i;
379 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
380 struct isl_hash_table_entry *entry;
382 entry = graph_find_edge_entry(graph, i, edge->src, edge->dst);
383 if (!entry)
384 continue;
385 if (entry->data != edge)
386 continue;
387 isl_hash_table_remove(ctx, graph->edge_table[i], entry);
391 /* Check whether the dependence graph has any edge
392 * between the given two nodes.
394 static int graph_has_any_edge(struct isl_sched_graph *graph,
395 struct isl_sched_node *src, struct isl_sched_node *dst)
397 enum isl_edge_type i;
398 int r;
400 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
401 r = graph_has_edge(graph, i, src, dst);
402 if (r < 0 || r)
403 return r;
406 return r;
409 /* Check whether the dependence graph has a validity edge
410 * between the given two nodes.
412 static int graph_has_validity_edge(struct isl_sched_graph *graph,
413 struct isl_sched_node *src, struct isl_sched_node *dst)
415 return graph_has_edge(graph, isl_edge_validity, src, dst);
418 static int graph_alloc(isl_ctx *ctx, struct isl_sched_graph *graph,
419 int n_node, int n_edge)
421 int i;
423 graph->n = n_node;
424 graph->n_edge = n_edge;
425 graph->node = isl_calloc_array(ctx, struct isl_sched_node, graph->n);
426 graph->sorted = isl_calloc_array(ctx, int, graph->n);
427 graph->region = isl_alloc_array(ctx, struct isl_region, graph->n);
428 graph->edge = isl_calloc_array(ctx,
429 struct isl_sched_edge, graph->n_edge);
431 graph->intra_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
432 graph->inter_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
434 if (!graph->node || !graph->region || (graph->n_edge && !graph->edge) ||
435 !graph->sorted)
436 return -1;
438 for(i = 0; i < graph->n; ++i)
439 graph->sorted[i] = i;
441 return 0;
444 static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
446 int i;
448 isl_map_to_basic_set_free(graph->intra_hmap);
449 isl_map_to_basic_set_free(graph->inter_hmap);
451 for (i = 0; i < graph->n; ++i) {
452 isl_space_free(graph->node[i].dim);
453 isl_mat_free(graph->node[i].sched);
454 isl_map_free(graph->node[i].sched_map);
455 isl_mat_free(graph->node[i].cmap);
456 if (graph->root) {
457 free(graph->node[i].band);
458 free(graph->node[i].band_id);
459 free(graph->node[i].zero);
462 free(graph->node);
463 free(graph->sorted);
464 for (i = 0; i < graph->n_edge; ++i)
465 isl_map_free(graph->edge[i].map);
466 free(graph->edge);
467 free(graph->region);
468 for (i = 0; i <= isl_edge_last; ++i)
469 isl_hash_table_free(ctx, graph->edge_table[i]);
470 isl_hash_table_free(ctx, graph->node_table);
471 isl_basic_set_free(graph->lp);
474 /* For each "set" on which this function is called, increment
475 * graph->n by one and update graph->maxvar.
477 static int init_n_maxvar(__isl_take isl_set *set, void *user)
479 struct isl_sched_graph *graph = user;
480 int nvar = isl_set_dim(set, isl_dim_set);
482 graph->n++;
483 if (nvar > graph->maxvar)
484 graph->maxvar = nvar;
486 isl_set_free(set);
488 return 0;
491 /* Compute the number of rows that should be allocated for the schedule.
492 * The graph can be split at most "n - 1" times, there can be at most
493 * two rows for each dimension in the iteration domains (in particular,
494 * we usually have one row, but it may be split by split_scaled),
495 * and there can be one extra row for ordering the statements.
496 * Note that if we have actually split "n - 1" times, then no ordering
497 * is needed, so in principle we could use "graph->n + 2 * graph->maxvar - 1".
499 static int compute_max_row(struct isl_sched_graph *graph,
500 __isl_keep isl_union_set *domain)
502 graph->n = 0;
503 graph->maxvar = 0;
504 if (isl_union_set_foreach_set(domain, &init_n_maxvar, graph) < 0)
505 return -1;
506 graph->max_row = graph->n + 2 * graph->maxvar;
508 return 0;
511 /* Add a new node to the graph representing the given set.
513 static int extract_node(__isl_take isl_set *set, void *user)
515 int nvar, nparam;
516 isl_ctx *ctx;
517 isl_space *dim;
518 isl_mat *sched;
519 struct isl_sched_graph *graph = user;
520 int *band, *band_id, *zero;
522 ctx = isl_set_get_ctx(set);
523 dim = isl_set_get_space(set);
524 isl_set_free(set);
525 nvar = isl_space_dim(dim, isl_dim_set);
526 nparam = isl_space_dim(dim, isl_dim_param);
527 if (!ctx->opt->schedule_parametric)
528 nparam = 0;
529 sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
530 graph->node[graph->n].dim = dim;
531 graph->node[graph->n].nvar = nvar;
532 graph->node[graph->n].nparam = nparam;
533 graph->node[graph->n].sched = sched;
534 graph->node[graph->n].sched_map = NULL;
535 band = isl_alloc_array(ctx, int, graph->max_row);
536 graph->node[graph->n].band = band;
537 band_id = isl_calloc_array(ctx, int, graph->max_row);
538 graph->node[graph->n].band_id = band_id;
539 zero = isl_calloc_array(ctx, int, graph->max_row);
540 graph->node[graph->n].zero = zero;
541 graph->n++;
543 if (!sched || (graph->max_row && (!band || !band_id || !zero)))
544 return -1;
546 return 0;
549 struct isl_extract_edge_data {
550 enum isl_edge_type type;
551 struct isl_sched_graph *graph;
554 /* Add a new edge to the graph based on the given map
555 * and add it to data->graph->edge_table[data->type].
556 * If a dependence relation of a given type happens to be identical
557 * to one of the dependence relations of a type that was added before,
558 * then we don't create a new edge, but instead mark the original edge
559 * as also representing a dependence of the current type.
561 static int extract_edge(__isl_take isl_map *map, void *user)
563 isl_ctx *ctx = isl_map_get_ctx(map);
564 struct isl_extract_edge_data *data = user;
565 struct isl_sched_graph *graph = data->graph;
566 struct isl_sched_node *src, *dst;
567 isl_space *dim;
568 struct isl_sched_edge *edge;
569 int is_equal;
571 dim = isl_space_domain(isl_map_get_space(map));
572 src = graph_find_node(ctx, graph, dim);
573 isl_space_free(dim);
574 dim = isl_space_range(isl_map_get_space(map));
575 dst = graph_find_node(ctx, graph, dim);
576 isl_space_free(dim);
578 if (!src || !dst) {
579 isl_map_free(map);
580 return 0;
583 graph->edge[graph->n_edge].src = src;
584 graph->edge[graph->n_edge].dst = dst;
585 graph->edge[graph->n_edge].map = map;
586 if (data->type == isl_edge_validity) {
587 graph->edge[graph->n_edge].validity = 1;
588 graph->edge[graph->n_edge].proximity = 0;
590 if (data->type == isl_edge_proximity) {
591 graph->edge[graph->n_edge].validity = 0;
592 graph->edge[graph->n_edge].proximity = 1;
594 graph->n_edge++;
596 edge = graph_find_any_edge(graph, src, dst);
597 if (!edge)
598 return graph_edge_table_add(ctx, graph, data->type,
599 &graph->edge[graph->n_edge - 1]);
600 is_equal = isl_map_plain_is_equal(map, edge->map);
601 if (is_equal < 0)
602 return -1;
603 if (!is_equal)
604 return graph_edge_table_add(ctx, graph, data->type,
605 &graph->edge[graph->n_edge - 1]);
607 graph->n_edge--;
608 edge->validity |= graph->edge[graph->n_edge].validity;
609 edge->proximity |= graph->edge[graph->n_edge].proximity;
610 isl_map_free(map);
612 return graph_edge_table_add(ctx, graph, data->type, edge);
615 /* Check whether there is any dependence from node[j] to node[i]
616 * or from node[i] to node[j].
618 static int node_follows_weak(int i, int j, void *user)
620 int f;
621 struct isl_sched_graph *graph = user;
623 f = graph_has_any_edge(graph, &graph->node[j], &graph->node[i]);
624 if (f < 0 || f)
625 return f;
626 return graph_has_any_edge(graph, &graph->node[i], &graph->node[j]);
629 /* Check whether there is a validity dependence from node[j] to node[i],
630 * forcing node[i] to follow node[j].
632 static int node_follows_strong(int i, int j, void *user)
634 struct isl_sched_graph *graph = user;
636 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
639 /* Use Tarjan's algorithm for computing the strongly connected components
640 * in the dependence graph (only validity edges).
641 * If weak is set, we consider the graph to be undirected and
642 * we effectively compute the (weakly) connected components.
643 * Additionally, we also consider other edges when weak is set.
645 static int detect_ccs(isl_ctx *ctx, struct isl_sched_graph *graph, int weak)
647 int i, n;
648 struct isl_tarjan_graph *g = NULL;
650 g = isl_tarjan_graph_init(ctx, graph->n,
651 weak ? &node_follows_weak : &node_follows_strong, graph);
652 if (!g)
653 return -1;
655 graph->scc = 0;
656 i = 0;
657 n = graph->n;
658 while (n) {
659 while (g->order[i] != -1) {
660 graph->node[g->order[i]].scc = graph->scc;
661 --n;
662 ++i;
664 ++i;
665 graph->scc++;
668 isl_tarjan_graph_free(g);
670 return 0;
673 /* Apply Tarjan's algorithm to detect the strongly connected components
674 * in the dependence graph.
676 static int detect_sccs(isl_ctx *ctx, struct isl_sched_graph *graph)
678 return detect_ccs(ctx, graph, 0);
681 /* Apply Tarjan's algorithm to detect the (weakly) connected components
682 * in the dependence graph.
684 static int detect_wccs(isl_ctx *ctx, struct isl_sched_graph *graph)
686 return detect_ccs(ctx, graph, 1);
689 static int cmp_scc(const void *a, const void *b, void *data)
691 struct isl_sched_graph *graph = data;
692 const int *i1 = a;
693 const int *i2 = b;
695 return graph->node[*i1].scc - graph->node[*i2].scc;
698 /* Sort the elements of graph->sorted according to the corresponding SCCs.
700 static int sort_sccs(struct isl_sched_graph *graph)
702 return isl_sort(graph->sorted, graph->n, sizeof(int), &cmp_scc, graph);
705 /* Given a dependence relation R from a node to itself,
706 * construct the set of coefficients of valid constraints for elements
707 * in that dependence relation.
708 * In particular, the result contains tuples of coefficients
709 * c_0, c_n, c_x such that
711 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
713 * or, equivalently,
715 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
717 * We choose here to compute the dual of delta R.
718 * Alternatively, we could have computed the dual of R, resulting
719 * in a set of tuples c_0, c_n, c_x, c_y, and then
720 * plugged in (c_0, c_n, c_x, -c_x).
722 static __isl_give isl_basic_set *intra_coefficients(
723 struct isl_sched_graph *graph, __isl_take isl_map *map)
725 isl_set *delta;
726 isl_basic_set *coef;
728 if (isl_map_to_basic_set_has(graph->intra_hmap, map))
729 return isl_map_to_basic_set_get(graph->intra_hmap, map);
731 delta = isl_set_remove_divs(isl_map_deltas(isl_map_copy(map)));
732 coef = isl_set_coefficients(delta);
733 graph->intra_hmap = isl_map_to_basic_set_set(graph->intra_hmap, map,
734 isl_basic_set_copy(coef));
736 return coef;
739 /* Given a dependence relation R, * construct the set of coefficients
740 * of valid constraints for elements in that dependence relation.
741 * In particular, the result contains tuples of coefficients
742 * c_0, c_n, c_x, c_y such that
744 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
747 static __isl_give isl_basic_set *inter_coefficients(
748 struct isl_sched_graph *graph, __isl_take isl_map *map)
750 isl_set *set;
751 isl_basic_set *coef;
753 if (isl_map_to_basic_set_has(graph->inter_hmap, map))
754 return isl_map_to_basic_set_get(graph->inter_hmap, map);
756 set = isl_map_wrap(isl_map_remove_divs(isl_map_copy(map)));
757 coef = isl_set_coefficients(set);
758 graph->inter_hmap = isl_map_to_basic_set_set(graph->inter_hmap, map,
759 isl_basic_set_copy(coef));
761 return coef;
764 /* Add constraints to graph->lp that force validity for the given
765 * dependence from a node i to itself.
766 * That is, add constraints that enforce
768 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
769 * = c_i_x (y - x) >= 0
771 * for each (x,y) in R.
772 * We obtain general constraints on coefficients (c_0, c_n, c_x)
773 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
774 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
775 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
777 * Actually, we do not construct constraints for the c_i_x themselves,
778 * but for the coefficients of c_i_x written as a linear combination
779 * of the columns in node->cmap.
781 static int add_intra_validity_constraints(struct isl_sched_graph *graph,
782 struct isl_sched_edge *edge)
784 unsigned total;
785 isl_map *map = isl_map_copy(edge->map);
786 isl_ctx *ctx = isl_map_get_ctx(map);
787 isl_space *dim;
788 isl_dim_map *dim_map;
789 isl_basic_set *coef;
790 struct isl_sched_node *node = edge->src;
792 coef = intra_coefficients(graph, map);
794 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
796 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
797 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
798 if (!coef)
799 goto error;
801 total = isl_basic_set_total_dim(graph->lp);
802 dim_map = isl_dim_map_alloc(ctx, total);
803 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
804 isl_space_dim(dim, isl_dim_set), 1,
805 node->nvar, -1);
806 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
807 isl_space_dim(dim, isl_dim_set), 1,
808 node->nvar, 1);
809 graph->lp = isl_basic_set_extend_constraints(graph->lp,
810 coef->n_eq, coef->n_ineq);
811 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
812 coef, dim_map);
813 isl_space_free(dim);
815 return 0;
816 error:
817 isl_space_free(dim);
818 return -1;
821 /* Add constraints to graph->lp that force validity for the given
822 * dependence from node i to node j.
823 * That is, add constraints that enforce
825 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
827 * for each (x,y) in R.
828 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
829 * of valid constraints for R and then plug in
830 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
831 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
832 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
833 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
835 * Actually, we do not construct constraints for the c_*_x themselves,
836 * but for the coefficients of c_*_x written as a linear combination
837 * of the columns in node->cmap.
839 static int add_inter_validity_constraints(struct isl_sched_graph *graph,
840 struct isl_sched_edge *edge)
842 unsigned total;
843 isl_map *map = isl_map_copy(edge->map);
844 isl_ctx *ctx = isl_map_get_ctx(map);
845 isl_space *dim;
846 isl_dim_map *dim_map;
847 isl_basic_set *coef;
848 struct isl_sched_node *src = edge->src;
849 struct isl_sched_node *dst = edge->dst;
851 coef = inter_coefficients(graph, map);
853 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
855 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
856 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
857 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
858 isl_space_dim(dim, isl_dim_set) + src->nvar,
859 isl_mat_copy(dst->cmap));
860 if (!coef)
861 goto error;
863 total = isl_basic_set_total_dim(graph->lp);
864 dim_map = isl_dim_map_alloc(ctx, total);
866 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
867 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
868 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
869 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
870 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
871 dst->nvar, -1);
872 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
873 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
874 dst->nvar, 1);
876 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
877 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
878 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
879 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
880 isl_space_dim(dim, isl_dim_set), 1,
881 src->nvar, 1);
882 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
883 isl_space_dim(dim, isl_dim_set), 1,
884 src->nvar, -1);
886 edge->start = graph->lp->n_ineq;
887 graph->lp = isl_basic_set_extend_constraints(graph->lp,
888 coef->n_eq, coef->n_ineq);
889 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
890 coef, dim_map);
891 if (!graph->lp)
892 goto error;
893 isl_space_free(dim);
894 edge->end = graph->lp->n_ineq;
896 return 0;
897 error:
898 isl_space_free(dim);
899 return -1;
902 /* Add constraints to graph->lp that bound the dependence distance for the given
903 * dependence from a node i to itself.
904 * If s = 1, we add the constraint
906 * c_i_x (y - x) <= m_0 + m_n n
908 * or
910 * -c_i_x (y - x) + m_0 + m_n n >= 0
912 * for each (x,y) in R.
913 * If s = -1, we add the constraint
915 * -c_i_x (y - x) <= m_0 + m_n n
917 * or
919 * c_i_x (y - x) + m_0 + m_n n >= 0
921 * for each (x,y) in R.
922 * We obtain general constraints on coefficients (c_0, c_n, c_x)
923 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
924 * with each coefficient (except m_0) represented as a pair of non-negative
925 * coefficients.
927 * Actually, we do not construct constraints for the c_i_x themselves,
928 * but for the coefficients of c_i_x written as a linear combination
929 * of the columns in node->cmap.
931 static int add_intra_proximity_constraints(struct isl_sched_graph *graph,
932 struct isl_sched_edge *edge, int s)
934 unsigned total;
935 unsigned nparam;
936 isl_map *map = isl_map_copy(edge->map);
937 isl_ctx *ctx = isl_map_get_ctx(map);
938 isl_space *dim;
939 isl_dim_map *dim_map;
940 isl_basic_set *coef;
941 struct isl_sched_node *node = edge->src;
943 coef = intra_coefficients(graph, map);
945 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
947 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
948 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
949 if (!coef)
950 goto error;
952 nparam = isl_space_dim(node->dim, isl_dim_param);
953 total = isl_basic_set_total_dim(graph->lp);
954 dim_map = isl_dim_map_alloc(ctx, total);
955 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
956 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
957 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
958 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
959 isl_space_dim(dim, isl_dim_set), 1,
960 node->nvar, s);
961 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
962 isl_space_dim(dim, isl_dim_set), 1,
963 node->nvar, -s);
964 graph->lp = isl_basic_set_extend_constraints(graph->lp,
965 coef->n_eq, coef->n_ineq);
966 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
967 coef, dim_map);
968 isl_space_free(dim);
970 return 0;
971 error:
972 isl_space_free(dim);
973 return -1;
976 /* Add constraints to graph->lp that bound the dependence distance for the given
977 * dependence from node i to node j.
978 * If s = 1, we add the constraint
980 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
981 * <= m_0 + m_n n
983 * or
985 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
986 * m_0 + m_n n >= 0
988 * for each (x,y) in R.
989 * If s = -1, we add the constraint
991 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
992 * <= m_0 + m_n n
994 * or
996 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
997 * m_0 + m_n n >= 0
999 * for each (x,y) in R.
1000 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1001 * of valid constraints for R and then plug in
1002 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1003 * -s*c_j_x+s*c_i_x)
1004 * with each coefficient (except m_0, c_j_0 and c_i_0)
1005 * represented as a pair of non-negative coefficients.
1007 * Actually, we do not construct constraints for the c_*_x themselves,
1008 * but for the coefficients of c_*_x written as a linear combination
1009 * of the columns in node->cmap.
1011 static int add_inter_proximity_constraints(struct isl_sched_graph *graph,
1012 struct isl_sched_edge *edge, int s)
1014 unsigned total;
1015 unsigned nparam;
1016 isl_map *map = isl_map_copy(edge->map);
1017 isl_ctx *ctx = isl_map_get_ctx(map);
1018 isl_space *dim;
1019 isl_dim_map *dim_map;
1020 isl_basic_set *coef;
1021 struct isl_sched_node *src = edge->src;
1022 struct isl_sched_node *dst = edge->dst;
1024 coef = inter_coefficients(graph, map);
1026 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1028 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1029 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1030 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1031 isl_space_dim(dim, isl_dim_set) + src->nvar,
1032 isl_mat_copy(dst->cmap));
1033 if (!coef)
1034 goto error;
1036 nparam = isl_space_dim(src->dim, isl_dim_param);
1037 total = isl_basic_set_total_dim(graph->lp);
1038 dim_map = isl_dim_map_alloc(ctx, total);
1040 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1041 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1042 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1044 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, -s);
1045 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, s);
1046 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, -s);
1047 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1048 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1049 dst->nvar, s);
1050 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1051 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1052 dst->nvar, -s);
1054 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, s);
1055 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, -s);
1056 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, s);
1057 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1058 isl_space_dim(dim, isl_dim_set), 1,
1059 src->nvar, -s);
1060 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1061 isl_space_dim(dim, isl_dim_set), 1,
1062 src->nvar, s);
1064 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1065 coef->n_eq, coef->n_ineq);
1066 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1067 coef, dim_map);
1068 isl_space_free(dim);
1070 return 0;
1071 error:
1072 isl_space_free(dim);
1073 return -1;
1076 static int add_all_validity_constraints(struct isl_sched_graph *graph)
1078 int i;
1080 for (i = 0; i < graph->n_edge; ++i) {
1081 struct isl_sched_edge *edge= &graph->edge[i];
1082 if (!edge->validity)
1083 continue;
1084 if (edge->src != edge->dst)
1085 continue;
1086 if (add_intra_validity_constraints(graph, edge) < 0)
1087 return -1;
1090 for (i = 0; i < graph->n_edge; ++i) {
1091 struct isl_sched_edge *edge = &graph->edge[i];
1092 if (!edge->validity)
1093 continue;
1094 if (edge->src == edge->dst)
1095 continue;
1096 if (add_inter_validity_constraints(graph, edge) < 0)
1097 return -1;
1100 return 0;
1103 /* Add constraints to graph->lp that bound the dependence distance
1104 * for all dependence relations.
1105 * If a given proximity dependence is identical to a validity
1106 * dependence, then the dependence distance is already bounded
1107 * from below (by zero), so we only need to bound the distance
1108 * from above.
1109 * Otherwise, we need to bound the distance both from above and from below.
1111 static int add_all_proximity_constraints(struct isl_sched_graph *graph)
1113 int i;
1115 for (i = 0; i < graph->n_edge; ++i) {
1116 struct isl_sched_edge *edge= &graph->edge[i];
1117 if (!edge->proximity)
1118 continue;
1119 if (edge->src == edge->dst &&
1120 add_intra_proximity_constraints(graph, edge, 1) < 0)
1121 return -1;
1122 if (edge->src != edge->dst &&
1123 add_inter_proximity_constraints(graph, edge, 1) < 0)
1124 return -1;
1125 if (edge->validity)
1126 continue;
1127 if (edge->src == edge->dst &&
1128 add_intra_proximity_constraints(graph, edge, -1) < 0)
1129 return -1;
1130 if (edge->src != edge->dst &&
1131 add_inter_proximity_constraints(graph, edge, -1) < 0)
1132 return -1;
1135 return 0;
1138 /* Compute a basis for the rows in the linear part of the schedule
1139 * and extend this basis to a full basis. The remaining rows
1140 * can then be used to force linear independence from the rows
1141 * in the schedule.
1143 * In particular, given the schedule rows S, we compute
1145 * S = H Q
1147 * with H the Hermite normal form of S. That is, all but the
1148 * first rank columns of Q are zero and so each row in S is
1149 * a linear combination of the first rank rows of Q.
1150 * The matrix Q is then transposed because we will write the
1151 * coefficients of the next schedule row as a column vector s
1152 * and express this s as a linear combination s = Q c of the
1153 * computed basis.
1155 static int node_update_cmap(struct isl_sched_node *node)
1157 isl_mat *H, *Q;
1158 int n_row = isl_mat_rows(node->sched);
1160 H = isl_mat_sub_alloc(node->sched, 0, n_row,
1161 1 + node->nparam, node->nvar);
1163 H = isl_mat_left_hermite(H, 0, NULL, &Q);
1164 isl_mat_free(node->cmap);
1165 node->cmap = isl_mat_transpose(Q);
1166 node->rank = isl_mat_initial_non_zero_cols(H);
1167 isl_mat_free(H);
1169 if (!node->cmap || node->rank < 0)
1170 return -1;
1171 return 0;
1174 /* Count the number of equality and inequality constraints
1175 * that will be added for the given map.
1176 * If carry is set, then we are counting the number of (validity)
1177 * constraints that will be added in setup_carry_lp and we count
1178 * each edge exactly once. Otherwise, we count as follows
1179 * validity -> 1 (>= 0)
1180 * validity+proximity -> 2 (>= 0 and upper bound)
1181 * proximity -> 2 (lower and upper bound)
1183 static int count_map_constraints(struct isl_sched_graph *graph,
1184 struct isl_sched_edge *edge, __isl_take isl_map *map,
1185 int *n_eq, int *n_ineq, int carry)
1187 isl_basic_set *coef;
1188 int f = carry ? 1 : edge->proximity ? 2 : 1;
1190 if (carry && !edge->validity) {
1191 isl_map_free(map);
1192 return 0;
1195 if (edge->src == edge->dst)
1196 coef = intra_coefficients(graph, map);
1197 else
1198 coef = inter_coefficients(graph, map);
1199 if (!coef)
1200 return -1;
1201 *n_eq += f * coef->n_eq;
1202 *n_ineq += f * coef->n_ineq;
1203 isl_basic_set_free(coef);
1205 return 0;
1208 /* Count the number of equality and inequality constraints
1209 * that will be added to the main lp problem.
1210 * We count as follows
1211 * validity -> 1 (>= 0)
1212 * validity+proximity -> 2 (>= 0 and upper bound)
1213 * proximity -> 2 (lower and upper bound)
1215 static int count_constraints(struct isl_sched_graph *graph,
1216 int *n_eq, int *n_ineq)
1218 int i;
1220 *n_eq = *n_ineq = 0;
1221 for (i = 0; i < graph->n_edge; ++i) {
1222 struct isl_sched_edge *edge= &graph->edge[i];
1223 isl_map *map = isl_map_copy(edge->map);
1225 if (count_map_constraints(graph, edge, map,
1226 n_eq, n_ineq, 0) < 0)
1227 return -1;
1230 return 0;
1233 /* Count the number of constraints that will be added by
1234 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
1235 * accordingly.
1237 * In practice, add_bound_coefficient_constraints only adds inequalities.
1239 static int count_bound_coefficient_constraints(isl_ctx *ctx,
1240 struct isl_sched_graph *graph, int *n_eq, int *n_ineq)
1242 int i;
1244 if (ctx->opt->schedule_max_coefficient == -1)
1245 return 0;
1247 for (i = 0; i < graph->n; ++i)
1248 *n_ineq += 2 * graph->node[i].nparam + 2 * graph->node[i].nvar;
1250 return 0;
1253 /* Add constraints that bound the values of the variable and parameter
1254 * coefficients of the schedule.
1256 * The maximal value of the coefficients is defined by the option
1257 * 'schedule_max_coefficient'.
1259 static int add_bound_coefficient_constraints(isl_ctx *ctx,
1260 struct isl_sched_graph *graph)
1262 int i, j, k;
1263 int max_coefficient;
1264 int total;
1266 max_coefficient = ctx->opt->schedule_max_coefficient;
1268 if (max_coefficient == -1)
1269 return 0;
1271 total = isl_basic_set_total_dim(graph->lp);
1273 for (i = 0; i < graph->n; ++i) {
1274 struct isl_sched_node *node = &graph->node[i];
1275 for (j = 0; j < 2 * node->nparam + 2 * node->nvar; ++j) {
1276 int dim;
1277 k = isl_basic_set_alloc_inequality(graph->lp);
1278 if (k < 0)
1279 return -1;
1280 dim = 1 + node->start + 1 + j;
1281 isl_seq_clr(graph->lp->ineq[k], 1 + total);
1282 isl_int_set_si(graph->lp->ineq[k][dim], -1);
1283 isl_int_set_si(graph->lp->ineq[k][0], max_coefficient);
1287 return 0;
1290 /* Construct an ILP problem for finding schedule coefficients
1291 * that result in non-negative, but small dependence distances
1292 * over all dependences.
1293 * In particular, the dependence distances over proximity edges
1294 * are bounded by m_0 + m_n n and we compute schedule coefficients
1295 * with small values (preferably zero) of m_n and m_0.
1297 * All variables of the ILP are non-negative. The actual coefficients
1298 * may be negative, so each coefficient is represented as the difference
1299 * of two non-negative variables. The negative part always appears
1300 * immediately before the positive part.
1301 * Other than that, the variables have the following order
1303 * - sum of positive and negative parts of m_n coefficients
1304 * - m_0
1305 * - sum of positive and negative parts of all c_n coefficients
1306 * (unconstrained when computing non-parametric schedules)
1307 * - sum of positive and negative parts of all c_x coefficients
1308 * - positive and negative parts of m_n coefficients
1309 * - for each node
1310 * - c_i_0
1311 * - positive and negative parts of c_i_n (if parametric)
1312 * - positive and negative parts of c_i_x
1314 * The c_i_x are not represented directly, but through the columns of
1315 * node->cmap. That is, the computed values are for variable t_i_x
1316 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
1318 * The constraints are those from the edges plus two or three equalities
1319 * to express the sums.
1321 * If force_zero is set, then we add equalities to ensure that
1322 * the sum of the m_n coefficients and m_0 are both zero.
1324 static int setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
1325 int force_zero)
1327 int i, j;
1328 int k;
1329 unsigned nparam;
1330 unsigned total;
1331 isl_space *dim;
1332 int parametric;
1333 int param_pos;
1334 int n_eq, n_ineq;
1335 int max_constant_term;
1337 max_constant_term = ctx->opt->schedule_max_constant_term;
1339 parametric = ctx->opt->schedule_parametric;
1340 nparam = isl_space_dim(graph->node[0].dim, isl_dim_param);
1341 param_pos = 4;
1342 total = param_pos + 2 * nparam;
1343 for (i = 0; i < graph->n; ++i) {
1344 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
1345 if (node_update_cmap(node) < 0)
1346 return -1;
1347 node->start = total;
1348 total += 1 + 2 * (node->nparam + node->nvar);
1351 if (count_constraints(graph, &n_eq, &n_ineq) < 0)
1352 return -1;
1353 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
1354 return -1;
1356 dim = isl_space_set_alloc(ctx, 0, total);
1357 isl_basic_set_free(graph->lp);
1358 n_eq += 2 + parametric + force_zero;
1359 if (max_constant_term != -1)
1360 n_ineq += graph->n;
1362 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
1364 k = isl_basic_set_alloc_equality(graph->lp);
1365 if (k < 0)
1366 return -1;
1367 isl_seq_clr(graph->lp->eq[k], 1 + total);
1368 if (!force_zero)
1369 isl_int_set_si(graph->lp->eq[k][1], -1);
1370 for (i = 0; i < 2 * nparam; ++i)
1371 isl_int_set_si(graph->lp->eq[k][1 + param_pos + i], 1);
1373 if (force_zero) {
1374 k = isl_basic_set_alloc_equality(graph->lp);
1375 if (k < 0)
1376 return -1;
1377 isl_seq_clr(graph->lp->eq[k], 1 + total);
1378 isl_int_set_si(graph->lp->eq[k][2], -1);
1381 if (parametric) {
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][3], -1);
1387 for (i = 0; i < graph->n; ++i) {
1388 int pos = 1 + graph->node[i].start + 1;
1390 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
1391 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
1395 k = isl_basic_set_alloc_equality(graph->lp);
1396 if (k < 0)
1397 return -1;
1398 isl_seq_clr(graph->lp->eq[k], 1 + total);
1399 isl_int_set_si(graph->lp->eq[k][4], -1);
1400 for (i = 0; i < graph->n; ++i) {
1401 struct isl_sched_node *node = &graph->node[i];
1402 int pos = 1 + node->start + 1 + 2 * node->nparam;
1404 for (j = 0; j < 2 * node->nvar; ++j)
1405 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
1408 if (max_constant_term != -1)
1409 for (i = 0; i < graph->n; ++i) {
1410 struct isl_sched_node *node = &graph->node[i];
1411 k = isl_basic_set_alloc_inequality(graph->lp);
1412 if (k < 0)
1413 return -1;
1414 isl_seq_clr(graph->lp->ineq[k], 1 + total);
1415 isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
1416 isl_int_set_si(graph->lp->ineq[k][0], max_constant_term);
1419 if (add_bound_coefficient_constraints(ctx, graph) < 0)
1420 return -1;
1421 if (add_all_validity_constraints(graph) < 0)
1422 return -1;
1423 if (add_all_proximity_constraints(graph) < 0)
1424 return -1;
1426 return 0;
1429 /* Analyze the conflicting constraint found by
1430 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
1431 * constraint of one of the edges between distinct nodes, living, moreover
1432 * in distinct SCCs, then record the source and sink SCC as this may
1433 * be a good place to cut between SCCs.
1435 static int check_conflict(int con, void *user)
1437 int i;
1438 struct isl_sched_graph *graph = user;
1440 if (graph->src_scc >= 0)
1441 return 0;
1443 con -= graph->lp->n_eq;
1445 if (con >= graph->lp->n_ineq)
1446 return 0;
1448 for (i = 0; i < graph->n_edge; ++i) {
1449 if (!graph->edge[i].validity)
1450 continue;
1451 if (graph->edge[i].src == graph->edge[i].dst)
1452 continue;
1453 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
1454 continue;
1455 if (graph->edge[i].start > con)
1456 continue;
1457 if (graph->edge[i].end <= con)
1458 continue;
1459 graph->src_scc = graph->edge[i].src->scc;
1460 graph->dst_scc = graph->edge[i].dst->scc;
1463 return 0;
1466 /* Check whether the next schedule row of the given node needs to be
1467 * non-trivial. Lower-dimensional domains may have some trivial rows,
1468 * but as soon as the number of remaining required non-trivial rows
1469 * is as large as the number or remaining rows to be computed,
1470 * all remaining rows need to be non-trivial.
1472 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
1474 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
1477 /* Solve the ILP problem constructed in setup_lp.
1478 * For each node such that all the remaining rows of its schedule
1479 * need to be non-trivial, we construct a non-triviality region.
1480 * This region imposes that the next row is independent of previous rows.
1481 * In particular the coefficients c_i_x are represented by t_i_x
1482 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
1483 * its first columns span the rows of the previously computed part
1484 * of the schedule. The non-triviality region enforces that at least
1485 * one of the remaining components of t_i_x is non-zero, i.e.,
1486 * that the new schedule row depends on at least one of the remaining
1487 * columns of Q.
1489 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
1491 int i;
1492 isl_vec *sol;
1493 isl_basic_set *lp;
1495 for (i = 0; i < graph->n; ++i) {
1496 struct isl_sched_node *node = &graph->node[i];
1497 int skip = node->rank;
1498 graph->region[i].pos = node->start + 1 + 2*(node->nparam+skip);
1499 if (needs_row(graph, node))
1500 graph->region[i].len = 2 * (node->nvar - skip);
1501 else
1502 graph->region[i].len = 0;
1504 lp = isl_basic_set_copy(graph->lp);
1505 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
1506 graph->region, &check_conflict, graph);
1507 return sol;
1510 /* Update the schedules of all nodes based on the given solution
1511 * of the LP problem.
1512 * The new row is added to the current band.
1513 * All possibly negative coefficients are encoded as a difference
1514 * of two non-negative variables, so we need to perform the subtraction
1515 * here. Moreover, if use_cmap is set, then the solution does
1516 * not refer to the actual coefficients c_i_x, but instead to variables
1517 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
1518 * In this case, we then also need to perform this multiplication
1519 * to obtain the values of c_i_x.
1521 * If check_zero is set, then the first two coordinates of sol are
1522 * assumed to correspond to the dependence distance. If these two
1523 * coordinates are zero, then the corresponding scheduling dimension
1524 * is marked as being zero distance.
1526 static int update_schedule(struct isl_sched_graph *graph,
1527 __isl_take isl_vec *sol, int use_cmap, int check_zero)
1529 int i, j;
1530 int zero = 0;
1531 isl_vec *csol = NULL;
1533 if (!sol)
1534 goto error;
1535 if (sol->size == 0)
1536 isl_die(sol->ctx, isl_error_internal,
1537 "no solution found", goto error);
1538 if (graph->n_total_row >= graph->max_row)
1539 isl_die(sol->ctx, isl_error_internal,
1540 "too many schedule rows", goto error);
1542 if (check_zero)
1543 zero = isl_int_is_zero(sol->el[1]) &&
1544 isl_int_is_zero(sol->el[2]);
1546 for (i = 0; i < graph->n; ++i) {
1547 struct isl_sched_node *node = &graph->node[i];
1548 int pos = node->start;
1549 int row = isl_mat_rows(node->sched);
1551 isl_vec_free(csol);
1552 csol = isl_vec_alloc(sol->ctx, node->nvar);
1553 if (!csol)
1554 goto error;
1556 isl_map_free(node->sched_map);
1557 node->sched_map = NULL;
1558 node->sched = isl_mat_add_rows(node->sched, 1);
1559 if (!node->sched)
1560 goto error;
1561 node->sched = isl_mat_set_element(node->sched, row, 0,
1562 sol->el[1 + pos]);
1563 for (j = 0; j < node->nparam + node->nvar; ++j)
1564 isl_int_sub(sol->el[1 + pos + 1 + 2 * j + 1],
1565 sol->el[1 + pos + 1 + 2 * j + 1],
1566 sol->el[1 + pos + 1 + 2 * j]);
1567 for (j = 0; j < node->nparam; ++j)
1568 node->sched = isl_mat_set_element(node->sched,
1569 row, 1 + j, sol->el[1+pos+1+2*j+1]);
1570 for (j = 0; j < node->nvar; ++j)
1571 isl_int_set(csol->el[j],
1572 sol->el[1+pos+1+2*(node->nparam+j)+1]);
1573 if (use_cmap)
1574 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
1575 csol);
1576 if (!csol)
1577 goto error;
1578 for (j = 0; j < node->nvar; ++j)
1579 node->sched = isl_mat_set_element(node->sched,
1580 row, 1 + node->nparam + j, csol->el[j]);
1581 node->band[graph->n_total_row] = graph->n_band;
1582 node->zero[graph->n_total_row] = zero;
1584 isl_vec_free(sol);
1585 isl_vec_free(csol);
1587 graph->n_row++;
1588 graph->n_total_row++;
1590 return 0;
1591 error:
1592 isl_vec_free(sol);
1593 isl_vec_free(csol);
1594 return -1;
1597 /* Convert node->sched into a multi_aff and return this multi_aff.
1599 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
1600 struct isl_sched_node *node)
1602 int i, j;
1603 isl_space *space;
1604 isl_local_space *ls;
1605 isl_aff *aff;
1606 isl_multi_aff *ma;
1607 int nrow, ncol;
1608 isl_int v;
1610 nrow = isl_mat_rows(node->sched);
1611 ncol = isl_mat_cols(node->sched) - 1;
1612 space = isl_space_from_domain(isl_space_copy(node->dim));
1613 space = isl_space_add_dims(space, isl_dim_out, nrow);
1614 ma = isl_multi_aff_zero(space);
1615 ls = isl_local_space_from_space(isl_space_copy(node->dim));
1617 isl_int_init(v);
1619 for (i = 0; i < nrow; ++i) {
1620 aff = isl_aff_zero_on_domain(isl_local_space_copy(ls));
1621 isl_mat_get_element(node->sched, i, 0, &v);
1622 aff = isl_aff_set_constant(aff, v);
1623 for (j = 0; j < node->nparam; ++j) {
1624 isl_mat_get_element(node->sched, i, 1 + j, &v);
1625 aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
1627 for (j = 0; j < node->nvar; ++j) {
1628 isl_mat_get_element(node->sched,
1629 i, 1 + node->nparam + j, &v);
1630 aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
1632 ma = isl_multi_aff_set_aff(ma, i, aff);
1635 isl_int_clear(v);
1637 isl_local_space_free(ls);
1639 return ma;
1642 /* Convert node->sched into a map and return this map.
1644 * The result is cached in node->sched_map, which needs to be released
1645 * whenever node->sched is updated.
1647 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
1649 if (!node->sched_map) {
1650 isl_multi_aff *ma;
1652 ma = node_extract_schedule_multi_aff(node);
1653 node->sched_map = isl_map_from_multi_aff(ma);
1656 return isl_map_copy(node->sched_map);
1659 /* Update the given dependence relation based on the current schedule.
1660 * That is, intersect the dependence relation with a map expressing
1661 * that source and sink are executed within the same iteration of
1662 * the current schedule.
1663 * This is not the most efficient way, but this shouldn't be a critical
1664 * operation.
1666 static __isl_give isl_map *specialize(__isl_take isl_map *map,
1667 struct isl_sched_node *src, struct isl_sched_node *dst)
1669 isl_map *src_sched, *dst_sched, *id;
1671 src_sched = node_extract_schedule(src);
1672 dst_sched = node_extract_schedule(dst);
1673 id = isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
1674 return isl_map_intersect(map, id);
1677 /* Update the dependence relations of all edges based on the current schedule.
1678 * If a dependence is carried completely by the current schedule, then
1679 * it is removed from the edge_tables. It is kept in the list of edges
1680 * as otherwise all edge_tables would have to be recomputed.
1682 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
1684 int i;
1686 for (i = graph->n_edge - 1; i >= 0; --i) {
1687 struct isl_sched_edge *edge = &graph->edge[i];
1688 edge->map = specialize(edge->map, edge->src, edge->dst);
1689 if (!edge->map)
1690 return -1;
1692 if (isl_map_plain_is_empty(edge->map))
1693 graph_remove_edge(graph, edge);
1696 return 0;
1699 static void next_band(struct isl_sched_graph *graph)
1701 graph->band_start = graph->n_total_row;
1702 graph->n_band++;
1705 /* Topologically sort statements mapped to the same schedule iteration
1706 * and add a row to the schedule corresponding to this order.
1708 static int sort_statements(isl_ctx *ctx, struct isl_sched_graph *graph)
1710 int i, j;
1712 if (graph->n <= 1)
1713 return 0;
1715 if (update_edges(ctx, graph) < 0)
1716 return -1;
1718 if (graph->n_edge == 0)
1719 return 0;
1721 if (detect_sccs(ctx, graph) < 0)
1722 return -1;
1724 if (graph->n_total_row >= graph->max_row)
1725 isl_die(ctx, isl_error_internal,
1726 "too many schedule rows", return -1);
1728 for (i = 0; i < graph->n; ++i) {
1729 struct isl_sched_node *node = &graph->node[i];
1730 int row = isl_mat_rows(node->sched);
1731 int cols = isl_mat_cols(node->sched);
1733 isl_map_free(node->sched_map);
1734 node->sched_map = NULL;
1735 node->sched = isl_mat_add_rows(node->sched, 1);
1736 if (!node->sched)
1737 return -1;
1738 node->sched = isl_mat_set_element_si(node->sched, row, 0,
1739 node->scc);
1740 for (j = 1; j < cols; ++j)
1741 node->sched = isl_mat_set_element_si(node->sched,
1742 row, j, 0);
1743 node->band[graph->n_total_row] = graph->n_band;
1746 graph->n_total_row++;
1747 next_band(graph);
1749 return 0;
1752 /* Construct an isl_schedule based on the computed schedule stored
1753 * in graph and with parameters specified by dim.
1755 static __isl_give isl_schedule *extract_schedule(struct isl_sched_graph *graph,
1756 __isl_take isl_space *dim)
1758 int i;
1759 isl_ctx *ctx;
1760 isl_schedule *sched = NULL;
1762 if (!dim)
1763 return NULL;
1765 ctx = isl_space_get_ctx(dim);
1766 sched = isl_calloc(ctx, struct isl_schedule,
1767 sizeof(struct isl_schedule) +
1768 (graph->n - 1) * sizeof(struct isl_schedule_node));
1769 if (!sched)
1770 goto error;
1772 sched->ref = 1;
1773 sched->n = graph->n;
1774 sched->n_band = graph->n_band;
1775 sched->n_total_row = graph->n_total_row;
1777 for (i = 0; i < sched->n; ++i) {
1778 int r, b;
1779 int *band_end, *band_id, *zero;
1781 sched->node[i].sched =
1782 node_extract_schedule_multi_aff(&graph->node[i]);
1783 if (!sched->node[i].sched)
1784 goto error;
1786 sched->node[i].n_band = graph->n_band;
1787 if (graph->n_band == 0)
1788 continue;
1790 band_end = isl_alloc_array(ctx, int, graph->n_band);
1791 band_id = isl_alloc_array(ctx, int, graph->n_band);
1792 zero = isl_alloc_array(ctx, int, graph->n_total_row);
1793 sched->node[i].band_end = band_end;
1794 sched->node[i].band_id = band_id;
1795 sched->node[i].zero = zero;
1796 if (!band_end || !band_id || !zero)
1797 goto error;
1799 for (r = 0; r < graph->n_total_row; ++r)
1800 zero[r] = graph->node[i].zero[r];
1801 for (r = b = 0; r < graph->n_total_row; ++r) {
1802 if (graph->node[i].band[r] == b)
1803 continue;
1804 band_end[b++] = r;
1805 if (graph->node[i].band[r] == -1)
1806 break;
1808 if (r == graph->n_total_row)
1809 band_end[b++] = r;
1810 sched->node[i].n_band = b;
1811 for (--b; b >= 0; --b)
1812 band_id[b] = graph->node[i].band_id[b];
1815 sched->dim = dim;
1817 return sched;
1818 error:
1819 isl_space_free(dim);
1820 isl_schedule_free(sched);
1821 return NULL;
1824 /* Copy nodes that satisfy node_pred from the src dependence graph
1825 * to the dst dependence graph.
1827 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
1828 int (*node_pred)(struct isl_sched_node *node, int data), int data)
1830 int i;
1832 dst->n = 0;
1833 for (i = 0; i < src->n; ++i) {
1834 if (!node_pred(&src->node[i], data))
1835 continue;
1836 dst->node[dst->n].dim = isl_space_copy(src->node[i].dim);
1837 dst->node[dst->n].nvar = src->node[i].nvar;
1838 dst->node[dst->n].nparam = src->node[i].nparam;
1839 dst->node[dst->n].sched = isl_mat_copy(src->node[i].sched);
1840 dst->node[dst->n].sched_map =
1841 isl_map_copy(src->node[i].sched_map);
1842 dst->node[dst->n].band = src->node[i].band;
1843 dst->node[dst->n].band_id = src->node[i].band_id;
1844 dst->node[dst->n].zero = src->node[i].zero;
1845 dst->n++;
1848 return 0;
1851 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
1852 * to the dst dependence graph.
1853 * If the source or destination node of the edge is not in the destination
1854 * graph, then it must be a backward proximity edge and it should simply
1855 * be ignored.
1857 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
1858 struct isl_sched_graph *src,
1859 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
1861 int i;
1862 enum isl_edge_type t;
1864 dst->n_edge = 0;
1865 for (i = 0; i < src->n_edge; ++i) {
1866 struct isl_sched_edge *edge = &src->edge[i];
1867 isl_map *map;
1868 struct isl_sched_node *dst_src, *dst_dst;
1870 if (!edge_pred(edge, data))
1871 continue;
1873 if (isl_map_plain_is_empty(edge->map))
1874 continue;
1876 dst_src = graph_find_node(ctx, dst, edge->src->dim);
1877 dst_dst = graph_find_node(ctx, dst, edge->dst->dim);
1878 if (!dst_src || !dst_dst) {
1879 if (edge->validity)
1880 isl_die(ctx, isl_error_internal,
1881 "backward validity edge", return -1);
1882 continue;
1885 map = isl_map_copy(edge->map);
1887 dst->edge[dst->n_edge].src = dst_src;
1888 dst->edge[dst->n_edge].dst = dst_dst;
1889 dst->edge[dst->n_edge].map = map;
1890 dst->edge[dst->n_edge].validity = edge->validity;
1891 dst->edge[dst->n_edge].proximity = edge->proximity;
1892 dst->n_edge++;
1894 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
1895 if (edge !=
1896 graph_find_edge(src, t, edge->src, edge->dst))
1897 continue;
1898 if (graph_edge_table_add(ctx, dst, t,
1899 &dst->edge[dst->n_edge - 1]) < 0)
1900 return -1;
1904 return 0;
1907 /* Given a "src" dependence graph that contains the nodes from "dst"
1908 * that satisfy node_pred, copy the schedule computed in "src"
1909 * for those nodes back to "dst".
1911 static int copy_schedule(struct isl_sched_graph *dst,
1912 struct isl_sched_graph *src,
1913 int (*node_pred)(struct isl_sched_node *node, int data), int data)
1915 int i;
1917 src->n = 0;
1918 for (i = 0; i < dst->n; ++i) {
1919 if (!node_pred(&dst->node[i], data))
1920 continue;
1921 isl_mat_free(dst->node[i].sched);
1922 isl_map_free(dst->node[i].sched_map);
1923 dst->node[i].sched = isl_mat_copy(src->node[src->n].sched);
1924 dst->node[i].sched_map =
1925 isl_map_copy(src->node[src->n].sched_map);
1926 src->n++;
1929 dst->max_row = src->max_row;
1930 dst->n_total_row = src->n_total_row;
1931 dst->n_band = src->n_band;
1933 return 0;
1936 /* Compute the maximal number of variables over all nodes.
1937 * This is the maximal number of linearly independent schedule
1938 * rows that we need to compute.
1939 * Just in case we end up in a part of the dependence graph
1940 * with only lower-dimensional domains, we make sure we will
1941 * compute the required amount of extra linearly independent rows.
1943 static int compute_maxvar(struct isl_sched_graph *graph)
1945 int i;
1947 graph->maxvar = 0;
1948 for (i = 0; i < graph->n; ++i) {
1949 struct isl_sched_node *node = &graph->node[i];
1950 int nvar;
1952 if (node_update_cmap(node) < 0)
1953 return -1;
1954 nvar = node->nvar + graph->n_row - node->rank;
1955 if (nvar > graph->maxvar)
1956 graph->maxvar = nvar;
1959 return 0;
1962 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph);
1963 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph);
1965 /* Compute a schedule for a subgraph of "graph". In particular, for
1966 * the graph composed of nodes that satisfy node_pred and edges that
1967 * that satisfy edge_pred. The caller should precompute the number
1968 * of nodes and edges that satisfy these predicates and pass them along
1969 * as "n" and "n_edge".
1970 * If the subgraph is known to consist of a single component, then wcc should
1971 * be set and then we call compute_schedule_wcc on the constructed subgraph.
1972 * Otherwise, we call compute_schedule, which will check whether the subgraph
1973 * is connected.
1975 static int compute_sub_schedule(isl_ctx *ctx,
1976 struct isl_sched_graph *graph, int n, int n_edge,
1977 int (*node_pred)(struct isl_sched_node *node, int data),
1978 int (*edge_pred)(struct isl_sched_edge *edge, int data),
1979 int data, int wcc)
1981 struct isl_sched_graph split = { 0 };
1982 int t;
1984 if (graph_alloc(ctx, &split, n, n_edge) < 0)
1985 goto error;
1986 if (copy_nodes(&split, graph, node_pred, data) < 0)
1987 goto error;
1988 if (graph_init_table(ctx, &split) < 0)
1989 goto error;
1990 for (t = 0; t <= isl_edge_last; ++t)
1991 split.max_edge[t] = graph->max_edge[t];
1992 if (graph_init_edge_tables(ctx, &split) < 0)
1993 goto error;
1994 if (copy_edges(ctx, &split, graph, edge_pred, data) < 0)
1995 goto error;
1996 split.n_row = graph->n_row;
1997 split.max_row = graph->max_row;
1998 split.n_total_row = graph->n_total_row;
1999 split.n_band = graph->n_band;
2000 split.band_start = graph->band_start;
2002 if (wcc && compute_schedule_wcc(ctx, &split) < 0)
2003 goto error;
2004 if (!wcc && compute_schedule(ctx, &split) < 0)
2005 goto error;
2007 copy_schedule(graph, &split, node_pred, data);
2009 graph_free(ctx, &split);
2010 return 0;
2011 error:
2012 graph_free(ctx, &split);
2013 return -1;
2016 static int node_scc_exactly(struct isl_sched_node *node, int scc)
2018 return node->scc == scc;
2021 static int node_scc_at_most(struct isl_sched_node *node, int scc)
2023 return node->scc <= scc;
2026 static int node_scc_at_least(struct isl_sched_node *node, int scc)
2028 return node->scc >= scc;
2031 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
2033 return edge->src->scc == scc && edge->dst->scc == scc;
2036 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
2038 return edge->dst->scc <= scc;
2041 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
2043 return edge->src->scc >= scc;
2046 /* Pad the schedules of all nodes with zero rows such that in the end
2047 * they all have graph->n_total_row rows.
2048 * The extra rows don't belong to any band, so they get assigned band number -1.
2050 static int pad_schedule(struct isl_sched_graph *graph)
2052 int i, j;
2054 for (i = 0; i < graph->n; ++i) {
2055 struct isl_sched_node *node = &graph->node[i];
2056 int row = isl_mat_rows(node->sched);
2057 if (graph->n_total_row > row) {
2058 isl_map_free(node->sched_map);
2059 node->sched_map = NULL;
2061 node->sched = isl_mat_add_zero_rows(node->sched,
2062 graph->n_total_row - row);
2063 if (!node->sched)
2064 return -1;
2065 for (j = row; j < graph->n_total_row; ++j)
2066 node->band[j] = -1;
2069 return 0;
2072 /* Split the current graph into two parts and compute a schedule for each
2073 * part individually. In particular, one part consists of all SCCs up
2074 * to and including graph->src_scc, while the other part contains the other
2075 * SCCS.
2077 * The split is enforced in the schedule by constant rows with two different
2078 * values (0 and 1). These constant rows replace the previously computed rows
2079 * in the current band.
2080 * It would be possible to reuse them as the first rows in the next
2081 * band, but recomputing them may result in better rows as we are looking
2082 * at a smaller part of the dependence graph.
2083 * compute_split_schedule is only called when no zero-distance schedule row
2084 * could be found on the entire graph, so we wark the splitting row as
2085 * non zero-distance.
2087 * The band_id of the second group is set to n, where n is the number
2088 * of nodes in the first group. This ensures that the band_ids over
2089 * the two groups remain disjoint, even if either or both of the two
2090 * groups contain independent components.
2092 static int compute_split_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
2094 int i, j, n, e1, e2;
2095 int n_total_row, orig_total_row;
2096 int n_band, orig_band;
2097 int drop;
2099 if (graph->n_total_row >= graph->max_row)
2100 isl_die(ctx, isl_error_internal,
2101 "too many schedule rows", return -1);
2103 drop = graph->n_total_row - graph->band_start;
2104 graph->n_total_row -= drop;
2105 graph->n_row -= drop;
2107 n = 0;
2108 for (i = 0; i < graph->n; ++i) {
2109 struct isl_sched_node *node = &graph->node[i];
2110 int row = isl_mat_rows(node->sched) - drop;
2111 int cols = isl_mat_cols(node->sched);
2112 int before = node->scc <= graph->src_scc;
2114 if (before)
2115 n++;
2117 isl_map_free(node->sched_map);
2118 node->sched_map = NULL;
2119 node->sched = isl_mat_drop_rows(node->sched,
2120 graph->band_start, drop);
2121 node->sched = isl_mat_add_rows(node->sched, 1);
2122 if (!node->sched)
2123 return -1;
2124 node->sched = isl_mat_set_element_si(node->sched, row, 0,
2125 !before);
2126 for (j = 1; j < cols; ++j)
2127 node->sched = isl_mat_set_element_si(node->sched,
2128 row, j, 0);
2129 node->band[graph->n_total_row] = graph->n_band;
2130 node->zero[graph->n_total_row] = 0;
2133 e1 = e2 = 0;
2134 for (i = 0; i < graph->n_edge; ++i) {
2135 if (graph->edge[i].dst->scc <= graph->src_scc)
2136 e1++;
2137 if (graph->edge[i].src->scc > graph->src_scc)
2138 e2++;
2141 graph->n_total_row++;
2142 next_band(graph);
2144 for (i = 0; i < graph->n; ++i) {
2145 struct isl_sched_node *node = &graph->node[i];
2146 if (node->scc > graph->src_scc)
2147 node->band_id[graph->n_band] = n;
2150 orig_total_row = graph->n_total_row;
2151 orig_band = graph->n_band;
2152 if (compute_sub_schedule(ctx, graph, n, e1,
2153 &node_scc_at_most, &edge_dst_scc_at_most,
2154 graph->src_scc, 0) < 0)
2155 return -1;
2156 n_total_row = graph->n_total_row;
2157 graph->n_total_row = orig_total_row;
2158 n_band = graph->n_band;
2159 graph->n_band = orig_band;
2160 if (compute_sub_schedule(ctx, graph, graph->n - n, e2,
2161 &node_scc_at_least, &edge_src_scc_at_least,
2162 graph->src_scc + 1, 0) < 0)
2163 return -1;
2164 if (n_total_row > graph->n_total_row)
2165 graph->n_total_row = n_total_row;
2166 if (n_band > graph->n_band)
2167 graph->n_band = n_band;
2169 return pad_schedule(graph);
2172 /* Compute the next band of the schedule after updating the dependence
2173 * relations based on the the current schedule.
2175 static int compute_next_band(isl_ctx *ctx, struct isl_sched_graph *graph)
2177 if (update_edges(ctx, graph) < 0)
2178 return -1;
2179 next_band(graph);
2181 return compute_schedule(ctx, graph);
2184 /* Add constraints to graph->lp that force the dependence "map" (which
2185 * is part of the dependence relation of "edge")
2186 * to be respected and attempt to carry it, where the edge is one from
2187 * a node j to itself. "pos" is the sequence number of the given map.
2188 * That is, add constraints that enforce
2190 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
2191 * = c_j_x (y - x) >= e_i
2193 * for each (x,y) in R.
2194 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2195 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
2196 * with each coefficient in c_j_x represented as a pair of non-negative
2197 * coefficients.
2199 static int add_intra_constraints(struct isl_sched_graph *graph,
2200 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
2202 unsigned total;
2203 isl_ctx *ctx = isl_map_get_ctx(map);
2204 isl_space *dim;
2205 isl_dim_map *dim_map;
2206 isl_basic_set *coef;
2207 struct isl_sched_node *node = edge->src;
2209 coef = intra_coefficients(graph, map);
2210 if (!coef)
2211 return -1;
2213 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
2215 total = isl_basic_set_total_dim(graph->lp);
2216 dim_map = isl_dim_map_alloc(ctx, total);
2217 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
2218 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
2219 isl_space_dim(dim, isl_dim_set), 1,
2220 node->nvar, -1);
2221 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
2222 isl_space_dim(dim, isl_dim_set), 1,
2223 node->nvar, 1);
2224 graph->lp = isl_basic_set_extend_constraints(graph->lp,
2225 coef->n_eq, coef->n_ineq);
2226 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
2227 coef, dim_map);
2228 isl_space_free(dim);
2230 return 0;
2233 /* Add constraints to graph->lp that force the dependence "map" (which
2234 * is part of the dependence relation of "edge")
2235 * to be respected and attempt to carry it, where the edge is one from
2236 * node j to node k. "pos" is the sequence number of the given map.
2237 * That is, add constraints that enforce
2239 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
2241 * for each (x,y) in R.
2242 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2243 * of valid constraints for R and then plug in
2244 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
2245 * with each coefficient (except e_i, c_k_0 and c_j_0)
2246 * represented as a pair of non-negative coefficients.
2248 static int add_inter_constraints(struct isl_sched_graph *graph,
2249 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
2251 unsigned total;
2252 isl_ctx *ctx = isl_map_get_ctx(map);
2253 isl_space *dim;
2254 isl_dim_map *dim_map;
2255 isl_basic_set *coef;
2256 struct isl_sched_node *src = edge->src;
2257 struct isl_sched_node *dst = edge->dst;
2259 coef = inter_coefficients(graph, map);
2260 if (!coef)
2261 return -1;
2263 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
2265 total = isl_basic_set_total_dim(graph->lp);
2266 dim_map = isl_dim_map_alloc(ctx, total);
2268 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
2270 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
2271 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
2272 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
2273 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
2274 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
2275 dst->nvar, -1);
2276 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
2277 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
2278 dst->nvar, 1);
2280 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
2281 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
2282 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
2283 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
2284 isl_space_dim(dim, isl_dim_set), 1,
2285 src->nvar, 1);
2286 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
2287 isl_space_dim(dim, isl_dim_set), 1,
2288 src->nvar, -1);
2290 graph->lp = isl_basic_set_extend_constraints(graph->lp,
2291 coef->n_eq, coef->n_ineq);
2292 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
2293 coef, dim_map);
2294 isl_space_free(dim);
2296 return 0;
2299 /* Add constraints to graph->lp that force all validity dependences
2300 * to be respected and attempt to carry them.
2302 static int add_all_constraints(struct isl_sched_graph *graph)
2304 int i, j;
2305 int pos;
2307 pos = 0;
2308 for (i = 0; i < graph->n_edge; ++i) {
2309 struct isl_sched_edge *edge= &graph->edge[i];
2311 if (!edge->validity)
2312 continue;
2314 for (j = 0; j < edge->map->n; ++j) {
2315 isl_basic_map *bmap;
2316 isl_map *map;
2318 bmap = isl_basic_map_copy(edge->map->p[j]);
2319 map = isl_map_from_basic_map(bmap);
2321 if (edge->src == edge->dst &&
2322 add_intra_constraints(graph, edge, map, pos) < 0)
2323 return -1;
2324 if (edge->src != edge->dst &&
2325 add_inter_constraints(graph, edge, map, pos) < 0)
2326 return -1;
2327 ++pos;
2331 return 0;
2334 /* Count the number of equality and inequality constraints
2335 * that will be added to the carry_lp problem.
2336 * We count each edge exactly once.
2338 static int count_all_constraints(struct isl_sched_graph *graph,
2339 int *n_eq, int *n_ineq)
2341 int i, j;
2343 *n_eq = *n_ineq = 0;
2344 for (i = 0; i < graph->n_edge; ++i) {
2345 struct isl_sched_edge *edge= &graph->edge[i];
2346 for (j = 0; j < edge->map->n; ++j) {
2347 isl_basic_map *bmap;
2348 isl_map *map;
2350 bmap = isl_basic_map_copy(edge->map->p[j]);
2351 map = isl_map_from_basic_map(bmap);
2353 if (count_map_constraints(graph, edge, map,
2354 n_eq, n_ineq, 1) < 0)
2355 return -1;
2359 return 0;
2362 /* Construct an LP problem for finding schedule coefficients
2363 * such that the schedule carries as many dependences as possible.
2364 * In particular, for each dependence i, we bound the dependence distance
2365 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
2366 * of all e_i's. Dependence with e_i = 0 in the solution are simply
2367 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
2368 * Note that if the dependence relation is a union of basic maps,
2369 * then we have to consider each basic map individually as it may only
2370 * be possible to carry the dependences expressed by some of those
2371 * basic maps and not all off them.
2372 * Below, we consider each of those basic maps as a separate "edge".
2374 * All variables of the LP are non-negative. The actual coefficients
2375 * may be negative, so each coefficient is represented as the difference
2376 * of two non-negative variables. The negative part always appears
2377 * immediately before the positive part.
2378 * Other than that, the variables have the following order
2380 * - sum of (1 - e_i) over all edges
2381 * - sum of positive and negative parts of all c_n coefficients
2382 * (unconstrained when computing non-parametric schedules)
2383 * - sum of positive and negative parts of all c_x coefficients
2384 * - for each edge
2385 * - e_i
2386 * - for each node
2387 * - c_i_0
2388 * - positive and negative parts of c_i_n (if parametric)
2389 * - positive and negative parts of c_i_x
2391 * The constraints are those from the (validity) edges plus three equalities
2392 * to express the sums and n_edge inequalities to express e_i <= 1.
2394 static int setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
2396 int i, j;
2397 int k;
2398 isl_space *dim;
2399 unsigned total;
2400 int n_eq, n_ineq;
2401 int n_edge;
2403 n_edge = 0;
2404 for (i = 0; i < graph->n_edge; ++i)
2405 n_edge += graph->edge[i].map->n;
2407 total = 3 + n_edge;
2408 for (i = 0; i < graph->n; ++i) {
2409 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2410 node->start = total;
2411 total += 1 + 2 * (node->nparam + node->nvar);
2414 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
2415 return -1;
2416 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
2417 return -1;
2419 dim = isl_space_set_alloc(ctx, 0, total);
2420 isl_basic_set_free(graph->lp);
2421 n_eq += 3;
2422 n_ineq += n_edge;
2423 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
2424 graph->lp = isl_basic_set_set_rational(graph->lp);
2426 k = isl_basic_set_alloc_equality(graph->lp);
2427 if (k < 0)
2428 return -1;
2429 isl_seq_clr(graph->lp->eq[k], 1 + total);
2430 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
2431 isl_int_set_si(graph->lp->eq[k][1], 1);
2432 for (i = 0; i < n_edge; ++i)
2433 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
2435 k = isl_basic_set_alloc_equality(graph->lp);
2436 if (k < 0)
2437 return -1;
2438 isl_seq_clr(graph->lp->eq[k], 1 + total);
2439 isl_int_set_si(graph->lp->eq[k][2], -1);
2440 for (i = 0; i < graph->n; ++i) {
2441 int pos = 1 + graph->node[i].start + 1;
2443 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
2444 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2447 k = isl_basic_set_alloc_equality(graph->lp);
2448 if (k < 0)
2449 return -1;
2450 isl_seq_clr(graph->lp->eq[k], 1 + total);
2451 isl_int_set_si(graph->lp->eq[k][3], -1);
2452 for (i = 0; i < graph->n; ++i) {
2453 struct isl_sched_node *node = &graph->node[i];
2454 int pos = 1 + node->start + 1 + 2 * node->nparam;
2456 for (j = 0; j < 2 * node->nvar; ++j)
2457 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2460 for (i = 0; i < n_edge; ++i) {
2461 k = isl_basic_set_alloc_inequality(graph->lp);
2462 if (k < 0)
2463 return -1;
2464 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2465 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
2466 isl_int_set_si(graph->lp->ineq[k][0], 1);
2469 if (add_bound_coefficient_constraints(ctx, graph) < 0)
2470 return -1;
2471 if (add_all_constraints(graph) < 0)
2472 return -1;
2474 return 0;
2477 /* If the schedule_split_scaled option is set and if the linear
2478 * parts of the scheduling rows for all nodes in the graphs have
2479 * non-trivial common divisor, then split off the constant term
2480 * from the linear part.
2481 * The constant term is then placed in a separate band and
2482 * the linear part is reduced.
2484 static int split_scaled(isl_ctx *ctx, struct isl_sched_graph *graph)
2486 int i;
2487 int row;
2488 isl_int gcd, gcd_i;
2490 if (!ctx->opt->schedule_split_scaled)
2491 return 0;
2492 if (graph->n <= 1)
2493 return 0;
2495 if (graph->n_total_row >= graph->max_row)
2496 isl_die(ctx, isl_error_internal,
2497 "too many schedule rows", return -1);
2499 isl_int_init(gcd);
2500 isl_int_init(gcd_i);
2502 isl_int_set_si(gcd, 0);
2504 row = isl_mat_rows(graph->node[0].sched) - 1;
2506 for (i = 0; i < graph->n; ++i) {
2507 struct isl_sched_node *node = &graph->node[i];
2508 int cols = isl_mat_cols(node->sched);
2510 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
2511 isl_int_gcd(gcd, gcd, gcd_i);
2514 isl_int_clear(gcd_i);
2516 if (isl_int_cmp_si(gcd, 1) <= 0) {
2517 isl_int_clear(gcd);
2518 return 0;
2521 next_band(graph);
2523 for (i = 0; i < graph->n; ++i) {
2524 struct isl_sched_node *node = &graph->node[i];
2526 isl_map_free(node->sched_map);
2527 node->sched_map = NULL;
2528 node->sched = isl_mat_add_zero_rows(node->sched, 1);
2529 if (!node->sched)
2530 goto error;
2531 isl_int_fdiv_r(node->sched->row[row + 1][0],
2532 node->sched->row[row][0], gcd);
2533 isl_int_fdiv_q(node->sched->row[row][0],
2534 node->sched->row[row][0], gcd);
2535 isl_int_mul(node->sched->row[row][0],
2536 node->sched->row[row][0], gcd);
2537 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
2538 if (!node->sched)
2539 goto error;
2540 node->band[graph->n_total_row] = graph->n_band;
2543 graph->n_total_row++;
2545 isl_int_clear(gcd);
2546 return 0;
2547 error:
2548 isl_int_clear(gcd);
2549 return -1;
2552 static int compute_component_schedule(isl_ctx *ctx,
2553 struct isl_sched_graph *graph);
2555 /* Is the schedule row "sol" trivial on node "node"?
2556 * That is, is the solution zero on the dimensions orthogonal to
2557 * the previously found solutions?
2558 * Each coefficient is represented as the difference between
2559 * two non-negative values in "sol". The coefficient is then
2560 * zero if those two values are equal to each other.
2562 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
2564 int i;
2565 int pos;
2566 int len;
2568 pos = 1 + node->start + 1 + 2 * (node->nparam + node->rank);
2569 len = 2 * (node->nvar - node->rank);
2571 if (len == 0)
2572 return 0;
2574 for (i = 0; i < len; i += 2)
2575 if (isl_int_ne(sol->el[pos + i], sol->el[pos + i + 1]))
2576 return 0;
2578 return 1;
2581 /* Is the schedule row "sol" trivial on any node where it should
2582 * not be trivial?
2584 static int is_any_trivial(struct isl_sched_graph *graph,
2585 __isl_keep isl_vec *sol)
2587 int i;
2589 for (i = 0; i < graph->n; ++i) {
2590 struct isl_sched_node *node = &graph->node[i];
2592 if (!needs_row(graph, node))
2593 continue;
2594 if (is_trivial(node, sol))
2595 return 1;
2598 return 0;
2601 /* Construct a schedule row for each node such that as many dependences
2602 * as possible are carried and then continue with the next band.
2604 * If the computed schedule row turns out to be trivial on one or
2605 * more nodes where it should not be trivial, then we throw it away
2606 * and try again on each component separately.
2608 static int carry_dependences(isl_ctx *ctx, struct isl_sched_graph *graph)
2610 int i;
2611 int n_edge;
2612 isl_vec *sol;
2613 isl_basic_set *lp;
2615 n_edge = 0;
2616 for (i = 0; i < graph->n_edge; ++i)
2617 n_edge += graph->edge[i].map->n;
2619 if (setup_carry_lp(ctx, graph) < 0)
2620 return -1;
2622 lp = isl_basic_set_copy(graph->lp);
2623 sol = isl_tab_basic_set_non_neg_lexmin(lp);
2624 if (!sol)
2625 return -1;
2627 if (sol->size == 0) {
2628 isl_vec_free(sol);
2629 isl_die(ctx, isl_error_internal,
2630 "error in schedule construction", return -1);
2633 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
2634 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
2635 isl_vec_free(sol);
2636 isl_die(ctx, isl_error_unknown,
2637 "unable to carry dependences", return -1);
2640 if (is_any_trivial(graph, sol)) {
2641 isl_vec_free(sol);
2642 if (graph->scc > 1)
2643 return compute_component_schedule(ctx, graph);
2644 isl_die(ctx, isl_error_unknown,
2645 "unable to construct non-trivial solution", return -1);
2648 if (update_schedule(graph, sol, 0, 0) < 0)
2649 return -1;
2651 if (split_scaled(ctx, graph) < 0)
2652 return -1;
2654 return compute_next_band(ctx, graph);
2657 /* Are there any (non-empty) validity edges in the graph?
2659 static int has_validity_edges(struct isl_sched_graph *graph)
2661 int i;
2663 for (i = 0; i < graph->n_edge; ++i) {
2664 int empty;
2666 empty = isl_map_plain_is_empty(graph->edge[i].map);
2667 if (empty < 0)
2668 return -1;
2669 if (empty)
2670 continue;
2671 if (graph->edge[i].validity)
2672 return 1;
2675 return 0;
2678 /* Should we apply a Feautrier step?
2679 * That is, did the user request the Feautrier algorithm and are
2680 * there any validity dependences (left)?
2682 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
2684 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
2685 return 0;
2687 return has_validity_edges(graph);
2690 /* Compute a schedule for a connected dependence graph using Feautrier's
2691 * multi-dimensional scheduling algorithm.
2692 * The original algorithm is described in [1].
2693 * The main idea is to minimize the number of scheduling dimensions, by
2694 * trying to satisfy as many dependences as possible per scheduling dimension.
2696 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
2697 * Problem, Part II: Multi-Dimensional Time.
2698 * In Intl. Journal of Parallel Programming, 1992.
2700 static int compute_schedule_wcc_feautrier(isl_ctx *ctx,
2701 struct isl_sched_graph *graph)
2703 return carry_dependences(ctx, graph);
2706 /* Compute a schedule for a connected dependence graph.
2707 * We try to find a sequence of as many schedule rows as possible that result
2708 * in non-negative dependence distances (independent of the previous rows
2709 * in the sequence, i.e., such that the sequence is tilable).
2710 * If we can't find any more rows we either
2711 * - split between SCCs and start over (assuming we found an interesting
2712 * pair of SCCs between which to split)
2713 * - continue with the next band (assuming the current band has at least
2714 * one row)
2715 * - try to carry as many dependences as possible and continue with the next
2716 * band
2718 * If Feautrier's algorithm is selected, we first recursively try to satisfy
2719 * as many validity dependences as possible. When all validity dependences
2720 * are satisfied we extend the schedule to a full-dimensional schedule.
2722 * If we manage to complete the schedule, we finish off by topologically
2723 * sorting the statements based on the remaining dependences.
2725 * If ctx->opt->schedule_outer_zero_distance is set, then we force the
2726 * outermost dimension in the current band to be zero distance. If this
2727 * turns out to be impossible, we fall back on the general scheme above
2728 * and try to carry as many dependences as possible.
2730 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph)
2732 int force_zero = 0;
2734 if (detect_sccs(ctx, graph) < 0)
2735 return -1;
2736 if (sort_sccs(graph) < 0)
2737 return -1;
2739 if (compute_maxvar(graph) < 0)
2740 return -1;
2742 if (need_feautrier_step(ctx, graph))
2743 return compute_schedule_wcc_feautrier(ctx, graph);
2745 if (ctx->opt->schedule_outer_zero_distance)
2746 force_zero = 1;
2748 while (graph->n_row < graph->maxvar) {
2749 isl_vec *sol;
2751 graph->src_scc = -1;
2752 graph->dst_scc = -1;
2754 if (setup_lp(ctx, graph, force_zero) < 0)
2755 return -1;
2756 sol = solve_lp(graph);
2757 if (!sol)
2758 return -1;
2759 if (sol->size == 0) {
2760 isl_vec_free(sol);
2761 if (!ctx->opt->schedule_maximize_band_depth &&
2762 graph->n_total_row > graph->band_start)
2763 return compute_next_band(ctx, graph);
2764 if (graph->src_scc >= 0)
2765 return compute_split_schedule(ctx, graph);
2766 if (graph->n_total_row > graph->band_start)
2767 return compute_next_band(ctx, graph);
2768 return carry_dependences(ctx, graph);
2770 if (update_schedule(graph, sol, 1, 1) < 0)
2771 return -1;
2772 force_zero = 0;
2775 if (graph->n_total_row > graph->band_start)
2776 next_band(graph);
2777 return sort_statements(ctx, graph);
2780 /* Add a row to the schedules that separates the SCCs and move
2781 * to the next band.
2783 static int split_on_scc(isl_ctx *ctx, struct isl_sched_graph *graph)
2785 int i;
2787 if (graph->n_total_row >= graph->max_row)
2788 isl_die(ctx, isl_error_internal,
2789 "too many schedule rows", return -1);
2791 for (i = 0; i < graph->n; ++i) {
2792 struct isl_sched_node *node = &graph->node[i];
2793 int row = isl_mat_rows(node->sched);
2795 isl_map_free(node->sched_map);
2796 node->sched_map = NULL;
2797 node->sched = isl_mat_add_zero_rows(node->sched, 1);
2798 node->sched = isl_mat_set_element_si(node->sched, row, 0,
2799 node->scc);
2800 if (!node->sched)
2801 return -1;
2802 node->band[graph->n_total_row] = graph->n_band;
2805 graph->n_total_row++;
2806 next_band(graph);
2808 return 0;
2811 /* Compute a schedule for each component (identified by node->scc)
2812 * of the dependence graph separately and then combine the results.
2813 * Depending on the setting of schedule_fuse, a component may be
2814 * either weakly or strongly connected.
2816 * The band_id is adjusted such that each component has a separate id.
2817 * Note that the band_id may have already been set to a value different
2818 * from zero by compute_split_schedule.
2820 static int compute_component_schedule(isl_ctx *ctx,
2821 struct isl_sched_graph *graph)
2823 int wcc, i;
2824 int n, n_edge;
2825 int n_total_row, orig_total_row;
2826 int n_band, orig_band;
2828 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN ||
2829 ctx->opt->schedule_separate_components)
2830 if (split_on_scc(ctx, graph) < 0)
2831 return -1;
2833 n_total_row = 0;
2834 orig_total_row = graph->n_total_row;
2835 n_band = 0;
2836 orig_band = graph->n_band;
2837 for (i = 0; i < graph->n; ++i)
2838 graph->node[i].band_id[graph->n_band] += graph->node[i].scc;
2839 for (wcc = 0; wcc < graph->scc; ++wcc) {
2840 n = 0;
2841 for (i = 0; i < graph->n; ++i)
2842 if (graph->node[i].scc == wcc)
2843 n++;
2844 n_edge = 0;
2845 for (i = 0; i < graph->n_edge; ++i)
2846 if (graph->edge[i].src->scc == wcc &&
2847 graph->edge[i].dst->scc == wcc)
2848 n_edge++;
2850 if (compute_sub_schedule(ctx, graph, n, n_edge,
2851 &node_scc_exactly,
2852 &edge_scc_exactly, wcc, 1) < 0)
2853 return -1;
2854 if (graph->n_total_row > n_total_row)
2855 n_total_row = graph->n_total_row;
2856 graph->n_total_row = orig_total_row;
2857 if (graph->n_band > n_band)
2858 n_band = graph->n_band;
2859 graph->n_band = orig_band;
2862 graph->n_total_row = n_total_row;
2863 graph->n_band = n_band;
2865 return pad_schedule(graph);
2868 /* Compute a schedule for the given dependence graph.
2869 * We first check if the graph is connected (through validity dependences)
2870 * and, if not, compute a schedule for each component separately.
2871 * If schedule_fuse is set to minimal fusion, then we check for strongly
2872 * connected components instead and compute a separate schedule for
2873 * each such strongly connected component.
2875 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
2877 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN) {
2878 if (detect_sccs(ctx, graph) < 0)
2879 return -1;
2880 } else {
2881 if (detect_wccs(ctx, graph) < 0)
2882 return -1;
2885 if (graph->scc > 1)
2886 return compute_component_schedule(ctx, graph);
2888 return compute_schedule_wcc(ctx, graph);
2891 /* Compute a schedule for the given union of domains that respects
2892 * all the validity dependences.
2893 * If the default isl scheduling algorithm is used, it tries to minimize
2894 * the dependence distances over the proximity dependences.
2895 * If Feautrier's scheduling algorithm is used, the proximity dependence
2896 * distances are only minimized during the extension to a full-dimensional
2897 * schedule.
2899 __isl_give isl_schedule *isl_union_set_compute_schedule(
2900 __isl_take isl_union_set *domain,
2901 __isl_take isl_union_map *validity,
2902 __isl_take isl_union_map *proximity)
2904 isl_ctx *ctx = isl_union_set_get_ctx(domain);
2905 isl_space *dim;
2906 struct isl_sched_graph graph = { 0 };
2907 isl_schedule *sched;
2908 struct isl_extract_edge_data data;
2910 domain = isl_union_set_align_params(domain,
2911 isl_union_map_get_space(validity));
2912 domain = isl_union_set_align_params(domain,
2913 isl_union_map_get_space(proximity));
2914 dim = isl_union_set_get_space(domain);
2915 validity = isl_union_map_align_params(validity, isl_space_copy(dim));
2916 proximity = isl_union_map_align_params(proximity, dim);
2918 if (!domain)
2919 goto error;
2921 graph.n = isl_union_set_n_set(domain);
2922 if (graph.n == 0)
2923 goto empty;
2924 if (graph_alloc(ctx, &graph, graph.n,
2925 isl_union_map_n_map(validity) + isl_union_map_n_map(proximity)) < 0)
2926 goto error;
2927 if (compute_max_row(&graph, domain) < 0)
2928 goto error;
2929 graph.root = 1;
2930 graph.n = 0;
2931 if (isl_union_set_foreach_set(domain, &extract_node, &graph) < 0)
2932 goto error;
2933 if (graph_init_table(ctx, &graph) < 0)
2934 goto error;
2935 graph.max_edge[isl_edge_validity] = isl_union_map_n_map(validity);
2936 graph.max_edge[isl_edge_proximity] = isl_union_map_n_map(proximity);
2937 if (graph_init_edge_tables(ctx, &graph) < 0)
2938 goto error;
2939 graph.n_edge = 0;
2940 data.graph = &graph;
2941 data.type = isl_edge_validity;
2942 if (isl_union_map_foreach_map(validity, &extract_edge, &data) < 0)
2943 goto error;
2944 data.type = isl_edge_proximity;
2945 if (isl_union_map_foreach_map(proximity, &extract_edge, &data) < 0)
2946 goto error;
2948 if (compute_schedule(ctx, &graph) < 0)
2949 goto error;
2951 empty:
2952 sched = extract_schedule(&graph, isl_union_set_get_space(domain));
2954 graph_free(ctx, &graph);
2955 isl_union_set_free(domain);
2956 isl_union_map_free(validity);
2957 isl_union_map_free(proximity);
2959 return sched;
2960 error:
2961 graph_free(ctx, &graph);
2962 isl_union_set_free(domain);
2963 isl_union_map_free(validity);
2964 isl_union_map_free(proximity);
2965 return NULL;
2968 void *isl_schedule_free(__isl_take isl_schedule *sched)
2970 int i;
2971 if (!sched)
2972 return NULL;
2974 if (--sched->ref > 0)
2975 return NULL;
2977 for (i = 0; i < sched->n; ++i) {
2978 isl_multi_aff_free(sched->node[i].sched);
2979 free(sched->node[i].band_end);
2980 free(sched->node[i].band_id);
2981 free(sched->node[i].zero);
2983 isl_space_free(sched->dim);
2984 isl_band_list_free(sched->band_forest);
2985 free(sched);
2986 return NULL;
2989 isl_ctx *isl_schedule_get_ctx(__isl_keep isl_schedule *schedule)
2991 return schedule ? isl_space_get_ctx(schedule->dim) : NULL;
2994 /* Set max_out to the maximal number of output dimensions over
2995 * all maps.
2997 static int update_max_out(__isl_take isl_map *map, void *user)
2999 int *max_out = user;
3000 int n_out = isl_map_dim(map, isl_dim_out);
3002 if (n_out > *max_out)
3003 *max_out = n_out;
3005 isl_map_free(map);
3006 return 0;
3009 /* Internal data structure for map_pad_range.
3011 * "max_out" is the maximal schedule dimension.
3012 * "res" collects the results.
3014 struct isl_pad_schedule_map_data {
3015 int max_out;
3016 isl_union_map *res;
3019 /* Pad the range of the given map with zeros to data->max_out and
3020 * then add the result to data->res.
3022 static int map_pad_range(__isl_take isl_map *map, void *user)
3024 struct isl_pad_schedule_map_data *data = user;
3025 int i;
3026 int n_out = isl_map_dim(map, isl_dim_out);
3028 map = isl_map_add_dims(map, isl_dim_out, data->max_out - n_out);
3029 for (i = n_out; i < data->max_out; ++i)
3030 map = isl_map_fix_si(map, isl_dim_out, i, 0);
3032 data->res = isl_union_map_add_map(data->res, map);
3033 if (!data->res)
3034 return -1;
3036 return 0;
3039 /* Pad the ranges of the maps in the union map with zeros such they all have
3040 * the same dimension.
3042 static __isl_give isl_union_map *pad_schedule_map(
3043 __isl_take isl_union_map *umap)
3045 struct isl_pad_schedule_map_data data;
3047 if (!umap)
3048 return NULL;
3049 if (isl_union_map_n_map(umap) <= 1)
3050 return umap;
3052 data.max_out = 0;
3053 if (isl_union_map_foreach_map(umap, &update_max_out, &data.max_out) < 0)
3054 return isl_union_map_free(umap);
3056 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3057 if (isl_union_map_foreach_map(umap, &map_pad_range, &data) < 0)
3058 data.res = isl_union_map_free(data.res);
3060 isl_union_map_free(umap);
3061 return data.res;
3064 /* Return an isl_union_map of the schedule. If we have already constructed
3065 * a band forest, then this band forest may have been modified so we need
3066 * to extract the isl_union_map from the forest rather than from
3067 * the originally computed schedule. This reconstructed schedule map
3068 * then needs to be padded with zeros to unify the schedule space
3069 * since the result of isl_band_list_get_suffix_schedule may not have
3070 * a unified schedule space.
3072 __isl_give isl_union_map *isl_schedule_get_map(__isl_keep isl_schedule *sched)
3074 int i;
3075 isl_union_map *umap;
3077 if (!sched)
3078 return NULL;
3080 if (sched->band_forest) {
3081 umap = isl_band_list_get_suffix_schedule(sched->band_forest);
3082 return pad_schedule_map(umap);
3085 umap = isl_union_map_empty(isl_space_copy(sched->dim));
3086 for (i = 0; i < sched->n; ++i) {
3087 isl_multi_aff *ma;
3089 ma = isl_multi_aff_copy(sched->node[i].sched);
3090 umap = isl_union_map_add_map(umap, isl_map_from_multi_aff(ma));
3093 return umap;
3096 static __isl_give isl_band_list *construct_band_list(
3097 __isl_keep isl_schedule *schedule, __isl_keep isl_band *parent,
3098 int band_nr, int *parent_active, int n_active);
3100 /* Construct an isl_band structure for the band in the given schedule
3101 * with sequence number band_nr for the n_active nodes marked by active.
3102 * If the nodes don't have a band with the given sequence number,
3103 * then a band without members is created.
3105 * Because of the way the schedule is constructed, we know that
3106 * the position of the band inside the schedule of a node is the same
3107 * for all active nodes.
3109 * The partial schedule for the band is created before the children
3110 * are created to that construct_band_list can refer to the partial
3111 * schedule of the parent.
3113 static __isl_give isl_band *construct_band(__isl_keep isl_schedule *schedule,
3114 __isl_keep isl_band *parent,
3115 int band_nr, int *active, int n_active)
3117 int i, j;
3118 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
3119 isl_band *band;
3120 unsigned start, end;
3122 band = isl_band_alloc(ctx);
3123 if (!band)
3124 return NULL;
3126 band->schedule = schedule;
3127 band->parent = parent;
3129 for (i = 0; i < schedule->n; ++i)
3130 if (active[i])
3131 break;
3133 if (i >= schedule->n)
3134 isl_die(ctx, isl_error_internal,
3135 "band without active statements", goto error);
3137 start = band_nr ? schedule->node[i].band_end[band_nr - 1] : 0;
3138 end = band_nr < schedule->node[i].n_band ?
3139 schedule->node[i].band_end[band_nr] : start;
3140 band->n = end - start;
3142 band->zero = isl_alloc_array(ctx, int, band->n);
3143 if (band->n && !band->zero)
3144 goto error;
3146 for (j = 0; j < band->n; ++j)
3147 band->zero[j] = schedule->node[i].zero[start + j];
3149 band->pma = isl_union_pw_multi_aff_empty(isl_space_copy(schedule->dim));
3150 for (i = 0; i < schedule->n; ++i) {
3151 isl_multi_aff *ma;
3152 isl_pw_multi_aff *pma;
3153 unsigned n_out;
3155 if (!active[i])
3156 continue;
3158 ma = isl_multi_aff_copy(schedule->node[i].sched);
3159 n_out = isl_multi_aff_dim(ma, isl_dim_out);
3160 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, end, n_out - end);
3161 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, 0, start);
3162 pma = isl_pw_multi_aff_from_multi_aff(ma);
3163 band->pma = isl_union_pw_multi_aff_add_pw_multi_aff(band->pma,
3164 pma);
3166 if (!band->pma)
3167 goto error;
3169 for (i = 0; i < schedule->n; ++i)
3170 if (active[i] && schedule->node[i].n_band > band_nr + 1)
3171 break;
3173 if (i < schedule->n) {
3174 band->children = construct_band_list(schedule, band,
3175 band_nr + 1, active, n_active);
3176 if (!band->children)
3177 goto error;
3180 return band;
3181 error:
3182 isl_band_free(band);
3183 return NULL;
3186 /* Internal data structure used inside cmp_band and pw_multi_aff_extract_int.
3188 * r is set to a negative value if anything goes wrong.
3190 * c1 stores the result of extract_int.
3191 * c2 is a temporary value used inside cmp_band_in_ancestor.
3192 * t is a temporary value used inside extract_int.
3194 * first and equal are used inside extract_int.
3195 * first is set if we are looking at the first isl_multi_aff inside
3196 * the isl_union_pw_multi_aff.
3197 * equal is set if all the isl_multi_affs have been equal so far.
3199 struct isl_cmp_band_data {
3200 int r;
3202 int first;
3203 int equal;
3205 isl_int t;
3206 isl_int c1;
3207 isl_int c2;
3210 /* Check if "ma" assigns a constant value.
3211 * Note that this function is only called on isl_multi_affs
3212 * with a single output dimension.
3214 * If "ma" assigns a constant value then we compare it to data->c1
3215 * or assign it to data->c1 if this is the first isl_multi_aff we consider.
3216 * If "ma" does not assign a constant value or if it assigns a value
3217 * that is different from data->c1, then we set data->equal to zero
3218 * and terminate the check.
3220 static int multi_aff_extract_int(__isl_take isl_set *set,
3221 __isl_take isl_multi_aff *ma, void *user)
3223 isl_aff *aff;
3224 struct isl_cmp_band_data *data = user;
3226 aff = isl_multi_aff_get_aff(ma, 0);
3227 data->r = isl_aff_is_cst(aff);
3228 if (data->r >= 0 && data->r) {
3229 isl_aff_get_constant(aff, &data->t);
3230 if (data->first) {
3231 isl_int_set(data->c1, data->t);
3232 data->first = 0;
3233 } else if (!isl_int_eq(data->c1, data->t))
3234 data->equal = 0;
3235 } else if (data->r >= 0 && !data->r)
3236 data->equal = 0;
3238 isl_aff_free(aff);
3239 isl_set_free(set);
3240 isl_multi_aff_free(ma);
3242 if (data->r < 0)
3243 return -1;
3244 if (!data->equal)
3245 return -1;
3246 return 0;
3249 /* This function is called for each isl_pw_multi_aff in
3250 * the isl_union_pw_multi_aff checked by extract_int.
3251 * Check all the isl_multi_affs inside "pma".
3253 static int pw_multi_aff_extract_int(__isl_take isl_pw_multi_aff *pma,
3254 void *user)
3256 int r;
3258 r = isl_pw_multi_aff_foreach_piece(pma, &multi_aff_extract_int, user);
3259 isl_pw_multi_aff_free(pma);
3261 return r;
3264 /* Check if "upma" assigns a single constant value to its domain.
3265 * If so, return 1 and store the result in data->c1.
3266 * If not, return 0.
3268 * A negative return value from isl_union_pw_multi_aff_foreach_pw_multi_aff
3269 * means that either an error occurred or that we have broken off the check
3270 * because we already know the result is going to be negative.
3271 * In the latter case, data->equal is set to zero.
3273 static int extract_int(__isl_keep isl_union_pw_multi_aff *upma,
3274 struct isl_cmp_band_data *data)
3276 data->first = 1;
3277 data->equal = 1;
3279 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3280 &pw_multi_aff_extract_int, data) < 0) {
3281 if (!data->equal)
3282 return 0;
3283 return -1;
3286 return !data->first && data->equal;
3289 /* Compare "b1" and "b2" based on the parent schedule of their ancestor
3290 * "ancestor".
3292 * If the parent of "ancestor" also has a single member, then we
3293 * first try to compare the two band based on the partial schedule
3294 * of this parent.
3296 * Otherwise, or if the result is inconclusive, we look at the partial schedule
3297 * of "ancestor" itself.
3298 * In particular, we specialize the parent schedule based
3299 * on the domains of the child schedules, check if both assign
3300 * a single constant value and, if so, compare the two constant values.
3301 * If the specialized parent schedules do not assign a constant value,
3302 * then they cannot be used to order the two bands and so in this case
3303 * we return 0.
3305 static int cmp_band_in_ancestor(__isl_keep isl_band *b1,
3306 __isl_keep isl_band *b2, struct isl_cmp_band_data *data,
3307 __isl_keep isl_band *ancestor)
3309 isl_union_pw_multi_aff *upma;
3310 isl_union_set *domain;
3311 int r;
3313 if (data->r < 0)
3314 return 0;
3316 if (ancestor->parent && ancestor->parent->n == 1) {
3317 r = cmp_band_in_ancestor(b1, b2, data, ancestor->parent);
3318 if (data->r < 0)
3319 return 0;
3320 if (r)
3321 return r;
3324 upma = isl_union_pw_multi_aff_copy(b1->pma);
3325 domain = isl_union_pw_multi_aff_domain(upma);
3326 upma = isl_union_pw_multi_aff_copy(ancestor->pma);
3327 upma = isl_union_pw_multi_aff_intersect_domain(upma, domain);
3328 r = extract_int(upma, data);
3329 isl_union_pw_multi_aff_free(upma);
3331 if (r < 0)
3332 data->r = -1;
3333 if (r < 0 || !r)
3334 return 0;
3336 isl_int_set(data->c2, data->c1);
3338 upma = isl_union_pw_multi_aff_copy(b2->pma);
3339 domain = isl_union_pw_multi_aff_domain(upma);
3340 upma = isl_union_pw_multi_aff_copy(ancestor->pma);
3341 upma = isl_union_pw_multi_aff_intersect_domain(upma, domain);
3342 r = extract_int(upma, data);
3343 isl_union_pw_multi_aff_free(upma);
3345 if (r < 0)
3346 data->r = -1;
3347 if (r < 0 || !r)
3348 return 0;
3350 return isl_int_cmp(data->c2, data->c1);
3353 /* Compare "a" and "b" based on the parent schedule of their parent.
3355 static int cmp_band(const void *a, const void *b, void *user)
3357 isl_band *b1 = *(isl_band * const *) a;
3358 isl_band *b2 = *(isl_band * const *) b;
3359 struct isl_cmp_band_data *data = user;
3361 return cmp_band_in_ancestor(b1, b2, data, b1->parent);
3364 /* Sort the elements in "list" based on the partial schedules of its parent
3365 * (and ancestors). In particular if the parent assigns constant values
3366 * to the domains of the bands in "list", then the elements are sorted
3367 * according to that order.
3368 * This order should be a more "natural" order for the user, but otherwise
3369 * shouldn't have any effect.
3370 * If we would be constructing an isl_band forest directly in
3371 * isl_union_set_compute_schedule then there wouldn't be any need
3372 * for a reordering, since the children would be added to the list
3373 * in their natural order automatically.
3375 * If there is only one element in the list, then there is no need to sort
3376 * anything.
3377 * If the partial schedule of the parent has more than one member
3378 * (or if there is no parent), then it's
3379 * defnitely not assigning constant values to the different children in
3380 * the list and so we wouldn't be able to use it to sort the list.
3382 static __isl_give isl_band_list *sort_band_list(__isl_take isl_band_list *list,
3383 __isl_keep isl_band *parent)
3385 struct isl_cmp_band_data data;
3387 if (!list)
3388 return NULL;
3389 if (list->n <= 1)
3390 return list;
3391 if (!parent || parent->n != 1)
3392 return list;
3394 data.r = 0;
3395 isl_int_init(data.c1);
3396 isl_int_init(data.c2);
3397 isl_int_init(data.t);
3398 isl_sort(list->p, list->n, sizeof(list->p[0]), &cmp_band, &data);
3399 if (data.r < 0)
3400 list = isl_band_list_free(list);
3401 isl_int_clear(data.c1);
3402 isl_int_clear(data.c2);
3403 isl_int_clear(data.t);
3405 return list;
3408 /* Construct a list of bands that start at the same position (with
3409 * sequence number band_nr) in the schedules of the nodes that
3410 * were active in the parent band.
3412 * A separate isl_band structure is created for each band_id
3413 * and for each node that does not have a band with sequence
3414 * number band_nr. In the latter case, a band without members
3415 * is created.
3416 * This ensures that if a band has any children, then each node
3417 * that was active in the band is active in exactly one of the children.
3419 static __isl_give isl_band_list *construct_band_list(
3420 __isl_keep isl_schedule *schedule, __isl_keep isl_band *parent,
3421 int band_nr, int *parent_active, int n_active)
3423 int i, j;
3424 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
3425 int *active;
3426 int n_band;
3427 isl_band_list *list;
3429 n_band = 0;
3430 for (i = 0; i < n_active; ++i) {
3431 for (j = 0; j < schedule->n; ++j) {
3432 if (!parent_active[j])
3433 continue;
3434 if (schedule->node[j].n_band <= band_nr)
3435 continue;
3436 if (schedule->node[j].band_id[band_nr] == i) {
3437 n_band++;
3438 break;
3442 for (j = 0; j < schedule->n; ++j)
3443 if (schedule->node[j].n_band <= band_nr)
3444 n_band++;
3446 if (n_band == 1) {
3447 isl_band *band;
3448 list = isl_band_list_alloc(ctx, n_band);
3449 band = construct_band(schedule, parent, band_nr,
3450 parent_active, n_active);
3451 return isl_band_list_add(list, band);
3454 active = isl_alloc_array(ctx, int, schedule->n);
3455 if (schedule->n && !active)
3456 return NULL;
3458 list = isl_band_list_alloc(ctx, n_band);
3460 for (i = 0; i < n_active; ++i) {
3461 int n = 0;
3462 isl_band *band;
3464 for (j = 0; j < schedule->n; ++j) {
3465 active[j] = parent_active[j] &&
3466 schedule->node[j].n_band > band_nr &&
3467 schedule->node[j].band_id[band_nr] == i;
3468 if (active[j])
3469 n++;
3471 if (n == 0)
3472 continue;
3474 band = construct_band(schedule, parent, band_nr, active, n);
3476 list = isl_band_list_add(list, band);
3478 for (i = 0; i < schedule->n; ++i) {
3479 isl_band *band;
3480 if (!parent_active[i])
3481 continue;
3482 if (schedule->node[i].n_band > band_nr)
3483 continue;
3484 for (j = 0; j < schedule->n; ++j)
3485 active[j] = j == i;
3486 band = construct_band(schedule, parent, band_nr, active, 1);
3487 list = isl_band_list_add(list, band);
3490 free(active);
3492 list = sort_band_list(list, parent);
3494 return list;
3497 /* Construct a band forest representation of the schedule and
3498 * return the list of roots.
3500 static __isl_give isl_band_list *construct_forest(
3501 __isl_keep isl_schedule *schedule)
3503 int i;
3504 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
3505 isl_band_list *forest;
3506 int *active;
3508 active = isl_alloc_array(ctx, int, schedule->n);
3509 if (schedule->n && !active)
3510 return NULL;
3512 for (i = 0; i < schedule->n; ++i)
3513 active[i] = 1;
3515 forest = construct_band_list(schedule, NULL, 0, active, schedule->n);
3517 free(active);
3519 return forest;
3522 /* Return the roots of a band forest representation of the schedule.
3524 __isl_give isl_band_list *isl_schedule_get_band_forest(
3525 __isl_keep isl_schedule *schedule)
3527 if (!schedule)
3528 return NULL;
3529 if (!schedule->band_forest)
3530 schedule->band_forest = construct_forest(schedule);
3531 return isl_band_list_dup(schedule->band_forest);
3534 /* Call "fn" on each band in the schedule in depth-first post-order.
3536 int isl_schedule_foreach_band(__isl_keep isl_schedule *sched,
3537 int (*fn)(__isl_keep isl_band *band, void *user), void *user)
3539 int r;
3540 isl_band_list *forest;
3542 if (!sched)
3543 return -1;
3545 forest = isl_schedule_get_band_forest(sched);
3546 r = isl_band_list_foreach_band(forest, fn, user);
3547 isl_band_list_free(forest);
3549 return r;
3552 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
3553 __isl_keep isl_band_list *list);
3555 static __isl_give isl_printer *print_band(__isl_take isl_printer *p,
3556 __isl_keep isl_band *band)
3558 isl_band_list *children;
3560 p = isl_printer_start_line(p);
3561 p = isl_printer_print_union_pw_multi_aff(p, band->pma);
3562 p = isl_printer_end_line(p);
3564 if (!isl_band_has_children(band))
3565 return p;
3567 children = isl_band_get_children(band);
3569 p = isl_printer_indent(p, 4);
3570 p = print_band_list(p, children);
3571 p = isl_printer_indent(p, -4);
3573 isl_band_list_free(children);
3575 return p;
3578 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
3579 __isl_keep isl_band_list *list)
3581 int i, n;
3583 n = isl_band_list_n_band(list);
3584 for (i = 0; i < n; ++i) {
3585 isl_band *band;
3586 band = isl_band_list_get_band(list, i);
3587 p = print_band(p, band);
3588 isl_band_free(band);
3591 return p;
3594 __isl_give isl_printer *isl_printer_print_schedule(__isl_take isl_printer *p,
3595 __isl_keep isl_schedule *schedule)
3597 isl_band_list *forest;
3599 forest = isl_schedule_get_band_forest(schedule);
3601 p = print_band_list(p, forest);
3603 isl_band_list_free(forest);
3605 return p;
3608 void isl_schedule_dump(__isl_keep isl_schedule *schedule)
3610 isl_printer *printer;
3612 if (!schedule)
3613 return;
3615 printer = isl_printer_to_file(isl_schedule_get_ctx(schedule), stderr);
3616 printer = isl_printer_print_schedule(printer, schedule);
3618 isl_printer_free(printer);