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_pw_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_pw_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 isl_size nvar
= isl_set_dim(set
, isl_dim_set
);
752 if (nvar
> graph
->maxvar
)
753 graph
->maxvar
= nvar
;
758 return isl_stat_error
;
762 /* Compute the number of rows that should be allocated for the schedule.
763 * In particular, we need one row for each variable or one row
764 * for each basic map in the dependences.
765 * Note that it is practically impossible to exhaust both
766 * the number of dependences and the number of variables.
768 static isl_stat
compute_max_row(struct isl_sched_graph
*graph
,
769 __isl_keep isl_schedule_constraints
*sc
)
773 isl_union_set
*domain
;
777 domain
= isl_schedule_constraints_get_domain(sc
);
778 r
= isl_union_set_foreach_set(domain
, &init_n_maxvar
, graph
);
779 isl_union_set_free(domain
);
781 return isl_stat_error
;
782 n_edge
= isl_schedule_constraints_n_basic_map(sc
);
784 return isl_stat_error
;
785 graph
->max_row
= n_edge
+ graph
->maxvar
;
790 /* Does "bset" have any defining equalities for its set variables?
792 static isl_bool
has_any_defining_equality(__isl_keep isl_basic_set
*bset
)
797 n
= isl_basic_set_dim(bset
, isl_dim_set
);
799 return isl_bool_error
;
801 for (i
= 0; i
< n
; ++i
) {
804 has
= isl_basic_set_has_defining_equality(bset
, isl_dim_set
, i
,
810 return isl_bool_false
;
813 /* Set the entries of node->max to the value of the schedule_max_coefficient
816 static isl_stat
set_max_coefficient(isl_ctx
*ctx
, struct isl_sched_node
*node
)
820 max
= isl_options_get_schedule_max_coefficient(ctx
);
824 node
->max
= isl_vec_alloc(ctx
, node
->nvar
);
825 node
->max
= isl_vec_set_si(node
->max
, max
);
827 return isl_stat_error
;
832 /* Set the entries of node->max to the minimum of the schedule_max_coefficient
833 * option (if set) and half of the minimum of the sizes in the other
834 * dimensions. Round up when computing the half such that
835 * if the minimum of the sizes is one, half of the size is taken to be one
837 * If the global minimum is unbounded (i.e., if both
838 * the schedule_max_coefficient is not set and the sizes in the other
839 * dimensions are unbounded), then store a negative value.
840 * If the schedule coefficient is close to the size of the instance set
841 * in another dimension, then the schedule may represent a loop
842 * coalescing transformation (especially if the coefficient
843 * in that other dimension is one). Forcing the coefficient to be
844 * smaller than or equal to half the minimal size should avoid this
847 static isl_stat
compute_max_coefficient(isl_ctx
*ctx
,
848 struct isl_sched_node
*node
)
854 max
= isl_options_get_schedule_max_coefficient(ctx
);
855 v
= isl_vec_alloc(ctx
, node
->nvar
);
857 return isl_stat_error
;
859 for (i
= 0; i
< node
->nvar
; ++i
) {
860 isl_int_set_si(v
->el
[i
], max
);
861 isl_int_mul_si(v
->el
[i
], v
->el
[i
], 2);
864 for (i
= 0; i
< node
->nvar
; ++i
) {
867 size
= isl_multi_val_get_val(node
->sizes
, i
);
870 if (!isl_val_is_int(size
)) {
874 for (j
= 0; j
< node
->nvar
; ++j
) {
877 if (isl_int_is_neg(v
->el
[j
]) ||
878 isl_int_gt(v
->el
[j
], size
->n
))
879 isl_int_set(v
->el
[j
], size
->n
);
884 for (i
= 0; i
< node
->nvar
; ++i
)
885 isl_int_cdiv_q_ui(v
->el
[i
], v
->el
[i
], 2);
891 return isl_stat_error
;
894 /* Construct an identifier for node "node", which will represent "set".
895 * The name of the identifier is either "compressed" or
896 * "compressed_<name>", with <name> the name of the space of "set".
897 * The user pointer of the identifier points to "node".
899 static __isl_give isl_id
*construct_compressed_id(__isl_keep isl_set
*set
,
900 struct isl_sched_node
*node
)
909 has_name
= isl_set_has_tuple_name(set
);
913 ctx
= isl_set_get_ctx(set
);
915 return isl_id_alloc(ctx
, "compressed", node
);
917 p
= isl_printer_to_str(ctx
);
918 name
= isl_set_get_tuple_name(set
);
919 p
= isl_printer_print_str(p
, "compressed_");
920 p
= isl_printer_print_str(p
, name
);
921 id_name
= isl_printer_get_str(p
);
924 id
= isl_id_alloc(ctx
, id_name
, node
);
930 /* Construct a map that isolates the variable in position "pos" in "set".
934 * [i_0, ..., i_pos-1, i_pos+1, ...] -> [i_pos]
936 static __isl_give isl_map
*isolate(__isl_take isl_set
*set
, int pos
)
940 map
= isl_set_project_onto_map(set
, isl_dim_set
, pos
, 1);
941 map
= isl_map_project_out(map
, isl_dim_in
, pos
, 1);
945 /* Compute and return the size of "set" in dimension "dim".
946 * The size is taken to be the difference in values for that variable
947 * for fixed values of the other variables.
948 * This assumes that "set" is convex.
949 * In particular, the variable is first isolated from the other variables
950 * in the range of a map
952 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [i_dim]
954 * and then duplicated
956 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [[i_dim] -> [i_dim']]
958 * The shared variables are then projected out and the maximal value
959 * of i_dim' - i_dim is computed.
961 static __isl_give isl_val
*compute_size(__isl_take isl_set
*set
, int dim
)
968 map
= isolate(set
, dim
);
969 map
= isl_map_range_product(map
, isl_map_copy(map
));
970 map
= isl_set_unwrap(isl_map_range(map
));
971 set
= isl_map_deltas(map
);
972 ls
= isl_local_space_from_space(isl_set_get_space(set
));
973 obj
= isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
974 v
= isl_set_max_val(set
, obj
);
981 /* Perform a compression on "node" where "hull" represents the constraints
982 * that were used to derive the compression, while "compress" and
983 * "decompress" map the original space to the compressed space and
986 * If "node" was not compressed already, then simply store
987 * the compression information.
988 * Otherwise the "original" space is actually the result
989 * of a previous compression, which is then combined
990 * with the present compression.
992 * The dimensionality of the compressed domain is also adjusted.
993 * Other information, such as the sizes and the maximal coefficient values,
994 * has not been computed yet and therefore does not need to be adjusted.
996 static isl_stat
compress_node(struct isl_sched_node
*node
,
997 __isl_take isl_set
*hull
, __isl_take isl_multi_aff
*compress
,
998 __isl_take isl_pw_multi_aff
*decompress
)
1000 node
->nvar
= isl_multi_aff_dim(compress
, isl_dim_out
);
1001 if (!node
->compressed
) {
1002 node
->compressed
= 1;
1004 node
->compress
= compress
;
1005 node
->decompress
= decompress
;
1007 hull
= isl_set_preimage_multi_aff(hull
,
1008 isl_multi_aff_copy(node
->compress
));
1009 node
->hull
= isl_set_intersect(node
->hull
, hull
);
1010 node
->compress
= isl_multi_aff_pullback_multi_aff(
1011 compress
, node
->compress
);
1012 node
->decompress
= isl_pw_multi_aff_pullback_pw_multi_aff(
1013 node
->decompress
, decompress
);
1016 if (!node
->hull
|| !node
->compress
|| !node
->decompress
)
1017 return isl_stat_error
;
1022 /* Given that dimension "pos" in "set" has a fixed value
1023 * in terms of the other dimensions, (further) compress "node"
1024 * by projecting out this dimension.
1025 * "set" may be the result of a previous compression.
1026 * "uncompressed" is the original domain (without compression).
1028 * The compression function simply projects out the dimension.
1029 * The decompression function adds back the dimension
1030 * in the right position as an expression of the other dimensions
1031 * derived from "set".
1032 * As in extract_node, the compressed space has an identifier
1033 * that references "node" such that each compressed space is unique and
1034 * such that the node can be recovered from the compressed space.
1036 * The constraint removed through the compression is added to the "hull"
1037 * such that only edges that relate to the original domains
1038 * are taken into account.
1039 * In particular, it is obtained by composing compression and decompression and
1040 * taking the relation among the variables in the range.
1042 static isl_stat
project_out_fixed(struct isl_sched_node
*node
,
1043 __isl_keep isl_set
*uncompressed
, __isl_take isl_set
*set
, int pos
)
1049 isl_multi_aff
*compress
;
1050 isl_pw_multi_aff
*decompress
, *pma
;
1051 isl_multi_pw_aff
*mpa
;
1054 map
= isolate(isl_set_copy(set
), pos
);
1055 pma
= isl_pw_multi_aff_from_map(map
);
1056 domain
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
1057 pma
= isl_pw_multi_aff_gist(pma
, domain
);
1058 space
= isl_pw_multi_aff_get_domain_space(pma
);
1059 mpa
= isl_multi_pw_aff_identity(isl_space_map_from_set(space
));
1060 mpa
= isl_multi_pw_aff_range_splice(mpa
, pos
,
1061 isl_multi_pw_aff_from_pw_multi_aff(pma
));
1062 decompress
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
1063 space
= isl_set_get_space(set
);
1064 compress
= isl_multi_aff_project_out_map(space
, isl_dim_set
, pos
, 1);
1065 id
= construct_compressed_id(uncompressed
, node
);
1066 compress
= isl_multi_aff_set_tuple_id(compress
, isl_dim_out
, id
);
1067 space
= isl_space_reverse(isl_multi_aff_get_space(compress
));
1068 decompress
= isl_pw_multi_aff_reset_space(decompress
, space
);
1069 pma
= isl_pw_multi_aff_pullback_multi_aff(
1070 isl_pw_multi_aff_copy(decompress
), isl_multi_aff_copy(compress
));
1071 hull
= isl_map_range(isl_map_from_pw_multi_aff(pma
));
1075 return compress_node(node
, hull
, compress
, decompress
);
1078 /* Compute the size of the compressed domain in each dimension and
1079 * store the results in node->sizes.
1080 * "uncompressed" is the original domain (without compression).
1082 * First compress the domain if needed and then compute the size
1083 * in each direction.
1084 * If the domain is not convex, then the sizes are computed
1085 * on a convex superset in order to avoid picking up sizes
1086 * that are valid for the individual disjuncts, but not for
1087 * the domain as a whole.
1089 * If any of the sizes turns out to be zero, then this means
1090 * that this dimension has a fixed value in terms of
1091 * the other dimensions. Perform an (extra) compression
1092 * to remove this dimensions.
1094 static isl_stat
compute_sizes(struct isl_sched_node
*node
,
1095 __isl_keep isl_set
*uncompressed
)
1100 isl_set
*set
= isl_set_copy(uncompressed
);
1102 if (node
->compressed
)
1103 set
= isl_set_preimage_pw_multi_aff(set
,
1104 isl_pw_multi_aff_copy(node
->decompress
));
1105 set
= isl_set_from_basic_set(isl_set_simple_hull(set
));
1106 mv
= isl_multi_val_zero(isl_set_get_space(set
));
1107 n
= isl_set_dim(set
, isl_dim_set
);
1109 mv
= isl_multi_val_free(mv
);
1110 for (j
= 0; j
< n
; ++j
) {
1114 v
= compute_size(isl_set_copy(set
), j
);
1115 is_zero
= isl_val_is_zero(v
);
1116 mv
= isl_multi_val_set_val(mv
, j
, v
);
1117 if (is_zero
>= 0 && is_zero
) {
1118 isl_multi_val_free(mv
);
1119 if (project_out_fixed(node
, uncompressed
, set
, j
) < 0)
1120 return isl_stat_error
;
1121 return compute_sizes(node
, uncompressed
);
1127 return isl_stat_error
;
1131 /* Compute the size of the instance set "set" of "node", after compression,
1132 * as well as bounds on the corresponding coefficients, if needed.
1134 * The sizes are needed when the schedule_treat_coalescing option is set.
1135 * The bounds are needed when the schedule_treat_coalescing option or
1136 * the schedule_max_coefficient option is set.
1138 * If the schedule_treat_coalescing option is not set, then at most
1139 * the bounds need to be set and this is done in set_max_coefficient.
1140 * Otherwise, compute the size of the compressed domain
1141 * in each direction and store the results in node->size.
1142 * Finally, set the bounds on the coefficients based on the sizes
1143 * and the schedule_max_coefficient option in compute_max_coefficient.
1145 static isl_stat
compute_sizes_and_max(isl_ctx
*ctx
, struct isl_sched_node
*node
,
1146 __isl_take isl_set
*set
)
1150 if (!isl_options_get_schedule_treat_coalescing(ctx
)) {
1152 return set_max_coefficient(ctx
, node
);
1155 r
= compute_sizes(node
, set
);
1158 return isl_stat_error
;
1159 return compute_max_coefficient(ctx
, node
);
1162 /* Add a new node to the graph representing the given instance set.
1163 * "nvar" is the (possibly compressed) number of variables and
1164 * may be smaller than then number of set variables in "set"
1165 * if "compressed" is set.
1166 * If "compressed" is set, then "hull" represents the constraints
1167 * that were used to derive the compression, while "compress" and
1168 * "decompress" map the original space to the compressed space and
1170 * If "compressed" is not set, then "hull", "compress" and "decompress"
1173 * Compute the size of the instance set and bounds on the coefficients,
1176 static isl_stat
add_node(struct isl_sched_graph
*graph
,
1177 __isl_take isl_set
*set
, int nvar
, int compressed
,
1178 __isl_take isl_set
*hull
, __isl_take isl_multi_aff
*compress
,
1179 __isl_take isl_pw_multi_aff
*decompress
)
1186 struct isl_sched_node
*node
;
1188 nparam
= isl_set_dim(set
, isl_dim_param
);
1192 ctx
= isl_set_get_ctx(set
);
1193 if (!ctx
->opt
->schedule_parametric
)
1195 sched
= isl_mat_alloc(ctx
, 0, 1 + nparam
+ nvar
);
1196 node
= &graph
->node
[graph
->n
];
1198 space
= isl_set_get_space(set
);
1199 node
->space
= space
;
1201 node
->nparam
= nparam
;
1202 node
->sched
= sched
;
1203 node
->sched_map
= NULL
;
1204 coincident
= isl_calloc_array(ctx
, int, graph
->max_row
);
1205 node
->coincident
= coincident
;
1206 node
->compressed
= compressed
;
1208 node
->compress
= compress
;
1209 node
->decompress
= decompress
;
1210 if (compute_sizes_and_max(ctx
, node
, set
) < 0)
1211 return isl_stat_error
;
1213 if (!space
|| !sched
|| (graph
->max_row
&& !coincident
))
1214 return isl_stat_error
;
1215 if (compressed
&& (!hull
|| !compress
|| !decompress
))
1216 return isl_stat_error
;
1222 isl_multi_aff_free(compress
);
1223 isl_pw_multi_aff_free(decompress
);
1224 return isl_stat_error
;
1227 /* Add a new node to the graph representing the given set.
1229 * If any of the set variables is defined by an equality, then
1230 * we perform variable compression such that we can perform
1231 * the scheduling on the compressed domain.
1232 * In this case, an identifier is used that references the new node
1233 * such that each compressed space is unique and
1234 * such that the node can be recovered from the compressed space.
1236 static isl_stat
extract_node(__isl_take isl_set
*set
, void *user
)
1239 isl_bool has_equality
;
1241 isl_basic_set
*hull
;
1244 isl_multi_aff
*compress
, *decompress_ma
;
1245 isl_pw_multi_aff
*decompress
;
1246 struct isl_sched_graph
*graph
= user
;
1248 hull
= isl_set_affine_hull(isl_set_copy(set
));
1249 hull
= isl_basic_set_remove_divs(hull
);
1250 nvar
= isl_set_dim(set
, isl_dim_set
);
1251 has_equality
= has_any_defining_equality(hull
);
1253 if (nvar
< 0 || has_equality
< 0)
1255 if (!has_equality
) {
1256 isl_basic_set_free(hull
);
1257 return add_node(graph
, set
, nvar
, 0, NULL
, NULL
, NULL
);
1260 id
= construct_compressed_id(set
, &graph
->node
[graph
->n
]);
1261 morph
= isl_basic_set_variable_compression_with_id(hull
, id
);
1263 nvar
= isl_morph_ran_dim(morph
, isl_dim_set
);
1265 set
= isl_set_free(set
);
1266 compress
= isl_morph_get_var_multi_aff(morph
);
1267 morph
= isl_morph_inverse(morph
);
1268 decompress_ma
= isl_morph_get_var_multi_aff(morph
);
1269 decompress
= isl_pw_multi_aff_from_multi_aff(decompress_ma
);
1270 isl_morph_free(morph
);
1272 hull_set
= isl_set_from_basic_set(hull
);
1273 return add_node(graph
, set
, nvar
, 1, hull_set
, compress
, decompress
);
1275 isl_basic_set_free(hull
);
1277 return isl_stat_error
;
1280 struct isl_extract_edge_data
{
1281 enum isl_edge_type type
;
1282 struct isl_sched_graph
*graph
;
1285 /* Merge edge2 into edge1, freeing the contents of edge2.
1286 * Return 0 on success and -1 on failure.
1288 * edge1 and edge2 are assumed to have the same value for the map field.
1290 static int merge_edge(struct isl_sched_edge
*edge1
,
1291 struct isl_sched_edge
*edge2
)
1293 edge1
->types
|= edge2
->types
;
1294 isl_map_free(edge2
->map
);
1296 if (is_condition(edge2
)) {
1297 if (!edge1
->tagged_condition
)
1298 edge1
->tagged_condition
= edge2
->tagged_condition
;
1300 edge1
->tagged_condition
=
1301 isl_union_map_union(edge1
->tagged_condition
,
1302 edge2
->tagged_condition
);
1305 if (is_conditional_validity(edge2
)) {
1306 if (!edge1
->tagged_validity
)
1307 edge1
->tagged_validity
= edge2
->tagged_validity
;
1309 edge1
->tagged_validity
=
1310 isl_union_map_union(edge1
->tagged_validity
,
1311 edge2
->tagged_validity
);
1314 if (is_condition(edge2
) && !edge1
->tagged_condition
)
1316 if (is_conditional_validity(edge2
) && !edge1
->tagged_validity
)
1322 /* Insert dummy tags in domain and range of "map".
1324 * In particular, if "map" is of the form
1330 * [A -> dummy_tag] -> [B -> dummy_tag]
1332 * where the dummy_tags are identical and equal to any dummy tags
1333 * introduced by any other call to this function.
1335 static __isl_give isl_map
*insert_dummy_tags(__isl_take isl_map
*map
)
1341 isl_set
*domain
, *range
;
1343 ctx
= isl_map_get_ctx(map
);
1345 id
= isl_id_alloc(ctx
, NULL
, &dummy
);
1346 space
= isl_space_params(isl_map_get_space(map
));
1347 space
= isl_space_set_from_params(space
);
1348 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
1349 space
= isl_space_map_from_set(space
);
1351 domain
= isl_map_wrap(map
);
1352 range
= isl_map_wrap(isl_map_universe(space
));
1353 map
= isl_map_from_domain_and_range(domain
, range
);
1354 map
= isl_map_zip(map
);
1359 /* Given that at least one of "src" or "dst" is compressed, return
1360 * a map between the spaces of these nodes restricted to the affine
1361 * hull that was used in the compression.
1363 static __isl_give isl_map
*extract_hull(struct isl_sched_node
*src
,
1364 struct isl_sched_node
*dst
)
1368 if (src
->compressed
)
1369 dom
= isl_set_copy(src
->hull
);
1371 dom
= isl_set_universe(isl_space_copy(src
->space
));
1372 if (dst
->compressed
)
1373 ran
= isl_set_copy(dst
->hull
);
1375 ran
= isl_set_universe(isl_space_copy(dst
->space
));
1377 return isl_map_from_domain_and_range(dom
, ran
);
1380 /* Intersect the domains of the nested relations in domain and range
1381 * of "tagged" with "map".
1383 static __isl_give isl_map
*map_intersect_domains(__isl_take isl_map
*tagged
,
1384 __isl_keep isl_map
*map
)
1388 tagged
= isl_map_zip(tagged
);
1389 set
= isl_map_wrap(isl_map_copy(map
));
1390 tagged
= isl_map_intersect_domain(tagged
, set
);
1391 tagged
= isl_map_zip(tagged
);
1395 /* Return a pointer to the node that lives in the domain space of "map",
1396 * an invalid node if there is no such node, or NULL in case of error.
1398 static struct isl_sched_node
*find_domain_node(isl_ctx
*ctx
,
1399 struct isl_sched_graph
*graph
, __isl_keep isl_map
*map
)
1401 struct isl_sched_node
*node
;
1404 space
= isl_space_domain(isl_map_get_space(map
));
1405 node
= graph_find_node(ctx
, graph
, space
);
1406 isl_space_free(space
);
1411 /* Return a pointer to the node that lives in the range space of "map",
1412 * an invalid node if there is no such node, or NULL in case of error.
1414 static struct isl_sched_node
*find_range_node(isl_ctx
*ctx
,
1415 struct isl_sched_graph
*graph
, __isl_keep isl_map
*map
)
1417 struct isl_sched_node
*node
;
1420 space
= isl_space_range(isl_map_get_space(map
));
1421 node
= graph_find_node(ctx
, graph
, space
);
1422 isl_space_free(space
);
1427 /* Refrain from adding a new edge based on "map".
1428 * Instead, just free the map.
1429 * "tagged" is either a copy of "map" with additional tags or NULL.
1431 static isl_stat
skip_edge(__isl_take isl_map
*map
, __isl_take isl_map
*tagged
)
1434 isl_map_free(tagged
);
1439 /* Add a new edge to the graph based on the given map
1440 * and add it to data->graph->edge_table[data->type].
1441 * If a dependence relation of a given type happens to be identical
1442 * to one of the dependence relations of a type that was added before,
1443 * then we don't create a new edge, but instead mark the original edge
1444 * as also representing a dependence of the current type.
1446 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1447 * may be specified as "tagged" dependence relations. That is, "map"
1448 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1449 * the dependence on iterations and a and b are tags.
1450 * edge->map is set to the relation containing the elements i -> j,
1451 * while edge->tagged_condition and edge->tagged_validity contain
1452 * the union of all the "map" relations
1453 * for which extract_edge is called that result in the same edge->map.
1455 * If the source or the destination node is compressed, then
1456 * intersect both "map" and "tagged" with the constraints that
1457 * were used to construct the compression.
1458 * This ensures that there are no schedule constraints defined
1459 * outside of these domains, while the scheduler no longer has
1460 * any control over those outside parts.
1462 static isl_stat
extract_edge(__isl_take isl_map
*map
, void *user
)
1465 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1466 struct isl_extract_edge_data
*data
= user
;
1467 struct isl_sched_graph
*graph
= data
->graph
;
1468 struct isl_sched_node
*src
, *dst
;
1469 struct isl_sched_edge
*edge
;
1470 isl_map
*tagged
= NULL
;
1472 if (data
->type
== isl_edge_condition
||
1473 data
->type
== isl_edge_conditional_validity
) {
1474 if (isl_map_can_zip(map
)) {
1475 tagged
= isl_map_copy(map
);
1476 map
= isl_set_unwrap(isl_map_domain(isl_map_zip(map
)));
1478 tagged
= insert_dummy_tags(isl_map_copy(map
));
1482 src
= find_domain_node(ctx
, graph
, map
);
1483 dst
= find_range_node(ctx
, graph
, map
);
1487 if (!is_node(graph
, src
) || !is_node(graph
, dst
))
1488 return skip_edge(map
, tagged
);
1490 if (src
->compressed
|| dst
->compressed
) {
1492 hull
= extract_hull(src
, dst
);
1494 tagged
= map_intersect_domains(tagged
, hull
);
1495 map
= isl_map_intersect(map
, hull
);
1498 empty
= isl_map_plain_is_empty(map
);
1502 return skip_edge(map
, tagged
);
1504 graph
->edge
[graph
->n_edge
].src
= src
;
1505 graph
->edge
[graph
->n_edge
].dst
= dst
;
1506 graph
->edge
[graph
->n_edge
].map
= map
;
1507 graph
->edge
[graph
->n_edge
].types
= 0;
1508 graph
->edge
[graph
->n_edge
].tagged_condition
= NULL
;
1509 graph
->edge
[graph
->n_edge
].tagged_validity
= NULL
;
1510 set_type(&graph
->edge
[graph
->n_edge
], data
->type
);
1511 if (data
->type
== isl_edge_condition
)
1512 graph
->edge
[graph
->n_edge
].tagged_condition
=
1513 isl_union_map_from_map(tagged
);
1514 if (data
->type
== isl_edge_conditional_validity
)
1515 graph
->edge
[graph
->n_edge
].tagged_validity
=
1516 isl_union_map_from_map(tagged
);
1518 edge
= graph_find_matching_edge(graph
, &graph
->edge
[graph
->n_edge
]);
1521 return isl_stat_error
;
1523 if (edge
== &graph
->edge
[graph
->n_edge
])
1524 return graph_edge_table_add(ctx
, graph
, data
->type
,
1525 &graph
->edge
[graph
->n_edge
++]);
1527 if (merge_edge(edge
, &graph
->edge
[graph
->n_edge
]) < 0)
1528 return isl_stat_error
;
1530 return graph_edge_table_add(ctx
, graph
, data
->type
, edge
);
1533 isl_map_free(tagged
);
1534 return isl_stat_error
;
1537 /* Initialize the schedule graph "graph" from the schedule constraints "sc".
1539 * The context is included in the domain before the nodes of
1540 * the graphs are extracted in order to be able to exploit
1541 * any possible additional equalities.
1542 * Note that this intersection is only performed locally here.
1544 static isl_stat
graph_init(struct isl_sched_graph
*graph
,
1545 __isl_keep isl_schedule_constraints
*sc
)
1548 isl_union_set
*domain
;
1550 struct isl_extract_edge_data data
;
1551 enum isl_edge_type i
;
1556 return isl_stat_error
;
1558 ctx
= isl_schedule_constraints_get_ctx(sc
);
1560 domain
= isl_schedule_constraints_get_domain(sc
);
1561 n
= isl_union_set_n_set(domain
);
1563 isl_union_set_free(domain
);
1565 return isl_stat_error
;
1567 n
= isl_schedule_constraints_n_map(sc
);
1568 if (n
< 0 || graph_alloc(ctx
, graph
, graph
->n
, n
) < 0)
1569 return isl_stat_error
;
1571 if (compute_max_row(graph
, sc
) < 0)
1572 return isl_stat_error
;
1573 graph
->root
= graph
;
1575 domain
= isl_schedule_constraints_get_domain(sc
);
1576 domain
= isl_union_set_intersect_params(domain
,
1577 isl_schedule_constraints_get_context(sc
));
1578 r
= isl_union_set_foreach_set(domain
, &extract_node
, graph
);
1579 isl_union_set_free(domain
);
1581 return isl_stat_error
;
1582 if (graph_init_table(ctx
, graph
) < 0)
1583 return isl_stat_error
;
1584 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
1587 c
= isl_schedule_constraints_get(sc
, i
);
1588 n
= isl_union_map_n_map(c
);
1589 graph
->max_edge
[i
] = n
;
1590 isl_union_map_free(c
);
1592 return isl_stat_error
;
1594 if (graph_init_edge_tables(ctx
, graph
) < 0)
1595 return isl_stat_error
;
1598 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
1602 c
= isl_schedule_constraints_get(sc
, i
);
1603 r
= isl_union_map_foreach_map(c
, &extract_edge
, &data
);
1604 isl_union_map_free(c
);
1606 return isl_stat_error
;
1612 /* Check whether there is any dependence from node[j] to node[i]
1613 * or from node[i] to node[j].
1615 static isl_bool
node_follows_weak(int i
, int j
, void *user
)
1618 struct isl_sched_graph
*graph
= user
;
1620 f
= graph_has_any_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1623 return graph_has_any_edge(graph
, &graph
->node
[i
], &graph
->node
[j
]);
1626 /* Check whether there is a (conditional) validity dependence from node[j]
1627 * to node[i], forcing node[i] to follow node[j].
1629 static isl_bool
node_follows_strong(int i
, int j
, void *user
)
1631 struct isl_sched_graph
*graph
= user
;
1633 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1636 /* Use Tarjan's algorithm for computing the strongly connected components
1637 * in the dependence graph only considering those edges defined by "follows".
1639 static isl_stat
detect_ccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
1640 isl_bool (*follows
)(int i
, int j
, void *user
))
1643 struct isl_tarjan_graph
*g
= NULL
;
1645 g
= isl_tarjan_graph_init(ctx
, graph
->n
, follows
, graph
);
1647 return isl_stat_error
;
1653 while (g
->order
[i
] != -1) {
1654 graph
->node
[g
->order
[i
]].scc
= graph
->scc
;
1662 isl_tarjan_graph_free(g
);
1667 /* Apply Tarjan's algorithm to detect the strongly connected components
1668 * in the dependence graph.
1669 * Only consider the (conditional) validity dependences and clear "weak".
1671 static isl_stat
detect_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1674 return detect_ccs(ctx
, graph
, &node_follows_strong
);
1677 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1678 * in the dependence graph.
1679 * Consider all dependences and set "weak".
1681 static isl_stat
detect_wccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1684 return detect_ccs(ctx
, graph
, &node_follows_weak
);
1687 static int cmp_scc(const void *a
, const void *b
, void *data
)
1689 struct isl_sched_graph
*graph
= data
;
1693 return graph
->node
[*i1
].scc
- graph
->node
[*i2
].scc
;
1696 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1698 static int sort_sccs(struct isl_sched_graph
*graph
)
1700 return isl_sort(graph
->sorted
, graph
->n
, sizeof(int), &cmp_scc
, graph
);
1703 /* Return a non-parametric set in the compressed space of "node" that is
1704 * bounded by the size in each direction
1706 * { [x] : -S_i <= x_i <= S_i }
1708 * If S_i is infinity in direction i, then there are no constraints
1709 * in that direction.
1711 * Cache the result in node->bounds.
1713 static __isl_give isl_basic_set
*get_size_bounds(struct isl_sched_node
*node
)
1716 isl_basic_set
*bounds
;
1720 return isl_basic_set_copy(node
->bounds
);
1722 if (node
->compressed
)
1723 space
= isl_pw_multi_aff_get_domain_space(node
->decompress
);
1725 space
= isl_space_copy(node
->space
);
1726 space
= isl_space_drop_all_params(space
);
1727 bounds
= isl_basic_set_universe(space
);
1729 for (i
= 0; i
< node
->nvar
; ++i
) {
1732 size
= isl_multi_val_get_val(node
->sizes
, i
);
1734 return isl_basic_set_free(bounds
);
1735 if (!isl_val_is_int(size
)) {
1739 bounds
= isl_basic_set_upper_bound_val(bounds
, isl_dim_set
, i
,
1740 isl_val_copy(size
));
1741 bounds
= isl_basic_set_lower_bound_val(bounds
, isl_dim_set
, i
,
1745 node
->bounds
= isl_basic_set_copy(bounds
);
1749 /* Compress the dependence relation "map", if needed, i.e.,
1750 * when the source node "src" and/or the destination node "dst"
1751 * has been compressed.
1753 static __isl_give isl_map
*compress(__isl_take isl_map
*map
,
1754 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
1756 if (src
->compressed
)
1757 map
= isl_map_preimage_domain_pw_multi_aff(map
,
1758 isl_pw_multi_aff_copy(src
->decompress
));
1759 if (dst
->compressed
)
1760 map
= isl_map_preimage_range_pw_multi_aff(map
,
1761 isl_pw_multi_aff_copy(dst
->decompress
));
1765 /* Drop some constraints from "delta" that could be exploited
1766 * to construct loop coalescing schedules.
1767 * In particular, drop those constraint that bound the difference
1768 * to the size of the domain.
1769 * First project out the parameters to improve the effectiveness.
1771 static __isl_give isl_set
*drop_coalescing_constraints(
1772 __isl_take isl_set
*delta
, struct isl_sched_node
*node
)
1775 isl_basic_set
*bounds
;
1777 nparam
= isl_set_dim(delta
, isl_dim_param
);
1779 return isl_set_free(delta
);
1781 bounds
= get_size_bounds(node
);
1783 delta
= isl_set_project_out(delta
, isl_dim_param
, 0, nparam
);
1784 delta
= isl_set_remove_divs(delta
);
1785 delta
= isl_set_plain_gist_basic_set(delta
, bounds
);
1789 /* Given a dependence relation R from "node" to itself,
1790 * construct the set of coefficients of valid constraints for elements
1791 * in that dependence relation.
1792 * In particular, the result contains tuples of coefficients
1793 * c_0, c_n, c_x such that
1795 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1799 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1801 * We choose here to compute the dual of delta R.
1802 * Alternatively, we could have computed the dual of R, resulting
1803 * in a set of tuples c_0, c_n, c_x, c_y, and then
1804 * plugged in (c_0, c_n, c_x, -c_x).
1806 * If "need_param" is set, then the resulting coefficients effectively
1807 * include coefficients for the parameters c_n. Otherwise, they may
1808 * have been projected out already.
1809 * Since the constraints may be different for these two cases,
1810 * they are stored in separate caches.
1811 * In particular, if no parameter coefficients are required and
1812 * the schedule_treat_coalescing option is set, then the parameters
1813 * are projected out and some constraints that could be exploited
1814 * to construct coalescing schedules are removed before the dual
1817 * If "node" has been compressed, then the dependence relation
1818 * is also compressed before the set of coefficients is computed.
1820 static __isl_give isl_basic_set
*intra_coefficients(
1821 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1822 __isl_take isl_map
*map
, int need_param
)
1827 isl_basic_set
*coef
;
1828 isl_maybe_isl_basic_set m
;
1829 isl_map_to_basic_set
**hmap
= &graph
->intra_hmap
;
1835 ctx
= isl_map_get_ctx(map
);
1836 treat
= !need_param
&& isl_options_get_schedule_treat_coalescing(ctx
);
1838 hmap
= &graph
->intra_hmap_param
;
1839 m
= isl_map_to_basic_set_try_get(*hmap
, map
);
1840 if (m
.valid
< 0 || m
.valid
) {
1845 key
= isl_map_copy(map
);
1846 map
= compress(map
, node
, node
);
1847 delta
= isl_map_deltas(map
);
1849 delta
= drop_coalescing_constraints(delta
, node
);
1850 delta
= isl_set_remove_divs(delta
);
1851 coef
= isl_set_coefficients(delta
);
1852 *hmap
= isl_map_to_basic_set_set(*hmap
, key
, isl_basic_set_copy(coef
));
1857 /* Given a dependence relation R, construct the set of coefficients
1858 * of valid constraints for elements in that dependence relation.
1859 * In particular, the result contains tuples of coefficients
1860 * c_0, c_n, c_x, c_y such that
1862 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1864 * If the source or destination nodes of "edge" have been compressed,
1865 * then the dependence relation is also compressed before
1866 * the set of coefficients is computed.
1868 static __isl_give isl_basic_set
*inter_coefficients(
1869 struct isl_sched_graph
*graph
, struct isl_sched_edge
*edge
,
1870 __isl_take isl_map
*map
)
1874 isl_basic_set
*coef
;
1875 isl_maybe_isl_basic_set m
;
1877 m
= isl_map_to_basic_set_try_get(graph
->inter_hmap
, map
);
1878 if (m
.valid
< 0 || m
.valid
) {
1883 key
= isl_map_copy(map
);
1884 map
= compress(map
, edge
->src
, edge
->dst
);
1885 set
= isl_map_wrap(isl_map_remove_divs(map
));
1886 coef
= isl_set_coefficients(set
);
1887 graph
->inter_hmap
= isl_map_to_basic_set_set(graph
->inter_hmap
, key
,
1888 isl_basic_set_copy(coef
));
1893 /* Return the position of the coefficients of the variables in
1894 * the coefficients constraints "coef".
1896 * The space of "coef" is of the form
1898 * { coefficients[[cst, params] -> S] }
1900 * Return the position of S.
1902 static isl_size
coef_var_offset(__isl_keep isl_basic_set
*coef
)
1907 space
= isl_space_unwrap(isl_basic_set_get_space(coef
));
1908 offset
= isl_space_dim(space
, isl_dim_in
);
1909 isl_space_free(space
);
1914 /* Return the offset of the coefficient of the constant term of "node"
1917 * Within each node, the coefficients have the following order:
1918 * - positive and negative parts of c_i_x
1919 * - c_i_n (if parametric)
1922 static int node_cst_coef_offset(struct isl_sched_node
*node
)
1924 return node
->start
+ 2 * node
->nvar
+ node
->nparam
;
1927 /* Return the offset of the coefficients of the parameters of "node"
1930 * Within each node, the coefficients have the following order:
1931 * - positive and negative parts of c_i_x
1932 * - c_i_n (if parametric)
1935 static int node_par_coef_offset(struct isl_sched_node
*node
)
1937 return node
->start
+ 2 * node
->nvar
;
1940 /* Return the offset of the coefficients of the variables of "node"
1943 * Within each node, the coefficients have the following order:
1944 * - positive and negative parts of c_i_x
1945 * - c_i_n (if parametric)
1948 static int node_var_coef_offset(struct isl_sched_node
*node
)
1953 /* Return the position of the pair of variables encoding
1954 * coefficient "i" of "node".
1956 * The order of these variable pairs is the opposite of
1957 * that of the coefficients, with 2 variables per coefficient.
1959 static int node_var_coef_pos(struct isl_sched_node
*node
, int i
)
1961 return node_var_coef_offset(node
) + 2 * (node
->nvar
- 1 - i
);
1964 /* Construct an isl_dim_map for mapping constraints on coefficients
1965 * for "node" to the corresponding positions in graph->lp.
1966 * "offset" is the offset of the coefficients for the variables
1967 * in the input constraints.
1968 * "s" is the sign of the mapping.
1970 * The input constraints are given in terms of the coefficients
1971 * (c_0, c_x) or (c_0, c_n, c_x).
1972 * The mapping produced by this function essentially plugs in
1973 * (0, c_i_x^+ - c_i_x^-) if s = 1 and
1974 * (0, -c_i_x^+ + c_i_x^-) if s = -1 or
1975 * (0, 0, c_i_x^+ - c_i_x^-) if s = 1 and
1976 * (0, 0, -c_i_x^+ + c_i_x^-) if s = -1.
1977 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1978 * Furthermore, the order of these pairs is the opposite of that
1979 * of the corresponding coefficients.
1981 * The caller can extend the mapping to also map the other coefficients
1982 * (and therefore not plug in 0).
1984 static __isl_give isl_dim_map
*intra_dim_map(isl_ctx
*ctx
,
1985 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1990 isl_dim_map
*dim_map
;
1992 total
= isl_basic_set_dim(graph
->lp
, isl_dim_all
);
1993 if (!node
|| total
< 0)
1996 pos
= node_var_coef_pos(node
, 0);
1997 dim_map
= isl_dim_map_alloc(ctx
, total
);
1998 isl_dim_map_range(dim_map
, pos
, -2, offset
, 1, node
->nvar
, -s
);
1999 isl_dim_map_range(dim_map
, pos
+ 1, -2, offset
, 1, node
->nvar
, s
);
2004 /* Construct an isl_dim_map for mapping constraints on coefficients
2005 * for "src" (node i) and "dst" (node j) to the corresponding positions
2007 * "offset" is the offset of the coefficients for the variables of "src"
2008 * in the input constraints.
2009 * "s" is the sign of the mapping.
2011 * The input constraints are given in terms of the coefficients
2012 * (c_0, c_n, c_x, c_y).
2013 * The mapping produced by this function essentially plugs in
2014 * (c_j_0 - c_i_0, c_j_n - c_i_n,
2015 * -(c_i_x^+ - c_i_x^-), c_j_x^+ - c_j_x^-) if s = 1 and
2016 * (-c_j_0 + c_i_0, -c_j_n + c_i_n,
2017 * c_i_x^+ - c_i_x^-, -(c_j_x^+ - c_j_x^-)) if s = -1.
2018 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
2019 * Furthermore, the order of these pairs is the opposite of that
2020 * of the corresponding coefficients.
2022 * The caller can further extend the mapping.
2024 static __isl_give isl_dim_map
*inter_dim_map(isl_ctx
*ctx
,
2025 struct isl_sched_graph
*graph
, struct isl_sched_node
*src
,
2026 struct isl_sched_node
*dst
, int offset
, int s
)
2030 isl_dim_map
*dim_map
;
2032 total
= isl_basic_set_dim(graph
->lp
, isl_dim_all
);
2033 if (!src
|| !dst
|| total
< 0)
2036 dim_map
= isl_dim_map_alloc(ctx
, total
);
2038 pos
= node_cst_coef_offset(dst
);
2039 isl_dim_map_range(dim_map
, pos
, 0, 0, 0, 1, s
);
2040 pos
= node_par_coef_offset(dst
);
2041 isl_dim_map_range(dim_map
, pos
, 1, 1, 1, dst
->nparam
, s
);
2042 pos
= node_var_coef_pos(dst
, 0);
2043 isl_dim_map_range(dim_map
, pos
, -2, offset
+ src
->nvar
, 1,
2045 isl_dim_map_range(dim_map
, pos
+ 1, -2, offset
+ src
->nvar
, 1,
2048 pos
= node_cst_coef_offset(src
);
2049 isl_dim_map_range(dim_map
, pos
, 0, 0, 0, 1, -s
);
2050 pos
= node_par_coef_offset(src
);
2051 isl_dim_map_range(dim_map
, pos
, 1, 1, 1, src
->nparam
, -s
);
2052 pos
= node_var_coef_pos(src
, 0);
2053 isl_dim_map_range(dim_map
, pos
, -2, offset
, 1, src
->nvar
, s
);
2054 isl_dim_map_range(dim_map
, pos
+ 1, -2, offset
, 1, src
->nvar
, -s
);
2059 /* Add the constraints from "src" to "dst" using "dim_map",
2060 * after making sure there is enough room in "dst" for the extra constraints.
2062 static __isl_give isl_basic_set
*add_constraints_dim_map(
2063 __isl_take isl_basic_set
*dst
, __isl_take isl_basic_set
*src
,
2064 __isl_take isl_dim_map
*dim_map
)
2068 n_eq
= isl_basic_set_n_equality(src
);
2069 n_ineq
= isl_basic_set_n_inequality(src
);
2070 dst
= isl_basic_set_extend_constraints(dst
, n_eq
, n_ineq
);
2071 dst
= isl_basic_set_add_constraints_dim_map(dst
, src
, dim_map
);
2075 /* Add constraints to graph->lp that force validity for the given
2076 * dependence from a node i to itself.
2077 * That is, add constraints that enforce
2079 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
2080 * = c_i_x (y - x) >= 0
2082 * for each (x,y) in R.
2083 * We obtain general constraints on coefficients (c_0, c_x)
2084 * of valid constraints for (y - x) and then plug in (0, c_i_x^+ - c_i_x^-),
2085 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
2086 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
2087 * Note that the result of intra_coefficients may also contain
2088 * parameter coefficients c_n, in which case 0 is plugged in for them as well.
2090 static isl_stat
add_intra_validity_constraints(struct isl_sched_graph
*graph
,
2091 struct isl_sched_edge
*edge
)
2094 isl_map
*map
= isl_map_copy(edge
->map
);
2095 isl_ctx
*ctx
= isl_map_get_ctx(map
);
2096 isl_dim_map
*dim_map
;
2097 isl_basic_set
*coef
;
2098 struct isl_sched_node
*node
= edge
->src
;
2100 coef
= intra_coefficients(graph
, node
, map
, 0);
2102 offset
= coef_var_offset(coef
);
2104 coef
= isl_basic_set_free(coef
);
2106 return isl_stat_error
;
2108 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, 1);
2109 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
2114 /* Add constraints to graph->lp that force validity for the given
2115 * dependence from node i to node j.
2116 * That is, add constraints that enforce
2118 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
2120 * for each (x,y) in R.
2121 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
2122 * of valid constraints for R and then plug in
2123 * (c_j_0 - c_i_0, c_j_n - c_i_n, -(c_i_x^+ - c_i_x^-), c_j_x^+ - c_j_x^-),
2124 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
2125 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
2127 static isl_stat
add_inter_validity_constraints(struct isl_sched_graph
*graph
,
2128 struct isl_sched_edge
*edge
)
2133 isl_dim_map
*dim_map
;
2134 isl_basic_set
*coef
;
2135 struct isl_sched_node
*src
= edge
->src
;
2136 struct isl_sched_node
*dst
= edge
->dst
;
2139 return isl_stat_error
;
2141 map
= isl_map_copy(edge
->map
);
2142 ctx
= isl_map_get_ctx(map
);
2143 coef
= inter_coefficients(graph
, edge
, map
);
2145 offset
= coef_var_offset(coef
);
2147 coef
= isl_basic_set_free(coef
);
2149 return isl_stat_error
;
2151 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, 1);
2153 edge
->start
= graph
->lp
->n_ineq
;
2154 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
2156 return isl_stat_error
;
2157 edge
->end
= graph
->lp
->n_ineq
;
2162 /* Add constraints to graph->lp that bound the dependence distance for the given
2163 * dependence from a node i to itself.
2164 * If s = 1, we add the constraint
2166 * c_i_x (y - x) <= m_0 + m_n n
2170 * -c_i_x (y - x) + m_0 + m_n n >= 0
2172 * for each (x,y) in R.
2173 * If s = -1, we add the constraint
2175 * -c_i_x (y - x) <= m_0 + m_n n
2179 * c_i_x (y - x) + m_0 + m_n n >= 0
2181 * for each (x,y) in R.
2182 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2183 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
2184 * with each coefficient (except m_0) represented as a pair of non-negative
2188 * If "local" is set, then we add constraints
2190 * c_i_x (y - x) <= 0
2194 * -c_i_x (y - x) <= 0
2196 * instead, forcing the dependence distance to be (less than or) equal to 0.
2197 * That is, we plug in (0, 0, -s * c_i_x),
2198 * intra_coefficients is not required to have c_n in its result when
2199 * "local" is set. If they are missing, then (0, -s * c_i_x) is plugged in.
2200 * Note that dependences marked local are treated as validity constraints
2201 * by add_all_validity_constraints and therefore also have
2202 * their distances bounded by 0 from below.
2204 static isl_stat
add_intra_proximity_constraints(struct isl_sched_graph
*graph
,
2205 struct isl_sched_edge
*edge
, int s
, int local
)
2209 isl_map
*map
= isl_map_copy(edge
->map
);
2210 isl_ctx
*ctx
= isl_map_get_ctx(map
);
2211 isl_dim_map
*dim_map
;
2212 isl_basic_set
*coef
;
2213 struct isl_sched_node
*node
= edge
->src
;
2215 coef
= intra_coefficients(graph
, node
, map
, !local
);
2216 nparam
= isl_space_dim(node
->space
, isl_dim_param
);
2218 offset
= coef_var_offset(coef
);
2219 if (nparam
< 0 || offset
< 0)
2220 coef
= isl_basic_set_free(coef
);
2222 return isl_stat_error
;
2224 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, -s
);
2227 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
2228 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
2229 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
2231 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
2236 /* Add constraints to graph->lp that bound the dependence distance for the given
2237 * dependence from node i to node j.
2238 * If s = 1, we add the constraint
2240 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
2245 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
2248 * for each (x,y) in R.
2249 * If s = -1, we add the constraint
2251 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
2256 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
2259 * for each (x,y) in R.
2260 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
2261 * of valid constraints for R and then plug in
2262 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
2263 * s*c_i_x, -s*c_j_x)
2264 * with each coefficient (except m_0, c_*_0 and c_*_n)
2265 * represented as a pair of non-negative coefficients.
2268 * If "local" is set (and s = 1), then we add constraints
2270 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
2274 * -((c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x)) >= 0
2276 * instead, forcing the dependence distance to be (less than or) equal to 0.
2277 * That is, we plug in
2278 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, s*c_i_x, -s*c_j_x).
2279 * Note that dependences marked local are treated as validity constraints
2280 * by add_all_validity_constraints and therefore also have
2281 * their distances bounded by 0 from below.
2283 static isl_stat
add_inter_proximity_constraints(struct isl_sched_graph
*graph
,
2284 struct isl_sched_edge
*edge
, int s
, int local
)
2288 isl_map
*map
= isl_map_copy(edge
->map
);
2289 isl_ctx
*ctx
= isl_map_get_ctx(map
);
2290 isl_dim_map
*dim_map
;
2291 isl_basic_set
*coef
;
2292 struct isl_sched_node
*src
= edge
->src
;
2293 struct isl_sched_node
*dst
= edge
->dst
;
2295 coef
= inter_coefficients(graph
, edge
, map
);
2296 nparam
= isl_space_dim(src
->space
, isl_dim_param
);
2298 offset
= coef_var_offset(coef
);
2299 if (nparam
< 0 || offset
< 0)
2300 coef
= isl_basic_set_free(coef
);
2302 return isl_stat_error
;
2304 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, -s
);
2307 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
2308 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
2309 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
2312 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
2317 /* Should the distance over "edge" be forced to zero?
2318 * That is, is it marked as a local edge?
2319 * If "use_coincidence" is set, then coincidence edges are treated
2322 static int force_zero(struct isl_sched_edge
*edge
, int use_coincidence
)
2324 return is_local(edge
) || (use_coincidence
&& is_coincidence(edge
));
2327 /* Add all validity constraints to graph->lp.
2329 * An edge that is forced to be local needs to have its dependence
2330 * distances equal to zero. We take care of bounding them by 0 from below
2331 * here. add_all_proximity_constraints takes care of bounding them by 0
2334 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2335 * Otherwise, we ignore them.
2337 static int add_all_validity_constraints(struct isl_sched_graph
*graph
,
2338 int use_coincidence
)
2342 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2343 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2346 zero
= force_zero(edge
, use_coincidence
);
2347 if (!is_validity(edge
) && !zero
)
2349 if (edge
->src
!= edge
->dst
)
2351 if (add_intra_validity_constraints(graph
, edge
) < 0)
2355 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2356 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2359 zero
= force_zero(edge
, use_coincidence
);
2360 if (!is_validity(edge
) && !zero
)
2362 if (edge
->src
== edge
->dst
)
2364 if (add_inter_validity_constraints(graph
, edge
) < 0)
2371 /* Add constraints to graph->lp that bound the dependence distance
2372 * for all dependence relations.
2373 * If a given proximity dependence is identical to a validity
2374 * dependence, then the dependence distance is already bounded
2375 * from below (by zero), so we only need to bound the distance
2376 * from above. (This includes the case of "local" dependences
2377 * which are treated as validity dependence by add_all_validity_constraints.)
2378 * Otherwise, we need to bound the distance both from above and from below.
2380 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2381 * Otherwise, we ignore them.
2383 static int add_all_proximity_constraints(struct isl_sched_graph
*graph
,
2384 int use_coincidence
)
2388 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2389 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2392 zero
= force_zero(edge
, use_coincidence
);
2393 if (!is_proximity(edge
) && !zero
)
2395 if (edge
->src
== edge
->dst
&&
2396 add_intra_proximity_constraints(graph
, edge
, 1, zero
) < 0)
2398 if (edge
->src
!= edge
->dst
&&
2399 add_inter_proximity_constraints(graph
, edge
, 1, zero
) < 0)
2401 if (is_validity(edge
) || zero
)
2403 if (edge
->src
== edge
->dst
&&
2404 add_intra_proximity_constraints(graph
, edge
, -1, 0) < 0)
2406 if (edge
->src
!= edge
->dst
&&
2407 add_inter_proximity_constraints(graph
, edge
, -1, 0) < 0)
2414 /* Normalize the rows of "indep" such that all rows are lexicographically
2415 * positive and such that each row contains as many final zeros as possible,
2416 * given the choice for the previous rows.
2417 * Do this by performing elementary row operations.
2419 static __isl_give isl_mat
*normalize_independent(__isl_take isl_mat
*indep
)
2421 indep
= isl_mat_reverse_gauss(indep
);
2422 indep
= isl_mat_lexnonneg_rows(indep
);
2426 /* Extract the linear part of the current schedule for node "node".
2428 static __isl_give isl_mat
*extract_linear_schedule(struct isl_sched_node
*node
)
2430 isl_size n_row
= isl_mat_rows(node
->sched
);
2434 return isl_mat_sub_alloc(node
->sched
, 0, n_row
,
2435 1 + node
->nparam
, node
->nvar
);
2438 /* Compute a basis for the rows in the linear part of the schedule
2439 * and extend this basis to a full basis. The remaining rows
2440 * can then be used to force linear independence from the rows
2443 * In particular, given the schedule rows S, we compute
2448 * with H the Hermite normal form of S. That is, all but the
2449 * first rank columns of H are zero and so each row in S is
2450 * a linear combination of the first rank rows of Q.
2451 * The matrix Q can be used as a variable transformation
2452 * that isolates the directions of S in the first rank rows.
2453 * Transposing S U = H yields
2457 * with all but the first rank rows of H^T zero.
2458 * The last rows of U^T are therefore linear combinations
2459 * of schedule coefficients that are all zero on schedule
2460 * coefficients that are linearly dependent on the rows of S.
2461 * At least one of these combinations is non-zero on
2462 * linearly independent schedule coefficients.
2463 * The rows are normalized to involve as few of the last
2464 * coefficients as possible and to have a positive initial value.
2466 static int node_update_vmap(struct isl_sched_node
*node
)
2470 H
= extract_linear_schedule(node
);
2472 H
= isl_mat_left_hermite(H
, 0, &U
, &Q
);
2473 isl_mat_free(node
->indep
);
2474 isl_mat_free(node
->vmap
);
2476 node
->indep
= isl_mat_transpose(U
);
2477 node
->rank
= isl_mat_initial_non_zero_cols(H
);
2478 node
->indep
= isl_mat_drop_rows(node
->indep
, 0, node
->rank
);
2479 node
->indep
= normalize_independent(node
->indep
);
2482 if (!node
->indep
|| !node
->vmap
|| node
->rank
< 0)
2487 /* Is "edge" marked as a validity or a conditional validity edge?
2489 static int is_any_validity(struct isl_sched_edge
*edge
)
2491 return is_validity(edge
) || is_conditional_validity(edge
);
2494 /* How many times should we count the constraints in "edge"?
2496 * We count as follows
2497 * validity -> 1 (>= 0)
2498 * validity+proximity -> 2 (>= 0 and upper bound)
2499 * proximity -> 2 (lower and upper bound)
2500 * local(+any) -> 2 (>= 0 and <= 0)
2502 * If an edge is only marked conditional_validity then it counts
2503 * as zero since it is only checked afterwards.
2505 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2506 * Otherwise, we ignore them.
2508 static int edge_multiplicity(struct isl_sched_edge
*edge
, int use_coincidence
)
2510 if (is_proximity(edge
) || force_zero(edge
, use_coincidence
))
2512 if (is_validity(edge
))
2517 /* How many times should the constraints in "edge" be counted
2518 * as a parametric intra-node constraint?
2520 * Only proximity edges that are not forced zero need
2521 * coefficient constraints that include coefficients for parameters.
2522 * If the edge is also a validity edge, then only
2523 * an upper bound is introduced. Otherwise, both lower and upper bounds
2526 static int parametric_intra_edge_multiplicity(struct isl_sched_edge
*edge
,
2527 int use_coincidence
)
2529 if (edge
->src
!= edge
->dst
)
2531 if (!is_proximity(edge
))
2533 if (force_zero(edge
, use_coincidence
))
2535 if (is_validity(edge
))
2541 /* Add "f" times the number of equality and inequality constraints of "bset"
2542 * to "n_eq" and "n_ineq" and free "bset".
2544 static isl_stat
update_count(__isl_take isl_basic_set
*bset
,
2545 int f
, int *n_eq
, int *n_ineq
)
2548 return isl_stat_error
;
2550 *n_eq
+= isl_basic_set_n_equality(bset
);
2551 *n_ineq
+= isl_basic_set_n_inequality(bset
);
2552 isl_basic_set_free(bset
);
2557 /* Count the number of equality and inequality constraints
2558 * that will be added for the given map.
2560 * The edges that require parameter coefficients are counted separately.
2562 * "use_coincidence" is set if we should take into account coincidence edges.
2564 static isl_stat
count_map_constraints(struct isl_sched_graph
*graph
,
2565 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
,
2566 int *n_eq
, int *n_ineq
, int use_coincidence
)
2569 isl_basic_set
*coef
;
2570 int f
= edge_multiplicity(edge
, use_coincidence
);
2571 int fp
= parametric_intra_edge_multiplicity(edge
, use_coincidence
);
2578 if (edge
->src
!= edge
->dst
) {
2579 coef
= inter_coefficients(graph
, edge
, map
);
2580 return update_count(coef
, f
, n_eq
, n_ineq
);
2584 copy
= isl_map_copy(map
);
2585 coef
= intra_coefficients(graph
, edge
->src
, copy
, 1);
2586 if (update_count(coef
, fp
, n_eq
, n_ineq
) < 0)
2591 copy
= isl_map_copy(map
);
2592 coef
= intra_coefficients(graph
, edge
->src
, copy
, 0);
2593 if (update_count(coef
, f
- fp
, n_eq
, n_ineq
) < 0)
2601 return isl_stat_error
;
2604 /* Count the number of equality and inequality constraints
2605 * that will be added to the main lp problem.
2606 * We count as follows
2607 * validity -> 1 (>= 0)
2608 * validity+proximity -> 2 (>= 0 and upper bound)
2609 * proximity -> 2 (lower and upper bound)
2610 * local(+any) -> 2 (>= 0 and <= 0)
2612 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2613 * Otherwise, we ignore them.
2615 static int count_constraints(struct isl_sched_graph
*graph
,
2616 int *n_eq
, int *n_ineq
, int use_coincidence
)
2620 *n_eq
= *n_ineq
= 0;
2621 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2622 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2623 isl_map
*map
= isl_map_copy(edge
->map
);
2625 if (count_map_constraints(graph
, edge
, map
, n_eq
, n_ineq
,
2626 use_coincidence
) < 0)
2633 /* Count the number of constraints that will be added by
2634 * add_bound_constant_constraints to bound the values of the constant terms
2635 * and increment *n_eq and *n_ineq accordingly.
2637 * In practice, add_bound_constant_constraints only adds inequalities.
2639 static isl_stat
count_bound_constant_constraints(isl_ctx
*ctx
,
2640 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
2642 if (isl_options_get_schedule_max_constant_term(ctx
) == -1)
2645 *n_ineq
+= graph
->n
;
2650 /* Add constraints to bound the values of the constant terms in the schedule,
2651 * if requested by the user.
2653 * The maximal value of the constant terms is defined by the option
2654 * "schedule_max_constant_term".
2656 static isl_stat
add_bound_constant_constraints(isl_ctx
*ctx
,
2657 struct isl_sched_graph
*graph
)
2663 max
= isl_options_get_schedule_max_constant_term(ctx
);
2667 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2669 return isl_stat_error
;
2671 for (i
= 0; i
< graph
->n
; ++i
) {
2672 struct isl_sched_node
*node
= &graph
->node
[i
];
2675 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2677 return isl_stat_error
;
2678 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2679 pos
= node_cst_coef_offset(node
);
2680 isl_int_set_si(graph
->lp
->ineq
[k
][1 + pos
], -1);
2681 isl_int_set_si(graph
->lp
->ineq
[k
][0], max
);
2687 /* Count the number of constraints that will be added by
2688 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
2691 * In practice, add_bound_coefficient_constraints only adds inequalities.
2693 static int count_bound_coefficient_constraints(isl_ctx
*ctx
,
2694 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
2698 if (isl_options_get_schedule_max_coefficient(ctx
) == -1 &&
2699 !isl_options_get_schedule_treat_coalescing(ctx
))
2702 for (i
= 0; i
< graph
->n
; ++i
)
2703 *n_ineq
+= graph
->node
[i
].nparam
+ 2 * graph
->node
[i
].nvar
;
2708 /* Add constraints to graph->lp that bound the values of
2709 * the parameter schedule coefficients of "node" to "max" and
2710 * the variable schedule coefficients to the corresponding entry
2712 * In either case, a negative value means that no bound needs to be imposed.
2714 * For parameter coefficients, this amounts to adding a constraint
2722 * The variables coefficients are, however, not represented directly.
2723 * Instead, the variable coefficients c_x are written as differences
2724 * c_x = c_x^+ - c_x^-.
2727 * -max_i <= c_x_i <= max_i
2731 * -max_i <= c_x_i^+ - c_x_i^- <= max_i
2735 * -(c_x_i^+ - c_x_i^-) + max_i >= 0
2736 * c_x_i^+ - c_x_i^- + max_i >= 0
2738 static isl_stat
node_add_coefficient_constraints(isl_ctx
*ctx
,
2739 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
, int max
)
2745 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2747 return isl_stat_error
;
2749 for (j
= 0; j
< node
->nparam
; ++j
) {
2755 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2757 return isl_stat_error
;
2758 dim
= 1 + node_par_coef_offset(node
) + j
;
2759 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2760 isl_int_set_si(graph
->lp
->ineq
[k
][dim
], -1);
2761 isl_int_set_si(graph
->lp
->ineq
[k
][0], max
);
2764 ineq
= isl_vec_alloc(ctx
, 1 + total
);
2765 ineq
= isl_vec_clr(ineq
);
2767 return isl_stat_error
;
2768 for (i
= 0; i
< node
->nvar
; ++i
) {
2769 int pos
= 1 + node_var_coef_pos(node
, i
);
2771 if (isl_int_is_neg(node
->max
->el
[i
]))
2774 isl_int_set_si(ineq
->el
[pos
], 1);
2775 isl_int_set_si(ineq
->el
[pos
+ 1], -1);
2776 isl_int_set(ineq
->el
[0], node
->max
->el
[i
]);
2778 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2781 isl_seq_cpy(graph
->lp
->ineq
[k
], ineq
->el
, 1 + total
);
2783 isl_seq_neg(ineq
->el
+ pos
, ineq
->el
+ pos
, 2);
2784 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2787 isl_seq_cpy(graph
->lp
->ineq
[k
], ineq
->el
, 1 + total
);
2789 isl_seq_clr(ineq
->el
+ pos
, 2);
2796 return isl_stat_error
;
2799 /* Add constraints that bound the values of the variable and parameter
2800 * coefficients of the schedule.
2802 * The maximal value of the coefficients is defined by the option
2803 * 'schedule_max_coefficient' and the entries in node->max.
2804 * These latter entries are only set if either the schedule_max_coefficient
2805 * option or the schedule_treat_coalescing option is set.
2807 static isl_stat
add_bound_coefficient_constraints(isl_ctx
*ctx
,
2808 struct isl_sched_graph
*graph
)
2813 max
= isl_options_get_schedule_max_coefficient(ctx
);
2815 if (max
== -1 && !isl_options_get_schedule_treat_coalescing(ctx
))
2818 for (i
= 0; i
< graph
->n
; ++i
) {
2819 struct isl_sched_node
*node
= &graph
->node
[i
];
2821 if (node_add_coefficient_constraints(ctx
, graph
, node
, max
) < 0)
2822 return isl_stat_error
;
2828 /* Add a constraint to graph->lp that equates the value at position
2829 * "sum_pos" to the sum of the "n" values starting at "first".
2831 static isl_stat
add_sum_constraint(struct isl_sched_graph
*graph
,
2832 int sum_pos
, int first
, int n
)
2837 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2839 return isl_stat_error
;
2841 k
= isl_basic_set_alloc_equality(graph
->lp
);
2843 return isl_stat_error
;
2844 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2845 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2846 for (i
= 0; i
< n
; ++i
)
2847 isl_int_set_si(graph
->lp
->eq
[k
][1 + first
+ i
], 1);
2852 /* Add a constraint to graph->lp that equates the value at position
2853 * "sum_pos" to the sum of the parameter coefficients of all nodes.
2855 static isl_stat
add_param_sum_constraint(struct isl_sched_graph
*graph
,
2861 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2863 return isl_stat_error
;
2865 k
= isl_basic_set_alloc_equality(graph
->lp
);
2867 return isl_stat_error
;
2868 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2869 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2870 for (i
= 0; i
< graph
->n
; ++i
) {
2871 int pos
= 1 + node_par_coef_offset(&graph
->node
[i
]);
2873 for (j
= 0; j
< graph
->node
[i
].nparam
; ++j
)
2874 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2880 /* Add a constraint to graph->lp that equates the value at position
2881 * "sum_pos" to the sum of the variable coefficients of all nodes.
2883 static isl_stat
add_var_sum_constraint(struct isl_sched_graph
*graph
,
2889 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2891 return isl_stat_error
;
2893 k
= isl_basic_set_alloc_equality(graph
->lp
);
2895 return isl_stat_error
;
2896 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2897 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2898 for (i
= 0; i
< graph
->n
; ++i
) {
2899 struct isl_sched_node
*node
= &graph
->node
[i
];
2900 int pos
= 1 + node_var_coef_offset(node
);
2902 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
2903 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2909 /* Construct an ILP problem for finding schedule coefficients
2910 * that result in non-negative, but small dependence distances
2911 * over all dependences.
2912 * In particular, the dependence distances over proximity edges
2913 * are bounded by m_0 + m_n n and we compute schedule coefficients
2914 * with small values (preferably zero) of m_n and m_0.
2916 * All variables of the ILP are non-negative. The actual coefficients
2917 * may be negative, so each coefficient is represented as the difference
2918 * of two non-negative variables. The negative part always appears
2919 * immediately before the positive part.
2920 * Other than that, the variables have the following order
2922 * - sum of positive and negative parts of m_n coefficients
2924 * - sum of all c_n coefficients
2925 * (unconstrained when computing non-parametric schedules)
2926 * - sum of positive and negative parts of all c_x coefficients
2927 * - positive and negative parts of m_n coefficients
2929 * - positive and negative parts of c_i_x, in opposite order
2930 * - c_i_n (if parametric)
2933 * The constraints are those from the edges plus two or three equalities
2934 * to express the sums.
2936 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2937 * Otherwise, we ignore them.
2939 static isl_stat
setup_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
2940 int use_coincidence
)
2950 parametric
= ctx
->opt
->schedule_parametric
;
2951 nparam
= isl_space_dim(graph
->node
[0].space
, isl_dim_param
);
2953 return isl_stat_error
;
2955 total
= param_pos
+ 2 * nparam
;
2956 for (i
= 0; i
< graph
->n
; ++i
) {
2957 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
2958 if (node_update_vmap(node
) < 0)
2959 return isl_stat_error
;
2960 node
->start
= total
;
2961 total
+= 1 + node
->nparam
+ 2 * node
->nvar
;
2964 if (count_constraints(graph
, &n_eq
, &n_ineq
, use_coincidence
) < 0)
2965 return isl_stat_error
;
2966 if (count_bound_constant_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2967 return isl_stat_error
;
2968 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2969 return isl_stat_error
;
2971 space
= isl_space_set_alloc(ctx
, 0, total
);
2972 isl_basic_set_free(graph
->lp
);
2973 n_eq
+= 2 + parametric
;
2975 graph
->lp
= isl_basic_set_alloc_space(space
, 0, n_eq
, n_ineq
);
2977 if (add_sum_constraint(graph
, 0, param_pos
, 2 * nparam
) < 0)
2978 return isl_stat_error
;
2979 if (parametric
&& add_param_sum_constraint(graph
, 2) < 0)
2980 return isl_stat_error
;
2981 if (add_var_sum_constraint(graph
, 3) < 0)
2982 return isl_stat_error
;
2983 if (add_bound_constant_constraints(ctx
, graph
) < 0)
2984 return isl_stat_error
;
2985 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
2986 return isl_stat_error
;
2987 if (add_all_validity_constraints(graph
, use_coincidence
) < 0)
2988 return isl_stat_error
;
2989 if (add_all_proximity_constraints(graph
, use_coincidence
) < 0)
2990 return isl_stat_error
;
2995 /* Analyze the conflicting constraint found by
2996 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2997 * constraint of one of the edges between distinct nodes, living, moreover
2998 * in distinct SCCs, then record the source and sink SCC as this may
2999 * be a good place to cut between SCCs.
3001 static int check_conflict(int con
, void *user
)
3004 struct isl_sched_graph
*graph
= user
;
3006 if (graph
->src_scc
>= 0)
3009 con
-= graph
->lp
->n_eq
;
3011 if (con
>= graph
->lp
->n_ineq
)
3014 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3015 if (!is_validity(&graph
->edge
[i
]))
3017 if (graph
->edge
[i
].src
== graph
->edge
[i
].dst
)
3019 if (graph
->edge
[i
].src
->scc
== graph
->edge
[i
].dst
->scc
)
3021 if (graph
->edge
[i
].start
> con
)
3023 if (graph
->edge
[i
].end
<= con
)
3025 graph
->src_scc
= graph
->edge
[i
].src
->scc
;
3026 graph
->dst_scc
= graph
->edge
[i
].dst
->scc
;
3032 /* Check whether the next schedule row of the given node needs to be
3033 * non-trivial. Lower-dimensional domains may have some trivial rows,
3034 * but as soon as the number of remaining required non-trivial rows
3035 * is as large as the number or remaining rows to be computed,
3036 * all remaining rows need to be non-trivial.
3038 static int needs_row(struct isl_sched_graph
*graph
, struct isl_sched_node
*node
)
3040 return node
->nvar
- node
->rank
>= graph
->maxvar
- graph
->n_row
;
3043 /* Construct a non-triviality region with triviality directions
3044 * corresponding to the rows of "indep".
3045 * The rows of "indep" are expressed in terms of the schedule coefficients c_i,
3046 * while the triviality directions are expressed in terms of
3047 * pairs of non-negative variables c^+_i - c^-_i, with c^-_i appearing
3048 * before c^+_i. Furthermore,
3049 * the pairs of non-negative variables representing the coefficients
3050 * are stored in the opposite order.
3052 static __isl_give isl_mat
*construct_trivial(__isl_keep isl_mat
*indep
)
3059 n
= isl_mat_rows(indep
);
3060 n_var
= isl_mat_cols(indep
);
3061 if (n
< 0 || n_var
< 0)
3064 ctx
= isl_mat_get_ctx(indep
);
3065 mat
= isl_mat_alloc(ctx
, n
, 2 * n_var
);
3068 for (i
= 0; i
< n
; ++i
) {
3069 for (j
= 0; j
< n_var
; ++j
) {
3070 int nj
= n_var
- 1 - j
;
3071 isl_int_neg(mat
->row
[i
][2 * nj
], indep
->row
[i
][j
]);
3072 isl_int_set(mat
->row
[i
][2 * nj
+ 1], indep
->row
[i
][j
]);
3079 /* Solve the ILP problem constructed in setup_lp.
3080 * For each node such that all the remaining rows of its schedule
3081 * need to be non-trivial, we construct a non-triviality region.
3082 * This region imposes that the next row is independent of previous rows.
3083 * In particular, the non-triviality region enforces that at least
3084 * one of the linear combinations in the rows of node->indep is non-zero.
3086 static __isl_give isl_vec
*solve_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3092 for (i
= 0; i
< graph
->n
; ++i
) {
3093 struct isl_sched_node
*node
= &graph
->node
[i
];
3096 graph
->region
[i
].pos
= node_var_coef_offset(node
);
3097 if (needs_row(graph
, node
))
3098 trivial
= construct_trivial(node
->indep
);
3100 trivial
= isl_mat_zero(ctx
, 0, 0);
3101 graph
->region
[i
].trivial
= trivial
;
3103 lp
= isl_basic_set_copy(graph
->lp
);
3104 sol
= isl_tab_basic_set_non_trivial_lexmin(lp
, 2, graph
->n
,
3105 graph
->region
, &check_conflict
, graph
);
3106 for (i
= 0; i
< graph
->n
; ++i
)
3107 isl_mat_free(graph
->region
[i
].trivial
);
3111 /* Extract the coefficients for the variables of "node" from "sol".
3113 * Each schedule coefficient c_i_x is represented as the difference
3114 * between two non-negative variables c_i_x^+ - c_i_x^-.
3115 * The c_i_x^- appear before their c_i_x^+ counterpart.
3116 * Furthermore, the order of these pairs is the opposite of that
3117 * of the corresponding coefficients.
3119 * Return c_i_x = c_i_x^+ - c_i_x^-
3121 static __isl_give isl_vec
*extract_var_coef(struct isl_sched_node
*node
,
3122 __isl_keep isl_vec
*sol
)
3130 csol
= isl_vec_alloc(isl_vec_get_ctx(sol
), node
->nvar
);
3134 pos
= 1 + node_var_coef_offset(node
);
3135 for (i
= 0; i
< node
->nvar
; ++i
)
3136 isl_int_sub(csol
->el
[node
->nvar
- 1 - i
],
3137 sol
->el
[pos
+ 2 * i
+ 1], sol
->el
[pos
+ 2 * i
]);
3142 /* Update the schedules of all nodes based on the given solution
3143 * of the LP problem.
3144 * The new row is added to the current band.
3145 * All possibly negative coefficients are encoded as a difference
3146 * of two non-negative variables, so we need to perform the subtraction
3149 * If coincident is set, then the caller guarantees that the new
3150 * row satisfies the coincidence constraints.
3152 static int update_schedule(struct isl_sched_graph
*graph
,
3153 __isl_take isl_vec
*sol
, int coincident
)
3156 isl_vec
*csol
= NULL
;
3161 isl_die(sol
->ctx
, isl_error_internal
,
3162 "no solution found", goto error
);
3163 if (graph
->n_total_row
>= graph
->max_row
)
3164 isl_die(sol
->ctx
, isl_error_internal
,
3165 "too many schedule rows", goto error
);
3167 for (i
= 0; i
< graph
->n
; ++i
) {
3168 struct isl_sched_node
*node
= &graph
->node
[i
];
3170 isl_size row
= isl_mat_rows(node
->sched
);
3173 csol
= extract_var_coef(node
, sol
);
3174 if (row
< 0 || !csol
)
3177 isl_map_free(node
->sched_map
);
3178 node
->sched_map
= NULL
;
3179 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
3182 pos
= node_cst_coef_offset(node
);
3183 node
->sched
= isl_mat_set_element(node
->sched
,
3184 row
, 0, sol
->el
[1 + pos
]);
3185 pos
= node_par_coef_offset(node
);
3186 for (j
= 0; j
< node
->nparam
; ++j
)
3187 node
->sched
= isl_mat_set_element(node
->sched
,
3188 row
, 1 + j
, sol
->el
[1 + pos
+ j
]);
3189 for (j
= 0; j
< node
->nvar
; ++j
)
3190 node
->sched
= isl_mat_set_element(node
->sched
,
3191 row
, 1 + node
->nparam
+ j
, csol
->el
[j
]);
3192 node
->coincident
[graph
->n_total_row
] = coincident
;
3198 graph
->n_total_row
++;
3207 /* Convert row "row" of node->sched into an isl_aff living in "ls"
3208 * and return this isl_aff.
3210 static __isl_give isl_aff
*extract_schedule_row(__isl_take isl_local_space
*ls
,
3211 struct isl_sched_node
*node
, int row
)
3219 aff
= isl_aff_zero_on_domain(ls
);
3220 if (isl_mat_get_element(node
->sched
, row
, 0, &v
) < 0)
3222 aff
= isl_aff_set_constant(aff
, v
);
3223 for (j
= 0; j
< node
->nparam
; ++j
) {
3224 if (isl_mat_get_element(node
->sched
, row
, 1 + j
, &v
) < 0)
3226 aff
= isl_aff_set_coefficient(aff
, isl_dim_param
, j
, v
);
3228 for (j
= 0; j
< node
->nvar
; ++j
) {
3229 if (isl_mat_get_element(node
->sched
, row
,
3230 1 + node
->nparam
+ j
, &v
) < 0)
3232 aff
= isl_aff_set_coefficient(aff
, isl_dim_in
, j
, v
);
3244 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
3245 * and return this multi_aff.
3247 * The result is defined over the uncompressed node domain.
3249 static __isl_give isl_multi_aff
*node_extract_partial_schedule_multi_aff(
3250 struct isl_sched_node
*node
, int first
, int n
)
3254 isl_local_space
*ls
;
3261 nrow
= isl_mat_rows(node
->sched
);
3264 if (node
->compressed
)
3265 space
= isl_pw_multi_aff_get_domain_space(node
->decompress
);
3267 space
= isl_space_copy(node
->space
);
3268 ls
= isl_local_space_from_space(isl_space_copy(space
));
3269 space
= isl_space_from_domain(space
);
3270 space
= isl_space_add_dims(space
, isl_dim_out
, n
);
3271 ma
= isl_multi_aff_zero(space
);
3273 for (i
= first
; i
< first
+ n
; ++i
) {
3274 aff
= extract_schedule_row(isl_local_space_copy(ls
), node
, i
);
3275 ma
= isl_multi_aff_set_aff(ma
, i
- first
, aff
);
3278 isl_local_space_free(ls
);
3280 if (node
->compressed
)
3281 ma
= isl_multi_aff_pullback_multi_aff(ma
,
3282 isl_multi_aff_copy(node
->compress
));
3287 /* Convert node->sched into a multi_aff and return this multi_aff.
3289 * The result is defined over the uncompressed node domain.
3291 static __isl_give isl_multi_aff
*node_extract_schedule_multi_aff(
3292 struct isl_sched_node
*node
)
3296 nrow
= isl_mat_rows(node
->sched
);
3299 return node_extract_partial_schedule_multi_aff(node
, 0, nrow
);
3302 /* Convert node->sched into a map and return this map.
3304 * The result is cached in node->sched_map, which needs to be released
3305 * whenever node->sched is updated.
3306 * It is defined over the uncompressed node domain.
3308 static __isl_give isl_map
*node_extract_schedule(struct isl_sched_node
*node
)
3310 if (!node
->sched_map
) {
3313 ma
= node_extract_schedule_multi_aff(node
);
3314 node
->sched_map
= isl_map_from_multi_aff(ma
);
3317 return isl_map_copy(node
->sched_map
);
3320 /* Construct a map that can be used to update a dependence relation
3321 * based on the current schedule.
3322 * That is, construct a map expressing that source and sink
3323 * are executed within the same iteration of the current schedule.
3324 * This map can then be intersected with the dependence relation.
3325 * This is not the most efficient way, but this shouldn't be a critical
3328 static __isl_give isl_map
*specializer(struct isl_sched_node
*src
,
3329 struct isl_sched_node
*dst
)
3331 isl_map
*src_sched
, *dst_sched
;
3333 src_sched
= node_extract_schedule(src
);
3334 dst_sched
= node_extract_schedule(dst
);
3335 return isl_map_apply_range(src_sched
, isl_map_reverse(dst_sched
));
3338 /* Intersect the domains of the nested relations in domain and range
3339 * of "umap" with "map".
3341 static __isl_give isl_union_map
*intersect_domains(
3342 __isl_take isl_union_map
*umap
, __isl_keep isl_map
*map
)
3344 isl_union_set
*uset
;
3346 umap
= isl_union_map_zip(umap
);
3347 uset
= isl_union_set_from_set(isl_map_wrap(isl_map_copy(map
)));
3348 umap
= isl_union_map_intersect_domain(umap
, uset
);
3349 umap
= isl_union_map_zip(umap
);
3353 /* Update the dependence relation of the given edge based
3354 * on the current schedule.
3355 * If the dependence is carried completely by the current schedule, then
3356 * it is removed from the edge_tables. It is kept in the list of edges
3357 * as otherwise all edge_tables would have to be recomputed.
3359 * If the edge is of a type that can appear multiple times
3360 * between the same pair of nodes, then it is added to
3361 * the edge table (again). This prevents the situation
3362 * where none of these edges is referenced from the edge table
3363 * because the one that was referenced turned out to be empty and
3364 * was therefore removed from the table.
3366 static isl_stat
update_edge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
3367 struct isl_sched_edge
*edge
)
3372 id
= specializer(edge
->src
, edge
->dst
);
3373 edge
->map
= isl_map_intersect(edge
->map
, isl_map_copy(id
));
3377 if (edge
->tagged_condition
) {
3378 edge
->tagged_condition
=
3379 intersect_domains(edge
->tagged_condition
, id
);
3380 if (!edge
->tagged_condition
)
3383 if (edge
->tagged_validity
) {
3384 edge
->tagged_validity
=
3385 intersect_domains(edge
->tagged_validity
, id
);
3386 if (!edge
->tagged_validity
)
3390 empty
= isl_map_plain_is_empty(edge
->map
);
3394 graph_remove_edge(graph
, edge
);
3395 } else if (is_multi_edge_type(edge
)) {
3396 if (graph_edge_tables_add(ctx
, graph
, edge
) < 0)
3404 return isl_stat_error
;
3407 /* Does the domain of "umap" intersect "uset"?
3409 static int domain_intersects(__isl_keep isl_union_map
*umap
,
3410 __isl_keep isl_union_set
*uset
)
3414 umap
= isl_union_map_copy(umap
);
3415 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(uset
));
3416 empty
= isl_union_map_is_empty(umap
);
3417 isl_union_map_free(umap
);
3419 return empty
< 0 ? -1 : !empty
;
3422 /* Does the range of "umap" intersect "uset"?
3424 static int range_intersects(__isl_keep isl_union_map
*umap
,
3425 __isl_keep isl_union_set
*uset
)
3429 umap
= isl_union_map_copy(umap
);
3430 umap
= isl_union_map_intersect_range(umap
, isl_union_set_copy(uset
));
3431 empty
= isl_union_map_is_empty(umap
);
3432 isl_union_map_free(umap
);
3434 return empty
< 0 ? -1 : !empty
;
3437 /* Are the condition dependences of "edge" local with respect to
3438 * the current schedule?
3440 * That is, are domain and range of the condition dependences mapped
3441 * to the same point?
3443 * In other words, is the condition false?
3445 static int is_condition_false(struct isl_sched_edge
*edge
)
3447 isl_union_map
*umap
;
3448 isl_map
*map
, *sched
, *test
;
3451 empty
= isl_union_map_is_empty(edge
->tagged_condition
);
3452 if (empty
< 0 || empty
)
3455 umap
= isl_union_map_copy(edge
->tagged_condition
);
3456 umap
= isl_union_map_zip(umap
);
3457 umap
= isl_union_set_unwrap(isl_union_map_domain(umap
));
3458 map
= isl_map_from_union_map(umap
);
3460 sched
= node_extract_schedule(edge
->src
);
3461 map
= isl_map_apply_domain(map
, sched
);
3462 sched
= node_extract_schedule(edge
->dst
);
3463 map
= isl_map_apply_range(map
, sched
);
3465 test
= isl_map_identity(isl_map_get_space(map
));
3466 local
= isl_map_is_subset(map
, test
);
3473 /* For each conditional validity constraint that is adjacent
3474 * to a condition with domain in condition_source or range in condition_sink,
3475 * turn it into an unconditional validity constraint.
3477 static int unconditionalize_adjacent_validity(struct isl_sched_graph
*graph
,
3478 __isl_take isl_union_set
*condition_source
,
3479 __isl_take isl_union_set
*condition_sink
)
3483 condition_source
= isl_union_set_coalesce(condition_source
);
3484 condition_sink
= isl_union_set_coalesce(condition_sink
);
3486 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3488 isl_union_map
*validity
;
3490 if (!is_conditional_validity(&graph
->edge
[i
]))
3492 if (is_validity(&graph
->edge
[i
]))
3495 validity
= graph
->edge
[i
].tagged_validity
;
3496 adjacent
= domain_intersects(validity
, condition_sink
);
3497 if (adjacent
>= 0 && !adjacent
)
3498 adjacent
= range_intersects(validity
, condition_source
);
3504 set_validity(&graph
->edge
[i
]);
3507 isl_union_set_free(condition_source
);
3508 isl_union_set_free(condition_sink
);
3511 isl_union_set_free(condition_source
);
3512 isl_union_set_free(condition_sink
);
3516 /* Update the dependence relations of all edges based on the current schedule
3517 * and enforce conditional validity constraints that are adjacent
3518 * to satisfied condition constraints.
3520 * First check if any of the condition constraints are satisfied
3521 * (i.e., not local to the outer schedule) and keep track of
3522 * their domain and range.
3523 * Then update all dependence relations (which removes the non-local
3525 * Finally, if any condition constraints turned out to be satisfied,
3526 * then turn all adjacent conditional validity constraints into
3527 * unconditional validity constraints.
3529 static int update_edges(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3533 isl_union_set
*source
, *sink
;
3535 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3536 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3537 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3539 isl_union_set
*uset
;
3540 isl_union_map
*umap
;
3542 if (!is_condition(&graph
->edge
[i
]))
3544 if (is_local(&graph
->edge
[i
]))
3546 local
= is_condition_false(&graph
->edge
[i
]);
3554 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
3555 uset
= isl_union_map_domain(umap
);
3556 source
= isl_union_set_union(source
, uset
);
3558 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
3559 uset
= isl_union_map_range(umap
);
3560 sink
= isl_union_set_union(sink
, uset
);
3563 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3564 if (update_edge(ctx
, graph
, &graph
->edge
[i
]) < 0)
3569 return unconditionalize_adjacent_validity(graph
, source
, sink
);
3571 isl_union_set_free(source
);
3572 isl_union_set_free(sink
);
3575 isl_union_set_free(source
);
3576 isl_union_set_free(sink
);
3580 static void next_band(struct isl_sched_graph
*graph
)
3582 graph
->band_start
= graph
->n_total_row
;
3585 /* Return the union of the universe domains of the nodes in "graph"
3586 * that satisfy "pred".
3588 static __isl_give isl_union_set
*isl_sched_graph_domain(isl_ctx
*ctx
,
3589 struct isl_sched_graph
*graph
,
3590 int (*pred
)(struct isl_sched_node
*node
, int data
), int data
)
3596 for (i
= 0; i
< graph
->n
; ++i
)
3597 if (pred(&graph
->node
[i
], data
))
3601 isl_die(ctx
, isl_error_internal
,
3602 "empty component", return NULL
);
3604 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
3605 dom
= isl_union_set_from_set(set
);
3607 for (i
= i
+ 1; i
< graph
->n
; ++i
) {
3608 if (!pred(&graph
->node
[i
], data
))
3610 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
3611 dom
= isl_union_set_union(dom
, isl_union_set_from_set(set
));
3617 /* Return a list of unions of universe domains, where each element
3618 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
3620 static __isl_give isl_union_set_list
*extract_sccs(isl_ctx
*ctx
,
3621 struct isl_sched_graph
*graph
)
3624 isl_union_set_list
*filters
;
3626 filters
= isl_union_set_list_alloc(ctx
, graph
->scc
);
3627 for (i
= 0; i
< graph
->scc
; ++i
) {
3630 dom
= isl_sched_graph_domain(ctx
, graph
, &node_scc_exactly
, i
);
3631 filters
= isl_union_set_list_add(filters
, dom
);
3637 /* Return a list of two unions of universe domains, one for the SCCs up
3638 * to and including graph->src_scc and another for the other SCCs.
3640 static __isl_give isl_union_set_list
*extract_split(isl_ctx
*ctx
,
3641 struct isl_sched_graph
*graph
)
3644 isl_union_set_list
*filters
;
3646 filters
= isl_union_set_list_alloc(ctx
, 2);
3647 dom
= isl_sched_graph_domain(ctx
, graph
,
3648 &node_scc_at_most
, graph
->src_scc
);
3649 filters
= isl_union_set_list_add(filters
, dom
);
3650 dom
= isl_sched_graph_domain(ctx
, graph
,
3651 &node_scc_at_least
, graph
->src_scc
+ 1);
3652 filters
= isl_union_set_list_add(filters
, dom
);
3657 /* Copy nodes that satisfy node_pred from the src dependence graph
3658 * to the dst dependence graph.
3660 static isl_stat
copy_nodes(struct isl_sched_graph
*dst
,
3661 struct isl_sched_graph
*src
,
3662 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
3667 for (i
= 0; i
< src
->n
; ++i
) {
3670 if (!node_pred(&src
->node
[i
], data
))
3674 dst
->node
[j
].space
= isl_space_copy(src
->node
[i
].space
);
3675 dst
->node
[j
].compressed
= src
->node
[i
].compressed
;
3676 dst
->node
[j
].hull
= isl_set_copy(src
->node
[i
].hull
);
3677 dst
->node
[j
].compress
=
3678 isl_multi_aff_copy(src
->node
[i
].compress
);
3679 dst
->node
[j
].decompress
=
3680 isl_pw_multi_aff_copy(src
->node
[i
].decompress
);
3681 dst
->node
[j
].nvar
= src
->node
[i
].nvar
;
3682 dst
->node
[j
].nparam
= src
->node
[i
].nparam
;
3683 dst
->node
[j
].sched
= isl_mat_copy(src
->node
[i
].sched
);
3684 dst
->node
[j
].sched_map
= isl_map_copy(src
->node
[i
].sched_map
);
3685 dst
->node
[j
].coincident
= src
->node
[i
].coincident
;
3686 dst
->node
[j
].sizes
= isl_multi_val_copy(src
->node
[i
].sizes
);
3687 dst
->node
[j
].bounds
= isl_basic_set_copy(src
->node
[i
].bounds
);
3688 dst
->node
[j
].max
= isl_vec_copy(src
->node
[i
].max
);
3691 if (!dst
->node
[j
].space
|| !dst
->node
[j
].sched
)
3692 return isl_stat_error
;
3693 if (dst
->node
[j
].compressed
&&
3694 (!dst
->node
[j
].hull
|| !dst
->node
[j
].compress
||
3695 !dst
->node
[j
].decompress
))
3696 return isl_stat_error
;
3702 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
3703 * to the dst dependence graph.
3704 * If the source or destination node of the edge is not in the destination
3705 * graph, then it must be a backward proximity edge and it should simply
3708 static isl_stat
copy_edges(isl_ctx
*ctx
, struct isl_sched_graph
*dst
,
3709 struct isl_sched_graph
*src
,
3710 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
), int data
)
3715 for (i
= 0; i
< src
->n_edge
; ++i
) {
3716 struct isl_sched_edge
*edge
= &src
->edge
[i
];
3718 isl_union_map
*tagged_condition
;
3719 isl_union_map
*tagged_validity
;
3720 struct isl_sched_node
*dst_src
, *dst_dst
;
3722 if (!edge_pred(edge
, data
))
3725 if (isl_map_plain_is_empty(edge
->map
))
3728 dst_src
= graph_find_node(ctx
, dst
, edge
->src
->space
);
3729 dst_dst
= graph_find_node(ctx
, dst
, edge
->dst
->space
);
3730 if (!dst_src
|| !dst_dst
)
3731 return isl_stat_error
;
3732 if (!is_node(dst
, dst_src
) || !is_node(dst
, dst_dst
)) {
3733 if (is_validity(edge
) || is_conditional_validity(edge
))
3734 isl_die(ctx
, isl_error_internal
,
3735 "backward (conditional) validity edge",
3736 return isl_stat_error
);
3740 map
= isl_map_copy(edge
->map
);
3741 tagged_condition
= isl_union_map_copy(edge
->tagged_condition
);
3742 tagged_validity
= isl_union_map_copy(edge
->tagged_validity
);
3744 dst
->edge
[dst
->n_edge
].src
= dst_src
;
3745 dst
->edge
[dst
->n_edge
].dst
= dst_dst
;
3746 dst
->edge
[dst
->n_edge
].map
= map
;
3747 dst
->edge
[dst
->n_edge
].tagged_condition
= tagged_condition
;
3748 dst
->edge
[dst
->n_edge
].tagged_validity
= tagged_validity
;
3749 dst
->edge
[dst
->n_edge
].types
= edge
->types
;
3752 if (edge
->tagged_condition
&& !tagged_condition
)
3753 return isl_stat_error
;
3754 if (edge
->tagged_validity
&& !tagged_validity
)
3755 return isl_stat_error
;
3757 if (graph_edge_tables_add(ctx
, dst
,
3758 &dst
->edge
[dst
->n_edge
- 1]) < 0)
3759 return isl_stat_error
;
3765 /* Compute the maximal number of variables over all nodes.
3766 * This is the maximal number of linearly independent schedule
3767 * rows that we need to compute.
3768 * Just in case we end up in a part of the dependence graph
3769 * with only lower-dimensional domains, we make sure we will
3770 * compute the required amount of extra linearly independent rows.
3772 static int compute_maxvar(struct isl_sched_graph
*graph
)
3777 for (i
= 0; i
< graph
->n
; ++i
) {
3778 struct isl_sched_node
*node
= &graph
->node
[i
];
3781 if (node_update_vmap(node
) < 0)
3783 nvar
= node
->nvar
+ graph
->n_row
- node
->rank
;
3784 if (nvar
> graph
->maxvar
)
3785 graph
->maxvar
= nvar
;
3791 /* Extract the subgraph of "graph" that consists of the nodes satisfying
3792 * "node_pred" and the edges satisfying "edge_pred" and store
3793 * the result in "sub".
3795 static isl_stat
extract_sub_graph(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
3796 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
3797 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
3798 int data
, struct isl_sched_graph
*sub
)
3800 int i
, n
= 0, n_edge
= 0;
3803 for (i
= 0; i
< graph
->n
; ++i
)
3804 if (node_pred(&graph
->node
[i
], data
))
3806 for (i
= 0; i
< graph
->n_edge
; ++i
)
3807 if (edge_pred(&graph
->edge
[i
], data
))
3809 if (graph_alloc(ctx
, sub
, n
, n_edge
) < 0)
3810 return isl_stat_error
;
3811 sub
->root
= graph
->root
;
3812 if (copy_nodes(sub
, graph
, node_pred
, data
) < 0)
3813 return isl_stat_error
;
3814 if (graph_init_table(ctx
, sub
) < 0)
3815 return isl_stat_error
;
3816 for (t
= 0; t
<= isl_edge_last
; ++t
)
3817 sub
->max_edge
[t
] = graph
->max_edge
[t
];
3818 if (graph_init_edge_tables(ctx
, sub
) < 0)
3819 return isl_stat_error
;
3820 if (copy_edges(ctx
, sub
, graph
, edge_pred
, data
) < 0)
3821 return isl_stat_error
;
3822 sub
->n_row
= graph
->n_row
;
3823 sub
->max_row
= graph
->max_row
;
3824 sub
->n_total_row
= graph
->n_total_row
;
3825 sub
->band_start
= graph
->band_start
;
3830 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
3831 struct isl_sched_graph
*graph
);
3832 static __isl_give isl_schedule_node
*compute_schedule_wcc(
3833 isl_schedule_node
*node
, struct isl_sched_graph
*graph
);
3835 /* Compute a schedule for a subgraph of "graph". In particular, for
3836 * the graph composed of nodes that satisfy node_pred and edges that
3837 * that satisfy edge_pred.
3838 * If the subgraph is known to consist of a single component, then wcc should
3839 * be set and then we call compute_schedule_wcc on the constructed subgraph.
3840 * Otherwise, we call compute_schedule, which will check whether the subgraph
3843 * The schedule is inserted at "node" and the updated schedule node
3846 static __isl_give isl_schedule_node
*compute_sub_schedule(
3847 __isl_take isl_schedule_node
*node
, isl_ctx
*ctx
,
3848 struct isl_sched_graph
*graph
,
3849 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
3850 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
3853 struct isl_sched_graph split
= { 0 };
3855 if (extract_sub_graph(ctx
, graph
, node_pred
, edge_pred
, data
,
3860 node
= compute_schedule_wcc(node
, &split
);
3862 node
= compute_schedule(node
, &split
);
3864 graph_free(ctx
, &split
);
3867 graph_free(ctx
, &split
);
3868 return isl_schedule_node_free(node
);
3871 static int edge_scc_exactly(struct isl_sched_edge
*edge
, int scc
)
3873 return edge
->src
->scc
== scc
&& edge
->dst
->scc
== scc
;
3876 static int edge_dst_scc_at_most(struct isl_sched_edge
*edge
, int scc
)
3878 return edge
->dst
->scc
<= scc
;
3881 static int edge_src_scc_at_least(struct isl_sched_edge
*edge
, int scc
)
3883 return edge
->src
->scc
>= scc
;
3886 /* Reset the current band by dropping all its schedule rows.
3888 static isl_stat
reset_band(struct isl_sched_graph
*graph
)
3893 drop
= graph
->n_total_row
- graph
->band_start
;
3894 graph
->n_total_row
-= drop
;
3895 graph
->n_row
-= drop
;
3897 for (i
= 0; i
< graph
->n
; ++i
) {
3898 struct isl_sched_node
*node
= &graph
->node
[i
];
3900 isl_map_free(node
->sched_map
);
3901 node
->sched_map
= NULL
;
3903 node
->sched
= isl_mat_drop_rows(node
->sched
,
3904 graph
->band_start
, drop
);
3907 return isl_stat_error
;
3913 /* Split the current graph into two parts and compute a schedule for each
3914 * part individually. In particular, one part consists of all SCCs up
3915 * to and including graph->src_scc, while the other part contains the other
3916 * SCCs. The split is enforced by a sequence node inserted at position "node"
3917 * in the schedule tree. Return the updated schedule node.
3918 * If either of these two parts consists of a sequence, then it is spliced
3919 * into the sequence containing the two parts.
3921 * The current band is reset. It would be possible to reuse
3922 * the previously computed rows as the first rows in the next
3923 * band, but recomputing them may result in better rows as we are looking
3924 * at a smaller part of the dependence graph.
3926 static __isl_give isl_schedule_node
*compute_split_schedule(
3927 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3931 isl_union_set_list
*filters
;
3936 if (reset_band(graph
) < 0)
3937 return isl_schedule_node_free(node
);
3941 ctx
= isl_schedule_node_get_ctx(node
);
3942 filters
= extract_split(ctx
, graph
);
3943 node
= isl_schedule_node_insert_sequence(node
, filters
);
3944 node
= isl_schedule_node_child(node
, 1);
3945 node
= isl_schedule_node_child(node
, 0);
3947 node
= compute_sub_schedule(node
, ctx
, graph
,
3948 &node_scc_at_least
, &edge_src_scc_at_least
,
3949 graph
->src_scc
+ 1, 0);
3950 is_seq
= isl_schedule_node_get_type(node
) == isl_schedule_node_sequence
;
3951 node
= isl_schedule_node_parent(node
);
3952 node
= isl_schedule_node_parent(node
);
3954 node
= isl_schedule_node_sequence_splice_child(node
, 1);
3955 node
= isl_schedule_node_child(node
, 0);
3956 node
= isl_schedule_node_child(node
, 0);
3957 node
= compute_sub_schedule(node
, ctx
, graph
,
3958 &node_scc_at_most
, &edge_dst_scc_at_most
,
3960 is_seq
= isl_schedule_node_get_type(node
) == isl_schedule_node_sequence
;
3961 node
= isl_schedule_node_parent(node
);
3962 node
= isl_schedule_node_parent(node
);
3964 node
= isl_schedule_node_sequence_splice_child(node
, 0);
3969 /* Insert a band node at position "node" in the schedule tree corresponding
3970 * to the current band in "graph". Mark the band node permutable
3971 * if "permutable" is set.
3972 * The partial schedules and the coincidence property are extracted
3973 * from the graph nodes.
3974 * Return the updated schedule node.
3976 static __isl_give isl_schedule_node
*insert_current_band(
3977 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
3983 isl_multi_pw_aff
*mpa
;
3984 isl_multi_union_pw_aff
*mupa
;
3990 isl_die(isl_schedule_node_get_ctx(node
), isl_error_internal
,
3991 "graph should have at least one node",
3992 return isl_schedule_node_free(node
));
3994 start
= graph
->band_start
;
3995 end
= graph
->n_total_row
;
3998 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[0], start
, n
);
3999 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
4000 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
4002 for (i
= 1; i
< graph
->n
; ++i
) {
4003 isl_multi_union_pw_aff
*mupa_i
;
4005 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[i
],
4007 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
4008 mupa_i
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
4009 mupa
= isl_multi_union_pw_aff_union_add(mupa
, mupa_i
);
4011 node
= isl_schedule_node_insert_partial_schedule(node
, mupa
);
4013 for (i
= 0; i
< n
; ++i
)
4014 node
= isl_schedule_node_band_member_set_coincident(node
, i
,
4015 graph
->node
[0].coincident
[start
+ i
]);
4016 node
= isl_schedule_node_band_set_permutable(node
, permutable
);
4021 /* Update the dependence relations based on the current schedule,
4022 * add the current band to "node" and then continue with the computation
4024 * Return the updated schedule node.
4026 static __isl_give isl_schedule_node
*compute_next_band(
4027 __isl_take isl_schedule_node
*node
,
4028 struct isl_sched_graph
*graph
, int permutable
)
4035 ctx
= isl_schedule_node_get_ctx(node
);
4036 if (update_edges(ctx
, graph
) < 0)
4037 return isl_schedule_node_free(node
);
4038 node
= insert_current_band(node
, graph
, permutable
);
4041 node
= isl_schedule_node_child(node
, 0);
4042 node
= compute_schedule(node
, graph
);
4043 node
= isl_schedule_node_parent(node
);
4048 /* Add the constraints "coef" derived from an edge from "node" to itself
4049 * to graph->lp in order to respect the dependences and to try and carry them.
4050 * "pos" is the sequence number of the edge that needs to be carried.
4051 * "coef" represents general constraints on coefficients (c_0, c_x)
4052 * of valid constraints for (y - x) with x and y instances of the node.
4054 * The constraints added to graph->lp need to enforce
4056 * (c_j_0 + c_j_x y) - (c_j_0 + c_j_x x)
4057 * = c_j_x (y - x) >= e_i
4059 * for each (x,y) in the dependence relation of the edge.
4060 * That is, (-e_i, c_j_x) needs to be plugged in for (c_0, c_x),
4061 * taking into account that each coefficient in c_j_x is represented
4062 * as a pair of non-negative coefficients.
4064 static isl_stat
add_intra_constraints(struct isl_sched_graph
*graph
,
4065 struct isl_sched_node
*node
, __isl_take isl_basic_set
*coef
, int pos
)
4069 isl_dim_map
*dim_map
;
4071 offset
= coef_var_offset(coef
);
4073 coef
= isl_basic_set_free(coef
);
4075 return isl_stat_error
;
4077 ctx
= isl_basic_set_get_ctx(coef
);
4078 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, 1);
4079 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
4080 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
4085 /* Add the constraints "coef" derived from an edge from "src" to "dst"
4086 * to graph->lp in order to respect the dependences and to try and carry them.
4087 * "pos" is the sequence number of the edge that needs to be carried or
4088 * -1 if no attempt should be made to carry the dependences.
4089 * "coef" represents general constraints on coefficients (c_0, c_n, c_x, c_y)
4090 * of valid constraints for (x, y) with x and y instances of "src" and "dst".
4092 * The constraints added to graph->lp need to enforce
4094 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
4096 * for each (x,y) in the dependence relation of the edge or
4098 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= 0
4102 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, -c_j_x, c_k_x)
4104 * (c_k_0 - c_j_0, c_k_n - c_j_n, -c_j_x, c_k_x)
4105 * needs to be plugged in for (c_0, c_n, c_x, c_y),
4106 * taking into account that each coefficient in c_j_x and c_k_x is represented
4107 * as a pair of non-negative coefficients.
4109 static isl_stat
add_inter_constraints(struct isl_sched_graph
*graph
,
4110 struct isl_sched_node
*src
, struct isl_sched_node
*dst
,
4111 __isl_take isl_basic_set
*coef
, int pos
)
4115 isl_dim_map
*dim_map
;
4117 offset
= coef_var_offset(coef
);
4119 coef
= isl_basic_set_free(coef
);
4121 return isl_stat_error
;
4123 ctx
= isl_basic_set_get_ctx(coef
);
4124 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, 1);
4126 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
4127 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
4132 /* Data structure for keeping track of the data needed
4133 * to exploit non-trivial lineality spaces.
4135 * "any_non_trivial" is true if there are any non-trivial lineality spaces.
4136 * If "any_non_trivial" is not true, then "equivalent" and "mask" may be NULL.
4137 * "equivalent" connects instances to other instances on the same line(s).
4138 * "mask" contains the domain spaces of "equivalent".
4139 * Any instance set not in "mask" does not have a non-trivial lineality space.
4141 struct isl_exploit_lineality_data
{
4142 isl_bool any_non_trivial
;
4143 isl_union_map
*equivalent
;
4144 isl_union_set
*mask
;
4147 /* Data structure collecting information used during the construction
4148 * of an LP for carrying dependences.
4150 * "intra" is a sequence of coefficient constraints for intra-node edges.
4151 * "inter" is a sequence of coefficient constraints for inter-node edges.
4152 * "lineality" contains data used to exploit non-trivial lineality spaces.
4155 isl_basic_set_list
*intra
;
4156 isl_basic_set_list
*inter
;
4157 struct isl_exploit_lineality_data lineality
;
4160 /* Free all the data stored in "carry".
4162 static void isl_carry_clear(struct isl_carry
*carry
)
4164 isl_basic_set_list_free(carry
->intra
);
4165 isl_basic_set_list_free(carry
->inter
);
4166 isl_union_map_free(carry
->lineality
.equivalent
);
4167 isl_union_set_free(carry
->lineality
.mask
);
4170 /* Return a pointer to the node in "graph" that lives in "space".
4171 * If the requested node has been compressed, then "space"
4172 * corresponds to the compressed space.
4173 * The graph is assumed to have such a node.
4174 * Return NULL in case of error.
4176 * First try and see if "space" is the space of an uncompressed node.
4177 * If so, return that node.
4178 * Otherwise, "space" was constructed by construct_compressed_id and
4179 * contains a user pointer pointing to the node in the tuple id.
4180 * However, this node belongs to the original dependence graph.
4181 * If "graph" is a subgraph of this original dependence graph,
4182 * then the node with the same space still needs to be looked up
4183 * in the current graph.
4185 static struct isl_sched_node
*graph_find_compressed_node(isl_ctx
*ctx
,
4186 struct isl_sched_graph
*graph
, __isl_keep isl_space
*space
)
4189 struct isl_sched_node
*node
;
4194 node
= graph_find_node(ctx
, graph
, space
);
4197 if (is_node(graph
, node
))
4200 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
4201 node
= isl_id_get_user(id
);
4207 if (!is_node(graph
->root
, node
))
4208 isl_die(ctx
, isl_error_internal
,
4209 "space points to invalid node", return NULL
);
4210 if (graph
!= graph
->root
)
4211 node
= graph_find_node(ctx
, graph
, node
->space
);
4212 if (!is_node(graph
, node
))
4213 isl_die(ctx
, isl_error_internal
,
4214 "unable to find node", return NULL
);
4219 /* Internal data structure for add_all_constraints.
4221 * "graph" is the schedule constraint graph for which an LP problem
4222 * is being constructed.
4223 * "carry_inter" indicates whether inter-node edges should be carried.
4224 * "pos" is the position of the next edge that needs to be carried.
4226 struct isl_add_all_constraints_data
{
4228 struct isl_sched_graph
*graph
;
4233 /* Add the constraints "coef" derived from an edge from a node to itself
4234 * to data->graph->lp in order to respect the dependences and
4235 * to try and carry them.
4237 * The space of "coef" is of the form
4239 * coefficients[[c_cst] -> S[c_x]]
4241 * with S[c_x] the (compressed) space of the node.
4242 * Extract the node from the space and call add_intra_constraints.
4244 static isl_stat
lp_add_intra(__isl_take isl_basic_set
*coef
, void *user
)
4246 struct isl_add_all_constraints_data
*data
= user
;
4248 struct isl_sched_node
*node
;
4250 space
= isl_basic_set_get_space(coef
);
4251 space
= isl_space_range(isl_space_unwrap(space
));
4252 node
= graph_find_compressed_node(data
->ctx
, data
->graph
, space
);
4253 isl_space_free(space
);
4254 return add_intra_constraints(data
->graph
, node
, coef
, data
->pos
++);
4257 /* Add the constraints "coef" derived from an edge from a node j
4258 * to a node k to data->graph->lp in order to respect the dependences and
4259 * to try and carry them (provided data->carry_inter is set).
4261 * The space of "coef" is of the form
4263 * coefficients[[c_cst, c_n] -> [S_j[c_x] -> S_k[c_y]]]
4265 * with S_j[c_x] and S_k[c_y] the (compressed) spaces of the nodes.
4266 * Extract the nodes from the space and call add_inter_constraints.
4268 static isl_stat
lp_add_inter(__isl_take isl_basic_set
*coef
, void *user
)
4270 struct isl_add_all_constraints_data
*data
= user
;
4271 isl_space
*space
, *dom
;
4272 struct isl_sched_node
*src
, *dst
;
4275 space
= isl_basic_set_get_space(coef
);
4276 space
= isl_space_unwrap(isl_space_range(isl_space_unwrap(space
)));
4277 dom
= isl_space_domain(isl_space_copy(space
));
4278 src
= graph_find_compressed_node(data
->ctx
, data
->graph
, dom
);
4279 isl_space_free(dom
);
4280 space
= isl_space_range(space
);
4281 dst
= graph_find_compressed_node(data
->ctx
, data
->graph
, space
);
4282 isl_space_free(space
);
4284 pos
= data
->carry_inter
? data
->pos
++ : -1;
4285 return add_inter_constraints(data
->graph
, src
, dst
, coef
, pos
);
4288 /* Add constraints to graph->lp that force all (conditional) validity
4289 * dependences to be respected and attempt to carry them.
4290 * "intra" is the sequence of coefficient constraints for intra-node edges.
4291 * "inter" is the sequence of coefficient constraints for inter-node edges.
4292 * "carry_inter" indicates whether inter-node edges should be carried or
4295 static isl_stat
add_all_constraints(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
4296 __isl_keep isl_basic_set_list
*intra
,
4297 __isl_keep isl_basic_set_list
*inter
, int carry_inter
)
4299 struct isl_add_all_constraints_data data
= { ctx
, graph
, carry_inter
};
4302 if (isl_basic_set_list_foreach(intra
, &lp_add_intra
, &data
) < 0)
4303 return isl_stat_error
;
4304 if (isl_basic_set_list_foreach(inter
, &lp_add_inter
, &data
) < 0)
4305 return isl_stat_error
;
4309 /* Internal data structure for count_all_constraints
4310 * for keeping track of the number of equality and inequality constraints.
4312 struct isl_sched_count
{
4317 /* Add the number of equality and inequality constraints of "bset"
4318 * to data->n_eq and data->n_ineq.
4320 static isl_stat
bset_update_count(__isl_take isl_basic_set
*bset
, void *user
)
4322 struct isl_sched_count
*data
= user
;
4324 return update_count(bset
, 1, &data
->n_eq
, &data
->n_ineq
);
4327 /* Count the number of equality and inequality constraints
4328 * that will be added to the carry_lp problem.
4329 * We count each edge exactly once.
4330 * "intra" is the sequence of coefficient constraints for intra-node edges.
4331 * "inter" is the sequence of coefficient constraints for inter-node edges.
4333 static isl_stat
count_all_constraints(__isl_keep isl_basic_set_list
*intra
,
4334 __isl_keep isl_basic_set_list
*inter
, int *n_eq
, int *n_ineq
)
4336 struct isl_sched_count data
;
4338 data
.n_eq
= data
.n_ineq
= 0;
4339 if (isl_basic_set_list_foreach(inter
, &bset_update_count
, &data
) < 0)
4340 return isl_stat_error
;
4341 if (isl_basic_set_list_foreach(intra
, &bset_update_count
, &data
) < 0)
4342 return isl_stat_error
;
4345 *n_ineq
= data
.n_ineq
;
4350 /* Construct an LP problem for finding schedule coefficients
4351 * such that the schedule carries as many validity dependences as possible.
4352 * In particular, for each dependence i, we bound the dependence distance
4353 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
4354 * of all e_i's. Dependences with e_i = 0 in the solution are simply
4355 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
4356 * "intra" is the sequence of coefficient constraints for intra-node edges.
4357 * "inter" is the sequence of coefficient constraints for inter-node edges.
4358 * "n_edge" is the total number of edges.
4359 * "carry_inter" indicates whether inter-node edges should be carried or
4360 * only respected. That is, if "carry_inter" is not set, then
4361 * no e_i variables are introduced for the inter-node edges.
4363 * All variables of the LP are non-negative. The actual coefficients
4364 * may be negative, so each coefficient is represented as the difference
4365 * of two non-negative variables. The negative part always appears
4366 * immediately before the positive part.
4367 * Other than that, the variables have the following order
4369 * - sum of (1 - e_i) over all edges
4370 * - sum of all c_n coefficients
4371 * (unconstrained when computing non-parametric schedules)
4372 * - sum of positive and negative parts of all c_x coefficients
4376 * - positive and negative parts of c_i_x, in opposite order
4377 * - c_i_n (if parametric)
4380 * The constraints are those from the (validity) edges plus three equalities
4381 * to express the sums and n_edge inequalities to express e_i <= 1.
4383 static isl_stat
setup_carry_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
4384 int n_edge
, __isl_keep isl_basic_set_list
*intra
,
4385 __isl_keep isl_basic_set_list
*inter
, int carry_inter
)
4394 for (i
= 0; i
< graph
->n
; ++i
) {
4395 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
4396 node
->start
= total
;
4397 total
+= 1 + node
->nparam
+ 2 * node
->nvar
;
4400 if (count_all_constraints(intra
, inter
, &n_eq
, &n_ineq
) < 0)
4401 return isl_stat_error
;
4403 dim
= isl_space_set_alloc(ctx
, 0, total
);
4404 isl_basic_set_free(graph
->lp
);
4407 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
4408 graph
->lp
= isl_basic_set_set_rational(graph
->lp
);
4410 k
= isl_basic_set_alloc_equality(graph
->lp
);
4412 return isl_stat_error
;
4413 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
4414 isl_int_set_si(graph
->lp
->eq
[k
][0], -n_edge
);
4415 isl_int_set_si(graph
->lp
->eq
[k
][1], 1);
4416 for (i
= 0; i
< n_edge
; ++i
)
4417 isl_int_set_si(graph
->lp
->eq
[k
][4 + i
], 1);
4419 if (add_param_sum_constraint(graph
, 1) < 0)
4420 return isl_stat_error
;
4421 if (add_var_sum_constraint(graph
, 2) < 0)
4422 return isl_stat_error
;
4424 for (i
= 0; i
< n_edge
; ++i
) {
4425 k
= isl_basic_set_alloc_inequality(graph
->lp
);
4427 return isl_stat_error
;
4428 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
4429 isl_int_set_si(graph
->lp
->ineq
[k
][4 + i
], -1);
4430 isl_int_set_si(graph
->lp
->ineq
[k
][0], 1);
4433 if (add_all_constraints(ctx
, graph
, intra
, inter
, carry_inter
) < 0)
4434 return isl_stat_error
;
4439 static __isl_give isl_schedule_node
*compute_component_schedule(
4440 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
4443 /* If the schedule_split_scaled option is set and if the linear
4444 * parts of the scheduling rows for all nodes in the graphs have
4445 * a non-trivial common divisor, then remove this
4446 * common divisor from the linear part.
4447 * Otherwise, insert a band node directly and continue with
4448 * the construction of the schedule.
4450 * If a non-trivial common divisor is found, then
4451 * the linear part is reduced and the remainder is ignored.
4452 * The pieces of the graph that are assigned different remainders
4453 * form (groups of) strongly connected components within
4454 * the scaled down band. If needed, they can therefore
4455 * be ordered along this remainder in a sequence node.
4456 * However, this ordering is not enforced here in order to allow
4457 * the scheduler to combine some of the strongly connected components.
4459 static __isl_give isl_schedule_node
*split_scaled(
4460 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4471 ctx
= isl_schedule_node_get_ctx(node
);
4472 if (!ctx
->opt
->schedule_split_scaled
)
4473 return compute_next_band(node
, graph
, 0);
4475 return compute_next_band(node
, graph
, 0);
4476 n_row
= isl_mat_rows(graph
->node
[0].sched
);
4478 return isl_schedule_node_free(node
);
4481 isl_int_init(gcd_i
);
4483 isl_int_set_si(gcd
, 0);
4487 for (i
= 0; i
< graph
->n
; ++i
) {
4488 struct isl_sched_node
*node
= &graph
->node
[i
];
4489 isl_size cols
= isl_mat_cols(node
->sched
);
4493 isl_seq_gcd(node
->sched
->row
[row
] + 1, cols
- 1, &gcd_i
);
4494 isl_int_gcd(gcd
, gcd
, gcd_i
);
4497 isl_int_clear(gcd_i
);
4501 if (isl_int_cmp_si(gcd
, 1) <= 0) {
4503 return compute_next_band(node
, graph
, 0);
4506 for (i
= 0; i
< graph
->n
; ++i
) {
4507 struct isl_sched_node
*node
= &graph
->node
[i
];
4509 isl_int_fdiv_q(node
->sched
->row
[row
][0],
4510 node
->sched
->row
[row
][0], gcd
);
4511 isl_int_mul(node
->sched
->row
[row
][0],
4512 node
->sched
->row
[row
][0], gcd
);
4513 node
->sched
= isl_mat_scale_down_row(node
->sched
, row
, gcd
);
4520 return compute_next_band(node
, graph
, 0);
4523 return isl_schedule_node_free(node
);
4526 /* Is the schedule row "sol" trivial on node "node"?
4527 * That is, is the solution zero on the dimensions linearly independent of
4528 * the previously found solutions?
4529 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
4531 * Each coefficient is represented as the difference between
4532 * two non-negative values in "sol".
4533 * We construct the schedule row s and check if it is linearly
4534 * independent of previously computed schedule rows
4535 * by computing T s, with T the linear combinations that are zero
4536 * on linearly dependent schedule rows.
4537 * If the result consists of all zeros, then the solution is trivial.
4539 static int is_trivial(struct isl_sched_node
*node
, __isl_keep isl_vec
*sol
)
4546 if (node
->nvar
== node
->rank
)
4549 node_sol
= extract_var_coef(node
, sol
);
4550 node_sol
= isl_mat_vec_product(isl_mat_copy(node
->indep
), node_sol
);
4554 trivial
= isl_seq_first_non_zero(node_sol
->el
,
4555 node
->nvar
- node
->rank
) == -1;
4557 isl_vec_free(node_sol
);
4562 /* Is the schedule row "sol" trivial on any node where it should
4564 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
4566 static int is_any_trivial(struct isl_sched_graph
*graph
,
4567 __isl_keep isl_vec
*sol
)
4571 for (i
= 0; i
< graph
->n
; ++i
) {
4572 struct isl_sched_node
*node
= &graph
->node
[i
];
4575 if (!needs_row(graph
, node
))
4577 trivial
= is_trivial(node
, sol
);
4578 if (trivial
< 0 || trivial
)
4585 /* Does the schedule represented by "sol" perform loop coalescing on "node"?
4586 * If so, return the position of the coalesced dimension.
4587 * Otherwise, return node->nvar or -1 on error.
4589 * In particular, look for pairs of coefficients c_i and c_j such that
4590 * |c_j/c_i| > ceil(size_i/2), i.e., |c_j| > |c_i * ceil(size_i/2)|.
4591 * If any such pair is found, then return i.
4592 * If size_i is infinity, then no check on c_i needs to be performed.
4594 static int find_node_coalescing(struct isl_sched_node
*node
,
4595 __isl_keep isl_vec
*sol
)
4601 if (node
->nvar
<= 1)
4604 csol
= extract_var_coef(node
, sol
);
4608 for (i
= 0; i
< node
->nvar
; ++i
) {
4611 if (isl_int_is_zero(csol
->el
[i
]))
4613 v
= isl_multi_val_get_val(node
->sizes
, i
);
4616 if (!isl_val_is_int(v
)) {
4620 v
= isl_val_div_ui(v
, 2);
4621 v
= isl_val_ceil(v
);
4624 isl_int_mul(max
, v
->n
, csol
->el
[i
]);
4627 for (j
= 0; j
< node
->nvar
; ++j
) {
4630 if (isl_int_abs_gt(csol
->el
[j
], max
))
4646 /* Force the schedule coefficient at position "pos" of "node" to be zero
4648 * The coefficient is encoded as the difference between two non-negative
4649 * variables. Force these two variables to have the same value.
4651 static __isl_give isl_tab_lexmin
*zero_out_node_coef(
4652 __isl_take isl_tab_lexmin
*tl
, struct isl_sched_node
*node
, int pos
)
4658 ctx
= isl_space_get_ctx(node
->space
);
4659 dim
= isl_tab_lexmin_dim(tl
);
4661 return isl_tab_lexmin_free(tl
);
4662 eq
= isl_vec_alloc(ctx
, 1 + dim
);
4663 eq
= isl_vec_clr(eq
);
4665 return isl_tab_lexmin_free(tl
);
4667 pos
= 1 + node_var_coef_pos(node
, pos
);
4668 isl_int_set_si(eq
->el
[pos
], 1);
4669 isl_int_set_si(eq
->el
[pos
+ 1], -1);
4670 tl
= isl_tab_lexmin_add_eq(tl
, eq
->el
);
4676 /* Return the lexicographically smallest rational point in the basic set
4677 * from which "tl" was constructed, double checking that this input set
4680 static __isl_give isl_vec
*non_empty_solution(__isl_keep isl_tab_lexmin
*tl
)
4684 sol
= isl_tab_lexmin_get_solution(tl
);
4688 isl_die(isl_vec_get_ctx(sol
), isl_error_internal
,
4689 "error in schedule construction",
4690 return isl_vec_free(sol
));
4694 /* Does the solution "sol" of the LP problem constructed by setup_carry_lp
4695 * carry any of the "n_edge" groups of dependences?
4696 * The value in the first position is the sum of (1 - e_i) over all "n_edge"
4697 * edges, with 0 <= e_i <= 1 equal to 1 when the dependences represented
4698 * by the edge are carried by the solution.
4699 * If the sum of the (1 - e_i) is smaller than "n_edge" then at least
4700 * one of those is carried.
4702 * Note that despite the fact that the problem is solved using a rational
4703 * solver, the solution is guaranteed to be integral.
4704 * Specifically, the dependence distance lower bounds e_i (and therefore
4705 * also their sum) are integers. See Lemma 5 of [1].
4707 * Any potential denominator of the sum is cleared by this function.
4708 * The denominator is not relevant for any of the other elements
4711 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4712 * Problem, Part II: Multi-Dimensional Time.
4713 * In Intl. Journal of Parallel Programming, 1992.
4715 static int carries_dependences(__isl_keep isl_vec
*sol
, int n_edge
)
4717 isl_int_divexact(sol
->el
[1], sol
->el
[1], sol
->el
[0]);
4718 isl_int_set_si(sol
->el
[0], 1);
4719 return isl_int_cmp_si(sol
->el
[1], n_edge
) < 0;
4722 /* Return the lexicographically smallest rational point in "lp",
4723 * assuming that all variables are non-negative and performing some
4724 * additional sanity checks.
4725 * If "want_integral" is set, then compute the lexicographically smallest
4726 * integer point instead.
4727 * In particular, "lp" should not be empty by construction.
4728 * Double check that this is the case.
4729 * If dependences are not carried for any of the "n_edge" edges,
4730 * then return an empty vector.
4732 * If the schedule_treat_coalescing option is set and
4733 * if the computed schedule performs loop coalescing on a given node,
4734 * i.e., if it is of the form
4736 * c_i i + c_j j + ...
4738 * with |c_j/c_i| >= size_i, then force the coefficient c_i to be zero
4739 * to cut out this solution. Repeat this process until no more loop
4740 * coalescing occurs or until no more dependences can be carried.
4741 * In the latter case, revert to the previously computed solution.
4743 * If the caller requests an integral solution and if coalescing should
4744 * be treated, then perform the coalescing treatment first as
4745 * an integral solution computed before coalescing treatment
4746 * would carry the same number of edges and would therefore probably
4747 * also be coalescing.
4749 * To allow the coalescing treatment to be performed first,
4750 * the initial solution is allowed to be rational and it is only
4751 * cut out (if needed) in the next iteration, if no coalescing measures
4754 static __isl_give isl_vec
*non_neg_lexmin(struct isl_sched_graph
*graph
,
4755 __isl_take isl_basic_set
*lp
, int n_edge
, int want_integral
)
4760 isl_vec
*sol
= NULL
, *prev
;
4761 int treat_coalescing
;
4766 ctx
= isl_basic_set_get_ctx(lp
);
4767 treat_coalescing
= isl_options_get_schedule_treat_coalescing(ctx
);
4768 tl
= isl_tab_lexmin_from_basic_set(lp
);
4776 tl
= isl_tab_lexmin_cut_to_integer(tl
);
4778 sol
= non_empty_solution(tl
);
4782 integral
= isl_int_is_one(sol
->el
[0]);
4783 if (!carries_dependences(sol
, n_edge
)) {
4785 prev
= isl_vec_alloc(ctx
, 0);
4790 prev
= isl_vec_free(prev
);
4791 cut
= want_integral
&& !integral
;
4794 if (!treat_coalescing
)
4796 for (i
= 0; i
< graph
->n
; ++i
) {
4797 struct isl_sched_node
*node
= &graph
->node
[i
];
4799 pos
= find_node_coalescing(node
, sol
);
4802 if (pos
< node
->nvar
)
4807 tl
= zero_out_node_coef(tl
, &graph
->node
[i
], pos
);
4810 } while (try_again
);
4812 isl_tab_lexmin_free(tl
);
4816 isl_tab_lexmin_free(tl
);
4822 /* If "edge" is an edge from a node to itself, then add the corresponding
4823 * dependence relation to "umap".
4824 * If "node" has been compressed, then the dependence relation
4825 * is also compressed first.
4827 static __isl_give isl_union_map
*add_intra(__isl_take isl_union_map
*umap
,
4828 struct isl_sched_edge
*edge
)
4831 struct isl_sched_node
*node
= edge
->src
;
4833 if (edge
->src
!= edge
->dst
)
4836 map
= isl_map_copy(edge
->map
);
4837 map
= compress(map
, node
, node
);
4838 umap
= isl_union_map_add_map(umap
, map
);
4842 /* If "edge" is an edge from a node to another node, then add the corresponding
4843 * dependence relation to "umap".
4844 * If the source or destination nodes of "edge" have been compressed,
4845 * then the dependence relation is also compressed first.
4847 static __isl_give isl_union_map
*add_inter(__isl_take isl_union_map
*umap
,
4848 struct isl_sched_edge
*edge
)
4852 if (edge
->src
== edge
->dst
)
4855 map
= isl_map_copy(edge
->map
);
4856 map
= compress(map
, edge
->src
, edge
->dst
);
4857 umap
= isl_union_map_add_map(umap
, map
);
4861 /* Internal data structure used by union_drop_coalescing_constraints
4862 * to collect bounds on all relevant statements.
4864 * "graph" is the schedule constraint graph for which an LP problem
4865 * is being constructed.
4866 * "bounds" collects the bounds.
4868 struct isl_collect_bounds_data
{
4870 struct isl_sched_graph
*graph
;
4871 isl_union_set
*bounds
;
4874 /* Add the size bounds for the node with instance deltas in "set"
4877 static isl_stat
collect_bounds(__isl_take isl_set
*set
, void *user
)
4879 struct isl_collect_bounds_data
*data
= user
;
4880 struct isl_sched_node
*node
;
4884 space
= isl_set_get_space(set
);
4887 node
= graph_find_compressed_node(data
->ctx
, data
->graph
, space
);
4888 isl_space_free(space
);
4890 bounds
= isl_set_from_basic_set(get_size_bounds(node
));
4891 data
->bounds
= isl_union_set_add_set(data
->bounds
, bounds
);
4896 /* Drop some constraints from "delta" that could be exploited
4897 * to construct loop coalescing schedules.
4898 * In particular, drop those constraint that bound the difference
4899 * to the size of the domain.
4900 * Do this for each set/node in "delta" separately.
4901 * The parameters are assumed to have been projected out by the caller.
4903 static __isl_give isl_union_set
*union_drop_coalescing_constraints(isl_ctx
*ctx
,
4904 struct isl_sched_graph
*graph
, __isl_take isl_union_set
*delta
)
4906 struct isl_collect_bounds_data data
= { ctx
, graph
};
4908 data
.bounds
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
4909 if (isl_union_set_foreach_set(delta
, &collect_bounds
, &data
) < 0)
4910 data
.bounds
= isl_union_set_free(data
.bounds
);
4911 delta
= isl_union_set_plain_gist(delta
, data
.bounds
);
4916 /* Given a non-trivial lineality space "lineality", add the corresponding
4917 * universe set to data->mask and add a map from elements to
4918 * other elements along the lines in "lineality" to data->equivalent.
4919 * If this is the first time this function gets called
4920 * (data->any_non_trivial is still false), then set data->any_non_trivial and
4921 * initialize data->mask and data->equivalent.
4923 * In particular, if the lineality space is defined by equality constraints
4927 * then construct an affine mapping
4931 * and compute the equivalence relation of having the same image under f:
4933 * { x -> x' : E x = E x' }
4935 static isl_stat
add_non_trivial_lineality(__isl_take isl_basic_set
*lineality
,
4936 struct isl_exploit_lineality_data
*data
)
4942 isl_multi_pw_aff
*mpa
;
4946 if (isl_basic_set_check_no_locals(lineality
) < 0)
4949 space
= isl_basic_set_get_space(lineality
);
4950 if (!data
->any_non_trivial
) {
4951 data
->equivalent
= isl_union_map_empty(isl_space_copy(space
));
4952 data
->mask
= isl_union_set_empty(isl_space_copy(space
));
4954 data
->any_non_trivial
= isl_bool_true
;
4956 univ
= isl_set_universe(isl_space_copy(space
));
4957 data
->mask
= isl_union_set_add_set(data
->mask
, univ
);
4959 eq
= isl_basic_set_extract_equalities(lineality
);
4960 n
= isl_mat_rows(eq
);
4962 space
= isl_space_free(space
);
4963 eq
= isl_mat_insert_zero_rows(eq
, 0, 1);
4964 eq
= isl_mat_set_element_si(eq
, 0, 0, 1);
4965 space
= isl_space_from_domain(space
);
4966 space
= isl_space_add_dims(space
, isl_dim_out
, n
);
4967 ma
= isl_multi_aff_from_aff_mat(space
, eq
);
4968 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
4969 map
= isl_multi_pw_aff_eq_map(mpa
, isl_multi_pw_aff_copy(mpa
));
4970 data
->equivalent
= isl_union_map_add_map(data
->equivalent
, map
);
4972 isl_basic_set_free(lineality
);
4975 isl_basic_set_free(lineality
);
4976 return isl_stat_error
;
4979 /* Check if the lineality space "set" is non-trivial (i.e., is not just
4980 * the origin or, in other words, satisfies a number of equality constraints
4981 * that is smaller than the dimension of the set).
4982 * If so, extend data->mask and data->equivalent accordingly.
4984 * The input should not have any local variables already, but
4985 * isl_set_remove_divs is called to make sure it does not.
4987 static isl_stat
add_lineality(__isl_take isl_set
*set
, void *user
)
4989 struct isl_exploit_lineality_data
*data
= user
;
4990 isl_basic_set
*hull
;
4994 set
= isl_set_remove_divs(set
);
4995 hull
= isl_set_unshifted_simple_hull(set
);
4996 dim
= isl_basic_set_dim(hull
, isl_dim_set
);
4997 n_eq
= isl_basic_set_n_equality(hull
);
5001 return add_non_trivial_lineality(hull
, data
);
5002 isl_basic_set_free(hull
);
5005 isl_basic_set_free(hull
);
5006 return isl_stat_error
;
5009 /* Check if the difference set on intra-node schedule constraints "intra"
5010 * has any non-trivial lineality space.
5011 * If so, then extend the difference set to a difference set
5012 * on equivalent elements. That is, if "intra" is
5014 * { y - x : (x,y) \in V }
5016 * and elements are equivalent if they have the same image under f,
5019 * { y' - x' : (x,y) \in V and f(x) = f(x') and f(y) = f(y') }
5021 * or, since f is linear,
5023 * { y' - x' : (x,y) \in V and f(y - x) = f(y' - x') }
5025 * The results of the search for non-trivial lineality spaces is stored
5028 static __isl_give isl_union_set
*exploit_intra_lineality(
5029 __isl_take isl_union_set
*intra
,
5030 struct isl_exploit_lineality_data
*data
)
5032 isl_union_set
*lineality
;
5033 isl_union_set
*uset
;
5035 data
->any_non_trivial
= isl_bool_false
;
5036 lineality
= isl_union_set_copy(intra
);
5037 lineality
= isl_union_set_combined_lineality_space(lineality
);
5038 if (isl_union_set_foreach_set(lineality
, &add_lineality
, data
) < 0)
5039 data
->any_non_trivial
= isl_bool_error
;
5040 isl_union_set_free(lineality
);
5042 if (data
->any_non_trivial
< 0)
5043 return isl_union_set_free(intra
);
5044 if (!data
->any_non_trivial
)
5047 uset
= isl_union_set_copy(intra
);
5048 intra
= isl_union_set_subtract(intra
, isl_union_set_copy(data
->mask
));
5049 uset
= isl_union_set_apply(uset
, isl_union_map_copy(data
->equivalent
));
5050 intra
= isl_union_set_union(intra
, uset
);
5052 intra
= isl_union_set_remove_divs(intra
);
5057 /* If the difference set on intra-node schedule constraints was found to have
5058 * any non-trivial lineality space by exploit_intra_lineality,
5059 * as recorded in "data", then extend the inter-node
5060 * schedule constraints "inter" to schedule constraints on equivalent elements.
5061 * That is, if "inter" is V and
5062 * elements are equivalent if they have the same image under f, then return
5064 * { (x', y') : (x,y) \in V and f(x) = f(x') and f(y) = f(y') }
5066 static __isl_give isl_union_map
*exploit_inter_lineality(
5067 __isl_take isl_union_map
*inter
,
5068 struct isl_exploit_lineality_data
*data
)
5070 isl_union_map
*umap
;
5072 if (data
->any_non_trivial
< 0)
5073 return isl_union_map_free(inter
);
5074 if (!data
->any_non_trivial
)
5077 umap
= isl_union_map_copy(inter
);
5078 inter
= isl_union_map_subtract_range(inter
,
5079 isl_union_set_copy(data
->mask
));
5080 umap
= isl_union_map_apply_range(umap
,
5081 isl_union_map_copy(data
->equivalent
));
5082 inter
= isl_union_map_union(inter
, umap
);
5083 umap
= isl_union_map_copy(inter
);
5084 inter
= isl_union_map_subtract_domain(inter
,
5085 isl_union_set_copy(data
->mask
));
5086 umap
= isl_union_map_apply_range(isl_union_map_copy(data
->equivalent
),
5088 inter
= isl_union_map_union(inter
, umap
);
5090 inter
= isl_union_map_remove_divs(inter
);
5095 /* For each (conditional) validity edge in "graph",
5096 * add the corresponding dependence relation using "add"
5097 * to a collection of dependence relations and return the result.
5098 * If "coincidence" is set, then coincidence edges are considered as well.
5100 static __isl_give isl_union_map
*collect_validity(struct isl_sched_graph
*graph
,
5101 __isl_give isl_union_map
*(*add
)(__isl_take isl_union_map
*umap
,
5102 struct isl_sched_edge
*edge
), int coincidence
)
5106 isl_union_map
*umap
;
5108 space
= isl_space_copy(graph
->node
[0].space
);
5109 umap
= isl_union_map_empty(space
);
5111 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5112 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
5114 if (!is_any_validity(edge
) &&
5115 (!coincidence
|| !is_coincidence(edge
)))
5118 umap
= add(umap
, edge
);
5124 /* For each dependence relation on a (conditional) validity edge
5125 * from a node to itself,
5126 * construct the set of coefficients of valid constraints for elements
5127 * in that dependence relation and collect the results.
5128 * If "coincidence" is set, then coincidence edges are considered as well.
5130 * In particular, for each dependence relation R, constraints
5131 * on coefficients (c_0, c_x) are constructed such that
5133 * c_0 + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
5135 * If the schedule_treat_coalescing option is set, then some constraints
5136 * that could be exploited to construct coalescing schedules
5137 * are removed before the dual is computed, but after the parameters
5138 * have been projected out.
5139 * The entire computation is essentially the same as that performed
5140 * by intra_coefficients, except that it operates on multiple
5141 * edges together and that the parameters are always projected out.
5143 * Additionally, exploit any non-trivial lineality space
5144 * in the difference set after removing coalescing constraints and
5145 * store the results of the non-trivial lineality space detection in "data".
5146 * The procedure is currently run unconditionally, but it is unlikely
5147 * to find any non-trivial lineality spaces if no coalescing constraints
5148 * have been removed.
5150 * Note that if a dependence relation is a union of basic maps,
5151 * then each basic map needs to be treated individually as it may only
5152 * be possible to carry the dependences expressed by some of those
5153 * basic maps and not all of them.
5154 * The collected validity constraints are therefore not coalesced and
5155 * it is assumed that they are not coalesced automatically.
5156 * Duplicate basic maps can be removed, however.
5157 * In particular, if the same basic map appears as a disjunct
5158 * in multiple edges, then it only needs to be carried once.
5160 static __isl_give isl_basic_set_list
*collect_intra_validity(isl_ctx
*ctx
,
5161 struct isl_sched_graph
*graph
, int coincidence
,
5162 struct isl_exploit_lineality_data
*data
)
5164 isl_union_map
*intra
;
5165 isl_union_set
*delta
;
5166 isl_basic_set_list
*list
;
5168 intra
= collect_validity(graph
, &add_intra
, coincidence
);
5169 delta
= isl_union_map_deltas(intra
);
5170 delta
= isl_union_set_project_out_all_params(delta
);
5171 delta
= isl_union_set_remove_divs(delta
);
5172 if (isl_options_get_schedule_treat_coalescing(ctx
))
5173 delta
= union_drop_coalescing_constraints(ctx
, graph
, delta
);
5174 delta
= exploit_intra_lineality(delta
, data
);
5175 list
= isl_union_set_get_basic_set_list(delta
);
5176 isl_union_set_free(delta
);
5178 return isl_basic_set_list_coefficients(list
);
5181 /* For each dependence relation on a (conditional) validity edge
5182 * from a node to some other node,
5183 * construct the set of coefficients of valid constraints for elements
5184 * in that dependence relation and collect the results.
5185 * If "coincidence" is set, then coincidence edges are considered as well.
5187 * In particular, for each dependence relation R, constraints
5188 * on coefficients (c_0, c_n, c_x, c_y) are constructed such that
5190 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
5192 * This computation is essentially the same as that performed
5193 * by inter_coefficients, except that it operates on multiple
5196 * Additionally, exploit any non-trivial lineality space
5197 * that may have been discovered by collect_intra_validity
5198 * (as stored in "data").
5200 * Note that if a dependence relation is a union of basic maps,
5201 * then each basic map needs to be treated individually as it may only
5202 * be possible to carry the dependences expressed by some of those
5203 * basic maps and not all of them.
5204 * The collected validity constraints are therefore not coalesced and
5205 * it is assumed that they are not coalesced automatically.
5206 * Duplicate basic maps can be removed, however.
5207 * In particular, if the same basic map appears as a disjunct
5208 * in multiple edges, then it only needs to be carried once.
5210 static __isl_give isl_basic_set_list
*collect_inter_validity(
5211 struct isl_sched_graph
*graph
, int coincidence
,
5212 struct isl_exploit_lineality_data
*data
)
5214 isl_union_map
*inter
;
5215 isl_union_set
*wrap
;
5216 isl_basic_set_list
*list
;
5218 inter
= collect_validity(graph
, &add_inter
, coincidence
);
5219 inter
= exploit_inter_lineality(inter
, data
);
5220 inter
= isl_union_map_remove_divs(inter
);
5221 wrap
= isl_union_map_wrap(inter
);
5222 list
= isl_union_set_get_basic_set_list(wrap
);
5223 isl_union_set_free(wrap
);
5224 return isl_basic_set_list_coefficients(list
);
5227 /* Construct an LP problem for finding schedule coefficients
5228 * such that the schedule carries as many of the "n_edge" groups of
5229 * dependences as possible based on the corresponding coefficient
5230 * constraints and return the lexicographically smallest non-trivial solution.
5231 * "intra" is the sequence of coefficient constraints for intra-node edges.
5232 * "inter" is the sequence of coefficient constraints for inter-node edges.
5233 * If "want_integral" is set, then compute an integral solution
5234 * for the coefficients rather than using the numerators
5235 * of a rational solution.
5236 * "carry_inter" indicates whether inter-node edges should be carried or
5239 * If none of the "n_edge" groups can be carried
5240 * then return an empty vector.
5242 static __isl_give isl_vec
*compute_carrying_sol_coef(isl_ctx
*ctx
,
5243 struct isl_sched_graph
*graph
, int n_edge
,
5244 __isl_keep isl_basic_set_list
*intra
,
5245 __isl_keep isl_basic_set_list
*inter
, int want_integral
,
5250 if (setup_carry_lp(ctx
, graph
, n_edge
, intra
, inter
, carry_inter
) < 0)
5253 lp
= isl_basic_set_copy(graph
->lp
);
5254 return non_neg_lexmin(graph
, lp
, n_edge
, want_integral
);
5257 /* Construct an LP problem for finding schedule coefficients
5258 * such that the schedule carries as many of the validity dependences
5260 * return the lexicographically smallest non-trivial solution.
5261 * If "fallback" is set, then the carrying is performed as a fallback
5262 * for the Pluto-like scheduler.
5263 * If "coincidence" is set, then try and carry coincidence edges as well.
5265 * The variable "n_edge" stores the number of groups that should be carried.
5266 * If none of the "n_edge" groups can be carried
5267 * then return an empty vector.
5268 * If, moreover, "n_edge" is zero, then the LP problem does not even
5269 * need to be constructed.
5271 * If a fallback solution is being computed, then compute an integral solution
5272 * for the coefficients rather than using the numerators
5273 * of a rational solution.
5275 * If a fallback solution is being computed, if there are any intra-node
5276 * dependences, and if requested by the user, then first try
5277 * to only carry those intra-node dependences.
5278 * If this fails to carry any dependences, then try again
5279 * with the inter-node dependences included.
5281 static __isl_give isl_vec
*compute_carrying_sol(isl_ctx
*ctx
,
5282 struct isl_sched_graph
*graph
, int fallback
, int coincidence
)
5284 isl_size n_intra
, n_inter
;
5286 struct isl_carry carry
= { 0 };
5289 carry
.intra
= collect_intra_validity(ctx
, graph
, coincidence
,
5291 carry
.inter
= collect_inter_validity(graph
, coincidence
,
5293 n_intra
= isl_basic_set_list_n_basic_set(carry
.intra
);
5294 n_inter
= isl_basic_set_list_n_basic_set(carry
.inter
);
5295 if (n_intra
< 0 || n_inter
< 0)
5298 if (fallback
&& n_intra
> 0 &&
5299 isl_options_get_schedule_carry_self_first(ctx
)) {
5300 sol
= compute_carrying_sol_coef(ctx
, graph
, n_intra
,
5301 carry
.intra
, carry
.inter
, fallback
, 0);
5302 if (!sol
|| sol
->size
!= 0 || n_inter
== 0) {
5303 isl_carry_clear(&carry
);
5309 n_edge
= n_intra
+ n_inter
;
5311 isl_carry_clear(&carry
);
5312 return isl_vec_alloc(ctx
, 0);
5315 sol
= compute_carrying_sol_coef(ctx
, graph
, n_edge
,
5316 carry
.intra
, carry
.inter
, fallback
, 1);
5317 isl_carry_clear(&carry
);
5320 isl_carry_clear(&carry
);
5324 /* Construct a schedule row for each node such that as many validity dependences
5325 * as possible are carried and then continue with the next band.
5326 * If "fallback" is set, then the carrying is performed as a fallback
5327 * for the Pluto-like scheduler.
5328 * If "coincidence" is set, then try and carry coincidence edges as well.
5330 * If there are no validity dependences, then no dependence can be carried and
5331 * the procedure is guaranteed to fail. If there is more than one component,
5332 * then try computing a schedule on each component separately
5333 * to prevent or at least postpone this failure.
5335 * If a schedule row is computed, then check that dependences are carried
5336 * for at least one of the edges.
5338 * If the computed schedule row turns out to be trivial on one or
5339 * more nodes where it should not be trivial, then we throw it away
5340 * and try again on each component separately.
5342 * If there is only one component, then we accept the schedule row anyway,
5343 * but we do not consider it as a complete row and therefore do not
5344 * increment graph->n_row. Note that the ranks of the nodes that
5345 * do get a non-trivial schedule part will get updated regardless and
5346 * graph->maxvar is computed based on these ranks. The test for
5347 * whether more schedule rows are required in compute_schedule_wcc
5348 * is therefore not affected.
5350 * Insert a band corresponding to the schedule row at position "node"
5351 * of the schedule tree and continue with the construction of the schedule.
5352 * This insertion and the continued construction is performed by split_scaled
5353 * after optionally checking for non-trivial common divisors.
5355 static __isl_give isl_schedule_node
*carry(__isl_take isl_schedule_node
*node
,
5356 struct isl_sched_graph
*graph
, int fallback
, int coincidence
)
5365 ctx
= isl_schedule_node_get_ctx(node
);
5366 sol
= compute_carrying_sol(ctx
, graph
, fallback
, coincidence
);
5368 return isl_schedule_node_free(node
);
5369 if (sol
->size
== 0) {
5372 return compute_component_schedule(node
, graph
, 1);
5373 isl_die(ctx
, isl_error_unknown
, "unable to carry dependences",
5374 return isl_schedule_node_free(node
));
5377 trivial
= is_any_trivial(graph
, sol
);
5379 sol
= isl_vec_free(sol
);
5380 } else if (trivial
&& graph
->scc
> 1) {
5382 return compute_component_schedule(node
, graph
, 1);
5385 if (update_schedule(graph
, sol
, 0) < 0)
5386 return isl_schedule_node_free(node
);
5390 return split_scaled(node
, graph
);
5393 /* Construct a schedule row for each node such that as many validity dependences
5394 * as possible are carried and then continue with the next band.
5395 * Do so as a fallback for the Pluto-like scheduler.
5396 * If "coincidence" is set, then try and carry coincidence edges as well.
5398 static __isl_give isl_schedule_node
*carry_fallback(
5399 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
5402 return carry(node
, graph
, 1, coincidence
);
5405 /* Construct a schedule row for each node such that as many validity dependences
5406 * as possible are carried and then continue with the next band.
5407 * Do so for the case where the Feautrier scheduler was selected
5410 static __isl_give isl_schedule_node
*carry_feautrier(
5411 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
5413 return carry(node
, graph
, 0, 0);
5416 /* Construct a schedule row for each node such that as many validity dependences
5417 * as possible are carried and then continue with the next band.
5418 * Do so as a fallback for the Pluto-like scheduler.
5420 static __isl_give isl_schedule_node
*carry_dependences(
5421 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
5423 return carry_fallback(node
, graph
, 0);
5426 /* Construct a schedule row for each node such that as many validity or
5427 * coincidence dependences as possible are carried and
5428 * then continue with the next band.
5429 * Do so as a fallback for the Pluto-like scheduler.
5431 static __isl_give isl_schedule_node
*carry_coincidence(
5432 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
5434 return carry_fallback(node
, graph
, 1);
5437 /* Topologically sort statements mapped to the same schedule iteration
5438 * and add insert a sequence node in front of "node"
5439 * corresponding to this order.
5440 * If "initialized" is set, then it may be assumed that compute_maxvar
5441 * has been called on the current band. Otherwise, call
5442 * compute_maxvar if and before carry_dependences gets called.
5444 * If it turns out to be impossible to sort the statements apart,
5445 * because different dependences impose different orderings
5446 * on the statements, then we extend the schedule such that
5447 * it carries at least one more dependence.
5449 static __isl_give isl_schedule_node
*sort_statements(
5450 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
5454 isl_union_set_list
*filters
;
5459 ctx
= isl_schedule_node_get_ctx(node
);
5461 isl_die(ctx
, isl_error_internal
,
5462 "graph should have at least one node",
5463 return isl_schedule_node_free(node
));
5468 if (update_edges(ctx
, graph
) < 0)
5469 return isl_schedule_node_free(node
);
5471 if (graph
->n_edge
== 0)
5474 if (detect_sccs(ctx
, graph
) < 0)
5475 return isl_schedule_node_free(node
);
5478 if (graph
->scc
< graph
->n
) {
5479 if (!initialized
&& compute_maxvar(graph
) < 0)
5480 return isl_schedule_node_free(node
);
5481 return carry_dependences(node
, graph
);
5484 filters
= extract_sccs(ctx
, graph
);
5485 node
= isl_schedule_node_insert_sequence(node
, filters
);
5490 /* Are there any (non-empty) (conditional) validity edges in the graph?
5492 static int has_validity_edges(struct isl_sched_graph
*graph
)
5496 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5499 empty
= isl_map_plain_is_empty(graph
->edge
[i
].map
);
5504 if (is_any_validity(&graph
->edge
[i
]))
5511 /* Should we apply a Feautrier step?
5512 * That is, did the user request the Feautrier algorithm and are
5513 * there any validity dependences (left)?
5515 static int need_feautrier_step(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
5517 if (ctx
->opt
->schedule_algorithm
!= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
)
5520 return has_validity_edges(graph
);
5523 /* Compute a schedule for a connected dependence graph using Feautrier's
5524 * multi-dimensional scheduling algorithm and return the updated schedule node.
5526 * The original algorithm is described in [1].
5527 * The main idea is to minimize the number of scheduling dimensions, by
5528 * trying to satisfy as many dependences as possible per scheduling dimension.
5530 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
5531 * Problem, Part II: Multi-Dimensional Time.
5532 * In Intl. Journal of Parallel Programming, 1992.
5534 static __isl_give isl_schedule_node
*compute_schedule_wcc_feautrier(
5535 isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
5537 return carry_feautrier(node
, graph
);
5540 /* Turn off the "local" bit on all (condition) edges.
5542 static void clear_local_edges(struct isl_sched_graph
*graph
)
5546 for (i
= 0; i
< graph
->n_edge
; ++i
)
5547 if (is_condition(&graph
->edge
[i
]))
5548 clear_local(&graph
->edge
[i
]);
5551 /* Does "graph" have both condition and conditional validity edges?
5553 static int need_condition_check(struct isl_sched_graph
*graph
)
5556 int any_condition
= 0;
5557 int any_conditional_validity
= 0;
5559 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5560 if (is_condition(&graph
->edge
[i
]))
5562 if (is_conditional_validity(&graph
->edge
[i
]))
5563 any_conditional_validity
= 1;
5566 return any_condition
&& any_conditional_validity
;
5569 /* Does "graph" contain any coincidence edge?
5571 static int has_any_coincidence(struct isl_sched_graph
*graph
)
5575 for (i
= 0; i
< graph
->n_edge
; ++i
)
5576 if (is_coincidence(&graph
->edge
[i
]))
5582 /* Extract the final schedule row as a map with the iteration domain
5583 * of "node" as domain.
5585 static __isl_give isl_map
*final_row(struct isl_sched_node
*node
)
5590 n_row
= isl_mat_rows(node
->sched
);
5593 ma
= node_extract_partial_schedule_multi_aff(node
, n_row
- 1, 1);
5594 return isl_map_from_multi_aff(ma
);
5597 /* Is the conditional validity dependence in the edge with index "edge_index"
5598 * violated by the latest (i.e., final) row of the schedule?
5599 * That is, is i scheduled after j
5600 * for any conditional validity dependence i -> j?
5602 static int is_violated(struct isl_sched_graph
*graph
, int edge_index
)
5604 isl_map
*src_sched
, *dst_sched
, *map
;
5605 struct isl_sched_edge
*edge
= &graph
->edge
[edge_index
];
5608 src_sched
= final_row(edge
->src
);
5609 dst_sched
= final_row(edge
->dst
);
5610 map
= isl_map_copy(edge
->map
);
5611 map
= isl_map_apply_domain(map
, src_sched
);
5612 map
= isl_map_apply_range(map
, dst_sched
);
5613 map
= isl_map_order_gt(map
, isl_dim_in
, 0, isl_dim_out
, 0);
5614 empty
= isl_map_is_empty(map
);
5623 /* Does "graph" have any satisfied condition edges that
5624 * are adjacent to the conditional validity constraint with
5625 * domain "conditional_source" and range "conditional_sink"?
5627 * A satisfied condition is one that is not local.
5628 * If a condition was forced to be local already (i.e., marked as local)
5629 * then there is no need to check if it is in fact local.
5631 * Additionally, mark all adjacent condition edges found as local.
5633 static int has_adjacent_true_conditions(struct isl_sched_graph
*graph
,
5634 __isl_keep isl_union_set
*conditional_source
,
5635 __isl_keep isl_union_set
*conditional_sink
)
5640 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5641 int adjacent
, local
;
5642 isl_union_map
*condition
;
5644 if (!is_condition(&graph
->edge
[i
]))
5646 if (is_local(&graph
->edge
[i
]))
5649 condition
= graph
->edge
[i
].tagged_condition
;
5650 adjacent
= domain_intersects(condition
, conditional_sink
);
5651 if (adjacent
>= 0 && !adjacent
)
5652 adjacent
= range_intersects(condition
,
5653 conditional_source
);
5659 set_local(&graph
->edge
[i
]);
5661 local
= is_condition_false(&graph
->edge
[i
]);
5671 /* Are there any violated conditional validity dependences with
5672 * adjacent condition dependences that are not local with respect
5673 * to the current schedule?
5674 * That is, is the conditional validity constraint violated?
5676 * Additionally, mark all those adjacent condition dependences as local.
5677 * We also mark those adjacent condition dependences that were not marked
5678 * as local before, but just happened to be local already. This ensures
5679 * that they remain local if the schedule is recomputed.
5681 * We first collect domain and range of all violated conditional validity
5682 * dependences and then check if there are any adjacent non-local
5683 * condition dependences.
5685 static int has_violated_conditional_constraint(isl_ctx
*ctx
,
5686 struct isl_sched_graph
*graph
)
5690 isl_union_set
*source
, *sink
;
5692 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
5693 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
5694 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5695 isl_union_set
*uset
;
5696 isl_union_map
*umap
;
5699 if (!is_conditional_validity(&graph
->edge
[i
]))
5702 violated
= is_violated(graph
, i
);
5710 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
5711 uset
= isl_union_map_domain(umap
);
5712 source
= isl_union_set_union(source
, uset
);
5713 source
= isl_union_set_coalesce(source
);
5715 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
5716 uset
= isl_union_map_range(umap
);
5717 sink
= isl_union_set_union(sink
, uset
);
5718 sink
= isl_union_set_coalesce(sink
);
5722 any
= has_adjacent_true_conditions(graph
, source
, sink
);
5724 isl_union_set_free(source
);
5725 isl_union_set_free(sink
);
5728 isl_union_set_free(source
);
5729 isl_union_set_free(sink
);
5733 /* Examine the current band (the rows between graph->band_start and
5734 * graph->n_total_row), deciding whether to drop it or add it to "node"
5735 * and then continue with the computation of the next band, if any.
5736 * If "initialized" is set, then it may be assumed that compute_maxvar
5737 * has been called on the current band. Otherwise, call
5738 * compute_maxvar if and before carry_dependences gets called.
5740 * The caller keeps looking for a new row as long as
5741 * graph->n_row < graph->maxvar. If the latest attempt to find
5742 * such a row failed (i.e., we still have graph->n_row < graph->maxvar),
5744 * - split between SCCs and start over (assuming we found an interesting
5745 * pair of SCCs between which to split)
5746 * - continue with the next band (assuming the current band has at least
5748 * - if there is more than one SCC left, then split along all SCCs
5749 * - if outer coincidence needs to be enforced, then try to carry as many
5750 * validity or coincidence dependences as possible and
5751 * continue with the next band
5752 * - try to carry as many validity dependences as possible and
5753 * continue with the next band
5754 * In each case, we first insert a band node in the schedule tree
5755 * if any rows have been computed.
5757 * If the caller managed to complete the schedule and the current band
5758 * is empty, then finish off by topologically
5759 * sorting the statements based on the remaining dependences.
5760 * If, on the other hand, the current band has at least one row,
5761 * then continue with the next band. Note that this next band
5762 * will necessarily be empty, but the graph may still be split up
5763 * into weakly connected components before arriving back here.
5765 static __isl_give isl_schedule_node
*compute_schedule_finish_band(
5766 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
5774 empty
= graph
->n_total_row
== graph
->band_start
;
5775 if (graph
->n_row
< graph
->maxvar
) {
5778 ctx
= isl_schedule_node_get_ctx(node
);
5779 if (!ctx
->opt
->schedule_maximize_band_depth
&& !empty
)
5780 return compute_next_band(node
, graph
, 1);
5781 if (graph
->src_scc
>= 0)
5782 return compute_split_schedule(node
, graph
);
5784 return compute_next_band(node
, graph
, 1);
5786 return compute_component_schedule(node
, graph
, 1);
5787 if (!initialized
&& compute_maxvar(graph
) < 0)
5788 return isl_schedule_node_free(node
);
5789 if (isl_options_get_schedule_outer_coincidence(ctx
))
5790 return carry_coincidence(node
, graph
);
5791 return carry_dependences(node
, graph
);
5795 return compute_next_band(node
, graph
, 1);
5796 return sort_statements(node
, graph
, initialized
);
5799 /* Construct a band of schedule rows for a connected dependence graph.
5800 * The caller is responsible for determining the strongly connected
5801 * components and calling compute_maxvar first.
5803 * We try to find a sequence of as many schedule rows as possible that result
5804 * in non-negative dependence distances (independent of the previous rows
5805 * in the sequence, i.e., such that the sequence is tilable), with as
5806 * many of the initial rows as possible satisfying the coincidence constraints.
5807 * The computation stops if we can't find any more rows or if we have found
5808 * all the rows we wanted to find.
5810 * If ctx->opt->schedule_outer_coincidence is set, then we force the
5811 * outermost dimension to satisfy the coincidence constraints. If this
5812 * turns out to be impossible, we fall back on the general scheme above
5813 * and try to carry as many dependences as possible.
5815 * If "graph" contains both condition and conditional validity dependences,
5816 * then we need to check that that the conditional schedule constraint
5817 * is satisfied, i.e., there are no violated conditional validity dependences
5818 * that are adjacent to any non-local condition dependences.
5819 * If there are, then we mark all those adjacent condition dependences
5820 * as local and recompute the current band. Those dependences that
5821 * are marked local will then be forced to be local.
5822 * The initial computation is performed with no dependences marked as local.
5823 * If we are lucky, then there will be no violated conditional validity
5824 * dependences adjacent to any non-local condition dependences.
5825 * Otherwise, we mark some additional condition dependences as local and
5826 * recompute. We continue this process until there are no violations left or
5827 * until we are no longer able to compute a schedule.
5828 * Since there are only a finite number of dependences,
5829 * there will only be a finite number of iterations.
5831 static isl_stat
compute_schedule_wcc_band(isl_ctx
*ctx
,
5832 struct isl_sched_graph
*graph
)
5834 int has_coincidence
;
5835 int use_coincidence
;
5836 int force_coincidence
= 0;
5837 int check_conditional
;
5839 if (sort_sccs(graph
) < 0)
5840 return isl_stat_error
;
5842 clear_local_edges(graph
);
5843 check_conditional
= need_condition_check(graph
);
5844 has_coincidence
= has_any_coincidence(graph
);
5846 if (ctx
->opt
->schedule_outer_coincidence
)
5847 force_coincidence
= 1;
5849 use_coincidence
= has_coincidence
;
5850 while (graph
->n_row
< graph
->maxvar
) {
5855 graph
->src_scc
= -1;
5856 graph
->dst_scc
= -1;
5858 if (setup_lp(ctx
, graph
, use_coincidence
) < 0)
5859 return isl_stat_error
;
5860 sol
= solve_lp(ctx
, graph
);
5862 return isl_stat_error
;
5863 if (sol
->size
== 0) {
5864 int empty
= graph
->n_total_row
== graph
->band_start
;
5867 if (use_coincidence
&& (!force_coincidence
|| !empty
)) {
5868 use_coincidence
= 0;
5873 coincident
= !has_coincidence
|| use_coincidence
;
5874 if (update_schedule(graph
, sol
, coincident
) < 0)
5875 return isl_stat_error
;
5877 if (!check_conditional
)
5879 violated
= has_violated_conditional_constraint(ctx
, graph
);
5881 return isl_stat_error
;
5884 if (reset_band(graph
) < 0)
5885 return isl_stat_error
;
5886 use_coincidence
= has_coincidence
;
5892 /* Compute a schedule for a connected dependence graph by considering
5893 * the graph as a whole and return the updated schedule node.
5895 * The actual schedule rows of the current band are computed by
5896 * compute_schedule_wcc_band. compute_schedule_finish_band takes
5897 * care of integrating the band into "node" and continuing
5900 static __isl_give isl_schedule_node
*compute_schedule_wcc_whole(
5901 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
5908 ctx
= isl_schedule_node_get_ctx(node
);
5909 if (compute_schedule_wcc_band(ctx
, graph
) < 0)
5910 return isl_schedule_node_free(node
);
5912 return compute_schedule_finish_band(node
, graph
, 1);
5915 /* Clustering information used by compute_schedule_wcc_clustering.
5917 * "n" is the number of SCCs in the original dependence graph
5918 * "scc" is an array of "n" elements, each representing an SCC
5919 * of the original dependence graph. All entries in the same cluster
5920 * have the same number of schedule rows.
5921 * "scc_cluster" maps each SCC index to the cluster to which it belongs,
5922 * where each cluster is represented by the index of the first SCC
5923 * in the cluster. Initially, each SCC belongs to a cluster containing
5926 * "scc_in_merge" is used by merge_clusters_along_edge to keep
5927 * track of which SCCs need to be merged.
5929 * "cluster" contains the merged clusters of SCCs after the clustering
5932 * "scc_node" is a temporary data structure used inside copy_partial.
5933 * For each SCC, it keeps track of the number of nodes in the SCC
5934 * that have already been copied.
5936 struct isl_clustering
{
5938 struct isl_sched_graph
*scc
;
5939 struct isl_sched_graph
*cluster
;
5945 /* Initialize the clustering data structure "c" from "graph".
5947 * In particular, allocate memory, extract the SCCs from "graph"
5948 * into c->scc, initialize scc_cluster and construct
5949 * a band of schedule rows for each SCC.
5950 * Within each SCC, there is only one SCC by definition.
5951 * Each SCC initially belongs to a cluster containing only that SCC.
5953 static isl_stat
clustering_init(isl_ctx
*ctx
, struct isl_clustering
*c
,
5954 struct isl_sched_graph
*graph
)
5959 c
->scc
= isl_calloc_array(ctx
, struct isl_sched_graph
, c
->n
);
5960 c
->cluster
= isl_calloc_array(ctx
, struct isl_sched_graph
, c
->n
);
5961 c
->scc_cluster
= isl_calloc_array(ctx
, int, c
->n
);
5962 c
->scc_node
= isl_calloc_array(ctx
, int, c
->n
);
5963 c
->scc_in_merge
= isl_calloc_array(ctx
, int, c
->n
);
5964 if (!c
->scc
|| !c
->cluster
||
5965 !c
->scc_cluster
|| !c
->scc_node
|| !c
->scc_in_merge
)
5966 return isl_stat_error
;
5968 for (i
= 0; i
< c
->n
; ++i
) {
5969 if (extract_sub_graph(ctx
, graph
, &node_scc_exactly
,
5970 &edge_scc_exactly
, i
, &c
->scc
[i
]) < 0)
5971 return isl_stat_error
;
5973 if (compute_maxvar(&c
->scc
[i
]) < 0)
5974 return isl_stat_error
;
5975 if (compute_schedule_wcc_band(ctx
, &c
->scc
[i
]) < 0)
5976 return isl_stat_error
;
5977 c
->scc_cluster
[i
] = i
;
5983 /* Free all memory allocated for "c".
5985 static void clustering_free(isl_ctx
*ctx
, struct isl_clustering
*c
)
5990 for (i
= 0; i
< c
->n
; ++i
)
5991 graph_free(ctx
, &c
->scc
[i
]);
5994 for (i
= 0; i
< c
->n
; ++i
)
5995 graph_free(ctx
, &c
->cluster
[i
]);
5997 free(c
->scc_cluster
);
5999 free(c
->scc_in_merge
);
6002 /* Should we refrain from merging the cluster in "graph" with
6003 * any other cluster?
6004 * In particular, is its current schedule band empty and incomplete.
6006 static int bad_cluster(struct isl_sched_graph
*graph
)
6008 return graph
->n_row
< graph
->maxvar
&&
6009 graph
->n_total_row
== graph
->band_start
;
6012 /* Is "edge" a proximity edge with a non-empty dependence relation?
6014 static isl_bool
is_non_empty_proximity(struct isl_sched_edge
*edge
)
6016 if (!is_proximity(edge
))
6017 return isl_bool_false
;
6018 return isl_bool_not(isl_map_plain_is_empty(edge
->map
));
6021 /* Return the index of an edge in "graph" that can be used to merge
6022 * two clusters in "c".
6023 * Return graph->n_edge if no such edge can be found.
6024 * Return -1 on error.
6026 * In particular, return a proximity edge between two clusters
6027 * that is not marked "no_merge" and such that neither of the
6028 * two clusters has an incomplete, empty band.
6030 * If there are multiple such edges, then try and find the most
6031 * appropriate edge to use for merging. In particular, pick the edge
6032 * with the greatest weight. If there are multiple of those,
6033 * then pick one with the shortest distance between
6034 * the two cluster representatives.
6036 static int find_proximity(struct isl_sched_graph
*graph
,
6037 struct isl_clustering
*c
)
6039 int i
, best
= graph
->n_edge
, best_dist
, best_weight
;
6041 for (i
= 0; i
< graph
->n_edge
; ++i
) {
6042 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
6046 prox
= is_non_empty_proximity(edge
);
6053 if (bad_cluster(&c
->scc
[edge
->src
->scc
]) ||
6054 bad_cluster(&c
->scc
[edge
->dst
->scc
]))
6056 dist
= c
->scc_cluster
[edge
->dst
->scc
] -
6057 c
->scc_cluster
[edge
->src
->scc
];
6060 weight
= edge
->weight
;
6061 if (best
< graph
->n_edge
) {
6062 if (best_weight
> weight
)
6064 if (best_weight
== weight
&& best_dist
<= dist
)
6069 best_weight
= weight
;
6075 /* Internal data structure used in mark_merge_sccs.
6077 * "graph" is the dependence graph in which a strongly connected
6078 * component is constructed.
6079 * "scc_cluster" maps each SCC index to the cluster to which it belongs.
6080 * "src" and "dst" are the indices of the nodes that are being merged.
6082 struct isl_mark_merge_sccs_data
{
6083 struct isl_sched_graph
*graph
;
6089 /* Check whether the cluster containing node "i" depends on the cluster
6090 * containing node "j". If "i" and "j" belong to the same cluster,
6091 * then they are taken to depend on each other to ensure that
6092 * the resulting strongly connected component consists of complete
6093 * clusters. Furthermore, if "i" and "j" are the two nodes that
6094 * are being merged, then they are taken to depend on each other as well.
6095 * Otherwise, check if there is a (conditional) validity dependence
6096 * from node[j] to node[i], forcing node[i] to follow node[j].
6098 static isl_bool
cluster_follows(int i
, int j
, void *user
)
6100 struct isl_mark_merge_sccs_data
*data
= user
;
6101 struct isl_sched_graph
*graph
= data
->graph
;
6102 int *scc_cluster
= data
->scc_cluster
;
6104 if (data
->src
== i
&& data
->dst
== j
)
6105 return isl_bool_true
;
6106 if (data
->src
== j
&& data
->dst
== i
)
6107 return isl_bool_true
;
6108 if (scc_cluster
[graph
->node
[i
].scc
] == scc_cluster
[graph
->node
[j
].scc
])
6109 return isl_bool_true
;
6111 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
6114 /* Mark all SCCs that belong to either of the two clusters in "c"
6115 * connected by the edge in "graph" with index "edge", or to any
6116 * of the intermediate clusters.
6117 * The marking is recorded in c->scc_in_merge.
6119 * The given edge has been selected for merging two clusters,
6120 * meaning that there is at least a proximity edge between the two nodes.
6121 * However, there may also be (indirect) validity dependences
6122 * between the two nodes. When merging the two clusters, all clusters
6123 * containing one or more of the intermediate nodes along the
6124 * indirect validity dependences need to be merged in as well.
6126 * First collect all such nodes by computing the strongly connected
6127 * component (SCC) containing the two nodes connected by the edge, where
6128 * the two nodes are considered to depend on each other to make
6129 * sure they end up in the same SCC. Similarly, each node is considered
6130 * to depend on every other node in the same cluster to ensure
6131 * that the SCC consists of complete clusters.
6133 * Then the original SCCs that contain any of these nodes are marked
6134 * in c->scc_in_merge.
6136 static isl_stat
mark_merge_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6137 int edge
, struct isl_clustering
*c
)
6139 struct isl_mark_merge_sccs_data data
;
6140 struct isl_tarjan_graph
*g
;
6143 for (i
= 0; i
< c
->n
; ++i
)
6144 c
->scc_in_merge
[i
] = 0;
6147 data
.scc_cluster
= c
->scc_cluster
;
6148 data
.src
= graph
->edge
[edge
].src
- graph
->node
;
6149 data
.dst
= graph
->edge
[edge
].dst
- graph
->node
;
6151 g
= isl_tarjan_graph_component(ctx
, graph
->n
, data
.dst
,
6152 &cluster_follows
, &data
);
6158 isl_die(ctx
, isl_error_internal
,
6159 "expecting at least two nodes in component",
6161 if (g
->order
[--i
] != -1)
6162 isl_die(ctx
, isl_error_internal
,
6163 "expecting end of component marker", goto error
);
6165 for (--i
; i
>= 0 && g
->order
[i
] != -1; --i
) {
6166 int scc
= graph
->node
[g
->order
[i
]].scc
;
6167 c
->scc_in_merge
[scc
] = 1;
6170 isl_tarjan_graph_free(g
);
6173 isl_tarjan_graph_free(g
);
6174 return isl_stat_error
;
6177 /* Construct the identifier "cluster_i".
6179 static __isl_give isl_id
*cluster_id(isl_ctx
*ctx
, int i
)
6183 snprintf(name
, sizeof(name
), "cluster_%d", i
);
6184 return isl_id_alloc(ctx
, name
, NULL
);
6187 /* Construct the space of the cluster with index "i" containing
6188 * the strongly connected component "scc".
6190 * In particular, construct a space called cluster_i with dimension equal
6191 * to the number of schedule rows in the current band of "scc".
6193 static __isl_give isl_space
*cluster_space(struct isl_sched_graph
*scc
, int i
)
6199 nvar
= scc
->n_total_row
- scc
->band_start
;
6200 space
= isl_space_copy(scc
->node
[0].space
);
6201 space
= isl_space_params(space
);
6202 space
= isl_space_set_from_params(space
);
6203 space
= isl_space_add_dims(space
, isl_dim_set
, nvar
);
6204 id
= cluster_id(isl_space_get_ctx(space
), i
);
6205 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
6210 /* Collect the domain of the graph for merging clusters.
6212 * In particular, for each cluster with first SCC "i", construct
6213 * a set in the space called cluster_i with dimension equal
6214 * to the number of schedule rows in the current band of the cluster.
6216 static __isl_give isl_union_set
*collect_domain(isl_ctx
*ctx
,
6217 struct isl_sched_graph
*graph
, struct isl_clustering
*c
)
6221 isl_union_set
*domain
;
6223 space
= isl_space_params_alloc(ctx
, 0);
6224 domain
= isl_union_set_empty(space
);
6226 for (i
= 0; i
< graph
->scc
; ++i
) {
6229 if (!c
->scc_in_merge
[i
])
6231 if (c
->scc_cluster
[i
] != i
)
6233 space
= cluster_space(&c
->scc
[i
], i
);
6234 domain
= isl_union_set_add_set(domain
, isl_set_universe(space
));
6240 /* Construct a map from the original instances to the corresponding
6241 * cluster instance in the current bands of the clusters in "c".
6243 static __isl_give isl_union_map
*collect_cluster_map(isl_ctx
*ctx
,
6244 struct isl_sched_graph
*graph
, struct isl_clustering
*c
)
6248 isl_union_map
*cluster_map
;
6250 space
= isl_space_params_alloc(ctx
, 0);
6251 cluster_map
= isl_union_map_empty(space
);
6252 for (i
= 0; i
< graph
->scc
; ++i
) {
6256 if (!c
->scc_in_merge
[i
])
6259 id
= cluster_id(ctx
, c
->scc_cluster
[i
]);
6260 start
= c
->scc
[i
].band_start
;
6261 n
= c
->scc
[i
].n_total_row
- start
;
6262 for (j
= 0; j
< c
->scc
[i
].n
; ++j
) {
6265 struct isl_sched_node
*node
= &c
->scc
[i
].node
[j
];
6267 ma
= node_extract_partial_schedule_multi_aff(node
,
6269 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
,
6271 map
= isl_map_from_multi_aff(ma
);
6272 cluster_map
= isl_union_map_add_map(cluster_map
, map
);
6280 /* Add "umap" to the schedule constraints "sc" of all types of "edge"
6281 * that are not isl_edge_condition or isl_edge_conditional_validity.
6283 static __isl_give isl_schedule_constraints
*add_non_conditional_constraints(
6284 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*umap
,
6285 __isl_take isl_schedule_constraints
*sc
)
6287 enum isl_edge_type t
;
6292 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
6293 if (t
== isl_edge_condition
||
6294 t
== isl_edge_conditional_validity
)
6296 if (!is_type(edge
, t
))
6298 sc
= isl_schedule_constraints_add(sc
, t
,
6299 isl_union_map_copy(umap
));
6305 /* Add schedule constraints of types isl_edge_condition and
6306 * isl_edge_conditional_validity to "sc" by applying "umap" to
6307 * the domains of the wrapped relations in domain and range
6308 * of the corresponding tagged constraints of "edge".
6310 static __isl_give isl_schedule_constraints
*add_conditional_constraints(
6311 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*umap
,
6312 __isl_take isl_schedule_constraints
*sc
)
6314 enum isl_edge_type t
;
6315 isl_union_map
*tagged
;
6317 for (t
= isl_edge_condition
; t
<= isl_edge_conditional_validity
; ++t
) {
6318 if (!is_type(edge
, t
))
6320 if (t
== isl_edge_condition
)
6321 tagged
= isl_union_map_copy(edge
->tagged_condition
);
6323 tagged
= isl_union_map_copy(edge
->tagged_validity
);
6324 tagged
= isl_union_map_zip(tagged
);
6325 tagged
= isl_union_map_apply_domain(tagged
,
6326 isl_union_map_copy(umap
));
6327 tagged
= isl_union_map_zip(tagged
);
6328 sc
= isl_schedule_constraints_add(sc
, t
, tagged
);
6336 /* Given a mapping "cluster_map" from the original instances to
6337 * the cluster instances, add schedule constraints on the clusters
6338 * to "sc" corresponding to the original constraints represented by "edge".
6340 * For non-tagged dependence constraints, the cluster constraints
6341 * are obtained by applying "cluster_map" to the edge->map.
6343 * For tagged dependence constraints, "cluster_map" needs to be applied
6344 * to the domains of the wrapped relations in domain and range
6345 * of the tagged dependence constraints. Pick out the mappings
6346 * from these domains from "cluster_map" and construct their product.
6347 * This mapping can then be applied to the pair of domains.
6349 static __isl_give isl_schedule_constraints
*collect_edge_constraints(
6350 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*cluster_map
,
6351 __isl_take isl_schedule_constraints
*sc
)
6353 isl_union_map
*umap
;
6355 isl_union_set
*uset
;
6356 isl_union_map
*umap1
, *umap2
;
6361 umap
= isl_union_map_from_map(isl_map_copy(edge
->map
));
6362 umap
= isl_union_map_apply_domain(umap
,
6363 isl_union_map_copy(cluster_map
));
6364 umap
= isl_union_map_apply_range(umap
,
6365 isl_union_map_copy(cluster_map
));
6366 sc
= add_non_conditional_constraints(edge
, umap
, sc
);
6367 isl_union_map_free(umap
);
6369 if (!sc
|| (!is_condition(edge
) && !is_conditional_validity(edge
)))
6372 space
= isl_space_domain(isl_map_get_space(edge
->map
));
6373 uset
= isl_union_set_from_set(isl_set_universe(space
));
6374 umap1
= isl_union_map_copy(cluster_map
);
6375 umap1
= isl_union_map_intersect_domain(umap1
, uset
);
6376 space
= isl_space_range(isl_map_get_space(edge
->map
));
6377 uset
= isl_union_set_from_set(isl_set_universe(space
));
6378 umap2
= isl_union_map_copy(cluster_map
);
6379 umap2
= isl_union_map_intersect_domain(umap2
, uset
);
6380 umap
= isl_union_map_product(umap1
, umap2
);
6382 sc
= add_conditional_constraints(edge
, umap
, sc
);
6384 isl_union_map_free(umap
);
6388 /* Given a mapping "cluster_map" from the original instances to
6389 * the cluster instances, add schedule constraints on the clusters
6390 * to "sc" corresponding to all edges in "graph" between nodes that
6391 * belong to SCCs that are marked for merging in "scc_in_merge".
6393 static __isl_give isl_schedule_constraints
*collect_constraints(
6394 struct isl_sched_graph
*graph
, int *scc_in_merge
,
6395 __isl_keep isl_union_map
*cluster_map
,
6396 __isl_take isl_schedule_constraints
*sc
)
6400 for (i
= 0; i
< graph
->n_edge
; ++i
) {
6401 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
6403 if (!scc_in_merge
[edge
->src
->scc
])
6405 if (!scc_in_merge
[edge
->dst
->scc
])
6407 sc
= collect_edge_constraints(edge
, cluster_map
, sc
);
6413 /* Construct a dependence graph for scheduling clusters with respect
6414 * to each other and store the result in "merge_graph".
6415 * In particular, the nodes of the graph correspond to the schedule
6416 * dimensions of the current bands of those clusters that have been
6417 * marked for merging in "c".
6419 * First construct an isl_schedule_constraints object for this domain
6420 * by transforming the edges in "graph" to the domain.
6421 * Then initialize a dependence graph for scheduling from these
6424 static isl_stat
init_merge_graph(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6425 struct isl_clustering
*c
, struct isl_sched_graph
*merge_graph
)
6427 isl_union_set
*domain
;
6428 isl_union_map
*cluster_map
;
6429 isl_schedule_constraints
*sc
;
6432 domain
= collect_domain(ctx
, graph
, c
);
6433 sc
= isl_schedule_constraints_on_domain(domain
);
6435 return isl_stat_error
;
6436 cluster_map
= collect_cluster_map(ctx
, graph
, c
);
6437 sc
= collect_constraints(graph
, c
->scc_in_merge
, cluster_map
, sc
);
6438 isl_union_map_free(cluster_map
);
6440 r
= graph_init(merge_graph
, sc
);
6442 isl_schedule_constraints_free(sc
);
6447 /* Compute the maximal number of remaining schedule rows that still need
6448 * to be computed for the nodes that belong to clusters with the maximal
6449 * dimension for the current band (i.e., the band that is to be merged).
6450 * Only clusters that are about to be merged are considered.
6451 * "maxvar" is the maximal dimension for the current band.
6452 * "c" contains information about the clusters.
6454 * Return the maximal number of remaining schedule rows or -1 on error.
6456 static int compute_maxvar_max_slack(int maxvar
, struct isl_clustering
*c
)
6462 for (i
= 0; i
< c
->n
; ++i
) {
6464 struct isl_sched_graph
*scc
;
6466 if (!c
->scc_in_merge
[i
])
6469 nvar
= scc
->n_total_row
- scc
->band_start
;
6472 for (j
= 0; j
< scc
->n
; ++j
) {
6473 struct isl_sched_node
*node
= &scc
->node
[j
];
6476 if (node_update_vmap(node
) < 0)
6478 slack
= node
->nvar
- node
->rank
;
6479 if (slack
> max_slack
)
6487 /* If there are any clusters where the dimension of the current band
6488 * (i.e., the band that is to be merged) is smaller than "maxvar" and
6489 * if there are any nodes in such a cluster where the number
6490 * of remaining schedule rows that still need to be computed
6491 * is greater than "max_slack", then return the smallest current band
6492 * dimension of all these clusters. Otherwise return the original value
6493 * of "maxvar". Return -1 in case of any error.
6494 * Only clusters that are about to be merged are considered.
6495 * "c" contains information about the clusters.
6497 static int limit_maxvar_to_slack(int maxvar
, int max_slack
,
6498 struct isl_clustering
*c
)
6502 for (i
= 0; i
< c
->n
; ++i
) {
6504 struct isl_sched_graph
*scc
;
6506 if (!c
->scc_in_merge
[i
])
6509 nvar
= scc
->n_total_row
- scc
->band_start
;
6512 for (j
= 0; j
< scc
->n
; ++j
) {
6513 struct isl_sched_node
*node
= &scc
->node
[j
];
6516 if (node_update_vmap(node
) < 0)
6518 slack
= node
->nvar
- node
->rank
;
6519 if (slack
> max_slack
) {
6529 /* Adjust merge_graph->maxvar based on the number of remaining schedule rows
6530 * that still need to be computed. In particular, if there is a node
6531 * in a cluster where the dimension of the current band is smaller
6532 * than merge_graph->maxvar, but the number of remaining schedule rows
6533 * is greater than that of any node in a cluster with the maximal
6534 * dimension for the current band (i.e., merge_graph->maxvar),
6535 * then adjust merge_graph->maxvar to the (smallest) current band dimension
6536 * of those clusters. Without this adjustment, the total number of
6537 * schedule dimensions would be increased, resulting in a skewed view
6538 * of the number of coincident dimensions.
6539 * "c" contains information about the clusters.
6541 * If the maximize_band_depth option is set and merge_graph->maxvar is reduced,
6542 * then there is no point in attempting any merge since it will be rejected
6543 * anyway. Set merge_graph->maxvar to zero in such cases.
6545 static isl_stat
adjust_maxvar_to_slack(isl_ctx
*ctx
,
6546 struct isl_sched_graph
*merge_graph
, struct isl_clustering
*c
)
6548 int max_slack
, maxvar
;
6550 max_slack
= compute_maxvar_max_slack(merge_graph
->maxvar
, c
);
6552 return isl_stat_error
;
6553 maxvar
= limit_maxvar_to_slack(merge_graph
->maxvar
, max_slack
, c
);
6555 return isl_stat_error
;
6557 if (maxvar
< merge_graph
->maxvar
) {
6558 if (isl_options_get_schedule_maximize_band_depth(ctx
))
6559 merge_graph
->maxvar
= 0;
6561 merge_graph
->maxvar
= maxvar
;
6567 /* Return the number of coincident dimensions in the current band of "graph",
6568 * where the nodes of "graph" are assumed to be scheduled by a single band.
6570 static int get_n_coincident(struct isl_sched_graph
*graph
)
6574 for (i
= graph
->band_start
; i
< graph
->n_total_row
; ++i
)
6575 if (!graph
->node
[0].coincident
[i
])
6578 return i
- graph
->band_start
;
6581 /* Should the clusters be merged based on the cluster schedule
6582 * in the current (and only) band of "merge_graph", given that
6583 * coincidence should be maximized?
6585 * If the number of coincident schedule dimensions in the merged band
6586 * would be less than the maximal number of coincident schedule dimensions
6587 * in any of the merged clusters, then the clusters should not be merged.
6589 static isl_bool
ok_to_merge_coincident(struct isl_clustering
*c
,
6590 struct isl_sched_graph
*merge_graph
)
6597 for (i
= 0; i
< c
->n
; ++i
) {
6598 if (!c
->scc_in_merge
[i
])
6600 n_coincident
= get_n_coincident(&c
->scc
[i
]);
6601 if (n_coincident
> max_coincident
)
6602 max_coincident
= n_coincident
;
6605 n_coincident
= get_n_coincident(merge_graph
);
6607 return isl_bool_ok(n_coincident
>= max_coincident
);
6610 /* Return the transformation on "node" expressed by the current (and only)
6611 * band of "merge_graph" applied to the clusters in "c".
6613 * First find the representation of "node" in its SCC in "c" and
6614 * extract the transformation expressed by the current band.
6615 * Then extract the transformation applied by "merge_graph"
6616 * to the cluster to which this SCC belongs.
6617 * Combine the two to obtain the complete transformation on the node.
6619 * Note that the range of the first transformation is an anonymous space,
6620 * while the domain of the second is named "cluster_X". The range
6621 * of the former therefore needs to be adjusted before the two
6624 static __isl_give isl_map
*extract_node_transformation(isl_ctx
*ctx
,
6625 struct isl_sched_node
*node
, struct isl_clustering
*c
,
6626 struct isl_sched_graph
*merge_graph
)
6628 struct isl_sched_node
*scc_node
, *cluster_node
;
6632 isl_multi_aff
*ma
, *ma2
;
6634 scc_node
= graph_find_node(ctx
, &c
->scc
[node
->scc
], node
->space
);
6635 if (scc_node
&& !is_node(&c
->scc
[node
->scc
], scc_node
))
6636 isl_die(ctx
, isl_error_internal
, "unable to find node",
6638 start
= c
->scc
[node
->scc
].band_start
;
6639 n
= c
->scc
[node
->scc
].n_total_row
- start
;
6640 ma
= node_extract_partial_schedule_multi_aff(scc_node
, start
, n
);
6641 space
= cluster_space(&c
->scc
[node
->scc
], c
->scc_cluster
[node
->scc
]);
6642 cluster_node
= graph_find_node(ctx
, merge_graph
, space
);
6643 if (cluster_node
&& !is_node(merge_graph
, cluster_node
))
6644 isl_die(ctx
, isl_error_internal
, "unable to find cluster",
6645 space
= isl_space_free(space
));
6646 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
6647 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
, id
);
6648 isl_space_free(space
);
6649 n
= merge_graph
->n_total_row
;
6650 ma2
= node_extract_partial_schedule_multi_aff(cluster_node
, 0, n
);
6651 ma
= isl_multi_aff_pullback_multi_aff(ma2
, ma
);
6653 return isl_map_from_multi_aff(ma
);
6656 /* Give a set of distances "set", are they bounded by a small constant
6657 * in direction "pos"?
6658 * In practice, check if they are bounded by 2 by checking that there
6659 * are no elements with a value greater than or equal to 3 or
6660 * smaller than or equal to -3.
6662 static isl_bool
distance_is_bounded(__isl_keep isl_set
*set
, int pos
)
6668 return isl_bool_error
;
6670 test
= isl_set_copy(set
);
6671 test
= isl_set_lower_bound_si(test
, isl_dim_set
, pos
, 3);
6672 bounded
= isl_set_is_empty(test
);
6675 if (bounded
< 0 || !bounded
)
6678 test
= isl_set_copy(set
);
6679 test
= isl_set_upper_bound_si(test
, isl_dim_set
, pos
, -3);
6680 bounded
= isl_set_is_empty(test
);
6686 /* Does the set "set" have a fixed (but possible parametric) value
6687 * at dimension "pos"?
6689 static isl_bool
has_single_value(__isl_keep isl_set
*set
, int pos
)
6694 n
= isl_set_dim(set
, isl_dim_set
);
6696 return isl_bool_error
;
6697 set
= isl_set_copy(set
);
6698 set
= isl_set_project_out(set
, isl_dim_set
, pos
+ 1, n
- (pos
+ 1));
6699 set
= isl_set_project_out(set
, isl_dim_set
, 0, pos
);
6700 single
= isl_set_is_singleton(set
);
6706 /* Does "map" have a fixed (but possible parametric) value
6707 * at dimension "pos" of either its domain or its range?
6709 static isl_bool
has_singular_src_or_dst(__isl_keep isl_map
*map
, int pos
)
6714 set
= isl_map_domain(isl_map_copy(map
));
6715 single
= has_single_value(set
, pos
);
6718 if (single
< 0 || single
)
6721 set
= isl_map_range(isl_map_copy(map
));
6722 single
= has_single_value(set
, pos
);
6728 /* Does the edge "edge" from "graph" have bounded dependence distances
6729 * in the merged graph "merge_graph" of a selection of clusters in "c"?
6731 * Extract the complete transformations of the source and destination
6732 * nodes of the edge, apply them to the edge constraints and
6733 * compute the differences. Finally, check if these differences are bounded
6734 * in each direction.
6736 * If the dimension of the band is greater than the number of
6737 * dimensions that can be expected to be optimized by the edge
6738 * (based on its weight), then also allow the differences to be unbounded
6739 * in the remaining dimensions, but only if either the source or
6740 * the destination has a fixed value in that direction.
6741 * This allows a statement that produces values that are used by
6742 * several instances of another statement to be merged with that
6744 * However, merging such clusters will introduce an inherently
6745 * large proximity distance inside the merged cluster, meaning
6746 * that proximity distances will no longer be optimized in
6747 * subsequent merges. These merges are therefore only allowed
6748 * after all other possible merges have been tried.
6749 * The first time such a merge is encountered, the weight of the edge
6750 * is replaced by a negative weight. The second time (i.e., after
6751 * all merges over edges with a non-negative weight have been tried),
6752 * the merge is allowed.
6754 static isl_bool
has_bounded_distances(isl_ctx
*ctx
, struct isl_sched_edge
*edge
,
6755 struct isl_sched_graph
*graph
, struct isl_clustering
*c
,
6756 struct isl_sched_graph
*merge_graph
)
6764 map
= isl_map_copy(edge
->map
);
6765 t
= extract_node_transformation(ctx
, edge
->src
, c
, merge_graph
);
6766 map
= isl_map_apply_domain(map
, t
);
6767 t
= extract_node_transformation(ctx
, edge
->dst
, c
, merge_graph
);
6768 map
= isl_map_apply_range(map
, t
);
6769 dist
= isl_map_deltas(isl_map_copy(map
));
6771 bounded
= isl_bool_true
;
6772 n
= isl_set_dim(dist
, isl_dim_set
);
6775 n_slack
= n
- edge
->weight
;
6776 if (edge
->weight
< 0)
6777 n_slack
-= graph
->max_weight
+ 1;
6778 for (i
= 0; i
< n
; ++i
) {
6779 isl_bool bounded_i
, singular_i
;
6781 bounded_i
= distance_is_bounded(dist
, i
);
6786 if (edge
->weight
>= 0)
6787 bounded
= isl_bool_false
;
6791 singular_i
= has_singular_src_or_dst(map
, i
);
6796 bounded
= isl_bool_false
;
6799 if (!bounded
&& i
>= n
&& edge
->weight
>= 0)
6800 edge
->weight
-= graph
->max_weight
+ 1;
6808 return isl_bool_error
;
6811 /* Should the clusters be merged based on the cluster schedule
6812 * in the current (and only) band of "merge_graph"?
6813 * "graph" is the original dependence graph, while "c" records
6814 * which SCCs are involved in the latest merge.
6816 * In particular, is there at least one proximity constraint
6817 * that is optimized by the merge?
6819 * A proximity constraint is considered to be optimized
6820 * if the dependence distances are small.
6822 static isl_bool
ok_to_merge_proximity(isl_ctx
*ctx
,
6823 struct isl_sched_graph
*graph
, struct isl_clustering
*c
,
6824 struct isl_sched_graph
*merge_graph
)
6828 for (i
= 0; i
< graph
->n_edge
; ++i
) {
6829 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
6832 if (!is_proximity(edge
))
6834 if (!c
->scc_in_merge
[edge
->src
->scc
])
6836 if (!c
->scc_in_merge
[edge
->dst
->scc
])
6838 if (c
->scc_cluster
[edge
->dst
->scc
] ==
6839 c
->scc_cluster
[edge
->src
->scc
])
6841 bounded
= has_bounded_distances(ctx
, edge
, graph
, c
,
6843 if (bounded
< 0 || bounded
)
6847 return isl_bool_false
;
6850 /* Should the clusters be merged based on the cluster schedule
6851 * in the current (and only) band of "merge_graph"?
6852 * "graph" is the original dependence graph, while "c" records
6853 * which SCCs are involved in the latest merge.
6855 * If the current band is empty, then the clusters should not be merged.
6857 * If the band depth should be maximized and the merge schedule
6858 * is incomplete (meaning that the dimension of some of the schedule
6859 * bands in the original schedule will be reduced), then the clusters
6860 * should not be merged.
6862 * If the schedule_maximize_coincidence option is set, then check that
6863 * the number of coincident schedule dimensions is not reduced.
6865 * Finally, only allow the merge if at least one proximity
6866 * constraint is optimized.
6868 static isl_bool
ok_to_merge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6869 struct isl_clustering
*c
, struct isl_sched_graph
*merge_graph
)
6871 if (merge_graph
->n_total_row
== merge_graph
->band_start
)
6872 return isl_bool_false
;
6874 if (isl_options_get_schedule_maximize_band_depth(ctx
) &&
6875 merge_graph
->n_total_row
< merge_graph
->maxvar
)
6876 return isl_bool_false
;
6878 if (isl_options_get_schedule_maximize_coincidence(ctx
)) {
6881 ok
= ok_to_merge_coincident(c
, merge_graph
);
6886 return ok_to_merge_proximity(ctx
, graph
, c
, merge_graph
);
6889 /* Apply the schedule in "t_node" to the "n" rows starting at "first"
6890 * of the schedule in "node" and return the result.
6892 * That is, essentially compute
6894 * T * N(first:first+n-1)
6896 * taking into account the constant term and the parameter coefficients
6899 static __isl_give isl_mat
*node_transformation(isl_ctx
*ctx
,
6900 struct isl_sched_node
*t_node
, struct isl_sched_node
*node
,
6905 isl_size n_row
, n_col
;
6908 n_param
= node
->nparam
;
6910 n_row
= isl_mat_rows(t_node
->sched
);
6911 n_col
= isl_mat_cols(node
->sched
);
6912 if (n_row
< 0 || n_col
< 0)
6914 t
= isl_mat_alloc(ctx
, n_row
, n_col
);
6917 for (i
= 0; i
< n_row
; ++i
) {
6918 isl_seq_cpy(t
->row
[i
], t_node
->sched
->row
[i
], 1 + n_param
);
6919 isl_seq_clr(t
->row
[i
] + 1 + n_param
, n_var
);
6920 for (j
= 0; j
< n
; ++j
)
6921 isl_seq_addmul(t
->row
[i
],
6922 t_node
->sched
->row
[i
][1 + n_param
+ j
],
6923 node
->sched
->row
[first
+ j
],
6924 1 + n_param
+ n_var
);
6929 /* Apply the cluster schedule in "t_node" to the current band
6930 * schedule of the nodes in "graph".
6932 * In particular, replace the rows starting at band_start
6933 * by the result of applying the cluster schedule in "t_node"
6934 * to the original rows.
6936 * The coincidence of the schedule is determined by the coincidence
6937 * of the cluster schedule.
6939 static isl_stat
transform(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6940 struct isl_sched_node
*t_node
)
6946 start
= graph
->band_start
;
6947 n
= graph
->n_total_row
- start
;
6949 n_new
= isl_mat_rows(t_node
->sched
);
6951 return isl_stat_error
;
6952 for (i
= 0; i
< graph
->n
; ++i
) {
6953 struct isl_sched_node
*node
= &graph
->node
[i
];
6956 t
= node_transformation(ctx
, t_node
, node
, start
, n
);
6957 node
->sched
= isl_mat_drop_rows(node
->sched
, start
, n
);
6958 node
->sched
= isl_mat_concat(node
->sched
, t
);
6959 node
->sched_map
= isl_map_free(node
->sched_map
);
6961 return isl_stat_error
;
6962 for (j
= 0; j
< n_new
; ++j
)
6963 node
->coincident
[start
+ j
] = t_node
->coincident
[j
];
6965 graph
->n_total_row
-= n
;
6967 graph
->n_total_row
+= n_new
;
6968 graph
->n_row
+= n_new
;
6973 /* Merge the clusters marked for merging in "c" into a single
6974 * cluster using the cluster schedule in the current band of "merge_graph".
6975 * The representative SCC for the new cluster is the SCC with
6976 * the smallest index.
6978 * The current band schedule of each SCC in the new cluster is obtained
6979 * by applying the schedule of the corresponding original cluster
6980 * to the original band schedule.
6981 * All SCCs in the new cluster have the same number of schedule rows.
6983 static isl_stat
merge(isl_ctx
*ctx
, struct isl_clustering
*c
,
6984 struct isl_sched_graph
*merge_graph
)
6990 for (i
= 0; i
< c
->n
; ++i
) {
6991 struct isl_sched_node
*node
;
6993 if (!c
->scc_in_merge
[i
])
6997 space
= cluster_space(&c
->scc
[i
], c
->scc_cluster
[i
]);
6998 node
= graph_find_node(ctx
, merge_graph
, space
);
6999 isl_space_free(space
);
7001 return isl_stat_error
;
7002 if (!is_node(merge_graph
, node
))
7003 isl_die(ctx
, isl_error_internal
,
7004 "unable to find cluster",
7005 return isl_stat_error
);
7006 if (transform(ctx
, &c
->scc
[i
], node
) < 0)
7007 return isl_stat_error
;
7008 c
->scc_cluster
[i
] = cluster
;
7014 /* Try and merge the clusters of SCCs marked in c->scc_in_merge
7015 * by scheduling the current cluster bands with respect to each other.
7017 * Construct a dependence graph with a space for each cluster and
7018 * with the coordinates of each space corresponding to the schedule
7019 * dimensions of the current band of that cluster.
7020 * Construct a cluster schedule in this cluster dependence graph and
7021 * apply it to the current cluster bands if it is applicable
7022 * according to ok_to_merge.
7024 * If the number of remaining schedule dimensions in a cluster
7025 * with a non-maximal current schedule dimension is greater than
7026 * the number of remaining schedule dimensions in clusters
7027 * with a maximal current schedule dimension, then restrict
7028 * the number of rows to be computed in the cluster schedule
7029 * to the minimal such non-maximal current schedule dimension.
7030 * Do this by adjusting merge_graph.maxvar.
7032 * Return isl_bool_true if the clusters have effectively been merged
7033 * into a single cluster.
7035 * Note that since the standard scheduling algorithm minimizes the maximal
7036 * distance over proximity constraints, the proximity constraints between
7037 * the merged clusters may not be optimized any further than what is
7038 * sufficient to bring the distances within the limits of the internal
7039 * proximity constraints inside the individual clusters.
7040 * It may therefore make sense to perform an additional translation step
7041 * to bring the clusters closer to each other, while maintaining
7042 * the linear part of the merging schedule found using the standard
7043 * scheduling algorithm.
7045 static isl_bool
try_merge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
7046 struct isl_clustering
*c
)
7048 struct isl_sched_graph merge_graph
= { 0 };
7051 if (init_merge_graph(ctx
, graph
, c
, &merge_graph
) < 0)
7054 if (compute_maxvar(&merge_graph
) < 0)
7056 if (adjust_maxvar_to_slack(ctx
, &merge_graph
,c
) < 0)
7058 if (compute_schedule_wcc_band(ctx
, &merge_graph
) < 0)
7060 merged
= ok_to_merge(ctx
, graph
, c
, &merge_graph
);
7061 if (merged
&& merge(ctx
, c
, &merge_graph
) < 0)
7064 graph_free(ctx
, &merge_graph
);
7067 graph_free(ctx
, &merge_graph
);
7068 return isl_bool_error
;
7071 /* Is there any edge marked "no_merge" between two SCCs that are
7072 * about to be merged (i.e., that are set in "scc_in_merge")?
7073 * "merge_edge" is the proximity edge along which the clusters of SCCs
7074 * are going to be merged.
7076 * If there is any edge between two SCCs with a negative weight,
7077 * while the weight of "merge_edge" is non-negative, then this
7078 * means that the edge was postponed. "merge_edge" should then
7079 * also be postponed since merging along the edge with negative weight should
7080 * be postponed until all edges with non-negative weight have been tried.
7081 * Replace the weight of "merge_edge" by a negative weight as well and
7082 * tell the caller not to attempt a merge.
7084 static int any_no_merge(struct isl_sched_graph
*graph
, int *scc_in_merge
,
7085 struct isl_sched_edge
*merge_edge
)
7089 for (i
= 0; i
< graph
->n_edge
; ++i
) {
7090 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
7092 if (!scc_in_merge
[edge
->src
->scc
])
7094 if (!scc_in_merge
[edge
->dst
->scc
])
7098 if (merge_edge
->weight
>= 0 && edge
->weight
< 0) {
7099 merge_edge
->weight
-= graph
->max_weight
+ 1;
7107 /* Merge the two clusters in "c" connected by the edge in "graph"
7108 * with index "edge" into a single cluster.
7109 * If it turns out to be impossible to merge these two clusters,
7110 * then mark the edge as "no_merge" such that it will not be
7113 * First mark all SCCs that need to be merged. This includes the SCCs
7114 * in the two clusters, but it may also include the SCCs
7115 * of intermediate clusters.
7116 * If there is already a no_merge edge between any pair of such SCCs,
7117 * then simply mark the current edge as no_merge as well.
7118 * Likewise, if any of those edges was postponed by has_bounded_distances,
7119 * then postpone the current edge as well.
7120 * Otherwise, try and merge the clusters and mark "edge" as "no_merge"
7121 * if the clusters did not end up getting merged, unless the non-merge
7122 * is due to the fact that the edge was postponed. This postponement
7123 * can be recognized by a change in weight (from non-negative to negative).
7125 static isl_stat
merge_clusters_along_edge(isl_ctx
*ctx
,
7126 struct isl_sched_graph
*graph
, int edge
, struct isl_clustering
*c
)
7129 int edge_weight
= graph
->edge
[edge
].weight
;
7131 if (mark_merge_sccs(ctx
, graph
, edge
, c
) < 0)
7132 return isl_stat_error
;
7134 if (any_no_merge(graph
, c
->scc_in_merge
, &graph
->edge
[edge
]))
7135 merged
= isl_bool_false
;
7137 merged
= try_merge(ctx
, graph
, c
);
7139 return isl_stat_error
;
7140 if (!merged
&& edge_weight
== graph
->edge
[edge
].weight
)
7141 graph
->edge
[edge
].no_merge
= 1;
7146 /* Does "node" belong to the cluster identified by "cluster"?
7148 static int node_cluster_exactly(struct isl_sched_node
*node
, int cluster
)
7150 return node
->cluster
== cluster
;
7153 /* Does "edge" connect two nodes belonging to the cluster
7154 * identified by "cluster"?
7156 static int edge_cluster_exactly(struct isl_sched_edge
*edge
, int cluster
)
7158 return edge
->src
->cluster
== cluster
&& edge
->dst
->cluster
== cluster
;
7161 /* Swap the schedule of "node1" and "node2".
7162 * Both nodes have been derived from the same node in a common parent graph.
7163 * Since the "coincident" field is shared with that node
7164 * in the parent graph, there is no need to also swap this field.
7166 static void swap_sched(struct isl_sched_node
*node1
,
7167 struct isl_sched_node
*node2
)
7172 sched
= node1
->sched
;
7173 node1
->sched
= node2
->sched
;
7174 node2
->sched
= sched
;
7176 sched_map
= node1
->sched_map
;
7177 node1
->sched_map
= node2
->sched_map
;
7178 node2
->sched_map
= sched_map
;
7181 /* Copy the current band schedule from the SCCs that form the cluster
7182 * with index "pos" to the actual cluster at position "pos".
7183 * By construction, the index of the first SCC that belongs to the cluster
7186 * The order of the nodes inside both the SCCs and the cluster
7187 * is assumed to be same as the order in the original "graph".
7189 * Since the SCC graphs will no longer be used after this function,
7190 * the schedules are actually swapped rather than copied.
7192 static isl_stat
copy_partial(struct isl_sched_graph
*graph
,
7193 struct isl_clustering
*c
, int pos
)
7197 c
->cluster
[pos
].n_total_row
= c
->scc
[pos
].n_total_row
;
7198 c
->cluster
[pos
].n_row
= c
->scc
[pos
].n_row
;
7199 c
->cluster
[pos
].maxvar
= c
->scc
[pos
].maxvar
;
7201 for (i
= 0; i
< graph
->n
; ++i
) {
7205 if (graph
->node
[i
].cluster
!= pos
)
7207 s
= graph
->node
[i
].scc
;
7208 k
= c
->scc_node
[s
]++;
7209 swap_sched(&c
->cluster
[pos
].node
[j
], &c
->scc
[s
].node
[k
]);
7210 if (c
->scc
[s
].maxvar
> c
->cluster
[pos
].maxvar
)
7211 c
->cluster
[pos
].maxvar
= c
->scc
[s
].maxvar
;
7218 /* Is there a (conditional) validity dependence from node[j] to node[i],
7219 * forcing node[i] to follow node[j] or do the nodes belong to the same
7222 static isl_bool
node_follows_strong_or_same_cluster(int i
, int j
, void *user
)
7224 struct isl_sched_graph
*graph
= user
;
7226 if (graph
->node
[i
].cluster
== graph
->node
[j
].cluster
)
7227 return isl_bool_true
;
7228 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
7231 /* Extract the merged clusters of SCCs in "graph", sort them, and
7232 * store them in c->clusters. Update c->scc_cluster accordingly.
7234 * First keep track of the cluster containing the SCC to which a node
7235 * belongs in the node itself.
7236 * Then extract the clusters into c->clusters, copying the current
7237 * band schedule from the SCCs that belong to the cluster.
7238 * Do this only once per cluster.
7240 * Finally, topologically sort the clusters and update c->scc_cluster
7241 * to match the new scc numbering. While the SCCs were originally
7242 * sorted already, some SCCs that depend on some other SCCs may
7243 * have been merged with SCCs that appear before these other SCCs.
7244 * A reordering may therefore be required.
7246 static isl_stat
extract_clusters(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
7247 struct isl_clustering
*c
)
7251 for (i
= 0; i
< graph
->n
; ++i
)
7252 graph
->node
[i
].cluster
= c
->scc_cluster
[graph
->node
[i
].scc
];
7254 for (i
= 0; i
< graph
->scc
; ++i
) {
7255 if (c
->scc_cluster
[i
] != i
)
7257 if (extract_sub_graph(ctx
, graph
, &node_cluster_exactly
,
7258 &edge_cluster_exactly
, i
, &c
->cluster
[i
]) < 0)
7259 return isl_stat_error
;
7260 c
->cluster
[i
].src_scc
= -1;
7261 c
->cluster
[i
].dst_scc
= -1;
7262 if (copy_partial(graph
, c
, i
) < 0)
7263 return isl_stat_error
;
7266 if (detect_ccs(ctx
, graph
, &node_follows_strong_or_same_cluster
) < 0)
7267 return isl_stat_error
;
7268 for (i
= 0; i
< graph
->n
; ++i
)
7269 c
->scc_cluster
[graph
->node
[i
].scc
] = graph
->node
[i
].cluster
;
7274 /* Compute weights on the proximity edges of "graph" that can
7275 * be used by find_proximity to find the most appropriate
7276 * proximity edge to use to merge two clusters in "c".
7277 * The weights are also used by has_bounded_distances to determine
7278 * whether the merge should be allowed.
7279 * Store the maximum of the computed weights in graph->max_weight.
7281 * The computed weight is a measure for the number of remaining schedule
7282 * dimensions that can still be completely aligned.
7283 * In particular, compute the number of equalities between
7284 * input dimensions and output dimensions in the proximity constraints.
7285 * The directions that are already handled by outer schedule bands
7286 * are projected out prior to determining this number.
7288 * Edges that will never be considered by find_proximity are ignored.
7290 static isl_stat
compute_weights(struct isl_sched_graph
*graph
,
7291 struct isl_clustering
*c
)
7295 graph
->max_weight
= 0;
7297 for (i
= 0; i
< graph
->n_edge
; ++i
) {
7298 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
7299 struct isl_sched_node
*src
= edge
->src
;
7300 struct isl_sched_node
*dst
= edge
->dst
;
7301 isl_basic_map
*hull
;
7303 isl_size n_in
, n_out
;
7305 prox
= is_non_empty_proximity(edge
);
7307 return isl_stat_error
;
7310 if (bad_cluster(&c
->scc
[edge
->src
->scc
]) ||
7311 bad_cluster(&c
->scc
[edge
->dst
->scc
]))
7313 if (c
->scc_cluster
[edge
->dst
->scc
] ==
7314 c
->scc_cluster
[edge
->src
->scc
])
7317 hull
= isl_map_affine_hull(isl_map_copy(edge
->map
));
7318 hull
= isl_basic_map_transform_dims(hull
, isl_dim_in
, 0,
7319 isl_mat_copy(src
->vmap
));
7320 hull
= isl_basic_map_transform_dims(hull
, isl_dim_out
, 0,
7321 isl_mat_copy(dst
->vmap
));
7322 hull
= isl_basic_map_project_out(hull
,
7323 isl_dim_in
, 0, src
->rank
);
7324 hull
= isl_basic_map_project_out(hull
,
7325 isl_dim_out
, 0, dst
->rank
);
7326 hull
= isl_basic_map_remove_divs(hull
);
7327 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
7328 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
7329 if (n_in
< 0 || n_out
< 0)
7330 hull
= isl_basic_map_free(hull
);
7331 hull
= isl_basic_map_drop_constraints_not_involving_dims(hull
,
7332 isl_dim_in
, 0, n_in
);
7333 hull
= isl_basic_map_drop_constraints_not_involving_dims(hull
,
7334 isl_dim_out
, 0, n_out
);
7336 return isl_stat_error
;
7337 edge
->weight
= isl_basic_map_n_equality(hull
);
7338 isl_basic_map_free(hull
);
7340 if (edge
->weight
> graph
->max_weight
)
7341 graph
->max_weight
= edge
->weight
;
7347 /* Call compute_schedule_finish_band on each of the clusters in "c"
7348 * in their topological order. This order is determined by the scc
7349 * fields of the nodes in "graph".
7350 * Combine the results in a sequence expressing the topological order.
7352 * If there is only one cluster left, then there is no need to introduce
7353 * a sequence node. Also, in this case, the cluster necessarily contains
7354 * the SCC at position 0 in the original graph and is therefore also
7355 * stored in the first cluster of "c".
7357 static __isl_give isl_schedule_node
*finish_bands_clustering(
7358 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
7359 struct isl_clustering
*c
)
7363 isl_union_set_list
*filters
;
7365 if (graph
->scc
== 1)
7366 return compute_schedule_finish_band(node
, &c
->cluster
[0], 0);
7368 ctx
= isl_schedule_node_get_ctx(node
);
7370 filters
= extract_sccs(ctx
, graph
);
7371 node
= isl_schedule_node_insert_sequence(node
, filters
);
7373 for (i
= 0; i
< graph
->scc
; ++i
) {
7374 int j
= c
->scc_cluster
[i
];
7375 node
= isl_schedule_node_child(node
, i
);
7376 node
= isl_schedule_node_child(node
, 0);
7377 node
= compute_schedule_finish_band(node
, &c
->cluster
[j
], 0);
7378 node
= isl_schedule_node_parent(node
);
7379 node
= isl_schedule_node_parent(node
);
7385 /* Compute a schedule for a connected dependence graph by first considering
7386 * each strongly connected component (SCC) in the graph separately and then
7387 * incrementally combining them into clusters.
7388 * Return the updated schedule node.
7390 * Initially, each cluster consists of a single SCC, each with its
7391 * own band schedule. The algorithm then tries to merge pairs
7392 * of clusters along a proximity edge until no more suitable
7393 * proximity edges can be found. During this merging, the schedule
7394 * is maintained in the individual SCCs.
7395 * After the merging is completed, the full resulting clusters
7396 * are extracted and in finish_bands_clustering,
7397 * compute_schedule_finish_band is called on each of them to integrate
7398 * the band into "node" and to continue the computation.
7400 * compute_weights initializes the weights that are used by find_proximity.
7402 static __isl_give isl_schedule_node
*compute_schedule_wcc_clustering(
7403 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
7406 struct isl_clustering c
;
7409 ctx
= isl_schedule_node_get_ctx(node
);
7411 if (clustering_init(ctx
, &c
, graph
) < 0)
7414 if (compute_weights(graph
, &c
) < 0)
7418 i
= find_proximity(graph
, &c
);
7421 if (i
>= graph
->n_edge
)
7423 if (merge_clusters_along_edge(ctx
, graph
, i
, &c
) < 0)
7427 if (extract_clusters(ctx
, graph
, &c
) < 0)
7430 node
= finish_bands_clustering(node
, graph
, &c
);
7432 clustering_free(ctx
, &c
);
7435 clustering_free(ctx
, &c
);
7436 return isl_schedule_node_free(node
);
7439 /* Compute a schedule for a connected dependence graph and return
7440 * the updated schedule node.
7442 * If Feautrier's algorithm is selected, we first recursively try to satisfy
7443 * as many validity dependences as possible. When all validity dependences
7444 * are satisfied we extend the schedule to a full-dimensional schedule.
7446 * Call compute_schedule_wcc_whole or compute_schedule_wcc_clustering
7447 * depending on whether the user has selected the option to try and
7448 * compute a schedule for the entire (weakly connected) component first.
7449 * If there is only a single strongly connected component (SCC), then
7450 * there is no point in trying to combine SCCs
7451 * in compute_schedule_wcc_clustering, so compute_schedule_wcc_whole
7452 * is called instead.
7454 static __isl_give isl_schedule_node
*compute_schedule_wcc(
7455 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
7462 ctx
= isl_schedule_node_get_ctx(node
);
7463 if (detect_sccs(ctx
, graph
) < 0)
7464 return isl_schedule_node_free(node
);
7466 if (compute_maxvar(graph
) < 0)
7467 return isl_schedule_node_free(node
);
7469 if (need_feautrier_step(ctx
, graph
))
7470 return compute_schedule_wcc_feautrier(node
, graph
);
7472 if (graph
->scc
<= 1 || isl_options_get_schedule_whole_component(ctx
))
7473 return compute_schedule_wcc_whole(node
, graph
);
7475 return compute_schedule_wcc_clustering(node
, graph
);
7478 /* Compute a schedule for each group of nodes identified by node->scc
7479 * separately and then combine them in a sequence node (or as set node
7480 * if graph->weak is set) inserted at position "node" of the schedule tree.
7481 * Return the updated schedule node.
7483 * If "wcc" is set then each of the groups belongs to a single
7484 * weakly connected component in the dependence graph so that
7485 * there is no need for compute_sub_schedule to look for weakly
7486 * connected components.
7488 * If a set node would be introduced and if the number of components
7489 * is equal to the number of nodes, then check if the schedule
7490 * is already complete. If so, a redundant set node would be introduced
7491 * (without any further descendants) stating that the statements
7492 * can be executed in arbitrary order, which is also expressed
7493 * by the absence of any node. Refrain from inserting any nodes
7494 * in this case and simply return.
7496 static __isl_give isl_schedule_node
*compute_component_schedule(
7497 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
7502 isl_union_set_list
*filters
;
7507 if (graph
->weak
&& graph
->scc
== graph
->n
) {
7508 if (compute_maxvar(graph
) < 0)
7509 return isl_schedule_node_free(node
);
7510 if (graph
->n_row
>= graph
->maxvar
)
7514 ctx
= isl_schedule_node_get_ctx(node
);
7515 filters
= extract_sccs(ctx
, graph
);
7517 node
= isl_schedule_node_insert_set(node
, filters
);
7519 node
= isl_schedule_node_insert_sequence(node
, filters
);
7521 for (component
= 0; component
< graph
->scc
; ++component
) {
7522 node
= isl_schedule_node_child(node
, component
);
7523 node
= isl_schedule_node_child(node
, 0);
7524 node
= compute_sub_schedule(node
, ctx
, graph
,
7526 &edge_scc_exactly
, component
, wcc
);
7527 node
= isl_schedule_node_parent(node
);
7528 node
= isl_schedule_node_parent(node
);
7534 /* Compute a schedule for the given dependence graph and insert it at "node".
7535 * Return the updated schedule node.
7537 * We first check if the graph is connected (through validity and conditional
7538 * validity dependences) and, if not, compute a schedule
7539 * for each component separately.
7540 * If the schedule_serialize_sccs option is set, then we check for strongly
7541 * connected components instead and compute a separate schedule for
7542 * each such strongly connected component.
7544 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
7545 struct isl_sched_graph
*graph
)
7552 ctx
= isl_schedule_node_get_ctx(node
);
7553 if (isl_options_get_schedule_serialize_sccs(ctx
)) {
7554 if (detect_sccs(ctx
, graph
) < 0)
7555 return isl_schedule_node_free(node
);
7557 if (detect_wccs(ctx
, graph
) < 0)
7558 return isl_schedule_node_free(node
);
7562 return compute_component_schedule(node
, graph
, 1);
7564 return compute_schedule_wcc(node
, graph
);
7567 /* Compute a schedule on sc->domain that respects the given schedule
7570 * In particular, the schedule respects all the validity dependences.
7571 * If the default isl scheduling algorithm is used, it tries to minimize
7572 * the dependence distances over the proximity dependences.
7573 * If Feautrier's scheduling algorithm is used, the proximity dependence
7574 * distances are only minimized during the extension to a full-dimensional
7577 * If there are any condition and conditional validity dependences,
7578 * then the conditional validity dependences may be violated inside
7579 * a tilable band, provided they have no adjacent non-local
7580 * condition dependences.
7582 __isl_give isl_schedule
*isl_schedule_constraints_compute_schedule(
7583 __isl_take isl_schedule_constraints
*sc
)
7585 isl_ctx
*ctx
= isl_schedule_constraints_get_ctx(sc
);
7586 struct isl_sched_graph graph
= { 0 };
7587 isl_schedule
*sched
;
7588 isl_schedule_node
*node
;
7589 isl_union_set
*domain
;
7592 sc
= isl_schedule_constraints_align_params(sc
);
7594 domain
= isl_schedule_constraints_get_domain(sc
);
7595 n
= isl_union_set_n_set(domain
);
7597 isl_schedule_constraints_free(sc
);
7598 return isl_schedule_from_domain(domain
);
7601 if (n
< 0 || graph_init(&graph
, sc
) < 0)
7602 domain
= isl_union_set_free(domain
);
7604 node
= isl_schedule_node_from_domain(domain
);
7605 node
= isl_schedule_node_child(node
, 0);
7607 node
= compute_schedule(node
, &graph
);
7608 sched
= isl_schedule_node_get_schedule(node
);
7609 isl_schedule_node_free(node
);
7611 graph_free(ctx
, &graph
);
7612 isl_schedule_constraints_free(sc
);
7617 /* Compute a schedule for the given union of domains that respects
7618 * all the validity dependences and minimizes
7619 * the dependence distances over the proximity dependences.
7621 * This function is kept for backward compatibility.
7623 __isl_give isl_schedule
*isl_union_set_compute_schedule(
7624 __isl_take isl_union_set
*domain
,
7625 __isl_take isl_union_map
*validity
,
7626 __isl_take isl_union_map
*proximity
)
7628 isl_schedule_constraints
*sc
;
7630 sc
= isl_schedule_constraints_on_domain(domain
);
7631 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
7632 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
7634 return isl_schedule_constraints_compute_schedule(sc
);