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>
24 #include <isl/constraint.h>
25 #include <isl/schedule.h>
26 #include <isl_schedule_constraints.h>
27 #include <isl/schedule_node.h>
28 #include <isl_mat_private.h>
29 #include <isl_vec_private.h>
31 #include <isl_union_set_private.h>
34 #include <isl_dim_map.h>
35 #include <isl/map_to_basic_set.h>
37 #include <isl_options_private.h>
38 #include <isl_tarjan.h>
39 #include <isl_morph.h>
41 #include <isl_val_private.h>
44 * The scheduling algorithm implemented in this file was inspired by
45 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
46 * Parallelization and Locality Optimization in the Polyhedral Model".
48 * For a detailed description of the variant implemented in isl,
49 * see Verdoolaege and Janssens, "Scheduling for PPCG" (2017).
53 /* Internal information about a node that is used during the construction
55 * space represents the original space in which the domain lives;
56 * that is, the space is not affected by compression
57 * sched is a matrix representation of the schedule being constructed
58 * for this node; if compressed is set, then this schedule is
59 * defined over the compressed domain space
60 * sched_map is an isl_map representation of the same (partial) schedule
61 * sched_map may be NULL; if compressed is set, then this map
62 * is defined over the uncompressed domain space
63 * rank is the number of linearly independent rows in the linear part
65 * the rows of "vmap" represent a change of basis for the node
66 * variables; the first rank rows span the linear part of
67 * the schedule rows; the remaining rows are linearly independent
68 * the rows of "indep" represent linear combinations of the schedule
69 * coefficients that are non-zero when the schedule coefficients are
70 * linearly independent of previously computed schedule rows.
71 * start is the first variable in the LP problem in the sequences that
72 * represents the schedule coefficients of this node
73 * nvar is the dimension of the (compressed) domain
74 * nparam is the number of parameters or 0 if we are not constructing
75 * a parametric schedule
77 * If compressed is set, then hull represents the constraints
78 * that were used to derive the compression, while compress and
79 * decompress map the original space to the compressed space and
82 * scc is the index of SCC (or WCC) this node belongs to
84 * "cluster" is only used inside extract_clusters and identifies
85 * the cluster of SCCs that the node belongs to.
87 * coincident contains a boolean for each of the rows of the schedule,
88 * indicating whether the corresponding scheduling dimension satisfies
89 * the coincidence constraints in the sense that the corresponding
90 * dependence distances are zero.
92 * If the schedule_treat_coalescing option is set, then
93 * "sizes" contains the sizes of the (compressed) instance set
94 * in each direction. If there is no fixed size in a given direction,
95 * then the corresponding size value is set to infinity.
96 * If the schedule_treat_coalescing option or the schedule_max_coefficient
97 * option is set, then "max" contains the maximal values for
98 * schedule coefficients of the (compressed) variables. If no bound
99 * needs to be imposed on a particular variable, then the corresponding
101 * If not NULL, then "bounds" contains a non-parametric set
102 * in the compressed space that is bounded by the size in each direction.
104 struct isl_sched_node
{
108 isl_multi_aff
*compress
;
109 isl_multi_aff
*decompress
;
124 isl_multi_val
*sizes
;
125 isl_basic_set
*bounds
;
129 static int node_has_tuples(const void *entry
, const void *val
)
131 struct isl_sched_node
*node
= (struct isl_sched_node
*)entry
;
132 isl_space
*space
= (isl_space
*) val
;
134 return isl_space_has_equal_tuples(node
->space
, space
);
137 static int node_scc_exactly(struct isl_sched_node
*node
, int scc
)
139 return node
->scc
== scc
;
142 static int node_scc_at_most(struct isl_sched_node
*node
, int scc
)
144 return node
->scc
<= scc
;
147 static int node_scc_at_least(struct isl_sched_node
*node
, int scc
)
149 return node
->scc
>= scc
;
152 /* An edge in the dependence graph. An edge may be used to
153 * ensure validity of the generated schedule, to minimize the dependence
156 * map is the dependence relation, with i -> j in the map if j depends on i
157 * tagged_condition and tagged_validity contain the union of all tagged
158 * condition or conditional validity dependence relations that
159 * specialize the dependence relation "map"; that is,
160 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
161 * or "tagged_validity", then i -> j is an element of "map".
162 * If these fields are NULL, then they represent the empty relation.
163 * src is the source node
164 * dst is the sink node
166 * types is a bit vector containing the types of this edge.
167 * validity is set if the edge is used to ensure correctness
168 * coincidence is used to enforce zero dependence distances
169 * proximity is set if the edge is used to minimize dependence distances
170 * condition is set if the edge represents a condition
171 * for a conditional validity schedule constraint
172 * local can only be set for condition edges and indicates that
173 * the dependence distance over the edge should be zero
174 * conditional_validity is set if the edge is used to conditionally
177 * For validity edges, start and end mark the sequence of inequality
178 * constraints in the LP problem that encode the validity constraint
179 * corresponding to this edge.
181 * During clustering, an edge may be marked "no_merge" if it should
182 * not be used to merge clusters.
183 * The weight is also only used during clustering and it is
184 * an indication of how many schedule dimensions on either side
185 * of the schedule constraints can be aligned.
186 * If the weight is negative, then this means that this edge was postponed
187 * by has_bounded_distances or any_no_merge. The original weight can
188 * be retrieved by adding 1 + graph->max_weight, with "graph"
189 * the graph containing this edge.
191 struct isl_sched_edge
{
193 isl_union_map
*tagged_condition
;
194 isl_union_map
*tagged_validity
;
196 struct isl_sched_node
*src
;
197 struct isl_sched_node
*dst
;
208 /* Is "edge" marked as being of type "type"?
210 static int is_type(struct isl_sched_edge
*edge
, enum isl_edge_type type
)
212 return ISL_FL_ISSET(edge
->types
, 1 << type
);
215 /* Mark "edge" as being of type "type".
217 static void set_type(struct isl_sched_edge
*edge
, enum isl_edge_type type
)
219 ISL_FL_SET(edge
->types
, 1 << type
);
222 /* No longer mark "edge" as being of type "type"?
224 static void clear_type(struct isl_sched_edge
*edge
, enum isl_edge_type type
)
226 ISL_FL_CLR(edge
->types
, 1 << type
);
229 /* Is "edge" marked as a validity edge?
231 static int is_validity(struct isl_sched_edge
*edge
)
233 return is_type(edge
, isl_edge_validity
);
236 /* Mark "edge" as a validity edge.
238 static void set_validity(struct isl_sched_edge
*edge
)
240 set_type(edge
, isl_edge_validity
);
243 /* Is "edge" marked as a proximity edge?
245 static int is_proximity(struct isl_sched_edge
*edge
)
247 return is_type(edge
, isl_edge_proximity
);
250 /* Is "edge" marked as a local edge?
252 static int is_local(struct isl_sched_edge
*edge
)
254 return is_type(edge
, isl_edge_local
);
257 /* Mark "edge" as a local edge.
259 static void set_local(struct isl_sched_edge
*edge
)
261 set_type(edge
, isl_edge_local
);
264 /* No longer mark "edge" as a local edge.
266 static void clear_local(struct isl_sched_edge
*edge
)
268 clear_type(edge
, isl_edge_local
);
271 /* Is "edge" marked as a coincidence edge?
273 static int is_coincidence(struct isl_sched_edge
*edge
)
275 return is_type(edge
, isl_edge_coincidence
);
278 /* Is "edge" marked as a condition edge?
280 static int is_condition(struct isl_sched_edge
*edge
)
282 return is_type(edge
, isl_edge_condition
);
285 /* Is "edge" marked as a conditional validity edge?
287 static int is_conditional_validity(struct isl_sched_edge
*edge
)
289 return is_type(edge
, isl_edge_conditional_validity
);
292 /* Is "edge" of a type that can appear multiple times between
293 * the same pair of nodes?
295 * Condition edges and conditional validity edges may have tagged
296 * dependence relations, in which case an edge is added for each
299 static int is_multi_edge_type(struct isl_sched_edge
*edge
)
301 return is_condition(edge
) || is_conditional_validity(edge
);
304 /* Internal information about the dependence graph used during
305 * the construction of the schedule.
307 * intra_hmap is a cache, mapping dependence relations to their dual,
308 * for dependences from a node to itself, possibly without
309 * coefficients for the parameters
310 * intra_hmap_param is a cache, mapping dependence relations to their dual,
311 * for dependences from a node to itself, including coefficients
313 * inter_hmap is a cache, mapping dependence relations to their dual,
314 * for dependences between distinct nodes
315 * if compression is involved then the key for these maps
316 * is the original, uncompressed dependence relation, while
317 * the value is the dual of the compressed dependence relation.
319 * n is the number of nodes
320 * node is the list of nodes
321 * maxvar is the maximal number of variables over all nodes
322 * max_row is the allocated number of rows in the schedule
323 * n_row is the current (maximal) number of linearly independent
324 * rows in the node schedules
325 * n_total_row is the current number of rows in the node schedules
326 * band_start is the starting row in the node schedules of the current band
327 * root is set to the original dependence graph from which this graph
328 * is derived through splitting. If this graph is not the result of
329 * splitting, then the root field points to the graph itself.
331 * sorted contains a list of node indices sorted according to the
332 * SCC to which a node belongs
334 * n_edge is the number of edges
335 * edge is the list of edges
336 * max_edge contains the maximal number of edges of each type;
337 * in particular, it contains the number of edges in the inital graph.
338 * edge_table contains pointers into the edge array, hashed on the source
339 * and sink spaces; there is one such table for each type;
340 * a given edge may be referenced from more than one table
341 * if the corresponding relation appears in more than one of the
342 * sets of dependences; however, for each type there is only
343 * a single edge between a given pair of source and sink space
344 * in the entire graph
346 * node_table contains pointers into the node array, hashed on the space tuples
348 * region contains a list of variable sequences that should be non-trivial
350 * lp contains the (I)LP problem used to obtain new schedule rows
352 * src_scc and dst_scc are the source and sink SCCs of an edge with
353 * conflicting constraints
355 * scc represents the number of components
356 * weak is set if the components are weakly connected
358 * max_weight is used during clustering and represents the maximal
359 * weight of the relevant proximity edges.
361 struct isl_sched_graph
{
362 isl_map_to_basic_set
*intra_hmap
;
363 isl_map_to_basic_set
*intra_hmap_param
;
364 isl_map_to_basic_set
*inter_hmap
;
366 struct isl_sched_node
*node
;
377 struct isl_sched_graph
*root
;
379 struct isl_sched_edge
*edge
;
381 int max_edge
[isl_edge_last
+ 1];
382 struct isl_hash_table
*edge_table
[isl_edge_last
+ 1];
384 struct isl_hash_table
*node_table
;
385 struct isl_trivial_region
*region
;
398 /* Initialize node_table based on the list of nodes.
400 static int graph_init_table(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
404 graph
->node_table
= isl_hash_table_alloc(ctx
, graph
->n
);
405 if (!graph
->node_table
)
408 for (i
= 0; i
< graph
->n
; ++i
) {
409 struct isl_hash_table_entry
*entry
;
412 hash
= isl_space_get_tuple_hash(graph
->node
[i
].space
);
413 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
415 graph
->node
[i
].space
, 1);
418 entry
->data
= &graph
->node
[i
];
424 /* Return a pointer to the node that lives within the given space,
425 * an invalid node if there is no such node, or NULL in case of error.
427 static struct isl_sched_node
*graph_find_node(isl_ctx
*ctx
,
428 struct isl_sched_graph
*graph
, __isl_keep isl_space
*space
)
430 struct isl_hash_table_entry
*entry
;
436 hash
= isl_space_get_tuple_hash(space
);
437 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
438 &node_has_tuples
, space
, 0);
440 return entry
? entry
->data
: graph
->node
+ graph
->n
;
443 /* Is "node" a node in "graph"?
445 static int is_node(struct isl_sched_graph
*graph
,
446 struct isl_sched_node
*node
)
448 return node
&& node
>= &graph
->node
[0] && node
< &graph
->node
[graph
->n
];
451 static int edge_has_src_and_dst(const void *entry
, const void *val
)
453 const struct isl_sched_edge
*edge
= entry
;
454 const struct isl_sched_edge
*temp
= val
;
456 return edge
->src
== temp
->src
&& edge
->dst
== temp
->dst
;
459 /* Add the given edge to graph->edge_table[type].
461 static isl_stat
graph_edge_table_add(isl_ctx
*ctx
,
462 struct isl_sched_graph
*graph
, enum isl_edge_type type
,
463 struct isl_sched_edge
*edge
)
465 struct isl_hash_table_entry
*entry
;
468 hash
= isl_hash_init();
469 hash
= isl_hash_builtin(hash
, edge
->src
);
470 hash
= isl_hash_builtin(hash
, edge
->dst
);
471 entry
= isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
472 &edge_has_src_and_dst
, edge
, 1);
474 return isl_stat_error
;
480 /* Add "edge" to all relevant edge tables.
481 * That is, for every type of the edge, add it to the corresponding table.
483 static isl_stat
graph_edge_tables_add(isl_ctx
*ctx
,
484 struct isl_sched_graph
*graph
, struct isl_sched_edge
*edge
)
486 enum isl_edge_type t
;
488 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
489 if (!is_type(edge
, t
))
491 if (graph_edge_table_add(ctx
, graph
, t
, edge
) < 0)
492 return isl_stat_error
;
498 /* Allocate the edge_tables based on the maximal number of edges of
501 static int graph_init_edge_tables(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
505 for (i
= 0; i
<= isl_edge_last
; ++i
) {
506 graph
->edge_table
[i
] = isl_hash_table_alloc(ctx
,
508 if (!graph
->edge_table
[i
])
515 /* If graph->edge_table[type] contains an edge from the given source
516 * to the given destination, then return the hash table entry of this edge.
517 * Otherwise, return NULL.
519 static struct isl_hash_table_entry
*graph_find_edge_entry(
520 struct isl_sched_graph
*graph
,
521 enum isl_edge_type type
,
522 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
524 isl_ctx
*ctx
= isl_space_get_ctx(src
->space
);
526 struct isl_sched_edge temp
= { .src
= src
, .dst
= dst
};
528 hash
= isl_hash_init();
529 hash
= isl_hash_builtin(hash
, temp
.src
);
530 hash
= isl_hash_builtin(hash
, temp
.dst
);
531 return isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
532 &edge_has_src_and_dst
, &temp
, 0);
536 /* If graph->edge_table[type] contains an edge from the given source
537 * to the given destination, then return this edge.
538 * Otherwise, return NULL.
540 static struct isl_sched_edge
*graph_find_edge(struct isl_sched_graph
*graph
,
541 enum isl_edge_type type
,
542 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
544 struct isl_hash_table_entry
*entry
;
546 entry
= graph_find_edge_entry(graph
, type
, src
, dst
);
553 /* Check whether the dependence graph has an edge of the given type
554 * between the given two nodes.
556 static isl_bool
graph_has_edge(struct isl_sched_graph
*graph
,
557 enum isl_edge_type type
,
558 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
560 struct isl_sched_edge
*edge
;
563 edge
= graph_find_edge(graph
, type
, src
, dst
);
565 return isl_bool_false
;
567 empty
= isl_map_plain_is_empty(edge
->map
);
569 return isl_bool_not(empty
);
572 /* Look for any edge with the same src, dst and map fields as "model".
574 * Return the matching edge if one can be found.
575 * Return "model" if no matching edge is found.
576 * Return NULL on error.
578 static struct isl_sched_edge
*graph_find_matching_edge(
579 struct isl_sched_graph
*graph
, struct isl_sched_edge
*model
)
581 enum isl_edge_type i
;
582 struct isl_sched_edge
*edge
;
584 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
587 edge
= graph_find_edge(graph
, i
, model
->src
, model
->dst
);
590 is_equal
= isl_map_plain_is_equal(model
->map
, edge
->map
);
600 /* Remove the given edge from all the edge_tables that refer to it.
602 static void graph_remove_edge(struct isl_sched_graph
*graph
,
603 struct isl_sched_edge
*edge
)
605 isl_ctx
*ctx
= isl_map_get_ctx(edge
->map
);
606 enum isl_edge_type i
;
608 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
609 struct isl_hash_table_entry
*entry
;
611 entry
= graph_find_edge_entry(graph
, i
, edge
->src
, edge
->dst
);
614 if (entry
->data
!= edge
)
616 isl_hash_table_remove(ctx
, graph
->edge_table
[i
], entry
);
620 /* Check whether the dependence graph has any edge
621 * between the given two nodes.
623 static isl_bool
graph_has_any_edge(struct isl_sched_graph
*graph
,
624 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
626 enum isl_edge_type i
;
629 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
630 r
= graph_has_edge(graph
, i
, src
, dst
);
638 /* Check whether the dependence graph has a validity edge
639 * between the given two nodes.
641 * Conditional validity edges are essentially validity edges that
642 * can be ignored if the corresponding condition edges are iteration private.
643 * Here, we are only checking for the presence of validity
644 * edges, so we need to consider the conditional validity edges too.
645 * In particular, this function is used during the detection
646 * of strongly connected components and we cannot ignore
647 * conditional validity edges during this detection.
649 static isl_bool
graph_has_validity_edge(struct isl_sched_graph
*graph
,
650 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
654 r
= graph_has_edge(graph
, isl_edge_validity
, src
, dst
);
658 return graph_has_edge(graph
, isl_edge_conditional_validity
, src
, dst
);
661 /* Perform all the required memory allocations for a schedule graph "graph"
662 * with "n_node" nodes and "n_edge" edge and initialize the corresponding
665 static isl_stat
graph_alloc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
666 int n_node
, int n_edge
)
671 graph
->n_edge
= n_edge
;
672 graph
->node
= isl_calloc_array(ctx
, struct isl_sched_node
, graph
->n
);
673 graph
->sorted
= isl_calloc_array(ctx
, int, graph
->n
);
674 graph
->region
= isl_alloc_array(ctx
,
675 struct isl_trivial_region
, graph
->n
);
676 graph
->edge
= isl_calloc_array(ctx
,
677 struct isl_sched_edge
, graph
->n_edge
);
679 graph
->intra_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
680 graph
->intra_hmap_param
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
681 graph
->inter_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
683 if (!graph
->node
|| !graph
->region
|| (graph
->n_edge
&& !graph
->edge
) ||
685 return isl_stat_error
;
687 for(i
= 0; i
< graph
->n
; ++i
)
688 graph
->sorted
[i
] = i
;
693 /* Free the memory associated to node "node" in "graph".
694 * The "coincident" field is shared by nodes in a graph and its subgraph.
695 * It therefore only needs to be freed for the original dependence graph,
696 * i.e., one that is not the result of splitting.
698 static void clear_node(struct isl_sched_graph
*graph
,
699 struct isl_sched_node
*node
)
701 isl_space_free(node
->space
);
702 isl_set_free(node
->hull
);
703 isl_multi_aff_free(node
->compress
);
704 isl_multi_aff_free(node
->decompress
);
705 isl_mat_free(node
->sched
);
706 isl_map_free(node
->sched_map
);
707 isl_mat_free(node
->indep
);
708 isl_mat_free(node
->vmap
);
709 if (graph
->root
== graph
)
710 free(node
->coincident
);
711 isl_multi_val_free(node
->sizes
);
712 isl_basic_set_free(node
->bounds
);
713 isl_vec_free(node
->max
);
716 static void graph_free(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
720 isl_map_to_basic_set_free(graph
->intra_hmap
);
721 isl_map_to_basic_set_free(graph
->intra_hmap_param
);
722 isl_map_to_basic_set_free(graph
->inter_hmap
);
725 for (i
= 0; i
< graph
->n
; ++i
)
726 clear_node(graph
, &graph
->node
[i
]);
730 for (i
= 0; i
< graph
->n_edge
; ++i
) {
731 isl_map_free(graph
->edge
[i
].map
);
732 isl_union_map_free(graph
->edge
[i
].tagged_condition
);
733 isl_union_map_free(graph
->edge
[i
].tagged_validity
);
737 for (i
= 0; i
<= isl_edge_last
; ++i
)
738 isl_hash_table_free(ctx
, graph
->edge_table
[i
]);
739 isl_hash_table_free(ctx
, graph
->node_table
);
740 isl_basic_set_free(graph
->lp
);
743 /* For each "set" on which this function is called, increment
744 * graph->n by one and update graph->maxvar.
746 static isl_stat
init_n_maxvar(__isl_take isl_set
*set
, void *user
)
748 struct isl_sched_graph
*graph
= user
;
749 int nvar
= isl_set_dim(set
, isl_dim_set
);
752 if (nvar
> graph
->maxvar
)
753 graph
->maxvar
= nvar
;
760 /* Compute the number of rows that should be allocated for the schedule.
761 * In particular, we need one row for each variable or one row
762 * for each basic map in the dependences.
763 * Note that it is practically impossible to exhaust both
764 * the number of dependences and the number of variables.
766 static isl_stat
compute_max_row(struct isl_sched_graph
*graph
,
767 __isl_keep isl_schedule_constraints
*sc
)
771 isl_union_set
*domain
;
775 domain
= isl_schedule_constraints_get_domain(sc
);
776 r
= isl_union_set_foreach_set(domain
, &init_n_maxvar
, graph
);
777 isl_union_set_free(domain
);
779 return isl_stat_error
;
780 n_edge
= isl_schedule_constraints_n_basic_map(sc
);
782 return isl_stat_error
;
783 graph
->max_row
= n_edge
+ graph
->maxvar
;
788 /* Does "bset" have any defining equalities for its set variables?
790 static isl_bool
has_any_defining_equality(__isl_keep isl_basic_set
*bset
)
795 return isl_bool_error
;
797 n
= isl_basic_set_dim(bset
, isl_dim_set
);
798 for (i
= 0; i
< n
; ++i
) {
801 has
= isl_basic_set_has_defining_equality(bset
, isl_dim_set
, i
,
807 return isl_bool_false
;
810 /* Set the entries of node->max to the value of the schedule_max_coefficient
813 static isl_stat
set_max_coefficient(isl_ctx
*ctx
, struct isl_sched_node
*node
)
817 max
= isl_options_get_schedule_max_coefficient(ctx
);
821 node
->max
= isl_vec_alloc(ctx
, node
->nvar
);
822 node
->max
= isl_vec_set_si(node
->max
, max
);
824 return isl_stat_error
;
829 /* Set the entries of node->max to the minimum of the schedule_max_coefficient
830 * option (if set) and half of the minimum of the sizes in the other
831 * dimensions. Round up when computing the half such that
832 * if the minimum of the sizes is one, half of the size is taken to be one
834 * If the global minimum is unbounded (i.e., if both
835 * the schedule_max_coefficient is not set and the sizes in the other
836 * dimensions are unbounded), then store a negative value.
837 * If the schedule coefficient is close to the size of the instance set
838 * in another dimension, then the schedule may represent a loop
839 * coalescing transformation (especially if the coefficient
840 * in that other dimension is one). Forcing the coefficient to be
841 * smaller than or equal to half the minimal size should avoid this
844 static isl_stat
compute_max_coefficient(isl_ctx
*ctx
,
845 struct isl_sched_node
*node
)
851 max
= isl_options_get_schedule_max_coefficient(ctx
);
852 v
= isl_vec_alloc(ctx
, node
->nvar
);
854 return isl_stat_error
;
856 for (i
= 0; i
< node
->nvar
; ++i
) {
857 isl_int_set_si(v
->el
[i
], max
);
858 isl_int_mul_si(v
->el
[i
], v
->el
[i
], 2);
861 for (i
= 0; i
< node
->nvar
; ++i
) {
864 size
= isl_multi_val_get_val(node
->sizes
, i
);
867 if (!isl_val_is_int(size
)) {
871 for (j
= 0; j
< node
->nvar
; ++j
) {
874 if (isl_int_is_neg(v
->el
[j
]) ||
875 isl_int_gt(v
->el
[j
], size
->n
))
876 isl_int_set(v
->el
[j
], size
->n
);
881 for (i
= 0; i
< node
->nvar
; ++i
)
882 isl_int_cdiv_q_ui(v
->el
[i
], v
->el
[i
], 2);
888 return isl_stat_error
;
891 /* Compute and return the size of "set" in dimension "dim".
892 * The size is taken to be the difference in values for that variable
893 * for fixed values of the other variables.
894 * This assumes that "set" is convex.
895 * In particular, the variable is first isolated from the other variables
896 * in the range of a map
898 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [i_dim]
900 * and then duplicated
902 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [[i_dim] -> [i_dim']]
904 * The shared variables are then projected out and the maximal value
905 * of i_dim' - i_dim is computed.
907 static __isl_give isl_val
*compute_size(__isl_take isl_set
*set
, int dim
)
914 map
= isl_set_project_onto_map(set
, isl_dim_set
, dim
, 1);
915 map
= isl_map_project_out(map
, isl_dim_in
, dim
, 1);
916 map
= isl_map_range_product(map
, isl_map_copy(map
));
917 map
= isl_set_unwrap(isl_map_range(map
));
918 set
= isl_map_deltas(map
);
919 ls
= isl_local_space_from_space(isl_set_get_space(set
));
920 obj
= isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
921 v
= isl_set_max_val(set
, obj
);
928 /* Compute the size of the instance set "set" of "node", after compression,
929 * as well as bounds on the corresponding coefficients, if needed.
931 * The sizes are needed when the schedule_treat_coalescing option is set.
932 * The bounds are needed when the schedule_treat_coalescing option or
933 * the schedule_max_coefficient option is set.
935 * If the schedule_treat_coalescing option is not set, then at most
936 * the bounds need to be set and this is done in set_max_coefficient.
937 * Otherwise, compress the domain if needed, compute the size
938 * in each direction and store the results in node->size.
939 * If the domain is not convex, then the sizes are computed
940 * on a convex superset in order to avoid picking up sizes
941 * that are valid for the individual disjuncts, but not for
942 * the domain as a whole.
943 * Finally, set the bounds on the coefficients based on the sizes
944 * and the schedule_max_coefficient option in compute_max_coefficient.
946 static isl_stat
compute_sizes_and_max(isl_ctx
*ctx
, struct isl_sched_node
*node
,
947 __isl_take isl_set
*set
)
952 if (!isl_options_get_schedule_treat_coalescing(ctx
)) {
954 return set_max_coefficient(ctx
, node
);
957 if (node
->compressed
)
958 set
= isl_set_preimage_multi_aff(set
,
959 isl_multi_aff_copy(node
->decompress
));
960 set
= isl_set_from_basic_set(isl_set_simple_hull(set
));
961 mv
= isl_multi_val_zero(isl_set_get_space(set
));
962 n
= isl_set_dim(set
, isl_dim_set
);
963 for (j
= 0; j
< n
; ++j
) {
966 v
= compute_size(isl_set_copy(set
), j
);
967 mv
= isl_multi_val_set_val(mv
, j
, v
);
972 return isl_stat_error
;
973 return compute_max_coefficient(ctx
, node
);
976 /* Add a new node to the graph representing the given instance set.
977 * "nvar" is the (possibly compressed) number of variables and
978 * may be smaller than then number of set variables in "set"
979 * if "compressed" is set.
980 * If "compressed" is set, then "hull" represents the constraints
981 * that were used to derive the compression, while "compress" and
982 * "decompress" map the original space to the compressed space and
984 * If "compressed" is not set, then "hull", "compress" and "decompress"
987 * Compute the size of the instance set and bounds on the coefficients,
990 static isl_stat
add_node(struct isl_sched_graph
*graph
,
991 __isl_take isl_set
*set
, int nvar
, int compressed
,
992 __isl_take isl_set
*hull
, __isl_take isl_multi_aff
*compress
,
993 __isl_take isl_multi_aff
*decompress
)
1000 struct isl_sched_node
*node
;
1005 ctx
= isl_set_get_ctx(set
);
1006 nparam
= isl_set_dim(set
, isl_dim_param
);
1007 if (!ctx
->opt
->schedule_parametric
)
1009 sched
= isl_mat_alloc(ctx
, 0, 1 + nparam
+ nvar
);
1010 node
= &graph
->node
[graph
->n
];
1012 space
= isl_set_get_space(set
);
1013 node
->space
= space
;
1015 node
->nparam
= nparam
;
1016 node
->sched
= sched
;
1017 node
->sched_map
= NULL
;
1018 coincident
= isl_calloc_array(ctx
, int, graph
->max_row
);
1019 node
->coincident
= coincident
;
1020 node
->compressed
= compressed
;
1022 node
->compress
= compress
;
1023 node
->decompress
= decompress
;
1024 if (compute_sizes_and_max(ctx
, node
, set
) < 0)
1025 return isl_stat_error
;
1027 if (!space
|| !sched
|| (graph
->max_row
&& !coincident
))
1028 return isl_stat_error
;
1029 if (compressed
&& (!hull
|| !compress
|| !decompress
))
1030 return isl_stat_error
;
1036 isl_multi_aff_free(compress
);
1037 isl_multi_aff_free(decompress
);
1038 return isl_stat_error
;
1041 /* Construct an identifier for node "node", which will represent "set".
1042 * The name of the identifier is either "compressed" or
1043 * "compressed_<name>", with <name> the name of the space of "set".
1044 * The user pointer of the identifier points to "node".
1046 static __isl_give isl_id
*construct_compressed_id(__isl_keep isl_set
*set
,
1047 struct isl_sched_node
*node
)
1056 has_name
= isl_set_has_tuple_name(set
);
1060 ctx
= isl_set_get_ctx(set
);
1062 return isl_id_alloc(ctx
, "compressed", node
);
1064 p
= isl_printer_to_str(ctx
);
1065 name
= isl_set_get_tuple_name(set
);
1066 p
= isl_printer_print_str(p
, "compressed_");
1067 p
= isl_printer_print_str(p
, name
);
1068 id_name
= isl_printer_get_str(p
);
1069 isl_printer_free(p
);
1071 id
= isl_id_alloc(ctx
, id_name
, node
);
1077 /* Add a new node to the graph representing the given set.
1079 * If any of the set variables is defined by an equality, then
1080 * we perform variable compression such that we can perform
1081 * the scheduling on the compressed domain.
1082 * In this case, an identifier is used that references the new node
1083 * such that each compressed space is unique and
1084 * such that the node can be recovered from the compressed space.
1086 static isl_stat
extract_node(__isl_take isl_set
*set
, void *user
)
1089 isl_bool has_equality
;
1091 isl_basic_set
*hull
;
1094 isl_multi_aff
*compress
, *decompress
;
1095 struct isl_sched_graph
*graph
= user
;
1097 hull
= isl_set_affine_hull(isl_set_copy(set
));
1098 hull
= isl_basic_set_remove_divs(hull
);
1099 nvar
= isl_set_dim(set
, isl_dim_set
);
1100 has_equality
= has_any_defining_equality(hull
);
1102 if (has_equality
< 0)
1104 if (!has_equality
) {
1105 isl_basic_set_free(hull
);
1106 return add_node(graph
, set
, nvar
, 0, NULL
, NULL
, NULL
);
1109 id
= construct_compressed_id(set
, &graph
->node
[graph
->n
]);
1110 morph
= isl_basic_set_variable_compression_with_id(hull
,
1113 nvar
= isl_morph_ran_dim(morph
, isl_dim_set
);
1114 compress
= isl_morph_get_var_multi_aff(morph
);
1115 morph
= isl_morph_inverse(morph
);
1116 decompress
= isl_morph_get_var_multi_aff(morph
);
1117 isl_morph_free(morph
);
1119 hull_set
= isl_set_from_basic_set(hull
);
1120 return add_node(graph
, set
, nvar
, 1, hull_set
, compress
, decompress
);
1122 isl_basic_set_free(hull
);
1124 return isl_stat_error
;
1127 struct isl_extract_edge_data
{
1128 enum isl_edge_type type
;
1129 struct isl_sched_graph
*graph
;
1132 /* Merge edge2 into edge1, freeing the contents of edge2.
1133 * Return 0 on success and -1 on failure.
1135 * edge1 and edge2 are assumed to have the same value for the map field.
1137 static int merge_edge(struct isl_sched_edge
*edge1
,
1138 struct isl_sched_edge
*edge2
)
1140 edge1
->types
|= edge2
->types
;
1141 isl_map_free(edge2
->map
);
1143 if (is_condition(edge2
)) {
1144 if (!edge1
->tagged_condition
)
1145 edge1
->tagged_condition
= edge2
->tagged_condition
;
1147 edge1
->tagged_condition
=
1148 isl_union_map_union(edge1
->tagged_condition
,
1149 edge2
->tagged_condition
);
1152 if (is_conditional_validity(edge2
)) {
1153 if (!edge1
->tagged_validity
)
1154 edge1
->tagged_validity
= edge2
->tagged_validity
;
1156 edge1
->tagged_validity
=
1157 isl_union_map_union(edge1
->tagged_validity
,
1158 edge2
->tagged_validity
);
1161 if (is_condition(edge2
) && !edge1
->tagged_condition
)
1163 if (is_conditional_validity(edge2
) && !edge1
->tagged_validity
)
1169 /* Insert dummy tags in domain and range of "map".
1171 * In particular, if "map" is of the form
1177 * [A -> dummy_tag] -> [B -> dummy_tag]
1179 * where the dummy_tags are identical and equal to any dummy tags
1180 * introduced by any other call to this function.
1182 static __isl_give isl_map
*insert_dummy_tags(__isl_take isl_map
*map
)
1188 isl_set
*domain
, *range
;
1190 ctx
= isl_map_get_ctx(map
);
1192 id
= isl_id_alloc(ctx
, NULL
, &dummy
);
1193 space
= isl_space_params(isl_map_get_space(map
));
1194 space
= isl_space_set_from_params(space
);
1195 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
1196 space
= isl_space_map_from_set(space
);
1198 domain
= isl_map_wrap(map
);
1199 range
= isl_map_wrap(isl_map_universe(space
));
1200 map
= isl_map_from_domain_and_range(domain
, range
);
1201 map
= isl_map_zip(map
);
1206 /* Given that at least one of "src" or "dst" is compressed, return
1207 * a map between the spaces of these nodes restricted to the affine
1208 * hull that was used in the compression.
1210 static __isl_give isl_map
*extract_hull(struct isl_sched_node
*src
,
1211 struct isl_sched_node
*dst
)
1215 if (src
->compressed
)
1216 dom
= isl_set_copy(src
->hull
);
1218 dom
= isl_set_universe(isl_space_copy(src
->space
));
1219 if (dst
->compressed
)
1220 ran
= isl_set_copy(dst
->hull
);
1222 ran
= isl_set_universe(isl_space_copy(dst
->space
));
1224 return isl_map_from_domain_and_range(dom
, ran
);
1227 /* Intersect the domains of the nested relations in domain and range
1228 * of "tagged" with "map".
1230 static __isl_give isl_map
*map_intersect_domains(__isl_take isl_map
*tagged
,
1231 __isl_keep isl_map
*map
)
1235 tagged
= isl_map_zip(tagged
);
1236 set
= isl_map_wrap(isl_map_copy(map
));
1237 tagged
= isl_map_intersect_domain(tagged
, set
);
1238 tagged
= isl_map_zip(tagged
);
1242 /* Return a pointer to the node that lives in the domain space of "map",
1243 * an invalid node if there is no such node, or NULL in case of error.
1245 static struct isl_sched_node
*find_domain_node(isl_ctx
*ctx
,
1246 struct isl_sched_graph
*graph
, __isl_keep isl_map
*map
)
1248 struct isl_sched_node
*node
;
1251 space
= isl_space_domain(isl_map_get_space(map
));
1252 node
= graph_find_node(ctx
, graph
, space
);
1253 isl_space_free(space
);
1258 /* Return a pointer to the node that lives in the range space of "map",
1259 * an invalid node if there is no such node, or NULL in case of error.
1261 static struct isl_sched_node
*find_range_node(isl_ctx
*ctx
,
1262 struct isl_sched_graph
*graph
, __isl_keep isl_map
*map
)
1264 struct isl_sched_node
*node
;
1267 space
= isl_space_range(isl_map_get_space(map
));
1268 node
= graph_find_node(ctx
, graph
, space
);
1269 isl_space_free(space
);
1274 /* Refrain from adding a new edge based on "map".
1275 * Instead, just free the map.
1276 * "tagged" is either a copy of "map" with additional tags or NULL.
1278 static isl_stat
skip_edge(__isl_take isl_map
*map
, __isl_take isl_map
*tagged
)
1281 isl_map_free(tagged
);
1286 /* Add a new edge to the graph based on the given map
1287 * and add it to data->graph->edge_table[data->type].
1288 * If a dependence relation of a given type happens to be identical
1289 * to one of the dependence relations of a type that was added before,
1290 * then we don't create a new edge, but instead mark the original edge
1291 * as also representing a dependence of the current type.
1293 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1294 * may be specified as "tagged" dependence relations. That is, "map"
1295 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1296 * the dependence on iterations and a and b are tags.
1297 * edge->map is set to the relation containing the elements i -> j,
1298 * while edge->tagged_condition and edge->tagged_validity contain
1299 * the union of all the "map" relations
1300 * for which extract_edge is called that result in the same edge->map.
1302 * If the source or the destination node is compressed, then
1303 * intersect both "map" and "tagged" with the constraints that
1304 * were used to construct the compression.
1305 * This ensures that there are no schedule constraints defined
1306 * outside of these domains, while the scheduler no longer has
1307 * any control over those outside parts.
1309 static isl_stat
extract_edge(__isl_take isl_map
*map
, void *user
)
1312 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1313 struct isl_extract_edge_data
*data
= user
;
1314 struct isl_sched_graph
*graph
= data
->graph
;
1315 struct isl_sched_node
*src
, *dst
;
1316 struct isl_sched_edge
*edge
;
1317 isl_map
*tagged
= NULL
;
1319 if (data
->type
== isl_edge_condition
||
1320 data
->type
== isl_edge_conditional_validity
) {
1321 if (isl_map_can_zip(map
)) {
1322 tagged
= isl_map_copy(map
);
1323 map
= isl_set_unwrap(isl_map_domain(isl_map_zip(map
)));
1325 tagged
= insert_dummy_tags(isl_map_copy(map
));
1329 src
= find_domain_node(ctx
, graph
, map
);
1330 dst
= find_range_node(ctx
, graph
, map
);
1334 if (!is_node(graph
, src
) || !is_node(graph
, dst
))
1335 return skip_edge(map
, tagged
);
1337 if (src
->compressed
|| dst
->compressed
) {
1339 hull
= extract_hull(src
, dst
);
1341 tagged
= map_intersect_domains(tagged
, hull
);
1342 map
= isl_map_intersect(map
, hull
);
1345 empty
= isl_map_plain_is_empty(map
);
1349 return skip_edge(map
, tagged
);
1351 graph
->edge
[graph
->n_edge
].src
= src
;
1352 graph
->edge
[graph
->n_edge
].dst
= dst
;
1353 graph
->edge
[graph
->n_edge
].map
= map
;
1354 graph
->edge
[graph
->n_edge
].types
= 0;
1355 graph
->edge
[graph
->n_edge
].tagged_condition
= NULL
;
1356 graph
->edge
[graph
->n_edge
].tagged_validity
= NULL
;
1357 set_type(&graph
->edge
[graph
->n_edge
], data
->type
);
1358 if (data
->type
== isl_edge_condition
)
1359 graph
->edge
[graph
->n_edge
].tagged_condition
=
1360 isl_union_map_from_map(tagged
);
1361 if (data
->type
== isl_edge_conditional_validity
)
1362 graph
->edge
[graph
->n_edge
].tagged_validity
=
1363 isl_union_map_from_map(tagged
);
1365 edge
= graph_find_matching_edge(graph
, &graph
->edge
[graph
->n_edge
]);
1368 return isl_stat_error
;
1370 if (edge
== &graph
->edge
[graph
->n_edge
])
1371 return graph_edge_table_add(ctx
, graph
, data
->type
,
1372 &graph
->edge
[graph
->n_edge
++]);
1374 if (merge_edge(edge
, &graph
->edge
[graph
->n_edge
]) < 0)
1375 return isl_stat_error
;
1377 return graph_edge_table_add(ctx
, graph
, data
->type
, edge
);
1380 isl_map_free(tagged
);
1381 return isl_stat_error
;
1384 /* Initialize the schedule graph "graph" from the schedule constraints "sc".
1386 * The context is included in the domain before the nodes of
1387 * the graphs are extracted in order to be able to exploit
1388 * any possible additional equalities.
1389 * Note that this intersection is only performed locally here.
1391 static isl_stat
graph_init(struct isl_sched_graph
*graph
,
1392 __isl_keep isl_schedule_constraints
*sc
)
1395 isl_union_set
*domain
;
1397 struct isl_extract_edge_data data
;
1398 enum isl_edge_type i
;
1402 return isl_stat_error
;
1404 ctx
= isl_schedule_constraints_get_ctx(sc
);
1406 domain
= isl_schedule_constraints_get_domain(sc
);
1407 graph
->n
= isl_union_set_n_set(domain
);
1408 isl_union_set_free(domain
);
1410 if (graph_alloc(ctx
, graph
, graph
->n
,
1411 isl_schedule_constraints_n_map(sc
)) < 0)
1412 return isl_stat_error
;
1414 if (compute_max_row(graph
, sc
) < 0)
1415 return isl_stat_error
;
1416 graph
->root
= graph
;
1418 domain
= isl_schedule_constraints_get_domain(sc
);
1419 domain
= isl_union_set_intersect_params(domain
,
1420 isl_schedule_constraints_get_context(sc
));
1421 r
= isl_union_set_foreach_set(domain
, &extract_node
, graph
);
1422 isl_union_set_free(domain
);
1424 return isl_stat_error
;
1425 if (graph_init_table(ctx
, graph
) < 0)
1426 return isl_stat_error
;
1427 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
1428 c
= isl_schedule_constraints_get(sc
, i
);
1429 graph
->max_edge
[i
] = isl_union_map_n_map(c
);
1430 isl_union_map_free(c
);
1432 return isl_stat_error
;
1434 if (graph_init_edge_tables(ctx
, graph
) < 0)
1435 return isl_stat_error
;
1438 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
1442 c
= isl_schedule_constraints_get(sc
, i
);
1443 r
= isl_union_map_foreach_map(c
, &extract_edge
, &data
);
1444 isl_union_map_free(c
);
1446 return isl_stat_error
;
1452 /* Check whether there is any dependence from node[j] to node[i]
1453 * or from node[i] to node[j].
1455 static isl_bool
node_follows_weak(int i
, int j
, void *user
)
1458 struct isl_sched_graph
*graph
= user
;
1460 f
= graph_has_any_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1463 return graph_has_any_edge(graph
, &graph
->node
[i
], &graph
->node
[j
]);
1466 /* Check whether there is a (conditional) validity dependence from node[j]
1467 * to node[i], forcing node[i] to follow node[j].
1469 static isl_bool
node_follows_strong(int i
, int j
, void *user
)
1471 struct isl_sched_graph
*graph
= user
;
1473 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1476 /* Use Tarjan's algorithm for computing the strongly connected components
1477 * in the dependence graph only considering those edges defined by "follows".
1479 static isl_stat
detect_ccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
1480 isl_bool (*follows
)(int i
, int j
, void *user
))
1483 struct isl_tarjan_graph
*g
= NULL
;
1485 g
= isl_tarjan_graph_init(ctx
, graph
->n
, follows
, graph
);
1487 return isl_stat_error
;
1493 while (g
->order
[i
] != -1) {
1494 graph
->node
[g
->order
[i
]].scc
= graph
->scc
;
1502 isl_tarjan_graph_free(g
);
1507 /* Apply Tarjan's algorithm to detect the strongly connected components
1508 * in the dependence graph.
1509 * Only consider the (conditional) validity dependences and clear "weak".
1511 static isl_stat
detect_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1514 return detect_ccs(ctx
, graph
, &node_follows_strong
);
1517 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1518 * in the dependence graph.
1519 * Consider all dependences and set "weak".
1521 static isl_stat
detect_wccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1524 return detect_ccs(ctx
, graph
, &node_follows_weak
);
1527 static int cmp_scc(const void *a
, const void *b
, void *data
)
1529 struct isl_sched_graph
*graph
= data
;
1533 return graph
->node
[*i1
].scc
- graph
->node
[*i2
].scc
;
1536 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1538 static int sort_sccs(struct isl_sched_graph
*graph
)
1540 return isl_sort(graph
->sorted
, graph
->n
, sizeof(int), &cmp_scc
, graph
);
1543 /* Return a non-parametric set in the compressed space of "node" that is
1544 * bounded by the size in each direction
1546 * { [x] : -S_i <= x_i <= S_i }
1548 * If S_i is infinity in direction i, then there are no constraints
1549 * in that direction.
1551 * Cache the result in node->bounds.
1553 static __isl_give isl_basic_set
*get_size_bounds(struct isl_sched_node
*node
)
1556 isl_basic_set
*bounds
;
1560 return isl_basic_set_copy(node
->bounds
);
1562 if (node
->compressed
)
1563 space
= isl_multi_aff_get_domain_space(node
->decompress
);
1565 space
= isl_space_copy(node
->space
);
1566 space
= isl_space_drop_all_params(space
);
1567 bounds
= isl_basic_set_universe(space
);
1569 for (i
= 0; i
< node
->nvar
; ++i
) {
1572 size
= isl_multi_val_get_val(node
->sizes
, i
);
1574 return isl_basic_set_free(bounds
);
1575 if (!isl_val_is_int(size
)) {
1579 bounds
= isl_basic_set_upper_bound_val(bounds
, isl_dim_set
, i
,
1580 isl_val_copy(size
));
1581 bounds
= isl_basic_set_lower_bound_val(bounds
, isl_dim_set
, i
,
1585 node
->bounds
= isl_basic_set_copy(bounds
);
1589 /* Drop some constraints from "delta" that could be exploited
1590 * to construct loop coalescing schedules.
1591 * In particular, drop those constraint that bound the difference
1592 * to the size of the domain.
1593 * First project out the parameters to improve the effectiveness.
1595 static __isl_give isl_set
*drop_coalescing_constraints(
1596 __isl_take isl_set
*delta
, struct isl_sched_node
*node
)
1599 isl_basic_set
*bounds
;
1601 bounds
= get_size_bounds(node
);
1603 nparam
= isl_set_dim(delta
, isl_dim_param
);
1604 delta
= isl_set_project_out(delta
, isl_dim_param
, 0, nparam
);
1605 delta
= isl_set_remove_divs(delta
);
1606 delta
= isl_set_plain_gist_basic_set(delta
, bounds
);
1610 /* Given a dependence relation R from "node" to itself,
1611 * construct the set of coefficients of valid constraints for elements
1612 * in that dependence relation.
1613 * In particular, the result contains tuples of coefficients
1614 * c_0, c_n, c_x such that
1616 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1620 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1622 * We choose here to compute the dual of delta R.
1623 * Alternatively, we could have computed the dual of R, resulting
1624 * in a set of tuples c_0, c_n, c_x, c_y, and then
1625 * plugged in (c_0, c_n, c_x, -c_x).
1627 * If "need_param" is set, then the resulting coefficients effectively
1628 * include coefficients for the parameters c_n. Otherwise, they may
1629 * have been projected out already.
1630 * Since the constraints may be different for these two cases,
1631 * they are stored in separate caches.
1632 * In particular, if no parameter coefficients are required and
1633 * the schedule_treat_coalescing option is set, then the parameters
1634 * are projected out and some constraints that could be exploited
1635 * to construct coalescing schedules are removed before the dual
1638 * If "node" has been compressed, then the dependence relation
1639 * is also compressed before the set of coefficients is computed.
1641 static __isl_give isl_basic_set
*intra_coefficients(
1642 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1643 __isl_take isl_map
*map
, int need_param
)
1648 isl_basic_set
*coef
;
1649 isl_maybe_isl_basic_set m
;
1650 isl_map_to_basic_set
**hmap
= &graph
->intra_hmap
;
1656 ctx
= isl_map_get_ctx(map
);
1657 treat
= !need_param
&& isl_options_get_schedule_treat_coalescing(ctx
);
1659 hmap
= &graph
->intra_hmap_param
;
1660 m
= isl_map_to_basic_set_try_get(*hmap
, map
);
1661 if (m
.valid
< 0 || m
.valid
) {
1666 key
= isl_map_copy(map
);
1667 if (node
->compressed
) {
1668 map
= isl_map_preimage_domain_multi_aff(map
,
1669 isl_multi_aff_copy(node
->decompress
));
1670 map
= isl_map_preimage_range_multi_aff(map
,
1671 isl_multi_aff_copy(node
->decompress
));
1673 delta
= isl_map_deltas(map
);
1675 delta
= drop_coalescing_constraints(delta
, node
);
1676 delta
= isl_set_remove_divs(delta
);
1677 coef
= isl_set_coefficients(delta
);
1678 *hmap
= isl_map_to_basic_set_set(*hmap
, key
, isl_basic_set_copy(coef
));
1683 /* Given a dependence relation R, construct the set of coefficients
1684 * of valid constraints for elements in that dependence relation.
1685 * In particular, the result contains tuples of coefficients
1686 * c_0, c_n, c_x, c_y such that
1688 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1690 * If the source or destination nodes of "edge" have been compressed,
1691 * then the dependence relation is also compressed before
1692 * the set of coefficients is computed.
1694 static __isl_give isl_basic_set
*inter_coefficients(
1695 struct isl_sched_graph
*graph
, struct isl_sched_edge
*edge
,
1696 __isl_take isl_map
*map
)
1700 isl_basic_set
*coef
;
1701 isl_maybe_isl_basic_set m
;
1703 m
= isl_map_to_basic_set_try_get(graph
->inter_hmap
, map
);
1704 if (m
.valid
< 0 || m
.valid
) {
1709 key
= isl_map_copy(map
);
1710 if (edge
->src
->compressed
)
1711 map
= isl_map_preimage_domain_multi_aff(map
,
1712 isl_multi_aff_copy(edge
->src
->decompress
));
1713 if (edge
->dst
->compressed
)
1714 map
= isl_map_preimage_range_multi_aff(map
,
1715 isl_multi_aff_copy(edge
->dst
->decompress
));
1716 set
= isl_map_wrap(isl_map_remove_divs(map
));
1717 coef
= isl_set_coefficients(set
);
1718 graph
->inter_hmap
= isl_map_to_basic_set_set(graph
->inter_hmap
, key
,
1719 isl_basic_set_copy(coef
));
1724 /* Return the position of the coefficients of the variables in
1725 * the coefficients constraints "coef".
1727 * The space of "coef" is of the form
1729 * { coefficients[[cst, params] -> S] }
1731 * Return the position of S.
1733 static int coef_var_offset(__isl_keep isl_basic_set
*coef
)
1738 space
= isl_space_unwrap(isl_basic_set_get_space(coef
));
1739 offset
= isl_space_dim(space
, isl_dim_in
);
1740 isl_space_free(space
);
1745 /* Return the offset of the coefficient of the constant term of "node"
1748 * Within each node, the coefficients have the following order:
1749 * - positive and negative parts of c_i_x
1750 * - c_i_n (if parametric)
1753 static int node_cst_coef_offset(struct isl_sched_node
*node
)
1755 return node
->start
+ 2 * node
->nvar
+ node
->nparam
;
1758 /* Return the offset of the coefficients of the parameters of "node"
1761 * Within each node, the coefficients have the following order:
1762 * - positive and negative parts of c_i_x
1763 * - c_i_n (if parametric)
1766 static int node_par_coef_offset(struct isl_sched_node
*node
)
1768 return node
->start
+ 2 * node
->nvar
;
1771 /* Return the offset of the coefficients of the variables of "node"
1774 * Within each node, the coefficients have the following order:
1775 * - positive and negative parts of c_i_x
1776 * - c_i_n (if parametric)
1779 static int node_var_coef_offset(struct isl_sched_node
*node
)
1784 /* Return the position of the pair of variables encoding
1785 * coefficient "i" of "node".
1787 * The order of these variable pairs is the opposite of
1788 * that of the coefficients, with 2 variables per coefficient.
1790 static int node_var_coef_pos(struct isl_sched_node
*node
, int i
)
1792 return node_var_coef_offset(node
) + 2 * (node
->nvar
- 1 - i
);
1795 /* Construct an isl_dim_map for mapping constraints on coefficients
1796 * for "node" to the corresponding positions in graph->lp.
1797 * "offset" is the offset of the coefficients for the variables
1798 * in the input constraints.
1799 * "s" is the sign of the mapping.
1801 * The input constraints are given in terms of the coefficients
1802 * (c_0, c_x) or (c_0, c_n, c_x).
1803 * The mapping produced by this function essentially plugs in
1804 * (0, c_i_x^+ - c_i_x^-) if s = 1 and
1805 * (0, -c_i_x^+ + c_i_x^-) if s = -1 or
1806 * (0, 0, c_i_x^+ - c_i_x^-) if s = 1 and
1807 * (0, 0, -c_i_x^+ + c_i_x^-) if s = -1.
1808 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1809 * Furthermore, the order of these pairs is the opposite of that
1810 * of the corresponding coefficients.
1812 * The caller can extend the mapping to also map the other coefficients
1813 * (and therefore not plug in 0).
1815 static __isl_give isl_dim_map
*intra_dim_map(isl_ctx
*ctx
,
1816 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1821 isl_dim_map
*dim_map
;
1823 if (!node
|| !graph
->lp
)
1826 total
= isl_basic_set_total_dim(graph
->lp
);
1827 pos
= node_var_coef_pos(node
, 0);
1828 dim_map
= isl_dim_map_alloc(ctx
, total
);
1829 isl_dim_map_range(dim_map
, pos
, -2, offset
, 1, node
->nvar
, -s
);
1830 isl_dim_map_range(dim_map
, pos
+ 1, -2, offset
, 1, node
->nvar
, s
);
1835 /* Construct an isl_dim_map for mapping constraints on coefficients
1836 * for "src" (node i) and "dst" (node j) to the corresponding positions
1838 * "offset" is the offset of the coefficients for the variables of "src"
1839 * in the input constraints.
1840 * "s" is the sign of the mapping.
1842 * The input constraints are given in terms of the coefficients
1843 * (c_0, c_n, c_x, c_y).
1844 * The mapping produced by this function essentially plugs in
1845 * (c_j_0 - c_i_0, c_j_n - c_i_n,
1846 * -(c_i_x^+ - c_i_x^-), c_j_x^+ - c_j_x^-) if s = 1 and
1847 * (-c_j_0 + c_i_0, -c_j_n + c_i_n,
1848 * c_i_x^+ - c_i_x^-, -(c_j_x^+ - c_j_x^-)) if s = -1.
1849 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1850 * Furthermore, the order of these pairs is the opposite of that
1851 * of the corresponding coefficients.
1853 * The caller can further extend the mapping.
1855 static __isl_give isl_dim_map
*inter_dim_map(isl_ctx
*ctx
,
1856 struct isl_sched_graph
*graph
, struct isl_sched_node
*src
,
1857 struct isl_sched_node
*dst
, int offset
, int s
)
1861 isl_dim_map
*dim_map
;
1863 if (!src
|| !dst
|| !graph
->lp
)
1866 total
= isl_basic_set_total_dim(graph
->lp
);
1867 dim_map
= isl_dim_map_alloc(ctx
, total
);
1869 pos
= node_cst_coef_offset(dst
);
1870 isl_dim_map_range(dim_map
, pos
, 0, 0, 0, 1, s
);
1871 pos
= node_par_coef_offset(dst
);
1872 isl_dim_map_range(dim_map
, pos
, 1, 1, 1, dst
->nparam
, s
);
1873 pos
= node_var_coef_pos(dst
, 0);
1874 isl_dim_map_range(dim_map
, pos
, -2, offset
+ src
->nvar
, 1,
1876 isl_dim_map_range(dim_map
, pos
+ 1, -2, offset
+ src
->nvar
, 1,
1879 pos
= node_cst_coef_offset(src
);
1880 isl_dim_map_range(dim_map
, pos
, 0, 0, 0, 1, -s
);
1881 pos
= node_par_coef_offset(src
);
1882 isl_dim_map_range(dim_map
, pos
, 1, 1, 1, src
->nparam
, -s
);
1883 pos
= node_var_coef_pos(src
, 0);
1884 isl_dim_map_range(dim_map
, pos
, -2, offset
, 1, src
->nvar
, s
);
1885 isl_dim_map_range(dim_map
, pos
+ 1, -2, offset
, 1, src
->nvar
, -s
);
1890 /* Add the constraints from "src" to "dst" using "dim_map",
1891 * after making sure there is enough room in "dst" for the extra constraints.
1893 static __isl_give isl_basic_set
*add_constraints_dim_map(
1894 __isl_take isl_basic_set
*dst
, __isl_take isl_basic_set
*src
,
1895 __isl_take isl_dim_map
*dim_map
)
1899 n_eq
= isl_basic_set_n_equality(src
);
1900 n_ineq
= isl_basic_set_n_inequality(src
);
1901 dst
= isl_basic_set_extend_constraints(dst
, n_eq
, n_ineq
);
1902 dst
= isl_basic_set_add_constraints_dim_map(dst
, src
, dim_map
);
1906 /* Add constraints to graph->lp that force validity for the given
1907 * dependence from a node i to itself.
1908 * That is, add constraints that enforce
1910 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1911 * = c_i_x (y - x) >= 0
1913 * for each (x,y) in R.
1914 * We obtain general constraints on coefficients (c_0, c_x)
1915 * of valid constraints for (y - x) and then plug in (0, c_i_x^+ - c_i_x^-),
1916 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1917 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1918 * Note that the result of intra_coefficients may also contain
1919 * parameter coefficients c_n, in which case 0 is plugged in for them as well.
1921 static isl_stat
add_intra_validity_constraints(struct isl_sched_graph
*graph
,
1922 struct isl_sched_edge
*edge
)
1925 isl_map
*map
= isl_map_copy(edge
->map
);
1926 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1927 isl_dim_map
*dim_map
;
1928 isl_basic_set
*coef
;
1929 struct isl_sched_node
*node
= edge
->src
;
1931 coef
= intra_coefficients(graph
, node
, map
, 0);
1933 offset
= coef_var_offset(coef
);
1936 return isl_stat_error
;
1938 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, 1);
1939 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
1944 /* Add constraints to graph->lp that force validity for the given
1945 * dependence from node i to node j.
1946 * That is, add constraints that enforce
1948 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1950 * for each (x,y) in R.
1951 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1952 * of valid constraints for R and then plug in
1953 * (c_j_0 - c_i_0, c_j_n - c_i_n, -(c_i_x^+ - c_i_x^-), c_j_x^+ - c_j_x^-),
1954 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1955 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1957 static isl_stat
add_inter_validity_constraints(struct isl_sched_graph
*graph
,
1958 struct isl_sched_edge
*edge
)
1963 isl_dim_map
*dim_map
;
1964 isl_basic_set
*coef
;
1965 struct isl_sched_node
*src
= edge
->src
;
1966 struct isl_sched_node
*dst
= edge
->dst
;
1969 return isl_stat_error
;
1971 map
= isl_map_copy(edge
->map
);
1972 ctx
= isl_map_get_ctx(map
);
1973 coef
= inter_coefficients(graph
, edge
, map
);
1975 offset
= coef_var_offset(coef
);
1978 return isl_stat_error
;
1980 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, 1);
1982 edge
->start
= graph
->lp
->n_ineq
;
1983 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
1985 return isl_stat_error
;
1986 edge
->end
= graph
->lp
->n_ineq
;
1991 /* Add constraints to graph->lp that bound the dependence distance for the given
1992 * dependence from a node i to itself.
1993 * If s = 1, we add the constraint
1995 * c_i_x (y - x) <= m_0 + m_n n
1999 * -c_i_x (y - x) + m_0 + m_n n >= 0
2001 * for each (x,y) in R.
2002 * If s = -1, we add the constraint
2004 * -c_i_x (y - x) <= m_0 + m_n n
2008 * c_i_x (y - x) + m_0 + m_n n >= 0
2010 * for each (x,y) in R.
2011 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2012 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
2013 * with each coefficient (except m_0) represented as a pair of non-negative
2017 * If "local" is set, then we add constraints
2019 * c_i_x (y - x) <= 0
2023 * -c_i_x (y - x) <= 0
2025 * instead, forcing the dependence distance to be (less than or) equal to 0.
2026 * That is, we plug in (0, 0, -s * c_i_x),
2027 * intra_coefficients is not required to have c_n in its result when
2028 * "local" is set. If they are missing, then (0, -s * c_i_x) is plugged in.
2029 * Note that dependences marked local are treated as validity constraints
2030 * by add_all_validity_constraints and therefore also have
2031 * their distances bounded by 0 from below.
2033 static isl_stat
add_intra_proximity_constraints(struct isl_sched_graph
*graph
,
2034 struct isl_sched_edge
*edge
, int s
, int local
)
2038 isl_map
*map
= isl_map_copy(edge
->map
);
2039 isl_ctx
*ctx
= isl_map_get_ctx(map
);
2040 isl_dim_map
*dim_map
;
2041 isl_basic_set
*coef
;
2042 struct isl_sched_node
*node
= edge
->src
;
2044 coef
= intra_coefficients(graph
, node
, map
, !local
);
2046 offset
= coef_var_offset(coef
);
2049 return isl_stat_error
;
2051 nparam
= isl_space_dim(node
->space
, isl_dim_param
);
2052 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, -s
);
2055 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
2056 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
2057 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
2059 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
2064 /* Add constraints to graph->lp that bound the dependence distance for the given
2065 * dependence from node i to node j.
2066 * If s = 1, we add the constraint
2068 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
2073 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
2076 * for each (x,y) in R.
2077 * If s = -1, we add the constraint
2079 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
2084 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
2087 * for each (x,y) in R.
2088 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
2089 * of valid constraints for R and then plug in
2090 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
2091 * s*c_i_x, -s*c_j_x)
2092 * with each coefficient (except m_0, c_*_0 and c_*_n)
2093 * represented as a pair of non-negative coefficients.
2096 * If "local" is set (and s = 1), then we add constraints
2098 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
2102 * -((c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x)) >= 0
2104 * instead, forcing the dependence distance to be (less than or) equal to 0.
2105 * That is, we plug in
2106 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, s*c_i_x, -s*c_j_x).
2107 * Note that dependences marked local are treated as validity constraints
2108 * by add_all_validity_constraints and therefore also have
2109 * their distances bounded by 0 from below.
2111 static isl_stat
add_inter_proximity_constraints(struct isl_sched_graph
*graph
,
2112 struct isl_sched_edge
*edge
, int s
, int local
)
2116 isl_map
*map
= isl_map_copy(edge
->map
);
2117 isl_ctx
*ctx
= isl_map_get_ctx(map
);
2118 isl_dim_map
*dim_map
;
2119 isl_basic_set
*coef
;
2120 struct isl_sched_node
*src
= edge
->src
;
2121 struct isl_sched_node
*dst
= edge
->dst
;
2123 coef
= inter_coefficients(graph
, edge
, map
);
2125 offset
= coef_var_offset(coef
);
2128 return isl_stat_error
;
2130 nparam
= isl_space_dim(src
->space
, isl_dim_param
);
2131 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, -s
);
2134 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
2135 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
2136 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
2139 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
2144 /* Should the distance over "edge" be forced to zero?
2145 * That is, is it marked as a local edge?
2146 * If "use_coincidence" is set, then coincidence edges are treated
2149 static int force_zero(struct isl_sched_edge
*edge
, int use_coincidence
)
2151 return is_local(edge
) || (use_coincidence
&& is_coincidence(edge
));
2154 /* Add all validity constraints to graph->lp.
2156 * An edge that is forced to be local needs to have its dependence
2157 * distances equal to zero. We take care of bounding them by 0 from below
2158 * here. add_all_proximity_constraints takes care of bounding them by 0
2161 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2162 * Otherwise, we ignore them.
2164 static int add_all_validity_constraints(struct isl_sched_graph
*graph
,
2165 int use_coincidence
)
2169 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2170 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2173 zero
= force_zero(edge
, use_coincidence
);
2174 if (!is_validity(edge
) && !zero
)
2176 if (edge
->src
!= edge
->dst
)
2178 if (add_intra_validity_constraints(graph
, edge
) < 0)
2182 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2183 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2186 zero
= force_zero(edge
, use_coincidence
);
2187 if (!is_validity(edge
) && !zero
)
2189 if (edge
->src
== edge
->dst
)
2191 if (add_inter_validity_constraints(graph
, edge
) < 0)
2198 /* Add constraints to graph->lp that bound the dependence distance
2199 * for all dependence relations.
2200 * If a given proximity dependence is identical to a validity
2201 * dependence, then the dependence distance is already bounded
2202 * from below (by zero), so we only need to bound the distance
2203 * from above. (This includes the case of "local" dependences
2204 * which are treated as validity dependence by add_all_validity_constraints.)
2205 * Otherwise, we need to bound the distance both from above and from below.
2207 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2208 * Otherwise, we ignore them.
2210 static int add_all_proximity_constraints(struct isl_sched_graph
*graph
,
2211 int use_coincidence
)
2215 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2216 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2219 zero
= force_zero(edge
, use_coincidence
);
2220 if (!is_proximity(edge
) && !zero
)
2222 if (edge
->src
== edge
->dst
&&
2223 add_intra_proximity_constraints(graph
, edge
, 1, zero
) < 0)
2225 if (edge
->src
!= edge
->dst
&&
2226 add_inter_proximity_constraints(graph
, edge
, 1, zero
) < 0)
2228 if (is_validity(edge
) || zero
)
2230 if (edge
->src
== edge
->dst
&&
2231 add_intra_proximity_constraints(graph
, edge
, -1, 0) < 0)
2233 if (edge
->src
!= edge
->dst
&&
2234 add_inter_proximity_constraints(graph
, edge
, -1, 0) < 0)
2241 /* Normalize the rows of "indep" such that all rows are lexicographically
2242 * positive and such that each row contains as many final zeros as possible,
2243 * given the choice for the previous rows.
2244 * Do this by performing elementary row operations.
2246 static __isl_give isl_mat
*normalize_independent(__isl_take isl_mat
*indep
)
2248 indep
= isl_mat_reverse_gauss(indep
);
2249 indep
= isl_mat_lexnonneg_rows(indep
);
2253 /* Extract the linear part of the current schedule for node "node".
2255 static __isl_give isl_mat
*extract_linear_schedule(struct isl_sched_node
*node
)
2257 int n_row
= isl_mat_rows(node
->sched
);
2259 return isl_mat_sub_alloc(node
->sched
, 0, n_row
,
2260 1 + node
->nparam
, node
->nvar
);
2263 /* Compute a basis for the rows in the linear part of the schedule
2264 * and extend this basis to a full basis. The remaining rows
2265 * can then be used to force linear independence from the rows
2268 * In particular, given the schedule rows S, we compute
2273 * with H the Hermite normal form of S. That is, all but the
2274 * first rank columns of H are zero and so each row in S is
2275 * a linear combination of the first rank rows of Q.
2276 * The matrix Q can be used as a variable transformation
2277 * that isolates the directions of S in the first rank rows.
2278 * Transposing S U = H yields
2282 * with all but the first rank rows of H^T zero.
2283 * The last rows of U^T are therefore linear combinations
2284 * of schedule coefficients that are all zero on schedule
2285 * coefficients that are linearly dependent on the rows of S.
2286 * At least one of these combinations is non-zero on
2287 * linearly independent schedule coefficients.
2288 * The rows are normalized to involve as few of the last
2289 * coefficients as possible and to have a positive initial value.
2291 static int node_update_vmap(struct isl_sched_node
*node
)
2295 H
= extract_linear_schedule(node
);
2297 H
= isl_mat_left_hermite(H
, 0, &U
, &Q
);
2298 isl_mat_free(node
->indep
);
2299 isl_mat_free(node
->vmap
);
2301 node
->indep
= isl_mat_transpose(U
);
2302 node
->rank
= isl_mat_initial_non_zero_cols(H
);
2303 node
->indep
= isl_mat_drop_rows(node
->indep
, 0, node
->rank
);
2304 node
->indep
= normalize_independent(node
->indep
);
2307 if (!node
->indep
|| !node
->vmap
|| node
->rank
< 0)
2312 /* Is "edge" marked as a validity or a conditional validity edge?
2314 static int is_any_validity(struct isl_sched_edge
*edge
)
2316 return is_validity(edge
) || is_conditional_validity(edge
);
2319 /* How many times should we count the constraints in "edge"?
2321 * We count as follows
2322 * validity -> 1 (>= 0)
2323 * validity+proximity -> 2 (>= 0 and upper bound)
2324 * proximity -> 2 (lower and upper bound)
2325 * local(+any) -> 2 (>= 0 and <= 0)
2327 * If an edge is only marked conditional_validity then it counts
2328 * as zero since it is only checked afterwards.
2330 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2331 * Otherwise, we ignore them.
2333 static int edge_multiplicity(struct isl_sched_edge
*edge
, int use_coincidence
)
2335 if (is_proximity(edge
) || force_zero(edge
, use_coincidence
))
2337 if (is_validity(edge
))
2342 /* How many times should the constraints in "edge" be counted
2343 * as a parametric intra-node constraint?
2345 * Only proximity edges that are not forced zero need
2346 * coefficient constraints that include coefficients for parameters.
2347 * If the edge is also a validity edge, then only
2348 * an upper bound is introduced. Otherwise, both lower and upper bounds
2351 static int parametric_intra_edge_multiplicity(struct isl_sched_edge
*edge
,
2352 int use_coincidence
)
2354 if (edge
->src
!= edge
->dst
)
2356 if (!is_proximity(edge
))
2358 if (force_zero(edge
, use_coincidence
))
2360 if (is_validity(edge
))
2366 /* Add "f" times the number of equality and inequality constraints of "bset"
2367 * to "n_eq" and "n_ineq" and free "bset".
2369 static isl_stat
update_count(__isl_take isl_basic_set
*bset
,
2370 int f
, int *n_eq
, int *n_ineq
)
2373 return isl_stat_error
;
2375 *n_eq
+= isl_basic_set_n_equality(bset
);
2376 *n_ineq
+= isl_basic_set_n_inequality(bset
);
2377 isl_basic_set_free(bset
);
2382 /* Count the number of equality and inequality constraints
2383 * that will be added for the given map.
2385 * The edges that require parameter coefficients are counted separately.
2387 * "use_coincidence" is set if we should take into account coincidence edges.
2389 static isl_stat
count_map_constraints(struct isl_sched_graph
*graph
,
2390 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
,
2391 int *n_eq
, int *n_ineq
, int use_coincidence
)
2394 isl_basic_set
*coef
;
2395 int f
= edge_multiplicity(edge
, use_coincidence
);
2396 int fp
= parametric_intra_edge_multiplicity(edge
, use_coincidence
);
2403 if (edge
->src
!= edge
->dst
) {
2404 coef
= inter_coefficients(graph
, edge
, map
);
2405 return update_count(coef
, f
, n_eq
, n_ineq
);
2409 copy
= isl_map_copy(map
);
2410 coef
= intra_coefficients(graph
, edge
->src
, copy
, 1);
2411 if (update_count(coef
, fp
, n_eq
, n_ineq
) < 0)
2416 copy
= isl_map_copy(map
);
2417 coef
= intra_coefficients(graph
, edge
->src
, copy
, 0);
2418 if (update_count(coef
, f
- fp
, n_eq
, n_ineq
) < 0)
2426 return isl_stat_error
;
2429 /* Count the number of equality and inequality constraints
2430 * that will be added to the main lp problem.
2431 * We count as follows
2432 * validity -> 1 (>= 0)
2433 * validity+proximity -> 2 (>= 0 and upper bound)
2434 * proximity -> 2 (lower and upper bound)
2435 * local(+any) -> 2 (>= 0 and <= 0)
2437 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2438 * Otherwise, we ignore them.
2440 static int count_constraints(struct isl_sched_graph
*graph
,
2441 int *n_eq
, int *n_ineq
, int use_coincidence
)
2445 *n_eq
= *n_ineq
= 0;
2446 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2447 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2448 isl_map
*map
= isl_map_copy(edge
->map
);
2450 if (count_map_constraints(graph
, edge
, map
, n_eq
, n_ineq
,
2451 use_coincidence
) < 0)
2458 /* Count the number of constraints that will be added by
2459 * add_bound_constant_constraints to bound the values of the constant terms
2460 * and increment *n_eq and *n_ineq accordingly.
2462 * In practice, add_bound_constant_constraints only adds inequalities.
2464 static isl_stat
count_bound_constant_constraints(isl_ctx
*ctx
,
2465 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
2467 if (isl_options_get_schedule_max_constant_term(ctx
) == -1)
2470 *n_ineq
+= graph
->n
;
2475 /* Add constraints to bound the values of the constant terms in the schedule,
2476 * if requested by the user.
2478 * The maximal value of the constant terms is defined by the option
2479 * "schedule_max_constant_term".
2481 static isl_stat
add_bound_constant_constraints(isl_ctx
*ctx
,
2482 struct isl_sched_graph
*graph
)
2488 max
= isl_options_get_schedule_max_constant_term(ctx
);
2492 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2494 for (i
= 0; i
< graph
->n
; ++i
) {
2495 struct isl_sched_node
*node
= &graph
->node
[i
];
2498 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2500 return isl_stat_error
;
2501 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2502 pos
= node_cst_coef_offset(node
);
2503 isl_int_set_si(graph
->lp
->ineq
[k
][1 + pos
], -1);
2504 isl_int_set_si(graph
->lp
->ineq
[k
][0], max
);
2510 /* Count the number of constraints that will be added by
2511 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
2514 * In practice, add_bound_coefficient_constraints only adds inequalities.
2516 static int count_bound_coefficient_constraints(isl_ctx
*ctx
,
2517 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
2521 if (isl_options_get_schedule_max_coefficient(ctx
) == -1 &&
2522 !isl_options_get_schedule_treat_coalescing(ctx
))
2525 for (i
= 0; i
< graph
->n
; ++i
)
2526 *n_ineq
+= graph
->node
[i
].nparam
+ 2 * graph
->node
[i
].nvar
;
2531 /* Add constraints to graph->lp that bound the values of
2532 * the parameter schedule coefficients of "node" to "max" and
2533 * the variable schedule coefficients to the corresponding entry
2535 * In either case, a negative value means that no bound needs to be imposed.
2537 * For parameter coefficients, this amounts to adding a constraint
2545 * The variables coefficients are, however, not represented directly.
2546 * Instead, the variable coefficients c_x are written as differences
2547 * c_x = c_x^+ - c_x^-.
2550 * -max_i <= c_x_i <= max_i
2554 * -max_i <= c_x_i^+ - c_x_i^- <= max_i
2558 * -(c_x_i^+ - c_x_i^-) + max_i >= 0
2559 * c_x_i^+ - c_x_i^- + max_i >= 0
2561 static isl_stat
node_add_coefficient_constraints(isl_ctx
*ctx
,
2562 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
, int max
)
2568 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2570 for (j
= 0; j
< node
->nparam
; ++j
) {
2576 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2578 return isl_stat_error
;
2579 dim
= 1 + node_par_coef_offset(node
) + j
;
2580 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2581 isl_int_set_si(graph
->lp
->ineq
[k
][dim
], -1);
2582 isl_int_set_si(graph
->lp
->ineq
[k
][0], max
);
2585 ineq
= isl_vec_alloc(ctx
, 1 + total
);
2586 ineq
= isl_vec_clr(ineq
);
2588 return isl_stat_error
;
2589 for (i
= 0; i
< node
->nvar
; ++i
) {
2590 int pos
= 1 + node_var_coef_pos(node
, i
);
2592 if (isl_int_is_neg(node
->max
->el
[i
]))
2595 isl_int_set_si(ineq
->el
[pos
], 1);
2596 isl_int_set_si(ineq
->el
[pos
+ 1], -1);
2597 isl_int_set(ineq
->el
[0], node
->max
->el
[i
]);
2599 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2602 isl_seq_cpy(graph
->lp
->ineq
[k
], ineq
->el
, 1 + total
);
2604 isl_seq_neg(ineq
->el
+ pos
, ineq
->el
+ pos
, 2);
2605 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2608 isl_seq_cpy(graph
->lp
->ineq
[k
], ineq
->el
, 1 + total
);
2610 isl_seq_clr(ineq
->el
+ pos
, 2);
2617 return isl_stat_error
;
2620 /* Add constraints that bound the values of the variable and parameter
2621 * coefficients of the schedule.
2623 * The maximal value of the coefficients is defined by the option
2624 * 'schedule_max_coefficient' and the entries in node->max.
2625 * These latter entries are only set if either the schedule_max_coefficient
2626 * option or the schedule_treat_coalescing option is set.
2628 static isl_stat
add_bound_coefficient_constraints(isl_ctx
*ctx
,
2629 struct isl_sched_graph
*graph
)
2634 max
= isl_options_get_schedule_max_coefficient(ctx
);
2636 if (max
== -1 && !isl_options_get_schedule_treat_coalescing(ctx
))
2639 for (i
= 0; i
< graph
->n
; ++i
) {
2640 struct isl_sched_node
*node
= &graph
->node
[i
];
2642 if (node_add_coefficient_constraints(ctx
, graph
, node
, max
) < 0)
2643 return isl_stat_error
;
2649 /* Add a constraint to graph->lp that equates the value at position
2650 * "sum_pos" to the sum of the "n" values starting at "first".
2652 static isl_stat
add_sum_constraint(struct isl_sched_graph
*graph
,
2653 int sum_pos
, int first
, int n
)
2658 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2660 k
= isl_basic_set_alloc_equality(graph
->lp
);
2662 return isl_stat_error
;
2663 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2664 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2665 for (i
= 0; i
< n
; ++i
)
2666 isl_int_set_si(graph
->lp
->eq
[k
][1 + first
+ i
], 1);
2671 /* Add a constraint to graph->lp that equates the value at position
2672 * "sum_pos" to the sum of the parameter coefficients of all nodes.
2674 static isl_stat
add_param_sum_constraint(struct isl_sched_graph
*graph
,
2680 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2682 k
= isl_basic_set_alloc_equality(graph
->lp
);
2684 return isl_stat_error
;
2685 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2686 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2687 for (i
= 0; i
< graph
->n
; ++i
) {
2688 int pos
= 1 + node_par_coef_offset(&graph
->node
[i
]);
2690 for (j
= 0; j
< graph
->node
[i
].nparam
; ++j
)
2691 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2697 /* Add a constraint to graph->lp that equates the value at position
2698 * "sum_pos" to the sum of the variable coefficients of all nodes.
2700 static isl_stat
add_var_sum_constraint(struct isl_sched_graph
*graph
,
2706 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2708 k
= isl_basic_set_alloc_equality(graph
->lp
);
2710 return isl_stat_error
;
2711 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2712 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2713 for (i
= 0; i
< graph
->n
; ++i
) {
2714 struct isl_sched_node
*node
= &graph
->node
[i
];
2715 int pos
= 1 + node_var_coef_offset(node
);
2717 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
2718 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2724 /* Construct an ILP problem for finding schedule coefficients
2725 * that result in non-negative, but small dependence distances
2726 * over all dependences.
2727 * In particular, the dependence distances over proximity edges
2728 * are bounded by m_0 + m_n n and we compute schedule coefficients
2729 * with small values (preferably zero) of m_n and m_0.
2731 * All variables of the ILP are non-negative. The actual coefficients
2732 * may be negative, so each coefficient is represented as the difference
2733 * of two non-negative variables. The negative part always appears
2734 * immediately before the positive part.
2735 * Other than that, the variables have the following order
2737 * - sum of positive and negative parts of m_n coefficients
2739 * - sum of all c_n coefficients
2740 * (unconstrained when computing non-parametric schedules)
2741 * - sum of positive and negative parts of all c_x coefficients
2742 * - positive and negative parts of m_n coefficients
2744 * - positive and negative parts of c_i_x, in opposite order
2745 * - c_i_n (if parametric)
2748 * The constraints are those from the edges plus two or three equalities
2749 * to express the sums.
2751 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2752 * Otherwise, we ignore them.
2754 static isl_stat
setup_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
2755 int use_coincidence
)
2765 parametric
= ctx
->opt
->schedule_parametric
;
2766 nparam
= isl_space_dim(graph
->node
[0].space
, isl_dim_param
);
2768 total
= param_pos
+ 2 * nparam
;
2769 for (i
= 0; i
< graph
->n
; ++i
) {
2770 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
2771 if (node_update_vmap(node
) < 0)
2772 return isl_stat_error
;
2773 node
->start
= total
;
2774 total
+= 1 + node
->nparam
+ 2 * node
->nvar
;
2777 if (count_constraints(graph
, &n_eq
, &n_ineq
, use_coincidence
) < 0)
2778 return isl_stat_error
;
2779 if (count_bound_constant_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2780 return isl_stat_error
;
2781 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2782 return isl_stat_error
;
2784 space
= isl_space_set_alloc(ctx
, 0, total
);
2785 isl_basic_set_free(graph
->lp
);
2786 n_eq
+= 2 + parametric
;
2788 graph
->lp
= isl_basic_set_alloc_space(space
, 0, n_eq
, n_ineq
);
2790 if (add_sum_constraint(graph
, 0, param_pos
, 2 * nparam
) < 0)
2791 return isl_stat_error
;
2792 if (parametric
&& add_param_sum_constraint(graph
, 2) < 0)
2793 return isl_stat_error
;
2794 if (add_var_sum_constraint(graph
, 3) < 0)
2795 return isl_stat_error
;
2796 if (add_bound_constant_constraints(ctx
, graph
) < 0)
2797 return isl_stat_error
;
2798 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
2799 return isl_stat_error
;
2800 if (add_all_validity_constraints(graph
, use_coincidence
) < 0)
2801 return isl_stat_error
;
2802 if (add_all_proximity_constraints(graph
, use_coincidence
) < 0)
2803 return isl_stat_error
;
2808 /* Analyze the conflicting constraint found by
2809 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2810 * constraint of one of the edges between distinct nodes, living, moreover
2811 * in distinct SCCs, then record the source and sink SCC as this may
2812 * be a good place to cut between SCCs.
2814 static int check_conflict(int con
, void *user
)
2817 struct isl_sched_graph
*graph
= user
;
2819 if (graph
->src_scc
>= 0)
2822 con
-= graph
->lp
->n_eq
;
2824 if (con
>= graph
->lp
->n_ineq
)
2827 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2828 if (!is_validity(&graph
->edge
[i
]))
2830 if (graph
->edge
[i
].src
== graph
->edge
[i
].dst
)
2832 if (graph
->edge
[i
].src
->scc
== graph
->edge
[i
].dst
->scc
)
2834 if (graph
->edge
[i
].start
> con
)
2836 if (graph
->edge
[i
].end
<= con
)
2838 graph
->src_scc
= graph
->edge
[i
].src
->scc
;
2839 graph
->dst_scc
= graph
->edge
[i
].dst
->scc
;
2845 /* Check whether the next schedule row of the given node needs to be
2846 * non-trivial. Lower-dimensional domains may have some trivial rows,
2847 * but as soon as the number of remaining required non-trivial rows
2848 * is as large as the number or remaining rows to be computed,
2849 * all remaining rows need to be non-trivial.
2851 static int needs_row(struct isl_sched_graph
*graph
, struct isl_sched_node
*node
)
2853 return node
->nvar
- node
->rank
>= graph
->maxvar
- graph
->n_row
;
2856 /* Construct a non-triviality region with triviality directions
2857 * corresponding to the rows of "indep".
2858 * The rows of "indep" are expressed in terms of the schedule coefficients c_i,
2859 * while the triviality directions are expressed in terms of
2860 * pairs of non-negative variables c^+_i - c^-_i, with c^-_i appearing
2861 * before c^+_i. Furthermore,
2862 * the pairs of non-negative variables representing the coefficients
2863 * are stored in the opposite order.
2865 static __isl_give isl_mat
*construct_trivial(__isl_keep isl_mat
*indep
)
2874 ctx
= isl_mat_get_ctx(indep
);
2875 n
= isl_mat_rows(indep
);
2876 n_var
= isl_mat_cols(indep
);
2877 mat
= isl_mat_alloc(ctx
, n
, 2 * n_var
);
2880 for (i
= 0; i
< n
; ++i
) {
2881 for (j
= 0; j
< n_var
; ++j
) {
2882 int nj
= n_var
- 1 - j
;
2883 isl_int_neg(mat
->row
[i
][2 * nj
], indep
->row
[i
][j
]);
2884 isl_int_set(mat
->row
[i
][2 * nj
+ 1], indep
->row
[i
][j
]);
2891 /* Solve the ILP problem constructed in setup_lp.
2892 * For each node such that all the remaining rows of its schedule
2893 * need to be non-trivial, we construct a non-triviality region.
2894 * This region imposes that the next row is independent of previous rows.
2895 * In particular, the non-triviality region enforces that at least
2896 * one of the linear combinations in the rows of node->indep is non-zero.
2898 static __isl_give isl_vec
*solve_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2904 for (i
= 0; i
< graph
->n
; ++i
) {
2905 struct isl_sched_node
*node
= &graph
->node
[i
];
2908 graph
->region
[i
].pos
= node_var_coef_offset(node
);
2909 if (needs_row(graph
, node
))
2910 trivial
= construct_trivial(node
->indep
);
2912 trivial
= isl_mat_zero(ctx
, 0, 0);
2913 graph
->region
[i
].trivial
= trivial
;
2915 lp
= isl_basic_set_copy(graph
->lp
);
2916 sol
= isl_tab_basic_set_non_trivial_lexmin(lp
, 2, graph
->n
,
2917 graph
->region
, &check_conflict
, graph
);
2918 for (i
= 0; i
< graph
->n
; ++i
)
2919 isl_mat_free(graph
->region
[i
].trivial
);
2923 /* Extract the coefficients for the variables of "node" from "sol".
2925 * Each schedule coefficient c_i_x is represented as the difference
2926 * between two non-negative variables c_i_x^+ - c_i_x^-.
2927 * The c_i_x^- appear before their c_i_x^+ counterpart.
2928 * Furthermore, the order of these pairs is the opposite of that
2929 * of the corresponding coefficients.
2931 * Return c_i_x = c_i_x^+ - c_i_x^-
2933 static __isl_give isl_vec
*extract_var_coef(struct isl_sched_node
*node
,
2934 __isl_keep isl_vec
*sol
)
2942 csol
= isl_vec_alloc(isl_vec_get_ctx(sol
), node
->nvar
);
2946 pos
= 1 + node_var_coef_offset(node
);
2947 for (i
= 0; i
< node
->nvar
; ++i
)
2948 isl_int_sub(csol
->el
[node
->nvar
- 1 - i
],
2949 sol
->el
[pos
+ 2 * i
+ 1], sol
->el
[pos
+ 2 * i
]);
2954 /* Update the schedules of all nodes based on the given solution
2955 * of the LP problem.
2956 * The new row is added to the current band.
2957 * All possibly negative coefficients are encoded as a difference
2958 * of two non-negative variables, so we need to perform the subtraction
2961 * If coincident is set, then the caller guarantees that the new
2962 * row satisfies the coincidence constraints.
2964 static int update_schedule(struct isl_sched_graph
*graph
,
2965 __isl_take isl_vec
*sol
, int coincident
)
2968 isl_vec
*csol
= NULL
;
2973 isl_die(sol
->ctx
, isl_error_internal
,
2974 "no solution found", goto error
);
2975 if (graph
->n_total_row
>= graph
->max_row
)
2976 isl_die(sol
->ctx
, isl_error_internal
,
2977 "too many schedule rows", goto error
);
2979 for (i
= 0; i
< graph
->n
; ++i
) {
2980 struct isl_sched_node
*node
= &graph
->node
[i
];
2982 int row
= isl_mat_rows(node
->sched
);
2985 csol
= extract_var_coef(node
, sol
);
2989 isl_map_free(node
->sched_map
);
2990 node
->sched_map
= NULL
;
2991 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
2994 pos
= node_cst_coef_offset(node
);
2995 node
->sched
= isl_mat_set_element(node
->sched
,
2996 row
, 0, sol
->el
[1 + pos
]);
2997 pos
= node_par_coef_offset(node
);
2998 for (j
= 0; j
< node
->nparam
; ++j
)
2999 node
->sched
= isl_mat_set_element(node
->sched
,
3000 row
, 1 + j
, sol
->el
[1 + pos
+ j
]);
3001 for (j
= 0; j
< node
->nvar
; ++j
)
3002 node
->sched
= isl_mat_set_element(node
->sched
,
3003 row
, 1 + node
->nparam
+ j
, csol
->el
[j
]);
3004 node
->coincident
[graph
->n_total_row
] = coincident
;
3010 graph
->n_total_row
++;
3019 /* Convert row "row" of node->sched into an isl_aff living in "ls"
3020 * and return this isl_aff.
3022 static __isl_give isl_aff
*extract_schedule_row(__isl_take isl_local_space
*ls
,
3023 struct isl_sched_node
*node
, int row
)
3031 aff
= isl_aff_zero_on_domain(ls
);
3032 if (isl_mat_get_element(node
->sched
, row
, 0, &v
) < 0)
3034 aff
= isl_aff_set_constant(aff
, v
);
3035 for (j
= 0; j
< node
->nparam
; ++j
) {
3036 if (isl_mat_get_element(node
->sched
, row
, 1 + j
, &v
) < 0)
3038 aff
= isl_aff_set_coefficient(aff
, isl_dim_param
, j
, v
);
3040 for (j
= 0; j
< node
->nvar
; ++j
) {
3041 if (isl_mat_get_element(node
->sched
, row
,
3042 1 + node
->nparam
+ j
, &v
) < 0)
3044 aff
= isl_aff_set_coefficient(aff
, isl_dim_in
, j
, v
);
3056 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
3057 * and return this multi_aff.
3059 * The result is defined over the uncompressed node domain.
3061 static __isl_give isl_multi_aff
*node_extract_partial_schedule_multi_aff(
3062 struct isl_sched_node
*node
, int first
, int n
)
3066 isl_local_space
*ls
;
3073 nrow
= isl_mat_rows(node
->sched
);
3074 if (node
->compressed
)
3075 space
= isl_multi_aff_get_domain_space(node
->decompress
);
3077 space
= isl_space_copy(node
->space
);
3078 ls
= isl_local_space_from_space(isl_space_copy(space
));
3079 space
= isl_space_from_domain(space
);
3080 space
= isl_space_add_dims(space
, isl_dim_out
, n
);
3081 ma
= isl_multi_aff_zero(space
);
3083 for (i
= first
; i
< first
+ n
; ++i
) {
3084 aff
= extract_schedule_row(isl_local_space_copy(ls
), node
, i
);
3085 ma
= isl_multi_aff_set_aff(ma
, i
- first
, aff
);
3088 isl_local_space_free(ls
);
3090 if (node
->compressed
)
3091 ma
= isl_multi_aff_pullback_multi_aff(ma
,
3092 isl_multi_aff_copy(node
->compress
));
3097 /* Convert node->sched into a multi_aff and return this multi_aff.
3099 * The result is defined over the uncompressed node domain.
3101 static __isl_give isl_multi_aff
*node_extract_schedule_multi_aff(
3102 struct isl_sched_node
*node
)
3106 nrow
= isl_mat_rows(node
->sched
);
3107 return node_extract_partial_schedule_multi_aff(node
, 0, nrow
);
3110 /* Convert node->sched into a map and return this map.
3112 * The result is cached in node->sched_map, which needs to be released
3113 * whenever node->sched is updated.
3114 * It is defined over the uncompressed node domain.
3116 static __isl_give isl_map
*node_extract_schedule(struct isl_sched_node
*node
)
3118 if (!node
->sched_map
) {
3121 ma
= node_extract_schedule_multi_aff(node
);
3122 node
->sched_map
= isl_map_from_multi_aff(ma
);
3125 return isl_map_copy(node
->sched_map
);
3128 /* Construct a map that can be used to update a dependence relation
3129 * based on the current schedule.
3130 * That is, construct a map expressing that source and sink
3131 * are executed within the same iteration of the current schedule.
3132 * This map can then be intersected with the dependence relation.
3133 * This is not the most efficient way, but this shouldn't be a critical
3136 static __isl_give isl_map
*specializer(struct isl_sched_node
*src
,
3137 struct isl_sched_node
*dst
)
3139 isl_map
*src_sched
, *dst_sched
;
3141 src_sched
= node_extract_schedule(src
);
3142 dst_sched
= node_extract_schedule(dst
);
3143 return isl_map_apply_range(src_sched
, isl_map_reverse(dst_sched
));
3146 /* Intersect the domains of the nested relations in domain and range
3147 * of "umap" with "map".
3149 static __isl_give isl_union_map
*intersect_domains(
3150 __isl_take isl_union_map
*umap
, __isl_keep isl_map
*map
)
3152 isl_union_set
*uset
;
3154 umap
= isl_union_map_zip(umap
);
3155 uset
= isl_union_set_from_set(isl_map_wrap(isl_map_copy(map
)));
3156 umap
= isl_union_map_intersect_domain(umap
, uset
);
3157 umap
= isl_union_map_zip(umap
);
3161 /* Update the dependence relation of the given edge based
3162 * on the current schedule.
3163 * If the dependence is carried completely by the current schedule, then
3164 * it is removed from the edge_tables. It is kept in the list of edges
3165 * as otherwise all edge_tables would have to be recomputed.
3167 * If the edge is of a type that can appear multiple times
3168 * between the same pair of nodes, then it is added to
3169 * the edge table (again). This prevents the situation
3170 * where none of these edges is referenced from the edge table
3171 * because the one that was referenced turned out to be empty and
3172 * was therefore removed from the table.
3174 static isl_stat
update_edge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
3175 struct isl_sched_edge
*edge
)
3180 id
= specializer(edge
->src
, edge
->dst
);
3181 edge
->map
= isl_map_intersect(edge
->map
, isl_map_copy(id
));
3185 if (edge
->tagged_condition
) {
3186 edge
->tagged_condition
=
3187 intersect_domains(edge
->tagged_condition
, id
);
3188 if (!edge
->tagged_condition
)
3191 if (edge
->tagged_validity
) {
3192 edge
->tagged_validity
=
3193 intersect_domains(edge
->tagged_validity
, id
);
3194 if (!edge
->tagged_validity
)
3198 empty
= isl_map_plain_is_empty(edge
->map
);
3202 graph_remove_edge(graph
, edge
);
3203 } else if (is_multi_edge_type(edge
)) {
3204 if (graph_edge_tables_add(ctx
, graph
, edge
) < 0)
3212 return isl_stat_error
;
3215 /* Does the domain of "umap" intersect "uset"?
3217 static int domain_intersects(__isl_keep isl_union_map
*umap
,
3218 __isl_keep isl_union_set
*uset
)
3222 umap
= isl_union_map_copy(umap
);
3223 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(uset
));
3224 empty
= isl_union_map_is_empty(umap
);
3225 isl_union_map_free(umap
);
3227 return empty
< 0 ? -1 : !empty
;
3230 /* Does the range of "umap" intersect "uset"?
3232 static int range_intersects(__isl_keep isl_union_map
*umap
,
3233 __isl_keep isl_union_set
*uset
)
3237 umap
= isl_union_map_copy(umap
);
3238 umap
= isl_union_map_intersect_range(umap
, isl_union_set_copy(uset
));
3239 empty
= isl_union_map_is_empty(umap
);
3240 isl_union_map_free(umap
);
3242 return empty
< 0 ? -1 : !empty
;
3245 /* Are the condition dependences of "edge" local with respect to
3246 * the current schedule?
3248 * That is, are domain and range of the condition dependences mapped
3249 * to the same point?
3251 * In other words, is the condition false?
3253 static int is_condition_false(struct isl_sched_edge
*edge
)
3255 isl_union_map
*umap
;
3256 isl_map
*map
, *sched
, *test
;
3259 empty
= isl_union_map_is_empty(edge
->tagged_condition
);
3260 if (empty
< 0 || empty
)
3263 umap
= isl_union_map_copy(edge
->tagged_condition
);
3264 umap
= isl_union_map_zip(umap
);
3265 umap
= isl_union_set_unwrap(isl_union_map_domain(umap
));
3266 map
= isl_map_from_union_map(umap
);
3268 sched
= node_extract_schedule(edge
->src
);
3269 map
= isl_map_apply_domain(map
, sched
);
3270 sched
= node_extract_schedule(edge
->dst
);
3271 map
= isl_map_apply_range(map
, sched
);
3273 test
= isl_map_identity(isl_map_get_space(map
));
3274 local
= isl_map_is_subset(map
, test
);
3281 /* For each conditional validity constraint that is adjacent
3282 * to a condition with domain in condition_source or range in condition_sink,
3283 * turn it into an unconditional validity constraint.
3285 static int unconditionalize_adjacent_validity(struct isl_sched_graph
*graph
,
3286 __isl_take isl_union_set
*condition_source
,
3287 __isl_take isl_union_set
*condition_sink
)
3291 condition_source
= isl_union_set_coalesce(condition_source
);
3292 condition_sink
= isl_union_set_coalesce(condition_sink
);
3294 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3296 isl_union_map
*validity
;
3298 if (!is_conditional_validity(&graph
->edge
[i
]))
3300 if (is_validity(&graph
->edge
[i
]))
3303 validity
= graph
->edge
[i
].tagged_validity
;
3304 adjacent
= domain_intersects(validity
, condition_sink
);
3305 if (adjacent
>= 0 && !adjacent
)
3306 adjacent
= range_intersects(validity
, condition_source
);
3312 set_validity(&graph
->edge
[i
]);
3315 isl_union_set_free(condition_source
);
3316 isl_union_set_free(condition_sink
);
3319 isl_union_set_free(condition_source
);
3320 isl_union_set_free(condition_sink
);
3324 /* Update the dependence relations of all edges based on the current schedule
3325 * and enforce conditional validity constraints that are adjacent
3326 * to satisfied condition constraints.
3328 * First check if any of the condition constraints are satisfied
3329 * (i.e., not local to the outer schedule) and keep track of
3330 * their domain and range.
3331 * Then update all dependence relations (which removes the non-local
3333 * Finally, if any condition constraints turned out to be satisfied,
3334 * then turn all adjacent conditional validity constraints into
3335 * unconditional validity constraints.
3337 static int update_edges(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3341 isl_union_set
*source
, *sink
;
3343 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3344 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3345 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3347 isl_union_set
*uset
;
3348 isl_union_map
*umap
;
3350 if (!is_condition(&graph
->edge
[i
]))
3352 if (is_local(&graph
->edge
[i
]))
3354 local
= is_condition_false(&graph
->edge
[i
]);
3362 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
3363 uset
= isl_union_map_domain(umap
);
3364 source
= isl_union_set_union(source
, uset
);
3366 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
3367 uset
= isl_union_map_range(umap
);
3368 sink
= isl_union_set_union(sink
, uset
);
3371 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3372 if (update_edge(ctx
, graph
, &graph
->edge
[i
]) < 0)
3377 return unconditionalize_adjacent_validity(graph
, source
, sink
);
3379 isl_union_set_free(source
);
3380 isl_union_set_free(sink
);
3383 isl_union_set_free(source
);
3384 isl_union_set_free(sink
);
3388 static void next_band(struct isl_sched_graph
*graph
)
3390 graph
->band_start
= graph
->n_total_row
;
3393 /* Return the union of the universe domains of the nodes in "graph"
3394 * that satisfy "pred".
3396 static __isl_give isl_union_set
*isl_sched_graph_domain(isl_ctx
*ctx
,
3397 struct isl_sched_graph
*graph
,
3398 int (*pred
)(struct isl_sched_node
*node
, int data
), int data
)
3404 for (i
= 0; i
< graph
->n
; ++i
)
3405 if (pred(&graph
->node
[i
], data
))
3409 isl_die(ctx
, isl_error_internal
,
3410 "empty component", return NULL
);
3412 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
3413 dom
= isl_union_set_from_set(set
);
3415 for (i
= i
+ 1; i
< graph
->n
; ++i
) {
3416 if (!pred(&graph
->node
[i
], data
))
3418 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
3419 dom
= isl_union_set_union(dom
, isl_union_set_from_set(set
));
3425 /* Return a list of unions of universe domains, where each element
3426 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
3428 static __isl_give isl_union_set_list
*extract_sccs(isl_ctx
*ctx
,
3429 struct isl_sched_graph
*graph
)
3432 isl_union_set_list
*filters
;
3434 filters
= isl_union_set_list_alloc(ctx
, graph
->scc
);
3435 for (i
= 0; i
< graph
->scc
; ++i
) {
3438 dom
= isl_sched_graph_domain(ctx
, graph
, &node_scc_exactly
, i
);
3439 filters
= isl_union_set_list_add(filters
, dom
);
3445 /* Return a list of two unions of universe domains, one for the SCCs up
3446 * to and including graph->src_scc and another for the other SCCs.
3448 static __isl_give isl_union_set_list
*extract_split(isl_ctx
*ctx
,
3449 struct isl_sched_graph
*graph
)
3452 isl_union_set_list
*filters
;
3454 filters
= isl_union_set_list_alloc(ctx
, 2);
3455 dom
= isl_sched_graph_domain(ctx
, graph
,
3456 &node_scc_at_most
, graph
->src_scc
);
3457 filters
= isl_union_set_list_add(filters
, dom
);
3458 dom
= isl_sched_graph_domain(ctx
, graph
,
3459 &node_scc_at_least
, graph
->src_scc
+ 1);
3460 filters
= isl_union_set_list_add(filters
, dom
);
3465 /* Copy nodes that satisfy node_pred from the src dependence graph
3466 * to the dst dependence graph.
3468 static isl_stat
copy_nodes(struct isl_sched_graph
*dst
,
3469 struct isl_sched_graph
*src
,
3470 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
3475 for (i
= 0; i
< src
->n
; ++i
) {
3478 if (!node_pred(&src
->node
[i
], data
))
3482 dst
->node
[j
].space
= isl_space_copy(src
->node
[i
].space
);
3483 dst
->node
[j
].compressed
= src
->node
[i
].compressed
;
3484 dst
->node
[j
].hull
= isl_set_copy(src
->node
[i
].hull
);
3485 dst
->node
[j
].compress
=
3486 isl_multi_aff_copy(src
->node
[i
].compress
);
3487 dst
->node
[j
].decompress
=
3488 isl_multi_aff_copy(src
->node
[i
].decompress
);
3489 dst
->node
[j
].nvar
= src
->node
[i
].nvar
;
3490 dst
->node
[j
].nparam
= src
->node
[i
].nparam
;
3491 dst
->node
[j
].sched
= isl_mat_copy(src
->node
[i
].sched
);
3492 dst
->node
[j
].sched_map
= isl_map_copy(src
->node
[i
].sched_map
);
3493 dst
->node
[j
].coincident
= src
->node
[i
].coincident
;
3494 dst
->node
[j
].sizes
= isl_multi_val_copy(src
->node
[i
].sizes
);
3495 dst
->node
[j
].bounds
= isl_basic_set_copy(src
->node
[i
].bounds
);
3496 dst
->node
[j
].max
= isl_vec_copy(src
->node
[i
].max
);
3499 if (!dst
->node
[j
].space
|| !dst
->node
[j
].sched
)
3500 return isl_stat_error
;
3501 if (dst
->node
[j
].compressed
&&
3502 (!dst
->node
[j
].hull
|| !dst
->node
[j
].compress
||
3503 !dst
->node
[j
].decompress
))
3504 return isl_stat_error
;
3510 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
3511 * to the dst dependence graph.
3512 * If the source or destination node of the edge is not in the destination
3513 * graph, then it must be a backward proximity edge and it should simply
3516 static isl_stat
copy_edges(isl_ctx
*ctx
, struct isl_sched_graph
*dst
,
3517 struct isl_sched_graph
*src
,
3518 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
), int data
)
3523 for (i
= 0; i
< src
->n_edge
; ++i
) {
3524 struct isl_sched_edge
*edge
= &src
->edge
[i
];
3526 isl_union_map
*tagged_condition
;
3527 isl_union_map
*tagged_validity
;
3528 struct isl_sched_node
*dst_src
, *dst_dst
;
3530 if (!edge_pred(edge
, data
))
3533 if (isl_map_plain_is_empty(edge
->map
))
3536 dst_src
= graph_find_node(ctx
, dst
, edge
->src
->space
);
3537 dst_dst
= graph_find_node(ctx
, dst
, edge
->dst
->space
);
3538 if (!dst_src
|| !dst_dst
)
3539 return isl_stat_error
;
3540 if (!is_node(dst
, dst_src
) || !is_node(dst
, dst_dst
)) {
3541 if (is_validity(edge
) || is_conditional_validity(edge
))
3542 isl_die(ctx
, isl_error_internal
,
3543 "backward (conditional) validity edge",
3544 return isl_stat_error
);
3548 map
= isl_map_copy(edge
->map
);
3549 tagged_condition
= isl_union_map_copy(edge
->tagged_condition
);
3550 tagged_validity
= isl_union_map_copy(edge
->tagged_validity
);
3552 dst
->edge
[dst
->n_edge
].src
= dst_src
;
3553 dst
->edge
[dst
->n_edge
].dst
= dst_dst
;
3554 dst
->edge
[dst
->n_edge
].map
= map
;
3555 dst
->edge
[dst
->n_edge
].tagged_condition
= tagged_condition
;
3556 dst
->edge
[dst
->n_edge
].tagged_validity
= tagged_validity
;
3557 dst
->edge
[dst
->n_edge
].types
= edge
->types
;
3560 if (edge
->tagged_condition
&& !tagged_condition
)
3561 return isl_stat_error
;
3562 if (edge
->tagged_validity
&& !tagged_validity
)
3563 return isl_stat_error
;
3565 if (graph_edge_tables_add(ctx
, dst
,
3566 &dst
->edge
[dst
->n_edge
- 1]) < 0)
3567 return isl_stat_error
;
3573 /* Compute the maximal number of variables over all nodes.
3574 * This is the maximal number of linearly independent schedule
3575 * rows that we need to compute.
3576 * Just in case we end up in a part of the dependence graph
3577 * with only lower-dimensional domains, we make sure we will
3578 * compute the required amount of extra linearly independent rows.
3580 static int compute_maxvar(struct isl_sched_graph
*graph
)
3585 for (i
= 0; i
< graph
->n
; ++i
) {
3586 struct isl_sched_node
*node
= &graph
->node
[i
];
3589 if (node_update_vmap(node
) < 0)
3591 nvar
= node
->nvar
+ graph
->n_row
- node
->rank
;
3592 if (nvar
> graph
->maxvar
)
3593 graph
->maxvar
= nvar
;
3599 /* Extract the subgraph of "graph" that consists of the nodes satisfying
3600 * "node_pred" and the edges satisfying "edge_pred" and store
3601 * the result in "sub".
3603 static isl_stat
extract_sub_graph(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
3604 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
3605 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
3606 int data
, struct isl_sched_graph
*sub
)
3608 int i
, n
= 0, n_edge
= 0;
3611 for (i
= 0; i
< graph
->n
; ++i
)
3612 if (node_pred(&graph
->node
[i
], data
))
3614 for (i
= 0; i
< graph
->n_edge
; ++i
)
3615 if (edge_pred(&graph
->edge
[i
], data
))
3617 if (graph_alloc(ctx
, sub
, n
, n_edge
) < 0)
3618 return isl_stat_error
;
3619 sub
->root
= graph
->root
;
3620 if (copy_nodes(sub
, graph
, node_pred
, data
) < 0)
3621 return isl_stat_error
;
3622 if (graph_init_table(ctx
, sub
) < 0)
3623 return isl_stat_error
;
3624 for (t
= 0; t
<= isl_edge_last
; ++t
)
3625 sub
->max_edge
[t
] = graph
->max_edge
[t
];
3626 if (graph_init_edge_tables(ctx
, sub
) < 0)
3627 return isl_stat_error
;
3628 if (copy_edges(ctx
, sub
, graph
, edge_pred
, data
) < 0)
3629 return isl_stat_error
;
3630 sub
->n_row
= graph
->n_row
;
3631 sub
->max_row
= graph
->max_row
;
3632 sub
->n_total_row
= graph
->n_total_row
;
3633 sub
->band_start
= graph
->band_start
;
3638 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
3639 struct isl_sched_graph
*graph
);
3640 static __isl_give isl_schedule_node
*compute_schedule_wcc(
3641 isl_schedule_node
*node
, struct isl_sched_graph
*graph
);
3643 /* Compute a schedule for a subgraph of "graph". In particular, for
3644 * the graph composed of nodes that satisfy node_pred and edges that
3645 * that satisfy edge_pred.
3646 * If the subgraph is known to consist of a single component, then wcc should
3647 * be set and then we call compute_schedule_wcc on the constructed subgraph.
3648 * Otherwise, we call compute_schedule, which will check whether the subgraph
3651 * The schedule is inserted at "node" and the updated schedule node
3654 static __isl_give isl_schedule_node
*compute_sub_schedule(
3655 __isl_take isl_schedule_node
*node
, isl_ctx
*ctx
,
3656 struct isl_sched_graph
*graph
,
3657 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
3658 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
3661 struct isl_sched_graph split
= { 0 };
3663 if (extract_sub_graph(ctx
, graph
, node_pred
, edge_pred
, data
,
3668 node
= compute_schedule_wcc(node
, &split
);
3670 node
= compute_schedule(node
, &split
);
3672 graph_free(ctx
, &split
);
3675 graph_free(ctx
, &split
);
3676 return isl_schedule_node_free(node
);
3679 static int edge_scc_exactly(struct isl_sched_edge
*edge
, int scc
)
3681 return edge
->src
->scc
== scc
&& edge
->dst
->scc
== scc
;
3684 static int edge_dst_scc_at_most(struct isl_sched_edge
*edge
, int scc
)
3686 return edge
->dst
->scc
<= scc
;
3689 static int edge_src_scc_at_least(struct isl_sched_edge
*edge
, int scc
)
3691 return edge
->src
->scc
>= scc
;
3694 /* Reset the current band by dropping all its schedule rows.
3696 static isl_stat
reset_band(struct isl_sched_graph
*graph
)
3701 drop
= graph
->n_total_row
- graph
->band_start
;
3702 graph
->n_total_row
-= drop
;
3703 graph
->n_row
-= drop
;
3705 for (i
= 0; i
< graph
->n
; ++i
) {
3706 struct isl_sched_node
*node
= &graph
->node
[i
];
3708 isl_map_free(node
->sched_map
);
3709 node
->sched_map
= NULL
;
3711 node
->sched
= isl_mat_drop_rows(node
->sched
,
3712 graph
->band_start
, drop
);
3715 return isl_stat_error
;
3721 /* Split the current graph into two parts and compute a schedule for each
3722 * part individually. In particular, one part consists of all SCCs up
3723 * to and including graph->src_scc, while the other part contains the other
3724 * SCCs. The split is enforced by a sequence node inserted at position "node"
3725 * in the schedule tree. Return the updated schedule node.
3726 * If either of these two parts consists of a sequence, then it is spliced
3727 * into the sequence containing the two parts.
3729 * The current band is reset. It would be possible to reuse
3730 * the previously computed rows as the first rows in the next
3731 * band, but recomputing them may result in better rows as we are looking
3732 * at a smaller part of the dependence graph.
3734 static __isl_give isl_schedule_node
*compute_split_schedule(
3735 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3739 isl_union_set_list
*filters
;
3744 if (reset_band(graph
) < 0)
3745 return isl_schedule_node_free(node
);
3749 ctx
= isl_schedule_node_get_ctx(node
);
3750 filters
= extract_split(ctx
, graph
);
3751 node
= isl_schedule_node_insert_sequence(node
, filters
);
3752 node
= isl_schedule_node_child(node
, 1);
3753 node
= isl_schedule_node_child(node
, 0);
3755 node
= compute_sub_schedule(node
, ctx
, graph
,
3756 &node_scc_at_least
, &edge_src_scc_at_least
,
3757 graph
->src_scc
+ 1, 0);
3758 is_seq
= isl_schedule_node_get_type(node
) == isl_schedule_node_sequence
;
3759 node
= isl_schedule_node_parent(node
);
3760 node
= isl_schedule_node_parent(node
);
3762 node
= isl_schedule_node_sequence_splice_child(node
, 1);
3763 node
= isl_schedule_node_child(node
, 0);
3764 node
= isl_schedule_node_child(node
, 0);
3765 node
= compute_sub_schedule(node
, ctx
, graph
,
3766 &node_scc_at_most
, &edge_dst_scc_at_most
,
3768 is_seq
= isl_schedule_node_get_type(node
) == isl_schedule_node_sequence
;
3769 node
= isl_schedule_node_parent(node
);
3770 node
= isl_schedule_node_parent(node
);
3772 node
= isl_schedule_node_sequence_splice_child(node
, 0);
3777 /* Insert a band node at position "node" in the schedule tree corresponding
3778 * to the current band in "graph". Mark the band node permutable
3779 * if "permutable" is set.
3780 * The partial schedules and the coincidence property are extracted
3781 * from the graph nodes.
3782 * Return the updated schedule node.
3784 static __isl_give isl_schedule_node
*insert_current_band(
3785 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
3791 isl_multi_pw_aff
*mpa
;
3792 isl_multi_union_pw_aff
*mupa
;
3798 isl_die(isl_schedule_node_get_ctx(node
), isl_error_internal
,
3799 "graph should have at least one node",
3800 return isl_schedule_node_free(node
));
3802 start
= graph
->band_start
;
3803 end
= graph
->n_total_row
;
3806 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[0], start
, n
);
3807 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3808 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3810 for (i
= 1; i
< graph
->n
; ++i
) {
3811 isl_multi_union_pw_aff
*mupa_i
;
3813 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[i
],
3815 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3816 mupa_i
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3817 mupa
= isl_multi_union_pw_aff_union_add(mupa
, mupa_i
);
3819 node
= isl_schedule_node_insert_partial_schedule(node
, mupa
);
3821 for (i
= 0; i
< n
; ++i
)
3822 node
= isl_schedule_node_band_member_set_coincident(node
, i
,
3823 graph
->node
[0].coincident
[start
+ i
]);
3824 node
= isl_schedule_node_band_set_permutable(node
, permutable
);
3829 /* Update the dependence relations based on the current schedule,
3830 * add the current band to "node" and then continue with the computation
3832 * Return the updated schedule node.
3834 static __isl_give isl_schedule_node
*compute_next_band(
3835 __isl_take isl_schedule_node
*node
,
3836 struct isl_sched_graph
*graph
, int permutable
)
3843 ctx
= isl_schedule_node_get_ctx(node
);
3844 if (update_edges(ctx
, graph
) < 0)
3845 return isl_schedule_node_free(node
);
3846 node
= insert_current_band(node
, graph
, permutable
);
3849 node
= isl_schedule_node_child(node
, 0);
3850 node
= compute_schedule(node
, graph
);
3851 node
= isl_schedule_node_parent(node
);
3856 /* Add the constraints "coef" derived from an edge from "node" to itself
3857 * to graph->lp in order to respect the dependences and to try and carry them.
3858 * "pos" is the sequence number of the edge that needs to be carried.
3859 * "coef" represents general constraints on coefficients (c_0, c_x)
3860 * of valid constraints for (y - x) with x and y instances of the node.
3862 * The constraints added to graph->lp need to enforce
3864 * (c_j_0 + c_j_x y) - (c_j_0 + c_j_x x)
3865 * = c_j_x (y - x) >= e_i
3867 * for each (x,y) in the dependence relation of the edge.
3868 * That is, (-e_i, c_j_x) needs to be plugged in for (c_0, c_x),
3869 * taking into account that each coefficient in c_j_x is represented
3870 * as a pair of non-negative coefficients.
3872 static isl_stat
add_intra_constraints(struct isl_sched_graph
*graph
,
3873 struct isl_sched_node
*node
, __isl_take isl_basic_set
*coef
, int pos
)
3877 isl_dim_map
*dim_map
;
3880 return isl_stat_error
;
3882 ctx
= isl_basic_set_get_ctx(coef
);
3883 offset
= coef_var_offset(coef
);
3884 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, 1);
3885 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3886 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
3891 /* Add the constraints "coef" derived from an edge from "src" to "dst"
3892 * to graph->lp in order to respect the dependences and to try and carry them.
3893 * "pos" is the sequence number of the edge that needs to be carried or
3894 * -1 if no attempt should be made to carry the dependences.
3895 * "coef" represents general constraints on coefficients (c_0, c_n, c_x, c_y)
3896 * of valid constraints for (x, y) with x and y instances of "src" and "dst".
3898 * The constraints added to graph->lp need to enforce
3900 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3902 * for each (x,y) in the dependence relation of the edge or
3904 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= 0
3908 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, -c_j_x, c_k_x)
3910 * (c_k_0 - c_j_0, c_k_n - c_j_n, -c_j_x, c_k_x)
3911 * needs to be plugged in for (c_0, c_n, c_x, c_y),
3912 * taking into account that each coefficient in c_j_x and c_k_x is represented
3913 * as a pair of non-negative coefficients.
3915 static isl_stat
add_inter_constraints(struct isl_sched_graph
*graph
,
3916 struct isl_sched_node
*src
, struct isl_sched_node
*dst
,
3917 __isl_take isl_basic_set
*coef
, int pos
)
3921 isl_dim_map
*dim_map
;
3924 return isl_stat_error
;
3926 ctx
= isl_basic_set_get_ctx(coef
);
3927 offset
= coef_var_offset(coef
);
3928 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, 1);
3930 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3931 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
3936 /* Data structure for keeping track of the data needed
3937 * to exploit non-trivial lineality spaces.
3939 * "any_non_trivial" is true if there are any non-trivial lineality spaces.
3940 * If "any_non_trivial" is not true, then "equivalent" and "mask" may be NULL.
3941 * "equivalent" connects instances to other instances on the same line(s).
3942 * "mask" contains the domain spaces of "equivalent".
3943 * Any instance set not in "mask" does not have a non-trivial lineality space.
3945 struct isl_exploit_lineality_data
{
3946 isl_bool any_non_trivial
;
3947 isl_union_map
*equivalent
;
3948 isl_union_set
*mask
;
3951 /* Data structure collecting information used during the construction
3952 * of an LP for carrying dependences.
3954 * "intra" is a sequence of coefficient constraints for intra-node edges.
3955 * "inter" is a sequence of coefficient constraints for inter-node edges.
3956 * "lineality" contains data used to exploit non-trivial lineality spaces.
3959 isl_basic_set_list
*intra
;
3960 isl_basic_set_list
*inter
;
3961 struct isl_exploit_lineality_data lineality
;
3964 /* Free all the data stored in "carry".
3966 static void isl_carry_clear(struct isl_carry
*carry
)
3968 isl_basic_set_list_free(carry
->intra
);
3969 isl_basic_set_list_free(carry
->inter
);
3970 isl_union_map_free(carry
->lineality
.equivalent
);
3971 isl_union_set_free(carry
->lineality
.mask
);
3974 /* Return a pointer to the node in "graph" that lives in "space".
3975 * If the requested node has been compressed, then "space"
3976 * corresponds to the compressed space.
3977 * The graph is assumed to have such a node.
3978 * Return NULL in case of error.
3980 * First try and see if "space" is the space of an uncompressed node.
3981 * If so, return that node.
3982 * Otherwise, "space" was constructed by construct_compressed_id and
3983 * contains a user pointer pointing to the node in the tuple id.
3984 * However, this node belongs to the original dependence graph.
3985 * If "graph" is a subgraph of this original dependence graph,
3986 * then the node with the same space still needs to be looked up
3987 * in the current graph.
3989 static struct isl_sched_node
*graph_find_compressed_node(isl_ctx
*ctx
,
3990 struct isl_sched_graph
*graph
, __isl_keep isl_space
*space
)
3993 struct isl_sched_node
*node
;
3998 node
= graph_find_node(ctx
, graph
, space
);
4001 if (is_node(graph
, node
))
4004 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
4005 node
= isl_id_get_user(id
);
4011 if (!is_node(graph
->root
, node
))
4012 isl_die(ctx
, isl_error_internal
,
4013 "space points to invalid node", return NULL
);
4014 if (graph
!= graph
->root
)
4015 node
= graph_find_node(ctx
, graph
, node
->space
);
4016 if (!is_node(graph
, node
))
4017 isl_die(ctx
, isl_error_internal
,
4018 "unable to find node", return NULL
);
4023 /* Internal data structure for add_all_constraints.
4025 * "graph" is the schedule constraint graph for which an LP problem
4026 * is being constructed.
4027 * "carry_inter" indicates whether inter-node edges should be carried.
4028 * "pos" is the position of the next edge that needs to be carried.
4030 struct isl_add_all_constraints_data
{
4032 struct isl_sched_graph
*graph
;
4037 /* Add the constraints "coef" derived from an edge from a node to itself
4038 * to data->graph->lp in order to respect the dependences and
4039 * to try and carry them.
4041 * The space of "coef" is of the form
4043 * coefficients[[c_cst] -> S[c_x]]
4045 * with S[c_x] the (compressed) space of the node.
4046 * Extract the node from the space and call add_intra_constraints.
4048 static isl_stat
lp_add_intra(__isl_take isl_basic_set
*coef
, void *user
)
4050 struct isl_add_all_constraints_data
*data
= user
;
4052 struct isl_sched_node
*node
;
4054 space
= isl_basic_set_get_space(coef
);
4055 space
= isl_space_range(isl_space_unwrap(space
));
4056 node
= graph_find_compressed_node(data
->ctx
, data
->graph
, space
);
4057 isl_space_free(space
);
4058 return add_intra_constraints(data
->graph
, node
, coef
, data
->pos
++);
4061 /* Add the constraints "coef" derived from an edge from a node j
4062 * to a node k to data->graph->lp in order to respect the dependences and
4063 * to try and carry them (provided data->carry_inter is set).
4065 * The space of "coef" is of the form
4067 * coefficients[[c_cst, c_n] -> [S_j[c_x] -> S_k[c_y]]]
4069 * with S_j[c_x] and S_k[c_y] the (compressed) spaces of the nodes.
4070 * Extract the nodes from the space and call add_inter_constraints.
4072 static isl_stat
lp_add_inter(__isl_take isl_basic_set
*coef
, void *user
)
4074 struct isl_add_all_constraints_data
*data
= user
;
4075 isl_space
*space
, *dom
;
4076 struct isl_sched_node
*src
, *dst
;
4079 space
= isl_basic_set_get_space(coef
);
4080 space
= isl_space_unwrap(isl_space_range(isl_space_unwrap(space
)));
4081 dom
= isl_space_domain(isl_space_copy(space
));
4082 src
= graph_find_compressed_node(data
->ctx
, data
->graph
, dom
);
4083 isl_space_free(dom
);
4084 space
= isl_space_range(space
);
4085 dst
= graph_find_compressed_node(data
->ctx
, data
->graph
, space
);
4086 isl_space_free(space
);
4088 pos
= data
->carry_inter
? data
->pos
++ : -1;
4089 return add_inter_constraints(data
->graph
, src
, dst
, coef
, pos
);
4092 /* Add constraints to graph->lp that force all (conditional) validity
4093 * dependences to be respected and attempt to carry them.
4094 * "intra" is the sequence of coefficient constraints for intra-node edges.
4095 * "inter" is the sequence of coefficient constraints for inter-node edges.
4096 * "carry_inter" indicates whether inter-node edges should be carried or
4099 static isl_stat
add_all_constraints(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
4100 __isl_keep isl_basic_set_list
*intra
,
4101 __isl_keep isl_basic_set_list
*inter
, int carry_inter
)
4103 struct isl_add_all_constraints_data data
= { ctx
, graph
, carry_inter
};
4106 if (isl_basic_set_list_foreach(intra
, &lp_add_intra
, &data
) < 0)
4107 return isl_stat_error
;
4108 if (isl_basic_set_list_foreach(inter
, &lp_add_inter
, &data
) < 0)
4109 return isl_stat_error
;
4113 /* Internal data structure for count_all_constraints
4114 * for keeping track of the number of equality and inequality constraints.
4116 struct isl_sched_count
{
4121 /* Add the number of equality and inequality constraints of "bset"
4122 * to data->n_eq and data->n_ineq.
4124 static isl_stat
bset_update_count(__isl_take isl_basic_set
*bset
, void *user
)
4126 struct isl_sched_count
*data
= user
;
4128 return update_count(bset
, 1, &data
->n_eq
, &data
->n_ineq
);
4131 /* Count the number of equality and inequality constraints
4132 * that will be added to the carry_lp problem.
4133 * We count each edge exactly once.
4134 * "intra" is the sequence of coefficient constraints for intra-node edges.
4135 * "inter" is the sequence of coefficient constraints for inter-node edges.
4137 static isl_stat
count_all_constraints(__isl_keep isl_basic_set_list
*intra
,
4138 __isl_keep isl_basic_set_list
*inter
, int *n_eq
, int *n_ineq
)
4140 struct isl_sched_count data
;
4142 data
.n_eq
= data
.n_ineq
= 0;
4143 if (isl_basic_set_list_foreach(inter
, &bset_update_count
, &data
) < 0)
4144 return isl_stat_error
;
4145 if (isl_basic_set_list_foreach(intra
, &bset_update_count
, &data
) < 0)
4146 return isl_stat_error
;
4149 *n_ineq
= data
.n_ineq
;
4154 /* Construct an LP problem for finding schedule coefficients
4155 * such that the schedule carries as many validity dependences as possible.
4156 * In particular, for each dependence i, we bound the dependence distance
4157 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
4158 * of all e_i's. Dependences with e_i = 0 in the solution are simply
4159 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
4160 * "intra" is the sequence of coefficient constraints for intra-node edges.
4161 * "inter" is the sequence of coefficient constraints for inter-node edges.
4162 * "n_edge" is the total number of edges.
4163 * "carry_inter" indicates whether inter-node edges should be carried or
4164 * only respected. That is, if "carry_inter" is not set, then
4165 * no e_i variables are introduced for the inter-node edges.
4167 * All variables of the LP are non-negative. The actual coefficients
4168 * may be negative, so each coefficient is represented as the difference
4169 * of two non-negative variables. The negative part always appears
4170 * immediately before the positive part.
4171 * Other than that, the variables have the following order
4173 * - sum of (1 - e_i) over all edges
4174 * - sum of all c_n coefficients
4175 * (unconstrained when computing non-parametric schedules)
4176 * - sum of positive and negative parts of all c_x coefficients
4180 * - positive and negative parts of c_i_x, in opposite order
4181 * - c_i_n (if parametric)
4184 * The constraints are those from the (validity) edges plus three equalities
4185 * to express the sums and n_edge inequalities to express e_i <= 1.
4187 static isl_stat
setup_carry_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
4188 int n_edge
, __isl_keep isl_basic_set_list
*intra
,
4189 __isl_keep isl_basic_set_list
*inter
, int carry_inter
)
4198 for (i
= 0; i
< graph
->n
; ++i
) {
4199 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
4200 node
->start
= total
;
4201 total
+= 1 + node
->nparam
+ 2 * node
->nvar
;
4204 if (count_all_constraints(intra
, inter
, &n_eq
, &n_ineq
) < 0)
4205 return isl_stat_error
;
4207 dim
= isl_space_set_alloc(ctx
, 0, total
);
4208 isl_basic_set_free(graph
->lp
);
4211 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
4212 graph
->lp
= isl_basic_set_set_rational(graph
->lp
);
4214 k
= isl_basic_set_alloc_equality(graph
->lp
);
4216 return isl_stat_error
;
4217 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
4218 isl_int_set_si(graph
->lp
->eq
[k
][0], -n_edge
);
4219 isl_int_set_si(graph
->lp
->eq
[k
][1], 1);
4220 for (i
= 0; i
< n_edge
; ++i
)
4221 isl_int_set_si(graph
->lp
->eq
[k
][4 + i
], 1);
4223 if (add_param_sum_constraint(graph
, 1) < 0)
4224 return isl_stat_error
;
4225 if (add_var_sum_constraint(graph
, 2) < 0)
4226 return isl_stat_error
;
4228 for (i
= 0; i
< n_edge
; ++i
) {
4229 k
= isl_basic_set_alloc_inequality(graph
->lp
);
4231 return isl_stat_error
;
4232 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
4233 isl_int_set_si(graph
->lp
->ineq
[k
][4 + i
], -1);
4234 isl_int_set_si(graph
->lp
->ineq
[k
][0], 1);
4237 if (add_all_constraints(ctx
, graph
, intra
, inter
, carry_inter
) < 0)
4238 return isl_stat_error
;
4243 static __isl_give isl_schedule_node
*compute_component_schedule(
4244 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
4247 /* If the schedule_split_scaled option is set and if the linear
4248 * parts of the scheduling rows for all nodes in the graphs have
4249 * a non-trivial common divisor, then remove this
4250 * common divisor from the linear part.
4251 * Otherwise, insert a band node directly and continue with
4252 * the construction of the schedule.
4254 * If a non-trivial common divisor is found, then
4255 * the linear part is reduced and the remainder is ignored.
4256 * The pieces of the graph that are assigned different remainders
4257 * form (groups of) strongly connected components within
4258 * the scaled down band. If needed, they can therefore
4259 * be ordered along this remainder in a sequence node.
4260 * However, this ordering is not enforced here in order to allow
4261 * the scheduler to combine some of the strongly connected components.
4263 static __isl_give isl_schedule_node
*split_scaled(
4264 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4274 ctx
= isl_schedule_node_get_ctx(node
);
4275 if (!ctx
->opt
->schedule_split_scaled
)
4276 return compute_next_band(node
, graph
, 0);
4278 return compute_next_band(node
, graph
, 0);
4281 isl_int_init(gcd_i
);
4283 isl_int_set_si(gcd
, 0);
4285 row
= isl_mat_rows(graph
->node
[0].sched
) - 1;
4287 for (i
= 0; i
< graph
->n
; ++i
) {
4288 struct isl_sched_node
*node
= &graph
->node
[i
];
4289 int cols
= isl_mat_cols(node
->sched
);
4291 isl_seq_gcd(node
->sched
->row
[row
] + 1, cols
- 1, &gcd_i
);
4292 isl_int_gcd(gcd
, gcd
, gcd_i
);
4295 isl_int_clear(gcd_i
);
4297 if (isl_int_cmp_si(gcd
, 1) <= 0) {
4299 return compute_next_band(node
, graph
, 0);
4302 for (i
= 0; i
< graph
->n
; ++i
) {
4303 struct isl_sched_node
*node
= &graph
->node
[i
];
4305 isl_int_fdiv_q(node
->sched
->row
[row
][0],
4306 node
->sched
->row
[row
][0], gcd
);
4307 isl_int_mul(node
->sched
->row
[row
][0],
4308 node
->sched
->row
[row
][0], gcd
);
4309 node
->sched
= isl_mat_scale_down_row(node
->sched
, row
, gcd
);
4316 return compute_next_band(node
, graph
, 0);
4319 return isl_schedule_node_free(node
);
4322 /* Is the schedule row "sol" trivial on node "node"?
4323 * That is, is the solution zero on the dimensions linearly independent of
4324 * the previously found solutions?
4325 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
4327 * Each coefficient is represented as the difference between
4328 * two non-negative values in "sol".
4329 * We construct the schedule row s and check if it is linearly
4330 * independent of previously computed schedule rows
4331 * by computing T s, with T the linear combinations that are zero
4332 * on linearly dependent schedule rows.
4333 * If the result consists of all zeros, then the solution is trivial.
4335 static int is_trivial(struct isl_sched_node
*node
, __isl_keep isl_vec
*sol
)
4342 if (node
->nvar
== node
->rank
)
4345 node_sol
= extract_var_coef(node
, sol
);
4346 node_sol
= isl_mat_vec_product(isl_mat_copy(node
->indep
), node_sol
);
4350 trivial
= isl_seq_first_non_zero(node_sol
->el
,
4351 node
->nvar
- node
->rank
) == -1;
4353 isl_vec_free(node_sol
);
4358 /* Is the schedule row "sol" trivial on any node where it should
4360 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
4362 static int is_any_trivial(struct isl_sched_graph
*graph
,
4363 __isl_keep isl_vec
*sol
)
4367 for (i
= 0; i
< graph
->n
; ++i
) {
4368 struct isl_sched_node
*node
= &graph
->node
[i
];
4371 if (!needs_row(graph
, node
))
4373 trivial
= is_trivial(node
, sol
);
4374 if (trivial
< 0 || trivial
)
4381 /* Does the schedule represented by "sol" perform loop coalescing on "node"?
4382 * If so, return the position of the coalesced dimension.
4383 * Otherwise, return node->nvar or -1 on error.
4385 * In particular, look for pairs of coefficients c_i and c_j such that
4386 * |c_j/c_i| > ceil(size_i/2), i.e., |c_j| > |c_i * ceil(size_i/2)|.
4387 * If any such pair is found, then return i.
4388 * If size_i is infinity, then no check on c_i needs to be performed.
4390 static int find_node_coalescing(struct isl_sched_node
*node
,
4391 __isl_keep isl_vec
*sol
)
4397 if (node
->nvar
<= 1)
4400 csol
= extract_var_coef(node
, sol
);
4404 for (i
= 0; i
< node
->nvar
; ++i
) {
4407 if (isl_int_is_zero(csol
->el
[i
]))
4409 v
= isl_multi_val_get_val(node
->sizes
, i
);
4412 if (!isl_val_is_int(v
)) {
4416 v
= isl_val_div_ui(v
, 2);
4417 v
= isl_val_ceil(v
);
4420 isl_int_mul(max
, v
->n
, csol
->el
[i
]);
4423 for (j
= 0; j
< node
->nvar
; ++j
) {
4426 if (isl_int_abs_gt(csol
->el
[j
], max
))
4442 /* Force the schedule coefficient at position "pos" of "node" to be zero
4444 * The coefficient is encoded as the difference between two non-negative
4445 * variables. Force these two variables to have the same value.
4447 static __isl_give isl_tab_lexmin
*zero_out_node_coef(
4448 __isl_take isl_tab_lexmin
*tl
, struct isl_sched_node
*node
, int pos
)
4454 ctx
= isl_space_get_ctx(node
->space
);
4455 dim
= isl_tab_lexmin_dim(tl
);
4457 return isl_tab_lexmin_free(tl
);
4458 eq
= isl_vec_alloc(ctx
, 1 + dim
);
4459 eq
= isl_vec_clr(eq
);
4461 return isl_tab_lexmin_free(tl
);
4463 pos
= 1 + node_var_coef_pos(node
, pos
);
4464 isl_int_set_si(eq
->el
[pos
], 1);
4465 isl_int_set_si(eq
->el
[pos
+ 1], -1);
4466 tl
= isl_tab_lexmin_add_eq(tl
, eq
->el
);
4472 /* Return the lexicographically smallest rational point in the basic set
4473 * from which "tl" was constructed, double checking that this input set
4476 static __isl_give isl_vec
*non_empty_solution(__isl_keep isl_tab_lexmin
*tl
)
4480 sol
= isl_tab_lexmin_get_solution(tl
);
4484 isl_die(isl_vec_get_ctx(sol
), isl_error_internal
,
4485 "error in schedule construction",
4486 return isl_vec_free(sol
));
4490 /* Does the solution "sol" of the LP problem constructed by setup_carry_lp
4491 * carry any of the "n_edge" groups of dependences?
4492 * The value in the first position is the sum of (1 - e_i) over all "n_edge"
4493 * edges, with 0 <= e_i <= 1 equal to 1 when the dependences represented
4494 * by the edge are carried by the solution.
4495 * If the sum of the (1 - e_i) is smaller than "n_edge" then at least
4496 * one of those is carried.
4498 * Note that despite the fact that the problem is solved using a rational
4499 * solver, the solution is guaranteed to be integral.
4500 * Specifically, the dependence distance lower bounds e_i (and therefore
4501 * also their sum) are integers. See Lemma 5 of [1].
4503 * Any potential denominator of the sum is cleared by this function.
4504 * The denominator is not relevant for any of the other elements
4507 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4508 * Problem, Part II: Multi-Dimensional Time.
4509 * In Intl. Journal of Parallel Programming, 1992.
4511 static int carries_dependences(__isl_keep isl_vec
*sol
, int n_edge
)
4513 isl_int_divexact(sol
->el
[1], sol
->el
[1], sol
->el
[0]);
4514 isl_int_set_si(sol
->el
[0], 1);
4515 return isl_int_cmp_si(sol
->el
[1], n_edge
) < 0;
4518 /* Return the lexicographically smallest rational point in "lp",
4519 * assuming that all variables are non-negative and performing some
4520 * additional sanity checks.
4521 * If "want_integral" is set, then compute the lexicographically smallest
4522 * integer point instead.
4523 * In particular, "lp" should not be empty by construction.
4524 * Double check that this is the case.
4525 * If dependences are not carried for any of the "n_edge" edges,
4526 * then return an empty vector.
4528 * If the schedule_treat_coalescing option is set and
4529 * if the computed schedule performs loop coalescing on a given node,
4530 * i.e., if it is of the form
4532 * c_i i + c_j j + ...
4534 * with |c_j/c_i| >= size_i, then force the coefficient c_i to be zero
4535 * to cut out this solution. Repeat this process until no more loop
4536 * coalescing occurs or until no more dependences can be carried.
4537 * In the latter case, revert to the previously computed solution.
4539 * If the caller requests an integral solution and if coalescing should
4540 * be treated, then perform the coalescing treatment first as
4541 * an integral solution computed before coalescing treatment
4542 * would carry the same number of edges and would therefore probably
4543 * also be coalescing.
4545 * To allow the coalescing treatment to be performed first,
4546 * the initial solution is allowed to be rational and it is only
4547 * cut out (if needed) in the next iteration, if no coalescing measures
4550 static __isl_give isl_vec
*non_neg_lexmin(struct isl_sched_graph
*graph
,
4551 __isl_take isl_basic_set
*lp
, int n_edge
, int want_integral
)
4556 isl_vec
*sol
= NULL
, *prev
;
4557 int treat_coalescing
;
4562 ctx
= isl_basic_set_get_ctx(lp
);
4563 treat_coalescing
= isl_options_get_schedule_treat_coalescing(ctx
);
4564 tl
= isl_tab_lexmin_from_basic_set(lp
);
4572 tl
= isl_tab_lexmin_cut_to_integer(tl
);
4574 sol
= non_empty_solution(tl
);
4578 integral
= isl_int_is_one(sol
->el
[0]);
4579 if (!carries_dependences(sol
, n_edge
)) {
4581 prev
= isl_vec_alloc(ctx
, 0);
4586 prev
= isl_vec_free(prev
);
4587 cut
= want_integral
&& !integral
;
4590 if (!treat_coalescing
)
4592 for (i
= 0; i
< graph
->n
; ++i
) {
4593 struct isl_sched_node
*node
= &graph
->node
[i
];
4595 pos
= find_node_coalescing(node
, sol
);
4598 if (pos
< node
->nvar
)
4603 tl
= zero_out_node_coef(tl
, &graph
->node
[i
], pos
);
4606 } while (try_again
);
4608 isl_tab_lexmin_free(tl
);
4612 isl_tab_lexmin_free(tl
);
4618 /* If "edge" is an edge from a node to itself, then add the corresponding
4619 * dependence relation to "umap".
4620 * If "node" has been compressed, then the dependence relation
4621 * is also compressed first.
4623 static __isl_give isl_union_map
*add_intra(__isl_take isl_union_map
*umap
,
4624 struct isl_sched_edge
*edge
)
4627 struct isl_sched_node
*node
= edge
->src
;
4629 if (edge
->src
!= edge
->dst
)
4632 map
= isl_map_copy(edge
->map
);
4633 if (node
->compressed
) {
4634 map
= isl_map_preimage_domain_multi_aff(map
,
4635 isl_multi_aff_copy(node
->decompress
));
4636 map
= isl_map_preimage_range_multi_aff(map
,
4637 isl_multi_aff_copy(node
->decompress
));
4639 umap
= isl_union_map_add_map(umap
, map
);
4643 /* If "edge" is an edge from a node to another node, then add the corresponding
4644 * dependence relation to "umap".
4645 * If the source or destination nodes of "edge" have been compressed,
4646 * then the dependence relation is also compressed first.
4648 static __isl_give isl_union_map
*add_inter(__isl_take isl_union_map
*umap
,
4649 struct isl_sched_edge
*edge
)
4653 if (edge
->src
== edge
->dst
)
4656 map
= isl_map_copy(edge
->map
);
4657 if (edge
->src
->compressed
)
4658 map
= isl_map_preimage_domain_multi_aff(map
,
4659 isl_multi_aff_copy(edge
->src
->decompress
));
4660 if (edge
->dst
->compressed
)
4661 map
= isl_map_preimage_range_multi_aff(map
,
4662 isl_multi_aff_copy(edge
->dst
->decompress
));
4663 umap
= isl_union_map_add_map(umap
, map
);
4667 /* Internal data structure used by union_drop_coalescing_constraints
4668 * to collect bounds on all relevant statements.
4670 * "graph" is the schedule constraint graph for which an LP problem
4671 * is being constructed.
4672 * "bounds" collects the bounds.
4674 struct isl_collect_bounds_data
{
4676 struct isl_sched_graph
*graph
;
4677 isl_union_set
*bounds
;
4680 /* Add the size bounds for the node with instance deltas in "set"
4683 static isl_stat
collect_bounds(__isl_take isl_set
*set
, void *user
)
4685 struct isl_collect_bounds_data
*data
= user
;
4686 struct isl_sched_node
*node
;
4690 space
= isl_set_get_space(set
);
4693 node
= graph_find_compressed_node(data
->ctx
, data
->graph
, space
);
4694 isl_space_free(space
);
4696 bounds
= isl_set_from_basic_set(get_size_bounds(node
));
4697 data
->bounds
= isl_union_set_add_set(data
->bounds
, bounds
);
4702 /* Drop some constraints from "delta" that could be exploited
4703 * to construct loop coalescing schedules.
4704 * In particular, drop those constraint that bound the difference
4705 * to the size of the domain.
4706 * Do this for each set/node in "delta" separately.
4707 * The parameters are assumed to have been projected out by the caller.
4709 static __isl_give isl_union_set
*union_drop_coalescing_constraints(isl_ctx
*ctx
,
4710 struct isl_sched_graph
*graph
, __isl_take isl_union_set
*delta
)
4712 struct isl_collect_bounds_data data
= { ctx
, graph
};
4714 data
.bounds
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
4715 if (isl_union_set_foreach_set(delta
, &collect_bounds
, &data
) < 0)
4716 data
.bounds
= isl_union_set_free(data
.bounds
);
4717 delta
= isl_union_set_plain_gist(delta
, data
.bounds
);
4722 /* Given a non-trivial lineality space "lineality", add the corresponding
4723 * universe set to data->mask and add a map from elements to
4724 * other elements along the lines in "lineality" to data->equivalent.
4725 * If this is the first time this function gets called
4726 * (data->any_non_trivial is still false), then set data->any_non_trivial and
4727 * initialize data->mask and data->equivalent.
4729 * In particular, if the lineality space is defined by equality constraints
4733 * then construct an affine mapping
4737 * and compute the equivalence relation of having the same image under f:
4739 * { x -> x' : E x = E x' }
4741 static isl_stat
add_non_trivial_lineality(__isl_take isl_basic_set
*lineality
,
4742 struct isl_exploit_lineality_data
*data
)
4748 isl_multi_pw_aff
*mpa
;
4752 if (isl_basic_set_check_no_locals(lineality
) < 0)
4755 space
= isl_basic_set_get_space(lineality
);
4756 if (!data
->any_non_trivial
) {
4757 data
->equivalent
= isl_union_map_empty(isl_space_copy(space
));
4758 data
->mask
= isl_union_set_empty(isl_space_copy(space
));
4760 data
->any_non_trivial
= isl_bool_true
;
4762 univ
= isl_set_universe(isl_space_copy(space
));
4763 data
->mask
= isl_union_set_add_set(data
->mask
, univ
);
4765 eq
= isl_basic_set_extract_equalities(lineality
);
4766 n
= isl_mat_rows(eq
);
4767 eq
= isl_mat_insert_zero_rows(eq
, 0, 1);
4768 eq
= isl_mat_set_element_si(eq
, 0, 0, 1);
4769 space
= isl_space_from_domain(space
);
4770 space
= isl_space_add_dims(space
, isl_dim_out
, n
);
4771 ma
= isl_multi_aff_from_aff_mat(space
, eq
);
4772 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
4773 map
= isl_multi_pw_aff_eq_map(mpa
, isl_multi_pw_aff_copy(mpa
));
4774 data
->equivalent
= isl_union_map_add_map(data
->equivalent
, map
);
4776 isl_basic_set_free(lineality
);
4779 isl_basic_set_free(lineality
);
4780 return isl_stat_error
;
4783 /* Check if the lineality space "set" is non-trivial (i.e., is not just
4784 * the origin or, in other words, satisfies a number of equality constraints
4785 * that is smaller than the dimension of the set).
4786 * If so, extend data->mask and data->equivalent accordingly.
4788 * The input should not have any local variables already, but
4789 * isl_set_remove_divs is called to make sure it does not.
4791 static isl_stat
add_lineality(__isl_take isl_set
*set
, void *user
)
4793 struct isl_exploit_lineality_data
*data
= user
;
4794 isl_basic_set
*hull
;
4797 set
= isl_set_remove_divs(set
);
4798 hull
= isl_set_unshifted_simple_hull(set
);
4799 dim
= isl_basic_set_dim(hull
, isl_dim_set
);
4800 n_eq
= isl_basic_set_n_equality(hull
);
4802 return isl_stat_error
;
4804 return add_non_trivial_lineality(hull
, data
);
4805 isl_basic_set_free(hull
);
4809 /* Check if the difference set on intra-node schedule constraints "intra"
4810 * has any non-trivial lineality space.
4811 * If so, then extend the difference set to a difference set
4812 * on equivalent elements. That is, if "intra" is
4814 * { y - x : (x,y) \in V }
4816 * and elements are equivalent if they have the same image under f,
4819 * { y' - x' : (x,y) \in V and f(x) = f(x') and f(y) = f(y') }
4821 * or, since f is linear,
4823 * { y' - x' : (x,y) \in V and f(y - x) = f(y' - x') }
4825 * The results of the search for non-trivial lineality spaces is stored
4828 static __isl_give isl_union_set
*exploit_intra_lineality(
4829 __isl_take isl_union_set
*intra
,
4830 struct isl_exploit_lineality_data
*data
)
4832 isl_union_set
*lineality
;
4833 isl_union_set
*uset
;
4835 data
->any_non_trivial
= isl_bool_false
;
4836 lineality
= isl_union_set_copy(intra
);
4837 lineality
= isl_union_set_combined_lineality_space(lineality
);
4838 if (isl_union_set_foreach_set(lineality
, &add_lineality
, data
) < 0)
4839 data
->any_non_trivial
= isl_bool_error
;
4840 isl_union_set_free(lineality
);
4842 if (data
->any_non_trivial
< 0)
4843 return isl_union_set_free(intra
);
4844 if (!data
->any_non_trivial
)
4847 uset
= isl_union_set_copy(intra
);
4848 intra
= isl_union_set_subtract(intra
, isl_union_set_copy(data
->mask
));
4849 uset
= isl_union_set_apply(uset
, isl_union_map_copy(data
->equivalent
));
4850 intra
= isl_union_set_union(intra
, uset
);
4852 intra
= isl_union_set_remove_divs(intra
);
4857 /* If the difference set on intra-node schedule constraints was found to have
4858 * any non-trivial lineality space by exploit_intra_lineality,
4859 * as recorded in "data", then extend the inter-node
4860 * schedule constraints "inter" to schedule constraints on equivalent elements.
4861 * That is, if "inter" is V and
4862 * elements are equivalent if they have the same image under f, then return
4864 * { (x', y') : (x,y) \in V and f(x) = f(x') and f(y) = f(y') }
4866 static __isl_give isl_union_map
*exploit_inter_lineality(
4867 __isl_take isl_union_map
*inter
,
4868 struct isl_exploit_lineality_data
*data
)
4870 isl_union_map
*umap
;
4872 if (data
->any_non_trivial
< 0)
4873 return isl_union_map_free(inter
);
4874 if (!data
->any_non_trivial
)
4877 umap
= isl_union_map_copy(inter
);
4878 inter
= isl_union_map_subtract_range(inter
,
4879 isl_union_set_copy(data
->mask
));
4880 umap
= isl_union_map_apply_range(umap
,
4881 isl_union_map_copy(data
->equivalent
));
4882 inter
= isl_union_map_union(inter
, umap
);
4883 umap
= isl_union_map_copy(inter
);
4884 inter
= isl_union_map_subtract_domain(inter
,
4885 isl_union_set_copy(data
->mask
));
4886 umap
= isl_union_map_apply_range(isl_union_map_copy(data
->equivalent
),
4888 inter
= isl_union_map_union(inter
, umap
);
4890 inter
= isl_union_map_remove_divs(inter
);
4895 /* For each (conditional) validity edge in "graph",
4896 * add the corresponding dependence relation using "add"
4897 * to a collection of dependence relations and return the result.
4898 * If "coincidence" is set, then coincidence edges are considered as well.
4900 static __isl_give isl_union_map
*collect_validity(struct isl_sched_graph
*graph
,
4901 __isl_give isl_union_map
*(*add
)(__isl_take isl_union_map
*umap
,
4902 struct isl_sched_edge
*edge
), int coincidence
)
4906 isl_union_map
*umap
;
4908 space
= isl_space_copy(graph
->node
[0].space
);
4909 umap
= isl_union_map_empty(space
);
4911 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4912 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
4914 if (!is_any_validity(edge
) &&
4915 (!coincidence
|| !is_coincidence(edge
)))
4918 umap
= add(umap
, edge
);
4924 /* For each dependence relation on a (conditional) validity edge
4925 * from a node to itself,
4926 * construct the set of coefficients of valid constraints for elements
4927 * in that dependence relation and collect the results.
4928 * If "coincidence" is set, then coincidence edges are considered as well.
4930 * In particular, for each dependence relation R, constraints
4931 * on coefficients (c_0, c_x) are constructed such that
4933 * c_0 + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
4935 * If the schedule_treat_coalescing option is set, then some constraints
4936 * that could be exploited to construct coalescing schedules
4937 * are removed before the dual is computed, but after the parameters
4938 * have been projected out.
4939 * The entire computation is essentially the same as that performed
4940 * by intra_coefficients, except that it operates on multiple
4941 * edges together and that the parameters are always projected out.
4943 * Additionally, exploit any non-trivial lineality space
4944 * in the difference set after removing coalescing constraints and
4945 * store the results of the non-trivial lineality space detection in "data".
4946 * The procedure is currently run unconditionally, but it is unlikely
4947 * to find any non-trivial lineality spaces if no coalescing constraints
4948 * have been removed.
4950 * Note that if a dependence relation is a union of basic maps,
4951 * then each basic map needs to be treated individually as it may only
4952 * be possible to carry the dependences expressed by some of those
4953 * basic maps and not all of them.
4954 * The collected validity constraints are therefore not coalesced and
4955 * it is assumed that they are not coalesced automatically.
4956 * Duplicate basic maps can be removed, however.
4957 * In particular, if the same basic map appears as a disjunct
4958 * in multiple edges, then it only needs to be carried once.
4960 static __isl_give isl_basic_set_list
*collect_intra_validity(isl_ctx
*ctx
,
4961 struct isl_sched_graph
*graph
, int coincidence
,
4962 struct isl_exploit_lineality_data
*data
)
4964 isl_union_map
*intra
;
4965 isl_union_set
*delta
;
4966 isl_basic_set_list
*list
;
4968 intra
= collect_validity(graph
, &add_intra
, coincidence
);
4969 delta
= isl_union_map_deltas(intra
);
4970 delta
= isl_union_set_project_out_all_params(delta
);
4971 delta
= isl_union_set_remove_divs(delta
);
4972 if (isl_options_get_schedule_treat_coalescing(ctx
))
4973 delta
= union_drop_coalescing_constraints(ctx
, graph
, delta
);
4974 delta
= exploit_intra_lineality(delta
, data
);
4975 list
= isl_union_set_get_basic_set_list(delta
);
4976 isl_union_set_free(delta
);
4978 return isl_basic_set_list_coefficients(list
);
4981 /* For each dependence relation on a (conditional) validity edge
4982 * from a node to some other node,
4983 * construct the set of coefficients of valid constraints for elements
4984 * in that dependence relation and collect the results.
4985 * If "coincidence" is set, then coincidence edges are considered as well.
4987 * In particular, for each dependence relation R, constraints
4988 * on coefficients (c_0, c_n, c_x, c_y) are constructed such that
4990 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
4992 * This computation is essentially the same as that performed
4993 * by inter_coefficients, except that it operates on multiple
4996 * Additionally, exploit any non-trivial lineality space
4997 * that may have been discovered by collect_intra_validity
4998 * (as stored in "data").
5000 * Note that if a dependence relation is a union of basic maps,
5001 * then each basic map needs to be treated individually as it may only
5002 * be possible to carry the dependences expressed by some of those
5003 * basic maps and not all of them.
5004 * The collected validity constraints are therefore not coalesced and
5005 * it is assumed that they are not coalesced automatically.
5006 * Duplicate basic maps can be removed, however.
5007 * In particular, if the same basic map appears as a disjunct
5008 * in multiple edges, then it only needs to be carried once.
5010 static __isl_give isl_basic_set_list
*collect_inter_validity(
5011 struct isl_sched_graph
*graph
, int coincidence
,
5012 struct isl_exploit_lineality_data
*data
)
5014 isl_union_map
*inter
;
5015 isl_union_set
*wrap
;
5016 isl_basic_set_list
*list
;
5018 inter
= collect_validity(graph
, &add_inter
, coincidence
);
5019 inter
= exploit_inter_lineality(inter
, data
);
5020 inter
= isl_union_map_remove_divs(inter
);
5021 wrap
= isl_union_map_wrap(inter
);
5022 list
= isl_union_set_get_basic_set_list(wrap
);
5023 isl_union_set_free(wrap
);
5024 return isl_basic_set_list_coefficients(list
);
5027 /* Construct an LP problem for finding schedule coefficients
5028 * such that the schedule carries as many of the "n_edge" groups of
5029 * dependences as possible based on the corresponding coefficient
5030 * constraints and return the lexicographically smallest non-trivial solution.
5031 * "intra" is the sequence of coefficient constraints for intra-node edges.
5032 * "inter" is the sequence of coefficient constraints for inter-node edges.
5033 * If "want_integral" is set, then compute an integral solution
5034 * for the coefficients rather than using the numerators
5035 * of a rational solution.
5036 * "carry_inter" indicates whether inter-node edges should be carried or
5039 * If none of the "n_edge" groups can be carried
5040 * then return an empty vector.
5042 static __isl_give isl_vec
*compute_carrying_sol_coef(isl_ctx
*ctx
,
5043 struct isl_sched_graph
*graph
, int n_edge
,
5044 __isl_keep isl_basic_set_list
*intra
,
5045 __isl_keep isl_basic_set_list
*inter
, int want_integral
,
5050 if (setup_carry_lp(ctx
, graph
, n_edge
, intra
, inter
, carry_inter
) < 0)
5053 lp
= isl_basic_set_copy(graph
->lp
);
5054 return non_neg_lexmin(graph
, lp
, n_edge
, want_integral
);
5057 /* Construct an LP problem for finding schedule coefficients
5058 * such that the schedule carries as many of the validity dependences
5060 * return the lexicographically smallest non-trivial solution.
5061 * If "fallback" is set, then the carrying is performed as a fallback
5062 * for the Pluto-like scheduler.
5063 * If "coincidence" is set, then try and carry coincidence edges as well.
5065 * The variable "n_edge" stores the number of groups that should be carried.
5066 * If none of the "n_edge" groups can be carried
5067 * then return an empty vector.
5068 * If, moreover, "n_edge" is zero, then the LP problem does not even
5069 * need to be constructed.
5071 * If a fallback solution is being computed, then compute an integral solution
5072 * for the coefficients rather than using the numerators
5073 * of a rational solution.
5075 * If a fallback solution is being computed, if there are any intra-node
5076 * dependences, and if requested by the user, then first try
5077 * to only carry those intra-node dependences.
5078 * If this fails to carry any dependences, then try again
5079 * with the inter-node dependences included.
5081 static __isl_give isl_vec
*compute_carrying_sol(isl_ctx
*ctx
,
5082 struct isl_sched_graph
*graph
, int fallback
, int coincidence
)
5084 int n_intra
, n_inter
;
5086 struct isl_carry carry
= { 0 };
5089 carry
.intra
= collect_intra_validity(ctx
, graph
, coincidence
,
5091 carry
.inter
= collect_inter_validity(graph
, coincidence
,
5093 if (!carry
.intra
|| !carry
.inter
)
5095 n_intra
= isl_basic_set_list_n_basic_set(carry
.intra
);
5096 n_inter
= isl_basic_set_list_n_basic_set(carry
.inter
);
5098 if (fallback
&& n_intra
> 0 &&
5099 isl_options_get_schedule_carry_self_first(ctx
)) {
5100 sol
= compute_carrying_sol_coef(ctx
, graph
, n_intra
,
5101 carry
.intra
, carry
.inter
, fallback
, 0);
5102 if (!sol
|| sol
->size
!= 0 || n_inter
== 0) {
5103 isl_carry_clear(&carry
);
5109 n_edge
= n_intra
+ n_inter
;
5111 isl_carry_clear(&carry
);
5112 return isl_vec_alloc(ctx
, 0);
5115 sol
= compute_carrying_sol_coef(ctx
, graph
, n_edge
,
5116 carry
.intra
, carry
.inter
, fallback
, 1);
5117 isl_carry_clear(&carry
);
5120 isl_carry_clear(&carry
);
5124 /* Construct a schedule row for each node such that as many validity dependences
5125 * as possible are carried and then continue with the next band.
5126 * If "fallback" is set, then the carrying is performed as a fallback
5127 * for the Pluto-like scheduler.
5128 * If "coincidence" is set, then try and carry coincidence edges as well.
5130 * If there are no validity dependences, then no dependence can be carried and
5131 * the procedure is guaranteed to fail. If there is more than one component,
5132 * then try computing a schedule on each component separately
5133 * to prevent or at least postpone this failure.
5135 * If a schedule row is computed, then check that dependences are carried
5136 * for at least one of the edges.
5138 * If the computed schedule row turns out to be trivial on one or
5139 * more nodes where it should not be trivial, then we throw it away
5140 * and try again on each component separately.
5142 * If there is only one component, then we accept the schedule row anyway,
5143 * but we do not consider it as a complete row and therefore do not
5144 * increment graph->n_row. Note that the ranks of the nodes that
5145 * do get a non-trivial schedule part will get updated regardless and
5146 * graph->maxvar is computed based on these ranks. The test for
5147 * whether more schedule rows are required in compute_schedule_wcc
5148 * is therefore not affected.
5150 * Insert a band corresponding to the schedule row at position "node"
5151 * of the schedule tree and continue with the construction of the schedule.
5152 * This insertion and the continued construction is performed by split_scaled
5153 * after optionally checking for non-trivial common divisors.
5155 static __isl_give isl_schedule_node
*carry(__isl_take isl_schedule_node
*node
,
5156 struct isl_sched_graph
*graph
, int fallback
, int coincidence
)
5165 ctx
= isl_schedule_node_get_ctx(node
);
5166 sol
= compute_carrying_sol(ctx
, graph
, fallback
, coincidence
);
5168 return isl_schedule_node_free(node
);
5169 if (sol
->size
== 0) {
5172 return compute_component_schedule(node
, graph
, 1);
5173 isl_die(ctx
, isl_error_unknown
, "unable to carry dependences",
5174 return isl_schedule_node_free(node
));
5177 trivial
= is_any_trivial(graph
, sol
);
5179 sol
= isl_vec_free(sol
);
5180 } else if (trivial
&& graph
->scc
> 1) {
5182 return compute_component_schedule(node
, graph
, 1);
5185 if (update_schedule(graph
, sol
, 0) < 0)
5186 return isl_schedule_node_free(node
);
5190 return split_scaled(node
, graph
);
5193 /* Construct a schedule row for each node such that as many validity dependences
5194 * as possible are carried and then continue with the next band.
5195 * Do so as a fallback for the Pluto-like scheduler.
5196 * If "coincidence" is set, then try and carry coincidence edges as well.
5198 static __isl_give isl_schedule_node
*carry_fallback(
5199 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
5202 return carry(node
, graph
, 1, coincidence
);
5205 /* Construct a schedule row for each node such that as many validity dependences
5206 * as possible are carried and then continue with the next band.
5207 * Do so for the case where the Feautrier scheduler was selected
5210 static __isl_give isl_schedule_node
*carry_feautrier(
5211 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
5213 return carry(node
, graph
, 0, 0);
5216 /* Construct a schedule row for each node such that as many validity dependences
5217 * as possible are carried and then continue with the next band.
5218 * Do so as a fallback for the Pluto-like scheduler.
5220 static __isl_give isl_schedule_node
*carry_dependences(
5221 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
5223 return carry_fallback(node
, graph
, 0);
5226 /* Construct a schedule row for each node such that as many validity or
5227 * coincidence dependences as possible are carried and
5228 * then continue with the next band.
5229 * Do so as a fallback for the Pluto-like scheduler.
5231 static __isl_give isl_schedule_node
*carry_coincidence(
5232 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
5234 return carry_fallback(node
, graph
, 1);
5237 /* Topologically sort statements mapped to the same schedule iteration
5238 * and add insert a sequence node in front of "node"
5239 * corresponding to this order.
5240 * If "initialized" is set, then it may be assumed that compute_maxvar
5241 * has been called on the current band. Otherwise, call
5242 * compute_maxvar if and before carry_dependences gets called.
5244 * If it turns out to be impossible to sort the statements apart,
5245 * because different dependences impose different orderings
5246 * on the statements, then we extend the schedule such that
5247 * it carries at least one more dependence.
5249 static __isl_give isl_schedule_node
*sort_statements(
5250 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
5254 isl_union_set_list
*filters
;
5259 ctx
= isl_schedule_node_get_ctx(node
);
5261 isl_die(ctx
, isl_error_internal
,
5262 "graph should have at least one node",
5263 return isl_schedule_node_free(node
));
5268 if (update_edges(ctx
, graph
) < 0)
5269 return isl_schedule_node_free(node
);
5271 if (graph
->n_edge
== 0)
5274 if (detect_sccs(ctx
, graph
) < 0)
5275 return isl_schedule_node_free(node
);
5278 if (graph
->scc
< graph
->n
) {
5279 if (!initialized
&& compute_maxvar(graph
) < 0)
5280 return isl_schedule_node_free(node
);
5281 return carry_dependences(node
, graph
);
5284 filters
= extract_sccs(ctx
, graph
);
5285 node
= isl_schedule_node_insert_sequence(node
, filters
);
5290 /* Are there any (non-empty) (conditional) validity edges in the graph?
5292 static int has_validity_edges(struct isl_sched_graph
*graph
)
5296 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5299 empty
= isl_map_plain_is_empty(graph
->edge
[i
].map
);
5304 if (is_any_validity(&graph
->edge
[i
]))
5311 /* Should we apply a Feautrier step?
5312 * That is, did the user request the Feautrier algorithm and are
5313 * there any validity dependences (left)?
5315 static int need_feautrier_step(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
5317 if (ctx
->opt
->schedule_algorithm
!= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
)
5320 return has_validity_edges(graph
);
5323 /* Compute a schedule for a connected dependence graph using Feautrier's
5324 * multi-dimensional scheduling algorithm and return the updated schedule node.
5326 * The original algorithm is described in [1].
5327 * The main idea is to minimize the number of scheduling dimensions, by
5328 * trying to satisfy as many dependences as possible per scheduling dimension.
5330 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
5331 * Problem, Part II: Multi-Dimensional Time.
5332 * In Intl. Journal of Parallel Programming, 1992.
5334 static __isl_give isl_schedule_node
*compute_schedule_wcc_feautrier(
5335 isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
5337 return carry_feautrier(node
, graph
);
5340 /* Turn off the "local" bit on all (condition) edges.
5342 static void clear_local_edges(struct isl_sched_graph
*graph
)
5346 for (i
= 0; i
< graph
->n_edge
; ++i
)
5347 if (is_condition(&graph
->edge
[i
]))
5348 clear_local(&graph
->edge
[i
]);
5351 /* Does "graph" have both condition and conditional validity edges?
5353 static int need_condition_check(struct isl_sched_graph
*graph
)
5356 int any_condition
= 0;
5357 int any_conditional_validity
= 0;
5359 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5360 if (is_condition(&graph
->edge
[i
]))
5362 if (is_conditional_validity(&graph
->edge
[i
]))
5363 any_conditional_validity
= 1;
5366 return any_condition
&& any_conditional_validity
;
5369 /* Does "graph" contain any coincidence edge?
5371 static int has_any_coincidence(struct isl_sched_graph
*graph
)
5375 for (i
= 0; i
< graph
->n_edge
; ++i
)
5376 if (is_coincidence(&graph
->edge
[i
]))
5382 /* Extract the final schedule row as a map with the iteration domain
5383 * of "node" as domain.
5385 static __isl_give isl_map
*final_row(struct isl_sched_node
*node
)
5390 row
= isl_mat_rows(node
->sched
) - 1;
5391 ma
= node_extract_partial_schedule_multi_aff(node
, row
, 1);
5392 return isl_map_from_multi_aff(ma
);
5395 /* Is the conditional validity dependence in the edge with index "edge_index"
5396 * violated by the latest (i.e., final) row of the schedule?
5397 * That is, is i scheduled after j
5398 * for any conditional validity dependence i -> j?
5400 static int is_violated(struct isl_sched_graph
*graph
, int edge_index
)
5402 isl_map
*src_sched
, *dst_sched
, *map
;
5403 struct isl_sched_edge
*edge
= &graph
->edge
[edge_index
];
5406 src_sched
= final_row(edge
->src
);
5407 dst_sched
= final_row(edge
->dst
);
5408 map
= isl_map_copy(edge
->map
);
5409 map
= isl_map_apply_domain(map
, src_sched
);
5410 map
= isl_map_apply_range(map
, dst_sched
);
5411 map
= isl_map_order_gt(map
, isl_dim_in
, 0, isl_dim_out
, 0);
5412 empty
= isl_map_is_empty(map
);
5421 /* Does "graph" have any satisfied condition edges that
5422 * are adjacent to the conditional validity constraint with
5423 * domain "conditional_source" and range "conditional_sink"?
5425 * A satisfied condition is one that is not local.
5426 * If a condition was forced to be local already (i.e., marked as local)
5427 * then there is no need to check if it is in fact local.
5429 * Additionally, mark all adjacent condition edges found as local.
5431 static int has_adjacent_true_conditions(struct isl_sched_graph
*graph
,
5432 __isl_keep isl_union_set
*conditional_source
,
5433 __isl_keep isl_union_set
*conditional_sink
)
5438 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5439 int adjacent
, local
;
5440 isl_union_map
*condition
;
5442 if (!is_condition(&graph
->edge
[i
]))
5444 if (is_local(&graph
->edge
[i
]))
5447 condition
= graph
->edge
[i
].tagged_condition
;
5448 adjacent
= domain_intersects(condition
, conditional_sink
);
5449 if (adjacent
>= 0 && !adjacent
)
5450 adjacent
= range_intersects(condition
,
5451 conditional_source
);
5457 set_local(&graph
->edge
[i
]);
5459 local
= is_condition_false(&graph
->edge
[i
]);
5469 /* Are there any violated conditional validity dependences with
5470 * adjacent condition dependences that are not local with respect
5471 * to the current schedule?
5472 * That is, is the conditional validity constraint violated?
5474 * Additionally, mark all those adjacent condition dependences as local.
5475 * We also mark those adjacent condition dependences that were not marked
5476 * as local before, but just happened to be local already. This ensures
5477 * that they remain local if the schedule is recomputed.
5479 * We first collect domain and range of all violated conditional validity
5480 * dependences and then check if there are any adjacent non-local
5481 * condition dependences.
5483 static int has_violated_conditional_constraint(isl_ctx
*ctx
,
5484 struct isl_sched_graph
*graph
)
5488 isl_union_set
*source
, *sink
;
5490 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
5491 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
5492 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5493 isl_union_set
*uset
;
5494 isl_union_map
*umap
;
5497 if (!is_conditional_validity(&graph
->edge
[i
]))
5500 violated
= is_violated(graph
, i
);
5508 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
5509 uset
= isl_union_map_domain(umap
);
5510 source
= isl_union_set_union(source
, uset
);
5511 source
= isl_union_set_coalesce(source
);
5513 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
5514 uset
= isl_union_map_range(umap
);
5515 sink
= isl_union_set_union(sink
, uset
);
5516 sink
= isl_union_set_coalesce(sink
);
5520 any
= has_adjacent_true_conditions(graph
, source
, sink
);
5522 isl_union_set_free(source
);
5523 isl_union_set_free(sink
);
5526 isl_union_set_free(source
);
5527 isl_union_set_free(sink
);
5531 /* Examine the current band (the rows between graph->band_start and
5532 * graph->n_total_row), deciding whether to drop it or add it to "node"
5533 * and then continue with the computation of the next band, if any.
5534 * If "initialized" is set, then it may be assumed that compute_maxvar
5535 * has been called on the current band. Otherwise, call
5536 * compute_maxvar if and before carry_dependences gets called.
5538 * The caller keeps looking for a new row as long as
5539 * graph->n_row < graph->maxvar. If the latest attempt to find
5540 * such a row failed (i.e., we still have graph->n_row < graph->maxvar),
5542 * - split between SCCs and start over (assuming we found an interesting
5543 * pair of SCCs between which to split)
5544 * - continue with the next band (assuming the current band has at least
5546 * - if there is more than one SCC left, then split along all SCCs
5547 * - if outer coincidence needs to be enforced, then try to carry as many
5548 * validity or coincidence dependences as possible and
5549 * continue with the next band
5550 * - try to carry as many validity dependences as possible and
5551 * continue with the next band
5552 * In each case, we first insert a band node in the schedule tree
5553 * if any rows have been computed.
5555 * If the caller managed to complete the schedule and the current band
5556 * is empty, then finish off by topologically
5557 * sorting the statements based on the remaining dependences.
5558 * If, on the other hand, the current band has at least one row,
5559 * then continue with the next band. Note that this next band
5560 * will necessarily be empty, but the graph may still be split up
5561 * into weakly connected components before arriving back here.
5563 static __isl_give isl_schedule_node
*compute_schedule_finish_band(
5564 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
5572 empty
= graph
->n_total_row
== graph
->band_start
;
5573 if (graph
->n_row
< graph
->maxvar
) {
5576 ctx
= isl_schedule_node_get_ctx(node
);
5577 if (!ctx
->opt
->schedule_maximize_band_depth
&& !empty
)
5578 return compute_next_band(node
, graph
, 1);
5579 if (graph
->src_scc
>= 0)
5580 return compute_split_schedule(node
, graph
);
5582 return compute_next_band(node
, graph
, 1);
5584 return compute_component_schedule(node
, graph
, 1);
5585 if (!initialized
&& compute_maxvar(graph
) < 0)
5586 return isl_schedule_node_free(node
);
5587 if (isl_options_get_schedule_outer_coincidence(ctx
))
5588 return carry_coincidence(node
, graph
);
5589 return carry_dependences(node
, graph
);
5593 return compute_next_band(node
, graph
, 1);
5594 return sort_statements(node
, graph
, initialized
);
5597 /* Construct a band of schedule rows for a connected dependence graph.
5598 * The caller is responsible for determining the strongly connected
5599 * components and calling compute_maxvar first.
5601 * We try to find a sequence of as many schedule rows as possible that result
5602 * in non-negative dependence distances (independent of the previous rows
5603 * in the sequence, i.e., such that the sequence is tilable), with as
5604 * many of the initial rows as possible satisfying the coincidence constraints.
5605 * The computation stops if we can't find any more rows or if we have found
5606 * all the rows we wanted to find.
5608 * If ctx->opt->schedule_outer_coincidence is set, then we force the
5609 * outermost dimension to satisfy the coincidence constraints. If this
5610 * turns out to be impossible, we fall back on the general scheme above
5611 * and try to carry as many dependences as possible.
5613 * If "graph" contains both condition and conditional validity dependences,
5614 * then we need to check that that the conditional schedule constraint
5615 * is satisfied, i.e., there are no violated conditional validity dependences
5616 * that are adjacent to any non-local condition dependences.
5617 * If there are, then we mark all those adjacent condition dependences
5618 * as local and recompute the current band. Those dependences that
5619 * are marked local will then be forced to be local.
5620 * The initial computation is performed with no dependences marked as local.
5621 * If we are lucky, then there will be no violated conditional validity
5622 * dependences adjacent to any non-local condition dependences.
5623 * Otherwise, we mark some additional condition dependences as local and
5624 * recompute. We continue this process until there are no violations left or
5625 * until we are no longer able to compute a schedule.
5626 * Since there are only a finite number of dependences,
5627 * there will only be a finite number of iterations.
5629 static isl_stat
compute_schedule_wcc_band(isl_ctx
*ctx
,
5630 struct isl_sched_graph
*graph
)
5632 int has_coincidence
;
5633 int use_coincidence
;
5634 int force_coincidence
= 0;
5635 int check_conditional
;
5637 if (sort_sccs(graph
) < 0)
5638 return isl_stat_error
;
5640 clear_local_edges(graph
);
5641 check_conditional
= need_condition_check(graph
);
5642 has_coincidence
= has_any_coincidence(graph
);
5644 if (ctx
->opt
->schedule_outer_coincidence
)
5645 force_coincidence
= 1;
5647 use_coincidence
= has_coincidence
;
5648 while (graph
->n_row
< graph
->maxvar
) {
5653 graph
->src_scc
= -1;
5654 graph
->dst_scc
= -1;
5656 if (setup_lp(ctx
, graph
, use_coincidence
) < 0)
5657 return isl_stat_error
;
5658 sol
= solve_lp(ctx
, graph
);
5660 return isl_stat_error
;
5661 if (sol
->size
== 0) {
5662 int empty
= graph
->n_total_row
== graph
->band_start
;
5665 if (use_coincidence
&& (!force_coincidence
|| !empty
)) {
5666 use_coincidence
= 0;
5671 coincident
= !has_coincidence
|| use_coincidence
;
5672 if (update_schedule(graph
, sol
, coincident
) < 0)
5673 return isl_stat_error
;
5675 if (!check_conditional
)
5677 violated
= has_violated_conditional_constraint(ctx
, graph
);
5679 return isl_stat_error
;
5682 if (reset_band(graph
) < 0)
5683 return isl_stat_error
;
5684 use_coincidence
= has_coincidence
;
5690 /* Compute a schedule for a connected dependence graph by considering
5691 * the graph as a whole and return the updated schedule node.
5693 * The actual schedule rows of the current band are computed by
5694 * compute_schedule_wcc_band. compute_schedule_finish_band takes
5695 * care of integrating the band into "node" and continuing
5698 static __isl_give isl_schedule_node
*compute_schedule_wcc_whole(
5699 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
5706 ctx
= isl_schedule_node_get_ctx(node
);
5707 if (compute_schedule_wcc_band(ctx
, graph
) < 0)
5708 return isl_schedule_node_free(node
);
5710 return compute_schedule_finish_band(node
, graph
, 1);
5713 /* Clustering information used by compute_schedule_wcc_clustering.
5715 * "n" is the number of SCCs in the original dependence graph
5716 * "scc" is an array of "n" elements, each representing an SCC
5717 * of the original dependence graph. All entries in the same cluster
5718 * have the same number of schedule rows.
5719 * "scc_cluster" maps each SCC index to the cluster to which it belongs,
5720 * where each cluster is represented by the index of the first SCC
5721 * in the cluster. Initially, each SCC belongs to a cluster containing
5724 * "scc_in_merge" is used by merge_clusters_along_edge to keep
5725 * track of which SCCs need to be merged.
5727 * "cluster" contains the merged clusters of SCCs after the clustering
5730 * "scc_node" is a temporary data structure used inside copy_partial.
5731 * For each SCC, it keeps track of the number of nodes in the SCC
5732 * that have already been copied.
5734 struct isl_clustering
{
5736 struct isl_sched_graph
*scc
;
5737 struct isl_sched_graph
*cluster
;
5743 /* Initialize the clustering data structure "c" from "graph".
5745 * In particular, allocate memory, extract the SCCs from "graph"
5746 * into c->scc, initialize scc_cluster and construct
5747 * a band of schedule rows for each SCC.
5748 * Within each SCC, there is only one SCC by definition.
5749 * Each SCC initially belongs to a cluster containing only that SCC.
5751 static isl_stat
clustering_init(isl_ctx
*ctx
, struct isl_clustering
*c
,
5752 struct isl_sched_graph
*graph
)
5757 c
->scc
= isl_calloc_array(ctx
, struct isl_sched_graph
, c
->n
);
5758 c
->cluster
= isl_calloc_array(ctx
, struct isl_sched_graph
, c
->n
);
5759 c
->scc_cluster
= isl_calloc_array(ctx
, int, c
->n
);
5760 c
->scc_node
= isl_calloc_array(ctx
, int, c
->n
);
5761 c
->scc_in_merge
= isl_calloc_array(ctx
, int, c
->n
);
5762 if (!c
->scc
|| !c
->cluster
||
5763 !c
->scc_cluster
|| !c
->scc_node
|| !c
->scc_in_merge
)
5764 return isl_stat_error
;
5766 for (i
= 0; i
< c
->n
; ++i
) {
5767 if (extract_sub_graph(ctx
, graph
, &node_scc_exactly
,
5768 &edge_scc_exactly
, i
, &c
->scc
[i
]) < 0)
5769 return isl_stat_error
;
5771 if (compute_maxvar(&c
->scc
[i
]) < 0)
5772 return isl_stat_error
;
5773 if (compute_schedule_wcc_band(ctx
, &c
->scc
[i
]) < 0)
5774 return isl_stat_error
;
5775 c
->scc_cluster
[i
] = i
;
5781 /* Free all memory allocated for "c".
5783 static void clustering_free(isl_ctx
*ctx
, struct isl_clustering
*c
)
5788 for (i
= 0; i
< c
->n
; ++i
)
5789 graph_free(ctx
, &c
->scc
[i
]);
5792 for (i
= 0; i
< c
->n
; ++i
)
5793 graph_free(ctx
, &c
->cluster
[i
]);
5795 free(c
->scc_cluster
);
5797 free(c
->scc_in_merge
);
5800 /* Should we refrain from merging the cluster in "graph" with
5801 * any other cluster?
5802 * In particular, is its current schedule band empty and incomplete.
5804 static int bad_cluster(struct isl_sched_graph
*graph
)
5806 return graph
->n_row
< graph
->maxvar
&&
5807 graph
->n_total_row
== graph
->band_start
;
5810 /* Is "edge" a proximity edge with a non-empty dependence relation?
5812 static isl_bool
is_non_empty_proximity(struct isl_sched_edge
*edge
)
5814 if (!is_proximity(edge
))
5815 return isl_bool_false
;
5816 return isl_bool_not(isl_map_plain_is_empty(edge
->map
));
5819 /* Return the index of an edge in "graph" that can be used to merge
5820 * two clusters in "c".
5821 * Return graph->n_edge if no such edge can be found.
5822 * Return -1 on error.
5824 * In particular, return a proximity edge between two clusters
5825 * that is not marked "no_merge" and such that neither of the
5826 * two clusters has an incomplete, empty band.
5828 * If there are multiple such edges, then try and find the most
5829 * appropriate edge to use for merging. In particular, pick the edge
5830 * with the greatest weight. If there are multiple of those,
5831 * then pick one with the shortest distance between
5832 * the two cluster representatives.
5834 static int find_proximity(struct isl_sched_graph
*graph
,
5835 struct isl_clustering
*c
)
5837 int i
, best
= graph
->n_edge
, best_dist
, best_weight
;
5839 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5840 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
5844 prox
= is_non_empty_proximity(edge
);
5851 if (bad_cluster(&c
->scc
[edge
->src
->scc
]) ||
5852 bad_cluster(&c
->scc
[edge
->dst
->scc
]))
5854 dist
= c
->scc_cluster
[edge
->dst
->scc
] -
5855 c
->scc_cluster
[edge
->src
->scc
];
5858 weight
= edge
->weight
;
5859 if (best
< graph
->n_edge
) {
5860 if (best_weight
> weight
)
5862 if (best_weight
== weight
&& best_dist
<= dist
)
5867 best_weight
= weight
;
5873 /* Internal data structure used in mark_merge_sccs.
5875 * "graph" is the dependence graph in which a strongly connected
5876 * component is constructed.
5877 * "scc_cluster" maps each SCC index to the cluster to which it belongs.
5878 * "src" and "dst" are the indices of the nodes that are being merged.
5880 struct isl_mark_merge_sccs_data
{
5881 struct isl_sched_graph
*graph
;
5887 /* Check whether the cluster containing node "i" depends on the cluster
5888 * containing node "j". If "i" and "j" belong to the same cluster,
5889 * then they are taken to depend on each other to ensure that
5890 * the resulting strongly connected component consists of complete
5891 * clusters. Furthermore, if "i" and "j" are the two nodes that
5892 * are being merged, then they are taken to depend on each other as well.
5893 * Otherwise, check if there is a (conditional) validity dependence
5894 * from node[j] to node[i], forcing node[i] to follow node[j].
5896 static isl_bool
cluster_follows(int i
, int j
, void *user
)
5898 struct isl_mark_merge_sccs_data
*data
= user
;
5899 struct isl_sched_graph
*graph
= data
->graph
;
5900 int *scc_cluster
= data
->scc_cluster
;
5902 if (data
->src
== i
&& data
->dst
== j
)
5903 return isl_bool_true
;
5904 if (data
->src
== j
&& data
->dst
== i
)
5905 return isl_bool_true
;
5906 if (scc_cluster
[graph
->node
[i
].scc
] == scc_cluster
[graph
->node
[j
].scc
])
5907 return isl_bool_true
;
5909 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
5912 /* Mark all SCCs that belong to either of the two clusters in "c"
5913 * connected by the edge in "graph" with index "edge", or to any
5914 * of the intermediate clusters.
5915 * The marking is recorded in c->scc_in_merge.
5917 * The given edge has been selected for merging two clusters,
5918 * meaning that there is at least a proximity edge between the two nodes.
5919 * However, there may also be (indirect) validity dependences
5920 * between the two nodes. When merging the two clusters, all clusters
5921 * containing one or more of the intermediate nodes along the
5922 * indirect validity dependences need to be merged in as well.
5924 * First collect all such nodes by computing the strongly connected
5925 * component (SCC) containing the two nodes connected by the edge, where
5926 * the two nodes are considered to depend on each other to make
5927 * sure they end up in the same SCC. Similarly, each node is considered
5928 * to depend on every other node in the same cluster to ensure
5929 * that the SCC consists of complete clusters.
5931 * Then the original SCCs that contain any of these nodes are marked
5932 * in c->scc_in_merge.
5934 static isl_stat
mark_merge_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5935 int edge
, struct isl_clustering
*c
)
5937 struct isl_mark_merge_sccs_data data
;
5938 struct isl_tarjan_graph
*g
;
5941 for (i
= 0; i
< c
->n
; ++i
)
5942 c
->scc_in_merge
[i
] = 0;
5945 data
.scc_cluster
= c
->scc_cluster
;
5946 data
.src
= graph
->edge
[edge
].src
- graph
->node
;
5947 data
.dst
= graph
->edge
[edge
].dst
- graph
->node
;
5949 g
= isl_tarjan_graph_component(ctx
, graph
->n
, data
.dst
,
5950 &cluster_follows
, &data
);
5956 isl_die(ctx
, isl_error_internal
,
5957 "expecting at least two nodes in component",
5959 if (g
->order
[--i
] != -1)
5960 isl_die(ctx
, isl_error_internal
,
5961 "expecting end of component marker", goto error
);
5963 for (--i
; i
>= 0 && g
->order
[i
] != -1; --i
) {
5964 int scc
= graph
->node
[g
->order
[i
]].scc
;
5965 c
->scc_in_merge
[scc
] = 1;
5968 isl_tarjan_graph_free(g
);
5971 isl_tarjan_graph_free(g
);
5972 return isl_stat_error
;
5975 /* Construct the identifier "cluster_i".
5977 static __isl_give isl_id
*cluster_id(isl_ctx
*ctx
, int i
)
5981 snprintf(name
, sizeof(name
), "cluster_%d", i
);
5982 return isl_id_alloc(ctx
, name
, NULL
);
5985 /* Construct the space of the cluster with index "i" containing
5986 * the strongly connected component "scc".
5988 * In particular, construct a space called cluster_i with dimension equal
5989 * to the number of schedule rows in the current band of "scc".
5991 static __isl_give isl_space
*cluster_space(struct isl_sched_graph
*scc
, int i
)
5997 nvar
= scc
->n_total_row
- scc
->band_start
;
5998 space
= isl_space_copy(scc
->node
[0].space
);
5999 space
= isl_space_params(space
);
6000 space
= isl_space_set_from_params(space
);
6001 space
= isl_space_add_dims(space
, isl_dim_set
, nvar
);
6002 id
= cluster_id(isl_space_get_ctx(space
), i
);
6003 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
6008 /* Collect the domain of the graph for merging clusters.
6010 * In particular, for each cluster with first SCC "i", construct
6011 * a set in the space called cluster_i with dimension equal
6012 * to the number of schedule rows in the current band of the cluster.
6014 static __isl_give isl_union_set
*collect_domain(isl_ctx
*ctx
,
6015 struct isl_sched_graph
*graph
, struct isl_clustering
*c
)
6019 isl_union_set
*domain
;
6021 space
= isl_space_params_alloc(ctx
, 0);
6022 domain
= isl_union_set_empty(space
);
6024 for (i
= 0; i
< graph
->scc
; ++i
) {
6027 if (!c
->scc_in_merge
[i
])
6029 if (c
->scc_cluster
[i
] != i
)
6031 space
= cluster_space(&c
->scc
[i
], i
);
6032 domain
= isl_union_set_add_set(domain
, isl_set_universe(space
));
6038 /* Construct a map from the original instances to the corresponding
6039 * cluster instance in the current bands of the clusters in "c".
6041 static __isl_give isl_union_map
*collect_cluster_map(isl_ctx
*ctx
,
6042 struct isl_sched_graph
*graph
, struct isl_clustering
*c
)
6046 isl_union_map
*cluster_map
;
6048 space
= isl_space_params_alloc(ctx
, 0);
6049 cluster_map
= isl_union_map_empty(space
);
6050 for (i
= 0; i
< graph
->scc
; ++i
) {
6054 if (!c
->scc_in_merge
[i
])
6057 id
= cluster_id(ctx
, c
->scc_cluster
[i
]);
6058 start
= c
->scc
[i
].band_start
;
6059 n
= c
->scc
[i
].n_total_row
- start
;
6060 for (j
= 0; j
< c
->scc
[i
].n
; ++j
) {
6063 struct isl_sched_node
*node
= &c
->scc
[i
].node
[j
];
6065 ma
= node_extract_partial_schedule_multi_aff(node
,
6067 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
,
6069 map
= isl_map_from_multi_aff(ma
);
6070 cluster_map
= isl_union_map_add_map(cluster_map
, map
);
6078 /* Add "umap" to the schedule constraints "sc" of all types of "edge"
6079 * that are not isl_edge_condition or isl_edge_conditional_validity.
6081 static __isl_give isl_schedule_constraints
*add_non_conditional_constraints(
6082 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*umap
,
6083 __isl_take isl_schedule_constraints
*sc
)
6085 enum isl_edge_type t
;
6090 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
6091 if (t
== isl_edge_condition
||
6092 t
== isl_edge_conditional_validity
)
6094 if (!is_type(edge
, t
))
6096 sc
= isl_schedule_constraints_add(sc
, t
,
6097 isl_union_map_copy(umap
));
6103 /* Add schedule constraints of types isl_edge_condition and
6104 * isl_edge_conditional_validity to "sc" by applying "umap" to
6105 * the domains of the wrapped relations in domain and range
6106 * of the corresponding tagged constraints of "edge".
6108 static __isl_give isl_schedule_constraints
*add_conditional_constraints(
6109 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*umap
,
6110 __isl_take isl_schedule_constraints
*sc
)
6112 enum isl_edge_type t
;
6113 isl_union_map
*tagged
;
6115 for (t
= isl_edge_condition
; t
<= isl_edge_conditional_validity
; ++t
) {
6116 if (!is_type(edge
, t
))
6118 if (t
== isl_edge_condition
)
6119 tagged
= isl_union_map_copy(edge
->tagged_condition
);
6121 tagged
= isl_union_map_copy(edge
->tagged_validity
);
6122 tagged
= isl_union_map_zip(tagged
);
6123 tagged
= isl_union_map_apply_domain(tagged
,
6124 isl_union_map_copy(umap
));
6125 tagged
= isl_union_map_zip(tagged
);
6126 sc
= isl_schedule_constraints_add(sc
, t
, tagged
);
6134 /* Given a mapping "cluster_map" from the original instances to
6135 * the cluster instances, add schedule constraints on the clusters
6136 * to "sc" corresponding to the original constraints represented by "edge".
6138 * For non-tagged dependence constraints, the cluster constraints
6139 * are obtained by applying "cluster_map" to the edge->map.
6141 * For tagged dependence constraints, "cluster_map" needs to be applied
6142 * to the domains of the wrapped relations in domain and range
6143 * of the tagged dependence constraints. Pick out the mappings
6144 * from these domains from "cluster_map" and construct their product.
6145 * This mapping can then be applied to the pair of domains.
6147 static __isl_give isl_schedule_constraints
*collect_edge_constraints(
6148 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*cluster_map
,
6149 __isl_take isl_schedule_constraints
*sc
)
6151 isl_union_map
*umap
;
6153 isl_union_set
*uset
;
6154 isl_union_map
*umap1
, *umap2
;
6159 umap
= isl_union_map_from_map(isl_map_copy(edge
->map
));
6160 umap
= isl_union_map_apply_domain(umap
,
6161 isl_union_map_copy(cluster_map
));
6162 umap
= isl_union_map_apply_range(umap
,
6163 isl_union_map_copy(cluster_map
));
6164 sc
= add_non_conditional_constraints(edge
, umap
, sc
);
6165 isl_union_map_free(umap
);
6167 if (!sc
|| (!is_condition(edge
) && !is_conditional_validity(edge
)))
6170 space
= isl_space_domain(isl_map_get_space(edge
->map
));
6171 uset
= isl_union_set_from_set(isl_set_universe(space
));
6172 umap1
= isl_union_map_copy(cluster_map
);
6173 umap1
= isl_union_map_intersect_domain(umap1
, uset
);
6174 space
= isl_space_range(isl_map_get_space(edge
->map
));
6175 uset
= isl_union_set_from_set(isl_set_universe(space
));
6176 umap2
= isl_union_map_copy(cluster_map
);
6177 umap2
= isl_union_map_intersect_domain(umap2
, uset
);
6178 umap
= isl_union_map_product(umap1
, umap2
);
6180 sc
= add_conditional_constraints(edge
, umap
, sc
);
6182 isl_union_map_free(umap
);
6186 /* Given a mapping "cluster_map" from the original instances to
6187 * the cluster instances, add schedule constraints on the clusters
6188 * to "sc" corresponding to all edges in "graph" between nodes that
6189 * belong to SCCs that are marked for merging in "scc_in_merge".
6191 static __isl_give isl_schedule_constraints
*collect_constraints(
6192 struct isl_sched_graph
*graph
, int *scc_in_merge
,
6193 __isl_keep isl_union_map
*cluster_map
,
6194 __isl_take isl_schedule_constraints
*sc
)
6198 for (i
= 0; i
< graph
->n_edge
; ++i
) {
6199 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
6201 if (!scc_in_merge
[edge
->src
->scc
])
6203 if (!scc_in_merge
[edge
->dst
->scc
])
6205 sc
= collect_edge_constraints(edge
, cluster_map
, sc
);
6211 /* Construct a dependence graph for scheduling clusters with respect
6212 * to each other and store the result in "merge_graph".
6213 * In particular, the nodes of the graph correspond to the schedule
6214 * dimensions of the current bands of those clusters that have been
6215 * marked for merging in "c".
6217 * First construct an isl_schedule_constraints object for this domain
6218 * by transforming the edges in "graph" to the domain.
6219 * Then initialize a dependence graph for scheduling from these
6222 static isl_stat
init_merge_graph(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6223 struct isl_clustering
*c
, struct isl_sched_graph
*merge_graph
)
6225 isl_union_set
*domain
;
6226 isl_union_map
*cluster_map
;
6227 isl_schedule_constraints
*sc
;
6230 domain
= collect_domain(ctx
, graph
, c
);
6231 sc
= isl_schedule_constraints_on_domain(domain
);
6233 return isl_stat_error
;
6234 cluster_map
= collect_cluster_map(ctx
, graph
, c
);
6235 sc
= collect_constraints(graph
, c
->scc_in_merge
, cluster_map
, sc
);
6236 isl_union_map_free(cluster_map
);
6238 r
= graph_init(merge_graph
, sc
);
6240 isl_schedule_constraints_free(sc
);
6245 /* Compute the maximal number of remaining schedule rows that still need
6246 * to be computed for the nodes that belong to clusters with the maximal
6247 * dimension for the current band (i.e., the band that is to be merged).
6248 * Only clusters that are about to be merged are considered.
6249 * "maxvar" is the maximal dimension for the current band.
6250 * "c" contains information about the clusters.
6252 * Return the maximal number of remaining schedule rows or -1 on error.
6254 static int compute_maxvar_max_slack(int maxvar
, struct isl_clustering
*c
)
6260 for (i
= 0; i
< c
->n
; ++i
) {
6262 struct isl_sched_graph
*scc
;
6264 if (!c
->scc_in_merge
[i
])
6267 nvar
= scc
->n_total_row
- scc
->band_start
;
6270 for (j
= 0; j
< scc
->n
; ++j
) {
6271 struct isl_sched_node
*node
= &scc
->node
[j
];
6274 if (node_update_vmap(node
) < 0)
6276 slack
= node
->nvar
- node
->rank
;
6277 if (slack
> max_slack
)
6285 /* If there are any clusters where the dimension of the current band
6286 * (i.e., the band that is to be merged) is smaller than "maxvar" and
6287 * if there are any nodes in such a cluster where the number
6288 * of remaining schedule rows that still need to be computed
6289 * is greater than "max_slack", then return the smallest current band
6290 * dimension of all these clusters. Otherwise return the original value
6291 * of "maxvar". Return -1 in case of any error.
6292 * Only clusters that are about to be merged are considered.
6293 * "c" contains information about the clusters.
6295 static int limit_maxvar_to_slack(int maxvar
, int max_slack
,
6296 struct isl_clustering
*c
)
6300 for (i
= 0; i
< c
->n
; ++i
) {
6302 struct isl_sched_graph
*scc
;
6304 if (!c
->scc_in_merge
[i
])
6307 nvar
= scc
->n_total_row
- scc
->band_start
;
6310 for (j
= 0; j
< scc
->n
; ++j
) {
6311 struct isl_sched_node
*node
= &scc
->node
[j
];
6314 if (node_update_vmap(node
) < 0)
6316 slack
= node
->nvar
- node
->rank
;
6317 if (slack
> max_slack
) {
6327 /* Adjust merge_graph->maxvar based on the number of remaining schedule rows
6328 * that still need to be computed. In particular, if there is a node
6329 * in a cluster where the dimension of the current band is smaller
6330 * than merge_graph->maxvar, but the number of remaining schedule rows
6331 * is greater than that of any node in a cluster with the maximal
6332 * dimension for the current band (i.e., merge_graph->maxvar),
6333 * then adjust merge_graph->maxvar to the (smallest) current band dimension
6334 * of those clusters. Without this adjustment, the total number of
6335 * schedule dimensions would be increased, resulting in a skewed view
6336 * of the number of coincident dimensions.
6337 * "c" contains information about the clusters.
6339 * If the maximize_band_depth option is set and merge_graph->maxvar is reduced,
6340 * then there is no point in attempting any merge since it will be rejected
6341 * anyway. Set merge_graph->maxvar to zero in such cases.
6343 static isl_stat
adjust_maxvar_to_slack(isl_ctx
*ctx
,
6344 struct isl_sched_graph
*merge_graph
, struct isl_clustering
*c
)
6346 int max_slack
, maxvar
;
6348 max_slack
= compute_maxvar_max_slack(merge_graph
->maxvar
, c
);
6350 return isl_stat_error
;
6351 maxvar
= limit_maxvar_to_slack(merge_graph
->maxvar
, max_slack
, c
);
6353 return isl_stat_error
;
6355 if (maxvar
< merge_graph
->maxvar
) {
6356 if (isl_options_get_schedule_maximize_band_depth(ctx
))
6357 merge_graph
->maxvar
= 0;
6359 merge_graph
->maxvar
= maxvar
;
6365 /* Return the number of coincident dimensions in the current band of "graph",
6366 * where the nodes of "graph" are assumed to be scheduled by a single band.
6368 static int get_n_coincident(struct isl_sched_graph
*graph
)
6372 for (i
= graph
->band_start
; i
< graph
->n_total_row
; ++i
)
6373 if (!graph
->node
[0].coincident
[i
])
6376 return i
- graph
->band_start
;
6379 /* Should the clusters be merged based on the cluster schedule
6380 * in the current (and only) band of "merge_graph", given that
6381 * coincidence should be maximized?
6383 * If the number of coincident schedule dimensions in the merged band
6384 * would be less than the maximal number of coincident schedule dimensions
6385 * in any of the merged clusters, then the clusters should not be merged.
6387 static isl_bool
ok_to_merge_coincident(struct isl_clustering
*c
,
6388 struct isl_sched_graph
*merge_graph
)
6395 for (i
= 0; i
< c
->n
; ++i
) {
6396 if (!c
->scc_in_merge
[i
])
6398 n_coincident
= get_n_coincident(&c
->scc
[i
]);
6399 if (n_coincident
> max_coincident
)
6400 max_coincident
= n_coincident
;
6403 n_coincident
= get_n_coincident(merge_graph
);
6405 return n_coincident
>= max_coincident
;
6408 /* Return the transformation on "node" expressed by the current (and only)
6409 * band of "merge_graph" applied to the clusters in "c".
6411 * First find the representation of "node" in its SCC in "c" and
6412 * extract the transformation expressed by the current band.
6413 * Then extract the transformation applied by "merge_graph"
6414 * to the cluster to which this SCC belongs.
6415 * Combine the two to obtain the complete transformation on the node.
6417 * Note that the range of the first transformation is an anonymous space,
6418 * while the domain of the second is named "cluster_X". The range
6419 * of the former therefore needs to be adjusted before the two
6422 static __isl_give isl_map
*extract_node_transformation(isl_ctx
*ctx
,
6423 struct isl_sched_node
*node
, struct isl_clustering
*c
,
6424 struct isl_sched_graph
*merge_graph
)
6426 struct isl_sched_node
*scc_node
, *cluster_node
;
6430 isl_multi_aff
*ma
, *ma2
;
6432 scc_node
= graph_find_node(ctx
, &c
->scc
[node
->scc
], node
->space
);
6433 if (scc_node
&& !is_node(&c
->scc
[node
->scc
], scc_node
))
6434 isl_die(ctx
, isl_error_internal
, "unable to find node",
6436 start
= c
->scc
[node
->scc
].band_start
;
6437 n
= c
->scc
[node
->scc
].n_total_row
- start
;
6438 ma
= node_extract_partial_schedule_multi_aff(scc_node
, start
, n
);
6439 space
= cluster_space(&c
->scc
[node
->scc
], c
->scc_cluster
[node
->scc
]);
6440 cluster_node
= graph_find_node(ctx
, merge_graph
, space
);
6441 if (cluster_node
&& !is_node(merge_graph
, cluster_node
))
6442 isl_die(ctx
, isl_error_internal
, "unable to find cluster",
6443 space
= isl_space_free(space
));
6444 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
6445 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
, id
);
6446 isl_space_free(space
);
6447 n
= merge_graph
->n_total_row
;
6448 ma2
= node_extract_partial_schedule_multi_aff(cluster_node
, 0, n
);
6449 ma
= isl_multi_aff_pullback_multi_aff(ma2
, ma
);
6451 return isl_map_from_multi_aff(ma
);
6454 /* Give a set of distances "set", are they bounded by a small constant
6455 * in direction "pos"?
6456 * In practice, check if they are bounded by 2 by checking that there
6457 * are no elements with a value greater than or equal to 3 or
6458 * smaller than or equal to -3.
6460 static isl_bool
distance_is_bounded(__isl_keep isl_set
*set
, int pos
)
6466 return isl_bool_error
;
6468 test
= isl_set_copy(set
);
6469 test
= isl_set_lower_bound_si(test
, isl_dim_set
, pos
, 3);
6470 bounded
= isl_set_is_empty(test
);
6473 if (bounded
< 0 || !bounded
)
6476 test
= isl_set_copy(set
);
6477 test
= isl_set_upper_bound_si(test
, isl_dim_set
, pos
, -3);
6478 bounded
= isl_set_is_empty(test
);
6484 /* Does the set "set" have a fixed (but possible parametric) value
6485 * at dimension "pos"?
6487 static isl_bool
has_single_value(__isl_keep isl_set
*set
, int pos
)
6493 return isl_bool_error
;
6494 set
= isl_set_copy(set
);
6495 n
= isl_set_dim(set
, isl_dim_set
);
6496 set
= isl_set_project_out(set
, isl_dim_set
, pos
+ 1, n
- (pos
+ 1));
6497 set
= isl_set_project_out(set
, isl_dim_set
, 0, pos
);
6498 single
= isl_set_is_singleton(set
);
6504 /* Does "map" have a fixed (but possible parametric) value
6505 * at dimension "pos" of either its domain or its range?
6507 static isl_bool
has_singular_src_or_dst(__isl_keep isl_map
*map
, int pos
)
6512 set
= isl_map_domain(isl_map_copy(map
));
6513 single
= has_single_value(set
, pos
);
6516 if (single
< 0 || single
)
6519 set
= isl_map_range(isl_map_copy(map
));
6520 single
= has_single_value(set
, pos
);
6526 /* Does the edge "edge" from "graph" have bounded dependence distances
6527 * in the merged graph "merge_graph" of a selection of clusters in "c"?
6529 * Extract the complete transformations of the source and destination
6530 * nodes of the edge, apply them to the edge constraints and
6531 * compute the differences. Finally, check if these differences are bounded
6532 * in each direction.
6534 * If the dimension of the band is greater than the number of
6535 * dimensions that can be expected to be optimized by the edge
6536 * (based on its weight), then also allow the differences to be unbounded
6537 * in the remaining dimensions, but only if either the source or
6538 * the destination has a fixed value in that direction.
6539 * This allows a statement that produces values that are used by
6540 * several instances of another statement to be merged with that
6542 * However, merging such clusters will introduce an inherently
6543 * large proximity distance inside the merged cluster, meaning
6544 * that proximity distances will no longer be optimized in
6545 * subsequent merges. These merges are therefore only allowed
6546 * after all other possible merges have been tried.
6547 * The first time such a merge is encountered, the weight of the edge
6548 * is replaced by a negative weight. The second time (i.e., after
6549 * all merges over edges with a non-negative weight have been tried),
6550 * the merge is allowed.
6552 static isl_bool
has_bounded_distances(isl_ctx
*ctx
, struct isl_sched_edge
*edge
,
6553 struct isl_sched_graph
*graph
, struct isl_clustering
*c
,
6554 struct isl_sched_graph
*merge_graph
)
6561 map
= isl_map_copy(edge
->map
);
6562 t
= extract_node_transformation(ctx
, edge
->src
, c
, merge_graph
);
6563 map
= isl_map_apply_domain(map
, t
);
6564 t
= extract_node_transformation(ctx
, edge
->dst
, c
, merge_graph
);
6565 map
= isl_map_apply_range(map
, t
);
6566 dist
= isl_map_deltas(isl_map_copy(map
));
6568 bounded
= isl_bool_true
;
6569 n
= isl_set_dim(dist
, isl_dim_set
);
6570 n_slack
= n
- edge
->weight
;
6571 if (edge
->weight
< 0)
6572 n_slack
-= graph
->max_weight
+ 1;
6573 for (i
= 0; i
< n
; ++i
) {
6574 isl_bool bounded_i
, singular_i
;
6576 bounded_i
= distance_is_bounded(dist
, i
);
6581 if (edge
->weight
>= 0)
6582 bounded
= isl_bool_false
;
6586 singular_i
= has_singular_src_or_dst(map
, i
);
6591 bounded
= isl_bool_false
;
6594 if (!bounded
&& i
>= n
&& edge
->weight
>= 0)
6595 edge
->weight
-= graph
->max_weight
+ 1;
6603 return isl_bool_error
;
6606 /* Should the clusters be merged based on the cluster schedule
6607 * in the current (and only) band of "merge_graph"?
6608 * "graph" is the original dependence graph, while "c" records
6609 * which SCCs are involved in the latest merge.
6611 * In particular, is there at least one proximity constraint
6612 * that is optimized by the merge?
6614 * A proximity constraint is considered to be optimized
6615 * if the dependence distances are small.
6617 static isl_bool
ok_to_merge_proximity(isl_ctx
*ctx
,
6618 struct isl_sched_graph
*graph
, struct isl_clustering
*c
,
6619 struct isl_sched_graph
*merge_graph
)
6623 for (i
= 0; i
< graph
->n_edge
; ++i
) {
6624 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
6627 if (!is_proximity(edge
))
6629 if (!c
->scc_in_merge
[edge
->src
->scc
])
6631 if (!c
->scc_in_merge
[edge
->dst
->scc
])
6633 if (c
->scc_cluster
[edge
->dst
->scc
] ==
6634 c
->scc_cluster
[edge
->src
->scc
])
6636 bounded
= has_bounded_distances(ctx
, edge
, graph
, c
,
6638 if (bounded
< 0 || bounded
)
6642 return isl_bool_false
;
6645 /* Should the clusters be merged based on the cluster schedule
6646 * in the current (and only) band of "merge_graph"?
6647 * "graph" is the original dependence graph, while "c" records
6648 * which SCCs are involved in the latest merge.
6650 * If the current band is empty, then the clusters should not be merged.
6652 * If the band depth should be maximized and the merge schedule
6653 * is incomplete (meaning that the dimension of some of the schedule
6654 * bands in the original schedule will be reduced), then the clusters
6655 * should not be merged.
6657 * If the schedule_maximize_coincidence option is set, then check that
6658 * the number of coincident schedule dimensions is not reduced.
6660 * Finally, only allow the merge if at least one proximity
6661 * constraint is optimized.
6663 static isl_bool
ok_to_merge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6664 struct isl_clustering
*c
, struct isl_sched_graph
*merge_graph
)
6666 if (merge_graph
->n_total_row
== merge_graph
->band_start
)
6667 return isl_bool_false
;
6669 if (isl_options_get_schedule_maximize_band_depth(ctx
) &&
6670 merge_graph
->n_total_row
< merge_graph
->maxvar
)
6671 return isl_bool_false
;
6673 if (isl_options_get_schedule_maximize_coincidence(ctx
)) {
6676 ok
= ok_to_merge_coincident(c
, merge_graph
);
6681 return ok_to_merge_proximity(ctx
, graph
, c
, merge_graph
);
6684 /* Apply the schedule in "t_node" to the "n" rows starting at "first"
6685 * of the schedule in "node" and return the result.
6687 * That is, essentially compute
6689 * T * N(first:first+n-1)
6691 * taking into account the constant term and the parameter coefficients
6694 static __isl_give isl_mat
*node_transformation(isl_ctx
*ctx
,
6695 struct isl_sched_node
*t_node
, struct isl_sched_node
*node
,
6700 int n_row
, n_col
, n_param
, n_var
;
6702 n_param
= node
->nparam
;
6704 n_row
= isl_mat_rows(t_node
->sched
);
6705 n_col
= isl_mat_cols(node
->sched
);
6706 t
= isl_mat_alloc(ctx
, n_row
, n_col
);
6709 for (i
= 0; i
< n_row
; ++i
) {
6710 isl_seq_cpy(t
->row
[i
], t_node
->sched
->row
[i
], 1 + n_param
);
6711 isl_seq_clr(t
->row
[i
] + 1 + n_param
, n_var
);
6712 for (j
= 0; j
< n
; ++j
)
6713 isl_seq_addmul(t
->row
[i
],
6714 t_node
->sched
->row
[i
][1 + n_param
+ j
],
6715 node
->sched
->row
[first
+ j
],
6716 1 + n_param
+ n_var
);
6721 /* Apply the cluster schedule in "t_node" to the current band
6722 * schedule of the nodes in "graph".
6724 * In particular, replace the rows starting at band_start
6725 * by the result of applying the cluster schedule in "t_node"
6726 * to the original rows.
6728 * The coincidence of the schedule is determined by the coincidence
6729 * of the cluster schedule.
6731 static isl_stat
transform(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6732 struct isl_sched_node
*t_node
)
6738 start
= graph
->band_start
;
6739 n
= graph
->n_total_row
- start
;
6741 n_new
= isl_mat_rows(t_node
->sched
);
6742 for (i
= 0; i
< graph
->n
; ++i
) {
6743 struct isl_sched_node
*node
= &graph
->node
[i
];
6746 t
= node_transformation(ctx
, t_node
, node
, start
, n
);
6747 node
->sched
= isl_mat_drop_rows(node
->sched
, start
, n
);
6748 node
->sched
= isl_mat_concat(node
->sched
, t
);
6749 node
->sched_map
= isl_map_free(node
->sched_map
);
6751 return isl_stat_error
;
6752 for (j
= 0; j
< n_new
; ++j
)
6753 node
->coincident
[start
+ j
] = t_node
->coincident
[j
];
6755 graph
->n_total_row
-= n
;
6757 graph
->n_total_row
+= n_new
;
6758 graph
->n_row
+= n_new
;
6763 /* Merge the clusters marked for merging in "c" into a single
6764 * cluster using the cluster schedule in the current band of "merge_graph".
6765 * The representative SCC for the new cluster is the SCC with
6766 * the smallest index.
6768 * The current band schedule of each SCC in the new cluster is obtained
6769 * by applying the schedule of the corresponding original cluster
6770 * to the original band schedule.
6771 * All SCCs in the new cluster have the same number of schedule rows.
6773 static isl_stat
merge(isl_ctx
*ctx
, struct isl_clustering
*c
,
6774 struct isl_sched_graph
*merge_graph
)
6780 for (i
= 0; i
< c
->n
; ++i
) {
6781 struct isl_sched_node
*node
;
6783 if (!c
->scc_in_merge
[i
])
6787 space
= cluster_space(&c
->scc
[i
], c
->scc_cluster
[i
]);
6788 node
= graph_find_node(ctx
, merge_graph
, space
);
6789 isl_space_free(space
);
6791 return isl_stat_error
;
6792 if (!is_node(merge_graph
, node
))
6793 isl_die(ctx
, isl_error_internal
,
6794 "unable to find cluster",
6795 return isl_stat_error
);
6796 if (transform(ctx
, &c
->scc
[i
], node
) < 0)
6797 return isl_stat_error
;
6798 c
->scc_cluster
[i
] = cluster
;
6804 /* Try and merge the clusters of SCCs marked in c->scc_in_merge
6805 * by scheduling the current cluster bands with respect to each other.
6807 * Construct a dependence graph with a space for each cluster and
6808 * with the coordinates of each space corresponding to the schedule
6809 * dimensions of the current band of that cluster.
6810 * Construct a cluster schedule in this cluster dependence graph and
6811 * apply it to the current cluster bands if it is applicable
6812 * according to ok_to_merge.
6814 * If the number of remaining schedule dimensions in a cluster
6815 * with a non-maximal current schedule dimension is greater than
6816 * the number of remaining schedule dimensions in clusters
6817 * with a maximal current schedule dimension, then restrict
6818 * the number of rows to be computed in the cluster schedule
6819 * to the minimal such non-maximal current schedule dimension.
6820 * Do this by adjusting merge_graph.maxvar.
6822 * Return isl_bool_true if the clusters have effectively been merged
6823 * into a single cluster.
6825 * Note that since the standard scheduling algorithm minimizes the maximal
6826 * distance over proximity constraints, the proximity constraints between
6827 * the merged clusters may not be optimized any further than what is
6828 * sufficient to bring the distances within the limits of the internal
6829 * proximity constraints inside the individual clusters.
6830 * It may therefore make sense to perform an additional translation step
6831 * to bring the clusters closer to each other, while maintaining
6832 * the linear part of the merging schedule found using the standard
6833 * scheduling algorithm.
6835 static isl_bool
try_merge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6836 struct isl_clustering
*c
)
6838 struct isl_sched_graph merge_graph
= { 0 };
6841 if (init_merge_graph(ctx
, graph
, c
, &merge_graph
) < 0)
6844 if (compute_maxvar(&merge_graph
) < 0)
6846 if (adjust_maxvar_to_slack(ctx
, &merge_graph
,c
) < 0)
6848 if (compute_schedule_wcc_band(ctx
, &merge_graph
) < 0)
6850 merged
= ok_to_merge(ctx
, graph
, c
, &merge_graph
);
6851 if (merged
&& merge(ctx
, c
, &merge_graph
) < 0)
6854 graph_free(ctx
, &merge_graph
);
6857 graph_free(ctx
, &merge_graph
);
6858 return isl_bool_error
;
6861 /* Is there any edge marked "no_merge" between two SCCs that are
6862 * about to be merged (i.e., that are set in "scc_in_merge")?
6863 * "merge_edge" is the proximity edge along which the clusters of SCCs
6864 * are going to be merged.
6866 * If there is any edge between two SCCs with a negative weight,
6867 * while the weight of "merge_edge" is non-negative, then this
6868 * means that the edge was postponed. "merge_edge" should then
6869 * also be postponed since merging along the edge with negative weight should
6870 * be postponed until all edges with non-negative weight have been tried.
6871 * Replace the weight of "merge_edge" by a negative weight as well and
6872 * tell the caller not to attempt a merge.
6874 static int any_no_merge(struct isl_sched_graph
*graph
, int *scc_in_merge
,
6875 struct isl_sched_edge
*merge_edge
)
6879 for (i
= 0; i
< graph
->n_edge
; ++i
) {
6880 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
6882 if (!scc_in_merge
[edge
->src
->scc
])
6884 if (!scc_in_merge
[edge
->dst
->scc
])
6888 if (merge_edge
->weight
>= 0 && edge
->weight
< 0) {
6889 merge_edge
->weight
-= graph
->max_weight
+ 1;
6897 /* Merge the two clusters in "c" connected by the edge in "graph"
6898 * with index "edge" into a single cluster.
6899 * If it turns out to be impossible to merge these two clusters,
6900 * then mark the edge as "no_merge" such that it will not be
6903 * First mark all SCCs that need to be merged. This includes the SCCs
6904 * in the two clusters, but it may also include the SCCs
6905 * of intermediate clusters.
6906 * If there is already a no_merge edge between any pair of such SCCs,
6907 * then simply mark the current edge as no_merge as well.
6908 * Likewise, if any of those edges was postponed by has_bounded_distances,
6909 * then postpone the current edge as well.
6910 * Otherwise, try and merge the clusters and mark "edge" as "no_merge"
6911 * if the clusters did not end up getting merged, unless the non-merge
6912 * is due to the fact that the edge was postponed. This postponement
6913 * can be recognized by a change in weight (from non-negative to negative).
6915 static isl_stat
merge_clusters_along_edge(isl_ctx
*ctx
,
6916 struct isl_sched_graph
*graph
, int edge
, struct isl_clustering
*c
)
6919 int edge_weight
= graph
->edge
[edge
].weight
;
6921 if (mark_merge_sccs(ctx
, graph
, edge
, c
) < 0)
6922 return isl_stat_error
;
6924 if (any_no_merge(graph
, c
->scc_in_merge
, &graph
->edge
[edge
]))
6925 merged
= isl_bool_false
;
6927 merged
= try_merge(ctx
, graph
, c
);
6929 return isl_stat_error
;
6930 if (!merged
&& edge_weight
== graph
->edge
[edge
].weight
)
6931 graph
->edge
[edge
].no_merge
= 1;
6936 /* Does "node" belong to the cluster identified by "cluster"?
6938 static int node_cluster_exactly(struct isl_sched_node
*node
, int cluster
)
6940 return node
->cluster
== cluster
;
6943 /* Does "edge" connect two nodes belonging to the cluster
6944 * identified by "cluster"?
6946 static int edge_cluster_exactly(struct isl_sched_edge
*edge
, int cluster
)
6948 return edge
->src
->cluster
== cluster
&& edge
->dst
->cluster
== cluster
;
6951 /* Swap the schedule of "node1" and "node2".
6952 * Both nodes have been derived from the same node in a common parent graph.
6953 * Since the "coincident" field is shared with that node
6954 * in the parent graph, there is no need to also swap this field.
6956 static void swap_sched(struct isl_sched_node
*node1
,
6957 struct isl_sched_node
*node2
)
6962 sched
= node1
->sched
;
6963 node1
->sched
= node2
->sched
;
6964 node2
->sched
= sched
;
6966 sched_map
= node1
->sched_map
;
6967 node1
->sched_map
= node2
->sched_map
;
6968 node2
->sched_map
= sched_map
;
6971 /* Copy the current band schedule from the SCCs that form the cluster
6972 * with index "pos" to the actual cluster at position "pos".
6973 * By construction, the index of the first SCC that belongs to the cluster
6976 * The order of the nodes inside both the SCCs and the cluster
6977 * is assumed to be same as the order in the original "graph".
6979 * Since the SCC graphs will no longer be used after this function,
6980 * the schedules are actually swapped rather than copied.
6982 static isl_stat
copy_partial(struct isl_sched_graph
*graph
,
6983 struct isl_clustering
*c
, int pos
)
6987 c
->cluster
[pos
].n_total_row
= c
->scc
[pos
].n_total_row
;
6988 c
->cluster
[pos
].n_row
= c
->scc
[pos
].n_row
;
6989 c
->cluster
[pos
].maxvar
= c
->scc
[pos
].maxvar
;
6991 for (i
= 0; i
< graph
->n
; ++i
) {
6995 if (graph
->node
[i
].cluster
!= pos
)
6997 s
= graph
->node
[i
].scc
;
6998 k
= c
->scc_node
[s
]++;
6999 swap_sched(&c
->cluster
[pos
].node
[j
], &c
->scc
[s
].node
[k
]);
7000 if (c
->scc
[s
].maxvar
> c
->cluster
[pos
].maxvar
)
7001 c
->cluster
[pos
].maxvar
= c
->scc
[s
].maxvar
;
7008 /* Is there a (conditional) validity dependence from node[j] to node[i],
7009 * forcing node[i] to follow node[j] or do the nodes belong to the same
7012 static isl_bool
node_follows_strong_or_same_cluster(int i
, int j
, void *user
)
7014 struct isl_sched_graph
*graph
= user
;
7016 if (graph
->node
[i
].cluster
== graph
->node
[j
].cluster
)
7017 return isl_bool_true
;
7018 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
7021 /* Extract the merged clusters of SCCs in "graph", sort them, and
7022 * store them in c->clusters. Update c->scc_cluster accordingly.
7024 * First keep track of the cluster containing the SCC to which a node
7025 * belongs in the node itself.
7026 * Then extract the clusters into c->clusters, copying the current
7027 * band schedule from the SCCs that belong to the cluster.
7028 * Do this only once per cluster.
7030 * Finally, topologically sort the clusters and update c->scc_cluster
7031 * to match the new scc numbering. While the SCCs were originally
7032 * sorted already, some SCCs that depend on some other SCCs may
7033 * have been merged with SCCs that appear before these other SCCs.
7034 * A reordering may therefore be required.
7036 static isl_stat
extract_clusters(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
7037 struct isl_clustering
*c
)
7041 for (i
= 0; i
< graph
->n
; ++i
)
7042 graph
->node
[i
].cluster
= c
->scc_cluster
[graph
->node
[i
].scc
];
7044 for (i
= 0; i
< graph
->scc
; ++i
) {
7045 if (c
->scc_cluster
[i
] != i
)
7047 if (extract_sub_graph(ctx
, graph
, &node_cluster_exactly
,
7048 &edge_cluster_exactly
, i
, &c
->cluster
[i
]) < 0)
7049 return isl_stat_error
;
7050 c
->cluster
[i
].src_scc
= -1;
7051 c
->cluster
[i
].dst_scc
= -1;
7052 if (copy_partial(graph
, c
, i
) < 0)
7053 return isl_stat_error
;
7056 if (detect_ccs(ctx
, graph
, &node_follows_strong_or_same_cluster
) < 0)
7057 return isl_stat_error
;
7058 for (i
= 0; i
< graph
->n
; ++i
)
7059 c
->scc_cluster
[graph
->node
[i
].scc
] = graph
->node
[i
].cluster
;
7064 /* Compute weights on the proximity edges of "graph" that can
7065 * be used by find_proximity to find the most appropriate
7066 * proximity edge to use to merge two clusters in "c".
7067 * The weights are also used by has_bounded_distances to determine
7068 * whether the merge should be allowed.
7069 * Store the maximum of the computed weights in graph->max_weight.
7071 * The computed weight is a measure for the number of remaining schedule
7072 * dimensions that can still be completely aligned.
7073 * In particular, compute the number of equalities between
7074 * input dimensions and output dimensions in the proximity constraints.
7075 * The directions that are already handled by outer schedule bands
7076 * are projected out prior to determining this number.
7078 * Edges that will never be considered by find_proximity are ignored.
7080 static isl_stat
compute_weights(struct isl_sched_graph
*graph
,
7081 struct isl_clustering
*c
)
7085 graph
->max_weight
= 0;
7087 for (i
= 0; i
< graph
->n_edge
; ++i
) {
7088 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
7089 struct isl_sched_node
*src
= edge
->src
;
7090 struct isl_sched_node
*dst
= edge
->dst
;
7091 isl_basic_map
*hull
;
7095 prox
= is_non_empty_proximity(edge
);
7097 return isl_stat_error
;
7100 if (bad_cluster(&c
->scc
[edge
->src
->scc
]) ||
7101 bad_cluster(&c
->scc
[edge
->dst
->scc
]))
7103 if (c
->scc_cluster
[edge
->dst
->scc
] ==
7104 c
->scc_cluster
[edge
->src
->scc
])
7107 hull
= isl_map_affine_hull(isl_map_copy(edge
->map
));
7108 hull
= isl_basic_map_transform_dims(hull
, isl_dim_in
, 0,
7109 isl_mat_copy(src
->vmap
));
7110 hull
= isl_basic_map_transform_dims(hull
, isl_dim_out
, 0,
7111 isl_mat_copy(dst
->vmap
));
7112 hull
= isl_basic_map_project_out(hull
,
7113 isl_dim_in
, 0, src
->rank
);
7114 hull
= isl_basic_map_project_out(hull
,
7115 isl_dim_out
, 0, dst
->rank
);
7116 hull
= isl_basic_map_remove_divs(hull
);
7117 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
7118 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
7119 hull
= isl_basic_map_drop_constraints_not_involving_dims(hull
,
7120 isl_dim_in
, 0, n_in
);
7121 hull
= isl_basic_map_drop_constraints_not_involving_dims(hull
,
7122 isl_dim_out
, 0, n_out
);
7124 return isl_stat_error
;
7125 edge
->weight
= isl_basic_map_n_equality(hull
);
7126 isl_basic_map_free(hull
);
7128 if (edge
->weight
> graph
->max_weight
)
7129 graph
->max_weight
= edge
->weight
;
7135 /* Call compute_schedule_finish_band on each of the clusters in "c"
7136 * in their topological order. This order is determined by the scc
7137 * fields of the nodes in "graph".
7138 * Combine the results in a sequence expressing the topological order.
7140 * If there is only one cluster left, then there is no need to introduce
7141 * a sequence node. Also, in this case, the cluster necessarily contains
7142 * the SCC at position 0 in the original graph and is therefore also
7143 * stored in the first cluster of "c".
7145 static __isl_give isl_schedule_node
*finish_bands_clustering(
7146 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
7147 struct isl_clustering
*c
)
7151 isl_union_set_list
*filters
;
7153 if (graph
->scc
== 1)
7154 return compute_schedule_finish_band(node
, &c
->cluster
[0], 0);
7156 ctx
= isl_schedule_node_get_ctx(node
);
7158 filters
= extract_sccs(ctx
, graph
);
7159 node
= isl_schedule_node_insert_sequence(node
, filters
);
7161 for (i
= 0; i
< graph
->scc
; ++i
) {
7162 int j
= c
->scc_cluster
[i
];
7163 node
= isl_schedule_node_child(node
, i
);
7164 node
= isl_schedule_node_child(node
, 0);
7165 node
= compute_schedule_finish_band(node
, &c
->cluster
[j
], 0);
7166 node
= isl_schedule_node_parent(node
);
7167 node
= isl_schedule_node_parent(node
);
7173 /* Compute a schedule for a connected dependence graph by first considering
7174 * each strongly connected component (SCC) in the graph separately and then
7175 * incrementally combining them into clusters.
7176 * Return the updated schedule node.
7178 * Initially, each cluster consists of a single SCC, each with its
7179 * own band schedule. The algorithm then tries to merge pairs
7180 * of clusters along a proximity edge until no more suitable
7181 * proximity edges can be found. During this merging, the schedule
7182 * is maintained in the individual SCCs.
7183 * After the merging is completed, the full resulting clusters
7184 * are extracted and in finish_bands_clustering,
7185 * compute_schedule_finish_band is called on each of them to integrate
7186 * the band into "node" and to continue the computation.
7188 * compute_weights initializes the weights that are used by find_proximity.
7190 static __isl_give isl_schedule_node
*compute_schedule_wcc_clustering(
7191 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
7194 struct isl_clustering c
;
7197 ctx
= isl_schedule_node_get_ctx(node
);
7199 if (clustering_init(ctx
, &c
, graph
) < 0)
7202 if (compute_weights(graph
, &c
) < 0)
7206 i
= find_proximity(graph
, &c
);
7209 if (i
>= graph
->n_edge
)
7211 if (merge_clusters_along_edge(ctx
, graph
, i
, &c
) < 0)
7215 if (extract_clusters(ctx
, graph
, &c
) < 0)
7218 node
= finish_bands_clustering(node
, graph
, &c
);
7220 clustering_free(ctx
, &c
);
7223 clustering_free(ctx
, &c
);
7224 return isl_schedule_node_free(node
);
7227 /* Compute a schedule for a connected dependence graph and return
7228 * the updated schedule node.
7230 * If Feautrier's algorithm is selected, we first recursively try to satisfy
7231 * as many validity dependences as possible. When all validity dependences
7232 * are satisfied we extend the schedule to a full-dimensional schedule.
7234 * Call compute_schedule_wcc_whole or compute_schedule_wcc_clustering
7235 * depending on whether the user has selected the option to try and
7236 * compute a schedule for the entire (weakly connected) component first.
7237 * If there is only a single strongly connected component (SCC), then
7238 * there is no point in trying to combine SCCs
7239 * in compute_schedule_wcc_clustering, so compute_schedule_wcc_whole
7240 * is called instead.
7242 static __isl_give isl_schedule_node
*compute_schedule_wcc(
7243 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
7250 ctx
= isl_schedule_node_get_ctx(node
);
7251 if (detect_sccs(ctx
, graph
) < 0)
7252 return isl_schedule_node_free(node
);
7254 if (compute_maxvar(graph
) < 0)
7255 return isl_schedule_node_free(node
);
7257 if (need_feautrier_step(ctx
, graph
))
7258 return compute_schedule_wcc_feautrier(node
, graph
);
7260 if (graph
->scc
<= 1 || isl_options_get_schedule_whole_component(ctx
))
7261 return compute_schedule_wcc_whole(node
, graph
);
7263 return compute_schedule_wcc_clustering(node
, graph
);
7266 /* Compute a schedule for each group of nodes identified by node->scc
7267 * separately and then combine them in a sequence node (or as set node
7268 * if graph->weak is set) inserted at position "node" of the schedule tree.
7269 * Return the updated schedule node.
7271 * If "wcc" is set then each of the groups belongs to a single
7272 * weakly connected component in the dependence graph so that
7273 * there is no need for compute_sub_schedule to look for weakly
7274 * connected components.
7276 * If a set node would be introduced and if the number of components
7277 * is equal to the number of nodes, then check if the schedule
7278 * is already complete. If so, a redundant set node would be introduced
7279 * (without any further descendants) stating that the statements
7280 * can be executed in arbitrary order, which is also expressed
7281 * by the absence of any node. Refrain from inserting any nodes
7282 * in this case and simply return.
7284 static __isl_give isl_schedule_node
*compute_component_schedule(
7285 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
7290 isl_union_set_list
*filters
;
7295 if (graph
->weak
&& graph
->scc
== graph
->n
) {
7296 if (compute_maxvar(graph
) < 0)
7297 return isl_schedule_node_free(node
);
7298 if (graph
->n_row
>= graph
->maxvar
)
7302 ctx
= isl_schedule_node_get_ctx(node
);
7303 filters
= extract_sccs(ctx
, graph
);
7305 node
= isl_schedule_node_insert_set(node
, filters
);
7307 node
= isl_schedule_node_insert_sequence(node
, filters
);
7309 for (component
= 0; component
< graph
->scc
; ++component
) {
7310 node
= isl_schedule_node_child(node
, component
);
7311 node
= isl_schedule_node_child(node
, 0);
7312 node
= compute_sub_schedule(node
, ctx
, graph
,
7314 &edge_scc_exactly
, component
, wcc
);
7315 node
= isl_schedule_node_parent(node
);
7316 node
= isl_schedule_node_parent(node
);
7322 /* Compute a schedule for the given dependence graph and insert it at "node".
7323 * Return the updated schedule node.
7325 * We first check if the graph is connected (through validity and conditional
7326 * validity dependences) and, if not, compute a schedule
7327 * for each component separately.
7328 * If the schedule_serialize_sccs option is set, then we check for strongly
7329 * connected components instead and compute a separate schedule for
7330 * each such strongly connected component.
7332 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
7333 struct isl_sched_graph
*graph
)
7340 ctx
= isl_schedule_node_get_ctx(node
);
7341 if (isl_options_get_schedule_serialize_sccs(ctx
)) {
7342 if (detect_sccs(ctx
, graph
) < 0)
7343 return isl_schedule_node_free(node
);
7345 if (detect_wccs(ctx
, graph
) < 0)
7346 return isl_schedule_node_free(node
);
7350 return compute_component_schedule(node
, graph
, 1);
7352 return compute_schedule_wcc(node
, graph
);
7355 /* Compute a schedule on sc->domain that respects the given schedule
7358 * In particular, the schedule respects all the validity dependences.
7359 * If the default isl scheduling algorithm is used, it tries to minimize
7360 * the dependence distances over the proximity dependences.
7361 * If Feautrier's scheduling algorithm is used, the proximity dependence
7362 * distances are only minimized during the extension to a full-dimensional
7365 * If there are any condition and conditional validity dependences,
7366 * then the conditional validity dependences may be violated inside
7367 * a tilable band, provided they have no adjacent non-local
7368 * condition dependences.
7370 __isl_give isl_schedule
*isl_schedule_constraints_compute_schedule(
7371 __isl_take isl_schedule_constraints
*sc
)
7373 isl_ctx
*ctx
= isl_schedule_constraints_get_ctx(sc
);
7374 struct isl_sched_graph graph
= { 0 };
7375 isl_schedule
*sched
;
7376 isl_schedule_node
*node
;
7377 isl_union_set
*domain
;
7379 sc
= isl_schedule_constraints_align_params(sc
);
7381 domain
= isl_schedule_constraints_get_domain(sc
);
7382 if (isl_union_set_n_set(domain
) == 0) {
7383 isl_schedule_constraints_free(sc
);
7384 return isl_schedule_from_domain(domain
);
7387 if (graph_init(&graph
, sc
) < 0)
7388 domain
= isl_union_set_free(domain
);
7390 node
= isl_schedule_node_from_domain(domain
);
7391 node
= isl_schedule_node_child(node
, 0);
7393 node
= compute_schedule(node
, &graph
);
7394 sched
= isl_schedule_node_get_schedule(node
);
7395 isl_schedule_node_free(node
);
7397 graph_free(ctx
, &graph
);
7398 isl_schedule_constraints_free(sc
);
7403 /* Compute a schedule for the given union of domains that respects
7404 * all the validity dependences and minimizes
7405 * the dependence distances over the proximity dependences.
7407 * This function is kept for backward compatibility.
7409 __isl_give isl_schedule
*isl_union_set_compute_schedule(
7410 __isl_take isl_union_set
*domain
,
7411 __isl_take isl_union_map
*validity
,
7412 __isl_take isl_union_map
*proximity
)
7414 isl_schedule_constraints
*sc
;
7416 sc
= isl_schedule_constraints_on_domain(domain
);
7417 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
7418 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
7420 return isl_schedule_constraints_compute_schedule(sc
);