isl_schedule.c: extract out reset_band
[isl.git] / isl_schedule.c
blob2d23969363ab353c28e52f9959ae041b81b46eec
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".
39 /* Construct an isl_schedule_constraints object for computing a schedule
40 * on "domain". The initial object does not impose any constraints.
42 __isl_give isl_schedule_constraints *isl_schedule_constraints_on_domain(
43 __isl_take isl_union_set *domain)
45 isl_ctx *ctx;
46 isl_space *space;
47 isl_schedule_constraints *sc;
48 isl_union_map *empty;
49 enum isl_edge_type i;
51 if (!domain)
52 return NULL;
54 ctx = isl_union_set_get_ctx(domain);
55 sc = isl_calloc_type(ctx, struct isl_schedule_constraints);
56 if (!sc)
57 return isl_union_set_free(domain);
59 space = isl_union_set_get_space(domain);
60 sc->domain = domain;
61 empty = isl_union_map_empty(space);
62 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
63 sc->constraint[i] = isl_union_map_copy(empty);
64 if (!sc->constraint[i])
65 sc->domain = isl_union_set_free(sc->domain);
67 isl_union_map_free(empty);
69 if (!sc->domain)
70 return isl_schedule_constraints_free(sc);
72 return sc;
75 /* Replace the validity constraints of "sc" by "validity".
77 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_validity(
78 __isl_take isl_schedule_constraints *sc,
79 __isl_take isl_union_map *validity)
81 if (!sc || !validity)
82 goto error;
84 isl_union_map_free(sc->constraint[isl_edge_validity]);
85 sc->constraint[isl_edge_validity] = validity;
87 return sc;
88 error:
89 isl_schedule_constraints_free(sc);
90 isl_union_map_free(validity);
91 return NULL;
94 /* Replace the proximity constraints of "sc" by "proximity".
96 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_proximity(
97 __isl_take isl_schedule_constraints *sc,
98 __isl_take isl_union_map *proximity)
100 if (!sc || !proximity)
101 goto error;
103 isl_union_map_free(sc->constraint[isl_edge_proximity]);
104 sc->constraint[isl_edge_proximity] = proximity;
106 return sc;
107 error:
108 isl_schedule_constraints_free(sc);
109 isl_union_map_free(proximity);
110 return NULL;
113 void *isl_schedule_constraints_free(__isl_take isl_schedule_constraints *sc)
115 enum isl_edge_type i;
117 if (!sc)
118 return NULL;
120 isl_union_set_free(sc->domain);
121 for (i = isl_edge_first; i <= isl_edge_last; ++i)
122 isl_union_map_free(sc->constraint[i]);
124 free(sc);
126 return NULL;
129 isl_ctx *isl_schedule_constraints_get_ctx(
130 __isl_keep isl_schedule_constraints *sc)
132 return sc ? isl_union_set_get_ctx(sc->domain) : NULL;
135 void isl_schedule_constraints_dump(__isl_keep isl_schedule_constraints *sc)
137 if (!sc)
138 return;
140 fprintf(stderr, "domain: ");
141 isl_union_set_dump(sc->domain);
142 fprintf(stderr, "validity: ");
143 isl_union_map_dump(sc->constraint[isl_edge_validity]);
144 fprintf(stderr, "proximity: ");
145 isl_union_map_dump(sc->constraint[isl_edge_proximity]);
148 /* Align the parameters of the fields of "sc".
150 static __isl_give isl_schedule_constraints *
151 isl_schedule_constraints_align_params(__isl_take isl_schedule_constraints *sc)
153 isl_space *space;
154 enum isl_edge_type i;
156 if (!sc)
157 return NULL;
159 space = isl_union_set_get_space(sc->domain);
160 for (i = isl_edge_first; i <= isl_edge_last; ++i)
161 space = isl_space_align_params(space,
162 isl_union_map_get_space(sc->constraint[i]));
164 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
165 sc->constraint[i] = isl_union_map_align_params(
166 sc->constraint[i], isl_space_copy(space));
167 if (!sc->constraint[i])
168 space = isl_space_free(space);
170 sc->domain = isl_union_set_align_params(sc->domain, space);
171 if (!sc->domain)
172 return isl_schedule_constraints_free(sc);
174 return sc;
177 /* Return the total number of isl_maps in the constraints of "sc".
179 static __isl_give int isl_schedule_constraints_n_map(
180 __isl_keep isl_schedule_constraints *sc)
182 enum isl_edge_type i;
183 int n = 0;
185 for (i = isl_edge_first; i <= isl_edge_last; ++i)
186 n += isl_union_map_n_map(sc->constraint[i]);
188 return n;
191 /* Internal information about a node that is used during the construction
192 * of a schedule.
193 * dim represents the space in which the domain lives
194 * sched is a matrix representation of the schedule being constructed
195 * for this node
196 * sched_map is an isl_map representation of the same (partial) schedule
197 * sched_map may be NULL
198 * rank is the number of linearly independent rows in the linear part
199 * of sched
200 * the columns of cmap represent a change of basis for the schedule
201 * coefficients; the first rank columns span the linear part of
202 * the schedule rows
203 * cinv is the inverse of cmap.
204 * start is the first variable in the LP problem in the sequences that
205 * represents the schedule coefficients of this node
206 * nvar is the dimension of the domain
207 * nparam is the number of parameters or 0 if we are not constructing
208 * a parametric schedule
210 * scc is the index of SCC (or WCC) this node belongs to
212 * band contains the band index for each of the rows of the schedule.
213 * band_id is used to differentiate between separate bands at the same
214 * level within the same parent band, i.e., bands that are separated
215 * by the parent band or bands that are independent of each other.
216 * zero contains a boolean for each of the rows of the schedule,
217 * indicating whether the corresponding scheduling dimension results
218 * in zero dependence distances within its band and with respect
219 * to the proximity edges.
221 struct isl_sched_node {
222 isl_space *dim;
223 isl_mat *sched;
224 isl_map *sched_map;
225 int rank;
226 isl_mat *cmap;
227 isl_mat *cinv;
228 int start;
229 int nvar;
230 int nparam;
232 int scc;
234 int *band;
235 int *band_id;
236 int *zero;
239 static int node_has_dim(const void *entry, const void *val)
241 struct isl_sched_node *node = (struct isl_sched_node *)entry;
242 isl_space *dim = (isl_space *)val;
244 return isl_space_is_equal(node->dim, dim);
247 /* An edge in the dependence graph. An edge may be used to
248 * ensure validity of the generated schedule, to minimize the dependence
249 * distance or both
251 * map is the dependence relation
252 * src is the source node
253 * dst is the sink node
254 * validity is set if the edge is used to ensure correctness
255 * proximity is set if the edge is used to minimize dependence distances
257 * For validity edges, start and end mark the sequence of inequality
258 * constraints in the LP problem that encode the validity constraint
259 * corresponding to this edge.
261 struct isl_sched_edge {
262 isl_map *map;
264 struct isl_sched_node *src;
265 struct isl_sched_node *dst;
267 int validity;
268 int proximity;
270 int start;
271 int end;
274 /* Internal information about the dependence graph used during
275 * the construction of the schedule.
277 * intra_hmap is a cache, mapping dependence relations to their dual,
278 * for dependences from a node to itself
279 * inter_hmap is a cache, mapping dependence relations to their dual,
280 * for dependences between distinct nodes
282 * n is the number of nodes
283 * node is the list of nodes
284 * maxvar is the maximal number of variables over all nodes
285 * max_row is the allocated number of rows in the schedule
286 * n_row is the current (maximal) number of linearly independent
287 * rows in the node schedules
288 * n_total_row is the current number of rows in the node schedules
289 * n_band is the current number of completed bands
290 * band_start is the starting row in the node schedules of the current band
291 * root is set if this graph is the original dependence graph,
292 * without any splitting
294 * sorted contains a list of node indices sorted according to the
295 * SCC to which a node belongs
297 * n_edge is the number of edges
298 * edge is the list of edges
299 * max_edge contains the maximal number of edges of each type;
300 * in particular, it contains the number of edges in the inital graph.
301 * edge_table contains pointers into the edge array, hashed on the source
302 * and sink spaces; there is one such table for each type;
303 * a given edge may be referenced from more than one table
304 * if the corresponding relation appears in more than of the
305 * sets of dependences
307 * node_table contains pointers into the node array, hashed on the space
309 * region contains a list of variable sequences that should be non-trivial
311 * lp contains the (I)LP problem used to obtain new schedule rows
313 * src_scc and dst_scc are the source and sink SCCs of an edge with
314 * conflicting constraints
316 * scc represents the number of components
318 struct isl_sched_graph {
319 isl_map_to_basic_set *intra_hmap;
320 isl_map_to_basic_set *inter_hmap;
322 struct isl_sched_node *node;
323 int n;
324 int maxvar;
325 int max_row;
326 int n_row;
328 int *sorted;
330 int n_band;
331 int n_total_row;
332 int band_start;
334 int root;
336 struct isl_sched_edge *edge;
337 int n_edge;
338 int max_edge[isl_edge_last + 1];
339 struct isl_hash_table *edge_table[isl_edge_last + 1];
341 struct isl_hash_table *node_table;
342 struct isl_region *region;
344 isl_basic_set *lp;
346 int src_scc;
347 int dst_scc;
349 int scc;
352 /* Initialize node_table based on the list of nodes.
354 static int graph_init_table(isl_ctx *ctx, struct isl_sched_graph *graph)
356 int i;
358 graph->node_table = isl_hash_table_alloc(ctx, graph->n);
359 if (!graph->node_table)
360 return -1;
362 for (i = 0; i < graph->n; ++i) {
363 struct isl_hash_table_entry *entry;
364 uint32_t hash;
366 hash = isl_space_get_hash(graph->node[i].dim);
367 entry = isl_hash_table_find(ctx, graph->node_table, hash,
368 &node_has_dim,
369 graph->node[i].dim, 1);
370 if (!entry)
371 return -1;
372 entry->data = &graph->node[i];
375 return 0;
378 /* Return a pointer to the node that lives within the given space,
379 * or NULL if there is no such node.
381 static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
382 struct isl_sched_graph *graph, __isl_keep isl_space *dim)
384 struct isl_hash_table_entry *entry;
385 uint32_t hash;
387 hash = isl_space_get_hash(dim);
388 entry = isl_hash_table_find(ctx, graph->node_table, hash,
389 &node_has_dim, dim, 0);
391 return entry ? entry->data : NULL;
394 static int edge_has_src_and_dst(const void *entry, const void *val)
396 const struct isl_sched_edge *edge = entry;
397 const struct isl_sched_edge *temp = val;
399 return edge->src == temp->src && edge->dst == temp->dst;
402 /* Add the given edge to graph->edge_table[type].
404 static int graph_edge_table_add(isl_ctx *ctx, struct isl_sched_graph *graph,
405 enum isl_edge_type type, struct isl_sched_edge *edge)
407 struct isl_hash_table_entry *entry;
408 uint32_t hash;
410 hash = isl_hash_init();
411 hash = isl_hash_builtin(hash, edge->src);
412 hash = isl_hash_builtin(hash, edge->dst);
413 entry = isl_hash_table_find(ctx, graph->edge_table[type], hash,
414 &edge_has_src_and_dst, edge, 1);
415 if (!entry)
416 return -1;
417 entry->data = edge;
419 return 0;
422 /* Allocate the edge_tables based on the maximal number of edges of
423 * each type.
425 static int graph_init_edge_tables(isl_ctx *ctx, struct isl_sched_graph *graph)
427 int i;
429 for (i = 0; i <= isl_edge_last; ++i) {
430 graph->edge_table[i] = isl_hash_table_alloc(ctx,
431 graph->max_edge[i]);
432 if (!graph->edge_table[i])
433 return -1;
436 return 0;
439 /* If graph->edge_table[type] contains an edge from the given source
440 * to the given destination, then return the hash table entry of this edge.
441 * Otherwise, return NULL.
443 static struct isl_hash_table_entry *graph_find_edge_entry(
444 struct isl_sched_graph *graph,
445 enum isl_edge_type type,
446 struct isl_sched_node *src, struct isl_sched_node *dst)
448 isl_ctx *ctx = isl_space_get_ctx(src->dim);
449 uint32_t hash;
450 struct isl_sched_edge temp = { .src = src, .dst = dst };
452 hash = isl_hash_init();
453 hash = isl_hash_builtin(hash, temp.src);
454 hash = isl_hash_builtin(hash, temp.dst);
455 return isl_hash_table_find(ctx, graph->edge_table[type], hash,
456 &edge_has_src_and_dst, &temp, 0);
460 /* If graph->edge_table[type] contains an edge from the given source
461 * to the given destination, then return this edge.
462 * Otherwise, return NULL.
464 static struct isl_sched_edge *graph_find_edge(struct isl_sched_graph *graph,
465 enum isl_edge_type type,
466 struct isl_sched_node *src, struct isl_sched_node *dst)
468 struct isl_hash_table_entry *entry;
470 entry = graph_find_edge_entry(graph, type, src, dst);
471 if (!entry)
472 return NULL;
474 return entry->data;
477 /* Check whether the dependence graph has an edge of the given type
478 * between the given two nodes.
480 static int graph_has_edge(struct isl_sched_graph *graph,
481 enum isl_edge_type type,
482 struct isl_sched_node *src, struct isl_sched_node *dst)
484 struct isl_sched_edge *edge;
485 int empty;
487 edge = graph_find_edge(graph, type, src, dst);
488 if (!edge)
489 return 0;
491 empty = isl_map_plain_is_empty(edge->map);
492 if (empty < 0)
493 return -1;
495 return !empty;
498 /* If there is an edge from the given source to the given destination
499 * of any type then return this edge.
500 * Otherwise, return NULL.
502 static struct isl_sched_edge *graph_find_any_edge(struct isl_sched_graph *graph,
503 struct isl_sched_node *src, struct isl_sched_node *dst)
505 enum isl_edge_type i;
506 struct isl_sched_edge *edge;
508 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
509 edge = graph_find_edge(graph, i, src, dst);
510 if (edge)
511 return edge;
514 return NULL;
517 /* Remove the given edge from all the edge_tables that refer to it.
519 static void graph_remove_edge(struct isl_sched_graph *graph,
520 struct isl_sched_edge *edge)
522 isl_ctx *ctx = isl_map_get_ctx(edge->map);
523 enum isl_edge_type i;
525 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
526 struct isl_hash_table_entry *entry;
528 entry = graph_find_edge_entry(graph, i, edge->src, edge->dst);
529 if (!entry)
530 continue;
531 if (entry->data != edge)
532 continue;
533 isl_hash_table_remove(ctx, graph->edge_table[i], entry);
537 /* Check whether the dependence graph has any edge
538 * between the given two nodes.
540 static int graph_has_any_edge(struct isl_sched_graph *graph,
541 struct isl_sched_node *src, struct isl_sched_node *dst)
543 enum isl_edge_type i;
544 int r;
546 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
547 r = graph_has_edge(graph, i, src, dst);
548 if (r < 0 || r)
549 return r;
552 return r;
555 /* Check whether the dependence graph has a validity edge
556 * between the given two nodes.
558 static int graph_has_validity_edge(struct isl_sched_graph *graph,
559 struct isl_sched_node *src, struct isl_sched_node *dst)
561 return graph_has_edge(graph, isl_edge_validity, src, dst);
564 static int graph_alloc(isl_ctx *ctx, struct isl_sched_graph *graph,
565 int n_node, int n_edge)
567 int i;
569 graph->n = n_node;
570 graph->n_edge = n_edge;
571 graph->node = isl_calloc_array(ctx, struct isl_sched_node, graph->n);
572 graph->sorted = isl_calloc_array(ctx, int, graph->n);
573 graph->region = isl_alloc_array(ctx, struct isl_region, graph->n);
574 graph->edge = isl_calloc_array(ctx,
575 struct isl_sched_edge, graph->n_edge);
577 graph->intra_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
578 graph->inter_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
580 if (!graph->node || !graph->region || (graph->n_edge && !graph->edge) ||
581 !graph->sorted)
582 return -1;
584 for(i = 0; i < graph->n; ++i)
585 graph->sorted[i] = i;
587 return 0;
590 static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
592 int i;
594 isl_map_to_basic_set_free(graph->intra_hmap);
595 isl_map_to_basic_set_free(graph->inter_hmap);
597 for (i = 0; i < graph->n; ++i) {
598 isl_space_free(graph->node[i].dim);
599 isl_mat_free(graph->node[i].sched);
600 isl_map_free(graph->node[i].sched_map);
601 isl_mat_free(graph->node[i].cmap);
602 isl_mat_free(graph->node[i].cinv);
603 if (graph->root) {
604 free(graph->node[i].band);
605 free(graph->node[i].band_id);
606 free(graph->node[i].zero);
609 free(graph->node);
610 free(graph->sorted);
611 for (i = 0; i < graph->n_edge; ++i)
612 isl_map_free(graph->edge[i].map);
613 free(graph->edge);
614 free(graph->region);
615 for (i = 0; i <= isl_edge_last; ++i)
616 isl_hash_table_free(ctx, graph->edge_table[i]);
617 isl_hash_table_free(ctx, graph->node_table);
618 isl_basic_set_free(graph->lp);
621 /* For each "set" on which this function is called, increment
622 * graph->n by one and update graph->maxvar.
624 static int init_n_maxvar(__isl_take isl_set *set, void *user)
626 struct isl_sched_graph *graph = user;
627 int nvar = isl_set_dim(set, isl_dim_set);
629 graph->n++;
630 if (nvar > graph->maxvar)
631 graph->maxvar = nvar;
633 isl_set_free(set);
635 return 0;
638 /* Compute the number of rows that should be allocated for the schedule.
639 * The graph can be split at most "n - 1" times, there can be at most
640 * two rows for each dimension in the iteration domains (in particular,
641 * we usually have one row, but it may be split by split_scaled),
642 * and there can be one extra row for ordering the statements.
643 * Note that if we have actually split "n - 1" times, then no ordering
644 * is needed, so in principle we could use "graph->n + 2 * graph->maxvar - 1".
646 static int compute_max_row(struct isl_sched_graph *graph,
647 __isl_keep isl_union_set *domain)
649 graph->n = 0;
650 graph->maxvar = 0;
651 if (isl_union_set_foreach_set(domain, &init_n_maxvar, graph) < 0)
652 return -1;
653 graph->max_row = graph->n + 2 * graph->maxvar;
655 return 0;
658 /* Add a new node to the graph representing the given set.
660 static int extract_node(__isl_take isl_set *set, void *user)
662 int nvar, nparam;
663 isl_ctx *ctx;
664 isl_space *dim;
665 isl_mat *sched;
666 struct isl_sched_graph *graph = user;
667 int *band, *band_id, *zero;
669 ctx = isl_set_get_ctx(set);
670 dim = isl_set_get_space(set);
671 isl_set_free(set);
672 nvar = isl_space_dim(dim, isl_dim_set);
673 nparam = isl_space_dim(dim, isl_dim_param);
674 if (!ctx->opt->schedule_parametric)
675 nparam = 0;
676 sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
677 graph->node[graph->n].dim = dim;
678 graph->node[graph->n].nvar = nvar;
679 graph->node[graph->n].nparam = nparam;
680 graph->node[graph->n].sched = sched;
681 graph->node[graph->n].sched_map = NULL;
682 band = isl_alloc_array(ctx, int, graph->max_row);
683 graph->node[graph->n].band = band;
684 band_id = isl_calloc_array(ctx, int, graph->max_row);
685 graph->node[graph->n].band_id = band_id;
686 zero = isl_calloc_array(ctx, int, graph->max_row);
687 graph->node[graph->n].zero = zero;
688 graph->n++;
690 if (!sched || (graph->max_row && (!band || !band_id || !zero)))
691 return -1;
693 return 0;
696 struct isl_extract_edge_data {
697 enum isl_edge_type type;
698 struct isl_sched_graph *graph;
701 /* Add a new edge to the graph based on the given map
702 * and add it to data->graph->edge_table[data->type].
703 * If a dependence relation of a given type happens to be identical
704 * to one of the dependence relations of a type that was added before,
705 * then we don't create a new edge, but instead mark the original edge
706 * as also representing a dependence of the current type.
708 static int extract_edge(__isl_take isl_map *map, void *user)
710 isl_ctx *ctx = isl_map_get_ctx(map);
711 struct isl_extract_edge_data *data = user;
712 struct isl_sched_graph *graph = data->graph;
713 struct isl_sched_node *src, *dst;
714 isl_space *dim;
715 struct isl_sched_edge *edge;
716 int is_equal;
718 dim = isl_space_domain(isl_map_get_space(map));
719 src = graph_find_node(ctx, graph, dim);
720 isl_space_free(dim);
721 dim = isl_space_range(isl_map_get_space(map));
722 dst = graph_find_node(ctx, graph, dim);
723 isl_space_free(dim);
725 if (!src || !dst) {
726 isl_map_free(map);
727 return 0;
730 graph->edge[graph->n_edge].src = src;
731 graph->edge[graph->n_edge].dst = dst;
732 graph->edge[graph->n_edge].map = map;
733 if (data->type == isl_edge_validity) {
734 graph->edge[graph->n_edge].validity = 1;
735 graph->edge[graph->n_edge].proximity = 0;
737 if (data->type == isl_edge_proximity) {
738 graph->edge[graph->n_edge].validity = 0;
739 graph->edge[graph->n_edge].proximity = 1;
741 graph->n_edge++;
743 edge = graph_find_any_edge(graph, src, dst);
744 if (!edge)
745 return graph_edge_table_add(ctx, graph, data->type,
746 &graph->edge[graph->n_edge - 1]);
747 is_equal = isl_map_plain_is_equal(map, edge->map);
748 if (is_equal < 0)
749 return -1;
750 if (!is_equal)
751 return graph_edge_table_add(ctx, graph, data->type,
752 &graph->edge[graph->n_edge - 1]);
754 graph->n_edge--;
755 edge->validity |= graph->edge[graph->n_edge].validity;
756 edge->proximity |= graph->edge[graph->n_edge].proximity;
757 isl_map_free(map);
759 return graph_edge_table_add(ctx, graph, data->type, edge);
762 /* Check whether there is any dependence from node[j] to node[i]
763 * or from node[i] to node[j].
765 static int node_follows_weak(int i, int j, void *user)
767 int f;
768 struct isl_sched_graph *graph = user;
770 f = graph_has_any_edge(graph, &graph->node[j], &graph->node[i]);
771 if (f < 0 || f)
772 return f;
773 return graph_has_any_edge(graph, &graph->node[i], &graph->node[j]);
776 /* Check whether there is a validity dependence from node[j] to node[i],
777 * forcing node[i] to follow node[j].
779 static int node_follows_strong(int i, int j, void *user)
781 struct isl_sched_graph *graph = user;
783 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
786 /* Use Tarjan's algorithm for computing the strongly connected components
787 * in the dependence graph (only validity edges).
788 * If weak is set, we consider the graph to be undirected and
789 * we effectively compute the (weakly) connected components.
790 * Additionally, we also consider other edges when weak is set.
792 static int detect_ccs(isl_ctx *ctx, struct isl_sched_graph *graph, int weak)
794 int i, n;
795 struct isl_tarjan_graph *g = NULL;
797 g = isl_tarjan_graph_init(ctx, graph->n,
798 weak ? &node_follows_weak : &node_follows_strong, graph);
799 if (!g)
800 return -1;
802 graph->scc = 0;
803 i = 0;
804 n = graph->n;
805 while (n) {
806 while (g->order[i] != -1) {
807 graph->node[g->order[i]].scc = graph->scc;
808 --n;
809 ++i;
811 ++i;
812 graph->scc++;
815 isl_tarjan_graph_free(g);
817 return 0;
820 /* Apply Tarjan's algorithm to detect the strongly connected components
821 * in the dependence graph.
823 static int detect_sccs(isl_ctx *ctx, struct isl_sched_graph *graph)
825 return detect_ccs(ctx, graph, 0);
828 /* Apply Tarjan's algorithm to detect the (weakly) connected components
829 * in the dependence graph.
831 static int detect_wccs(isl_ctx *ctx, struct isl_sched_graph *graph)
833 return detect_ccs(ctx, graph, 1);
836 static int cmp_scc(const void *a, const void *b, void *data)
838 struct isl_sched_graph *graph = data;
839 const int *i1 = a;
840 const int *i2 = b;
842 return graph->node[*i1].scc - graph->node[*i2].scc;
845 /* Sort the elements of graph->sorted according to the corresponding SCCs.
847 static int sort_sccs(struct isl_sched_graph *graph)
849 return isl_sort(graph->sorted, graph->n, sizeof(int), &cmp_scc, graph);
852 /* Given a dependence relation R from a node to itself,
853 * construct the set of coefficients of valid constraints for elements
854 * in that dependence relation.
855 * In particular, the result contains tuples of coefficients
856 * c_0, c_n, c_x such that
858 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
860 * or, equivalently,
862 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
864 * We choose here to compute the dual of delta R.
865 * Alternatively, we could have computed the dual of R, resulting
866 * in a set of tuples c_0, c_n, c_x, c_y, and then
867 * plugged in (c_0, c_n, c_x, -c_x).
869 static __isl_give isl_basic_set *intra_coefficients(
870 struct isl_sched_graph *graph, __isl_take isl_map *map)
872 isl_set *delta;
873 isl_basic_set *coef;
875 if (isl_map_to_basic_set_has(graph->intra_hmap, map))
876 return isl_map_to_basic_set_get(graph->intra_hmap, map);
878 delta = isl_set_remove_divs(isl_map_deltas(isl_map_copy(map)));
879 coef = isl_set_coefficients(delta);
880 graph->intra_hmap = isl_map_to_basic_set_set(graph->intra_hmap, map,
881 isl_basic_set_copy(coef));
883 return coef;
886 /* Given a dependence relation R, * construct the set of coefficients
887 * of valid constraints for elements in that dependence relation.
888 * In particular, the result contains tuples of coefficients
889 * c_0, c_n, c_x, c_y such that
891 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
894 static __isl_give isl_basic_set *inter_coefficients(
895 struct isl_sched_graph *graph, __isl_take isl_map *map)
897 isl_set *set;
898 isl_basic_set *coef;
900 if (isl_map_to_basic_set_has(graph->inter_hmap, map))
901 return isl_map_to_basic_set_get(graph->inter_hmap, map);
903 set = isl_map_wrap(isl_map_remove_divs(isl_map_copy(map)));
904 coef = isl_set_coefficients(set);
905 graph->inter_hmap = isl_map_to_basic_set_set(graph->inter_hmap, map,
906 isl_basic_set_copy(coef));
908 return coef;
911 /* Add constraints to graph->lp that force validity for the given
912 * dependence from a node i to itself.
913 * That is, add constraints that enforce
915 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
916 * = c_i_x (y - x) >= 0
918 * for each (x,y) in R.
919 * We obtain general constraints on coefficients (c_0, c_n, c_x)
920 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
921 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
922 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
924 * Actually, we do not construct constraints for the c_i_x themselves,
925 * but for the coefficients of c_i_x written as a linear combination
926 * of the columns in node->cmap.
928 static int add_intra_validity_constraints(struct isl_sched_graph *graph,
929 struct isl_sched_edge *edge)
931 unsigned total;
932 isl_map *map = isl_map_copy(edge->map);
933 isl_ctx *ctx = isl_map_get_ctx(map);
934 isl_space *dim;
935 isl_dim_map *dim_map;
936 isl_basic_set *coef;
937 struct isl_sched_node *node = edge->src;
939 coef = intra_coefficients(graph, map);
941 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
943 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
944 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
945 if (!coef)
946 goto error;
948 total = isl_basic_set_total_dim(graph->lp);
949 dim_map = isl_dim_map_alloc(ctx, total);
950 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
951 isl_space_dim(dim, isl_dim_set), 1,
952 node->nvar, -1);
953 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
954 isl_space_dim(dim, isl_dim_set), 1,
955 node->nvar, 1);
956 graph->lp = isl_basic_set_extend_constraints(graph->lp,
957 coef->n_eq, coef->n_ineq);
958 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
959 coef, dim_map);
960 isl_space_free(dim);
962 return 0;
963 error:
964 isl_space_free(dim);
965 return -1;
968 /* Add constraints to graph->lp that force validity for the given
969 * dependence from node i to node j.
970 * That is, add constraints that enforce
972 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
974 * for each (x,y) in R.
975 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
976 * of valid constraints for R and then plug in
977 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
978 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
979 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
980 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
982 * Actually, we do not construct constraints for the c_*_x themselves,
983 * but for the coefficients of c_*_x written as a linear combination
984 * of the columns in node->cmap.
986 static int add_inter_validity_constraints(struct isl_sched_graph *graph,
987 struct isl_sched_edge *edge)
989 unsigned total;
990 isl_map *map = isl_map_copy(edge->map);
991 isl_ctx *ctx = isl_map_get_ctx(map);
992 isl_space *dim;
993 isl_dim_map *dim_map;
994 isl_basic_set *coef;
995 struct isl_sched_node *src = edge->src;
996 struct isl_sched_node *dst = edge->dst;
998 coef = inter_coefficients(graph, map);
1000 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1002 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1003 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1004 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1005 isl_space_dim(dim, isl_dim_set) + src->nvar,
1006 isl_mat_copy(dst->cmap));
1007 if (!coef)
1008 goto error;
1010 total = isl_basic_set_total_dim(graph->lp);
1011 dim_map = isl_dim_map_alloc(ctx, total);
1013 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
1014 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
1015 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
1016 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1017 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1018 dst->nvar, -1);
1019 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1020 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1021 dst->nvar, 1);
1023 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
1024 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
1025 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
1026 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1027 isl_space_dim(dim, isl_dim_set), 1,
1028 src->nvar, 1);
1029 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1030 isl_space_dim(dim, isl_dim_set), 1,
1031 src->nvar, -1);
1033 edge->start = graph->lp->n_ineq;
1034 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1035 coef->n_eq, coef->n_ineq);
1036 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1037 coef, dim_map);
1038 if (!graph->lp)
1039 goto error;
1040 isl_space_free(dim);
1041 edge->end = graph->lp->n_ineq;
1043 return 0;
1044 error:
1045 isl_space_free(dim);
1046 return -1;
1049 /* Add constraints to graph->lp that bound the dependence distance for the given
1050 * dependence from a node i to itself.
1051 * If s = 1, we add the constraint
1053 * c_i_x (y - x) <= m_0 + m_n n
1055 * or
1057 * -c_i_x (y - x) + m_0 + m_n n >= 0
1059 * for each (x,y) in R.
1060 * If s = -1, we add the constraint
1062 * -c_i_x (y - x) <= m_0 + m_n n
1064 * or
1066 * c_i_x (y - x) + m_0 + m_n n >= 0
1068 * for each (x,y) in R.
1069 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1070 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1071 * with each coefficient (except m_0) represented as a pair of non-negative
1072 * coefficients.
1074 * Actually, we do not construct constraints for the c_i_x themselves,
1075 * but for the coefficients of c_i_x written as a linear combination
1076 * of the columns in node->cmap.
1078 static int add_intra_proximity_constraints(struct isl_sched_graph *graph,
1079 struct isl_sched_edge *edge, int s)
1081 unsigned total;
1082 unsigned nparam;
1083 isl_map *map = isl_map_copy(edge->map);
1084 isl_ctx *ctx = isl_map_get_ctx(map);
1085 isl_space *dim;
1086 isl_dim_map *dim_map;
1087 isl_basic_set *coef;
1088 struct isl_sched_node *node = edge->src;
1090 coef = intra_coefficients(graph, map);
1092 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1094 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1095 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
1096 if (!coef)
1097 goto error;
1099 nparam = isl_space_dim(node->dim, isl_dim_param);
1100 total = isl_basic_set_total_dim(graph->lp);
1101 dim_map = isl_dim_map_alloc(ctx, total);
1102 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1103 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1104 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1105 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
1106 isl_space_dim(dim, isl_dim_set), 1,
1107 node->nvar, s);
1108 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
1109 isl_space_dim(dim, isl_dim_set), 1,
1110 node->nvar, -s);
1111 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1112 coef->n_eq, coef->n_ineq);
1113 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1114 coef, dim_map);
1115 isl_space_free(dim);
1117 return 0;
1118 error:
1119 isl_space_free(dim);
1120 return -1;
1123 /* Add constraints to graph->lp that bound the dependence distance for the given
1124 * dependence from node i to node j.
1125 * If s = 1, we add the constraint
1127 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1128 * <= m_0 + m_n n
1130 * or
1132 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1133 * m_0 + m_n n >= 0
1135 * for each (x,y) in R.
1136 * If s = -1, we add the constraint
1138 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1139 * <= m_0 + m_n n
1141 * or
1143 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1144 * m_0 + m_n n >= 0
1146 * for each (x,y) in R.
1147 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1148 * of valid constraints for R and then plug in
1149 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1150 * -s*c_j_x+s*c_i_x)
1151 * with each coefficient (except m_0, c_j_0 and c_i_0)
1152 * represented as a pair of non-negative coefficients.
1154 * Actually, we do not construct constraints for the c_*_x themselves,
1155 * but for the coefficients of c_*_x written as a linear combination
1156 * of the columns in node->cmap.
1158 static int add_inter_proximity_constraints(struct isl_sched_graph *graph,
1159 struct isl_sched_edge *edge, int s)
1161 unsigned total;
1162 unsigned nparam;
1163 isl_map *map = isl_map_copy(edge->map);
1164 isl_ctx *ctx = isl_map_get_ctx(map);
1165 isl_space *dim;
1166 isl_dim_map *dim_map;
1167 isl_basic_set *coef;
1168 struct isl_sched_node *src = edge->src;
1169 struct isl_sched_node *dst = edge->dst;
1171 coef = inter_coefficients(graph, map);
1173 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1175 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1176 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1177 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1178 isl_space_dim(dim, isl_dim_set) + src->nvar,
1179 isl_mat_copy(dst->cmap));
1180 if (!coef)
1181 goto error;
1183 nparam = isl_space_dim(src->dim, isl_dim_param);
1184 total = isl_basic_set_total_dim(graph->lp);
1185 dim_map = isl_dim_map_alloc(ctx, total);
1187 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1188 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1189 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1191 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, -s);
1192 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, s);
1193 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, -s);
1194 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1195 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1196 dst->nvar, s);
1197 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1198 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1199 dst->nvar, -s);
1201 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, s);
1202 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, -s);
1203 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, s);
1204 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1205 isl_space_dim(dim, isl_dim_set), 1,
1206 src->nvar, -s);
1207 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1208 isl_space_dim(dim, isl_dim_set), 1,
1209 src->nvar, s);
1211 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1212 coef->n_eq, coef->n_ineq);
1213 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1214 coef, dim_map);
1215 isl_space_free(dim);
1217 return 0;
1218 error:
1219 isl_space_free(dim);
1220 return -1;
1223 static int add_all_validity_constraints(struct isl_sched_graph *graph)
1225 int i;
1227 for (i = 0; i < graph->n_edge; ++i) {
1228 struct isl_sched_edge *edge= &graph->edge[i];
1229 if (!edge->validity)
1230 continue;
1231 if (edge->src != edge->dst)
1232 continue;
1233 if (add_intra_validity_constraints(graph, edge) < 0)
1234 return -1;
1237 for (i = 0; i < graph->n_edge; ++i) {
1238 struct isl_sched_edge *edge = &graph->edge[i];
1239 if (!edge->validity)
1240 continue;
1241 if (edge->src == edge->dst)
1242 continue;
1243 if (add_inter_validity_constraints(graph, edge) < 0)
1244 return -1;
1247 return 0;
1250 /* Add constraints to graph->lp that bound the dependence distance
1251 * for all dependence relations.
1252 * If a given proximity dependence is identical to a validity
1253 * dependence, then the dependence distance is already bounded
1254 * from below (by zero), so we only need to bound the distance
1255 * from above.
1256 * Otherwise, we need to bound the distance both from above and from below.
1258 static int add_all_proximity_constraints(struct isl_sched_graph *graph)
1260 int i;
1262 for (i = 0; i < graph->n_edge; ++i) {
1263 struct isl_sched_edge *edge= &graph->edge[i];
1264 if (!edge->proximity)
1265 continue;
1266 if (edge->src == edge->dst &&
1267 add_intra_proximity_constraints(graph, edge, 1) < 0)
1268 return -1;
1269 if (edge->src != edge->dst &&
1270 add_inter_proximity_constraints(graph, edge, 1) < 0)
1271 return -1;
1272 if (edge->validity)
1273 continue;
1274 if (edge->src == edge->dst &&
1275 add_intra_proximity_constraints(graph, edge, -1) < 0)
1276 return -1;
1277 if (edge->src != edge->dst &&
1278 add_inter_proximity_constraints(graph, edge, -1) < 0)
1279 return -1;
1282 return 0;
1285 /* Compute a basis for the rows in the linear part of the schedule
1286 * and extend this basis to a full basis. The remaining rows
1287 * can then be used to force linear independence from the rows
1288 * in the schedule.
1290 * In particular, given the schedule rows S, we compute
1292 * S = H Q
1293 * S U = H
1295 * with H the Hermite normal form of S. That is, all but the
1296 * first rank columns of H are zero and so each row in S is
1297 * a linear combination of the first rank rows of Q.
1298 * The matrix Q is then transposed because we will write the
1299 * coefficients of the next schedule row as a column vector s
1300 * and express this s as a linear combination s = Q c of the
1301 * computed basis.
1302 * Similarly, the matrix U is transposed such that we can
1303 * compute the coefficients c = U s from a schedule row s.
1305 static int node_update_cmap(struct isl_sched_node *node)
1307 isl_mat *H, *U, *Q;
1308 int n_row = isl_mat_rows(node->sched);
1310 H = isl_mat_sub_alloc(node->sched, 0, n_row,
1311 1 + node->nparam, node->nvar);
1313 H = isl_mat_left_hermite(H, 0, &U, &Q);
1314 isl_mat_free(node->cmap);
1315 isl_mat_free(node->cinv);
1316 node->cmap = isl_mat_transpose(Q);
1317 node->cinv = isl_mat_transpose(U);
1318 node->rank = isl_mat_initial_non_zero_cols(H);
1319 isl_mat_free(H);
1321 if (!node->cmap || !node->cinv || node->rank < 0)
1322 return -1;
1323 return 0;
1326 /* Count the number of equality and inequality constraints
1327 * that will be added for the given map.
1328 * If carry is set, then we are counting the number of (validity)
1329 * constraints that will be added in setup_carry_lp and we count
1330 * each edge exactly once. Otherwise, we count as follows
1331 * validity -> 1 (>= 0)
1332 * validity+proximity -> 2 (>= 0 and upper bound)
1333 * proximity -> 2 (lower and upper bound)
1335 static int count_map_constraints(struct isl_sched_graph *graph,
1336 struct isl_sched_edge *edge, __isl_take isl_map *map,
1337 int *n_eq, int *n_ineq, int carry)
1339 isl_basic_set *coef;
1340 int f = carry ? 1 : edge->proximity ? 2 : 1;
1342 if (carry && !edge->validity) {
1343 isl_map_free(map);
1344 return 0;
1347 if (edge->src == edge->dst)
1348 coef = intra_coefficients(graph, map);
1349 else
1350 coef = inter_coefficients(graph, map);
1351 if (!coef)
1352 return -1;
1353 *n_eq += f * coef->n_eq;
1354 *n_ineq += f * coef->n_ineq;
1355 isl_basic_set_free(coef);
1357 return 0;
1360 /* Count the number of equality and inequality constraints
1361 * that will be added to the main lp problem.
1362 * We count as follows
1363 * validity -> 1 (>= 0)
1364 * validity+proximity -> 2 (>= 0 and upper bound)
1365 * proximity -> 2 (lower and upper bound)
1367 static int count_constraints(struct isl_sched_graph *graph,
1368 int *n_eq, int *n_ineq)
1370 int i;
1372 *n_eq = *n_ineq = 0;
1373 for (i = 0; i < graph->n_edge; ++i) {
1374 struct isl_sched_edge *edge= &graph->edge[i];
1375 isl_map *map = isl_map_copy(edge->map);
1377 if (count_map_constraints(graph, edge, map,
1378 n_eq, n_ineq, 0) < 0)
1379 return -1;
1382 return 0;
1385 /* Count the number of constraints that will be added by
1386 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
1387 * accordingly.
1389 * In practice, add_bound_coefficient_constraints only adds inequalities.
1391 static int count_bound_coefficient_constraints(isl_ctx *ctx,
1392 struct isl_sched_graph *graph, int *n_eq, int *n_ineq)
1394 int i;
1396 if (ctx->opt->schedule_max_coefficient == -1)
1397 return 0;
1399 for (i = 0; i < graph->n; ++i)
1400 *n_ineq += 2 * graph->node[i].nparam + 2 * graph->node[i].nvar;
1402 return 0;
1405 /* Add constraints that bound the values of the variable and parameter
1406 * coefficients of the schedule.
1408 * The maximal value of the coefficients is defined by the option
1409 * 'schedule_max_coefficient'.
1411 static int add_bound_coefficient_constraints(isl_ctx *ctx,
1412 struct isl_sched_graph *graph)
1414 int i, j, k;
1415 int max_coefficient;
1416 int total;
1418 max_coefficient = ctx->opt->schedule_max_coefficient;
1420 if (max_coefficient == -1)
1421 return 0;
1423 total = isl_basic_set_total_dim(graph->lp);
1425 for (i = 0; i < graph->n; ++i) {
1426 struct isl_sched_node *node = &graph->node[i];
1427 for (j = 0; j < 2 * node->nparam + 2 * node->nvar; ++j) {
1428 int dim;
1429 k = isl_basic_set_alloc_inequality(graph->lp);
1430 if (k < 0)
1431 return -1;
1432 dim = 1 + node->start + 1 + j;
1433 isl_seq_clr(graph->lp->ineq[k], 1 + total);
1434 isl_int_set_si(graph->lp->ineq[k][dim], -1);
1435 isl_int_set_si(graph->lp->ineq[k][0], max_coefficient);
1439 return 0;
1442 /* Construct an ILP problem for finding schedule coefficients
1443 * that result in non-negative, but small dependence distances
1444 * over all dependences.
1445 * In particular, the dependence distances over proximity edges
1446 * are bounded by m_0 + m_n n and we compute schedule coefficients
1447 * with small values (preferably zero) of m_n and m_0.
1449 * All variables of the ILP are non-negative. The actual coefficients
1450 * may be negative, so each coefficient is represented as the difference
1451 * of two non-negative variables. The negative part always appears
1452 * immediately before the positive part.
1453 * Other than that, the variables have the following order
1455 * - sum of positive and negative parts of m_n coefficients
1456 * - m_0
1457 * - sum of positive and negative parts of all c_n coefficients
1458 * (unconstrained when computing non-parametric schedules)
1459 * - sum of positive and negative parts of all c_x coefficients
1460 * - positive and negative parts of m_n coefficients
1461 * - for each node
1462 * - c_i_0
1463 * - positive and negative parts of c_i_n (if parametric)
1464 * - positive and negative parts of c_i_x
1466 * The c_i_x are not represented directly, but through the columns of
1467 * node->cmap. That is, the computed values are for variable t_i_x
1468 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
1470 * The constraints are those from the edges plus two or three equalities
1471 * to express the sums.
1473 * If force_zero is set, then we add equalities to ensure that
1474 * the sum of the m_n coefficients and m_0 are both zero.
1476 static int setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
1477 int force_zero)
1479 int i, j;
1480 int k;
1481 unsigned nparam;
1482 unsigned total;
1483 isl_space *dim;
1484 int parametric;
1485 int param_pos;
1486 int n_eq, n_ineq;
1487 int max_constant_term;
1489 max_constant_term = ctx->opt->schedule_max_constant_term;
1491 parametric = ctx->opt->schedule_parametric;
1492 nparam = isl_space_dim(graph->node[0].dim, isl_dim_param);
1493 param_pos = 4;
1494 total = param_pos + 2 * nparam;
1495 for (i = 0; i < graph->n; ++i) {
1496 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
1497 if (node_update_cmap(node) < 0)
1498 return -1;
1499 node->start = total;
1500 total += 1 + 2 * (node->nparam + node->nvar);
1503 if (count_constraints(graph, &n_eq, &n_ineq) < 0)
1504 return -1;
1505 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
1506 return -1;
1508 dim = isl_space_set_alloc(ctx, 0, total);
1509 isl_basic_set_free(graph->lp);
1510 n_eq += 2 + parametric + force_zero;
1511 if (max_constant_term != -1)
1512 n_ineq += graph->n;
1514 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
1516 k = isl_basic_set_alloc_equality(graph->lp);
1517 if (k < 0)
1518 return -1;
1519 isl_seq_clr(graph->lp->eq[k], 1 + total);
1520 if (!force_zero)
1521 isl_int_set_si(graph->lp->eq[k][1], -1);
1522 for (i = 0; i < 2 * nparam; ++i)
1523 isl_int_set_si(graph->lp->eq[k][1 + param_pos + i], 1);
1525 if (force_zero) {
1526 k = isl_basic_set_alloc_equality(graph->lp);
1527 if (k < 0)
1528 return -1;
1529 isl_seq_clr(graph->lp->eq[k], 1 + total);
1530 isl_int_set_si(graph->lp->eq[k][2], -1);
1533 if (parametric) {
1534 k = isl_basic_set_alloc_equality(graph->lp);
1535 if (k < 0)
1536 return -1;
1537 isl_seq_clr(graph->lp->eq[k], 1 + total);
1538 isl_int_set_si(graph->lp->eq[k][3], -1);
1539 for (i = 0; i < graph->n; ++i) {
1540 int pos = 1 + graph->node[i].start + 1;
1542 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
1543 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
1547 k = isl_basic_set_alloc_equality(graph->lp);
1548 if (k < 0)
1549 return -1;
1550 isl_seq_clr(graph->lp->eq[k], 1 + total);
1551 isl_int_set_si(graph->lp->eq[k][4], -1);
1552 for (i = 0; i < graph->n; ++i) {
1553 struct isl_sched_node *node = &graph->node[i];
1554 int pos = 1 + node->start + 1 + 2 * node->nparam;
1556 for (j = 0; j < 2 * node->nvar; ++j)
1557 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
1560 if (max_constant_term != -1)
1561 for (i = 0; i < graph->n; ++i) {
1562 struct isl_sched_node *node = &graph->node[i];
1563 k = isl_basic_set_alloc_inequality(graph->lp);
1564 if (k < 0)
1565 return -1;
1566 isl_seq_clr(graph->lp->ineq[k], 1 + total);
1567 isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
1568 isl_int_set_si(graph->lp->ineq[k][0], max_constant_term);
1571 if (add_bound_coefficient_constraints(ctx, graph) < 0)
1572 return -1;
1573 if (add_all_validity_constraints(graph) < 0)
1574 return -1;
1575 if (add_all_proximity_constraints(graph) < 0)
1576 return -1;
1578 return 0;
1581 /* Analyze the conflicting constraint found by
1582 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
1583 * constraint of one of the edges between distinct nodes, living, moreover
1584 * in distinct SCCs, then record the source and sink SCC as this may
1585 * be a good place to cut between SCCs.
1587 static int check_conflict(int con, void *user)
1589 int i;
1590 struct isl_sched_graph *graph = user;
1592 if (graph->src_scc >= 0)
1593 return 0;
1595 con -= graph->lp->n_eq;
1597 if (con >= graph->lp->n_ineq)
1598 return 0;
1600 for (i = 0; i < graph->n_edge; ++i) {
1601 if (!graph->edge[i].validity)
1602 continue;
1603 if (graph->edge[i].src == graph->edge[i].dst)
1604 continue;
1605 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
1606 continue;
1607 if (graph->edge[i].start > con)
1608 continue;
1609 if (graph->edge[i].end <= con)
1610 continue;
1611 graph->src_scc = graph->edge[i].src->scc;
1612 graph->dst_scc = graph->edge[i].dst->scc;
1615 return 0;
1618 /* Check whether the next schedule row of the given node needs to be
1619 * non-trivial. Lower-dimensional domains may have some trivial rows,
1620 * but as soon as the number of remaining required non-trivial rows
1621 * is as large as the number or remaining rows to be computed,
1622 * all remaining rows need to be non-trivial.
1624 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
1626 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
1629 /* Solve the ILP problem constructed in setup_lp.
1630 * For each node such that all the remaining rows of its schedule
1631 * need to be non-trivial, we construct a non-triviality region.
1632 * This region imposes that the next row is independent of previous rows.
1633 * In particular the coefficients c_i_x are represented by t_i_x
1634 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
1635 * its first columns span the rows of the previously computed part
1636 * of the schedule. The non-triviality region enforces that at least
1637 * one of the remaining components of t_i_x is non-zero, i.e.,
1638 * that the new schedule row depends on at least one of the remaining
1639 * columns of Q.
1641 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
1643 int i;
1644 isl_vec *sol;
1645 isl_basic_set *lp;
1647 for (i = 0; i < graph->n; ++i) {
1648 struct isl_sched_node *node = &graph->node[i];
1649 int skip = node->rank;
1650 graph->region[i].pos = node->start + 1 + 2*(node->nparam+skip);
1651 if (needs_row(graph, node))
1652 graph->region[i].len = 2 * (node->nvar - skip);
1653 else
1654 graph->region[i].len = 0;
1656 lp = isl_basic_set_copy(graph->lp);
1657 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
1658 graph->region, &check_conflict, graph);
1659 return sol;
1662 /* Update the schedules of all nodes based on the given solution
1663 * of the LP problem.
1664 * The new row is added to the current band.
1665 * All possibly negative coefficients are encoded as a difference
1666 * of two non-negative variables, so we need to perform the subtraction
1667 * here. Moreover, if use_cmap is set, then the solution does
1668 * not refer to the actual coefficients c_i_x, but instead to variables
1669 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
1670 * In this case, we then also need to perform this multiplication
1671 * to obtain the values of c_i_x.
1673 * If check_zero is set, then the first two coordinates of sol are
1674 * assumed to correspond to the dependence distance. If these two
1675 * coordinates are zero, then the corresponding scheduling dimension
1676 * is marked as being zero distance.
1678 static int update_schedule(struct isl_sched_graph *graph,
1679 __isl_take isl_vec *sol, int use_cmap, int check_zero)
1681 int i, j;
1682 int zero = 0;
1683 isl_vec *csol = NULL;
1685 if (!sol)
1686 goto error;
1687 if (sol->size == 0)
1688 isl_die(sol->ctx, isl_error_internal,
1689 "no solution found", goto error);
1690 if (graph->n_total_row >= graph->max_row)
1691 isl_die(sol->ctx, isl_error_internal,
1692 "too many schedule rows", goto error);
1694 if (check_zero)
1695 zero = isl_int_is_zero(sol->el[1]) &&
1696 isl_int_is_zero(sol->el[2]);
1698 for (i = 0; i < graph->n; ++i) {
1699 struct isl_sched_node *node = &graph->node[i];
1700 int pos = node->start;
1701 int row = isl_mat_rows(node->sched);
1703 isl_vec_free(csol);
1704 csol = isl_vec_alloc(sol->ctx, node->nvar);
1705 if (!csol)
1706 goto error;
1708 isl_map_free(node->sched_map);
1709 node->sched_map = NULL;
1710 node->sched = isl_mat_add_rows(node->sched, 1);
1711 if (!node->sched)
1712 goto error;
1713 node->sched = isl_mat_set_element(node->sched, row, 0,
1714 sol->el[1 + pos]);
1715 for (j = 0; j < node->nparam + node->nvar; ++j)
1716 isl_int_sub(sol->el[1 + pos + 1 + 2 * j + 1],
1717 sol->el[1 + pos + 1 + 2 * j + 1],
1718 sol->el[1 + pos + 1 + 2 * j]);
1719 for (j = 0; j < node->nparam; ++j)
1720 node->sched = isl_mat_set_element(node->sched,
1721 row, 1 + j, sol->el[1+pos+1+2*j+1]);
1722 for (j = 0; j < node->nvar; ++j)
1723 isl_int_set(csol->el[j],
1724 sol->el[1+pos+1+2*(node->nparam+j)+1]);
1725 if (use_cmap)
1726 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
1727 csol);
1728 if (!csol)
1729 goto error;
1730 for (j = 0; j < node->nvar; ++j)
1731 node->sched = isl_mat_set_element(node->sched,
1732 row, 1 + node->nparam + j, csol->el[j]);
1733 node->band[graph->n_total_row] = graph->n_band;
1734 node->zero[graph->n_total_row] = zero;
1736 isl_vec_free(sol);
1737 isl_vec_free(csol);
1739 graph->n_row++;
1740 graph->n_total_row++;
1742 return 0;
1743 error:
1744 isl_vec_free(sol);
1745 isl_vec_free(csol);
1746 return -1;
1749 /* Convert row "row" of node->sched into an isl_aff living in "ls"
1750 * and return this isl_aff.
1752 static __isl_give isl_aff *extract_schedule_row(__isl_take isl_local_space *ls,
1753 struct isl_sched_node *node, int row)
1755 int j;
1756 isl_int v;
1757 isl_aff *aff;
1759 isl_int_init(v);
1761 aff = isl_aff_zero_on_domain(ls);
1762 isl_mat_get_element(node->sched, row, 0, &v);
1763 aff = isl_aff_set_constant(aff, v);
1764 for (j = 0; j < node->nparam; ++j) {
1765 isl_mat_get_element(node->sched, row, 1 + j, &v);
1766 aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
1768 for (j = 0; j < node->nvar; ++j) {
1769 isl_mat_get_element(node->sched, row, 1 + node->nparam + j, &v);
1770 aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
1773 isl_int_clear(v);
1775 return aff;
1778 /* Convert node->sched into a multi_aff and return this multi_aff.
1780 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
1781 struct isl_sched_node *node)
1783 int i;
1784 isl_space *space;
1785 isl_local_space *ls;
1786 isl_aff *aff;
1787 isl_multi_aff *ma;
1788 int nrow, ncol;
1790 nrow = isl_mat_rows(node->sched);
1791 ncol = isl_mat_cols(node->sched) - 1;
1792 space = isl_space_from_domain(isl_space_copy(node->dim));
1793 space = isl_space_add_dims(space, isl_dim_out, nrow);
1794 ma = isl_multi_aff_zero(space);
1795 ls = isl_local_space_from_space(isl_space_copy(node->dim));
1797 for (i = 0; i < nrow; ++i) {
1798 aff = extract_schedule_row(isl_local_space_copy(ls), node, i);
1799 ma = isl_multi_aff_set_aff(ma, i, aff);
1802 isl_local_space_free(ls);
1804 return ma;
1807 /* Convert node->sched into a map and return this map.
1809 * The result is cached in node->sched_map, which needs to be released
1810 * whenever node->sched is updated.
1812 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
1814 if (!node->sched_map) {
1815 isl_multi_aff *ma;
1817 ma = node_extract_schedule_multi_aff(node);
1818 node->sched_map = isl_map_from_multi_aff(ma);
1821 return isl_map_copy(node->sched_map);
1824 /* Update the given dependence relation based on the current schedule.
1825 * That is, intersect the dependence relation with a map expressing
1826 * that source and sink are executed within the same iteration of
1827 * the current schedule.
1828 * This is not the most efficient way, but this shouldn't be a critical
1829 * operation.
1831 static __isl_give isl_map *specialize(__isl_take isl_map *map,
1832 struct isl_sched_node *src, struct isl_sched_node *dst)
1834 isl_map *src_sched, *dst_sched, *id;
1836 src_sched = node_extract_schedule(src);
1837 dst_sched = node_extract_schedule(dst);
1838 id = isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
1839 return isl_map_intersect(map, id);
1842 /* Update the dependence relations of all edges based on the current schedule.
1843 * If a dependence is carried completely by the current schedule, then
1844 * it is removed from the edge_tables. It is kept in the list of edges
1845 * as otherwise all edge_tables would have to be recomputed.
1847 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
1849 int i;
1851 for (i = graph->n_edge - 1; i >= 0; --i) {
1852 struct isl_sched_edge *edge = &graph->edge[i];
1853 edge->map = specialize(edge->map, edge->src, edge->dst);
1854 if (!edge->map)
1855 return -1;
1857 if (isl_map_plain_is_empty(edge->map))
1858 graph_remove_edge(graph, edge);
1861 return 0;
1864 static void next_band(struct isl_sched_graph *graph)
1866 graph->band_start = graph->n_total_row;
1867 graph->n_band++;
1870 /* Topologically sort statements mapped to the same schedule iteration
1871 * and add a row to the schedule corresponding to this order.
1873 static int sort_statements(isl_ctx *ctx, struct isl_sched_graph *graph)
1875 int i, j;
1877 if (graph->n <= 1)
1878 return 0;
1880 if (update_edges(ctx, graph) < 0)
1881 return -1;
1883 if (graph->n_edge == 0)
1884 return 0;
1886 if (detect_sccs(ctx, graph) < 0)
1887 return -1;
1889 if (graph->n_total_row >= graph->max_row)
1890 isl_die(ctx, isl_error_internal,
1891 "too many schedule rows", return -1);
1893 for (i = 0; i < graph->n; ++i) {
1894 struct isl_sched_node *node = &graph->node[i];
1895 int row = isl_mat_rows(node->sched);
1896 int cols = isl_mat_cols(node->sched);
1898 isl_map_free(node->sched_map);
1899 node->sched_map = NULL;
1900 node->sched = isl_mat_add_rows(node->sched, 1);
1901 if (!node->sched)
1902 return -1;
1903 node->sched = isl_mat_set_element_si(node->sched, row, 0,
1904 node->scc);
1905 for (j = 1; j < cols; ++j)
1906 node->sched = isl_mat_set_element_si(node->sched,
1907 row, j, 0);
1908 node->band[graph->n_total_row] = graph->n_band;
1911 graph->n_total_row++;
1912 next_band(graph);
1914 return 0;
1917 /* Construct an isl_schedule based on the computed schedule stored
1918 * in graph and with parameters specified by dim.
1920 static __isl_give isl_schedule *extract_schedule(struct isl_sched_graph *graph,
1921 __isl_take isl_space *dim)
1923 int i;
1924 isl_ctx *ctx;
1925 isl_schedule *sched = NULL;
1927 if (!dim)
1928 return NULL;
1930 ctx = isl_space_get_ctx(dim);
1931 sched = isl_calloc(ctx, struct isl_schedule,
1932 sizeof(struct isl_schedule) +
1933 (graph->n - 1) * sizeof(struct isl_schedule_node));
1934 if (!sched)
1935 goto error;
1937 sched->ref = 1;
1938 sched->n = graph->n;
1939 sched->n_band = graph->n_band;
1940 sched->n_total_row = graph->n_total_row;
1942 for (i = 0; i < sched->n; ++i) {
1943 int r, b;
1944 int *band_end, *band_id, *zero;
1946 sched->node[i].sched =
1947 node_extract_schedule_multi_aff(&graph->node[i]);
1948 if (!sched->node[i].sched)
1949 goto error;
1951 sched->node[i].n_band = graph->n_band;
1952 if (graph->n_band == 0)
1953 continue;
1955 band_end = isl_alloc_array(ctx, int, graph->n_band);
1956 band_id = isl_alloc_array(ctx, int, graph->n_band);
1957 zero = isl_alloc_array(ctx, int, graph->n_total_row);
1958 sched->node[i].band_end = band_end;
1959 sched->node[i].band_id = band_id;
1960 sched->node[i].zero = zero;
1961 if (!band_end || !band_id || !zero)
1962 goto error;
1964 for (r = 0; r < graph->n_total_row; ++r)
1965 zero[r] = graph->node[i].zero[r];
1966 for (r = b = 0; r < graph->n_total_row; ++r) {
1967 if (graph->node[i].band[r] == b)
1968 continue;
1969 band_end[b++] = r;
1970 if (graph->node[i].band[r] == -1)
1971 break;
1973 if (r == graph->n_total_row)
1974 band_end[b++] = r;
1975 sched->node[i].n_band = b;
1976 for (--b; b >= 0; --b)
1977 band_id[b] = graph->node[i].band_id[b];
1980 sched->dim = dim;
1982 return sched;
1983 error:
1984 isl_space_free(dim);
1985 isl_schedule_free(sched);
1986 return NULL;
1989 /* Copy nodes that satisfy node_pred from the src dependence graph
1990 * to the dst dependence graph.
1992 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
1993 int (*node_pred)(struct isl_sched_node *node, int data), int data)
1995 int i;
1997 dst->n = 0;
1998 for (i = 0; i < src->n; ++i) {
1999 if (!node_pred(&src->node[i], data))
2000 continue;
2001 dst->node[dst->n].dim = isl_space_copy(src->node[i].dim);
2002 dst->node[dst->n].nvar = src->node[i].nvar;
2003 dst->node[dst->n].nparam = src->node[i].nparam;
2004 dst->node[dst->n].sched = isl_mat_copy(src->node[i].sched);
2005 dst->node[dst->n].sched_map =
2006 isl_map_copy(src->node[i].sched_map);
2007 dst->node[dst->n].band = src->node[i].band;
2008 dst->node[dst->n].band_id = src->node[i].band_id;
2009 dst->node[dst->n].zero = src->node[i].zero;
2010 dst->n++;
2013 return 0;
2016 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
2017 * to the dst dependence graph.
2018 * If the source or destination node of the edge is not in the destination
2019 * graph, then it must be a backward proximity edge and it should simply
2020 * be ignored.
2022 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
2023 struct isl_sched_graph *src,
2024 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
2026 int i;
2027 enum isl_edge_type t;
2029 dst->n_edge = 0;
2030 for (i = 0; i < src->n_edge; ++i) {
2031 struct isl_sched_edge *edge = &src->edge[i];
2032 isl_map *map;
2033 struct isl_sched_node *dst_src, *dst_dst;
2035 if (!edge_pred(edge, data))
2036 continue;
2038 if (isl_map_plain_is_empty(edge->map))
2039 continue;
2041 dst_src = graph_find_node(ctx, dst, edge->src->dim);
2042 dst_dst = graph_find_node(ctx, dst, edge->dst->dim);
2043 if (!dst_src || !dst_dst) {
2044 if (edge->validity)
2045 isl_die(ctx, isl_error_internal,
2046 "backward validity edge", return -1);
2047 continue;
2050 map = isl_map_copy(edge->map);
2052 dst->edge[dst->n_edge].src = dst_src;
2053 dst->edge[dst->n_edge].dst = dst_dst;
2054 dst->edge[dst->n_edge].map = map;
2055 dst->edge[dst->n_edge].validity = edge->validity;
2056 dst->edge[dst->n_edge].proximity = edge->proximity;
2057 dst->n_edge++;
2059 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
2060 if (edge !=
2061 graph_find_edge(src, t, edge->src, edge->dst))
2062 continue;
2063 if (graph_edge_table_add(ctx, dst, t,
2064 &dst->edge[dst->n_edge - 1]) < 0)
2065 return -1;
2069 return 0;
2072 /* Given a "src" dependence graph that contains the nodes from "dst"
2073 * that satisfy node_pred, copy the schedule computed in "src"
2074 * for those nodes back to "dst".
2076 static int copy_schedule(struct isl_sched_graph *dst,
2077 struct isl_sched_graph *src,
2078 int (*node_pred)(struct isl_sched_node *node, int data), int data)
2080 int i;
2082 src->n = 0;
2083 for (i = 0; i < dst->n; ++i) {
2084 if (!node_pred(&dst->node[i], data))
2085 continue;
2086 isl_mat_free(dst->node[i].sched);
2087 isl_map_free(dst->node[i].sched_map);
2088 dst->node[i].sched = isl_mat_copy(src->node[src->n].sched);
2089 dst->node[i].sched_map =
2090 isl_map_copy(src->node[src->n].sched_map);
2091 src->n++;
2094 dst->max_row = src->max_row;
2095 dst->n_total_row = src->n_total_row;
2096 dst->n_band = src->n_band;
2098 return 0;
2101 /* Compute the maximal number of variables over all nodes.
2102 * This is the maximal number of linearly independent schedule
2103 * rows that we need to compute.
2104 * Just in case we end up in a part of the dependence graph
2105 * with only lower-dimensional domains, we make sure we will
2106 * compute the required amount of extra linearly independent rows.
2108 static int compute_maxvar(struct isl_sched_graph *graph)
2110 int i;
2112 graph->maxvar = 0;
2113 for (i = 0; i < graph->n; ++i) {
2114 struct isl_sched_node *node = &graph->node[i];
2115 int nvar;
2117 if (node_update_cmap(node) < 0)
2118 return -1;
2119 nvar = node->nvar + graph->n_row - node->rank;
2120 if (nvar > graph->maxvar)
2121 graph->maxvar = nvar;
2124 return 0;
2127 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph);
2128 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph);
2130 /* Compute a schedule for a subgraph of "graph". In particular, for
2131 * the graph composed of nodes that satisfy node_pred and edges that
2132 * that satisfy edge_pred. The caller should precompute the number
2133 * of nodes and edges that satisfy these predicates and pass them along
2134 * as "n" and "n_edge".
2135 * If the subgraph is known to consist of a single component, then wcc should
2136 * be set and then we call compute_schedule_wcc on the constructed subgraph.
2137 * Otherwise, we call compute_schedule, which will check whether the subgraph
2138 * is connected.
2140 static int compute_sub_schedule(isl_ctx *ctx,
2141 struct isl_sched_graph *graph, int n, int n_edge,
2142 int (*node_pred)(struct isl_sched_node *node, int data),
2143 int (*edge_pred)(struct isl_sched_edge *edge, int data),
2144 int data, int wcc)
2146 struct isl_sched_graph split = { 0 };
2147 int t;
2149 if (graph_alloc(ctx, &split, n, n_edge) < 0)
2150 goto error;
2151 if (copy_nodes(&split, graph, node_pred, data) < 0)
2152 goto error;
2153 if (graph_init_table(ctx, &split) < 0)
2154 goto error;
2155 for (t = 0; t <= isl_edge_last; ++t)
2156 split.max_edge[t] = graph->max_edge[t];
2157 if (graph_init_edge_tables(ctx, &split) < 0)
2158 goto error;
2159 if (copy_edges(ctx, &split, graph, edge_pred, data) < 0)
2160 goto error;
2161 split.n_row = graph->n_row;
2162 split.max_row = graph->max_row;
2163 split.n_total_row = graph->n_total_row;
2164 split.n_band = graph->n_band;
2165 split.band_start = graph->band_start;
2167 if (wcc && compute_schedule_wcc(ctx, &split) < 0)
2168 goto error;
2169 if (!wcc && compute_schedule(ctx, &split) < 0)
2170 goto error;
2172 copy_schedule(graph, &split, node_pred, data);
2174 graph_free(ctx, &split);
2175 return 0;
2176 error:
2177 graph_free(ctx, &split);
2178 return -1;
2181 static int node_scc_exactly(struct isl_sched_node *node, int scc)
2183 return node->scc == scc;
2186 static int node_scc_at_most(struct isl_sched_node *node, int scc)
2188 return node->scc <= scc;
2191 static int node_scc_at_least(struct isl_sched_node *node, int scc)
2193 return node->scc >= scc;
2196 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
2198 return edge->src->scc == scc && edge->dst->scc == scc;
2201 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
2203 return edge->dst->scc <= scc;
2206 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
2208 return edge->src->scc >= scc;
2211 /* Pad the schedules of all nodes with zero rows such that in the end
2212 * they all have graph->n_total_row rows.
2213 * The extra rows don't belong to any band, so they get assigned band number -1.
2215 static int pad_schedule(struct isl_sched_graph *graph)
2217 int i, j;
2219 for (i = 0; i < graph->n; ++i) {
2220 struct isl_sched_node *node = &graph->node[i];
2221 int row = isl_mat_rows(node->sched);
2222 if (graph->n_total_row > row) {
2223 isl_map_free(node->sched_map);
2224 node->sched_map = NULL;
2226 node->sched = isl_mat_add_zero_rows(node->sched,
2227 graph->n_total_row - row);
2228 if (!node->sched)
2229 return -1;
2230 for (j = row; j < graph->n_total_row; ++j)
2231 node->band[j] = -1;
2234 return 0;
2237 /* Reset the current band by dropping all its schedule rows.
2239 static int reset_band(struct isl_sched_graph *graph)
2241 int i;
2242 int drop;
2244 drop = graph->n_total_row - graph->band_start;
2245 graph->n_total_row -= drop;
2246 graph->n_row -= drop;
2248 for (i = 0; i < graph->n; ++i) {
2249 struct isl_sched_node *node = &graph->node[i];
2251 isl_map_free(node->sched_map);
2252 node->sched_map = NULL;
2254 node->sched = isl_mat_drop_rows(node->sched,
2255 graph->band_start, drop);
2257 if (!node->sched)
2258 return -1;
2261 return 0;
2264 /* Split the current graph into two parts and compute a schedule for each
2265 * part individually. In particular, one part consists of all SCCs up
2266 * to and including graph->src_scc, while the other part contains the other
2267 * SCCS.
2269 * The split is enforced in the schedule by constant rows with two different
2270 * values (0 and 1). These constant rows replace the previously computed rows
2271 * in the current band.
2272 * It would be possible to reuse them as the first rows in the next
2273 * band, but recomputing them may result in better rows as we are looking
2274 * at a smaller part of the dependence graph.
2275 * compute_split_schedule is only called when no zero-distance schedule row
2276 * could be found on the entire graph, so we wark the splitting row as
2277 * non zero-distance.
2279 * The band_id of the second group is set to n, where n is the number
2280 * of nodes in the first group. This ensures that the band_ids over
2281 * the two groups remain disjoint, even if either or both of the two
2282 * groups contain independent components.
2284 static int compute_split_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
2286 int i, j, n, e1, e2;
2287 int n_total_row, orig_total_row;
2288 int n_band, orig_band;
2290 if (graph->n_total_row >= graph->max_row)
2291 isl_die(ctx, isl_error_internal,
2292 "too many schedule rows", return -1);
2294 if (reset_band(graph) < 0)
2295 return -1;
2297 n = 0;
2298 for (i = 0; i < graph->n; ++i) {
2299 struct isl_sched_node *node = &graph->node[i];
2300 int row = isl_mat_rows(node->sched);
2301 int cols = isl_mat_cols(node->sched);
2302 int before = node->scc <= graph->src_scc;
2304 if (before)
2305 n++;
2307 isl_map_free(node->sched_map);
2308 node->sched_map = NULL;
2309 node->sched = isl_mat_add_rows(node->sched, 1);
2310 if (!node->sched)
2311 return -1;
2312 node->sched = isl_mat_set_element_si(node->sched, row, 0,
2313 !before);
2314 for (j = 1; j < cols; ++j)
2315 node->sched = isl_mat_set_element_si(node->sched,
2316 row, j, 0);
2317 node->band[graph->n_total_row] = graph->n_band;
2318 node->zero[graph->n_total_row] = 0;
2321 e1 = e2 = 0;
2322 for (i = 0; i < graph->n_edge; ++i) {
2323 if (graph->edge[i].dst->scc <= graph->src_scc)
2324 e1++;
2325 if (graph->edge[i].src->scc > graph->src_scc)
2326 e2++;
2329 graph->n_total_row++;
2330 next_band(graph);
2332 for (i = 0; i < graph->n; ++i) {
2333 struct isl_sched_node *node = &graph->node[i];
2334 if (node->scc > graph->src_scc)
2335 node->band_id[graph->n_band] = n;
2338 orig_total_row = graph->n_total_row;
2339 orig_band = graph->n_band;
2340 if (compute_sub_schedule(ctx, graph, n, e1,
2341 &node_scc_at_most, &edge_dst_scc_at_most,
2342 graph->src_scc, 0) < 0)
2343 return -1;
2344 n_total_row = graph->n_total_row;
2345 graph->n_total_row = orig_total_row;
2346 n_band = graph->n_band;
2347 graph->n_band = orig_band;
2348 if (compute_sub_schedule(ctx, graph, graph->n - n, e2,
2349 &node_scc_at_least, &edge_src_scc_at_least,
2350 graph->src_scc + 1, 0) < 0)
2351 return -1;
2352 if (n_total_row > graph->n_total_row)
2353 graph->n_total_row = n_total_row;
2354 if (n_band > graph->n_band)
2355 graph->n_band = n_band;
2357 return pad_schedule(graph);
2360 /* Compute the next band of the schedule after updating the dependence
2361 * relations based on the the current schedule.
2363 static int compute_next_band(isl_ctx *ctx, struct isl_sched_graph *graph)
2365 if (update_edges(ctx, graph) < 0)
2366 return -1;
2367 next_band(graph);
2369 return compute_schedule(ctx, graph);
2372 /* Add constraints to graph->lp that force the dependence "map" (which
2373 * is part of the dependence relation of "edge")
2374 * to be respected and attempt to carry it, where the edge is one from
2375 * a node j to itself. "pos" is the sequence number of the given map.
2376 * That is, add constraints that enforce
2378 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
2379 * = c_j_x (y - x) >= e_i
2381 * for each (x,y) in R.
2382 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2383 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
2384 * with each coefficient in c_j_x represented as a pair of non-negative
2385 * coefficients.
2387 static int add_intra_constraints(struct isl_sched_graph *graph,
2388 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
2390 unsigned total;
2391 isl_ctx *ctx = isl_map_get_ctx(map);
2392 isl_space *dim;
2393 isl_dim_map *dim_map;
2394 isl_basic_set *coef;
2395 struct isl_sched_node *node = edge->src;
2397 coef = intra_coefficients(graph, map);
2398 if (!coef)
2399 return -1;
2401 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
2403 total = isl_basic_set_total_dim(graph->lp);
2404 dim_map = isl_dim_map_alloc(ctx, total);
2405 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
2406 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
2407 isl_space_dim(dim, isl_dim_set), 1,
2408 node->nvar, -1);
2409 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
2410 isl_space_dim(dim, isl_dim_set), 1,
2411 node->nvar, 1);
2412 graph->lp = isl_basic_set_extend_constraints(graph->lp,
2413 coef->n_eq, coef->n_ineq);
2414 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
2415 coef, dim_map);
2416 isl_space_free(dim);
2418 return 0;
2421 /* Add constraints to graph->lp that force the dependence "map" (which
2422 * is part of the dependence relation of "edge")
2423 * to be respected and attempt to carry it, where the edge is one from
2424 * node j to node k. "pos" is the sequence number of the given map.
2425 * That is, add constraints that enforce
2427 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
2429 * for each (x,y) in R.
2430 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2431 * of valid constraints for R and then plug in
2432 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
2433 * with each coefficient (except e_i, c_k_0 and c_j_0)
2434 * represented as a pair of non-negative coefficients.
2436 static int add_inter_constraints(struct isl_sched_graph *graph,
2437 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
2439 unsigned total;
2440 isl_ctx *ctx = isl_map_get_ctx(map);
2441 isl_space *dim;
2442 isl_dim_map *dim_map;
2443 isl_basic_set *coef;
2444 struct isl_sched_node *src = edge->src;
2445 struct isl_sched_node *dst = edge->dst;
2447 coef = inter_coefficients(graph, map);
2448 if (!coef)
2449 return -1;
2451 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
2453 total = isl_basic_set_total_dim(graph->lp);
2454 dim_map = isl_dim_map_alloc(ctx, total);
2456 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
2458 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
2459 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
2460 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
2461 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
2462 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
2463 dst->nvar, -1);
2464 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
2465 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
2466 dst->nvar, 1);
2468 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
2469 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
2470 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
2471 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
2472 isl_space_dim(dim, isl_dim_set), 1,
2473 src->nvar, 1);
2474 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
2475 isl_space_dim(dim, isl_dim_set), 1,
2476 src->nvar, -1);
2478 graph->lp = isl_basic_set_extend_constraints(graph->lp,
2479 coef->n_eq, coef->n_ineq);
2480 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
2481 coef, dim_map);
2482 isl_space_free(dim);
2484 return 0;
2487 /* Add constraints to graph->lp that force all validity dependences
2488 * to be respected and attempt to carry them.
2490 static int add_all_constraints(struct isl_sched_graph *graph)
2492 int i, j;
2493 int pos;
2495 pos = 0;
2496 for (i = 0; i < graph->n_edge; ++i) {
2497 struct isl_sched_edge *edge= &graph->edge[i];
2499 if (!edge->validity)
2500 continue;
2502 for (j = 0; j < edge->map->n; ++j) {
2503 isl_basic_map *bmap;
2504 isl_map *map;
2506 bmap = isl_basic_map_copy(edge->map->p[j]);
2507 map = isl_map_from_basic_map(bmap);
2509 if (edge->src == edge->dst &&
2510 add_intra_constraints(graph, edge, map, pos) < 0)
2511 return -1;
2512 if (edge->src != edge->dst &&
2513 add_inter_constraints(graph, edge, map, pos) < 0)
2514 return -1;
2515 ++pos;
2519 return 0;
2522 /* Count the number of equality and inequality constraints
2523 * that will be added to the carry_lp problem.
2524 * We count each edge exactly once.
2526 static int count_all_constraints(struct isl_sched_graph *graph,
2527 int *n_eq, int *n_ineq)
2529 int i, j;
2531 *n_eq = *n_ineq = 0;
2532 for (i = 0; i < graph->n_edge; ++i) {
2533 struct isl_sched_edge *edge= &graph->edge[i];
2534 for (j = 0; j < edge->map->n; ++j) {
2535 isl_basic_map *bmap;
2536 isl_map *map;
2538 bmap = isl_basic_map_copy(edge->map->p[j]);
2539 map = isl_map_from_basic_map(bmap);
2541 if (count_map_constraints(graph, edge, map,
2542 n_eq, n_ineq, 1) < 0)
2543 return -1;
2547 return 0;
2550 /* Construct an LP problem for finding schedule coefficients
2551 * such that the schedule carries as many dependences as possible.
2552 * In particular, for each dependence i, we bound the dependence distance
2553 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
2554 * of all e_i's. Dependence with e_i = 0 in the solution are simply
2555 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
2556 * Note that if the dependence relation is a union of basic maps,
2557 * then we have to consider each basic map individually as it may only
2558 * be possible to carry the dependences expressed by some of those
2559 * basic maps and not all off them.
2560 * Below, we consider each of those basic maps as a separate "edge".
2562 * All variables of the LP are non-negative. The actual coefficients
2563 * may be negative, so each coefficient is represented as the difference
2564 * of two non-negative variables. The negative part always appears
2565 * immediately before the positive part.
2566 * Other than that, the variables have the following order
2568 * - sum of (1 - e_i) over all edges
2569 * - sum of positive and negative parts of all c_n coefficients
2570 * (unconstrained when computing non-parametric schedules)
2571 * - sum of positive and negative parts of all c_x coefficients
2572 * - for each edge
2573 * - e_i
2574 * - for each node
2575 * - c_i_0
2576 * - positive and negative parts of c_i_n (if parametric)
2577 * - positive and negative parts of c_i_x
2579 * The constraints are those from the (validity) edges plus three equalities
2580 * to express the sums and n_edge inequalities to express e_i <= 1.
2582 static int setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
2584 int i, j;
2585 int k;
2586 isl_space *dim;
2587 unsigned total;
2588 int n_eq, n_ineq;
2589 int n_edge;
2591 n_edge = 0;
2592 for (i = 0; i < graph->n_edge; ++i)
2593 n_edge += graph->edge[i].map->n;
2595 total = 3 + n_edge;
2596 for (i = 0; i < graph->n; ++i) {
2597 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2598 node->start = total;
2599 total += 1 + 2 * (node->nparam + node->nvar);
2602 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
2603 return -1;
2604 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
2605 return -1;
2607 dim = isl_space_set_alloc(ctx, 0, total);
2608 isl_basic_set_free(graph->lp);
2609 n_eq += 3;
2610 n_ineq += n_edge;
2611 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
2612 graph->lp = isl_basic_set_set_rational(graph->lp);
2614 k = isl_basic_set_alloc_equality(graph->lp);
2615 if (k < 0)
2616 return -1;
2617 isl_seq_clr(graph->lp->eq[k], 1 + total);
2618 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
2619 isl_int_set_si(graph->lp->eq[k][1], 1);
2620 for (i = 0; i < n_edge; ++i)
2621 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
2623 k = isl_basic_set_alloc_equality(graph->lp);
2624 if (k < 0)
2625 return -1;
2626 isl_seq_clr(graph->lp->eq[k], 1 + total);
2627 isl_int_set_si(graph->lp->eq[k][2], -1);
2628 for (i = 0; i < graph->n; ++i) {
2629 int pos = 1 + graph->node[i].start + 1;
2631 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
2632 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2635 k = isl_basic_set_alloc_equality(graph->lp);
2636 if (k < 0)
2637 return -1;
2638 isl_seq_clr(graph->lp->eq[k], 1 + total);
2639 isl_int_set_si(graph->lp->eq[k][3], -1);
2640 for (i = 0; i < graph->n; ++i) {
2641 struct isl_sched_node *node = &graph->node[i];
2642 int pos = 1 + node->start + 1 + 2 * node->nparam;
2644 for (j = 0; j < 2 * node->nvar; ++j)
2645 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2648 for (i = 0; i < n_edge; ++i) {
2649 k = isl_basic_set_alloc_inequality(graph->lp);
2650 if (k < 0)
2651 return -1;
2652 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2653 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
2654 isl_int_set_si(graph->lp->ineq[k][0], 1);
2657 if (add_bound_coefficient_constraints(ctx, graph) < 0)
2658 return -1;
2659 if (add_all_constraints(graph) < 0)
2660 return -1;
2662 return 0;
2665 /* If the schedule_split_scaled option is set and if the linear
2666 * parts of the scheduling rows for all nodes in the graphs have
2667 * non-trivial common divisor, then split off the constant term
2668 * from the linear part.
2669 * The constant term is then placed in a separate band and
2670 * the linear part is reduced.
2672 static int split_scaled(isl_ctx *ctx, struct isl_sched_graph *graph)
2674 int i;
2675 int row;
2676 isl_int gcd, gcd_i;
2678 if (!ctx->opt->schedule_split_scaled)
2679 return 0;
2680 if (graph->n <= 1)
2681 return 0;
2683 if (graph->n_total_row >= graph->max_row)
2684 isl_die(ctx, isl_error_internal,
2685 "too many schedule rows", return -1);
2687 isl_int_init(gcd);
2688 isl_int_init(gcd_i);
2690 isl_int_set_si(gcd, 0);
2692 row = isl_mat_rows(graph->node[0].sched) - 1;
2694 for (i = 0; i < graph->n; ++i) {
2695 struct isl_sched_node *node = &graph->node[i];
2696 int cols = isl_mat_cols(node->sched);
2698 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
2699 isl_int_gcd(gcd, gcd, gcd_i);
2702 isl_int_clear(gcd_i);
2704 if (isl_int_cmp_si(gcd, 1) <= 0) {
2705 isl_int_clear(gcd);
2706 return 0;
2709 next_band(graph);
2711 for (i = 0; i < graph->n; ++i) {
2712 struct isl_sched_node *node = &graph->node[i];
2714 isl_map_free(node->sched_map);
2715 node->sched_map = NULL;
2716 node->sched = isl_mat_add_zero_rows(node->sched, 1);
2717 if (!node->sched)
2718 goto error;
2719 isl_int_fdiv_r(node->sched->row[row + 1][0],
2720 node->sched->row[row][0], gcd);
2721 isl_int_fdiv_q(node->sched->row[row][0],
2722 node->sched->row[row][0], gcd);
2723 isl_int_mul(node->sched->row[row][0],
2724 node->sched->row[row][0], gcd);
2725 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
2726 if (!node->sched)
2727 goto error;
2728 node->band[graph->n_total_row] = graph->n_band;
2731 graph->n_total_row++;
2733 isl_int_clear(gcd);
2734 return 0;
2735 error:
2736 isl_int_clear(gcd);
2737 return -1;
2740 static int compute_component_schedule(isl_ctx *ctx,
2741 struct isl_sched_graph *graph);
2743 /* Is the schedule row "sol" trivial on node "node"?
2744 * That is, is the solution zero on the dimensions orthogonal to
2745 * the previously found solutions?
2746 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
2748 * Each coefficient is represented as the difference between
2749 * two non-negative values in "sol". "sol" has been computed
2750 * in terms of the original iterators (i.e., without use of cmap).
2751 * We construct the schedule row s and write it as a linear
2752 * combination of (linear combinations of) previously computed schedule rows.
2753 * s = Q c or c = U s.
2754 * If the final entries of c are all zero, then the solution is trivial.
2756 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
2758 int i;
2759 int pos;
2760 int trivial;
2761 isl_ctx *ctx;
2762 isl_vec *node_sol;
2764 if (!sol)
2765 return -1;
2766 if (node->nvar == node->rank)
2767 return 0;
2769 ctx = isl_vec_get_ctx(sol);
2770 node_sol = isl_vec_alloc(ctx, node->nvar);
2771 if (!node_sol)
2772 return -1;
2774 pos = 1 + node->start + 1 + 2 * node->nparam;
2776 for (i = 0; i < node->nvar; ++i)
2777 isl_int_sub(node_sol->el[i],
2778 sol->el[pos + 2 * i + 1], sol->el[pos + 2 * i]);
2780 node_sol = isl_mat_vec_product(isl_mat_copy(node->cinv), node_sol);
2782 if (!node_sol)
2783 return -1;
2785 trivial = isl_seq_first_non_zero(node_sol->el + node->rank,
2786 node->nvar - node->rank) == -1;
2788 isl_vec_free(node_sol);
2790 return trivial;
2793 /* Is the schedule row "sol" trivial on any node where it should
2794 * not be trivial?
2795 * "sol" has been computed in terms of the original iterators
2796 * (i.e., without use of cmap).
2797 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
2799 static int is_any_trivial(struct isl_sched_graph *graph,
2800 __isl_keep isl_vec *sol)
2802 int i;
2804 for (i = 0; i < graph->n; ++i) {
2805 struct isl_sched_node *node = &graph->node[i];
2806 int trivial;
2808 if (!needs_row(graph, node))
2809 continue;
2810 trivial = is_trivial(node, sol);
2811 if (trivial < 0 || trivial)
2812 return trivial;
2815 return 0;
2818 /* Construct a schedule row for each node such that as many dependences
2819 * as possible are carried and then continue with the next band.
2821 * If the computed schedule row turns out to be trivial on one or
2822 * more nodes where it should not be trivial, then we throw it away
2823 * and try again on each component separately.
2825 static int carry_dependences(isl_ctx *ctx, struct isl_sched_graph *graph)
2827 int i;
2828 int n_edge;
2829 int trivial;
2830 isl_vec *sol;
2831 isl_basic_set *lp;
2833 n_edge = 0;
2834 for (i = 0; i < graph->n_edge; ++i)
2835 n_edge += graph->edge[i].map->n;
2837 if (setup_carry_lp(ctx, graph) < 0)
2838 return -1;
2840 lp = isl_basic_set_copy(graph->lp);
2841 sol = isl_tab_basic_set_non_neg_lexmin(lp);
2842 if (!sol)
2843 return -1;
2845 if (sol->size == 0) {
2846 isl_vec_free(sol);
2847 isl_die(ctx, isl_error_internal,
2848 "error in schedule construction", return -1);
2851 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
2852 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
2853 isl_vec_free(sol);
2854 isl_die(ctx, isl_error_unknown,
2855 "unable to carry dependences", return -1);
2858 trivial = is_any_trivial(graph, sol);
2859 if (trivial < 0) {
2860 sol = isl_vec_free(sol);
2861 } else if (trivial) {
2862 isl_vec_free(sol);
2863 if (graph->scc > 1)
2864 return compute_component_schedule(ctx, graph);
2865 isl_die(ctx, isl_error_unknown,
2866 "unable to construct non-trivial solution", return -1);
2869 if (update_schedule(graph, sol, 0, 0) < 0)
2870 return -1;
2872 if (split_scaled(ctx, graph) < 0)
2873 return -1;
2875 return compute_next_band(ctx, graph);
2878 /* Are there any (non-empty) validity edges in the graph?
2880 static int has_validity_edges(struct isl_sched_graph *graph)
2882 int i;
2884 for (i = 0; i < graph->n_edge; ++i) {
2885 int empty;
2887 empty = isl_map_plain_is_empty(graph->edge[i].map);
2888 if (empty < 0)
2889 return -1;
2890 if (empty)
2891 continue;
2892 if (graph->edge[i].validity)
2893 return 1;
2896 return 0;
2899 /* Should we apply a Feautrier step?
2900 * That is, did the user request the Feautrier algorithm and are
2901 * there any validity dependences (left)?
2903 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
2905 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
2906 return 0;
2908 return has_validity_edges(graph);
2911 /* Compute a schedule for a connected dependence graph using Feautrier's
2912 * multi-dimensional scheduling algorithm.
2913 * The original algorithm is described in [1].
2914 * The main idea is to minimize the number of scheduling dimensions, by
2915 * trying to satisfy as many dependences as possible per scheduling dimension.
2917 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
2918 * Problem, Part II: Multi-Dimensional Time.
2919 * In Intl. Journal of Parallel Programming, 1992.
2921 static int compute_schedule_wcc_feautrier(isl_ctx *ctx,
2922 struct isl_sched_graph *graph)
2924 return carry_dependences(ctx, graph);
2927 /* Compute a schedule for a connected dependence graph.
2928 * We try to find a sequence of as many schedule rows as possible that result
2929 * in non-negative dependence distances (independent of the previous rows
2930 * in the sequence, i.e., such that the sequence is tilable).
2931 * If we can't find any more rows we either
2932 * - split between SCCs and start over (assuming we found an interesting
2933 * pair of SCCs between which to split)
2934 * - continue with the next band (assuming the current band has at least
2935 * one row)
2936 * - try to carry as many dependences as possible and continue with the next
2937 * band
2939 * If Feautrier's algorithm is selected, we first recursively try to satisfy
2940 * as many validity dependences as possible. When all validity dependences
2941 * are satisfied we extend the schedule to a full-dimensional schedule.
2943 * If we manage to complete the schedule, we finish off by topologically
2944 * sorting the statements based on the remaining dependences.
2946 * If ctx->opt->schedule_outer_zero_distance is set, then we force the
2947 * outermost dimension in the current band to be zero distance. If this
2948 * turns out to be impossible, we fall back on the general scheme above
2949 * and try to carry as many dependences as possible.
2951 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph)
2953 int force_zero = 0;
2955 if (detect_sccs(ctx, graph) < 0)
2956 return -1;
2957 if (sort_sccs(graph) < 0)
2958 return -1;
2960 if (compute_maxvar(graph) < 0)
2961 return -1;
2963 if (need_feautrier_step(ctx, graph))
2964 return compute_schedule_wcc_feautrier(ctx, graph);
2966 if (ctx->opt->schedule_outer_zero_distance)
2967 force_zero = 1;
2969 while (graph->n_row < graph->maxvar) {
2970 isl_vec *sol;
2972 graph->src_scc = -1;
2973 graph->dst_scc = -1;
2975 if (setup_lp(ctx, graph, force_zero) < 0)
2976 return -1;
2977 sol = solve_lp(graph);
2978 if (!sol)
2979 return -1;
2980 if (sol->size == 0) {
2981 isl_vec_free(sol);
2982 if (!ctx->opt->schedule_maximize_band_depth &&
2983 graph->n_total_row > graph->band_start)
2984 return compute_next_band(ctx, graph);
2985 if (graph->src_scc >= 0)
2986 return compute_split_schedule(ctx, graph);
2987 if (graph->n_total_row > graph->band_start)
2988 return compute_next_band(ctx, graph);
2989 return carry_dependences(ctx, graph);
2991 if (update_schedule(graph, sol, 1, 1) < 0)
2992 return -1;
2993 force_zero = 0;
2996 if (graph->n_total_row > graph->band_start)
2997 next_band(graph);
2998 return sort_statements(ctx, graph);
3001 /* Add a row to the schedules that separates the SCCs and move
3002 * to the next band.
3004 static int split_on_scc(isl_ctx *ctx, struct isl_sched_graph *graph)
3006 int i;
3008 if (graph->n_total_row >= graph->max_row)
3009 isl_die(ctx, isl_error_internal,
3010 "too many schedule rows", return -1);
3012 for (i = 0; i < graph->n; ++i) {
3013 struct isl_sched_node *node = &graph->node[i];
3014 int row = isl_mat_rows(node->sched);
3016 isl_map_free(node->sched_map);
3017 node->sched_map = NULL;
3018 node->sched = isl_mat_add_zero_rows(node->sched, 1);
3019 node->sched = isl_mat_set_element_si(node->sched, row, 0,
3020 node->scc);
3021 if (!node->sched)
3022 return -1;
3023 node->band[graph->n_total_row] = graph->n_band;
3026 graph->n_total_row++;
3027 next_band(graph);
3029 return 0;
3032 /* Compute a schedule for each component (identified by node->scc)
3033 * of the dependence graph separately and then combine the results.
3034 * Depending on the setting of schedule_fuse, a component may be
3035 * either weakly or strongly connected.
3037 * The band_id is adjusted such that each component has a separate id.
3038 * Note that the band_id may have already been set to a value different
3039 * from zero by compute_split_schedule.
3041 static int compute_component_schedule(isl_ctx *ctx,
3042 struct isl_sched_graph *graph)
3044 int wcc, i;
3045 int n, n_edge;
3046 int n_total_row, orig_total_row;
3047 int n_band, orig_band;
3049 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN ||
3050 ctx->opt->schedule_separate_components)
3051 if (split_on_scc(ctx, graph) < 0)
3052 return -1;
3054 n_total_row = 0;
3055 orig_total_row = graph->n_total_row;
3056 n_band = 0;
3057 orig_band = graph->n_band;
3058 for (i = 0; i < graph->n; ++i)
3059 graph->node[i].band_id[graph->n_band] += graph->node[i].scc;
3060 for (wcc = 0; wcc < graph->scc; ++wcc) {
3061 n = 0;
3062 for (i = 0; i < graph->n; ++i)
3063 if (graph->node[i].scc == wcc)
3064 n++;
3065 n_edge = 0;
3066 for (i = 0; i < graph->n_edge; ++i)
3067 if (graph->edge[i].src->scc == wcc &&
3068 graph->edge[i].dst->scc == wcc)
3069 n_edge++;
3071 if (compute_sub_schedule(ctx, graph, n, n_edge,
3072 &node_scc_exactly,
3073 &edge_scc_exactly, wcc, 1) < 0)
3074 return -1;
3075 if (graph->n_total_row > n_total_row)
3076 n_total_row = graph->n_total_row;
3077 graph->n_total_row = orig_total_row;
3078 if (graph->n_band > n_band)
3079 n_band = graph->n_band;
3080 graph->n_band = orig_band;
3083 graph->n_total_row = n_total_row;
3084 graph->n_band = n_band;
3086 return pad_schedule(graph);
3089 /* Compute a schedule for the given dependence graph.
3090 * We first check if the graph is connected (through validity dependences)
3091 * and, if not, compute a schedule for each component separately.
3092 * If schedule_fuse is set to minimal fusion, then we check for strongly
3093 * connected components instead and compute a separate schedule for
3094 * each such strongly connected component.
3096 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
3098 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN) {
3099 if (detect_sccs(ctx, graph) < 0)
3100 return -1;
3101 } else {
3102 if (detect_wccs(ctx, graph) < 0)
3103 return -1;
3106 if (graph->scc > 1)
3107 return compute_component_schedule(ctx, graph);
3109 return compute_schedule_wcc(ctx, graph);
3112 /* Compute a schedule on sc->domain that respects the given schedule
3113 * constraints.
3115 * In particular, the schedule respects all the validity dependences.
3116 * If the default isl scheduling algorithm is used, it tries to minimize
3117 * the dependence distances over the proximity dependences.
3118 * If Feautrier's scheduling algorithm is used, the proximity dependence
3119 * distances are only minimized during the extension to a full-dimensional
3120 * schedule.
3122 __isl_give isl_schedule *isl_schedule_constraints_compute_schedule(
3123 __isl_take isl_schedule_constraints *sc)
3125 isl_ctx *ctx = isl_schedule_constraints_get_ctx(sc);
3126 struct isl_sched_graph graph = { 0 };
3127 isl_schedule *sched;
3128 struct isl_extract_edge_data data;
3129 enum isl_edge_type i;
3131 sc = isl_schedule_constraints_align_params(sc);
3132 if (!sc)
3133 return NULL;
3135 graph.n = isl_union_set_n_set(sc->domain);
3136 if (graph.n == 0)
3137 goto empty;
3138 if (graph_alloc(ctx, &graph, graph.n,
3139 isl_schedule_constraints_n_map(sc)) < 0)
3140 goto error;
3141 if (compute_max_row(&graph, sc->domain) < 0)
3142 goto error;
3143 graph.root = 1;
3144 graph.n = 0;
3145 if (isl_union_set_foreach_set(sc->domain, &extract_node, &graph) < 0)
3146 goto error;
3147 if (graph_init_table(ctx, &graph) < 0)
3148 goto error;
3149 for (i = isl_edge_first; i <= isl_edge_last; ++i)
3150 graph.max_edge[i] = isl_union_map_n_map(sc->constraint[i]);
3151 if (graph_init_edge_tables(ctx, &graph) < 0)
3152 goto error;
3153 graph.n_edge = 0;
3154 data.graph = &graph;
3155 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
3156 data.type = i;
3157 if (isl_union_map_foreach_map(sc->constraint[i],
3158 &extract_edge, &data) < 0)
3159 goto error;
3162 if (compute_schedule(ctx, &graph) < 0)
3163 goto error;
3165 empty:
3166 sched = extract_schedule(&graph, isl_union_set_get_space(sc->domain));
3168 graph_free(ctx, &graph);
3169 isl_schedule_constraints_free(sc);
3171 return sched;
3172 error:
3173 graph_free(ctx, &graph);
3174 isl_schedule_constraints_free(sc);
3175 return NULL;
3178 /* Compute a schedule for the given union of domains that respects
3179 * all the validity dependences and minimizes
3180 * the dependence distances over the proximity dependences.
3182 * This function is kept for backward compatibility.
3184 __isl_give isl_schedule *isl_union_set_compute_schedule(
3185 __isl_take isl_union_set *domain,
3186 __isl_take isl_union_map *validity,
3187 __isl_take isl_union_map *proximity)
3189 isl_schedule_constraints *sc;
3191 sc = isl_schedule_constraints_on_domain(domain);
3192 sc = isl_schedule_constraints_set_validity(sc, validity);
3193 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3195 return isl_schedule_constraints_compute_schedule(sc);
3198 void *isl_schedule_free(__isl_take isl_schedule *sched)
3200 int i;
3201 if (!sched)
3202 return NULL;
3204 if (--sched->ref > 0)
3205 return NULL;
3207 for (i = 0; i < sched->n; ++i) {
3208 isl_multi_aff_free(sched->node[i].sched);
3209 free(sched->node[i].band_end);
3210 free(sched->node[i].band_id);
3211 free(sched->node[i].zero);
3213 isl_space_free(sched->dim);
3214 isl_band_list_free(sched->band_forest);
3215 free(sched);
3216 return NULL;
3219 isl_ctx *isl_schedule_get_ctx(__isl_keep isl_schedule *schedule)
3221 return schedule ? isl_space_get_ctx(schedule->dim) : NULL;
3224 /* Set max_out to the maximal number of output dimensions over
3225 * all maps.
3227 static int update_max_out(__isl_take isl_map *map, void *user)
3229 int *max_out = user;
3230 int n_out = isl_map_dim(map, isl_dim_out);
3232 if (n_out > *max_out)
3233 *max_out = n_out;
3235 isl_map_free(map);
3236 return 0;
3239 /* Internal data structure for map_pad_range.
3241 * "max_out" is the maximal schedule dimension.
3242 * "res" collects the results.
3244 struct isl_pad_schedule_map_data {
3245 int max_out;
3246 isl_union_map *res;
3249 /* Pad the range of the given map with zeros to data->max_out and
3250 * then add the result to data->res.
3252 static int map_pad_range(__isl_take isl_map *map, void *user)
3254 struct isl_pad_schedule_map_data *data = user;
3255 int i;
3256 int n_out = isl_map_dim(map, isl_dim_out);
3258 map = isl_map_add_dims(map, isl_dim_out, data->max_out - n_out);
3259 for (i = n_out; i < data->max_out; ++i)
3260 map = isl_map_fix_si(map, isl_dim_out, i, 0);
3262 data->res = isl_union_map_add_map(data->res, map);
3263 if (!data->res)
3264 return -1;
3266 return 0;
3269 /* Pad the ranges of the maps in the union map with zeros such they all have
3270 * the same dimension.
3272 static __isl_give isl_union_map *pad_schedule_map(
3273 __isl_take isl_union_map *umap)
3275 struct isl_pad_schedule_map_data data;
3277 if (!umap)
3278 return NULL;
3279 if (isl_union_map_n_map(umap) <= 1)
3280 return umap;
3282 data.max_out = 0;
3283 if (isl_union_map_foreach_map(umap, &update_max_out, &data.max_out) < 0)
3284 return isl_union_map_free(umap);
3286 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3287 if (isl_union_map_foreach_map(umap, &map_pad_range, &data) < 0)
3288 data.res = isl_union_map_free(data.res);
3290 isl_union_map_free(umap);
3291 return data.res;
3294 /* Return an isl_union_map of the schedule. If we have already constructed
3295 * a band forest, then this band forest may have been modified so we need
3296 * to extract the isl_union_map from the forest rather than from
3297 * the originally computed schedule. This reconstructed schedule map
3298 * then needs to be padded with zeros to unify the schedule space
3299 * since the result of isl_band_list_get_suffix_schedule may not have
3300 * a unified schedule space.
3302 __isl_give isl_union_map *isl_schedule_get_map(__isl_keep isl_schedule *sched)
3304 int i;
3305 isl_union_map *umap;
3307 if (!sched)
3308 return NULL;
3310 if (sched->band_forest) {
3311 umap = isl_band_list_get_suffix_schedule(sched->band_forest);
3312 return pad_schedule_map(umap);
3315 umap = isl_union_map_empty(isl_space_copy(sched->dim));
3316 for (i = 0; i < sched->n; ++i) {
3317 isl_multi_aff *ma;
3319 ma = isl_multi_aff_copy(sched->node[i].sched);
3320 umap = isl_union_map_add_map(umap, isl_map_from_multi_aff(ma));
3323 return umap;
3326 static __isl_give isl_band_list *construct_band_list(
3327 __isl_keep isl_schedule *schedule, __isl_keep isl_band *parent,
3328 int band_nr, int *parent_active, int n_active);
3330 /* Construct an isl_band structure for the band in the given schedule
3331 * with sequence number band_nr for the n_active nodes marked by active.
3332 * If the nodes don't have a band with the given sequence number,
3333 * then a band without members is created.
3335 * Because of the way the schedule is constructed, we know that
3336 * the position of the band inside the schedule of a node is the same
3337 * for all active nodes.
3339 * The partial schedule for the band is created before the children
3340 * are created to that construct_band_list can refer to the partial
3341 * schedule of the parent.
3343 static __isl_give isl_band *construct_band(__isl_keep isl_schedule *schedule,
3344 __isl_keep isl_band *parent,
3345 int band_nr, int *active, int n_active)
3347 int i, j;
3348 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
3349 isl_band *band;
3350 unsigned start, end;
3352 band = isl_band_alloc(ctx);
3353 if (!band)
3354 return NULL;
3356 band->schedule = schedule;
3357 band->parent = parent;
3359 for (i = 0; i < schedule->n; ++i)
3360 if (active[i])
3361 break;
3363 if (i >= schedule->n)
3364 isl_die(ctx, isl_error_internal,
3365 "band without active statements", goto error);
3367 start = band_nr ? schedule->node[i].band_end[band_nr - 1] : 0;
3368 end = band_nr < schedule->node[i].n_band ?
3369 schedule->node[i].band_end[band_nr] : start;
3370 band->n = end - start;
3372 band->zero = isl_alloc_array(ctx, int, band->n);
3373 if (band->n && !band->zero)
3374 goto error;
3376 for (j = 0; j < band->n; ++j)
3377 band->zero[j] = schedule->node[i].zero[start + j];
3379 band->pma = isl_union_pw_multi_aff_empty(isl_space_copy(schedule->dim));
3380 for (i = 0; i < schedule->n; ++i) {
3381 isl_multi_aff *ma;
3382 isl_pw_multi_aff *pma;
3383 unsigned n_out;
3385 if (!active[i])
3386 continue;
3388 ma = isl_multi_aff_copy(schedule->node[i].sched);
3389 n_out = isl_multi_aff_dim(ma, isl_dim_out);
3390 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, end, n_out - end);
3391 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, 0, start);
3392 pma = isl_pw_multi_aff_from_multi_aff(ma);
3393 band->pma = isl_union_pw_multi_aff_add_pw_multi_aff(band->pma,
3394 pma);
3396 if (!band->pma)
3397 goto error;
3399 for (i = 0; i < schedule->n; ++i)
3400 if (active[i] && schedule->node[i].n_band > band_nr + 1)
3401 break;
3403 if (i < schedule->n) {
3404 band->children = construct_band_list(schedule, band,
3405 band_nr + 1, active, n_active);
3406 if (!band->children)
3407 goto error;
3410 return band;
3411 error:
3412 isl_band_free(band);
3413 return NULL;
3416 /* Internal data structure used inside cmp_band and pw_multi_aff_extract_int.
3418 * r is set to a negative value if anything goes wrong.
3420 * c1 stores the result of extract_int.
3421 * c2 is a temporary value used inside cmp_band_in_ancestor.
3422 * t is a temporary value used inside extract_int.
3424 * first and equal are used inside extract_int.
3425 * first is set if we are looking at the first isl_multi_aff inside
3426 * the isl_union_pw_multi_aff.
3427 * equal is set if all the isl_multi_affs have been equal so far.
3429 struct isl_cmp_band_data {
3430 int r;
3432 int first;
3433 int equal;
3435 isl_int t;
3436 isl_int c1;
3437 isl_int c2;
3440 /* Check if "ma" assigns a constant value.
3441 * Note that this function is only called on isl_multi_affs
3442 * with a single output dimension.
3444 * If "ma" assigns a constant value then we compare it to data->c1
3445 * or assign it to data->c1 if this is the first isl_multi_aff we consider.
3446 * If "ma" does not assign a constant value or if it assigns a value
3447 * that is different from data->c1, then we set data->equal to zero
3448 * and terminate the check.
3450 static int multi_aff_extract_int(__isl_take isl_set *set,
3451 __isl_take isl_multi_aff *ma, void *user)
3453 isl_aff *aff;
3454 struct isl_cmp_band_data *data = user;
3456 aff = isl_multi_aff_get_aff(ma, 0);
3457 data->r = isl_aff_is_cst(aff);
3458 if (data->r >= 0 && data->r) {
3459 isl_aff_get_constant(aff, &data->t);
3460 if (data->first) {
3461 isl_int_set(data->c1, data->t);
3462 data->first = 0;
3463 } else if (!isl_int_eq(data->c1, data->t))
3464 data->equal = 0;
3465 } else if (data->r >= 0 && !data->r)
3466 data->equal = 0;
3468 isl_aff_free(aff);
3469 isl_set_free(set);
3470 isl_multi_aff_free(ma);
3472 if (data->r < 0)
3473 return -1;
3474 if (!data->equal)
3475 return -1;
3476 return 0;
3479 /* This function is called for each isl_pw_multi_aff in
3480 * the isl_union_pw_multi_aff checked by extract_int.
3481 * Check all the isl_multi_affs inside "pma".
3483 static int pw_multi_aff_extract_int(__isl_take isl_pw_multi_aff *pma,
3484 void *user)
3486 int r;
3488 r = isl_pw_multi_aff_foreach_piece(pma, &multi_aff_extract_int, user);
3489 isl_pw_multi_aff_free(pma);
3491 return r;
3494 /* Check if "upma" assigns a single constant value to its domain.
3495 * If so, return 1 and store the result in data->c1.
3496 * If not, return 0.
3498 * A negative return value from isl_union_pw_multi_aff_foreach_pw_multi_aff
3499 * means that either an error occurred or that we have broken off the check
3500 * because we already know the result is going to be negative.
3501 * In the latter case, data->equal is set to zero.
3503 static int extract_int(__isl_keep isl_union_pw_multi_aff *upma,
3504 struct isl_cmp_band_data *data)
3506 data->first = 1;
3507 data->equal = 1;
3509 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3510 &pw_multi_aff_extract_int, data) < 0) {
3511 if (!data->equal)
3512 return 0;
3513 return -1;
3516 return !data->first && data->equal;
3519 /* Compare "b1" and "b2" based on the parent schedule of their ancestor
3520 * "ancestor".
3522 * If the parent of "ancestor" also has a single member, then we
3523 * first try to compare the two band based on the partial schedule
3524 * of this parent.
3526 * Otherwise, or if the result is inconclusive, we look at the partial schedule
3527 * of "ancestor" itself.
3528 * In particular, we specialize the parent schedule based
3529 * on the domains of the child schedules, check if both assign
3530 * a single constant value and, if so, compare the two constant values.
3531 * If the specialized parent schedules do not assign a constant value,
3532 * then they cannot be used to order the two bands and so in this case
3533 * we return 0.
3535 static int cmp_band_in_ancestor(__isl_keep isl_band *b1,
3536 __isl_keep isl_band *b2, struct isl_cmp_band_data *data,
3537 __isl_keep isl_band *ancestor)
3539 isl_union_pw_multi_aff *upma;
3540 isl_union_set *domain;
3541 int r;
3543 if (data->r < 0)
3544 return 0;
3546 if (ancestor->parent && ancestor->parent->n == 1) {
3547 r = cmp_band_in_ancestor(b1, b2, data, ancestor->parent);
3548 if (data->r < 0)
3549 return 0;
3550 if (r)
3551 return r;
3554 upma = isl_union_pw_multi_aff_copy(b1->pma);
3555 domain = isl_union_pw_multi_aff_domain(upma);
3556 upma = isl_union_pw_multi_aff_copy(ancestor->pma);
3557 upma = isl_union_pw_multi_aff_intersect_domain(upma, domain);
3558 r = extract_int(upma, data);
3559 isl_union_pw_multi_aff_free(upma);
3561 if (r < 0)
3562 data->r = -1;
3563 if (r < 0 || !r)
3564 return 0;
3566 isl_int_set(data->c2, data->c1);
3568 upma = isl_union_pw_multi_aff_copy(b2->pma);
3569 domain = isl_union_pw_multi_aff_domain(upma);
3570 upma = isl_union_pw_multi_aff_copy(ancestor->pma);
3571 upma = isl_union_pw_multi_aff_intersect_domain(upma, domain);
3572 r = extract_int(upma, data);
3573 isl_union_pw_multi_aff_free(upma);
3575 if (r < 0)
3576 data->r = -1;
3577 if (r < 0 || !r)
3578 return 0;
3580 return isl_int_cmp(data->c2, data->c1);
3583 /* Compare "a" and "b" based on the parent schedule of their parent.
3585 static int cmp_band(const void *a, const void *b, void *user)
3587 isl_band *b1 = *(isl_band * const *) a;
3588 isl_band *b2 = *(isl_band * const *) b;
3589 struct isl_cmp_band_data *data = user;
3591 return cmp_band_in_ancestor(b1, b2, data, b1->parent);
3594 /* Sort the elements in "list" based on the partial schedules of its parent
3595 * (and ancestors). In particular if the parent assigns constant values
3596 * to the domains of the bands in "list", then the elements are sorted
3597 * according to that order.
3598 * This order should be a more "natural" order for the user, but otherwise
3599 * shouldn't have any effect.
3600 * If we would be constructing an isl_band forest directly in
3601 * isl_schedule_constraints_compute_schedule then there wouldn't be any need
3602 * for a reordering, since the children would be added to the list
3603 * in their natural order automatically.
3605 * If there is only one element in the list, then there is no need to sort
3606 * anything.
3607 * If the partial schedule of the parent has more than one member
3608 * (or if there is no parent), then it's
3609 * defnitely not assigning constant values to the different children in
3610 * the list and so we wouldn't be able to use it to sort the list.
3612 static __isl_give isl_band_list *sort_band_list(__isl_take isl_band_list *list,
3613 __isl_keep isl_band *parent)
3615 struct isl_cmp_band_data data;
3617 if (!list)
3618 return NULL;
3619 if (list->n <= 1)
3620 return list;
3621 if (!parent || parent->n != 1)
3622 return list;
3624 data.r = 0;
3625 isl_int_init(data.c1);
3626 isl_int_init(data.c2);
3627 isl_int_init(data.t);
3628 isl_sort(list->p, list->n, sizeof(list->p[0]), &cmp_band, &data);
3629 if (data.r < 0)
3630 list = isl_band_list_free(list);
3631 isl_int_clear(data.c1);
3632 isl_int_clear(data.c2);
3633 isl_int_clear(data.t);
3635 return list;
3638 /* Construct a list of bands that start at the same position (with
3639 * sequence number band_nr) in the schedules of the nodes that
3640 * were active in the parent band.
3642 * A separate isl_band structure is created for each band_id
3643 * and for each node that does not have a band with sequence
3644 * number band_nr. In the latter case, a band without members
3645 * is created.
3646 * This ensures that if a band has any children, then each node
3647 * that was active in the band is active in exactly one of the children.
3649 static __isl_give isl_band_list *construct_band_list(
3650 __isl_keep isl_schedule *schedule, __isl_keep isl_band *parent,
3651 int band_nr, int *parent_active, int n_active)
3653 int i, j;
3654 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
3655 int *active;
3656 int n_band;
3657 isl_band_list *list;
3659 n_band = 0;
3660 for (i = 0; i < n_active; ++i) {
3661 for (j = 0; j < schedule->n; ++j) {
3662 if (!parent_active[j])
3663 continue;
3664 if (schedule->node[j].n_band <= band_nr)
3665 continue;
3666 if (schedule->node[j].band_id[band_nr] == i) {
3667 n_band++;
3668 break;
3672 for (j = 0; j < schedule->n; ++j)
3673 if (schedule->node[j].n_band <= band_nr)
3674 n_band++;
3676 if (n_band == 1) {
3677 isl_band *band;
3678 list = isl_band_list_alloc(ctx, n_band);
3679 band = construct_band(schedule, parent, band_nr,
3680 parent_active, n_active);
3681 return isl_band_list_add(list, band);
3684 active = isl_alloc_array(ctx, int, schedule->n);
3685 if (schedule->n && !active)
3686 return NULL;
3688 list = isl_band_list_alloc(ctx, n_band);
3690 for (i = 0; i < n_active; ++i) {
3691 int n = 0;
3692 isl_band *band;
3694 for (j = 0; j < schedule->n; ++j) {
3695 active[j] = parent_active[j] &&
3696 schedule->node[j].n_band > band_nr &&
3697 schedule->node[j].band_id[band_nr] == i;
3698 if (active[j])
3699 n++;
3701 if (n == 0)
3702 continue;
3704 band = construct_band(schedule, parent, band_nr, active, n);
3706 list = isl_band_list_add(list, band);
3708 for (i = 0; i < schedule->n; ++i) {
3709 isl_band *band;
3710 if (!parent_active[i])
3711 continue;
3712 if (schedule->node[i].n_band > band_nr)
3713 continue;
3714 for (j = 0; j < schedule->n; ++j)
3715 active[j] = j == i;
3716 band = construct_band(schedule, parent, band_nr, active, 1);
3717 list = isl_band_list_add(list, band);
3720 free(active);
3722 list = sort_band_list(list, parent);
3724 return list;
3727 /* Construct a band forest representation of the schedule and
3728 * return the list of roots.
3730 static __isl_give isl_band_list *construct_forest(
3731 __isl_keep isl_schedule *schedule)
3733 int i;
3734 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
3735 isl_band_list *forest;
3736 int *active;
3738 active = isl_alloc_array(ctx, int, schedule->n);
3739 if (schedule->n && !active)
3740 return NULL;
3742 for (i = 0; i < schedule->n; ++i)
3743 active[i] = 1;
3745 forest = construct_band_list(schedule, NULL, 0, active, schedule->n);
3747 free(active);
3749 return forest;
3752 /* Return the roots of a band forest representation of the schedule.
3754 __isl_give isl_band_list *isl_schedule_get_band_forest(
3755 __isl_keep isl_schedule *schedule)
3757 if (!schedule)
3758 return NULL;
3759 if (!schedule->band_forest)
3760 schedule->band_forest = construct_forest(schedule);
3761 return isl_band_list_dup(schedule->band_forest);
3764 /* Call "fn" on each band in the schedule in depth-first post-order.
3766 int isl_schedule_foreach_band(__isl_keep isl_schedule *sched,
3767 int (*fn)(__isl_keep isl_band *band, void *user), void *user)
3769 int r;
3770 isl_band_list *forest;
3772 if (!sched)
3773 return -1;
3775 forest = isl_schedule_get_band_forest(sched);
3776 r = isl_band_list_foreach_band(forest, fn, user);
3777 isl_band_list_free(forest);
3779 return r;
3782 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
3783 __isl_keep isl_band_list *list);
3785 static __isl_give isl_printer *print_band(__isl_take isl_printer *p,
3786 __isl_keep isl_band *band)
3788 isl_band_list *children;
3790 p = isl_printer_start_line(p);
3791 p = isl_printer_print_union_pw_multi_aff(p, band->pma);
3792 p = isl_printer_end_line(p);
3794 if (!isl_band_has_children(band))
3795 return p;
3797 children = isl_band_get_children(band);
3799 p = isl_printer_indent(p, 4);
3800 p = print_band_list(p, children);
3801 p = isl_printer_indent(p, -4);
3803 isl_band_list_free(children);
3805 return p;
3808 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
3809 __isl_keep isl_band_list *list)
3811 int i, n;
3813 n = isl_band_list_n_band(list);
3814 for (i = 0; i < n; ++i) {
3815 isl_band *band;
3816 band = isl_band_list_get_band(list, i);
3817 p = print_band(p, band);
3818 isl_band_free(band);
3821 return p;
3824 __isl_give isl_printer *isl_printer_print_schedule(__isl_take isl_printer *p,
3825 __isl_keep isl_schedule *schedule)
3827 isl_band_list *forest;
3829 forest = isl_schedule_get_band_forest(schedule);
3831 p = print_band_list(p, forest);
3833 isl_band_list_free(forest);
3835 return p;
3838 void isl_schedule_dump(__isl_keep isl_schedule *schedule)
3840 isl_printer *printer;
3842 if (!schedule)
3843 return;
3845 printer = isl_printer_to_file(isl_schedule_get_ctx(schedule), stderr);
3846 printer = isl_printer_print_schedule(printer, schedule);
3848 isl_printer_free(printer);