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
;
1561 return isl_basic_set_copy(node
->bounds
);
1563 if (node
->compressed
)
1564 space
= isl_multi_aff_get_domain_space(node
->decompress
);
1566 space
= isl_space_copy(node
->space
);
1567 nparam
= isl_space_dim(space
, isl_dim_param
);
1568 space
= isl_space_drop_dims(space
, isl_dim_param
, 0, nparam
);
1569 bounds
= isl_basic_set_universe(space
);
1571 for (i
= 0; i
< node
->nvar
; ++i
) {
1574 size
= isl_multi_val_get_val(node
->sizes
, i
);
1576 return isl_basic_set_free(bounds
);
1577 if (!isl_val_is_int(size
)) {
1581 bounds
= isl_basic_set_upper_bound_val(bounds
, isl_dim_set
, i
,
1582 isl_val_copy(size
));
1583 bounds
= isl_basic_set_lower_bound_val(bounds
, isl_dim_set
, i
,
1587 node
->bounds
= isl_basic_set_copy(bounds
);
1591 /* Drop some constraints from "delta" that could be exploited
1592 * to construct loop coalescing schedules.
1593 * In particular, drop those constraint that bound the difference
1594 * to the size of the domain.
1595 * First project out the parameters to improve the effectiveness.
1597 static __isl_give isl_set
*drop_coalescing_constraints(
1598 __isl_take isl_set
*delta
, struct isl_sched_node
*node
)
1601 isl_basic_set
*bounds
;
1603 bounds
= get_size_bounds(node
);
1605 nparam
= isl_set_dim(delta
, isl_dim_param
);
1606 delta
= isl_set_project_out(delta
, isl_dim_param
, 0, nparam
);
1607 delta
= isl_set_remove_divs(delta
);
1608 delta
= isl_set_plain_gist_basic_set(delta
, bounds
);
1612 /* Given a dependence relation R from "node" to itself,
1613 * construct the set of coefficients of valid constraints for elements
1614 * in that dependence relation.
1615 * In particular, the result contains tuples of coefficients
1616 * c_0, c_n, c_x such that
1618 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1622 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1624 * We choose here to compute the dual of delta R.
1625 * Alternatively, we could have computed the dual of R, resulting
1626 * in a set of tuples c_0, c_n, c_x, c_y, and then
1627 * plugged in (c_0, c_n, c_x, -c_x).
1629 * If "need_param" is set, then the resulting coefficients effectively
1630 * include coefficients for the parameters c_n. Otherwise, they may
1631 * have been projected out already.
1632 * Since the constraints may be different for these two cases,
1633 * they are stored in separate caches.
1634 * In particular, if no parameter coefficients are required and
1635 * the schedule_treat_coalescing option is set, then the parameters
1636 * are projected out and some constraints that could be exploited
1637 * to construct coalescing schedules are removed before the dual
1640 * If "node" has been compressed, then the dependence relation
1641 * is also compressed before the set of coefficients is computed.
1643 static __isl_give isl_basic_set
*intra_coefficients(
1644 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1645 __isl_take isl_map
*map
, int need_param
)
1650 isl_basic_set
*coef
;
1651 isl_maybe_isl_basic_set m
;
1652 isl_map_to_basic_set
**hmap
= &graph
->intra_hmap
;
1658 ctx
= isl_map_get_ctx(map
);
1659 treat
= !need_param
&& isl_options_get_schedule_treat_coalescing(ctx
);
1661 hmap
= &graph
->intra_hmap_param
;
1662 m
= isl_map_to_basic_set_try_get(*hmap
, map
);
1663 if (m
.valid
< 0 || m
.valid
) {
1668 key
= isl_map_copy(map
);
1669 if (node
->compressed
) {
1670 map
= isl_map_preimage_domain_multi_aff(map
,
1671 isl_multi_aff_copy(node
->decompress
));
1672 map
= isl_map_preimage_range_multi_aff(map
,
1673 isl_multi_aff_copy(node
->decompress
));
1675 delta
= isl_map_deltas(map
);
1677 delta
= drop_coalescing_constraints(delta
, node
);
1678 delta
= isl_set_remove_divs(delta
);
1679 coef
= isl_set_coefficients(delta
);
1680 *hmap
= isl_map_to_basic_set_set(*hmap
, key
, isl_basic_set_copy(coef
));
1685 /* Given a dependence relation R, construct the set of coefficients
1686 * of valid constraints for elements in that dependence relation.
1687 * In particular, the result contains tuples of coefficients
1688 * c_0, c_n, c_x, c_y such that
1690 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1692 * If the source or destination nodes of "edge" have been compressed,
1693 * then the dependence relation is also compressed before
1694 * the set of coefficients is computed.
1696 static __isl_give isl_basic_set
*inter_coefficients(
1697 struct isl_sched_graph
*graph
, struct isl_sched_edge
*edge
,
1698 __isl_take isl_map
*map
)
1702 isl_basic_set
*coef
;
1703 isl_maybe_isl_basic_set m
;
1705 m
= isl_map_to_basic_set_try_get(graph
->inter_hmap
, map
);
1706 if (m
.valid
< 0 || m
.valid
) {
1711 key
= isl_map_copy(map
);
1712 if (edge
->src
->compressed
)
1713 map
= isl_map_preimage_domain_multi_aff(map
,
1714 isl_multi_aff_copy(edge
->src
->decompress
));
1715 if (edge
->dst
->compressed
)
1716 map
= isl_map_preimage_range_multi_aff(map
,
1717 isl_multi_aff_copy(edge
->dst
->decompress
));
1718 set
= isl_map_wrap(isl_map_remove_divs(map
));
1719 coef
= isl_set_coefficients(set
);
1720 graph
->inter_hmap
= isl_map_to_basic_set_set(graph
->inter_hmap
, key
,
1721 isl_basic_set_copy(coef
));
1726 /* Return the position of the coefficients of the variables in
1727 * the coefficients constraints "coef".
1729 * The space of "coef" is of the form
1731 * { coefficients[[cst, params] -> S] }
1733 * Return the position of S.
1735 static int coef_var_offset(__isl_keep isl_basic_set
*coef
)
1740 space
= isl_space_unwrap(isl_basic_set_get_space(coef
));
1741 offset
= isl_space_dim(space
, isl_dim_in
);
1742 isl_space_free(space
);
1747 /* Return the offset of the coefficient of the constant term of "node"
1750 * Within each node, the coefficients have the following order:
1751 * - positive and negative parts of c_i_x
1752 * - c_i_n (if parametric)
1755 static int node_cst_coef_offset(struct isl_sched_node
*node
)
1757 return node
->start
+ 2 * node
->nvar
+ node
->nparam
;
1760 /* Return the offset of the coefficients of the parameters of "node"
1763 * Within each node, the coefficients have the following order:
1764 * - positive and negative parts of c_i_x
1765 * - c_i_n (if parametric)
1768 static int node_par_coef_offset(struct isl_sched_node
*node
)
1770 return node
->start
+ 2 * node
->nvar
;
1773 /* Return the offset of the coefficients of the variables of "node"
1776 * Within each node, the coefficients have the following order:
1777 * - positive and negative parts of c_i_x
1778 * - c_i_n (if parametric)
1781 static int node_var_coef_offset(struct isl_sched_node
*node
)
1786 /* Return the position of the pair of variables encoding
1787 * coefficient "i" of "node".
1789 * The order of these variable pairs is the opposite of
1790 * that of the coefficients, with 2 variables per coefficient.
1792 static int node_var_coef_pos(struct isl_sched_node
*node
, int i
)
1794 return node_var_coef_offset(node
) + 2 * (node
->nvar
- 1 - i
);
1797 /* Construct an isl_dim_map for mapping constraints on coefficients
1798 * for "node" to the corresponding positions in graph->lp.
1799 * "offset" is the offset of the coefficients for the variables
1800 * in the input constraints.
1801 * "s" is the sign of the mapping.
1803 * The input constraints are given in terms of the coefficients
1804 * (c_0, c_x) or (c_0, c_n, c_x).
1805 * The mapping produced by this function essentially plugs in
1806 * (0, c_i_x^+ - c_i_x^-) if s = 1 and
1807 * (0, -c_i_x^+ + c_i_x^-) if s = -1 or
1808 * (0, 0, c_i_x^+ - c_i_x^-) if s = 1 and
1809 * (0, 0, -c_i_x^+ + c_i_x^-) if s = -1.
1810 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1811 * Furthermore, the order of these pairs is the opposite of that
1812 * of the corresponding coefficients.
1814 * The caller can extend the mapping to also map the other coefficients
1815 * (and therefore not plug in 0).
1817 static __isl_give isl_dim_map
*intra_dim_map(isl_ctx
*ctx
,
1818 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1823 isl_dim_map
*dim_map
;
1825 if (!node
|| !graph
->lp
)
1828 total
= isl_basic_set_total_dim(graph
->lp
);
1829 pos
= node_var_coef_pos(node
, 0);
1830 dim_map
= isl_dim_map_alloc(ctx
, total
);
1831 isl_dim_map_range(dim_map
, pos
, -2, offset
, 1, node
->nvar
, -s
);
1832 isl_dim_map_range(dim_map
, pos
+ 1, -2, offset
, 1, node
->nvar
, s
);
1837 /* Construct an isl_dim_map for mapping constraints on coefficients
1838 * for "src" (node i) and "dst" (node j) to the corresponding positions
1840 * "offset" is the offset of the coefficients for the variables of "src"
1841 * in the input constraints.
1842 * "s" is the sign of the mapping.
1844 * The input constraints are given in terms of the coefficients
1845 * (c_0, c_n, c_x, c_y).
1846 * The mapping produced by this function essentially plugs in
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 and
1849 * (-c_j_0 + c_i_0, -c_j_n + c_i_n,
1850 * c_i_x^+ - c_i_x^-, -(c_j_x^+ - c_j_x^-)) if s = -1.
1851 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1852 * Furthermore, the order of these pairs is the opposite of that
1853 * of the corresponding coefficients.
1855 * The caller can further extend the mapping.
1857 static __isl_give isl_dim_map
*inter_dim_map(isl_ctx
*ctx
,
1858 struct isl_sched_graph
*graph
, struct isl_sched_node
*src
,
1859 struct isl_sched_node
*dst
, int offset
, int s
)
1863 isl_dim_map
*dim_map
;
1865 if (!src
|| !dst
|| !graph
->lp
)
1868 total
= isl_basic_set_total_dim(graph
->lp
);
1869 dim_map
= isl_dim_map_alloc(ctx
, total
);
1871 pos
= node_cst_coef_offset(dst
);
1872 isl_dim_map_range(dim_map
, pos
, 0, 0, 0, 1, s
);
1873 pos
= node_par_coef_offset(dst
);
1874 isl_dim_map_range(dim_map
, pos
, 1, 1, 1, dst
->nparam
, s
);
1875 pos
= node_var_coef_pos(dst
, 0);
1876 isl_dim_map_range(dim_map
, pos
, -2, offset
+ src
->nvar
, 1,
1878 isl_dim_map_range(dim_map
, pos
+ 1, -2, offset
+ src
->nvar
, 1,
1881 pos
= node_cst_coef_offset(src
);
1882 isl_dim_map_range(dim_map
, pos
, 0, 0, 0, 1, -s
);
1883 pos
= node_par_coef_offset(src
);
1884 isl_dim_map_range(dim_map
, pos
, 1, 1, 1, src
->nparam
, -s
);
1885 pos
= node_var_coef_pos(src
, 0);
1886 isl_dim_map_range(dim_map
, pos
, -2, offset
, 1, src
->nvar
, s
);
1887 isl_dim_map_range(dim_map
, pos
+ 1, -2, offset
, 1, src
->nvar
, -s
);
1892 /* Add the constraints from "src" to "dst" using "dim_map",
1893 * after making sure there is enough room in "dst" for the extra constraints.
1895 static __isl_give isl_basic_set
*add_constraints_dim_map(
1896 __isl_take isl_basic_set
*dst
, __isl_take isl_basic_set
*src
,
1897 __isl_take isl_dim_map
*dim_map
)
1901 n_eq
= isl_basic_set_n_equality(src
);
1902 n_ineq
= isl_basic_set_n_inequality(src
);
1903 dst
= isl_basic_set_extend_constraints(dst
, n_eq
, n_ineq
);
1904 dst
= isl_basic_set_add_constraints_dim_map(dst
, src
, dim_map
);
1908 /* Add constraints to graph->lp that force validity for the given
1909 * dependence from a node i to itself.
1910 * That is, add constraints that enforce
1912 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1913 * = c_i_x (y - x) >= 0
1915 * for each (x,y) in R.
1916 * We obtain general constraints on coefficients (c_0, c_x)
1917 * of valid constraints for (y - x) and then plug in (0, c_i_x^+ - c_i_x^-),
1918 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1919 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1920 * Note that the result of intra_coefficients may also contain
1921 * parameter coefficients c_n, in which case 0 is plugged in for them as well.
1923 static isl_stat
add_intra_validity_constraints(struct isl_sched_graph
*graph
,
1924 struct isl_sched_edge
*edge
)
1927 isl_map
*map
= isl_map_copy(edge
->map
);
1928 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1929 isl_dim_map
*dim_map
;
1930 isl_basic_set
*coef
;
1931 struct isl_sched_node
*node
= edge
->src
;
1933 coef
= intra_coefficients(graph
, node
, map
, 0);
1935 offset
= coef_var_offset(coef
);
1938 return isl_stat_error
;
1940 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, 1);
1941 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
1946 /* Add constraints to graph->lp that force validity for the given
1947 * dependence from node i to node j.
1948 * That is, add constraints that enforce
1950 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1952 * for each (x,y) in R.
1953 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1954 * of valid constraints for R and then plug in
1955 * (c_j_0 - c_i_0, c_j_n - c_i_n, -(c_i_x^+ - c_i_x^-), c_j_x^+ - c_j_x^-),
1956 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1957 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1959 static isl_stat
add_inter_validity_constraints(struct isl_sched_graph
*graph
,
1960 struct isl_sched_edge
*edge
)
1965 isl_dim_map
*dim_map
;
1966 isl_basic_set
*coef
;
1967 struct isl_sched_node
*src
= edge
->src
;
1968 struct isl_sched_node
*dst
= edge
->dst
;
1971 return isl_stat_error
;
1973 map
= isl_map_copy(edge
->map
);
1974 ctx
= isl_map_get_ctx(map
);
1975 coef
= inter_coefficients(graph
, edge
, map
);
1977 offset
= coef_var_offset(coef
);
1980 return isl_stat_error
;
1982 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, 1);
1984 edge
->start
= graph
->lp
->n_ineq
;
1985 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
1987 return isl_stat_error
;
1988 edge
->end
= graph
->lp
->n_ineq
;
1993 /* Add constraints to graph->lp that bound the dependence distance for the given
1994 * dependence from a node i to itself.
1995 * If s = 1, we add the constraint
1997 * c_i_x (y - x) <= m_0 + m_n n
2001 * -c_i_x (y - x) + m_0 + m_n n >= 0
2003 * for each (x,y) in R.
2004 * If s = -1, we add the constraint
2006 * -c_i_x (y - x) <= m_0 + m_n n
2010 * c_i_x (y - x) + m_0 + m_n n >= 0
2012 * for each (x,y) in R.
2013 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2014 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
2015 * with each coefficient (except m_0) represented as a pair of non-negative
2019 * If "local" is set, then we add constraints
2021 * c_i_x (y - x) <= 0
2025 * -c_i_x (y - x) <= 0
2027 * instead, forcing the dependence distance to be (less than or) equal to 0.
2028 * That is, we plug in (0, 0, -s * c_i_x),
2029 * intra_coefficients is not required to have c_n in its result when
2030 * "local" is set. If they are missing, then (0, -s * c_i_x) is plugged in.
2031 * Note that dependences marked local are treated as validity constraints
2032 * by add_all_validity_constraints and therefore also have
2033 * their distances bounded by 0 from below.
2035 static isl_stat
add_intra_proximity_constraints(struct isl_sched_graph
*graph
,
2036 struct isl_sched_edge
*edge
, int s
, int local
)
2040 isl_map
*map
= isl_map_copy(edge
->map
);
2041 isl_ctx
*ctx
= isl_map_get_ctx(map
);
2042 isl_dim_map
*dim_map
;
2043 isl_basic_set
*coef
;
2044 struct isl_sched_node
*node
= edge
->src
;
2046 coef
= intra_coefficients(graph
, node
, map
, !local
);
2048 offset
= coef_var_offset(coef
);
2051 return isl_stat_error
;
2053 nparam
= isl_space_dim(node
->space
, isl_dim_param
);
2054 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, -s
);
2057 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
2058 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
2059 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
2061 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
2066 /* Add constraints to graph->lp that bound the dependence distance for the given
2067 * dependence from node i to node j.
2068 * If s = 1, we add the constraint
2070 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
2075 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
2078 * for each (x,y) in R.
2079 * If s = -1, we add the constraint
2081 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
2086 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
2089 * for each (x,y) in R.
2090 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
2091 * of valid constraints for R and then plug in
2092 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
2093 * s*c_i_x, -s*c_j_x)
2094 * with each coefficient (except m_0, c_*_0 and c_*_n)
2095 * represented as a pair of non-negative coefficients.
2098 * If "local" is set (and s = 1), then we add constraints
2100 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
2104 * -((c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x)) >= 0
2106 * instead, forcing the dependence distance to be (less than or) equal to 0.
2107 * That is, we plug in
2108 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, s*c_i_x, -s*c_j_x).
2109 * Note that dependences marked local are treated as validity constraints
2110 * by add_all_validity_constraints and therefore also have
2111 * their distances bounded by 0 from below.
2113 static isl_stat
add_inter_proximity_constraints(struct isl_sched_graph
*graph
,
2114 struct isl_sched_edge
*edge
, int s
, int local
)
2118 isl_map
*map
= isl_map_copy(edge
->map
);
2119 isl_ctx
*ctx
= isl_map_get_ctx(map
);
2120 isl_dim_map
*dim_map
;
2121 isl_basic_set
*coef
;
2122 struct isl_sched_node
*src
= edge
->src
;
2123 struct isl_sched_node
*dst
= edge
->dst
;
2125 coef
= inter_coefficients(graph
, edge
, map
);
2127 offset
= coef_var_offset(coef
);
2130 return isl_stat_error
;
2132 nparam
= isl_space_dim(src
->space
, isl_dim_param
);
2133 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, -s
);
2136 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
2137 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
2138 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
2141 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
2146 /* Should the distance over "edge" be forced to zero?
2147 * That is, is it marked as a local edge?
2148 * If "use_coincidence" is set, then coincidence edges are treated
2151 static int force_zero(struct isl_sched_edge
*edge
, int use_coincidence
)
2153 return is_local(edge
) || (use_coincidence
&& is_coincidence(edge
));
2156 /* Add all validity constraints to graph->lp.
2158 * An edge that is forced to be local needs to have its dependence
2159 * distances equal to zero. We take care of bounding them by 0 from below
2160 * here. add_all_proximity_constraints takes care of bounding them by 0
2163 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2164 * Otherwise, we ignore them.
2166 static int add_all_validity_constraints(struct isl_sched_graph
*graph
,
2167 int use_coincidence
)
2171 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2172 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2175 zero
= force_zero(edge
, use_coincidence
);
2176 if (!is_validity(edge
) && !zero
)
2178 if (edge
->src
!= edge
->dst
)
2180 if (add_intra_validity_constraints(graph
, edge
) < 0)
2184 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2185 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2188 zero
= force_zero(edge
, use_coincidence
);
2189 if (!is_validity(edge
) && !zero
)
2191 if (edge
->src
== edge
->dst
)
2193 if (add_inter_validity_constraints(graph
, edge
) < 0)
2200 /* Add constraints to graph->lp that bound the dependence distance
2201 * for all dependence relations.
2202 * If a given proximity dependence is identical to a validity
2203 * dependence, then the dependence distance is already bounded
2204 * from below (by zero), so we only need to bound the distance
2205 * from above. (This includes the case of "local" dependences
2206 * which are treated as validity dependence by add_all_validity_constraints.)
2207 * Otherwise, we need to bound the distance both from above and from below.
2209 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2210 * Otherwise, we ignore them.
2212 static int add_all_proximity_constraints(struct isl_sched_graph
*graph
,
2213 int use_coincidence
)
2217 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2218 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2221 zero
= force_zero(edge
, use_coincidence
);
2222 if (!is_proximity(edge
) && !zero
)
2224 if (edge
->src
== edge
->dst
&&
2225 add_intra_proximity_constraints(graph
, edge
, 1, zero
) < 0)
2227 if (edge
->src
!= edge
->dst
&&
2228 add_inter_proximity_constraints(graph
, edge
, 1, zero
) < 0)
2230 if (is_validity(edge
) || zero
)
2232 if (edge
->src
== edge
->dst
&&
2233 add_intra_proximity_constraints(graph
, edge
, -1, 0) < 0)
2235 if (edge
->src
!= edge
->dst
&&
2236 add_inter_proximity_constraints(graph
, edge
, -1, 0) < 0)
2243 /* Normalize the rows of "indep" such that all rows are lexicographically
2244 * positive and such that each row contains as many final zeros as possible,
2245 * given the choice for the previous rows.
2246 * Do this by performing elementary row operations.
2248 static __isl_give isl_mat
*normalize_independent(__isl_take isl_mat
*indep
)
2250 indep
= isl_mat_reverse_gauss(indep
);
2251 indep
= isl_mat_lexnonneg_rows(indep
);
2255 /* Compute a basis for the rows in the linear part of the schedule
2256 * and extend this basis to a full basis. The remaining rows
2257 * can then be used to force linear independence from the rows
2260 * In particular, given the schedule rows S, we compute
2265 * with H the Hermite normal form of S. That is, all but the
2266 * first rank columns of H are zero and so each row in S is
2267 * a linear combination of the first rank rows of Q.
2268 * The matrix Q can be used as a variable transformation
2269 * that isolates the directions of S in the first rank rows.
2270 * Transposing S U = H yields
2274 * with all but the first rank rows of H^T zero.
2275 * The last rows of U^T are therefore linear combinations
2276 * of schedule coefficients that are all zero on schedule
2277 * coefficients that are linearly dependent on the rows of S.
2278 * At least one of these combinations is non-zero on
2279 * linearly independent schedule coefficients.
2280 * The rows are normalized to involve as few of the last
2281 * coefficients as possible and to have a positive initial value.
2283 static int node_update_vmap(struct isl_sched_node
*node
)
2286 int n_row
= isl_mat_rows(node
->sched
);
2288 H
= isl_mat_sub_alloc(node
->sched
, 0, n_row
,
2289 1 + node
->nparam
, node
->nvar
);
2291 H
= isl_mat_left_hermite(H
, 0, &U
, &Q
);
2292 isl_mat_free(node
->indep
);
2293 isl_mat_free(node
->vmap
);
2295 node
->indep
= isl_mat_transpose(U
);
2296 node
->rank
= isl_mat_initial_non_zero_cols(H
);
2297 node
->indep
= isl_mat_drop_rows(node
->indep
, 0, node
->rank
);
2298 node
->indep
= normalize_independent(node
->indep
);
2301 if (!node
->indep
|| !node
->vmap
|| node
->rank
< 0)
2306 /* Is "edge" marked as a validity or a conditional validity edge?
2308 static int is_any_validity(struct isl_sched_edge
*edge
)
2310 return is_validity(edge
) || is_conditional_validity(edge
);
2313 /* How many times should we count the constraints in "edge"?
2315 * We count as follows
2316 * validity -> 1 (>= 0)
2317 * validity+proximity -> 2 (>= 0 and upper bound)
2318 * proximity -> 2 (lower and upper bound)
2319 * local(+any) -> 2 (>= 0 and <= 0)
2321 * If an edge is only marked conditional_validity then it counts
2322 * as zero since it is only checked afterwards.
2324 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2325 * Otherwise, we ignore them.
2327 static int edge_multiplicity(struct isl_sched_edge
*edge
, int use_coincidence
)
2329 if (is_proximity(edge
) || force_zero(edge
, use_coincidence
))
2331 if (is_validity(edge
))
2336 /* How many times should the constraints in "edge" be counted
2337 * as a parametric intra-node constraint?
2339 * Only proximity edges that are not forced zero need
2340 * coefficient constraints that include coefficients for parameters.
2341 * If the edge is also a validity edge, then only
2342 * an upper bound is introduced. Otherwise, both lower and upper bounds
2345 static int parametric_intra_edge_multiplicity(struct isl_sched_edge
*edge
,
2346 int use_coincidence
)
2348 if (edge
->src
!= edge
->dst
)
2350 if (!is_proximity(edge
))
2352 if (force_zero(edge
, use_coincidence
))
2354 if (is_validity(edge
))
2360 /* Add "f" times the number of equality and inequality constraints of "bset"
2361 * to "n_eq" and "n_ineq" and free "bset".
2363 static isl_stat
update_count(__isl_take isl_basic_set
*bset
,
2364 int f
, int *n_eq
, int *n_ineq
)
2367 return isl_stat_error
;
2369 *n_eq
+= isl_basic_set_n_equality(bset
);
2370 *n_ineq
+= isl_basic_set_n_inequality(bset
);
2371 isl_basic_set_free(bset
);
2376 /* Count the number of equality and inequality constraints
2377 * that will be added for the given map.
2379 * The edges that require parameter coefficients are counted separately.
2381 * "use_coincidence" is set if we should take into account coincidence edges.
2383 static isl_stat
count_map_constraints(struct isl_sched_graph
*graph
,
2384 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
,
2385 int *n_eq
, int *n_ineq
, int use_coincidence
)
2388 isl_basic_set
*coef
;
2389 int f
= edge_multiplicity(edge
, use_coincidence
);
2390 int fp
= parametric_intra_edge_multiplicity(edge
, use_coincidence
);
2397 if (edge
->src
!= edge
->dst
) {
2398 coef
= inter_coefficients(graph
, edge
, map
);
2399 return update_count(coef
, f
, n_eq
, n_ineq
);
2403 copy
= isl_map_copy(map
);
2404 coef
= intra_coefficients(graph
, edge
->src
, copy
, 1);
2405 if (update_count(coef
, fp
, n_eq
, n_ineq
) < 0)
2410 copy
= isl_map_copy(map
);
2411 coef
= intra_coefficients(graph
, edge
->src
, copy
, 0);
2412 if (update_count(coef
, f
- fp
, n_eq
, n_ineq
) < 0)
2420 return isl_stat_error
;
2423 /* Count the number of equality and inequality constraints
2424 * that will be added to the main lp problem.
2425 * We count as follows
2426 * validity -> 1 (>= 0)
2427 * validity+proximity -> 2 (>= 0 and upper bound)
2428 * proximity -> 2 (lower and upper bound)
2429 * local(+any) -> 2 (>= 0 and <= 0)
2431 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2432 * Otherwise, we ignore them.
2434 static int count_constraints(struct isl_sched_graph
*graph
,
2435 int *n_eq
, int *n_ineq
, int use_coincidence
)
2439 *n_eq
= *n_ineq
= 0;
2440 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2441 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2442 isl_map
*map
= isl_map_copy(edge
->map
);
2444 if (count_map_constraints(graph
, edge
, map
, n_eq
, n_ineq
,
2445 use_coincidence
) < 0)
2452 /* Count the number of constraints that will be added by
2453 * add_bound_constant_constraints to bound the values of the constant terms
2454 * and increment *n_eq and *n_ineq accordingly.
2456 * In practice, add_bound_constant_constraints only adds inequalities.
2458 static isl_stat
count_bound_constant_constraints(isl_ctx
*ctx
,
2459 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
2461 if (isl_options_get_schedule_max_constant_term(ctx
) == -1)
2464 *n_ineq
+= graph
->n
;
2469 /* Add constraints to bound the values of the constant terms in the schedule,
2470 * if requested by the user.
2472 * The maximal value of the constant terms is defined by the option
2473 * "schedule_max_constant_term".
2475 static isl_stat
add_bound_constant_constraints(isl_ctx
*ctx
,
2476 struct isl_sched_graph
*graph
)
2482 max
= isl_options_get_schedule_max_constant_term(ctx
);
2486 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2488 for (i
= 0; i
< graph
->n
; ++i
) {
2489 struct isl_sched_node
*node
= &graph
->node
[i
];
2492 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2494 return isl_stat_error
;
2495 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2496 pos
= node_cst_coef_offset(node
);
2497 isl_int_set_si(graph
->lp
->ineq
[k
][1 + pos
], -1);
2498 isl_int_set_si(graph
->lp
->ineq
[k
][0], max
);
2504 /* Count the number of constraints that will be added by
2505 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
2508 * In practice, add_bound_coefficient_constraints only adds inequalities.
2510 static int count_bound_coefficient_constraints(isl_ctx
*ctx
,
2511 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
2515 if (isl_options_get_schedule_max_coefficient(ctx
) == -1 &&
2516 !isl_options_get_schedule_treat_coalescing(ctx
))
2519 for (i
= 0; i
< graph
->n
; ++i
)
2520 *n_ineq
+= graph
->node
[i
].nparam
+ 2 * graph
->node
[i
].nvar
;
2525 /* Add constraints to graph->lp that bound the values of
2526 * the parameter schedule coefficients of "node" to "max" and
2527 * the variable schedule coefficients to the corresponding entry
2529 * In either case, a negative value means that no bound needs to be imposed.
2531 * For parameter coefficients, this amounts to adding a constraint
2539 * The variables coefficients are, however, not represented directly.
2540 * Instead, the variable coefficients c_x are written as differences
2541 * c_x = c_x^+ - c_x^-.
2544 * -max_i <= c_x_i <= max_i
2548 * -max_i <= c_x_i^+ - c_x_i^- <= max_i
2552 * -(c_x_i^+ - c_x_i^-) + max_i >= 0
2553 * c_x_i^+ - c_x_i^- + max_i >= 0
2555 static isl_stat
node_add_coefficient_constraints(isl_ctx
*ctx
,
2556 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
, int max
)
2562 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2564 for (j
= 0; j
< node
->nparam
; ++j
) {
2570 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2572 return isl_stat_error
;
2573 dim
= 1 + node_par_coef_offset(node
) + j
;
2574 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2575 isl_int_set_si(graph
->lp
->ineq
[k
][dim
], -1);
2576 isl_int_set_si(graph
->lp
->ineq
[k
][0], max
);
2579 ineq
= isl_vec_alloc(ctx
, 1 + total
);
2580 ineq
= isl_vec_clr(ineq
);
2582 return isl_stat_error
;
2583 for (i
= 0; i
< node
->nvar
; ++i
) {
2584 int pos
= 1 + node_var_coef_pos(node
, i
);
2586 if (isl_int_is_neg(node
->max
->el
[i
]))
2589 isl_int_set_si(ineq
->el
[pos
], 1);
2590 isl_int_set_si(ineq
->el
[pos
+ 1], -1);
2591 isl_int_set(ineq
->el
[0], node
->max
->el
[i
]);
2593 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2596 isl_seq_cpy(graph
->lp
->ineq
[k
], ineq
->el
, 1 + total
);
2598 isl_seq_neg(ineq
->el
+ pos
, ineq
->el
+ pos
, 2);
2599 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2602 isl_seq_cpy(graph
->lp
->ineq
[k
], ineq
->el
, 1 + total
);
2604 isl_seq_clr(ineq
->el
+ pos
, 2);
2611 return isl_stat_error
;
2614 /* Add constraints that bound the values of the variable and parameter
2615 * coefficients of the schedule.
2617 * The maximal value of the coefficients is defined by the option
2618 * 'schedule_max_coefficient' and the entries in node->max.
2619 * These latter entries are only set if either the schedule_max_coefficient
2620 * option or the schedule_treat_coalescing option is set.
2622 static isl_stat
add_bound_coefficient_constraints(isl_ctx
*ctx
,
2623 struct isl_sched_graph
*graph
)
2628 max
= isl_options_get_schedule_max_coefficient(ctx
);
2630 if (max
== -1 && !isl_options_get_schedule_treat_coalescing(ctx
))
2633 for (i
= 0; i
< graph
->n
; ++i
) {
2634 struct isl_sched_node
*node
= &graph
->node
[i
];
2636 if (node_add_coefficient_constraints(ctx
, graph
, node
, max
) < 0)
2637 return isl_stat_error
;
2643 /* Add a constraint to graph->lp that equates the value at position
2644 * "sum_pos" to the sum of the "n" values starting at "first".
2646 static isl_stat
add_sum_constraint(struct isl_sched_graph
*graph
,
2647 int sum_pos
, int first
, int n
)
2652 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2654 k
= isl_basic_set_alloc_equality(graph
->lp
);
2656 return isl_stat_error
;
2657 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2658 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2659 for (i
= 0; i
< n
; ++i
)
2660 isl_int_set_si(graph
->lp
->eq
[k
][1 + first
+ i
], 1);
2665 /* Add a constraint to graph->lp that equates the value at position
2666 * "sum_pos" to the sum of the parameter coefficients of all nodes.
2668 static isl_stat
add_param_sum_constraint(struct isl_sched_graph
*graph
,
2674 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2676 k
= isl_basic_set_alloc_equality(graph
->lp
);
2678 return isl_stat_error
;
2679 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2680 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2681 for (i
= 0; i
< graph
->n
; ++i
) {
2682 int pos
= 1 + node_par_coef_offset(&graph
->node
[i
]);
2684 for (j
= 0; j
< graph
->node
[i
].nparam
; ++j
)
2685 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2691 /* Add a constraint to graph->lp that equates the value at position
2692 * "sum_pos" to the sum of the variable coefficients of all nodes.
2694 static isl_stat
add_var_sum_constraint(struct isl_sched_graph
*graph
,
2700 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2702 k
= isl_basic_set_alloc_equality(graph
->lp
);
2704 return isl_stat_error
;
2705 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2706 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2707 for (i
= 0; i
< graph
->n
; ++i
) {
2708 struct isl_sched_node
*node
= &graph
->node
[i
];
2709 int pos
= 1 + node_var_coef_offset(node
);
2711 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
2712 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2718 /* Construct an ILP problem for finding schedule coefficients
2719 * that result in non-negative, but small dependence distances
2720 * over all dependences.
2721 * In particular, the dependence distances over proximity edges
2722 * are bounded by m_0 + m_n n and we compute schedule coefficients
2723 * with small values (preferably zero) of m_n and m_0.
2725 * All variables of the ILP are non-negative. The actual coefficients
2726 * may be negative, so each coefficient is represented as the difference
2727 * of two non-negative variables. The negative part always appears
2728 * immediately before the positive part.
2729 * Other than that, the variables have the following order
2731 * - sum of positive and negative parts of m_n coefficients
2733 * - sum of all c_n coefficients
2734 * (unconstrained when computing non-parametric schedules)
2735 * - sum of positive and negative parts of all c_x coefficients
2736 * - positive and negative parts of m_n coefficients
2738 * - positive and negative parts of c_i_x, in opposite order
2739 * - c_i_n (if parametric)
2742 * The constraints are those from the edges plus two or three equalities
2743 * to express the sums.
2745 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2746 * Otherwise, we ignore them.
2748 static isl_stat
setup_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
2749 int use_coincidence
)
2759 parametric
= ctx
->opt
->schedule_parametric
;
2760 nparam
= isl_space_dim(graph
->node
[0].space
, isl_dim_param
);
2762 total
= param_pos
+ 2 * nparam
;
2763 for (i
= 0; i
< graph
->n
; ++i
) {
2764 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
2765 if (node_update_vmap(node
) < 0)
2766 return isl_stat_error
;
2767 node
->start
= total
;
2768 total
+= 1 + node
->nparam
+ 2 * node
->nvar
;
2771 if (count_constraints(graph
, &n_eq
, &n_ineq
, use_coincidence
) < 0)
2772 return isl_stat_error
;
2773 if (count_bound_constant_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2774 return isl_stat_error
;
2775 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2776 return isl_stat_error
;
2778 space
= isl_space_set_alloc(ctx
, 0, total
);
2779 isl_basic_set_free(graph
->lp
);
2780 n_eq
+= 2 + parametric
;
2782 graph
->lp
= isl_basic_set_alloc_space(space
, 0, n_eq
, n_ineq
);
2784 if (add_sum_constraint(graph
, 0, param_pos
, 2 * nparam
) < 0)
2785 return isl_stat_error
;
2786 if (parametric
&& add_param_sum_constraint(graph
, 2) < 0)
2787 return isl_stat_error
;
2788 if (add_var_sum_constraint(graph
, 3) < 0)
2789 return isl_stat_error
;
2790 if (add_bound_constant_constraints(ctx
, graph
) < 0)
2791 return isl_stat_error
;
2792 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
2793 return isl_stat_error
;
2794 if (add_all_validity_constraints(graph
, use_coincidence
) < 0)
2795 return isl_stat_error
;
2796 if (add_all_proximity_constraints(graph
, use_coincidence
) < 0)
2797 return isl_stat_error
;
2802 /* Analyze the conflicting constraint found by
2803 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2804 * constraint of one of the edges between distinct nodes, living, moreover
2805 * in distinct SCCs, then record the source and sink SCC as this may
2806 * be a good place to cut between SCCs.
2808 static int check_conflict(int con
, void *user
)
2811 struct isl_sched_graph
*graph
= user
;
2813 if (graph
->src_scc
>= 0)
2816 con
-= graph
->lp
->n_eq
;
2818 if (con
>= graph
->lp
->n_ineq
)
2821 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2822 if (!is_validity(&graph
->edge
[i
]))
2824 if (graph
->edge
[i
].src
== graph
->edge
[i
].dst
)
2826 if (graph
->edge
[i
].src
->scc
== graph
->edge
[i
].dst
->scc
)
2828 if (graph
->edge
[i
].start
> con
)
2830 if (graph
->edge
[i
].end
<= con
)
2832 graph
->src_scc
= graph
->edge
[i
].src
->scc
;
2833 graph
->dst_scc
= graph
->edge
[i
].dst
->scc
;
2839 /* Check whether the next schedule row of the given node needs to be
2840 * non-trivial. Lower-dimensional domains may have some trivial rows,
2841 * but as soon as the number of remaining required non-trivial rows
2842 * is as large as the number or remaining rows to be computed,
2843 * all remaining rows need to be non-trivial.
2845 static int needs_row(struct isl_sched_graph
*graph
, struct isl_sched_node
*node
)
2847 return node
->nvar
- node
->rank
>= graph
->maxvar
- graph
->n_row
;
2850 /* Construct a non-triviality region with triviality directions
2851 * corresponding to the rows of "indep".
2852 * The rows of "indep" are expressed in terms of the schedule coefficients c_i,
2853 * while the triviality directions are expressed in terms of
2854 * pairs of non-negative variables c^+_i - c^-_i, with c^-_i appearing
2855 * before c^+_i. Furthermore,
2856 * the pairs of non-negative variables representing the coefficients
2857 * are stored in the opposite order.
2859 static __isl_give isl_mat
*construct_trivial(__isl_keep isl_mat
*indep
)
2868 ctx
= isl_mat_get_ctx(indep
);
2869 n
= isl_mat_rows(indep
);
2870 n_var
= isl_mat_cols(indep
);
2871 mat
= isl_mat_alloc(ctx
, n
, 2 * n_var
);
2874 for (i
= 0; i
< n
; ++i
) {
2875 for (j
= 0; j
< n_var
; ++j
) {
2876 int nj
= n_var
- 1 - j
;
2877 isl_int_neg(mat
->row
[i
][2 * nj
], indep
->row
[i
][j
]);
2878 isl_int_set(mat
->row
[i
][2 * nj
+ 1], indep
->row
[i
][j
]);
2885 /* Solve the ILP problem constructed in setup_lp.
2886 * For each node such that all the remaining rows of its schedule
2887 * need to be non-trivial, we construct a non-triviality region.
2888 * This region imposes that the next row is independent of previous rows.
2889 * In particular, the non-triviality region enforces that at least
2890 * one of the linear combinations in the rows of node->indep is non-zero.
2892 static __isl_give isl_vec
*solve_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2898 for (i
= 0; i
< graph
->n
; ++i
) {
2899 struct isl_sched_node
*node
= &graph
->node
[i
];
2902 graph
->region
[i
].pos
= node_var_coef_offset(node
);
2903 if (needs_row(graph
, node
))
2904 trivial
= construct_trivial(node
->indep
);
2906 trivial
= isl_mat_zero(ctx
, 0, 0);
2907 graph
->region
[i
].trivial
= trivial
;
2909 lp
= isl_basic_set_copy(graph
->lp
);
2910 sol
= isl_tab_basic_set_non_trivial_lexmin(lp
, 2, graph
->n
,
2911 graph
->region
, &check_conflict
, graph
);
2912 for (i
= 0; i
< graph
->n
; ++i
)
2913 isl_mat_free(graph
->region
[i
].trivial
);
2917 /* Extract the coefficients for the variables of "node" from "sol".
2919 * Each schedule coefficient c_i_x is represented as the difference
2920 * between two non-negative variables c_i_x^+ - c_i_x^-.
2921 * The c_i_x^- appear before their c_i_x^+ counterpart.
2922 * Furthermore, the order of these pairs is the opposite of that
2923 * of the corresponding coefficients.
2925 * Return c_i_x = c_i_x^+ - c_i_x^-
2927 static __isl_give isl_vec
*extract_var_coef(struct isl_sched_node
*node
,
2928 __isl_keep isl_vec
*sol
)
2936 csol
= isl_vec_alloc(isl_vec_get_ctx(sol
), node
->nvar
);
2940 pos
= 1 + node_var_coef_offset(node
);
2941 for (i
= 0; i
< node
->nvar
; ++i
)
2942 isl_int_sub(csol
->el
[node
->nvar
- 1 - i
],
2943 sol
->el
[pos
+ 2 * i
+ 1], sol
->el
[pos
+ 2 * i
]);
2948 /* Update the schedules of all nodes based on the given solution
2949 * of the LP problem.
2950 * The new row is added to the current band.
2951 * All possibly negative coefficients are encoded as a difference
2952 * of two non-negative variables, so we need to perform the subtraction
2955 * If coincident is set, then the caller guarantees that the new
2956 * row satisfies the coincidence constraints.
2958 static int update_schedule(struct isl_sched_graph
*graph
,
2959 __isl_take isl_vec
*sol
, int coincident
)
2962 isl_vec
*csol
= NULL
;
2967 isl_die(sol
->ctx
, isl_error_internal
,
2968 "no solution found", goto error
);
2969 if (graph
->n_total_row
>= graph
->max_row
)
2970 isl_die(sol
->ctx
, isl_error_internal
,
2971 "too many schedule rows", goto error
);
2973 for (i
= 0; i
< graph
->n
; ++i
) {
2974 struct isl_sched_node
*node
= &graph
->node
[i
];
2976 int row
= isl_mat_rows(node
->sched
);
2979 csol
= extract_var_coef(node
, sol
);
2983 isl_map_free(node
->sched_map
);
2984 node
->sched_map
= NULL
;
2985 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
2988 pos
= node_cst_coef_offset(node
);
2989 node
->sched
= isl_mat_set_element(node
->sched
,
2990 row
, 0, sol
->el
[1 + pos
]);
2991 pos
= node_par_coef_offset(node
);
2992 for (j
= 0; j
< node
->nparam
; ++j
)
2993 node
->sched
= isl_mat_set_element(node
->sched
,
2994 row
, 1 + j
, sol
->el
[1 + pos
+ j
]);
2995 for (j
= 0; j
< node
->nvar
; ++j
)
2996 node
->sched
= isl_mat_set_element(node
->sched
,
2997 row
, 1 + node
->nparam
+ j
, csol
->el
[j
]);
2998 node
->coincident
[graph
->n_total_row
] = coincident
;
3004 graph
->n_total_row
++;
3013 /* Convert row "row" of node->sched into an isl_aff living in "ls"
3014 * and return this isl_aff.
3016 static __isl_give isl_aff
*extract_schedule_row(__isl_take isl_local_space
*ls
,
3017 struct isl_sched_node
*node
, int row
)
3025 aff
= isl_aff_zero_on_domain(ls
);
3026 if (isl_mat_get_element(node
->sched
, row
, 0, &v
) < 0)
3028 aff
= isl_aff_set_constant(aff
, v
);
3029 for (j
= 0; j
< node
->nparam
; ++j
) {
3030 if (isl_mat_get_element(node
->sched
, row
, 1 + j
, &v
) < 0)
3032 aff
= isl_aff_set_coefficient(aff
, isl_dim_param
, j
, v
);
3034 for (j
= 0; j
< node
->nvar
; ++j
) {
3035 if (isl_mat_get_element(node
->sched
, row
,
3036 1 + node
->nparam
+ j
, &v
) < 0)
3038 aff
= isl_aff_set_coefficient(aff
, isl_dim_in
, j
, v
);
3050 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
3051 * and return this multi_aff.
3053 * The result is defined over the uncompressed node domain.
3055 static __isl_give isl_multi_aff
*node_extract_partial_schedule_multi_aff(
3056 struct isl_sched_node
*node
, int first
, int n
)
3060 isl_local_space
*ls
;
3067 nrow
= isl_mat_rows(node
->sched
);
3068 if (node
->compressed
)
3069 space
= isl_multi_aff_get_domain_space(node
->decompress
);
3071 space
= isl_space_copy(node
->space
);
3072 ls
= isl_local_space_from_space(isl_space_copy(space
));
3073 space
= isl_space_from_domain(space
);
3074 space
= isl_space_add_dims(space
, isl_dim_out
, n
);
3075 ma
= isl_multi_aff_zero(space
);
3077 for (i
= first
; i
< first
+ n
; ++i
) {
3078 aff
= extract_schedule_row(isl_local_space_copy(ls
), node
, i
);
3079 ma
= isl_multi_aff_set_aff(ma
, i
- first
, aff
);
3082 isl_local_space_free(ls
);
3084 if (node
->compressed
)
3085 ma
= isl_multi_aff_pullback_multi_aff(ma
,
3086 isl_multi_aff_copy(node
->compress
));
3091 /* Convert node->sched into a multi_aff and return this multi_aff.
3093 * The result is defined over the uncompressed node domain.
3095 static __isl_give isl_multi_aff
*node_extract_schedule_multi_aff(
3096 struct isl_sched_node
*node
)
3100 nrow
= isl_mat_rows(node
->sched
);
3101 return node_extract_partial_schedule_multi_aff(node
, 0, nrow
);
3104 /* Convert node->sched into a map and return this map.
3106 * The result is cached in node->sched_map, which needs to be released
3107 * whenever node->sched is updated.
3108 * It is defined over the uncompressed node domain.
3110 static __isl_give isl_map
*node_extract_schedule(struct isl_sched_node
*node
)
3112 if (!node
->sched_map
) {
3115 ma
= node_extract_schedule_multi_aff(node
);
3116 node
->sched_map
= isl_map_from_multi_aff(ma
);
3119 return isl_map_copy(node
->sched_map
);
3122 /* Construct a map that can be used to update a dependence relation
3123 * based on the current schedule.
3124 * That is, construct a map expressing that source and sink
3125 * are executed within the same iteration of the current schedule.
3126 * This map can then be intersected with the dependence relation.
3127 * This is not the most efficient way, but this shouldn't be a critical
3130 static __isl_give isl_map
*specializer(struct isl_sched_node
*src
,
3131 struct isl_sched_node
*dst
)
3133 isl_map
*src_sched
, *dst_sched
;
3135 src_sched
= node_extract_schedule(src
);
3136 dst_sched
= node_extract_schedule(dst
);
3137 return isl_map_apply_range(src_sched
, isl_map_reverse(dst_sched
));
3140 /* Intersect the domains of the nested relations in domain and range
3141 * of "umap" with "map".
3143 static __isl_give isl_union_map
*intersect_domains(
3144 __isl_take isl_union_map
*umap
, __isl_keep isl_map
*map
)
3146 isl_union_set
*uset
;
3148 umap
= isl_union_map_zip(umap
);
3149 uset
= isl_union_set_from_set(isl_map_wrap(isl_map_copy(map
)));
3150 umap
= isl_union_map_intersect_domain(umap
, uset
);
3151 umap
= isl_union_map_zip(umap
);
3155 /* Update the dependence relation of the given edge based
3156 * on the current schedule.
3157 * If the dependence is carried completely by the current schedule, then
3158 * it is removed from the edge_tables. It is kept in the list of edges
3159 * as otherwise all edge_tables would have to be recomputed.
3161 * If the edge is of a type that can appear multiple times
3162 * between the same pair of nodes, then it is added to
3163 * the edge table (again). This prevents the situation
3164 * where none of these edges is referenced from the edge table
3165 * because the one that was referenced turned out to be empty and
3166 * was therefore removed from the table.
3168 static isl_stat
update_edge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
3169 struct isl_sched_edge
*edge
)
3174 id
= specializer(edge
->src
, edge
->dst
);
3175 edge
->map
= isl_map_intersect(edge
->map
, isl_map_copy(id
));
3179 if (edge
->tagged_condition
) {
3180 edge
->tagged_condition
=
3181 intersect_domains(edge
->tagged_condition
, id
);
3182 if (!edge
->tagged_condition
)
3185 if (edge
->tagged_validity
) {
3186 edge
->tagged_validity
=
3187 intersect_domains(edge
->tagged_validity
, id
);
3188 if (!edge
->tagged_validity
)
3192 empty
= isl_map_plain_is_empty(edge
->map
);
3196 graph_remove_edge(graph
, edge
);
3197 } else if (is_multi_edge_type(edge
)) {
3198 if (graph_edge_tables_add(ctx
, graph
, edge
) < 0)
3206 return isl_stat_error
;
3209 /* Does the domain of "umap" intersect "uset"?
3211 static int domain_intersects(__isl_keep isl_union_map
*umap
,
3212 __isl_keep isl_union_set
*uset
)
3216 umap
= isl_union_map_copy(umap
);
3217 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(uset
));
3218 empty
= isl_union_map_is_empty(umap
);
3219 isl_union_map_free(umap
);
3221 return empty
< 0 ? -1 : !empty
;
3224 /* Does the range of "umap" intersect "uset"?
3226 static int range_intersects(__isl_keep isl_union_map
*umap
,
3227 __isl_keep isl_union_set
*uset
)
3231 umap
= isl_union_map_copy(umap
);
3232 umap
= isl_union_map_intersect_range(umap
, isl_union_set_copy(uset
));
3233 empty
= isl_union_map_is_empty(umap
);
3234 isl_union_map_free(umap
);
3236 return empty
< 0 ? -1 : !empty
;
3239 /* Are the condition dependences of "edge" local with respect to
3240 * the current schedule?
3242 * That is, are domain and range of the condition dependences mapped
3243 * to the same point?
3245 * In other words, is the condition false?
3247 static int is_condition_false(struct isl_sched_edge
*edge
)
3249 isl_union_map
*umap
;
3250 isl_map
*map
, *sched
, *test
;
3253 empty
= isl_union_map_is_empty(edge
->tagged_condition
);
3254 if (empty
< 0 || empty
)
3257 umap
= isl_union_map_copy(edge
->tagged_condition
);
3258 umap
= isl_union_map_zip(umap
);
3259 umap
= isl_union_set_unwrap(isl_union_map_domain(umap
));
3260 map
= isl_map_from_union_map(umap
);
3262 sched
= node_extract_schedule(edge
->src
);
3263 map
= isl_map_apply_domain(map
, sched
);
3264 sched
= node_extract_schedule(edge
->dst
);
3265 map
= isl_map_apply_range(map
, sched
);
3267 test
= isl_map_identity(isl_map_get_space(map
));
3268 local
= isl_map_is_subset(map
, test
);
3275 /* For each conditional validity constraint that is adjacent
3276 * to a condition with domain in condition_source or range in condition_sink,
3277 * turn it into an unconditional validity constraint.
3279 static int unconditionalize_adjacent_validity(struct isl_sched_graph
*graph
,
3280 __isl_take isl_union_set
*condition_source
,
3281 __isl_take isl_union_set
*condition_sink
)
3285 condition_source
= isl_union_set_coalesce(condition_source
);
3286 condition_sink
= isl_union_set_coalesce(condition_sink
);
3288 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3290 isl_union_map
*validity
;
3292 if (!is_conditional_validity(&graph
->edge
[i
]))
3294 if (is_validity(&graph
->edge
[i
]))
3297 validity
= graph
->edge
[i
].tagged_validity
;
3298 adjacent
= domain_intersects(validity
, condition_sink
);
3299 if (adjacent
>= 0 && !adjacent
)
3300 adjacent
= range_intersects(validity
, condition_source
);
3306 set_validity(&graph
->edge
[i
]);
3309 isl_union_set_free(condition_source
);
3310 isl_union_set_free(condition_sink
);
3313 isl_union_set_free(condition_source
);
3314 isl_union_set_free(condition_sink
);
3318 /* Update the dependence relations of all edges based on the current schedule
3319 * and enforce conditional validity constraints that are adjacent
3320 * to satisfied condition constraints.
3322 * First check if any of the condition constraints are satisfied
3323 * (i.e., not local to the outer schedule) and keep track of
3324 * their domain and range.
3325 * Then update all dependence relations (which removes the non-local
3327 * Finally, if any condition constraints turned out to be satisfied,
3328 * then turn all adjacent conditional validity constraints into
3329 * unconditional validity constraints.
3331 static int update_edges(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3335 isl_union_set
*source
, *sink
;
3337 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3338 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3339 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3341 isl_union_set
*uset
;
3342 isl_union_map
*umap
;
3344 if (!is_condition(&graph
->edge
[i
]))
3346 if (is_local(&graph
->edge
[i
]))
3348 local
= is_condition_false(&graph
->edge
[i
]);
3356 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
3357 uset
= isl_union_map_domain(umap
);
3358 source
= isl_union_set_union(source
, uset
);
3360 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
3361 uset
= isl_union_map_range(umap
);
3362 sink
= isl_union_set_union(sink
, uset
);
3365 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3366 if (update_edge(ctx
, graph
, &graph
->edge
[i
]) < 0)
3371 return unconditionalize_adjacent_validity(graph
, source
, sink
);
3373 isl_union_set_free(source
);
3374 isl_union_set_free(sink
);
3377 isl_union_set_free(source
);
3378 isl_union_set_free(sink
);
3382 static void next_band(struct isl_sched_graph
*graph
)
3384 graph
->band_start
= graph
->n_total_row
;
3387 /* Return the union of the universe domains of the nodes in "graph"
3388 * that satisfy "pred".
3390 static __isl_give isl_union_set
*isl_sched_graph_domain(isl_ctx
*ctx
,
3391 struct isl_sched_graph
*graph
,
3392 int (*pred
)(struct isl_sched_node
*node
, int data
), int data
)
3398 for (i
= 0; i
< graph
->n
; ++i
)
3399 if (pred(&graph
->node
[i
], data
))
3403 isl_die(ctx
, isl_error_internal
,
3404 "empty component", return NULL
);
3406 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
3407 dom
= isl_union_set_from_set(set
);
3409 for (i
= i
+ 1; i
< graph
->n
; ++i
) {
3410 if (!pred(&graph
->node
[i
], data
))
3412 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
3413 dom
= isl_union_set_union(dom
, isl_union_set_from_set(set
));
3419 /* Return a list of unions of universe domains, where each element
3420 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
3422 static __isl_give isl_union_set_list
*extract_sccs(isl_ctx
*ctx
,
3423 struct isl_sched_graph
*graph
)
3426 isl_union_set_list
*filters
;
3428 filters
= isl_union_set_list_alloc(ctx
, graph
->scc
);
3429 for (i
= 0; i
< graph
->scc
; ++i
) {
3432 dom
= isl_sched_graph_domain(ctx
, graph
, &node_scc_exactly
, i
);
3433 filters
= isl_union_set_list_add(filters
, dom
);
3439 /* Return a list of two unions of universe domains, one for the SCCs up
3440 * to and including graph->src_scc and another for the other SCCs.
3442 static __isl_give isl_union_set_list
*extract_split(isl_ctx
*ctx
,
3443 struct isl_sched_graph
*graph
)
3446 isl_union_set_list
*filters
;
3448 filters
= isl_union_set_list_alloc(ctx
, 2);
3449 dom
= isl_sched_graph_domain(ctx
, graph
,
3450 &node_scc_at_most
, graph
->src_scc
);
3451 filters
= isl_union_set_list_add(filters
, dom
);
3452 dom
= isl_sched_graph_domain(ctx
, graph
,
3453 &node_scc_at_least
, graph
->src_scc
+ 1);
3454 filters
= isl_union_set_list_add(filters
, dom
);
3459 /* Copy nodes that satisfy node_pred from the src dependence graph
3460 * to the dst dependence graph.
3462 static isl_stat
copy_nodes(struct isl_sched_graph
*dst
,
3463 struct isl_sched_graph
*src
,
3464 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
3469 for (i
= 0; i
< src
->n
; ++i
) {
3472 if (!node_pred(&src
->node
[i
], data
))
3476 dst
->node
[j
].space
= isl_space_copy(src
->node
[i
].space
);
3477 dst
->node
[j
].compressed
= src
->node
[i
].compressed
;
3478 dst
->node
[j
].hull
= isl_set_copy(src
->node
[i
].hull
);
3479 dst
->node
[j
].compress
=
3480 isl_multi_aff_copy(src
->node
[i
].compress
);
3481 dst
->node
[j
].decompress
=
3482 isl_multi_aff_copy(src
->node
[i
].decompress
);
3483 dst
->node
[j
].nvar
= src
->node
[i
].nvar
;
3484 dst
->node
[j
].nparam
= src
->node
[i
].nparam
;
3485 dst
->node
[j
].sched
= isl_mat_copy(src
->node
[i
].sched
);
3486 dst
->node
[j
].sched_map
= isl_map_copy(src
->node
[i
].sched_map
);
3487 dst
->node
[j
].coincident
= src
->node
[i
].coincident
;
3488 dst
->node
[j
].sizes
= isl_multi_val_copy(src
->node
[i
].sizes
);
3489 dst
->node
[j
].bounds
= isl_basic_set_copy(src
->node
[i
].bounds
);
3490 dst
->node
[j
].max
= isl_vec_copy(src
->node
[i
].max
);
3493 if (!dst
->node
[j
].space
|| !dst
->node
[j
].sched
)
3494 return isl_stat_error
;
3495 if (dst
->node
[j
].compressed
&&
3496 (!dst
->node
[j
].hull
|| !dst
->node
[j
].compress
||
3497 !dst
->node
[j
].decompress
))
3498 return isl_stat_error
;
3504 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
3505 * to the dst dependence graph.
3506 * If the source or destination node of the edge is not in the destination
3507 * graph, then it must be a backward proximity edge and it should simply
3510 static isl_stat
copy_edges(isl_ctx
*ctx
, struct isl_sched_graph
*dst
,
3511 struct isl_sched_graph
*src
,
3512 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
), int data
)
3517 for (i
= 0; i
< src
->n_edge
; ++i
) {
3518 struct isl_sched_edge
*edge
= &src
->edge
[i
];
3520 isl_union_map
*tagged_condition
;
3521 isl_union_map
*tagged_validity
;
3522 struct isl_sched_node
*dst_src
, *dst_dst
;
3524 if (!edge_pred(edge
, data
))
3527 if (isl_map_plain_is_empty(edge
->map
))
3530 dst_src
= graph_find_node(ctx
, dst
, edge
->src
->space
);
3531 dst_dst
= graph_find_node(ctx
, dst
, edge
->dst
->space
);
3532 if (!dst_src
|| !dst_dst
)
3533 return isl_stat_error
;
3534 if (!is_node(dst
, dst_src
) || !is_node(dst
, dst_dst
)) {
3535 if (is_validity(edge
) || is_conditional_validity(edge
))
3536 isl_die(ctx
, isl_error_internal
,
3537 "backward (conditional) validity edge",
3538 return isl_stat_error
);
3542 map
= isl_map_copy(edge
->map
);
3543 tagged_condition
= isl_union_map_copy(edge
->tagged_condition
);
3544 tagged_validity
= isl_union_map_copy(edge
->tagged_validity
);
3546 dst
->edge
[dst
->n_edge
].src
= dst_src
;
3547 dst
->edge
[dst
->n_edge
].dst
= dst_dst
;
3548 dst
->edge
[dst
->n_edge
].map
= map
;
3549 dst
->edge
[dst
->n_edge
].tagged_condition
= tagged_condition
;
3550 dst
->edge
[dst
->n_edge
].tagged_validity
= tagged_validity
;
3551 dst
->edge
[dst
->n_edge
].types
= edge
->types
;
3554 if (edge
->tagged_condition
&& !tagged_condition
)
3555 return isl_stat_error
;
3556 if (edge
->tagged_validity
&& !tagged_validity
)
3557 return isl_stat_error
;
3559 if (graph_edge_tables_add(ctx
, dst
,
3560 &dst
->edge
[dst
->n_edge
- 1]) < 0)
3561 return isl_stat_error
;
3567 /* Compute the maximal number of variables over all nodes.
3568 * This is the maximal number of linearly independent schedule
3569 * rows that we need to compute.
3570 * Just in case we end up in a part of the dependence graph
3571 * with only lower-dimensional domains, we make sure we will
3572 * compute the required amount of extra linearly independent rows.
3574 static int compute_maxvar(struct isl_sched_graph
*graph
)
3579 for (i
= 0; i
< graph
->n
; ++i
) {
3580 struct isl_sched_node
*node
= &graph
->node
[i
];
3583 if (node_update_vmap(node
) < 0)
3585 nvar
= node
->nvar
+ graph
->n_row
- node
->rank
;
3586 if (nvar
> graph
->maxvar
)
3587 graph
->maxvar
= nvar
;
3593 /* Extract the subgraph of "graph" that consists of the nodes satisfying
3594 * "node_pred" and the edges satisfying "edge_pred" and store
3595 * the result in "sub".
3597 static isl_stat
extract_sub_graph(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
3598 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
3599 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
3600 int data
, struct isl_sched_graph
*sub
)
3602 int i
, n
= 0, n_edge
= 0;
3605 for (i
= 0; i
< graph
->n
; ++i
)
3606 if (node_pred(&graph
->node
[i
], data
))
3608 for (i
= 0; i
< graph
->n_edge
; ++i
)
3609 if (edge_pred(&graph
->edge
[i
], data
))
3611 if (graph_alloc(ctx
, sub
, n
, n_edge
) < 0)
3612 return isl_stat_error
;
3613 sub
->root
= graph
->root
;
3614 if (copy_nodes(sub
, graph
, node_pred
, data
) < 0)
3615 return isl_stat_error
;
3616 if (graph_init_table(ctx
, sub
) < 0)
3617 return isl_stat_error
;
3618 for (t
= 0; t
<= isl_edge_last
; ++t
)
3619 sub
->max_edge
[t
] = graph
->max_edge
[t
];
3620 if (graph_init_edge_tables(ctx
, sub
) < 0)
3621 return isl_stat_error
;
3622 if (copy_edges(ctx
, sub
, graph
, edge_pred
, data
) < 0)
3623 return isl_stat_error
;
3624 sub
->n_row
= graph
->n_row
;
3625 sub
->max_row
= graph
->max_row
;
3626 sub
->n_total_row
= graph
->n_total_row
;
3627 sub
->band_start
= graph
->band_start
;
3632 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
3633 struct isl_sched_graph
*graph
);
3634 static __isl_give isl_schedule_node
*compute_schedule_wcc(
3635 isl_schedule_node
*node
, struct isl_sched_graph
*graph
);
3637 /* Compute a schedule for a subgraph of "graph". In particular, for
3638 * the graph composed of nodes that satisfy node_pred and edges that
3639 * that satisfy edge_pred.
3640 * If the subgraph is known to consist of a single component, then wcc should
3641 * be set and then we call compute_schedule_wcc on the constructed subgraph.
3642 * Otherwise, we call compute_schedule, which will check whether the subgraph
3645 * The schedule is inserted at "node" and the updated schedule node
3648 static __isl_give isl_schedule_node
*compute_sub_schedule(
3649 __isl_take isl_schedule_node
*node
, isl_ctx
*ctx
,
3650 struct isl_sched_graph
*graph
,
3651 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
3652 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
3655 struct isl_sched_graph split
= { 0 };
3657 if (extract_sub_graph(ctx
, graph
, node_pred
, edge_pred
, data
,
3662 node
= compute_schedule_wcc(node
, &split
);
3664 node
= compute_schedule(node
, &split
);
3666 graph_free(ctx
, &split
);
3669 graph_free(ctx
, &split
);
3670 return isl_schedule_node_free(node
);
3673 static int edge_scc_exactly(struct isl_sched_edge
*edge
, int scc
)
3675 return edge
->src
->scc
== scc
&& edge
->dst
->scc
== scc
;
3678 static int edge_dst_scc_at_most(struct isl_sched_edge
*edge
, int scc
)
3680 return edge
->dst
->scc
<= scc
;
3683 static int edge_src_scc_at_least(struct isl_sched_edge
*edge
, int scc
)
3685 return edge
->src
->scc
>= scc
;
3688 /* Reset the current band by dropping all its schedule rows.
3690 static isl_stat
reset_band(struct isl_sched_graph
*graph
)
3695 drop
= graph
->n_total_row
- graph
->band_start
;
3696 graph
->n_total_row
-= drop
;
3697 graph
->n_row
-= drop
;
3699 for (i
= 0; i
< graph
->n
; ++i
) {
3700 struct isl_sched_node
*node
= &graph
->node
[i
];
3702 isl_map_free(node
->sched_map
);
3703 node
->sched_map
= NULL
;
3705 node
->sched
= isl_mat_drop_rows(node
->sched
,
3706 graph
->band_start
, drop
);
3709 return isl_stat_error
;
3715 /* Split the current graph into two parts and compute a schedule for each
3716 * part individually. In particular, one part consists of all SCCs up
3717 * to and including graph->src_scc, while the other part contains the other
3718 * SCCs. The split is enforced by a sequence node inserted at position "node"
3719 * in the schedule tree. Return the updated schedule node.
3720 * If either of these two parts consists of a sequence, then it is spliced
3721 * into the sequence containing the two parts.
3723 * The current band is reset. It would be possible to reuse
3724 * the previously computed rows as the first rows in the next
3725 * band, but recomputing them may result in better rows as we are looking
3726 * at a smaller part of the dependence graph.
3728 static __isl_give isl_schedule_node
*compute_split_schedule(
3729 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3733 isl_union_set_list
*filters
;
3738 if (reset_band(graph
) < 0)
3739 return isl_schedule_node_free(node
);
3743 ctx
= isl_schedule_node_get_ctx(node
);
3744 filters
= extract_split(ctx
, graph
);
3745 node
= isl_schedule_node_insert_sequence(node
, filters
);
3746 node
= isl_schedule_node_child(node
, 1);
3747 node
= isl_schedule_node_child(node
, 0);
3749 node
= compute_sub_schedule(node
, ctx
, graph
,
3750 &node_scc_at_least
, &edge_src_scc_at_least
,
3751 graph
->src_scc
+ 1, 0);
3752 is_seq
= isl_schedule_node_get_type(node
) == isl_schedule_node_sequence
;
3753 node
= isl_schedule_node_parent(node
);
3754 node
= isl_schedule_node_parent(node
);
3756 node
= isl_schedule_node_sequence_splice_child(node
, 1);
3757 node
= isl_schedule_node_child(node
, 0);
3758 node
= isl_schedule_node_child(node
, 0);
3759 node
= compute_sub_schedule(node
, ctx
, graph
,
3760 &node_scc_at_most
, &edge_dst_scc_at_most
,
3762 is_seq
= isl_schedule_node_get_type(node
) == isl_schedule_node_sequence
;
3763 node
= isl_schedule_node_parent(node
);
3764 node
= isl_schedule_node_parent(node
);
3766 node
= isl_schedule_node_sequence_splice_child(node
, 0);
3771 /* Insert a band node at position "node" in the schedule tree corresponding
3772 * to the current band in "graph". Mark the band node permutable
3773 * if "permutable" is set.
3774 * The partial schedules and the coincidence property are extracted
3775 * from the graph nodes.
3776 * Return the updated schedule node.
3778 static __isl_give isl_schedule_node
*insert_current_band(
3779 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
3785 isl_multi_pw_aff
*mpa
;
3786 isl_multi_union_pw_aff
*mupa
;
3792 isl_die(isl_schedule_node_get_ctx(node
), isl_error_internal
,
3793 "graph should have at least one node",
3794 return isl_schedule_node_free(node
));
3796 start
= graph
->band_start
;
3797 end
= graph
->n_total_row
;
3800 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[0], start
, n
);
3801 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3802 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3804 for (i
= 1; i
< graph
->n
; ++i
) {
3805 isl_multi_union_pw_aff
*mupa_i
;
3807 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[i
],
3809 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3810 mupa_i
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3811 mupa
= isl_multi_union_pw_aff_union_add(mupa
, mupa_i
);
3813 node
= isl_schedule_node_insert_partial_schedule(node
, mupa
);
3815 for (i
= 0; i
< n
; ++i
)
3816 node
= isl_schedule_node_band_member_set_coincident(node
, i
,
3817 graph
->node
[0].coincident
[start
+ i
]);
3818 node
= isl_schedule_node_band_set_permutable(node
, permutable
);
3823 /* Update the dependence relations based on the current schedule,
3824 * add the current band to "node" and then continue with the computation
3826 * Return the updated schedule node.
3828 static __isl_give isl_schedule_node
*compute_next_band(
3829 __isl_take isl_schedule_node
*node
,
3830 struct isl_sched_graph
*graph
, int permutable
)
3837 ctx
= isl_schedule_node_get_ctx(node
);
3838 if (update_edges(ctx
, graph
) < 0)
3839 return isl_schedule_node_free(node
);
3840 node
= insert_current_band(node
, graph
, permutable
);
3843 node
= isl_schedule_node_child(node
, 0);
3844 node
= compute_schedule(node
, graph
);
3845 node
= isl_schedule_node_parent(node
);
3850 /* Add the constraints "coef" derived from an edge from "node" to itself
3851 * to graph->lp in order to respect the dependences and to try and carry them.
3852 * "pos" is the sequence number of the edge that needs to be carried.
3853 * "coef" represents general constraints on coefficients (c_0, c_x)
3854 * of valid constraints for (y - x) with x and y instances of the node.
3856 * The constraints added to graph->lp need to enforce
3858 * (c_j_0 + c_j_x y) - (c_j_0 + c_j_x x)
3859 * = c_j_x (y - x) >= e_i
3861 * for each (x,y) in the dependence relation of the edge.
3862 * That is, (-e_i, c_j_x) needs to be plugged in for (c_0, c_x),
3863 * taking into account that each coefficient in c_j_x is represented
3864 * as a pair of non-negative coefficients.
3866 static isl_stat
add_intra_constraints(struct isl_sched_graph
*graph
,
3867 struct isl_sched_node
*node
, __isl_take isl_basic_set
*coef
, int pos
)
3871 isl_dim_map
*dim_map
;
3874 return isl_stat_error
;
3876 ctx
= isl_basic_set_get_ctx(coef
);
3877 offset
= coef_var_offset(coef
);
3878 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, 1);
3879 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3880 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
3885 /* Add the constraints "coef" derived from an edge from "src" to "dst"
3886 * to graph->lp in order to respect the dependences and to try and carry them.
3887 * "pos" is the sequence number of the edge that needs to be carried or
3888 * -1 if no attempt should be made to carry the dependences.
3889 * "coef" represents general constraints on coefficients (c_0, c_n, c_x, c_y)
3890 * of valid constraints for (x, y) with x and y instances of "src" and "dst".
3892 * The constraints added to graph->lp need to enforce
3894 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3896 * for each (x,y) in the dependence relation of the edge or
3898 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= 0
3902 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, -c_j_x, c_k_x)
3904 * (c_k_0 - c_j_0, c_k_n - c_j_n, -c_j_x, c_k_x)
3905 * needs to be plugged in for (c_0, c_n, c_x, c_y),
3906 * taking into account that each coefficient in c_j_x and c_k_x is represented
3907 * as a pair of non-negative coefficients.
3909 static isl_stat
add_inter_constraints(struct isl_sched_graph
*graph
,
3910 struct isl_sched_node
*src
, struct isl_sched_node
*dst
,
3911 __isl_take isl_basic_set
*coef
, int pos
)
3915 isl_dim_map
*dim_map
;
3918 return isl_stat_error
;
3920 ctx
= isl_basic_set_get_ctx(coef
);
3921 offset
= coef_var_offset(coef
);
3922 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, 1);
3924 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3925 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
3930 /* Data structure for keeping track of the data needed
3931 * to exploit non-trivial lineality spaces.
3933 * "any_non_trivial" is true if there are any non-trivial lineality spaces.
3934 * If "any_non_trivial" is not true, then "equivalent" and "mask" may be NULL.
3935 * "equivalent" connects instances to other instances on the same line(s).
3936 * "mask" contains the domain spaces of "equivalent".
3937 * Any instance set not in "mask" does not have a non-trivial lineality space.
3939 struct isl_exploit_lineality_data
{
3940 isl_bool any_non_trivial
;
3941 isl_union_map
*equivalent
;
3942 isl_union_set
*mask
;
3945 /* Data structure collecting information used during the construction
3946 * of an LP for carrying dependences.
3948 * "intra" is a sequence of coefficient constraints for intra-node edges.
3949 * "inter" is a sequence of coefficient constraints for inter-node edges.
3950 * "lineality" contains data used to exploit non-trivial lineality spaces.
3953 isl_basic_set_list
*intra
;
3954 isl_basic_set_list
*inter
;
3955 struct isl_exploit_lineality_data lineality
;
3958 /* Free all the data stored in "carry".
3960 static void isl_carry_clear(struct isl_carry
*carry
)
3962 isl_basic_set_list_free(carry
->intra
);
3963 isl_basic_set_list_free(carry
->inter
);
3964 isl_union_map_free(carry
->lineality
.equivalent
);
3965 isl_union_set_free(carry
->lineality
.mask
);
3968 /* Return a pointer to the node in "graph" that lives in "space".
3969 * If the requested node has been compressed, then "space"
3970 * corresponds to the compressed space.
3971 * The graph is assumed to have such a node.
3972 * Return NULL in case of error.
3974 * First try and see if "space" is the space of an uncompressed node.
3975 * If so, return that node.
3976 * Otherwise, "space" was constructed by construct_compressed_id and
3977 * contains a user pointer pointing to the node in the tuple id.
3978 * However, this node belongs to the original dependence graph.
3979 * If "graph" is a subgraph of this original dependence graph,
3980 * then the node with the same space still needs to be looked up
3981 * in the current graph.
3983 static struct isl_sched_node
*graph_find_compressed_node(isl_ctx
*ctx
,
3984 struct isl_sched_graph
*graph
, __isl_keep isl_space
*space
)
3987 struct isl_sched_node
*node
;
3992 node
= graph_find_node(ctx
, graph
, space
);
3995 if (is_node(graph
, node
))
3998 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
3999 node
= isl_id_get_user(id
);
4005 if (!is_node(graph
->root
, node
))
4006 isl_die(ctx
, isl_error_internal
,
4007 "space points to invalid node", return NULL
);
4008 if (graph
!= graph
->root
)
4009 node
= graph_find_node(ctx
, graph
, node
->space
);
4010 if (!is_node(graph
, node
))
4011 isl_die(ctx
, isl_error_internal
,
4012 "unable to find node", return NULL
);
4017 /* Internal data structure for add_all_constraints.
4019 * "graph" is the schedule constraint graph for which an LP problem
4020 * is being constructed.
4021 * "carry_inter" indicates whether inter-node edges should be carried.
4022 * "pos" is the position of the next edge that needs to be carried.
4024 struct isl_add_all_constraints_data
{
4026 struct isl_sched_graph
*graph
;
4031 /* Add the constraints "coef" derived from an edge from a node to itself
4032 * to data->graph->lp in order to respect the dependences and
4033 * to try and carry them.
4035 * The space of "coef" is of the form
4037 * coefficients[[c_cst] -> S[c_x]]
4039 * with S[c_x] the (compressed) space of the node.
4040 * Extract the node from the space and call add_intra_constraints.
4042 static isl_stat
lp_add_intra(__isl_take isl_basic_set
*coef
, void *user
)
4044 struct isl_add_all_constraints_data
*data
= user
;
4046 struct isl_sched_node
*node
;
4048 space
= isl_basic_set_get_space(coef
);
4049 space
= isl_space_range(isl_space_unwrap(space
));
4050 node
= graph_find_compressed_node(data
->ctx
, data
->graph
, space
);
4051 isl_space_free(space
);
4052 return add_intra_constraints(data
->graph
, node
, coef
, data
->pos
++);
4055 /* Add the constraints "coef" derived from an edge from a node j
4056 * to a node k to data->graph->lp in order to respect the dependences and
4057 * to try and carry them (provided data->carry_inter is set).
4059 * The space of "coef" is of the form
4061 * coefficients[[c_cst, c_n] -> [S_j[c_x] -> S_k[c_y]]]
4063 * with S_j[c_x] and S_k[c_y] the (compressed) spaces of the nodes.
4064 * Extract the nodes from the space and call add_inter_constraints.
4066 static isl_stat
lp_add_inter(__isl_take isl_basic_set
*coef
, void *user
)
4068 struct isl_add_all_constraints_data
*data
= user
;
4069 isl_space
*space
, *dom
;
4070 struct isl_sched_node
*src
, *dst
;
4073 space
= isl_basic_set_get_space(coef
);
4074 space
= isl_space_unwrap(isl_space_range(isl_space_unwrap(space
)));
4075 dom
= isl_space_domain(isl_space_copy(space
));
4076 src
= graph_find_compressed_node(data
->ctx
, data
->graph
, dom
);
4077 isl_space_free(dom
);
4078 space
= isl_space_range(space
);
4079 dst
= graph_find_compressed_node(data
->ctx
, data
->graph
, space
);
4080 isl_space_free(space
);
4082 pos
= data
->carry_inter
? data
->pos
++ : -1;
4083 return add_inter_constraints(data
->graph
, src
, dst
, coef
, pos
);
4086 /* Add constraints to graph->lp that force all (conditional) validity
4087 * dependences to be respected and attempt to carry them.
4088 * "intra" is the sequence of coefficient constraints for intra-node edges.
4089 * "inter" is the sequence of coefficient constraints for inter-node edges.
4090 * "carry_inter" indicates whether inter-node edges should be carried or
4093 static isl_stat
add_all_constraints(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
4094 __isl_keep isl_basic_set_list
*intra
,
4095 __isl_keep isl_basic_set_list
*inter
, int carry_inter
)
4097 struct isl_add_all_constraints_data data
= { ctx
, graph
, carry_inter
};
4100 if (isl_basic_set_list_foreach(intra
, &lp_add_intra
, &data
) < 0)
4101 return isl_stat_error
;
4102 if (isl_basic_set_list_foreach(inter
, &lp_add_inter
, &data
) < 0)
4103 return isl_stat_error
;
4107 /* Internal data structure for count_all_constraints
4108 * for keeping track of the number of equality and inequality constraints.
4110 struct isl_sched_count
{
4115 /* Add the number of equality and inequality constraints of "bset"
4116 * to data->n_eq and data->n_ineq.
4118 static isl_stat
bset_update_count(__isl_take isl_basic_set
*bset
, void *user
)
4120 struct isl_sched_count
*data
= user
;
4122 return update_count(bset
, 1, &data
->n_eq
, &data
->n_ineq
);
4125 /* Count the number of equality and inequality constraints
4126 * that will be added to the carry_lp problem.
4127 * We count each edge exactly once.
4128 * "intra" is the sequence of coefficient constraints for intra-node edges.
4129 * "inter" is the sequence of coefficient constraints for inter-node edges.
4131 static isl_stat
count_all_constraints(__isl_keep isl_basic_set_list
*intra
,
4132 __isl_keep isl_basic_set_list
*inter
, int *n_eq
, int *n_ineq
)
4134 struct isl_sched_count data
;
4136 data
.n_eq
= data
.n_ineq
= 0;
4137 if (isl_basic_set_list_foreach(inter
, &bset_update_count
, &data
) < 0)
4138 return isl_stat_error
;
4139 if (isl_basic_set_list_foreach(intra
, &bset_update_count
, &data
) < 0)
4140 return isl_stat_error
;
4143 *n_ineq
= data
.n_ineq
;
4148 /* Construct an LP problem for finding schedule coefficients
4149 * such that the schedule carries as many validity dependences as possible.
4150 * In particular, for each dependence i, we bound the dependence distance
4151 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
4152 * of all e_i's. Dependences with e_i = 0 in the solution are simply
4153 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
4154 * "intra" is the sequence of coefficient constraints for intra-node edges.
4155 * "inter" is the sequence of coefficient constraints for inter-node edges.
4156 * "n_edge" is the total number of edges.
4157 * "carry_inter" indicates whether inter-node edges should be carried or
4158 * only respected. That is, if "carry_inter" is not set, then
4159 * no e_i variables are introduced for the inter-node edges.
4161 * All variables of the LP are non-negative. The actual coefficients
4162 * may be negative, so each coefficient is represented as the difference
4163 * of two non-negative variables. The negative part always appears
4164 * immediately before the positive part.
4165 * Other than that, the variables have the following order
4167 * - sum of (1 - e_i) over all edges
4168 * - sum of all c_n coefficients
4169 * (unconstrained when computing non-parametric schedules)
4170 * - sum of positive and negative parts of all c_x coefficients
4174 * - positive and negative parts of c_i_x, in opposite order
4175 * - c_i_n (if parametric)
4178 * The constraints are those from the (validity) edges plus three equalities
4179 * to express the sums and n_edge inequalities to express e_i <= 1.
4181 static isl_stat
setup_carry_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
4182 int n_edge
, __isl_keep isl_basic_set_list
*intra
,
4183 __isl_keep isl_basic_set_list
*inter
, int carry_inter
)
4192 for (i
= 0; i
< graph
->n
; ++i
) {
4193 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
4194 node
->start
= total
;
4195 total
+= 1 + node
->nparam
+ 2 * node
->nvar
;
4198 if (count_all_constraints(intra
, inter
, &n_eq
, &n_ineq
) < 0)
4199 return isl_stat_error
;
4201 dim
= isl_space_set_alloc(ctx
, 0, total
);
4202 isl_basic_set_free(graph
->lp
);
4205 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
4206 graph
->lp
= isl_basic_set_set_rational(graph
->lp
);
4208 k
= isl_basic_set_alloc_equality(graph
->lp
);
4210 return isl_stat_error
;
4211 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
4212 isl_int_set_si(graph
->lp
->eq
[k
][0], -n_edge
);
4213 isl_int_set_si(graph
->lp
->eq
[k
][1], 1);
4214 for (i
= 0; i
< n_edge
; ++i
)
4215 isl_int_set_si(graph
->lp
->eq
[k
][4 + i
], 1);
4217 if (add_param_sum_constraint(graph
, 1) < 0)
4218 return isl_stat_error
;
4219 if (add_var_sum_constraint(graph
, 2) < 0)
4220 return isl_stat_error
;
4222 for (i
= 0; i
< n_edge
; ++i
) {
4223 k
= isl_basic_set_alloc_inequality(graph
->lp
);
4225 return isl_stat_error
;
4226 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
4227 isl_int_set_si(graph
->lp
->ineq
[k
][4 + i
], -1);
4228 isl_int_set_si(graph
->lp
->ineq
[k
][0], 1);
4231 if (add_all_constraints(ctx
, graph
, intra
, inter
, carry_inter
) < 0)
4232 return isl_stat_error
;
4237 static __isl_give isl_schedule_node
*compute_component_schedule(
4238 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
4241 /* If the schedule_split_scaled option is set and if the linear
4242 * parts of the scheduling rows for all nodes in the graphs have
4243 * a non-trivial common divisor, then remove this
4244 * common divisor from the linear part.
4245 * Otherwise, insert a band node directly and continue with
4246 * the construction of the schedule.
4248 * If a non-trivial common divisor is found, then
4249 * the linear part is reduced and the remainder is ignored.
4250 * The pieces of the graph that are assigned different remainders
4251 * form (groups of) strongly connected components within
4252 * the scaled down band. If needed, they can therefore
4253 * be ordered along this remainder in a sequence node.
4254 * However, this ordering is not enforced here in order to allow
4255 * the scheduler to combine some of the strongly connected components.
4257 static __isl_give isl_schedule_node
*split_scaled(
4258 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4268 ctx
= isl_schedule_node_get_ctx(node
);
4269 if (!ctx
->opt
->schedule_split_scaled
)
4270 return compute_next_band(node
, graph
, 0);
4272 return compute_next_band(node
, graph
, 0);
4275 isl_int_init(gcd_i
);
4277 isl_int_set_si(gcd
, 0);
4279 row
= isl_mat_rows(graph
->node
[0].sched
) - 1;
4281 for (i
= 0; i
< graph
->n
; ++i
) {
4282 struct isl_sched_node
*node
= &graph
->node
[i
];
4283 int cols
= isl_mat_cols(node
->sched
);
4285 isl_seq_gcd(node
->sched
->row
[row
] + 1, cols
- 1, &gcd_i
);
4286 isl_int_gcd(gcd
, gcd
, gcd_i
);
4289 isl_int_clear(gcd_i
);
4291 if (isl_int_cmp_si(gcd
, 1) <= 0) {
4293 return compute_next_band(node
, graph
, 0);
4296 for (i
= 0; i
< graph
->n
; ++i
) {
4297 struct isl_sched_node
*node
= &graph
->node
[i
];
4299 isl_int_fdiv_q(node
->sched
->row
[row
][0],
4300 node
->sched
->row
[row
][0], gcd
);
4301 isl_int_mul(node
->sched
->row
[row
][0],
4302 node
->sched
->row
[row
][0], gcd
);
4303 node
->sched
= isl_mat_scale_down_row(node
->sched
, row
, gcd
);
4310 return compute_next_band(node
, graph
, 0);
4313 return isl_schedule_node_free(node
);
4316 /* Is the schedule row "sol" trivial on node "node"?
4317 * That is, is the solution zero on the dimensions linearly independent of
4318 * the previously found solutions?
4319 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
4321 * Each coefficient is represented as the difference between
4322 * two non-negative values in "sol".
4323 * We construct the schedule row s and check if it is linearly
4324 * independent of previously computed schedule rows
4325 * by computing T s, with T the linear combinations that are zero
4326 * on linearly dependent schedule rows.
4327 * If the result consists of all zeros, then the solution is trivial.
4329 static int is_trivial(struct isl_sched_node
*node
, __isl_keep isl_vec
*sol
)
4336 if (node
->nvar
== node
->rank
)
4339 node_sol
= extract_var_coef(node
, sol
);
4340 node_sol
= isl_mat_vec_product(isl_mat_copy(node
->indep
), node_sol
);
4344 trivial
= isl_seq_first_non_zero(node_sol
->el
,
4345 node
->nvar
- node
->rank
) == -1;
4347 isl_vec_free(node_sol
);
4352 /* Is the schedule row "sol" trivial on any node where it should
4354 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
4356 static int is_any_trivial(struct isl_sched_graph
*graph
,
4357 __isl_keep isl_vec
*sol
)
4361 for (i
= 0; i
< graph
->n
; ++i
) {
4362 struct isl_sched_node
*node
= &graph
->node
[i
];
4365 if (!needs_row(graph
, node
))
4367 trivial
= is_trivial(node
, sol
);
4368 if (trivial
< 0 || trivial
)
4375 /* Does the schedule represented by "sol" perform loop coalescing on "node"?
4376 * If so, return the position of the coalesced dimension.
4377 * Otherwise, return node->nvar or -1 on error.
4379 * In particular, look for pairs of coefficients c_i and c_j such that
4380 * |c_j/c_i| > ceil(size_i/2), i.e., |c_j| > |c_i * ceil(size_i/2)|.
4381 * If any such pair is found, then return i.
4382 * If size_i is infinity, then no check on c_i needs to be performed.
4384 static int find_node_coalescing(struct isl_sched_node
*node
,
4385 __isl_keep isl_vec
*sol
)
4391 if (node
->nvar
<= 1)
4394 csol
= extract_var_coef(node
, sol
);
4398 for (i
= 0; i
< node
->nvar
; ++i
) {
4401 if (isl_int_is_zero(csol
->el
[i
]))
4403 v
= isl_multi_val_get_val(node
->sizes
, i
);
4406 if (!isl_val_is_int(v
)) {
4410 v
= isl_val_div_ui(v
, 2);
4411 v
= isl_val_ceil(v
);
4414 isl_int_mul(max
, v
->n
, csol
->el
[i
]);
4417 for (j
= 0; j
< node
->nvar
; ++j
) {
4420 if (isl_int_abs_gt(csol
->el
[j
], max
))
4436 /* Force the schedule coefficient at position "pos" of "node" to be zero
4438 * The coefficient is encoded as the difference between two non-negative
4439 * variables. Force these two variables to have the same value.
4441 static __isl_give isl_tab_lexmin
*zero_out_node_coef(
4442 __isl_take isl_tab_lexmin
*tl
, struct isl_sched_node
*node
, int pos
)
4448 ctx
= isl_space_get_ctx(node
->space
);
4449 dim
= isl_tab_lexmin_dim(tl
);
4451 return isl_tab_lexmin_free(tl
);
4452 eq
= isl_vec_alloc(ctx
, 1 + dim
);
4453 eq
= isl_vec_clr(eq
);
4455 return isl_tab_lexmin_free(tl
);
4457 pos
= 1 + node_var_coef_pos(node
, pos
);
4458 isl_int_set_si(eq
->el
[pos
], 1);
4459 isl_int_set_si(eq
->el
[pos
+ 1], -1);
4460 tl
= isl_tab_lexmin_add_eq(tl
, eq
->el
);
4466 /* Return the lexicographically smallest rational point in the basic set
4467 * from which "tl" was constructed, double checking that this input set
4470 static __isl_give isl_vec
*non_empty_solution(__isl_keep isl_tab_lexmin
*tl
)
4474 sol
= isl_tab_lexmin_get_solution(tl
);
4478 isl_die(isl_vec_get_ctx(sol
), isl_error_internal
,
4479 "error in schedule construction",
4480 return isl_vec_free(sol
));
4484 /* Does the solution "sol" of the LP problem constructed by setup_carry_lp
4485 * carry any of the "n_edge" groups of dependences?
4486 * The value in the first position is the sum of (1 - e_i) over all "n_edge"
4487 * edges, with 0 <= e_i <= 1 equal to 1 when the dependences represented
4488 * by the edge are carried by the solution.
4489 * If the sum of the (1 - e_i) is smaller than "n_edge" then at least
4490 * one of those is carried.
4492 * Note that despite the fact that the problem is solved using a rational
4493 * solver, the solution is guaranteed to be integral.
4494 * Specifically, the dependence distance lower bounds e_i (and therefore
4495 * also their sum) are integers. See Lemma 5 of [1].
4497 * Any potential denominator of the sum is cleared by this function.
4498 * The denominator is not relevant for any of the other elements
4501 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4502 * Problem, Part II: Multi-Dimensional Time.
4503 * In Intl. Journal of Parallel Programming, 1992.
4505 static int carries_dependences(__isl_keep isl_vec
*sol
, int n_edge
)
4507 isl_int_divexact(sol
->el
[1], sol
->el
[1], sol
->el
[0]);
4508 isl_int_set_si(sol
->el
[0], 1);
4509 return isl_int_cmp_si(sol
->el
[1], n_edge
) < 0;
4512 /* Return the lexicographically smallest rational point in "lp",
4513 * assuming that all variables are non-negative and performing some
4514 * additional sanity checks.
4515 * If "want_integral" is set, then compute the lexicographically smallest
4516 * integer point instead.
4517 * In particular, "lp" should not be empty by construction.
4518 * Double check that this is the case.
4519 * If dependences are not carried for any of the "n_edge" edges,
4520 * then return an empty vector.
4522 * If the schedule_treat_coalescing option is set and
4523 * if the computed schedule performs loop coalescing on a given node,
4524 * i.e., if it is of the form
4526 * c_i i + c_j j + ...
4528 * with |c_j/c_i| >= size_i, then force the coefficient c_i to be zero
4529 * to cut out this solution. Repeat this process until no more loop
4530 * coalescing occurs or until no more dependences can be carried.
4531 * In the latter case, revert to the previously computed solution.
4533 * If the caller requests an integral solution and if coalescing should
4534 * be treated, then perform the coalescing treatment first as
4535 * an integral solution computed before coalescing treatment
4536 * would carry the same number of edges and would therefore probably
4537 * also be coalescing.
4539 * To allow the coalescing treatment to be performed first,
4540 * the initial solution is allowed to be rational and it is only
4541 * cut out (if needed) in the next iteration, if no coalescing measures
4544 static __isl_give isl_vec
*non_neg_lexmin(struct isl_sched_graph
*graph
,
4545 __isl_take isl_basic_set
*lp
, int n_edge
, int want_integral
)
4550 isl_vec
*sol
= NULL
, *prev
;
4551 int treat_coalescing
;
4556 ctx
= isl_basic_set_get_ctx(lp
);
4557 treat_coalescing
= isl_options_get_schedule_treat_coalescing(ctx
);
4558 tl
= isl_tab_lexmin_from_basic_set(lp
);
4566 tl
= isl_tab_lexmin_cut_to_integer(tl
);
4568 sol
= non_empty_solution(tl
);
4572 integral
= isl_int_is_one(sol
->el
[0]);
4573 if (!carries_dependences(sol
, n_edge
)) {
4575 prev
= isl_vec_alloc(ctx
, 0);
4580 prev
= isl_vec_free(prev
);
4581 cut
= want_integral
&& !integral
;
4584 if (!treat_coalescing
)
4586 for (i
= 0; i
< graph
->n
; ++i
) {
4587 struct isl_sched_node
*node
= &graph
->node
[i
];
4589 pos
= find_node_coalescing(node
, sol
);
4592 if (pos
< node
->nvar
)
4597 tl
= zero_out_node_coef(tl
, &graph
->node
[i
], pos
);
4600 } while (try_again
);
4602 isl_tab_lexmin_free(tl
);
4606 isl_tab_lexmin_free(tl
);
4612 /* If "edge" is an edge from a node to itself, then add the corresponding
4613 * dependence relation to "umap".
4614 * If "node" has been compressed, then the dependence relation
4615 * is also compressed first.
4617 static __isl_give isl_union_map
*add_intra(__isl_take isl_union_map
*umap
,
4618 struct isl_sched_edge
*edge
)
4621 struct isl_sched_node
*node
= edge
->src
;
4623 if (edge
->src
!= edge
->dst
)
4626 map
= isl_map_copy(edge
->map
);
4627 if (node
->compressed
) {
4628 map
= isl_map_preimage_domain_multi_aff(map
,
4629 isl_multi_aff_copy(node
->decompress
));
4630 map
= isl_map_preimage_range_multi_aff(map
,
4631 isl_multi_aff_copy(node
->decompress
));
4633 umap
= isl_union_map_add_map(umap
, map
);
4637 /* If "edge" is an edge from a node to another node, then add the corresponding
4638 * dependence relation to "umap".
4639 * If the source or destination nodes of "edge" have been compressed,
4640 * then the dependence relation is also compressed first.
4642 static __isl_give isl_union_map
*add_inter(__isl_take isl_union_map
*umap
,
4643 struct isl_sched_edge
*edge
)
4647 if (edge
->src
== edge
->dst
)
4650 map
= isl_map_copy(edge
->map
);
4651 if (edge
->src
->compressed
)
4652 map
= isl_map_preimage_domain_multi_aff(map
,
4653 isl_multi_aff_copy(edge
->src
->decompress
));
4654 if (edge
->dst
->compressed
)
4655 map
= isl_map_preimage_range_multi_aff(map
,
4656 isl_multi_aff_copy(edge
->dst
->decompress
));
4657 umap
= isl_union_map_add_map(umap
, map
);
4661 /* Internal data structure used by union_drop_coalescing_constraints
4662 * to collect bounds on all relevant statements.
4664 * "graph" is the schedule constraint graph for which an LP problem
4665 * is being constructed.
4666 * "bounds" collects the bounds.
4668 struct isl_collect_bounds_data
{
4670 struct isl_sched_graph
*graph
;
4671 isl_union_set
*bounds
;
4674 /* Add the size bounds for the node with instance deltas in "set"
4677 static isl_stat
collect_bounds(__isl_take isl_set
*set
, void *user
)
4679 struct isl_collect_bounds_data
*data
= user
;
4680 struct isl_sched_node
*node
;
4684 space
= isl_set_get_space(set
);
4687 node
= graph_find_compressed_node(data
->ctx
, data
->graph
, space
);
4688 isl_space_free(space
);
4690 bounds
= isl_set_from_basic_set(get_size_bounds(node
));
4691 data
->bounds
= isl_union_set_add_set(data
->bounds
, bounds
);
4696 /* Drop some constraints from "delta" that could be exploited
4697 * to construct loop coalescing schedules.
4698 * In particular, drop those constraint that bound the difference
4699 * to the size of the domain.
4700 * Do this for each set/node in "delta" separately.
4701 * The parameters are assumed to have been projected out by the caller.
4703 static __isl_give isl_union_set
*union_drop_coalescing_constraints(isl_ctx
*ctx
,
4704 struct isl_sched_graph
*graph
, __isl_take isl_union_set
*delta
)
4706 struct isl_collect_bounds_data data
= { ctx
, graph
};
4708 data
.bounds
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
4709 if (isl_union_set_foreach_set(delta
, &collect_bounds
, &data
) < 0)
4710 data
.bounds
= isl_union_set_free(data
.bounds
);
4711 delta
= isl_union_set_plain_gist(delta
, data
.bounds
);
4716 /* Given a non-trivial lineality space "lineality", add the corresponding
4717 * universe set to data->mask and add a map from elements to
4718 * other elements along the lines in "lineality" to data->equivalent.
4719 * If this is the first time this function gets called
4720 * (data->any_non_trivial is still false), then set data->any_non_trivial and
4721 * initialize data->mask and data->equivalent.
4723 * In particular, if the lineality space is defined by equality constraints
4727 * then construct an affine mapping
4731 * and compute the equivalence relation of having the same image under f:
4733 * { x -> x' : E x = E x' }
4735 static isl_stat
add_non_trivial_lineality(__isl_take isl_basic_set
*lineality
,
4736 struct isl_exploit_lineality_data
*data
)
4742 isl_multi_pw_aff
*mpa
;
4747 return isl_stat_error
;
4748 if (isl_basic_set_dim(lineality
, isl_dim_div
) != 0)
4749 isl_die(isl_basic_set_get_ctx(lineality
), isl_error_internal
,
4750 "local variables not allowed", goto error
);
4752 space
= isl_basic_set_get_space(lineality
);
4753 if (!data
->any_non_trivial
) {
4754 data
->equivalent
= isl_union_map_empty(isl_space_copy(space
));
4755 data
->mask
= isl_union_set_empty(isl_space_copy(space
));
4757 data
->any_non_trivial
= isl_bool_true
;
4759 univ
= isl_set_universe(isl_space_copy(space
));
4760 data
->mask
= isl_union_set_add_set(data
->mask
, univ
);
4762 eq
= isl_basic_set_extract_equalities(lineality
);
4763 n
= isl_mat_rows(eq
);
4764 eq
= isl_mat_insert_zero_rows(eq
, 0, 1);
4765 eq
= isl_mat_set_element_si(eq
, 0, 0, 1);
4766 space
= isl_space_from_domain(space
);
4767 space
= isl_space_add_dims(space
, isl_dim_out
, n
);
4768 ma
= isl_multi_aff_from_aff_mat(space
, eq
);
4769 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
4770 map
= isl_multi_pw_aff_eq_map(mpa
, isl_multi_pw_aff_copy(mpa
));
4771 data
->equivalent
= isl_union_map_add_map(data
->equivalent
, map
);
4773 isl_basic_set_free(lineality
);
4776 isl_basic_set_free(lineality
);
4777 return isl_stat_error
;
4780 /* Check if the lineality space "set" is non-trivial (i.e., is not just
4781 * the origin or, in other words, satisfies a number of equality constraints
4782 * that is smaller than the dimension of the set).
4783 * If so, extend data->mask and data->equivalent accordingly.
4785 * The input should not have any local variables already, but
4786 * isl_set_remove_divs is called to make sure it does not.
4788 static isl_stat
add_lineality(__isl_take isl_set
*set
, void *user
)
4790 struct isl_exploit_lineality_data
*data
= user
;
4791 isl_basic_set
*hull
;
4794 set
= isl_set_remove_divs(set
);
4795 hull
= isl_set_unshifted_simple_hull(set
);
4796 dim
= isl_basic_set_dim(hull
, isl_dim_set
);
4797 n_eq
= isl_basic_set_n_equality(hull
);
4799 return isl_stat_error
;
4801 return add_non_trivial_lineality(hull
, data
);
4802 isl_basic_set_free(hull
);
4806 /* Check if the difference set on intra-node schedule constraints "intra"
4807 * has any non-trivial lineality space.
4808 * If so, then extend the difference set to a difference set
4809 * on equivalent elements. That is, if "intra" is
4811 * { y - x : (x,y) \in V }
4813 * and elements are equivalent if they have the same image under f,
4816 * { y' - x' : (x,y) \in V and f(x) = f(x') and f(y) = f(y') }
4818 * or, since f is linear,
4820 * { y' - x' : (x,y) \in V and f(y - x) = f(y' - x') }
4822 * The results of the search for non-trivial lineality spaces is stored
4825 static __isl_give isl_union_set
*exploit_intra_lineality(
4826 __isl_take isl_union_set
*intra
,
4827 struct isl_exploit_lineality_data
*data
)
4829 isl_union_set
*lineality
;
4830 isl_union_set
*uset
;
4832 data
->any_non_trivial
= isl_bool_false
;
4833 lineality
= isl_union_set_copy(intra
);
4834 lineality
= isl_union_set_combined_lineality_space(lineality
);
4835 if (isl_union_set_foreach_set(lineality
, &add_lineality
, data
) < 0)
4836 data
->any_non_trivial
= isl_bool_error
;
4837 isl_union_set_free(lineality
);
4839 if (data
->any_non_trivial
< 0)
4840 return isl_union_set_free(intra
);
4841 if (!data
->any_non_trivial
)
4844 uset
= isl_union_set_copy(intra
);
4845 intra
= isl_union_set_subtract(intra
, isl_union_set_copy(data
->mask
));
4846 uset
= isl_union_set_apply(uset
, isl_union_map_copy(data
->equivalent
));
4847 intra
= isl_union_set_union(intra
, uset
);
4849 intra
= isl_union_set_remove_divs(intra
);
4854 /* If the difference set on intra-node schedule constraints was found to have
4855 * any non-trivial lineality space by exploit_intra_lineality,
4856 * as recorded in "data", then extend the inter-node
4857 * schedule constraints "inter" to schedule constraints on equivalent elements.
4858 * That is, if "inter" is V and
4859 * elements are equivalent if they have the same image under f, then return
4861 * { (x', y') : (x,y) \in V and f(x) = f(x') and f(y) = f(y') }
4863 static __isl_give isl_union_map
*exploit_inter_lineality(
4864 __isl_take isl_union_map
*inter
,
4865 struct isl_exploit_lineality_data
*data
)
4867 isl_union_map
*umap
;
4869 if (data
->any_non_trivial
< 0)
4870 return isl_union_map_free(inter
);
4871 if (!data
->any_non_trivial
)
4874 umap
= isl_union_map_copy(inter
);
4875 inter
= isl_union_map_subtract_range(inter
,
4876 isl_union_set_copy(data
->mask
));
4877 umap
= isl_union_map_apply_range(umap
,
4878 isl_union_map_copy(data
->equivalent
));
4879 inter
= isl_union_map_union(inter
, umap
);
4880 umap
= isl_union_map_copy(inter
);
4881 inter
= isl_union_map_subtract_domain(inter
,
4882 isl_union_set_copy(data
->mask
));
4883 umap
= isl_union_map_apply_range(isl_union_map_copy(data
->equivalent
),
4885 inter
= isl_union_map_union(inter
, umap
);
4887 inter
= isl_union_map_remove_divs(inter
);
4892 /* For each (conditional) validity edge in "graph",
4893 * add the corresponding dependence relation using "add"
4894 * to a collection of dependence relations and return the result.
4895 * If "coincidence" is set, then coincidence edges are considered as well.
4897 static __isl_give isl_union_map
*collect_validity(struct isl_sched_graph
*graph
,
4898 __isl_give isl_union_map
*(*add
)(__isl_take isl_union_map
*umap
,
4899 struct isl_sched_edge
*edge
), int coincidence
)
4903 isl_union_map
*umap
;
4905 space
= isl_space_copy(graph
->node
[0].space
);
4906 umap
= isl_union_map_empty(space
);
4908 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4909 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
4911 if (!is_any_validity(edge
) &&
4912 (!coincidence
|| !is_coincidence(edge
)))
4915 umap
= add(umap
, edge
);
4921 /* Project out all parameters from "uset" and return the result.
4923 static __isl_give isl_union_set
*union_set_drop_parameters(
4924 __isl_take isl_union_set
*uset
)
4928 nparam
= isl_union_set_dim(uset
, isl_dim_param
);
4929 return isl_union_set_project_out(uset
, isl_dim_param
, 0, nparam
);
4932 /* For each dependence relation on a (conditional) validity edge
4933 * from a node to itself,
4934 * construct the set of coefficients of valid constraints for elements
4935 * in that dependence relation and collect the results.
4936 * If "coincidence" is set, then coincidence edges are considered as well.
4938 * In particular, for each dependence relation R, constraints
4939 * on coefficients (c_0, c_x) are constructed such that
4941 * c_0 + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
4943 * If the schedule_treat_coalescing option is set, then some constraints
4944 * that could be exploited to construct coalescing schedules
4945 * are removed before the dual is computed, but after the parameters
4946 * have been projected out.
4947 * The entire computation is essentially the same as that performed
4948 * by intra_coefficients, except that it operates on multiple
4949 * edges together and that the parameters are always projected out.
4951 * Additionally, exploit any non-trivial lineality space
4952 * in the difference set after removing coalescing constraints and
4953 * store the results of the non-trivial lineality space detection in "data".
4954 * The procedure is currently run unconditionally, but it is unlikely
4955 * to find any non-trivial lineality spaces if no coalescing constraints
4956 * have been removed.
4958 * Note that if a dependence relation is a union of basic maps,
4959 * then each basic map needs to be treated individually as it may only
4960 * be possible to carry the dependences expressed by some of those
4961 * basic maps and not all of them.
4962 * The collected validity constraints are therefore not coalesced and
4963 * it is assumed that they are not coalesced automatically.
4964 * Duplicate basic maps can be removed, however.
4965 * In particular, if the same basic map appears as a disjunct
4966 * in multiple edges, then it only needs to be carried once.
4968 static __isl_give isl_basic_set_list
*collect_intra_validity(isl_ctx
*ctx
,
4969 struct isl_sched_graph
*graph
, int coincidence
,
4970 struct isl_exploit_lineality_data
*data
)
4972 isl_union_map
*intra
;
4973 isl_union_set
*delta
;
4974 isl_basic_set_list
*list
;
4976 intra
= collect_validity(graph
, &add_intra
, coincidence
);
4977 delta
= isl_union_map_deltas(intra
);
4978 delta
= union_set_drop_parameters(delta
);
4979 delta
= isl_union_set_remove_divs(delta
);
4980 if (isl_options_get_schedule_treat_coalescing(ctx
))
4981 delta
= union_drop_coalescing_constraints(ctx
, graph
, delta
);
4982 delta
= exploit_intra_lineality(delta
, data
);
4983 list
= isl_union_set_get_basic_set_list(delta
);
4984 isl_union_set_free(delta
);
4986 return isl_basic_set_list_coefficients(list
);
4989 /* For each dependence relation on a (conditional) validity edge
4990 * from a node to some other node,
4991 * construct the set of coefficients of valid constraints for elements
4992 * in that dependence relation and collect the results.
4993 * If "coincidence" is set, then coincidence edges are considered as well.
4995 * In particular, for each dependence relation R, constraints
4996 * on coefficients (c_0, c_n, c_x, c_y) are constructed such that
4998 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
5000 * This computation is essentially the same as that performed
5001 * by inter_coefficients, except that it operates on multiple
5004 * Additionally, exploit any non-trivial lineality space
5005 * that may have been discovered by collect_intra_validity
5006 * (as stored in "data").
5008 * Note that if a dependence relation is a union of basic maps,
5009 * then each basic map needs to be treated individually as it may only
5010 * be possible to carry the dependences expressed by some of those
5011 * basic maps and not all of them.
5012 * The collected validity constraints are therefore not coalesced and
5013 * it is assumed that they are not coalesced automatically.
5014 * Duplicate basic maps can be removed, however.
5015 * In particular, if the same basic map appears as a disjunct
5016 * in multiple edges, then it only needs to be carried once.
5018 static __isl_give isl_basic_set_list
*collect_inter_validity(
5019 struct isl_sched_graph
*graph
, int coincidence
,
5020 struct isl_exploit_lineality_data
*data
)
5022 isl_union_map
*inter
;
5023 isl_union_set
*wrap
;
5024 isl_basic_set_list
*list
;
5026 inter
= collect_validity(graph
, &add_inter
, coincidence
);
5027 inter
= exploit_inter_lineality(inter
, data
);
5028 inter
= isl_union_map_remove_divs(inter
);
5029 wrap
= isl_union_map_wrap(inter
);
5030 list
= isl_union_set_get_basic_set_list(wrap
);
5031 isl_union_set_free(wrap
);
5032 return isl_basic_set_list_coefficients(list
);
5035 /* Construct an LP problem for finding schedule coefficients
5036 * such that the schedule carries as many of the "n_edge" groups of
5037 * dependences as possible based on the corresponding coefficient
5038 * constraints and return the lexicographically smallest non-trivial solution.
5039 * "intra" is the sequence of coefficient constraints for intra-node edges.
5040 * "inter" is the sequence of coefficient constraints for inter-node edges.
5041 * If "want_integral" is set, then compute an integral solution
5042 * for the coefficients rather than using the numerators
5043 * of a rational solution.
5044 * "carry_inter" indicates whether inter-node edges should be carried or
5047 * If none of the "n_edge" groups can be carried
5048 * then return an empty vector.
5050 static __isl_give isl_vec
*compute_carrying_sol_coef(isl_ctx
*ctx
,
5051 struct isl_sched_graph
*graph
, int n_edge
,
5052 __isl_keep isl_basic_set_list
*intra
,
5053 __isl_keep isl_basic_set_list
*inter
, int want_integral
,
5058 if (setup_carry_lp(ctx
, graph
, n_edge
, intra
, inter
, carry_inter
) < 0)
5061 lp
= isl_basic_set_copy(graph
->lp
);
5062 return non_neg_lexmin(graph
, lp
, n_edge
, want_integral
);
5065 /* Construct an LP problem for finding schedule coefficients
5066 * such that the schedule carries as many of the validity dependences
5068 * return the lexicographically smallest non-trivial solution.
5069 * If "fallback" is set, then the carrying is performed as a fallback
5070 * for the Pluto-like scheduler.
5071 * If "coincidence" is set, then try and carry coincidence edges as well.
5073 * The variable "n_edge" stores the number of groups that should be carried.
5074 * If none of the "n_edge" groups can be carried
5075 * then return an empty vector.
5076 * If, moreover, "n_edge" is zero, then the LP problem does not even
5077 * need to be constructed.
5079 * If a fallback solution is being computed, then compute an integral solution
5080 * for the coefficients rather than using the numerators
5081 * of a rational solution.
5083 * If a fallback solution is being computed, if there are any intra-node
5084 * dependences, and if requested by the user, then first try
5085 * to only carry those intra-node dependences.
5086 * If this fails to carry any dependences, then try again
5087 * with the inter-node dependences included.
5089 static __isl_give isl_vec
*compute_carrying_sol(isl_ctx
*ctx
,
5090 struct isl_sched_graph
*graph
, int fallback
, int coincidence
)
5092 int n_intra
, n_inter
;
5094 struct isl_carry carry
= { 0 };
5097 carry
.intra
= collect_intra_validity(ctx
, graph
, coincidence
,
5099 carry
.inter
= collect_inter_validity(graph
, coincidence
,
5101 if (!carry
.intra
|| !carry
.inter
)
5103 n_intra
= isl_basic_set_list_n_basic_set(carry
.intra
);
5104 n_inter
= isl_basic_set_list_n_basic_set(carry
.inter
);
5106 if (fallback
&& n_intra
> 0 &&
5107 isl_options_get_schedule_carry_self_first(ctx
)) {
5108 sol
= compute_carrying_sol_coef(ctx
, graph
, n_intra
,
5109 carry
.intra
, carry
.inter
, fallback
, 0);
5110 if (!sol
|| sol
->size
!= 0 || n_inter
== 0) {
5111 isl_carry_clear(&carry
);
5117 n_edge
= n_intra
+ n_inter
;
5119 isl_carry_clear(&carry
);
5120 return isl_vec_alloc(ctx
, 0);
5123 sol
= compute_carrying_sol_coef(ctx
, graph
, n_edge
,
5124 carry
.intra
, carry
.inter
, fallback
, 1);
5125 isl_carry_clear(&carry
);
5128 isl_carry_clear(&carry
);
5132 /* Construct a schedule row for each node such that as many validity dependences
5133 * as possible are carried and then continue with the next band.
5134 * If "fallback" is set, then the carrying is performed as a fallback
5135 * for the Pluto-like scheduler.
5136 * If "coincidence" is set, then try and carry coincidence edges as well.
5138 * If there are no validity dependences, then no dependence can be carried and
5139 * the procedure is guaranteed to fail. If there is more than one component,
5140 * then try computing a schedule on each component separately
5141 * to prevent or at least postpone this failure.
5143 * If a schedule row is computed, then check that dependences are carried
5144 * for at least one of the edges.
5146 * If the computed schedule row turns out to be trivial on one or
5147 * more nodes where it should not be trivial, then we throw it away
5148 * and try again on each component separately.
5150 * If there is only one component, then we accept the schedule row anyway,
5151 * but we do not consider it as a complete row and therefore do not
5152 * increment graph->n_row. Note that the ranks of the nodes that
5153 * do get a non-trivial schedule part will get updated regardless and
5154 * graph->maxvar is computed based on these ranks. The test for
5155 * whether more schedule rows are required in compute_schedule_wcc
5156 * is therefore not affected.
5158 * Insert a band corresponding to the schedule row at position "node"
5159 * of the schedule tree and continue with the construction of the schedule.
5160 * This insertion and the continued construction is performed by split_scaled
5161 * after optionally checking for non-trivial common divisors.
5163 static __isl_give isl_schedule_node
*carry(__isl_take isl_schedule_node
*node
,
5164 struct isl_sched_graph
*graph
, int fallback
, int coincidence
)
5173 ctx
= isl_schedule_node_get_ctx(node
);
5174 sol
= compute_carrying_sol(ctx
, graph
, fallback
, coincidence
);
5176 return isl_schedule_node_free(node
);
5177 if (sol
->size
== 0) {
5180 return compute_component_schedule(node
, graph
, 1);
5181 isl_die(ctx
, isl_error_unknown
, "unable to carry dependences",
5182 return isl_schedule_node_free(node
));
5185 trivial
= is_any_trivial(graph
, sol
);
5187 sol
= isl_vec_free(sol
);
5188 } else if (trivial
&& graph
->scc
> 1) {
5190 return compute_component_schedule(node
, graph
, 1);
5193 if (update_schedule(graph
, sol
, 0) < 0)
5194 return isl_schedule_node_free(node
);
5198 return split_scaled(node
, graph
);
5201 /* Construct a schedule row for each node such that as many validity dependences
5202 * as possible are carried and then continue with the next band.
5203 * Do so as a fallback for the Pluto-like scheduler.
5204 * If "coincidence" is set, then try and carry coincidence edges as well.
5206 static __isl_give isl_schedule_node
*carry_fallback(
5207 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
5210 return carry(node
, graph
, 1, coincidence
);
5213 /* Construct a schedule row for each node such that as many validity dependences
5214 * as possible are carried and then continue with the next band.
5215 * Do so for the case where the Feautrier scheduler was selected
5218 static __isl_give isl_schedule_node
*carry_feautrier(
5219 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
5221 return carry(node
, graph
, 0, 0);
5224 /* Construct a schedule row for each node such that as many validity dependences
5225 * as possible are carried and then continue with the next band.
5226 * Do so as a fallback for the Pluto-like scheduler.
5228 static __isl_give isl_schedule_node
*carry_dependences(
5229 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
5231 return carry_fallback(node
, graph
, 0);
5234 /* Construct a schedule row for each node such that as many validity or
5235 * coincidence dependences as possible are carried and
5236 * then continue with the next band.
5237 * Do so as a fallback for the Pluto-like scheduler.
5239 static __isl_give isl_schedule_node
*carry_coincidence(
5240 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
5242 return carry_fallback(node
, graph
, 1);
5245 /* Topologically sort statements mapped to the same schedule iteration
5246 * and add insert a sequence node in front of "node"
5247 * corresponding to this order.
5248 * If "initialized" is set, then it may be assumed that compute_maxvar
5249 * has been called on the current band. Otherwise, call
5250 * compute_maxvar if and before carry_dependences gets called.
5252 * If it turns out to be impossible to sort the statements apart,
5253 * because different dependences impose different orderings
5254 * on the statements, then we extend the schedule such that
5255 * it carries at least one more dependence.
5257 static __isl_give isl_schedule_node
*sort_statements(
5258 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
5262 isl_union_set_list
*filters
;
5267 ctx
= isl_schedule_node_get_ctx(node
);
5269 isl_die(ctx
, isl_error_internal
,
5270 "graph should have at least one node",
5271 return isl_schedule_node_free(node
));
5276 if (update_edges(ctx
, graph
) < 0)
5277 return isl_schedule_node_free(node
);
5279 if (graph
->n_edge
== 0)
5282 if (detect_sccs(ctx
, graph
) < 0)
5283 return isl_schedule_node_free(node
);
5286 if (graph
->scc
< graph
->n
) {
5287 if (!initialized
&& compute_maxvar(graph
) < 0)
5288 return isl_schedule_node_free(node
);
5289 return carry_dependences(node
, graph
);
5292 filters
= extract_sccs(ctx
, graph
);
5293 node
= isl_schedule_node_insert_sequence(node
, filters
);
5298 /* Are there any (non-empty) (conditional) validity edges in the graph?
5300 static int has_validity_edges(struct isl_sched_graph
*graph
)
5304 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5307 empty
= isl_map_plain_is_empty(graph
->edge
[i
].map
);
5312 if (is_any_validity(&graph
->edge
[i
]))
5319 /* Should we apply a Feautrier step?
5320 * That is, did the user request the Feautrier algorithm and are
5321 * there any validity dependences (left)?
5323 static int need_feautrier_step(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
5325 if (ctx
->opt
->schedule_algorithm
!= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
)
5328 return has_validity_edges(graph
);
5331 /* Compute a schedule for a connected dependence graph using Feautrier's
5332 * multi-dimensional scheduling algorithm and return the updated schedule node.
5334 * The original algorithm is described in [1].
5335 * The main idea is to minimize the number of scheduling dimensions, by
5336 * trying to satisfy as many dependences as possible per scheduling dimension.
5338 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
5339 * Problem, Part II: Multi-Dimensional Time.
5340 * In Intl. Journal of Parallel Programming, 1992.
5342 static __isl_give isl_schedule_node
*compute_schedule_wcc_feautrier(
5343 isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
5345 return carry_feautrier(node
, graph
);
5348 /* Turn off the "local" bit on all (condition) edges.
5350 static void clear_local_edges(struct isl_sched_graph
*graph
)
5354 for (i
= 0; i
< graph
->n_edge
; ++i
)
5355 if (is_condition(&graph
->edge
[i
]))
5356 clear_local(&graph
->edge
[i
]);
5359 /* Does "graph" have both condition and conditional validity edges?
5361 static int need_condition_check(struct isl_sched_graph
*graph
)
5364 int any_condition
= 0;
5365 int any_conditional_validity
= 0;
5367 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5368 if (is_condition(&graph
->edge
[i
]))
5370 if (is_conditional_validity(&graph
->edge
[i
]))
5371 any_conditional_validity
= 1;
5374 return any_condition
&& any_conditional_validity
;
5377 /* Does "graph" contain any coincidence edge?
5379 static int has_any_coincidence(struct isl_sched_graph
*graph
)
5383 for (i
= 0; i
< graph
->n_edge
; ++i
)
5384 if (is_coincidence(&graph
->edge
[i
]))
5390 /* Extract the final schedule row as a map with the iteration domain
5391 * of "node" as domain.
5393 static __isl_give isl_map
*final_row(struct isl_sched_node
*node
)
5398 row
= isl_mat_rows(node
->sched
) - 1;
5399 ma
= node_extract_partial_schedule_multi_aff(node
, row
, 1);
5400 return isl_map_from_multi_aff(ma
);
5403 /* Is the conditional validity dependence in the edge with index "edge_index"
5404 * violated by the latest (i.e., final) row of the schedule?
5405 * That is, is i scheduled after j
5406 * for any conditional validity dependence i -> j?
5408 static int is_violated(struct isl_sched_graph
*graph
, int edge_index
)
5410 isl_map
*src_sched
, *dst_sched
, *map
;
5411 struct isl_sched_edge
*edge
= &graph
->edge
[edge_index
];
5414 src_sched
= final_row(edge
->src
);
5415 dst_sched
= final_row(edge
->dst
);
5416 map
= isl_map_copy(edge
->map
);
5417 map
= isl_map_apply_domain(map
, src_sched
);
5418 map
= isl_map_apply_range(map
, dst_sched
);
5419 map
= isl_map_order_gt(map
, isl_dim_in
, 0, isl_dim_out
, 0);
5420 empty
= isl_map_is_empty(map
);
5429 /* Does "graph" have any satisfied condition edges that
5430 * are adjacent to the conditional validity constraint with
5431 * domain "conditional_source" and range "conditional_sink"?
5433 * A satisfied condition is one that is not local.
5434 * If a condition was forced to be local already (i.e., marked as local)
5435 * then there is no need to check if it is in fact local.
5437 * Additionally, mark all adjacent condition edges found as local.
5439 static int has_adjacent_true_conditions(struct isl_sched_graph
*graph
,
5440 __isl_keep isl_union_set
*conditional_source
,
5441 __isl_keep isl_union_set
*conditional_sink
)
5446 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5447 int adjacent
, local
;
5448 isl_union_map
*condition
;
5450 if (!is_condition(&graph
->edge
[i
]))
5452 if (is_local(&graph
->edge
[i
]))
5455 condition
= graph
->edge
[i
].tagged_condition
;
5456 adjacent
= domain_intersects(condition
, conditional_sink
);
5457 if (adjacent
>= 0 && !adjacent
)
5458 adjacent
= range_intersects(condition
,
5459 conditional_source
);
5465 set_local(&graph
->edge
[i
]);
5467 local
= is_condition_false(&graph
->edge
[i
]);
5477 /* Are there any violated conditional validity dependences with
5478 * adjacent condition dependences that are not local with respect
5479 * to the current schedule?
5480 * That is, is the conditional validity constraint violated?
5482 * Additionally, mark all those adjacent condition dependences as local.
5483 * We also mark those adjacent condition dependences that were not marked
5484 * as local before, but just happened to be local already. This ensures
5485 * that they remain local if the schedule is recomputed.
5487 * We first collect domain and range of all violated conditional validity
5488 * dependences and then check if there are any adjacent non-local
5489 * condition dependences.
5491 static int has_violated_conditional_constraint(isl_ctx
*ctx
,
5492 struct isl_sched_graph
*graph
)
5496 isl_union_set
*source
, *sink
;
5498 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
5499 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
5500 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5501 isl_union_set
*uset
;
5502 isl_union_map
*umap
;
5505 if (!is_conditional_validity(&graph
->edge
[i
]))
5508 violated
= is_violated(graph
, i
);
5516 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
5517 uset
= isl_union_map_domain(umap
);
5518 source
= isl_union_set_union(source
, uset
);
5519 source
= isl_union_set_coalesce(source
);
5521 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
5522 uset
= isl_union_map_range(umap
);
5523 sink
= isl_union_set_union(sink
, uset
);
5524 sink
= isl_union_set_coalesce(sink
);
5528 any
= has_adjacent_true_conditions(graph
, source
, sink
);
5530 isl_union_set_free(source
);
5531 isl_union_set_free(sink
);
5534 isl_union_set_free(source
);
5535 isl_union_set_free(sink
);
5539 /* Examine the current band (the rows between graph->band_start and
5540 * graph->n_total_row), deciding whether to drop it or add it to "node"
5541 * and then continue with the computation of the next band, if any.
5542 * If "initialized" is set, then it may be assumed that compute_maxvar
5543 * has been called on the current band. Otherwise, call
5544 * compute_maxvar if and before carry_dependences gets called.
5546 * The caller keeps looking for a new row as long as
5547 * graph->n_row < graph->maxvar. If the latest attempt to find
5548 * such a row failed (i.e., we still have graph->n_row < graph->maxvar),
5550 * - split between SCCs and start over (assuming we found an interesting
5551 * pair of SCCs between which to split)
5552 * - continue with the next band (assuming the current band has at least
5554 * - if there is more than one SCC left, then split along all SCCs
5555 * - if outer coincidence needs to be enforced, then try to carry as many
5556 * validity or coincidence dependences as possible and
5557 * continue with the next band
5558 * - try to carry as many validity dependences as possible and
5559 * continue with the next band
5560 * In each case, we first insert a band node in the schedule tree
5561 * if any rows have been computed.
5563 * If the caller managed to complete the schedule and the current band
5564 * is empty, then finish off by topologically
5565 * sorting the statements based on the remaining dependences.
5566 * If, on the other hand, the current band has at least one row,
5567 * then continue with the next band. Note that this next band
5568 * will necessarily be empty, but the graph may still be split up
5569 * into weakly connected components before arriving back here.
5571 static __isl_give isl_schedule_node
*compute_schedule_finish_band(
5572 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
5580 empty
= graph
->n_total_row
== graph
->band_start
;
5581 if (graph
->n_row
< graph
->maxvar
) {
5584 ctx
= isl_schedule_node_get_ctx(node
);
5585 if (!ctx
->opt
->schedule_maximize_band_depth
&& !empty
)
5586 return compute_next_band(node
, graph
, 1);
5587 if (graph
->src_scc
>= 0)
5588 return compute_split_schedule(node
, graph
);
5590 return compute_next_band(node
, graph
, 1);
5592 return compute_component_schedule(node
, graph
, 1);
5593 if (!initialized
&& compute_maxvar(graph
) < 0)
5594 return isl_schedule_node_free(node
);
5595 if (isl_options_get_schedule_outer_coincidence(ctx
))
5596 return carry_coincidence(node
, graph
);
5597 return carry_dependences(node
, graph
);
5601 return compute_next_band(node
, graph
, 1);
5602 return sort_statements(node
, graph
, initialized
);
5605 /* Construct a band of schedule rows for a connected dependence graph.
5606 * The caller is responsible for determining the strongly connected
5607 * components and calling compute_maxvar first.
5609 * We try to find a sequence of as many schedule rows as possible that result
5610 * in non-negative dependence distances (independent of the previous rows
5611 * in the sequence, i.e., such that the sequence is tilable), with as
5612 * many of the initial rows as possible satisfying the coincidence constraints.
5613 * The computation stops if we can't find any more rows or if we have found
5614 * all the rows we wanted to find.
5616 * If ctx->opt->schedule_outer_coincidence is set, then we force the
5617 * outermost dimension to satisfy the coincidence constraints. If this
5618 * turns out to be impossible, we fall back on the general scheme above
5619 * and try to carry as many dependences as possible.
5621 * If "graph" contains both condition and conditional validity dependences,
5622 * then we need to check that that the conditional schedule constraint
5623 * is satisfied, i.e., there are no violated conditional validity dependences
5624 * that are adjacent to any non-local condition dependences.
5625 * If there are, then we mark all those adjacent condition dependences
5626 * as local and recompute the current band. Those dependences that
5627 * are marked local will then be forced to be local.
5628 * The initial computation is performed with no dependences marked as local.
5629 * If we are lucky, then there will be no violated conditional validity
5630 * dependences adjacent to any non-local condition dependences.
5631 * Otherwise, we mark some additional condition dependences as local and
5632 * recompute. We continue this process until there are no violations left or
5633 * until we are no longer able to compute a schedule.
5634 * Since there are only a finite number of dependences,
5635 * there will only be a finite number of iterations.
5637 static isl_stat
compute_schedule_wcc_band(isl_ctx
*ctx
,
5638 struct isl_sched_graph
*graph
)
5640 int has_coincidence
;
5641 int use_coincidence
;
5642 int force_coincidence
= 0;
5643 int check_conditional
;
5645 if (sort_sccs(graph
) < 0)
5646 return isl_stat_error
;
5648 clear_local_edges(graph
);
5649 check_conditional
= need_condition_check(graph
);
5650 has_coincidence
= has_any_coincidence(graph
);
5652 if (ctx
->opt
->schedule_outer_coincidence
)
5653 force_coincidence
= 1;
5655 use_coincidence
= has_coincidence
;
5656 while (graph
->n_row
< graph
->maxvar
) {
5661 graph
->src_scc
= -1;
5662 graph
->dst_scc
= -1;
5664 if (setup_lp(ctx
, graph
, use_coincidence
) < 0)
5665 return isl_stat_error
;
5666 sol
= solve_lp(ctx
, graph
);
5668 return isl_stat_error
;
5669 if (sol
->size
== 0) {
5670 int empty
= graph
->n_total_row
== graph
->band_start
;
5673 if (use_coincidence
&& (!force_coincidence
|| !empty
)) {
5674 use_coincidence
= 0;
5679 coincident
= !has_coincidence
|| use_coincidence
;
5680 if (update_schedule(graph
, sol
, coincident
) < 0)
5681 return isl_stat_error
;
5683 if (!check_conditional
)
5685 violated
= has_violated_conditional_constraint(ctx
, graph
);
5687 return isl_stat_error
;
5690 if (reset_band(graph
) < 0)
5691 return isl_stat_error
;
5692 use_coincidence
= has_coincidence
;
5698 /* Compute a schedule for a connected dependence graph by considering
5699 * the graph as a whole and return the updated schedule node.
5701 * The actual schedule rows of the current band are computed by
5702 * compute_schedule_wcc_band. compute_schedule_finish_band takes
5703 * care of integrating the band into "node" and continuing
5706 static __isl_give isl_schedule_node
*compute_schedule_wcc_whole(
5707 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
5714 ctx
= isl_schedule_node_get_ctx(node
);
5715 if (compute_schedule_wcc_band(ctx
, graph
) < 0)
5716 return isl_schedule_node_free(node
);
5718 return compute_schedule_finish_band(node
, graph
, 1);
5721 /* Clustering information used by compute_schedule_wcc_clustering.
5723 * "n" is the number of SCCs in the original dependence graph
5724 * "scc" is an array of "n" elements, each representing an SCC
5725 * of the original dependence graph. All entries in the same cluster
5726 * have the same number of schedule rows.
5727 * "scc_cluster" maps each SCC index to the cluster to which it belongs,
5728 * where each cluster is represented by the index of the first SCC
5729 * in the cluster. Initially, each SCC belongs to a cluster containing
5732 * "scc_in_merge" is used by merge_clusters_along_edge to keep
5733 * track of which SCCs need to be merged.
5735 * "cluster" contains the merged clusters of SCCs after the clustering
5738 * "scc_node" is a temporary data structure used inside copy_partial.
5739 * For each SCC, it keeps track of the number of nodes in the SCC
5740 * that have already been copied.
5742 struct isl_clustering
{
5744 struct isl_sched_graph
*scc
;
5745 struct isl_sched_graph
*cluster
;
5751 /* Initialize the clustering data structure "c" from "graph".
5753 * In particular, allocate memory, extract the SCCs from "graph"
5754 * into c->scc, initialize scc_cluster and construct
5755 * a band of schedule rows for each SCC.
5756 * Within each SCC, there is only one SCC by definition.
5757 * Each SCC initially belongs to a cluster containing only that SCC.
5759 static isl_stat
clustering_init(isl_ctx
*ctx
, struct isl_clustering
*c
,
5760 struct isl_sched_graph
*graph
)
5765 c
->scc
= isl_calloc_array(ctx
, struct isl_sched_graph
, c
->n
);
5766 c
->cluster
= isl_calloc_array(ctx
, struct isl_sched_graph
, c
->n
);
5767 c
->scc_cluster
= isl_calloc_array(ctx
, int, c
->n
);
5768 c
->scc_node
= isl_calloc_array(ctx
, int, c
->n
);
5769 c
->scc_in_merge
= isl_calloc_array(ctx
, int, c
->n
);
5770 if (!c
->scc
|| !c
->cluster
||
5771 !c
->scc_cluster
|| !c
->scc_node
|| !c
->scc_in_merge
)
5772 return isl_stat_error
;
5774 for (i
= 0; i
< c
->n
; ++i
) {
5775 if (extract_sub_graph(ctx
, graph
, &node_scc_exactly
,
5776 &edge_scc_exactly
, i
, &c
->scc
[i
]) < 0)
5777 return isl_stat_error
;
5779 if (compute_maxvar(&c
->scc
[i
]) < 0)
5780 return isl_stat_error
;
5781 if (compute_schedule_wcc_band(ctx
, &c
->scc
[i
]) < 0)
5782 return isl_stat_error
;
5783 c
->scc_cluster
[i
] = i
;
5789 /* Free all memory allocated for "c".
5791 static void clustering_free(isl_ctx
*ctx
, struct isl_clustering
*c
)
5796 for (i
= 0; i
< c
->n
; ++i
)
5797 graph_free(ctx
, &c
->scc
[i
]);
5800 for (i
= 0; i
< c
->n
; ++i
)
5801 graph_free(ctx
, &c
->cluster
[i
]);
5803 free(c
->scc_cluster
);
5805 free(c
->scc_in_merge
);
5808 /* Should we refrain from merging the cluster in "graph" with
5809 * any other cluster?
5810 * In particular, is its current schedule band empty and incomplete.
5812 static int bad_cluster(struct isl_sched_graph
*graph
)
5814 return graph
->n_row
< graph
->maxvar
&&
5815 graph
->n_total_row
== graph
->band_start
;
5818 /* Is "edge" a proximity edge with a non-empty dependence relation?
5820 static isl_bool
is_non_empty_proximity(struct isl_sched_edge
*edge
)
5822 if (!is_proximity(edge
))
5823 return isl_bool_false
;
5824 return isl_bool_not(isl_map_plain_is_empty(edge
->map
));
5827 /* Return the index of an edge in "graph" that can be used to merge
5828 * two clusters in "c".
5829 * Return graph->n_edge if no such edge can be found.
5830 * Return -1 on error.
5832 * In particular, return a proximity edge between two clusters
5833 * that is not marked "no_merge" and such that neither of the
5834 * two clusters has an incomplete, empty band.
5836 * If there are multiple such edges, then try and find the most
5837 * appropriate edge to use for merging. In particular, pick the edge
5838 * with the greatest weight. If there are multiple of those,
5839 * then pick one with the shortest distance between
5840 * the two cluster representatives.
5842 static int find_proximity(struct isl_sched_graph
*graph
,
5843 struct isl_clustering
*c
)
5845 int i
, best
= graph
->n_edge
, best_dist
, best_weight
;
5847 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5848 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
5852 prox
= is_non_empty_proximity(edge
);
5859 if (bad_cluster(&c
->scc
[edge
->src
->scc
]) ||
5860 bad_cluster(&c
->scc
[edge
->dst
->scc
]))
5862 dist
= c
->scc_cluster
[edge
->dst
->scc
] -
5863 c
->scc_cluster
[edge
->src
->scc
];
5866 weight
= edge
->weight
;
5867 if (best
< graph
->n_edge
) {
5868 if (best_weight
> weight
)
5870 if (best_weight
== weight
&& best_dist
<= dist
)
5875 best_weight
= weight
;
5881 /* Internal data structure used in mark_merge_sccs.
5883 * "graph" is the dependence graph in which a strongly connected
5884 * component is constructed.
5885 * "scc_cluster" maps each SCC index to the cluster to which it belongs.
5886 * "src" and "dst" are the indices of the nodes that are being merged.
5888 struct isl_mark_merge_sccs_data
{
5889 struct isl_sched_graph
*graph
;
5895 /* Check whether the cluster containing node "i" depends on the cluster
5896 * containing node "j". If "i" and "j" belong to the same cluster,
5897 * then they are taken to depend on each other to ensure that
5898 * the resulting strongly connected component consists of complete
5899 * clusters. Furthermore, if "i" and "j" are the two nodes that
5900 * are being merged, then they are taken to depend on each other as well.
5901 * Otherwise, check if there is a (conditional) validity dependence
5902 * from node[j] to node[i], forcing node[i] to follow node[j].
5904 static isl_bool
cluster_follows(int i
, int j
, void *user
)
5906 struct isl_mark_merge_sccs_data
*data
= user
;
5907 struct isl_sched_graph
*graph
= data
->graph
;
5908 int *scc_cluster
= data
->scc_cluster
;
5910 if (data
->src
== i
&& data
->dst
== j
)
5911 return isl_bool_true
;
5912 if (data
->src
== j
&& data
->dst
== i
)
5913 return isl_bool_true
;
5914 if (scc_cluster
[graph
->node
[i
].scc
] == scc_cluster
[graph
->node
[j
].scc
])
5915 return isl_bool_true
;
5917 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
5920 /* Mark all SCCs that belong to either of the two clusters in "c"
5921 * connected by the edge in "graph" with index "edge", or to any
5922 * of the intermediate clusters.
5923 * The marking is recorded in c->scc_in_merge.
5925 * The given edge has been selected for merging two clusters,
5926 * meaning that there is at least a proximity edge between the two nodes.
5927 * However, there may also be (indirect) validity dependences
5928 * between the two nodes. When merging the two clusters, all clusters
5929 * containing one or more of the intermediate nodes along the
5930 * indirect validity dependences need to be merged in as well.
5932 * First collect all such nodes by computing the strongly connected
5933 * component (SCC) containing the two nodes connected by the edge, where
5934 * the two nodes are considered to depend on each other to make
5935 * sure they end up in the same SCC. Similarly, each node is considered
5936 * to depend on every other node in the same cluster to ensure
5937 * that the SCC consists of complete clusters.
5939 * Then the original SCCs that contain any of these nodes are marked
5940 * in c->scc_in_merge.
5942 static isl_stat
mark_merge_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5943 int edge
, struct isl_clustering
*c
)
5945 struct isl_mark_merge_sccs_data data
;
5946 struct isl_tarjan_graph
*g
;
5949 for (i
= 0; i
< c
->n
; ++i
)
5950 c
->scc_in_merge
[i
] = 0;
5953 data
.scc_cluster
= c
->scc_cluster
;
5954 data
.src
= graph
->edge
[edge
].src
- graph
->node
;
5955 data
.dst
= graph
->edge
[edge
].dst
- graph
->node
;
5957 g
= isl_tarjan_graph_component(ctx
, graph
->n
, data
.dst
,
5958 &cluster_follows
, &data
);
5964 isl_die(ctx
, isl_error_internal
,
5965 "expecting at least two nodes in component",
5967 if (g
->order
[--i
] != -1)
5968 isl_die(ctx
, isl_error_internal
,
5969 "expecting end of component marker", goto error
);
5971 for (--i
; i
>= 0 && g
->order
[i
] != -1; --i
) {
5972 int scc
= graph
->node
[g
->order
[i
]].scc
;
5973 c
->scc_in_merge
[scc
] = 1;
5976 isl_tarjan_graph_free(g
);
5979 isl_tarjan_graph_free(g
);
5980 return isl_stat_error
;
5983 /* Construct the identifier "cluster_i".
5985 static __isl_give isl_id
*cluster_id(isl_ctx
*ctx
, int i
)
5989 snprintf(name
, sizeof(name
), "cluster_%d", i
);
5990 return isl_id_alloc(ctx
, name
, NULL
);
5993 /* Construct the space of the cluster with index "i" containing
5994 * the strongly connected component "scc".
5996 * In particular, construct a space called cluster_i with dimension equal
5997 * to the number of schedule rows in the current band of "scc".
5999 static __isl_give isl_space
*cluster_space(struct isl_sched_graph
*scc
, int i
)
6005 nvar
= scc
->n_total_row
- scc
->band_start
;
6006 space
= isl_space_copy(scc
->node
[0].space
);
6007 space
= isl_space_params(space
);
6008 space
= isl_space_set_from_params(space
);
6009 space
= isl_space_add_dims(space
, isl_dim_set
, nvar
);
6010 id
= cluster_id(isl_space_get_ctx(space
), i
);
6011 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
6016 /* Collect the domain of the graph for merging clusters.
6018 * In particular, for each cluster with first SCC "i", construct
6019 * a set in the space called cluster_i with dimension equal
6020 * to the number of schedule rows in the current band of the cluster.
6022 static __isl_give isl_union_set
*collect_domain(isl_ctx
*ctx
,
6023 struct isl_sched_graph
*graph
, struct isl_clustering
*c
)
6027 isl_union_set
*domain
;
6029 space
= isl_space_params_alloc(ctx
, 0);
6030 domain
= isl_union_set_empty(space
);
6032 for (i
= 0; i
< graph
->scc
; ++i
) {
6035 if (!c
->scc_in_merge
[i
])
6037 if (c
->scc_cluster
[i
] != i
)
6039 space
= cluster_space(&c
->scc
[i
], i
);
6040 domain
= isl_union_set_add_set(domain
, isl_set_universe(space
));
6046 /* Construct a map from the original instances to the corresponding
6047 * cluster instance in the current bands of the clusters in "c".
6049 static __isl_give isl_union_map
*collect_cluster_map(isl_ctx
*ctx
,
6050 struct isl_sched_graph
*graph
, struct isl_clustering
*c
)
6054 isl_union_map
*cluster_map
;
6056 space
= isl_space_params_alloc(ctx
, 0);
6057 cluster_map
= isl_union_map_empty(space
);
6058 for (i
= 0; i
< graph
->scc
; ++i
) {
6062 if (!c
->scc_in_merge
[i
])
6065 id
= cluster_id(ctx
, c
->scc_cluster
[i
]);
6066 start
= c
->scc
[i
].band_start
;
6067 n
= c
->scc
[i
].n_total_row
- start
;
6068 for (j
= 0; j
< c
->scc
[i
].n
; ++j
) {
6071 struct isl_sched_node
*node
= &c
->scc
[i
].node
[j
];
6073 ma
= node_extract_partial_schedule_multi_aff(node
,
6075 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
,
6077 map
= isl_map_from_multi_aff(ma
);
6078 cluster_map
= isl_union_map_add_map(cluster_map
, map
);
6086 /* Add "umap" to the schedule constraints "sc" of all types of "edge"
6087 * that are not isl_edge_condition or isl_edge_conditional_validity.
6089 static __isl_give isl_schedule_constraints
*add_non_conditional_constraints(
6090 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*umap
,
6091 __isl_take isl_schedule_constraints
*sc
)
6093 enum isl_edge_type t
;
6098 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
6099 if (t
== isl_edge_condition
||
6100 t
== isl_edge_conditional_validity
)
6102 if (!is_type(edge
, t
))
6104 sc
= isl_schedule_constraints_add(sc
, t
,
6105 isl_union_map_copy(umap
));
6111 /* Add schedule constraints of types isl_edge_condition and
6112 * isl_edge_conditional_validity to "sc" by applying "umap" to
6113 * the domains of the wrapped relations in domain and range
6114 * of the corresponding tagged constraints of "edge".
6116 static __isl_give isl_schedule_constraints
*add_conditional_constraints(
6117 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*umap
,
6118 __isl_take isl_schedule_constraints
*sc
)
6120 enum isl_edge_type t
;
6121 isl_union_map
*tagged
;
6123 for (t
= isl_edge_condition
; t
<= isl_edge_conditional_validity
; ++t
) {
6124 if (!is_type(edge
, t
))
6126 if (t
== isl_edge_condition
)
6127 tagged
= isl_union_map_copy(edge
->tagged_condition
);
6129 tagged
= isl_union_map_copy(edge
->tagged_validity
);
6130 tagged
= isl_union_map_zip(tagged
);
6131 tagged
= isl_union_map_apply_domain(tagged
,
6132 isl_union_map_copy(umap
));
6133 tagged
= isl_union_map_zip(tagged
);
6134 sc
= isl_schedule_constraints_add(sc
, t
, tagged
);
6142 /* Given a mapping "cluster_map" from the original instances to
6143 * the cluster instances, add schedule constraints on the clusters
6144 * to "sc" corresponding to the original constraints represented by "edge".
6146 * For non-tagged dependence constraints, the cluster constraints
6147 * are obtained by applying "cluster_map" to the edge->map.
6149 * For tagged dependence constraints, "cluster_map" needs to be applied
6150 * to the domains of the wrapped relations in domain and range
6151 * of the tagged dependence constraints. Pick out the mappings
6152 * from these domains from "cluster_map" and construct their product.
6153 * This mapping can then be applied to the pair of domains.
6155 static __isl_give isl_schedule_constraints
*collect_edge_constraints(
6156 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*cluster_map
,
6157 __isl_take isl_schedule_constraints
*sc
)
6159 isl_union_map
*umap
;
6161 isl_union_set
*uset
;
6162 isl_union_map
*umap1
, *umap2
;
6167 umap
= isl_union_map_from_map(isl_map_copy(edge
->map
));
6168 umap
= isl_union_map_apply_domain(umap
,
6169 isl_union_map_copy(cluster_map
));
6170 umap
= isl_union_map_apply_range(umap
,
6171 isl_union_map_copy(cluster_map
));
6172 sc
= add_non_conditional_constraints(edge
, umap
, sc
);
6173 isl_union_map_free(umap
);
6175 if (!sc
|| (!is_condition(edge
) && !is_conditional_validity(edge
)))
6178 space
= isl_space_domain(isl_map_get_space(edge
->map
));
6179 uset
= isl_union_set_from_set(isl_set_universe(space
));
6180 umap1
= isl_union_map_copy(cluster_map
);
6181 umap1
= isl_union_map_intersect_domain(umap1
, uset
);
6182 space
= isl_space_range(isl_map_get_space(edge
->map
));
6183 uset
= isl_union_set_from_set(isl_set_universe(space
));
6184 umap2
= isl_union_map_copy(cluster_map
);
6185 umap2
= isl_union_map_intersect_domain(umap2
, uset
);
6186 umap
= isl_union_map_product(umap1
, umap2
);
6188 sc
= add_conditional_constraints(edge
, umap
, sc
);
6190 isl_union_map_free(umap
);
6194 /* Given a mapping "cluster_map" from the original instances to
6195 * the cluster instances, add schedule constraints on the clusters
6196 * to "sc" corresponding to all edges in "graph" between nodes that
6197 * belong to SCCs that are marked for merging in "scc_in_merge".
6199 static __isl_give isl_schedule_constraints
*collect_constraints(
6200 struct isl_sched_graph
*graph
, int *scc_in_merge
,
6201 __isl_keep isl_union_map
*cluster_map
,
6202 __isl_take isl_schedule_constraints
*sc
)
6206 for (i
= 0; i
< graph
->n_edge
; ++i
) {
6207 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
6209 if (!scc_in_merge
[edge
->src
->scc
])
6211 if (!scc_in_merge
[edge
->dst
->scc
])
6213 sc
= collect_edge_constraints(edge
, cluster_map
, sc
);
6219 /* Construct a dependence graph for scheduling clusters with respect
6220 * to each other and store the result in "merge_graph".
6221 * In particular, the nodes of the graph correspond to the schedule
6222 * dimensions of the current bands of those clusters that have been
6223 * marked for merging in "c".
6225 * First construct an isl_schedule_constraints object for this domain
6226 * by transforming the edges in "graph" to the domain.
6227 * Then initialize a dependence graph for scheduling from these
6230 static isl_stat
init_merge_graph(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6231 struct isl_clustering
*c
, struct isl_sched_graph
*merge_graph
)
6233 isl_union_set
*domain
;
6234 isl_union_map
*cluster_map
;
6235 isl_schedule_constraints
*sc
;
6238 domain
= collect_domain(ctx
, graph
, c
);
6239 sc
= isl_schedule_constraints_on_domain(domain
);
6241 return isl_stat_error
;
6242 cluster_map
= collect_cluster_map(ctx
, graph
, c
);
6243 sc
= collect_constraints(graph
, c
->scc_in_merge
, cluster_map
, sc
);
6244 isl_union_map_free(cluster_map
);
6246 r
= graph_init(merge_graph
, sc
);
6248 isl_schedule_constraints_free(sc
);
6253 /* Compute the maximal number of remaining schedule rows that still need
6254 * to be computed for the nodes that belong to clusters with the maximal
6255 * dimension for the current band (i.e., the band that is to be merged).
6256 * Only clusters that are about to be merged are considered.
6257 * "maxvar" is the maximal dimension for the current band.
6258 * "c" contains information about the clusters.
6260 * Return the maximal number of remaining schedule rows or -1 on error.
6262 static int compute_maxvar_max_slack(int maxvar
, struct isl_clustering
*c
)
6268 for (i
= 0; i
< c
->n
; ++i
) {
6270 struct isl_sched_graph
*scc
;
6272 if (!c
->scc_in_merge
[i
])
6275 nvar
= scc
->n_total_row
- scc
->band_start
;
6278 for (j
= 0; j
< scc
->n
; ++j
) {
6279 struct isl_sched_node
*node
= &scc
->node
[j
];
6282 if (node_update_vmap(node
) < 0)
6284 slack
= node
->nvar
- node
->rank
;
6285 if (slack
> max_slack
)
6293 /* If there are any clusters where the dimension of the current band
6294 * (i.e., the band that is to be merged) is smaller than "maxvar" and
6295 * if there are any nodes in such a cluster where the number
6296 * of remaining schedule rows that still need to be computed
6297 * is greater than "max_slack", then return the smallest current band
6298 * dimension of all these clusters. Otherwise return the original value
6299 * of "maxvar". Return -1 in case of any error.
6300 * Only clusters that are about to be merged are considered.
6301 * "c" contains information about the clusters.
6303 static int limit_maxvar_to_slack(int maxvar
, int max_slack
,
6304 struct isl_clustering
*c
)
6308 for (i
= 0; i
< c
->n
; ++i
) {
6310 struct isl_sched_graph
*scc
;
6312 if (!c
->scc_in_merge
[i
])
6315 nvar
= scc
->n_total_row
- scc
->band_start
;
6318 for (j
= 0; j
< scc
->n
; ++j
) {
6319 struct isl_sched_node
*node
= &scc
->node
[j
];
6322 if (node_update_vmap(node
) < 0)
6324 slack
= node
->nvar
- node
->rank
;
6325 if (slack
> max_slack
) {
6335 /* Adjust merge_graph->maxvar based on the number of remaining schedule rows
6336 * that still need to be computed. In particular, if there is a node
6337 * in a cluster where the dimension of the current band is smaller
6338 * than merge_graph->maxvar, but the number of remaining schedule rows
6339 * is greater than that of any node in a cluster with the maximal
6340 * dimension for the current band (i.e., merge_graph->maxvar),
6341 * then adjust merge_graph->maxvar to the (smallest) current band dimension
6342 * of those clusters. Without this adjustment, the total number of
6343 * schedule dimensions would be increased, resulting in a skewed view
6344 * of the number of coincident dimensions.
6345 * "c" contains information about the clusters.
6347 * If the maximize_band_depth option is set and merge_graph->maxvar is reduced,
6348 * then there is no point in attempting any merge since it will be rejected
6349 * anyway. Set merge_graph->maxvar to zero in such cases.
6351 static isl_stat
adjust_maxvar_to_slack(isl_ctx
*ctx
,
6352 struct isl_sched_graph
*merge_graph
, struct isl_clustering
*c
)
6354 int max_slack
, maxvar
;
6356 max_slack
= compute_maxvar_max_slack(merge_graph
->maxvar
, c
);
6358 return isl_stat_error
;
6359 maxvar
= limit_maxvar_to_slack(merge_graph
->maxvar
, max_slack
, c
);
6361 return isl_stat_error
;
6363 if (maxvar
< merge_graph
->maxvar
) {
6364 if (isl_options_get_schedule_maximize_band_depth(ctx
))
6365 merge_graph
->maxvar
= 0;
6367 merge_graph
->maxvar
= maxvar
;
6373 /* Return the number of coincident dimensions in the current band of "graph",
6374 * where the nodes of "graph" are assumed to be scheduled by a single band.
6376 static int get_n_coincident(struct isl_sched_graph
*graph
)
6380 for (i
= graph
->band_start
; i
< graph
->n_total_row
; ++i
)
6381 if (!graph
->node
[0].coincident
[i
])
6384 return i
- graph
->band_start
;
6387 /* Should the clusters be merged based on the cluster schedule
6388 * in the current (and only) band of "merge_graph", given that
6389 * coincidence should be maximized?
6391 * If the number of coincident schedule dimensions in the merged band
6392 * would be less than the maximal number of coincident schedule dimensions
6393 * in any of the merged clusters, then the clusters should not be merged.
6395 static isl_bool
ok_to_merge_coincident(struct isl_clustering
*c
,
6396 struct isl_sched_graph
*merge_graph
)
6403 for (i
= 0; i
< c
->n
; ++i
) {
6404 if (!c
->scc_in_merge
[i
])
6406 n_coincident
= get_n_coincident(&c
->scc
[i
]);
6407 if (n_coincident
> max_coincident
)
6408 max_coincident
= n_coincident
;
6411 n_coincident
= get_n_coincident(merge_graph
);
6413 return n_coincident
>= max_coincident
;
6416 /* Return the transformation on "node" expressed by the current (and only)
6417 * band of "merge_graph" applied to the clusters in "c".
6419 * First find the representation of "node" in its SCC in "c" and
6420 * extract the transformation expressed by the current band.
6421 * Then extract the transformation applied by "merge_graph"
6422 * to the cluster to which this SCC belongs.
6423 * Combine the two to obtain the complete transformation on the node.
6425 * Note that the range of the first transformation is an anonymous space,
6426 * while the domain of the second is named "cluster_X". The range
6427 * of the former therefore needs to be adjusted before the two
6430 static __isl_give isl_map
*extract_node_transformation(isl_ctx
*ctx
,
6431 struct isl_sched_node
*node
, struct isl_clustering
*c
,
6432 struct isl_sched_graph
*merge_graph
)
6434 struct isl_sched_node
*scc_node
, *cluster_node
;
6438 isl_multi_aff
*ma
, *ma2
;
6440 scc_node
= graph_find_node(ctx
, &c
->scc
[node
->scc
], node
->space
);
6441 if (scc_node
&& !is_node(&c
->scc
[node
->scc
], scc_node
))
6442 isl_die(ctx
, isl_error_internal
, "unable to find node",
6444 start
= c
->scc
[node
->scc
].band_start
;
6445 n
= c
->scc
[node
->scc
].n_total_row
- start
;
6446 ma
= node_extract_partial_schedule_multi_aff(scc_node
, start
, n
);
6447 space
= cluster_space(&c
->scc
[node
->scc
], c
->scc_cluster
[node
->scc
]);
6448 cluster_node
= graph_find_node(ctx
, merge_graph
, space
);
6449 if (cluster_node
&& !is_node(merge_graph
, cluster_node
))
6450 isl_die(ctx
, isl_error_internal
, "unable to find cluster",
6451 space
= isl_space_free(space
));
6452 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
6453 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
, id
);
6454 isl_space_free(space
);
6455 n
= merge_graph
->n_total_row
;
6456 ma2
= node_extract_partial_schedule_multi_aff(cluster_node
, 0, n
);
6457 ma
= isl_multi_aff_pullback_multi_aff(ma2
, ma
);
6459 return isl_map_from_multi_aff(ma
);
6462 /* Give a set of distances "set", are they bounded by a small constant
6463 * in direction "pos"?
6464 * In practice, check if they are bounded by 2 by checking that there
6465 * are no elements with a value greater than or equal to 3 or
6466 * smaller than or equal to -3.
6468 static isl_bool
distance_is_bounded(__isl_keep isl_set
*set
, int pos
)
6474 return isl_bool_error
;
6476 test
= isl_set_copy(set
);
6477 test
= isl_set_lower_bound_si(test
, isl_dim_set
, pos
, 3);
6478 bounded
= isl_set_is_empty(test
);
6481 if (bounded
< 0 || !bounded
)
6484 test
= isl_set_copy(set
);
6485 test
= isl_set_upper_bound_si(test
, isl_dim_set
, pos
, -3);
6486 bounded
= isl_set_is_empty(test
);
6492 /* Does the set "set" have a fixed (but possible parametric) value
6493 * at dimension "pos"?
6495 static isl_bool
has_single_value(__isl_keep isl_set
*set
, int pos
)
6501 return isl_bool_error
;
6502 set
= isl_set_copy(set
);
6503 n
= isl_set_dim(set
, isl_dim_set
);
6504 set
= isl_set_project_out(set
, isl_dim_set
, pos
+ 1, n
- (pos
+ 1));
6505 set
= isl_set_project_out(set
, isl_dim_set
, 0, pos
);
6506 single
= isl_set_is_singleton(set
);
6512 /* Does "map" have a fixed (but possible parametric) value
6513 * at dimension "pos" of either its domain or its range?
6515 static isl_bool
has_singular_src_or_dst(__isl_keep isl_map
*map
, int pos
)
6520 set
= isl_map_domain(isl_map_copy(map
));
6521 single
= has_single_value(set
, pos
);
6524 if (single
< 0 || single
)
6527 set
= isl_map_range(isl_map_copy(map
));
6528 single
= has_single_value(set
, pos
);
6534 /* Does the edge "edge" from "graph" have bounded dependence distances
6535 * in the merged graph "merge_graph" of a selection of clusters in "c"?
6537 * Extract the complete transformations of the source and destination
6538 * nodes of the edge, apply them to the edge constraints and
6539 * compute the differences. Finally, check if these differences are bounded
6540 * in each direction.
6542 * If the dimension of the band is greater than the number of
6543 * dimensions that can be expected to be optimized by the edge
6544 * (based on its weight), then also allow the differences to be unbounded
6545 * in the remaining dimensions, but only if either the source or
6546 * the destination has a fixed value in that direction.
6547 * This allows a statement that produces values that are used by
6548 * several instances of another statement to be merged with that
6550 * However, merging such clusters will introduce an inherently
6551 * large proximity distance inside the merged cluster, meaning
6552 * that proximity distances will no longer be optimized in
6553 * subsequent merges. These merges are therefore only allowed
6554 * after all other possible merges have been tried.
6555 * The first time such a merge is encountered, the weight of the edge
6556 * is replaced by a negative weight. The second time (i.e., after
6557 * all merges over edges with a non-negative weight have been tried),
6558 * the merge is allowed.
6560 static isl_bool
has_bounded_distances(isl_ctx
*ctx
, struct isl_sched_edge
*edge
,
6561 struct isl_sched_graph
*graph
, struct isl_clustering
*c
,
6562 struct isl_sched_graph
*merge_graph
)
6569 map
= isl_map_copy(edge
->map
);
6570 t
= extract_node_transformation(ctx
, edge
->src
, c
, merge_graph
);
6571 map
= isl_map_apply_domain(map
, t
);
6572 t
= extract_node_transformation(ctx
, edge
->dst
, c
, merge_graph
);
6573 map
= isl_map_apply_range(map
, t
);
6574 dist
= isl_map_deltas(isl_map_copy(map
));
6576 bounded
= isl_bool_true
;
6577 n
= isl_set_dim(dist
, isl_dim_set
);
6578 n_slack
= n
- edge
->weight
;
6579 if (edge
->weight
< 0)
6580 n_slack
-= graph
->max_weight
+ 1;
6581 for (i
= 0; i
< n
; ++i
) {
6582 isl_bool bounded_i
, singular_i
;
6584 bounded_i
= distance_is_bounded(dist
, i
);
6589 if (edge
->weight
>= 0)
6590 bounded
= isl_bool_false
;
6594 singular_i
= has_singular_src_or_dst(map
, i
);
6599 bounded
= isl_bool_false
;
6602 if (!bounded
&& i
>= n
&& edge
->weight
>= 0)
6603 edge
->weight
-= graph
->max_weight
+ 1;
6611 return isl_bool_error
;
6614 /* Should the clusters be merged based on the cluster schedule
6615 * in the current (and only) band of "merge_graph"?
6616 * "graph" is the original dependence graph, while "c" records
6617 * which SCCs are involved in the latest merge.
6619 * In particular, is there at least one proximity constraint
6620 * that is optimized by the merge?
6622 * A proximity constraint is considered to be optimized
6623 * if the dependence distances are small.
6625 static isl_bool
ok_to_merge_proximity(isl_ctx
*ctx
,
6626 struct isl_sched_graph
*graph
, struct isl_clustering
*c
,
6627 struct isl_sched_graph
*merge_graph
)
6631 for (i
= 0; i
< graph
->n_edge
; ++i
) {
6632 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
6635 if (!is_proximity(edge
))
6637 if (!c
->scc_in_merge
[edge
->src
->scc
])
6639 if (!c
->scc_in_merge
[edge
->dst
->scc
])
6641 if (c
->scc_cluster
[edge
->dst
->scc
] ==
6642 c
->scc_cluster
[edge
->src
->scc
])
6644 bounded
= has_bounded_distances(ctx
, edge
, graph
, c
,
6646 if (bounded
< 0 || bounded
)
6650 return isl_bool_false
;
6653 /* Should the clusters be merged based on the cluster schedule
6654 * in the current (and only) band of "merge_graph"?
6655 * "graph" is the original dependence graph, while "c" records
6656 * which SCCs are involved in the latest merge.
6658 * If the current band is empty, then the clusters should not be merged.
6660 * If the band depth should be maximized and the merge schedule
6661 * is incomplete (meaning that the dimension of some of the schedule
6662 * bands in the original schedule will be reduced), then the clusters
6663 * should not be merged.
6665 * If the schedule_maximize_coincidence option is set, then check that
6666 * the number of coincident schedule dimensions is not reduced.
6668 * Finally, only allow the merge if at least one proximity
6669 * constraint is optimized.
6671 static isl_bool
ok_to_merge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6672 struct isl_clustering
*c
, struct isl_sched_graph
*merge_graph
)
6674 if (merge_graph
->n_total_row
== merge_graph
->band_start
)
6675 return isl_bool_false
;
6677 if (isl_options_get_schedule_maximize_band_depth(ctx
) &&
6678 merge_graph
->n_total_row
< merge_graph
->maxvar
)
6679 return isl_bool_false
;
6681 if (isl_options_get_schedule_maximize_coincidence(ctx
)) {
6684 ok
= ok_to_merge_coincident(c
, merge_graph
);
6689 return ok_to_merge_proximity(ctx
, graph
, c
, merge_graph
);
6692 /* Apply the schedule in "t_node" to the "n" rows starting at "first"
6693 * of the schedule in "node" and return the result.
6695 * That is, essentially compute
6697 * T * N(first:first+n-1)
6699 * taking into account the constant term and the parameter coefficients
6702 static __isl_give isl_mat
*node_transformation(isl_ctx
*ctx
,
6703 struct isl_sched_node
*t_node
, struct isl_sched_node
*node
,
6708 int n_row
, n_col
, n_param
, n_var
;
6710 n_param
= node
->nparam
;
6712 n_row
= isl_mat_rows(t_node
->sched
);
6713 n_col
= isl_mat_cols(node
->sched
);
6714 t
= isl_mat_alloc(ctx
, n_row
, n_col
);
6717 for (i
= 0; i
< n_row
; ++i
) {
6718 isl_seq_cpy(t
->row
[i
], t_node
->sched
->row
[i
], 1 + n_param
);
6719 isl_seq_clr(t
->row
[i
] + 1 + n_param
, n_var
);
6720 for (j
= 0; j
< n
; ++j
)
6721 isl_seq_addmul(t
->row
[i
],
6722 t_node
->sched
->row
[i
][1 + n_param
+ j
],
6723 node
->sched
->row
[first
+ j
],
6724 1 + n_param
+ n_var
);
6729 /* Apply the cluster schedule in "t_node" to the current band
6730 * schedule of the nodes in "graph".
6732 * In particular, replace the rows starting at band_start
6733 * by the result of applying the cluster schedule in "t_node"
6734 * to the original rows.
6736 * The coincidence of the schedule is determined by the coincidence
6737 * of the cluster schedule.
6739 static isl_stat
transform(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6740 struct isl_sched_node
*t_node
)
6746 start
= graph
->band_start
;
6747 n
= graph
->n_total_row
- start
;
6749 n_new
= isl_mat_rows(t_node
->sched
);
6750 for (i
= 0; i
< graph
->n
; ++i
) {
6751 struct isl_sched_node
*node
= &graph
->node
[i
];
6754 t
= node_transformation(ctx
, t_node
, node
, start
, n
);
6755 node
->sched
= isl_mat_drop_rows(node
->sched
, start
, n
);
6756 node
->sched
= isl_mat_concat(node
->sched
, t
);
6757 node
->sched_map
= isl_map_free(node
->sched_map
);
6759 return isl_stat_error
;
6760 for (j
= 0; j
< n_new
; ++j
)
6761 node
->coincident
[start
+ j
] = t_node
->coincident
[j
];
6763 graph
->n_total_row
-= n
;
6765 graph
->n_total_row
+= n_new
;
6766 graph
->n_row
+= n_new
;
6771 /* Merge the clusters marked for merging in "c" into a single
6772 * cluster using the cluster schedule in the current band of "merge_graph".
6773 * The representative SCC for the new cluster is the SCC with
6774 * the smallest index.
6776 * The current band schedule of each SCC in the new cluster is obtained
6777 * by applying the schedule of the corresponding original cluster
6778 * to the original band schedule.
6779 * All SCCs in the new cluster have the same number of schedule rows.
6781 static isl_stat
merge(isl_ctx
*ctx
, struct isl_clustering
*c
,
6782 struct isl_sched_graph
*merge_graph
)
6788 for (i
= 0; i
< c
->n
; ++i
) {
6789 struct isl_sched_node
*node
;
6791 if (!c
->scc_in_merge
[i
])
6795 space
= cluster_space(&c
->scc
[i
], c
->scc_cluster
[i
]);
6796 node
= graph_find_node(ctx
, merge_graph
, space
);
6797 isl_space_free(space
);
6799 return isl_stat_error
;
6800 if (!is_node(merge_graph
, node
))
6801 isl_die(ctx
, isl_error_internal
,
6802 "unable to find cluster",
6803 return isl_stat_error
);
6804 if (transform(ctx
, &c
->scc
[i
], node
) < 0)
6805 return isl_stat_error
;
6806 c
->scc_cluster
[i
] = cluster
;
6812 /* Try and merge the clusters of SCCs marked in c->scc_in_merge
6813 * by scheduling the current cluster bands with respect to each other.
6815 * Construct a dependence graph with a space for each cluster and
6816 * with the coordinates of each space corresponding to the schedule
6817 * dimensions of the current band of that cluster.
6818 * Construct a cluster schedule in this cluster dependence graph and
6819 * apply it to the current cluster bands if it is applicable
6820 * according to ok_to_merge.
6822 * If the number of remaining schedule dimensions in a cluster
6823 * with a non-maximal current schedule dimension is greater than
6824 * the number of remaining schedule dimensions in clusters
6825 * with a maximal current schedule dimension, then restrict
6826 * the number of rows to be computed in the cluster schedule
6827 * to the minimal such non-maximal current schedule dimension.
6828 * Do this by adjusting merge_graph.maxvar.
6830 * Return isl_bool_true if the clusters have effectively been merged
6831 * into a single cluster.
6833 * Note that since the standard scheduling algorithm minimizes the maximal
6834 * distance over proximity constraints, the proximity constraints between
6835 * the merged clusters may not be optimized any further than what is
6836 * sufficient to bring the distances within the limits of the internal
6837 * proximity constraints inside the individual clusters.
6838 * It may therefore make sense to perform an additional translation step
6839 * to bring the clusters closer to each other, while maintaining
6840 * the linear part of the merging schedule found using the standard
6841 * scheduling algorithm.
6843 static isl_bool
try_merge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6844 struct isl_clustering
*c
)
6846 struct isl_sched_graph merge_graph
= { 0 };
6849 if (init_merge_graph(ctx
, graph
, c
, &merge_graph
) < 0)
6852 if (compute_maxvar(&merge_graph
) < 0)
6854 if (adjust_maxvar_to_slack(ctx
, &merge_graph
,c
) < 0)
6856 if (compute_schedule_wcc_band(ctx
, &merge_graph
) < 0)
6858 merged
= ok_to_merge(ctx
, graph
, c
, &merge_graph
);
6859 if (merged
&& merge(ctx
, c
, &merge_graph
) < 0)
6862 graph_free(ctx
, &merge_graph
);
6865 graph_free(ctx
, &merge_graph
);
6866 return isl_bool_error
;
6869 /* Is there any edge marked "no_merge" between two SCCs that are
6870 * about to be merged (i.e., that are set in "scc_in_merge")?
6871 * "merge_edge" is the proximity edge along which the clusters of SCCs
6872 * are going to be merged.
6874 * If there is any edge between two SCCs with a negative weight,
6875 * while the weight of "merge_edge" is non-negative, then this
6876 * means that the edge was postponed. "merge_edge" should then
6877 * also be postponed since merging along the edge with negative weight should
6878 * be postponed until all edges with non-negative weight have been tried.
6879 * Replace the weight of "merge_edge" by a negative weight as well and
6880 * tell the caller not to attempt a merge.
6882 static int any_no_merge(struct isl_sched_graph
*graph
, int *scc_in_merge
,
6883 struct isl_sched_edge
*merge_edge
)
6887 for (i
= 0; i
< graph
->n_edge
; ++i
) {
6888 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
6890 if (!scc_in_merge
[edge
->src
->scc
])
6892 if (!scc_in_merge
[edge
->dst
->scc
])
6896 if (merge_edge
->weight
>= 0 && edge
->weight
< 0) {
6897 merge_edge
->weight
-= graph
->max_weight
+ 1;
6905 /* Merge the two clusters in "c" connected by the edge in "graph"
6906 * with index "edge" into a single cluster.
6907 * If it turns out to be impossible to merge these two clusters,
6908 * then mark the edge as "no_merge" such that it will not be
6911 * First mark all SCCs that need to be merged. This includes the SCCs
6912 * in the two clusters, but it may also include the SCCs
6913 * of intermediate clusters.
6914 * If there is already a no_merge edge between any pair of such SCCs,
6915 * then simply mark the current edge as no_merge as well.
6916 * Likewise, if any of those edges was postponed by has_bounded_distances,
6917 * then postpone the current edge as well.
6918 * Otherwise, try and merge the clusters and mark "edge" as "no_merge"
6919 * if the clusters did not end up getting merged, unless the non-merge
6920 * is due to the fact that the edge was postponed. This postponement
6921 * can be recognized by a change in weight (from non-negative to negative).
6923 static isl_stat
merge_clusters_along_edge(isl_ctx
*ctx
,
6924 struct isl_sched_graph
*graph
, int edge
, struct isl_clustering
*c
)
6927 int edge_weight
= graph
->edge
[edge
].weight
;
6929 if (mark_merge_sccs(ctx
, graph
, edge
, c
) < 0)
6930 return isl_stat_error
;
6932 if (any_no_merge(graph
, c
->scc_in_merge
, &graph
->edge
[edge
]))
6933 merged
= isl_bool_false
;
6935 merged
= try_merge(ctx
, graph
, c
);
6937 return isl_stat_error
;
6938 if (!merged
&& edge_weight
== graph
->edge
[edge
].weight
)
6939 graph
->edge
[edge
].no_merge
= 1;
6944 /* Does "node" belong to the cluster identified by "cluster"?
6946 static int node_cluster_exactly(struct isl_sched_node
*node
, int cluster
)
6948 return node
->cluster
== cluster
;
6951 /* Does "edge" connect two nodes belonging to the cluster
6952 * identified by "cluster"?
6954 static int edge_cluster_exactly(struct isl_sched_edge
*edge
, int cluster
)
6956 return edge
->src
->cluster
== cluster
&& edge
->dst
->cluster
== cluster
;
6959 /* Swap the schedule of "node1" and "node2".
6960 * Both nodes have been derived from the same node in a common parent graph.
6961 * Since the "coincident" field is shared with that node
6962 * in the parent graph, there is no need to also swap this field.
6964 static void swap_sched(struct isl_sched_node
*node1
,
6965 struct isl_sched_node
*node2
)
6970 sched
= node1
->sched
;
6971 node1
->sched
= node2
->sched
;
6972 node2
->sched
= sched
;
6974 sched_map
= node1
->sched_map
;
6975 node1
->sched_map
= node2
->sched_map
;
6976 node2
->sched_map
= sched_map
;
6979 /* Copy the current band schedule from the SCCs that form the cluster
6980 * with index "pos" to the actual cluster at position "pos".
6981 * By construction, the index of the first SCC that belongs to the cluster
6984 * The order of the nodes inside both the SCCs and the cluster
6985 * is assumed to be same as the order in the original "graph".
6987 * Since the SCC graphs will no longer be used after this function,
6988 * the schedules are actually swapped rather than copied.
6990 static isl_stat
copy_partial(struct isl_sched_graph
*graph
,
6991 struct isl_clustering
*c
, int pos
)
6995 c
->cluster
[pos
].n_total_row
= c
->scc
[pos
].n_total_row
;
6996 c
->cluster
[pos
].n_row
= c
->scc
[pos
].n_row
;
6997 c
->cluster
[pos
].maxvar
= c
->scc
[pos
].maxvar
;
6999 for (i
= 0; i
< graph
->n
; ++i
) {
7003 if (graph
->node
[i
].cluster
!= pos
)
7005 s
= graph
->node
[i
].scc
;
7006 k
= c
->scc_node
[s
]++;
7007 swap_sched(&c
->cluster
[pos
].node
[j
], &c
->scc
[s
].node
[k
]);
7008 if (c
->scc
[s
].maxvar
> c
->cluster
[pos
].maxvar
)
7009 c
->cluster
[pos
].maxvar
= c
->scc
[s
].maxvar
;
7016 /* Is there a (conditional) validity dependence from node[j] to node[i],
7017 * forcing node[i] to follow node[j] or do the nodes belong to the same
7020 static isl_bool
node_follows_strong_or_same_cluster(int i
, int j
, void *user
)
7022 struct isl_sched_graph
*graph
= user
;
7024 if (graph
->node
[i
].cluster
== graph
->node
[j
].cluster
)
7025 return isl_bool_true
;
7026 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
7029 /* Extract the merged clusters of SCCs in "graph", sort them, and
7030 * store them in c->clusters. Update c->scc_cluster accordingly.
7032 * First keep track of the cluster containing the SCC to which a node
7033 * belongs in the node itself.
7034 * Then extract the clusters into c->clusters, copying the current
7035 * band schedule from the SCCs that belong to the cluster.
7036 * Do this only once per cluster.
7038 * Finally, topologically sort the clusters and update c->scc_cluster
7039 * to match the new scc numbering. While the SCCs were originally
7040 * sorted already, some SCCs that depend on some other SCCs may
7041 * have been merged with SCCs that appear before these other SCCs.
7042 * A reordering may therefore be required.
7044 static isl_stat
extract_clusters(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
7045 struct isl_clustering
*c
)
7049 for (i
= 0; i
< graph
->n
; ++i
)
7050 graph
->node
[i
].cluster
= c
->scc_cluster
[graph
->node
[i
].scc
];
7052 for (i
= 0; i
< graph
->scc
; ++i
) {
7053 if (c
->scc_cluster
[i
] != i
)
7055 if (extract_sub_graph(ctx
, graph
, &node_cluster_exactly
,
7056 &edge_cluster_exactly
, i
, &c
->cluster
[i
]) < 0)
7057 return isl_stat_error
;
7058 c
->cluster
[i
].src_scc
= -1;
7059 c
->cluster
[i
].dst_scc
= -1;
7060 if (copy_partial(graph
, c
, i
) < 0)
7061 return isl_stat_error
;
7064 if (detect_ccs(ctx
, graph
, &node_follows_strong_or_same_cluster
) < 0)
7065 return isl_stat_error
;
7066 for (i
= 0; i
< graph
->n
; ++i
)
7067 c
->scc_cluster
[graph
->node
[i
].scc
] = graph
->node
[i
].cluster
;
7072 /* Compute weights on the proximity edges of "graph" that can
7073 * be used by find_proximity to find the most appropriate
7074 * proximity edge to use to merge two clusters in "c".
7075 * The weights are also used by has_bounded_distances to determine
7076 * whether the merge should be allowed.
7077 * Store the maximum of the computed weights in graph->max_weight.
7079 * The computed weight is a measure for the number of remaining schedule
7080 * dimensions that can still be completely aligned.
7081 * In particular, compute the number of equalities between
7082 * input dimensions and output dimensions in the proximity constraints.
7083 * The directions that are already handled by outer schedule bands
7084 * are projected out prior to determining this number.
7086 * Edges that will never be considered by find_proximity are ignored.
7088 static isl_stat
compute_weights(struct isl_sched_graph
*graph
,
7089 struct isl_clustering
*c
)
7093 graph
->max_weight
= 0;
7095 for (i
= 0; i
< graph
->n_edge
; ++i
) {
7096 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
7097 struct isl_sched_node
*src
= edge
->src
;
7098 struct isl_sched_node
*dst
= edge
->dst
;
7099 isl_basic_map
*hull
;
7103 prox
= is_non_empty_proximity(edge
);
7105 return isl_stat_error
;
7108 if (bad_cluster(&c
->scc
[edge
->src
->scc
]) ||
7109 bad_cluster(&c
->scc
[edge
->dst
->scc
]))
7111 if (c
->scc_cluster
[edge
->dst
->scc
] ==
7112 c
->scc_cluster
[edge
->src
->scc
])
7115 hull
= isl_map_affine_hull(isl_map_copy(edge
->map
));
7116 hull
= isl_basic_map_transform_dims(hull
, isl_dim_in
, 0,
7117 isl_mat_copy(src
->vmap
));
7118 hull
= isl_basic_map_transform_dims(hull
, isl_dim_out
, 0,
7119 isl_mat_copy(dst
->vmap
));
7120 hull
= isl_basic_map_project_out(hull
,
7121 isl_dim_in
, 0, src
->rank
);
7122 hull
= isl_basic_map_project_out(hull
,
7123 isl_dim_out
, 0, dst
->rank
);
7124 hull
= isl_basic_map_remove_divs(hull
);
7125 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
7126 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
7127 hull
= isl_basic_map_drop_constraints_not_involving_dims(hull
,
7128 isl_dim_in
, 0, n_in
);
7129 hull
= isl_basic_map_drop_constraints_not_involving_dims(hull
,
7130 isl_dim_out
, 0, n_out
);
7132 return isl_stat_error
;
7133 edge
->weight
= isl_basic_map_n_equality(hull
);
7134 isl_basic_map_free(hull
);
7136 if (edge
->weight
> graph
->max_weight
)
7137 graph
->max_weight
= edge
->weight
;
7143 /* Call compute_schedule_finish_band on each of the clusters in "c"
7144 * in their topological order. This order is determined by the scc
7145 * fields of the nodes in "graph".
7146 * Combine the results in a sequence expressing the topological order.
7148 * If there is only one cluster left, then there is no need to introduce
7149 * a sequence node. Also, in this case, the cluster necessarily contains
7150 * the SCC at position 0 in the original graph and is therefore also
7151 * stored in the first cluster of "c".
7153 static __isl_give isl_schedule_node
*finish_bands_clustering(
7154 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
7155 struct isl_clustering
*c
)
7159 isl_union_set_list
*filters
;
7161 if (graph
->scc
== 1)
7162 return compute_schedule_finish_band(node
, &c
->cluster
[0], 0);
7164 ctx
= isl_schedule_node_get_ctx(node
);
7166 filters
= extract_sccs(ctx
, graph
);
7167 node
= isl_schedule_node_insert_sequence(node
, filters
);
7169 for (i
= 0; i
< graph
->scc
; ++i
) {
7170 int j
= c
->scc_cluster
[i
];
7171 node
= isl_schedule_node_child(node
, i
);
7172 node
= isl_schedule_node_child(node
, 0);
7173 node
= compute_schedule_finish_band(node
, &c
->cluster
[j
], 0);
7174 node
= isl_schedule_node_parent(node
);
7175 node
= isl_schedule_node_parent(node
);
7181 /* Compute a schedule for a connected dependence graph by first considering
7182 * each strongly connected component (SCC) in the graph separately and then
7183 * incrementally combining them into clusters.
7184 * Return the updated schedule node.
7186 * Initially, each cluster consists of a single SCC, each with its
7187 * own band schedule. The algorithm then tries to merge pairs
7188 * of clusters along a proximity edge until no more suitable
7189 * proximity edges can be found. During this merging, the schedule
7190 * is maintained in the individual SCCs.
7191 * After the merging is completed, the full resulting clusters
7192 * are extracted and in finish_bands_clustering,
7193 * compute_schedule_finish_band is called on each of them to integrate
7194 * the band into "node" and to continue the computation.
7196 * compute_weights initializes the weights that are used by find_proximity.
7198 static __isl_give isl_schedule_node
*compute_schedule_wcc_clustering(
7199 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
7202 struct isl_clustering c
;
7205 ctx
= isl_schedule_node_get_ctx(node
);
7207 if (clustering_init(ctx
, &c
, graph
) < 0)
7210 if (compute_weights(graph
, &c
) < 0)
7214 i
= find_proximity(graph
, &c
);
7217 if (i
>= graph
->n_edge
)
7219 if (merge_clusters_along_edge(ctx
, graph
, i
, &c
) < 0)
7223 if (extract_clusters(ctx
, graph
, &c
) < 0)
7226 node
= finish_bands_clustering(node
, graph
, &c
);
7228 clustering_free(ctx
, &c
);
7231 clustering_free(ctx
, &c
);
7232 return isl_schedule_node_free(node
);
7235 /* Compute a schedule for a connected dependence graph and return
7236 * the updated schedule node.
7238 * If Feautrier's algorithm is selected, we first recursively try to satisfy
7239 * as many validity dependences as possible. When all validity dependences
7240 * are satisfied we extend the schedule to a full-dimensional schedule.
7242 * Call compute_schedule_wcc_whole or compute_schedule_wcc_clustering
7243 * depending on whether the user has selected the option to try and
7244 * compute a schedule for the entire (weakly connected) component first.
7245 * If there is only a single strongly connected component (SCC), then
7246 * there is no point in trying to combine SCCs
7247 * in compute_schedule_wcc_clustering, so compute_schedule_wcc_whole
7248 * is called instead.
7250 static __isl_give isl_schedule_node
*compute_schedule_wcc(
7251 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
7258 ctx
= isl_schedule_node_get_ctx(node
);
7259 if (detect_sccs(ctx
, graph
) < 0)
7260 return isl_schedule_node_free(node
);
7262 if (compute_maxvar(graph
) < 0)
7263 return isl_schedule_node_free(node
);
7265 if (need_feautrier_step(ctx
, graph
))
7266 return compute_schedule_wcc_feautrier(node
, graph
);
7268 if (graph
->scc
<= 1 || isl_options_get_schedule_whole_component(ctx
))
7269 return compute_schedule_wcc_whole(node
, graph
);
7271 return compute_schedule_wcc_clustering(node
, graph
);
7274 /* Compute a schedule for each group of nodes identified by node->scc
7275 * separately and then combine them in a sequence node (or as set node
7276 * if graph->weak is set) inserted at position "node" of the schedule tree.
7277 * Return the updated schedule node.
7279 * If "wcc" is set then each of the groups belongs to a single
7280 * weakly connected component in the dependence graph so that
7281 * there is no need for compute_sub_schedule to look for weakly
7282 * connected components.
7284 * If a set node would be introduced and if the number of components
7285 * is equal to the number of nodes, then check if the schedule
7286 * is already complete. If so, a redundant set node would be introduced
7287 * (without any further descendants) stating that the statements
7288 * can be executed in arbitrary order, which is also expressed
7289 * by the absence of any node. Refrain from inserting any nodes
7290 * in this case and simply return.
7292 static __isl_give isl_schedule_node
*compute_component_schedule(
7293 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
7298 isl_union_set_list
*filters
;
7303 if (graph
->weak
&& graph
->scc
== graph
->n
) {
7304 if (compute_maxvar(graph
) < 0)
7305 return isl_schedule_node_free(node
);
7306 if (graph
->n_row
>= graph
->maxvar
)
7310 ctx
= isl_schedule_node_get_ctx(node
);
7311 filters
= extract_sccs(ctx
, graph
);
7313 node
= isl_schedule_node_insert_set(node
, filters
);
7315 node
= isl_schedule_node_insert_sequence(node
, filters
);
7317 for (component
= 0; component
< graph
->scc
; ++component
) {
7318 node
= isl_schedule_node_child(node
, component
);
7319 node
= isl_schedule_node_child(node
, 0);
7320 node
= compute_sub_schedule(node
, ctx
, graph
,
7322 &edge_scc_exactly
, component
, wcc
);
7323 node
= isl_schedule_node_parent(node
);
7324 node
= isl_schedule_node_parent(node
);
7330 /* Compute a schedule for the given dependence graph and insert it at "node".
7331 * Return the updated schedule node.
7333 * We first check if the graph is connected (through validity and conditional
7334 * validity dependences) and, if not, compute a schedule
7335 * for each component separately.
7336 * If the schedule_serialize_sccs option is set, then we check for strongly
7337 * connected components instead and compute a separate schedule for
7338 * each such strongly connected component.
7340 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
7341 struct isl_sched_graph
*graph
)
7348 ctx
= isl_schedule_node_get_ctx(node
);
7349 if (isl_options_get_schedule_serialize_sccs(ctx
)) {
7350 if (detect_sccs(ctx
, graph
) < 0)
7351 return isl_schedule_node_free(node
);
7353 if (detect_wccs(ctx
, graph
) < 0)
7354 return isl_schedule_node_free(node
);
7358 return compute_component_schedule(node
, graph
, 1);
7360 return compute_schedule_wcc(node
, graph
);
7363 /* Compute a schedule on sc->domain that respects the given schedule
7366 * In particular, the schedule respects all the validity dependences.
7367 * If the default isl scheduling algorithm is used, it tries to minimize
7368 * the dependence distances over the proximity dependences.
7369 * If Feautrier's scheduling algorithm is used, the proximity dependence
7370 * distances are only minimized during the extension to a full-dimensional
7373 * If there are any condition and conditional validity dependences,
7374 * then the conditional validity dependences may be violated inside
7375 * a tilable band, provided they have no adjacent non-local
7376 * condition dependences.
7378 __isl_give isl_schedule
*isl_schedule_constraints_compute_schedule(
7379 __isl_take isl_schedule_constraints
*sc
)
7381 isl_ctx
*ctx
= isl_schedule_constraints_get_ctx(sc
);
7382 struct isl_sched_graph graph
= { 0 };
7383 isl_schedule
*sched
;
7384 isl_schedule_node
*node
;
7385 isl_union_set
*domain
;
7387 sc
= isl_schedule_constraints_align_params(sc
);
7389 domain
= isl_schedule_constraints_get_domain(sc
);
7390 if (isl_union_set_n_set(domain
) == 0) {
7391 isl_schedule_constraints_free(sc
);
7392 return isl_schedule_from_domain(domain
);
7395 if (graph_init(&graph
, sc
) < 0)
7396 domain
= isl_union_set_free(domain
);
7398 node
= isl_schedule_node_from_domain(domain
);
7399 node
= isl_schedule_node_child(node
, 0);
7401 node
= compute_schedule(node
, &graph
);
7402 sched
= isl_schedule_node_get_schedule(node
);
7403 isl_schedule_node_free(node
);
7405 graph_free(ctx
, &graph
);
7406 isl_schedule_constraints_free(sc
);
7411 /* Compute a schedule for the given union of domains that respects
7412 * all the validity dependences and minimizes
7413 * the dependence distances over the proximity dependences.
7415 * This function is kept for backward compatibility.
7417 __isl_give isl_schedule
*isl_union_set_compute_schedule(
7418 __isl_take isl_union_set
*domain
,
7419 __isl_take isl_union_map
*validity
,
7420 __isl_take isl_union_map
*proximity
)
7422 isl_schedule_constraints
*sc
;
7424 sc
= isl_schedule_constraints_on_domain(domain
);
7425 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
7426 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
7428 return isl_schedule_constraints_compute_schedule(sc
);