2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2014 Ecole Normale Superieure
4 * Copyright 2015-2016 Sven Verdoolaege
5 * Copyright 2016 INRIA Paris
6 * Copyright 2017 Sven Verdoolaege
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
11 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
13 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
14 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
15 * CS 42112, 75589 Paris Cedex 12, France
18 #include <isl_ctx_private.h>
19 #include <isl_map_private.h>
20 #include <isl_space_private.h>
21 #include <isl_aff_private.h>
23 #include <isl/constraint.h>
24 #include <isl/schedule.h>
25 #include <isl_schedule_constraints.h>
26 #include <isl/schedule_node.h>
27 #include <isl_mat_private.h>
28 #include <isl_vec_private.h>
30 #include <isl/union_set.h>
33 #include <isl_dim_map.h>
34 #include <isl/map_to_basic_set.h>
36 #include <isl_options_private.h>
37 #include <isl_tarjan.h>
38 #include <isl_morph.h>
40 #include <isl_val_private.h>
43 * The scheduling algorithm implemented in this file was inspired by
44 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
45 * Parallelization and Locality Optimization in the Polyhedral Model".
49 /* Internal information about a node that is used during the construction
51 * space represents the original space in which the domain lives;
52 * that is, the space is not affected by compression
53 * sched is a matrix representation of the schedule being constructed
54 * for this node; if compressed is set, then this schedule is
55 * defined over the compressed domain space
56 * sched_map is an isl_map representation of the same (partial) schedule
57 * sched_map may be NULL; if compressed is set, then this map
58 * is defined over the uncompressed domain space
59 * rank is the number of linearly independent rows in the linear part
61 * the columns of cmap represent a change of basis for the schedule
62 * coefficients; the first rank columns span the linear part of
64 * cinv is the inverse of cmap.
65 * ctrans is the transpose of cmap.
66 * start is the first variable in the LP problem in the sequences that
67 * represents the schedule coefficients of this node
68 * nvar is the dimension of the domain
69 * nparam is the number of parameters or 0 if we are not constructing
70 * a parametric schedule
72 * If compressed is set, then hull represents the constraints
73 * that were used to derive the compression, while compress and
74 * decompress map the original space to the compressed space and
77 * scc is the index of SCC (or WCC) this node belongs to
79 * "cluster" is only used inside extract_clusters and identifies
80 * the cluster of SCCs that the node belongs to.
82 * coincident contains a boolean for each of the rows of the schedule,
83 * indicating whether the corresponding scheduling dimension satisfies
84 * the coincidence constraints in the sense that the corresponding
85 * dependence distances are zero.
87 * If the schedule_treat_coalescing option is set, then
88 * "sizes" contains the sizes of the (compressed) instance set
89 * in each direction. If there is no fixed size in a given direction,
90 * then the corresponding size value is set to infinity.
91 * If the schedule_treat_coalescing option or the schedule_max_coefficient
92 * option is set, then "max" contains the maximal values for
93 * schedule coefficients of the (compressed) variables. If no bound
94 * needs to be imposed on a particular variable, then the corresponding
97 struct isl_sched_node
{
101 isl_multi_aff
*compress
;
102 isl_multi_aff
*decompress
;
118 isl_multi_val
*sizes
;
122 static int node_has_tuples(const void *entry
, const void *val
)
124 struct isl_sched_node
*node
= (struct isl_sched_node
*)entry
;
125 isl_space
*space
= (isl_space
*) val
;
127 return isl_space_has_equal_tuples(node
->space
, space
);
130 static int node_scc_exactly(struct isl_sched_node
*node
, int scc
)
132 return node
->scc
== scc
;
135 static int node_scc_at_most(struct isl_sched_node
*node
, int scc
)
137 return node
->scc
<= scc
;
140 static int node_scc_at_least(struct isl_sched_node
*node
, int scc
)
142 return node
->scc
>= scc
;
145 /* An edge in the dependence graph. An edge may be used to
146 * ensure validity of the generated schedule, to minimize the dependence
149 * map is the dependence relation, with i -> j in the map if j depends on i
150 * tagged_condition and tagged_validity contain the union of all tagged
151 * condition or conditional validity dependence relations that
152 * specialize the dependence relation "map"; that is,
153 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
154 * or "tagged_validity", then i -> j is an element of "map".
155 * If these fields are NULL, then they represent the empty relation.
156 * src is the source node
157 * dst is the sink node
159 * types is a bit vector containing the types of this edge.
160 * validity is set if the edge is used to ensure correctness
161 * coincidence is used to enforce zero dependence distances
162 * proximity is set if the edge is used to minimize dependence distances
163 * condition is set if the edge represents a condition
164 * for a conditional validity schedule constraint
165 * local can only be set for condition edges and indicates that
166 * the dependence distance over the edge should be zero
167 * conditional_validity is set if the edge is used to conditionally
170 * For validity edges, start and end mark the sequence of inequality
171 * constraints in the LP problem that encode the validity constraint
172 * corresponding to this edge.
174 * During clustering, an edge may be marked "no_merge" if it should
175 * not be used to merge clusters.
176 * The weight is also only used during clustering and it is
177 * an indication of how many schedule dimensions on either side
178 * of the schedule constraints can be aligned.
179 * If the weight is negative, then this means that this edge was postponed
180 * by has_bounded_distances or any_no_merge. The original weight can
181 * be retrieved by adding 1 + graph->max_weight, with "graph"
182 * the graph containing this edge.
184 struct isl_sched_edge
{
186 isl_union_map
*tagged_condition
;
187 isl_union_map
*tagged_validity
;
189 struct isl_sched_node
*src
;
190 struct isl_sched_node
*dst
;
201 /* Is "edge" marked as being of type "type"?
203 static int is_type(struct isl_sched_edge
*edge
, enum isl_edge_type type
)
205 return ISL_FL_ISSET(edge
->types
, 1 << type
);
208 /* Mark "edge" as being of type "type".
210 static void set_type(struct isl_sched_edge
*edge
, enum isl_edge_type type
)
212 ISL_FL_SET(edge
->types
, 1 << type
);
215 /* No longer mark "edge" as being of type "type"?
217 static void clear_type(struct isl_sched_edge
*edge
, enum isl_edge_type type
)
219 ISL_FL_CLR(edge
->types
, 1 << type
);
222 /* Is "edge" marked as a validity edge?
224 static int is_validity(struct isl_sched_edge
*edge
)
226 return is_type(edge
, isl_edge_validity
);
229 /* Mark "edge" as a validity edge.
231 static void set_validity(struct isl_sched_edge
*edge
)
233 set_type(edge
, isl_edge_validity
);
236 /* Is "edge" marked as a proximity edge?
238 static int is_proximity(struct isl_sched_edge
*edge
)
240 return is_type(edge
, isl_edge_proximity
);
243 /* Is "edge" marked as a local edge?
245 static int is_local(struct isl_sched_edge
*edge
)
247 return is_type(edge
, isl_edge_local
);
250 /* Mark "edge" as a local edge.
252 static void set_local(struct isl_sched_edge
*edge
)
254 set_type(edge
, isl_edge_local
);
257 /* No longer mark "edge" as a local edge.
259 static void clear_local(struct isl_sched_edge
*edge
)
261 clear_type(edge
, isl_edge_local
);
264 /* Is "edge" marked as a coincidence edge?
266 static int is_coincidence(struct isl_sched_edge
*edge
)
268 return is_type(edge
, isl_edge_coincidence
);
271 /* Is "edge" marked as a condition edge?
273 static int is_condition(struct isl_sched_edge
*edge
)
275 return is_type(edge
, isl_edge_condition
);
278 /* Is "edge" marked as a conditional validity edge?
280 static int is_conditional_validity(struct isl_sched_edge
*edge
)
282 return is_type(edge
, isl_edge_conditional_validity
);
285 /* Internal information about the dependence graph used during
286 * the construction of the schedule.
288 * intra_hmap is a cache, mapping dependence relations to their dual,
289 * for dependences from a node to itself
290 * inter_hmap is a cache, mapping dependence relations to their dual,
291 * for dependences between distinct nodes
292 * if compression is involved then the key for these maps
293 * is the original, uncompressed dependence relation, while
294 * the value is the dual of the compressed dependence relation.
296 * n is the number of nodes
297 * node is the list of nodes
298 * maxvar is the maximal number of variables over all nodes
299 * max_row is the allocated number of rows in the schedule
300 * n_row is the current (maximal) number of linearly independent
301 * rows in the node schedules
302 * n_total_row is the current number of rows in the node schedules
303 * band_start is the starting row in the node schedules of the current band
304 * root is set if this graph is the original dependence graph,
305 * without any splitting
307 * sorted contains a list of node indices sorted according to the
308 * SCC to which a node belongs
310 * n_edge is the number of edges
311 * edge is the list of edges
312 * max_edge contains the maximal number of edges of each type;
313 * in particular, it contains the number of edges in the inital graph.
314 * edge_table contains pointers into the edge array, hashed on the source
315 * and sink spaces; there is one such table for each type;
316 * a given edge may be referenced from more than one table
317 * if the corresponding relation appears in more than one of the
318 * sets of dependences; however, for each type there is only
319 * a single edge between a given pair of source and sink space
320 * in the entire graph
322 * node_table contains pointers into the node array, hashed on the space tuples
324 * region contains a list of variable sequences that should be non-trivial
326 * lp contains the (I)LP problem used to obtain new schedule rows
328 * src_scc and dst_scc are the source and sink SCCs of an edge with
329 * conflicting constraints
331 * scc represents the number of components
332 * weak is set if the components are weakly connected
334 * max_weight is used during clustering and represents the maximal
335 * weight of the relevant proximity edges.
337 struct isl_sched_graph
{
338 isl_map_to_basic_set
*intra_hmap
;
339 isl_map_to_basic_set
*inter_hmap
;
341 struct isl_sched_node
*node
;
354 struct isl_sched_edge
*edge
;
356 int max_edge
[isl_edge_last
+ 1];
357 struct isl_hash_table
*edge_table
[isl_edge_last
+ 1];
359 struct isl_hash_table
*node_table
;
360 struct isl_region
*region
;
373 /* Initialize node_table based on the list of nodes.
375 static int graph_init_table(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
379 graph
->node_table
= isl_hash_table_alloc(ctx
, graph
->n
);
380 if (!graph
->node_table
)
383 for (i
= 0; i
< graph
->n
; ++i
) {
384 struct isl_hash_table_entry
*entry
;
387 hash
= isl_space_get_tuple_hash(graph
->node
[i
].space
);
388 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
390 graph
->node
[i
].space
, 1);
393 entry
->data
= &graph
->node
[i
];
399 /* Return a pointer to the node that lives within the given space,
400 * or NULL if there is no such node.
402 static struct isl_sched_node
*graph_find_node(isl_ctx
*ctx
,
403 struct isl_sched_graph
*graph
, __isl_keep isl_space
*space
)
405 struct isl_hash_table_entry
*entry
;
408 hash
= isl_space_get_tuple_hash(space
);
409 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
410 &node_has_tuples
, space
, 0);
412 return entry
? entry
->data
: NULL
;
415 static int edge_has_src_and_dst(const void *entry
, const void *val
)
417 const struct isl_sched_edge
*edge
= entry
;
418 const struct isl_sched_edge
*temp
= val
;
420 return edge
->src
== temp
->src
&& edge
->dst
== temp
->dst
;
423 /* Add the given edge to graph->edge_table[type].
425 static isl_stat
graph_edge_table_add(isl_ctx
*ctx
,
426 struct isl_sched_graph
*graph
, enum isl_edge_type type
,
427 struct isl_sched_edge
*edge
)
429 struct isl_hash_table_entry
*entry
;
432 hash
= isl_hash_init();
433 hash
= isl_hash_builtin(hash
, edge
->src
);
434 hash
= isl_hash_builtin(hash
, edge
->dst
);
435 entry
= isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
436 &edge_has_src_and_dst
, edge
, 1);
438 return isl_stat_error
;
444 /* Allocate the edge_tables based on the maximal number of edges of
447 static int graph_init_edge_tables(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
451 for (i
= 0; i
<= isl_edge_last
; ++i
) {
452 graph
->edge_table
[i
] = isl_hash_table_alloc(ctx
,
454 if (!graph
->edge_table
[i
])
461 /* If graph->edge_table[type] contains an edge from the given source
462 * to the given destination, then return the hash table entry of this edge.
463 * Otherwise, return NULL.
465 static struct isl_hash_table_entry
*graph_find_edge_entry(
466 struct isl_sched_graph
*graph
,
467 enum isl_edge_type type
,
468 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
470 isl_ctx
*ctx
= isl_space_get_ctx(src
->space
);
472 struct isl_sched_edge temp
= { .src
= src
, .dst
= dst
};
474 hash
= isl_hash_init();
475 hash
= isl_hash_builtin(hash
, temp
.src
);
476 hash
= isl_hash_builtin(hash
, temp
.dst
);
477 return isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
478 &edge_has_src_and_dst
, &temp
, 0);
482 /* If graph->edge_table[type] contains an edge from the given source
483 * to the given destination, then return this edge.
484 * Otherwise, return NULL.
486 static struct isl_sched_edge
*graph_find_edge(struct isl_sched_graph
*graph
,
487 enum isl_edge_type type
,
488 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
490 struct isl_hash_table_entry
*entry
;
492 entry
= graph_find_edge_entry(graph
, type
, src
, dst
);
499 /* Check whether the dependence graph has an edge of the given type
500 * between the given two nodes.
502 static isl_bool
graph_has_edge(struct isl_sched_graph
*graph
,
503 enum isl_edge_type type
,
504 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
506 struct isl_sched_edge
*edge
;
509 edge
= graph_find_edge(graph
, type
, src
, dst
);
513 empty
= isl_map_plain_is_empty(edge
->map
);
515 return isl_bool_error
;
520 /* Look for any edge with the same src, dst and map fields as "model".
522 * Return the matching edge if one can be found.
523 * Return "model" if no matching edge is found.
524 * Return NULL on error.
526 static struct isl_sched_edge
*graph_find_matching_edge(
527 struct isl_sched_graph
*graph
, struct isl_sched_edge
*model
)
529 enum isl_edge_type i
;
530 struct isl_sched_edge
*edge
;
532 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
535 edge
= graph_find_edge(graph
, i
, model
->src
, model
->dst
);
538 is_equal
= isl_map_plain_is_equal(model
->map
, edge
->map
);
548 /* Remove the given edge from all the edge_tables that refer to it.
550 static void graph_remove_edge(struct isl_sched_graph
*graph
,
551 struct isl_sched_edge
*edge
)
553 isl_ctx
*ctx
= isl_map_get_ctx(edge
->map
);
554 enum isl_edge_type i
;
556 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
557 struct isl_hash_table_entry
*entry
;
559 entry
= graph_find_edge_entry(graph
, i
, edge
->src
, edge
->dst
);
562 if (entry
->data
!= edge
)
564 isl_hash_table_remove(ctx
, graph
->edge_table
[i
], entry
);
568 /* Check whether the dependence graph has any edge
569 * between the given two nodes.
571 static isl_bool
graph_has_any_edge(struct isl_sched_graph
*graph
,
572 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
574 enum isl_edge_type i
;
577 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
578 r
= graph_has_edge(graph
, i
, src
, dst
);
586 /* Check whether the dependence graph has a validity edge
587 * between the given two nodes.
589 * Conditional validity edges are essentially validity edges that
590 * can be ignored if the corresponding condition edges are iteration private.
591 * Here, we are only checking for the presence of validity
592 * edges, so we need to consider the conditional validity edges too.
593 * In particular, this function is used during the detection
594 * of strongly connected components and we cannot ignore
595 * conditional validity edges during this detection.
597 static isl_bool
graph_has_validity_edge(struct isl_sched_graph
*graph
,
598 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
602 r
= graph_has_edge(graph
, isl_edge_validity
, src
, dst
);
606 return graph_has_edge(graph
, isl_edge_conditional_validity
, src
, dst
);
609 static int graph_alloc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
610 int n_node
, int n_edge
)
615 graph
->n_edge
= n_edge
;
616 graph
->node
= isl_calloc_array(ctx
, struct isl_sched_node
, graph
->n
);
617 graph
->sorted
= isl_calloc_array(ctx
, int, graph
->n
);
618 graph
->region
= isl_alloc_array(ctx
, struct isl_region
, graph
->n
);
619 graph
->edge
= isl_calloc_array(ctx
,
620 struct isl_sched_edge
, graph
->n_edge
);
622 graph
->intra_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
623 graph
->inter_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
625 if (!graph
->node
|| !graph
->region
|| (graph
->n_edge
&& !graph
->edge
) ||
629 for(i
= 0; i
< graph
->n
; ++i
)
630 graph
->sorted
[i
] = i
;
635 static void graph_free(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
639 isl_map_to_basic_set_free(graph
->intra_hmap
);
640 isl_map_to_basic_set_free(graph
->inter_hmap
);
643 for (i
= 0; i
< graph
->n
; ++i
) {
644 isl_space_free(graph
->node
[i
].space
);
645 isl_set_free(graph
->node
[i
].hull
);
646 isl_multi_aff_free(graph
->node
[i
].compress
);
647 isl_multi_aff_free(graph
->node
[i
].decompress
);
648 isl_mat_free(graph
->node
[i
].sched
);
649 isl_map_free(graph
->node
[i
].sched_map
);
650 isl_mat_free(graph
->node
[i
].cmap
);
651 isl_mat_free(graph
->node
[i
].cinv
);
652 isl_mat_free(graph
->node
[i
].ctrans
);
654 free(graph
->node
[i
].coincident
);
655 isl_multi_val_free(graph
->node
[i
].sizes
);
656 isl_vec_free(graph
->node
[i
].max
);
661 for (i
= 0; i
< graph
->n_edge
; ++i
) {
662 isl_map_free(graph
->edge
[i
].map
);
663 isl_union_map_free(graph
->edge
[i
].tagged_condition
);
664 isl_union_map_free(graph
->edge
[i
].tagged_validity
);
668 for (i
= 0; i
<= isl_edge_last
; ++i
)
669 isl_hash_table_free(ctx
, graph
->edge_table
[i
]);
670 isl_hash_table_free(ctx
, graph
->node_table
);
671 isl_basic_set_free(graph
->lp
);
674 /* For each "set" on which this function is called, increment
675 * graph->n by one and update graph->maxvar.
677 static isl_stat
init_n_maxvar(__isl_take isl_set
*set
, void *user
)
679 struct isl_sched_graph
*graph
= user
;
680 int nvar
= isl_set_dim(set
, isl_dim_set
);
683 if (nvar
> graph
->maxvar
)
684 graph
->maxvar
= nvar
;
691 /* Compute the number of rows that should be allocated for the schedule.
692 * In particular, we need one row for each variable or one row
693 * for each basic map in the dependences.
694 * Note that it is practically impossible to exhaust both
695 * the number of dependences and the number of variables.
697 static isl_stat
compute_max_row(struct isl_sched_graph
*graph
,
698 __isl_keep isl_schedule_constraints
*sc
)
702 isl_union_set
*domain
;
706 domain
= isl_schedule_constraints_get_domain(sc
);
707 r
= isl_union_set_foreach_set(domain
, &init_n_maxvar
, graph
);
708 isl_union_set_free(domain
);
710 return isl_stat_error
;
711 n_edge
= isl_schedule_constraints_n_basic_map(sc
);
713 return isl_stat_error
;
714 graph
->max_row
= n_edge
+ graph
->maxvar
;
719 /* Does "bset" have any defining equalities for its set variables?
721 static isl_bool
has_any_defining_equality(__isl_keep isl_basic_set
*bset
)
726 return isl_bool_error
;
728 n
= isl_basic_set_dim(bset
, isl_dim_set
);
729 for (i
= 0; i
< n
; ++i
) {
732 has
= isl_basic_set_has_defining_equality(bset
, isl_dim_set
, i
,
738 return isl_bool_false
;
741 /* Set the entries of node->max to the value of the schedule_max_coefficient
744 static isl_stat
set_max_coefficient(isl_ctx
*ctx
, struct isl_sched_node
*node
)
748 max
= isl_options_get_schedule_max_coefficient(ctx
);
752 node
->max
= isl_vec_alloc(ctx
, node
->nvar
);
753 node
->max
= isl_vec_set_si(node
->max
, max
);
755 return isl_stat_error
;
760 /* Set the entries of node->max to the minimum of the schedule_max_coefficient
761 * option (if set) and half of the minimum of the sizes in the other
762 * dimensions. If the minimum of the sizes is one, half of the size
763 * is zero and this value is reset to one.
764 * If the global minimum is unbounded (i.e., if both
765 * the schedule_max_coefficient is not set and the sizes in the other
766 * dimensions are unbounded), then store a negative value.
767 * If the schedule coefficient is close to the size of the instance set
768 * in another dimension, then the schedule may represent a loop
769 * coalescing transformation (especially if the coefficient
770 * in that other dimension is one). Forcing the coefficient to be
771 * smaller than or equal to half the minimal size should avoid this
774 static isl_stat
compute_max_coefficient(isl_ctx
*ctx
,
775 struct isl_sched_node
*node
)
781 max
= isl_options_get_schedule_max_coefficient(ctx
);
782 v
= isl_vec_alloc(ctx
, node
->nvar
);
784 return isl_stat_error
;
786 for (i
= 0; i
< node
->nvar
; ++i
) {
787 isl_int_set_si(v
->el
[i
], max
);
788 isl_int_mul_si(v
->el
[i
], v
->el
[i
], 2);
791 for (i
= 0; i
< node
->nvar
; ++i
) {
794 size
= isl_multi_val_get_val(node
->sizes
, i
);
797 if (!isl_val_is_int(size
)) {
801 for (j
= 0; j
< node
->nvar
; ++j
) {
804 if (isl_int_is_neg(v
->el
[j
]) ||
805 isl_int_gt(v
->el
[j
], size
->n
))
806 isl_int_set(v
->el
[j
], size
->n
);
811 for (i
= 0; i
< node
->nvar
; ++i
) {
812 isl_int_fdiv_q_ui(v
->el
[i
], v
->el
[i
], 2);
813 if (isl_int_is_zero(v
->el
[i
]))
814 isl_int_set_si(v
->el
[i
], 1);
821 return isl_stat_error
;
824 /* Compute and return the size of "set" in dimension "dim".
825 * The size is taken to be the difference in values for that variable
826 * for fixed values of the other variables.
827 * In particular, the variable is first isolated from the other variables
828 * in the range of a map
830 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [i_dim]
832 * and then duplicated
834 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [[i_dim] -> [i_dim']]
836 * The shared variables are then projected out and the maximal value
837 * of i_dim' - i_dim is computed.
839 static __isl_give isl_val
*compute_size(__isl_take isl_set
*set
, int dim
)
846 map
= isl_set_project_onto_map(set
, isl_dim_set
, dim
, 1);
847 map
= isl_map_project_out(map
, isl_dim_in
, dim
, 1);
848 map
= isl_map_range_product(map
, isl_map_copy(map
));
849 map
= isl_set_unwrap(isl_map_range(map
));
850 set
= isl_map_deltas(map
);
851 ls
= isl_local_space_from_space(isl_set_get_space(set
));
852 obj
= isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
853 v
= isl_set_max_val(set
, obj
);
860 /* Compute the size of the instance set "set" of "node", after compression,
861 * as well as bounds on the corresponding coefficients, if needed.
863 * The sizes are needed when the schedule_treat_coalescing option is set.
864 * The bounds are needed when the schedule_treat_coalescing option or
865 * the schedule_max_coefficient option is set.
867 * If the schedule_treat_coalescing option is not set, then at most
868 * the bounds need to be set and this is done in set_max_coefficient.
869 * Otherwise, compress the domain if needed, compute the size
870 * in each direction and store the results in node->size.
871 * Finally, set the bounds on the coefficients based on the sizes
872 * and the schedule_max_coefficient option in compute_max_coefficient.
874 static isl_stat
compute_sizes_and_max(isl_ctx
*ctx
, struct isl_sched_node
*node
,
875 __isl_take isl_set
*set
)
880 if (!isl_options_get_schedule_treat_coalescing(ctx
)) {
882 return set_max_coefficient(ctx
, node
);
885 if (node
->compressed
)
886 set
= isl_set_preimage_multi_aff(set
,
887 isl_multi_aff_copy(node
->decompress
));
888 mv
= isl_multi_val_zero(isl_set_get_space(set
));
889 n
= isl_set_dim(set
, isl_dim_set
);
890 for (j
= 0; j
< n
; ++j
) {
893 v
= compute_size(isl_set_copy(set
), j
);
894 mv
= isl_multi_val_set_val(mv
, j
, v
);
899 return isl_stat_error
;
900 return compute_max_coefficient(ctx
, node
);
903 /* Add a new node to the graph representing the given instance set.
904 * "nvar" is the (possibly compressed) number of variables and
905 * may be smaller than then number of set variables in "set"
906 * if "compressed" is set.
907 * If "compressed" is set, then "hull" represents the constraints
908 * that were used to derive the compression, while "compress" and
909 * "decompress" map the original space to the compressed space and
911 * If "compressed" is not set, then "hull", "compress" and "decompress"
914 * Compute the size of the instance set and bounds on the coefficients,
917 static isl_stat
add_node(struct isl_sched_graph
*graph
,
918 __isl_take isl_set
*set
, int nvar
, int compressed
,
919 __isl_take isl_set
*hull
, __isl_take isl_multi_aff
*compress
,
920 __isl_take isl_multi_aff
*decompress
)
927 struct isl_sched_node
*node
;
930 return isl_stat_error
;
932 ctx
= isl_set_get_ctx(set
);
933 nparam
= isl_set_dim(set
, isl_dim_param
);
934 if (!ctx
->opt
->schedule_parametric
)
936 sched
= isl_mat_alloc(ctx
, 0, 1 + nparam
+ nvar
);
937 node
= &graph
->node
[graph
->n
];
939 space
= isl_set_get_space(set
);
942 node
->nparam
= nparam
;
944 node
->sched_map
= NULL
;
945 coincident
= isl_calloc_array(ctx
, int, graph
->max_row
);
946 node
->coincident
= coincident
;
947 node
->compressed
= compressed
;
949 node
->compress
= compress
;
950 node
->decompress
= decompress
;
951 if (compute_sizes_and_max(ctx
, node
, set
) < 0)
952 return isl_stat_error
;
954 if (!space
|| !sched
|| (graph
->max_row
&& !coincident
))
955 return isl_stat_error
;
956 if (compressed
&& (!hull
|| !compress
|| !decompress
))
957 return isl_stat_error
;
962 /* Construct an identifier for node "node", which will represent "set".
963 * The name of the identifier is either "compressed" or
964 * "compressed_<name>", with <name> the name of the space of "set".
965 * The user pointer of the identifier points to "node".
967 static __isl_give isl_id
*construct_compressed_id(__isl_keep isl_set
*set
,
968 struct isl_sched_node
*node
)
977 has_name
= isl_set_has_tuple_name(set
);
981 ctx
= isl_set_get_ctx(set
);
983 return isl_id_alloc(ctx
, "compressed", node
);
985 p
= isl_printer_to_str(ctx
);
986 name
= isl_set_get_tuple_name(set
);
987 p
= isl_printer_print_str(p
, "compressed_");
988 p
= isl_printer_print_str(p
, name
);
989 id_name
= isl_printer_get_str(p
);
992 id
= isl_id_alloc(ctx
, id_name
, node
);
998 /* Add a new node to the graph representing the given set.
1000 * If any of the set variables is defined by an equality, then
1001 * we perform variable compression such that we can perform
1002 * the scheduling on the compressed domain.
1003 * In this case, an identifier is used that references the new node
1004 * such that each compressed space is unique and
1005 * such that the node can be recovered from the compressed space.
1007 static isl_stat
extract_node(__isl_take isl_set
*set
, void *user
)
1010 isl_bool has_equality
;
1012 isl_basic_set
*hull
;
1015 isl_multi_aff
*compress
, *decompress
;
1016 struct isl_sched_graph
*graph
= user
;
1018 hull
= isl_set_affine_hull(isl_set_copy(set
));
1019 hull
= isl_basic_set_remove_divs(hull
);
1020 nvar
= isl_set_dim(set
, isl_dim_set
);
1021 has_equality
= has_any_defining_equality(hull
);
1023 if (has_equality
< 0)
1025 if (!has_equality
) {
1026 isl_basic_set_free(hull
);
1027 return add_node(graph
, set
, nvar
, 0, NULL
, NULL
, NULL
);
1030 id
= construct_compressed_id(set
, &graph
->node
[graph
->n
]);
1031 morph
= isl_basic_set_variable_compression_with_id(hull
,
1034 nvar
= isl_morph_ran_dim(morph
, isl_dim_set
);
1035 compress
= isl_morph_get_var_multi_aff(morph
);
1036 morph
= isl_morph_inverse(morph
);
1037 decompress
= isl_morph_get_var_multi_aff(morph
);
1038 isl_morph_free(morph
);
1040 hull_set
= isl_set_from_basic_set(hull
);
1041 return add_node(graph
, set
, nvar
, 1, hull_set
, compress
, decompress
);
1043 isl_basic_set_free(hull
);
1045 return isl_stat_error
;
1048 struct isl_extract_edge_data
{
1049 enum isl_edge_type type
;
1050 struct isl_sched_graph
*graph
;
1053 /* Merge edge2 into edge1, freeing the contents of edge2.
1054 * Return 0 on success and -1 on failure.
1056 * edge1 and edge2 are assumed to have the same value for the map field.
1058 static int merge_edge(struct isl_sched_edge
*edge1
,
1059 struct isl_sched_edge
*edge2
)
1061 edge1
->types
|= edge2
->types
;
1062 isl_map_free(edge2
->map
);
1064 if (is_condition(edge2
)) {
1065 if (!edge1
->tagged_condition
)
1066 edge1
->tagged_condition
= edge2
->tagged_condition
;
1068 edge1
->tagged_condition
=
1069 isl_union_map_union(edge1
->tagged_condition
,
1070 edge2
->tagged_condition
);
1073 if (is_conditional_validity(edge2
)) {
1074 if (!edge1
->tagged_validity
)
1075 edge1
->tagged_validity
= edge2
->tagged_validity
;
1077 edge1
->tagged_validity
=
1078 isl_union_map_union(edge1
->tagged_validity
,
1079 edge2
->tagged_validity
);
1082 if (is_condition(edge2
) && !edge1
->tagged_condition
)
1084 if (is_conditional_validity(edge2
) && !edge1
->tagged_validity
)
1090 /* Insert dummy tags in domain and range of "map".
1092 * In particular, if "map" is of the form
1098 * [A -> dummy_tag] -> [B -> dummy_tag]
1100 * where the dummy_tags are identical and equal to any dummy tags
1101 * introduced by any other call to this function.
1103 static __isl_give isl_map
*insert_dummy_tags(__isl_take isl_map
*map
)
1109 isl_set
*domain
, *range
;
1111 ctx
= isl_map_get_ctx(map
);
1113 id
= isl_id_alloc(ctx
, NULL
, &dummy
);
1114 space
= isl_space_params(isl_map_get_space(map
));
1115 space
= isl_space_set_from_params(space
);
1116 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
1117 space
= isl_space_map_from_set(space
);
1119 domain
= isl_map_wrap(map
);
1120 range
= isl_map_wrap(isl_map_universe(space
));
1121 map
= isl_map_from_domain_and_range(domain
, range
);
1122 map
= isl_map_zip(map
);
1127 /* Given that at least one of "src" or "dst" is compressed, return
1128 * a map between the spaces of these nodes restricted to the affine
1129 * hull that was used in the compression.
1131 static __isl_give isl_map
*extract_hull(struct isl_sched_node
*src
,
1132 struct isl_sched_node
*dst
)
1136 if (src
->compressed
)
1137 dom
= isl_set_copy(src
->hull
);
1139 dom
= isl_set_universe(isl_space_copy(src
->space
));
1140 if (dst
->compressed
)
1141 ran
= isl_set_copy(dst
->hull
);
1143 ran
= isl_set_universe(isl_space_copy(dst
->space
));
1145 return isl_map_from_domain_and_range(dom
, ran
);
1148 /* Intersect the domains of the nested relations in domain and range
1149 * of "tagged" with "map".
1151 static __isl_give isl_map
*map_intersect_domains(__isl_take isl_map
*tagged
,
1152 __isl_keep isl_map
*map
)
1156 tagged
= isl_map_zip(tagged
);
1157 set
= isl_map_wrap(isl_map_copy(map
));
1158 tagged
= isl_map_intersect_domain(tagged
, set
);
1159 tagged
= isl_map_zip(tagged
);
1163 /* Return a pointer to the node that lives in the domain space of "map"
1164 * or NULL if there is no such node.
1166 static struct isl_sched_node
*find_domain_node(isl_ctx
*ctx
,
1167 struct isl_sched_graph
*graph
, __isl_keep isl_map
*map
)
1169 struct isl_sched_node
*node
;
1172 space
= isl_space_domain(isl_map_get_space(map
));
1173 node
= graph_find_node(ctx
, graph
, space
);
1174 isl_space_free(space
);
1179 /* Return a pointer to the node that lives in the range space of "map"
1180 * or NULL if there is no such node.
1182 static struct isl_sched_node
*find_range_node(isl_ctx
*ctx
,
1183 struct isl_sched_graph
*graph
, __isl_keep isl_map
*map
)
1185 struct isl_sched_node
*node
;
1188 space
= isl_space_range(isl_map_get_space(map
));
1189 node
= graph_find_node(ctx
, graph
, space
);
1190 isl_space_free(space
);
1195 /* Add a new edge to the graph based on the given map
1196 * and add it to data->graph->edge_table[data->type].
1197 * If a dependence relation of a given type happens to be identical
1198 * to one of the dependence relations of a type that was added before,
1199 * then we don't create a new edge, but instead mark the original edge
1200 * as also representing a dependence of the current type.
1202 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1203 * may be specified as "tagged" dependence relations. That is, "map"
1204 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1205 * the dependence on iterations and a and b are tags.
1206 * edge->map is set to the relation containing the elements i -> j,
1207 * while edge->tagged_condition and edge->tagged_validity contain
1208 * the union of all the "map" relations
1209 * for which extract_edge is called that result in the same edge->map.
1211 * If the source or the destination node is compressed, then
1212 * intersect both "map" and "tagged" with the constraints that
1213 * were used to construct the compression.
1214 * This ensures that there are no schedule constraints defined
1215 * outside of these domains, while the scheduler no longer has
1216 * any control over those outside parts.
1218 static isl_stat
extract_edge(__isl_take isl_map
*map
, void *user
)
1220 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1221 struct isl_extract_edge_data
*data
= user
;
1222 struct isl_sched_graph
*graph
= data
->graph
;
1223 struct isl_sched_node
*src
, *dst
;
1224 struct isl_sched_edge
*edge
;
1225 isl_map
*tagged
= NULL
;
1227 if (data
->type
== isl_edge_condition
||
1228 data
->type
== isl_edge_conditional_validity
) {
1229 if (isl_map_can_zip(map
)) {
1230 tagged
= isl_map_copy(map
);
1231 map
= isl_set_unwrap(isl_map_domain(isl_map_zip(map
)));
1233 tagged
= insert_dummy_tags(isl_map_copy(map
));
1237 src
= find_domain_node(ctx
, graph
, map
);
1238 dst
= find_range_node(ctx
, graph
, map
);
1242 isl_map_free(tagged
);
1246 if (src
->compressed
|| dst
->compressed
) {
1248 hull
= extract_hull(src
, dst
);
1250 tagged
= map_intersect_domains(tagged
, hull
);
1251 map
= isl_map_intersect(map
, hull
);
1254 graph
->edge
[graph
->n_edge
].src
= src
;
1255 graph
->edge
[graph
->n_edge
].dst
= dst
;
1256 graph
->edge
[graph
->n_edge
].map
= map
;
1257 graph
->edge
[graph
->n_edge
].types
= 0;
1258 graph
->edge
[graph
->n_edge
].tagged_condition
= NULL
;
1259 graph
->edge
[graph
->n_edge
].tagged_validity
= NULL
;
1260 set_type(&graph
->edge
[graph
->n_edge
], data
->type
);
1261 if (data
->type
== isl_edge_condition
)
1262 graph
->edge
[graph
->n_edge
].tagged_condition
=
1263 isl_union_map_from_map(tagged
);
1264 if (data
->type
== isl_edge_conditional_validity
)
1265 graph
->edge
[graph
->n_edge
].tagged_validity
=
1266 isl_union_map_from_map(tagged
);
1268 edge
= graph_find_matching_edge(graph
, &graph
->edge
[graph
->n_edge
]);
1271 return isl_stat_error
;
1273 if (edge
== &graph
->edge
[graph
->n_edge
])
1274 return graph_edge_table_add(ctx
, graph
, data
->type
,
1275 &graph
->edge
[graph
->n_edge
++]);
1277 if (merge_edge(edge
, &graph
->edge
[graph
->n_edge
]) < 0)
1280 return graph_edge_table_add(ctx
, graph
, data
->type
, edge
);
1283 /* Initialize the schedule graph "graph" from the schedule constraints "sc".
1285 * The context is included in the domain before the nodes of
1286 * the graphs are extracted in order to be able to exploit
1287 * any possible additional equalities.
1288 * Note that this intersection is only performed locally here.
1290 static isl_stat
graph_init(struct isl_sched_graph
*graph
,
1291 __isl_keep isl_schedule_constraints
*sc
)
1294 isl_union_set
*domain
;
1296 struct isl_extract_edge_data data
;
1297 enum isl_edge_type i
;
1301 return isl_stat_error
;
1303 ctx
= isl_schedule_constraints_get_ctx(sc
);
1305 domain
= isl_schedule_constraints_get_domain(sc
);
1306 graph
->n
= isl_union_set_n_set(domain
);
1307 isl_union_set_free(domain
);
1309 if (graph_alloc(ctx
, graph
, graph
->n
,
1310 isl_schedule_constraints_n_map(sc
)) < 0)
1311 return isl_stat_error
;
1313 if (compute_max_row(graph
, sc
) < 0)
1314 return isl_stat_error
;
1317 domain
= isl_schedule_constraints_get_domain(sc
);
1318 domain
= isl_union_set_intersect_params(domain
,
1319 isl_schedule_constraints_get_context(sc
));
1320 r
= isl_union_set_foreach_set(domain
, &extract_node
, graph
);
1321 isl_union_set_free(domain
);
1323 return isl_stat_error
;
1324 if (graph_init_table(ctx
, graph
) < 0)
1325 return isl_stat_error
;
1326 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
1327 c
= isl_schedule_constraints_get(sc
, i
);
1328 graph
->max_edge
[i
] = isl_union_map_n_map(c
);
1329 isl_union_map_free(c
);
1331 return isl_stat_error
;
1333 if (graph_init_edge_tables(ctx
, graph
) < 0)
1334 return isl_stat_error
;
1337 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
1341 c
= isl_schedule_constraints_get(sc
, i
);
1342 r
= isl_union_map_foreach_map(c
, &extract_edge
, &data
);
1343 isl_union_map_free(c
);
1345 return isl_stat_error
;
1351 /* Check whether there is any dependence from node[j] to node[i]
1352 * or from node[i] to node[j].
1354 static isl_bool
node_follows_weak(int i
, int j
, void *user
)
1357 struct isl_sched_graph
*graph
= user
;
1359 f
= graph_has_any_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1362 return graph_has_any_edge(graph
, &graph
->node
[i
], &graph
->node
[j
]);
1365 /* Check whether there is a (conditional) validity dependence from node[j]
1366 * to node[i], forcing node[i] to follow node[j].
1368 static isl_bool
node_follows_strong(int i
, int j
, void *user
)
1370 struct isl_sched_graph
*graph
= user
;
1372 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1375 /* Use Tarjan's algorithm for computing the strongly connected components
1376 * in the dependence graph only considering those edges defined by "follows".
1378 static int detect_ccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
1379 isl_bool (*follows
)(int i
, int j
, void *user
))
1382 struct isl_tarjan_graph
*g
= NULL
;
1384 g
= isl_tarjan_graph_init(ctx
, graph
->n
, follows
, graph
);
1392 while (g
->order
[i
] != -1) {
1393 graph
->node
[g
->order
[i
]].scc
= graph
->scc
;
1401 isl_tarjan_graph_free(g
);
1406 /* Apply Tarjan's algorithm to detect the strongly connected components
1407 * in the dependence graph.
1408 * Only consider the (conditional) validity dependences and clear "weak".
1410 static int detect_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1413 return detect_ccs(ctx
, graph
, &node_follows_strong
);
1416 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1417 * in the dependence graph.
1418 * Consider all dependences and set "weak".
1420 static int detect_wccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1423 return detect_ccs(ctx
, graph
, &node_follows_weak
);
1426 static int cmp_scc(const void *a
, const void *b
, void *data
)
1428 struct isl_sched_graph
*graph
= data
;
1432 return graph
->node
[*i1
].scc
- graph
->node
[*i2
].scc
;
1435 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1437 static int sort_sccs(struct isl_sched_graph
*graph
)
1439 return isl_sort(graph
->sorted
, graph
->n
, sizeof(int), &cmp_scc
, graph
);
1442 /* Given a dependence relation R from "node" to itself,
1443 * construct the set of coefficients of valid constraints for elements
1444 * in that dependence relation.
1445 * In particular, the result contains tuples of coefficients
1446 * c_0, c_n, c_x such that
1448 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1452 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1454 * We choose here to compute the dual of delta R.
1455 * Alternatively, we could have computed the dual of R, resulting
1456 * in a set of tuples c_0, c_n, c_x, c_y, and then
1457 * plugged in (c_0, c_n, c_x, -c_x).
1459 * If "node" has been compressed, then the dependence relation
1460 * is also compressed before the set of coefficients is computed.
1462 static __isl_give isl_basic_set
*intra_coefficients(
1463 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1464 __isl_take isl_map
*map
)
1468 isl_basic_set
*coef
;
1469 isl_maybe_isl_basic_set m
;
1471 m
= isl_map_to_basic_set_try_get(graph
->intra_hmap
, map
);
1472 if (m
.valid
< 0 || m
.valid
) {
1477 key
= isl_map_copy(map
);
1478 if (node
->compressed
) {
1479 map
= isl_map_preimage_domain_multi_aff(map
,
1480 isl_multi_aff_copy(node
->decompress
));
1481 map
= isl_map_preimage_range_multi_aff(map
,
1482 isl_multi_aff_copy(node
->decompress
));
1484 delta
= isl_set_remove_divs(isl_map_deltas(map
));
1485 coef
= isl_set_coefficients(delta
);
1486 graph
->intra_hmap
= isl_map_to_basic_set_set(graph
->intra_hmap
, key
,
1487 isl_basic_set_copy(coef
));
1492 /* Given a dependence relation R, construct the set of coefficients
1493 * of valid constraints for elements in that dependence relation.
1494 * In particular, the result contains tuples of coefficients
1495 * c_0, c_n, c_x, c_y such that
1497 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1499 * If the source or destination nodes of "edge" have been compressed,
1500 * then the dependence relation is also compressed before
1501 * the set of coefficients is computed.
1503 static __isl_give isl_basic_set
*inter_coefficients(
1504 struct isl_sched_graph
*graph
, struct isl_sched_edge
*edge
,
1505 __isl_take isl_map
*map
)
1509 isl_basic_set
*coef
;
1510 isl_maybe_isl_basic_set m
;
1512 m
= isl_map_to_basic_set_try_get(graph
->inter_hmap
, map
);
1513 if (m
.valid
< 0 || m
.valid
) {
1518 key
= isl_map_copy(map
);
1519 if (edge
->src
->compressed
)
1520 map
= isl_map_preimage_domain_multi_aff(map
,
1521 isl_multi_aff_copy(edge
->src
->decompress
));
1522 if (edge
->dst
->compressed
)
1523 map
= isl_map_preimage_range_multi_aff(map
,
1524 isl_multi_aff_copy(edge
->dst
->decompress
));
1525 set
= isl_map_wrap(isl_map_remove_divs(map
));
1526 coef
= isl_set_coefficients(set
);
1527 graph
->inter_hmap
= isl_map_to_basic_set_set(graph
->inter_hmap
, key
,
1528 isl_basic_set_copy(coef
));
1533 /* Return the position of the coefficients of the variables in
1534 * the coefficients constraints "coef".
1536 * The space of "coef" is of the form
1538 * { coefficients[[cst, params] -> S] }
1540 * Return the position of S.
1542 static int coef_var_offset(__isl_keep isl_basic_set
*coef
)
1547 space
= isl_space_unwrap(isl_basic_set_get_space(coef
));
1548 offset
= isl_space_dim(space
, isl_dim_in
);
1549 isl_space_free(space
);
1554 /* Return the offset of the coefficients of the variables of "node"
1557 * Within each node, the coefficients have the following order:
1559 * - c_i_n (if parametric)
1560 * - positive and negative parts of c_i_x
1562 static int node_var_coef_offset(struct isl_sched_node
*node
)
1564 return node
->start
+ 1 + node
->nparam
;
1567 /* Construct an isl_dim_map for mapping constraints on coefficients
1568 * for "node" to the corresponding positions in graph->lp.
1569 * "offset" is the offset of the coefficients for the variables
1570 * in the input constraints.
1571 * "s" is the sign of the mapping.
1573 * The input constraints are given in terms of the coefficients (c_0, c_n, c_x).
1574 * The mapping produced by this function essentially plugs in
1575 * (0, 0, c_i_x^+ - c_i_x^-) if s = 1 and
1576 * (0, 0, -c_i_x^+ + c_i_x^-) if s = -1.
1577 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1579 * The caller can extend the mapping to also map the other coefficients
1580 * (and therefore not plug in 0).
1582 static __isl_give isl_dim_map
*intra_dim_map(isl_ctx
*ctx
,
1583 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1588 isl_dim_map
*dim_map
;
1593 total
= isl_basic_set_total_dim(graph
->lp
);
1594 pos
= node_var_coef_offset(node
);
1595 dim_map
= isl_dim_map_alloc(ctx
, total
);
1596 isl_dim_map_range(dim_map
, pos
, 2, offset
, 1, node
->nvar
, -s
);
1597 isl_dim_map_range(dim_map
, pos
+ 1, 2, offset
, 1, node
->nvar
, s
);
1602 /* Construct an isl_dim_map for mapping constraints on coefficients
1603 * for "src" (node i) and "dst" (node j) to the corresponding positions
1605 * "offset" is the offset of the coefficients for the variables of "src"
1606 * in the input constraints.
1607 * "s" is the sign of the mapping.
1609 * The input constraints are given in terms of the coefficients
1610 * (c_0, c_n, c_x, c_y).
1611 * The mapping produced by this function essentially plugs in
1612 * (c_j_0 - c_i_0, c_j_n - c_i_n,
1613 * -(c_i_x^+ - c_i_x^-), c_j_x^+ - c_j_x^-) if s = 1 and
1614 * (-c_j_0 + c_i_0, -c_j_n + c_i_n,
1615 * c_i_x^+ - c_i_x^-, -(c_j_x^+ - c_j_x^-)) if s = -1.
1616 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1618 * The caller can further extend the mapping.
1620 static __isl_give isl_dim_map
*inter_dim_map(isl_ctx
*ctx
,
1621 struct isl_sched_graph
*graph
, struct isl_sched_node
*src
,
1622 struct isl_sched_node
*dst
, int offset
, int s
)
1626 isl_dim_map
*dim_map
;
1631 total
= isl_basic_set_total_dim(graph
->lp
);
1632 dim_map
= isl_dim_map_alloc(ctx
, total
);
1634 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, s
);
1635 isl_dim_map_range(dim_map
, dst
->start
+ 1, 1, 1, 1, dst
->nparam
, s
);
1636 pos
= node_var_coef_offset(dst
);
1637 isl_dim_map_range(dim_map
, pos
, 2, offset
+ src
->nvar
, 1,
1639 isl_dim_map_range(dim_map
, pos
+ 1, 2, offset
+ src
->nvar
, 1,
1642 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, -s
);
1643 isl_dim_map_range(dim_map
, src
->start
+ 1, 1, 1, 1, src
->nparam
, -s
);
1644 pos
= node_var_coef_offset(src
);
1645 isl_dim_map_range(dim_map
, pos
, 2, offset
, 1, src
->nvar
, s
);
1646 isl_dim_map_range(dim_map
, pos
+ 1, 2, offset
, 1, src
->nvar
, -s
);
1651 /* Add the constraints from "src" to "dst" using "dim_map",
1652 * after making sure there is enough room in "dst" for the extra constraints.
1654 static __isl_give isl_basic_set
*add_constraints_dim_map(
1655 __isl_take isl_basic_set
*dst
, __isl_take isl_basic_set
*src
,
1656 __isl_take isl_dim_map
*dim_map
)
1660 n_eq
= isl_basic_set_n_equality(src
);
1661 n_ineq
= isl_basic_set_n_inequality(src
);
1662 dst
= isl_basic_set_extend_constraints(dst
, n_eq
, n_ineq
);
1663 dst
= isl_basic_set_add_constraints_dim_map(dst
, src
, dim_map
);
1667 /* Add constraints to graph->lp that force validity for the given
1668 * dependence from a node i to itself.
1669 * That is, add constraints that enforce
1671 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1672 * = c_i_x (y - x) >= 0
1674 * for each (x,y) in R.
1675 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1676 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1677 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1678 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1680 * Actually, we do not construct constraints for the c_i_x themselves,
1681 * but for the coefficients of c_i_x written as a linear combination
1682 * of the columns in node->cmap.
1684 static isl_stat
add_intra_validity_constraints(struct isl_sched_graph
*graph
,
1685 struct isl_sched_edge
*edge
)
1688 isl_map
*map
= isl_map_copy(edge
->map
);
1689 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1690 isl_dim_map
*dim_map
;
1691 isl_basic_set
*coef
;
1692 struct isl_sched_node
*node
= edge
->src
;
1694 coef
= intra_coefficients(graph
, node
, map
);
1696 offset
= coef_var_offset(coef
);
1698 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1699 offset
, isl_mat_copy(node
->cmap
));
1701 return isl_stat_error
;
1703 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, 1);
1704 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
1709 /* Add constraints to graph->lp that force validity for the given
1710 * dependence from node i to node j.
1711 * That is, add constraints that enforce
1713 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1715 * for each (x,y) in R.
1716 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1717 * of valid constraints for R and then plug in
1718 * (c_j_0 - c_i_0, c_j_n - c_i_n, -(c_i_x^+ - c_i_x^-), c_j_x^+ - c_j_x^-),
1719 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1720 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1722 * Actually, we do not construct constraints for the c_*_x themselves,
1723 * but for the coefficients of c_*_x written as a linear combination
1724 * of the columns in node->cmap.
1726 static isl_stat
add_inter_validity_constraints(struct isl_sched_graph
*graph
,
1727 struct isl_sched_edge
*edge
)
1732 isl_dim_map
*dim_map
;
1733 isl_basic_set
*coef
;
1734 struct isl_sched_node
*src
= edge
->src
;
1735 struct isl_sched_node
*dst
= edge
->dst
;
1738 return isl_stat_error
;
1740 map
= isl_map_copy(edge
->map
);
1741 ctx
= isl_map_get_ctx(map
);
1742 coef
= inter_coefficients(graph
, edge
, map
);
1744 offset
= coef_var_offset(coef
);
1746 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1747 offset
, isl_mat_copy(src
->cmap
));
1748 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1749 offset
+ src
->nvar
, isl_mat_copy(dst
->cmap
));
1751 return isl_stat_error
;
1753 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, 1);
1755 edge
->start
= graph
->lp
->n_ineq
;
1756 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
1758 return isl_stat_error
;
1759 edge
->end
= graph
->lp
->n_ineq
;
1764 /* Add constraints to graph->lp that bound the dependence distance for the given
1765 * dependence from a node i to itself.
1766 * If s = 1, we add the constraint
1768 * c_i_x (y - x) <= m_0 + m_n n
1772 * -c_i_x (y - x) + m_0 + m_n n >= 0
1774 * for each (x,y) in R.
1775 * If s = -1, we add the constraint
1777 * -c_i_x (y - x) <= m_0 + m_n n
1781 * c_i_x (y - x) + m_0 + m_n n >= 0
1783 * for each (x,y) in R.
1784 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1785 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1786 * with each coefficient (except m_0) represented as a pair of non-negative
1789 * Actually, we do not construct constraints for the c_i_x themselves,
1790 * but for the coefficients of c_i_x written as a linear combination
1791 * of the columns in node->cmap.
1794 * If "local" is set, then we add constraints
1796 * c_i_x (y - x) <= 0
1800 * -c_i_x (y - x) <= 0
1802 * instead, forcing the dependence distance to be (less than or) equal to 0.
1803 * That is, we plug in (0, 0, -s * c_i_x),
1804 * Note that dependences marked local are treated as validity constraints
1805 * by add_all_validity_constraints and therefore also have
1806 * their distances bounded by 0 from below.
1808 static isl_stat
add_intra_proximity_constraints(struct isl_sched_graph
*graph
,
1809 struct isl_sched_edge
*edge
, int s
, int local
)
1813 isl_map
*map
= isl_map_copy(edge
->map
);
1814 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1815 isl_dim_map
*dim_map
;
1816 isl_basic_set
*coef
;
1817 struct isl_sched_node
*node
= edge
->src
;
1819 coef
= intra_coefficients(graph
, node
, map
);
1821 offset
= coef_var_offset(coef
);
1823 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1824 offset
, isl_mat_copy(node
->cmap
));
1826 return isl_stat_error
;
1828 nparam
= isl_space_dim(node
->space
, isl_dim_param
);
1829 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, -s
);
1832 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1833 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1834 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1836 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
1841 /* Add constraints to graph->lp that bound the dependence distance for the given
1842 * dependence from node i to node j.
1843 * If s = 1, we add the constraint
1845 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1850 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1853 * for each (x,y) in R.
1854 * If s = -1, we add the constraint
1856 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1861 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1864 * for each (x,y) in R.
1865 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1866 * of valid constraints for R and then plug in
1867 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1868 * s*c_i_x, -s*c_j_x)
1869 * with each coefficient (except m_0, c_*_0 and c_*_n)
1870 * represented as a pair of non-negative coefficients.
1872 * Actually, we do not construct constraints for the c_*_x themselves,
1873 * but for the coefficients of c_*_x written as a linear combination
1874 * of the columns in node->cmap.
1877 * If "local" is set (and s = 1), then we add constraints
1879 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1883 * -((c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x)) >= 0
1885 * instead, forcing the dependence distance to be (less than or) equal to 0.
1886 * That is, we plug in
1887 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, s*c_i_x, -s*c_j_x).
1888 * Note that dependences marked local are treated as validity constraints
1889 * by add_all_validity_constraints and therefore also have
1890 * their distances bounded by 0 from below.
1892 static isl_stat
add_inter_proximity_constraints(struct isl_sched_graph
*graph
,
1893 struct isl_sched_edge
*edge
, int s
, int local
)
1897 isl_map
*map
= isl_map_copy(edge
->map
);
1898 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1899 isl_dim_map
*dim_map
;
1900 isl_basic_set
*coef
;
1901 struct isl_sched_node
*src
= edge
->src
;
1902 struct isl_sched_node
*dst
= edge
->dst
;
1904 coef
= inter_coefficients(graph
, edge
, map
);
1906 offset
= coef_var_offset(coef
);
1908 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1909 offset
, isl_mat_copy(src
->cmap
));
1910 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1911 offset
+ src
->nvar
, isl_mat_copy(dst
->cmap
));
1913 return isl_stat_error
;
1915 nparam
= isl_space_dim(src
->space
, isl_dim_param
);
1916 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, -s
);
1919 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1920 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1921 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1924 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
1929 /* Add all validity constraints to graph->lp.
1931 * An edge that is forced to be local needs to have its dependence
1932 * distances equal to zero. We take care of bounding them by 0 from below
1933 * here. add_all_proximity_constraints takes care of bounding them by 0
1936 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1937 * Otherwise, we ignore them.
1939 static int add_all_validity_constraints(struct isl_sched_graph
*graph
,
1940 int use_coincidence
)
1944 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1945 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1948 local
= is_local(edge
) ||
1949 (is_coincidence(edge
) && use_coincidence
);
1950 if (!is_validity(edge
) && !local
)
1952 if (edge
->src
!= edge
->dst
)
1954 if (add_intra_validity_constraints(graph
, edge
) < 0)
1958 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1959 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1962 local
= is_local(edge
) ||
1963 (is_coincidence(edge
) && use_coincidence
);
1964 if (!is_validity(edge
) && !local
)
1966 if (edge
->src
== edge
->dst
)
1968 if (add_inter_validity_constraints(graph
, edge
) < 0)
1975 /* Add constraints to graph->lp that bound the dependence distance
1976 * for all dependence relations.
1977 * If a given proximity dependence is identical to a validity
1978 * dependence, then the dependence distance is already bounded
1979 * from below (by zero), so we only need to bound the distance
1980 * from above. (This includes the case of "local" dependences
1981 * which are treated as validity dependence by add_all_validity_constraints.)
1982 * Otherwise, we need to bound the distance both from above and from below.
1984 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1985 * Otherwise, we ignore them.
1987 static int add_all_proximity_constraints(struct isl_sched_graph
*graph
,
1988 int use_coincidence
)
1992 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1993 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1996 local
= is_local(edge
) ||
1997 (is_coincidence(edge
) && use_coincidence
);
1998 if (!is_proximity(edge
) && !local
)
2000 if (edge
->src
== edge
->dst
&&
2001 add_intra_proximity_constraints(graph
, edge
, 1, local
) < 0)
2003 if (edge
->src
!= edge
->dst
&&
2004 add_inter_proximity_constraints(graph
, edge
, 1, local
) < 0)
2006 if (is_validity(edge
) || local
)
2008 if (edge
->src
== edge
->dst
&&
2009 add_intra_proximity_constraints(graph
, edge
, -1, 0) < 0)
2011 if (edge
->src
!= edge
->dst
&&
2012 add_inter_proximity_constraints(graph
, edge
, -1, 0) < 0)
2019 /* Compute a basis for the rows in the linear part of the schedule
2020 * and extend this basis to a full basis. The remaining rows
2021 * can then be used to force linear independence from the rows
2024 * In particular, given the schedule rows S, we compute
2029 * with H the Hermite normal form of S. That is, all but the
2030 * first rank columns of H are zero and so each row in S is
2031 * a linear combination of the first rank rows of Q.
2032 * The matrix Q is then transposed because we will write the
2033 * coefficients of the next schedule row as a column vector s
2034 * and express this s as a linear combination s = Q c of the
2036 * Similarly, the matrix U is transposed such that we can
2037 * compute the coefficients c = U s from a schedule row s.
2039 static int node_update_cmap(struct isl_sched_node
*node
)
2042 int n_row
= isl_mat_rows(node
->sched
);
2044 H
= isl_mat_sub_alloc(node
->sched
, 0, n_row
,
2045 1 + node
->nparam
, node
->nvar
);
2047 H
= isl_mat_left_hermite(H
, 0, &U
, &Q
);
2048 isl_mat_free(node
->cmap
);
2049 isl_mat_free(node
->cinv
);
2050 isl_mat_free(node
->ctrans
);
2051 node
->ctrans
= isl_mat_copy(Q
);
2052 node
->cmap
= isl_mat_transpose(Q
);
2053 node
->cinv
= isl_mat_transpose(U
);
2054 node
->rank
= isl_mat_initial_non_zero_cols(H
);
2057 if (!node
->cmap
|| !node
->cinv
|| !node
->ctrans
|| node
->rank
< 0)
2062 /* Is "edge" marked as a validity or a conditional validity edge?
2064 static int is_any_validity(struct isl_sched_edge
*edge
)
2066 return is_validity(edge
) || is_conditional_validity(edge
);
2069 /* How many times should we count the constraints in "edge"?
2071 * We count as follows
2072 * validity -> 1 (>= 0)
2073 * validity+proximity -> 2 (>= 0 and upper bound)
2074 * proximity -> 2 (lower and upper bound)
2075 * local(+any) -> 2 (>= 0 and <= 0)
2077 * If an edge is only marked conditional_validity then it counts
2078 * as zero since it is only checked afterwards.
2080 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2081 * Otherwise, we ignore them.
2083 static int edge_multiplicity(struct isl_sched_edge
*edge
, int use_coincidence
)
2085 if (is_proximity(edge
) || is_local(edge
))
2087 if (use_coincidence
&& is_coincidence(edge
))
2089 if (is_validity(edge
))
2094 /* Count the number of equality and inequality constraints
2095 * that will be added for the given map.
2097 * "use_coincidence" is set if we should take into account coincidence edges.
2099 static isl_stat
count_map_constraints(struct isl_sched_graph
*graph
,
2100 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
,
2101 int *n_eq
, int *n_ineq
, int use_coincidence
)
2103 isl_basic_set
*coef
;
2104 int f
= edge_multiplicity(edge
, use_coincidence
);
2111 if (edge
->src
== edge
->dst
)
2112 coef
= intra_coefficients(graph
, edge
->src
, map
);
2114 coef
= inter_coefficients(graph
, edge
, map
);
2116 return isl_stat_error
;
2117 *n_eq
+= f
* isl_basic_set_n_equality(coef
);
2118 *n_ineq
+= f
* isl_basic_set_n_inequality(coef
);
2119 isl_basic_set_free(coef
);
2124 /* Count the number of equality and inequality constraints
2125 * that will be added to the main lp problem.
2126 * We count as follows
2127 * validity -> 1 (>= 0)
2128 * validity+proximity -> 2 (>= 0 and upper bound)
2129 * proximity -> 2 (lower and upper bound)
2130 * local(+any) -> 2 (>= 0 and <= 0)
2132 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2133 * Otherwise, we ignore them.
2135 static int count_constraints(struct isl_sched_graph
*graph
,
2136 int *n_eq
, int *n_ineq
, int use_coincidence
)
2140 *n_eq
= *n_ineq
= 0;
2141 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2142 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2143 isl_map
*map
= isl_map_copy(edge
->map
);
2145 if (count_map_constraints(graph
, edge
, map
, n_eq
, n_ineq
,
2146 use_coincidence
) < 0)
2153 /* Count the number of constraints that will be added by
2154 * add_bound_constant_constraints to bound the values of the constant terms
2155 * and increment *n_eq and *n_ineq accordingly.
2157 * In practice, add_bound_constant_constraints only adds inequalities.
2159 static isl_stat
count_bound_constant_constraints(isl_ctx
*ctx
,
2160 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
2162 if (isl_options_get_schedule_max_constant_term(ctx
) == -1)
2165 *n_ineq
+= graph
->n
;
2170 /* Add constraints to bound the values of the constant terms in the schedule,
2171 * if requested by the user.
2173 * The maximal value of the constant terms is defined by the option
2174 * "schedule_max_constant_term".
2176 * Within each node, the coefficients have the following order:
2178 * - c_i_n (if parametric)
2179 * - positive and negative parts of c_i_x
2181 static isl_stat
add_bound_constant_constraints(isl_ctx
*ctx
,
2182 struct isl_sched_graph
*graph
)
2188 max
= isl_options_get_schedule_max_constant_term(ctx
);
2192 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2194 for (i
= 0; i
< graph
->n
; ++i
) {
2195 struct isl_sched_node
*node
= &graph
->node
[i
];
2196 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2198 return isl_stat_error
;
2199 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2200 isl_int_set_si(graph
->lp
->ineq
[k
][1 + node
->start
], -1);
2201 isl_int_set_si(graph
->lp
->ineq
[k
][0], max
);
2207 /* Count the number of constraints that will be added by
2208 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
2211 * In practice, add_bound_coefficient_constraints only adds inequalities.
2213 static int count_bound_coefficient_constraints(isl_ctx
*ctx
,
2214 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
2218 if (isl_options_get_schedule_max_coefficient(ctx
) == -1 &&
2219 !isl_options_get_schedule_treat_coalescing(ctx
))
2222 for (i
= 0; i
< graph
->n
; ++i
)
2223 *n_ineq
+= graph
->node
[i
].nparam
+ 2 * graph
->node
[i
].nvar
;
2228 /* Add constraints to graph->lp that bound the values of
2229 * the parameter schedule coefficients of "node" to "max" and
2230 * the variable schedule coefficients to the corresponding entry
2232 * In either case, a negative value means that no bound needs to be imposed.
2234 * For parameter coefficients, this amounts to adding a constraint
2242 * The variables coefficients are, however, not represented directly.
2243 * Instead, the variables coefficients c_x are written as a linear
2244 * combination c_x = cmap c_z of some other coefficients c_z,
2245 * which are in turn encoded as c_z = c_z^+ - c_z^-.
2246 * Let a_j be the elements of row i of node->cmap, then
2248 * -max_i <= c_x_i <= max_i
2252 * -max_i <= \sum_j a_j (c_z_j^+ - c_z_j^-) <= max_i
2256 * -\sum_j a_j (c_z_j^+ - c_z_j^-) + max_i >= 0
2257 * \sum_j a_j (c_z_j^+ - c_z_j^-) + max_i >= 0
2259 static isl_stat
node_add_coefficient_constraints(isl_ctx
*ctx
,
2260 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
, int max
)
2266 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2268 for (j
= 0; j
< node
->nparam
; ++j
) {
2274 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2276 return isl_stat_error
;
2277 dim
= 1 + node
->start
+ 1 + j
;
2278 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2279 isl_int_set_si(graph
->lp
->ineq
[k
][dim
], -1);
2280 isl_int_set_si(graph
->lp
->ineq
[k
][0], max
);
2283 ineq
= isl_vec_alloc(ctx
, 1 + total
);
2284 ineq
= isl_vec_clr(ineq
);
2286 return isl_stat_error
;
2287 for (i
= 0; i
< node
->nvar
; ++i
) {
2288 int pos
= 1 + node_var_coef_offset(node
);
2290 if (isl_int_is_neg(node
->max
->el
[i
]))
2293 for (j
= 0; j
< node
->nvar
; ++j
) {
2294 isl_int_set(ineq
->el
[pos
+ 2 * j
],
2295 node
->cmap
->row
[i
][j
]);
2296 isl_int_neg(ineq
->el
[pos
+ 2 * j
+ 1],
2297 node
->cmap
->row
[i
][j
]);
2299 isl_int_set(ineq
->el
[0], node
->max
->el
[i
]);
2301 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2304 isl_seq_cpy(graph
->lp
->ineq
[k
], ineq
->el
, 1 + total
);
2306 isl_seq_neg(ineq
->el
+ pos
, ineq
->el
+ pos
, 2 * node
->nvar
);
2307 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2310 isl_seq_cpy(graph
->lp
->ineq
[k
], ineq
->el
, 1 + total
);
2317 return isl_stat_error
;
2320 /* Add constraints that bound the values of the variable and parameter
2321 * coefficients of the schedule.
2323 * The maximal value of the coefficients is defined by the option
2324 * 'schedule_max_coefficient' and the entries in node->max.
2325 * These latter entries are only set if either the schedule_max_coefficient
2326 * option or the schedule_treat_coalescing option is set.
2328 static isl_stat
add_bound_coefficient_constraints(isl_ctx
*ctx
,
2329 struct isl_sched_graph
*graph
)
2334 max
= isl_options_get_schedule_max_coefficient(ctx
);
2336 if (max
== -1 && !isl_options_get_schedule_treat_coalescing(ctx
))
2339 for (i
= 0; i
< graph
->n
; ++i
) {
2340 struct isl_sched_node
*node
= &graph
->node
[i
];
2342 if (node_add_coefficient_constraints(ctx
, graph
, node
, max
) < 0)
2343 return isl_stat_error
;
2349 /* Add a constraint to graph->lp that equates the value at position
2350 * "sum_pos" to the sum of the "n" values starting at "first".
2352 static isl_stat
add_sum_constraint(struct isl_sched_graph
*graph
,
2353 int sum_pos
, int first
, int n
)
2358 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2360 k
= isl_basic_set_alloc_equality(graph
->lp
);
2362 return isl_stat_error
;
2363 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2364 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2365 for (i
= 0; i
< n
; ++i
)
2366 isl_int_set_si(graph
->lp
->eq
[k
][1 + first
+ i
], 1);
2371 /* Add a constraint to graph->lp that equates the value at position
2372 * "sum_pos" to the sum of the parameter coefficients of all nodes.
2374 * Within each node, the coefficients have the following order:
2376 * - c_i_n (if parametric)
2377 * - positive and negative parts of c_i_x
2379 static isl_stat
add_param_sum_constraint(struct isl_sched_graph
*graph
,
2385 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2387 k
= isl_basic_set_alloc_equality(graph
->lp
);
2389 return isl_stat_error
;
2390 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2391 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2392 for (i
= 0; i
< graph
->n
; ++i
) {
2393 int pos
= 1 + graph
->node
[i
].start
+ 1;
2395 for (j
= 0; j
< graph
->node
[i
].nparam
; ++j
)
2396 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2402 /* Add a constraint to graph->lp that equates the value at position
2403 * "sum_pos" to the sum of the variable coefficients of all nodes.
2405 * Within each node, the coefficients have the following order:
2407 * - c_i_n (if parametric)
2408 * - positive and negative parts of c_i_x
2410 static isl_stat
add_var_sum_constraint(struct isl_sched_graph
*graph
,
2416 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2418 k
= isl_basic_set_alloc_equality(graph
->lp
);
2420 return isl_stat_error
;
2421 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2422 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2423 for (i
= 0; i
< graph
->n
; ++i
) {
2424 struct isl_sched_node
*node
= &graph
->node
[i
];
2425 int pos
= 1 + node_var_coef_offset(node
);
2427 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
2428 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2434 /* Construct an ILP problem for finding schedule coefficients
2435 * that result in non-negative, but small dependence distances
2436 * over all dependences.
2437 * In particular, the dependence distances over proximity edges
2438 * are bounded by m_0 + m_n n and we compute schedule coefficients
2439 * with small values (preferably zero) of m_n and m_0.
2441 * All variables of the ILP are non-negative. The actual coefficients
2442 * may be negative, so each coefficient is represented as the difference
2443 * of two non-negative variables. The negative part always appears
2444 * immediately before the positive part.
2445 * Other than that, the variables have the following order
2447 * - sum of positive and negative parts of m_n coefficients
2449 * - sum of all c_n coefficients
2450 * (unconstrained when computing non-parametric schedules)
2451 * - sum of positive and negative parts of all c_x coefficients
2452 * - positive and negative parts of m_n coefficients
2455 * - c_i_n (if parametric)
2456 * - positive and negative parts of c_i_x
2458 * The c_i_x are not represented directly, but through the columns of
2459 * node->cmap. That is, the computed values are for variable t_i_x
2460 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
2462 * The constraints are those from the edges plus two or three equalities
2463 * to express the sums.
2465 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2466 * Otherwise, we ignore them.
2468 static isl_stat
setup_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
2469 int use_coincidence
)
2479 parametric
= ctx
->opt
->schedule_parametric
;
2480 nparam
= isl_space_dim(graph
->node
[0].space
, isl_dim_param
);
2482 total
= param_pos
+ 2 * nparam
;
2483 for (i
= 0; i
< graph
->n
; ++i
) {
2484 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
2485 if (node_update_cmap(node
) < 0)
2486 return isl_stat_error
;
2487 node
->start
= total
;
2488 total
+= 1 + node
->nparam
+ 2 * node
->nvar
;
2491 if (count_constraints(graph
, &n_eq
, &n_ineq
, use_coincidence
) < 0)
2492 return isl_stat_error
;
2493 if (count_bound_constant_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2494 return isl_stat_error
;
2495 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2496 return isl_stat_error
;
2498 space
= isl_space_set_alloc(ctx
, 0, total
);
2499 isl_basic_set_free(graph
->lp
);
2500 n_eq
+= 2 + parametric
;
2502 graph
->lp
= isl_basic_set_alloc_space(space
, 0, n_eq
, n_ineq
);
2504 if (add_sum_constraint(graph
, 0, param_pos
, 2 * nparam
) < 0)
2505 return isl_stat_error
;
2506 if (parametric
&& add_param_sum_constraint(graph
, 2) < 0)
2507 return isl_stat_error
;
2508 if (add_var_sum_constraint(graph
, 3) < 0)
2509 return isl_stat_error
;
2510 if (add_bound_constant_constraints(ctx
, graph
) < 0)
2511 return isl_stat_error
;
2512 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
2513 return isl_stat_error
;
2514 if (add_all_validity_constraints(graph
, use_coincidence
) < 0)
2515 return isl_stat_error
;
2516 if (add_all_proximity_constraints(graph
, use_coincidence
) < 0)
2517 return isl_stat_error
;
2522 /* Analyze the conflicting constraint found by
2523 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2524 * constraint of one of the edges between distinct nodes, living, moreover
2525 * in distinct SCCs, then record the source and sink SCC as this may
2526 * be a good place to cut between SCCs.
2528 static int check_conflict(int con
, void *user
)
2531 struct isl_sched_graph
*graph
= user
;
2533 if (graph
->src_scc
>= 0)
2536 con
-= graph
->lp
->n_eq
;
2538 if (con
>= graph
->lp
->n_ineq
)
2541 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2542 if (!is_validity(&graph
->edge
[i
]))
2544 if (graph
->edge
[i
].src
== graph
->edge
[i
].dst
)
2546 if (graph
->edge
[i
].src
->scc
== graph
->edge
[i
].dst
->scc
)
2548 if (graph
->edge
[i
].start
> con
)
2550 if (graph
->edge
[i
].end
<= con
)
2552 graph
->src_scc
= graph
->edge
[i
].src
->scc
;
2553 graph
->dst_scc
= graph
->edge
[i
].dst
->scc
;
2559 /* Check whether the next schedule row of the given node needs to be
2560 * non-trivial. Lower-dimensional domains may have some trivial rows,
2561 * but as soon as the number of remaining required non-trivial rows
2562 * is as large as the number or remaining rows to be computed,
2563 * all remaining rows need to be non-trivial.
2565 static int needs_row(struct isl_sched_graph
*graph
, struct isl_sched_node
*node
)
2567 return node
->nvar
- node
->rank
>= graph
->maxvar
- graph
->n_row
;
2570 /* Solve the ILP problem constructed in setup_lp.
2571 * For each node such that all the remaining rows of its schedule
2572 * need to be non-trivial, we construct a non-triviality region.
2573 * This region imposes that the next row is independent of previous rows.
2574 * In particular the coefficients c_i_x are represented by t_i_x
2575 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
2576 * its first columns span the rows of the previously computed part
2577 * of the schedule. The non-triviality region enforces that at least
2578 * one of the remaining components of t_i_x is non-zero, i.e.,
2579 * that the new schedule row depends on at least one of the remaining
2582 static __isl_give isl_vec
*solve_lp(struct isl_sched_graph
*graph
)
2588 for (i
= 0; i
< graph
->n
; ++i
) {
2589 struct isl_sched_node
*node
= &graph
->node
[i
];
2590 int skip
= node
->rank
;
2591 graph
->region
[i
].pos
= node_var_coef_offset(node
) + 2 * skip
;
2592 if (needs_row(graph
, node
))
2593 graph
->region
[i
].len
= 2 * (node
->nvar
- skip
);
2595 graph
->region
[i
].len
= 0;
2597 lp
= isl_basic_set_copy(graph
->lp
);
2598 sol
= isl_tab_basic_set_non_trivial_lexmin(lp
, 2, graph
->n
,
2599 graph
->region
, &check_conflict
, graph
);
2603 /* Extract the coefficients for the variables of "node" from "sol".
2605 * Within each node, the coefficients have the following order:
2607 * - c_i_n (if parametric)
2608 * - positive and negative parts of c_i_x
2610 * The c_i_x^- appear before their c_i_x^+ counterpart.
2612 * Return c_i_x = c_i_x^+ - c_i_x^-
2614 static __isl_give isl_vec
*extract_var_coef(struct isl_sched_node
*node
,
2615 __isl_keep isl_vec
*sol
)
2623 csol
= isl_vec_alloc(isl_vec_get_ctx(sol
), node
->nvar
);
2627 pos
= 1 + node_var_coef_offset(node
);
2628 for (i
= 0; i
< node
->nvar
; ++i
)
2629 isl_int_sub(csol
->el
[i
],
2630 sol
->el
[pos
+ 2 * i
+ 1], sol
->el
[pos
+ 2 * i
]);
2635 /* Update the schedules of all nodes based on the given solution
2636 * of the LP problem.
2637 * The new row is added to the current band.
2638 * All possibly negative coefficients are encoded as a difference
2639 * of two non-negative variables, so we need to perform the subtraction
2640 * here. Moreover, if use_cmap is set, then the solution does
2641 * not refer to the actual coefficients c_i_x, but instead to variables
2642 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2643 * In this case, we then also need to perform this multiplication
2644 * to obtain the values of c_i_x.
2646 * If coincident is set, then the caller guarantees that the new
2647 * row satisfies the coincidence constraints.
2649 static int update_schedule(struct isl_sched_graph
*graph
,
2650 __isl_take isl_vec
*sol
, int use_cmap
, int coincident
)
2653 isl_vec
*csol
= NULL
;
2658 isl_die(sol
->ctx
, isl_error_internal
,
2659 "no solution found", goto error
);
2660 if (graph
->n_total_row
>= graph
->max_row
)
2661 isl_die(sol
->ctx
, isl_error_internal
,
2662 "too many schedule rows", goto error
);
2664 for (i
= 0; i
< graph
->n
; ++i
) {
2665 struct isl_sched_node
*node
= &graph
->node
[i
];
2666 int pos
= node
->start
;
2667 int row
= isl_mat_rows(node
->sched
);
2670 csol
= extract_var_coef(node
, sol
);
2674 isl_map_free(node
->sched_map
);
2675 node
->sched_map
= NULL
;
2676 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
2679 for (j
= 0; j
< 1 + node
->nparam
; ++j
)
2680 node
->sched
= isl_mat_set_element(node
->sched
,
2681 row
, j
, sol
->el
[1 + pos
+ j
]);
2683 csol
= isl_mat_vec_product(isl_mat_copy(node
->cmap
),
2687 for (j
= 0; j
< node
->nvar
; ++j
)
2688 node
->sched
= isl_mat_set_element(node
->sched
,
2689 row
, 1 + node
->nparam
+ j
, csol
->el
[j
]);
2690 node
->coincident
[graph
->n_total_row
] = coincident
;
2696 graph
->n_total_row
++;
2705 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2706 * and return this isl_aff.
2708 static __isl_give isl_aff
*extract_schedule_row(__isl_take isl_local_space
*ls
,
2709 struct isl_sched_node
*node
, int row
)
2717 aff
= isl_aff_zero_on_domain(ls
);
2718 isl_mat_get_element(node
->sched
, row
, 0, &v
);
2719 aff
= isl_aff_set_constant(aff
, v
);
2720 for (j
= 0; j
< node
->nparam
; ++j
) {
2721 isl_mat_get_element(node
->sched
, row
, 1 + j
, &v
);
2722 aff
= isl_aff_set_coefficient(aff
, isl_dim_param
, j
, v
);
2724 for (j
= 0; j
< node
->nvar
; ++j
) {
2725 isl_mat_get_element(node
->sched
, row
, 1 + node
->nparam
+ j
, &v
);
2726 aff
= isl_aff_set_coefficient(aff
, isl_dim_in
, j
, v
);
2734 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
2735 * and return this multi_aff.
2737 * The result is defined over the uncompressed node domain.
2739 static __isl_give isl_multi_aff
*node_extract_partial_schedule_multi_aff(
2740 struct isl_sched_node
*node
, int first
, int n
)
2744 isl_local_space
*ls
;
2751 nrow
= isl_mat_rows(node
->sched
);
2752 if (node
->compressed
)
2753 space
= isl_multi_aff_get_domain_space(node
->decompress
);
2755 space
= isl_space_copy(node
->space
);
2756 ls
= isl_local_space_from_space(isl_space_copy(space
));
2757 space
= isl_space_from_domain(space
);
2758 space
= isl_space_add_dims(space
, isl_dim_out
, n
);
2759 ma
= isl_multi_aff_zero(space
);
2761 for (i
= first
; i
< first
+ n
; ++i
) {
2762 aff
= extract_schedule_row(isl_local_space_copy(ls
), node
, i
);
2763 ma
= isl_multi_aff_set_aff(ma
, i
- first
, aff
);
2766 isl_local_space_free(ls
);
2768 if (node
->compressed
)
2769 ma
= isl_multi_aff_pullback_multi_aff(ma
,
2770 isl_multi_aff_copy(node
->compress
));
2775 /* Convert node->sched into a multi_aff and return this multi_aff.
2777 * The result is defined over the uncompressed node domain.
2779 static __isl_give isl_multi_aff
*node_extract_schedule_multi_aff(
2780 struct isl_sched_node
*node
)
2784 nrow
= isl_mat_rows(node
->sched
);
2785 return node_extract_partial_schedule_multi_aff(node
, 0, nrow
);
2788 /* Convert node->sched into a map and return this map.
2790 * The result is cached in node->sched_map, which needs to be released
2791 * whenever node->sched is updated.
2792 * It is defined over the uncompressed node domain.
2794 static __isl_give isl_map
*node_extract_schedule(struct isl_sched_node
*node
)
2796 if (!node
->sched_map
) {
2799 ma
= node_extract_schedule_multi_aff(node
);
2800 node
->sched_map
= isl_map_from_multi_aff(ma
);
2803 return isl_map_copy(node
->sched_map
);
2806 /* Construct a map that can be used to update a dependence relation
2807 * based on the current schedule.
2808 * That is, construct a map expressing that source and sink
2809 * are executed within the same iteration of the current schedule.
2810 * This map can then be intersected with the dependence relation.
2811 * This is not the most efficient way, but this shouldn't be a critical
2814 static __isl_give isl_map
*specializer(struct isl_sched_node
*src
,
2815 struct isl_sched_node
*dst
)
2817 isl_map
*src_sched
, *dst_sched
;
2819 src_sched
= node_extract_schedule(src
);
2820 dst_sched
= node_extract_schedule(dst
);
2821 return isl_map_apply_range(src_sched
, isl_map_reverse(dst_sched
));
2824 /* Intersect the domains of the nested relations in domain and range
2825 * of "umap" with "map".
2827 static __isl_give isl_union_map
*intersect_domains(
2828 __isl_take isl_union_map
*umap
, __isl_keep isl_map
*map
)
2830 isl_union_set
*uset
;
2832 umap
= isl_union_map_zip(umap
);
2833 uset
= isl_union_set_from_set(isl_map_wrap(isl_map_copy(map
)));
2834 umap
= isl_union_map_intersect_domain(umap
, uset
);
2835 umap
= isl_union_map_zip(umap
);
2839 /* Update the dependence relation of the given edge based
2840 * on the current schedule.
2841 * If the dependence is carried completely by the current schedule, then
2842 * it is removed from the edge_tables. It is kept in the list of edges
2843 * as otherwise all edge_tables would have to be recomputed.
2845 static int update_edge(struct isl_sched_graph
*graph
,
2846 struct isl_sched_edge
*edge
)
2851 id
= specializer(edge
->src
, edge
->dst
);
2852 edge
->map
= isl_map_intersect(edge
->map
, isl_map_copy(id
));
2856 if (edge
->tagged_condition
) {
2857 edge
->tagged_condition
=
2858 intersect_domains(edge
->tagged_condition
, id
);
2859 if (!edge
->tagged_condition
)
2862 if (edge
->tagged_validity
) {
2863 edge
->tagged_validity
=
2864 intersect_domains(edge
->tagged_validity
, id
);
2865 if (!edge
->tagged_validity
)
2869 empty
= isl_map_plain_is_empty(edge
->map
);
2873 graph_remove_edge(graph
, edge
);
2882 /* Does the domain of "umap" intersect "uset"?
2884 static int domain_intersects(__isl_keep isl_union_map
*umap
,
2885 __isl_keep isl_union_set
*uset
)
2889 umap
= isl_union_map_copy(umap
);
2890 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(uset
));
2891 empty
= isl_union_map_is_empty(umap
);
2892 isl_union_map_free(umap
);
2894 return empty
< 0 ? -1 : !empty
;
2897 /* Does the range of "umap" intersect "uset"?
2899 static int range_intersects(__isl_keep isl_union_map
*umap
,
2900 __isl_keep isl_union_set
*uset
)
2904 umap
= isl_union_map_copy(umap
);
2905 umap
= isl_union_map_intersect_range(umap
, isl_union_set_copy(uset
));
2906 empty
= isl_union_map_is_empty(umap
);
2907 isl_union_map_free(umap
);
2909 return empty
< 0 ? -1 : !empty
;
2912 /* Are the condition dependences of "edge" local with respect to
2913 * the current schedule?
2915 * That is, are domain and range of the condition dependences mapped
2916 * to the same point?
2918 * In other words, is the condition false?
2920 static int is_condition_false(struct isl_sched_edge
*edge
)
2922 isl_union_map
*umap
;
2923 isl_map
*map
, *sched
, *test
;
2926 empty
= isl_union_map_is_empty(edge
->tagged_condition
);
2927 if (empty
< 0 || empty
)
2930 umap
= isl_union_map_copy(edge
->tagged_condition
);
2931 umap
= isl_union_map_zip(umap
);
2932 umap
= isl_union_set_unwrap(isl_union_map_domain(umap
));
2933 map
= isl_map_from_union_map(umap
);
2935 sched
= node_extract_schedule(edge
->src
);
2936 map
= isl_map_apply_domain(map
, sched
);
2937 sched
= node_extract_schedule(edge
->dst
);
2938 map
= isl_map_apply_range(map
, sched
);
2940 test
= isl_map_identity(isl_map_get_space(map
));
2941 local
= isl_map_is_subset(map
, test
);
2948 /* For each conditional validity constraint that is adjacent
2949 * to a condition with domain in condition_source or range in condition_sink,
2950 * turn it into an unconditional validity constraint.
2952 static int unconditionalize_adjacent_validity(struct isl_sched_graph
*graph
,
2953 __isl_take isl_union_set
*condition_source
,
2954 __isl_take isl_union_set
*condition_sink
)
2958 condition_source
= isl_union_set_coalesce(condition_source
);
2959 condition_sink
= isl_union_set_coalesce(condition_sink
);
2961 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2963 isl_union_map
*validity
;
2965 if (!is_conditional_validity(&graph
->edge
[i
]))
2967 if (is_validity(&graph
->edge
[i
]))
2970 validity
= graph
->edge
[i
].tagged_validity
;
2971 adjacent
= domain_intersects(validity
, condition_sink
);
2972 if (adjacent
>= 0 && !adjacent
)
2973 adjacent
= range_intersects(validity
, condition_source
);
2979 set_validity(&graph
->edge
[i
]);
2982 isl_union_set_free(condition_source
);
2983 isl_union_set_free(condition_sink
);
2986 isl_union_set_free(condition_source
);
2987 isl_union_set_free(condition_sink
);
2991 /* Update the dependence relations of all edges based on the current schedule
2992 * and enforce conditional validity constraints that are adjacent
2993 * to satisfied condition constraints.
2995 * First check if any of the condition constraints are satisfied
2996 * (i.e., not local to the outer schedule) and keep track of
2997 * their domain and range.
2998 * Then update all dependence relations (which removes the non-local
3000 * Finally, if any condition constraints turned out to be satisfied,
3001 * then turn all adjacent conditional validity constraints into
3002 * unconditional validity constraints.
3004 static int update_edges(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3008 isl_union_set
*source
, *sink
;
3010 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3011 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3012 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3014 isl_union_set
*uset
;
3015 isl_union_map
*umap
;
3017 if (!is_condition(&graph
->edge
[i
]))
3019 if (is_local(&graph
->edge
[i
]))
3021 local
= is_condition_false(&graph
->edge
[i
]);
3029 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
3030 uset
= isl_union_map_domain(umap
);
3031 source
= isl_union_set_union(source
, uset
);
3033 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
3034 uset
= isl_union_map_range(umap
);
3035 sink
= isl_union_set_union(sink
, uset
);
3038 for (i
= graph
->n_edge
- 1; i
>= 0; --i
) {
3039 if (update_edge(graph
, &graph
->edge
[i
]) < 0)
3044 return unconditionalize_adjacent_validity(graph
, source
, sink
);
3046 isl_union_set_free(source
);
3047 isl_union_set_free(sink
);
3050 isl_union_set_free(source
);
3051 isl_union_set_free(sink
);
3055 static void next_band(struct isl_sched_graph
*graph
)
3057 graph
->band_start
= graph
->n_total_row
;
3060 /* Return the union of the universe domains of the nodes in "graph"
3061 * that satisfy "pred".
3063 static __isl_give isl_union_set
*isl_sched_graph_domain(isl_ctx
*ctx
,
3064 struct isl_sched_graph
*graph
,
3065 int (*pred
)(struct isl_sched_node
*node
, int data
), int data
)
3071 for (i
= 0; i
< graph
->n
; ++i
)
3072 if (pred(&graph
->node
[i
], data
))
3076 isl_die(ctx
, isl_error_internal
,
3077 "empty component", return NULL
);
3079 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
3080 dom
= isl_union_set_from_set(set
);
3082 for (i
= i
+ 1; i
< graph
->n
; ++i
) {
3083 if (!pred(&graph
->node
[i
], data
))
3085 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
3086 dom
= isl_union_set_union(dom
, isl_union_set_from_set(set
));
3092 /* Return a list of unions of universe domains, where each element
3093 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
3095 static __isl_give isl_union_set_list
*extract_sccs(isl_ctx
*ctx
,
3096 struct isl_sched_graph
*graph
)
3099 isl_union_set_list
*filters
;
3101 filters
= isl_union_set_list_alloc(ctx
, graph
->scc
);
3102 for (i
= 0; i
< graph
->scc
; ++i
) {
3105 dom
= isl_sched_graph_domain(ctx
, graph
, &node_scc_exactly
, i
);
3106 filters
= isl_union_set_list_add(filters
, dom
);
3112 /* Return a list of two unions of universe domains, one for the SCCs up
3113 * to and including graph->src_scc and another for the other SCCs.
3115 static __isl_give isl_union_set_list
*extract_split(isl_ctx
*ctx
,
3116 struct isl_sched_graph
*graph
)
3119 isl_union_set_list
*filters
;
3121 filters
= isl_union_set_list_alloc(ctx
, 2);
3122 dom
= isl_sched_graph_domain(ctx
, graph
,
3123 &node_scc_at_most
, graph
->src_scc
);
3124 filters
= isl_union_set_list_add(filters
, dom
);
3125 dom
= isl_sched_graph_domain(ctx
, graph
,
3126 &node_scc_at_least
, graph
->src_scc
+ 1);
3127 filters
= isl_union_set_list_add(filters
, dom
);
3132 /* Copy nodes that satisfy node_pred from the src dependence graph
3133 * to the dst dependence graph.
3135 static int copy_nodes(struct isl_sched_graph
*dst
, struct isl_sched_graph
*src
,
3136 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
3141 for (i
= 0; i
< src
->n
; ++i
) {
3144 if (!node_pred(&src
->node
[i
], data
))
3148 dst
->node
[j
].space
= isl_space_copy(src
->node
[i
].space
);
3149 dst
->node
[j
].compressed
= src
->node
[i
].compressed
;
3150 dst
->node
[j
].hull
= isl_set_copy(src
->node
[i
].hull
);
3151 dst
->node
[j
].compress
=
3152 isl_multi_aff_copy(src
->node
[i
].compress
);
3153 dst
->node
[j
].decompress
=
3154 isl_multi_aff_copy(src
->node
[i
].decompress
);
3155 dst
->node
[j
].nvar
= src
->node
[i
].nvar
;
3156 dst
->node
[j
].nparam
= src
->node
[i
].nparam
;
3157 dst
->node
[j
].sched
= isl_mat_copy(src
->node
[i
].sched
);
3158 dst
->node
[j
].sched_map
= isl_map_copy(src
->node
[i
].sched_map
);
3159 dst
->node
[j
].coincident
= src
->node
[i
].coincident
;
3160 dst
->node
[j
].sizes
= isl_multi_val_copy(src
->node
[i
].sizes
);
3161 dst
->node
[j
].max
= isl_vec_copy(src
->node
[i
].max
);
3164 if (!dst
->node
[j
].space
|| !dst
->node
[j
].sched
)
3166 if (dst
->node
[j
].compressed
&&
3167 (!dst
->node
[j
].hull
|| !dst
->node
[j
].compress
||
3168 !dst
->node
[j
].decompress
))
3175 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
3176 * to the dst dependence graph.
3177 * If the source or destination node of the edge is not in the destination
3178 * graph, then it must be a backward proximity edge and it should simply
3181 static int copy_edges(isl_ctx
*ctx
, struct isl_sched_graph
*dst
,
3182 struct isl_sched_graph
*src
,
3183 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
), int data
)
3186 enum isl_edge_type t
;
3189 for (i
= 0; i
< src
->n_edge
; ++i
) {
3190 struct isl_sched_edge
*edge
= &src
->edge
[i
];
3192 isl_union_map
*tagged_condition
;
3193 isl_union_map
*tagged_validity
;
3194 struct isl_sched_node
*dst_src
, *dst_dst
;
3196 if (!edge_pred(edge
, data
))
3199 if (isl_map_plain_is_empty(edge
->map
))
3202 dst_src
= graph_find_node(ctx
, dst
, edge
->src
->space
);
3203 dst_dst
= graph_find_node(ctx
, dst
, edge
->dst
->space
);
3204 if (!dst_src
|| !dst_dst
) {
3205 if (is_validity(edge
) || is_conditional_validity(edge
))
3206 isl_die(ctx
, isl_error_internal
,
3207 "backward (conditional) validity edge",
3212 map
= isl_map_copy(edge
->map
);
3213 tagged_condition
= isl_union_map_copy(edge
->tagged_condition
);
3214 tagged_validity
= isl_union_map_copy(edge
->tagged_validity
);
3216 dst
->edge
[dst
->n_edge
].src
= dst_src
;
3217 dst
->edge
[dst
->n_edge
].dst
= dst_dst
;
3218 dst
->edge
[dst
->n_edge
].map
= map
;
3219 dst
->edge
[dst
->n_edge
].tagged_condition
= tagged_condition
;
3220 dst
->edge
[dst
->n_edge
].tagged_validity
= tagged_validity
;
3221 dst
->edge
[dst
->n_edge
].types
= edge
->types
;
3224 if (edge
->tagged_condition
&& !tagged_condition
)
3226 if (edge
->tagged_validity
&& !tagged_validity
)
3229 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
3231 graph_find_edge(src
, t
, edge
->src
, edge
->dst
))
3233 if (graph_edge_table_add(ctx
, dst
, t
,
3234 &dst
->edge
[dst
->n_edge
- 1]) < 0)
3242 /* Compute the maximal number of variables over all nodes.
3243 * This is the maximal number of linearly independent schedule
3244 * rows that we need to compute.
3245 * Just in case we end up in a part of the dependence graph
3246 * with only lower-dimensional domains, we make sure we will
3247 * compute the required amount of extra linearly independent rows.
3249 static int compute_maxvar(struct isl_sched_graph
*graph
)
3254 for (i
= 0; i
< graph
->n
; ++i
) {
3255 struct isl_sched_node
*node
= &graph
->node
[i
];
3258 if (node_update_cmap(node
) < 0)
3260 nvar
= node
->nvar
+ graph
->n_row
- node
->rank
;
3261 if (nvar
> graph
->maxvar
)
3262 graph
->maxvar
= nvar
;
3268 /* Extract the subgraph of "graph" that consists of the node satisfying
3269 * "node_pred" and the edges satisfying "edge_pred" and store
3270 * the result in "sub".
3272 static int extract_sub_graph(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
3273 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
3274 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
3275 int data
, struct isl_sched_graph
*sub
)
3277 int i
, n
= 0, n_edge
= 0;
3280 for (i
= 0; i
< graph
->n
; ++i
)
3281 if (node_pred(&graph
->node
[i
], data
))
3283 for (i
= 0; i
< graph
->n_edge
; ++i
)
3284 if (edge_pred(&graph
->edge
[i
], data
))
3286 if (graph_alloc(ctx
, sub
, n
, n_edge
) < 0)
3288 if (copy_nodes(sub
, graph
, node_pred
, data
) < 0)
3290 if (graph_init_table(ctx
, sub
) < 0)
3292 for (t
= 0; t
<= isl_edge_last
; ++t
)
3293 sub
->max_edge
[t
] = graph
->max_edge
[t
];
3294 if (graph_init_edge_tables(ctx
, sub
) < 0)
3296 if (copy_edges(ctx
, sub
, graph
, edge_pred
, data
) < 0)
3298 sub
->n_row
= graph
->n_row
;
3299 sub
->max_row
= graph
->max_row
;
3300 sub
->n_total_row
= graph
->n_total_row
;
3301 sub
->band_start
= graph
->band_start
;
3306 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
3307 struct isl_sched_graph
*graph
);
3308 static __isl_give isl_schedule_node
*compute_schedule_wcc(
3309 isl_schedule_node
*node
, struct isl_sched_graph
*graph
);
3311 /* Compute a schedule for a subgraph of "graph". In particular, for
3312 * the graph composed of nodes that satisfy node_pred and edges that
3313 * that satisfy edge_pred.
3314 * If the subgraph is known to consist of a single component, then wcc should
3315 * be set and then we call compute_schedule_wcc on the constructed subgraph.
3316 * Otherwise, we call compute_schedule, which will check whether the subgraph
3319 * The schedule is inserted at "node" and the updated schedule node
3322 static __isl_give isl_schedule_node
*compute_sub_schedule(
3323 __isl_take isl_schedule_node
*node
, isl_ctx
*ctx
,
3324 struct isl_sched_graph
*graph
,
3325 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
3326 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
3329 struct isl_sched_graph split
= { 0 };
3331 if (extract_sub_graph(ctx
, graph
, node_pred
, edge_pred
, data
,
3336 node
= compute_schedule_wcc(node
, &split
);
3338 node
= compute_schedule(node
, &split
);
3340 graph_free(ctx
, &split
);
3343 graph_free(ctx
, &split
);
3344 return isl_schedule_node_free(node
);
3347 static int edge_scc_exactly(struct isl_sched_edge
*edge
, int scc
)
3349 return edge
->src
->scc
== scc
&& edge
->dst
->scc
== scc
;
3352 static int edge_dst_scc_at_most(struct isl_sched_edge
*edge
, int scc
)
3354 return edge
->dst
->scc
<= scc
;
3357 static int edge_src_scc_at_least(struct isl_sched_edge
*edge
, int scc
)
3359 return edge
->src
->scc
>= scc
;
3362 /* Reset the current band by dropping all its schedule rows.
3364 static int reset_band(struct isl_sched_graph
*graph
)
3369 drop
= graph
->n_total_row
- graph
->band_start
;
3370 graph
->n_total_row
-= drop
;
3371 graph
->n_row
-= drop
;
3373 for (i
= 0; i
< graph
->n
; ++i
) {
3374 struct isl_sched_node
*node
= &graph
->node
[i
];
3376 isl_map_free(node
->sched_map
);
3377 node
->sched_map
= NULL
;
3379 node
->sched
= isl_mat_drop_rows(node
->sched
,
3380 graph
->band_start
, drop
);
3389 /* Split the current graph into two parts and compute a schedule for each
3390 * part individually. In particular, one part consists of all SCCs up
3391 * to and including graph->src_scc, while the other part contains the other
3392 * SCCs. The split is enforced by a sequence node inserted at position "node"
3393 * in the schedule tree. Return the updated schedule node.
3394 * If either of these two parts consists of a sequence, then it is spliced
3395 * into the sequence containing the two parts.
3397 * The current band is reset. It would be possible to reuse
3398 * the previously computed rows as the first rows in the next
3399 * band, but recomputing them may result in better rows as we are looking
3400 * at a smaller part of the dependence graph.
3402 static __isl_give isl_schedule_node
*compute_split_schedule(
3403 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3407 isl_union_set_list
*filters
;
3412 if (reset_band(graph
) < 0)
3413 return isl_schedule_node_free(node
);
3417 ctx
= isl_schedule_node_get_ctx(node
);
3418 filters
= extract_split(ctx
, graph
);
3419 node
= isl_schedule_node_insert_sequence(node
, filters
);
3420 node
= isl_schedule_node_child(node
, 1);
3421 node
= isl_schedule_node_child(node
, 0);
3423 node
= compute_sub_schedule(node
, ctx
, graph
,
3424 &node_scc_at_least
, &edge_src_scc_at_least
,
3425 graph
->src_scc
+ 1, 0);
3426 is_seq
= isl_schedule_node_get_type(node
) == isl_schedule_node_sequence
;
3427 node
= isl_schedule_node_parent(node
);
3428 node
= isl_schedule_node_parent(node
);
3430 node
= isl_schedule_node_sequence_splice_child(node
, 1);
3431 node
= isl_schedule_node_child(node
, 0);
3432 node
= isl_schedule_node_child(node
, 0);
3433 node
= compute_sub_schedule(node
, ctx
, graph
,
3434 &node_scc_at_most
, &edge_dst_scc_at_most
,
3436 is_seq
= isl_schedule_node_get_type(node
) == isl_schedule_node_sequence
;
3437 node
= isl_schedule_node_parent(node
);
3438 node
= isl_schedule_node_parent(node
);
3440 node
= isl_schedule_node_sequence_splice_child(node
, 0);
3445 /* Insert a band node at position "node" in the schedule tree corresponding
3446 * to the current band in "graph". Mark the band node permutable
3447 * if "permutable" is set.
3448 * The partial schedules and the coincidence property are extracted
3449 * from the graph nodes.
3450 * Return the updated schedule node.
3452 static __isl_give isl_schedule_node
*insert_current_band(
3453 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
3459 isl_multi_pw_aff
*mpa
;
3460 isl_multi_union_pw_aff
*mupa
;
3466 isl_die(isl_schedule_node_get_ctx(node
), isl_error_internal
,
3467 "graph should have at least one node",
3468 return isl_schedule_node_free(node
));
3470 start
= graph
->band_start
;
3471 end
= graph
->n_total_row
;
3474 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[0], start
, n
);
3475 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3476 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3478 for (i
= 1; i
< graph
->n
; ++i
) {
3479 isl_multi_union_pw_aff
*mupa_i
;
3481 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[i
],
3483 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3484 mupa_i
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3485 mupa
= isl_multi_union_pw_aff_union_add(mupa
, mupa_i
);
3487 node
= isl_schedule_node_insert_partial_schedule(node
, mupa
);
3489 for (i
= 0; i
< n
; ++i
)
3490 node
= isl_schedule_node_band_member_set_coincident(node
, i
,
3491 graph
->node
[0].coincident
[start
+ i
]);
3492 node
= isl_schedule_node_band_set_permutable(node
, permutable
);
3497 /* Update the dependence relations based on the current schedule,
3498 * add the current band to "node" and then continue with the computation
3500 * Return the updated schedule node.
3502 static __isl_give isl_schedule_node
*compute_next_band(
3503 __isl_take isl_schedule_node
*node
,
3504 struct isl_sched_graph
*graph
, int permutable
)
3511 ctx
= isl_schedule_node_get_ctx(node
);
3512 if (update_edges(ctx
, graph
) < 0)
3513 return isl_schedule_node_free(node
);
3514 node
= insert_current_band(node
, graph
, permutable
);
3517 node
= isl_schedule_node_child(node
, 0);
3518 node
= compute_schedule(node
, graph
);
3519 node
= isl_schedule_node_parent(node
);
3524 /* Add the constraints "coef" derived from an edge from "node" to itself
3525 * to graph->lp in order to respect the dependences and to try and carry them.
3526 * "pos" is the sequence number of the edge that needs to be carried.
3527 * "coef" represents general constraints on coefficients (c_0, c_n, c_x)
3528 * of valid constraints for (y - x) with x and y instances of the node.
3530 * The constraints added to graph->lp need to enforce
3532 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3533 * = c_j_x (y - x) >= e_i
3535 * for each (x,y) in the dependence relation of the edge.
3536 * That is, (-e_i, 0, c_j_x) needs to be plugged in for (c_0, c_n, c_x),
3537 * taking into account that each coefficient in c_j_x is represented
3538 * as a pair of non-negative coefficients.
3540 static isl_stat
add_intra_constraints(struct isl_sched_graph
*graph
,
3541 struct isl_sched_node
*node
, __isl_take isl_basic_set
*coef
, int pos
)
3545 isl_dim_map
*dim_map
;
3548 return isl_stat_error
;
3550 ctx
= isl_basic_set_get_ctx(coef
);
3551 offset
= coef_var_offset(coef
);
3552 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, 1);
3553 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3554 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
3559 /* Add the constraints "coef" derived from an edge from "src" to "dst"
3560 * to graph->lp in order to respect the dependences and to try and carry them.
3561 * "pos" is the sequence number of the edge that needs to be carried.
3562 * "coef" represents general constraints on coefficients (c_0, c_n, c_x, c_y)
3563 * of valid constraints for (x, y) with x and y instances of "src" and "dst".
3565 * The constraints added to graph->lp need to enforce
3567 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3569 * for each (x,y) in the dependence relation of the edge.
3571 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, -c_j_x, c_k_x)
3572 * needs to be plugged in for (c_0, c_n, c_x, c_y),
3573 * taking into account that each coefficient in c_j_x and c_k_x is represented
3574 * as a pair of non-negative coefficients.
3576 static isl_stat
add_inter_constraints(struct isl_sched_graph
*graph
,
3577 struct isl_sched_node
*src
, struct isl_sched_node
*dst
,
3578 __isl_take isl_basic_set
*coef
, int pos
)
3582 isl_dim_map
*dim_map
;
3585 return isl_stat_error
;
3587 ctx
= isl_basic_set_get_ctx(coef
);
3588 offset
= coef_var_offset(coef
);
3589 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, 1);
3590 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3591 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
3596 /* Data structure collecting information used during the construction
3597 * of an LP for carrying dependences.
3599 * "intra" is a sequence of coefficient constraints for intra-node edges.
3600 * "inter" is a sequence of coefficient constraints for inter-node edges.
3603 isl_basic_set_list
*intra
;
3604 isl_basic_set_list
*inter
;
3607 /* Free all the data stored in "carry".
3609 static void isl_carry_clear(struct isl_carry
*carry
)
3611 isl_basic_set_list_free(carry
->intra
);
3612 isl_basic_set_list_free(carry
->inter
);
3615 /* Return a pointer to the node in "graph" that lives in "space".
3616 * If the requested node has been compressed, then "space"
3617 * corresponds to the compressed space.
3619 * First try and see if "space" is the space of an uncompressed node.
3620 * If so, return that node.
3621 * Otherwise, "space" was constructed by construct_compressed_id and
3622 * contains a user pointer pointing to the node in the tuple id.
3624 static struct isl_sched_node
*graph_find_compressed_node(isl_ctx
*ctx
,
3625 struct isl_sched_graph
*graph
, __isl_keep isl_space
*space
)
3628 struct isl_sched_node
*node
;
3633 node
= graph_find_node(ctx
, graph
, space
);
3637 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
3638 node
= isl_id_get_user(id
);
3644 if (!(node
>= &graph
->node
[0] && node
< &graph
->node
[graph
->n
]))
3645 isl_die(ctx
, isl_error_internal
,
3646 "space points to invalid node", return NULL
);
3651 /* Internal data structure for add_all_constraints.
3653 * "graph" is the schedule constraint graph for which an LP problem
3654 * is being constructed.
3655 * "pos" is the position of the next edge that needs to be carried.
3657 struct isl_add_all_constraints_data
{
3659 struct isl_sched_graph
*graph
;
3663 /* Add the constraints "coef" derived from an edge from a node to itself
3664 * to data->graph->lp in order to respect the dependences and
3665 * to try and carry them.
3667 * The space of "coef" is of the form
3669 * coefficients[[c_cst, c_n] -> S[c_x]]
3671 * with S[c_x] the (compressed) space of the node.
3672 * Extract the node from the space and call add_intra_constraints.
3674 static isl_stat
lp_add_intra(__isl_take isl_basic_set
*coef
, void *user
)
3676 struct isl_add_all_constraints_data
*data
= user
;
3678 struct isl_sched_node
*node
;
3680 space
= isl_basic_set_get_space(coef
);
3681 space
= isl_space_range(isl_space_unwrap(space
));
3682 node
= graph_find_compressed_node(data
->ctx
, data
->graph
, space
);
3683 isl_space_free(space
);
3684 return add_intra_constraints(data
->graph
, node
, coef
, data
->pos
++);
3687 /* Add the constraints "coef" derived from an edge from a node j
3688 * to a node k to data->graph->lp in order to respect the dependences and
3689 * to try and carry them.
3691 * The space of "coef" is of the form
3693 * coefficients[[c_cst, c_n] -> [S_j[c_x] -> S_k[c_y]]]
3695 * with S_j[c_x] and S_k[c_y] the (compressed) spaces of the nodes.
3696 * Extract the nodes from the space and call add_inter_constraints.
3698 static isl_stat
lp_add_inter(__isl_take isl_basic_set
*coef
, void *user
)
3700 struct isl_add_all_constraints_data
*data
= user
;
3701 isl_space
*space
, *dom
;
3702 struct isl_sched_node
*src
, *dst
;
3704 space
= isl_basic_set_get_space(coef
);
3705 space
= isl_space_unwrap(isl_space_range(isl_space_unwrap(space
)));
3706 dom
= isl_space_domain(isl_space_copy(space
));
3707 src
= graph_find_compressed_node(data
->ctx
, data
->graph
, dom
);
3708 isl_space_free(dom
);
3709 space
= isl_space_range(space
);
3710 dst
= graph_find_compressed_node(data
->ctx
, data
->graph
, space
);
3711 isl_space_free(space
);
3713 return add_inter_constraints(data
->graph
, src
, dst
, coef
, data
->pos
++);
3716 /* Add constraints to graph->lp that force all (conditional) validity
3717 * dependences to be respected and attempt to carry them.
3718 * "intra" is the sequence of coefficient constraints for intra-node edges.
3719 * "inter" is the sequence of coefficient constraints for inter-node edges.
3721 static isl_stat
add_all_constraints(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
3722 __isl_keep isl_basic_set_list
*intra
,
3723 __isl_keep isl_basic_set_list
*inter
)
3725 struct isl_add_all_constraints_data data
= { ctx
, graph
};
3728 if (isl_basic_set_list_foreach(intra
, &lp_add_intra
, &data
) < 0)
3729 return isl_stat_error
;
3730 if (isl_basic_set_list_foreach(inter
, &lp_add_inter
, &data
) < 0)
3731 return isl_stat_error
;
3735 /* Internal data structure for count_all_constraints
3736 * for keeping track of the number of equality and inequality constraints.
3738 struct isl_sched_count
{
3743 /* Add the number of equality and inequality constraints of "bset"
3744 * to data->n_eq and data->n_ineq.
3746 static isl_stat
bset_update_count(__isl_take isl_basic_set
*bset
, void *user
)
3748 struct isl_sched_count
*data
= user
;
3750 data
->n_eq
+= isl_basic_set_n_equality(bset
);
3751 data
->n_ineq
+= isl_basic_set_n_inequality(bset
);
3752 isl_basic_set_free(bset
);
3757 /* Count the number of equality and inequality constraints
3758 * that will be added to the carry_lp problem.
3759 * We count each edge exactly once.
3760 * "intra" is the sequence of coefficient constraints for intra-node edges.
3761 * "inter" is the sequence of coefficient constraints for inter-node edges.
3763 static isl_stat
count_all_constraints(__isl_keep isl_basic_set_list
*intra
,
3764 __isl_keep isl_basic_set_list
*inter
, int *n_eq
, int *n_ineq
)
3766 struct isl_sched_count data
;
3768 data
.n_eq
= data
.n_ineq
= 0;
3769 if (isl_basic_set_list_foreach(inter
, &bset_update_count
, &data
) < 0)
3770 return isl_stat_error
;
3771 if (isl_basic_set_list_foreach(intra
, &bset_update_count
, &data
) < 0)
3772 return isl_stat_error
;
3775 *n_ineq
= data
.n_ineq
;
3780 /* Construct an LP problem for finding schedule coefficients
3781 * such that the schedule carries as many validity dependences as possible.
3782 * In particular, for each dependence i, we bound the dependence distance
3783 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3784 * of all e_i's. Dependences with e_i = 0 in the solution are simply
3785 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3786 * "intra" is the sequence of coefficient constraints for intra-node edges.
3787 * "inter" is the sequence of coefficient constraints for inter-node edges.
3788 * "n_edge" is the total number of edges.
3790 * All variables of the LP are non-negative. The actual coefficients
3791 * may be negative, so each coefficient is represented as the difference
3792 * of two non-negative variables. The negative part always appears
3793 * immediately before the positive part.
3794 * Other than that, the variables have the following order
3796 * - sum of (1 - e_i) over all edges
3797 * - sum of all c_n coefficients
3798 * (unconstrained when computing non-parametric schedules)
3799 * - sum of positive and negative parts of all c_x coefficients
3804 * - c_i_n (if parametric)
3805 * - positive and negative parts of c_i_x
3807 * The constraints are those from the (validity) edges plus three equalities
3808 * to express the sums and n_edge inequalities to express e_i <= 1.
3810 static isl_stat
setup_carry_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
3811 int n_edge
, __isl_keep isl_basic_set_list
*intra
,
3812 __isl_keep isl_basic_set_list
*inter
)
3821 for (i
= 0; i
< graph
->n
; ++i
) {
3822 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
3823 node
->start
= total
;
3824 total
+= 1 + node
->nparam
+ 2 * node
->nvar
;
3827 if (count_all_constraints(intra
, inter
, &n_eq
, &n_ineq
) < 0)
3828 return isl_stat_error
;
3830 dim
= isl_space_set_alloc(ctx
, 0, total
);
3831 isl_basic_set_free(graph
->lp
);
3834 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
3835 graph
->lp
= isl_basic_set_set_rational(graph
->lp
);
3837 k
= isl_basic_set_alloc_equality(graph
->lp
);
3839 return isl_stat_error
;
3840 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
3841 isl_int_set_si(graph
->lp
->eq
[k
][0], -n_edge
);
3842 isl_int_set_si(graph
->lp
->eq
[k
][1], 1);
3843 for (i
= 0; i
< n_edge
; ++i
)
3844 isl_int_set_si(graph
->lp
->eq
[k
][4 + i
], 1);
3846 if (add_param_sum_constraint(graph
, 1) < 0)
3847 return isl_stat_error
;
3848 if (add_var_sum_constraint(graph
, 2) < 0)
3849 return isl_stat_error
;
3851 for (i
= 0; i
< n_edge
; ++i
) {
3852 k
= isl_basic_set_alloc_inequality(graph
->lp
);
3854 return isl_stat_error
;
3855 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
3856 isl_int_set_si(graph
->lp
->ineq
[k
][4 + i
], -1);
3857 isl_int_set_si(graph
->lp
->ineq
[k
][0], 1);
3860 if (add_all_constraints(ctx
, graph
, intra
, inter
) < 0)
3861 return isl_stat_error
;
3866 static __isl_give isl_schedule_node
*compute_component_schedule(
3867 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
3870 /* Comparison function for sorting the statements based on
3871 * the corresponding value in "r".
3873 static int smaller_value(const void *a
, const void *b
, void *data
)
3879 return isl_int_cmp(r
->el
[*i1
], r
->el
[*i2
]);
3882 /* If the schedule_split_scaled option is set and if the linear
3883 * parts of the scheduling rows for all nodes in the graphs have
3884 * a non-trivial common divisor, then split off the remainder of the
3885 * constant term modulo this common divisor from the linear part.
3886 * Otherwise, insert a band node directly and continue with
3887 * the construction of the schedule.
3889 * If a non-trivial common divisor is found, then
3890 * the linear part is reduced and the remainder is enforced
3891 * by a sequence node with the children placed in the order
3892 * of this remainder.
3893 * In particular, we assign an scc index based on the remainder and
3894 * then rely on compute_component_schedule to insert the sequence and
3895 * to continue the schedule construction on each part.
3897 static __isl_give isl_schedule_node
*split_scaled(
3898 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3911 ctx
= isl_schedule_node_get_ctx(node
);
3912 if (!ctx
->opt
->schedule_split_scaled
)
3913 return compute_next_band(node
, graph
, 0);
3915 return compute_next_band(node
, graph
, 0);
3918 isl_int_init(gcd_i
);
3920 isl_int_set_si(gcd
, 0);
3922 row
= isl_mat_rows(graph
->node
[0].sched
) - 1;
3924 for (i
= 0; i
< graph
->n
; ++i
) {
3925 struct isl_sched_node
*node
= &graph
->node
[i
];
3926 int cols
= isl_mat_cols(node
->sched
);
3928 isl_seq_gcd(node
->sched
->row
[row
] + 1, cols
- 1, &gcd_i
);
3929 isl_int_gcd(gcd
, gcd
, gcd_i
);
3932 isl_int_clear(gcd_i
);
3934 if (isl_int_cmp_si(gcd
, 1) <= 0) {
3936 return compute_next_band(node
, graph
, 0);
3939 r
= isl_vec_alloc(ctx
, graph
->n
);
3940 order
= isl_calloc_array(ctx
, int, graph
->n
);
3944 for (i
= 0; i
< graph
->n
; ++i
) {
3945 struct isl_sched_node
*node
= &graph
->node
[i
];
3948 isl_int_fdiv_r(r
->el
[i
], node
->sched
->row
[row
][0], gcd
);
3949 isl_int_fdiv_q(node
->sched
->row
[row
][0],
3950 node
->sched
->row
[row
][0], gcd
);
3951 isl_int_mul(node
->sched
->row
[row
][0],
3952 node
->sched
->row
[row
][0], gcd
);
3953 node
->sched
= isl_mat_scale_down_row(node
->sched
, row
, gcd
);
3958 if (isl_sort(order
, graph
->n
, sizeof(order
[0]), &smaller_value
, r
) < 0)
3962 for (i
= 0; i
< graph
->n
; ++i
) {
3963 if (i
> 0 && isl_int_ne(r
->el
[order
[i
- 1]], r
->el
[order
[i
]]))
3965 graph
->node
[order
[i
]].scc
= scc
;
3974 if (update_edges(ctx
, graph
) < 0)
3975 return isl_schedule_node_free(node
);
3976 node
= insert_current_band(node
, graph
, 0);
3979 node
= isl_schedule_node_child(node
, 0);
3980 node
= compute_component_schedule(node
, graph
, 0);
3981 node
= isl_schedule_node_parent(node
);
3988 return isl_schedule_node_free(node
);
3991 /* Is the schedule row "sol" trivial on node "node"?
3992 * That is, is the solution zero on the dimensions linearly independent of
3993 * the previously found solutions?
3994 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3996 * Each coefficient is represented as the difference between
3997 * two non-negative values in "sol". "sol" has been computed
3998 * in terms of the original iterators (i.e., without use of cmap).
3999 * We construct the schedule row s and write it as a linear
4000 * combination of (linear combinations of) previously computed schedule rows.
4001 * s = Q c or c = U s.
4002 * If the final entries of c are all zero, then the solution is trivial.
4004 static int is_trivial(struct isl_sched_node
*node
, __isl_keep isl_vec
*sol
)
4011 if (node
->nvar
== node
->rank
)
4014 node_sol
= extract_var_coef(node
, sol
);
4015 node_sol
= isl_mat_vec_product(isl_mat_copy(node
->cinv
), node_sol
);
4019 trivial
= isl_seq_first_non_zero(node_sol
->el
+ node
->rank
,
4020 node
->nvar
- node
->rank
) == -1;
4022 isl_vec_free(node_sol
);
4027 /* Is the schedule row "sol" trivial on any node where it should
4029 * "sol" has been computed in terms of the original iterators
4030 * (i.e., without use of cmap).
4031 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
4033 static int is_any_trivial(struct isl_sched_graph
*graph
,
4034 __isl_keep isl_vec
*sol
)
4038 for (i
= 0; i
< graph
->n
; ++i
) {
4039 struct isl_sched_node
*node
= &graph
->node
[i
];
4042 if (!needs_row(graph
, node
))
4044 trivial
= is_trivial(node
, sol
);
4045 if (trivial
< 0 || trivial
)
4052 /* Does the schedule represented by "sol" perform loop coalescing on "node"?
4053 * If so, return the position of the coalesced dimension.
4054 * Otherwise, return node->nvar or -1 on error.
4056 * In particular, look for pairs of coefficients c_i and c_j such that
4057 * |c_j/c_i| >= size_i, i.e., |c_j| >= |c_i * size_i|.
4058 * If any such pair is found, then return i.
4059 * If size_i is infinity, then no check on c_i needs to be performed.
4061 static int find_node_coalescing(struct isl_sched_node
*node
,
4062 __isl_keep isl_vec
*sol
)
4068 if (node
->nvar
<= 1)
4071 csol
= extract_var_coef(node
, sol
);
4075 for (i
= 0; i
< node
->nvar
; ++i
) {
4078 if (isl_int_is_zero(csol
->el
[i
]))
4080 v
= isl_multi_val_get_val(node
->sizes
, i
);
4083 if (!isl_val_is_int(v
)) {
4087 isl_int_mul(max
, v
->n
, csol
->el
[i
]);
4090 for (j
= 0; j
< node
->nvar
; ++j
) {
4093 if (isl_int_abs_ge(csol
->el
[j
], max
))
4109 /* Force the schedule coefficient at position "pos" of "node" to be zero
4111 * The coefficient is encoded as the difference between two non-negative
4112 * variables. Force these two variables to have the same value.
4114 static __isl_give isl_tab_lexmin
*zero_out_node_coef(
4115 __isl_take isl_tab_lexmin
*tl
, struct isl_sched_node
*node
, int pos
)
4121 ctx
= isl_space_get_ctx(node
->space
);
4122 dim
= isl_tab_lexmin_dim(tl
);
4124 return isl_tab_lexmin_free(tl
);
4125 eq
= isl_vec_alloc(ctx
, 1 + dim
);
4126 eq
= isl_vec_clr(eq
);
4128 return isl_tab_lexmin_free(tl
);
4130 pos
= 1 + node_var_coef_offset(node
) + 2 * pos
;
4131 isl_int_set_si(eq
->el
[pos
], 1);
4132 isl_int_set_si(eq
->el
[pos
+ 1], -1);
4133 tl
= isl_tab_lexmin_add_eq(tl
, eq
->el
);
4139 /* Return the lexicographically smallest rational point in the basic set
4140 * from which "tl" was constructed, double checking that this input set
4143 static __isl_give isl_vec
*non_empty_solution(__isl_keep isl_tab_lexmin
*tl
)
4147 sol
= isl_tab_lexmin_get_solution(tl
);
4151 isl_die(isl_vec_get_ctx(sol
), isl_error_internal
,
4152 "error in schedule construction",
4153 return isl_vec_free(sol
));
4157 /* Does the solution "sol" of the LP problem constructed by setup_carry_lp
4158 * carry any of the "n_edge" groups of dependences?
4159 * The value in the first position is the sum of (1 - e_i) over all "n_edge"
4160 * edges, with 0 <= e_i <= 1 equal to 1 when the dependences represented
4161 * by the edge are carried by the solution.
4162 * If the sum of the (1 - e_i) is smaller than "n_edge" then at least
4163 * one of those is carried.
4165 * Note that despite the fact that the problem is solved using a rational
4166 * solver, the solution is guaranteed to be integral.
4167 * Specifically, the dependence distance lower bounds e_i (and therefore
4168 * also their sum) are integers. See Lemma 5 of [1].
4170 * Any potential denominator of the sum is cleared by this function.
4171 * The denominator is not relevant for any of the other elements
4174 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4175 * Problem, Part II: Multi-Dimensional Time.
4176 * In Intl. Journal of Parallel Programming, 1992.
4178 static int carries_dependences(__isl_keep isl_vec
*sol
, int n_edge
)
4180 isl_int_divexact(sol
->el
[1], sol
->el
[1], sol
->el
[0]);
4181 isl_int_set_si(sol
->el
[0], 1);
4182 return isl_int_cmp_si(sol
->el
[1], n_edge
) < 0;
4185 /* Return the lexicographically smallest rational point in "lp",
4186 * assuming that all variables are non-negative and performing some
4187 * additional sanity checks.
4188 * In particular, "lp" should not be empty by construction.
4189 * Double check that this is the case.
4190 * If dependences are not carried for any of the "n_edge" edges,
4191 * then return an empty vector.
4193 * If the schedule_treat_coalescing option is set and
4194 * if the computed schedule performs loop coalescing on a given node,
4195 * i.e., if it is of the form
4197 * c_i i + c_j j + ...
4199 * with |c_j/c_i| >= size_i, then force the coefficient c_i to be zero
4200 * to cut out this solution. Repeat this process until no more loop
4201 * coalescing occurs or until no more dependences can be carried.
4202 * In the latter case, revert to the previously computed solution.
4204 static __isl_give isl_vec
*non_neg_lexmin(struct isl_sched_graph
*graph
,
4205 __isl_take isl_basic_set
*lp
, int n_edge
)
4210 isl_vec
*sol
, *prev
= NULL
;
4211 int treat_coalescing
;
4215 ctx
= isl_basic_set_get_ctx(lp
);
4216 treat_coalescing
= isl_options_get_schedule_treat_coalescing(ctx
);
4217 tl
= isl_tab_lexmin_from_basic_set(lp
);
4220 sol
= non_empty_solution(tl
);
4224 if (!carries_dependences(sol
, n_edge
)) {
4226 prev
= isl_vec_alloc(ctx
, 0);
4231 prev
= isl_vec_free(prev
);
4232 if (!treat_coalescing
)
4234 for (i
= 0; i
< graph
->n
; ++i
) {
4235 struct isl_sched_node
*node
= &graph
->node
[i
];
4237 pos
= find_node_coalescing(node
, sol
);
4240 if (pos
< node
->nvar
)
4245 tl
= zero_out_node_coef(tl
, &graph
->node
[i
], pos
);
4247 } while (i
< graph
->n
);
4249 isl_tab_lexmin_free(tl
);
4253 isl_tab_lexmin_free(tl
);
4259 /* If "edge" is an edge from a node to itself, then add the corresponding
4260 * dependence relation to "umap".
4261 * If "node" has been compressed, then the dependence relation
4262 * is also compressed first.
4264 static __isl_give isl_union_map
*add_intra(__isl_take isl_union_map
*umap
,
4265 struct isl_sched_edge
*edge
)
4268 struct isl_sched_node
*node
= edge
->src
;
4270 if (edge
->src
!= edge
->dst
)
4273 map
= isl_map_copy(edge
->map
);
4274 if (node
->compressed
) {
4275 map
= isl_map_preimage_domain_multi_aff(map
,
4276 isl_multi_aff_copy(node
->decompress
));
4277 map
= isl_map_preimage_range_multi_aff(map
,
4278 isl_multi_aff_copy(node
->decompress
));
4280 umap
= isl_union_map_add_map(umap
, map
);
4284 /* If "edge" is an edge from a node to another node, then add the corresponding
4285 * dependence relation to "umap".
4286 * If the source or destination nodes of "edge" have been compressed,
4287 * then the dependence relation is also compressed first.
4289 static __isl_give isl_union_map
*add_inter(__isl_take isl_union_map
*umap
,
4290 struct isl_sched_edge
*edge
)
4294 if (edge
->src
== edge
->dst
)
4297 map
= isl_map_copy(edge
->map
);
4298 if (edge
->src
->compressed
)
4299 map
= isl_map_preimage_domain_multi_aff(map
,
4300 isl_multi_aff_copy(edge
->src
->decompress
));
4301 if (edge
->dst
->compressed
)
4302 map
= isl_map_preimage_range_multi_aff(map
,
4303 isl_multi_aff_copy(edge
->dst
->decompress
));
4304 umap
= isl_union_map_add_map(umap
, map
);
4308 /* For each (conditional) validity edge in "graph",
4309 * add the corresponding dependence relation using "add"
4310 * to a collection of dependence relations and return the result.
4312 static __isl_give isl_union_map
*collect_validity(struct isl_sched_graph
*graph
,
4313 __isl_give isl_union_map
*(*add
)(__isl_take isl_union_map
*umap
,
4314 struct isl_sched_edge
*edge
))
4318 isl_union_map
*umap
;
4320 space
= isl_space_copy(graph
->node
[0].space
);
4321 umap
= isl_union_map_empty(space
);
4323 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4324 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
4326 if (!is_any_validity(edge
))
4329 umap
= add(umap
, edge
);
4335 /* For each dependence relation on a (conditional) validity edge
4336 * from a node to itself,
4337 * construct the set of coefficients of valid constraints for elements
4338 * in that dependence relation and collect the results.
4339 * In particular, for each dependence relation R, constraints
4340 * on coefficients (c_0, c_n, c_x) are constructed such that
4342 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
4344 * This computation is essentially the same as that performed
4345 * by intra_coefficients, except that it operates on multiple
4348 * Note that if a dependence relation is a union of basic maps,
4349 * then each basic map needs to be treated individually as it may only
4350 * be possible to carry the dependences expressed by some of those
4351 * basic maps and not all of them.
4352 * The collected validity constraints are therefore not coalesced and
4353 * it is assumed that they are not coalesced automatically.
4354 * Duplicate basic maps can be removed, however.
4355 * In particular, if the same basic map appears as a disjunct
4356 * in multiple edges, then it only needs to be carried once.
4358 static __isl_give isl_basic_set_list
*collect_intra_validity(
4359 struct isl_sched_graph
*graph
)
4361 isl_union_map
*intra
;
4362 isl_union_set
*delta
;
4363 isl_basic_set_list
*list
;
4365 intra
= collect_validity(graph
, &add_intra
);
4366 delta
= isl_union_map_deltas(intra
);
4367 delta
= isl_union_set_remove_divs(delta
);
4368 list
= isl_union_set_get_basic_set_list(delta
);
4369 isl_union_set_free(delta
);
4371 return isl_basic_set_list_coefficients(list
);
4374 /* For each dependence relation on a (conditional) validity edge
4375 * from a node to some other node,
4376 * construct the set of coefficients of valid constraints for elements
4377 * in that dependence relation and collect the results.
4378 * In particular, for each dependence relation R, constraints
4379 * on coefficients (c_0, c_n, c_x, c_y) are constructed such that
4381 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
4383 * This computation is essentially the same as that performed
4384 * by inter_coefficients, except that it operates on multiple
4387 * Note that if a dependence relation is a union of basic maps,
4388 * then each basic map needs to be treated individually as it may only
4389 * be possible to carry the dependences expressed by some of those
4390 * basic maps and not all of them.
4391 * The collected validity constraints are therefore not coalesced and
4392 * it is assumed that they are not coalesced automatically.
4393 * Duplicate basic maps can be removed, however.
4394 * In particular, if the same basic map appears as a disjunct
4395 * in multiple edges, then it only needs to be carried once.
4397 static __isl_give isl_basic_set_list
*collect_inter_validity(
4398 struct isl_sched_graph
*graph
)
4400 isl_union_map
*inter
;
4401 isl_union_set
*wrap
;
4402 isl_basic_set_list
*list
;
4404 inter
= collect_validity(graph
, &add_inter
);
4405 inter
= isl_union_map_remove_divs(inter
);
4406 wrap
= isl_union_map_wrap(inter
);
4407 list
= isl_union_set_get_basic_set_list(wrap
);
4408 isl_union_set_free(wrap
);
4409 return isl_basic_set_list_coefficients(list
);
4412 /* Construct an LP problem for finding schedule coefficients
4413 * such that the schedule carries as many of the validity dependences
4415 * return the lexicographically smallest non-trivial solution.
4417 * The variable "n_edge" stores the number of groups that should be carried.
4418 * If none of the "n_edge" groups can be carried
4419 * then return an empty vector.
4420 * If, moreover, "n_edge" is zero, then the LP problem does not even
4421 * need to be constructed.
4423 static __isl_give isl_vec
*compute_carrying_sol(isl_ctx
*ctx
,
4424 struct isl_sched_graph
*graph
)
4426 int n_intra
, n_inter
;
4429 struct isl_carry carry
= { 0 };
4431 carry
.intra
= collect_intra_validity(graph
);
4432 carry
.inter
= collect_inter_validity(graph
);
4433 if (!carry
.intra
|| !carry
.inter
)
4435 n_intra
= isl_basic_set_list_n_basic_set(carry
.intra
);
4436 n_inter
= isl_basic_set_list_n_basic_set(carry
.inter
);
4437 n_edge
= n_intra
+ n_inter
;
4439 isl_carry_clear(&carry
);
4440 return isl_vec_alloc(ctx
, 0);
4443 if (setup_carry_lp(ctx
, graph
, n_edge
, carry
.intra
, carry
.inter
) < 0)
4446 isl_carry_clear(&carry
);
4447 lp
= isl_basic_set_copy(graph
->lp
);
4448 return non_neg_lexmin(graph
, lp
, n_edge
);
4450 isl_carry_clear(&carry
);
4454 /* Construct a schedule row for each node such that as many validity dependences
4455 * as possible are carried and then continue with the next band.
4457 * If there are no validity dependences, then no dependence can be carried and
4458 * the procedure is guaranteed to fail. If there is more than one component,
4459 * then try computing a schedule on each component separately
4460 * to prevent or at least postpone this failure.
4462 * If a schedule row is computed, then check that dependences are carried
4463 * for at least one of the edges.
4465 * If the computed schedule row turns out to be trivial on one or
4466 * more nodes where it should not be trivial, then we throw it away
4467 * and try again on each component separately.
4469 * If there is only one component, then we accept the schedule row anyway,
4470 * but we do not consider it as a complete row and therefore do not
4471 * increment graph->n_row. Note that the ranks of the nodes that
4472 * do get a non-trivial schedule part will get updated regardless and
4473 * graph->maxvar is computed based on these ranks. The test for
4474 * whether more schedule rows are required in compute_schedule_wcc
4475 * is therefore not affected.
4477 * Insert a band corresponding to the schedule row at position "node"
4478 * of the schedule tree and continue with the construction of the schedule.
4479 * This insertion and the continued construction is performed by split_scaled
4480 * after optionally checking for non-trivial common divisors.
4482 static __isl_give isl_schedule_node
*carry_dependences(
4483 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4492 ctx
= isl_schedule_node_get_ctx(node
);
4493 sol
= compute_carrying_sol(ctx
, graph
);
4495 return isl_schedule_node_free(node
);
4496 if (sol
->size
== 0) {
4499 return compute_component_schedule(node
, graph
, 1);
4500 isl_die(ctx
, isl_error_unknown
, "unable to carry dependences",
4501 return isl_schedule_node_free(node
));
4504 trivial
= is_any_trivial(graph
, sol
);
4506 sol
= isl_vec_free(sol
);
4507 } else if (trivial
&& graph
->scc
> 1) {
4509 return compute_component_schedule(node
, graph
, 1);
4512 if (update_schedule(graph
, sol
, 0, 0) < 0)
4513 return isl_schedule_node_free(node
);
4517 return split_scaled(node
, graph
);
4520 /* Topologically sort statements mapped to the same schedule iteration
4521 * and add insert a sequence node in front of "node"
4522 * corresponding to this order.
4523 * If "initialized" is set, then it may be assumed that compute_maxvar
4524 * has been called on the current band. Otherwise, call
4525 * compute_maxvar if and before carry_dependences gets called.
4527 * If it turns out to be impossible to sort the statements apart,
4528 * because different dependences impose different orderings
4529 * on the statements, then we extend the schedule such that
4530 * it carries at least one more dependence.
4532 static __isl_give isl_schedule_node
*sort_statements(
4533 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
4537 isl_union_set_list
*filters
;
4542 ctx
= isl_schedule_node_get_ctx(node
);
4544 isl_die(ctx
, isl_error_internal
,
4545 "graph should have at least one node",
4546 return isl_schedule_node_free(node
));
4551 if (update_edges(ctx
, graph
) < 0)
4552 return isl_schedule_node_free(node
);
4554 if (graph
->n_edge
== 0)
4557 if (detect_sccs(ctx
, graph
) < 0)
4558 return isl_schedule_node_free(node
);
4561 if (graph
->scc
< graph
->n
) {
4562 if (!initialized
&& compute_maxvar(graph
) < 0)
4563 return isl_schedule_node_free(node
);
4564 return carry_dependences(node
, graph
);
4567 filters
= extract_sccs(ctx
, graph
);
4568 node
= isl_schedule_node_insert_sequence(node
, filters
);
4573 /* Are there any (non-empty) (conditional) validity edges in the graph?
4575 static int has_validity_edges(struct isl_sched_graph
*graph
)
4579 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4582 empty
= isl_map_plain_is_empty(graph
->edge
[i
].map
);
4587 if (is_any_validity(&graph
->edge
[i
]))
4594 /* Should we apply a Feautrier step?
4595 * That is, did the user request the Feautrier algorithm and are
4596 * there any validity dependences (left)?
4598 static int need_feautrier_step(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
4600 if (ctx
->opt
->schedule_algorithm
!= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
)
4603 return has_validity_edges(graph
);
4606 /* Compute a schedule for a connected dependence graph using Feautrier's
4607 * multi-dimensional scheduling algorithm and return the updated schedule node.
4609 * The original algorithm is described in [1].
4610 * The main idea is to minimize the number of scheduling dimensions, by
4611 * trying to satisfy as many dependences as possible per scheduling dimension.
4613 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4614 * Problem, Part II: Multi-Dimensional Time.
4615 * In Intl. Journal of Parallel Programming, 1992.
4617 static __isl_give isl_schedule_node
*compute_schedule_wcc_feautrier(
4618 isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4620 return carry_dependences(node
, graph
);
4623 /* Turn off the "local" bit on all (condition) edges.
4625 static void clear_local_edges(struct isl_sched_graph
*graph
)
4629 for (i
= 0; i
< graph
->n_edge
; ++i
)
4630 if (is_condition(&graph
->edge
[i
]))
4631 clear_local(&graph
->edge
[i
]);
4634 /* Does "graph" have both condition and conditional validity edges?
4636 static int need_condition_check(struct isl_sched_graph
*graph
)
4639 int any_condition
= 0;
4640 int any_conditional_validity
= 0;
4642 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4643 if (is_condition(&graph
->edge
[i
]))
4645 if (is_conditional_validity(&graph
->edge
[i
]))
4646 any_conditional_validity
= 1;
4649 return any_condition
&& any_conditional_validity
;
4652 /* Does "graph" contain any coincidence edge?
4654 static int has_any_coincidence(struct isl_sched_graph
*graph
)
4658 for (i
= 0; i
< graph
->n_edge
; ++i
)
4659 if (is_coincidence(&graph
->edge
[i
]))
4665 /* Extract the final schedule row as a map with the iteration domain
4666 * of "node" as domain.
4668 static __isl_give isl_map
*final_row(struct isl_sched_node
*node
)
4673 row
= isl_mat_rows(node
->sched
) - 1;
4674 ma
= node_extract_partial_schedule_multi_aff(node
, row
, 1);
4675 return isl_map_from_multi_aff(ma
);
4678 /* Is the conditional validity dependence in the edge with index "edge_index"
4679 * violated by the latest (i.e., final) row of the schedule?
4680 * That is, is i scheduled after j
4681 * for any conditional validity dependence i -> j?
4683 static int is_violated(struct isl_sched_graph
*graph
, int edge_index
)
4685 isl_map
*src_sched
, *dst_sched
, *map
;
4686 struct isl_sched_edge
*edge
= &graph
->edge
[edge_index
];
4689 src_sched
= final_row(edge
->src
);
4690 dst_sched
= final_row(edge
->dst
);
4691 map
= isl_map_copy(edge
->map
);
4692 map
= isl_map_apply_domain(map
, src_sched
);
4693 map
= isl_map_apply_range(map
, dst_sched
);
4694 map
= isl_map_order_gt(map
, isl_dim_in
, 0, isl_dim_out
, 0);
4695 empty
= isl_map_is_empty(map
);
4704 /* Does "graph" have any satisfied condition edges that
4705 * are adjacent to the conditional validity constraint with
4706 * domain "conditional_source" and range "conditional_sink"?
4708 * A satisfied condition is one that is not local.
4709 * If a condition was forced to be local already (i.e., marked as local)
4710 * then there is no need to check if it is in fact local.
4712 * Additionally, mark all adjacent condition edges found as local.
4714 static int has_adjacent_true_conditions(struct isl_sched_graph
*graph
,
4715 __isl_keep isl_union_set
*conditional_source
,
4716 __isl_keep isl_union_set
*conditional_sink
)
4721 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4722 int adjacent
, local
;
4723 isl_union_map
*condition
;
4725 if (!is_condition(&graph
->edge
[i
]))
4727 if (is_local(&graph
->edge
[i
]))
4730 condition
= graph
->edge
[i
].tagged_condition
;
4731 adjacent
= domain_intersects(condition
, conditional_sink
);
4732 if (adjacent
>= 0 && !adjacent
)
4733 adjacent
= range_intersects(condition
,
4734 conditional_source
);
4740 set_local(&graph
->edge
[i
]);
4742 local
= is_condition_false(&graph
->edge
[i
]);
4752 /* Are there any violated conditional validity dependences with
4753 * adjacent condition dependences that are not local with respect
4754 * to the current schedule?
4755 * That is, is the conditional validity constraint violated?
4757 * Additionally, mark all those adjacent condition dependences as local.
4758 * We also mark those adjacent condition dependences that were not marked
4759 * as local before, but just happened to be local already. This ensures
4760 * that they remain local if the schedule is recomputed.
4762 * We first collect domain and range of all violated conditional validity
4763 * dependences and then check if there are any adjacent non-local
4764 * condition dependences.
4766 static int has_violated_conditional_constraint(isl_ctx
*ctx
,
4767 struct isl_sched_graph
*graph
)
4771 isl_union_set
*source
, *sink
;
4773 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
4774 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
4775 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4776 isl_union_set
*uset
;
4777 isl_union_map
*umap
;
4780 if (!is_conditional_validity(&graph
->edge
[i
]))
4783 violated
= is_violated(graph
, i
);
4791 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
4792 uset
= isl_union_map_domain(umap
);
4793 source
= isl_union_set_union(source
, uset
);
4794 source
= isl_union_set_coalesce(source
);
4796 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
4797 uset
= isl_union_map_range(umap
);
4798 sink
= isl_union_set_union(sink
, uset
);
4799 sink
= isl_union_set_coalesce(sink
);
4803 any
= has_adjacent_true_conditions(graph
, source
, sink
);
4805 isl_union_set_free(source
);
4806 isl_union_set_free(sink
);
4809 isl_union_set_free(source
);
4810 isl_union_set_free(sink
);
4814 /* Examine the current band (the rows between graph->band_start and
4815 * graph->n_total_row), deciding whether to drop it or add it to "node"
4816 * and then continue with the computation of the next band, if any.
4817 * If "initialized" is set, then it may be assumed that compute_maxvar
4818 * has been called on the current band. Otherwise, call
4819 * compute_maxvar if and before carry_dependences gets called.
4821 * The caller keeps looking for a new row as long as
4822 * graph->n_row < graph->maxvar. If the latest attempt to find
4823 * such a row failed (i.e., we still have graph->n_row < graph->maxvar),
4825 * - split between SCCs and start over (assuming we found an interesting
4826 * pair of SCCs between which to split)
4827 * - continue with the next band (assuming the current band has at least
4829 * - try to carry as many dependences as possible and continue with the next
4831 * In each case, we first insert a band node in the schedule tree
4832 * if any rows have been computed.
4834 * If the caller managed to complete the schedule, we insert a band node
4835 * (if any schedule rows were computed) and we finish off by topologically
4836 * sorting the statements based on the remaining dependences.
4838 static __isl_give isl_schedule_node
*compute_schedule_finish_band(
4839 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
4847 if (graph
->n_row
< graph
->maxvar
) {
4849 int empty
= graph
->n_total_row
== graph
->band_start
;
4851 ctx
= isl_schedule_node_get_ctx(node
);
4852 if (!ctx
->opt
->schedule_maximize_band_depth
&& !empty
)
4853 return compute_next_band(node
, graph
, 1);
4854 if (graph
->src_scc
>= 0)
4855 return compute_split_schedule(node
, graph
);
4857 return compute_next_band(node
, graph
, 1);
4858 if (!initialized
&& compute_maxvar(graph
) < 0)
4859 return isl_schedule_node_free(node
);
4860 return carry_dependences(node
, graph
);
4863 insert
= graph
->n_total_row
> graph
->band_start
;
4865 node
= insert_current_band(node
, graph
, 1);
4866 node
= isl_schedule_node_child(node
, 0);
4868 node
= sort_statements(node
, graph
, initialized
);
4870 node
= isl_schedule_node_parent(node
);
4875 /* Construct a band of schedule rows for a connected dependence graph.
4876 * The caller is responsible for determining the strongly connected
4877 * components and calling compute_maxvar first.
4879 * We try to find a sequence of as many schedule rows as possible that result
4880 * in non-negative dependence distances (independent of the previous rows
4881 * in the sequence, i.e., such that the sequence is tilable), with as
4882 * many of the initial rows as possible satisfying the coincidence constraints.
4883 * The computation stops if we can't find any more rows or if we have found
4884 * all the rows we wanted to find.
4886 * If ctx->opt->schedule_outer_coincidence is set, then we force the
4887 * outermost dimension to satisfy the coincidence constraints. If this
4888 * turns out to be impossible, we fall back on the general scheme above
4889 * and try to carry as many dependences as possible.
4891 * If "graph" contains both condition and conditional validity dependences,
4892 * then we need to check that that the conditional schedule constraint
4893 * is satisfied, i.e., there are no violated conditional validity dependences
4894 * that are adjacent to any non-local condition dependences.
4895 * If there are, then we mark all those adjacent condition dependences
4896 * as local and recompute the current band. Those dependences that
4897 * are marked local will then be forced to be local.
4898 * The initial computation is performed with no dependences marked as local.
4899 * If we are lucky, then there will be no violated conditional validity
4900 * dependences adjacent to any non-local condition dependences.
4901 * Otherwise, we mark some additional condition dependences as local and
4902 * recompute. We continue this process until there are no violations left or
4903 * until we are no longer able to compute a schedule.
4904 * Since there are only a finite number of dependences,
4905 * there will only be a finite number of iterations.
4907 static isl_stat
compute_schedule_wcc_band(isl_ctx
*ctx
,
4908 struct isl_sched_graph
*graph
)
4910 int has_coincidence
;
4911 int use_coincidence
;
4912 int force_coincidence
= 0;
4913 int check_conditional
;
4915 if (sort_sccs(graph
) < 0)
4916 return isl_stat_error
;
4918 clear_local_edges(graph
);
4919 check_conditional
= need_condition_check(graph
);
4920 has_coincidence
= has_any_coincidence(graph
);
4922 if (ctx
->opt
->schedule_outer_coincidence
)
4923 force_coincidence
= 1;
4925 use_coincidence
= has_coincidence
;
4926 while (graph
->n_row
< graph
->maxvar
) {
4931 graph
->src_scc
= -1;
4932 graph
->dst_scc
= -1;
4934 if (setup_lp(ctx
, graph
, use_coincidence
) < 0)
4935 return isl_stat_error
;
4936 sol
= solve_lp(graph
);
4938 return isl_stat_error
;
4939 if (sol
->size
== 0) {
4940 int empty
= graph
->n_total_row
== graph
->band_start
;
4943 if (use_coincidence
&& (!force_coincidence
|| !empty
)) {
4944 use_coincidence
= 0;
4949 coincident
= !has_coincidence
|| use_coincidence
;
4950 if (update_schedule(graph
, sol
, 1, coincident
) < 0)
4951 return isl_stat_error
;
4953 if (!check_conditional
)
4955 violated
= has_violated_conditional_constraint(ctx
, graph
);
4957 return isl_stat_error
;
4960 if (reset_band(graph
) < 0)
4961 return isl_stat_error
;
4962 use_coincidence
= has_coincidence
;
4968 /* Compute a schedule for a connected dependence graph by considering
4969 * the graph as a whole and return the updated schedule node.
4971 * The actual schedule rows of the current band are computed by
4972 * compute_schedule_wcc_band. compute_schedule_finish_band takes
4973 * care of integrating the band into "node" and continuing
4976 static __isl_give isl_schedule_node
*compute_schedule_wcc_whole(
4977 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4984 ctx
= isl_schedule_node_get_ctx(node
);
4985 if (compute_schedule_wcc_band(ctx
, graph
) < 0)
4986 return isl_schedule_node_free(node
);
4988 return compute_schedule_finish_band(node
, graph
, 1);
4991 /* Clustering information used by compute_schedule_wcc_clustering.
4993 * "n" is the number of SCCs in the original dependence graph
4994 * "scc" is an array of "n" elements, each representing an SCC
4995 * of the original dependence graph. All entries in the same cluster
4996 * have the same number of schedule rows.
4997 * "scc_cluster" maps each SCC index to the cluster to which it belongs,
4998 * where each cluster is represented by the index of the first SCC
4999 * in the cluster. Initially, each SCC belongs to a cluster containing
5002 * "scc_in_merge" is used by merge_clusters_along_edge to keep
5003 * track of which SCCs need to be merged.
5005 * "cluster" contains the merged clusters of SCCs after the clustering
5008 * "scc_node" is a temporary data structure used inside copy_partial.
5009 * For each SCC, it keeps track of the number of nodes in the SCC
5010 * that have already been copied.
5012 struct isl_clustering
{
5014 struct isl_sched_graph
*scc
;
5015 struct isl_sched_graph
*cluster
;
5021 /* Initialize the clustering data structure "c" from "graph".
5023 * In particular, allocate memory, extract the SCCs from "graph"
5024 * into c->scc, initialize scc_cluster and construct
5025 * a band of schedule rows for each SCC.
5026 * Within each SCC, there is only one SCC by definition.
5027 * Each SCC initially belongs to a cluster containing only that SCC.
5029 static isl_stat
clustering_init(isl_ctx
*ctx
, struct isl_clustering
*c
,
5030 struct isl_sched_graph
*graph
)
5035 c
->scc
= isl_calloc_array(ctx
, struct isl_sched_graph
, c
->n
);
5036 c
->cluster
= isl_calloc_array(ctx
, struct isl_sched_graph
, c
->n
);
5037 c
->scc_cluster
= isl_calloc_array(ctx
, int, c
->n
);
5038 c
->scc_node
= isl_calloc_array(ctx
, int, c
->n
);
5039 c
->scc_in_merge
= isl_calloc_array(ctx
, int, c
->n
);
5040 if (!c
->scc
|| !c
->cluster
||
5041 !c
->scc_cluster
|| !c
->scc_node
|| !c
->scc_in_merge
)
5042 return isl_stat_error
;
5044 for (i
= 0; i
< c
->n
; ++i
) {
5045 if (extract_sub_graph(ctx
, graph
, &node_scc_exactly
,
5046 &edge_scc_exactly
, i
, &c
->scc
[i
]) < 0)
5047 return isl_stat_error
;
5049 if (compute_maxvar(&c
->scc
[i
]) < 0)
5050 return isl_stat_error
;
5051 if (compute_schedule_wcc_band(ctx
, &c
->scc
[i
]) < 0)
5052 return isl_stat_error
;
5053 c
->scc_cluster
[i
] = i
;
5059 /* Free all memory allocated for "c".
5061 static void clustering_free(isl_ctx
*ctx
, struct isl_clustering
*c
)
5066 for (i
= 0; i
< c
->n
; ++i
)
5067 graph_free(ctx
, &c
->scc
[i
]);
5070 for (i
= 0; i
< c
->n
; ++i
)
5071 graph_free(ctx
, &c
->cluster
[i
]);
5073 free(c
->scc_cluster
);
5075 free(c
->scc_in_merge
);
5078 /* Should we refrain from merging the cluster in "graph" with
5079 * any other cluster?
5080 * In particular, is its current schedule band empty and incomplete.
5082 static int bad_cluster(struct isl_sched_graph
*graph
)
5084 return graph
->n_row
< graph
->maxvar
&&
5085 graph
->n_total_row
== graph
->band_start
;
5088 /* Is "edge" a proximity edge with a non-empty dependence relation?
5090 static isl_bool
is_non_empty_proximity(struct isl_sched_edge
*edge
)
5092 if (!is_proximity(edge
))
5093 return isl_bool_false
;
5094 return isl_bool_not(isl_map_plain_is_empty(edge
->map
));
5097 /* Return the index of an edge in "graph" that can be used to merge
5098 * two clusters in "c".
5099 * Return graph->n_edge if no such edge can be found.
5100 * Return -1 on error.
5102 * In particular, return a proximity edge between two clusters
5103 * that is not marked "no_merge" and such that neither of the
5104 * two clusters has an incomplete, empty band.
5106 * If there are multiple such edges, then try and find the most
5107 * appropriate edge to use for merging. In particular, pick the edge
5108 * with the greatest weight. If there are multiple of those,
5109 * then pick one with the shortest distance between
5110 * the two cluster representatives.
5112 static int find_proximity(struct isl_sched_graph
*graph
,
5113 struct isl_clustering
*c
)
5115 int i
, best
= graph
->n_edge
, best_dist
, best_weight
;
5117 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5118 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
5122 prox
= is_non_empty_proximity(edge
);
5129 if (bad_cluster(&c
->scc
[edge
->src
->scc
]) ||
5130 bad_cluster(&c
->scc
[edge
->dst
->scc
]))
5132 dist
= c
->scc_cluster
[edge
->dst
->scc
] -
5133 c
->scc_cluster
[edge
->src
->scc
];
5136 weight
= edge
->weight
;
5137 if (best
< graph
->n_edge
) {
5138 if (best_weight
> weight
)
5140 if (best_weight
== weight
&& best_dist
<= dist
)
5145 best_weight
= weight
;
5151 /* Internal data structure used in mark_merge_sccs.
5153 * "graph" is the dependence graph in which a strongly connected
5154 * component is constructed.
5155 * "scc_cluster" maps each SCC index to the cluster to which it belongs.
5156 * "src" and "dst" are the indices of the nodes that are being merged.
5158 struct isl_mark_merge_sccs_data
{
5159 struct isl_sched_graph
*graph
;
5165 /* Check whether the cluster containing node "i" depends on the cluster
5166 * containing node "j". If "i" and "j" belong to the same cluster,
5167 * then they are taken to depend on each other to ensure that
5168 * the resulting strongly connected component consists of complete
5169 * clusters. Furthermore, if "i" and "j" are the two nodes that
5170 * are being merged, then they are taken to depend on each other as well.
5171 * Otherwise, check if there is a (conditional) validity dependence
5172 * from node[j] to node[i], forcing node[i] to follow node[j].
5174 static isl_bool
cluster_follows(int i
, int j
, void *user
)
5176 struct isl_mark_merge_sccs_data
*data
= user
;
5177 struct isl_sched_graph
*graph
= data
->graph
;
5178 int *scc_cluster
= data
->scc_cluster
;
5180 if (data
->src
== i
&& data
->dst
== j
)
5181 return isl_bool_true
;
5182 if (data
->src
== j
&& data
->dst
== i
)
5183 return isl_bool_true
;
5184 if (scc_cluster
[graph
->node
[i
].scc
] == scc_cluster
[graph
->node
[j
].scc
])
5185 return isl_bool_true
;
5187 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
5190 /* Mark all SCCs that belong to either of the two clusters in "c"
5191 * connected by the edge in "graph" with index "edge", or to any
5192 * of the intermediate clusters.
5193 * The marking is recorded in c->scc_in_merge.
5195 * The given edge has been selected for merging two clusters,
5196 * meaning that there is at least a proximity edge between the two nodes.
5197 * However, there may also be (indirect) validity dependences
5198 * between the two nodes. When merging the two clusters, all clusters
5199 * containing one or more of the intermediate nodes along the
5200 * indirect validity dependences need to be merged in as well.
5202 * First collect all such nodes by computing the strongly connected
5203 * component (SCC) containing the two nodes connected by the edge, where
5204 * the two nodes are considered to depend on each other to make
5205 * sure they end up in the same SCC. Similarly, each node is considered
5206 * to depend on every other node in the same cluster to ensure
5207 * that the SCC consists of complete clusters.
5209 * Then the original SCCs that contain any of these nodes are marked
5210 * in c->scc_in_merge.
5212 static isl_stat
mark_merge_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5213 int edge
, struct isl_clustering
*c
)
5215 struct isl_mark_merge_sccs_data data
;
5216 struct isl_tarjan_graph
*g
;
5219 for (i
= 0; i
< c
->n
; ++i
)
5220 c
->scc_in_merge
[i
] = 0;
5223 data
.scc_cluster
= c
->scc_cluster
;
5224 data
.src
= graph
->edge
[edge
].src
- graph
->node
;
5225 data
.dst
= graph
->edge
[edge
].dst
- graph
->node
;
5227 g
= isl_tarjan_graph_component(ctx
, graph
->n
, data
.dst
,
5228 &cluster_follows
, &data
);
5234 isl_die(ctx
, isl_error_internal
,
5235 "expecting at least two nodes in component",
5237 if (g
->order
[--i
] != -1)
5238 isl_die(ctx
, isl_error_internal
,
5239 "expecting end of component marker", goto error
);
5241 for (--i
; i
>= 0 && g
->order
[i
] != -1; --i
) {
5242 int scc
= graph
->node
[g
->order
[i
]].scc
;
5243 c
->scc_in_merge
[scc
] = 1;
5246 isl_tarjan_graph_free(g
);
5249 isl_tarjan_graph_free(g
);
5250 return isl_stat_error
;
5253 /* Construct the identifier "cluster_i".
5255 static __isl_give isl_id
*cluster_id(isl_ctx
*ctx
, int i
)
5259 snprintf(name
, sizeof(name
), "cluster_%d", i
);
5260 return isl_id_alloc(ctx
, name
, NULL
);
5263 /* Construct the space of the cluster with index "i" containing
5264 * the strongly connected component "scc".
5266 * In particular, construct a space called cluster_i with dimension equal
5267 * to the number of schedule rows in the current band of "scc".
5269 static __isl_give isl_space
*cluster_space(struct isl_sched_graph
*scc
, int i
)
5275 nvar
= scc
->n_total_row
- scc
->band_start
;
5276 space
= isl_space_copy(scc
->node
[0].space
);
5277 space
= isl_space_params(space
);
5278 space
= isl_space_set_from_params(space
);
5279 space
= isl_space_add_dims(space
, isl_dim_set
, nvar
);
5280 id
= cluster_id(isl_space_get_ctx(space
), i
);
5281 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
5286 /* Collect the domain of the graph for merging clusters.
5288 * In particular, for each cluster with first SCC "i", construct
5289 * a set in the space called cluster_i with dimension equal
5290 * to the number of schedule rows in the current band of the cluster.
5292 static __isl_give isl_union_set
*collect_domain(isl_ctx
*ctx
,
5293 struct isl_sched_graph
*graph
, struct isl_clustering
*c
)
5297 isl_union_set
*domain
;
5299 space
= isl_space_params_alloc(ctx
, 0);
5300 domain
= isl_union_set_empty(space
);
5302 for (i
= 0; i
< graph
->scc
; ++i
) {
5305 if (!c
->scc_in_merge
[i
])
5307 if (c
->scc_cluster
[i
] != i
)
5309 space
= cluster_space(&c
->scc
[i
], i
);
5310 domain
= isl_union_set_add_set(domain
, isl_set_universe(space
));
5316 /* Construct a map from the original instances to the corresponding
5317 * cluster instance in the current bands of the clusters in "c".
5319 static __isl_give isl_union_map
*collect_cluster_map(isl_ctx
*ctx
,
5320 struct isl_sched_graph
*graph
, struct isl_clustering
*c
)
5324 isl_union_map
*cluster_map
;
5326 space
= isl_space_params_alloc(ctx
, 0);
5327 cluster_map
= isl_union_map_empty(space
);
5328 for (i
= 0; i
< graph
->scc
; ++i
) {
5332 if (!c
->scc_in_merge
[i
])
5335 id
= cluster_id(ctx
, c
->scc_cluster
[i
]);
5336 start
= c
->scc
[i
].band_start
;
5337 n
= c
->scc
[i
].n_total_row
- start
;
5338 for (j
= 0; j
< c
->scc
[i
].n
; ++j
) {
5341 struct isl_sched_node
*node
= &c
->scc
[i
].node
[j
];
5343 ma
= node_extract_partial_schedule_multi_aff(node
,
5345 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
,
5347 map
= isl_map_from_multi_aff(ma
);
5348 cluster_map
= isl_union_map_add_map(cluster_map
, map
);
5356 /* Add "umap" to the schedule constraints "sc" of all types of "edge"
5357 * that are not isl_edge_condition or isl_edge_conditional_validity.
5359 static __isl_give isl_schedule_constraints
*add_non_conditional_constraints(
5360 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*umap
,
5361 __isl_take isl_schedule_constraints
*sc
)
5363 enum isl_edge_type t
;
5368 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
5369 if (t
== isl_edge_condition
||
5370 t
== isl_edge_conditional_validity
)
5372 if (!is_type(edge
, t
))
5374 sc
= isl_schedule_constraints_add(sc
, t
,
5375 isl_union_map_copy(umap
));
5381 /* Add schedule constraints of types isl_edge_condition and
5382 * isl_edge_conditional_validity to "sc" by applying "umap" to
5383 * the domains of the wrapped relations in domain and range
5384 * of the corresponding tagged constraints of "edge".
5386 static __isl_give isl_schedule_constraints
*add_conditional_constraints(
5387 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*umap
,
5388 __isl_take isl_schedule_constraints
*sc
)
5390 enum isl_edge_type t
;
5391 isl_union_map
*tagged
;
5393 for (t
= isl_edge_condition
; t
<= isl_edge_conditional_validity
; ++t
) {
5394 if (!is_type(edge
, t
))
5396 if (t
== isl_edge_condition
)
5397 tagged
= isl_union_map_copy(edge
->tagged_condition
);
5399 tagged
= isl_union_map_copy(edge
->tagged_validity
);
5400 tagged
= isl_union_map_zip(tagged
);
5401 tagged
= isl_union_map_apply_domain(tagged
,
5402 isl_union_map_copy(umap
));
5403 tagged
= isl_union_map_zip(tagged
);
5404 sc
= isl_schedule_constraints_add(sc
, t
, tagged
);
5412 /* Given a mapping "cluster_map" from the original instances to
5413 * the cluster instances, add schedule constraints on the clusters
5414 * to "sc" corresponding to the original constraints represented by "edge".
5416 * For non-tagged dependence constraints, the cluster constraints
5417 * are obtained by applying "cluster_map" to the edge->map.
5419 * For tagged dependence constraints, "cluster_map" needs to be applied
5420 * to the domains of the wrapped relations in domain and range
5421 * of the tagged dependence constraints. Pick out the mappings
5422 * from these domains from "cluster_map" and construct their product.
5423 * This mapping can then be applied to the pair of domains.
5425 static __isl_give isl_schedule_constraints
*collect_edge_constraints(
5426 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*cluster_map
,
5427 __isl_take isl_schedule_constraints
*sc
)
5429 isl_union_map
*umap
;
5431 isl_union_set
*uset
;
5432 isl_union_map
*umap1
, *umap2
;
5437 umap
= isl_union_map_from_map(isl_map_copy(edge
->map
));
5438 umap
= isl_union_map_apply_domain(umap
,
5439 isl_union_map_copy(cluster_map
));
5440 umap
= isl_union_map_apply_range(umap
,
5441 isl_union_map_copy(cluster_map
));
5442 sc
= add_non_conditional_constraints(edge
, umap
, sc
);
5443 isl_union_map_free(umap
);
5445 if (!sc
|| (!is_condition(edge
) && !is_conditional_validity(edge
)))
5448 space
= isl_space_domain(isl_map_get_space(edge
->map
));
5449 uset
= isl_union_set_from_set(isl_set_universe(space
));
5450 umap1
= isl_union_map_copy(cluster_map
);
5451 umap1
= isl_union_map_intersect_domain(umap1
, uset
);
5452 space
= isl_space_range(isl_map_get_space(edge
->map
));
5453 uset
= isl_union_set_from_set(isl_set_universe(space
));
5454 umap2
= isl_union_map_copy(cluster_map
);
5455 umap2
= isl_union_map_intersect_domain(umap2
, uset
);
5456 umap
= isl_union_map_product(umap1
, umap2
);
5458 sc
= add_conditional_constraints(edge
, umap
, sc
);
5460 isl_union_map_free(umap
);
5464 /* Given a mapping "cluster_map" from the original instances to
5465 * the cluster instances, add schedule constraints on the clusters
5466 * to "sc" corresponding to all edges in "graph" between nodes that
5467 * belong to SCCs that are marked for merging in "scc_in_merge".
5469 static __isl_give isl_schedule_constraints
*collect_constraints(
5470 struct isl_sched_graph
*graph
, int *scc_in_merge
,
5471 __isl_keep isl_union_map
*cluster_map
,
5472 __isl_take isl_schedule_constraints
*sc
)
5476 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5477 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
5479 if (!scc_in_merge
[edge
->src
->scc
])
5481 if (!scc_in_merge
[edge
->dst
->scc
])
5483 sc
= collect_edge_constraints(edge
, cluster_map
, sc
);
5489 /* Construct a dependence graph for scheduling clusters with respect
5490 * to each other and store the result in "merge_graph".
5491 * In particular, the nodes of the graph correspond to the schedule
5492 * dimensions of the current bands of those clusters that have been
5493 * marked for merging in "c".
5495 * First construct an isl_schedule_constraints object for this domain
5496 * by transforming the edges in "graph" to the domain.
5497 * Then initialize a dependence graph for scheduling from these
5500 static isl_stat
init_merge_graph(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5501 struct isl_clustering
*c
, struct isl_sched_graph
*merge_graph
)
5503 isl_union_set
*domain
;
5504 isl_union_map
*cluster_map
;
5505 isl_schedule_constraints
*sc
;
5508 domain
= collect_domain(ctx
, graph
, c
);
5509 sc
= isl_schedule_constraints_on_domain(domain
);
5511 return isl_stat_error
;
5512 cluster_map
= collect_cluster_map(ctx
, graph
, c
);
5513 sc
= collect_constraints(graph
, c
->scc_in_merge
, cluster_map
, sc
);
5514 isl_union_map_free(cluster_map
);
5516 r
= graph_init(merge_graph
, sc
);
5518 isl_schedule_constraints_free(sc
);
5523 /* Compute the maximal number of remaining schedule rows that still need
5524 * to be computed for the nodes that belong to clusters with the maximal
5525 * dimension for the current band (i.e., the band that is to be merged).
5526 * Only clusters that are about to be merged are considered.
5527 * "maxvar" is the maximal dimension for the current band.
5528 * "c" contains information about the clusters.
5530 * Return the maximal number of remaining schedule rows or -1 on error.
5532 static int compute_maxvar_max_slack(int maxvar
, struct isl_clustering
*c
)
5538 for (i
= 0; i
< c
->n
; ++i
) {
5540 struct isl_sched_graph
*scc
;
5542 if (!c
->scc_in_merge
[i
])
5545 nvar
= scc
->n_total_row
- scc
->band_start
;
5548 for (j
= 0; j
< scc
->n
; ++j
) {
5549 struct isl_sched_node
*node
= &scc
->node
[j
];
5552 if (node_update_cmap(node
) < 0)
5554 slack
= node
->nvar
- node
->rank
;
5555 if (slack
> max_slack
)
5563 /* If there are any clusters where the dimension of the current band
5564 * (i.e., the band that is to be merged) is smaller than "maxvar" and
5565 * if there are any nodes in such a cluster where the number
5566 * of remaining schedule rows that still need to be computed
5567 * is greater than "max_slack", then return the smallest current band
5568 * dimension of all these clusters. Otherwise return the original value
5569 * of "maxvar". Return -1 in case of any error.
5570 * Only clusters that are about to be merged are considered.
5571 * "c" contains information about the clusters.
5573 static int limit_maxvar_to_slack(int maxvar
, int max_slack
,
5574 struct isl_clustering
*c
)
5578 for (i
= 0; i
< c
->n
; ++i
) {
5580 struct isl_sched_graph
*scc
;
5582 if (!c
->scc_in_merge
[i
])
5585 nvar
= scc
->n_total_row
- scc
->band_start
;
5588 for (j
= 0; j
< scc
->n
; ++j
) {
5589 struct isl_sched_node
*node
= &scc
->node
[j
];
5592 if (node_update_cmap(node
) < 0)
5594 slack
= node
->nvar
- node
->rank
;
5595 if (slack
> max_slack
) {
5605 /* Adjust merge_graph->maxvar based on the number of remaining schedule rows
5606 * that still need to be computed. In particular, if there is a node
5607 * in a cluster where the dimension of the current band is smaller
5608 * than merge_graph->maxvar, but the number of remaining schedule rows
5609 * is greater than that of any node in a cluster with the maximal
5610 * dimension for the current band (i.e., merge_graph->maxvar),
5611 * then adjust merge_graph->maxvar to the (smallest) current band dimension
5612 * of those clusters. Without this adjustment, the total number of
5613 * schedule dimensions would be increased, resulting in a skewed view
5614 * of the number of coincident dimensions.
5615 * "c" contains information about the clusters.
5617 * If the maximize_band_depth option is set and merge_graph->maxvar is reduced,
5618 * then there is no point in attempting any merge since it will be rejected
5619 * anyway. Set merge_graph->maxvar to zero in such cases.
5621 static isl_stat
adjust_maxvar_to_slack(isl_ctx
*ctx
,
5622 struct isl_sched_graph
*merge_graph
, struct isl_clustering
*c
)
5624 int max_slack
, maxvar
;
5626 max_slack
= compute_maxvar_max_slack(merge_graph
->maxvar
, c
);
5628 return isl_stat_error
;
5629 maxvar
= limit_maxvar_to_slack(merge_graph
->maxvar
, max_slack
, c
);
5631 return isl_stat_error
;
5633 if (maxvar
< merge_graph
->maxvar
) {
5634 if (isl_options_get_schedule_maximize_band_depth(ctx
))
5635 merge_graph
->maxvar
= 0;
5637 merge_graph
->maxvar
= maxvar
;
5643 /* Return the number of coincident dimensions in the current band of "graph",
5644 * where the nodes of "graph" are assumed to be scheduled by a single band.
5646 static int get_n_coincident(struct isl_sched_graph
*graph
)
5650 for (i
= graph
->band_start
; i
< graph
->n_total_row
; ++i
)
5651 if (!graph
->node
[0].coincident
[i
])
5654 return i
- graph
->band_start
;
5657 /* Should the clusters be merged based on the cluster schedule
5658 * in the current (and only) band of "merge_graph", given that
5659 * coincidence should be maximized?
5661 * If the number of coincident schedule dimensions in the merged band
5662 * would be less than the maximal number of coincident schedule dimensions
5663 * in any of the merged clusters, then the clusters should not be merged.
5665 static isl_bool
ok_to_merge_coincident(struct isl_clustering
*c
,
5666 struct isl_sched_graph
*merge_graph
)
5673 for (i
= 0; i
< c
->n
; ++i
) {
5674 if (!c
->scc_in_merge
[i
])
5676 n_coincident
= get_n_coincident(&c
->scc
[i
]);
5677 if (n_coincident
> max_coincident
)
5678 max_coincident
= n_coincident
;
5681 n_coincident
= get_n_coincident(merge_graph
);
5683 return n_coincident
>= max_coincident
;
5686 /* Return the transformation on "node" expressed by the current (and only)
5687 * band of "merge_graph" applied to the clusters in "c".
5689 * First find the representation of "node" in its SCC in "c" and
5690 * extract the transformation expressed by the current band.
5691 * Then extract the transformation applied by "merge_graph"
5692 * to the cluster to which this SCC belongs.
5693 * Combine the two to obtain the complete transformation on the node.
5695 * Note that the range of the first transformation is an anonymous space,
5696 * while the domain of the second is named "cluster_X". The range
5697 * of the former therefore needs to be adjusted before the two
5700 static __isl_give isl_map
*extract_node_transformation(isl_ctx
*ctx
,
5701 struct isl_sched_node
*node
, struct isl_clustering
*c
,
5702 struct isl_sched_graph
*merge_graph
)
5704 struct isl_sched_node
*scc_node
, *cluster_node
;
5708 isl_multi_aff
*ma
, *ma2
;
5710 scc_node
= graph_find_node(ctx
, &c
->scc
[node
->scc
], node
->space
);
5711 start
= c
->scc
[node
->scc
].band_start
;
5712 n
= c
->scc
[node
->scc
].n_total_row
- start
;
5713 ma
= node_extract_partial_schedule_multi_aff(scc_node
, start
, n
);
5714 space
= cluster_space(&c
->scc
[node
->scc
], c
->scc_cluster
[node
->scc
]);
5715 cluster_node
= graph_find_node(ctx
, merge_graph
, space
);
5716 if (space
&& !cluster_node
)
5717 isl_die(ctx
, isl_error_internal
, "unable to find cluster",
5718 space
= isl_space_free(space
));
5719 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
5720 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
, id
);
5721 isl_space_free(space
);
5722 n
= merge_graph
->n_total_row
;
5723 ma2
= node_extract_partial_schedule_multi_aff(cluster_node
, 0, n
);
5724 ma
= isl_multi_aff_pullback_multi_aff(ma2
, ma
);
5726 return isl_map_from_multi_aff(ma
);
5729 /* Give a set of distances "set", are they bounded by a small constant
5730 * in direction "pos"?
5731 * In practice, check if they are bounded by 2 by checking that there
5732 * are no elements with a value greater than or equal to 3 or
5733 * smaller than or equal to -3.
5735 static isl_bool
distance_is_bounded(__isl_keep isl_set
*set
, int pos
)
5741 return isl_bool_error
;
5743 test
= isl_set_copy(set
);
5744 test
= isl_set_lower_bound_si(test
, isl_dim_set
, pos
, 3);
5745 bounded
= isl_set_is_empty(test
);
5748 if (bounded
< 0 || !bounded
)
5751 test
= isl_set_copy(set
);
5752 test
= isl_set_upper_bound_si(test
, isl_dim_set
, pos
, -3);
5753 bounded
= isl_set_is_empty(test
);
5759 /* Does the set "set" have a fixed (but possible parametric) value
5760 * at dimension "pos"?
5762 static isl_bool
has_single_value(__isl_keep isl_set
*set
, int pos
)
5768 return isl_bool_error
;
5769 set
= isl_set_copy(set
);
5770 n
= isl_set_dim(set
, isl_dim_set
);
5771 set
= isl_set_project_out(set
, isl_dim_set
, pos
+ 1, n
- (pos
+ 1));
5772 set
= isl_set_project_out(set
, isl_dim_set
, 0, pos
);
5773 single
= isl_set_is_singleton(set
);
5779 /* Does "map" have a fixed (but possible parametric) value
5780 * at dimension "pos" of either its domain or its range?
5782 static isl_bool
has_singular_src_or_dst(__isl_keep isl_map
*map
, int pos
)
5787 set
= isl_map_domain(isl_map_copy(map
));
5788 single
= has_single_value(set
, pos
);
5791 if (single
< 0 || single
)
5794 set
= isl_map_range(isl_map_copy(map
));
5795 single
= has_single_value(set
, pos
);
5801 /* Does the edge "edge" from "graph" have bounded dependence distances
5802 * in the merged graph "merge_graph" of a selection of clusters in "c"?
5804 * Extract the complete transformations of the source and destination
5805 * nodes of the edge, apply them to the edge constraints and
5806 * compute the differences. Finally, check if these differences are bounded
5807 * in each direction.
5809 * If the dimension of the band is greater than the number of
5810 * dimensions that can be expected to be optimized by the edge
5811 * (based on its weight), then also allow the differences to be unbounded
5812 * in the remaining dimensions, but only if either the source or
5813 * the destination has a fixed value in that direction.
5814 * This allows a statement that produces values that are used by
5815 * several instances of another statement to be merged with that
5817 * However, merging such clusters will introduce an inherently
5818 * large proximity distance inside the merged cluster, meaning
5819 * that proximity distances will no longer be optimized in
5820 * subsequent merges. These merges are therefore only allowed
5821 * after all other possible merges have been tried.
5822 * The first time such a merge is encountered, the weight of the edge
5823 * is replaced by a negative weight. The second time (i.e., after
5824 * all merges over edges with a non-negative weight have been tried),
5825 * the merge is allowed.
5827 static isl_bool
has_bounded_distances(isl_ctx
*ctx
, struct isl_sched_edge
*edge
,
5828 struct isl_sched_graph
*graph
, struct isl_clustering
*c
,
5829 struct isl_sched_graph
*merge_graph
)
5836 map
= isl_map_copy(edge
->map
);
5837 t
= extract_node_transformation(ctx
, edge
->src
, c
, merge_graph
);
5838 map
= isl_map_apply_domain(map
, t
);
5839 t
= extract_node_transformation(ctx
, edge
->dst
, c
, merge_graph
);
5840 map
= isl_map_apply_range(map
, t
);
5841 dist
= isl_map_deltas(isl_map_copy(map
));
5843 bounded
= isl_bool_true
;
5844 n
= isl_set_dim(dist
, isl_dim_set
);
5845 n_slack
= n
- edge
->weight
;
5846 if (edge
->weight
< 0)
5847 n_slack
-= graph
->max_weight
+ 1;
5848 for (i
= 0; i
< n
; ++i
) {
5849 isl_bool bounded_i
, singular_i
;
5851 bounded_i
= distance_is_bounded(dist
, i
);
5856 if (edge
->weight
>= 0)
5857 bounded
= isl_bool_false
;
5861 singular_i
= has_singular_src_or_dst(map
, i
);
5866 bounded
= isl_bool_false
;
5869 if (!bounded
&& i
>= n
&& edge
->weight
>= 0)
5870 edge
->weight
-= graph
->max_weight
+ 1;
5878 return isl_bool_error
;
5881 /* Should the clusters be merged based on the cluster schedule
5882 * in the current (and only) band of "merge_graph"?
5883 * "graph" is the original dependence graph, while "c" records
5884 * which SCCs are involved in the latest merge.
5886 * In particular, is there at least one proximity constraint
5887 * that is optimized by the merge?
5889 * A proximity constraint is considered to be optimized
5890 * if the dependence distances are small.
5892 static isl_bool
ok_to_merge_proximity(isl_ctx
*ctx
,
5893 struct isl_sched_graph
*graph
, struct isl_clustering
*c
,
5894 struct isl_sched_graph
*merge_graph
)
5898 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5899 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
5902 if (!is_proximity(edge
))
5904 if (!c
->scc_in_merge
[edge
->src
->scc
])
5906 if (!c
->scc_in_merge
[edge
->dst
->scc
])
5908 if (c
->scc_cluster
[edge
->dst
->scc
] ==
5909 c
->scc_cluster
[edge
->src
->scc
])
5911 bounded
= has_bounded_distances(ctx
, edge
, graph
, c
,
5913 if (bounded
< 0 || bounded
)
5917 return isl_bool_false
;
5920 /* Should the clusters be merged based on the cluster schedule
5921 * in the current (and only) band of "merge_graph"?
5922 * "graph" is the original dependence graph, while "c" records
5923 * which SCCs are involved in the latest merge.
5925 * If the current band is empty, then the clusters should not be merged.
5927 * If the band depth should be maximized and the merge schedule
5928 * is incomplete (meaning that the dimension of some of the schedule
5929 * bands in the original schedule will be reduced), then the clusters
5930 * should not be merged.
5932 * If the schedule_maximize_coincidence option is set, then check that
5933 * the number of coincident schedule dimensions is not reduced.
5935 * Finally, only allow the merge if at least one proximity
5936 * constraint is optimized.
5938 static isl_bool
ok_to_merge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5939 struct isl_clustering
*c
, struct isl_sched_graph
*merge_graph
)
5941 if (merge_graph
->n_total_row
== merge_graph
->band_start
)
5942 return isl_bool_false
;
5944 if (isl_options_get_schedule_maximize_band_depth(ctx
) &&
5945 merge_graph
->n_total_row
< merge_graph
->maxvar
)
5946 return isl_bool_false
;
5948 if (isl_options_get_schedule_maximize_coincidence(ctx
)) {
5951 ok
= ok_to_merge_coincident(c
, merge_graph
);
5956 return ok_to_merge_proximity(ctx
, graph
, c
, merge_graph
);
5959 /* Apply the schedule in "t_node" to the "n" rows starting at "first"
5960 * of the schedule in "node" and return the result.
5962 * That is, essentially compute
5964 * T * N(first:first+n-1)
5966 * taking into account the constant term and the parameter coefficients
5969 static __isl_give isl_mat
*node_transformation(isl_ctx
*ctx
,
5970 struct isl_sched_node
*t_node
, struct isl_sched_node
*node
,
5975 int n_row
, n_col
, n_param
, n_var
;
5977 n_param
= node
->nparam
;
5979 n_row
= isl_mat_rows(t_node
->sched
);
5980 n_col
= isl_mat_cols(node
->sched
);
5981 t
= isl_mat_alloc(ctx
, n_row
, n_col
);
5984 for (i
= 0; i
< n_row
; ++i
) {
5985 isl_seq_cpy(t
->row
[i
], t_node
->sched
->row
[i
], 1 + n_param
);
5986 isl_seq_clr(t
->row
[i
] + 1 + n_param
, n_var
);
5987 for (j
= 0; j
< n
; ++j
)
5988 isl_seq_addmul(t
->row
[i
],
5989 t_node
->sched
->row
[i
][1 + n_param
+ j
],
5990 node
->sched
->row
[first
+ j
],
5991 1 + n_param
+ n_var
);
5996 /* Apply the cluster schedule in "t_node" to the current band
5997 * schedule of the nodes in "graph".
5999 * In particular, replace the rows starting at band_start
6000 * by the result of applying the cluster schedule in "t_node"
6001 * to the original rows.
6003 * The coincidence of the schedule is determined by the coincidence
6004 * of the cluster schedule.
6006 static isl_stat
transform(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6007 struct isl_sched_node
*t_node
)
6013 start
= graph
->band_start
;
6014 n
= graph
->n_total_row
- start
;
6016 n_new
= isl_mat_rows(t_node
->sched
);
6017 for (i
= 0; i
< graph
->n
; ++i
) {
6018 struct isl_sched_node
*node
= &graph
->node
[i
];
6021 t
= node_transformation(ctx
, t_node
, node
, start
, n
);
6022 node
->sched
= isl_mat_drop_rows(node
->sched
, start
, n
);
6023 node
->sched
= isl_mat_concat(node
->sched
, t
);
6024 node
->sched_map
= isl_map_free(node
->sched_map
);
6026 return isl_stat_error
;
6027 for (j
= 0; j
< n_new
; ++j
)
6028 node
->coincident
[start
+ j
] = t_node
->coincident
[j
];
6030 graph
->n_total_row
-= n
;
6032 graph
->n_total_row
+= n_new
;
6033 graph
->n_row
+= n_new
;
6038 /* Merge the clusters marked for merging in "c" into a single
6039 * cluster using the cluster schedule in the current band of "merge_graph".
6040 * The representative SCC for the new cluster is the SCC with
6041 * the smallest index.
6043 * The current band schedule of each SCC in the new cluster is obtained
6044 * by applying the schedule of the corresponding original cluster
6045 * to the original band schedule.
6046 * All SCCs in the new cluster have the same number of schedule rows.
6048 static isl_stat
merge(isl_ctx
*ctx
, struct isl_clustering
*c
,
6049 struct isl_sched_graph
*merge_graph
)
6055 for (i
= 0; i
< c
->n
; ++i
) {
6056 struct isl_sched_node
*node
;
6058 if (!c
->scc_in_merge
[i
])
6062 space
= cluster_space(&c
->scc
[i
], c
->scc_cluster
[i
]);
6064 return isl_stat_error
;
6065 node
= graph_find_node(ctx
, merge_graph
, space
);
6066 isl_space_free(space
);
6068 isl_die(ctx
, isl_error_internal
,
6069 "unable to find cluster",
6070 return isl_stat_error
);
6071 if (transform(ctx
, &c
->scc
[i
], node
) < 0)
6072 return isl_stat_error
;
6073 c
->scc_cluster
[i
] = cluster
;
6079 /* Try and merge the clusters of SCCs marked in c->scc_in_merge
6080 * by scheduling the current cluster bands with respect to each other.
6082 * Construct a dependence graph with a space for each cluster and
6083 * with the coordinates of each space corresponding to the schedule
6084 * dimensions of the current band of that cluster.
6085 * Construct a cluster schedule in this cluster dependence graph and
6086 * apply it to the current cluster bands if it is applicable
6087 * according to ok_to_merge.
6089 * If the number of remaining schedule dimensions in a cluster
6090 * with a non-maximal current schedule dimension is greater than
6091 * the number of remaining schedule dimensions in clusters
6092 * with a maximal current schedule dimension, then restrict
6093 * the number of rows to be computed in the cluster schedule
6094 * to the minimal such non-maximal current schedule dimension.
6095 * Do this by adjusting merge_graph.maxvar.
6097 * Return isl_bool_true if the clusters have effectively been merged
6098 * into a single cluster.
6100 * Note that since the standard scheduling algorithm minimizes the maximal
6101 * distance over proximity constraints, the proximity constraints between
6102 * the merged clusters may not be optimized any further than what is
6103 * sufficient to bring the distances within the limits of the internal
6104 * proximity constraints inside the individual clusters.
6105 * It may therefore make sense to perform an additional translation step
6106 * to bring the clusters closer to each other, while maintaining
6107 * the linear part of the merging schedule found using the standard
6108 * scheduling algorithm.
6110 static isl_bool
try_merge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6111 struct isl_clustering
*c
)
6113 struct isl_sched_graph merge_graph
= { 0 };
6116 if (init_merge_graph(ctx
, graph
, c
, &merge_graph
) < 0)
6119 if (compute_maxvar(&merge_graph
) < 0)
6121 if (adjust_maxvar_to_slack(ctx
, &merge_graph
,c
) < 0)
6123 if (compute_schedule_wcc_band(ctx
, &merge_graph
) < 0)
6125 merged
= ok_to_merge(ctx
, graph
, c
, &merge_graph
);
6126 if (merged
&& merge(ctx
, c
, &merge_graph
) < 0)
6129 graph_free(ctx
, &merge_graph
);
6132 graph_free(ctx
, &merge_graph
);
6133 return isl_bool_error
;
6136 /* Is there any edge marked "no_merge" between two SCCs that are
6137 * about to be merged (i.e., that are set in "scc_in_merge")?
6138 * "merge_edge" is the proximity edge along which the clusters of SCCs
6139 * are going to be merged.
6141 * If there is any edge between two SCCs with a negative weight,
6142 * while the weight of "merge_edge" is non-negative, then this
6143 * means that the edge was postponed. "merge_edge" should then
6144 * also be postponed since merging along the edge with negative weight should
6145 * be postponed until all edges with non-negative weight have been tried.
6146 * Replace the weight of "merge_edge" by a negative weight as well and
6147 * tell the caller not to attempt a merge.
6149 static int any_no_merge(struct isl_sched_graph
*graph
, int *scc_in_merge
,
6150 struct isl_sched_edge
*merge_edge
)
6154 for (i
= 0; i
< graph
->n_edge
; ++i
) {
6155 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
6157 if (!scc_in_merge
[edge
->src
->scc
])
6159 if (!scc_in_merge
[edge
->dst
->scc
])
6163 if (merge_edge
->weight
>= 0 && edge
->weight
< 0) {
6164 merge_edge
->weight
-= graph
->max_weight
+ 1;
6172 /* Merge the two clusters in "c" connected by the edge in "graph"
6173 * with index "edge" into a single cluster.
6174 * If it turns out to be impossible to merge these two clusters,
6175 * then mark the edge as "no_merge" such that it will not be
6178 * First mark all SCCs that need to be merged. This includes the SCCs
6179 * in the two clusters, but it may also include the SCCs
6180 * of intermediate clusters.
6181 * If there is already a no_merge edge between any pair of such SCCs,
6182 * then simply mark the current edge as no_merge as well.
6183 * Likewise, if any of those edges was postponed by has_bounded_distances,
6184 * then postpone the current edge as well.
6185 * Otherwise, try and merge the clusters and mark "edge" as "no_merge"
6186 * if the clusters did not end up getting merged, unless the non-merge
6187 * is due to the fact that the edge was postponed. This postponement
6188 * can be recognized by a change in weight (from non-negative to negative).
6190 static isl_stat
merge_clusters_along_edge(isl_ctx
*ctx
,
6191 struct isl_sched_graph
*graph
, int edge
, struct isl_clustering
*c
)
6194 int edge_weight
= graph
->edge
[edge
].weight
;
6196 if (mark_merge_sccs(ctx
, graph
, edge
, c
) < 0)
6197 return isl_stat_error
;
6199 if (any_no_merge(graph
, c
->scc_in_merge
, &graph
->edge
[edge
]))
6200 merged
= isl_bool_false
;
6202 merged
= try_merge(ctx
, graph
, c
);
6204 return isl_stat_error
;
6205 if (!merged
&& edge_weight
== graph
->edge
[edge
].weight
)
6206 graph
->edge
[edge
].no_merge
= 1;
6211 /* Does "node" belong to the cluster identified by "cluster"?
6213 static int node_cluster_exactly(struct isl_sched_node
*node
, int cluster
)
6215 return node
->cluster
== cluster
;
6218 /* Does "edge" connect two nodes belonging to the cluster
6219 * identified by "cluster"?
6221 static int edge_cluster_exactly(struct isl_sched_edge
*edge
, int cluster
)
6223 return edge
->src
->cluster
== cluster
&& edge
->dst
->cluster
== cluster
;
6226 /* Swap the schedule of "node1" and "node2".
6227 * Both nodes have been derived from the same node in a common parent graph.
6228 * Since the "coincident" field is shared with that node
6229 * in the parent graph, there is no need to also swap this field.
6231 static void swap_sched(struct isl_sched_node
*node1
,
6232 struct isl_sched_node
*node2
)
6237 sched
= node1
->sched
;
6238 node1
->sched
= node2
->sched
;
6239 node2
->sched
= sched
;
6241 sched_map
= node1
->sched_map
;
6242 node1
->sched_map
= node2
->sched_map
;
6243 node2
->sched_map
= sched_map
;
6246 /* Copy the current band schedule from the SCCs that form the cluster
6247 * with index "pos" to the actual cluster at position "pos".
6248 * By construction, the index of the first SCC that belongs to the cluster
6251 * The order of the nodes inside both the SCCs and the cluster
6252 * is assumed to be same as the order in the original "graph".
6254 * Since the SCC graphs will no longer be used after this function,
6255 * the schedules are actually swapped rather than copied.
6257 static isl_stat
copy_partial(struct isl_sched_graph
*graph
,
6258 struct isl_clustering
*c
, int pos
)
6262 c
->cluster
[pos
].n_total_row
= c
->scc
[pos
].n_total_row
;
6263 c
->cluster
[pos
].n_row
= c
->scc
[pos
].n_row
;
6264 c
->cluster
[pos
].maxvar
= c
->scc
[pos
].maxvar
;
6266 for (i
= 0; i
< graph
->n
; ++i
) {
6270 if (graph
->node
[i
].cluster
!= pos
)
6272 s
= graph
->node
[i
].scc
;
6273 k
= c
->scc_node
[s
]++;
6274 swap_sched(&c
->cluster
[pos
].node
[j
], &c
->scc
[s
].node
[k
]);
6275 if (c
->scc
[s
].maxvar
> c
->cluster
[pos
].maxvar
)
6276 c
->cluster
[pos
].maxvar
= c
->scc
[s
].maxvar
;
6283 /* Is there a (conditional) validity dependence from node[j] to node[i],
6284 * forcing node[i] to follow node[j] or do the nodes belong to the same
6287 static isl_bool
node_follows_strong_or_same_cluster(int i
, int j
, void *user
)
6289 struct isl_sched_graph
*graph
= user
;
6291 if (graph
->node
[i
].cluster
== graph
->node
[j
].cluster
)
6292 return isl_bool_true
;
6293 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
6296 /* Extract the merged clusters of SCCs in "graph", sort them, and
6297 * store them in c->clusters. Update c->scc_cluster accordingly.
6299 * First keep track of the cluster containing the SCC to which a node
6300 * belongs in the node itself.
6301 * Then extract the clusters into c->clusters, copying the current
6302 * band schedule from the SCCs that belong to the cluster.
6303 * Do this only once per cluster.
6305 * Finally, topologically sort the clusters and update c->scc_cluster
6306 * to match the new scc numbering. While the SCCs were originally
6307 * sorted already, some SCCs that depend on some other SCCs may
6308 * have been merged with SCCs that appear before these other SCCs.
6309 * A reordering may therefore be required.
6311 static isl_stat
extract_clusters(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6312 struct isl_clustering
*c
)
6316 for (i
= 0; i
< graph
->n
; ++i
)
6317 graph
->node
[i
].cluster
= c
->scc_cluster
[graph
->node
[i
].scc
];
6319 for (i
= 0; i
< graph
->scc
; ++i
) {
6320 if (c
->scc_cluster
[i
] != i
)
6322 if (extract_sub_graph(ctx
, graph
, &node_cluster_exactly
,
6323 &edge_cluster_exactly
, i
, &c
->cluster
[i
]) < 0)
6324 return isl_stat_error
;
6325 c
->cluster
[i
].src_scc
= -1;
6326 c
->cluster
[i
].dst_scc
= -1;
6327 if (copy_partial(graph
, c
, i
) < 0)
6328 return isl_stat_error
;
6331 if (detect_ccs(ctx
, graph
, &node_follows_strong_or_same_cluster
) < 0)
6332 return isl_stat_error
;
6333 for (i
= 0; i
< graph
->n
; ++i
)
6334 c
->scc_cluster
[graph
->node
[i
].scc
] = graph
->node
[i
].cluster
;
6339 /* Compute weights on the proximity edges of "graph" that can
6340 * be used by find_proximity to find the most appropriate
6341 * proximity edge to use to merge two clusters in "c".
6342 * The weights are also used by has_bounded_distances to determine
6343 * whether the merge should be allowed.
6344 * Store the maximum of the computed weights in graph->max_weight.
6346 * The computed weight is a measure for the number of remaining schedule
6347 * dimensions that can still be completely aligned.
6348 * In particular, compute the number of equalities between
6349 * input dimensions and output dimensions in the proximity constraints.
6350 * The directions that are already handled by outer schedule bands
6351 * are projected out prior to determining this number.
6353 * Edges that will never be considered by find_proximity are ignored.
6355 static isl_stat
compute_weights(struct isl_sched_graph
*graph
,
6356 struct isl_clustering
*c
)
6360 graph
->max_weight
= 0;
6362 for (i
= 0; i
< graph
->n_edge
; ++i
) {
6363 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
6364 struct isl_sched_node
*src
= edge
->src
;
6365 struct isl_sched_node
*dst
= edge
->dst
;
6366 isl_basic_map
*hull
;
6370 prox
= is_non_empty_proximity(edge
);
6372 return isl_stat_error
;
6375 if (bad_cluster(&c
->scc
[edge
->src
->scc
]) ||
6376 bad_cluster(&c
->scc
[edge
->dst
->scc
]))
6378 if (c
->scc_cluster
[edge
->dst
->scc
] ==
6379 c
->scc_cluster
[edge
->src
->scc
])
6382 hull
= isl_map_affine_hull(isl_map_copy(edge
->map
));
6383 hull
= isl_basic_map_transform_dims(hull
, isl_dim_in
, 0,
6384 isl_mat_copy(src
->ctrans
));
6385 hull
= isl_basic_map_transform_dims(hull
, isl_dim_out
, 0,
6386 isl_mat_copy(dst
->ctrans
));
6387 hull
= isl_basic_map_project_out(hull
,
6388 isl_dim_in
, 0, src
->rank
);
6389 hull
= isl_basic_map_project_out(hull
,
6390 isl_dim_out
, 0, dst
->rank
);
6391 hull
= isl_basic_map_remove_divs(hull
);
6392 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
6393 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
6394 hull
= isl_basic_map_drop_constraints_not_involving_dims(hull
,
6395 isl_dim_in
, 0, n_in
);
6396 hull
= isl_basic_map_drop_constraints_not_involving_dims(hull
,
6397 isl_dim_out
, 0, n_out
);
6399 return isl_stat_error
;
6400 edge
->weight
= isl_basic_map_n_equality(hull
);
6401 isl_basic_map_free(hull
);
6403 if (edge
->weight
> graph
->max_weight
)
6404 graph
->max_weight
= edge
->weight
;
6410 /* Call compute_schedule_finish_band on each of the clusters in "c"
6411 * in their topological order. This order is determined by the scc
6412 * fields of the nodes in "graph".
6413 * Combine the results in a sequence expressing the topological order.
6415 * If there is only one cluster left, then there is no need to introduce
6416 * a sequence node. Also, in this case, the cluster necessarily contains
6417 * the SCC at position 0 in the original graph and is therefore also
6418 * stored in the first cluster of "c".
6420 static __isl_give isl_schedule_node
*finish_bands_clustering(
6421 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
6422 struct isl_clustering
*c
)
6426 isl_union_set_list
*filters
;
6428 if (graph
->scc
== 1)
6429 return compute_schedule_finish_band(node
, &c
->cluster
[0], 0);
6431 ctx
= isl_schedule_node_get_ctx(node
);
6433 filters
= extract_sccs(ctx
, graph
);
6434 node
= isl_schedule_node_insert_sequence(node
, filters
);
6436 for (i
= 0; i
< graph
->scc
; ++i
) {
6437 int j
= c
->scc_cluster
[i
];
6438 node
= isl_schedule_node_child(node
, i
);
6439 node
= isl_schedule_node_child(node
, 0);
6440 node
= compute_schedule_finish_band(node
, &c
->cluster
[j
], 0);
6441 node
= isl_schedule_node_parent(node
);
6442 node
= isl_schedule_node_parent(node
);
6448 /* Compute a schedule for a connected dependence graph by first considering
6449 * each strongly connected component (SCC) in the graph separately and then
6450 * incrementally combining them into clusters.
6451 * Return the updated schedule node.
6453 * Initially, each cluster consists of a single SCC, each with its
6454 * own band schedule. The algorithm then tries to merge pairs
6455 * of clusters along a proximity edge until no more suitable
6456 * proximity edges can be found. During this merging, the schedule
6457 * is maintained in the individual SCCs.
6458 * After the merging is completed, the full resulting clusters
6459 * are extracted and in finish_bands_clustering,
6460 * compute_schedule_finish_band is called on each of them to integrate
6461 * the band into "node" and to continue the computation.
6463 * compute_weights initializes the weights that are used by find_proximity.
6465 static __isl_give isl_schedule_node
*compute_schedule_wcc_clustering(
6466 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
6469 struct isl_clustering c
;
6472 ctx
= isl_schedule_node_get_ctx(node
);
6474 if (clustering_init(ctx
, &c
, graph
) < 0)
6477 if (compute_weights(graph
, &c
) < 0)
6481 i
= find_proximity(graph
, &c
);
6484 if (i
>= graph
->n_edge
)
6486 if (merge_clusters_along_edge(ctx
, graph
, i
, &c
) < 0)
6490 if (extract_clusters(ctx
, graph
, &c
) < 0)
6493 node
= finish_bands_clustering(node
, graph
, &c
);
6495 clustering_free(ctx
, &c
);
6498 clustering_free(ctx
, &c
);
6499 return isl_schedule_node_free(node
);
6502 /* Compute a schedule for a connected dependence graph and return
6503 * the updated schedule node.
6505 * If Feautrier's algorithm is selected, we first recursively try to satisfy
6506 * as many validity dependences as possible. When all validity dependences
6507 * are satisfied we extend the schedule to a full-dimensional schedule.
6509 * Call compute_schedule_wcc_whole or compute_schedule_wcc_clustering
6510 * depending on whether the user has selected the option to try and
6511 * compute a schedule for the entire (weakly connected) component first.
6512 * If there is only a single strongly connected component (SCC), then
6513 * there is no point in trying to combine SCCs
6514 * in compute_schedule_wcc_clustering, so compute_schedule_wcc_whole
6515 * is called instead.
6517 static __isl_give isl_schedule_node
*compute_schedule_wcc(
6518 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
6525 ctx
= isl_schedule_node_get_ctx(node
);
6526 if (detect_sccs(ctx
, graph
) < 0)
6527 return isl_schedule_node_free(node
);
6529 if (compute_maxvar(graph
) < 0)
6530 return isl_schedule_node_free(node
);
6532 if (need_feautrier_step(ctx
, graph
))
6533 return compute_schedule_wcc_feautrier(node
, graph
);
6535 if (graph
->scc
<= 1 || isl_options_get_schedule_whole_component(ctx
))
6536 return compute_schedule_wcc_whole(node
, graph
);
6538 return compute_schedule_wcc_clustering(node
, graph
);
6541 /* Compute a schedule for each group of nodes identified by node->scc
6542 * separately and then combine them in a sequence node (or as set node
6543 * if graph->weak is set) inserted at position "node" of the schedule tree.
6544 * Return the updated schedule node.
6546 * If "wcc" is set then each of the groups belongs to a single
6547 * weakly connected component in the dependence graph so that
6548 * there is no need for compute_sub_schedule to look for weakly
6549 * connected components.
6551 static __isl_give isl_schedule_node
*compute_component_schedule(
6552 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
6557 isl_union_set_list
*filters
;
6561 ctx
= isl_schedule_node_get_ctx(node
);
6563 filters
= extract_sccs(ctx
, graph
);
6565 node
= isl_schedule_node_insert_set(node
, filters
);
6567 node
= isl_schedule_node_insert_sequence(node
, filters
);
6569 for (component
= 0; component
< graph
->scc
; ++component
) {
6570 node
= isl_schedule_node_child(node
, component
);
6571 node
= isl_schedule_node_child(node
, 0);
6572 node
= compute_sub_schedule(node
, ctx
, graph
,
6574 &edge_scc_exactly
, component
, wcc
);
6575 node
= isl_schedule_node_parent(node
);
6576 node
= isl_schedule_node_parent(node
);
6582 /* Compute a schedule for the given dependence graph and insert it at "node".
6583 * Return the updated schedule node.
6585 * We first check if the graph is connected (through validity and conditional
6586 * validity dependences) and, if not, compute a schedule
6587 * for each component separately.
6588 * If the schedule_serialize_sccs option is set, then we check for strongly
6589 * connected components instead and compute a separate schedule for
6590 * each such strongly connected component.
6592 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
6593 struct isl_sched_graph
*graph
)
6600 ctx
= isl_schedule_node_get_ctx(node
);
6601 if (isl_options_get_schedule_serialize_sccs(ctx
)) {
6602 if (detect_sccs(ctx
, graph
) < 0)
6603 return isl_schedule_node_free(node
);
6605 if (detect_wccs(ctx
, graph
) < 0)
6606 return isl_schedule_node_free(node
);
6610 return compute_component_schedule(node
, graph
, 1);
6612 return compute_schedule_wcc(node
, graph
);
6615 /* Compute a schedule on sc->domain that respects the given schedule
6618 * In particular, the schedule respects all the validity dependences.
6619 * If the default isl scheduling algorithm is used, it tries to minimize
6620 * the dependence distances over the proximity dependences.
6621 * If Feautrier's scheduling algorithm is used, the proximity dependence
6622 * distances are only minimized during the extension to a full-dimensional
6625 * If there are any condition and conditional validity dependences,
6626 * then the conditional validity dependences may be violated inside
6627 * a tilable band, provided they have no adjacent non-local
6628 * condition dependences.
6630 __isl_give isl_schedule
*isl_schedule_constraints_compute_schedule(
6631 __isl_take isl_schedule_constraints
*sc
)
6633 isl_ctx
*ctx
= isl_schedule_constraints_get_ctx(sc
);
6634 struct isl_sched_graph graph
= { 0 };
6635 isl_schedule
*sched
;
6636 isl_schedule_node
*node
;
6637 isl_union_set
*domain
;
6639 sc
= isl_schedule_constraints_align_params(sc
);
6641 domain
= isl_schedule_constraints_get_domain(sc
);
6642 if (isl_union_set_n_set(domain
) == 0) {
6643 isl_schedule_constraints_free(sc
);
6644 return isl_schedule_from_domain(domain
);
6647 if (graph_init(&graph
, sc
) < 0)
6648 domain
= isl_union_set_free(domain
);
6650 node
= isl_schedule_node_from_domain(domain
);
6651 node
= isl_schedule_node_child(node
, 0);
6653 node
= compute_schedule(node
, &graph
);
6654 sched
= isl_schedule_node_get_schedule(node
);
6655 isl_schedule_node_free(node
);
6657 graph_free(ctx
, &graph
);
6658 isl_schedule_constraints_free(sc
);
6663 /* Compute a schedule for the given union of domains that respects
6664 * all the validity dependences and minimizes
6665 * the dependence distances over the proximity dependences.
6667 * This function is kept for backward compatibility.
6669 __isl_give isl_schedule
*isl_union_set_compute_schedule(
6670 __isl_take isl_union_set
*domain
,
6671 __isl_take isl_union_map
*validity
,
6672 __isl_take isl_union_map
*proximity
)
6674 isl_schedule_constraints
*sc
;
6676 sc
= isl_schedule_constraints_on_domain(domain
);
6677 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
6678 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
6680 return isl_schedule_constraints_compute_schedule(sc
);