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_dim_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>
27 * The scheduling algorithm implemented in this file was inspired by
28 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
29 * Parallelization and Locality Optimization in the Polyhedral Model".
33 /* Internal information about a node that is used during the construction
35 * dim represents the space in which the domain lives
36 * sched is a matrix representation of the schedule being constructed
38 * sched_map is an isl_map representation of the same (partial) schedule
39 * sched_map may be NULL
40 * rank is the number of linearly independent rows in the linear part
42 * the columns of cmap represent a change of basis for the schedule
43 * coefficients; the first rank columns span the linear part of
45 * start is the first variable in the LP problem in the sequences that
46 * represents the schedule coefficients of this node
47 * nvar is the dimension of the domain
48 * nparam is the number of parameters or 0 if we are not constructing
49 * a parametric schedule
51 * scc is the index of SCC (or WCC) this node belongs to
53 * band contains the band index for each of the rows of the schedule
55 * index, min_index and on_stack are used during the SCC detection
56 * index represents the order in which nodes are visited.
57 * min_index is the index of the root of a (sub)component.
58 * on_stack indicates whether the node is currently on the stack.
60 struct isl_sched_node {
80 static int node_has_dim(const void *entry, const void *val)
82 struct isl_sched_node *node = (struct isl_sched_node *)entry;
83 isl_dim *dim = (isl_dim *)val;
85 return isl_dim_equal(node->dim, dim);
88 /* An edge in the dependence graph. An edge may be used to
89 * ensure validity of the generated schedule, to minimize the dependence
92 * map is the dependence relation
93 * src is the source node
94 * dst is the sink node
95 * validity is set if the edge is used to ensure correctness
96 * proximity is set if the edge is used to minimize dependence distances
98 * For validity edges, start and end mark the sequence of inequality
99 * constraints in the LP problem that encode the validity constraint
100 * corresponding to this edge.
102 struct isl_sched_edge {
105 struct isl_sched_node *src;
106 struct isl_sched_node *dst;
115 /* Internal information about the dependence graph used during
116 * the construction of the schedule.
118 * intra_hmap is a cache, mapping dependence relations to their dual,
119 * for dependences from a node to itself
120 * inter_hmap is a cache, mapping dependence relations to their dual,
121 * for dependences between distinct nodes
123 * n is the number of nodes
124 * node is the list of nodes
125 * maxvar is the maximal number of variables over all nodes
126 * n_row is the current (maximal) number of linearly independent
127 * rows in the node schedules
128 * n_total_row is the current number of rows in the node schedules
129 * n_band is the current number of completed bands
130 * band_start is the starting row in the node schedules of the current band
131 * root is set if this graph is the original dependence graph,
132 * without any splitting
134 * sorted contains a list of node indices sorted according to the
135 * SCC to which a node belongs
137 * n_edge is the number of edges
138 * edge is the list of edges
139 * edge_table contains pointers into the edge array, hashed on the source
140 * and sink spaces; the table only contains edges that represent
141 * validity constraints (and that may or may not also represent proximity
144 * node_table contains pointers into the node array, hashed on the space
146 * region contains a list of variable sequences that should be non-trivial
148 * lp contains the (I)LP problem used to obtain new schedule rows
150 * src_scc and dst_scc are the source and sink SCCs of an edge with
151 * conflicting constraints
153 * scc, sp, index and stack are used during the detection of SCCs
154 * scc is the number of the next SCC
155 * stack contains the nodes on the path from the root to the current node
156 * sp is the stack pointer
157 * index is the index of the last node visited
159 struct isl_sched_graph {
160 isl_hmap_map_basic_set *intra_hmap;
161 isl_hmap_map_basic_set *inter_hmap;
163 struct isl_sched_node *node;
176 struct isl_sched_edge *edge;
178 struct isl_hash_table *edge_table;
180 struct isl_hash_table *node_table;
181 struct isl_region *region;
195 /* Initialize node_table based on the list of nodes.
197 static int graph_init_table(isl_ctx *ctx, struct isl_sched_graph *graph)
201 graph->node_table = isl_hash_table_alloc(ctx, graph->n);
202 if (!graph->node_table)
205 for (i = 0; i < graph->n; ++i) {
206 struct isl_hash_table_entry *entry;
209 hash = isl_dim_get_hash(graph->node[i].dim);
210 entry = isl_hash_table_find(ctx, graph->node_table, hash,
212 graph->node[i].dim, 1);
215 entry->data = &graph->node[i];
221 /* Return a pointer to the node that lives within the given space,
222 * or NULL if there is no such node.
224 static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
225 struct isl_sched_graph *graph, __isl_keep isl_dim *dim)
227 struct isl_hash_table_entry *entry;
230 hash = isl_dim_get_hash(dim);
231 entry = isl_hash_table_find(ctx, graph->node_table, hash,
232 &node_has_dim, dim, 0);
234 return entry ? entry->data : NULL;
237 static int edge_has_src_and_dst(const void *entry, const void *val)
239 const struct isl_sched_edge *edge = entry;
240 const struct isl_sched_edge *temp = val;
242 return edge->src == temp->src && edge->dst == temp->dst;
245 /* Initialize edge_table based on the list of edges.
246 * Only edges with validity set are added to the table.
248 static int graph_init_edge_table(isl_ctx *ctx, struct isl_sched_graph *graph)
252 graph->edge_table = isl_hash_table_alloc(ctx, graph->n_edge);
253 if (!graph->edge_table)
256 for (i = 0; i < graph->n_edge; ++i) {
257 struct isl_hash_table_entry *entry;
260 if (!graph->edge[i].validity)
263 hash = isl_hash_init();
264 hash = isl_hash_builtin(hash, graph->edge[i].src);
265 hash = isl_hash_builtin(hash, graph->edge[i].dst);
266 entry = isl_hash_table_find(ctx, graph->edge_table, hash,
267 &edge_has_src_and_dst,
271 entry->data = &graph->edge[i];
277 /* Check whether the dependence graph has a (validity) edge
278 * between the given two nodes.
280 static int graph_has_edge(struct isl_sched_graph *graph,
281 struct isl_sched_node *src, struct isl_sched_node *dst)
283 isl_ctx *ctx = isl_dim_get_ctx(src->dim);
284 struct isl_hash_table_entry *entry;
286 struct isl_sched_edge temp = { .src = src, .dst = dst };
287 struct isl_sched_edge *edge;
290 hash = isl_hash_init();
291 hash = isl_hash_builtin(hash, temp.src);
292 hash = isl_hash_builtin(hash, temp.dst);
293 entry = isl_hash_table_find(ctx, graph->edge_table, hash,
294 &edge_has_src_and_dst, &temp, 0);
299 empty = isl_map_plain_is_empty(edge->map);
306 static int graph_alloc(isl_ctx *ctx, struct isl_sched_graph *graph,
307 int n_node, int n_edge)
312 graph->n_edge = n_edge;
313 graph->node = isl_calloc_array(ctx, struct isl_sched_node, graph->n);
314 graph->sorted = isl_calloc_array(ctx, int, graph->n);
315 graph->region = isl_alloc_array(ctx, struct isl_region, graph->n);
316 graph->stack = isl_alloc_array(ctx, int, graph->n);
317 graph->edge = isl_calloc_array(ctx,
318 struct isl_sched_edge, graph->n_edge);
320 graph->intra_hmap = isl_hmap_map_basic_set_alloc(ctx, 2 * n_edge);
321 graph->inter_hmap = isl_hmap_map_basic_set_alloc(ctx, 2 * n_edge);
323 if (!graph->node || !graph->region || !graph->stack || !graph->edge ||
327 for(i = 0; i < graph->n; ++i)
328 graph->sorted[i] = i;
333 static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
337 isl_hmap_map_basic_set_free(ctx, graph->intra_hmap);
338 isl_hmap_map_basic_set_free(ctx, graph->inter_hmap);
340 for (i = 0; i < graph->n; ++i) {
341 isl_dim_free(graph->node[i].dim);
342 isl_mat_free(graph->node[i].sched);
343 isl_map_free(graph->node[i].sched_map);
344 isl_mat_free(graph->node[i].cmap);
346 free(graph->node[i].band);
350 for (i = 0; i < graph->n_edge; ++i)
351 isl_map_free(graph->edge[i].map);
355 isl_hash_table_free(ctx, graph->edge_table);
356 isl_hash_table_free(ctx, graph->node_table);
357 isl_basic_set_free(graph->lp);
360 /* Add a new node to the graph representing the given set.
362 static int extract_node(__isl_take isl_set *set, void *user)
368 struct isl_sched_graph *graph = user;
371 ctx = isl_set_get_ctx(set);
372 dim = isl_set_get_dim(set);
374 nvar = isl_dim_size(dim, isl_dim_set);
375 nparam = isl_dim_size(dim, isl_dim_param);
376 if (!ctx->opt->schedule_parametric)
378 sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
379 graph->node[graph->n].dim = dim;
380 graph->node[graph->n].nvar = nvar;
381 graph->node[graph->n].nparam = nparam;
382 graph->node[graph->n].sched = sched;
383 graph->node[graph->n].sched_map = NULL;
384 band = isl_alloc_array(ctx, int, graph->n_edge + nvar);
385 graph->node[graph->n].band = band;
394 /* Add a new edge to the graph based on the given map.
395 * Edges are first extracted from the validity dependences,
396 * from which the edge_table is constructed.
397 * Afterwards, the proximity dependences are added. If a proximity
398 * dependence relation happens to be identical to one of the
399 * validity dependence relations added before, then we don't create
400 * a new edge, but instead mark the original edge as also representing
401 * a proximity dependence.
403 static int extract_edge(__isl_take isl_map *map, void *user)
405 isl_ctx *ctx = isl_map_get_ctx(map);
406 struct isl_sched_graph *graph = user;
407 struct isl_sched_node *src, *dst;
410 dim = isl_dim_domain(isl_map_get_dim(map));
411 src = graph_find_node(ctx, graph, dim);
413 dim = isl_dim_range(isl_map_get_dim(map));
414 dst = graph_find_node(ctx, graph, dim);
422 graph->edge[graph->n_edge].src = src;
423 graph->edge[graph->n_edge].dst = dst;
424 graph->edge[graph->n_edge].map = map;
425 graph->edge[graph->n_edge].validity = !graph->edge_table;
426 graph->edge[graph->n_edge].proximity = !!graph->edge_table;
429 if (graph->edge_table) {
431 struct isl_hash_table_entry *entry;
432 struct isl_sched_edge *edge;
435 hash = isl_hash_init();
436 hash = isl_hash_builtin(hash, src);
437 hash = isl_hash_builtin(hash, dst);
438 entry = isl_hash_table_find(ctx, graph->edge_table, hash,
439 &edge_has_src_and_dst,
440 &graph->edge[graph->n_edge - 1], 0);
444 is_equal = isl_map_plain_is_equal(map, edge->map);
458 /* Check whether there is a validity dependence from src to dst,
459 * forcing dst to follow src.
461 static int node_follows(struct isl_sched_graph *graph,
462 struct isl_sched_node *dst, struct isl_sched_node *src)
464 return graph_has_edge(graph, src, dst);
467 /* Perform Tarjan's algorithm for computing the strongly connected components
468 * in the dependence graph (only validity edges).
469 * If directed is not set, we consider the graph to be undirected and
470 * we effectively compute the (weakly) connected components.
472 static int detect_sccs_tarjan(struct isl_sched_graph *g, int i, int directed)
476 g->node[i].index = g->index;
477 g->node[i].min_index = g->index;
478 g->node[i].on_stack = 1;
480 g->stack[g->sp++] = i;
482 for (j = g->n - 1; j >= 0; --j) {
487 if (g->node[j].index >= 0 &&
488 (!g->node[j].on_stack ||
489 g->node[j].index > g->node[i].min_index))
492 f = node_follows(g, &g->node[i], &g->node[j]);
495 if (!f && !directed) {
496 f = node_follows(g, &g->node[j], &g->node[i]);
502 if (g->node[j].index < 0) {
503 detect_sccs_tarjan(g, j, directed);
504 if (g->node[j].min_index < g->node[i].min_index)
505 g->node[i].min_index = g->node[j].min_index;
506 } else if (g->node[j].index < g->node[i].min_index)
507 g->node[i].min_index = g->node[j].index;
510 if (g->node[i].index != g->node[i].min_index)
514 j = g->stack[--g->sp];
515 g->node[j].on_stack = 0;
516 g->node[j].scc = g->scc;
523 static int detect_ccs(struct isl_sched_graph *graph, int directed)
530 for (i = graph->n - 1; i >= 0; --i)
531 graph->node[i].index = -1;
533 for (i = graph->n - 1; i >= 0; --i) {
534 if (graph->node[i].index >= 0)
536 if (detect_sccs_tarjan(graph, i, directed) < 0)
543 /* Apply Tarjan's algorithm to detect the strongly connected components
544 * in the dependence graph.
546 static int detect_sccs(struct isl_sched_graph *graph)
548 return detect_ccs(graph, 1);
551 /* Apply Tarjan's algorithm to detect the (weakly) connected components
552 * in the dependence graph.
554 static int detect_wccs(struct isl_sched_graph *graph)
556 return detect_ccs(graph, 0);
559 static int cmp_scc(const void *a, const void *b, void *data)
561 struct isl_sched_graph *graph = data;
565 return graph->node[*i1].scc - graph->node[*i2].scc;
568 /* Sort the elements of graph->sorted according to the corresponding SCCs.
570 static void sort_sccs(struct isl_sched_graph *graph)
572 isl_quicksort(graph->sorted, graph->n, sizeof(int), &cmp_scc, graph);
575 /* Given a dependence relation R from a node to itself,
576 * construct the set of coefficients of valid constraints for elements
577 * in that dependence relation.
578 * In particular, the result contains tuples of coefficients
579 * c_0, c_n, c_x such that
581 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
585 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
587 * We choose here to compute the dual of delta R.
588 * Alternatively, we could have computed the dual of R, resulting
589 * in a set of tuples c_0, c_n, c_x, c_y, and then
590 * plugged in (c_0, c_n, c_x, -c_x).
592 static __isl_give isl_basic_set *intra_coefficients(
593 struct isl_sched_graph *graph, __isl_take isl_map *map)
595 isl_ctx *ctx = isl_map_get_ctx(map);
599 if (isl_hmap_map_basic_set_has(ctx, graph->intra_hmap, map))
600 return isl_hmap_map_basic_set_get(ctx, graph->intra_hmap, map);
602 delta = isl_set_remove_divs(isl_map_deltas(isl_map_copy(map)));
603 coef = isl_set_coefficients(delta);
604 isl_hmap_map_basic_set_set(ctx, graph->intra_hmap, map,
605 isl_basic_set_copy(coef));
610 /* Given a dependence relation R, * construct the set of coefficients
611 * of valid constraints for elements in that dependence relation.
612 * In particular, the result contains tuples of coefficients
613 * c_0, c_n, c_x, c_y such that
615 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
618 static __isl_give isl_basic_set *inter_coefficients(
619 struct isl_sched_graph *graph, __isl_take isl_map *map)
621 isl_ctx *ctx = isl_map_get_ctx(map);
625 if (isl_hmap_map_basic_set_has(ctx, graph->inter_hmap, map))
626 return isl_hmap_map_basic_set_get(ctx, graph->inter_hmap, map);
628 set = isl_map_wrap(isl_map_remove_divs(isl_map_copy(map)));
629 coef = isl_set_coefficients(set);
630 isl_hmap_map_basic_set_set(ctx, graph->inter_hmap, map,
631 isl_basic_set_copy(coef));
636 /* Add constraints to graph->lp that force validity for the given
637 * dependence from a node i to itself.
638 * That is, add constraints that enforce
640 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
641 * = c_i_x (y - x) >= 0
643 * for each (x,y) in R.
644 * We obtain general constraints on coefficients (c_0, c_n, c_x)
645 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
646 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
647 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
649 * Actually, we do not construct constraints for the c_i_x themselves,
650 * but for the coefficients of c_i_x written as a linear combination
651 * of the columns in node->cmap.
653 static int add_intra_validity_constraints(struct isl_sched_graph *graph,
654 struct isl_sched_edge *edge)
657 isl_map *map = isl_map_copy(edge->map);
658 isl_ctx *ctx = isl_map_get_ctx(map);
660 isl_dim_map *dim_map;
662 struct isl_sched_node *node = edge->src;
664 coef = intra_coefficients(graph, map);
666 dim = isl_dim_domain(isl_dim_unwrap(isl_basic_set_get_dim(coef)));
668 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
669 isl_dim_size(dim, isl_dim_set), isl_mat_copy(node->cmap));
671 total = isl_basic_set_total_dim(graph->lp);
672 dim_map = isl_dim_map_alloc(ctx, total);
673 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
674 isl_dim_size(dim, isl_dim_set), 1,
676 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
677 isl_dim_size(dim, isl_dim_set), 1,
679 graph->lp = isl_basic_set_extend_constraints(graph->lp,
680 coef->n_eq, coef->n_ineq);
681 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
688 /* Add constraints to graph->lp that force validity for the given
689 * dependence from node i to node j.
690 * That is, add constraints that enforce
692 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
694 * for each (x,y) in R.
695 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
696 * of valid constraints for R and then plug in
697 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
698 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
699 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
700 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
702 * Actually, we do not construct constraints for the c_*_x themselves,
703 * but for the coefficients of c_*_x written as a linear combination
704 * of the columns in node->cmap.
706 static int add_inter_validity_constraints(struct isl_sched_graph *graph,
707 struct isl_sched_edge *edge)
710 isl_map *map = isl_map_copy(edge->map);
711 isl_ctx *ctx = isl_map_get_ctx(map);
713 isl_dim_map *dim_map;
715 struct isl_sched_node *src = edge->src;
716 struct isl_sched_node *dst = edge->dst;
718 coef = inter_coefficients(graph, map);
720 dim = isl_dim_domain(isl_dim_unwrap(isl_basic_set_get_dim(coef)));
722 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
723 isl_dim_size(dim, isl_dim_set), isl_mat_copy(src->cmap));
724 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
725 isl_dim_size(dim, isl_dim_set) + src->nvar,
726 isl_mat_copy(dst->cmap));
728 total = isl_basic_set_total_dim(graph->lp);
729 dim_map = isl_dim_map_alloc(ctx, total);
731 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
732 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
733 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
734 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
735 isl_dim_size(dim, isl_dim_set) + src->nvar, 1,
737 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
738 isl_dim_size(dim, isl_dim_set) + src->nvar, 1,
741 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
742 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
743 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
744 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
745 isl_dim_size(dim, isl_dim_set), 1,
747 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
748 isl_dim_size(dim, isl_dim_set), 1,
751 edge->start = graph->lp->n_ineq;
752 graph->lp = isl_basic_set_extend_constraints(graph->lp,
753 coef->n_eq, coef->n_ineq);
754 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
757 edge->end = graph->lp->n_ineq;
762 /* Add constraints to graph->lp that bound the dependence distance for the given
763 * dependence from a node i to itself.
764 * If s = 1, we add the constraint
766 * c_i_x (y - x) <= m_0 + m_n n
770 * -c_i_x (y - x) + m_0 + m_n n >= 0
772 * for each (x,y) in R.
773 * If s = -1, we add the constraint
775 * -c_i_x (y - x) <= m_0 + m_n n
779 * c_i_x (y - x) + m_0 + m_n n >= 0
781 * for each (x,y) in R.
782 * We obtain general constraints on coefficients (c_0, c_n, c_x)
783 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
784 * with each coefficient (except m_0) represented as a pair of non-negative
787 * Actually, we do not construct constraints for the c_i_x themselves,
788 * but for the coefficients of c_i_x written as a linear combination
789 * of the columns in node->cmap.
791 static int add_intra_proximity_constraints(struct isl_sched_graph *graph,
792 struct isl_sched_edge *edge, int s)
796 isl_map *map = isl_map_copy(edge->map);
797 isl_ctx *ctx = isl_map_get_ctx(map);
799 isl_dim_map *dim_map;
801 struct isl_sched_node *node = edge->src;
803 coef = intra_coefficients(graph, map);
805 dim = isl_dim_domain(isl_dim_unwrap(isl_basic_set_get_dim(coef)));
807 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
808 isl_dim_size(dim, isl_dim_set), isl_mat_copy(node->cmap));
810 nparam = isl_dim_size(node->dim, isl_dim_param);
811 total = isl_basic_set_total_dim(graph->lp);
812 dim_map = isl_dim_map_alloc(ctx, total);
813 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
814 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
815 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
816 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
817 isl_dim_size(dim, isl_dim_set), 1,
819 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
820 isl_dim_size(dim, isl_dim_set), 1,
822 graph->lp = isl_basic_set_extend_constraints(graph->lp,
823 coef->n_eq, coef->n_ineq);
824 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
831 /* Add constraints to graph->lp that bound the dependence distance for the given
832 * dependence from node i to node j.
833 * If s = 1, we add the constraint
835 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
840 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
843 * for each (x,y) in R.
844 * If s = -1, we add the constraint
846 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
851 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
854 * for each (x,y) in R.
855 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
856 * of valid constraints for R and then plug in
857 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
859 * with each coefficient (except m_0, c_j_0 and c_i_0)
860 * represented as a pair of non-negative coefficients.
862 * Actually, we do not construct constraints for the c_*_x themselves,
863 * but for the coefficients of c_*_x written as a linear combination
864 * of the columns in node->cmap.
866 static int add_inter_proximity_constraints(struct isl_sched_graph *graph,
867 struct isl_sched_edge *edge, int s)
871 isl_map *map = isl_map_copy(edge->map);
872 isl_ctx *ctx = isl_map_get_ctx(map);
874 isl_dim_map *dim_map;
876 struct isl_sched_node *src = edge->src;
877 struct isl_sched_node *dst = edge->dst;
879 coef = inter_coefficients(graph, map);
881 dim = isl_dim_domain(isl_dim_unwrap(isl_basic_set_get_dim(coef)));
883 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
884 isl_dim_size(dim, isl_dim_set), isl_mat_copy(src->cmap));
885 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
886 isl_dim_size(dim, isl_dim_set) + src->nvar,
887 isl_mat_copy(dst->cmap));
889 nparam = isl_dim_size(src->dim, isl_dim_param);
890 total = isl_basic_set_total_dim(graph->lp);
891 dim_map = isl_dim_map_alloc(ctx, total);
893 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
894 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
895 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
897 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, -s);
898 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, s);
899 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, -s);
900 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
901 isl_dim_size(dim, isl_dim_set) + src->nvar, 1,
903 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
904 isl_dim_size(dim, isl_dim_set) + src->nvar, 1,
907 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, s);
908 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, -s);
909 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, s);
910 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
911 isl_dim_size(dim, isl_dim_set), 1,
913 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
914 isl_dim_size(dim, isl_dim_set), 1,
917 graph->lp = isl_basic_set_extend_constraints(graph->lp,
918 coef->n_eq, coef->n_ineq);
919 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
926 static int add_all_validity_constraints(struct isl_sched_graph *graph)
930 for (i = 0; i < graph->n_edge; ++i) {
931 struct isl_sched_edge *edge= &graph->edge[i];
934 if (edge->src != edge->dst)
936 if (add_intra_validity_constraints(graph, edge) < 0)
940 for (i = 0; i < graph->n_edge; ++i) {
941 struct isl_sched_edge *edge = &graph->edge[i];
944 if (edge->src == edge->dst)
946 if (add_inter_validity_constraints(graph, edge) < 0)
953 /* Add constraints to graph->lp that bound the dependence distance
954 * for all dependence relations.
955 * If a given proximity dependence is identical to a validity
956 * dependence, then the dependence distance is already bounded
957 * from below (by zero), so we only need to bound the distance
959 * Otherwise, we need to bound the distance both from above and from below.
961 static int add_all_proximity_constraints(struct isl_sched_graph *graph)
965 for (i = 0; i < graph->n_edge; ++i) {
966 struct isl_sched_edge *edge= &graph->edge[i];
967 if (!edge->proximity)
969 if (edge->src == edge->dst &&
970 add_intra_proximity_constraints(graph, edge, 1) < 0)
972 if (edge->src != edge->dst &&
973 add_inter_proximity_constraints(graph, edge, 1) < 0)
977 if (edge->src == edge->dst &&
978 add_intra_proximity_constraints(graph, edge, -1) < 0)
980 if (edge->src != edge->dst &&
981 add_inter_proximity_constraints(graph, edge, -1) < 0)
988 /* Compute a basis for the rows in the linear part of the schedule
989 * and extend this basis to a full basis. The remaining rows
990 * can then be used to force linear independence from the rows
993 * In particular, given the schedule rows S, we compute
997 * with H the Hermite normal form of S. That is, all but the
998 * first rank columns of Q are zero and so each row in S is
999 * a linear combination of the first rank rows of Q.
1000 * The matrix Q is then transposed because we will write the
1001 * coefficients of the next schedule row as a column vector s
1002 * and express this s as a linear combination s = Q c of the
1005 static int node_update_cmap(struct isl_sched_node *node)
1008 int n_row = isl_mat_rows(node->sched);
1010 H = isl_mat_sub_alloc(node->sched, 0, n_row,
1011 1 + node->nparam, node->nvar);
1013 H = isl_mat_left_hermite(H, 0, NULL, &Q);
1014 isl_mat_free(node->cmap);
1015 node->cmap = isl_mat_transpose(Q);
1016 node->rank = isl_mat_initial_non_zero_cols(H);
1019 if (!node->cmap || node->rank < 0)
1024 /* Count the number of equality and inequality constraints
1025 * that will be added. If once is set, then we count
1026 * each edge exactly once. Otherwise, we count as follows
1027 * validity -> 1 (>= 0)
1028 * validity+proximity -> 2 (>= 0 and upper bound)
1029 * proximity -> 2 (lower and upper bound)
1031 static int count_constraints(struct isl_sched_graph *graph,
1032 int *n_eq, int *n_ineq, int once)
1035 isl_basic_set *coef;
1037 *n_eq = *n_ineq = 0;
1038 for (i = 0; i < graph->n_edge; ++i) {
1039 struct isl_sched_edge *edge= &graph->edge[i];
1040 isl_map *map = isl_map_copy(edge->map);
1041 int f = once ? 1 : edge->proximity ? 2 : 1;
1043 if (edge->src == edge->dst)
1044 coef = intra_coefficients(graph, map);
1046 coef = inter_coefficients(graph, map);
1049 *n_eq += f * coef->n_eq;
1050 *n_ineq += f * coef->n_ineq;
1051 isl_basic_set_free(coef);
1057 /* Construct an ILP problem for finding schedule coefficients
1058 * that result in non-negative, but small dependence distances
1059 * over all dependences.
1060 * In particular, the dependence distances over proximity edges
1061 * are bounded by m_0 + m_n n and we compute schedule coefficients
1062 * with small values (preferably zero) of m_n and m_0.
1064 * All variables of the ILP are non-negative. The actual coefficients
1065 * may be negative, so each coefficient is represented as the difference
1066 * of two non-negative variables. The negative part always appears
1067 * immediately before the positive part.
1068 * Other than that, the variables have the following order
1070 * - sum of positive and negative parts of m_n coefficients
1072 * - sum of positive and negative parts of all c_n coefficients
1073 * (unconstrained when computing non-parametric schedules)
1074 * - sum of positive and negative parts of all c_x coefficients
1075 * - positive and negative parts of m_n coefficients
1078 * - positive and negative parts of c_i_n (if parametric)
1079 * - positive and negative parts of c_i_x
1081 * The c_i_x are not represented directly, but through the columns of
1082 * node->cmap. That is, the computed values are for variable t_i_x
1083 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
1085 * The constraints are those from the edges plus two or three equalities
1086 * to express the sums.
1088 static int setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
1099 parametric = ctx->opt->schedule_parametric;
1100 nparam = isl_dim_size(graph->node[0].dim, isl_dim_param);
1102 total = param_pos + 2 * nparam;
1103 for (i = 0; i < graph->n; ++i) {
1104 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
1105 if (node_update_cmap(node) < 0)
1107 node->start = total;
1108 total += 1 + 2 * (node->nparam + node->nvar);
1111 if (count_constraints(graph, &n_eq, &n_ineq, 0) < 0)
1114 dim = isl_dim_set_alloc(ctx, 0, total);
1115 isl_basic_set_free(graph->lp);
1116 n_eq += 2 + parametric;
1117 graph->lp = isl_basic_set_alloc_dim(dim, 0, n_eq, n_ineq);
1119 k = isl_basic_set_alloc_equality(graph->lp);
1122 isl_seq_clr(graph->lp->eq[k], 1 + total);
1123 isl_int_set_si(graph->lp->eq[k][1], -1);
1124 for (i = 0; i < 2 * nparam; ++i)
1125 isl_int_set_si(graph->lp->eq[k][1 + param_pos + i], 1);
1128 k = isl_basic_set_alloc_equality(graph->lp);
1131 isl_seq_clr(graph->lp->eq[k], 1 + total);
1132 isl_int_set_si(graph->lp->eq[k][3], -1);
1133 for (i = 0; i < graph->n; ++i) {
1134 int pos = 1 + graph->node[i].start + 1;
1136 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
1137 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
1141 k = isl_basic_set_alloc_equality(graph->lp);
1144 isl_seq_clr(graph->lp->eq[k], 1 + total);
1145 isl_int_set_si(graph->lp->eq[k][4], -1);
1146 for (i = 0; i < graph->n; ++i) {
1147 struct isl_sched_node *node = &graph->node[i];
1148 int pos = 1 + node->start + 1 + 2 * node->nparam;
1150 for (j = 0; j < 2 * node->nvar; ++j)
1151 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
1154 if (add_all_validity_constraints(graph) < 0)
1156 if (add_all_proximity_constraints(graph) < 0)
1162 /* Analyze the conflicting constraint found by
1163 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
1164 * constraint of one of the edges between distinct nodes, living, moreover
1165 * in distinct SCCs, then record the source and sink SCC as this may
1166 * be a good place to cut between SCCs.
1168 static int check_conflict(int con, void *user)
1171 struct isl_sched_graph *graph = user;
1173 if (graph->src_scc >= 0)
1176 con -= graph->lp->n_eq;
1178 if (con >= graph->lp->n_ineq)
1181 for (i = 0; i < graph->n_edge; ++i) {
1182 if (!graph->edge[i].validity)
1184 if (graph->edge[i].src == graph->edge[i].dst)
1186 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
1188 if (graph->edge[i].start > con)
1190 if (graph->edge[i].end <= con)
1192 graph->src_scc = graph->edge[i].src->scc;
1193 graph->dst_scc = graph->edge[i].dst->scc;
1199 /* Check whether the next schedule row of the given node needs to be
1200 * non-trivial. Lower-dimensional domains may have some trivial rows,
1201 * but as soon as the number of remaining required non-trivial rows
1202 * is as large as the number or remaining rows to be computed,
1203 * all remaining rows need to be non-trivial.
1205 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
1207 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
1210 /* Solve the ILP problem constructed in setup_lp.
1211 * For each node such that all the remaining rows of its schedule
1212 * need to be non-trivial, we construct a non-triviality region.
1213 * This region imposes that the next row is independent of previous rows.
1214 * In particular the coefficients c_i_x are represented by t_i_x
1215 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
1216 * its first columns span the rows of the previously computed part
1217 * of the schedule. The non-triviality region enforces that at least
1218 * one of the remaining components of t_i_x is non-zero, i.e.,
1219 * that the new schedule row depends on at least one of the remaining
1222 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
1228 for (i = 0; i < graph->n; ++i) {
1229 struct isl_sched_node *node = &graph->node[i];
1230 int skip = node->rank;
1231 graph->region[i].pos = node->start + 1 + 2*(node->nparam+skip);
1232 if (needs_row(graph, node))
1233 graph->region[i].len = 2 * (node->nvar - skip);
1235 graph->region[i].len = 0;
1237 lp = isl_basic_set_copy(graph->lp);
1238 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
1239 graph->region, &check_conflict, graph);
1243 /* Update the schedules of all nodes based on the given solution
1244 * of the LP problem.
1245 * The new row is added to the current band.
1246 * All possibly negative coefficients are encoded as a difference
1247 * of two non-negative variables, so we need to perform the subtraction
1248 * here. Moreover, if use_cmap is set, then the solution does
1249 * not refer to the actual coefficients c_i_x, but instead to variables
1250 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
1251 * In this case, we then also need to perform this multiplication
1252 * to obtain the values of c_i_x.
1254 static int update_schedule(struct isl_sched_graph *graph,
1255 __isl_take isl_vec *sol, int use_cmap)
1258 isl_vec *csol = NULL;
1263 isl_die(sol->ctx, isl_error_internal,
1264 "no solution found", goto error);
1266 for (i = 0; i < graph->n; ++i) {
1267 struct isl_sched_node *node = &graph->node[i];
1268 int pos = node->start;
1269 int row = isl_mat_rows(node->sched);
1272 csol = isl_vec_alloc(sol->ctx, node->nvar);
1276 isl_map_free(node->sched_map);
1277 node->sched_map = NULL;
1278 node->sched = isl_mat_add_rows(node->sched, 1);
1281 node->sched = isl_mat_set_element(node->sched, row, 0,
1283 for (j = 0; j < node->nparam + node->nvar; ++j)
1284 isl_int_sub(sol->el[1 + pos + 1 + 2 * j + 1],
1285 sol->el[1 + pos + 1 + 2 * j + 1],
1286 sol->el[1 + pos + 1 + 2 * j]);
1287 for (j = 0; j < node->nparam; ++j)
1288 node->sched = isl_mat_set_element(node->sched,
1289 row, 1 + j, sol->el[1+pos+1+2*j+1]);
1290 for (j = 0; j < node->nvar; ++j)
1291 isl_int_set(csol->el[j],
1292 sol->el[1+pos+1+2*(node->nparam+j)+1]);
1294 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
1298 for (j = 0; j < node->nvar; ++j)
1299 node->sched = isl_mat_set_element(node->sched,
1300 row, 1 + node->nparam + j, csol->el[j]);
1301 node->band[graph->n_total_row] = graph->n_band;
1307 graph->n_total_row++;
1316 /* Convert node->sched into a map and return this map.
1317 * We simply add equality constraints that express each output variable
1318 * as the affine combination of parameters and input variables specified
1319 * by the schedule matrix.
1321 * The result is cached in node->sched_map, which needs to be released
1322 * whenever node->sched is updated.
1324 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
1328 isl_basic_map *bmap;
1333 if (node->sched_map)
1334 return isl_map_copy(node->sched_map);
1336 nrow = isl_mat_rows(node->sched);
1337 ncol = isl_mat_cols(node->sched) - 1;
1338 dim = isl_dim_from_domain(isl_dim_copy(node->dim));
1339 dim = isl_dim_add(dim, isl_dim_out, nrow);
1340 bmap = isl_basic_map_universe(isl_dim_copy(dim));
1344 for (i = 0; i < nrow; ++i) {
1345 c = isl_equality_alloc(isl_dim_copy(dim));
1346 isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
1347 isl_mat_get_element(node->sched, i, 0, &v);
1348 isl_constraint_set_constant(c, v);
1349 for (j = 0; j < node->nparam; ++j) {
1350 isl_mat_get_element(node->sched, i, 1 + j, &v);
1351 isl_constraint_set_coefficient(c, isl_dim_param, j, v);
1353 for (j = 0; j < node->nvar; ++j) {
1354 isl_mat_get_element(node->sched,
1355 i, 1 + node->nparam + j, &v);
1356 isl_constraint_set_coefficient(c, isl_dim_in, j, v);
1358 bmap = isl_basic_map_add_constraint(bmap, c);
1365 node->sched_map = isl_map_from_basic_map(bmap);
1366 return isl_map_copy(node->sched_map);
1369 /* Update the given dependence relation based on the current schedule.
1370 * That is, intersect the dependence relation with a map expressing
1371 * that source and sink are executed within the same iteration of
1372 * the current schedule.
1373 * This is not the most efficient way, but this shouldn't be a critical
1376 static __isl_give isl_map *specialize(__isl_take isl_map *map,
1377 struct isl_sched_node *src, struct isl_sched_node *dst)
1379 isl_map *src_sched, *dst_sched, *id;
1381 src_sched = node_extract_schedule(src);
1382 dst_sched = node_extract_schedule(dst);
1383 id = isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
1384 return isl_map_intersect(map, id);
1387 /* Update the dependence relations of all edges based on the current schedule.
1388 * If a dependence is carried completely by the current schedule, then
1389 * it is removed and edge_table is updated accordingly.
1391 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
1394 int reset_table = 0;
1396 for (i = graph->n_edge - 1; i >= 0; --i) {
1397 struct isl_sched_edge *edge = &graph->edge[i];
1398 edge->map = specialize(edge->map, edge->src, edge->dst);
1402 if (isl_map_plain_is_empty(edge->map)) {
1404 isl_map_free(edge->map);
1405 if (i != graph->n_edge - 1)
1406 graph->edge[i] = graph->edge[graph->n_edge - 1];
1412 isl_hash_table_free(ctx, graph->edge_table);
1413 graph->edge_table = NULL;
1414 return graph_init_edge_table(ctx, graph);
1420 static void next_band(struct isl_sched_graph *graph)
1422 graph->band_start = graph->n_total_row;
1426 /* Topologically sort statements mapped to same schedule iteration
1427 * and add a row to the schedule corresponding to this order.
1429 static int sort_statements(isl_ctx *ctx, struct isl_sched_graph *graph)
1436 if (update_edges(ctx, graph) < 0)
1439 if (graph->n_edge == 0)
1442 if (detect_sccs(graph) < 0)
1445 for (i = 0; i < graph->n; ++i) {
1446 struct isl_sched_node *node = &graph->node[i];
1447 int row = isl_mat_rows(node->sched);
1448 int cols = isl_mat_cols(node->sched);
1450 isl_map_free(node->sched_map);
1451 node->sched_map = NULL;
1452 node->sched = isl_mat_add_rows(node->sched, 1);
1455 node->sched = isl_mat_set_element_si(node->sched, row, 0,
1457 for (j = 1; j < cols; ++j)
1458 node->sched = isl_mat_set_element_si(node->sched,
1460 node->band[graph->n_total_row] = graph->n_band;
1463 graph->n_total_row++;
1469 /* Construct an isl_schedule based on the computed schedule stored
1470 * in graph and with parameters specified by dim.
1472 static __isl_give isl_schedule *extract_schedule(struct isl_sched_graph *graph,
1473 __isl_take isl_dim *dim)
1477 isl_schedule *sched = NULL;
1482 ctx = isl_dim_get_ctx(dim);
1483 sched = isl_calloc(ctx, struct isl_schedule,
1484 sizeof(struct isl_schedule) +
1485 (graph->n - 1) * sizeof(struct isl_schedule_node));
1489 sched->n = graph->n;
1490 sched->n_band = graph->n_band;
1491 sched->n_total_row = graph->n_total_row;
1493 for (i = 0; i < sched->n; ++i) {
1497 band_end = isl_alloc_array(ctx, int, graph->n_band);
1500 sched->node[i].sched = node_extract_schedule(&graph->node[i]);
1501 sched->node[i].band_end = band_end;
1503 for (r = b = 0; r < graph->n_total_row; ++r) {
1504 if (graph->node[i].band[r] == b)
1507 if (graph->node[i].band[r] == -1)
1510 if (r == graph->n_total_row)
1512 sched->node[i].n_band = b;
1520 isl_schedule_free(sched);
1524 /* Copy nodes that satisfy node_pred from the src dependence graph
1525 * to the dst dependence graph.
1527 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
1528 int (*node_pred)(struct isl_sched_node *node, int data), int data)
1533 for (i = 0; i < src->n; ++i) {
1534 if (!node_pred(&src->node[i], data))
1536 dst->node[dst->n].dim = isl_dim_copy(src->node[i].dim);
1537 dst->node[dst->n].nvar = src->node[i].nvar;
1538 dst->node[dst->n].nparam = src->node[i].nparam;
1539 dst->node[dst->n].sched = isl_mat_copy(src->node[i].sched);
1540 dst->node[dst->n].sched_map =
1541 isl_map_copy(src->node[i].sched_map);
1542 dst->node[dst->n].band = src->node[i].band;
1549 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
1550 * to the dst dependence graph.
1552 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
1553 struct isl_sched_graph *src,
1554 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
1559 for (i = 0; i < src->n_edge; ++i) {
1560 struct isl_sched_edge *edge = &src->edge[i];
1563 if (!edge_pred(edge, data))
1566 if (isl_map_plain_is_empty(edge->map))
1569 map = isl_map_copy(edge->map);
1571 dst->edge[dst->n_edge].src =
1572 graph_find_node(ctx, dst, edge->src->dim);
1573 dst->edge[dst->n_edge].dst =
1574 graph_find_node(ctx, dst, edge->dst->dim);
1575 dst->edge[dst->n_edge].map = map;
1576 dst->edge[dst->n_edge].validity = edge->validity;
1577 dst->edge[dst->n_edge].proximity = edge->proximity;
1584 /* Given a "src" dependence graph that contains the nodes from "dst"
1585 * that satisfy node_pred, copy the schedule computed in "src"
1586 * for those nodes back to "dst".
1588 static int copy_schedule(struct isl_sched_graph *dst,
1589 struct isl_sched_graph *src,
1590 int (*node_pred)(struct isl_sched_node *node, int data), int data)
1595 for (i = 0; i < dst->n; ++i) {
1596 if (!node_pred(&dst->node[i], data))
1598 isl_mat_free(dst->node[i].sched);
1599 isl_map_free(dst->node[i].sched_map);
1600 dst->node[i].sched = isl_mat_copy(src->node[src->n].sched);
1601 dst->node[i].sched_map =
1602 isl_map_copy(src->node[src->n].sched_map);
1606 dst->n_total_row = src->n_total_row;
1607 dst->n_band = src->n_band;
1612 /* Compute the maximal number of variables over all nodes.
1613 * This is the maximal number of linearly independent schedule
1614 * rows that we need to compute.
1615 * Just in case we end up in a part of the dependence graph
1616 * with only lower-dimensional domains, we make sure we will
1617 * compute the required amount of extra linearly independent rows.
1619 static int compute_maxvar(struct isl_sched_graph *graph)
1624 for (i = 0; i < graph->n; ++i) {
1625 struct isl_sched_node *node = &graph->node[i];
1628 if (node_update_cmap(node) < 0)
1630 nvar = node->nvar + graph->n_row - node->rank;
1631 if (nvar > graph->maxvar)
1632 graph->maxvar = nvar;
1638 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph);
1639 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph);
1641 /* Compute a schedule for a subgraph of "graph". In particular, for
1642 * the graph composed of nodes that satisfy node_pred and edges that
1643 * that satisfy edge_pred. The caller should precompute the number
1644 * of nodes and edges that satisfy these predicates and pass them along
1645 * as "n" and "n_edge".
1646 * If the subgraph is known to consist of a single component, then wcc should
1647 * be set and then we call compute_schedule_wcc on the constructed subgraph.
1648 * Otherwise, we call compute_schedule, which will check whether the subgraph
1651 static int compute_sub_schedule(isl_ctx *ctx,
1652 struct isl_sched_graph *graph, int n, int n_edge,
1653 int (*node_pred)(struct isl_sched_node *node, int data),
1654 int (*edge_pred)(struct isl_sched_edge *edge, int data),
1657 struct isl_sched_graph split = { 0 };
1659 if (graph_alloc(ctx, &split, n, n_edge) < 0)
1661 if (copy_nodes(&split, graph, node_pred, data) < 0)
1663 if (graph_init_table(ctx, &split) < 0)
1665 if (copy_edges(ctx, &split, graph, edge_pred, data) < 0)
1667 if (graph_init_edge_table(ctx, &split) < 0)
1669 split.n_row = graph->n_row;
1670 split.n_total_row = graph->n_total_row;
1671 split.n_band = graph->n_band;
1672 split.band_start = graph->band_start;
1674 if (wcc && compute_schedule_wcc(ctx, &split) < 0)
1676 if (!wcc && compute_schedule(ctx, &split) < 0)
1679 copy_schedule(graph, &split, node_pred, data);
1681 graph_free(ctx, &split);
1684 graph_free(ctx, &split);
1688 static int node_scc_exactly(struct isl_sched_node *node, int scc)
1690 return node->scc == scc;
1693 static int node_scc_at_most(struct isl_sched_node *node, int scc)
1695 return node->scc <= scc;
1698 static int node_scc_at_least(struct isl_sched_node *node, int scc)
1700 return node->scc >= scc;
1703 static int edge_src_scc_exactly(struct isl_sched_edge *edge, int scc)
1705 return edge->src->scc == scc;
1708 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
1710 return edge->dst->scc <= scc;
1713 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
1715 return edge->src->scc >= scc;
1718 /* Pad the schedules of all nodes with zero rows such that in the end
1719 * they all have graph->n_total_row rows.
1720 * The extra rows don't belong to any band, so they get assigned band number -1.
1722 static int pad_schedule(struct isl_sched_graph *graph)
1726 for (i = 0; i < graph->n; ++i) {
1727 struct isl_sched_node *node = &graph->node[i];
1728 int row = isl_mat_rows(node->sched);
1729 if (graph->n_total_row > row) {
1730 isl_map_free(node->sched_map);
1731 node->sched_map = NULL;
1733 node->sched = isl_mat_add_zero_rows(node->sched,
1734 graph->n_total_row - row);
1737 for (j = row; j < graph->n_total_row; ++j)
1744 /* Split the current graph into two parts and compute a schedule for each
1745 * part individually. In particular, one part consists of all SCCs up
1746 * to and including graph->src_scc, while the other part contains the other
1749 * The split is enforced in the schedule by constant rows with two different
1750 * values (0 and 1). These constant rows replace the previously computed rows
1751 * in the current band.
1752 * It would be possible to reuse them as the first rows in the next
1753 * band, but recomputing them may result in better rows as we are looking
1754 * at a smaller part of the dependence graph.
1756 static int compute_split_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
1758 int i, j, n, e1, e2;
1759 int n_total_row, orig_total_row;
1760 int n_band, orig_band;
1763 drop = graph->n_total_row - graph->band_start;
1764 graph->n_total_row -= drop;
1765 graph->n_row -= drop;
1768 for (i = 0; i < graph->n; ++i) {
1769 struct isl_sched_node *node = &graph->node[i];
1770 int row = isl_mat_rows(node->sched) - drop;
1771 int cols = isl_mat_cols(node->sched);
1772 int before = node->scc <= graph->src_scc;
1777 isl_map_free(node->sched_map);
1778 node->sched_map = NULL;
1779 node->sched = isl_mat_drop_rows(node->sched,
1780 graph->band_start, drop);
1781 node->sched = isl_mat_add_rows(node->sched, 1);
1784 node->sched = isl_mat_set_element_si(node->sched, row, 0,
1786 for (j = 1; j < cols; ++j)
1787 node->sched = isl_mat_set_element_si(node->sched,
1789 node->band[graph->n_total_row] = graph->n_band;
1793 for (i = 0; i < graph->n_edge; ++i) {
1794 if (graph->edge[i].dst->scc <= graph->src_scc)
1796 if (graph->edge[i].src->scc > graph->src_scc)
1800 graph->n_total_row++;
1803 orig_total_row = graph->n_total_row;
1804 orig_band = graph->n_band;
1805 if (compute_sub_schedule(ctx, graph, n, e1,
1806 &node_scc_at_most, &edge_dst_scc_at_most,
1807 graph->src_scc, 0) < 0)
1809 n_total_row = graph->n_total_row;
1810 graph->n_total_row = orig_total_row;
1811 n_band = graph->n_band;
1812 graph->n_band = orig_band;
1813 if (compute_sub_schedule(ctx, graph, graph->n - n, e2,
1814 &node_scc_at_least, &edge_src_scc_at_least,
1815 graph->src_scc + 1, 0) < 0)
1817 if (n_total_row > graph->n_total_row)
1818 graph->n_total_row = n_total_row;
1819 if (n_band > graph->n_band)
1820 graph->n_band = n_band;
1822 return pad_schedule(graph);
1825 /* Compute the next band of the schedule after updating the dependence
1826 * relations based on the the current schedule.
1828 static int compute_next_band(isl_ctx *ctx, struct isl_sched_graph *graph)
1830 if (update_edges(ctx, graph) < 0)
1834 return compute_schedule(ctx, graph);
1837 /* Add constraints to graph->lp that force the dependence of edge i
1838 * to be respected and attempt to carry it, where edge i is one from
1839 * a node j to itself.
1840 * That is, add constraints that enforce
1842 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
1843 * = c_j_x (y - x) >= e_i
1845 * for each (x,y) in R.
1846 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1847 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
1848 * with each coefficient in c_j_x represented as a pair of non-negative
1851 static int add_intra_constraints(struct isl_sched_graph *graph, int i)
1854 struct isl_sched_edge *edge= &graph->edge[i];
1855 isl_map *map = isl_map_copy(edge->map);
1856 isl_ctx *ctx = isl_map_get_ctx(map);
1858 isl_dim_map *dim_map;
1859 isl_basic_set *coef;
1860 struct isl_sched_node *node = edge->src;
1862 coef = intra_coefficients(graph, map);
1864 dim = isl_dim_domain(isl_dim_unwrap(isl_basic_set_get_dim(coef)));
1866 total = isl_basic_set_total_dim(graph->lp);
1867 dim_map = isl_dim_map_alloc(ctx, total);
1868 isl_dim_map_range(dim_map, 3 + i, 0, 0, 0, 1, -1);
1869 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
1870 isl_dim_size(dim, isl_dim_set), 1,
1872 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
1873 isl_dim_size(dim, isl_dim_set), 1,
1875 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1876 coef->n_eq, coef->n_ineq);
1877 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1884 /* Add constraints to graph->lp that force the dependence of edge i
1885 * to be respected and attempt to carry it, where edge i is one from
1887 * That is, add constraints that enforce
1889 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
1891 * for each (x,y) in R.
1892 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1893 * of valid constraints for R and then plug in
1894 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
1895 * with each coefficient (except e_i, c_k_0 and c_j_0)
1896 * represented as a pair of non-negative coefficients.
1898 static int add_inter_constraints(struct isl_sched_graph *graph, int i)
1901 struct isl_sched_edge *edge= &graph->edge[i];
1902 isl_map *map = isl_map_copy(edge->map);
1903 isl_ctx *ctx = isl_map_get_ctx(map);
1905 isl_dim_map *dim_map;
1906 isl_basic_set *coef;
1907 struct isl_sched_node *src = edge->src;
1908 struct isl_sched_node *dst = edge->dst;
1910 coef = inter_coefficients(graph, map);
1912 dim = isl_dim_domain(isl_dim_unwrap(isl_basic_set_get_dim(coef)));
1914 total = isl_basic_set_total_dim(graph->lp);
1915 dim_map = isl_dim_map_alloc(ctx, total);
1917 isl_dim_map_range(dim_map, 3 + i, 0, 0, 0, 1, -1);
1919 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
1920 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
1921 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
1922 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1923 isl_dim_size(dim, isl_dim_set) + src->nvar, 1,
1925 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1926 isl_dim_size(dim, isl_dim_set) + src->nvar, 1,
1929 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
1930 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
1931 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
1932 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1933 isl_dim_size(dim, isl_dim_set), 1,
1935 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1936 isl_dim_size(dim, isl_dim_set), 1,
1939 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1940 coef->n_eq, coef->n_ineq);
1941 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1948 /* Add constraints to graph->lp that force all dependence
1949 * to be respected and attempt to carry it.
1951 static int add_all_constraints(struct isl_sched_graph *graph)
1955 for (i = 0; i < graph->n_edge; ++i) {
1956 struct isl_sched_edge *edge= &graph->edge[i];
1957 if (edge->src == edge->dst &&
1958 add_intra_constraints(graph, i) < 0)
1960 if (edge->src != edge->dst &&
1961 add_inter_constraints(graph, i) < 0)
1968 /* Construct an LP problem for finding schedule coefficients
1969 * such that the schedule carries as many dependences as possible.
1970 * In particular, for each dependence i, we bound the dependence distance
1971 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
1972 * of all e_i's. Dependence with e_i = 0 in the solution are simply
1973 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
1975 * All variables of the LP are non-negative. The actual coefficients
1976 * may be negative, so each coefficient is represented as the difference
1977 * of two non-negative variables. The negative part always appears
1978 * immediately before the positive part.
1979 * Other than that, the variables have the following order
1981 * - sum of (1 - e_i) over all edges
1982 * - sum of positive and negative parts of all c_n coefficients
1983 * (unconstrained when computing non-parametric schedules)
1984 * - sum of positive and negative parts of all c_x coefficients
1989 * - positive and negative parts of c_i_n (if parametric)
1990 * - positive and negative parts of c_i_x
1992 * The constraints are those from the edges plus three equalities
1993 * to express the sums and n_edge inequalities to express e_i <= 1.
1995 static int setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
2003 total = 3 + graph->n_edge;
2004 for (i = 0; i < graph->n; ++i) {
2005 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2006 node->start = total;
2007 total += 1 + 2 * (node->nparam + node->nvar);
2010 if (count_constraints(graph, &n_eq, &n_ineq, 1) < 0)
2013 dim = isl_dim_set_alloc(ctx, 0, total);
2014 isl_basic_set_free(graph->lp);
2016 n_ineq += graph->n_edge;
2017 graph->lp = isl_basic_set_alloc_dim(dim, 0, n_eq, n_ineq);
2018 graph->lp = isl_basic_set_set_rational(graph->lp);
2020 k = isl_basic_set_alloc_equality(graph->lp);
2023 isl_seq_clr(graph->lp->eq[k], 1 + total);
2024 isl_int_set_si(graph->lp->eq[k][0], -graph->n_edge);
2025 isl_int_set_si(graph->lp->eq[k][1], 1);
2026 for (i = 0; i < graph->n_edge; ++i)
2027 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
2029 k = isl_basic_set_alloc_equality(graph->lp);
2032 isl_seq_clr(graph->lp->eq[k], 1 + total);
2033 isl_int_set_si(graph->lp->eq[k][2], -1);
2034 for (i = 0; i < graph->n; ++i) {
2035 int pos = 1 + graph->node[i].start + 1;
2037 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
2038 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2041 k = isl_basic_set_alloc_equality(graph->lp);
2044 isl_seq_clr(graph->lp->eq[k], 1 + total);
2045 isl_int_set_si(graph->lp->eq[k][3], -1);
2046 for (i = 0; i < graph->n; ++i) {
2047 struct isl_sched_node *node = &graph->node[i];
2048 int pos = 1 + node->start + 1 + 2 * node->nparam;
2050 for (j = 0; j < 2 * node->nvar; ++j)
2051 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2054 for (i = 0; i < graph->n_edge; ++i) {
2055 k = isl_basic_set_alloc_inequality(graph->lp);
2058 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2059 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
2060 isl_int_set_si(graph->lp->ineq[k][0], 1);
2063 if (add_all_constraints(graph) < 0)
2069 /* Construct a schedule row for each node such that as many dependences
2070 * as possible are carried and then continue with the next band.
2072 static int carry_dependences(isl_ctx *ctx, struct isl_sched_graph *graph)
2077 if (setup_carry_lp(ctx, graph) < 0)
2080 lp = isl_basic_set_copy(graph->lp);
2081 sol = isl_tab_basic_set_non_neg_lexmin(lp);
2085 if (sol->size == 0) {
2087 isl_die(ctx, isl_error_internal,
2088 "error in schedule construction", return -1);
2091 if (isl_int_cmp_si(sol->el[1], graph->n_edge) >= 0) {
2093 isl_die(ctx, isl_error_unknown,
2094 "unable to carry dependences", return -1);
2097 if (update_schedule(graph, sol, 0) < 0)
2100 return compute_next_band(ctx, graph);
2103 /* Compute a schedule for a connected dependence graph.
2104 * We try to find a sequence of as many schedule rows as possible that result
2105 * in non-negative dependence distances (independent of the previous rows
2106 * in the sequence, i.e., such that the sequence is tilable).
2107 * If we can't find any more rows we either
2108 * - split between SCCs and start over (assuming we found an interesting
2109 * pair of SCCs between which to split)
2110 * - continue with the next band (assuming the current band has at least
2112 * - try to carry as many dependences as possible and continue with the next
2115 * If we manage to complete the schedule, we finish off by topologically
2116 * sorting the statements based on the remaining dependences.
2118 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph)
2120 if (detect_sccs(graph) < 0)
2124 if (compute_maxvar(graph) < 0)
2127 while (graph->n_row < graph->maxvar) {
2130 graph->src_scc = -1;
2131 graph->dst_scc = -1;
2133 if (setup_lp(ctx, graph) < 0)
2135 sol = solve_lp(graph);
2138 if (sol->size == 0) {
2140 if (graph->src_scc >= 0)
2141 return compute_split_schedule(ctx, graph);
2142 if (graph->n_total_row > graph->band_start)
2143 return compute_next_band(ctx, graph);
2144 return carry_dependences(ctx, graph);
2146 if (update_schedule(graph, sol, 1) < 0)
2150 if (graph->n_total_row > graph->band_start)
2152 return sort_statements(ctx, graph);
2155 /* Compute a schedule for each component (identified by node->scc)
2156 * of the dependence graph separately and then combine the results.
2158 static int compute_component_schedule(isl_ctx *ctx,
2159 struct isl_sched_graph *graph)
2163 int n_total_row, orig_total_row;
2164 int n_band, orig_band;
2167 orig_total_row = graph->n_total_row;
2169 orig_band = graph->n_band;
2170 for (wcc = 0; wcc < graph->scc; ++wcc) {
2172 for (i = 0; i < graph->n; ++i)
2173 if (graph->node[i].scc == wcc)
2176 for (i = 0; i < graph->n_edge; ++i)
2177 if (graph->edge[i].src->scc == wcc)
2180 if (compute_sub_schedule(ctx, graph, n, n_edge,
2182 &edge_src_scc_exactly, wcc, 1) < 0)
2184 if (graph->n_total_row > n_total_row)
2185 n_total_row = graph->n_total_row;
2186 graph->n_total_row = orig_total_row;
2187 if (graph->n_band > n_band)
2188 n_band = graph->n_band;
2189 graph->n_band = orig_band;
2192 graph->n_total_row = n_total_row;
2193 graph->n_band = n_band;
2195 return pad_schedule(graph);
2198 /* Compute a schedule for the given dependence graph.
2199 * We first check if the graph is connected (through validity dependences)
2200 * and if so compute a schedule for each component separately.
2202 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
2204 if (detect_wccs(graph) < 0)
2208 return compute_component_schedule(ctx, graph);
2210 return compute_schedule_wcc(ctx, graph);
2213 /* Compute a schedule for the given union of domains that respects
2214 * all the validity dependences and tries to minimize the dependence
2215 * distances over the proximity dependences.
2217 __isl_give isl_schedule *isl_union_set_compute_schedule(
2218 __isl_take isl_union_set *domain,
2219 __isl_take isl_union_map *validity,
2220 __isl_take isl_union_map *proximity)
2222 isl_ctx *ctx = isl_union_set_get_ctx(domain);
2224 struct isl_sched_graph graph = { 0 };
2225 isl_schedule *sched;
2227 domain = isl_union_set_align_params(domain,
2228 isl_union_map_get_dim(validity));
2229 domain = isl_union_set_align_params(domain,
2230 isl_union_map_get_dim(proximity));
2231 dim = isl_union_set_get_dim(domain);
2232 validity = isl_union_map_align_params(validity, isl_dim_copy(dim));
2233 proximity = isl_union_map_align_params(proximity, dim);
2238 graph.n = isl_union_set_n_set(domain);
2241 if (graph_alloc(ctx, &graph, graph.n,
2242 isl_union_map_n_map(validity) + isl_union_map_n_map(proximity)) < 0)
2246 if (isl_union_set_foreach_set(domain, &extract_node, &graph) < 0)
2248 if (graph_init_table(ctx, &graph) < 0)
2251 if (isl_union_map_foreach_map(validity, &extract_edge, &graph) < 0)
2253 if (graph_init_edge_table(ctx, &graph) < 0)
2255 if (isl_union_map_foreach_map(proximity, &extract_edge, &graph) < 0)
2258 if (compute_schedule(ctx, &graph) < 0)
2262 sched = extract_schedule(&graph, isl_union_set_get_dim(domain));
2264 graph_free(ctx, &graph);
2265 isl_union_set_free(domain);
2266 isl_union_map_free(validity);
2267 isl_union_map_free(proximity);
2271 graph_free(ctx, &graph);
2272 isl_union_set_free(domain);
2273 isl_union_map_free(validity);
2274 isl_union_map_free(proximity);
2278 void *isl_schedule_free(__isl_take isl_schedule *sched)
2283 for (i = 0; i < sched->n; ++i) {
2284 isl_map_free(sched->node[i].sched);
2285 free(sched->node[i].band_end);
2287 isl_dim_free(sched->dim);
2292 __isl_give isl_union_map *isl_schedule_get_map(__isl_keep isl_schedule *sched)
2295 isl_union_map *umap;
2300 umap = isl_union_map_empty(isl_dim_copy(sched->dim));
2301 for (i = 0; i < sched->n; ++i)
2302 umap = isl_union_map_add_map(umap,
2303 isl_map_copy(sched->node[i].sched));
2308 int isl_schedule_n_band(__isl_keep isl_schedule *sched)
2310 return sched ? sched->n_band : 0;
2313 /* Construct a mapping that maps each domain to the band in its schedule
2314 * with the specified band index. Note that bands with the same index
2315 * but for different domains do not need to be related.
2317 __isl_give isl_union_map *isl_schedule_get_band(__isl_keep isl_schedule *sched,
2321 isl_union_map *umap;
2326 umap = isl_union_map_empty(isl_dim_copy(sched->dim));
2327 for (i = 0; i < sched->n; ++i) {
2331 if (band >= sched->node[i].n_band)
2334 start = band > 0 ? sched->node[i].band_end[band - 1] : 0;
2335 end = sched->node[i].band_end[band];
2337 map = isl_map_copy(sched->node[i].sched);
2339 map = isl_map_project_out(map, isl_dim_out, end,
2340 sched->n_total_row - end);
2341 map = isl_map_project_out(map, isl_dim_out, 0, start);
2343 umap = isl_union_map_add_map(umap, map);