2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_space_private.h>
18 #include <isl/constraint.h>
19 #include <isl/schedule.h>
20 #include <isl_mat_private.h>
24 #include <isl_dim_map.h>
25 #include <isl_hmap_map_basic_set.h>
27 #include <isl_schedule_private.h>
28 #include <isl_band_private.h>
29 #include <isl_options_private.h>
30 #include <isl_tarjan.h>
33 * The scheduling algorithm implemented in this file was inspired by
34 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
35 * Parallelization and Locality Optimization in the Polyhedral Model".
39 /* Internal information about a node that is used during the construction
41 * dim represents the space in which the domain lives
42 * sched is a matrix representation of the schedule being constructed
44 * sched_map is an isl_map representation of the same (partial) schedule
45 * sched_map may be NULL
46 * rank is the number of linearly independent rows in the linear part
48 * the columns of cmap represent a change of basis for the schedule
49 * coefficients; the first rank columns span the linear part of
51 * cinv is the inverse of cmap.
52 * start is the first variable in the LP problem in the sequences that
53 * represents the schedule coefficients of this node
54 * nvar is the dimension of the domain
55 * nparam is the number of parameters or 0 if we are not constructing
56 * a parametric schedule
58 * scc is the index of SCC (or WCC) this node belongs to
60 * band contains the band index for each of the rows of the schedule.
61 * band_id is used to differentiate between separate bands at the same
62 * level within the same parent band, i.e., bands that are separated
63 * by the parent band or bands that are independent of each other.
64 * zero contains a boolean for each of the rows of the schedule,
65 * indicating whether the corresponding scheduling dimension results
66 * in zero dependence distances within its band and with respect
67 * to the proximity edges.
69 struct isl_sched_node
{
87 static int node_has_dim(const void *entry
, const void *val
)
89 struct isl_sched_node
*node
= (struct isl_sched_node
*)entry
;
90 isl_space
*dim
= (isl_space
*)val
;
92 return isl_space_is_equal(node
->dim
, dim
);
95 /* An edge in the dependence graph. An edge may be used to
96 * ensure validity of the generated schedule, to minimize the dependence
99 * map is the dependence relation
100 * src is the source node
101 * dst is the sink node
102 * validity is set if the edge is used to ensure correctness
103 * proximity is set if the edge is used to minimize dependence distances
105 * For validity edges, start and end mark the sequence of inequality
106 * constraints in the LP problem that encode the validity constraint
107 * corresponding to this edge.
109 struct isl_sched_edge
{
112 struct isl_sched_node
*src
;
113 struct isl_sched_node
*dst
;
123 isl_edge_validity
= 0,
124 isl_edge_first
= isl_edge_validity
,
126 isl_edge_last
= isl_edge_proximity
129 /* Internal information about the dependence graph used during
130 * the construction of the schedule.
132 * intra_hmap is a cache, mapping dependence relations to their dual,
133 * for dependences from a node to itself
134 * inter_hmap is a cache, mapping dependence relations to their dual,
135 * for dependences between distinct nodes
137 * n is the number of nodes
138 * node is the list of nodes
139 * maxvar is the maximal number of variables over all nodes
140 * max_row is the allocated number of rows in the schedule
141 * n_row is the current (maximal) number of linearly independent
142 * rows in the node schedules
143 * n_total_row is the current number of rows in the node schedules
144 * n_band is the current number of completed bands
145 * band_start is the starting row in the node schedules of the current band
146 * root is set if this graph is the original dependence graph,
147 * without any splitting
149 * sorted contains a list of node indices sorted according to the
150 * SCC to which a node belongs
152 * n_edge is the number of edges
153 * edge is the list of edges
154 * max_edge contains the maximal number of edges of each type;
155 * in particular, it contains the number of edges in the inital graph.
156 * edge_table contains pointers into the edge array, hashed on the source
157 * and sink spaces; there is one such table for each type;
158 * a given edge may be referenced from more than one table
159 * if the corresponding relation appears in more than of the
160 * sets of dependences
162 * node_table contains pointers into the node array, hashed on the space
164 * region contains a list of variable sequences that should be non-trivial
166 * lp contains the (I)LP problem used to obtain new schedule rows
168 * src_scc and dst_scc are the source and sink SCCs of an edge with
169 * conflicting constraints
171 * scc represents the number of components
173 struct isl_sched_graph
{
174 isl_hmap_map_basic_set
*intra_hmap
;
175 isl_hmap_map_basic_set
*inter_hmap
;
177 struct isl_sched_node
*node
;
191 struct isl_sched_edge
*edge
;
193 int max_edge
[isl_edge_last
+ 1];
194 struct isl_hash_table
*edge_table
[isl_edge_last
+ 1];
196 struct isl_hash_table
*node_table
;
197 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[type].
259 static int graph_edge_table_add(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
260 enum isl_edge_type type
, struct isl_sched_edge
*edge
)
262 struct isl_hash_table_entry
*entry
;
265 hash
= isl_hash_init();
266 hash
= isl_hash_builtin(hash
, edge
->src
);
267 hash
= isl_hash_builtin(hash
, edge
->dst
);
268 entry
= isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
269 &edge_has_src_and_dst
, edge
, 1);
277 /* Allocate the edge_tables based on the maximal number of edges of
280 static int graph_init_edge_tables(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
284 for (i
= 0; i
<= isl_edge_last
; ++i
) {
285 graph
->edge_table
[i
] = isl_hash_table_alloc(ctx
,
287 if (!graph
->edge_table
[i
])
294 /* If graph->edge_table[type] contains an edge from the given source
295 * to the given destination, then return the hash table entry of this edge.
296 * Otherwise, return NULL.
298 static struct isl_hash_table_entry
*graph_find_edge_entry(
299 struct isl_sched_graph
*graph
,
300 enum isl_edge_type type
,
301 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
303 isl_ctx
*ctx
= isl_space_get_ctx(src
->dim
);
305 struct isl_sched_edge temp
= { .src
= src
, .dst
= dst
};
307 hash
= isl_hash_init();
308 hash
= isl_hash_builtin(hash
, temp
.src
);
309 hash
= isl_hash_builtin(hash
, temp
.dst
);
310 return isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
311 &edge_has_src_and_dst
, &temp
, 0);
315 /* If graph->edge_table[type] contains an edge from the given source
316 * to the given destination, then return this edge.
317 * Otherwise, return NULL.
319 static struct isl_sched_edge
*graph_find_edge(struct isl_sched_graph
*graph
,
320 enum isl_edge_type type
,
321 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
323 struct isl_hash_table_entry
*entry
;
325 entry
= graph_find_edge_entry(graph
, type
, src
, dst
);
332 /* Check whether the dependence graph has an edge of the given type
333 * between the given two nodes.
335 static int graph_has_edge(struct isl_sched_graph
*graph
,
336 enum isl_edge_type type
,
337 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
339 struct isl_sched_edge
*edge
;
342 edge
= graph_find_edge(graph
, type
, src
, dst
);
346 empty
= isl_map_plain_is_empty(edge
->map
);
353 /* If there is an edge from the given source to the given destination
354 * of any type then return this edge.
355 * Otherwise, return NULL.
357 static struct isl_sched_edge
*graph_find_any_edge(struct isl_sched_graph
*graph
,
358 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
360 enum isl_edge_type i
;
361 struct isl_sched_edge
*edge
;
363 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
364 edge
= graph_find_edge(graph
, i
, src
, dst
);
372 /* Remove the given edge from all the edge_tables that refer to it.
374 static void graph_remove_edge(struct isl_sched_graph
*graph
,
375 struct isl_sched_edge
*edge
)
377 isl_ctx
*ctx
= isl_map_get_ctx(edge
->map
);
378 enum isl_edge_type i
;
380 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
381 struct isl_hash_table_entry
*entry
;
383 entry
= graph_find_edge_entry(graph
, i
, edge
->src
, edge
->dst
);
386 if (entry
->data
!= edge
)
388 isl_hash_table_remove(ctx
, graph
->edge_table
[i
], entry
);
392 /* Check whether the dependence graph has any edge
393 * between the given two nodes.
395 static int graph_has_any_edge(struct isl_sched_graph
*graph
,
396 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
398 enum isl_edge_type i
;
401 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
402 r
= graph_has_edge(graph
, i
, src
, dst
);
410 /* Check whether the dependence graph has a validity edge
411 * between the given two nodes.
413 static int graph_has_validity_edge(struct isl_sched_graph
*graph
,
414 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
416 return graph_has_edge(graph
, isl_edge_validity
, src
, dst
);
419 static int graph_alloc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
420 int n_node
, int n_edge
)
425 graph
->n_edge
= n_edge
;
426 graph
->node
= isl_calloc_array(ctx
, struct isl_sched_node
, graph
->n
);
427 graph
->sorted
= isl_calloc_array(ctx
, int, graph
->n
);
428 graph
->region
= isl_alloc_array(ctx
, struct isl_region
, graph
->n
);
429 graph
->edge
= isl_calloc_array(ctx
,
430 struct isl_sched_edge
, graph
->n_edge
);
432 graph
->intra_hmap
= isl_hmap_map_basic_set_alloc(ctx
, 2 * n_edge
);
433 graph
->inter_hmap
= isl_hmap_map_basic_set_alloc(ctx
, 2 * n_edge
);
435 if (!graph
->node
|| !graph
->region
|| (graph
->n_edge
&& !graph
->edge
) ||
439 for(i
= 0; i
< graph
->n
; ++i
)
440 graph
->sorted
[i
] = i
;
445 static void graph_free(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
449 isl_hmap_map_basic_set_free(ctx
, graph
->intra_hmap
);
450 isl_hmap_map_basic_set_free(ctx
, graph
->inter_hmap
);
452 for (i
= 0; i
< graph
->n
; ++i
) {
453 isl_space_free(graph
->node
[i
].dim
);
454 isl_mat_free(graph
->node
[i
].sched
);
455 isl_map_free(graph
->node
[i
].sched_map
);
456 isl_mat_free(graph
->node
[i
].cmap
);
457 isl_mat_free(graph
->node
[i
].cinv
);
459 free(graph
->node
[i
].band
);
460 free(graph
->node
[i
].band_id
);
461 free(graph
->node
[i
].zero
);
466 for (i
= 0; i
< graph
->n_edge
; ++i
)
467 isl_map_free(graph
->edge
[i
].map
);
470 for (i
= 0; i
<= isl_edge_last
; ++i
)
471 isl_hash_table_free(ctx
, graph
->edge_table
[i
]);
472 isl_hash_table_free(ctx
, graph
->node_table
);
473 isl_basic_set_free(graph
->lp
);
476 /* For each "set" on which this function is called, increment
477 * graph->n by one and update graph->maxvar.
479 static int init_n_maxvar(__isl_take isl_set
*set
, void *user
)
481 struct isl_sched_graph
*graph
= user
;
482 int nvar
= isl_set_dim(set
, isl_dim_set
);
485 if (nvar
> graph
->maxvar
)
486 graph
->maxvar
= nvar
;
493 /* Compute the number of rows that should be allocated for the schedule.
494 * The graph can be split at most "n - 1" times, there can be at most
495 * two rows for each dimension in the iteration domains (in particular,
496 * we usually have one row, but it may be split by split_scaled),
497 * and there can be one extra row for ordering the statements.
498 * Note that if we have actually split "n - 1" times, then no ordering
499 * is needed, so in principle we could use "graph->n + 2 * graph->maxvar - 1".
501 static int compute_max_row(struct isl_sched_graph
*graph
,
502 __isl_keep isl_union_set
*domain
)
506 if (isl_union_set_foreach_set(domain
, &init_n_maxvar
, graph
) < 0)
508 graph
->max_row
= graph
->n
+ 2 * graph
->maxvar
;
513 /* Add a new node to the graph representing the given set.
515 static int extract_node(__isl_take isl_set
*set
, void *user
)
521 struct isl_sched_graph
*graph
= user
;
522 int *band
, *band_id
, *zero
;
524 ctx
= isl_set_get_ctx(set
);
525 dim
= isl_set_get_space(set
);
527 nvar
= isl_space_dim(dim
, isl_dim_set
);
528 nparam
= isl_space_dim(dim
, isl_dim_param
);
529 if (!ctx
->opt
->schedule_parametric
)
531 sched
= isl_mat_alloc(ctx
, 0, 1 + nparam
+ nvar
);
532 graph
->node
[graph
->n
].dim
= dim
;
533 graph
->node
[graph
->n
].nvar
= nvar
;
534 graph
->node
[graph
->n
].nparam
= nparam
;
535 graph
->node
[graph
->n
].sched
= sched
;
536 graph
->node
[graph
->n
].sched_map
= NULL
;
537 band
= isl_alloc_array(ctx
, int, graph
->max_row
);
538 graph
->node
[graph
->n
].band
= band
;
539 band_id
= isl_calloc_array(ctx
, int, graph
->max_row
);
540 graph
->node
[graph
->n
].band_id
= band_id
;
541 zero
= isl_calloc_array(ctx
, int, graph
->max_row
);
542 graph
->node
[graph
->n
].zero
= zero
;
545 if (!sched
|| (graph
->max_row
&& (!band
|| !band_id
|| !zero
)))
551 struct isl_extract_edge_data
{
552 enum isl_edge_type type
;
553 struct isl_sched_graph
*graph
;
556 /* Add a new edge to the graph based on the given map
557 * and add it to data->graph->edge_table[data->type].
558 * If a dependence relation of a given type happens to be identical
559 * to one of the dependence relations of a type that was added before,
560 * then we don't create a new edge, but instead mark the original edge
561 * as also representing a dependence of the current type.
563 static int extract_edge(__isl_take isl_map
*map
, void *user
)
565 isl_ctx
*ctx
= isl_map_get_ctx(map
);
566 struct isl_extract_edge_data
*data
= user
;
567 struct isl_sched_graph
*graph
= data
->graph
;
568 struct isl_sched_node
*src
, *dst
;
570 struct isl_sched_edge
*edge
;
573 dim
= isl_space_domain(isl_map_get_space(map
));
574 src
= graph_find_node(ctx
, graph
, dim
);
576 dim
= isl_space_range(isl_map_get_space(map
));
577 dst
= graph_find_node(ctx
, graph
, dim
);
585 graph
->edge
[graph
->n_edge
].src
= src
;
586 graph
->edge
[graph
->n_edge
].dst
= dst
;
587 graph
->edge
[graph
->n_edge
].map
= map
;
588 if (data
->type
== isl_edge_validity
) {
589 graph
->edge
[graph
->n_edge
].validity
= 1;
590 graph
->edge
[graph
->n_edge
].proximity
= 0;
592 if (data
->type
== isl_edge_proximity
) {
593 graph
->edge
[graph
->n_edge
].validity
= 0;
594 graph
->edge
[graph
->n_edge
].proximity
= 1;
598 edge
= graph_find_any_edge(graph
, src
, dst
);
600 return graph_edge_table_add(ctx
, graph
, data
->type
,
601 &graph
->edge
[graph
->n_edge
- 1]);
602 is_equal
= isl_map_plain_is_equal(map
, edge
->map
);
606 return graph_edge_table_add(ctx
, graph
, data
->type
,
607 &graph
->edge
[graph
->n_edge
- 1]);
610 edge
->validity
|= graph
->edge
[graph
->n_edge
].validity
;
611 edge
->proximity
|= graph
->edge
[graph
->n_edge
].proximity
;
614 return graph_edge_table_add(ctx
, graph
, data
->type
, edge
);
617 /* Check whether there is any dependence from node[j] to node[i]
618 * or from node[i] to node[j].
620 static int node_follows_weak(int i
, int j
, void *user
)
623 struct isl_sched_graph
*graph
= user
;
625 f
= graph_has_any_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
628 return graph_has_any_edge(graph
, &graph
->node
[i
], &graph
->node
[j
]);
631 /* Check whether there is a validity dependence from node[j] to node[i],
632 * forcing node[i] to follow node[j].
634 static int node_follows_strong(int i
, int j
, void *user
)
636 struct isl_sched_graph
*graph
= user
;
638 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
641 /* Use Tarjan's algorithm for computing the strongly connected components
642 * in the dependence graph (only validity edges).
643 * If weak is set, we consider the graph to be undirected and
644 * we effectively compute the (weakly) connected components.
645 * Additionally, we also consider other edges when weak is set.
647 static int detect_ccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
, int weak
)
650 struct isl_tarjan_graph
*g
= NULL
;
652 g
= isl_tarjan_graph_init(ctx
, graph
->n
,
653 weak
? &node_follows_weak
: &node_follows_strong
, graph
);
661 while (g
->order
[i
] != -1) {
662 graph
->node
[g
->order
[i
]].scc
= graph
->scc
;
670 isl_tarjan_graph_free(g
);
675 /* Apply Tarjan's algorithm to detect the strongly connected components
676 * in the dependence graph.
678 static int detect_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
680 return detect_ccs(ctx
, graph
, 0);
683 /* Apply Tarjan's algorithm to detect the (weakly) connected components
684 * in the dependence graph.
686 static int detect_wccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
688 return detect_ccs(ctx
, graph
, 1);
691 static int cmp_scc(const void *a
, const void *b
, void *data
)
693 struct isl_sched_graph
*graph
= data
;
697 return graph
->node
[*i1
].scc
- graph
->node
[*i2
].scc
;
700 /* Sort the elements of graph->sorted according to the corresponding SCCs.
702 static int sort_sccs(struct isl_sched_graph
*graph
)
704 return isl_sort(graph
->sorted
, graph
->n
, sizeof(int), &cmp_scc
, graph
);
707 /* Given a dependence relation R from a node to itself,
708 * construct the set of coefficients of valid constraints for elements
709 * in that dependence relation.
710 * In particular, the result contains tuples of coefficients
711 * c_0, c_n, c_x such that
713 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
717 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
719 * We choose here to compute the dual of delta R.
720 * Alternatively, we could have computed the dual of R, resulting
721 * in a set of tuples c_0, c_n, c_x, c_y, and then
722 * plugged in (c_0, c_n, c_x, -c_x).
724 static __isl_give isl_basic_set
*intra_coefficients(
725 struct isl_sched_graph
*graph
, __isl_take isl_map
*map
)
727 isl_ctx
*ctx
= isl_map_get_ctx(map
);
731 if (isl_hmap_map_basic_set_has(ctx
, graph
->intra_hmap
, map
))
732 return isl_hmap_map_basic_set_get(ctx
, graph
->intra_hmap
, map
);
734 delta
= isl_set_remove_divs(isl_map_deltas(isl_map_copy(map
)));
735 coef
= isl_set_coefficients(delta
);
736 isl_hmap_map_basic_set_set(ctx
, graph
->intra_hmap
, map
,
737 isl_basic_set_copy(coef
));
742 /* Given a dependence relation R, * construct the set of coefficients
743 * of valid constraints for elements in that dependence relation.
744 * In particular, the result contains tuples of coefficients
745 * c_0, c_n, c_x, c_y such that
747 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
750 static __isl_give isl_basic_set
*inter_coefficients(
751 struct isl_sched_graph
*graph
, __isl_take isl_map
*map
)
753 isl_ctx
*ctx
= isl_map_get_ctx(map
);
757 if (isl_hmap_map_basic_set_has(ctx
, graph
->inter_hmap
, map
))
758 return isl_hmap_map_basic_set_get(ctx
, graph
->inter_hmap
, map
);
760 set
= isl_map_wrap(isl_map_remove_divs(isl_map_copy(map
)));
761 coef
= isl_set_coefficients(set
);
762 isl_hmap_map_basic_set_set(ctx
, graph
->inter_hmap
, map
,
763 isl_basic_set_copy(coef
));
768 /* Add constraints to graph->lp that force validity for the given
769 * dependence from a node i to itself.
770 * That is, add constraints that enforce
772 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
773 * = c_i_x (y - x) >= 0
775 * for each (x,y) in R.
776 * We obtain general constraints on coefficients (c_0, c_n, c_x)
777 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
778 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
779 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
781 * Actually, we do not construct constraints for the c_i_x themselves,
782 * but for the coefficients of c_i_x written as a linear combination
783 * of the columns in node->cmap.
785 static int add_intra_validity_constraints(struct isl_sched_graph
*graph
,
786 struct isl_sched_edge
*edge
)
789 isl_map
*map
= isl_map_copy(edge
->map
);
790 isl_ctx
*ctx
= isl_map_get_ctx(map
);
792 isl_dim_map
*dim_map
;
794 struct isl_sched_node
*node
= edge
->src
;
796 coef
= intra_coefficients(graph
, map
);
798 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
800 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
801 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(node
->cmap
));
805 total
= isl_basic_set_total_dim(graph
->lp
);
806 dim_map
= isl_dim_map_alloc(ctx
, total
);
807 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
808 isl_space_dim(dim
, isl_dim_set
), 1,
810 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
811 isl_space_dim(dim
, isl_dim_set
), 1,
813 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
814 coef
->n_eq
, coef
->n_ineq
);
815 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
825 /* Add constraints to graph->lp that force validity for the given
826 * dependence from node i to node j.
827 * That is, add constraints that enforce
829 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
831 * for each (x,y) in R.
832 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
833 * of valid constraints for R and then plug in
834 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
835 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
836 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
837 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
839 * Actually, we do not construct constraints for the c_*_x themselves,
840 * but for the coefficients of c_*_x written as a linear combination
841 * of the columns in node->cmap.
843 static int add_inter_validity_constraints(struct isl_sched_graph
*graph
,
844 struct isl_sched_edge
*edge
)
847 isl_map
*map
= isl_map_copy(edge
->map
);
848 isl_ctx
*ctx
= isl_map_get_ctx(map
);
850 isl_dim_map
*dim_map
;
852 struct isl_sched_node
*src
= edge
->src
;
853 struct isl_sched_node
*dst
= edge
->dst
;
855 coef
= inter_coefficients(graph
, map
);
857 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
859 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
860 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(src
->cmap
));
861 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
862 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
,
863 isl_mat_copy(dst
->cmap
));
867 total
= isl_basic_set_total_dim(graph
->lp
);
868 dim_map
= isl_dim_map_alloc(ctx
, total
);
870 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, 1);
871 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, -1);
872 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, 1);
873 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
874 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
876 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
877 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
880 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, -1);
881 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, 1);
882 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, -1);
883 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
884 isl_space_dim(dim
, isl_dim_set
), 1,
886 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
887 isl_space_dim(dim
, isl_dim_set
), 1,
890 edge
->start
= graph
->lp
->n_ineq
;
891 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
892 coef
->n_eq
, coef
->n_ineq
);
893 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
898 edge
->end
= graph
->lp
->n_ineq
;
906 /* Add constraints to graph->lp that bound the dependence distance for the given
907 * dependence from a node i to itself.
908 * If s = 1, we add the constraint
910 * c_i_x (y - x) <= m_0 + m_n n
914 * -c_i_x (y - x) + m_0 + m_n n >= 0
916 * for each (x,y) in R.
917 * If s = -1, we add the constraint
919 * -c_i_x (y - x) <= m_0 + m_n n
923 * c_i_x (y - x) + m_0 + m_n n >= 0
925 * for each (x,y) in R.
926 * We obtain general constraints on coefficients (c_0, c_n, c_x)
927 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
928 * with each coefficient (except m_0) represented as a pair of non-negative
931 * Actually, we do not construct constraints for the c_i_x themselves,
932 * but for the coefficients of c_i_x written as a linear combination
933 * of the columns in node->cmap.
935 static int add_intra_proximity_constraints(struct isl_sched_graph
*graph
,
936 struct isl_sched_edge
*edge
, int s
)
940 isl_map
*map
= isl_map_copy(edge
->map
);
941 isl_ctx
*ctx
= isl_map_get_ctx(map
);
943 isl_dim_map
*dim_map
;
945 struct isl_sched_node
*node
= edge
->src
;
947 coef
= intra_coefficients(graph
, map
);
949 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
951 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
952 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(node
->cmap
));
956 nparam
= isl_space_dim(node
->dim
, isl_dim_param
);
957 total
= isl_basic_set_total_dim(graph
->lp
);
958 dim_map
= isl_dim_map_alloc(ctx
, total
);
959 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
960 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
961 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
962 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
963 isl_space_dim(dim
, isl_dim_set
), 1,
965 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
966 isl_space_dim(dim
, isl_dim_set
), 1,
968 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
969 coef
->n_eq
, coef
->n_ineq
);
970 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
980 /* Add constraints to graph->lp that bound the dependence distance for the given
981 * dependence from node i to node j.
982 * If s = 1, we add the constraint
984 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
989 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
992 * for each (x,y) in R.
993 * If s = -1, we add the constraint
995 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1000 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1003 * for each (x,y) in R.
1004 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1005 * of valid constraints for R and then plug in
1006 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1008 * with each coefficient (except m_0, c_j_0 and c_i_0)
1009 * represented as a pair of non-negative coefficients.
1011 * Actually, we do not construct constraints for the c_*_x themselves,
1012 * but for the coefficients of c_*_x written as a linear combination
1013 * of the columns in node->cmap.
1015 static int add_inter_proximity_constraints(struct isl_sched_graph
*graph
,
1016 struct isl_sched_edge
*edge
, int s
)
1020 isl_map
*map
= isl_map_copy(edge
->map
);
1021 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1023 isl_dim_map
*dim_map
;
1024 isl_basic_set
*coef
;
1025 struct isl_sched_node
*src
= edge
->src
;
1026 struct isl_sched_node
*dst
= edge
->dst
;
1028 coef
= inter_coefficients(graph
, map
);
1030 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1032 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1033 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(src
->cmap
));
1034 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1035 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
,
1036 isl_mat_copy(dst
->cmap
));
1040 nparam
= isl_space_dim(src
->dim
, isl_dim_param
);
1041 total
= isl_basic_set_total_dim(graph
->lp
);
1042 dim_map
= isl_dim_map_alloc(ctx
, total
);
1044 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1045 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1046 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1048 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, -s
);
1049 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, s
);
1050 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, -s
);
1051 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
1052 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1054 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
1055 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1058 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, s
);
1059 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, -s
);
1060 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, s
);
1061 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
1062 isl_space_dim(dim
, isl_dim_set
), 1,
1064 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
1065 isl_space_dim(dim
, isl_dim_set
), 1,
1068 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1069 coef
->n_eq
, coef
->n_ineq
);
1070 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1072 isl_space_free(dim
);
1076 isl_space_free(dim
);
1080 static int add_all_validity_constraints(struct isl_sched_graph
*graph
)
1084 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1085 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1086 if (!edge
->validity
)
1088 if (edge
->src
!= edge
->dst
)
1090 if (add_intra_validity_constraints(graph
, edge
) < 0)
1094 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1095 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1096 if (!edge
->validity
)
1098 if (edge
->src
== edge
->dst
)
1100 if (add_inter_validity_constraints(graph
, edge
) < 0)
1107 /* Add constraints to graph->lp that bound the dependence distance
1108 * for all dependence relations.
1109 * If a given proximity dependence is identical to a validity
1110 * dependence, then the dependence distance is already bounded
1111 * from below (by zero), so we only need to bound the distance
1113 * Otherwise, we need to bound the distance both from above and from below.
1115 static int add_all_proximity_constraints(struct isl_sched_graph
*graph
)
1119 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1120 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1121 if (!edge
->proximity
)
1123 if (edge
->src
== edge
->dst
&&
1124 add_intra_proximity_constraints(graph
, edge
, 1) < 0)
1126 if (edge
->src
!= edge
->dst
&&
1127 add_inter_proximity_constraints(graph
, edge
, 1) < 0)
1131 if (edge
->src
== edge
->dst
&&
1132 add_intra_proximity_constraints(graph
, edge
, -1) < 0)
1134 if (edge
->src
!= edge
->dst
&&
1135 add_inter_proximity_constraints(graph
, edge
, -1) < 0)
1142 /* Compute a basis for the rows in the linear part of the schedule
1143 * and extend this basis to a full basis. The remaining rows
1144 * can then be used to force linear independence from the rows
1147 * In particular, given the schedule rows S, we compute
1152 * with H the Hermite normal form of S. That is, all but the
1153 * first rank columns of Q are zero and so each row in S is
1154 * a linear combination of the first rank rows of Q.
1155 * The matrix Q is then transposed because we will write the
1156 * coefficients of the next schedule row as a column vector s
1157 * and express this s as a linear combination s = Q c of the
1159 * Similarly, the matrix U is transposed such that we can
1160 * compute the coefficients c = U s from a schedule row s.
1162 static int node_update_cmap(struct isl_sched_node
*node
)
1165 int n_row
= isl_mat_rows(node
->sched
);
1167 H
= isl_mat_sub_alloc(node
->sched
, 0, n_row
,
1168 1 + node
->nparam
, node
->nvar
);
1170 H
= isl_mat_left_hermite(H
, 0, &U
, &Q
);
1171 isl_mat_free(node
->cmap
);
1172 isl_mat_free(node
->cinv
);
1173 node
->cmap
= isl_mat_transpose(Q
);
1174 node
->cinv
= isl_mat_transpose(U
);
1175 node
->rank
= isl_mat_initial_non_zero_cols(H
);
1178 if (!node
->cmap
|| !node
->cinv
|| node
->rank
< 0)
1183 /* Count the number of equality and inequality constraints
1184 * that will be added for the given map.
1185 * If carry is set, then we are counting the number of (validity)
1186 * constraints that will be added in setup_carry_lp and we count
1187 * each edge exactly once. Otherwise, we count as follows
1188 * validity -> 1 (>= 0)
1189 * validity+proximity -> 2 (>= 0 and upper bound)
1190 * proximity -> 2 (lower and upper bound)
1192 static int count_map_constraints(struct isl_sched_graph
*graph
,
1193 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
,
1194 int *n_eq
, int *n_ineq
, int carry
)
1196 isl_basic_set
*coef
;
1197 int f
= carry
? 1 : edge
->proximity
? 2 : 1;
1199 if (carry
&& !edge
->validity
) {
1204 if (edge
->src
== edge
->dst
)
1205 coef
= intra_coefficients(graph
, map
);
1207 coef
= inter_coefficients(graph
, map
);
1210 *n_eq
+= f
* coef
->n_eq
;
1211 *n_ineq
+= f
* coef
->n_ineq
;
1212 isl_basic_set_free(coef
);
1217 /* Count the number of equality and inequality constraints
1218 * that will be added to the main lp problem.
1219 * We count as follows
1220 * validity -> 1 (>= 0)
1221 * validity+proximity -> 2 (>= 0 and upper bound)
1222 * proximity -> 2 (lower and upper bound)
1224 static int count_constraints(struct isl_sched_graph
*graph
,
1225 int *n_eq
, int *n_ineq
)
1229 *n_eq
= *n_ineq
= 0;
1230 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1231 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1232 isl_map
*map
= isl_map_copy(edge
->map
);
1234 if (count_map_constraints(graph
, edge
, map
,
1235 n_eq
, n_ineq
, 0) < 0)
1242 /* Add constraints that bound the values of the variable and parameter
1243 * coefficients of the schedule.
1245 * The maximal value of the coefficients is defined by the option
1246 * 'schedule_max_coefficient'.
1248 static int add_bound_coefficient_constraints(isl_ctx
*ctx
,
1249 struct isl_sched_graph
*graph
)
1252 int max_coefficient
;
1255 max_coefficient
= ctx
->opt
->schedule_max_coefficient
;
1257 if (max_coefficient
== -1)
1260 total
= isl_basic_set_total_dim(graph
->lp
);
1262 for (i
= 0; i
< graph
->n
; ++i
) {
1263 struct isl_sched_node
*node
= &graph
->node
[i
];
1264 for (j
= 0; j
< 2 * node
->nparam
+ 2 * node
->nvar
; ++j
) {
1266 k
= isl_basic_set_alloc_inequality(graph
->lp
);
1269 dim
= 1 + node
->start
+ 1 + j
;
1270 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
1271 isl_int_set_si(graph
->lp
->ineq
[k
][dim
], -1);
1272 isl_int_set_si(graph
->lp
->ineq
[k
][0], max_coefficient
);
1279 /* Construct an ILP problem for finding schedule coefficients
1280 * that result in non-negative, but small dependence distances
1281 * over all dependences.
1282 * In particular, the dependence distances over proximity edges
1283 * are bounded by m_0 + m_n n and we compute schedule coefficients
1284 * with small values (preferably zero) of m_n and m_0.
1286 * All variables of the ILP are non-negative. The actual coefficients
1287 * may be negative, so each coefficient is represented as the difference
1288 * of two non-negative variables. The negative part always appears
1289 * immediately before the positive part.
1290 * Other than that, the variables have the following order
1292 * - sum of positive and negative parts of m_n coefficients
1294 * - sum of positive and negative parts of all c_n coefficients
1295 * (unconstrained when computing non-parametric schedules)
1296 * - sum of positive and negative parts of all c_x coefficients
1297 * - positive and negative parts of m_n coefficients
1300 * - positive and negative parts of c_i_n (if parametric)
1301 * - positive and negative parts of c_i_x
1303 * The c_i_x are not represented directly, but through the columns of
1304 * node->cmap. That is, the computed values are for variable t_i_x
1305 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
1307 * The constraints are those from the edges plus two or three equalities
1308 * to express the sums.
1310 * If force_zero is set, then we add equalities to ensure that
1311 * the sum of the m_n coefficients and m_0 are both zero.
1313 static int setup_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
1324 int max_constant_term
;
1325 int max_coefficient
;
1327 max_constant_term
= ctx
->opt
->schedule_max_constant_term
;
1328 max_coefficient
= ctx
->opt
->schedule_max_coefficient
;
1330 parametric
= ctx
->opt
->schedule_parametric
;
1331 nparam
= isl_space_dim(graph
->node
[0].dim
, isl_dim_param
);
1333 total
= param_pos
+ 2 * nparam
;
1334 for (i
= 0; i
< graph
->n
; ++i
) {
1335 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
1336 if (node_update_cmap(node
) < 0)
1338 node
->start
= total
;
1339 total
+= 1 + 2 * (node
->nparam
+ node
->nvar
);
1342 if (count_constraints(graph
, &n_eq
, &n_ineq
) < 0)
1345 dim
= isl_space_set_alloc(ctx
, 0, total
);
1346 isl_basic_set_free(graph
->lp
);
1347 n_eq
+= 2 + parametric
+ force_zero
;
1348 if (max_constant_term
!= -1)
1350 if (max_coefficient
!= -1)
1351 for (i
= 0; i
< graph
->n
; ++i
)
1352 n_ineq
+= 2 * graph
->node
[i
].nparam
+
1353 2 * graph
->node
[i
].nvar
;
1355 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
1357 k
= isl_basic_set_alloc_equality(graph
->lp
);
1360 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
1362 isl_int_set_si(graph
->lp
->eq
[k
][1], -1);
1363 for (i
= 0; i
< 2 * nparam
; ++i
)
1364 isl_int_set_si(graph
->lp
->eq
[k
][1 + param_pos
+ i
], 1);
1367 k
= isl_basic_set_alloc_equality(graph
->lp
);
1370 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
1371 isl_int_set_si(graph
->lp
->eq
[k
][2], -1);
1375 k
= isl_basic_set_alloc_equality(graph
->lp
);
1378 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
1379 isl_int_set_si(graph
->lp
->eq
[k
][3], -1);
1380 for (i
= 0; i
< graph
->n
; ++i
) {
1381 int pos
= 1 + graph
->node
[i
].start
+ 1;
1383 for (j
= 0; j
< 2 * graph
->node
[i
].nparam
; ++j
)
1384 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
1388 k
= isl_basic_set_alloc_equality(graph
->lp
);
1391 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
1392 isl_int_set_si(graph
->lp
->eq
[k
][4], -1);
1393 for (i
= 0; i
< graph
->n
; ++i
) {
1394 struct isl_sched_node
*node
= &graph
->node
[i
];
1395 int pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
1397 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
1398 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
1401 if (max_constant_term
!= -1)
1402 for (i
= 0; i
< graph
->n
; ++i
) {
1403 struct isl_sched_node
*node
= &graph
->node
[i
];
1404 k
= isl_basic_set_alloc_inequality(graph
->lp
);
1407 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
1408 isl_int_set_si(graph
->lp
->ineq
[k
][1 + node
->start
], -1);
1409 isl_int_set_si(graph
->lp
->ineq
[k
][0], max_constant_term
);
1412 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
1414 if (add_all_validity_constraints(graph
) < 0)
1416 if (add_all_proximity_constraints(graph
) < 0)
1422 /* Analyze the conflicting constraint found by
1423 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
1424 * constraint of one of the edges between distinct nodes, living, moreover
1425 * in distinct SCCs, then record the source and sink SCC as this may
1426 * be a good place to cut between SCCs.
1428 static int check_conflict(int con
, void *user
)
1431 struct isl_sched_graph
*graph
= user
;
1433 if (graph
->src_scc
>= 0)
1436 con
-= graph
->lp
->n_eq
;
1438 if (con
>= graph
->lp
->n_ineq
)
1441 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1442 if (!graph
->edge
[i
].validity
)
1444 if (graph
->edge
[i
].src
== graph
->edge
[i
].dst
)
1446 if (graph
->edge
[i
].src
->scc
== graph
->edge
[i
].dst
->scc
)
1448 if (graph
->edge
[i
].start
> con
)
1450 if (graph
->edge
[i
].end
<= con
)
1452 graph
->src_scc
= graph
->edge
[i
].src
->scc
;
1453 graph
->dst_scc
= graph
->edge
[i
].dst
->scc
;
1459 /* Check whether the next schedule row of the given node needs to be
1460 * non-trivial. Lower-dimensional domains may have some trivial rows,
1461 * but as soon as the number of remaining required non-trivial rows
1462 * is as large as the number or remaining rows to be computed,
1463 * all remaining rows need to be non-trivial.
1465 static int needs_row(struct isl_sched_graph
*graph
, struct isl_sched_node
*node
)
1467 return node
->nvar
- node
->rank
>= graph
->maxvar
- graph
->n_row
;
1470 /* Solve the ILP problem constructed in setup_lp.
1471 * For each node such that all the remaining rows of its schedule
1472 * need to be non-trivial, we construct a non-triviality region.
1473 * This region imposes that the next row is independent of previous rows.
1474 * In particular the coefficients c_i_x are represented by t_i_x
1475 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
1476 * its first columns span the rows of the previously computed part
1477 * of the schedule. The non-triviality region enforces that at least
1478 * one of the remaining components of t_i_x is non-zero, i.e.,
1479 * that the new schedule row depends on at least one of the remaining
1482 static __isl_give isl_vec
*solve_lp(struct isl_sched_graph
*graph
)
1488 for (i
= 0; i
< graph
->n
; ++i
) {
1489 struct isl_sched_node
*node
= &graph
->node
[i
];
1490 int skip
= node
->rank
;
1491 graph
->region
[i
].pos
= node
->start
+ 1 + 2*(node
->nparam
+skip
);
1492 if (needs_row(graph
, node
))
1493 graph
->region
[i
].len
= 2 * (node
->nvar
- skip
);
1495 graph
->region
[i
].len
= 0;
1497 lp
= isl_basic_set_copy(graph
->lp
);
1498 sol
= isl_tab_basic_set_non_trivial_lexmin(lp
, 2, graph
->n
,
1499 graph
->region
, &check_conflict
, graph
);
1503 /* Update the schedules of all nodes based on the given solution
1504 * of the LP problem.
1505 * The new row is added to the current band.
1506 * All possibly negative coefficients are encoded as a difference
1507 * of two non-negative variables, so we need to perform the subtraction
1508 * here. Moreover, if use_cmap is set, then the solution does
1509 * not refer to the actual coefficients c_i_x, but instead to variables
1510 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
1511 * In this case, we then also need to perform this multiplication
1512 * to obtain the values of c_i_x.
1514 * If check_zero is set, then the first two coordinates of sol are
1515 * assumed to correspond to the dependence distance. If these two
1516 * coordinates are zero, then the corresponding scheduling dimension
1517 * is marked as being zero distance.
1519 static int update_schedule(struct isl_sched_graph
*graph
,
1520 __isl_take isl_vec
*sol
, int use_cmap
, int check_zero
)
1524 isl_vec
*csol
= NULL
;
1529 isl_die(sol
->ctx
, isl_error_internal
,
1530 "no solution found", goto error
);
1531 if (graph
->n_total_row
>= graph
->max_row
)
1532 isl_die(sol
->ctx
, isl_error_internal
,
1533 "too many schedule rows", goto error
);
1536 zero
= isl_int_is_zero(sol
->el
[1]) &&
1537 isl_int_is_zero(sol
->el
[2]);
1539 for (i
= 0; i
< graph
->n
; ++i
) {
1540 struct isl_sched_node
*node
= &graph
->node
[i
];
1541 int pos
= node
->start
;
1542 int row
= isl_mat_rows(node
->sched
);
1545 csol
= isl_vec_alloc(sol
->ctx
, node
->nvar
);
1549 isl_map_free(node
->sched_map
);
1550 node
->sched_map
= NULL
;
1551 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
1554 node
->sched
= isl_mat_set_element(node
->sched
, row
, 0,
1556 for (j
= 0; j
< node
->nparam
+ node
->nvar
; ++j
)
1557 isl_int_sub(sol
->el
[1 + pos
+ 1 + 2 * j
+ 1],
1558 sol
->el
[1 + pos
+ 1 + 2 * j
+ 1],
1559 sol
->el
[1 + pos
+ 1 + 2 * j
]);
1560 for (j
= 0; j
< node
->nparam
; ++j
)
1561 node
->sched
= isl_mat_set_element(node
->sched
,
1562 row
, 1 + j
, sol
->el
[1+pos
+1+2*j
+1]);
1563 for (j
= 0; j
< node
->nvar
; ++j
)
1564 isl_int_set(csol
->el
[j
],
1565 sol
->el
[1+pos
+1+2*(node
->nparam
+j
)+1]);
1567 csol
= isl_mat_vec_product(isl_mat_copy(node
->cmap
),
1571 for (j
= 0; j
< node
->nvar
; ++j
)
1572 node
->sched
= isl_mat_set_element(node
->sched
,
1573 row
, 1 + node
->nparam
+ j
, csol
->el
[j
]);
1574 node
->band
[graph
->n_total_row
] = graph
->n_band
;
1575 node
->zero
[graph
->n_total_row
] = zero
;
1581 graph
->n_total_row
++;
1590 /* Convert node->sched into a multi_aff and return this multi_aff.
1592 static __isl_give isl_multi_aff
*node_extract_schedule_multi_aff(
1593 struct isl_sched_node
*node
)
1597 isl_local_space
*ls
;
1603 nrow
= isl_mat_rows(node
->sched
);
1604 ncol
= isl_mat_cols(node
->sched
) - 1;
1605 space
= isl_space_from_domain(isl_space_copy(node
->dim
));
1606 space
= isl_space_add_dims(space
, isl_dim_out
, nrow
);
1607 ma
= isl_multi_aff_zero(space
);
1608 ls
= isl_local_space_from_space(isl_space_copy(node
->dim
));
1612 for (i
= 0; i
< nrow
; ++i
) {
1613 aff
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
1614 isl_mat_get_element(node
->sched
, i
, 0, &v
);
1615 aff
= isl_aff_set_constant(aff
, v
);
1616 for (j
= 0; j
< node
->nparam
; ++j
) {
1617 isl_mat_get_element(node
->sched
, i
, 1 + j
, &v
);
1618 aff
= isl_aff_set_coefficient(aff
, isl_dim_param
, j
, v
);
1620 for (j
= 0; j
< node
->nvar
; ++j
) {
1621 isl_mat_get_element(node
->sched
,
1622 i
, 1 + node
->nparam
+ j
, &v
);
1623 aff
= isl_aff_set_coefficient(aff
, isl_dim_in
, j
, v
);
1625 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
1630 isl_local_space_free(ls
);
1635 /* Convert node->sched into a map and return this map.
1637 * The result is cached in node->sched_map, which needs to be released
1638 * whenever node->sched is updated.
1640 static __isl_give isl_map
*node_extract_schedule(struct isl_sched_node
*node
)
1642 if (!node
->sched_map
) {
1645 ma
= node_extract_schedule_multi_aff(node
);
1646 node
->sched_map
= isl_map_from_multi_aff(ma
);
1649 return isl_map_copy(node
->sched_map
);
1652 /* Update the given dependence relation based on the current schedule.
1653 * That is, intersect the dependence relation with a map expressing
1654 * that source and sink are executed within the same iteration of
1655 * the current schedule.
1656 * This is not the most efficient way, but this shouldn't be a critical
1659 static __isl_give isl_map
*specialize(__isl_take isl_map
*map
,
1660 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
1662 isl_map
*src_sched
, *dst_sched
, *id
;
1664 src_sched
= node_extract_schedule(src
);
1665 dst_sched
= node_extract_schedule(dst
);
1666 id
= isl_map_apply_range(src_sched
, isl_map_reverse(dst_sched
));
1667 return isl_map_intersect(map
, id
);
1670 /* Update the dependence relations of all edges based on the current schedule.
1671 * If a dependence is carried completely by the current schedule, then
1672 * it is removed from the edge_tables. It is kept in the list of edges
1673 * as otherwise all edge_tables would have to be recomputed.
1675 static int update_edges(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1679 for (i
= graph
->n_edge
- 1; i
>= 0; --i
) {
1680 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1681 edge
->map
= specialize(edge
->map
, edge
->src
, edge
->dst
);
1685 if (isl_map_plain_is_empty(edge
->map
))
1686 graph_remove_edge(graph
, edge
);
1692 static void next_band(struct isl_sched_graph
*graph
)
1694 graph
->band_start
= graph
->n_total_row
;
1698 /* Topologically sort statements mapped to the same schedule iteration
1699 * and add a row to the schedule corresponding to this order.
1701 static int sort_statements(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1708 if (update_edges(ctx
, graph
) < 0)
1711 if (graph
->n_edge
== 0)
1714 if (detect_sccs(ctx
, graph
) < 0)
1717 if (graph
->n_total_row
>= graph
->max_row
)
1718 isl_die(ctx
, isl_error_internal
,
1719 "too many schedule rows", return -1);
1721 for (i
= 0; i
< graph
->n
; ++i
) {
1722 struct isl_sched_node
*node
= &graph
->node
[i
];
1723 int row
= isl_mat_rows(node
->sched
);
1724 int cols
= isl_mat_cols(node
->sched
);
1726 isl_map_free(node
->sched_map
);
1727 node
->sched_map
= NULL
;
1728 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
1731 node
->sched
= isl_mat_set_element_si(node
->sched
, row
, 0,
1733 for (j
= 1; j
< cols
; ++j
)
1734 node
->sched
= isl_mat_set_element_si(node
->sched
,
1736 node
->band
[graph
->n_total_row
] = graph
->n_band
;
1739 graph
->n_total_row
++;
1745 /* Construct an isl_schedule based on the computed schedule stored
1746 * in graph and with parameters specified by dim.
1748 static __isl_give isl_schedule
*extract_schedule(struct isl_sched_graph
*graph
,
1749 __isl_take isl_space
*dim
)
1753 isl_schedule
*sched
= NULL
;
1758 ctx
= isl_space_get_ctx(dim
);
1759 sched
= isl_calloc(ctx
, struct isl_schedule
,
1760 sizeof(struct isl_schedule
) +
1761 (graph
->n
- 1) * sizeof(struct isl_schedule_node
));
1766 sched
->n
= graph
->n
;
1767 sched
->n_band
= graph
->n_band
;
1768 sched
->n_total_row
= graph
->n_total_row
;
1770 for (i
= 0; i
< sched
->n
; ++i
) {
1772 int *band_end
, *band_id
, *zero
;
1774 sched
->node
[i
].sched
=
1775 node_extract_schedule_multi_aff(&graph
->node
[i
]);
1776 if (!sched
->node
[i
].sched
)
1779 sched
->node
[i
].n_band
= graph
->n_band
;
1780 if (graph
->n_band
== 0)
1783 band_end
= isl_alloc_array(ctx
, int, graph
->n_band
);
1784 band_id
= isl_alloc_array(ctx
, int, graph
->n_band
);
1785 zero
= isl_alloc_array(ctx
, int, graph
->n_total_row
);
1786 sched
->node
[i
].band_end
= band_end
;
1787 sched
->node
[i
].band_id
= band_id
;
1788 sched
->node
[i
].zero
= zero
;
1789 if (!band_end
|| !band_id
|| !zero
)
1792 for (r
= 0; r
< graph
->n_total_row
; ++r
)
1793 zero
[r
] = graph
->node
[i
].zero
[r
];
1794 for (r
= b
= 0; r
< graph
->n_total_row
; ++r
) {
1795 if (graph
->node
[i
].band
[r
] == b
)
1798 if (graph
->node
[i
].band
[r
] == -1)
1801 if (r
== graph
->n_total_row
)
1803 sched
->node
[i
].n_band
= b
;
1804 for (--b
; b
>= 0; --b
)
1805 band_id
[b
] = graph
->node
[i
].band_id
[b
];
1812 isl_space_free(dim
);
1813 isl_schedule_free(sched
);
1817 /* Copy nodes that satisfy node_pred from the src dependence graph
1818 * to the dst dependence graph.
1820 static int copy_nodes(struct isl_sched_graph
*dst
, struct isl_sched_graph
*src
,
1821 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
1826 for (i
= 0; i
< src
->n
; ++i
) {
1827 if (!node_pred(&src
->node
[i
], data
))
1829 dst
->node
[dst
->n
].dim
= isl_space_copy(src
->node
[i
].dim
);
1830 dst
->node
[dst
->n
].nvar
= src
->node
[i
].nvar
;
1831 dst
->node
[dst
->n
].nparam
= src
->node
[i
].nparam
;
1832 dst
->node
[dst
->n
].sched
= isl_mat_copy(src
->node
[i
].sched
);
1833 dst
->node
[dst
->n
].sched_map
=
1834 isl_map_copy(src
->node
[i
].sched_map
);
1835 dst
->node
[dst
->n
].band
= src
->node
[i
].band
;
1836 dst
->node
[dst
->n
].band_id
= src
->node
[i
].band_id
;
1837 dst
->node
[dst
->n
].zero
= src
->node
[i
].zero
;
1844 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
1845 * to the dst dependence graph.
1846 * If the source or destination node of the edge is not in the destination
1847 * graph, then it must be a backward proximity edge and it should simply
1850 static int copy_edges(isl_ctx
*ctx
, struct isl_sched_graph
*dst
,
1851 struct isl_sched_graph
*src
,
1852 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
), int data
)
1855 enum isl_edge_type t
;
1858 for (i
= 0; i
< src
->n_edge
; ++i
) {
1859 struct isl_sched_edge
*edge
= &src
->edge
[i
];
1861 struct isl_sched_node
*dst_src
, *dst_dst
;
1863 if (!edge_pred(edge
, data
))
1866 if (isl_map_plain_is_empty(edge
->map
))
1869 dst_src
= graph_find_node(ctx
, dst
, edge
->src
->dim
);
1870 dst_dst
= graph_find_node(ctx
, dst
, edge
->dst
->dim
);
1871 if (!dst_src
|| !dst_dst
) {
1873 isl_die(ctx
, isl_error_internal
,
1874 "backward validity edge", return -1);
1878 map
= isl_map_copy(edge
->map
);
1880 dst
->edge
[dst
->n_edge
].src
= dst_src
;
1881 dst
->edge
[dst
->n_edge
].dst
= dst_dst
;
1882 dst
->edge
[dst
->n_edge
].map
= map
;
1883 dst
->edge
[dst
->n_edge
].validity
= edge
->validity
;
1884 dst
->edge
[dst
->n_edge
].proximity
= edge
->proximity
;
1887 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
1889 graph_find_edge(src
, t
, edge
->src
, edge
->dst
))
1891 if (graph_edge_table_add(ctx
, dst
, t
,
1892 &dst
->edge
[dst
->n_edge
- 1]) < 0)
1900 /* Given a "src" dependence graph that contains the nodes from "dst"
1901 * that satisfy node_pred, copy the schedule computed in "src"
1902 * for those nodes back to "dst".
1904 static int copy_schedule(struct isl_sched_graph
*dst
,
1905 struct isl_sched_graph
*src
,
1906 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
1911 for (i
= 0; i
< dst
->n
; ++i
) {
1912 if (!node_pred(&dst
->node
[i
], data
))
1914 isl_mat_free(dst
->node
[i
].sched
);
1915 isl_map_free(dst
->node
[i
].sched_map
);
1916 dst
->node
[i
].sched
= isl_mat_copy(src
->node
[src
->n
].sched
);
1917 dst
->node
[i
].sched_map
=
1918 isl_map_copy(src
->node
[src
->n
].sched_map
);
1922 dst
->max_row
= src
->max_row
;
1923 dst
->n_total_row
= src
->n_total_row
;
1924 dst
->n_band
= src
->n_band
;
1929 /* Compute the maximal number of variables over all nodes.
1930 * This is the maximal number of linearly independent schedule
1931 * rows that we need to compute.
1932 * Just in case we end up in a part of the dependence graph
1933 * with only lower-dimensional domains, we make sure we will
1934 * compute the required amount of extra linearly independent rows.
1936 static int compute_maxvar(struct isl_sched_graph
*graph
)
1941 for (i
= 0; i
< graph
->n
; ++i
) {
1942 struct isl_sched_node
*node
= &graph
->node
[i
];
1945 if (node_update_cmap(node
) < 0)
1947 nvar
= node
->nvar
+ graph
->n_row
- node
->rank
;
1948 if (nvar
> graph
->maxvar
)
1949 graph
->maxvar
= nvar
;
1955 static int compute_schedule(isl_ctx
*ctx
, struct isl_sched_graph
*graph
);
1956 static int compute_schedule_wcc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
);
1958 /* Compute a schedule for a subgraph of "graph". In particular, for
1959 * the graph composed of nodes that satisfy node_pred and edges that
1960 * that satisfy edge_pred. The caller should precompute the number
1961 * of nodes and edges that satisfy these predicates and pass them along
1962 * as "n" and "n_edge".
1963 * If the subgraph is known to consist of a single component, then wcc should
1964 * be set and then we call compute_schedule_wcc on the constructed subgraph.
1965 * Otherwise, we call compute_schedule, which will check whether the subgraph
1968 static int compute_sub_schedule(isl_ctx
*ctx
,
1969 struct isl_sched_graph
*graph
, int n
, int n_edge
,
1970 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
1971 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
1974 struct isl_sched_graph split
= { 0 };
1977 if (graph_alloc(ctx
, &split
, n
, n_edge
) < 0)
1979 if (copy_nodes(&split
, graph
, node_pred
, data
) < 0)
1981 if (graph_init_table(ctx
, &split
) < 0)
1983 for (t
= 0; t
<= isl_edge_last
; ++t
)
1984 split
.max_edge
[t
] = graph
->max_edge
[t
];
1985 if (graph_init_edge_tables(ctx
, &split
) < 0)
1987 if (copy_edges(ctx
, &split
, graph
, edge_pred
, data
) < 0)
1989 split
.n_row
= graph
->n_row
;
1990 split
.max_row
= graph
->max_row
;
1991 split
.n_total_row
= graph
->n_total_row
;
1992 split
.n_band
= graph
->n_band
;
1993 split
.band_start
= graph
->band_start
;
1995 if (wcc
&& compute_schedule_wcc(ctx
, &split
) < 0)
1997 if (!wcc
&& compute_schedule(ctx
, &split
) < 0)
2000 copy_schedule(graph
, &split
, node_pred
, data
);
2002 graph_free(ctx
, &split
);
2005 graph_free(ctx
, &split
);
2009 static int node_scc_exactly(struct isl_sched_node
*node
, int scc
)
2011 return node
->scc
== scc
;
2014 static int node_scc_at_most(struct isl_sched_node
*node
, int scc
)
2016 return node
->scc
<= scc
;
2019 static int node_scc_at_least(struct isl_sched_node
*node
, int scc
)
2021 return node
->scc
>= scc
;
2024 static int edge_scc_exactly(struct isl_sched_edge
*edge
, int scc
)
2026 return edge
->src
->scc
== scc
&& edge
->dst
->scc
== scc
;
2029 static int edge_dst_scc_at_most(struct isl_sched_edge
*edge
, int scc
)
2031 return edge
->dst
->scc
<= scc
;
2034 static int edge_src_scc_at_least(struct isl_sched_edge
*edge
, int scc
)
2036 return edge
->src
->scc
>= scc
;
2039 /* Pad the schedules of all nodes with zero rows such that in the end
2040 * they all have graph->n_total_row rows.
2041 * The extra rows don't belong to any band, so they get assigned band number -1.
2043 static int pad_schedule(struct isl_sched_graph
*graph
)
2047 for (i
= 0; i
< graph
->n
; ++i
) {
2048 struct isl_sched_node
*node
= &graph
->node
[i
];
2049 int row
= isl_mat_rows(node
->sched
);
2050 if (graph
->n_total_row
> row
) {
2051 isl_map_free(node
->sched_map
);
2052 node
->sched_map
= NULL
;
2054 node
->sched
= isl_mat_add_zero_rows(node
->sched
,
2055 graph
->n_total_row
- row
);
2058 for (j
= row
; j
< graph
->n_total_row
; ++j
)
2065 /* Split the current graph into two parts and compute a schedule for each
2066 * part individually. In particular, one part consists of all SCCs up
2067 * to and including graph->src_scc, while the other part contains the other
2070 * The split is enforced in the schedule by constant rows with two different
2071 * values (0 and 1). These constant rows replace the previously computed rows
2072 * in the current band.
2073 * It would be possible to reuse them as the first rows in the next
2074 * band, but recomputing them may result in better rows as we are looking
2075 * at a smaller part of the dependence graph.
2076 * compute_split_schedule is only called when no zero-distance schedule row
2077 * could be found on the entire graph, so we wark the splitting row as
2078 * non zero-distance.
2080 * The band_id of the second group is set to n, where n is the number
2081 * of nodes in the first group. This ensures that the band_ids over
2082 * the two groups remain disjoint, even if either or both of the two
2083 * groups contain independent components.
2085 static int compute_split_schedule(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2087 int i
, j
, n
, e1
, e2
;
2088 int n_total_row
, orig_total_row
;
2089 int n_band
, orig_band
;
2092 if (graph
->n_total_row
>= graph
->max_row
)
2093 isl_die(ctx
, isl_error_internal
,
2094 "too many schedule rows", return -1);
2096 drop
= graph
->n_total_row
- graph
->band_start
;
2097 graph
->n_total_row
-= drop
;
2098 graph
->n_row
-= drop
;
2101 for (i
= 0; i
< graph
->n
; ++i
) {
2102 struct isl_sched_node
*node
= &graph
->node
[i
];
2103 int row
= isl_mat_rows(node
->sched
) - drop
;
2104 int cols
= isl_mat_cols(node
->sched
);
2105 int before
= node
->scc
<= graph
->src_scc
;
2110 isl_map_free(node
->sched_map
);
2111 node
->sched_map
= NULL
;
2112 node
->sched
= isl_mat_drop_rows(node
->sched
,
2113 graph
->band_start
, drop
);
2114 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
2117 node
->sched
= isl_mat_set_element_si(node
->sched
, row
, 0,
2119 for (j
= 1; j
< cols
; ++j
)
2120 node
->sched
= isl_mat_set_element_si(node
->sched
,
2122 node
->band
[graph
->n_total_row
] = graph
->n_band
;
2123 node
->zero
[graph
->n_total_row
] = 0;
2127 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2128 if (graph
->edge
[i
].dst
->scc
<= graph
->src_scc
)
2130 if (graph
->edge
[i
].src
->scc
> graph
->src_scc
)
2134 graph
->n_total_row
++;
2137 for (i
= 0; i
< graph
->n
; ++i
) {
2138 struct isl_sched_node
*node
= &graph
->node
[i
];
2139 if (node
->scc
> graph
->src_scc
)
2140 node
->band_id
[graph
->n_band
] = n
;
2143 orig_total_row
= graph
->n_total_row
;
2144 orig_band
= graph
->n_band
;
2145 if (compute_sub_schedule(ctx
, graph
, n
, e1
,
2146 &node_scc_at_most
, &edge_dst_scc_at_most
,
2147 graph
->src_scc
, 0) < 0)
2149 n_total_row
= graph
->n_total_row
;
2150 graph
->n_total_row
= orig_total_row
;
2151 n_band
= graph
->n_band
;
2152 graph
->n_band
= orig_band
;
2153 if (compute_sub_schedule(ctx
, graph
, graph
->n
- n
, e2
,
2154 &node_scc_at_least
, &edge_src_scc_at_least
,
2155 graph
->src_scc
+ 1, 0) < 0)
2157 if (n_total_row
> graph
->n_total_row
)
2158 graph
->n_total_row
= n_total_row
;
2159 if (n_band
> graph
->n_band
)
2160 graph
->n_band
= n_band
;
2162 return pad_schedule(graph
);
2165 /* Compute the next band of the schedule after updating the dependence
2166 * relations based on the the current schedule.
2168 static int compute_next_band(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2170 if (update_edges(ctx
, graph
) < 0)
2174 return compute_schedule(ctx
, graph
);
2177 /* Add constraints to graph->lp that force the dependence "map" (which
2178 * is part of the dependence relation of "edge")
2179 * to be respected and attempt to carry it, where the edge is one from
2180 * a node j to itself. "pos" is the sequence number of the given map.
2181 * That is, add constraints that enforce
2183 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
2184 * = c_j_x (y - x) >= e_i
2186 * for each (x,y) in R.
2187 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2188 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
2189 * with each coefficient in c_j_x represented as a pair of non-negative
2192 static int add_intra_constraints(struct isl_sched_graph
*graph
,
2193 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
2196 isl_ctx
*ctx
= isl_map_get_ctx(map
);
2198 isl_dim_map
*dim_map
;
2199 isl_basic_set
*coef
;
2200 struct isl_sched_node
*node
= edge
->src
;
2202 coef
= intra_coefficients(graph
, map
);
2206 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
2208 total
= isl_basic_set_total_dim(graph
->lp
);
2209 dim_map
= isl_dim_map_alloc(ctx
, total
);
2210 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
2211 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
2212 isl_space_dim(dim
, isl_dim_set
), 1,
2214 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
2215 isl_space_dim(dim
, isl_dim_set
), 1,
2217 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
2218 coef
->n_eq
, coef
->n_ineq
);
2219 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
2221 isl_space_free(dim
);
2226 /* Add constraints to graph->lp that force the dependence "map" (which
2227 * is part of the dependence relation of "edge")
2228 * to be respected and attempt to carry it, where the edge is one from
2229 * node j to node k. "pos" is the sequence number of the given map.
2230 * That is, add constraints that enforce
2232 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
2234 * for each (x,y) in R.
2235 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2236 * of valid constraints for R and then plug in
2237 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
2238 * with each coefficient (except e_i, c_k_0 and c_j_0)
2239 * represented as a pair of non-negative coefficients.
2241 static int add_inter_constraints(struct isl_sched_graph
*graph
,
2242 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
2245 isl_ctx
*ctx
= isl_map_get_ctx(map
);
2247 isl_dim_map
*dim_map
;
2248 isl_basic_set
*coef
;
2249 struct isl_sched_node
*src
= edge
->src
;
2250 struct isl_sched_node
*dst
= edge
->dst
;
2252 coef
= inter_coefficients(graph
, map
);
2256 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
2258 total
= isl_basic_set_total_dim(graph
->lp
);
2259 dim_map
= isl_dim_map_alloc(ctx
, total
);
2261 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
2263 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, 1);
2264 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, -1);
2265 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, 1);
2266 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
2267 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
2269 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
2270 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
2273 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, -1);
2274 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, 1);
2275 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, -1);
2276 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
2277 isl_space_dim(dim
, isl_dim_set
), 1,
2279 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
2280 isl_space_dim(dim
, isl_dim_set
), 1,
2283 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
2284 coef
->n_eq
, coef
->n_ineq
);
2285 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
2287 isl_space_free(dim
);
2292 /* Add constraints to graph->lp that force all validity dependences
2293 * to be respected and attempt to carry them.
2295 static int add_all_constraints(struct isl_sched_graph
*graph
)
2301 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2302 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2304 if (!edge
->validity
)
2307 for (j
= 0; j
< edge
->map
->n
; ++j
) {
2308 isl_basic_map
*bmap
;
2311 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
2312 map
= isl_map_from_basic_map(bmap
);
2314 if (edge
->src
== edge
->dst
&&
2315 add_intra_constraints(graph
, edge
, map
, pos
) < 0)
2317 if (edge
->src
!= edge
->dst
&&
2318 add_inter_constraints(graph
, edge
, map
, pos
) < 0)
2327 /* Count the number of equality and inequality constraints
2328 * that will be added to the carry_lp problem.
2329 * We count each edge exactly once.
2331 static int count_all_constraints(struct isl_sched_graph
*graph
,
2332 int *n_eq
, int *n_ineq
)
2336 *n_eq
= *n_ineq
= 0;
2337 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2338 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2339 for (j
= 0; j
< edge
->map
->n
; ++j
) {
2340 isl_basic_map
*bmap
;
2343 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
2344 map
= isl_map_from_basic_map(bmap
);
2346 if (count_map_constraints(graph
, edge
, map
,
2347 n_eq
, n_ineq
, 1) < 0)
2355 /* Construct an LP problem for finding schedule coefficients
2356 * such that the schedule carries as many dependences as possible.
2357 * In particular, for each dependence i, we bound the dependence distance
2358 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
2359 * of all e_i's. Dependence with e_i = 0 in the solution are simply
2360 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
2361 * Note that if the dependence relation is a union of basic maps,
2362 * then we have to consider each basic map individually as it may only
2363 * be possible to carry the dependences expressed by some of those
2364 * basic maps and not all off them.
2365 * Below, we consider each of those basic maps as a separate "edge".
2367 * All variables of the LP are non-negative. The actual coefficients
2368 * may be negative, so each coefficient is represented as the difference
2369 * of two non-negative variables. The negative part always appears
2370 * immediately before the positive part.
2371 * Other than that, the variables have the following order
2373 * - sum of (1 - e_i) over all edges
2374 * - sum of positive and negative parts of all c_n coefficients
2375 * (unconstrained when computing non-parametric schedules)
2376 * - sum of positive and negative parts of all c_x coefficients
2381 * - positive and negative parts of c_i_n (if parametric)
2382 * - positive and negative parts of c_i_x
2384 * The constraints are those from the (validity) edges plus three equalities
2385 * to express the sums and n_edge inequalities to express e_i <= 1.
2387 static int setup_carry_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2397 for (i
= 0; i
< graph
->n_edge
; ++i
)
2398 n_edge
+= graph
->edge
[i
].map
->n
;
2401 for (i
= 0; i
< graph
->n
; ++i
) {
2402 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
2403 node
->start
= total
;
2404 total
+= 1 + 2 * (node
->nparam
+ node
->nvar
);
2407 if (count_all_constraints(graph
, &n_eq
, &n_ineq
) < 0)
2410 dim
= isl_space_set_alloc(ctx
, 0, total
);
2411 isl_basic_set_free(graph
->lp
);
2414 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
2415 graph
->lp
= isl_basic_set_set_rational(graph
->lp
);
2417 k
= isl_basic_set_alloc_equality(graph
->lp
);
2420 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2421 isl_int_set_si(graph
->lp
->eq
[k
][0], -n_edge
);
2422 isl_int_set_si(graph
->lp
->eq
[k
][1], 1);
2423 for (i
= 0; i
< n_edge
; ++i
)
2424 isl_int_set_si(graph
->lp
->eq
[k
][4 + i
], 1);
2426 k
= isl_basic_set_alloc_equality(graph
->lp
);
2429 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2430 isl_int_set_si(graph
->lp
->eq
[k
][2], -1);
2431 for (i
= 0; i
< graph
->n
; ++i
) {
2432 int pos
= 1 + graph
->node
[i
].start
+ 1;
2434 for (j
= 0; j
< 2 * graph
->node
[i
].nparam
; ++j
)
2435 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2438 k
= isl_basic_set_alloc_equality(graph
->lp
);
2441 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2442 isl_int_set_si(graph
->lp
->eq
[k
][3], -1);
2443 for (i
= 0; i
< graph
->n
; ++i
) {
2444 struct isl_sched_node
*node
= &graph
->node
[i
];
2445 int pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
2447 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
2448 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2451 for (i
= 0; i
< n_edge
; ++i
) {
2452 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2455 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2456 isl_int_set_si(graph
->lp
->ineq
[k
][4 + i
], -1);
2457 isl_int_set_si(graph
->lp
->ineq
[k
][0], 1);
2460 if (add_all_constraints(graph
) < 0)
2466 /* If the schedule_split_scaled option is set and if the linear
2467 * parts of the scheduling rows for all nodes in the graphs have
2468 * non-trivial common divisor, then split off the constant term
2469 * from the linear part.
2470 * The constant term is then placed in a separate band and
2471 * the linear part is reduced.
2473 static int split_scaled(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2479 if (!ctx
->opt
->schedule_split_scaled
)
2484 if (graph
->n_total_row
>= graph
->max_row
)
2485 isl_die(ctx
, isl_error_internal
,
2486 "too many schedule rows", return -1);
2489 isl_int_init(gcd_i
);
2491 isl_int_set_si(gcd
, 0);
2493 row
= isl_mat_rows(graph
->node
[0].sched
) - 1;
2495 for (i
= 0; i
< graph
->n
; ++i
) {
2496 struct isl_sched_node
*node
= &graph
->node
[i
];
2497 int cols
= isl_mat_cols(node
->sched
);
2499 isl_seq_gcd(node
->sched
->row
[row
] + 1, cols
- 1, &gcd_i
);
2500 isl_int_gcd(gcd
, gcd
, gcd_i
);
2503 isl_int_clear(gcd_i
);
2505 if (isl_int_cmp_si(gcd
, 1) <= 0) {
2512 for (i
= 0; i
< graph
->n
; ++i
) {
2513 struct isl_sched_node
*node
= &graph
->node
[i
];
2515 isl_map_free(node
->sched_map
);
2516 node
->sched_map
= NULL
;
2517 node
->sched
= isl_mat_add_zero_rows(node
->sched
, 1);
2520 isl_int_fdiv_r(node
->sched
->row
[row
+ 1][0],
2521 node
->sched
->row
[row
][0], gcd
);
2522 isl_int_fdiv_q(node
->sched
->row
[row
][0],
2523 node
->sched
->row
[row
][0], gcd
);
2524 isl_int_mul(node
->sched
->row
[row
][0],
2525 node
->sched
->row
[row
][0], gcd
);
2526 node
->sched
= isl_mat_scale_down_row(node
->sched
, row
, gcd
);
2529 node
->band
[graph
->n_total_row
] = graph
->n_band
;
2532 graph
->n_total_row
++;
2541 static int compute_component_schedule(isl_ctx
*ctx
,
2542 struct isl_sched_graph
*graph
);
2544 /* Is the schedule row "sol" trivial on node "node"?
2545 * That is, is the solution zero on the dimensions orthogonal to
2546 * the previously found solutions?
2547 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
2549 * Each coefficient is represented as the difference between
2550 * two non-negative values in "sol". "sol" has been computed
2551 * in terms of the original iterators (i.e., without use of cmap).
2552 * We construct the schedule row s and write it as a linear
2553 * combination of (linear combinations of) previously computed schedule rows.
2554 * s = Q c or c = U s.
2555 * If the final entries of c are all zero, then the solution is trivial.
2557 static int is_trivial(struct isl_sched_node
*node
, __isl_keep isl_vec
*sol
)
2567 if (node
->nvar
== node
->rank
)
2570 ctx
= isl_vec_get_ctx(sol
);
2571 node_sol
= isl_vec_alloc(ctx
, node
->nvar
);
2575 pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
2577 for (i
= 0; i
< node
->nvar
; ++i
)
2578 isl_int_sub(node_sol
->el
[i
],
2579 sol
->el
[pos
+ 2 * i
+ 1], sol
->el
[pos
+ 2 * i
]);
2581 node_sol
= isl_mat_vec_product(isl_mat_copy(node
->cinv
), node_sol
);
2586 trivial
= isl_seq_first_non_zero(node_sol
->el
+ node
->rank
,
2587 node
->nvar
- node
->rank
) == -1;
2589 isl_vec_free(node_sol
);
2594 /* Is the schedule row "sol" trivial on any node where it should
2596 * "sol" has been computed in terms of the original iterators
2597 * (i.e., without use of cmap).
2598 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
2600 static int is_any_trivial(struct isl_sched_graph
*graph
,
2601 __isl_keep isl_vec
*sol
)
2605 for (i
= 0; i
< graph
->n
; ++i
) {
2606 struct isl_sched_node
*node
= &graph
->node
[i
];
2609 if (!needs_row(graph
, node
))
2611 trivial
= is_trivial(node
, sol
);
2612 if (trivial
< 0 || trivial
)
2619 /* Construct a schedule row for each node such that as many dependences
2620 * as possible are carried and then continue with the next band.
2622 * If the computed schedule row turns out to be trivial on one or
2623 * more nodes where it should not be trivial, then we throw it away
2624 * and try again on each component separately.
2626 static int carry_dependences(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2635 for (i
= 0; i
< graph
->n_edge
; ++i
)
2636 n_edge
+= graph
->edge
[i
].map
->n
;
2638 if (setup_carry_lp(ctx
, graph
) < 0)
2641 lp
= isl_basic_set_copy(graph
->lp
);
2642 sol
= isl_tab_basic_set_non_neg_lexmin(lp
);
2646 if (sol
->size
== 0) {
2648 isl_die(ctx
, isl_error_internal
,
2649 "error in schedule construction", return -1);
2652 isl_int_divexact(sol
->el
[1], sol
->el
[1], sol
->el
[0]);
2653 if (isl_int_cmp_si(sol
->el
[1], n_edge
) >= 0) {
2655 isl_die(ctx
, isl_error_unknown
,
2656 "unable to carry dependences", return -1);
2659 trivial
= is_any_trivial(graph
, sol
);
2661 sol
= isl_vec_free(sol
);
2662 } else if (trivial
) {
2665 return compute_component_schedule(ctx
, graph
);
2666 isl_die(ctx
, isl_error_unknown
,
2667 "unable to construct non-trivial solution", return -1);
2670 if (update_schedule(graph
, sol
, 0, 0) < 0)
2673 if (split_scaled(ctx
, graph
) < 0)
2676 return compute_next_band(ctx
, graph
);
2679 /* Are there any (non-empty) validity edges in the graph?
2681 static int has_validity_edges(struct isl_sched_graph
*graph
)
2685 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2688 empty
= isl_map_plain_is_empty(graph
->edge
[i
].map
);
2693 if (graph
->edge
[i
].validity
)
2700 /* Should we apply a Feautrier step?
2701 * That is, did the user request the Feautrier algorithm and are
2702 * there any validity dependences (left)?
2704 static int need_feautrier_step(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2706 if (ctx
->opt
->schedule_algorithm
!= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
)
2709 return has_validity_edges(graph
);
2712 /* Compute a schedule for a connected dependence graph using Feautrier's
2713 * multi-dimensional scheduling algorithm.
2714 * The original algorithm is described in [1].
2715 * The main idea is to minimize the number of scheduling dimensions, by
2716 * trying to satisfy as many dependences as possible per scheduling dimension.
2718 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
2719 * Problem, Part II: Multi-Dimensional Time.
2720 * In Intl. Journal of Parallel Programming, 1992.
2722 static int compute_schedule_wcc_feautrier(isl_ctx
*ctx
,
2723 struct isl_sched_graph
*graph
)
2725 return carry_dependences(ctx
, graph
);
2728 /* Compute a schedule for a connected dependence graph.
2729 * We try to find a sequence of as many schedule rows as possible that result
2730 * in non-negative dependence distances (independent of the previous rows
2731 * in the sequence, i.e., such that the sequence is tilable).
2732 * If we can't find any more rows we either
2733 * - split between SCCs and start over (assuming we found an interesting
2734 * pair of SCCs between which to split)
2735 * - continue with the next band (assuming the current band has at least
2737 * - try to carry as many dependences as possible and continue with the next
2740 * If Feautrier's algorithm is selected, we first recursively try to satisfy
2741 * as many validity dependences as possible. When all validity dependences
2742 * are satisfied we extend the schedule to a full-dimensional schedule.
2744 * If we manage to complete the schedule, we finish off by topologically
2745 * sorting the statements based on the remaining dependences.
2747 * If ctx->opt->schedule_outer_zero_distance is set, then we force the
2748 * outermost dimension in the current band to be zero distance. If this
2749 * turns out to be impossible, we fall back on the general scheme above
2750 * and try to carry as many dependences as possible.
2752 static int compute_schedule_wcc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2756 if (detect_sccs(ctx
, graph
) < 0)
2758 if (sort_sccs(graph
) < 0)
2761 if (compute_maxvar(graph
) < 0)
2764 if (need_feautrier_step(ctx
, graph
))
2765 return compute_schedule_wcc_feautrier(ctx
, graph
);
2767 if (ctx
->opt
->schedule_outer_zero_distance
)
2770 while (graph
->n_row
< graph
->maxvar
) {
2773 graph
->src_scc
= -1;
2774 graph
->dst_scc
= -1;
2776 if (setup_lp(ctx
, graph
, force_zero
) < 0)
2778 sol
= solve_lp(graph
);
2781 if (sol
->size
== 0) {
2783 if (!ctx
->opt
->schedule_maximize_band_depth
&&
2784 graph
->n_total_row
> graph
->band_start
)
2785 return compute_next_band(ctx
, graph
);
2786 if (graph
->src_scc
>= 0)
2787 return compute_split_schedule(ctx
, graph
);
2788 if (graph
->n_total_row
> graph
->band_start
)
2789 return compute_next_band(ctx
, graph
);
2790 return carry_dependences(ctx
, graph
);
2792 if (update_schedule(graph
, sol
, 1, 1) < 0)
2797 if (graph
->n_total_row
> graph
->band_start
)
2799 return sort_statements(ctx
, graph
);
2802 /* Add a row to the schedules that separates the SCCs and move
2805 static int split_on_scc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2809 if (graph
->n_total_row
>= graph
->max_row
)
2810 isl_die(ctx
, isl_error_internal
,
2811 "too many schedule rows", return -1);
2813 for (i
= 0; i
< graph
->n
; ++i
) {
2814 struct isl_sched_node
*node
= &graph
->node
[i
];
2815 int row
= isl_mat_rows(node
->sched
);
2817 isl_map_free(node
->sched_map
);
2818 node
->sched_map
= NULL
;
2819 node
->sched
= isl_mat_add_zero_rows(node
->sched
, 1);
2820 node
->sched
= isl_mat_set_element_si(node
->sched
, row
, 0,
2824 node
->band
[graph
->n_total_row
] = graph
->n_band
;
2827 graph
->n_total_row
++;
2833 /* Compute a schedule for each component (identified by node->scc)
2834 * of the dependence graph separately and then combine the results.
2835 * Depending on the setting of schedule_fuse, a component may be
2836 * either weakly or strongly connected.
2838 * The band_id is adjusted such that each component has a separate id.
2839 * Note that the band_id may have already been set to a value different
2840 * from zero by compute_split_schedule.
2842 static int compute_component_schedule(isl_ctx
*ctx
,
2843 struct isl_sched_graph
*graph
)
2847 int n_total_row
, orig_total_row
;
2848 int n_band
, orig_band
;
2850 if (ctx
->opt
->schedule_fuse
== ISL_SCHEDULE_FUSE_MIN
||
2851 ctx
->opt
->schedule_separate_components
)
2852 if (split_on_scc(ctx
, graph
) < 0)
2856 orig_total_row
= graph
->n_total_row
;
2858 orig_band
= graph
->n_band
;
2859 for (i
= 0; i
< graph
->n
; ++i
)
2860 graph
->node
[i
].band_id
[graph
->n_band
] += graph
->node
[i
].scc
;
2861 for (wcc
= 0; wcc
< graph
->scc
; ++wcc
) {
2863 for (i
= 0; i
< graph
->n
; ++i
)
2864 if (graph
->node
[i
].scc
== wcc
)
2867 for (i
= 0; i
< graph
->n_edge
; ++i
)
2868 if (graph
->edge
[i
].src
->scc
== wcc
&&
2869 graph
->edge
[i
].dst
->scc
== wcc
)
2872 if (compute_sub_schedule(ctx
, graph
, n
, n_edge
,
2874 &edge_scc_exactly
, wcc
, 1) < 0)
2876 if (graph
->n_total_row
> n_total_row
)
2877 n_total_row
= graph
->n_total_row
;
2878 graph
->n_total_row
= orig_total_row
;
2879 if (graph
->n_band
> n_band
)
2880 n_band
= graph
->n_band
;
2881 graph
->n_band
= orig_band
;
2884 graph
->n_total_row
= n_total_row
;
2885 graph
->n_band
= n_band
;
2887 return pad_schedule(graph
);
2890 /* Compute a schedule for the given dependence graph.
2891 * We first check if the graph is connected (through validity dependences)
2892 * and, if not, compute a schedule for each component separately.
2893 * If schedule_fuse is set to minimal fusion, then we check for strongly
2894 * connected components instead and compute a separate schedule for
2895 * each such strongly connected component.
2897 static int compute_schedule(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2899 if (ctx
->opt
->schedule_fuse
== ISL_SCHEDULE_FUSE_MIN
) {
2900 if (detect_sccs(ctx
, graph
) < 0)
2903 if (detect_wccs(ctx
, graph
) < 0)
2908 return compute_component_schedule(ctx
, graph
);
2910 return compute_schedule_wcc(ctx
, graph
);
2913 /* Compute a schedule for the given union of domains that respects
2914 * all the validity dependences.
2915 * If the default isl scheduling algorithm is used, it tries to minimize
2916 * the dependence distances over the proximity dependences.
2917 * If Feautrier's scheduling algorithm is used, the proximity dependence
2918 * distances are only minimized during the extension to a full-dimensional
2921 __isl_give isl_schedule
*isl_union_set_compute_schedule(
2922 __isl_take isl_union_set
*domain
,
2923 __isl_take isl_union_map
*validity
,
2924 __isl_take isl_union_map
*proximity
)
2926 isl_ctx
*ctx
= isl_union_set_get_ctx(domain
);
2928 struct isl_sched_graph graph
= { 0 };
2929 isl_schedule
*sched
;
2930 struct isl_extract_edge_data data
;
2932 domain
= isl_union_set_align_params(domain
,
2933 isl_union_map_get_space(validity
));
2934 domain
= isl_union_set_align_params(domain
,
2935 isl_union_map_get_space(proximity
));
2936 dim
= isl_union_set_get_space(domain
);
2937 validity
= isl_union_map_align_params(validity
, isl_space_copy(dim
));
2938 proximity
= isl_union_map_align_params(proximity
, dim
);
2943 graph
.n
= isl_union_set_n_set(domain
);
2946 if (graph_alloc(ctx
, &graph
, graph
.n
,
2947 isl_union_map_n_map(validity
) + isl_union_map_n_map(proximity
)) < 0)
2949 if (compute_max_row(&graph
, domain
) < 0)
2953 if (isl_union_set_foreach_set(domain
, &extract_node
, &graph
) < 0)
2955 if (graph_init_table(ctx
, &graph
) < 0)
2957 graph
.max_edge
[isl_edge_validity
] = isl_union_map_n_map(validity
);
2958 graph
.max_edge
[isl_edge_proximity
] = isl_union_map_n_map(proximity
);
2959 if (graph_init_edge_tables(ctx
, &graph
) < 0)
2962 data
.graph
= &graph
;
2963 data
.type
= isl_edge_validity
;
2964 if (isl_union_map_foreach_map(validity
, &extract_edge
, &data
) < 0)
2966 data
.type
= isl_edge_proximity
;
2967 if (isl_union_map_foreach_map(proximity
, &extract_edge
, &data
) < 0)
2970 if (compute_schedule(ctx
, &graph
) < 0)
2974 sched
= extract_schedule(&graph
, isl_union_set_get_space(domain
));
2976 graph_free(ctx
, &graph
);
2977 isl_union_set_free(domain
);
2978 isl_union_map_free(validity
);
2979 isl_union_map_free(proximity
);
2983 graph_free(ctx
, &graph
);
2984 isl_union_set_free(domain
);
2985 isl_union_map_free(validity
);
2986 isl_union_map_free(proximity
);
2990 void *isl_schedule_free(__isl_take isl_schedule
*sched
)
2996 if (--sched
->ref
> 0)
2999 for (i
= 0; i
< sched
->n
; ++i
) {
3000 isl_multi_aff_free(sched
->node
[i
].sched
);
3001 free(sched
->node
[i
].band_end
);
3002 free(sched
->node
[i
].band_id
);
3003 free(sched
->node
[i
].zero
);
3005 isl_space_free(sched
->dim
);
3006 isl_band_list_free(sched
->band_forest
);
3011 isl_ctx
*isl_schedule_get_ctx(__isl_keep isl_schedule
*schedule
)
3013 return schedule
? isl_space_get_ctx(schedule
->dim
) : NULL
;
3016 /* Set max_out to the maximal number of output dimensions over
3019 static int update_max_out(__isl_take isl_map
*map
, void *user
)
3021 int *max_out
= user
;
3022 int n_out
= isl_map_dim(map
, isl_dim_out
);
3024 if (n_out
> *max_out
)
3031 /* Internal data structure for map_pad_range.
3033 * "max_out" is the maximal schedule dimension.
3034 * "res" collects the results.
3036 struct isl_pad_schedule_map_data
{
3041 /* Pad the range of the given map with zeros to data->max_out and
3042 * then add the result to data->res.
3044 static int map_pad_range(__isl_take isl_map
*map
, void *user
)
3046 struct isl_pad_schedule_map_data
*data
= user
;
3048 int n_out
= isl_map_dim(map
, isl_dim_out
);
3050 map
= isl_map_add_dims(map
, isl_dim_out
, data
->max_out
- n_out
);
3051 for (i
= n_out
; i
< data
->max_out
; ++i
)
3052 map
= isl_map_fix_si(map
, isl_dim_out
, i
, 0);
3054 data
->res
= isl_union_map_add_map(data
->res
, map
);
3061 /* Pad the ranges of the maps in the union map with zeros such they all have
3062 * the same dimension.
3064 static __isl_give isl_union_map
*pad_schedule_map(
3065 __isl_take isl_union_map
*umap
)
3067 struct isl_pad_schedule_map_data data
;
3071 if (isl_union_map_n_map(umap
) <= 1)
3075 if (isl_union_map_foreach_map(umap
, &update_max_out
, &data
.max_out
) < 0)
3076 return isl_union_map_free(umap
);
3078 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3079 if (isl_union_map_foreach_map(umap
, &map_pad_range
, &data
) < 0)
3080 data
.res
= isl_union_map_free(data
.res
);
3082 isl_union_map_free(umap
);
3086 /* Return an isl_union_map of the schedule. If we have already constructed
3087 * a band forest, then this band forest may have been modified so we need
3088 * to extract the isl_union_map from the forest rather than from
3089 * the originally computed schedule. This reconstructed schedule map
3090 * then needs to be padded with zeros to unify the schedule space
3091 * since the result of isl_band_list_get_suffix_schedule may not have
3092 * a unified schedule space.
3094 __isl_give isl_union_map
*isl_schedule_get_map(__isl_keep isl_schedule
*sched
)
3097 isl_union_map
*umap
;
3102 if (sched
->band_forest
) {
3103 umap
= isl_band_list_get_suffix_schedule(sched
->band_forest
);
3104 return pad_schedule_map(umap
);
3107 umap
= isl_union_map_empty(isl_space_copy(sched
->dim
));
3108 for (i
= 0; i
< sched
->n
; ++i
) {
3111 ma
= isl_multi_aff_copy(sched
->node
[i
].sched
);
3112 umap
= isl_union_map_add_map(umap
, isl_map_from_multi_aff(ma
));
3118 static __isl_give isl_band_list
*construct_band_list(
3119 __isl_keep isl_schedule
*schedule
, __isl_keep isl_band
*parent
,
3120 int band_nr
, int *parent_active
, int n_active
);
3122 /* Construct an isl_band structure for the band in the given schedule
3123 * with sequence number band_nr for the n_active nodes marked by active.
3124 * If the nodes don't have a band with the given sequence number,
3125 * then a band without members is created.
3127 * Because of the way the schedule is constructed, we know that
3128 * the position of the band inside the schedule of a node is the same
3129 * for all active nodes.
3131 * The partial schedule for the band is created before the children
3132 * are created to that construct_band_list can refer to the partial
3133 * schedule of the parent.
3135 static __isl_give isl_band
*construct_band(__isl_keep isl_schedule
*schedule
,
3136 __isl_keep isl_band
*parent
,
3137 int band_nr
, int *active
, int n_active
)
3140 isl_ctx
*ctx
= isl_schedule_get_ctx(schedule
);
3142 unsigned start
, end
;
3144 band
= isl_band_alloc(ctx
);
3148 band
->schedule
= schedule
;
3149 band
->parent
= parent
;
3151 for (i
= 0; i
< schedule
->n
; ++i
)
3155 if (i
>= schedule
->n
)
3156 isl_die(ctx
, isl_error_internal
,
3157 "band without active statements", goto error
);
3159 start
= band_nr
? schedule
->node
[i
].band_end
[band_nr
- 1] : 0;
3160 end
= band_nr
< schedule
->node
[i
].n_band
?
3161 schedule
->node
[i
].band_end
[band_nr
] : start
;
3162 band
->n
= end
- start
;
3164 band
->zero
= isl_alloc_array(ctx
, int, band
->n
);
3165 if (band
->n
&& !band
->zero
)
3168 for (j
= 0; j
< band
->n
; ++j
)
3169 band
->zero
[j
] = schedule
->node
[i
].zero
[start
+ j
];
3171 band
->pma
= isl_union_pw_multi_aff_empty(isl_space_copy(schedule
->dim
));
3172 for (i
= 0; i
< schedule
->n
; ++i
) {
3174 isl_pw_multi_aff
*pma
;
3180 ma
= isl_multi_aff_copy(schedule
->node
[i
].sched
);
3181 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
3182 ma
= isl_multi_aff_drop_dims(ma
, isl_dim_out
, end
, n_out
- end
);
3183 ma
= isl_multi_aff_drop_dims(ma
, isl_dim_out
, 0, start
);
3184 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
3185 band
->pma
= isl_union_pw_multi_aff_add_pw_multi_aff(band
->pma
,
3191 for (i
= 0; i
< schedule
->n
; ++i
)
3192 if (active
[i
] && schedule
->node
[i
].n_band
> band_nr
+ 1)
3195 if (i
< schedule
->n
) {
3196 band
->children
= construct_band_list(schedule
, band
,
3197 band_nr
+ 1, active
, n_active
);
3198 if (!band
->children
)
3204 isl_band_free(band
);
3208 /* Internal data structure used inside cmp_band and pw_multi_aff_extract_int.
3210 * r is set to a negative value if anything goes wrong.
3212 * c1 stores the result of extract_int.
3213 * c2 is a temporary value used inside cmp_band_in_ancestor.
3214 * t is a temporary value used inside extract_int.
3216 * first and equal are used inside extract_int.
3217 * first is set if we are looking at the first isl_multi_aff inside
3218 * the isl_union_pw_multi_aff.
3219 * equal is set if all the isl_multi_affs have been equal so far.
3221 struct isl_cmp_band_data
{
3232 /* Check if "ma" assigns a constant value.
3233 * Note that this function is only called on isl_multi_affs
3234 * with a single output dimension.
3236 * If "ma" assigns a constant value then we compare it to data->c1
3237 * or assign it to data->c1 if this is the first isl_multi_aff we consider.
3238 * If "ma" does not assign a constant value or if it assigns a value
3239 * that is different from data->c1, then we set data->equal to zero
3240 * and terminate the check.
3242 static int multi_aff_extract_int(__isl_take isl_set
*set
,
3243 __isl_take isl_multi_aff
*ma
, void *user
)
3246 struct isl_cmp_band_data
*data
= user
;
3248 aff
= isl_multi_aff_get_aff(ma
, 0);
3249 data
->r
= isl_aff_is_cst(aff
);
3250 if (data
->r
>= 0 && data
->r
) {
3251 isl_aff_get_constant(aff
, &data
->t
);
3253 isl_int_set(data
->c1
, data
->t
);
3255 } else if (!isl_int_eq(data
->c1
, data
->t
))
3257 } else if (data
->r
>= 0 && !data
->r
)
3262 isl_multi_aff_free(ma
);
3271 /* This function is called for each isl_pw_multi_aff in
3272 * the isl_union_pw_multi_aff checked by extract_int.
3273 * Check all the isl_multi_affs inside "pma".
3275 static int pw_multi_aff_extract_int(__isl_take isl_pw_multi_aff
*pma
,
3280 r
= isl_pw_multi_aff_foreach_piece(pma
, &multi_aff_extract_int
, user
);
3281 isl_pw_multi_aff_free(pma
);
3286 /* Check if "upma" assigns a single constant value to its domain.
3287 * If so, return 1 and store the result in data->c1.
3290 * A negative return value from isl_union_pw_multi_aff_foreach_pw_multi_aff
3291 * means that either an error occurred or that we have broken off the check
3292 * because we already know the result is going to be negative.
3293 * In the latter case, data->equal is set to zero.
3295 static int extract_int(__isl_keep isl_union_pw_multi_aff
*upma
,
3296 struct isl_cmp_band_data
*data
)
3301 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
3302 &pw_multi_aff_extract_int
, data
) < 0) {
3308 return !data
->first
&& data
->equal
;
3311 /* Compare "b1" and "b2" based on the parent schedule of their ancestor
3314 * If the parent of "ancestor" also has a single member, then we
3315 * first try to compare the two band based on the partial schedule
3318 * Otherwise, or if the result is inconclusive, we look at the partial schedule
3319 * of "ancestor" itself.
3320 * In particular, we specialize the parent schedule based
3321 * on the domains of the child schedules, check if both assign
3322 * a single constant value and, if so, compare the two constant values.
3323 * If the specialized parent schedules do not assign a constant value,
3324 * then they cannot be used to order the two bands and so in this case
3327 static int cmp_band_in_ancestor(__isl_keep isl_band
*b1
,
3328 __isl_keep isl_band
*b2
, struct isl_cmp_band_data
*data
,
3329 __isl_keep isl_band
*ancestor
)
3331 isl_union_pw_multi_aff
*upma
;
3332 isl_union_set
*domain
;
3338 if (ancestor
->parent
&& ancestor
->parent
->n
== 1) {
3339 r
= cmp_band_in_ancestor(b1
, b2
, data
, ancestor
->parent
);
3346 upma
= isl_union_pw_multi_aff_copy(b1
->pma
);
3347 domain
= isl_union_pw_multi_aff_domain(upma
);
3348 upma
= isl_union_pw_multi_aff_copy(ancestor
->pma
);
3349 upma
= isl_union_pw_multi_aff_intersect_domain(upma
, domain
);
3350 r
= extract_int(upma
, data
);
3351 isl_union_pw_multi_aff_free(upma
);
3358 isl_int_set(data
->c2
, data
->c1
);
3360 upma
= isl_union_pw_multi_aff_copy(b2
->pma
);
3361 domain
= isl_union_pw_multi_aff_domain(upma
);
3362 upma
= isl_union_pw_multi_aff_copy(ancestor
->pma
);
3363 upma
= isl_union_pw_multi_aff_intersect_domain(upma
, domain
);
3364 r
= extract_int(upma
, data
);
3365 isl_union_pw_multi_aff_free(upma
);
3372 return isl_int_cmp(data
->c2
, data
->c1
);
3375 /* Compare "a" and "b" based on the parent schedule of their parent.
3377 static int cmp_band(const void *a
, const void *b
, void *user
)
3379 isl_band
*b1
= *(isl_band
* const *) a
;
3380 isl_band
*b2
= *(isl_band
* const *) b
;
3381 struct isl_cmp_band_data
*data
= user
;
3383 return cmp_band_in_ancestor(b1
, b2
, data
, b1
->parent
);
3386 /* Sort the elements in "list" based on the partial schedules of its parent
3387 * (and ancestors). In particular if the parent assigns constant values
3388 * to the domains of the bands in "list", then the elements are sorted
3389 * according to that order.
3390 * This order should be a more "natural" order for the user, but otherwise
3391 * shouldn't have any effect.
3392 * If we would be constructing an isl_band forest directly in
3393 * isl_union_set_compute_schedule then there wouldn't be any need
3394 * for a reordering, since the children would be added to the list
3395 * in their natural order automatically.
3397 * If there is only one element in the list, then there is no need to sort
3399 * If the partial schedule of the parent has more than one member
3400 * (or if there is no parent), then it's
3401 * defnitely not assigning constant values to the different children in
3402 * the list and so we wouldn't be able to use it to sort the list.
3404 static __isl_give isl_band_list
*sort_band_list(__isl_take isl_band_list
*list
,
3405 __isl_keep isl_band
*parent
)
3407 struct isl_cmp_band_data data
;
3413 if (!parent
|| parent
->n
!= 1)
3417 isl_int_init(data
.c1
);
3418 isl_int_init(data
.c2
);
3419 isl_int_init(data
.t
);
3420 isl_sort(list
->p
, list
->n
, sizeof(list
->p
[0]), &cmp_band
, &data
);
3422 list
= isl_band_list_free(list
);
3423 isl_int_clear(data
.c1
);
3424 isl_int_clear(data
.c2
);
3425 isl_int_clear(data
.t
);
3430 /* Construct a list of bands that start at the same position (with
3431 * sequence number band_nr) in the schedules of the nodes that
3432 * were active in the parent band.
3434 * A separate isl_band structure is created for each band_id
3435 * and for each node that does not have a band with sequence
3436 * number band_nr. In the latter case, a band without members
3438 * This ensures that if a band has any children, then each node
3439 * that was active in the band is active in exactly one of the children.
3441 static __isl_give isl_band_list
*construct_band_list(
3442 __isl_keep isl_schedule
*schedule
, __isl_keep isl_band
*parent
,
3443 int band_nr
, int *parent_active
, int n_active
)
3446 isl_ctx
*ctx
= isl_schedule_get_ctx(schedule
);
3449 isl_band_list
*list
;
3452 for (i
= 0; i
< n_active
; ++i
) {
3453 for (j
= 0; j
< schedule
->n
; ++j
) {
3454 if (!parent_active
[j
])
3456 if (schedule
->node
[j
].n_band
<= band_nr
)
3458 if (schedule
->node
[j
].band_id
[band_nr
] == i
) {
3464 for (j
= 0; j
< schedule
->n
; ++j
)
3465 if (schedule
->node
[j
].n_band
<= band_nr
)
3470 list
= isl_band_list_alloc(ctx
, n_band
);
3471 band
= construct_band(schedule
, parent
, band_nr
,
3472 parent_active
, n_active
);
3473 return isl_band_list_add(list
, band
);
3476 active
= isl_alloc_array(ctx
, int, schedule
->n
);
3477 if (schedule
->n
&& !active
)
3480 list
= isl_band_list_alloc(ctx
, n_band
);
3482 for (i
= 0; i
< n_active
; ++i
) {
3486 for (j
= 0; j
< schedule
->n
; ++j
) {
3487 active
[j
] = parent_active
[j
] &&
3488 schedule
->node
[j
].n_band
> band_nr
&&
3489 schedule
->node
[j
].band_id
[band_nr
] == i
;
3496 band
= construct_band(schedule
, parent
, band_nr
, active
, n
);
3498 list
= isl_band_list_add(list
, band
);
3500 for (i
= 0; i
< schedule
->n
; ++i
) {
3502 if (!parent_active
[i
])
3504 if (schedule
->node
[i
].n_band
> band_nr
)
3506 for (j
= 0; j
< schedule
->n
; ++j
)
3508 band
= construct_band(schedule
, parent
, band_nr
, active
, 1);
3509 list
= isl_band_list_add(list
, band
);
3514 list
= sort_band_list(list
, parent
);
3519 /* Construct a band forest representation of the schedule and
3520 * return the list of roots.
3522 static __isl_give isl_band_list
*construct_forest(
3523 __isl_keep isl_schedule
*schedule
)
3526 isl_ctx
*ctx
= isl_schedule_get_ctx(schedule
);
3527 isl_band_list
*forest
;
3530 active
= isl_alloc_array(ctx
, int, schedule
->n
);
3531 if (schedule
->n
&& !active
)
3534 for (i
= 0; i
< schedule
->n
; ++i
)
3537 forest
= construct_band_list(schedule
, NULL
, 0, active
, schedule
->n
);
3544 /* Return the roots of a band forest representation of the schedule.
3546 __isl_give isl_band_list
*isl_schedule_get_band_forest(
3547 __isl_keep isl_schedule
*schedule
)
3551 if (!schedule
->band_forest
)
3552 schedule
->band_forest
= construct_forest(schedule
);
3553 return isl_band_list_dup(schedule
->band_forest
);
3556 /* Call "fn" on each band in the schedule in depth-first post-order.
3558 int isl_schedule_foreach_band(__isl_keep isl_schedule
*sched
,
3559 int (*fn
)(__isl_keep isl_band
*band
, void *user
), void *user
)
3562 isl_band_list
*forest
;
3567 forest
= isl_schedule_get_band_forest(sched
);
3568 r
= isl_band_list_foreach_band(forest
, fn
, user
);
3569 isl_band_list_free(forest
);
3574 static __isl_give isl_printer
*print_band_list(__isl_take isl_printer
*p
,
3575 __isl_keep isl_band_list
*list
);
3577 static __isl_give isl_printer
*print_band(__isl_take isl_printer
*p
,
3578 __isl_keep isl_band
*band
)
3580 isl_band_list
*children
;
3582 p
= isl_printer_start_line(p
);
3583 p
= isl_printer_print_union_pw_multi_aff(p
, band
->pma
);
3584 p
= isl_printer_end_line(p
);
3586 if (!isl_band_has_children(band
))
3589 children
= isl_band_get_children(band
);
3591 p
= isl_printer_indent(p
, 4);
3592 p
= print_band_list(p
, children
);
3593 p
= isl_printer_indent(p
, -4);
3595 isl_band_list_free(children
);
3600 static __isl_give isl_printer
*print_band_list(__isl_take isl_printer
*p
,
3601 __isl_keep isl_band_list
*list
)
3605 n
= isl_band_list_n_band(list
);
3606 for (i
= 0; i
< n
; ++i
) {
3608 band
= isl_band_list_get_band(list
, i
);
3609 p
= print_band(p
, band
);
3610 isl_band_free(band
);
3616 __isl_give isl_printer
*isl_printer_print_schedule(__isl_take isl_printer
*p
,
3617 __isl_keep isl_schedule
*schedule
)
3619 isl_band_list
*forest
;
3621 forest
= isl_schedule_get_band_forest(schedule
);
3623 p
= print_band_list(p
, forest
);
3625 isl_band_list_free(forest
);
3630 void isl_schedule_dump(__isl_keep isl_schedule
*schedule
)
3632 isl_printer
*printer
;
3637 printer
= isl_printer_to_file(isl_schedule_get_ctx(schedule
), stderr
);
3638 printer
= isl_printer_print_schedule(printer
, schedule
);
3640 isl_printer_free(printer
);