2 * Copyright 2011 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 #include <isl_ctx_private.h>
12 #include <isl_map_private.h>
13 #include <isl_space_private.h>
15 #include <isl/constraint.h>
16 #include <isl/schedule.h>
17 #include <isl_mat_private.h>
21 #include <isl_dim_map.h>
22 #include <isl_hmap_map_basic_set.h>
23 #include <isl_qsort.h>
24 #include <isl_schedule_private.h>
25 #include <isl_band_private.h>
26 #include <isl_list_private.h>
27 #include <isl_options_private.h>
30 * The scheduling algorithm implemented in this file was inspired by
31 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
32 * Parallelization and Locality Optimization in the Polyhedral Model".
36 /* Internal information about a node that is used during the construction
38 * dim represents the space in which the domain lives
39 * sched is a matrix representation of the schedule being constructed
41 * sched_map is an isl_map representation of the same (partial) schedule
42 * sched_map may be NULL
43 * rank is the number of linearly independent rows in the linear part
45 * the columns of cmap represent a change of basis for the schedule
46 * coefficients; the first rank columns span the linear part of
48 * start is the first variable in the LP problem in the sequences that
49 * represents the schedule coefficients of this node
50 * nvar is the dimension of the domain
51 * nparam is the number of parameters or 0 if we are not constructing
52 * a parametric schedule
54 * scc is the index of SCC (or WCC) this node belongs to
56 * band contains the band index for each of the rows of the schedule.
57 * band_id is used to differentiate between separate bands at the same
58 * level within the same parent band, i.e., bands that are separated
59 * by the parent band or bands that are independent of each other.
60 * zero contains a boolean for each of the rows of the schedule,
61 * indicating whether the corresponding scheduling dimension results
62 * in zero dependence distances within its band and with respect
63 * to the proximity edges.
65 * index, min_index and on_stack are used during the SCC detection
66 * index represents the order in which nodes are visited.
67 * min_index is the index of the root of a (sub)component.
68 * on_stack indicates whether the node is currently on the stack.
70 struct isl_sched_node {
92 static int node_has_dim(const void *entry, const void *val)
94 struct isl_sched_node *node = (struct isl_sched_node *)entry;
95 isl_space *dim = (isl_space *)val;
97 return isl_space_is_equal(node->dim, dim);
100 /* An edge in the dependence graph. An edge may be used to
101 * ensure validity of the generated schedule, to minimize the dependence
104 * map is the dependence relation
105 * src is the source node
106 * dst is the sink node
107 * validity is set if the edge is used to ensure correctness
108 * proximity is set if the edge is used to minimize dependence distances
110 * For validity edges, start and end mark the sequence of inequality
111 * constraints in the LP problem that encode the validity constraint
112 * corresponding to this edge.
114 struct isl_sched_edge {
117 struct isl_sched_node *src;
118 struct isl_sched_node *dst;
127 /* Internal information about the dependence graph used during
128 * the construction of the schedule.
130 * intra_hmap is a cache, mapping dependence relations to their dual,
131 * for dependences from a node to itself
132 * inter_hmap is a cache, mapping dependence relations to their dual,
133 * for dependences between distinct nodes
135 * n is the number of nodes
136 * node is the list of nodes
137 * maxvar is the maximal number of variables over all nodes
138 * n_row is the current (maximal) number of linearly independent
139 * rows in the node schedules
140 * n_total_row is the current number of rows in the node schedules
141 * n_band is the current number of completed bands
142 * band_start is the starting row in the node schedules of the current band
143 * root is set if this graph is the original dependence graph,
144 * without any splitting
146 * sorted contains a list of node indices sorted according to the
147 * SCC to which a node belongs
149 * n_edge is the number of edges
150 * edge is the list of edges
151 * edge_table contains pointers into the edge array, hashed on the source
152 * and sink spaces; the table only contains edges that represent
153 * validity constraints (and that may or may not also represent proximity
156 * node_table contains pointers into the node array, hashed on the space
158 * region contains a list of variable sequences that should be non-trivial
160 * lp contains the (I)LP problem used to obtain new schedule rows
162 * src_scc and dst_scc are the source and sink SCCs of an edge with
163 * conflicting constraints
165 * scc, sp, index and stack are used during the detection of SCCs
166 * scc is the number of the next SCC
167 * stack contains the nodes on the path from the root to the current node
168 * sp is the stack pointer
169 * index is the index of the last node visited
171 struct isl_sched_graph {
172 isl_hmap_map_basic_set *intra_hmap;
173 isl_hmap_map_basic_set *inter_hmap;
175 struct isl_sched_node *node;
188 struct isl_sched_edge *edge;
190 struct isl_hash_table *edge_table;
192 struct isl_hash_table *node_table;
193 struct isl_region *region;
207 /* Initialize node_table based on the list of nodes.
209 static int graph_init_table(isl_ctx *ctx, struct isl_sched_graph *graph)
213 graph->node_table = isl_hash_table_alloc(ctx, graph->n);
214 if (!graph->node_table)
217 for (i = 0; i < graph->n; ++i) {
218 struct isl_hash_table_entry *entry;
221 hash = isl_space_get_hash(graph->node[i].dim);
222 entry = isl_hash_table_find(ctx, graph->node_table, hash,
224 graph->node[i].dim, 1);
227 entry->data = &graph->node[i];
233 /* Return a pointer to the node that lives within the given space,
234 * or NULL if there is no such node.
236 static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
237 struct isl_sched_graph *graph, __isl_keep isl_space *dim)
239 struct isl_hash_table_entry *entry;
242 hash = isl_space_get_hash(dim);
243 entry = isl_hash_table_find(ctx, graph->node_table, hash,
244 &node_has_dim, dim, 0);
246 return entry ? entry->data : NULL;
249 static int edge_has_src_and_dst(const void *entry, const void *val)
251 const struct isl_sched_edge *edge = entry;
252 const struct isl_sched_edge *temp = val;
254 return edge->src == temp->src && edge->dst == temp->dst;
257 /* Add the given edge to graph->edge_table if it is a validity edge.
259 static int graph_edge_table_add(isl_ctx *ctx, struct isl_sched_graph *graph,
260 struct isl_sched_edge *edge)
262 struct isl_hash_table_entry *entry;
268 hash = isl_hash_init();
269 hash = isl_hash_builtin(hash, edge->src);
270 hash = isl_hash_builtin(hash, edge->dst);
271 entry = isl_hash_table_find(ctx, graph->edge_table, hash,
272 &edge_has_src_and_dst, edge, 1);
280 /* Initialize edge_table based on the list of edges.
281 * Only edges with validity set are added to the table.
283 static int graph_init_edge_table(isl_ctx *ctx, struct isl_sched_graph *graph)
287 graph->edge_table = isl_hash_table_alloc(ctx, graph->n_edge);
288 if (!graph->edge_table)
291 for (i = 0; i < graph->n_edge; ++i)
292 if (graph_edge_table_add(ctx, graph, &graph->edge[i]) < 0)
298 /* Check whether the dependence graph has a (validity) edge
299 * between the given two nodes.
301 static int graph_has_edge(struct isl_sched_graph *graph,
302 struct isl_sched_node *src, struct isl_sched_node *dst)
304 isl_ctx *ctx = isl_space_get_ctx(src->dim);
305 struct isl_hash_table_entry *entry;
307 struct isl_sched_edge temp = { .src = src, .dst = dst };
308 struct isl_sched_edge *edge;
311 hash = isl_hash_init();
312 hash = isl_hash_builtin(hash, temp.src);
313 hash = isl_hash_builtin(hash, temp.dst);
314 entry = isl_hash_table_find(ctx, graph->edge_table, hash,
315 &edge_has_src_and_dst, &temp, 0);
320 empty = isl_map_plain_is_empty(edge->map);
327 static int graph_alloc(isl_ctx *ctx, struct isl_sched_graph *graph,
328 int n_node, int n_edge)
333 graph->n_edge = n_edge;
334 graph->node = isl_calloc_array(ctx, struct isl_sched_node, graph->n);
335 graph->sorted = isl_calloc_array(ctx, int, graph->n);
336 graph->region = isl_alloc_array(ctx, struct isl_region, graph->n);
337 graph->stack = isl_alloc_array(ctx, int, graph->n);
338 graph->edge = isl_calloc_array(ctx,
339 struct isl_sched_edge, graph->n_edge);
341 graph->intra_hmap = isl_hmap_map_basic_set_alloc(ctx, 2 * n_edge);
342 graph->inter_hmap = isl_hmap_map_basic_set_alloc(ctx, 2 * n_edge);
344 if (!graph->node || !graph->region || !graph->stack || !graph->edge ||
348 for(i = 0; i < graph->n; ++i)
349 graph->sorted[i] = i;
354 static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
358 isl_hmap_map_basic_set_free(ctx, graph->intra_hmap);
359 isl_hmap_map_basic_set_free(ctx, graph->inter_hmap);
361 for (i = 0; i < graph->n; ++i) {
362 isl_space_free(graph->node[i].dim);
363 isl_mat_free(graph->node[i].sched);
364 isl_map_free(graph->node[i].sched_map);
365 isl_mat_free(graph->node[i].cmap);
367 free(graph->node[i].band);
368 free(graph->node[i].band_id);
369 free(graph->node[i].zero);
374 for (i = 0; i < graph->n_edge; ++i)
375 isl_map_free(graph->edge[i].map);
379 isl_hash_table_free(ctx, graph->edge_table);
380 isl_hash_table_free(ctx, graph->node_table);
381 isl_basic_set_free(graph->lp);
384 /* Add a new node to the graph representing the given set.
386 static int extract_node(__isl_take isl_set *set, void *user)
392 struct isl_sched_graph *graph = user;
393 int *band, *band_id, *zero;
395 ctx = isl_set_get_ctx(set);
396 dim = isl_set_get_space(set);
398 nvar = isl_space_dim(dim, isl_dim_set);
399 nparam = isl_space_dim(dim, isl_dim_param);
400 if (!ctx->opt->schedule_parametric)
402 sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
403 graph->node[graph->n].dim = dim;
404 graph->node[graph->n].nvar = nvar;
405 graph->node[graph->n].nparam = nparam;
406 graph->node[graph->n].sched = sched;
407 graph->node[graph->n].sched_map = NULL;
408 band = isl_alloc_array(ctx, int, graph->n_edge + nvar);
409 graph->node[graph->n].band = band;
410 band_id = isl_calloc_array(ctx, int, graph->n_edge + nvar);
411 graph->node[graph->n].band_id = band_id;
412 zero = isl_calloc_array(ctx, int, graph->n_edge + nvar);
413 graph->node[graph->n].zero = zero;
416 if (!sched || !band || !band_id || !zero)
422 /* Add a new edge to the graph based on the given map.
423 * Edges are first extracted from the validity dependences,
424 * from which the edge_table is constructed.
425 * Afterwards, the proximity dependences are added. If a proximity
426 * dependence relation happens to be identical to one of the
427 * validity dependence relations added before, then we don't create
428 * a new edge, but instead mark the original edge as also representing
429 * a proximity dependence.
431 static int extract_edge(__isl_take isl_map *map, void *user)
433 isl_ctx *ctx = isl_map_get_ctx(map);
434 struct isl_sched_graph *graph = user;
435 struct isl_sched_node *src, *dst;
438 dim = isl_space_domain(isl_map_get_space(map));
439 src = graph_find_node(ctx, graph, dim);
441 dim = isl_space_range(isl_map_get_space(map));
442 dst = graph_find_node(ctx, graph, dim);
450 graph->edge[graph->n_edge].src = src;
451 graph->edge[graph->n_edge].dst = dst;
452 graph->edge[graph->n_edge].map = map;
453 graph->edge[graph->n_edge].validity = !graph->edge_table;
454 graph->edge[graph->n_edge].proximity = !!graph->edge_table;
457 if (graph->edge_table) {
459 struct isl_hash_table_entry *entry;
460 struct isl_sched_edge *edge;
463 hash = isl_hash_init();
464 hash = isl_hash_builtin(hash, src);
465 hash = isl_hash_builtin(hash, dst);
466 entry = isl_hash_table_find(ctx, graph->edge_table, hash,
467 &edge_has_src_and_dst,
468 &graph->edge[graph->n_edge - 1], 0);
472 is_equal = isl_map_plain_is_equal(map, edge->map);
486 /* Check whether there is a validity dependence from src to dst,
487 * forcing dst to follow src.
489 static int node_follows(struct isl_sched_graph *graph,
490 struct isl_sched_node *dst, struct isl_sched_node *src)
492 return graph_has_edge(graph, src, dst);
495 /* Perform Tarjan's algorithm for computing the strongly connected components
496 * in the dependence graph (only validity edges).
497 * If directed is not set, we consider the graph to be undirected and
498 * we effectively compute the (weakly) connected components.
500 static int detect_sccs_tarjan(struct isl_sched_graph *g, int i, int directed)
504 g->node[i].index = g->index;
505 g->node[i].min_index = g->index;
506 g->node[i].on_stack = 1;
508 g->stack[g->sp++] = i;
510 for (j = g->n - 1; j >= 0; --j) {
515 if (g->node[j].index >= 0 &&
516 (!g->node[j].on_stack ||
517 g->node[j].index > g->node[i].min_index))
520 f = node_follows(g, &g->node[i], &g->node[j]);
523 if (!f && !directed) {
524 f = node_follows(g, &g->node[j], &g->node[i]);
530 if (g->node[j].index < 0) {
531 detect_sccs_tarjan(g, j, directed);
532 if (g->node[j].min_index < g->node[i].min_index)
533 g->node[i].min_index = g->node[j].min_index;
534 } else if (g->node[j].index < g->node[i].min_index)
535 g->node[i].min_index = g->node[j].index;
538 if (g->node[i].index != g->node[i].min_index)
542 j = g->stack[--g->sp];
543 g->node[j].on_stack = 0;
544 g->node[j].scc = g->scc;
551 static int detect_ccs(struct isl_sched_graph *graph, int directed)
558 for (i = graph->n - 1; i >= 0; --i)
559 graph->node[i].index = -1;
561 for (i = graph->n - 1; i >= 0; --i) {
562 if (graph->node[i].index >= 0)
564 if (detect_sccs_tarjan(graph, i, directed) < 0)
571 /* Apply Tarjan's algorithm to detect the strongly connected components
572 * in the dependence graph.
574 static int detect_sccs(struct isl_sched_graph *graph)
576 return detect_ccs(graph, 1);
579 /* Apply Tarjan's algorithm to detect the (weakly) connected components
580 * in the dependence graph.
582 static int detect_wccs(struct isl_sched_graph *graph)
584 return detect_ccs(graph, 0);
587 static int cmp_scc(const void *a, const void *b, void *data)
589 struct isl_sched_graph *graph = data;
593 return graph->node[*i1].scc - graph->node[*i2].scc;
596 /* Sort the elements of graph->sorted according to the corresponding SCCs.
598 static void sort_sccs(struct isl_sched_graph *graph)
600 isl_quicksort(graph->sorted, graph->n, sizeof(int), &cmp_scc, graph);
603 /* Given a dependence relation R from a node to itself,
604 * construct the set of coefficients of valid constraints for elements
605 * in that dependence relation.
606 * In particular, the result contains tuples of coefficients
607 * c_0, c_n, c_x such that
609 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
613 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
615 * We choose here to compute the dual of delta R.
616 * Alternatively, we could have computed the dual of R, resulting
617 * in a set of tuples c_0, c_n, c_x, c_y, and then
618 * plugged in (c_0, c_n, c_x, -c_x).
620 static __isl_give isl_basic_set *intra_coefficients(
621 struct isl_sched_graph *graph, __isl_take isl_map *map)
623 isl_ctx *ctx = isl_map_get_ctx(map);
627 if (isl_hmap_map_basic_set_has(ctx, graph->intra_hmap, map))
628 return isl_hmap_map_basic_set_get(ctx, graph->intra_hmap, map);
630 delta = isl_set_remove_divs(isl_map_deltas(isl_map_copy(map)));
631 coef = isl_set_coefficients(delta);
632 isl_hmap_map_basic_set_set(ctx, graph->intra_hmap, map,
633 isl_basic_set_copy(coef));
638 /* Given a dependence relation R, * construct the set of coefficients
639 * of valid constraints for elements in that dependence relation.
640 * In particular, the result contains tuples of coefficients
641 * c_0, c_n, c_x, c_y such that
643 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
646 static __isl_give isl_basic_set *inter_coefficients(
647 struct isl_sched_graph *graph, __isl_take isl_map *map)
649 isl_ctx *ctx = isl_map_get_ctx(map);
653 if (isl_hmap_map_basic_set_has(ctx, graph->inter_hmap, map))
654 return isl_hmap_map_basic_set_get(ctx, graph->inter_hmap, map);
656 set = isl_map_wrap(isl_map_remove_divs(isl_map_copy(map)));
657 coef = isl_set_coefficients(set);
658 isl_hmap_map_basic_set_set(ctx, graph->inter_hmap, map,
659 isl_basic_set_copy(coef));
664 /* Add constraints to graph->lp that force validity for the given
665 * dependence from a node i to itself.
666 * That is, add constraints that enforce
668 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
669 * = c_i_x (y - x) >= 0
671 * for each (x,y) in R.
672 * We obtain general constraints on coefficients (c_0, c_n, c_x)
673 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
674 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
675 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
677 * Actually, we do not construct constraints for the c_i_x themselves,
678 * but for the coefficients of c_i_x written as a linear combination
679 * of the columns in node->cmap.
681 static int add_intra_validity_constraints(struct isl_sched_graph *graph,
682 struct isl_sched_edge *edge)
685 isl_map *map = isl_map_copy(edge->map);
686 isl_ctx *ctx = isl_map_get_ctx(map);
688 isl_dim_map *dim_map;
690 struct isl_sched_node *node = edge->src;
692 coef = intra_coefficients(graph, map);
694 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
696 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
697 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
699 total = isl_basic_set_total_dim(graph->lp);
700 dim_map = isl_dim_map_alloc(ctx, total);
701 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
702 isl_space_dim(dim, isl_dim_set), 1,
704 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
705 isl_space_dim(dim, isl_dim_set), 1,
707 graph->lp = isl_basic_set_extend_constraints(graph->lp,
708 coef->n_eq, coef->n_ineq);
709 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
716 /* Add constraints to graph->lp that force validity for the given
717 * dependence from node i to node j.
718 * That is, add constraints that enforce
720 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
722 * for each (x,y) in R.
723 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
724 * of valid constraints for R and then plug in
725 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
726 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
727 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
728 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
730 * Actually, we do not construct constraints for the c_*_x themselves,
731 * but for the coefficients of c_*_x written as a linear combination
732 * of the columns in node->cmap.
734 static int add_inter_validity_constraints(struct isl_sched_graph *graph,
735 struct isl_sched_edge *edge)
738 isl_map *map = isl_map_copy(edge->map);
739 isl_ctx *ctx = isl_map_get_ctx(map);
741 isl_dim_map *dim_map;
743 struct isl_sched_node *src = edge->src;
744 struct isl_sched_node *dst = edge->dst;
746 coef = inter_coefficients(graph, map);
748 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
750 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
751 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
752 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
753 isl_space_dim(dim, isl_dim_set) + src->nvar,
754 isl_mat_copy(dst->cmap));
756 total = isl_basic_set_total_dim(graph->lp);
757 dim_map = isl_dim_map_alloc(ctx, total);
759 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
760 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
761 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
762 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
763 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
765 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
766 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
769 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
770 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
771 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
772 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
773 isl_space_dim(dim, isl_dim_set), 1,
775 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
776 isl_space_dim(dim, isl_dim_set), 1,
779 edge->start = graph->lp->n_ineq;
780 graph->lp = isl_basic_set_extend_constraints(graph->lp,
781 coef->n_eq, coef->n_ineq);
782 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
785 edge->end = graph->lp->n_ineq;
790 /* Add constraints to graph->lp that bound the dependence distance for the given
791 * dependence from a node i to itself.
792 * If s = 1, we add the constraint
794 * c_i_x (y - x) <= m_0 + m_n n
798 * -c_i_x (y - x) + m_0 + m_n n >= 0
800 * for each (x,y) in R.
801 * If s = -1, we add the constraint
803 * -c_i_x (y - x) <= m_0 + m_n n
807 * c_i_x (y - x) + m_0 + m_n n >= 0
809 * for each (x,y) in R.
810 * We obtain general constraints on coefficients (c_0, c_n, c_x)
811 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
812 * with each coefficient (except m_0) represented as a pair of non-negative
815 * Actually, we do not construct constraints for the c_i_x themselves,
816 * but for the coefficients of c_i_x written as a linear combination
817 * of the columns in node->cmap.
819 static int add_intra_proximity_constraints(struct isl_sched_graph *graph,
820 struct isl_sched_edge *edge, int s)
824 isl_map *map = isl_map_copy(edge->map);
825 isl_ctx *ctx = isl_map_get_ctx(map);
827 isl_dim_map *dim_map;
829 struct isl_sched_node *node = edge->src;
831 coef = intra_coefficients(graph, map);
833 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
835 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
836 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
838 nparam = isl_space_dim(node->dim, isl_dim_param);
839 total = isl_basic_set_total_dim(graph->lp);
840 dim_map = isl_dim_map_alloc(ctx, total);
841 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
842 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
843 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
844 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
845 isl_space_dim(dim, isl_dim_set), 1,
847 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
848 isl_space_dim(dim, isl_dim_set), 1,
850 graph->lp = isl_basic_set_extend_constraints(graph->lp,
851 coef->n_eq, coef->n_ineq);
852 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
859 /* Add constraints to graph->lp that bound the dependence distance for the given
860 * dependence from node i to node j.
861 * If s = 1, we add the constraint
863 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
868 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
871 * for each (x,y) in R.
872 * If s = -1, we add the constraint
874 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
879 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
882 * for each (x,y) in R.
883 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
884 * of valid constraints for R and then plug in
885 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
887 * with each coefficient (except m_0, c_j_0 and c_i_0)
888 * represented as a pair of non-negative coefficients.
890 * Actually, we do not construct constraints for the c_*_x themselves,
891 * but for the coefficients of c_*_x written as a linear combination
892 * of the columns in node->cmap.
894 static int add_inter_proximity_constraints(struct isl_sched_graph *graph,
895 struct isl_sched_edge *edge, int s)
899 isl_map *map = isl_map_copy(edge->map);
900 isl_ctx *ctx = isl_map_get_ctx(map);
902 isl_dim_map *dim_map;
904 struct isl_sched_node *src = edge->src;
905 struct isl_sched_node *dst = edge->dst;
907 coef = inter_coefficients(graph, map);
909 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
911 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
912 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
913 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
914 isl_space_dim(dim, isl_dim_set) + src->nvar,
915 isl_mat_copy(dst->cmap));
917 nparam = isl_space_dim(src->dim, isl_dim_param);
918 total = isl_basic_set_total_dim(graph->lp);
919 dim_map = isl_dim_map_alloc(ctx, total);
921 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
922 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
923 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
925 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, -s);
926 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, s);
927 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, -s);
928 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
929 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
931 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
932 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
935 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, s);
936 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, -s);
937 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, s);
938 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
939 isl_space_dim(dim, isl_dim_set), 1,
941 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
942 isl_space_dim(dim, isl_dim_set), 1,
945 graph->lp = isl_basic_set_extend_constraints(graph->lp,
946 coef->n_eq, coef->n_ineq);
947 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
954 static int add_all_validity_constraints(struct isl_sched_graph *graph)
958 for (i = 0; i < graph->n_edge; ++i) {
959 struct isl_sched_edge *edge= &graph->edge[i];
962 if (edge->src != edge->dst)
964 if (add_intra_validity_constraints(graph, edge) < 0)
968 for (i = 0; i < graph->n_edge; ++i) {
969 struct isl_sched_edge *edge = &graph->edge[i];
972 if (edge->src == edge->dst)
974 if (add_inter_validity_constraints(graph, edge) < 0)
981 /* Add constraints to graph->lp that bound the dependence distance
982 * for all dependence relations.
983 * If a given proximity dependence is identical to a validity
984 * dependence, then the dependence distance is already bounded
985 * from below (by zero), so we only need to bound the distance
987 * Otherwise, we need to bound the distance both from above and from below.
989 static int add_all_proximity_constraints(struct isl_sched_graph *graph)
993 for (i = 0; i < graph->n_edge; ++i) {
994 struct isl_sched_edge *edge= &graph->edge[i];
995 if (!edge->proximity)
997 if (edge->src == edge->dst &&
998 add_intra_proximity_constraints(graph, edge, 1) < 0)
1000 if (edge->src != edge->dst &&
1001 add_inter_proximity_constraints(graph, edge, 1) < 0)
1005 if (edge->src == edge->dst &&
1006 add_intra_proximity_constraints(graph, edge, -1) < 0)
1008 if (edge->src != edge->dst &&
1009 add_inter_proximity_constraints(graph, edge, -1) < 0)
1016 /* Compute a basis for the rows in the linear part of the schedule
1017 * and extend this basis to a full basis. The remaining rows
1018 * can then be used to force linear independence from the rows
1021 * In particular, given the schedule rows S, we compute
1025 * with H the Hermite normal form of S. That is, all but the
1026 * first rank columns of Q are zero and so each row in S is
1027 * a linear combination of the first rank rows of Q.
1028 * The matrix Q is then transposed because we will write the
1029 * coefficients of the next schedule row as a column vector s
1030 * and express this s as a linear combination s = Q c of the
1033 static int node_update_cmap(struct isl_sched_node *node)
1036 int n_row = isl_mat_rows(node->sched);
1038 H = isl_mat_sub_alloc(node->sched, 0, n_row,
1039 1 + node->nparam, node->nvar);
1041 H = isl_mat_left_hermite(H, 0, NULL, &Q);
1042 isl_mat_free(node->cmap);
1043 node->cmap = isl_mat_transpose(Q);
1044 node->rank = isl_mat_initial_non_zero_cols(H);
1047 if (!node->cmap || node->rank < 0)
1052 /* Count the number of equality and inequality constraints
1053 * that will be added for the given map.
1054 * If carry is set, then we are counting the number of (validity)
1055 * constraints that will be added in setup_carry_lp and we count
1056 * each edge exactly once. Otherwise, we count as follows
1057 * validity -> 1 (>= 0)
1058 * validity+proximity -> 2 (>= 0 and upper bound)
1059 * proximity -> 2 (lower and upper bound)
1061 static int count_map_constraints(struct isl_sched_graph *graph,
1062 struct isl_sched_edge *edge, __isl_take isl_map *map,
1063 int *n_eq, int *n_ineq, int carry)
1065 isl_basic_set *coef;
1066 int f = carry ? 1 : edge->proximity ? 2 : 1;
1068 if (carry && !edge->validity) {
1073 if (edge->src == edge->dst)
1074 coef = intra_coefficients(graph, map);
1076 coef = inter_coefficients(graph, map);
1079 *n_eq += f * coef->n_eq;
1080 *n_ineq += f * coef->n_ineq;
1081 isl_basic_set_free(coef);
1086 /* Count the number of equality and inequality constraints
1087 * that will be added to the main lp problem.
1088 * We count as follows
1089 * validity -> 1 (>= 0)
1090 * validity+proximity -> 2 (>= 0 and upper bound)
1091 * proximity -> 2 (lower and upper bound)
1093 static int count_constraints(struct isl_sched_graph *graph,
1094 int *n_eq, int *n_ineq)
1098 *n_eq = *n_ineq = 0;
1099 for (i = 0; i < graph->n_edge; ++i) {
1100 struct isl_sched_edge *edge= &graph->edge[i];
1101 isl_map *map = isl_map_copy(edge->map);
1103 if (count_map_constraints(graph, edge, map,
1104 n_eq, n_ineq, 0) < 0)
1111 /* Add constraints that bound the values of the variable and parameter
1112 * coefficients of the schedule.
1114 * The maximal value of the coefficients is defined by the option
1115 * 'schedule_max_coefficient'.
1117 static int add_bound_coefficient_constraints(isl_ctx *ctx,
1118 struct isl_sched_graph *graph)
1121 int max_coefficient;
1124 max_coefficient = ctx->opt->schedule_max_coefficient;
1126 if (max_coefficient == -1)
1129 total = isl_basic_set_total_dim(graph->lp);
1131 for (i = 0; i < graph->n; ++i) {
1132 struct isl_sched_node *node = &graph->node[i];
1133 for (j = 0; j < 2 * node->nparam + 2 * node->nvar; ++j) {
1135 k = isl_basic_set_alloc_inequality(graph->lp);
1138 dim = 1 + node->start + 1 + j;
1139 isl_seq_clr(graph->lp->ineq[k], 1 + total);
1140 isl_int_set_si(graph->lp->ineq[k][dim], -1);
1141 isl_int_set_si(graph->lp->ineq[k][0], max_coefficient);
1148 /* Construct an ILP problem for finding schedule coefficients
1149 * that result in non-negative, but small dependence distances
1150 * over all dependences.
1151 * In particular, the dependence distances over proximity edges
1152 * are bounded by m_0 + m_n n and we compute schedule coefficients
1153 * with small values (preferably zero) of m_n and m_0.
1155 * All variables of the ILP are non-negative. The actual coefficients
1156 * may be negative, so each coefficient is represented as the difference
1157 * of two non-negative variables. The negative part always appears
1158 * immediately before the positive part.
1159 * Other than that, the variables have the following order
1161 * - sum of positive and negative parts of m_n coefficients
1163 * - sum of positive and negative parts of all c_n coefficients
1164 * (unconstrained when computing non-parametric schedules)
1165 * - sum of positive and negative parts of all c_x coefficients
1166 * - positive and negative parts of m_n coefficients
1169 * - positive and negative parts of c_i_n (if parametric)
1170 * - positive and negative parts of c_i_x
1172 * The c_i_x are not represented directly, but through the columns of
1173 * node->cmap. That is, the computed values are for variable t_i_x
1174 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
1176 * The constraints are those from the edges plus two or three equalities
1177 * to express the sums.
1179 * If force_zero is set, then we add equalities to ensure that
1180 * the sum of the m_n coefficients and m_0 are both zero.
1182 static int setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
1193 int max_constant_term;
1194 int max_coefficient;
1196 max_constant_term = ctx->opt->schedule_max_constant_term;
1197 max_coefficient = ctx->opt->schedule_max_coefficient;
1199 parametric = ctx->opt->schedule_parametric;
1200 nparam = isl_space_dim(graph->node[0].dim, isl_dim_param);
1202 total = param_pos + 2 * nparam;
1203 for (i = 0; i < graph->n; ++i) {
1204 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
1205 if (node_update_cmap(node) < 0)
1207 node->start = total;
1208 total += 1 + 2 * (node->nparam + node->nvar);
1211 if (count_constraints(graph, &n_eq, &n_ineq) < 0)
1214 dim = isl_space_set_alloc(ctx, 0, total);
1215 isl_basic_set_free(graph->lp);
1216 n_eq += 2 + parametric + force_zero;
1217 if (max_constant_term != -1)
1219 if (max_coefficient != -1)
1220 for (i = 0; i < graph->n; ++i)
1221 n_ineq += 2 * graph->node[i].nparam +
1222 2 * graph->node[i].nvar;
1224 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
1226 k = isl_basic_set_alloc_equality(graph->lp);
1229 isl_seq_clr(graph->lp->eq[k], 1 + total);
1231 isl_int_set_si(graph->lp->eq[k][1], -1);
1232 for (i = 0; i < 2 * nparam; ++i)
1233 isl_int_set_si(graph->lp->eq[k][1 + param_pos + i], 1);
1236 k = isl_basic_set_alloc_equality(graph->lp);
1239 isl_seq_clr(graph->lp->eq[k], 1 + total);
1240 isl_int_set_si(graph->lp->eq[k][2], -1);
1244 k = isl_basic_set_alloc_equality(graph->lp);
1247 isl_seq_clr(graph->lp->eq[k], 1 + total);
1248 isl_int_set_si(graph->lp->eq[k][3], -1);
1249 for (i = 0; i < graph->n; ++i) {
1250 int pos = 1 + graph->node[i].start + 1;
1252 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
1253 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
1257 k = isl_basic_set_alloc_equality(graph->lp);
1260 isl_seq_clr(graph->lp->eq[k], 1 + total);
1261 isl_int_set_si(graph->lp->eq[k][4], -1);
1262 for (i = 0; i < graph->n; ++i) {
1263 struct isl_sched_node *node = &graph->node[i];
1264 int pos = 1 + node->start + 1 + 2 * node->nparam;
1266 for (j = 0; j < 2 * node->nvar; ++j)
1267 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
1270 if (max_constant_term != -1)
1271 for (i = 0; i < graph->n; ++i) {
1272 struct isl_sched_node *node = &graph->node[i];
1273 k = isl_basic_set_alloc_inequality(graph->lp);
1276 isl_seq_clr(graph->lp->ineq[k], 1 + total);
1277 isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
1278 isl_int_set_si(graph->lp->ineq[k][0], max_constant_term);
1281 if (add_bound_coefficient_constraints(ctx, graph) < 0)
1283 if (add_all_validity_constraints(graph) < 0)
1285 if (add_all_proximity_constraints(graph) < 0)
1291 /* Analyze the conflicting constraint found by
1292 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
1293 * constraint of one of the edges between distinct nodes, living, moreover
1294 * in distinct SCCs, then record the source and sink SCC as this may
1295 * be a good place to cut between SCCs.
1297 static int check_conflict(int con, void *user)
1300 struct isl_sched_graph *graph = user;
1302 if (graph->src_scc >= 0)
1305 con -= graph->lp->n_eq;
1307 if (con >= graph->lp->n_ineq)
1310 for (i = 0; i < graph->n_edge; ++i) {
1311 if (!graph->edge[i].validity)
1313 if (graph->edge[i].src == graph->edge[i].dst)
1315 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
1317 if (graph->edge[i].start > con)
1319 if (graph->edge[i].end <= con)
1321 graph->src_scc = graph->edge[i].src->scc;
1322 graph->dst_scc = graph->edge[i].dst->scc;
1328 /* Check whether the next schedule row of the given node needs to be
1329 * non-trivial. Lower-dimensional domains may have some trivial rows,
1330 * but as soon as the number of remaining required non-trivial rows
1331 * is as large as the number or remaining rows to be computed,
1332 * all remaining rows need to be non-trivial.
1334 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
1336 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
1339 /* Solve the ILP problem constructed in setup_lp.
1340 * For each node such that all the remaining rows of its schedule
1341 * need to be non-trivial, we construct a non-triviality region.
1342 * This region imposes that the next row is independent of previous rows.
1343 * In particular the coefficients c_i_x are represented by t_i_x
1344 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
1345 * its first columns span the rows of the previously computed part
1346 * of the schedule. The non-triviality region enforces that at least
1347 * one of the remaining components of t_i_x is non-zero, i.e.,
1348 * that the new schedule row depends on at least one of the remaining
1351 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
1357 for (i = 0; i < graph->n; ++i) {
1358 struct isl_sched_node *node = &graph->node[i];
1359 int skip = node->rank;
1360 graph->region[i].pos = node->start + 1 + 2*(node->nparam+skip);
1361 if (needs_row(graph, node))
1362 graph->region[i].len = 2 * (node->nvar - skip);
1364 graph->region[i].len = 0;
1366 lp = isl_basic_set_copy(graph->lp);
1367 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
1368 graph->region, &check_conflict, graph);
1372 /* Update the schedules of all nodes based on the given solution
1373 * of the LP problem.
1374 * The new row is added to the current band.
1375 * All possibly negative coefficients are encoded as a difference
1376 * of two non-negative variables, so we need to perform the subtraction
1377 * here. Moreover, if use_cmap is set, then the solution does
1378 * not refer to the actual coefficients c_i_x, but instead to variables
1379 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
1380 * In this case, we then also need to perform this multiplication
1381 * to obtain the values of c_i_x.
1383 * If check_zero is set, then the first two coordinates of sol are
1384 * assumed to correspond to the dependence distance. If these two
1385 * coordinates are zero, then the corresponding scheduling dimension
1386 * is marked as being zero distance.
1388 static int update_schedule(struct isl_sched_graph *graph,
1389 __isl_take isl_vec *sol, int use_cmap, int check_zero)
1393 isl_vec *csol = NULL;
1398 isl_die(sol->ctx, isl_error_internal,
1399 "no solution found", goto error);
1402 zero = isl_int_is_zero(sol->el[1]) &&
1403 isl_int_is_zero(sol->el[2]);
1405 for (i = 0; i < graph->n; ++i) {
1406 struct isl_sched_node *node = &graph->node[i];
1407 int pos = node->start;
1408 int row = isl_mat_rows(node->sched);
1411 csol = isl_vec_alloc(sol->ctx, node->nvar);
1415 isl_map_free(node->sched_map);
1416 node->sched_map = NULL;
1417 node->sched = isl_mat_add_rows(node->sched, 1);
1420 node->sched = isl_mat_set_element(node->sched, row, 0,
1422 for (j = 0; j < node->nparam + node->nvar; ++j)
1423 isl_int_sub(sol->el[1 + pos + 1 + 2 * j + 1],
1424 sol->el[1 + pos + 1 + 2 * j + 1],
1425 sol->el[1 + pos + 1 + 2 * j]);
1426 for (j = 0; j < node->nparam; ++j)
1427 node->sched = isl_mat_set_element(node->sched,
1428 row, 1 + j, sol->el[1+pos+1+2*j+1]);
1429 for (j = 0; j < node->nvar; ++j)
1430 isl_int_set(csol->el[j],
1431 sol->el[1+pos+1+2*(node->nparam+j)+1]);
1433 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
1437 for (j = 0; j < node->nvar; ++j)
1438 node->sched = isl_mat_set_element(node->sched,
1439 row, 1 + node->nparam + j, csol->el[j]);
1440 node->band[graph->n_total_row] = graph->n_band;
1441 node->zero[graph->n_total_row] = zero;
1447 graph->n_total_row++;
1456 /* Convert node->sched into a map and return this map.
1457 * We simply add equality constraints that express each output variable
1458 * as the affine combination of parameters and input variables specified
1459 * by the schedule matrix.
1461 * The result is cached in node->sched_map, which needs to be released
1462 * whenever node->sched is updated.
1464 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
1468 isl_local_space *ls;
1469 isl_basic_map *bmap;
1474 if (node->sched_map)
1475 return isl_map_copy(node->sched_map);
1477 nrow = isl_mat_rows(node->sched);
1478 ncol = isl_mat_cols(node->sched) - 1;
1479 dim = isl_space_from_domain(isl_space_copy(node->dim));
1480 dim = isl_space_add_dims(dim, isl_dim_out, nrow);
1481 bmap = isl_basic_map_universe(isl_space_copy(dim));
1482 ls = isl_local_space_from_space(dim);
1486 for (i = 0; i < nrow; ++i) {
1487 c = isl_equality_alloc(isl_local_space_copy(ls));
1488 isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
1489 isl_mat_get_element(node->sched, i, 0, &v);
1490 isl_constraint_set_constant(c, v);
1491 for (j = 0; j < node->nparam; ++j) {
1492 isl_mat_get_element(node->sched, i, 1 + j, &v);
1493 isl_constraint_set_coefficient(c, isl_dim_param, j, v);
1495 for (j = 0; j < node->nvar; ++j) {
1496 isl_mat_get_element(node->sched,
1497 i, 1 + node->nparam + j, &v);
1498 isl_constraint_set_coefficient(c, isl_dim_in, j, v);
1500 bmap = isl_basic_map_add_constraint(bmap, c);
1505 isl_local_space_free(ls);
1507 node->sched_map = isl_map_from_basic_map(bmap);
1508 return isl_map_copy(node->sched_map);
1511 /* Update the given dependence relation based on the current schedule.
1512 * That is, intersect the dependence relation with a map expressing
1513 * that source and sink are executed within the same iteration of
1514 * the current schedule.
1515 * This is not the most efficient way, but this shouldn't be a critical
1518 static __isl_give isl_map *specialize(__isl_take isl_map *map,
1519 struct isl_sched_node *src, struct isl_sched_node *dst)
1521 isl_map *src_sched, *dst_sched, *id;
1523 src_sched = node_extract_schedule(src);
1524 dst_sched = node_extract_schedule(dst);
1525 id = isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
1526 return isl_map_intersect(map, id);
1529 /* Update the dependence relations of all edges based on the current schedule.
1530 * If a dependence is carried completely by the current schedule, then
1531 * it is removed and edge_table is updated accordingly.
1533 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
1536 int reset_table = 0;
1538 for (i = graph->n_edge - 1; i >= 0; --i) {
1539 struct isl_sched_edge *edge = &graph->edge[i];
1540 edge->map = specialize(edge->map, edge->src, edge->dst);
1544 if (isl_map_plain_is_empty(edge->map)) {
1546 isl_map_free(edge->map);
1547 if (i != graph->n_edge - 1)
1548 graph->edge[i] = graph->edge[graph->n_edge - 1];
1554 isl_hash_table_free(ctx, graph->edge_table);
1555 graph->edge_table = NULL;
1556 return graph_init_edge_table(ctx, graph);
1562 static void next_band(struct isl_sched_graph *graph)
1564 graph->band_start = graph->n_total_row;
1568 /* Topologically sort statements mapped to the same schedule iteration
1569 * and add a row to the schedule corresponding to this order.
1571 static int sort_statements(isl_ctx *ctx, struct isl_sched_graph *graph)
1578 if (update_edges(ctx, graph) < 0)
1581 if (graph->n_edge == 0)
1584 if (detect_sccs(graph) < 0)
1587 for (i = 0; i < graph->n; ++i) {
1588 struct isl_sched_node *node = &graph->node[i];
1589 int row = isl_mat_rows(node->sched);
1590 int cols = isl_mat_cols(node->sched);
1592 isl_map_free(node->sched_map);
1593 node->sched_map = NULL;
1594 node->sched = isl_mat_add_rows(node->sched, 1);
1597 node->sched = isl_mat_set_element_si(node->sched, row, 0,
1599 for (j = 1; j < cols; ++j)
1600 node->sched = isl_mat_set_element_si(node->sched,
1602 node->band[graph->n_total_row] = graph->n_band;
1605 graph->n_total_row++;
1611 /* Construct an isl_schedule based on the computed schedule stored
1612 * in graph and with parameters specified by dim.
1614 static __isl_give isl_schedule *extract_schedule(struct isl_sched_graph *graph,
1615 __isl_take isl_space *dim)
1619 isl_schedule *sched = NULL;
1624 ctx = isl_space_get_ctx(dim);
1625 sched = isl_calloc(ctx, struct isl_schedule,
1626 sizeof(struct isl_schedule) +
1627 (graph->n - 1) * sizeof(struct isl_schedule_node));
1632 sched->n = graph->n;
1633 sched->n_band = graph->n_band;
1634 sched->n_total_row = graph->n_total_row;
1636 for (i = 0; i < sched->n; ++i) {
1638 int *band_end, *band_id, *zero;
1640 band_end = isl_alloc_array(ctx, int, graph->n_band);
1641 band_id = isl_alloc_array(ctx, int, graph->n_band);
1642 zero = isl_alloc_array(ctx, int, graph->n_total_row);
1643 sched->node[i].sched = node_extract_schedule(&graph->node[i]);
1644 sched->node[i].band_end = band_end;
1645 sched->node[i].band_id = band_id;
1646 sched->node[i].zero = zero;
1647 if (!band_end || !band_id || !zero)
1650 for (r = 0; r < graph->n_total_row; ++r)
1651 zero[r] = graph->node[i].zero[r];
1652 for (r = b = 0; r < graph->n_total_row; ++r) {
1653 if (graph->node[i].band[r] == b)
1656 if (graph->node[i].band[r] == -1)
1659 if (r == graph->n_total_row)
1661 sched->node[i].n_band = b;
1662 for (--b; b >= 0; --b)
1663 band_id[b] = graph->node[i].band_id[b];
1670 isl_space_free(dim);
1671 isl_schedule_free(sched);
1675 /* Copy nodes that satisfy node_pred from the src dependence graph
1676 * to the dst dependence graph.
1678 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
1679 int (*node_pred)(struct isl_sched_node *node, int data), int data)
1684 for (i = 0; i < src->n; ++i) {
1685 if (!node_pred(&src->node[i], data))
1687 dst->node[dst->n].dim = isl_space_copy(src->node[i].dim);
1688 dst->node[dst->n].nvar = src->node[i].nvar;
1689 dst->node[dst->n].nparam = src->node[i].nparam;
1690 dst->node[dst->n].sched = isl_mat_copy(src->node[i].sched);
1691 dst->node[dst->n].sched_map =
1692 isl_map_copy(src->node[i].sched_map);
1693 dst->node[dst->n].band = src->node[i].band;
1694 dst->node[dst->n].band_id = src->node[i].band_id;
1695 dst->node[dst->n].zero = src->node[i].zero;
1702 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
1703 * to the dst dependence graph.
1704 * If the source or destination node of the edge is not in the destination
1705 * graph, then it must be a backward proximity edge and it should simply
1708 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
1709 struct isl_sched_graph *src,
1710 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
1715 for (i = 0; i < src->n_edge; ++i) {
1716 struct isl_sched_edge *edge = &src->edge[i];
1718 struct isl_sched_node *dst_src, *dst_dst;
1720 if (!edge_pred(edge, data))
1723 if (isl_map_plain_is_empty(edge->map))
1726 dst_src = graph_find_node(ctx, dst, edge->src->dim);
1727 dst_dst = graph_find_node(ctx, dst, edge->dst->dim);
1728 if (!dst_src || !dst_dst) {
1730 isl_die(ctx, isl_error_internal,
1731 "backward validity edge", return -1);
1735 map = isl_map_copy(edge->map);
1737 dst->edge[dst->n_edge].src = dst_src;
1738 dst->edge[dst->n_edge].dst = dst_dst;
1739 dst->edge[dst->n_edge].map = map;
1740 dst->edge[dst->n_edge].validity = edge->validity;
1741 dst->edge[dst->n_edge].proximity = edge->proximity;
1748 /* Given a "src" dependence graph that contains the nodes from "dst"
1749 * that satisfy node_pred, copy the schedule computed in "src"
1750 * for those nodes back to "dst".
1752 static int copy_schedule(struct isl_sched_graph *dst,
1753 struct isl_sched_graph *src,
1754 int (*node_pred)(struct isl_sched_node *node, int data), int data)
1759 for (i = 0; i < dst->n; ++i) {
1760 if (!node_pred(&dst->node[i], data))
1762 isl_mat_free(dst->node[i].sched);
1763 isl_map_free(dst->node[i].sched_map);
1764 dst->node[i].sched = isl_mat_copy(src->node[src->n].sched);
1765 dst->node[i].sched_map =
1766 isl_map_copy(src->node[src->n].sched_map);
1770 dst->n_total_row = src->n_total_row;
1771 dst->n_band = src->n_band;
1776 /* Compute the maximal number of variables over all nodes.
1777 * This is the maximal number of linearly independent schedule
1778 * rows that we need to compute.
1779 * Just in case we end up in a part of the dependence graph
1780 * with only lower-dimensional domains, we make sure we will
1781 * compute the required amount of extra linearly independent rows.
1783 static int compute_maxvar(struct isl_sched_graph *graph)
1788 for (i = 0; i < graph->n; ++i) {
1789 struct isl_sched_node *node = &graph->node[i];
1792 if (node_update_cmap(node) < 0)
1794 nvar = node->nvar + graph->n_row - node->rank;
1795 if (nvar > graph->maxvar)
1796 graph->maxvar = nvar;
1802 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph);
1803 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph);
1805 /* Compute a schedule for a subgraph of "graph". In particular, for
1806 * the graph composed of nodes that satisfy node_pred and edges that
1807 * that satisfy edge_pred. The caller should precompute the number
1808 * of nodes and edges that satisfy these predicates and pass them along
1809 * as "n" and "n_edge".
1810 * If the subgraph is known to consist of a single component, then wcc should
1811 * be set and then we call compute_schedule_wcc on the constructed subgraph.
1812 * Otherwise, we call compute_schedule, which will check whether the subgraph
1815 static int compute_sub_schedule(isl_ctx *ctx,
1816 struct isl_sched_graph *graph, int n, int n_edge,
1817 int (*node_pred)(struct isl_sched_node *node, int data),
1818 int (*edge_pred)(struct isl_sched_edge *edge, int data),
1821 struct isl_sched_graph split = { 0 };
1823 if (graph_alloc(ctx, &split, n, n_edge) < 0)
1825 if (copy_nodes(&split, graph, node_pred, data) < 0)
1827 if (graph_init_table(ctx, &split) < 0)
1829 if (copy_edges(ctx, &split, graph, edge_pred, data) < 0)
1831 if (graph_init_edge_table(ctx, &split) < 0)
1833 split.n_row = graph->n_row;
1834 split.n_total_row = graph->n_total_row;
1835 split.n_band = graph->n_band;
1836 split.band_start = graph->band_start;
1838 if (wcc && compute_schedule_wcc(ctx, &split) < 0)
1840 if (!wcc && compute_schedule(ctx, &split) < 0)
1843 copy_schedule(graph, &split, node_pred, data);
1845 graph_free(ctx, &split);
1848 graph_free(ctx, &split);
1852 static int node_scc_exactly(struct isl_sched_node *node, int scc)
1854 return node->scc == scc;
1857 static int node_scc_at_most(struct isl_sched_node *node, int scc)
1859 return node->scc <= scc;
1862 static int node_scc_at_least(struct isl_sched_node *node, int scc)
1864 return node->scc >= scc;
1867 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
1869 return edge->src->scc == scc && edge->dst->scc == scc;
1872 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
1874 return edge->dst->scc <= scc;
1877 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
1879 return edge->src->scc >= scc;
1882 /* Pad the schedules of all nodes with zero rows such that in the end
1883 * they all have graph->n_total_row rows.
1884 * The extra rows don't belong to any band, so they get assigned band number -1.
1886 static int pad_schedule(struct isl_sched_graph *graph)
1890 for (i = 0; i < graph->n; ++i) {
1891 struct isl_sched_node *node = &graph->node[i];
1892 int row = isl_mat_rows(node->sched);
1893 if (graph->n_total_row > row) {
1894 isl_map_free(node->sched_map);
1895 node->sched_map = NULL;
1897 node->sched = isl_mat_add_zero_rows(node->sched,
1898 graph->n_total_row - row);
1901 for (j = row; j < graph->n_total_row; ++j)
1908 /* Split the current graph into two parts and compute a schedule for each
1909 * part individually. In particular, one part consists of all SCCs up
1910 * to and including graph->src_scc, while the other part contains the other
1913 * The split is enforced in the schedule by constant rows with two different
1914 * values (0 and 1). These constant rows replace the previously computed rows
1915 * in the current band.
1916 * It would be possible to reuse them as the first rows in the next
1917 * band, but recomputing them may result in better rows as we are looking
1918 * at a smaller part of the dependence graph.
1919 * compute_split_schedule is only called when no zero-distance schedule row
1920 * could be found on the entire graph, so we wark the splitting row as
1921 * non zero-distance.
1923 * The band_id of the second group is set to n, where n is the number
1924 * of nodes in the first group. This ensures that the band_ids over
1925 * the two groups remain disjoint, even if either or both of the two
1926 * groups contain independent components.
1928 static int compute_split_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
1930 int i, j, n, e1, e2;
1931 int n_total_row, orig_total_row;
1932 int n_band, orig_band;
1935 drop = graph->n_total_row - graph->band_start;
1936 graph->n_total_row -= drop;
1937 graph->n_row -= drop;
1940 for (i = 0; i < graph->n; ++i) {
1941 struct isl_sched_node *node = &graph->node[i];
1942 int row = isl_mat_rows(node->sched) - drop;
1943 int cols = isl_mat_cols(node->sched);
1944 int before = node->scc <= graph->src_scc;
1949 isl_map_free(node->sched_map);
1950 node->sched_map = NULL;
1951 node->sched = isl_mat_drop_rows(node->sched,
1952 graph->band_start, drop);
1953 node->sched = isl_mat_add_rows(node->sched, 1);
1956 node->sched = isl_mat_set_element_si(node->sched, row, 0,
1958 for (j = 1; j < cols; ++j)
1959 node->sched = isl_mat_set_element_si(node->sched,
1961 node->band[graph->n_total_row] = graph->n_band;
1962 node->zero[graph->n_total_row] = 0;
1966 for (i = 0; i < graph->n_edge; ++i) {
1967 if (graph->edge[i].dst->scc <= graph->src_scc)
1969 if (graph->edge[i].src->scc > graph->src_scc)
1973 graph->n_total_row++;
1976 for (i = 0; i < graph->n; ++i) {
1977 struct isl_sched_node *node = &graph->node[i];
1978 if (node->scc > graph->src_scc)
1979 node->band_id[graph->n_band] = n;
1982 orig_total_row = graph->n_total_row;
1983 orig_band = graph->n_band;
1984 if (compute_sub_schedule(ctx, graph, n, e1,
1985 &node_scc_at_most, &edge_dst_scc_at_most,
1986 graph->src_scc, 0) < 0)
1988 n_total_row = graph->n_total_row;
1989 graph->n_total_row = orig_total_row;
1990 n_band = graph->n_band;
1991 graph->n_band = orig_band;
1992 if (compute_sub_schedule(ctx, graph, graph->n - n, e2,
1993 &node_scc_at_least, &edge_src_scc_at_least,
1994 graph->src_scc + 1, 0) < 0)
1996 if (n_total_row > graph->n_total_row)
1997 graph->n_total_row = n_total_row;
1998 if (n_band > graph->n_band)
1999 graph->n_band = n_band;
2001 return pad_schedule(graph);
2004 /* Compute the next band of the schedule after updating the dependence
2005 * relations based on the the current schedule.
2007 static int compute_next_band(isl_ctx *ctx, struct isl_sched_graph *graph)
2009 if (update_edges(ctx, graph) < 0)
2013 return compute_schedule(ctx, graph);
2016 /* Add constraints to graph->lp that force the dependence "map" (which
2017 * is part of the dependence relation of "edge")
2018 * to be respected and attempt to carry it, where the edge is one from
2019 * a node j to itself. "pos" is the sequence number of the given map.
2020 * That is, add constraints that enforce
2022 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
2023 * = c_j_x (y - x) >= e_i
2025 * for each (x,y) in R.
2026 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2027 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
2028 * with each coefficient in c_j_x represented as a pair of non-negative
2031 static int add_intra_constraints(struct isl_sched_graph *graph,
2032 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
2035 isl_ctx *ctx = isl_map_get_ctx(map);
2037 isl_dim_map *dim_map;
2038 isl_basic_set *coef;
2039 struct isl_sched_node *node = edge->src;
2041 coef = intra_coefficients(graph, map);
2043 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
2045 total = isl_basic_set_total_dim(graph->lp);
2046 dim_map = isl_dim_map_alloc(ctx, total);
2047 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
2048 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
2049 isl_space_dim(dim, isl_dim_set), 1,
2051 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
2052 isl_space_dim(dim, isl_dim_set), 1,
2054 graph->lp = isl_basic_set_extend_constraints(graph->lp,
2055 coef->n_eq, coef->n_ineq);
2056 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
2058 isl_space_free(dim);
2063 /* Add constraints to graph->lp that force the dependence "map" (which
2064 * is part of the dependence relation of "edge")
2065 * to be respected and attempt to carry it, where the edge is one from
2066 * node j to node k. "pos" is the sequence number of the given map.
2067 * That is, add constraints that enforce
2069 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
2071 * for each (x,y) in R.
2072 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2073 * of valid constraints for R and then plug in
2074 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
2075 * with each coefficient (except e_i, c_k_0 and c_j_0)
2076 * represented as a pair of non-negative coefficients.
2078 static int add_inter_constraints(struct isl_sched_graph *graph,
2079 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
2082 isl_ctx *ctx = isl_map_get_ctx(map);
2084 isl_dim_map *dim_map;
2085 isl_basic_set *coef;
2086 struct isl_sched_node *src = edge->src;
2087 struct isl_sched_node *dst = edge->dst;
2089 coef = inter_coefficients(graph, map);
2091 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
2093 total = isl_basic_set_total_dim(graph->lp);
2094 dim_map = isl_dim_map_alloc(ctx, total);
2096 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
2098 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
2099 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
2100 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
2101 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
2102 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
2104 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
2105 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
2108 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
2109 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
2110 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
2111 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
2112 isl_space_dim(dim, isl_dim_set), 1,
2114 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
2115 isl_space_dim(dim, isl_dim_set), 1,
2118 graph->lp = isl_basic_set_extend_constraints(graph->lp,
2119 coef->n_eq, coef->n_ineq);
2120 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
2122 isl_space_free(dim);
2127 /* Add constraints to graph->lp that force all validity dependences
2128 * to be respected and attempt to carry them.
2130 static int add_all_constraints(struct isl_sched_graph *graph)
2136 for (i = 0; i < graph->n_edge; ++i) {
2137 struct isl_sched_edge *edge= &graph->edge[i];
2139 if (!edge->validity)
2142 for (j = 0; j < edge->map->n; ++j) {
2143 isl_basic_map *bmap;
2146 bmap = isl_basic_map_copy(edge->map->p[j]);
2147 map = isl_map_from_basic_map(bmap);
2149 if (edge->src == edge->dst &&
2150 add_intra_constraints(graph, edge, map, pos) < 0)
2152 if (edge->src != edge->dst &&
2153 add_inter_constraints(graph, edge, map, pos) < 0)
2162 /* Count the number of equality and inequality constraints
2163 * that will be added to the carry_lp problem.
2164 * We count each edge exactly once.
2166 static int count_all_constraints(struct isl_sched_graph *graph,
2167 int *n_eq, int *n_ineq)
2171 *n_eq = *n_ineq = 0;
2172 for (i = 0; i < graph->n_edge; ++i) {
2173 struct isl_sched_edge *edge= &graph->edge[i];
2174 for (j = 0; j < edge->map->n; ++j) {
2175 isl_basic_map *bmap;
2178 bmap = isl_basic_map_copy(edge->map->p[j]);
2179 map = isl_map_from_basic_map(bmap);
2181 if (count_map_constraints(graph, edge, map,
2182 n_eq, n_ineq, 1) < 0)
2190 /* Construct an LP problem for finding schedule coefficients
2191 * such that the schedule carries as many dependences as possible.
2192 * In particular, for each dependence i, we bound the dependence distance
2193 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
2194 * of all e_i's. Dependence with e_i = 0 in the solution are simply
2195 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
2196 * Note that if the dependence relation is a union of basic maps,
2197 * then we have to consider each basic map individually as it may only
2198 * be possible to carry the dependences expressed by some of those
2199 * basic maps and not all off them.
2200 * Below, we consider each of those basic maps as a separate "edge".
2202 * All variables of the LP are non-negative. The actual coefficients
2203 * may be negative, so each coefficient is represented as the difference
2204 * of two non-negative variables. The negative part always appears
2205 * immediately before the positive part.
2206 * Other than that, the variables have the following order
2208 * - sum of (1 - e_i) over all edges
2209 * - sum of positive and negative parts of all c_n coefficients
2210 * (unconstrained when computing non-parametric schedules)
2211 * - sum of positive and negative parts of all c_x coefficients
2216 * - positive and negative parts of c_i_n (if parametric)
2217 * - positive and negative parts of c_i_x
2219 * The constraints are those from the (validity) edges plus three equalities
2220 * to express the sums and n_edge inequalities to express e_i <= 1.
2222 static int setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
2232 for (i = 0; i < graph->n_edge; ++i)
2233 n_edge += graph->edge[i].map->n;
2236 for (i = 0; i < graph->n; ++i) {
2237 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2238 node->start = total;
2239 total += 1 + 2 * (node->nparam + node->nvar);
2242 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
2245 dim = isl_space_set_alloc(ctx, 0, total);
2246 isl_basic_set_free(graph->lp);
2249 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
2250 graph->lp = isl_basic_set_set_rational(graph->lp);
2252 k = isl_basic_set_alloc_equality(graph->lp);
2255 isl_seq_clr(graph->lp->eq[k], 1 + total);
2256 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
2257 isl_int_set_si(graph->lp->eq[k][1], 1);
2258 for (i = 0; i < n_edge; ++i)
2259 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
2261 k = isl_basic_set_alloc_equality(graph->lp);
2264 isl_seq_clr(graph->lp->eq[k], 1 + total);
2265 isl_int_set_si(graph->lp->eq[k][2], -1);
2266 for (i = 0; i < graph->n; ++i) {
2267 int pos = 1 + graph->node[i].start + 1;
2269 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
2270 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2273 k = isl_basic_set_alloc_equality(graph->lp);
2276 isl_seq_clr(graph->lp->eq[k], 1 + total);
2277 isl_int_set_si(graph->lp->eq[k][3], -1);
2278 for (i = 0; i < graph->n; ++i) {
2279 struct isl_sched_node *node = &graph->node[i];
2280 int pos = 1 + node->start + 1 + 2 * node->nparam;
2282 for (j = 0; j < 2 * node->nvar; ++j)
2283 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2286 for (i = 0; i < n_edge; ++i) {
2287 k = isl_basic_set_alloc_inequality(graph->lp);
2290 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2291 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
2292 isl_int_set_si(graph->lp->ineq[k][0], 1);
2295 if (add_all_constraints(graph) < 0)
2301 /* If the schedule_split_scaled option is set and if the linear
2302 * parts of the scheduling rows for all nodes in the graphs have
2303 * non-trivial common divisor, then split off the constant term
2304 * from the linear part.
2305 * The constant term is then placed in a separate band and
2306 * the linear part is reduced.
2308 static int split_scaled(isl_ctx *ctx, struct isl_sched_graph *graph)
2314 if (!ctx->opt->schedule_split_scaled)
2320 isl_int_init(gcd_i);
2322 isl_int_set_si(gcd, 0);
2324 row = isl_mat_rows(graph->node[0].sched) - 1;
2326 for (i = 0; i < graph->n; ++i) {
2327 struct isl_sched_node *node = &graph->node[i];
2328 int cols = isl_mat_cols(node->sched);
2330 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
2331 isl_int_gcd(gcd, gcd, gcd_i);
2334 isl_int_clear(gcd_i);
2336 if (isl_int_cmp_si(gcd, 1) <= 0) {
2343 for (i = 0; i < graph->n; ++i) {
2344 struct isl_sched_node *node = &graph->node[i];
2346 isl_map_free(node->sched_map);
2347 node->sched_map = NULL;
2348 node->sched = isl_mat_add_zero_rows(node->sched, 1);
2351 isl_int_fdiv_r(node->sched->row[row + 1][0],
2352 node->sched->row[row][0], gcd);
2353 isl_int_fdiv_q(node->sched->row[row][0],
2354 node->sched->row[row][0], gcd);
2355 isl_int_mul(node->sched->row[row][0],
2356 node->sched->row[row][0], gcd);
2357 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
2360 node->band[graph->n_total_row] = graph->n_band;
2363 graph->n_total_row++;
2372 /* Construct a schedule row for each node such that as many dependences
2373 * as possible are carried and then continue with the next band.
2375 static int carry_dependences(isl_ctx *ctx, struct isl_sched_graph *graph)
2383 for (i = 0; i < graph->n_edge; ++i)
2384 n_edge += graph->edge[i].map->n;
2386 if (setup_carry_lp(ctx, graph) < 0)
2389 lp = isl_basic_set_copy(graph->lp);
2390 sol = isl_tab_basic_set_non_neg_lexmin(lp);
2394 if (sol->size == 0) {
2396 isl_die(ctx, isl_error_internal,
2397 "error in schedule construction", return -1);
2400 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
2402 isl_die(ctx, isl_error_unknown,
2403 "unable to carry dependences", return -1);
2406 if (update_schedule(graph, sol, 0, 0) < 0)
2409 if (split_scaled(ctx, graph) < 0)
2412 return compute_next_band(ctx, graph);
2415 /* Are there any validity edges in the graph?
2417 static int has_validity_edges(struct isl_sched_graph *graph)
2421 for (i = 0; i < graph->n_edge; ++i)
2422 if (graph->edge[i].validity)
2428 /* Should we apply a Feautrier step?
2429 * That is, did the user request the Feautrier algorithm and are
2430 * there any validity dependences (left)?
2432 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
2434 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
2437 return has_validity_edges(graph);
2440 /* Compute a schedule for a connected dependence graph using Feautrier's
2441 * multi-dimensional scheduling algorithm.
2442 * The original algorithm is described in [1].
2443 * The main idea is to minimize the number of scheduling dimensions, by
2444 * trying to satisfy as many dependences as possible per scheduling dimension.
2446 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
2447 * Problem, Part II: Multi-Dimensional Time.
2448 * In Intl. Journal of Parallel Programming, 1992.
2450 static int compute_schedule_wcc_feautrier(isl_ctx *ctx,
2451 struct isl_sched_graph *graph)
2453 return carry_dependences(ctx, graph);
2456 /* Compute a schedule for a connected dependence graph.
2457 * We try to find a sequence of as many schedule rows as possible that result
2458 * in non-negative dependence distances (independent of the previous rows
2459 * in the sequence, i.e., such that the sequence is tilable).
2460 * If we can't find any more rows we either
2461 * - split between SCCs and start over (assuming we found an interesting
2462 * pair of SCCs between which to split)
2463 * - continue with the next band (assuming the current band has at least
2465 * - try to carry as many dependences as possible and continue with the next
2468 * If Feautrier's algorithm is selected, we first recursively try to satisfy
2469 * as many validity dependences as possible. When all validity dependences
2470 * are satisfied we extend the schedule to a full-dimensional schedule.
2472 * If we manage to complete the schedule, we finish off by topologically
2473 * sorting the statements based on the remaining dependences.
2475 * If ctx->opt->schedule_outer_zero_distance is set, then we force the
2476 * outermost dimension in the current band to be zero distance. If this
2477 * turns out to be impossible, we fall back on the general scheme above
2478 * and try to carry as many dependences as possible.
2480 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph)
2484 if (detect_sccs(graph) < 0)
2488 if (compute_maxvar(graph) < 0)
2491 if (need_feautrier_step(ctx, graph))
2492 return compute_schedule_wcc_feautrier(ctx, graph);
2494 if (ctx->opt->schedule_outer_zero_distance)
2497 while (graph->n_row < graph->maxvar) {
2500 graph->src_scc = -1;
2501 graph->dst_scc = -1;
2503 if (setup_lp(ctx, graph, force_zero) < 0)
2505 sol = solve_lp(graph);
2508 if (sol->size == 0) {
2510 if (!ctx->opt->schedule_maximize_band_depth &&
2511 graph->n_total_row > graph->band_start)
2512 return compute_next_band(ctx, graph);
2513 if (graph->src_scc >= 0)
2514 return compute_split_schedule(ctx, graph);
2515 if (graph->n_total_row > graph->band_start)
2516 return compute_next_band(ctx, graph);
2517 return carry_dependences(ctx, graph);
2519 if (update_schedule(graph, sol, 1, 1) < 0)
2524 if (graph->n_total_row > graph->band_start)
2526 return sort_statements(ctx, graph);
2529 /* Add a row to the schedules that separates the SCCs and move
2532 static int split_on_scc(struct isl_sched_graph *graph)
2536 for (i = 0; i < graph->n; ++i) {
2537 struct isl_sched_node *node = &graph->node[i];
2538 int row = isl_mat_rows(node->sched);
2540 isl_map_free(node->sched_map);
2541 node->sched_map = NULL;
2542 node->sched = isl_mat_add_zero_rows(node->sched, 1);
2543 node->sched = isl_mat_set_element_si(node->sched, row, 0,
2547 node->band[graph->n_total_row] = graph->n_band;
2550 graph->n_total_row++;
2556 /* Compute a schedule for each component (identified by node->scc)
2557 * of the dependence graph separately and then combine the results.
2558 * Depending on the setting of schedule_fuse, a component may be
2559 * either weakly or strongly connected.
2561 * The band_id is adjusted such that each component has a separate id.
2562 * Note that the band_id may have already been set to a value different
2563 * from zero by compute_split_schedule.
2565 static int compute_component_schedule(isl_ctx *ctx,
2566 struct isl_sched_graph *graph)
2570 int n_total_row, orig_total_row;
2571 int n_band, orig_band;
2573 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN)
2574 split_on_scc(graph);
2577 orig_total_row = graph->n_total_row;
2579 orig_band = graph->n_band;
2580 for (i = 0; i < graph->n; ++i)
2581 graph->node[i].band_id[graph->n_band] += graph->node[i].scc;
2582 for (wcc = 0; wcc < graph->scc; ++wcc) {
2584 for (i = 0; i < graph->n; ++i)
2585 if (graph->node[i].scc == wcc)
2588 for (i = 0; i < graph->n_edge; ++i)
2589 if (graph->edge[i].src->scc == wcc &&
2590 graph->edge[i].dst->scc == wcc)
2593 if (compute_sub_schedule(ctx, graph, n, n_edge,
2595 &edge_scc_exactly, wcc, 1) < 0)
2597 if (graph->n_total_row > n_total_row)
2598 n_total_row = graph->n_total_row;
2599 graph->n_total_row = orig_total_row;
2600 if (graph->n_band > n_band)
2601 n_band = graph->n_band;
2602 graph->n_band = orig_band;
2605 graph->n_total_row = n_total_row;
2606 graph->n_band = n_band;
2608 return pad_schedule(graph);
2611 /* Compute a schedule for the given dependence graph.
2612 * We first check if the graph is connected (through validity dependences)
2613 * and, if not, compute a schedule for each component separately.
2614 * If schedule_fuse is set to minimal fusion, then we check for strongly
2615 * connected components instead and compute a separate schedule for
2616 * each such strongly connected component.
2618 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
2620 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN) {
2621 if (detect_sccs(graph) < 0)
2624 if (detect_wccs(graph) < 0)
2629 return compute_component_schedule(ctx, graph);
2631 return compute_schedule_wcc(ctx, graph);
2634 /* Compute a schedule for the given union of domains that respects
2635 * all the validity dependences.
2636 * If the default isl scheduling algorithm is used, it tries to minimize
2637 * the dependence distances over the proximity dependences.
2638 * If Feautrier's scheduling algorithm is used, the proximity dependence
2639 * distances are only minimized during the extension to a full-dimensional
2642 __isl_give isl_schedule *isl_union_set_compute_schedule(
2643 __isl_take isl_union_set *domain,
2644 __isl_take isl_union_map *validity,
2645 __isl_take isl_union_map *proximity)
2647 isl_ctx *ctx = isl_union_set_get_ctx(domain);
2649 struct isl_sched_graph graph = { 0 };
2650 isl_schedule *sched;
2652 domain = isl_union_set_align_params(domain,
2653 isl_union_map_get_space(validity));
2654 domain = isl_union_set_align_params(domain,
2655 isl_union_map_get_space(proximity));
2656 dim = isl_union_set_get_space(domain);
2657 validity = isl_union_map_align_params(validity, isl_space_copy(dim));
2658 proximity = isl_union_map_align_params(proximity, dim);
2663 graph.n = isl_union_set_n_set(domain);
2666 if (graph_alloc(ctx, &graph, graph.n,
2667 isl_union_map_n_map(validity) + isl_union_map_n_map(proximity)) < 0)
2671 if (isl_union_set_foreach_set(domain, &extract_node, &graph) < 0)
2673 if (graph_init_table(ctx, &graph) < 0)
2676 if (isl_union_map_foreach_map(validity, &extract_edge, &graph) < 0)
2678 if (graph_init_edge_table(ctx, &graph) < 0)
2680 if (isl_union_map_foreach_map(proximity, &extract_edge, &graph) < 0)
2683 if (compute_schedule(ctx, &graph) < 0)
2687 sched = extract_schedule(&graph, isl_union_set_get_space(domain));
2689 graph_free(ctx, &graph);
2690 isl_union_set_free(domain);
2691 isl_union_map_free(validity);
2692 isl_union_map_free(proximity);
2696 graph_free(ctx, &graph);
2697 isl_union_set_free(domain);
2698 isl_union_map_free(validity);
2699 isl_union_map_free(proximity);
2703 void *isl_schedule_free(__isl_take isl_schedule *sched)
2709 if (--sched->ref > 0)
2712 for (i = 0; i < sched->n; ++i) {
2713 isl_map_free(sched->node[i].sched);
2714 free(sched->node[i].band_end);
2715 free(sched->node[i].band_id);
2716 free(sched->node[i].zero);
2718 isl_space_free(sched->dim);
2719 isl_band_list_free(sched->band_forest);
2724 isl_ctx *isl_schedule_get_ctx(__isl_keep isl_schedule *schedule)
2726 return schedule ? isl_space_get_ctx(schedule->dim) : NULL;
2729 __isl_give isl_union_map *isl_schedule_get_map(__isl_keep isl_schedule *sched)
2732 isl_union_map *umap;
2737 umap = isl_union_map_empty(isl_space_copy(sched->dim));
2738 for (i = 0; i < sched->n; ++i)
2739 umap = isl_union_map_add_map(umap,
2740 isl_map_copy(sched->node[i].sched));
2745 static __isl_give isl_band_list *construct_band_list(
2746 __isl_keep isl_schedule *schedule, __isl_keep isl_band *parent,
2747 int band_nr, int *parent_active, int n_active);
2749 /* Construct an isl_band structure for the band in the given schedule
2750 * with sequence number band_nr for the n_active nodes marked by active.
2751 * If the nodes don't have a band with the given sequence number,
2752 * then a band without members is created.
2754 * Because of the way the schedule is constructed, we know that
2755 * the position of the band inside the schedule of a node is the same
2756 * for all active nodes.
2758 static __isl_give isl_band *construct_band(__isl_keep isl_schedule *schedule,
2759 __isl_keep isl_band *parent,
2760 int band_nr, int *active, int n_active)
2763 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
2765 unsigned start, end;
2767 band = isl_calloc_type(ctx, isl_band);
2772 band->schedule = schedule;
2773 band->parent = parent;
2775 for (i = 0; i < schedule->n; ++i)
2776 if (active[i] && schedule->node[i].n_band > band_nr + 1)
2779 if (i < schedule->n) {
2780 band->children = construct_band_list(schedule, band,
2781 band_nr + 1, active, n_active);
2782 if (!band->children)
2786 for (i = 0; i < schedule->n; ++i)
2790 if (i >= schedule->n)
2791 isl_die(ctx, isl_error_internal,
2792 "band without active statements", goto error);
2794 start = band_nr ? schedule->node[i].band_end[band_nr - 1] : 0;
2795 end = band_nr < schedule->node[i].n_band ?
2796 schedule->node[i].band_end[band_nr] : start;
2797 band->n = end - start;
2799 band->zero = isl_alloc_array(ctx, int, band->n);
2803 for (j = 0; j < band->n; ++j)
2804 band->zero[j] = schedule->node[i].zero[start + j];
2806 band->map = isl_union_map_empty(isl_space_copy(schedule->dim));
2807 for (i = 0; i < schedule->n; ++i) {
2814 map = isl_map_copy(schedule->node[i].sched);
2815 n_out = isl_map_dim(map, isl_dim_out);
2816 map = isl_map_project_out(map, isl_dim_out, end, n_out - end);
2817 map = isl_map_project_out(map, isl_dim_out, 0, start);
2818 band->map = isl_union_map_union(band->map,
2819 isl_union_map_from_map(map));
2826 isl_band_free(band);
2830 /* Construct a list of bands that start at the same position (with
2831 * sequence number band_nr) in the schedules of the nodes that
2832 * were active in the parent band.
2834 * A separate isl_band structure is created for each band_id
2835 * and for each node that does not have a band with sequence
2836 * number band_nr. In the latter case, a band without members
2838 * This ensures that if a band has any children, then each node
2839 * that was active in the band is active in exactly one of the children.
2841 static __isl_give isl_band_list *construct_band_list(
2842 __isl_keep isl_schedule *schedule, __isl_keep isl_band *parent,
2843 int band_nr, int *parent_active, int n_active)
2846 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
2849 isl_band_list *list;
2852 for (i = 0; i < n_active; ++i) {
2853 for (j = 0; j < schedule->n; ++j) {
2854 if (!parent_active[j])
2856 if (schedule->node[j].n_band <= band_nr)
2858 if (schedule->node[j].band_id[band_nr] == i) {
2864 for (j = 0; j < schedule->n; ++j)
2865 if (schedule->node[j].n_band <= band_nr)
2870 list = isl_band_list_alloc(ctx, n_band);
2871 band = construct_band(schedule, parent, band_nr,
2872 parent_active, n_active);
2873 return isl_band_list_add(list, band);
2876 active = isl_alloc_array(ctx, int, schedule->n);
2880 list = isl_band_list_alloc(ctx, n_band);
2882 for (i = 0; i < n_active; ++i) {
2886 for (j = 0; j < schedule->n; ++j) {
2887 active[j] = parent_active[j] &&
2888 schedule->node[j].n_band > band_nr &&
2889 schedule->node[j].band_id[band_nr] == i;
2896 band = construct_band(schedule, parent, band_nr, active, n);
2898 list = isl_band_list_add(list, band);
2900 for (i = 0; i < schedule->n; ++i) {
2902 if (!parent_active[i])
2904 if (schedule->node[i].n_band > band_nr)
2906 for (j = 0; j < schedule->n; ++j)
2908 band = construct_band(schedule, parent, band_nr, active, 1);
2909 list = isl_band_list_add(list, band);
2917 /* Construct a band forest representation of the schedule and
2918 * return the list of roots.
2920 static __isl_give isl_band_list *construct_forest(
2921 __isl_keep isl_schedule *schedule)
2924 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
2925 isl_band_list *forest;
2928 active = isl_alloc_array(ctx, int, schedule->n);
2932 for (i = 0; i < schedule->n; ++i)
2935 forest = construct_band_list(schedule, NULL, 0, active, schedule->n);
2942 /* Return the roots of a band forest representation of the schedule.
2944 __isl_give isl_band_list *isl_schedule_get_band_forest(
2945 __isl_keep isl_schedule *schedule)
2949 if (!schedule->band_forest)
2950 schedule->band_forest = construct_forest(schedule);
2951 return isl_band_list_dup(schedule->band_forest);
2954 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
2955 __isl_keep isl_band_list *list);
2957 static __isl_give isl_printer *print_band(__isl_take isl_printer *p,
2958 __isl_keep isl_band *band)
2960 isl_band_list *children;
2962 p = isl_printer_start_line(p);
2963 p = isl_printer_print_union_map(p, band->map);
2964 p = isl_printer_end_line(p);
2966 if (!isl_band_has_children(band))
2969 children = isl_band_get_children(band);
2971 p = isl_printer_indent(p, 4);
2972 p = print_band_list(p, children);
2973 p = isl_printer_indent(p, -4);
2975 isl_band_list_free(children);
2980 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
2981 __isl_keep isl_band_list *list)
2985 n = isl_band_list_n_band(list);
2986 for (i = 0; i < n; ++i) {
2988 band = isl_band_list_get_band(list, i);
2989 p = print_band(p, band);
2990 isl_band_free(band);
2996 __isl_give isl_printer *isl_printer_print_schedule(__isl_take isl_printer *p,
2997 __isl_keep isl_schedule *schedule)
2999 isl_band_list *forest;
3001 forest = isl_schedule_get_band_forest(schedule);
3003 p = print_band_list(p, forest);
3005 isl_band_list_free(forest);
3010 void isl_schedule_dump(__isl_keep isl_schedule *schedule)
3012 isl_printer *printer;
3017 printer = isl_printer_to_file(isl_schedule_get_ctx(schedule), stderr);
3018 printer = isl_printer_print_schedule(printer, schedule);
3020 isl_printer_free(printer);