2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2014 Ecole Normale Superieure
4 * Copyright 2015-2016 Sven Verdoolaege
5 * Copyright 2016 INRIA Paris
6 * Copyright 2017 Sven Verdoolaege
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
11 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
13 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
14 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
15 * CS 42112, 75589 Paris Cedex 12, France
18 #include <isl_ctx_private.h>
19 #include <isl_map_private.h>
20 #include <isl_space_private.h>
21 #include <isl_aff_private.h>
23 #include <isl/constraint.h>
24 #include <isl/schedule.h>
25 #include <isl_schedule_constraints.h>
26 #include <isl/schedule_node.h>
27 #include <isl_mat_private.h>
28 #include <isl_vec_private.h>
30 #include <isl/union_set.h>
33 #include <isl_dim_map.h>
34 #include <isl/map_to_basic_set.h>
36 #include <isl_options_private.h>
37 #include <isl_tarjan.h>
38 #include <isl_morph.h>
40 #include <isl_val_private.h>
43 * The scheduling algorithm implemented in this file was inspired by
44 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
45 * Parallelization and Locality Optimization in the Polyhedral Model".
49 /* Internal information about a node that is used during the construction
51 * space represents the original space in which the domain lives;
52 * that is, the space is not affected by compression
53 * sched is a matrix representation of the schedule being constructed
54 * for this node; if compressed is set, then this schedule is
55 * defined over the compressed domain space
56 * sched_map is an isl_map representation of the same (partial) schedule
57 * sched_map may be NULL; if compressed is set, then this map
58 * is defined over the uncompressed domain space
59 * rank is the number of linearly independent rows in the linear part
61 * the rows of "vmap" represent a change of basis for the node
62 * variables; the first rank rows span the linear part of
63 * the schedule rows; the remaining rows are linearly independent
64 * the rows of "indep" represent linear combinations of the schedule
65 * coefficients that are non-zero when the schedule coefficients are
66 * linearly independent of previously computed schedule rows.
67 * start is the first variable in the LP problem in the sequences that
68 * represents the schedule coefficients of this node
69 * nvar is the dimension of the domain
70 * nparam is the number of parameters or 0 if we are not constructing
71 * a parametric schedule
73 * If compressed is set, then hull represents the constraints
74 * that were used to derive the compression, while compress and
75 * decompress map the original space to the compressed space and
78 * scc is the index of SCC (or WCC) this node belongs to
80 * "cluster" is only used inside extract_clusters and identifies
81 * the cluster of SCCs that the node belongs to.
83 * coincident contains a boolean for each of the rows of the schedule,
84 * indicating whether the corresponding scheduling dimension satisfies
85 * the coincidence constraints in the sense that the corresponding
86 * dependence distances are zero.
88 * If the schedule_treat_coalescing option is set, then
89 * "sizes" contains the sizes of the (compressed) instance set
90 * in each direction. If there is no fixed size in a given direction,
91 * then the corresponding size value is set to infinity.
92 * If the schedule_treat_coalescing option or the schedule_max_coefficient
93 * option is set, then "max" contains the maximal values for
94 * schedule coefficients of the (compressed) variables. If no bound
95 * needs to be imposed on a particular variable, then the corresponding
98 struct isl_sched_node
{
102 isl_multi_aff
*compress
;
103 isl_multi_aff
*decompress
;
118 isl_multi_val
*sizes
;
122 static int node_has_tuples(const void *entry
, const void *val
)
124 struct isl_sched_node
*node
= (struct isl_sched_node
*)entry
;
125 isl_space
*space
= (isl_space
*) val
;
127 return isl_space_has_equal_tuples(node
->space
, space
);
130 static int node_scc_exactly(struct isl_sched_node
*node
, int scc
)
132 return node
->scc
== scc
;
135 static int node_scc_at_most(struct isl_sched_node
*node
, int scc
)
137 return node
->scc
<= scc
;
140 static int node_scc_at_least(struct isl_sched_node
*node
, int scc
)
142 return node
->scc
>= scc
;
145 /* An edge in the dependence graph. An edge may be used to
146 * ensure validity of the generated schedule, to minimize the dependence
149 * map is the dependence relation, with i -> j in the map if j depends on i
150 * tagged_condition and tagged_validity contain the union of all tagged
151 * condition or conditional validity dependence relations that
152 * specialize the dependence relation "map"; that is,
153 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
154 * or "tagged_validity", then i -> j is an element of "map".
155 * If these fields are NULL, then they represent the empty relation.
156 * src is the source node
157 * dst is the sink node
159 * types is a bit vector containing the types of this edge.
160 * validity is set if the edge is used to ensure correctness
161 * coincidence is used to enforce zero dependence distances
162 * proximity is set if the edge is used to minimize dependence distances
163 * condition is set if the edge represents a condition
164 * for a conditional validity schedule constraint
165 * local can only be set for condition edges and indicates that
166 * the dependence distance over the edge should be zero
167 * conditional_validity is set if the edge is used to conditionally
170 * For validity edges, start and end mark the sequence of inequality
171 * constraints in the LP problem that encode the validity constraint
172 * corresponding to this edge.
174 * During clustering, an edge may be marked "no_merge" if it should
175 * not be used to merge clusters.
176 * The weight is also only used during clustering and it is
177 * an indication of how many schedule dimensions on either side
178 * of the schedule constraints can be aligned.
179 * If the weight is negative, then this means that this edge was postponed
180 * by has_bounded_distances or any_no_merge. The original weight can
181 * be retrieved by adding 1 + graph->max_weight, with "graph"
182 * the graph containing this edge.
184 struct isl_sched_edge
{
186 isl_union_map
*tagged_condition
;
187 isl_union_map
*tagged_validity
;
189 struct isl_sched_node
*src
;
190 struct isl_sched_node
*dst
;
201 /* Is "edge" marked as being of type "type"?
203 static int is_type(struct isl_sched_edge
*edge
, enum isl_edge_type type
)
205 return ISL_FL_ISSET(edge
->types
, 1 << type
);
208 /* Mark "edge" as being of type "type".
210 static void set_type(struct isl_sched_edge
*edge
, enum isl_edge_type type
)
212 ISL_FL_SET(edge
->types
, 1 << type
);
215 /* No longer mark "edge" as being of type "type"?
217 static void clear_type(struct isl_sched_edge
*edge
, enum isl_edge_type type
)
219 ISL_FL_CLR(edge
->types
, 1 << type
);
222 /* Is "edge" marked as a validity edge?
224 static int is_validity(struct isl_sched_edge
*edge
)
226 return is_type(edge
, isl_edge_validity
);
229 /* Mark "edge" as a validity edge.
231 static void set_validity(struct isl_sched_edge
*edge
)
233 set_type(edge
, isl_edge_validity
);
236 /* Is "edge" marked as a proximity edge?
238 static int is_proximity(struct isl_sched_edge
*edge
)
240 return is_type(edge
, isl_edge_proximity
);
243 /* Is "edge" marked as a local edge?
245 static int is_local(struct isl_sched_edge
*edge
)
247 return is_type(edge
, isl_edge_local
);
250 /* Mark "edge" as a local edge.
252 static void set_local(struct isl_sched_edge
*edge
)
254 set_type(edge
, isl_edge_local
);
257 /* No longer mark "edge" as a local edge.
259 static void clear_local(struct isl_sched_edge
*edge
)
261 clear_type(edge
, isl_edge_local
);
264 /* Is "edge" marked as a coincidence edge?
266 static int is_coincidence(struct isl_sched_edge
*edge
)
268 return is_type(edge
, isl_edge_coincidence
);
271 /* Is "edge" marked as a condition edge?
273 static int is_condition(struct isl_sched_edge
*edge
)
275 return is_type(edge
, isl_edge_condition
);
278 /* Is "edge" marked as a conditional validity edge?
280 static int is_conditional_validity(struct isl_sched_edge
*edge
)
282 return is_type(edge
, isl_edge_conditional_validity
);
285 /* Internal information about the dependence graph used during
286 * the construction of the schedule.
288 * intra_hmap is a cache, mapping dependence relations to their dual,
289 * for dependences from a node to itself
290 * inter_hmap is a cache, mapping dependence relations to their dual,
291 * for dependences between distinct nodes
292 * if compression is involved then the key for these maps
293 * is the original, uncompressed dependence relation, while
294 * the value is the dual of the compressed dependence relation.
296 * n is the number of nodes
297 * node is the list of nodes
298 * maxvar is the maximal number of variables over all nodes
299 * max_row is the allocated number of rows in the schedule
300 * n_row is the current (maximal) number of linearly independent
301 * rows in the node schedules
302 * n_total_row is the current number of rows in the node schedules
303 * band_start is the starting row in the node schedules of the current band
304 * root is set if this graph is the original dependence graph,
305 * without any splitting
307 * sorted contains a list of node indices sorted according to the
308 * SCC to which a node belongs
310 * n_edge is the number of edges
311 * edge is the list of edges
312 * max_edge contains the maximal number of edges of each type;
313 * in particular, it contains the number of edges in the inital graph.
314 * edge_table contains pointers into the edge array, hashed on the source
315 * and sink spaces; there is one such table for each type;
316 * a given edge may be referenced from more than one table
317 * if the corresponding relation appears in more than one of the
318 * sets of dependences; however, for each type there is only
319 * a single edge between a given pair of source and sink space
320 * in the entire graph
322 * node_table contains pointers into the node array, hashed on the space tuples
324 * region contains a list of variable sequences that should be non-trivial
326 * lp contains the (I)LP problem used to obtain new schedule rows
328 * src_scc and dst_scc are the source and sink SCCs of an edge with
329 * conflicting constraints
331 * scc represents the number of components
332 * weak is set if the components are weakly connected
334 * max_weight is used during clustering and represents the maximal
335 * weight of the relevant proximity edges.
337 struct isl_sched_graph
{
338 isl_map_to_basic_set
*intra_hmap
;
339 isl_map_to_basic_set
*inter_hmap
;
341 struct isl_sched_node
*node
;
354 struct isl_sched_edge
*edge
;
356 int max_edge
[isl_edge_last
+ 1];
357 struct isl_hash_table
*edge_table
[isl_edge_last
+ 1];
359 struct isl_hash_table
*node_table
;
360 struct isl_trivial_region
*region
;
373 /* Initialize node_table based on the list of nodes.
375 static int graph_init_table(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
379 graph
->node_table
= isl_hash_table_alloc(ctx
, graph
->n
);
380 if (!graph
->node_table
)
383 for (i
= 0; i
< graph
->n
; ++i
) {
384 struct isl_hash_table_entry
*entry
;
387 hash
= isl_space_get_tuple_hash(graph
->node
[i
].space
);
388 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
390 graph
->node
[i
].space
, 1);
393 entry
->data
= &graph
->node
[i
];
399 /* Return a pointer to the node that lives within the given space,
400 * or NULL if there is no such node.
402 static struct isl_sched_node
*graph_find_node(isl_ctx
*ctx
,
403 struct isl_sched_graph
*graph
, __isl_keep isl_space
*space
)
405 struct isl_hash_table_entry
*entry
;
408 hash
= isl_space_get_tuple_hash(space
);
409 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
410 &node_has_tuples
, space
, 0);
412 return entry
? entry
->data
: NULL
;
415 static int edge_has_src_and_dst(const void *entry
, const void *val
)
417 const struct isl_sched_edge
*edge
= entry
;
418 const struct isl_sched_edge
*temp
= val
;
420 return edge
->src
== temp
->src
&& edge
->dst
== temp
->dst
;
423 /* Add the given edge to graph->edge_table[type].
425 static isl_stat
graph_edge_table_add(isl_ctx
*ctx
,
426 struct isl_sched_graph
*graph
, enum isl_edge_type type
,
427 struct isl_sched_edge
*edge
)
429 struct isl_hash_table_entry
*entry
;
432 hash
= isl_hash_init();
433 hash
= isl_hash_builtin(hash
, edge
->src
);
434 hash
= isl_hash_builtin(hash
, edge
->dst
);
435 entry
= isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
436 &edge_has_src_and_dst
, edge
, 1);
438 return isl_stat_error
;
444 /* Allocate the edge_tables based on the maximal number of edges of
447 static int graph_init_edge_tables(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
451 for (i
= 0; i
<= isl_edge_last
; ++i
) {
452 graph
->edge_table
[i
] = isl_hash_table_alloc(ctx
,
454 if (!graph
->edge_table
[i
])
461 /* If graph->edge_table[type] contains an edge from the given source
462 * to the given destination, then return the hash table entry of this edge.
463 * Otherwise, return NULL.
465 static struct isl_hash_table_entry
*graph_find_edge_entry(
466 struct isl_sched_graph
*graph
,
467 enum isl_edge_type type
,
468 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
470 isl_ctx
*ctx
= isl_space_get_ctx(src
->space
);
472 struct isl_sched_edge temp
= { .src
= src
, .dst
= dst
};
474 hash
= isl_hash_init();
475 hash
= isl_hash_builtin(hash
, temp
.src
);
476 hash
= isl_hash_builtin(hash
, temp
.dst
);
477 return isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
478 &edge_has_src_and_dst
, &temp
, 0);
482 /* If graph->edge_table[type] contains an edge from the given source
483 * to the given destination, then return this edge.
484 * Otherwise, return NULL.
486 static struct isl_sched_edge
*graph_find_edge(struct isl_sched_graph
*graph
,
487 enum isl_edge_type type
,
488 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
490 struct isl_hash_table_entry
*entry
;
492 entry
= graph_find_edge_entry(graph
, type
, src
, dst
);
499 /* Check whether the dependence graph has an edge of the given type
500 * between the given two nodes.
502 static isl_bool
graph_has_edge(struct isl_sched_graph
*graph
,
503 enum isl_edge_type type
,
504 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
506 struct isl_sched_edge
*edge
;
509 edge
= graph_find_edge(graph
, type
, src
, dst
);
513 empty
= isl_map_plain_is_empty(edge
->map
);
515 return isl_bool_error
;
520 /* Look for any edge with the same src, dst and map fields as "model".
522 * Return the matching edge if one can be found.
523 * Return "model" if no matching edge is found.
524 * Return NULL on error.
526 static struct isl_sched_edge
*graph_find_matching_edge(
527 struct isl_sched_graph
*graph
, struct isl_sched_edge
*model
)
529 enum isl_edge_type i
;
530 struct isl_sched_edge
*edge
;
532 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
535 edge
= graph_find_edge(graph
, i
, model
->src
, model
->dst
);
538 is_equal
= isl_map_plain_is_equal(model
->map
, edge
->map
);
548 /* Remove the given edge from all the edge_tables that refer to it.
550 static void graph_remove_edge(struct isl_sched_graph
*graph
,
551 struct isl_sched_edge
*edge
)
553 isl_ctx
*ctx
= isl_map_get_ctx(edge
->map
);
554 enum isl_edge_type i
;
556 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
557 struct isl_hash_table_entry
*entry
;
559 entry
= graph_find_edge_entry(graph
, i
, edge
->src
, edge
->dst
);
562 if (entry
->data
!= edge
)
564 isl_hash_table_remove(ctx
, graph
->edge_table
[i
], entry
);
568 /* Check whether the dependence graph has any edge
569 * between the given two nodes.
571 static isl_bool
graph_has_any_edge(struct isl_sched_graph
*graph
,
572 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
574 enum isl_edge_type i
;
577 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
578 r
= graph_has_edge(graph
, i
, src
, dst
);
586 /* Check whether the dependence graph has a validity edge
587 * between the given two nodes.
589 * Conditional validity edges are essentially validity edges that
590 * can be ignored if the corresponding condition edges are iteration private.
591 * Here, we are only checking for the presence of validity
592 * edges, so we need to consider the conditional validity edges too.
593 * In particular, this function is used during the detection
594 * of strongly connected components and we cannot ignore
595 * conditional validity edges during this detection.
597 static isl_bool
graph_has_validity_edge(struct isl_sched_graph
*graph
,
598 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
602 r
= graph_has_edge(graph
, isl_edge_validity
, src
, dst
);
606 return graph_has_edge(graph
, isl_edge_conditional_validity
, src
, dst
);
609 static int graph_alloc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
610 int n_node
, int n_edge
)
615 graph
->n_edge
= n_edge
;
616 graph
->node
= isl_calloc_array(ctx
, struct isl_sched_node
, graph
->n
);
617 graph
->sorted
= isl_calloc_array(ctx
, int, graph
->n
);
618 graph
->region
= isl_alloc_array(ctx
,
619 struct isl_trivial_region
, graph
->n
);
620 graph
->edge
= isl_calloc_array(ctx
,
621 struct isl_sched_edge
, graph
->n_edge
);
623 graph
->intra_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
624 graph
->inter_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
626 if (!graph
->node
|| !graph
->region
|| (graph
->n_edge
&& !graph
->edge
) ||
630 for(i
= 0; i
< graph
->n
; ++i
)
631 graph
->sorted
[i
] = i
;
636 static void graph_free(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
640 isl_map_to_basic_set_free(graph
->intra_hmap
);
641 isl_map_to_basic_set_free(graph
->inter_hmap
);
644 for (i
= 0; i
< graph
->n
; ++i
) {
645 isl_space_free(graph
->node
[i
].space
);
646 isl_set_free(graph
->node
[i
].hull
);
647 isl_multi_aff_free(graph
->node
[i
].compress
);
648 isl_multi_aff_free(graph
->node
[i
].decompress
);
649 isl_mat_free(graph
->node
[i
].sched
);
650 isl_map_free(graph
->node
[i
].sched_map
);
651 isl_mat_free(graph
->node
[i
].indep
);
652 isl_mat_free(graph
->node
[i
].vmap
);
654 free(graph
->node
[i
].coincident
);
655 isl_multi_val_free(graph
->node
[i
].sizes
);
656 isl_vec_free(graph
->node
[i
].max
);
661 for (i
= 0; i
< graph
->n_edge
; ++i
) {
662 isl_map_free(graph
->edge
[i
].map
);
663 isl_union_map_free(graph
->edge
[i
].tagged_condition
);
664 isl_union_map_free(graph
->edge
[i
].tagged_validity
);
668 for (i
= 0; i
<= isl_edge_last
; ++i
)
669 isl_hash_table_free(ctx
, graph
->edge_table
[i
]);
670 isl_hash_table_free(ctx
, graph
->node_table
);
671 isl_basic_set_free(graph
->lp
);
674 /* For each "set" on which this function is called, increment
675 * graph->n by one and update graph->maxvar.
677 static isl_stat
init_n_maxvar(__isl_take isl_set
*set
, void *user
)
679 struct isl_sched_graph
*graph
= user
;
680 int nvar
= isl_set_dim(set
, isl_dim_set
);
683 if (nvar
> graph
->maxvar
)
684 graph
->maxvar
= nvar
;
691 /* Compute the number of rows that should be allocated for the schedule.
692 * In particular, we need one row for each variable or one row
693 * for each basic map in the dependences.
694 * Note that it is practically impossible to exhaust both
695 * the number of dependences and the number of variables.
697 static isl_stat
compute_max_row(struct isl_sched_graph
*graph
,
698 __isl_keep isl_schedule_constraints
*sc
)
702 isl_union_set
*domain
;
706 domain
= isl_schedule_constraints_get_domain(sc
);
707 r
= isl_union_set_foreach_set(domain
, &init_n_maxvar
, graph
);
708 isl_union_set_free(domain
);
710 return isl_stat_error
;
711 n_edge
= isl_schedule_constraints_n_basic_map(sc
);
713 return isl_stat_error
;
714 graph
->max_row
= n_edge
+ graph
->maxvar
;
719 /* Does "bset" have any defining equalities for its set variables?
721 static isl_bool
has_any_defining_equality(__isl_keep isl_basic_set
*bset
)
726 return isl_bool_error
;
728 n
= isl_basic_set_dim(bset
, isl_dim_set
);
729 for (i
= 0; i
< n
; ++i
) {
732 has
= isl_basic_set_has_defining_equality(bset
, isl_dim_set
, i
,
738 return isl_bool_false
;
741 /* Set the entries of node->max to the value of the schedule_max_coefficient
744 static isl_stat
set_max_coefficient(isl_ctx
*ctx
, struct isl_sched_node
*node
)
748 max
= isl_options_get_schedule_max_coefficient(ctx
);
752 node
->max
= isl_vec_alloc(ctx
, node
->nvar
);
753 node
->max
= isl_vec_set_si(node
->max
, max
);
755 return isl_stat_error
;
760 /* Set the entries of node->max to the minimum of the schedule_max_coefficient
761 * option (if set) and half of the minimum of the sizes in the other
762 * dimensions. Round up when computing the half such that
763 * if the minimum of the sizes is one, half of the size is taken to be one
765 * If the global minimum is unbounded (i.e., if both
766 * the schedule_max_coefficient is not set and the sizes in the other
767 * dimensions are unbounded), then store a negative value.
768 * If the schedule coefficient is close to the size of the instance set
769 * in another dimension, then the schedule may represent a loop
770 * coalescing transformation (especially if the coefficient
771 * in that other dimension is one). Forcing the coefficient to be
772 * smaller than or equal to half the minimal size should avoid this
775 static isl_stat
compute_max_coefficient(isl_ctx
*ctx
,
776 struct isl_sched_node
*node
)
782 max
= isl_options_get_schedule_max_coefficient(ctx
);
783 v
= isl_vec_alloc(ctx
, node
->nvar
);
785 return isl_stat_error
;
787 for (i
= 0; i
< node
->nvar
; ++i
) {
788 isl_int_set_si(v
->el
[i
], max
);
789 isl_int_mul_si(v
->el
[i
], v
->el
[i
], 2);
792 for (i
= 0; i
< node
->nvar
; ++i
) {
795 size
= isl_multi_val_get_val(node
->sizes
, i
);
798 if (!isl_val_is_int(size
)) {
802 for (j
= 0; j
< node
->nvar
; ++j
) {
805 if (isl_int_is_neg(v
->el
[j
]) ||
806 isl_int_gt(v
->el
[j
], size
->n
))
807 isl_int_set(v
->el
[j
], size
->n
);
812 for (i
= 0; i
< node
->nvar
; ++i
)
813 isl_int_cdiv_q_ui(v
->el
[i
], v
->el
[i
], 2);
819 return isl_stat_error
;
822 /* Compute and return the size of "set" in dimension "dim".
823 * The size is taken to be the difference in values for that variable
824 * for fixed values of the other variables.
825 * In particular, the variable is first isolated from the other variables
826 * in the range of a map
828 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [i_dim]
830 * and then duplicated
832 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [[i_dim] -> [i_dim']]
834 * The shared variables are then projected out and the maximal value
835 * of i_dim' - i_dim is computed.
837 static __isl_give isl_val
*compute_size(__isl_take isl_set
*set
, int dim
)
844 map
= isl_set_project_onto_map(set
, isl_dim_set
, dim
, 1);
845 map
= isl_map_project_out(map
, isl_dim_in
, dim
, 1);
846 map
= isl_map_range_product(map
, isl_map_copy(map
));
847 map
= isl_set_unwrap(isl_map_range(map
));
848 set
= isl_map_deltas(map
);
849 ls
= isl_local_space_from_space(isl_set_get_space(set
));
850 obj
= isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
851 v
= isl_set_max_val(set
, obj
);
858 /* Compute the size of the instance set "set" of "node", after compression,
859 * as well as bounds on the corresponding coefficients, if needed.
861 * The sizes are needed when the schedule_treat_coalescing option is set.
862 * The bounds are needed when the schedule_treat_coalescing option or
863 * the schedule_max_coefficient option is set.
865 * If the schedule_treat_coalescing option is not set, then at most
866 * the bounds need to be set and this is done in set_max_coefficient.
867 * Otherwise, compress the domain if needed, compute the size
868 * in each direction and store the results in node->size.
869 * Finally, set the bounds on the coefficients based on the sizes
870 * and the schedule_max_coefficient option in compute_max_coefficient.
872 static isl_stat
compute_sizes_and_max(isl_ctx
*ctx
, struct isl_sched_node
*node
,
873 __isl_take isl_set
*set
)
878 if (!isl_options_get_schedule_treat_coalescing(ctx
)) {
880 return set_max_coefficient(ctx
, node
);
883 if (node
->compressed
)
884 set
= isl_set_preimage_multi_aff(set
,
885 isl_multi_aff_copy(node
->decompress
));
886 mv
= isl_multi_val_zero(isl_set_get_space(set
));
887 n
= isl_set_dim(set
, isl_dim_set
);
888 for (j
= 0; j
< n
; ++j
) {
891 v
= compute_size(isl_set_copy(set
), j
);
892 mv
= isl_multi_val_set_val(mv
, j
, v
);
897 return isl_stat_error
;
898 return compute_max_coefficient(ctx
, node
);
901 /* Add a new node to the graph representing the given instance set.
902 * "nvar" is the (possibly compressed) number of variables and
903 * may be smaller than then number of set variables in "set"
904 * if "compressed" is set.
905 * If "compressed" is set, then "hull" represents the constraints
906 * that were used to derive the compression, while "compress" and
907 * "decompress" map the original space to the compressed space and
909 * If "compressed" is not set, then "hull", "compress" and "decompress"
912 * Compute the size of the instance set and bounds on the coefficients,
915 static isl_stat
add_node(struct isl_sched_graph
*graph
,
916 __isl_take isl_set
*set
, int nvar
, int compressed
,
917 __isl_take isl_set
*hull
, __isl_take isl_multi_aff
*compress
,
918 __isl_take isl_multi_aff
*decompress
)
925 struct isl_sched_node
*node
;
928 return isl_stat_error
;
930 ctx
= isl_set_get_ctx(set
);
931 nparam
= isl_set_dim(set
, isl_dim_param
);
932 if (!ctx
->opt
->schedule_parametric
)
934 sched
= isl_mat_alloc(ctx
, 0, 1 + nparam
+ nvar
);
935 node
= &graph
->node
[graph
->n
];
937 space
= isl_set_get_space(set
);
940 node
->nparam
= nparam
;
942 node
->sched_map
= NULL
;
943 coincident
= isl_calloc_array(ctx
, int, graph
->max_row
);
944 node
->coincident
= coincident
;
945 node
->compressed
= compressed
;
947 node
->compress
= compress
;
948 node
->decompress
= decompress
;
949 if (compute_sizes_and_max(ctx
, node
, set
) < 0)
950 return isl_stat_error
;
952 if (!space
|| !sched
|| (graph
->max_row
&& !coincident
))
953 return isl_stat_error
;
954 if (compressed
&& (!hull
|| !compress
|| !decompress
))
955 return isl_stat_error
;
960 /* Construct an identifier for node "node", which will represent "set".
961 * The name of the identifier is either "compressed" or
962 * "compressed_<name>", with <name> the name of the space of "set".
963 * The user pointer of the identifier points to "node".
965 static __isl_give isl_id
*construct_compressed_id(__isl_keep isl_set
*set
,
966 struct isl_sched_node
*node
)
975 has_name
= isl_set_has_tuple_name(set
);
979 ctx
= isl_set_get_ctx(set
);
981 return isl_id_alloc(ctx
, "compressed", node
);
983 p
= isl_printer_to_str(ctx
);
984 name
= isl_set_get_tuple_name(set
);
985 p
= isl_printer_print_str(p
, "compressed_");
986 p
= isl_printer_print_str(p
, name
);
987 id_name
= isl_printer_get_str(p
);
990 id
= isl_id_alloc(ctx
, id_name
, node
);
996 /* Add a new node to the graph representing the given set.
998 * If any of the set variables is defined by an equality, then
999 * we perform variable compression such that we can perform
1000 * the scheduling on the compressed domain.
1001 * In this case, an identifier is used that references the new node
1002 * such that each compressed space is unique and
1003 * such that the node can be recovered from the compressed space.
1005 static isl_stat
extract_node(__isl_take isl_set
*set
, void *user
)
1008 isl_bool has_equality
;
1010 isl_basic_set
*hull
;
1013 isl_multi_aff
*compress
, *decompress
;
1014 struct isl_sched_graph
*graph
= user
;
1016 hull
= isl_set_affine_hull(isl_set_copy(set
));
1017 hull
= isl_basic_set_remove_divs(hull
);
1018 nvar
= isl_set_dim(set
, isl_dim_set
);
1019 has_equality
= has_any_defining_equality(hull
);
1021 if (has_equality
< 0)
1023 if (!has_equality
) {
1024 isl_basic_set_free(hull
);
1025 return add_node(graph
, set
, nvar
, 0, NULL
, NULL
, NULL
);
1028 id
= construct_compressed_id(set
, &graph
->node
[graph
->n
]);
1029 morph
= isl_basic_set_variable_compression_with_id(hull
,
1032 nvar
= isl_morph_ran_dim(morph
, isl_dim_set
);
1033 compress
= isl_morph_get_var_multi_aff(morph
);
1034 morph
= isl_morph_inverse(morph
);
1035 decompress
= isl_morph_get_var_multi_aff(morph
);
1036 isl_morph_free(morph
);
1038 hull_set
= isl_set_from_basic_set(hull
);
1039 return add_node(graph
, set
, nvar
, 1, hull_set
, compress
, decompress
);
1041 isl_basic_set_free(hull
);
1043 return isl_stat_error
;
1046 struct isl_extract_edge_data
{
1047 enum isl_edge_type type
;
1048 struct isl_sched_graph
*graph
;
1051 /* Merge edge2 into edge1, freeing the contents of edge2.
1052 * Return 0 on success and -1 on failure.
1054 * edge1 and edge2 are assumed to have the same value for the map field.
1056 static int merge_edge(struct isl_sched_edge
*edge1
,
1057 struct isl_sched_edge
*edge2
)
1059 edge1
->types
|= edge2
->types
;
1060 isl_map_free(edge2
->map
);
1062 if (is_condition(edge2
)) {
1063 if (!edge1
->tagged_condition
)
1064 edge1
->tagged_condition
= edge2
->tagged_condition
;
1066 edge1
->tagged_condition
=
1067 isl_union_map_union(edge1
->tagged_condition
,
1068 edge2
->tagged_condition
);
1071 if (is_conditional_validity(edge2
)) {
1072 if (!edge1
->tagged_validity
)
1073 edge1
->tagged_validity
= edge2
->tagged_validity
;
1075 edge1
->tagged_validity
=
1076 isl_union_map_union(edge1
->tagged_validity
,
1077 edge2
->tagged_validity
);
1080 if (is_condition(edge2
) && !edge1
->tagged_condition
)
1082 if (is_conditional_validity(edge2
) && !edge1
->tagged_validity
)
1088 /* Insert dummy tags in domain and range of "map".
1090 * In particular, if "map" is of the form
1096 * [A -> dummy_tag] -> [B -> dummy_tag]
1098 * where the dummy_tags are identical and equal to any dummy tags
1099 * introduced by any other call to this function.
1101 static __isl_give isl_map
*insert_dummy_tags(__isl_take isl_map
*map
)
1107 isl_set
*domain
, *range
;
1109 ctx
= isl_map_get_ctx(map
);
1111 id
= isl_id_alloc(ctx
, NULL
, &dummy
);
1112 space
= isl_space_params(isl_map_get_space(map
));
1113 space
= isl_space_set_from_params(space
);
1114 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
1115 space
= isl_space_map_from_set(space
);
1117 domain
= isl_map_wrap(map
);
1118 range
= isl_map_wrap(isl_map_universe(space
));
1119 map
= isl_map_from_domain_and_range(domain
, range
);
1120 map
= isl_map_zip(map
);
1125 /* Given that at least one of "src" or "dst" is compressed, return
1126 * a map between the spaces of these nodes restricted to the affine
1127 * hull that was used in the compression.
1129 static __isl_give isl_map
*extract_hull(struct isl_sched_node
*src
,
1130 struct isl_sched_node
*dst
)
1134 if (src
->compressed
)
1135 dom
= isl_set_copy(src
->hull
);
1137 dom
= isl_set_universe(isl_space_copy(src
->space
));
1138 if (dst
->compressed
)
1139 ran
= isl_set_copy(dst
->hull
);
1141 ran
= isl_set_universe(isl_space_copy(dst
->space
));
1143 return isl_map_from_domain_and_range(dom
, ran
);
1146 /* Intersect the domains of the nested relations in domain and range
1147 * of "tagged" with "map".
1149 static __isl_give isl_map
*map_intersect_domains(__isl_take isl_map
*tagged
,
1150 __isl_keep isl_map
*map
)
1154 tagged
= isl_map_zip(tagged
);
1155 set
= isl_map_wrap(isl_map_copy(map
));
1156 tagged
= isl_map_intersect_domain(tagged
, set
);
1157 tagged
= isl_map_zip(tagged
);
1161 /* Return a pointer to the node that lives in the domain space of "map"
1162 * or NULL if there is no such node.
1164 static struct isl_sched_node
*find_domain_node(isl_ctx
*ctx
,
1165 struct isl_sched_graph
*graph
, __isl_keep isl_map
*map
)
1167 struct isl_sched_node
*node
;
1170 space
= isl_space_domain(isl_map_get_space(map
));
1171 node
= graph_find_node(ctx
, graph
, space
);
1172 isl_space_free(space
);
1177 /* Return a pointer to the node that lives in the range space of "map"
1178 * or NULL if there is no such node.
1180 static struct isl_sched_node
*find_range_node(isl_ctx
*ctx
,
1181 struct isl_sched_graph
*graph
, __isl_keep isl_map
*map
)
1183 struct isl_sched_node
*node
;
1186 space
= isl_space_range(isl_map_get_space(map
));
1187 node
= graph_find_node(ctx
, graph
, space
);
1188 isl_space_free(space
);
1193 /* Add a new edge to the graph based on the given map
1194 * and add it to data->graph->edge_table[data->type].
1195 * If a dependence relation of a given type happens to be identical
1196 * to one of the dependence relations of a type that was added before,
1197 * then we don't create a new edge, but instead mark the original edge
1198 * as also representing a dependence of the current type.
1200 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1201 * may be specified as "tagged" dependence relations. That is, "map"
1202 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1203 * the dependence on iterations and a and b are tags.
1204 * edge->map is set to the relation containing the elements i -> j,
1205 * while edge->tagged_condition and edge->tagged_validity contain
1206 * the union of all the "map" relations
1207 * for which extract_edge is called that result in the same edge->map.
1209 * If the source or the destination node is compressed, then
1210 * intersect both "map" and "tagged" with the constraints that
1211 * were used to construct the compression.
1212 * This ensures that there are no schedule constraints defined
1213 * outside of these domains, while the scheduler no longer has
1214 * any control over those outside parts.
1216 static isl_stat
extract_edge(__isl_take isl_map
*map
, void *user
)
1218 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1219 struct isl_extract_edge_data
*data
= user
;
1220 struct isl_sched_graph
*graph
= data
->graph
;
1221 struct isl_sched_node
*src
, *dst
;
1222 struct isl_sched_edge
*edge
;
1223 isl_map
*tagged
= NULL
;
1225 if (data
->type
== isl_edge_condition
||
1226 data
->type
== isl_edge_conditional_validity
) {
1227 if (isl_map_can_zip(map
)) {
1228 tagged
= isl_map_copy(map
);
1229 map
= isl_set_unwrap(isl_map_domain(isl_map_zip(map
)));
1231 tagged
= insert_dummy_tags(isl_map_copy(map
));
1235 src
= find_domain_node(ctx
, graph
, map
);
1236 dst
= find_range_node(ctx
, graph
, map
);
1240 isl_map_free(tagged
);
1244 if (src
->compressed
|| dst
->compressed
) {
1246 hull
= extract_hull(src
, dst
);
1248 tagged
= map_intersect_domains(tagged
, hull
);
1249 map
= isl_map_intersect(map
, hull
);
1252 graph
->edge
[graph
->n_edge
].src
= src
;
1253 graph
->edge
[graph
->n_edge
].dst
= dst
;
1254 graph
->edge
[graph
->n_edge
].map
= map
;
1255 graph
->edge
[graph
->n_edge
].types
= 0;
1256 graph
->edge
[graph
->n_edge
].tagged_condition
= NULL
;
1257 graph
->edge
[graph
->n_edge
].tagged_validity
= NULL
;
1258 set_type(&graph
->edge
[graph
->n_edge
], data
->type
);
1259 if (data
->type
== isl_edge_condition
)
1260 graph
->edge
[graph
->n_edge
].tagged_condition
=
1261 isl_union_map_from_map(tagged
);
1262 if (data
->type
== isl_edge_conditional_validity
)
1263 graph
->edge
[graph
->n_edge
].tagged_validity
=
1264 isl_union_map_from_map(tagged
);
1266 edge
= graph_find_matching_edge(graph
, &graph
->edge
[graph
->n_edge
]);
1269 return isl_stat_error
;
1271 if (edge
== &graph
->edge
[graph
->n_edge
])
1272 return graph_edge_table_add(ctx
, graph
, data
->type
,
1273 &graph
->edge
[graph
->n_edge
++]);
1275 if (merge_edge(edge
, &graph
->edge
[graph
->n_edge
]) < 0)
1278 return graph_edge_table_add(ctx
, graph
, data
->type
, edge
);
1281 /* Initialize the schedule graph "graph" from the schedule constraints "sc".
1283 * The context is included in the domain before the nodes of
1284 * the graphs are extracted in order to be able to exploit
1285 * any possible additional equalities.
1286 * Note that this intersection is only performed locally here.
1288 static isl_stat
graph_init(struct isl_sched_graph
*graph
,
1289 __isl_keep isl_schedule_constraints
*sc
)
1292 isl_union_set
*domain
;
1294 struct isl_extract_edge_data data
;
1295 enum isl_edge_type i
;
1299 return isl_stat_error
;
1301 ctx
= isl_schedule_constraints_get_ctx(sc
);
1303 domain
= isl_schedule_constraints_get_domain(sc
);
1304 graph
->n
= isl_union_set_n_set(domain
);
1305 isl_union_set_free(domain
);
1307 if (graph_alloc(ctx
, graph
, graph
->n
,
1308 isl_schedule_constraints_n_map(sc
)) < 0)
1309 return isl_stat_error
;
1311 if (compute_max_row(graph
, sc
) < 0)
1312 return isl_stat_error
;
1315 domain
= isl_schedule_constraints_get_domain(sc
);
1316 domain
= isl_union_set_intersect_params(domain
,
1317 isl_schedule_constraints_get_context(sc
));
1318 r
= isl_union_set_foreach_set(domain
, &extract_node
, graph
);
1319 isl_union_set_free(domain
);
1321 return isl_stat_error
;
1322 if (graph_init_table(ctx
, graph
) < 0)
1323 return isl_stat_error
;
1324 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
1325 c
= isl_schedule_constraints_get(sc
, i
);
1326 graph
->max_edge
[i
] = isl_union_map_n_map(c
);
1327 isl_union_map_free(c
);
1329 return isl_stat_error
;
1331 if (graph_init_edge_tables(ctx
, graph
) < 0)
1332 return isl_stat_error
;
1335 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
1339 c
= isl_schedule_constraints_get(sc
, i
);
1340 r
= isl_union_map_foreach_map(c
, &extract_edge
, &data
);
1341 isl_union_map_free(c
);
1343 return isl_stat_error
;
1349 /* Check whether there is any dependence from node[j] to node[i]
1350 * or from node[i] to node[j].
1352 static isl_bool
node_follows_weak(int i
, int j
, void *user
)
1355 struct isl_sched_graph
*graph
= user
;
1357 f
= graph_has_any_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1360 return graph_has_any_edge(graph
, &graph
->node
[i
], &graph
->node
[j
]);
1363 /* Check whether there is a (conditional) validity dependence from node[j]
1364 * to node[i], forcing node[i] to follow node[j].
1366 static isl_bool
node_follows_strong(int i
, int j
, void *user
)
1368 struct isl_sched_graph
*graph
= user
;
1370 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1373 /* Use Tarjan's algorithm for computing the strongly connected components
1374 * in the dependence graph only considering those edges defined by "follows".
1376 static int detect_ccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
1377 isl_bool (*follows
)(int i
, int j
, void *user
))
1380 struct isl_tarjan_graph
*g
= NULL
;
1382 g
= isl_tarjan_graph_init(ctx
, graph
->n
, follows
, graph
);
1390 while (g
->order
[i
] != -1) {
1391 graph
->node
[g
->order
[i
]].scc
= graph
->scc
;
1399 isl_tarjan_graph_free(g
);
1404 /* Apply Tarjan's algorithm to detect the strongly connected components
1405 * in the dependence graph.
1406 * Only consider the (conditional) validity dependences and clear "weak".
1408 static int detect_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1411 return detect_ccs(ctx
, graph
, &node_follows_strong
);
1414 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1415 * in the dependence graph.
1416 * Consider all dependences and set "weak".
1418 static int detect_wccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1421 return detect_ccs(ctx
, graph
, &node_follows_weak
);
1424 static int cmp_scc(const void *a
, const void *b
, void *data
)
1426 struct isl_sched_graph
*graph
= data
;
1430 return graph
->node
[*i1
].scc
- graph
->node
[*i2
].scc
;
1433 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1435 static int sort_sccs(struct isl_sched_graph
*graph
)
1437 return isl_sort(graph
->sorted
, graph
->n
, sizeof(int), &cmp_scc
, graph
);
1440 /* Given a dependence relation R from "node" to itself,
1441 * construct the set of coefficients of valid constraints for elements
1442 * in that dependence relation.
1443 * In particular, the result contains tuples of coefficients
1444 * c_0, c_n, c_x such that
1446 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1450 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1452 * We choose here to compute the dual of delta R.
1453 * Alternatively, we could have computed the dual of R, resulting
1454 * in a set of tuples c_0, c_n, c_x, c_y, and then
1455 * plugged in (c_0, c_n, c_x, -c_x).
1457 * If "node" has been compressed, then the dependence relation
1458 * is also compressed before the set of coefficients is computed.
1460 static __isl_give isl_basic_set
*intra_coefficients(
1461 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1462 __isl_take isl_map
*map
)
1466 isl_basic_set
*coef
;
1467 isl_maybe_isl_basic_set m
;
1469 m
= isl_map_to_basic_set_try_get(graph
->intra_hmap
, map
);
1470 if (m
.valid
< 0 || m
.valid
) {
1475 key
= isl_map_copy(map
);
1476 if (node
->compressed
) {
1477 map
= isl_map_preimage_domain_multi_aff(map
,
1478 isl_multi_aff_copy(node
->decompress
));
1479 map
= isl_map_preimage_range_multi_aff(map
,
1480 isl_multi_aff_copy(node
->decompress
));
1482 delta
= isl_set_remove_divs(isl_map_deltas(map
));
1483 coef
= isl_set_coefficients(delta
);
1484 graph
->intra_hmap
= isl_map_to_basic_set_set(graph
->intra_hmap
, key
,
1485 isl_basic_set_copy(coef
));
1490 /* Given a dependence relation R, construct the set of coefficients
1491 * of valid constraints for elements in that dependence relation.
1492 * In particular, the result contains tuples of coefficients
1493 * c_0, c_n, c_x, c_y such that
1495 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1497 * If the source or destination nodes of "edge" have been compressed,
1498 * then the dependence relation is also compressed before
1499 * the set of coefficients is computed.
1501 static __isl_give isl_basic_set
*inter_coefficients(
1502 struct isl_sched_graph
*graph
, struct isl_sched_edge
*edge
,
1503 __isl_take isl_map
*map
)
1507 isl_basic_set
*coef
;
1508 isl_maybe_isl_basic_set m
;
1510 m
= isl_map_to_basic_set_try_get(graph
->inter_hmap
, map
);
1511 if (m
.valid
< 0 || m
.valid
) {
1516 key
= isl_map_copy(map
);
1517 if (edge
->src
->compressed
)
1518 map
= isl_map_preimage_domain_multi_aff(map
,
1519 isl_multi_aff_copy(edge
->src
->decompress
));
1520 if (edge
->dst
->compressed
)
1521 map
= isl_map_preimage_range_multi_aff(map
,
1522 isl_multi_aff_copy(edge
->dst
->decompress
));
1523 set
= isl_map_wrap(isl_map_remove_divs(map
));
1524 coef
= isl_set_coefficients(set
);
1525 graph
->inter_hmap
= isl_map_to_basic_set_set(graph
->inter_hmap
, key
,
1526 isl_basic_set_copy(coef
));
1531 /* Return the position of the coefficients of the variables in
1532 * the coefficients constraints "coef".
1534 * The space of "coef" is of the form
1536 * { coefficients[[cst, params] -> S] }
1538 * Return the position of S.
1540 static int coef_var_offset(__isl_keep isl_basic_set
*coef
)
1545 space
= isl_space_unwrap(isl_basic_set_get_space(coef
));
1546 offset
= isl_space_dim(space
, isl_dim_in
);
1547 isl_space_free(space
);
1552 /* Return the offset of the coefficient of the constant term of "node"
1555 * Within each node, the coefficients have the following order:
1556 * - positive and negative parts of c_i_x
1557 * - c_i_n (if parametric)
1560 static int node_cst_coef_offset(struct isl_sched_node
*node
)
1562 return node
->start
+ 2 * node
->nvar
+ node
->nparam
;
1565 /* Return the offset of the coefficients of the parameters of "node"
1568 * Within each node, the coefficients have the following order:
1569 * - positive and negative parts of c_i_x
1570 * - c_i_n (if parametric)
1573 static int node_par_coef_offset(struct isl_sched_node
*node
)
1575 return node
->start
+ 2 * node
->nvar
;
1578 /* Return the offset of the coefficients of the variables of "node"
1581 * Within each node, the coefficients have the following order:
1582 * - positive and negative parts of c_i_x
1583 * - c_i_n (if parametric)
1586 static int node_var_coef_offset(struct isl_sched_node
*node
)
1591 /* Return the position of the pair of variables encoding
1592 * coefficient "i" of "node".
1594 * The order of these variable pairs is the opposite of
1595 * that of the coefficients, with 2 variables per coefficient.
1597 static int node_var_coef_pos(struct isl_sched_node
*node
, int i
)
1599 return node_var_coef_offset(node
) + 2 * (node
->nvar
- 1 - i
);
1602 /* Construct an isl_dim_map for mapping constraints on coefficients
1603 * for "node" to the corresponding positions in graph->lp.
1604 * "offset" is the offset of the coefficients for the variables
1605 * in the input constraints.
1606 * "s" is the sign of the mapping.
1608 * The input constraints are given in terms of the coefficients (c_0, c_n, c_x).
1609 * The mapping produced by this function essentially plugs in
1610 * (0, 0, c_i_x^+ - c_i_x^-) if s = 1 and
1611 * (0, 0, -c_i_x^+ + c_i_x^-) if s = -1.
1612 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1613 * Furthermore, the order of these pairs is the opposite of that
1614 * of the corresponding coefficients.
1616 * The caller can extend the mapping to also map the other coefficients
1617 * (and therefore not plug in 0).
1619 static __isl_give isl_dim_map
*intra_dim_map(isl_ctx
*ctx
,
1620 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1625 isl_dim_map
*dim_map
;
1630 total
= isl_basic_set_total_dim(graph
->lp
);
1631 pos
= node_var_coef_pos(node
, 0);
1632 dim_map
= isl_dim_map_alloc(ctx
, total
);
1633 isl_dim_map_range(dim_map
, pos
, -2, offset
, 1, node
->nvar
, -s
);
1634 isl_dim_map_range(dim_map
, pos
+ 1, -2, offset
, 1, node
->nvar
, s
);
1639 /* Construct an isl_dim_map for mapping constraints on coefficients
1640 * for "src" (node i) and "dst" (node j) to the corresponding positions
1642 * "offset" is the offset of the coefficients for the variables of "src"
1643 * in the input constraints.
1644 * "s" is the sign of the mapping.
1646 * The input constraints are given in terms of the coefficients
1647 * (c_0, c_n, c_x, c_y).
1648 * The mapping produced by this function essentially plugs in
1649 * (c_j_0 - c_i_0, c_j_n - c_i_n,
1650 * -(c_i_x^+ - c_i_x^-), c_j_x^+ - c_j_x^-) if s = 1 and
1651 * (-c_j_0 + c_i_0, -c_j_n + c_i_n,
1652 * c_i_x^+ - c_i_x^-, -(c_j_x^+ - c_j_x^-)) if s = -1.
1653 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1654 * Furthermore, the order of these pairs is the opposite of that
1655 * of the corresponding coefficients.
1657 * The caller can further extend the mapping.
1659 static __isl_give isl_dim_map
*inter_dim_map(isl_ctx
*ctx
,
1660 struct isl_sched_graph
*graph
, struct isl_sched_node
*src
,
1661 struct isl_sched_node
*dst
, int offset
, int s
)
1665 isl_dim_map
*dim_map
;
1670 total
= isl_basic_set_total_dim(graph
->lp
);
1671 dim_map
= isl_dim_map_alloc(ctx
, total
);
1673 pos
= node_cst_coef_offset(dst
);
1674 isl_dim_map_range(dim_map
, pos
, 0, 0, 0, 1, s
);
1675 pos
= node_par_coef_offset(dst
);
1676 isl_dim_map_range(dim_map
, pos
, 1, 1, 1, dst
->nparam
, s
);
1677 pos
= node_var_coef_pos(dst
, 0);
1678 isl_dim_map_range(dim_map
, pos
, -2, offset
+ src
->nvar
, 1,
1680 isl_dim_map_range(dim_map
, pos
+ 1, -2, offset
+ src
->nvar
, 1,
1683 pos
= node_cst_coef_offset(src
);
1684 isl_dim_map_range(dim_map
, pos
, 0, 0, 0, 1, -s
);
1685 pos
= node_par_coef_offset(src
);
1686 isl_dim_map_range(dim_map
, pos
, 1, 1, 1, src
->nparam
, -s
);
1687 pos
= node_var_coef_pos(src
, 0);
1688 isl_dim_map_range(dim_map
, pos
, -2, offset
, 1, src
->nvar
, s
);
1689 isl_dim_map_range(dim_map
, pos
+ 1, -2, offset
, 1, src
->nvar
, -s
);
1694 /* Add the constraints from "src" to "dst" using "dim_map",
1695 * after making sure there is enough room in "dst" for the extra constraints.
1697 static __isl_give isl_basic_set
*add_constraints_dim_map(
1698 __isl_take isl_basic_set
*dst
, __isl_take isl_basic_set
*src
,
1699 __isl_take isl_dim_map
*dim_map
)
1703 n_eq
= isl_basic_set_n_equality(src
);
1704 n_ineq
= isl_basic_set_n_inequality(src
);
1705 dst
= isl_basic_set_extend_constraints(dst
, n_eq
, n_ineq
);
1706 dst
= isl_basic_set_add_constraints_dim_map(dst
, src
, dim_map
);
1710 /* Add constraints to graph->lp that force validity for the given
1711 * dependence from a node i to itself.
1712 * That is, add constraints that enforce
1714 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1715 * = c_i_x (y - x) >= 0
1717 * for each (x,y) in R.
1718 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1719 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1720 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1721 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1723 static isl_stat
add_intra_validity_constraints(struct isl_sched_graph
*graph
,
1724 struct isl_sched_edge
*edge
)
1727 isl_map
*map
= isl_map_copy(edge
->map
);
1728 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1729 isl_dim_map
*dim_map
;
1730 isl_basic_set
*coef
;
1731 struct isl_sched_node
*node
= edge
->src
;
1733 coef
= intra_coefficients(graph
, node
, map
);
1735 offset
= coef_var_offset(coef
);
1738 return isl_stat_error
;
1740 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, 1);
1741 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
1746 /* Add constraints to graph->lp that force validity for the given
1747 * dependence from node i to node j.
1748 * That is, add constraints that enforce
1750 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1752 * for each (x,y) in R.
1753 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1754 * of valid constraints for R and then plug in
1755 * (c_j_0 - c_i_0, c_j_n - c_i_n, -(c_i_x^+ - c_i_x^-), c_j_x^+ - c_j_x^-),
1756 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1757 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1759 static isl_stat
add_inter_validity_constraints(struct isl_sched_graph
*graph
,
1760 struct isl_sched_edge
*edge
)
1765 isl_dim_map
*dim_map
;
1766 isl_basic_set
*coef
;
1767 struct isl_sched_node
*src
= edge
->src
;
1768 struct isl_sched_node
*dst
= edge
->dst
;
1771 return isl_stat_error
;
1773 map
= isl_map_copy(edge
->map
);
1774 ctx
= isl_map_get_ctx(map
);
1775 coef
= inter_coefficients(graph
, edge
, map
);
1777 offset
= coef_var_offset(coef
);
1780 return isl_stat_error
;
1782 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, 1);
1784 edge
->start
= graph
->lp
->n_ineq
;
1785 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
1787 return isl_stat_error
;
1788 edge
->end
= graph
->lp
->n_ineq
;
1793 /* Add constraints to graph->lp that bound the dependence distance for the given
1794 * dependence from a node i to itself.
1795 * If s = 1, we add the constraint
1797 * c_i_x (y - x) <= m_0 + m_n n
1801 * -c_i_x (y - x) + m_0 + m_n n >= 0
1803 * for each (x,y) in R.
1804 * If s = -1, we add the constraint
1806 * -c_i_x (y - x) <= m_0 + m_n n
1810 * c_i_x (y - x) + m_0 + m_n n >= 0
1812 * for each (x,y) in R.
1813 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1814 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1815 * with each coefficient (except m_0) represented as a pair of non-negative
1819 * If "local" is set, then we add constraints
1821 * c_i_x (y - x) <= 0
1825 * -c_i_x (y - x) <= 0
1827 * instead, forcing the dependence distance to be (less than or) equal to 0.
1828 * That is, we plug in (0, 0, -s * c_i_x),
1829 * Note that dependences marked local are treated as validity constraints
1830 * by add_all_validity_constraints and therefore also have
1831 * their distances bounded by 0 from below.
1833 static isl_stat
add_intra_proximity_constraints(struct isl_sched_graph
*graph
,
1834 struct isl_sched_edge
*edge
, int s
, int local
)
1838 isl_map
*map
= isl_map_copy(edge
->map
);
1839 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1840 isl_dim_map
*dim_map
;
1841 isl_basic_set
*coef
;
1842 struct isl_sched_node
*node
= edge
->src
;
1844 coef
= intra_coefficients(graph
, node
, map
);
1846 offset
= coef_var_offset(coef
);
1849 return isl_stat_error
;
1851 nparam
= isl_space_dim(node
->space
, isl_dim_param
);
1852 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, -s
);
1855 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1856 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1857 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1859 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
1864 /* Add constraints to graph->lp that bound the dependence distance for the given
1865 * dependence from node i to node j.
1866 * If s = 1, we add the constraint
1868 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1873 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1876 * for each (x,y) in R.
1877 * If s = -1, we add the constraint
1879 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1884 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1887 * for each (x,y) in R.
1888 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1889 * of valid constraints for R and then plug in
1890 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1891 * s*c_i_x, -s*c_j_x)
1892 * with each coefficient (except m_0, c_*_0 and c_*_n)
1893 * represented as a pair of non-negative coefficients.
1896 * If "local" is set (and s = 1), then we add constraints
1898 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1902 * -((c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x)) >= 0
1904 * instead, forcing the dependence distance to be (less than or) equal to 0.
1905 * That is, we plug in
1906 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, s*c_i_x, -s*c_j_x).
1907 * Note that dependences marked local are treated as validity constraints
1908 * by add_all_validity_constraints and therefore also have
1909 * their distances bounded by 0 from below.
1911 static isl_stat
add_inter_proximity_constraints(struct isl_sched_graph
*graph
,
1912 struct isl_sched_edge
*edge
, int s
, int local
)
1916 isl_map
*map
= isl_map_copy(edge
->map
);
1917 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1918 isl_dim_map
*dim_map
;
1919 isl_basic_set
*coef
;
1920 struct isl_sched_node
*src
= edge
->src
;
1921 struct isl_sched_node
*dst
= edge
->dst
;
1923 coef
= inter_coefficients(graph
, edge
, map
);
1925 offset
= coef_var_offset(coef
);
1928 return isl_stat_error
;
1930 nparam
= isl_space_dim(src
->space
, isl_dim_param
);
1931 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, -s
);
1934 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1935 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1936 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1939 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
1944 /* Add all validity constraints to graph->lp.
1946 * An edge that is forced to be local needs to have its dependence
1947 * distances equal to zero. We take care of bounding them by 0 from below
1948 * here. add_all_proximity_constraints takes care of bounding them by 0
1951 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1952 * Otherwise, we ignore them.
1954 static int add_all_validity_constraints(struct isl_sched_graph
*graph
,
1955 int use_coincidence
)
1959 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1960 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1963 local
= is_local(edge
) ||
1964 (is_coincidence(edge
) && use_coincidence
);
1965 if (!is_validity(edge
) && !local
)
1967 if (edge
->src
!= edge
->dst
)
1969 if (add_intra_validity_constraints(graph
, edge
) < 0)
1973 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1974 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1977 local
= is_local(edge
) ||
1978 (is_coincidence(edge
) && use_coincidence
);
1979 if (!is_validity(edge
) && !local
)
1981 if (edge
->src
== edge
->dst
)
1983 if (add_inter_validity_constraints(graph
, edge
) < 0)
1990 /* Add constraints to graph->lp that bound the dependence distance
1991 * for all dependence relations.
1992 * If a given proximity dependence is identical to a validity
1993 * dependence, then the dependence distance is already bounded
1994 * from below (by zero), so we only need to bound the distance
1995 * from above. (This includes the case of "local" dependences
1996 * which are treated as validity dependence by add_all_validity_constraints.)
1997 * Otherwise, we need to bound the distance both from above and from below.
1999 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2000 * Otherwise, we ignore them.
2002 static int add_all_proximity_constraints(struct isl_sched_graph
*graph
,
2003 int use_coincidence
)
2007 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2008 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2011 local
= is_local(edge
) ||
2012 (is_coincidence(edge
) && use_coincidence
);
2013 if (!is_proximity(edge
) && !local
)
2015 if (edge
->src
== edge
->dst
&&
2016 add_intra_proximity_constraints(graph
, edge
, 1, local
) < 0)
2018 if (edge
->src
!= edge
->dst
&&
2019 add_inter_proximity_constraints(graph
, edge
, 1, local
) < 0)
2021 if (is_validity(edge
) || local
)
2023 if (edge
->src
== edge
->dst
&&
2024 add_intra_proximity_constraints(graph
, edge
, -1, 0) < 0)
2026 if (edge
->src
!= edge
->dst
&&
2027 add_inter_proximity_constraints(graph
, edge
, -1, 0) < 0)
2034 /* Normalize the rows of "indep" such that all rows are lexicographically
2035 * positive and such that each row contains as many final zeros as possible,
2036 * given the choice for the previous rows.
2037 * Do this by performing elementary row operations.
2039 static __isl_give isl_mat
*normalize_independent(__isl_take isl_mat
*indep
)
2041 indep
= isl_mat_reverse_gauss(indep
);
2042 indep
= isl_mat_lexnonneg_rows(indep
);
2046 /* Compute a basis for the rows in the linear part of the schedule
2047 * and extend this basis to a full basis. The remaining rows
2048 * can then be used to force linear independence from the rows
2051 * In particular, given the schedule rows S, we compute
2056 * with H the Hermite normal form of S. That is, all but the
2057 * first rank columns of H are zero and so each row in S is
2058 * a linear combination of the first rank rows of Q.
2059 * The matrix Q can be used as a variable transformation
2060 * that isolates the directions of S in the first rank rows.
2061 * Transposing S U = H yields
2065 * with all but the first rank rows of H^T zero.
2066 * The last rows of U^T are therefore linear combinations
2067 * of schedule coefficients that are all zero on schedule
2068 * coefficients that are linearly dependent on the rows of S.
2069 * At least one of these combinations is non-zero on
2070 * linearly independent schedule coefficients.
2071 * The rows are normalized to involve as few of the last
2072 * coefficients as possible and to have a positive initial value.
2074 static int node_update_vmap(struct isl_sched_node
*node
)
2077 int n_row
= isl_mat_rows(node
->sched
);
2079 H
= isl_mat_sub_alloc(node
->sched
, 0, n_row
,
2080 1 + node
->nparam
, node
->nvar
);
2082 H
= isl_mat_left_hermite(H
, 0, &U
, &Q
);
2083 isl_mat_free(node
->indep
);
2084 isl_mat_free(node
->vmap
);
2086 node
->indep
= isl_mat_transpose(U
);
2087 node
->rank
= isl_mat_initial_non_zero_cols(H
);
2088 node
->indep
= isl_mat_drop_rows(node
->indep
, 0, node
->rank
);
2089 node
->indep
= normalize_independent(node
->indep
);
2092 if (!node
->indep
|| !node
->vmap
|| node
->rank
< 0)
2097 /* Is "edge" marked as a validity or a conditional validity edge?
2099 static int is_any_validity(struct isl_sched_edge
*edge
)
2101 return is_validity(edge
) || is_conditional_validity(edge
);
2104 /* How many times should we count the constraints in "edge"?
2106 * We count as follows
2107 * validity -> 1 (>= 0)
2108 * validity+proximity -> 2 (>= 0 and upper bound)
2109 * proximity -> 2 (lower and upper bound)
2110 * local(+any) -> 2 (>= 0 and <= 0)
2112 * If an edge is only marked conditional_validity then it counts
2113 * as zero since it is only checked afterwards.
2115 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2116 * Otherwise, we ignore them.
2118 static int edge_multiplicity(struct isl_sched_edge
*edge
, int use_coincidence
)
2120 if (is_proximity(edge
) || is_local(edge
))
2122 if (use_coincidence
&& is_coincidence(edge
))
2124 if (is_validity(edge
))
2129 /* Count the number of equality and inequality constraints
2130 * that will be added for the given map.
2132 * "use_coincidence" is set if we should take into account coincidence edges.
2134 static isl_stat
count_map_constraints(struct isl_sched_graph
*graph
,
2135 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
,
2136 int *n_eq
, int *n_ineq
, int use_coincidence
)
2138 isl_basic_set
*coef
;
2139 int f
= edge_multiplicity(edge
, use_coincidence
);
2146 if (edge
->src
== edge
->dst
)
2147 coef
= intra_coefficients(graph
, edge
->src
, map
);
2149 coef
= inter_coefficients(graph
, edge
, map
);
2151 return isl_stat_error
;
2152 *n_eq
+= f
* isl_basic_set_n_equality(coef
);
2153 *n_ineq
+= f
* isl_basic_set_n_inequality(coef
);
2154 isl_basic_set_free(coef
);
2159 /* Count the number of equality and inequality constraints
2160 * that will be added to the main lp problem.
2161 * We count as follows
2162 * validity -> 1 (>= 0)
2163 * validity+proximity -> 2 (>= 0 and upper bound)
2164 * proximity -> 2 (lower and upper bound)
2165 * local(+any) -> 2 (>= 0 and <= 0)
2167 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2168 * Otherwise, we ignore them.
2170 static int count_constraints(struct isl_sched_graph
*graph
,
2171 int *n_eq
, int *n_ineq
, int use_coincidence
)
2175 *n_eq
= *n_ineq
= 0;
2176 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2177 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2178 isl_map
*map
= isl_map_copy(edge
->map
);
2180 if (count_map_constraints(graph
, edge
, map
, n_eq
, n_ineq
,
2181 use_coincidence
) < 0)
2188 /* Count the number of constraints that will be added by
2189 * add_bound_constant_constraints to bound the values of the constant terms
2190 * and increment *n_eq and *n_ineq accordingly.
2192 * In practice, add_bound_constant_constraints only adds inequalities.
2194 static isl_stat
count_bound_constant_constraints(isl_ctx
*ctx
,
2195 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
2197 if (isl_options_get_schedule_max_constant_term(ctx
) == -1)
2200 *n_ineq
+= graph
->n
;
2205 /* Add constraints to bound the values of the constant terms in the schedule,
2206 * if requested by the user.
2208 * The maximal value of the constant terms is defined by the option
2209 * "schedule_max_constant_term".
2211 static isl_stat
add_bound_constant_constraints(isl_ctx
*ctx
,
2212 struct isl_sched_graph
*graph
)
2218 max
= isl_options_get_schedule_max_constant_term(ctx
);
2222 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2224 for (i
= 0; i
< graph
->n
; ++i
) {
2225 struct isl_sched_node
*node
= &graph
->node
[i
];
2228 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2230 return isl_stat_error
;
2231 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2232 pos
= node_cst_coef_offset(node
);
2233 isl_int_set_si(graph
->lp
->ineq
[k
][1 + pos
], -1);
2234 isl_int_set_si(graph
->lp
->ineq
[k
][0], max
);
2240 /* Count the number of constraints that will be added by
2241 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
2244 * In practice, add_bound_coefficient_constraints only adds inequalities.
2246 static int count_bound_coefficient_constraints(isl_ctx
*ctx
,
2247 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
2251 if (isl_options_get_schedule_max_coefficient(ctx
) == -1 &&
2252 !isl_options_get_schedule_treat_coalescing(ctx
))
2255 for (i
= 0; i
< graph
->n
; ++i
)
2256 *n_ineq
+= graph
->node
[i
].nparam
+ 2 * graph
->node
[i
].nvar
;
2261 /* Add constraints to graph->lp that bound the values of
2262 * the parameter schedule coefficients of "node" to "max" and
2263 * the variable schedule coefficients to the corresponding entry
2265 * In either case, a negative value means that no bound needs to be imposed.
2267 * For parameter coefficients, this amounts to adding a constraint
2275 * The variables coefficients are, however, not represented directly.
2276 * Instead, the variable coefficients c_x are written as differences
2277 * c_x = c_x^+ - c_x^-.
2280 * -max_i <= c_x_i <= max_i
2284 * -max_i <= c_x_i^+ - c_x_i^- <= max_i
2288 * -(c_x_i^+ - c_x_i^-) + max_i >= 0
2289 * c_x_i^+ - c_x_i^- + max_i >= 0
2291 static isl_stat
node_add_coefficient_constraints(isl_ctx
*ctx
,
2292 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
, int max
)
2298 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2300 for (j
= 0; j
< node
->nparam
; ++j
) {
2306 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2308 return isl_stat_error
;
2309 dim
= 1 + node_par_coef_offset(node
) + j
;
2310 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2311 isl_int_set_si(graph
->lp
->ineq
[k
][dim
], -1);
2312 isl_int_set_si(graph
->lp
->ineq
[k
][0], max
);
2315 ineq
= isl_vec_alloc(ctx
, 1 + total
);
2316 ineq
= isl_vec_clr(ineq
);
2318 return isl_stat_error
;
2319 for (i
= 0; i
< node
->nvar
; ++i
) {
2320 int pos
= 1 + node_var_coef_pos(node
, i
);
2322 if (isl_int_is_neg(node
->max
->el
[i
]))
2325 isl_int_set_si(ineq
->el
[pos
], 1);
2326 isl_int_set_si(ineq
->el
[pos
+ 1], -1);
2327 isl_int_set(ineq
->el
[0], node
->max
->el
[i
]);
2329 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2332 isl_seq_cpy(graph
->lp
->ineq
[k
], ineq
->el
, 1 + total
);
2334 isl_seq_neg(ineq
->el
+ pos
, ineq
->el
+ pos
+ 2 * i
, 2);
2335 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2338 isl_seq_cpy(graph
->lp
->ineq
[k
], ineq
->el
, 1 + total
);
2345 return isl_stat_error
;
2348 /* Add constraints that bound the values of the variable and parameter
2349 * coefficients of the schedule.
2351 * The maximal value of the coefficients is defined by the option
2352 * 'schedule_max_coefficient' and the entries in node->max.
2353 * These latter entries are only set if either the schedule_max_coefficient
2354 * option or the schedule_treat_coalescing option is set.
2356 static isl_stat
add_bound_coefficient_constraints(isl_ctx
*ctx
,
2357 struct isl_sched_graph
*graph
)
2362 max
= isl_options_get_schedule_max_coefficient(ctx
);
2364 if (max
== -1 && !isl_options_get_schedule_treat_coalescing(ctx
))
2367 for (i
= 0; i
< graph
->n
; ++i
) {
2368 struct isl_sched_node
*node
= &graph
->node
[i
];
2370 if (node_add_coefficient_constraints(ctx
, graph
, node
, max
) < 0)
2371 return isl_stat_error
;
2377 /* Add a constraint to graph->lp that equates the value at position
2378 * "sum_pos" to the sum of the "n" values starting at "first".
2380 static isl_stat
add_sum_constraint(struct isl_sched_graph
*graph
,
2381 int sum_pos
, int first
, int n
)
2386 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2388 k
= isl_basic_set_alloc_equality(graph
->lp
);
2390 return isl_stat_error
;
2391 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2392 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2393 for (i
= 0; i
< n
; ++i
)
2394 isl_int_set_si(graph
->lp
->eq
[k
][1 + first
+ i
], 1);
2399 /* Add a constraint to graph->lp that equates the value at position
2400 * "sum_pos" to the sum of the parameter coefficients of all nodes.
2402 static isl_stat
add_param_sum_constraint(struct isl_sched_graph
*graph
,
2408 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2410 k
= isl_basic_set_alloc_equality(graph
->lp
);
2412 return isl_stat_error
;
2413 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2414 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2415 for (i
= 0; i
< graph
->n
; ++i
) {
2416 int pos
= 1 + node_par_coef_offset(&graph
->node
[i
]);
2418 for (j
= 0; j
< graph
->node
[i
].nparam
; ++j
)
2419 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2425 /* Add a constraint to graph->lp that equates the value at position
2426 * "sum_pos" to the sum of the variable coefficients of all nodes.
2428 static isl_stat
add_var_sum_constraint(struct isl_sched_graph
*graph
,
2434 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2436 k
= isl_basic_set_alloc_equality(graph
->lp
);
2438 return isl_stat_error
;
2439 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2440 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2441 for (i
= 0; i
< graph
->n
; ++i
) {
2442 struct isl_sched_node
*node
= &graph
->node
[i
];
2443 int pos
= 1 + node_var_coef_offset(node
);
2445 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
2446 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2452 /* Construct an ILP problem for finding schedule coefficients
2453 * that result in non-negative, but small dependence distances
2454 * over all dependences.
2455 * In particular, the dependence distances over proximity edges
2456 * are bounded by m_0 + m_n n and we compute schedule coefficients
2457 * with small values (preferably zero) of m_n and m_0.
2459 * All variables of the ILP are non-negative. The actual coefficients
2460 * may be negative, so each coefficient is represented as the difference
2461 * of two non-negative variables. The negative part always appears
2462 * immediately before the positive part.
2463 * Other than that, the variables have the following order
2465 * - sum of positive and negative parts of m_n coefficients
2467 * - sum of all c_n coefficients
2468 * (unconstrained when computing non-parametric schedules)
2469 * - sum of positive and negative parts of all c_x coefficients
2470 * - positive and negative parts of m_n coefficients
2472 * - positive and negative parts of c_i_x, in opposite order
2473 * - c_i_n (if parametric)
2476 * The constraints are those from the edges plus two or three equalities
2477 * to express the sums.
2479 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2480 * Otherwise, we ignore them.
2482 static isl_stat
setup_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
2483 int use_coincidence
)
2493 parametric
= ctx
->opt
->schedule_parametric
;
2494 nparam
= isl_space_dim(graph
->node
[0].space
, isl_dim_param
);
2496 total
= param_pos
+ 2 * nparam
;
2497 for (i
= 0; i
< graph
->n
; ++i
) {
2498 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
2499 if (node_update_vmap(node
) < 0)
2500 return isl_stat_error
;
2501 node
->start
= total
;
2502 total
+= 1 + node
->nparam
+ 2 * node
->nvar
;
2505 if (count_constraints(graph
, &n_eq
, &n_ineq
, use_coincidence
) < 0)
2506 return isl_stat_error
;
2507 if (count_bound_constant_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2508 return isl_stat_error
;
2509 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2510 return isl_stat_error
;
2512 space
= isl_space_set_alloc(ctx
, 0, total
);
2513 isl_basic_set_free(graph
->lp
);
2514 n_eq
+= 2 + parametric
;
2516 graph
->lp
= isl_basic_set_alloc_space(space
, 0, n_eq
, n_ineq
);
2518 if (add_sum_constraint(graph
, 0, param_pos
, 2 * nparam
) < 0)
2519 return isl_stat_error
;
2520 if (parametric
&& add_param_sum_constraint(graph
, 2) < 0)
2521 return isl_stat_error
;
2522 if (add_var_sum_constraint(graph
, 3) < 0)
2523 return isl_stat_error
;
2524 if (add_bound_constant_constraints(ctx
, graph
) < 0)
2525 return isl_stat_error
;
2526 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
2527 return isl_stat_error
;
2528 if (add_all_validity_constraints(graph
, use_coincidence
) < 0)
2529 return isl_stat_error
;
2530 if (add_all_proximity_constraints(graph
, use_coincidence
) < 0)
2531 return isl_stat_error
;
2536 /* Analyze the conflicting constraint found by
2537 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2538 * constraint of one of the edges between distinct nodes, living, moreover
2539 * in distinct SCCs, then record the source and sink SCC as this may
2540 * be a good place to cut between SCCs.
2542 static int check_conflict(int con
, void *user
)
2545 struct isl_sched_graph
*graph
= user
;
2547 if (graph
->src_scc
>= 0)
2550 con
-= graph
->lp
->n_eq
;
2552 if (con
>= graph
->lp
->n_ineq
)
2555 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2556 if (!is_validity(&graph
->edge
[i
]))
2558 if (graph
->edge
[i
].src
== graph
->edge
[i
].dst
)
2560 if (graph
->edge
[i
].src
->scc
== graph
->edge
[i
].dst
->scc
)
2562 if (graph
->edge
[i
].start
> con
)
2564 if (graph
->edge
[i
].end
<= con
)
2566 graph
->src_scc
= graph
->edge
[i
].src
->scc
;
2567 graph
->dst_scc
= graph
->edge
[i
].dst
->scc
;
2573 /* Check whether the next schedule row of the given node needs to be
2574 * non-trivial. Lower-dimensional domains may have some trivial rows,
2575 * but as soon as the number of remaining required non-trivial rows
2576 * is as large as the number or remaining rows to be computed,
2577 * all remaining rows need to be non-trivial.
2579 static int needs_row(struct isl_sched_graph
*graph
, struct isl_sched_node
*node
)
2581 return node
->nvar
- node
->rank
>= graph
->maxvar
- graph
->n_row
;
2584 /* Construct a non-triviality region with triviality directions
2585 * corresponding to the rows of "indep".
2586 * The rows of "indep" are expressed in terms of the schedule coefficients c_i,
2587 * while the triviality directions are expressed in terms of
2588 * pairs of non-negative variables c^+_i - c^-_i, with c^-_i appearing
2589 * before c^+_i. Furthermore,
2590 * the pairs of non-negative variables representing the coefficients
2591 * are stored in the opposite order.
2593 static __isl_give isl_mat
*construct_trivial(__isl_keep isl_mat
*indep
)
2602 ctx
= isl_mat_get_ctx(indep
);
2603 n
= isl_mat_rows(indep
);
2604 n_var
= isl_mat_cols(indep
);
2605 mat
= isl_mat_alloc(ctx
, n
, 2 * n_var
);
2608 for (i
= 0; i
< n
; ++i
) {
2609 for (j
= 0; j
< n_var
; ++j
) {
2610 int nj
= n_var
- 1 - j
;
2611 isl_int_neg(mat
->row
[i
][2 * nj
], indep
->row
[i
][j
]);
2612 isl_int_set(mat
->row
[i
][2 * nj
+ 1], indep
->row
[i
][j
]);
2619 /* Solve the ILP problem constructed in setup_lp.
2620 * For each node such that all the remaining rows of its schedule
2621 * need to be non-trivial, we construct a non-triviality region.
2622 * This region imposes that the next row is independent of previous rows.
2623 * In particular, the non-triviality region enforces that at least
2624 * one of the linear combinations in the rows of node->indep is non-zero.
2626 static __isl_give isl_vec
*solve_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2632 for (i
= 0; i
< graph
->n
; ++i
) {
2633 struct isl_sched_node
*node
= &graph
->node
[i
];
2636 graph
->region
[i
].pos
= node_var_coef_offset(node
);
2637 if (needs_row(graph
, node
))
2638 trivial
= construct_trivial(node
->indep
);
2640 trivial
= isl_mat_zero(ctx
, 0, 0);
2641 graph
->region
[i
].trivial
= trivial
;
2643 lp
= isl_basic_set_copy(graph
->lp
);
2644 sol
= isl_tab_basic_set_non_trivial_lexmin(lp
, 2, graph
->n
,
2645 graph
->region
, &check_conflict
, graph
);
2646 for (i
= 0; i
< graph
->n
; ++i
)
2647 isl_mat_free(graph
->region
[i
].trivial
);
2651 /* Extract the coefficients for the variables of "node" from "sol".
2653 * Each schedule coefficient c_i_x is represented as the difference
2654 * between two non-negative variables c_i_x^+ - c_i_x^-.
2655 * The c_i_x^- appear before their c_i_x^+ counterpart.
2656 * Furthermore, the order of these pairs is the opposite of that
2657 * of the corresponding coefficients.
2659 * Return c_i_x = c_i_x^+ - c_i_x^-
2661 static __isl_give isl_vec
*extract_var_coef(struct isl_sched_node
*node
,
2662 __isl_keep isl_vec
*sol
)
2670 csol
= isl_vec_alloc(isl_vec_get_ctx(sol
), node
->nvar
);
2674 pos
= 1 + node_var_coef_offset(node
);
2675 for (i
= 0; i
< node
->nvar
; ++i
)
2676 isl_int_sub(csol
->el
[node
->nvar
- 1 - i
],
2677 sol
->el
[pos
+ 2 * i
+ 1], sol
->el
[pos
+ 2 * i
]);
2682 /* Update the schedules of all nodes based on the given solution
2683 * of the LP problem.
2684 * The new row is added to the current band.
2685 * All possibly negative coefficients are encoded as a difference
2686 * of two non-negative variables, so we need to perform the subtraction
2689 * If coincident is set, then the caller guarantees that the new
2690 * row satisfies the coincidence constraints.
2692 static int update_schedule(struct isl_sched_graph
*graph
,
2693 __isl_take isl_vec
*sol
, int coincident
)
2696 isl_vec
*csol
= NULL
;
2701 isl_die(sol
->ctx
, isl_error_internal
,
2702 "no solution found", goto error
);
2703 if (graph
->n_total_row
>= graph
->max_row
)
2704 isl_die(sol
->ctx
, isl_error_internal
,
2705 "too many schedule rows", goto error
);
2707 for (i
= 0; i
< graph
->n
; ++i
) {
2708 struct isl_sched_node
*node
= &graph
->node
[i
];
2710 int row
= isl_mat_rows(node
->sched
);
2713 csol
= extract_var_coef(node
, sol
);
2717 isl_map_free(node
->sched_map
);
2718 node
->sched_map
= NULL
;
2719 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
2722 pos
= node_cst_coef_offset(node
);
2723 node
->sched
= isl_mat_set_element(node
->sched
,
2724 row
, 0, sol
->el
[1 + pos
]);
2725 pos
= node_par_coef_offset(node
);
2726 for (j
= 0; j
< node
->nparam
; ++j
)
2727 node
->sched
= isl_mat_set_element(node
->sched
,
2728 row
, 1 + j
, sol
->el
[1 + pos
+ j
]);
2729 for (j
= 0; j
< node
->nvar
; ++j
)
2730 node
->sched
= isl_mat_set_element(node
->sched
,
2731 row
, 1 + node
->nparam
+ j
, csol
->el
[j
]);
2732 node
->coincident
[graph
->n_total_row
] = coincident
;
2738 graph
->n_total_row
++;
2747 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2748 * and return this isl_aff.
2750 static __isl_give isl_aff
*extract_schedule_row(__isl_take isl_local_space
*ls
,
2751 struct isl_sched_node
*node
, int row
)
2759 aff
= isl_aff_zero_on_domain(ls
);
2760 isl_mat_get_element(node
->sched
, row
, 0, &v
);
2761 aff
= isl_aff_set_constant(aff
, v
);
2762 for (j
= 0; j
< node
->nparam
; ++j
) {
2763 isl_mat_get_element(node
->sched
, row
, 1 + j
, &v
);
2764 aff
= isl_aff_set_coefficient(aff
, isl_dim_param
, j
, v
);
2766 for (j
= 0; j
< node
->nvar
; ++j
) {
2767 isl_mat_get_element(node
->sched
, row
, 1 + node
->nparam
+ j
, &v
);
2768 aff
= isl_aff_set_coefficient(aff
, isl_dim_in
, j
, v
);
2776 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
2777 * and return this multi_aff.
2779 * The result is defined over the uncompressed node domain.
2781 static __isl_give isl_multi_aff
*node_extract_partial_schedule_multi_aff(
2782 struct isl_sched_node
*node
, int first
, int n
)
2786 isl_local_space
*ls
;
2793 nrow
= isl_mat_rows(node
->sched
);
2794 if (node
->compressed
)
2795 space
= isl_multi_aff_get_domain_space(node
->decompress
);
2797 space
= isl_space_copy(node
->space
);
2798 ls
= isl_local_space_from_space(isl_space_copy(space
));
2799 space
= isl_space_from_domain(space
);
2800 space
= isl_space_add_dims(space
, isl_dim_out
, n
);
2801 ma
= isl_multi_aff_zero(space
);
2803 for (i
= first
; i
< first
+ n
; ++i
) {
2804 aff
= extract_schedule_row(isl_local_space_copy(ls
), node
, i
);
2805 ma
= isl_multi_aff_set_aff(ma
, i
- first
, aff
);
2808 isl_local_space_free(ls
);
2810 if (node
->compressed
)
2811 ma
= isl_multi_aff_pullback_multi_aff(ma
,
2812 isl_multi_aff_copy(node
->compress
));
2817 /* Convert node->sched into a multi_aff and return this multi_aff.
2819 * The result is defined over the uncompressed node domain.
2821 static __isl_give isl_multi_aff
*node_extract_schedule_multi_aff(
2822 struct isl_sched_node
*node
)
2826 nrow
= isl_mat_rows(node
->sched
);
2827 return node_extract_partial_schedule_multi_aff(node
, 0, nrow
);
2830 /* Convert node->sched into a map and return this map.
2832 * The result is cached in node->sched_map, which needs to be released
2833 * whenever node->sched is updated.
2834 * It is defined over the uncompressed node domain.
2836 static __isl_give isl_map
*node_extract_schedule(struct isl_sched_node
*node
)
2838 if (!node
->sched_map
) {
2841 ma
= node_extract_schedule_multi_aff(node
);
2842 node
->sched_map
= isl_map_from_multi_aff(ma
);
2845 return isl_map_copy(node
->sched_map
);
2848 /* Construct a map that can be used to update a dependence relation
2849 * based on the current schedule.
2850 * That is, construct a map expressing that source and sink
2851 * are executed within the same iteration of the current schedule.
2852 * This map can then be intersected with the dependence relation.
2853 * This is not the most efficient way, but this shouldn't be a critical
2856 static __isl_give isl_map
*specializer(struct isl_sched_node
*src
,
2857 struct isl_sched_node
*dst
)
2859 isl_map
*src_sched
, *dst_sched
;
2861 src_sched
= node_extract_schedule(src
);
2862 dst_sched
= node_extract_schedule(dst
);
2863 return isl_map_apply_range(src_sched
, isl_map_reverse(dst_sched
));
2866 /* Intersect the domains of the nested relations in domain and range
2867 * of "umap" with "map".
2869 static __isl_give isl_union_map
*intersect_domains(
2870 __isl_take isl_union_map
*umap
, __isl_keep isl_map
*map
)
2872 isl_union_set
*uset
;
2874 umap
= isl_union_map_zip(umap
);
2875 uset
= isl_union_set_from_set(isl_map_wrap(isl_map_copy(map
)));
2876 umap
= isl_union_map_intersect_domain(umap
, uset
);
2877 umap
= isl_union_map_zip(umap
);
2881 /* Update the dependence relation of the given edge based
2882 * on the current schedule.
2883 * If the dependence is carried completely by the current schedule, then
2884 * it is removed from the edge_tables. It is kept in the list of edges
2885 * as otherwise all edge_tables would have to be recomputed.
2887 static int update_edge(struct isl_sched_graph
*graph
,
2888 struct isl_sched_edge
*edge
)
2893 id
= specializer(edge
->src
, edge
->dst
);
2894 edge
->map
= isl_map_intersect(edge
->map
, isl_map_copy(id
));
2898 if (edge
->tagged_condition
) {
2899 edge
->tagged_condition
=
2900 intersect_domains(edge
->tagged_condition
, id
);
2901 if (!edge
->tagged_condition
)
2904 if (edge
->tagged_validity
) {
2905 edge
->tagged_validity
=
2906 intersect_domains(edge
->tagged_validity
, id
);
2907 if (!edge
->tagged_validity
)
2911 empty
= isl_map_plain_is_empty(edge
->map
);
2915 graph_remove_edge(graph
, edge
);
2924 /* Does the domain of "umap" intersect "uset"?
2926 static int domain_intersects(__isl_keep isl_union_map
*umap
,
2927 __isl_keep isl_union_set
*uset
)
2931 umap
= isl_union_map_copy(umap
);
2932 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(uset
));
2933 empty
= isl_union_map_is_empty(umap
);
2934 isl_union_map_free(umap
);
2936 return empty
< 0 ? -1 : !empty
;
2939 /* Does the range of "umap" intersect "uset"?
2941 static int range_intersects(__isl_keep isl_union_map
*umap
,
2942 __isl_keep isl_union_set
*uset
)
2946 umap
= isl_union_map_copy(umap
);
2947 umap
= isl_union_map_intersect_range(umap
, isl_union_set_copy(uset
));
2948 empty
= isl_union_map_is_empty(umap
);
2949 isl_union_map_free(umap
);
2951 return empty
< 0 ? -1 : !empty
;
2954 /* Are the condition dependences of "edge" local with respect to
2955 * the current schedule?
2957 * That is, are domain and range of the condition dependences mapped
2958 * to the same point?
2960 * In other words, is the condition false?
2962 static int is_condition_false(struct isl_sched_edge
*edge
)
2964 isl_union_map
*umap
;
2965 isl_map
*map
, *sched
, *test
;
2968 empty
= isl_union_map_is_empty(edge
->tagged_condition
);
2969 if (empty
< 0 || empty
)
2972 umap
= isl_union_map_copy(edge
->tagged_condition
);
2973 umap
= isl_union_map_zip(umap
);
2974 umap
= isl_union_set_unwrap(isl_union_map_domain(umap
));
2975 map
= isl_map_from_union_map(umap
);
2977 sched
= node_extract_schedule(edge
->src
);
2978 map
= isl_map_apply_domain(map
, sched
);
2979 sched
= node_extract_schedule(edge
->dst
);
2980 map
= isl_map_apply_range(map
, sched
);
2982 test
= isl_map_identity(isl_map_get_space(map
));
2983 local
= isl_map_is_subset(map
, test
);
2990 /* For each conditional validity constraint that is adjacent
2991 * to a condition with domain in condition_source or range in condition_sink,
2992 * turn it into an unconditional validity constraint.
2994 static int unconditionalize_adjacent_validity(struct isl_sched_graph
*graph
,
2995 __isl_take isl_union_set
*condition_source
,
2996 __isl_take isl_union_set
*condition_sink
)
3000 condition_source
= isl_union_set_coalesce(condition_source
);
3001 condition_sink
= isl_union_set_coalesce(condition_sink
);
3003 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3005 isl_union_map
*validity
;
3007 if (!is_conditional_validity(&graph
->edge
[i
]))
3009 if (is_validity(&graph
->edge
[i
]))
3012 validity
= graph
->edge
[i
].tagged_validity
;
3013 adjacent
= domain_intersects(validity
, condition_sink
);
3014 if (adjacent
>= 0 && !adjacent
)
3015 adjacent
= range_intersects(validity
, condition_source
);
3021 set_validity(&graph
->edge
[i
]);
3024 isl_union_set_free(condition_source
);
3025 isl_union_set_free(condition_sink
);
3028 isl_union_set_free(condition_source
);
3029 isl_union_set_free(condition_sink
);
3033 /* Update the dependence relations of all edges based on the current schedule
3034 * and enforce conditional validity constraints that are adjacent
3035 * to satisfied condition constraints.
3037 * First check if any of the condition constraints are satisfied
3038 * (i.e., not local to the outer schedule) and keep track of
3039 * their domain and range.
3040 * Then update all dependence relations (which removes the non-local
3042 * Finally, if any condition constraints turned out to be satisfied,
3043 * then turn all adjacent conditional validity constraints into
3044 * unconditional validity constraints.
3046 static int update_edges(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3050 isl_union_set
*source
, *sink
;
3052 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3053 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3054 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3056 isl_union_set
*uset
;
3057 isl_union_map
*umap
;
3059 if (!is_condition(&graph
->edge
[i
]))
3061 if (is_local(&graph
->edge
[i
]))
3063 local
= is_condition_false(&graph
->edge
[i
]);
3071 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
3072 uset
= isl_union_map_domain(umap
);
3073 source
= isl_union_set_union(source
, uset
);
3075 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
3076 uset
= isl_union_map_range(umap
);
3077 sink
= isl_union_set_union(sink
, uset
);
3080 for (i
= graph
->n_edge
- 1; i
>= 0; --i
) {
3081 if (update_edge(graph
, &graph
->edge
[i
]) < 0)
3086 return unconditionalize_adjacent_validity(graph
, source
, sink
);
3088 isl_union_set_free(source
);
3089 isl_union_set_free(sink
);
3092 isl_union_set_free(source
);
3093 isl_union_set_free(sink
);
3097 static void next_band(struct isl_sched_graph
*graph
)
3099 graph
->band_start
= graph
->n_total_row
;
3102 /* Return the union of the universe domains of the nodes in "graph"
3103 * that satisfy "pred".
3105 static __isl_give isl_union_set
*isl_sched_graph_domain(isl_ctx
*ctx
,
3106 struct isl_sched_graph
*graph
,
3107 int (*pred
)(struct isl_sched_node
*node
, int data
), int data
)
3113 for (i
= 0; i
< graph
->n
; ++i
)
3114 if (pred(&graph
->node
[i
], data
))
3118 isl_die(ctx
, isl_error_internal
,
3119 "empty component", return NULL
);
3121 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
3122 dom
= isl_union_set_from_set(set
);
3124 for (i
= i
+ 1; i
< graph
->n
; ++i
) {
3125 if (!pred(&graph
->node
[i
], data
))
3127 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
3128 dom
= isl_union_set_union(dom
, isl_union_set_from_set(set
));
3134 /* Return a list of unions of universe domains, where each element
3135 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
3137 static __isl_give isl_union_set_list
*extract_sccs(isl_ctx
*ctx
,
3138 struct isl_sched_graph
*graph
)
3141 isl_union_set_list
*filters
;
3143 filters
= isl_union_set_list_alloc(ctx
, graph
->scc
);
3144 for (i
= 0; i
< graph
->scc
; ++i
) {
3147 dom
= isl_sched_graph_domain(ctx
, graph
, &node_scc_exactly
, i
);
3148 filters
= isl_union_set_list_add(filters
, dom
);
3154 /* Return a list of two unions of universe domains, one for the SCCs up
3155 * to and including graph->src_scc and another for the other SCCs.
3157 static __isl_give isl_union_set_list
*extract_split(isl_ctx
*ctx
,
3158 struct isl_sched_graph
*graph
)
3161 isl_union_set_list
*filters
;
3163 filters
= isl_union_set_list_alloc(ctx
, 2);
3164 dom
= isl_sched_graph_domain(ctx
, graph
,
3165 &node_scc_at_most
, graph
->src_scc
);
3166 filters
= isl_union_set_list_add(filters
, dom
);
3167 dom
= isl_sched_graph_domain(ctx
, graph
,
3168 &node_scc_at_least
, graph
->src_scc
+ 1);
3169 filters
= isl_union_set_list_add(filters
, dom
);
3174 /* Copy nodes that satisfy node_pred from the src dependence graph
3175 * to the dst dependence graph.
3177 static int copy_nodes(struct isl_sched_graph
*dst
, struct isl_sched_graph
*src
,
3178 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
3183 for (i
= 0; i
< src
->n
; ++i
) {
3186 if (!node_pred(&src
->node
[i
], data
))
3190 dst
->node
[j
].space
= isl_space_copy(src
->node
[i
].space
);
3191 dst
->node
[j
].compressed
= src
->node
[i
].compressed
;
3192 dst
->node
[j
].hull
= isl_set_copy(src
->node
[i
].hull
);
3193 dst
->node
[j
].compress
=
3194 isl_multi_aff_copy(src
->node
[i
].compress
);
3195 dst
->node
[j
].decompress
=
3196 isl_multi_aff_copy(src
->node
[i
].decompress
);
3197 dst
->node
[j
].nvar
= src
->node
[i
].nvar
;
3198 dst
->node
[j
].nparam
= src
->node
[i
].nparam
;
3199 dst
->node
[j
].sched
= isl_mat_copy(src
->node
[i
].sched
);
3200 dst
->node
[j
].sched_map
= isl_map_copy(src
->node
[i
].sched_map
);
3201 dst
->node
[j
].coincident
= src
->node
[i
].coincident
;
3202 dst
->node
[j
].sizes
= isl_multi_val_copy(src
->node
[i
].sizes
);
3203 dst
->node
[j
].max
= isl_vec_copy(src
->node
[i
].max
);
3206 if (!dst
->node
[j
].space
|| !dst
->node
[j
].sched
)
3208 if (dst
->node
[j
].compressed
&&
3209 (!dst
->node
[j
].hull
|| !dst
->node
[j
].compress
||
3210 !dst
->node
[j
].decompress
))
3217 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
3218 * to the dst dependence graph.
3219 * If the source or destination node of the edge is not in the destination
3220 * graph, then it must be a backward proximity edge and it should simply
3223 static int copy_edges(isl_ctx
*ctx
, struct isl_sched_graph
*dst
,
3224 struct isl_sched_graph
*src
,
3225 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
), int data
)
3228 enum isl_edge_type t
;
3231 for (i
= 0; i
< src
->n_edge
; ++i
) {
3232 struct isl_sched_edge
*edge
= &src
->edge
[i
];
3234 isl_union_map
*tagged_condition
;
3235 isl_union_map
*tagged_validity
;
3236 struct isl_sched_node
*dst_src
, *dst_dst
;
3238 if (!edge_pred(edge
, data
))
3241 if (isl_map_plain_is_empty(edge
->map
))
3244 dst_src
= graph_find_node(ctx
, dst
, edge
->src
->space
);
3245 dst_dst
= graph_find_node(ctx
, dst
, edge
->dst
->space
);
3246 if (!dst_src
|| !dst_dst
) {
3247 if (is_validity(edge
) || is_conditional_validity(edge
))
3248 isl_die(ctx
, isl_error_internal
,
3249 "backward (conditional) validity edge",
3254 map
= isl_map_copy(edge
->map
);
3255 tagged_condition
= isl_union_map_copy(edge
->tagged_condition
);
3256 tagged_validity
= isl_union_map_copy(edge
->tagged_validity
);
3258 dst
->edge
[dst
->n_edge
].src
= dst_src
;
3259 dst
->edge
[dst
->n_edge
].dst
= dst_dst
;
3260 dst
->edge
[dst
->n_edge
].map
= map
;
3261 dst
->edge
[dst
->n_edge
].tagged_condition
= tagged_condition
;
3262 dst
->edge
[dst
->n_edge
].tagged_validity
= tagged_validity
;
3263 dst
->edge
[dst
->n_edge
].types
= edge
->types
;
3266 if (edge
->tagged_condition
&& !tagged_condition
)
3268 if (edge
->tagged_validity
&& !tagged_validity
)
3271 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
3273 graph_find_edge(src
, t
, edge
->src
, edge
->dst
))
3275 if (graph_edge_table_add(ctx
, dst
, t
,
3276 &dst
->edge
[dst
->n_edge
- 1]) < 0)
3284 /* Compute the maximal number of variables over all nodes.
3285 * This is the maximal number of linearly independent schedule
3286 * rows that we need to compute.
3287 * Just in case we end up in a part of the dependence graph
3288 * with only lower-dimensional domains, we make sure we will
3289 * compute the required amount of extra linearly independent rows.
3291 static int compute_maxvar(struct isl_sched_graph
*graph
)
3296 for (i
= 0; i
< graph
->n
; ++i
) {
3297 struct isl_sched_node
*node
= &graph
->node
[i
];
3300 if (node_update_vmap(node
) < 0)
3302 nvar
= node
->nvar
+ graph
->n_row
- node
->rank
;
3303 if (nvar
> graph
->maxvar
)
3304 graph
->maxvar
= nvar
;
3310 /* Extract the subgraph of "graph" that consists of the node satisfying
3311 * "node_pred" and the edges satisfying "edge_pred" and store
3312 * the result in "sub".
3314 static int extract_sub_graph(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
3315 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
3316 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
3317 int data
, struct isl_sched_graph
*sub
)
3319 int i
, n
= 0, n_edge
= 0;
3322 for (i
= 0; i
< graph
->n
; ++i
)
3323 if (node_pred(&graph
->node
[i
], data
))
3325 for (i
= 0; i
< graph
->n_edge
; ++i
)
3326 if (edge_pred(&graph
->edge
[i
], data
))
3328 if (graph_alloc(ctx
, sub
, n
, n_edge
) < 0)
3330 if (copy_nodes(sub
, graph
, node_pred
, data
) < 0)
3332 if (graph_init_table(ctx
, sub
) < 0)
3334 for (t
= 0; t
<= isl_edge_last
; ++t
)
3335 sub
->max_edge
[t
] = graph
->max_edge
[t
];
3336 if (graph_init_edge_tables(ctx
, sub
) < 0)
3338 if (copy_edges(ctx
, sub
, graph
, edge_pred
, data
) < 0)
3340 sub
->n_row
= graph
->n_row
;
3341 sub
->max_row
= graph
->max_row
;
3342 sub
->n_total_row
= graph
->n_total_row
;
3343 sub
->band_start
= graph
->band_start
;
3348 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
3349 struct isl_sched_graph
*graph
);
3350 static __isl_give isl_schedule_node
*compute_schedule_wcc(
3351 isl_schedule_node
*node
, struct isl_sched_graph
*graph
);
3353 /* Compute a schedule for a subgraph of "graph". In particular, for
3354 * the graph composed of nodes that satisfy node_pred and edges that
3355 * that satisfy edge_pred.
3356 * If the subgraph is known to consist of a single component, then wcc should
3357 * be set and then we call compute_schedule_wcc on the constructed subgraph.
3358 * Otherwise, we call compute_schedule, which will check whether the subgraph
3361 * The schedule is inserted at "node" and the updated schedule node
3364 static __isl_give isl_schedule_node
*compute_sub_schedule(
3365 __isl_take isl_schedule_node
*node
, isl_ctx
*ctx
,
3366 struct isl_sched_graph
*graph
,
3367 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
3368 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
3371 struct isl_sched_graph split
= { 0 };
3373 if (extract_sub_graph(ctx
, graph
, node_pred
, edge_pred
, data
,
3378 node
= compute_schedule_wcc(node
, &split
);
3380 node
= compute_schedule(node
, &split
);
3382 graph_free(ctx
, &split
);
3385 graph_free(ctx
, &split
);
3386 return isl_schedule_node_free(node
);
3389 static int edge_scc_exactly(struct isl_sched_edge
*edge
, int scc
)
3391 return edge
->src
->scc
== scc
&& edge
->dst
->scc
== scc
;
3394 static int edge_dst_scc_at_most(struct isl_sched_edge
*edge
, int scc
)
3396 return edge
->dst
->scc
<= scc
;
3399 static int edge_src_scc_at_least(struct isl_sched_edge
*edge
, int scc
)
3401 return edge
->src
->scc
>= scc
;
3404 /* Reset the current band by dropping all its schedule rows.
3406 static int reset_band(struct isl_sched_graph
*graph
)
3411 drop
= graph
->n_total_row
- graph
->band_start
;
3412 graph
->n_total_row
-= drop
;
3413 graph
->n_row
-= drop
;
3415 for (i
= 0; i
< graph
->n
; ++i
) {
3416 struct isl_sched_node
*node
= &graph
->node
[i
];
3418 isl_map_free(node
->sched_map
);
3419 node
->sched_map
= NULL
;
3421 node
->sched
= isl_mat_drop_rows(node
->sched
,
3422 graph
->band_start
, drop
);
3431 /* Split the current graph into two parts and compute a schedule for each
3432 * part individually. In particular, one part consists of all SCCs up
3433 * to and including graph->src_scc, while the other part contains the other
3434 * SCCs. The split is enforced by a sequence node inserted at position "node"
3435 * in the schedule tree. Return the updated schedule node.
3436 * If either of these two parts consists of a sequence, then it is spliced
3437 * into the sequence containing the two parts.
3439 * The current band is reset. It would be possible to reuse
3440 * the previously computed rows as the first rows in the next
3441 * band, but recomputing them may result in better rows as we are looking
3442 * at a smaller part of the dependence graph.
3444 static __isl_give isl_schedule_node
*compute_split_schedule(
3445 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3449 isl_union_set_list
*filters
;
3454 if (reset_band(graph
) < 0)
3455 return isl_schedule_node_free(node
);
3459 ctx
= isl_schedule_node_get_ctx(node
);
3460 filters
= extract_split(ctx
, graph
);
3461 node
= isl_schedule_node_insert_sequence(node
, filters
);
3462 node
= isl_schedule_node_child(node
, 1);
3463 node
= isl_schedule_node_child(node
, 0);
3465 node
= compute_sub_schedule(node
, ctx
, graph
,
3466 &node_scc_at_least
, &edge_src_scc_at_least
,
3467 graph
->src_scc
+ 1, 0);
3468 is_seq
= isl_schedule_node_get_type(node
) == isl_schedule_node_sequence
;
3469 node
= isl_schedule_node_parent(node
);
3470 node
= isl_schedule_node_parent(node
);
3472 node
= isl_schedule_node_sequence_splice_child(node
, 1);
3473 node
= isl_schedule_node_child(node
, 0);
3474 node
= isl_schedule_node_child(node
, 0);
3475 node
= compute_sub_schedule(node
, ctx
, graph
,
3476 &node_scc_at_most
, &edge_dst_scc_at_most
,
3478 is_seq
= isl_schedule_node_get_type(node
) == isl_schedule_node_sequence
;
3479 node
= isl_schedule_node_parent(node
);
3480 node
= isl_schedule_node_parent(node
);
3482 node
= isl_schedule_node_sequence_splice_child(node
, 0);
3487 /* Insert a band node at position "node" in the schedule tree corresponding
3488 * to the current band in "graph". Mark the band node permutable
3489 * if "permutable" is set.
3490 * The partial schedules and the coincidence property are extracted
3491 * from the graph nodes.
3492 * Return the updated schedule node.
3494 static __isl_give isl_schedule_node
*insert_current_band(
3495 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
3501 isl_multi_pw_aff
*mpa
;
3502 isl_multi_union_pw_aff
*mupa
;
3508 isl_die(isl_schedule_node_get_ctx(node
), isl_error_internal
,
3509 "graph should have at least one node",
3510 return isl_schedule_node_free(node
));
3512 start
= graph
->band_start
;
3513 end
= graph
->n_total_row
;
3516 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[0], start
, n
);
3517 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3518 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3520 for (i
= 1; i
< graph
->n
; ++i
) {
3521 isl_multi_union_pw_aff
*mupa_i
;
3523 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[i
],
3525 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3526 mupa_i
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3527 mupa
= isl_multi_union_pw_aff_union_add(mupa
, mupa_i
);
3529 node
= isl_schedule_node_insert_partial_schedule(node
, mupa
);
3531 for (i
= 0; i
< n
; ++i
)
3532 node
= isl_schedule_node_band_member_set_coincident(node
, i
,
3533 graph
->node
[0].coincident
[start
+ i
]);
3534 node
= isl_schedule_node_band_set_permutable(node
, permutable
);
3539 /* Update the dependence relations based on the current schedule,
3540 * add the current band to "node" and then continue with the computation
3542 * Return the updated schedule node.
3544 static __isl_give isl_schedule_node
*compute_next_band(
3545 __isl_take isl_schedule_node
*node
,
3546 struct isl_sched_graph
*graph
, int permutable
)
3553 ctx
= isl_schedule_node_get_ctx(node
);
3554 if (update_edges(ctx
, graph
) < 0)
3555 return isl_schedule_node_free(node
);
3556 node
= insert_current_band(node
, graph
, permutable
);
3559 node
= isl_schedule_node_child(node
, 0);
3560 node
= compute_schedule(node
, graph
);
3561 node
= isl_schedule_node_parent(node
);
3566 /* Add the constraints "coef" derived from an edge from "node" to itself
3567 * to graph->lp in order to respect the dependences and to try and carry them.
3568 * "pos" is the sequence number of the edge that needs to be carried.
3569 * "coef" represents general constraints on coefficients (c_0, c_n, c_x)
3570 * of valid constraints for (y - x) with x and y instances of the node.
3572 * The constraints added to graph->lp need to enforce
3574 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3575 * = c_j_x (y - x) >= e_i
3577 * for each (x,y) in the dependence relation of the edge.
3578 * That is, (-e_i, 0, c_j_x) needs to be plugged in for (c_0, c_n, c_x),
3579 * taking into account that each coefficient in c_j_x is represented
3580 * as a pair of non-negative coefficients.
3582 static isl_stat
add_intra_constraints(struct isl_sched_graph
*graph
,
3583 struct isl_sched_node
*node
, __isl_take isl_basic_set
*coef
, int pos
)
3587 isl_dim_map
*dim_map
;
3590 return isl_stat_error
;
3592 ctx
= isl_basic_set_get_ctx(coef
);
3593 offset
= coef_var_offset(coef
);
3594 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, 1);
3595 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3596 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
3601 /* Add the constraints "coef" derived from an edge from "src" to "dst"
3602 * to graph->lp in order to respect the dependences and to try and carry them.
3603 * "pos" is the sequence number of the edge that needs to be carried or
3604 * -1 if no attempt should be made to carry the dependences.
3605 * "coef" represents general constraints on coefficients (c_0, c_n, c_x, c_y)
3606 * of valid constraints for (x, y) with x and y instances of "src" and "dst".
3608 * The constraints added to graph->lp need to enforce
3610 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3612 * for each (x,y) in the dependence relation of the edge or
3614 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= 0
3618 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, -c_j_x, c_k_x)
3620 * (c_k_0 - c_j_0, c_k_n - c_j_n, -c_j_x, c_k_x)
3621 * needs to be plugged in for (c_0, c_n, c_x, c_y),
3622 * taking into account that each coefficient in c_j_x and c_k_x is represented
3623 * as a pair of non-negative coefficients.
3625 static isl_stat
add_inter_constraints(struct isl_sched_graph
*graph
,
3626 struct isl_sched_node
*src
, struct isl_sched_node
*dst
,
3627 __isl_take isl_basic_set
*coef
, int pos
)
3631 isl_dim_map
*dim_map
;
3634 return isl_stat_error
;
3636 ctx
= isl_basic_set_get_ctx(coef
);
3637 offset
= coef_var_offset(coef
);
3638 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, 1);
3640 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3641 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
3646 /* Data structure collecting information used during the construction
3647 * of an LP for carrying dependences.
3649 * "intra" is a sequence of coefficient constraints for intra-node edges.
3650 * "inter" is a sequence of coefficient constraints for inter-node edges.
3653 isl_basic_set_list
*intra
;
3654 isl_basic_set_list
*inter
;
3657 /* Free all the data stored in "carry".
3659 static void isl_carry_clear(struct isl_carry
*carry
)
3661 isl_basic_set_list_free(carry
->intra
);
3662 isl_basic_set_list_free(carry
->inter
);
3665 /* Return a pointer to the node in "graph" that lives in "space".
3666 * If the requested node has been compressed, then "space"
3667 * corresponds to the compressed space.
3669 * First try and see if "space" is the space of an uncompressed node.
3670 * If so, return that node.
3671 * Otherwise, "space" was constructed by construct_compressed_id and
3672 * contains a user pointer pointing to the node in the tuple id.
3674 static struct isl_sched_node
*graph_find_compressed_node(isl_ctx
*ctx
,
3675 struct isl_sched_graph
*graph
, __isl_keep isl_space
*space
)
3678 struct isl_sched_node
*node
;
3683 node
= graph_find_node(ctx
, graph
, space
);
3687 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
3688 node
= isl_id_get_user(id
);
3694 if (!(node
>= &graph
->node
[0] && node
< &graph
->node
[graph
->n
]))
3695 isl_die(ctx
, isl_error_internal
,
3696 "space points to invalid node", return NULL
);
3701 /* Internal data structure for add_all_constraints.
3703 * "graph" is the schedule constraint graph for which an LP problem
3704 * is being constructed.
3705 * "carry_inter" indicates whether inter-node edges should be carried.
3706 * "pos" is the position of the next edge that needs to be carried.
3708 struct isl_add_all_constraints_data
{
3710 struct isl_sched_graph
*graph
;
3715 /* Add the constraints "coef" derived from an edge from a node to itself
3716 * to data->graph->lp in order to respect the dependences and
3717 * to try and carry them.
3719 * The space of "coef" is of the form
3721 * coefficients[[c_cst, c_n] -> S[c_x]]
3723 * with S[c_x] the (compressed) space of the node.
3724 * Extract the node from the space and call add_intra_constraints.
3726 static isl_stat
lp_add_intra(__isl_take isl_basic_set
*coef
, void *user
)
3728 struct isl_add_all_constraints_data
*data
= user
;
3730 struct isl_sched_node
*node
;
3732 space
= isl_basic_set_get_space(coef
);
3733 space
= isl_space_range(isl_space_unwrap(space
));
3734 node
= graph_find_compressed_node(data
->ctx
, data
->graph
, space
);
3735 isl_space_free(space
);
3736 return add_intra_constraints(data
->graph
, node
, coef
, data
->pos
++);
3739 /* Add the constraints "coef" derived from an edge from a node j
3740 * to a node k to data->graph->lp in order to respect the dependences and
3741 * to try and carry them (provided data->carry_inter is set).
3743 * The space of "coef" is of the form
3745 * coefficients[[c_cst, c_n] -> [S_j[c_x] -> S_k[c_y]]]
3747 * with S_j[c_x] and S_k[c_y] the (compressed) spaces of the nodes.
3748 * Extract the nodes from the space and call add_inter_constraints.
3750 static isl_stat
lp_add_inter(__isl_take isl_basic_set
*coef
, void *user
)
3752 struct isl_add_all_constraints_data
*data
= user
;
3753 isl_space
*space
, *dom
;
3754 struct isl_sched_node
*src
, *dst
;
3757 space
= isl_basic_set_get_space(coef
);
3758 space
= isl_space_unwrap(isl_space_range(isl_space_unwrap(space
)));
3759 dom
= isl_space_domain(isl_space_copy(space
));
3760 src
= graph_find_compressed_node(data
->ctx
, data
->graph
, dom
);
3761 isl_space_free(dom
);
3762 space
= isl_space_range(space
);
3763 dst
= graph_find_compressed_node(data
->ctx
, data
->graph
, space
);
3764 isl_space_free(space
);
3766 pos
= data
->carry_inter
? data
->pos
++ : -1;
3767 return add_inter_constraints(data
->graph
, src
, dst
, coef
, pos
);
3770 /* Add constraints to graph->lp that force all (conditional) validity
3771 * dependences to be respected and attempt to carry them.
3772 * "intra" is the sequence of coefficient constraints for intra-node edges.
3773 * "inter" is the sequence of coefficient constraints for inter-node edges.
3774 * "carry_inter" indicates whether inter-node edges should be carried or
3777 static isl_stat
add_all_constraints(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
3778 __isl_keep isl_basic_set_list
*intra
,
3779 __isl_keep isl_basic_set_list
*inter
, int carry_inter
)
3781 struct isl_add_all_constraints_data data
= { ctx
, graph
, carry_inter
};
3784 if (isl_basic_set_list_foreach(intra
, &lp_add_intra
, &data
) < 0)
3785 return isl_stat_error
;
3786 if (isl_basic_set_list_foreach(inter
, &lp_add_inter
, &data
) < 0)
3787 return isl_stat_error
;
3791 /* Internal data structure for count_all_constraints
3792 * for keeping track of the number of equality and inequality constraints.
3794 struct isl_sched_count
{
3799 /* Add the number of equality and inequality constraints of "bset"
3800 * to data->n_eq and data->n_ineq.
3802 static isl_stat
bset_update_count(__isl_take isl_basic_set
*bset
, void *user
)
3804 struct isl_sched_count
*data
= user
;
3806 data
->n_eq
+= isl_basic_set_n_equality(bset
);
3807 data
->n_ineq
+= isl_basic_set_n_inequality(bset
);
3808 isl_basic_set_free(bset
);
3813 /* Count the number of equality and inequality constraints
3814 * that will be added to the carry_lp problem.
3815 * We count each edge exactly once.
3816 * "intra" is the sequence of coefficient constraints for intra-node edges.
3817 * "inter" is the sequence of coefficient constraints for inter-node edges.
3819 static isl_stat
count_all_constraints(__isl_keep isl_basic_set_list
*intra
,
3820 __isl_keep isl_basic_set_list
*inter
, int *n_eq
, int *n_ineq
)
3822 struct isl_sched_count data
;
3824 data
.n_eq
= data
.n_ineq
= 0;
3825 if (isl_basic_set_list_foreach(inter
, &bset_update_count
, &data
) < 0)
3826 return isl_stat_error
;
3827 if (isl_basic_set_list_foreach(intra
, &bset_update_count
, &data
) < 0)
3828 return isl_stat_error
;
3831 *n_ineq
= data
.n_ineq
;
3836 /* Construct an LP problem for finding schedule coefficients
3837 * such that the schedule carries as many validity dependences as possible.
3838 * In particular, for each dependence i, we bound the dependence distance
3839 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3840 * of all e_i's. Dependences with e_i = 0 in the solution are simply
3841 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3842 * "intra" is the sequence of coefficient constraints for intra-node edges.
3843 * "inter" is the sequence of coefficient constraints for inter-node edges.
3844 * "n_edge" is the total number of edges.
3845 * "carry_inter" indicates whether inter-node edges should be carried or
3846 * only respected. That is, if "carry_inter" is not set, then
3847 * no e_i variables are introduced for the inter-node edges.
3849 * All variables of the LP are non-negative. The actual coefficients
3850 * may be negative, so each coefficient is represented as the difference
3851 * of two non-negative variables. The negative part always appears
3852 * immediately before the positive part.
3853 * Other than that, the variables have the following order
3855 * - sum of (1 - e_i) over all edges
3856 * - sum of all c_n coefficients
3857 * (unconstrained when computing non-parametric schedules)
3858 * - sum of positive and negative parts of all c_x coefficients
3862 * - positive and negative parts of c_i_x, in opposite order
3863 * - c_i_n (if parametric)
3866 * The constraints are those from the (validity) edges plus three equalities
3867 * to express the sums and n_edge inequalities to express e_i <= 1.
3869 static isl_stat
setup_carry_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
3870 int n_edge
, __isl_keep isl_basic_set_list
*intra
,
3871 __isl_keep isl_basic_set_list
*inter
, int carry_inter
)
3880 for (i
= 0; i
< graph
->n
; ++i
) {
3881 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
3882 node
->start
= total
;
3883 total
+= 1 + node
->nparam
+ 2 * node
->nvar
;
3886 if (count_all_constraints(intra
, inter
, &n_eq
, &n_ineq
) < 0)
3887 return isl_stat_error
;
3889 dim
= isl_space_set_alloc(ctx
, 0, total
);
3890 isl_basic_set_free(graph
->lp
);
3893 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
3894 graph
->lp
= isl_basic_set_set_rational(graph
->lp
);
3896 k
= isl_basic_set_alloc_equality(graph
->lp
);
3898 return isl_stat_error
;
3899 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
3900 isl_int_set_si(graph
->lp
->eq
[k
][0], -n_edge
);
3901 isl_int_set_si(graph
->lp
->eq
[k
][1], 1);
3902 for (i
= 0; i
< n_edge
; ++i
)
3903 isl_int_set_si(graph
->lp
->eq
[k
][4 + i
], 1);
3905 if (add_param_sum_constraint(graph
, 1) < 0)
3906 return isl_stat_error
;
3907 if (add_var_sum_constraint(graph
, 2) < 0)
3908 return isl_stat_error
;
3910 for (i
= 0; i
< n_edge
; ++i
) {
3911 k
= isl_basic_set_alloc_inequality(graph
->lp
);
3913 return isl_stat_error
;
3914 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
3915 isl_int_set_si(graph
->lp
->ineq
[k
][4 + i
], -1);
3916 isl_int_set_si(graph
->lp
->ineq
[k
][0], 1);
3919 if (add_all_constraints(ctx
, graph
, intra
, inter
, carry_inter
) < 0)
3920 return isl_stat_error
;
3925 static __isl_give isl_schedule_node
*compute_component_schedule(
3926 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
3929 /* If the schedule_split_scaled option is set and if the linear
3930 * parts of the scheduling rows for all nodes in the graphs have
3931 * a non-trivial common divisor, then remove this
3932 * common divisor from the linear part.
3933 * Otherwise, insert a band node directly and continue with
3934 * the construction of the schedule.
3936 * If a non-trivial common divisor is found, then
3937 * the linear part is reduced and the remainder is ignored.
3938 * The pieces of the graph that are assigned different remainders
3939 * form (groups of) strongly connected components within
3940 * the scaled down band. If needed, they can therefore
3941 * be ordered along this remainder in a sequence node.
3942 * However, this ordering is not enforced here in order to allow
3943 * the scheduler to combine some of the strongly connected components.
3945 static __isl_give isl_schedule_node
*split_scaled(
3946 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3956 ctx
= isl_schedule_node_get_ctx(node
);
3957 if (!ctx
->opt
->schedule_split_scaled
)
3958 return compute_next_band(node
, graph
, 0);
3960 return compute_next_band(node
, graph
, 0);
3963 isl_int_init(gcd_i
);
3965 isl_int_set_si(gcd
, 0);
3967 row
= isl_mat_rows(graph
->node
[0].sched
) - 1;
3969 for (i
= 0; i
< graph
->n
; ++i
) {
3970 struct isl_sched_node
*node
= &graph
->node
[i
];
3971 int cols
= isl_mat_cols(node
->sched
);
3973 isl_seq_gcd(node
->sched
->row
[row
] + 1, cols
- 1, &gcd_i
);
3974 isl_int_gcd(gcd
, gcd
, gcd_i
);
3977 isl_int_clear(gcd_i
);
3979 if (isl_int_cmp_si(gcd
, 1) <= 0) {
3981 return compute_next_band(node
, graph
, 0);
3984 for (i
= 0; i
< graph
->n
; ++i
) {
3985 struct isl_sched_node
*node
= &graph
->node
[i
];
3987 isl_int_fdiv_q(node
->sched
->row
[row
][0],
3988 node
->sched
->row
[row
][0], gcd
);
3989 isl_int_mul(node
->sched
->row
[row
][0],
3990 node
->sched
->row
[row
][0], gcd
);
3991 node
->sched
= isl_mat_scale_down_row(node
->sched
, row
, gcd
);
3998 return compute_next_band(node
, graph
, 0);
4001 return isl_schedule_node_free(node
);
4004 /* Is the schedule row "sol" trivial on node "node"?
4005 * That is, is the solution zero on the dimensions linearly independent of
4006 * the previously found solutions?
4007 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
4009 * Each coefficient is represented as the difference between
4010 * two non-negative values in "sol".
4011 * We construct the schedule row s and check if it is linearly
4012 * independent of previously computed schedule rows
4013 * by computing T s, with T the linear combinations that are zero
4014 * on linearly dependent schedule rows.
4015 * If the result consists of all zeros, then the solution is trivial.
4017 static int is_trivial(struct isl_sched_node
*node
, __isl_keep isl_vec
*sol
)
4024 if (node
->nvar
== node
->rank
)
4027 node_sol
= extract_var_coef(node
, sol
);
4028 node_sol
= isl_mat_vec_product(isl_mat_copy(node
->indep
), node_sol
);
4032 trivial
= isl_seq_first_non_zero(node_sol
->el
,
4033 node
->nvar
- node
->rank
) == -1;
4035 isl_vec_free(node_sol
);
4040 /* Is the schedule row "sol" trivial on any node where it should
4042 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
4044 static int is_any_trivial(struct isl_sched_graph
*graph
,
4045 __isl_keep isl_vec
*sol
)
4049 for (i
= 0; i
< graph
->n
; ++i
) {
4050 struct isl_sched_node
*node
= &graph
->node
[i
];
4053 if (!needs_row(graph
, node
))
4055 trivial
= is_trivial(node
, sol
);
4056 if (trivial
< 0 || trivial
)
4063 /* Does the schedule represented by "sol" perform loop coalescing on "node"?
4064 * If so, return the position of the coalesced dimension.
4065 * Otherwise, return node->nvar or -1 on error.
4067 * In particular, look for pairs of coefficients c_i and c_j such that
4068 * |c_j/c_i| > ceil(size_i/2), i.e., |c_j| > |c_i * ceil(size_i/2)|.
4069 * If any such pair is found, then return i.
4070 * If size_i is infinity, then no check on c_i needs to be performed.
4072 static int find_node_coalescing(struct isl_sched_node
*node
,
4073 __isl_keep isl_vec
*sol
)
4079 if (node
->nvar
<= 1)
4082 csol
= extract_var_coef(node
, sol
);
4086 for (i
= 0; i
< node
->nvar
; ++i
) {
4089 if (isl_int_is_zero(csol
->el
[i
]))
4091 v
= isl_multi_val_get_val(node
->sizes
, i
);
4094 if (!isl_val_is_int(v
)) {
4098 v
= isl_val_div_ui(v
, 2);
4099 v
= isl_val_ceil(v
);
4102 isl_int_mul(max
, v
->n
, csol
->el
[i
]);
4105 for (j
= 0; j
< node
->nvar
; ++j
) {
4108 if (isl_int_abs_gt(csol
->el
[j
], max
))
4124 /* Force the schedule coefficient at position "pos" of "node" to be zero
4126 * The coefficient is encoded as the difference between two non-negative
4127 * variables. Force these two variables to have the same value.
4129 static __isl_give isl_tab_lexmin
*zero_out_node_coef(
4130 __isl_take isl_tab_lexmin
*tl
, struct isl_sched_node
*node
, int pos
)
4136 ctx
= isl_space_get_ctx(node
->space
);
4137 dim
= isl_tab_lexmin_dim(tl
);
4139 return isl_tab_lexmin_free(tl
);
4140 eq
= isl_vec_alloc(ctx
, 1 + dim
);
4141 eq
= isl_vec_clr(eq
);
4143 return isl_tab_lexmin_free(tl
);
4145 pos
= 1 + node_var_coef_pos(node
, pos
);
4146 isl_int_set_si(eq
->el
[pos
], 1);
4147 isl_int_set_si(eq
->el
[pos
+ 1], -1);
4148 tl
= isl_tab_lexmin_add_eq(tl
, eq
->el
);
4154 /* Return the lexicographically smallest rational point in the basic set
4155 * from which "tl" was constructed, double checking that this input set
4158 static __isl_give isl_vec
*non_empty_solution(__isl_keep isl_tab_lexmin
*tl
)
4162 sol
= isl_tab_lexmin_get_solution(tl
);
4166 isl_die(isl_vec_get_ctx(sol
), isl_error_internal
,
4167 "error in schedule construction",
4168 return isl_vec_free(sol
));
4172 /* Does the solution "sol" of the LP problem constructed by setup_carry_lp
4173 * carry any of the "n_edge" groups of dependences?
4174 * The value in the first position is the sum of (1 - e_i) over all "n_edge"
4175 * edges, with 0 <= e_i <= 1 equal to 1 when the dependences represented
4176 * by the edge are carried by the solution.
4177 * If the sum of the (1 - e_i) is smaller than "n_edge" then at least
4178 * one of those is carried.
4180 * Note that despite the fact that the problem is solved using a rational
4181 * solver, the solution is guaranteed to be integral.
4182 * Specifically, the dependence distance lower bounds e_i (and therefore
4183 * also their sum) are integers. See Lemma 5 of [1].
4185 * Any potential denominator of the sum is cleared by this function.
4186 * The denominator is not relevant for any of the other elements
4189 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4190 * Problem, Part II: Multi-Dimensional Time.
4191 * In Intl. Journal of Parallel Programming, 1992.
4193 static int carries_dependences(__isl_keep isl_vec
*sol
, int n_edge
)
4195 isl_int_divexact(sol
->el
[1], sol
->el
[1], sol
->el
[0]);
4196 isl_int_set_si(sol
->el
[0], 1);
4197 return isl_int_cmp_si(sol
->el
[1], n_edge
) < 0;
4200 /* Return the lexicographically smallest rational point in "lp",
4201 * assuming that all variables are non-negative and performing some
4202 * additional sanity checks.
4203 * If "want_integral" is set, then compute the lexicographically smallest
4204 * integer point instead.
4205 * In particular, "lp" should not be empty by construction.
4206 * Double check that this is the case.
4207 * If dependences are not carried for any of the "n_edge" edges,
4208 * then return an empty vector.
4210 * If the schedule_treat_coalescing option is set and
4211 * if the computed schedule performs loop coalescing on a given node,
4212 * i.e., if it is of the form
4214 * c_i i + c_j j + ...
4216 * with |c_j/c_i| >= size_i, then force the coefficient c_i to be zero
4217 * to cut out this solution. Repeat this process until no more loop
4218 * coalescing occurs or until no more dependences can be carried.
4219 * In the latter case, revert to the previously computed solution.
4221 * If the caller requests an integral solution and if coalescing should
4222 * be treated, then perform the coalescing treatment first as
4223 * an integral solution computed before coalescing treatment
4224 * would carry the same number of edges and would therefore probably
4225 * also be coalescing.
4227 * To allow the coalescing treatment to be performed first,
4228 * the initial solution is allowed to be rational and it is only
4229 * cut out (if needed) in the next iteration, if no coalescing measures
4232 static __isl_give isl_vec
*non_neg_lexmin(struct isl_sched_graph
*graph
,
4233 __isl_take isl_basic_set
*lp
, int n_edge
, int want_integral
)
4238 isl_vec
*sol
, *prev
= NULL
;
4239 int treat_coalescing
;
4243 ctx
= isl_basic_set_get_ctx(lp
);
4244 treat_coalescing
= isl_options_get_schedule_treat_coalescing(ctx
);
4245 tl
= isl_tab_lexmin_from_basic_set(lp
);
4252 tl
= isl_tab_lexmin_cut_to_integer(tl
);
4253 sol
= non_empty_solution(tl
);
4257 integral
= isl_int_is_one(sol
->el
[0]);
4258 if (!carries_dependences(sol
, n_edge
)) {
4260 prev
= isl_vec_alloc(ctx
, 0);
4265 prev
= isl_vec_free(prev
);
4266 cut
= want_integral
&& !integral
;
4269 if (!treat_coalescing
)
4271 for (i
= 0; i
< graph
->n
; ++i
) {
4272 struct isl_sched_node
*node
= &graph
->node
[i
];
4274 pos
= find_node_coalescing(node
, sol
);
4277 if (pos
< node
->nvar
)
4282 tl
= zero_out_node_coef(tl
, &graph
->node
[i
], pos
);
4287 isl_tab_lexmin_free(tl
);
4291 isl_tab_lexmin_free(tl
);
4297 /* If "edge" is an edge from a node to itself, then add the corresponding
4298 * dependence relation to "umap".
4299 * If "node" has been compressed, then the dependence relation
4300 * is also compressed first.
4302 static __isl_give isl_union_map
*add_intra(__isl_take isl_union_map
*umap
,
4303 struct isl_sched_edge
*edge
)
4306 struct isl_sched_node
*node
= edge
->src
;
4308 if (edge
->src
!= edge
->dst
)
4311 map
= isl_map_copy(edge
->map
);
4312 if (node
->compressed
) {
4313 map
= isl_map_preimage_domain_multi_aff(map
,
4314 isl_multi_aff_copy(node
->decompress
));
4315 map
= isl_map_preimage_range_multi_aff(map
,
4316 isl_multi_aff_copy(node
->decompress
));
4318 umap
= isl_union_map_add_map(umap
, map
);
4322 /* If "edge" is an edge from a node to another node, then add the corresponding
4323 * dependence relation to "umap".
4324 * If the source or destination nodes of "edge" have been compressed,
4325 * then the dependence relation is also compressed first.
4327 static __isl_give isl_union_map
*add_inter(__isl_take isl_union_map
*umap
,
4328 struct isl_sched_edge
*edge
)
4332 if (edge
->src
== edge
->dst
)
4335 map
= isl_map_copy(edge
->map
);
4336 if (edge
->src
->compressed
)
4337 map
= isl_map_preimage_domain_multi_aff(map
,
4338 isl_multi_aff_copy(edge
->src
->decompress
));
4339 if (edge
->dst
->compressed
)
4340 map
= isl_map_preimage_range_multi_aff(map
,
4341 isl_multi_aff_copy(edge
->dst
->decompress
));
4342 umap
= isl_union_map_add_map(umap
, map
);
4346 /* For each (conditional) validity edge in "graph",
4347 * add the corresponding dependence relation using "add"
4348 * to a collection of dependence relations and return the result.
4349 * If "coincidence" is set, then coincidence edges are considered as well.
4351 static __isl_give isl_union_map
*collect_validity(struct isl_sched_graph
*graph
,
4352 __isl_give isl_union_map
*(*add
)(__isl_take isl_union_map
*umap
,
4353 struct isl_sched_edge
*edge
), int coincidence
)
4357 isl_union_map
*umap
;
4359 space
= isl_space_copy(graph
->node
[0].space
);
4360 umap
= isl_union_map_empty(space
);
4362 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4363 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
4365 if (!is_any_validity(edge
) &&
4366 (!coincidence
|| !is_coincidence(edge
)))
4369 umap
= add(umap
, edge
);
4375 /* For each dependence relation on a (conditional) validity edge
4376 * from a node to itself,
4377 * construct the set of coefficients of valid constraints for elements
4378 * in that dependence relation and collect the results.
4379 * If "coincidence" is set, then coincidence edges are considered as well.
4381 * In particular, for each dependence relation R, constraints
4382 * on coefficients (c_0, c_n, c_x) are constructed such that
4384 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
4386 * This computation is essentially the same as that performed
4387 * by intra_coefficients, except that it operates on multiple
4390 * Note that if a dependence relation is a union of basic maps,
4391 * then each basic map needs to be treated individually as it may only
4392 * be possible to carry the dependences expressed by some of those
4393 * basic maps and not all of them.
4394 * The collected validity constraints are therefore not coalesced and
4395 * it is assumed that they are not coalesced automatically.
4396 * Duplicate basic maps can be removed, however.
4397 * In particular, if the same basic map appears as a disjunct
4398 * in multiple edges, then it only needs to be carried once.
4400 static __isl_give isl_basic_set_list
*collect_intra_validity(
4401 struct isl_sched_graph
*graph
, int coincidence
)
4403 isl_union_map
*intra
;
4404 isl_union_set
*delta
;
4405 isl_basic_set_list
*list
;
4407 intra
= collect_validity(graph
, &add_intra
, coincidence
);
4408 delta
= isl_union_map_deltas(intra
);
4409 delta
= isl_union_set_remove_divs(delta
);
4410 list
= isl_union_set_get_basic_set_list(delta
);
4411 isl_union_set_free(delta
);
4413 return isl_basic_set_list_coefficients(list
);
4416 /* For each dependence relation on a (conditional) validity edge
4417 * from a node to some other node,
4418 * construct the set of coefficients of valid constraints for elements
4419 * in that dependence relation and collect the results.
4420 * If "coincidence" is set, then coincidence edges are considered as well.
4422 * In particular, for each dependence relation R, constraints
4423 * on coefficients (c_0, c_n, c_x, c_y) are constructed such that
4425 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
4427 * This computation is essentially the same as that performed
4428 * by inter_coefficients, except that it operates on multiple
4431 * Note that if a dependence relation is a union of basic maps,
4432 * then each basic map needs to be treated individually as it may only
4433 * be possible to carry the dependences expressed by some of those
4434 * basic maps and not all of them.
4435 * The collected validity constraints are therefore not coalesced and
4436 * it is assumed that they are not coalesced automatically.
4437 * Duplicate basic maps can be removed, however.
4438 * In particular, if the same basic map appears as a disjunct
4439 * in multiple edges, then it only needs to be carried once.
4441 static __isl_give isl_basic_set_list
*collect_inter_validity(
4442 struct isl_sched_graph
*graph
, int coincidence
)
4444 isl_union_map
*inter
;
4445 isl_union_set
*wrap
;
4446 isl_basic_set_list
*list
;
4448 inter
= collect_validity(graph
, &add_inter
, coincidence
);
4449 inter
= isl_union_map_remove_divs(inter
);
4450 wrap
= isl_union_map_wrap(inter
);
4451 list
= isl_union_set_get_basic_set_list(wrap
);
4452 isl_union_set_free(wrap
);
4453 return isl_basic_set_list_coefficients(list
);
4456 /* Construct an LP problem for finding schedule coefficients
4457 * such that the schedule carries as many of the "n_edge" groups of
4458 * dependences as possible based on the corresponding coefficient
4459 * constraints and return the lexicographically smallest non-trivial solution.
4460 * "intra" is the sequence of coefficient constraints for intra-node edges.
4461 * "inter" is the sequence of coefficient constraints for inter-node edges.
4462 * If "want_integral" is set, then compute an integral solution
4463 * for the coefficients rather than using the numerators
4464 * of a rational solution.
4465 * "carry_inter" indicates whether inter-node edges should be carried or
4468 * If none of the "n_edge" groups can be carried
4469 * then return an empty vector.
4471 static __isl_give isl_vec
*compute_carrying_sol_coef(isl_ctx
*ctx
,
4472 struct isl_sched_graph
*graph
, int n_edge
,
4473 __isl_keep isl_basic_set_list
*intra
,
4474 __isl_keep isl_basic_set_list
*inter
, int want_integral
,
4479 if (setup_carry_lp(ctx
, graph
, n_edge
, intra
, inter
, carry_inter
) < 0)
4482 lp
= isl_basic_set_copy(graph
->lp
);
4483 return non_neg_lexmin(graph
, lp
, n_edge
, want_integral
);
4486 /* Construct an LP problem for finding schedule coefficients
4487 * such that the schedule carries as many of the validity dependences
4489 * return the lexicographically smallest non-trivial solution.
4490 * If "fallback" is set, then the carrying is performed as a fallback
4491 * for the Pluto-like scheduler.
4492 * If "coincidence" is set, then try and carry coincidence edges as well.
4494 * The variable "n_edge" stores the number of groups that should be carried.
4495 * If none of the "n_edge" groups can be carried
4496 * then return an empty vector.
4497 * If, moreover, "n_edge" is zero, then the LP problem does not even
4498 * need to be constructed.
4500 * If a fallback solution is being computed, then compute an integral solution
4501 * for the coefficients rather than using the numerators
4502 * of a rational solution.
4504 * If a fallback solution is being computed, if there are any intra-node
4505 * dependences, and if requested by the user, then first try
4506 * to only carry those intra-node dependences.
4507 * If this fails to carry any dependences, then try again
4508 * with the inter-node dependences included.
4510 static __isl_give isl_vec
*compute_carrying_sol(isl_ctx
*ctx
,
4511 struct isl_sched_graph
*graph
, int fallback
, int coincidence
)
4513 int n_intra
, n_inter
;
4515 struct isl_carry carry
= { 0 };
4518 carry
.intra
= collect_intra_validity(graph
, coincidence
);
4519 carry
.inter
= collect_inter_validity(graph
, coincidence
);
4520 if (!carry
.intra
|| !carry
.inter
)
4522 n_intra
= isl_basic_set_list_n_basic_set(carry
.intra
);
4523 n_inter
= isl_basic_set_list_n_basic_set(carry
.inter
);
4525 if (fallback
&& n_intra
> 0 &&
4526 isl_options_get_schedule_carry_self_first(ctx
)) {
4527 sol
= compute_carrying_sol_coef(ctx
, graph
, n_intra
,
4528 carry
.intra
, carry
.inter
, fallback
, 0);
4529 if (!sol
|| sol
->size
!= 0 || n_inter
== 0) {
4530 isl_carry_clear(&carry
);
4536 n_edge
= n_intra
+ n_inter
;
4538 isl_carry_clear(&carry
);
4539 return isl_vec_alloc(ctx
, 0);
4542 sol
= compute_carrying_sol_coef(ctx
, graph
, n_edge
,
4543 carry
.intra
, carry
.inter
, fallback
, 1);
4544 isl_carry_clear(&carry
);
4547 isl_carry_clear(&carry
);
4551 /* Construct a schedule row for each node such that as many validity dependences
4552 * as possible are carried and then continue with the next band.
4553 * If "fallback" is set, then the carrying is performed as a fallback
4554 * for the Pluto-like scheduler.
4555 * If "coincidence" is set, then try and carry coincidence edges as well.
4557 * If there are no validity dependences, then no dependence can be carried and
4558 * the procedure is guaranteed to fail. If there is more than one component,
4559 * then try computing a schedule on each component separately
4560 * to prevent or at least postpone this failure.
4562 * If a schedule row is computed, then check that dependences are carried
4563 * for at least one of the edges.
4565 * If the computed schedule row turns out to be trivial on one or
4566 * more nodes where it should not be trivial, then we throw it away
4567 * and try again on each component separately.
4569 * If there is only one component, then we accept the schedule row anyway,
4570 * but we do not consider it as a complete row and therefore do not
4571 * increment graph->n_row. Note that the ranks of the nodes that
4572 * do get a non-trivial schedule part will get updated regardless and
4573 * graph->maxvar is computed based on these ranks. The test for
4574 * whether more schedule rows are required in compute_schedule_wcc
4575 * is therefore not affected.
4577 * Insert a band corresponding to the schedule row at position "node"
4578 * of the schedule tree and continue with the construction of the schedule.
4579 * This insertion and the continued construction is performed by split_scaled
4580 * after optionally checking for non-trivial common divisors.
4582 static __isl_give isl_schedule_node
*carry(__isl_take isl_schedule_node
*node
,
4583 struct isl_sched_graph
*graph
, int fallback
, int coincidence
)
4592 ctx
= isl_schedule_node_get_ctx(node
);
4593 sol
= compute_carrying_sol(ctx
, graph
, fallback
, coincidence
);
4595 return isl_schedule_node_free(node
);
4596 if (sol
->size
== 0) {
4599 return compute_component_schedule(node
, graph
, 1);
4600 isl_die(ctx
, isl_error_unknown
, "unable to carry dependences",
4601 return isl_schedule_node_free(node
));
4604 trivial
= is_any_trivial(graph
, sol
);
4606 sol
= isl_vec_free(sol
);
4607 } else if (trivial
&& graph
->scc
> 1) {
4609 return compute_component_schedule(node
, graph
, 1);
4612 if (update_schedule(graph
, sol
, 0) < 0)
4613 return isl_schedule_node_free(node
);
4617 return split_scaled(node
, graph
);
4620 /* Construct a schedule row for each node such that as many validity dependences
4621 * as possible are carried and then continue with the next band.
4622 * Do so as a fallback for the Pluto-like scheduler.
4623 * If "coincidence" is set, then try and carry coincidence edges as well.
4625 static __isl_give isl_schedule_node
*carry_fallback(
4626 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
4629 return carry(node
, graph
, 1, coincidence
);
4632 /* Construct a schedule row for each node such that as many validity dependences
4633 * as possible are carried and then continue with the next band.
4634 * Do so for the case where the Feautrier scheduler was selected
4637 static __isl_give isl_schedule_node
*carry_feautrier(
4638 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4640 return carry(node
, graph
, 0, 0);
4643 /* Construct a schedule row for each node such that as many validity dependences
4644 * as possible are carried and then continue with the next band.
4645 * Do so as a fallback for the Pluto-like scheduler.
4647 static __isl_give isl_schedule_node
*carry_dependences(
4648 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4650 return carry_fallback(node
, graph
, 0);
4653 /* Construct a schedule row for each node such that as many validity or
4654 * coincidence dependences as possible are carried and
4655 * then continue with the next band.
4656 * Do so as a fallback for the Pluto-like scheduler.
4658 static __isl_give isl_schedule_node
*carry_coincidence(
4659 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4661 return carry_fallback(node
, graph
, 1);
4664 /* Topologically sort statements mapped to the same schedule iteration
4665 * and add insert a sequence node in front of "node"
4666 * corresponding to this order.
4667 * If "initialized" is set, then it may be assumed that compute_maxvar
4668 * has been called on the current band. Otherwise, call
4669 * compute_maxvar if and before carry_dependences gets called.
4671 * If it turns out to be impossible to sort the statements apart,
4672 * because different dependences impose different orderings
4673 * on the statements, then we extend the schedule such that
4674 * it carries at least one more dependence.
4676 static __isl_give isl_schedule_node
*sort_statements(
4677 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
4681 isl_union_set_list
*filters
;
4686 ctx
= isl_schedule_node_get_ctx(node
);
4688 isl_die(ctx
, isl_error_internal
,
4689 "graph should have at least one node",
4690 return isl_schedule_node_free(node
));
4695 if (update_edges(ctx
, graph
) < 0)
4696 return isl_schedule_node_free(node
);
4698 if (graph
->n_edge
== 0)
4701 if (detect_sccs(ctx
, graph
) < 0)
4702 return isl_schedule_node_free(node
);
4705 if (graph
->scc
< graph
->n
) {
4706 if (!initialized
&& compute_maxvar(graph
) < 0)
4707 return isl_schedule_node_free(node
);
4708 return carry_dependences(node
, graph
);
4711 filters
= extract_sccs(ctx
, graph
);
4712 node
= isl_schedule_node_insert_sequence(node
, filters
);
4717 /* Are there any (non-empty) (conditional) validity edges in the graph?
4719 static int has_validity_edges(struct isl_sched_graph
*graph
)
4723 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4726 empty
= isl_map_plain_is_empty(graph
->edge
[i
].map
);
4731 if (is_any_validity(&graph
->edge
[i
]))
4738 /* Should we apply a Feautrier step?
4739 * That is, did the user request the Feautrier algorithm and are
4740 * there any validity dependences (left)?
4742 static int need_feautrier_step(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
4744 if (ctx
->opt
->schedule_algorithm
!= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
)
4747 return has_validity_edges(graph
);
4750 /* Compute a schedule for a connected dependence graph using Feautrier's
4751 * multi-dimensional scheduling algorithm and return the updated schedule node.
4753 * The original algorithm is described in [1].
4754 * The main idea is to minimize the number of scheduling dimensions, by
4755 * trying to satisfy as many dependences as possible per scheduling dimension.
4757 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4758 * Problem, Part II: Multi-Dimensional Time.
4759 * In Intl. Journal of Parallel Programming, 1992.
4761 static __isl_give isl_schedule_node
*compute_schedule_wcc_feautrier(
4762 isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4764 return carry_feautrier(node
, graph
);
4767 /* Turn off the "local" bit on all (condition) edges.
4769 static void clear_local_edges(struct isl_sched_graph
*graph
)
4773 for (i
= 0; i
< graph
->n_edge
; ++i
)
4774 if (is_condition(&graph
->edge
[i
]))
4775 clear_local(&graph
->edge
[i
]);
4778 /* Does "graph" have both condition and conditional validity edges?
4780 static int need_condition_check(struct isl_sched_graph
*graph
)
4783 int any_condition
= 0;
4784 int any_conditional_validity
= 0;
4786 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4787 if (is_condition(&graph
->edge
[i
]))
4789 if (is_conditional_validity(&graph
->edge
[i
]))
4790 any_conditional_validity
= 1;
4793 return any_condition
&& any_conditional_validity
;
4796 /* Does "graph" contain any coincidence edge?
4798 static int has_any_coincidence(struct isl_sched_graph
*graph
)
4802 for (i
= 0; i
< graph
->n_edge
; ++i
)
4803 if (is_coincidence(&graph
->edge
[i
]))
4809 /* Extract the final schedule row as a map with the iteration domain
4810 * of "node" as domain.
4812 static __isl_give isl_map
*final_row(struct isl_sched_node
*node
)
4817 row
= isl_mat_rows(node
->sched
) - 1;
4818 ma
= node_extract_partial_schedule_multi_aff(node
, row
, 1);
4819 return isl_map_from_multi_aff(ma
);
4822 /* Is the conditional validity dependence in the edge with index "edge_index"
4823 * violated by the latest (i.e., final) row of the schedule?
4824 * That is, is i scheduled after j
4825 * for any conditional validity dependence i -> j?
4827 static int is_violated(struct isl_sched_graph
*graph
, int edge_index
)
4829 isl_map
*src_sched
, *dst_sched
, *map
;
4830 struct isl_sched_edge
*edge
= &graph
->edge
[edge_index
];
4833 src_sched
= final_row(edge
->src
);
4834 dst_sched
= final_row(edge
->dst
);
4835 map
= isl_map_copy(edge
->map
);
4836 map
= isl_map_apply_domain(map
, src_sched
);
4837 map
= isl_map_apply_range(map
, dst_sched
);
4838 map
= isl_map_order_gt(map
, isl_dim_in
, 0, isl_dim_out
, 0);
4839 empty
= isl_map_is_empty(map
);
4848 /* Does "graph" have any satisfied condition edges that
4849 * are adjacent to the conditional validity constraint with
4850 * domain "conditional_source" and range "conditional_sink"?
4852 * A satisfied condition is one that is not local.
4853 * If a condition was forced to be local already (i.e., marked as local)
4854 * then there is no need to check if it is in fact local.
4856 * Additionally, mark all adjacent condition edges found as local.
4858 static int has_adjacent_true_conditions(struct isl_sched_graph
*graph
,
4859 __isl_keep isl_union_set
*conditional_source
,
4860 __isl_keep isl_union_set
*conditional_sink
)
4865 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4866 int adjacent
, local
;
4867 isl_union_map
*condition
;
4869 if (!is_condition(&graph
->edge
[i
]))
4871 if (is_local(&graph
->edge
[i
]))
4874 condition
= graph
->edge
[i
].tagged_condition
;
4875 adjacent
= domain_intersects(condition
, conditional_sink
);
4876 if (adjacent
>= 0 && !adjacent
)
4877 adjacent
= range_intersects(condition
,
4878 conditional_source
);
4884 set_local(&graph
->edge
[i
]);
4886 local
= is_condition_false(&graph
->edge
[i
]);
4896 /* Are there any violated conditional validity dependences with
4897 * adjacent condition dependences that are not local with respect
4898 * to the current schedule?
4899 * That is, is the conditional validity constraint violated?
4901 * Additionally, mark all those adjacent condition dependences as local.
4902 * We also mark those adjacent condition dependences that were not marked
4903 * as local before, but just happened to be local already. This ensures
4904 * that they remain local if the schedule is recomputed.
4906 * We first collect domain and range of all violated conditional validity
4907 * dependences and then check if there are any adjacent non-local
4908 * condition dependences.
4910 static int has_violated_conditional_constraint(isl_ctx
*ctx
,
4911 struct isl_sched_graph
*graph
)
4915 isl_union_set
*source
, *sink
;
4917 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
4918 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
4919 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4920 isl_union_set
*uset
;
4921 isl_union_map
*umap
;
4924 if (!is_conditional_validity(&graph
->edge
[i
]))
4927 violated
= is_violated(graph
, i
);
4935 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
4936 uset
= isl_union_map_domain(umap
);
4937 source
= isl_union_set_union(source
, uset
);
4938 source
= isl_union_set_coalesce(source
);
4940 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
4941 uset
= isl_union_map_range(umap
);
4942 sink
= isl_union_set_union(sink
, uset
);
4943 sink
= isl_union_set_coalesce(sink
);
4947 any
= has_adjacent_true_conditions(graph
, source
, sink
);
4949 isl_union_set_free(source
);
4950 isl_union_set_free(sink
);
4953 isl_union_set_free(source
);
4954 isl_union_set_free(sink
);
4958 /* Examine the current band (the rows between graph->band_start and
4959 * graph->n_total_row), deciding whether to drop it or add it to "node"
4960 * and then continue with the computation of the next band, if any.
4961 * If "initialized" is set, then it may be assumed that compute_maxvar
4962 * has been called on the current band. Otherwise, call
4963 * compute_maxvar if and before carry_dependences gets called.
4965 * The caller keeps looking for a new row as long as
4966 * graph->n_row < graph->maxvar. If the latest attempt to find
4967 * such a row failed (i.e., we still have graph->n_row < graph->maxvar),
4969 * - split between SCCs and start over (assuming we found an interesting
4970 * pair of SCCs between which to split)
4971 * - continue with the next band (assuming the current band has at least
4973 * - if there is more than one SCC left, then split along all SCCs
4974 * - if outer coincidence needs to be enforced, then try to carry as many
4975 * validity or coincidence dependences as possible and
4976 * continue with the next band
4977 * - try to carry as many validity dependences as possible and
4978 * continue with the next band
4979 * In each case, we first insert a band node in the schedule tree
4980 * if any rows have been computed.
4982 * If the caller managed to complete the schedule, we insert a band node
4983 * (if any schedule rows were computed) and we finish off by topologically
4984 * sorting the statements based on the remaining dependences.
4986 static __isl_give isl_schedule_node
*compute_schedule_finish_band(
4987 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
4995 if (graph
->n_row
< graph
->maxvar
) {
4997 int empty
= graph
->n_total_row
== graph
->band_start
;
4999 ctx
= isl_schedule_node_get_ctx(node
);
5000 if (!ctx
->opt
->schedule_maximize_band_depth
&& !empty
)
5001 return compute_next_band(node
, graph
, 1);
5002 if (graph
->src_scc
>= 0)
5003 return compute_split_schedule(node
, graph
);
5005 return compute_next_band(node
, graph
, 1);
5007 return compute_component_schedule(node
, graph
, 1);
5008 if (!initialized
&& compute_maxvar(graph
) < 0)
5009 return isl_schedule_node_free(node
);
5010 if (isl_options_get_schedule_outer_coincidence(ctx
))
5011 return carry_coincidence(node
, graph
);
5012 return carry_dependences(node
, graph
);
5015 insert
= graph
->n_total_row
> graph
->band_start
;
5017 node
= insert_current_band(node
, graph
, 1);
5018 node
= isl_schedule_node_child(node
, 0);
5020 node
= sort_statements(node
, graph
, initialized
);
5022 node
= isl_schedule_node_parent(node
);
5027 /* Construct a band of schedule rows for a connected dependence graph.
5028 * The caller is responsible for determining the strongly connected
5029 * components and calling compute_maxvar first.
5031 * We try to find a sequence of as many schedule rows as possible that result
5032 * in non-negative dependence distances (independent of the previous rows
5033 * in the sequence, i.e., such that the sequence is tilable), with as
5034 * many of the initial rows as possible satisfying the coincidence constraints.
5035 * The computation stops if we can't find any more rows or if we have found
5036 * all the rows we wanted to find.
5038 * If ctx->opt->schedule_outer_coincidence is set, then we force the
5039 * outermost dimension to satisfy the coincidence constraints. If this
5040 * turns out to be impossible, we fall back on the general scheme above
5041 * and try to carry as many dependences as possible.
5043 * If "graph" contains both condition and conditional validity dependences,
5044 * then we need to check that that the conditional schedule constraint
5045 * is satisfied, i.e., there are no violated conditional validity dependences
5046 * that are adjacent to any non-local condition dependences.
5047 * If there are, then we mark all those adjacent condition dependences
5048 * as local and recompute the current band. Those dependences that
5049 * are marked local will then be forced to be local.
5050 * The initial computation is performed with no dependences marked as local.
5051 * If we are lucky, then there will be no violated conditional validity
5052 * dependences adjacent to any non-local condition dependences.
5053 * Otherwise, we mark some additional condition dependences as local and
5054 * recompute. We continue this process until there are no violations left or
5055 * until we are no longer able to compute a schedule.
5056 * Since there are only a finite number of dependences,
5057 * there will only be a finite number of iterations.
5059 static isl_stat
compute_schedule_wcc_band(isl_ctx
*ctx
,
5060 struct isl_sched_graph
*graph
)
5062 int has_coincidence
;
5063 int use_coincidence
;
5064 int force_coincidence
= 0;
5065 int check_conditional
;
5067 if (sort_sccs(graph
) < 0)
5068 return isl_stat_error
;
5070 clear_local_edges(graph
);
5071 check_conditional
= need_condition_check(graph
);
5072 has_coincidence
= has_any_coincidence(graph
);
5074 if (ctx
->opt
->schedule_outer_coincidence
)
5075 force_coincidence
= 1;
5077 use_coincidence
= has_coincidence
;
5078 while (graph
->n_row
< graph
->maxvar
) {
5083 graph
->src_scc
= -1;
5084 graph
->dst_scc
= -1;
5086 if (setup_lp(ctx
, graph
, use_coincidence
) < 0)
5087 return isl_stat_error
;
5088 sol
= solve_lp(ctx
, graph
);
5090 return isl_stat_error
;
5091 if (sol
->size
== 0) {
5092 int empty
= graph
->n_total_row
== graph
->band_start
;
5095 if (use_coincidence
&& (!force_coincidence
|| !empty
)) {
5096 use_coincidence
= 0;
5101 coincident
= !has_coincidence
|| use_coincidence
;
5102 if (update_schedule(graph
, sol
, coincident
) < 0)
5103 return isl_stat_error
;
5105 if (!check_conditional
)
5107 violated
= has_violated_conditional_constraint(ctx
, graph
);
5109 return isl_stat_error
;
5112 if (reset_band(graph
) < 0)
5113 return isl_stat_error
;
5114 use_coincidence
= has_coincidence
;
5120 /* Compute a schedule for a connected dependence graph by considering
5121 * the graph as a whole and return the updated schedule node.
5123 * The actual schedule rows of the current band are computed by
5124 * compute_schedule_wcc_band. compute_schedule_finish_band takes
5125 * care of integrating the band into "node" and continuing
5128 static __isl_give isl_schedule_node
*compute_schedule_wcc_whole(
5129 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
5136 ctx
= isl_schedule_node_get_ctx(node
);
5137 if (compute_schedule_wcc_band(ctx
, graph
) < 0)
5138 return isl_schedule_node_free(node
);
5140 return compute_schedule_finish_band(node
, graph
, 1);
5143 /* Clustering information used by compute_schedule_wcc_clustering.
5145 * "n" is the number of SCCs in the original dependence graph
5146 * "scc" is an array of "n" elements, each representing an SCC
5147 * of the original dependence graph. All entries in the same cluster
5148 * have the same number of schedule rows.
5149 * "scc_cluster" maps each SCC index to the cluster to which it belongs,
5150 * where each cluster is represented by the index of the first SCC
5151 * in the cluster. Initially, each SCC belongs to a cluster containing
5154 * "scc_in_merge" is used by merge_clusters_along_edge to keep
5155 * track of which SCCs need to be merged.
5157 * "cluster" contains the merged clusters of SCCs after the clustering
5160 * "scc_node" is a temporary data structure used inside copy_partial.
5161 * For each SCC, it keeps track of the number of nodes in the SCC
5162 * that have already been copied.
5164 struct isl_clustering
{
5166 struct isl_sched_graph
*scc
;
5167 struct isl_sched_graph
*cluster
;
5173 /* Initialize the clustering data structure "c" from "graph".
5175 * In particular, allocate memory, extract the SCCs from "graph"
5176 * into c->scc, initialize scc_cluster and construct
5177 * a band of schedule rows for each SCC.
5178 * Within each SCC, there is only one SCC by definition.
5179 * Each SCC initially belongs to a cluster containing only that SCC.
5181 static isl_stat
clustering_init(isl_ctx
*ctx
, struct isl_clustering
*c
,
5182 struct isl_sched_graph
*graph
)
5187 c
->scc
= isl_calloc_array(ctx
, struct isl_sched_graph
, c
->n
);
5188 c
->cluster
= isl_calloc_array(ctx
, struct isl_sched_graph
, c
->n
);
5189 c
->scc_cluster
= isl_calloc_array(ctx
, int, c
->n
);
5190 c
->scc_node
= isl_calloc_array(ctx
, int, c
->n
);
5191 c
->scc_in_merge
= isl_calloc_array(ctx
, int, c
->n
);
5192 if (!c
->scc
|| !c
->cluster
||
5193 !c
->scc_cluster
|| !c
->scc_node
|| !c
->scc_in_merge
)
5194 return isl_stat_error
;
5196 for (i
= 0; i
< c
->n
; ++i
) {
5197 if (extract_sub_graph(ctx
, graph
, &node_scc_exactly
,
5198 &edge_scc_exactly
, i
, &c
->scc
[i
]) < 0)
5199 return isl_stat_error
;
5201 if (compute_maxvar(&c
->scc
[i
]) < 0)
5202 return isl_stat_error
;
5203 if (compute_schedule_wcc_band(ctx
, &c
->scc
[i
]) < 0)
5204 return isl_stat_error
;
5205 c
->scc_cluster
[i
] = i
;
5211 /* Free all memory allocated for "c".
5213 static void clustering_free(isl_ctx
*ctx
, struct isl_clustering
*c
)
5218 for (i
= 0; i
< c
->n
; ++i
)
5219 graph_free(ctx
, &c
->scc
[i
]);
5222 for (i
= 0; i
< c
->n
; ++i
)
5223 graph_free(ctx
, &c
->cluster
[i
]);
5225 free(c
->scc_cluster
);
5227 free(c
->scc_in_merge
);
5230 /* Should we refrain from merging the cluster in "graph" with
5231 * any other cluster?
5232 * In particular, is its current schedule band empty and incomplete.
5234 static int bad_cluster(struct isl_sched_graph
*graph
)
5236 return graph
->n_row
< graph
->maxvar
&&
5237 graph
->n_total_row
== graph
->band_start
;
5240 /* Is "edge" a proximity edge with a non-empty dependence relation?
5242 static isl_bool
is_non_empty_proximity(struct isl_sched_edge
*edge
)
5244 if (!is_proximity(edge
))
5245 return isl_bool_false
;
5246 return isl_bool_not(isl_map_plain_is_empty(edge
->map
));
5249 /* Return the index of an edge in "graph" that can be used to merge
5250 * two clusters in "c".
5251 * Return graph->n_edge if no such edge can be found.
5252 * Return -1 on error.
5254 * In particular, return a proximity edge between two clusters
5255 * that is not marked "no_merge" and such that neither of the
5256 * two clusters has an incomplete, empty band.
5258 * If there are multiple such edges, then try and find the most
5259 * appropriate edge to use for merging. In particular, pick the edge
5260 * with the greatest weight. If there are multiple of those,
5261 * then pick one with the shortest distance between
5262 * the two cluster representatives.
5264 static int find_proximity(struct isl_sched_graph
*graph
,
5265 struct isl_clustering
*c
)
5267 int i
, best
= graph
->n_edge
, best_dist
, best_weight
;
5269 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5270 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
5274 prox
= is_non_empty_proximity(edge
);
5281 if (bad_cluster(&c
->scc
[edge
->src
->scc
]) ||
5282 bad_cluster(&c
->scc
[edge
->dst
->scc
]))
5284 dist
= c
->scc_cluster
[edge
->dst
->scc
] -
5285 c
->scc_cluster
[edge
->src
->scc
];
5288 weight
= edge
->weight
;
5289 if (best
< graph
->n_edge
) {
5290 if (best_weight
> weight
)
5292 if (best_weight
== weight
&& best_dist
<= dist
)
5297 best_weight
= weight
;
5303 /* Internal data structure used in mark_merge_sccs.
5305 * "graph" is the dependence graph in which a strongly connected
5306 * component is constructed.
5307 * "scc_cluster" maps each SCC index to the cluster to which it belongs.
5308 * "src" and "dst" are the indices of the nodes that are being merged.
5310 struct isl_mark_merge_sccs_data
{
5311 struct isl_sched_graph
*graph
;
5317 /* Check whether the cluster containing node "i" depends on the cluster
5318 * containing node "j". If "i" and "j" belong to the same cluster,
5319 * then they are taken to depend on each other to ensure that
5320 * the resulting strongly connected component consists of complete
5321 * clusters. Furthermore, if "i" and "j" are the two nodes that
5322 * are being merged, then they are taken to depend on each other as well.
5323 * Otherwise, check if there is a (conditional) validity dependence
5324 * from node[j] to node[i], forcing node[i] to follow node[j].
5326 static isl_bool
cluster_follows(int i
, int j
, void *user
)
5328 struct isl_mark_merge_sccs_data
*data
= user
;
5329 struct isl_sched_graph
*graph
= data
->graph
;
5330 int *scc_cluster
= data
->scc_cluster
;
5332 if (data
->src
== i
&& data
->dst
== j
)
5333 return isl_bool_true
;
5334 if (data
->src
== j
&& data
->dst
== i
)
5335 return isl_bool_true
;
5336 if (scc_cluster
[graph
->node
[i
].scc
] == scc_cluster
[graph
->node
[j
].scc
])
5337 return isl_bool_true
;
5339 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
5342 /* Mark all SCCs that belong to either of the two clusters in "c"
5343 * connected by the edge in "graph" with index "edge", or to any
5344 * of the intermediate clusters.
5345 * The marking is recorded in c->scc_in_merge.
5347 * The given edge has been selected for merging two clusters,
5348 * meaning that there is at least a proximity edge between the two nodes.
5349 * However, there may also be (indirect) validity dependences
5350 * between the two nodes. When merging the two clusters, all clusters
5351 * containing one or more of the intermediate nodes along the
5352 * indirect validity dependences need to be merged in as well.
5354 * First collect all such nodes by computing the strongly connected
5355 * component (SCC) containing the two nodes connected by the edge, where
5356 * the two nodes are considered to depend on each other to make
5357 * sure they end up in the same SCC. Similarly, each node is considered
5358 * to depend on every other node in the same cluster to ensure
5359 * that the SCC consists of complete clusters.
5361 * Then the original SCCs that contain any of these nodes are marked
5362 * in c->scc_in_merge.
5364 static isl_stat
mark_merge_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5365 int edge
, struct isl_clustering
*c
)
5367 struct isl_mark_merge_sccs_data data
;
5368 struct isl_tarjan_graph
*g
;
5371 for (i
= 0; i
< c
->n
; ++i
)
5372 c
->scc_in_merge
[i
] = 0;
5375 data
.scc_cluster
= c
->scc_cluster
;
5376 data
.src
= graph
->edge
[edge
].src
- graph
->node
;
5377 data
.dst
= graph
->edge
[edge
].dst
- graph
->node
;
5379 g
= isl_tarjan_graph_component(ctx
, graph
->n
, data
.dst
,
5380 &cluster_follows
, &data
);
5386 isl_die(ctx
, isl_error_internal
,
5387 "expecting at least two nodes in component",
5389 if (g
->order
[--i
] != -1)
5390 isl_die(ctx
, isl_error_internal
,
5391 "expecting end of component marker", goto error
);
5393 for (--i
; i
>= 0 && g
->order
[i
] != -1; --i
) {
5394 int scc
= graph
->node
[g
->order
[i
]].scc
;
5395 c
->scc_in_merge
[scc
] = 1;
5398 isl_tarjan_graph_free(g
);
5401 isl_tarjan_graph_free(g
);
5402 return isl_stat_error
;
5405 /* Construct the identifier "cluster_i".
5407 static __isl_give isl_id
*cluster_id(isl_ctx
*ctx
, int i
)
5411 snprintf(name
, sizeof(name
), "cluster_%d", i
);
5412 return isl_id_alloc(ctx
, name
, NULL
);
5415 /* Construct the space of the cluster with index "i" containing
5416 * the strongly connected component "scc".
5418 * In particular, construct a space called cluster_i with dimension equal
5419 * to the number of schedule rows in the current band of "scc".
5421 static __isl_give isl_space
*cluster_space(struct isl_sched_graph
*scc
, int i
)
5427 nvar
= scc
->n_total_row
- scc
->band_start
;
5428 space
= isl_space_copy(scc
->node
[0].space
);
5429 space
= isl_space_params(space
);
5430 space
= isl_space_set_from_params(space
);
5431 space
= isl_space_add_dims(space
, isl_dim_set
, nvar
);
5432 id
= cluster_id(isl_space_get_ctx(space
), i
);
5433 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
5438 /* Collect the domain of the graph for merging clusters.
5440 * In particular, for each cluster with first SCC "i", construct
5441 * a set in the space called cluster_i with dimension equal
5442 * to the number of schedule rows in the current band of the cluster.
5444 static __isl_give isl_union_set
*collect_domain(isl_ctx
*ctx
,
5445 struct isl_sched_graph
*graph
, struct isl_clustering
*c
)
5449 isl_union_set
*domain
;
5451 space
= isl_space_params_alloc(ctx
, 0);
5452 domain
= isl_union_set_empty(space
);
5454 for (i
= 0; i
< graph
->scc
; ++i
) {
5457 if (!c
->scc_in_merge
[i
])
5459 if (c
->scc_cluster
[i
] != i
)
5461 space
= cluster_space(&c
->scc
[i
], i
);
5462 domain
= isl_union_set_add_set(domain
, isl_set_universe(space
));
5468 /* Construct a map from the original instances to the corresponding
5469 * cluster instance in the current bands of the clusters in "c".
5471 static __isl_give isl_union_map
*collect_cluster_map(isl_ctx
*ctx
,
5472 struct isl_sched_graph
*graph
, struct isl_clustering
*c
)
5476 isl_union_map
*cluster_map
;
5478 space
= isl_space_params_alloc(ctx
, 0);
5479 cluster_map
= isl_union_map_empty(space
);
5480 for (i
= 0; i
< graph
->scc
; ++i
) {
5484 if (!c
->scc_in_merge
[i
])
5487 id
= cluster_id(ctx
, c
->scc_cluster
[i
]);
5488 start
= c
->scc
[i
].band_start
;
5489 n
= c
->scc
[i
].n_total_row
- start
;
5490 for (j
= 0; j
< c
->scc
[i
].n
; ++j
) {
5493 struct isl_sched_node
*node
= &c
->scc
[i
].node
[j
];
5495 ma
= node_extract_partial_schedule_multi_aff(node
,
5497 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
,
5499 map
= isl_map_from_multi_aff(ma
);
5500 cluster_map
= isl_union_map_add_map(cluster_map
, map
);
5508 /* Add "umap" to the schedule constraints "sc" of all types of "edge"
5509 * that are not isl_edge_condition or isl_edge_conditional_validity.
5511 static __isl_give isl_schedule_constraints
*add_non_conditional_constraints(
5512 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*umap
,
5513 __isl_take isl_schedule_constraints
*sc
)
5515 enum isl_edge_type t
;
5520 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
5521 if (t
== isl_edge_condition
||
5522 t
== isl_edge_conditional_validity
)
5524 if (!is_type(edge
, t
))
5526 sc
= isl_schedule_constraints_add(sc
, t
,
5527 isl_union_map_copy(umap
));
5533 /* Add schedule constraints of types isl_edge_condition and
5534 * isl_edge_conditional_validity to "sc" by applying "umap" to
5535 * the domains of the wrapped relations in domain and range
5536 * of the corresponding tagged constraints of "edge".
5538 static __isl_give isl_schedule_constraints
*add_conditional_constraints(
5539 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*umap
,
5540 __isl_take isl_schedule_constraints
*sc
)
5542 enum isl_edge_type t
;
5543 isl_union_map
*tagged
;
5545 for (t
= isl_edge_condition
; t
<= isl_edge_conditional_validity
; ++t
) {
5546 if (!is_type(edge
, t
))
5548 if (t
== isl_edge_condition
)
5549 tagged
= isl_union_map_copy(edge
->tagged_condition
);
5551 tagged
= isl_union_map_copy(edge
->tagged_validity
);
5552 tagged
= isl_union_map_zip(tagged
);
5553 tagged
= isl_union_map_apply_domain(tagged
,
5554 isl_union_map_copy(umap
));
5555 tagged
= isl_union_map_zip(tagged
);
5556 sc
= isl_schedule_constraints_add(sc
, t
, tagged
);
5564 /* Given a mapping "cluster_map" from the original instances to
5565 * the cluster instances, add schedule constraints on the clusters
5566 * to "sc" corresponding to the original constraints represented by "edge".
5568 * For non-tagged dependence constraints, the cluster constraints
5569 * are obtained by applying "cluster_map" to the edge->map.
5571 * For tagged dependence constraints, "cluster_map" needs to be applied
5572 * to the domains of the wrapped relations in domain and range
5573 * of the tagged dependence constraints. Pick out the mappings
5574 * from these domains from "cluster_map" and construct their product.
5575 * This mapping can then be applied to the pair of domains.
5577 static __isl_give isl_schedule_constraints
*collect_edge_constraints(
5578 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*cluster_map
,
5579 __isl_take isl_schedule_constraints
*sc
)
5581 isl_union_map
*umap
;
5583 isl_union_set
*uset
;
5584 isl_union_map
*umap1
, *umap2
;
5589 umap
= isl_union_map_from_map(isl_map_copy(edge
->map
));
5590 umap
= isl_union_map_apply_domain(umap
,
5591 isl_union_map_copy(cluster_map
));
5592 umap
= isl_union_map_apply_range(umap
,
5593 isl_union_map_copy(cluster_map
));
5594 sc
= add_non_conditional_constraints(edge
, umap
, sc
);
5595 isl_union_map_free(umap
);
5597 if (!sc
|| (!is_condition(edge
) && !is_conditional_validity(edge
)))
5600 space
= isl_space_domain(isl_map_get_space(edge
->map
));
5601 uset
= isl_union_set_from_set(isl_set_universe(space
));
5602 umap1
= isl_union_map_copy(cluster_map
);
5603 umap1
= isl_union_map_intersect_domain(umap1
, uset
);
5604 space
= isl_space_range(isl_map_get_space(edge
->map
));
5605 uset
= isl_union_set_from_set(isl_set_universe(space
));
5606 umap2
= isl_union_map_copy(cluster_map
);
5607 umap2
= isl_union_map_intersect_domain(umap2
, uset
);
5608 umap
= isl_union_map_product(umap1
, umap2
);
5610 sc
= add_conditional_constraints(edge
, umap
, sc
);
5612 isl_union_map_free(umap
);
5616 /* Given a mapping "cluster_map" from the original instances to
5617 * the cluster instances, add schedule constraints on the clusters
5618 * to "sc" corresponding to all edges in "graph" between nodes that
5619 * belong to SCCs that are marked for merging in "scc_in_merge".
5621 static __isl_give isl_schedule_constraints
*collect_constraints(
5622 struct isl_sched_graph
*graph
, int *scc_in_merge
,
5623 __isl_keep isl_union_map
*cluster_map
,
5624 __isl_take isl_schedule_constraints
*sc
)
5628 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5629 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
5631 if (!scc_in_merge
[edge
->src
->scc
])
5633 if (!scc_in_merge
[edge
->dst
->scc
])
5635 sc
= collect_edge_constraints(edge
, cluster_map
, sc
);
5641 /* Construct a dependence graph for scheduling clusters with respect
5642 * to each other and store the result in "merge_graph".
5643 * In particular, the nodes of the graph correspond to the schedule
5644 * dimensions of the current bands of those clusters that have been
5645 * marked for merging in "c".
5647 * First construct an isl_schedule_constraints object for this domain
5648 * by transforming the edges in "graph" to the domain.
5649 * Then initialize a dependence graph for scheduling from these
5652 static isl_stat
init_merge_graph(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5653 struct isl_clustering
*c
, struct isl_sched_graph
*merge_graph
)
5655 isl_union_set
*domain
;
5656 isl_union_map
*cluster_map
;
5657 isl_schedule_constraints
*sc
;
5660 domain
= collect_domain(ctx
, graph
, c
);
5661 sc
= isl_schedule_constraints_on_domain(domain
);
5663 return isl_stat_error
;
5664 cluster_map
= collect_cluster_map(ctx
, graph
, c
);
5665 sc
= collect_constraints(graph
, c
->scc_in_merge
, cluster_map
, sc
);
5666 isl_union_map_free(cluster_map
);
5668 r
= graph_init(merge_graph
, sc
);
5670 isl_schedule_constraints_free(sc
);
5675 /* Compute the maximal number of remaining schedule rows that still need
5676 * to be computed for the nodes that belong to clusters with the maximal
5677 * dimension for the current band (i.e., the band that is to be merged).
5678 * Only clusters that are about to be merged are considered.
5679 * "maxvar" is the maximal dimension for the current band.
5680 * "c" contains information about the clusters.
5682 * Return the maximal number of remaining schedule rows or -1 on error.
5684 static int compute_maxvar_max_slack(int maxvar
, struct isl_clustering
*c
)
5690 for (i
= 0; i
< c
->n
; ++i
) {
5692 struct isl_sched_graph
*scc
;
5694 if (!c
->scc_in_merge
[i
])
5697 nvar
= scc
->n_total_row
- scc
->band_start
;
5700 for (j
= 0; j
< scc
->n
; ++j
) {
5701 struct isl_sched_node
*node
= &scc
->node
[j
];
5704 if (node_update_vmap(node
) < 0)
5706 slack
= node
->nvar
- node
->rank
;
5707 if (slack
> max_slack
)
5715 /* If there are any clusters where the dimension of the current band
5716 * (i.e., the band that is to be merged) is smaller than "maxvar" and
5717 * if there are any nodes in such a cluster where the number
5718 * of remaining schedule rows that still need to be computed
5719 * is greater than "max_slack", then return the smallest current band
5720 * dimension of all these clusters. Otherwise return the original value
5721 * of "maxvar". Return -1 in case of any error.
5722 * Only clusters that are about to be merged are considered.
5723 * "c" contains information about the clusters.
5725 static int limit_maxvar_to_slack(int maxvar
, int max_slack
,
5726 struct isl_clustering
*c
)
5730 for (i
= 0; i
< c
->n
; ++i
) {
5732 struct isl_sched_graph
*scc
;
5734 if (!c
->scc_in_merge
[i
])
5737 nvar
= scc
->n_total_row
- scc
->band_start
;
5740 for (j
= 0; j
< scc
->n
; ++j
) {
5741 struct isl_sched_node
*node
= &scc
->node
[j
];
5744 if (node_update_vmap(node
) < 0)
5746 slack
= node
->nvar
- node
->rank
;
5747 if (slack
> max_slack
) {
5757 /* Adjust merge_graph->maxvar based on the number of remaining schedule rows
5758 * that still need to be computed. In particular, if there is a node
5759 * in a cluster where the dimension of the current band is smaller
5760 * than merge_graph->maxvar, but the number of remaining schedule rows
5761 * is greater than that of any node in a cluster with the maximal
5762 * dimension for the current band (i.e., merge_graph->maxvar),
5763 * then adjust merge_graph->maxvar to the (smallest) current band dimension
5764 * of those clusters. Without this adjustment, the total number of
5765 * schedule dimensions would be increased, resulting in a skewed view
5766 * of the number of coincident dimensions.
5767 * "c" contains information about the clusters.
5769 * If the maximize_band_depth option is set and merge_graph->maxvar is reduced,
5770 * then there is no point in attempting any merge since it will be rejected
5771 * anyway. Set merge_graph->maxvar to zero in such cases.
5773 static isl_stat
adjust_maxvar_to_slack(isl_ctx
*ctx
,
5774 struct isl_sched_graph
*merge_graph
, struct isl_clustering
*c
)
5776 int max_slack
, maxvar
;
5778 max_slack
= compute_maxvar_max_slack(merge_graph
->maxvar
, c
);
5780 return isl_stat_error
;
5781 maxvar
= limit_maxvar_to_slack(merge_graph
->maxvar
, max_slack
, c
);
5783 return isl_stat_error
;
5785 if (maxvar
< merge_graph
->maxvar
) {
5786 if (isl_options_get_schedule_maximize_band_depth(ctx
))
5787 merge_graph
->maxvar
= 0;
5789 merge_graph
->maxvar
= maxvar
;
5795 /* Return the number of coincident dimensions in the current band of "graph",
5796 * where the nodes of "graph" are assumed to be scheduled by a single band.
5798 static int get_n_coincident(struct isl_sched_graph
*graph
)
5802 for (i
= graph
->band_start
; i
< graph
->n_total_row
; ++i
)
5803 if (!graph
->node
[0].coincident
[i
])
5806 return i
- graph
->band_start
;
5809 /* Should the clusters be merged based on the cluster schedule
5810 * in the current (and only) band of "merge_graph", given that
5811 * coincidence should be maximized?
5813 * If the number of coincident schedule dimensions in the merged band
5814 * would be less than the maximal number of coincident schedule dimensions
5815 * in any of the merged clusters, then the clusters should not be merged.
5817 static isl_bool
ok_to_merge_coincident(struct isl_clustering
*c
,
5818 struct isl_sched_graph
*merge_graph
)
5825 for (i
= 0; i
< c
->n
; ++i
) {
5826 if (!c
->scc_in_merge
[i
])
5828 n_coincident
= get_n_coincident(&c
->scc
[i
]);
5829 if (n_coincident
> max_coincident
)
5830 max_coincident
= n_coincident
;
5833 n_coincident
= get_n_coincident(merge_graph
);
5835 return n_coincident
>= max_coincident
;
5838 /* Return the transformation on "node" expressed by the current (and only)
5839 * band of "merge_graph" applied to the clusters in "c".
5841 * First find the representation of "node" in its SCC in "c" and
5842 * extract the transformation expressed by the current band.
5843 * Then extract the transformation applied by "merge_graph"
5844 * to the cluster to which this SCC belongs.
5845 * Combine the two to obtain the complete transformation on the node.
5847 * Note that the range of the first transformation is an anonymous space,
5848 * while the domain of the second is named "cluster_X". The range
5849 * of the former therefore needs to be adjusted before the two
5852 static __isl_give isl_map
*extract_node_transformation(isl_ctx
*ctx
,
5853 struct isl_sched_node
*node
, struct isl_clustering
*c
,
5854 struct isl_sched_graph
*merge_graph
)
5856 struct isl_sched_node
*scc_node
, *cluster_node
;
5860 isl_multi_aff
*ma
, *ma2
;
5862 scc_node
= graph_find_node(ctx
, &c
->scc
[node
->scc
], node
->space
);
5863 start
= c
->scc
[node
->scc
].band_start
;
5864 n
= c
->scc
[node
->scc
].n_total_row
- start
;
5865 ma
= node_extract_partial_schedule_multi_aff(scc_node
, start
, n
);
5866 space
= cluster_space(&c
->scc
[node
->scc
], c
->scc_cluster
[node
->scc
]);
5867 cluster_node
= graph_find_node(ctx
, merge_graph
, space
);
5868 if (space
&& !cluster_node
)
5869 isl_die(ctx
, isl_error_internal
, "unable to find cluster",
5870 space
= isl_space_free(space
));
5871 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
5872 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
, id
);
5873 isl_space_free(space
);
5874 n
= merge_graph
->n_total_row
;
5875 ma2
= node_extract_partial_schedule_multi_aff(cluster_node
, 0, n
);
5876 ma
= isl_multi_aff_pullback_multi_aff(ma2
, ma
);
5878 return isl_map_from_multi_aff(ma
);
5881 /* Give a set of distances "set", are they bounded by a small constant
5882 * in direction "pos"?
5883 * In practice, check if they are bounded by 2 by checking that there
5884 * are no elements with a value greater than or equal to 3 or
5885 * smaller than or equal to -3.
5887 static isl_bool
distance_is_bounded(__isl_keep isl_set
*set
, int pos
)
5893 return isl_bool_error
;
5895 test
= isl_set_copy(set
);
5896 test
= isl_set_lower_bound_si(test
, isl_dim_set
, pos
, 3);
5897 bounded
= isl_set_is_empty(test
);
5900 if (bounded
< 0 || !bounded
)
5903 test
= isl_set_copy(set
);
5904 test
= isl_set_upper_bound_si(test
, isl_dim_set
, pos
, -3);
5905 bounded
= isl_set_is_empty(test
);
5911 /* Does the set "set" have a fixed (but possible parametric) value
5912 * at dimension "pos"?
5914 static isl_bool
has_single_value(__isl_keep isl_set
*set
, int pos
)
5920 return isl_bool_error
;
5921 set
= isl_set_copy(set
);
5922 n
= isl_set_dim(set
, isl_dim_set
);
5923 set
= isl_set_project_out(set
, isl_dim_set
, pos
+ 1, n
- (pos
+ 1));
5924 set
= isl_set_project_out(set
, isl_dim_set
, 0, pos
);
5925 single
= isl_set_is_singleton(set
);
5931 /* Does "map" have a fixed (but possible parametric) value
5932 * at dimension "pos" of either its domain or its range?
5934 static isl_bool
has_singular_src_or_dst(__isl_keep isl_map
*map
, int pos
)
5939 set
= isl_map_domain(isl_map_copy(map
));
5940 single
= has_single_value(set
, pos
);
5943 if (single
< 0 || single
)
5946 set
= isl_map_range(isl_map_copy(map
));
5947 single
= has_single_value(set
, pos
);
5953 /* Does the edge "edge" from "graph" have bounded dependence distances
5954 * in the merged graph "merge_graph" of a selection of clusters in "c"?
5956 * Extract the complete transformations of the source and destination
5957 * nodes of the edge, apply them to the edge constraints and
5958 * compute the differences. Finally, check if these differences are bounded
5959 * in each direction.
5961 * If the dimension of the band is greater than the number of
5962 * dimensions that can be expected to be optimized by the edge
5963 * (based on its weight), then also allow the differences to be unbounded
5964 * in the remaining dimensions, but only if either the source or
5965 * the destination has a fixed value in that direction.
5966 * This allows a statement that produces values that are used by
5967 * several instances of another statement to be merged with that
5969 * However, merging such clusters will introduce an inherently
5970 * large proximity distance inside the merged cluster, meaning
5971 * that proximity distances will no longer be optimized in
5972 * subsequent merges. These merges are therefore only allowed
5973 * after all other possible merges have been tried.
5974 * The first time such a merge is encountered, the weight of the edge
5975 * is replaced by a negative weight. The second time (i.e., after
5976 * all merges over edges with a non-negative weight have been tried),
5977 * the merge is allowed.
5979 static isl_bool
has_bounded_distances(isl_ctx
*ctx
, struct isl_sched_edge
*edge
,
5980 struct isl_sched_graph
*graph
, struct isl_clustering
*c
,
5981 struct isl_sched_graph
*merge_graph
)
5988 map
= isl_map_copy(edge
->map
);
5989 t
= extract_node_transformation(ctx
, edge
->src
, c
, merge_graph
);
5990 map
= isl_map_apply_domain(map
, t
);
5991 t
= extract_node_transformation(ctx
, edge
->dst
, c
, merge_graph
);
5992 map
= isl_map_apply_range(map
, t
);
5993 dist
= isl_map_deltas(isl_map_copy(map
));
5995 bounded
= isl_bool_true
;
5996 n
= isl_set_dim(dist
, isl_dim_set
);
5997 n_slack
= n
- edge
->weight
;
5998 if (edge
->weight
< 0)
5999 n_slack
-= graph
->max_weight
+ 1;
6000 for (i
= 0; i
< n
; ++i
) {
6001 isl_bool bounded_i
, singular_i
;
6003 bounded_i
= distance_is_bounded(dist
, i
);
6008 if (edge
->weight
>= 0)
6009 bounded
= isl_bool_false
;
6013 singular_i
= has_singular_src_or_dst(map
, i
);
6018 bounded
= isl_bool_false
;
6021 if (!bounded
&& i
>= n
&& edge
->weight
>= 0)
6022 edge
->weight
-= graph
->max_weight
+ 1;
6030 return isl_bool_error
;
6033 /* Should the clusters be merged based on the cluster schedule
6034 * in the current (and only) band of "merge_graph"?
6035 * "graph" is the original dependence graph, while "c" records
6036 * which SCCs are involved in the latest merge.
6038 * In particular, is there at least one proximity constraint
6039 * that is optimized by the merge?
6041 * A proximity constraint is considered to be optimized
6042 * if the dependence distances are small.
6044 static isl_bool
ok_to_merge_proximity(isl_ctx
*ctx
,
6045 struct isl_sched_graph
*graph
, struct isl_clustering
*c
,
6046 struct isl_sched_graph
*merge_graph
)
6050 for (i
= 0; i
< graph
->n_edge
; ++i
) {
6051 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
6054 if (!is_proximity(edge
))
6056 if (!c
->scc_in_merge
[edge
->src
->scc
])
6058 if (!c
->scc_in_merge
[edge
->dst
->scc
])
6060 if (c
->scc_cluster
[edge
->dst
->scc
] ==
6061 c
->scc_cluster
[edge
->src
->scc
])
6063 bounded
= has_bounded_distances(ctx
, edge
, graph
, c
,
6065 if (bounded
< 0 || bounded
)
6069 return isl_bool_false
;
6072 /* Should the clusters be merged based on the cluster schedule
6073 * in the current (and only) band of "merge_graph"?
6074 * "graph" is the original dependence graph, while "c" records
6075 * which SCCs are involved in the latest merge.
6077 * If the current band is empty, then the clusters should not be merged.
6079 * If the band depth should be maximized and the merge schedule
6080 * is incomplete (meaning that the dimension of some of the schedule
6081 * bands in the original schedule will be reduced), then the clusters
6082 * should not be merged.
6084 * If the schedule_maximize_coincidence option is set, then check that
6085 * the number of coincident schedule dimensions is not reduced.
6087 * Finally, only allow the merge if at least one proximity
6088 * constraint is optimized.
6090 static isl_bool
ok_to_merge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6091 struct isl_clustering
*c
, struct isl_sched_graph
*merge_graph
)
6093 if (merge_graph
->n_total_row
== merge_graph
->band_start
)
6094 return isl_bool_false
;
6096 if (isl_options_get_schedule_maximize_band_depth(ctx
) &&
6097 merge_graph
->n_total_row
< merge_graph
->maxvar
)
6098 return isl_bool_false
;
6100 if (isl_options_get_schedule_maximize_coincidence(ctx
)) {
6103 ok
= ok_to_merge_coincident(c
, merge_graph
);
6108 return ok_to_merge_proximity(ctx
, graph
, c
, merge_graph
);
6111 /* Apply the schedule in "t_node" to the "n" rows starting at "first"
6112 * of the schedule in "node" and return the result.
6114 * That is, essentially compute
6116 * T * N(first:first+n-1)
6118 * taking into account the constant term and the parameter coefficients
6121 static __isl_give isl_mat
*node_transformation(isl_ctx
*ctx
,
6122 struct isl_sched_node
*t_node
, struct isl_sched_node
*node
,
6127 int n_row
, n_col
, n_param
, n_var
;
6129 n_param
= node
->nparam
;
6131 n_row
= isl_mat_rows(t_node
->sched
);
6132 n_col
= isl_mat_cols(node
->sched
);
6133 t
= isl_mat_alloc(ctx
, n_row
, n_col
);
6136 for (i
= 0; i
< n_row
; ++i
) {
6137 isl_seq_cpy(t
->row
[i
], t_node
->sched
->row
[i
], 1 + n_param
);
6138 isl_seq_clr(t
->row
[i
] + 1 + n_param
, n_var
);
6139 for (j
= 0; j
< n
; ++j
)
6140 isl_seq_addmul(t
->row
[i
],
6141 t_node
->sched
->row
[i
][1 + n_param
+ j
],
6142 node
->sched
->row
[first
+ j
],
6143 1 + n_param
+ n_var
);
6148 /* Apply the cluster schedule in "t_node" to the current band
6149 * schedule of the nodes in "graph".
6151 * In particular, replace the rows starting at band_start
6152 * by the result of applying the cluster schedule in "t_node"
6153 * to the original rows.
6155 * The coincidence of the schedule is determined by the coincidence
6156 * of the cluster schedule.
6158 static isl_stat
transform(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6159 struct isl_sched_node
*t_node
)
6165 start
= graph
->band_start
;
6166 n
= graph
->n_total_row
- start
;
6168 n_new
= isl_mat_rows(t_node
->sched
);
6169 for (i
= 0; i
< graph
->n
; ++i
) {
6170 struct isl_sched_node
*node
= &graph
->node
[i
];
6173 t
= node_transformation(ctx
, t_node
, node
, start
, n
);
6174 node
->sched
= isl_mat_drop_rows(node
->sched
, start
, n
);
6175 node
->sched
= isl_mat_concat(node
->sched
, t
);
6176 node
->sched_map
= isl_map_free(node
->sched_map
);
6178 return isl_stat_error
;
6179 for (j
= 0; j
< n_new
; ++j
)
6180 node
->coincident
[start
+ j
] = t_node
->coincident
[j
];
6182 graph
->n_total_row
-= n
;
6184 graph
->n_total_row
+= n_new
;
6185 graph
->n_row
+= n_new
;
6190 /* Merge the clusters marked for merging in "c" into a single
6191 * cluster using the cluster schedule in the current band of "merge_graph".
6192 * The representative SCC for the new cluster is the SCC with
6193 * the smallest index.
6195 * The current band schedule of each SCC in the new cluster is obtained
6196 * by applying the schedule of the corresponding original cluster
6197 * to the original band schedule.
6198 * All SCCs in the new cluster have the same number of schedule rows.
6200 static isl_stat
merge(isl_ctx
*ctx
, struct isl_clustering
*c
,
6201 struct isl_sched_graph
*merge_graph
)
6207 for (i
= 0; i
< c
->n
; ++i
) {
6208 struct isl_sched_node
*node
;
6210 if (!c
->scc_in_merge
[i
])
6214 space
= cluster_space(&c
->scc
[i
], c
->scc_cluster
[i
]);
6216 return isl_stat_error
;
6217 node
= graph_find_node(ctx
, merge_graph
, space
);
6218 isl_space_free(space
);
6220 isl_die(ctx
, isl_error_internal
,
6221 "unable to find cluster",
6222 return isl_stat_error
);
6223 if (transform(ctx
, &c
->scc
[i
], node
) < 0)
6224 return isl_stat_error
;
6225 c
->scc_cluster
[i
] = cluster
;
6231 /* Try and merge the clusters of SCCs marked in c->scc_in_merge
6232 * by scheduling the current cluster bands with respect to each other.
6234 * Construct a dependence graph with a space for each cluster and
6235 * with the coordinates of each space corresponding to the schedule
6236 * dimensions of the current band of that cluster.
6237 * Construct a cluster schedule in this cluster dependence graph and
6238 * apply it to the current cluster bands if it is applicable
6239 * according to ok_to_merge.
6241 * If the number of remaining schedule dimensions in a cluster
6242 * with a non-maximal current schedule dimension is greater than
6243 * the number of remaining schedule dimensions in clusters
6244 * with a maximal current schedule dimension, then restrict
6245 * the number of rows to be computed in the cluster schedule
6246 * to the minimal such non-maximal current schedule dimension.
6247 * Do this by adjusting merge_graph.maxvar.
6249 * Return isl_bool_true if the clusters have effectively been merged
6250 * into a single cluster.
6252 * Note that since the standard scheduling algorithm minimizes the maximal
6253 * distance over proximity constraints, the proximity constraints between
6254 * the merged clusters may not be optimized any further than what is
6255 * sufficient to bring the distances within the limits of the internal
6256 * proximity constraints inside the individual clusters.
6257 * It may therefore make sense to perform an additional translation step
6258 * to bring the clusters closer to each other, while maintaining
6259 * the linear part of the merging schedule found using the standard
6260 * scheduling algorithm.
6262 static isl_bool
try_merge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6263 struct isl_clustering
*c
)
6265 struct isl_sched_graph merge_graph
= { 0 };
6268 if (init_merge_graph(ctx
, graph
, c
, &merge_graph
) < 0)
6271 if (compute_maxvar(&merge_graph
) < 0)
6273 if (adjust_maxvar_to_slack(ctx
, &merge_graph
,c
) < 0)
6275 if (compute_schedule_wcc_band(ctx
, &merge_graph
) < 0)
6277 merged
= ok_to_merge(ctx
, graph
, c
, &merge_graph
);
6278 if (merged
&& merge(ctx
, c
, &merge_graph
) < 0)
6281 graph_free(ctx
, &merge_graph
);
6284 graph_free(ctx
, &merge_graph
);
6285 return isl_bool_error
;
6288 /* Is there any edge marked "no_merge" between two SCCs that are
6289 * about to be merged (i.e., that are set in "scc_in_merge")?
6290 * "merge_edge" is the proximity edge along which the clusters of SCCs
6291 * are going to be merged.
6293 * If there is any edge between two SCCs with a negative weight,
6294 * while the weight of "merge_edge" is non-negative, then this
6295 * means that the edge was postponed. "merge_edge" should then
6296 * also be postponed since merging along the edge with negative weight should
6297 * be postponed until all edges with non-negative weight have been tried.
6298 * Replace the weight of "merge_edge" by a negative weight as well and
6299 * tell the caller not to attempt a merge.
6301 static int any_no_merge(struct isl_sched_graph
*graph
, int *scc_in_merge
,
6302 struct isl_sched_edge
*merge_edge
)
6306 for (i
= 0; i
< graph
->n_edge
; ++i
) {
6307 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
6309 if (!scc_in_merge
[edge
->src
->scc
])
6311 if (!scc_in_merge
[edge
->dst
->scc
])
6315 if (merge_edge
->weight
>= 0 && edge
->weight
< 0) {
6316 merge_edge
->weight
-= graph
->max_weight
+ 1;
6324 /* Merge the two clusters in "c" connected by the edge in "graph"
6325 * with index "edge" into a single cluster.
6326 * If it turns out to be impossible to merge these two clusters,
6327 * then mark the edge as "no_merge" such that it will not be
6330 * First mark all SCCs that need to be merged. This includes the SCCs
6331 * in the two clusters, but it may also include the SCCs
6332 * of intermediate clusters.
6333 * If there is already a no_merge edge between any pair of such SCCs,
6334 * then simply mark the current edge as no_merge as well.
6335 * Likewise, if any of those edges was postponed by has_bounded_distances,
6336 * then postpone the current edge as well.
6337 * Otherwise, try and merge the clusters and mark "edge" as "no_merge"
6338 * if the clusters did not end up getting merged, unless the non-merge
6339 * is due to the fact that the edge was postponed. This postponement
6340 * can be recognized by a change in weight (from non-negative to negative).
6342 static isl_stat
merge_clusters_along_edge(isl_ctx
*ctx
,
6343 struct isl_sched_graph
*graph
, int edge
, struct isl_clustering
*c
)
6346 int edge_weight
= graph
->edge
[edge
].weight
;
6348 if (mark_merge_sccs(ctx
, graph
, edge
, c
) < 0)
6349 return isl_stat_error
;
6351 if (any_no_merge(graph
, c
->scc_in_merge
, &graph
->edge
[edge
]))
6352 merged
= isl_bool_false
;
6354 merged
= try_merge(ctx
, graph
, c
);
6356 return isl_stat_error
;
6357 if (!merged
&& edge_weight
== graph
->edge
[edge
].weight
)
6358 graph
->edge
[edge
].no_merge
= 1;
6363 /* Does "node" belong to the cluster identified by "cluster"?
6365 static int node_cluster_exactly(struct isl_sched_node
*node
, int cluster
)
6367 return node
->cluster
== cluster
;
6370 /* Does "edge" connect two nodes belonging to the cluster
6371 * identified by "cluster"?
6373 static int edge_cluster_exactly(struct isl_sched_edge
*edge
, int cluster
)
6375 return edge
->src
->cluster
== cluster
&& edge
->dst
->cluster
== cluster
;
6378 /* Swap the schedule of "node1" and "node2".
6379 * Both nodes have been derived from the same node in a common parent graph.
6380 * Since the "coincident" field is shared with that node
6381 * in the parent graph, there is no need to also swap this field.
6383 static void swap_sched(struct isl_sched_node
*node1
,
6384 struct isl_sched_node
*node2
)
6389 sched
= node1
->sched
;
6390 node1
->sched
= node2
->sched
;
6391 node2
->sched
= sched
;
6393 sched_map
= node1
->sched_map
;
6394 node1
->sched_map
= node2
->sched_map
;
6395 node2
->sched_map
= sched_map
;
6398 /* Copy the current band schedule from the SCCs that form the cluster
6399 * with index "pos" to the actual cluster at position "pos".
6400 * By construction, the index of the first SCC that belongs to the cluster
6403 * The order of the nodes inside both the SCCs and the cluster
6404 * is assumed to be same as the order in the original "graph".
6406 * Since the SCC graphs will no longer be used after this function,
6407 * the schedules are actually swapped rather than copied.
6409 static isl_stat
copy_partial(struct isl_sched_graph
*graph
,
6410 struct isl_clustering
*c
, int pos
)
6414 c
->cluster
[pos
].n_total_row
= c
->scc
[pos
].n_total_row
;
6415 c
->cluster
[pos
].n_row
= c
->scc
[pos
].n_row
;
6416 c
->cluster
[pos
].maxvar
= c
->scc
[pos
].maxvar
;
6418 for (i
= 0; i
< graph
->n
; ++i
) {
6422 if (graph
->node
[i
].cluster
!= pos
)
6424 s
= graph
->node
[i
].scc
;
6425 k
= c
->scc_node
[s
]++;
6426 swap_sched(&c
->cluster
[pos
].node
[j
], &c
->scc
[s
].node
[k
]);
6427 if (c
->scc
[s
].maxvar
> c
->cluster
[pos
].maxvar
)
6428 c
->cluster
[pos
].maxvar
= c
->scc
[s
].maxvar
;
6435 /* Is there a (conditional) validity dependence from node[j] to node[i],
6436 * forcing node[i] to follow node[j] or do the nodes belong to the same
6439 static isl_bool
node_follows_strong_or_same_cluster(int i
, int j
, void *user
)
6441 struct isl_sched_graph
*graph
= user
;
6443 if (graph
->node
[i
].cluster
== graph
->node
[j
].cluster
)
6444 return isl_bool_true
;
6445 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
6448 /* Extract the merged clusters of SCCs in "graph", sort them, and
6449 * store them in c->clusters. Update c->scc_cluster accordingly.
6451 * First keep track of the cluster containing the SCC to which a node
6452 * belongs in the node itself.
6453 * Then extract the clusters into c->clusters, copying the current
6454 * band schedule from the SCCs that belong to the cluster.
6455 * Do this only once per cluster.
6457 * Finally, topologically sort the clusters and update c->scc_cluster
6458 * to match the new scc numbering. While the SCCs were originally
6459 * sorted already, some SCCs that depend on some other SCCs may
6460 * have been merged with SCCs that appear before these other SCCs.
6461 * A reordering may therefore be required.
6463 static isl_stat
extract_clusters(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6464 struct isl_clustering
*c
)
6468 for (i
= 0; i
< graph
->n
; ++i
)
6469 graph
->node
[i
].cluster
= c
->scc_cluster
[graph
->node
[i
].scc
];
6471 for (i
= 0; i
< graph
->scc
; ++i
) {
6472 if (c
->scc_cluster
[i
] != i
)
6474 if (extract_sub_graph(ctx
, graph
, &node_cluster_exactly
,
6475 &edge_cluster_exactly
, i
, &c
->cluster
[i
]) < 0)
6476 return isl_stat_error
;
6477 c
->cluster
[i
].src_scc
= -1;
6478 c
->cluster
[i
].dst_scc
= -1;
6479 if (copy_partial(graph
, c
, i
) < 0)
6480 return isl_stat_error
;
6483 if (detect_ccs(ctx
, graph
, &node_follows_strong_or_same_cluster
) < 0)
6484 return isl_stat_error
;
6485 for (i
= 0; i
< graph
->n
; ++i
)
6486 c
->scc_cluster
[graph
->node
[i
].scc
] = graph
->node
[i
].cluster
;
6491 /* Compute weights on the proximity edges of "graph" that can
6492 * be used by find_proximity to find the most appropriate
6493 * proximity edge to use to merge two clusters in "c".
6494 * The weights are also used by has_bounded_distances to determine
6495 * whether the merge should be allowed.
6496 * Store the maximum of the computed weights in graph->max_weight.
6498 * The computed weight is a measure for the number of remaining schedule
6499 * dimensions that can still be completely aligned.
6500 * In particular, compute the number of equalities between
6501 * input dimensions and output dimensions in the proximity constraints.
6502 * The directions that are already handled by outer schedule bands
6503 * are projected out prior to determining this number.
6505 * Edges that will never be considered by find_proximity are ignored.
6507 static isl_stat
compute_weights(struct isl_sched_graph
*graph
,
6508 struct isl_clustering
*c
)
6512 graph
->max_weight
= 0;
6514 for (i
= 0; i
< graph
->n_edge
; ++i
) {
6515 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
6516 struct isl_sched_node
*src
= edge
->src
;
6517 struct isl_sched_node
*dst
= edge
->dst
;
6518 isl_basic_map
*hull
;
6522 prox
= is_non_empty_proximity(edge
);
6524 return isl_stat_error
;
6527 if (bad_cluster(&c
->scc
[edge
->src
->scc
]) ||
6528 bad_cluster(&c
->scc
[edge
->dst
->scc
]))
6530 if (c
->scc_cluster
[edge
->dst
->scc
] ==
6531 c
->scc_cluster
[edge
->src
->scc
])
6534 hull
= isl_map_affine_hull(isl_map_copy(edge
->map
));
6535 hull
= isl_basic_map_transform_dims(hull
, isl_dim_in
, 0,
6536 isl_mat_copy(src
->vmap
));
6537 hull
= isl_basic_map_transform_dims(hull
, isl_dim_out
, 0,
6538 isl_mat_copy(dst
->vmap
));
6539 hull
= isl_basic_map_project_out(hull
,
6540 isl_dim_in
, 0, src
->rank
);
6541 hull
= isl_basic_map_project_out(hull
,
6542 isl_dim_out
, 0, dst
->rank
);
6543 hull
= isl_basic_map_remove_divs(hull
);
6544 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
6545 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
6546 hull
= isl_basic_map_drop_constraints_not_involving_dims(hull
,
6547 isl_dim_in
, 0, n_in
);
6548 hull
= isl_basic_map_drop_constraints_not_involving_dims(hull
,
6549 isl_dim_out
, 0, n_out
);
6551 return isl_stat_error
;
6552 edge
->weight
= isl_basic_map_n_equality(hull
);
6553 isl_basic_map_free(hull
);
6555 if (edge
->weight
> graph
->max_weight
)
6556 graph
->max_weight
= edge
->weight
;
6562 /* Call compute_schedule_finish_band on each of the clusters in "c"
6563 * in their topological order. This order is determined by the scc
6564 * fields of the nodes in "graph".
6565 * Combine the results in a sequence expressing the topological order.
6567 * If there is only one cluster left, then there is no need to introduce
6568 * a sequence node. Also, in this case, the cluster necessarily contains
6569 * the SCC at position 0 in the original graph and is therefore also
6570 * stored in the first cluster of "c".
6572 static __isl_give isl_schedule_node
*finish_bands_clustering(
6573 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
6574 struct isl_clustering
*c
)
6578 isl_union_set_list
*filters
;
6580 if (graph
->scc
== 1)
6581 return compute_schedule_finish_band(node
, &c
->cluster
[0], 0);
6583 ctx
= isl_schedule_node_get_ctx(node
);
6585 filters
= extract_sccs(ctx
, graph
);
6586 node
= isl_schedule_node_insert_sequence(node
, filters
);
6588 for (i
= 0; i
< graph
->scc
; ++i
) {
6589 int j
= c
->scc_cluster
[i
];
6590 node
= isl_schedule_node_child(node
, i
);
6591 node
= isl_schedule_node_child(node
, 0);
6592 node
= compute_schedule_finish_band(node
, &c
->cluster
[j
], 0);
6593 node
= isl_schedule_node_parent(node
);
6594 node
= isl_schedule_node_parent(node
);
6600 /* Compute a schedule for a connected dependence graph by first considering
6601 * each strongly connected component (SCC) in the graph separately and then
6602 * incrementally combining them into clusters.
6603 * Return the updated schedule node.
6605 * Initially, each cluster consists of a single SCC, each with its
6606 * own band schedule. The algorithm then tries to merge pairs
6607 * of clusters along a proximity edge until no more suitable
6608 * proximity edges can be found. During this merging, the schedule
6609 * is maintained in the individual SCCs.
6610 * After the merging is completed, the full resulting clusters
6611 * are extracted and in finish_bands_clustering,
6612 * compute_schedule_finish_band is called on each of them to integrate
6613 * the band into "node" and to continue the computation.
6615 * compute_weights initializes the weights that are used by find_proximity.
6617 static __isl_give isl_schedule_node
*compute_schedule_wcc_clustering(
6618 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
6621 struct isl_clustering c
;
6624 ctx
= isl_schedule_node_get_ctx(node
);
6626 if (clustering_init(ctx
, &c
, graph
) < 0)
6629 if (compute_weights(graph
, &c
) < 0)
6633 i
= find_proximity(graph
, &c
);
6636 if (i
>= graph
->n_edge
)
6638 if (merge_clusters_along_edge(ctx
, graph
, i
, &c
) < 0)
6642 if (extract_clusters(ctx
, graph
, &c
) < 0)
6645 node
= finish_bands_clustering(node
, graph
, &c
);
6647 clustering_free(ctx
, &c
);
6650 clustering_free(ctx
, &c
);
6651 return isl_schedule_node_free(node
);
6654 /* Compute a schedule for a connected dependence graph and return
6655 * the updated schedule node.
6657 * If Feautrier's algorithm is selected, we first recursively try to satisfy
6658 * as many validity dependences as possible. When all validity dependences
6659 * are satisfied we extend the schedule to a full-dimensional schedule.
6661 * Call compute_schedule_wcc_whole or compute_schedule_wcc_clustering
6662 * depending on whether the user has selected the option to try and
6663 * compute a schedule for the entire (weakly connected) component first.
6664 * If there is only a single strongly connected component (SCC), then
6665 * there is no point in trying to combine SCCs
6666 * in compute_schedule_wcc_clustering, so compute_schedule_wcc_whole
6667 * is called instead.
6669 static __isl_give isl_schedule_node
*compute_schedule_wcc(
6670 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
6677 ctx
= isl_schedule_node_get_ctx(node
);
6678 if (detect_sccs(ctx
, graph
) < 0)
6679 return isl_schedule_node_free(node
);
6681 if (compute_maxvar(graph
) < 0)
6682 return isl_schedule_node_free(node
);
6684 if (need_feautrier_step(ctx
, graph
))
6685 return compute_schedule_wcc_feautrier(node
, graph
);
6687 if (graph
->scc
<= 1 || isl_options_get_schedule_whole_component(ctx
))
6688 return compute_schedule_wcc_whole(node
, graph
);
6690 return compute_schedule_wcc_clustering(node
, graph
);
6693 /* Compute a schedule for each group of nodes identified by node->scc
6694 * separately and then combine them in a sequence node (or as set node
6695 * if graph->weak is set) inserted at position "node" of the schedule tree.
6696 * Return the updated schedule node.
6698 * If "wcc" is set then each of the groups belongs to a single
6699 * weakly connected component in the dependence graph so that
6700 * there is no need for compute_sub_schedule to look for weakly
6701 * connected components.
6703 static __isl_give isl_schedule_node
*compute_component_schedule(
6704 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
6709 isl_union_set_list
*filters
;
6713 ctx
= isl_schedule_node_get_ctx(node
);
6715 filters
= extract_sccs(ctx
, graph
);
6717 node
= isl_schedule_node_insert_set(node
, filters
);
6719 node
= isl_schedule_node_insert_sequence(node
, filters
);
6721 for (component
= 0; component
< graph
->scc
; ++component
) {
6722 node
= isl_schedule_node_child(node
, component
);
6723 node
= isl_schedule_node_child(node
, 0);
6724 node
= compute_sub_schedule(node
, ctx
, graph
,
6726 &edge_scc_exactly
, component
, wcc
);
6727 node
= isl_schedule_node_parent(node
);
6728 node
= isl_schedule_node_parent(node
);
6734 /* Compute a schedule for the given dependence graph and insert it at "node".
6735 * Return the updated schedule node.
6737 * We first check if the graph is connected (through validity and conditional
6738 * validity dependences) and, if not, compute a schedule
6739 * for each component separately.
6740 * If the schedule_serialize_sccs option is set, then we check for strongly
6741 * connected components instead and compute a separate schedule for
6742 * each such strongly connected component.
6744 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
6745 struct isl_sched_graph
*graph
)
6752 ctx
= isl_schedule_node_get_ctx(node
);
6753 if (isl_options_get_schedule_serialize_sccs(ctx
)) {
6754 if (detect_sccs(ctx
, graph
) < 0)
6755 return isl_schedule_node_free(node
);
6757 if (detect_wccs(ctx
, graph
) < 0)
6758 return isl_schedule_node_free(node
);
6762 return compute_component_schedule(node
, graph
, 1);
6764 return compute_schedule_wcc(node
, graph
);
6767 /* Compute a schedule on sc->domain that respects the given schedule
6770 * In particular, the schedule respects all the validity dependences.
6771 * If the default isl scheduling algorithm is used, it tries to minimize
6772 * the dependence distances over the proximity dependences.
6773 * If Feautrier's scheduling algorithm is used, the proximity dependence
6774 * distances are only minimized during the extension to a full-dimensional
6777 * If there are any condition and conditional validity dependences,
6778 * then the conditional validity dependences may be violated inside
6779 * a tilable band, provided they have no adjacent non-local
6780 * condition dependences.
6782 __isl_give isl_schedule
*isl_schedule_constraints_compute_schedule(
6783 __isl_take isl_schedule_constraints
*sc
)
6785 isl_ctx
*ctx
= isl_schedule_constraints_get_ctx(sc
);
6786 struct isl_sched_graph graph
= { 0 };
6787 isl_schedule
*sched
;
6788 isl_schedule_node
*node
;
6789 isl_union_set
*domain
;
6791 sc
= isl_schedule_constraints_align_params(sc
);
6793 domain
= isl_schedule_constraints_get_domain(sc
);
6794 if (isl_union_set_n_set(domain
) == 0) {
6795 isl_schedule_constraints_free(sc
);
6796 return isl_schedule_from_domain(domain
);
6799 if (graph_init(&graph
, sc
) < 0)
6800 domain
= isl_union_set_free(domain
);
6802 node
= isl_schedule_node_from_domain(domain
);
6803 node
= isl_schedule_node_child(node
, 0);
6805 node
= compute_schedule(node
, &graph
);
6806 sched
= isl_schedule_node_get_schedule(node
);
6807 isl_schedule_node_free(node
);
6809 graph_free(ctx
, &graph
);
6810 isl_schedule_constraints_free(sc
);
6815 /* Compute a schedule for the given union of domains that respects
6816 * all the validity dependences and minimizes
6817 * the dependence distances over the proximity dependences.
6819 * This function is kept for backward compatibility.
6821 __isl_give isl_schedule
*isl_union_set_compute_schedule(
6822 __isl_take isl_union_set
*domain
,
6823 __isl_take isl_union_map
*validity
,
6824 __isl_take isl_union_map
*proximity
)
6826 isl_schedule_constraints
*sc
;
6828 sc
= isl_schedule_constraints_on_domain(domain
);
6829 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
6830 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
6832 return isl_schedule_constraints_compute_schedule(sc
);