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 * the rows of "indep" represent linear combinations of the schedule
65 * coefficients that are non-zero when the schedule coefficients are
66 * linearly independent of previously computed schedule rows.
67 * ctrans is the transpose of cmap.
68 * start is the first variable in the LP problem in the sequences that
69 * represents the schedule coefficients of this node
70 * nvar is the dimension of the domain
71 * nparam is the number of parameters or 0 if we are not constructing
72 * a parametric schedule
74 * If compressed is set, then hull represents the constraints
75 * that were used to derive the compression, while compress and
76 * decompress map the original space to the compressed space and
79 * scc is the index of SCC (or WCC) this node belongs to
81 * "cluster" is only used inside extract_clusters and identifies
82 * the cluster of SCCs that the node belongs to.
84 * coincident contains a boolean for each of the rows of the schedule,
85 * indicating whether the corresponding scheduling dimension satisfies
86 * the coincidence constraints in the sense that the corresponding
87 * dependence distances are zero.
89 * If the schedule_treat_coalescing option is set, then
90 * "sizes" contains the sizes of the (compressed) instance set
91 * in each direction. If there is no fixed size in a given direction,
92 * then the corresponding size value is set to infinity.
93 * If the schedule_treat_coalescing option or the schedule_max_coefficient
94 * option is set, then "max" contains the maximal values for
95 * schedule coefficients of the (compressed) variables. If no bound
96 * needs to be imposed on a particular variable, then the corresponding
99 struct isl_sched_node
{
103 isl_multi_aff
*compress
;
104 isl_multi_aff
*decompress
;
120 isl_multi_val
*sizes
;
124 static int node_has_tuples(const void *entry
, const void *val
)
126 struct isl_sched_node
*node
= (struct isl_sched_node
*)entry
;
127 isl_space
*space
= (isl_space
*) val
;
129 return isl_space_has_equal_tuples(node
->space
, space
);
132 static int node_scc_exactly(struct isl_sched_node
*node
, int scc
)
134 return node
->scc
== scc
;
137 static int node_scc_at_most(struct isl_sched_node
*node
, int scc
)
139 return node
->scc
<= scc
;
142 static int node_scc_at_least(struct isl_sched_node
*node
, int scc
)
144 return node
->scc
>= scc
;
147 /* An edge in the dependence graph. An edge may be used to
148 * ensure validity of the generated schedule, to minimize the dependence
151 * map is the dependence relation, with i -> j in the map if j depends on i
152 * tagged_condition and tagged_validity contain the union of all tagged
153 * condition or conditional validity dependence relations that
154 * specialize the dependence relation "map"; that is,
155 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
156 * or "tagged_validity", then i -> j is an element of "map".
157 * If these fields are NULL, then they represent the empty relation.
158 * src is the source node
159 * dst is the sink node
161 * types is a bit vector containing the types of this edge.
162 * validity is set if the edge is used to ensure correctness
163 * coincidence is used to enforce zero dependence distances
164 * proximity is set if the edge is used to minimize dependence distances
165 * condition is set if the edge represents a condition
166 * for a conditional validity schedule constraint
167 * local can only be set for condition edges and indicates that
168 * the dependence distance over the edge should be zero
169 * conditional_validity is set if the edge is used to conditionally
172 * For validity edges, start and end mark the sequence of inequality
173 * constraints in the LP problem that encode the validity constraint
174 * corresponding to this edge.
176 * During clustering, an edge may be marked "no_merge" if it should
177 * not be used to merge clusters.
178 * The weight is also only used during clustering and it is
179 * an indication of how many schedule dimensions on either side
180 * of the schedule constraints can be aligned.
181 * If the weight is negative, then this means that this edge was postponed
182 * by has_bounded_distances or any_no_merge. The original weight can
183 * be retrieved by adding 1 + graph->max_weight, with "graph"
184 * the graph containing this edge.
186 struct isl_sched_edge
{
188 isl_union_map
*tagged_condition
;
189 isl_union_map
*tagged_validity
;
191 struct isl_sched_node
*src
;
192 struct isl_sched_node
*dst
;
203 /* Is "edge" marked as being of type "type"?
205 static int is_type(struct isl_sched_edge
*edge
, enum isl_edge_type type
)
207 return ISL_FL_ISSET(edge
->types
, 1 << type
);
210 /* Mark "edge" as being of type "type".
212 static void set_type(struct isl_sched_edge
*edge
, enum isl_edge_type type
)
214 ISL_FL_SET(edge
->types
, 1 << type
);
217 /* No longer mark "edge" as being of type "type"?
219 static void clear_type(struct isl_sched_edge
*edge
, enum isl_edge_type type
)
221 ISL_FL_CLR(edge
->types
, 1 << type
);
224 /* Is "edge" marked as a validity edge?
226 static int is_validity(struct isl_sched_edge
*edge
)
228 return is_type(edge
, isl_edge_validity
);
231 /* Mark "edge" as a validity edge.
233 static void set_validity(struct isl_sched_edge
*edge
)
235 set_type(edge
, isl_edge_validity
);
238 /* Is "edge" marked as a proximity edge?
240 static int is_proximity(struct isl_sched_edge
*edge
)
242 return is_type(edge
, isl_edge_proximity
);
245 /* Is "edge" marked as a local edge?
247 static int is_local(struct isl_sched_edge
*edge
)
249 return is_type(edge
, isl_edge_local
);
252 /* Mark "edge" as a local edge.
254 static void set_local(struct isl_sched_edge
*edge
)
256 set_type(edge
, isl_edge_local
);
259 /* No longer mark "edge" as a local edge.
261 static void clear_local(struct isl_sched_edge
*edge
)
263 clear_type(edge
, isl_edge_local
);
266 /* Is "edge" marked as a coincidence edge?
268 static int is_coincidence(struct isl_sched_edge
*edge
)
270 return is_type(edge
, isl_edge_coincidence
);
273 /* Is "edge" marked as a condition edge?
275 static int is_condition(struct isl_sched_edge
*edge
)
277 return is_type(edge
, isl_edge_condition
);
280 /* Is "edge" marked as a conditional validity edge?
282 static int is_conditional_validity(struct isl_sched_edge
*edge
)
284 return is_type(edge
, isl_edge_conditional_validity
);
287 /* Internal information about the dependence graph used during
288 * the construction of the schedule.
290 * intra_hmap is a cache, mapping dependence relations to their dual,
291 * for dependences from a node to itself
292 * inter_hmap is a cache, mapping dependence relations to their dual,
293 * for dependences between distinct nodes
294 * if compression is involved then the key for these maps
295 * is the original, uncompressed dependence relation, while
296 * the value is the dual of the compressed dependence relation.
298 * n is the number of nodes
299 * node is the list of nodes
300 * maxvar is the maximal number of variables over all nodes
301 * max_row is the allocated number of rows in the schedule
302 * n_row is the current (maximal) number of linearly independent
303 * rows in the node schedules
304 * n_total_row is the current number of rows in the node schedules
305 * band_start is the starting row in the node schedules of the current band
306 * root is set if this graph is the original dependence graph,
307 * without any splitting
309 * sorted contains a list of node indices sorted according to the
310 * SCC to which a node belongs
312 * n_edge is the number of edges
313 * edge is the list of edges
314 * max_edge contains the maximal number of edges of each type;
315 * in particular, it contains the number of edges in the inital graph.
316 * edge_table contains pointers into the edge array, hashed on the source
317 * and sink spaces; there is one such table for each type;
318 * a given edge may be referenced from more than one table
319 * if the corresponding relation appears in more than one of the
320 * sets of dependences; however, for each type there is only
321 * a single edge between a given pair of source and sink space
322 * in the entire graph
324 * node_table contains pointers into the node array, hashed on the space tuples
326 * region contains a list of variable sequences that should be non-trivial
328 * lp contains the (I)LP problem used to obtain new schedule rows
330 * src_scc and dst_scc are the source and sink SCCs of an edge with
331 * conflicting constraints
333 * scc represents the number of components
334 * weak is set if the components are weakly connected
336 * max_weight is used during clustering and represents the maximal
337 * weight of the relevant proximity edges.
339 struct isl_sched_graph
{
340 isl_map_to_basic_set
*intra_hmap
;
341 isl_map_to_basic_set
*inter_hmap
;
343 struct isl_sched_node
*node
;
356 struct isl_sched_edge
*edge
;
358 int max_edge
[isl_edge_last
+ 1];
359 struct isl_hash_table
*edge_table
[isl_edge_last
+ 1];
361 struct isl_hash_table
*node_table
;
362 struct isl_trivial_region
*region
;
375 /* Initialize node_table based on the list of nodes.
377 static int graph_init_table(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
381 graph
->node_table
= isl_hash_table_alloc(ctx
, graph
->n
);
382 if (!graph
->node_table
)
385 for (i
= 0; i
< graph
->n
; ++i
) {
386 struct isl_hash_table_entry
*entry
;
389 hash
= isl_space_get_tuple_hash(graph
->node
[i
].space
);
390 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
392 graph
->node
[i
].space
, 1);
395 entry
->data
= &graph
->node
[i
];
401 /* Return a pointer to the node that lives within the given space,
402 * or NULL if there is no such node.
404 static struct isl_sched_node
*graph_find_node(isl_ctx
*ctx
,
405 struct isl_sched_graph
*graph
, __isl_keep isl_space
*space
)
407 struct isl_hash_table_entry
*entry
;
410 hash
= isl_space_get_tuple_hash(space
);
411 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
412 &node_has_tuples
, space
, 0);
414 return entry
? entry
->data
: NULL
;
417 static int edge_has_src_and_dst(const void *entry
, const void *val
)
419 const struct isl_sched_edge
*edge
= entry
;
420 const struct isl_sched_edge
*temp
= val
;
422 return edge
->src
== temp
->src
&& edge
->dst
== temp
->dst
;
425 /* Add the given edge to graph->edge_table[type].
427 static isl_stat
graph_edge_table_add(isl_ctx
*ctx
,
428 struct isl_sched_graph
*graph
, enum isl_edge_type type
,
429 struct isl_sched_edge
*edge
)
431 struct isl_hash_table_entry
*entry
;
434 hash
= isl_hash_init();
435 hash
= isl_hash_builtin(hash
, edge
->src
);
436 hash
= isl_hash_builtin(hash
, edge
->dst
);
437 entry
= isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
438 &edge_has_src_and_dst
, edge
, 1);
440 return isl_stat_error
;
446 /* Allocate the edge_tables based on the maximal number of edges of
449 static int graph_init_edge_tables(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
453 for (i
= 0; i
<= isl_edge_last
; ++i
) {
454 graph
->edge_table
[i
] = isl_hash_table_alloc(ctx
,
456 if (!graph
->edge_table
[i
])
463 /* If graph->edge_table[type] contains an edge from the given source
464 * to the given destination, then return the hash table entry of this edge.
465 * Otherwise, return NULL.
467 static struct isl_hash_table_entry
*graph_find_edge_entry(
468 struct isl_sched_graph
*graph
,
469 enum isl_edge_type type
,
470 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
472 isl_ctx
*ctx
= isl_space_get_ctx(src
->space
);
474 struct isl_sched_edge temp
= { .src
= src
, .dst
= dst
};
476 hash
= isl_hash_init();
477 hash
= isl_hash_builtin(hash
, temp
.src
);
478 hash
= isl_hash_builtin(hash
, temp
.dst
);
479 return isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
480 &edge_has_src_and_dst
, &temp
, 0);
484 /* If graph->edge_table[type] contains an edge from the given source
485 * to the given destination, then return this edge.
486 * Otherwise, return NULL.
488 static struct isl_sched_edge
*graph_find_edge(struct isl_sched_graph
*graph
,
489 enum isl_edge_type type
,
490 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
492 struct isl_hash_table_entry
*entry
;
494 entry
= graph_find_edge_entry(graph
, type
, src
, dst
);
501 /* Check whether the dependence graph has an edge of the given type
502 * between the given two nodes.
504 static isl_bool
graph_has_edge(struct isl_sched_graph
*graph
,
505 enum isl_edge_type type
,
506 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
508 struct isl_sched_edge
*edge
;
511 edge
= graph_find_edge(graph
, type
, src
, dst
);
515 empty
= isl_map_plain_is_empty(edge
->map
);
517 return isl_bool_error
;
522 /* Look for any edge with the same src, dst and map fields as "model".
524 * Return the matching edge if one can be found.
525 * Return "model" if no matching edge is found.
526 * Return NULL on error.
528 static struct isl_sched_edge
*graph_find_matching_edge(
529 struct isl_sched_graph
*graph
, struct isl_sched_edge
*model
)
531 enum isl_edge_type i
;
532 struct isl_sched_edge
*edge
;
534 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
537 edge
= graph_find_edge(graph
, i
, model
->src
, model
->dst
);
540 is_equal
= isl_map_plain_is_equal(model
->map
, edge
->map
);
550 /* Remove the given edge from all the edge_tables that refer to it.
552 static void graph_remove_edge(struct isl_sched_graph
*graph
,
553 struct isl_sched_edge
*edge
)
555 isl_ctx
*ctx
= isl_map_get_ctx(edge
->map
);
556 enum isl_edge_type i
;
558 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
559 struct isl_hash_table_entry
*entry
;
561 entry
= graph_find_edge_entry(graph
, i
, edge
->src
, edge
->dst
);
564 if (entry
->data
!= edge
)
566 isl_hash_table_remove(ctx
, graph
->edge_table
[i
], entry
);
570 /* Check whether the dependence graph has any edge
571 * between the given two nodes.
573 static isl_bool
graph_has_any_edge(struct isl_sched_graph
*graph
,
574 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
576 enum isl_edge_type i
;
579 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
580 r
= graph_has_edge(graph
, i
, src
, dst
);
588 /* Check whether the dependence graph has a validity edge
589 * between the given two nodes.
591 * Conditional validity edges are essentially validity edges that
592 * can be ignored if the corresponding condition edges are iteration private.
593 * Here, we are only checking for the presence of validity
594 * edges, so we need to consider the conditional validity edges too.
595 * In particular, this function is used during the detection
596 * of strongly connected components and we cannot ignore
597 * conditional validity edges during this detection.
599 static isl_bool
graph_has_validity_edge(struct isl_sched_graph
*graph
,
600 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
604 r
= graph_has_edge(graph
, isl_edge_validity
, src
, dst
);
608 return graph_has_edge(graph
, isl_edge_conditional_validity
, src
, dst
);
611 static int graph_alloc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
612 int n_node
, int n_edge
)
617 graph
->n_edge
= n_edge
;
618 graph
->node
= isl_calloc_array(ctx
, struct isl_sched_node
, graph
->n
);
619 graph
->sorted
= isl_calloc_array(ctx
, int, graph
->n
);
620 graph
->region
= isl_alloc_array(ctx
,
621 struct isl_trivial_region
, graph
->n
);
622 graph
->edge
= isl_calloc_array(ctx
,
623 struct isl_sched_edge
, graph
->n_edge
);
625 graph
->intra_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
626 graph
->inter_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
628 if (!graph
->node
|| !graph
->region
|| (graph
->n_edge
&& !graph
->edge
) ||
632 for(i
= 0; i
< graph
->n
; ++i
)
633 graph
->sorted
[i
] = i
;
638 static void graph_free(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
642 isl_map_to_basic_set_free(graph
->intra_hmap
);
643 isl_map_to_basic_set_free(graph
->inter_hmap
);
646 for (i
= 0; i
< graph
->n
; ++i
) {
647 isl_space_free(graph
->node
[i
].space
);
648 isl_set_free(graph
->node
[i
].hull
);
649 isl_multi_aff_free(graph
->node
[i
].compress
);
650 isl_multi_aff_free(graph
->node
[i
].decompress
);
651 isl_mat_free(graph
->node
[i
].sched
);
652 isl_map_free(graph
->node
[i
].sched_map
);
653 isl_mat_free(graph
->node
[i
].cmap
);
654 isl_mat_free(graph
->node
[i
].indep
);
655 isl_mat_free(graph
->node
[i
].ctrans
);
657 free(graph
->node
[i
].coincident
);
658 isl_multi_val_free(graph
->node
[i
].sizes
);
659 isl_vec_free(graph
->node
[i
].max
);
664 for (i
= 0; i
< graph
->n_edge
; ++i
) {
665 isl_map_free(graph
->edge
[i
].map
);
666 isl_union_map_free(graph
->edge
[i
].tagged_condition
);
667 isl_union_map_free(graph
->edge
[i
].tagged_validity
);
671 for (i
= 0; i
<= isl_edge_last
; ++i
)
672 isl_hash_table_free(ctx
, graph
->edge_table
[i
]);
673 isl_hash_table_free(ctx
, graph
->node_table
);
674 isl_basic_set_free(graph
->lp
);
677 /* For each "set" on which this function is called, increment
678 * graph->n by one and update graph->maxvar.
680 static isl_stat
init_n_maxvar(__isl_take isl_set
*set
, void *user
)
682 struct isl_sched_graph
*graph
= user
;
683 int nvar
= isl_set_dim(set
, isl_dim_set
);
686 if (nvar
> graph
->maxvar
)
687 graph
->maxvar
= nvar
;
694 /* Compute the number of rows that should be allocated for the schedule.
695 * In particular, we need one row for each variable or one row
696 * for each basic map in the dependences.
697 * Note that it is practically impossible to exhaust both
698 * the number of dependences and the number of variables.
700 static isl_stat
compute_max_row(struct isl_sched_graph
*graph
,
701 __isl_keep isl_schedule_constraints
*sc
)
705 isl_union_set
*domain
;
709 domain
= isl_schedule_constraints_get_domain(sc
);
710 r
= isl_union_set_foreach_set(domain
, &init_n_maxvar
, graph
);
711 isl_union_set_free(domain
);
713 return isl_stat_error
;
714 n_edge
= isl_schedule_constraints_n_basic_map(sc
);
716 return isl_stat_error
;
717 graph
->max_row
= n_edge
+ graph
->maxvar
;
722 /* Does "bset" have any defining equalities for its set variables?
724 static isl_bool
has_any_defining_equality(__isl_keep isl_basic_set
*bset
)
729 return isl_bool_error
;
731 n
= isl_basic_set_dim(bset
, isl_dim_set
);
732 for (i
= 0; i
< n
; ++i
) {
735 has
= isl_basic_set_has_defining_equality(bset
, isl_dim_set
, i
,
741 return isl_bool_false
;
744 /* Set the entries of node->max to the value of the schedule_max_coefficient
747 static isl_stat
set_max_coefficient(isl_ctx
*ctx
, struct isl_sched_node
*node
)
751 max
= isl_options_get_schedule_max_coefficient(ctx
);
755 node
->max
= isl_vec_alloc(ctx
, node
->nvar
);
756 node
->max
= isl_vec_set_si(node
->max
, max
);
758 return isl_stat_error
;
763 /* Set the entries of node->max to the minimum of the schedule_max_coefficient
764 * option (if set) and half of the minimum of the sizes in the other
765 * dimensions. If the minimum of the sizes is one, half of the size
766 * is zero and this value is reset to one.
767 * If the global minimum is unbounded (i.e., if both
768 * the schedule_max_coefficient is not set and the sizes in the other
769 * dimensions are unbounded), then store a negative value.
770 * If the schedule coefficient is close to the size of the instance set
771 * in another dimension, then the schedule may represent a loop
772 * coalescing transformation (especially if the coefficient
773 * in that other dimension is one). Forcing the coefficient to be
774 * smaller than or equal to half the minimal size should avoid this
777 static isl_stat
compute_max_coefficient(isl_ctx
*ctx
,
778 struct isl_sched_node
*node
)
784 max
= isl_options_get_schedule_max_coefficient(ctx
);
785 v
= isl_vec_alloc(ctx
, node
->nvar
);
787 return isl_stat_error
;
789 for (i
= 0; i
< node
->nvar
; ++i
) {
790 isl_int_set_si(v
->el
[i
], max
);
791 isl_int_mul_si(v
->el
[i
], v
->el
[i
], 2);
794 for (i
= 0; i
< node
->nvar
; ++i
) {
797 size
= isl_multi_val_get_val(node
->sizes
, i
);
800 if (!isl_val_is_int(size
)) {
804 for (j
= 0; j
< node
->nvar
; ++j
) {
807 if (isl_int_is_neg(v
->el
[j
]) ||
808 isl_int_gt(v
->el
[j
], size
->n
))
809 isl_int_set(v
->el
[j
], size
->n
);
814 for (i
= 0; i
< node
->nvar
; ++i
) {
815 isl_int_fdiv_q_ui(v
->el
[i
], v
->el
[i
], 2);
816 if (isl_int_is_zero(v
->el
[i
]))
817 isl_int_set_si(v
->el
[i
], 1);
824 return isl_stat_error
;
827 /* Compute and return the size of "set" in dimension "dim".
828 * The size is taken to be the difference in values for that variable
829 * for fixed values of the other variables.
830 * In particular, the variable is first isolated from the other variables
831 * in the range of a map
833 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [i_dim]
835 * and then duplicated
837 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [[i_dim] -> [i_dim']]
839 * The shared variables are then projected out and the maximal value
840 * of i_dim' - i_dim is computed.
842 static __isl_give isl_val
*compute_size(__isl_take isl_set
*set
, int dim
)
849 map
= isl_set_project_onto_map(set
, isl_dim_set
, dim
, 1);
850 map
= isl_map_project_out(map
, isl_dim_in
, dim
, 1);
851 map
= isl_map_range_product(map
, isl_map_copy(map
));
852 map
= isl_set_unwrap(isl_map_range(map
));
853 set
= isl_map_deltas(map
);
854 ls
= isl_local_space_from_space(isl_set_get_space(set
));
855 obj
= isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
856 v
= isl_set_max_val(set
, obj
);
863 /* Compute the size of the instance set "set" of "node", after compression,
864 * as well as bounds on the corresponding coefficients, if needed.
866 * The sizes are needed when the schedule_treat_coalescing option is set.
867 * The bounds are needed when the schedule_treat_coalescing option or
868 * the schedule_max_coefficient option is set.
870 * If the schedule_treat_coalescing option is not set, then at most
871 * the bounds need to be set and this is done in set_max_coefficient.
872 * Otherwise, compress the domain if needed, compute the size
873 * in each direction and store the results in node->size.
874 * Finally, set the bounds on the coefficients based on the sizes
875 * and the schedule_max_coefficient option in compute_max_coefficient.
877 static isl_stat
compute_sizes_and_max(isl_ctx
*ctx
, struct isl_sched_node
*node
,
878 __isl_take isl_set
*set
)
883 if (!isl_options_get_schedule_treat_coalescing(ctx
)) {
885 return set_max_coefficient(ctx
, node
);
888 if (node
->compressed
)
889 set
= isl_set_preimage_multi_aff(set
,
890 isl_multi_aff_copy(node
->decompress
));
891 mv
= isl_multi_val_zero(isl_set_get_space(set
));
892 n
= isl_set_dim(set
, isl_dim_set
);
893 for (j
= 0; j
< n
; ++j
) {
896 v
= compute_size(isl_set_copy(set
), j
);
897 mv
= isl_multi_val_set_val(mv
, j
, v
);
902 return isl_stat_error
;
903 return compute_max_coefficient(ctx
, node
);
906 /* Add a new node to the graph representing the given instance set.
907 * "nvar" is the (possibly compressed) number of variables and
908 * may be smaller than then number of set variables in "set"
909 * if "compressed" is set.
910 * If "compressed" is set, then "hull" represents the constraints
911 * that were used to derive the compression, while "compress" and
912 * "decompress" map the original space to the compressed space and
914 * If "compressed" is not set, then "hull", "compress" and "decompress"
917 * Compute the size of the instance set and bounds on the coefficients,
920 static isl_stat
add_node(struct isl_sched_graph
*graph
,
921 __isl_take isl_set
*set
, int nvar
, int compressed
,
922 __isl_take isl_set
*hull
, __isl_take isl_multi_aff
*compress
,
923 __isl_take isl_multi_aff
*decompress
)
930 struct isl_sched_node
*node
;
933 return isl_stat_error
;
935 ctx
= isl_set_get_ctx(set
);
936 nparam
= isl_set_dim(set
, isl_dim_param
);
937 if (!ctx
->opt
->schedule_parametric
)
939 sched
= isl_mat_alloc(ctx
, 0, 1 + nparam
+ nvar
);
940 node
= &graph
->node
[graph
->n
];
942 space
= isl_set_get_space(set
);
945 node
->nparam
= nparam
;
947 node
->sched_map
= NULL
;
948 coincident
= isl_calloc_array(ctx
, int, graph
->max_row
);
949 node
->coincident
= coincident
;
950 node
->compressed
= compressed
;
952 node
->compress
= compress
;
953 node
->decompress
= decompress
;
954 if (compute_sizes_and_max(ctx
, node
, set
) < 0)
955 return isl_stat_error
;
957 if (!space
|| !sched
|| (graph
->max_row
&& !coincident
))
958 return isl_stat_error
;
959 if (compressed
&& (!hull
|| !compress
|| !decompress
))
960 return isl_stat_error
;
965 /* Construct an identifier for node "node", which will represent "set".
966 * The name of the identifier is either "compressed" or
967 * "compressed_<name>", with <name> the name of the space of "set".
968 * The user pointer of the identifier points to "node".
970 static __isl_give isl_id
*construct_compressed_id(__isl_keep isl_set
*set
,
971 struct isl_sched_node
*node
)
980 has_name
= isl_set_has_tuple_name(set
);
984 ctx
= isl_set_get_ctx(set
);
986 return isl_id_alloc(ctx
, "compressed", node
);
988 p
= isl_printer_to_str(ctx
);
989 name
= isl_set_get_tuple_name(set
);
990 p
= isl_printer_print_str(p
, "compressed_");
991 p
= isl_printer_print_str(p
, name
);
992 id_name
= isl_printer_get_str(p
);
995 id
= isl_id_alloc(ctx
, id_name
, node
);
1001 /* Add a new node to the graph representing the given set.
1003 * If any of the set variables is defined by an equality, then
1004 * we perform variable compression such that we can perform
1005 * the scheduling on the compressed domain.
1006 * In this case, an identifier is used that references the new node
1007 * such that each compressed space is unique and
1008 * such that the node can be recovered from the compressed space.
1010 static isl_stat
extract_node(__isl_take isl_set
*set
, void *user
)
1013 isl_bool has_equality
;
1015 isl_basic_set
*hull
;
1018 isl_multi_aff
*compress
, *decompress
;
1019 struct isl_sched_graph
*graph
= user
;
1021 hull
= isl_set_affine_hull(isl_set_copy(set
));
1022 hull
= isl_basic_set_remove_divs(hull
);
1023 nvar
= isl_set_dim(set
, isl_dim_set
);
1024 has_equality
= has_any_defining_equality(hull
);
1026 if (has_equality
< 0)
1028 if (!has_equality
) {
1029 isl_basic_set_free(hull
);
1030 return add_node(graph
, set
, nvar
, 0, NULL
, NULL
, NULL
);
1033 id
= construct_compressed_id(set
, &graph
->node
[graph
->n
]);
1034 morph
= isl_basic_set_variable_compression_with_id(hull
,
1037 nvar
= isl_morph_ran_dim(morph
, isl_dim_set
);
1038 compress
= isl_morph_get_var_multi_aff(morph
);
1039 morph
= isl_morph_inverse(morph
);
1040 decompress
= isl_morph_get_var_multi_aff(morph
);
1041 isl_morph_free(morph
);
1043 hull_set
= isl_set_from_basic_set(hull
);
1044 return add_node(graph
, set
, nvar
, 1, hull_set
, compress
, decompress
);
1046 isl_basic_set_free(hull
);
1048 return isl_stat_error
;
1051 struct isl_extract_edge_data
{
1052 enum isl_edge_type type
;
1053 struct isl_sched_graph
*graph
;
1056 /* Merge edge2 into edge1, freeing the contents of edge2.
1057 * Return 0 on success and -1 on failure.
1059 * edge1 and edge2 are assumed to have the same value for the map field.
1061 static int merge_edge(struct isl_sched_edge
*edge1
,
1062 struct isl_sched_edge
*edge2
)
1064 edge1
->types
|= edge2
->types
;
1065 isl_map_free(edge2
->map
);
1067 if (is_condition(edge2
)) {
1068 if (!edge1
->tagged_condition
)
1069 edge1
->tagged_condition
= edge2
->tagged_condition
;
1071 edge1
->tagged_condition
=
1072 isl_union_map_union(edge1
->tagged_condition
,
1073 edge2
->tagged_condition
);
1076 if (is_conditional_validity(edge2
)) {
1077 if (!edge1
->tagged_validity
)
1078 edge1
->tagged_validity
= edge2
->tagged_validity
;
1080 edge1
->tagged_validity
=
1081 isl_union_map_union(edge1
->tagged_validity
,
1082 edge2
->tagged_validity
);
1085 if (is_condition(edge2
) && !edge1
->tagged_condition
)
1087 if (is_conditional_validity(edge2
) && !edge1
->tagged_validity
)
1093 /* Insert dummy tags in domain and range of "map".
1095 * In particular, if "map" is of the form
1101 * [A -> dummy_tag] -> [B -> dummy_tag]
1103 * where the dummy_tags are identical and equal to any dummy tags
1104 * introduced by any other call to this function.
1106 static __isl_give isl_map
*insert_dummy_tags(__isl_take isl_map
*map
)
1112 isl_set
*domain
, *range
;
1114 ctx
= isl_map_get_ctx(map
);
1116 id
= isl_id_alloc(ctx
, NULL
, &dummy
);
1117 space
= isl_space_params(isl_map_get_space(map
));
1118 space
= isl_space_set_from_params(space
);
1119 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
1120 space
= isl_space_map_from_set(space
);
1122 domain
= isl_map_wrap(map
);
1123 range
= isl_map_wrap(isl_map_universe(space
));
1124 map
= isl_map_from_domain_and_range(domain
, range
);
1125 map
= isl_map_zip(map
);
1130 /* Given that at least one of "src" or "dst" is compressed, return
1131 * a map between the spaces of these nodes restricted to the affine
1132 * hull that was used in the compression.
1134 static __isl_give isl_map
*extract_hull(struct isl_sched_node
*src
,
1135 struct isl_sched_node
*dst
)
1139 if (src
->compressed
)
1140 dom
= isl_set_copy(src
->hull
);
1142 dom
= isl_set_universe(isl_space_copy(src
->space
));
1143 if (dst
->compressed
)
1144 ran
= isl_set_copy(dst
->hull
);
1146 ran
= isl_set_universe(isl_space_copy(dst
->space
));
1148 return isl_map_from_domain_and_range(dom
, ran
);
1151 /* Intersect the domains of the nested relations in domain and range
1152 * of "tagged" with "map".
1154 static __isl_give isl_map
*map_intersect_domains(__isl_take isl_map
*tagged
,
1155 __isl_keep isl_map
*map
)
1159 tagged
= isl_map_zip(tagged
);
1160 set
= isl_map_wrap(isl_map_copy(map
));
1161 tagged
= isl_map_intersect_domain(tagged
, set
);
1162 tagged
= isl_map_zip(tagged
);
1166 /* Return a pointer to the node that lives in the domain space of "map"
1167 * or NULL if there is no such node.
1169 static struct isl_sched_node
*find_domain_node(isl_ctx
*ctx
,
1170 struct isl_sched_graph
*graph
, __isl_keep isl_map
*map
)
1172 struct isl_sched_node
*node
;
1175 space
= isl_space_domain(isl_map_get_space(map
));
1176 node
= graph_find_node(ctx
, graph
, space
);
1177 isl_space_free(space
);
1182 /* Return a pointer to the node that lives in the range space of "map"
1183 * or NULL if there is no such node.
1185 static struct isl_sched_node
*find_range_node(isl_ctx
*ctx
,
1186 struct isl_sched_graph
*graph
, __isl_keep isl_map
*map
)
1188 struct isl_sched_node
*node
;
1191 space
= isl_space_range(isl_map_get_space(map
));
1192 node
= graph_find_node(ctx
, graph
, space
);
1193 isl_space_free(space
);
1198 /* Add a new edge to the graph based on the given map
1199 * and add it to data->graph->edge_table[data->type].
1200 * If a dependence relation of a given type happens to be identical
1201 * to one of the dependence relations of a type that was added before,
1202 * then we don't create a new edge, but instead mark the original edge
1203 * as also representing a dependence of the current type.
1205 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1206 * may be specified as "tagged" dependence relations. That is, "map"
1207 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1208 * the dependence on iterations and a and b are tags.
1209 * edge->map is set to the relation containing the elements i -> j,
1210 * while edge->tagged_condition and edge->tagged_validity contain
1211 * the union of all the "map" relations
1212 * for which extract_edge is called that result in the same edge->map.
1214 * If the source or the destination node is compressed, then
1215 * intersect both "map" and "tagged" with the constraints that
1216 * were used to construct the compression.
1217 * This ensures that there are no schedule constraints defined
1218 * outside of these domains, while the scheduler no longer has
1219 * any control over those outside parts.
1221 static isl_stat
extract_edge(__isl_take isl_map
*map
, void *user
)
1223 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1224 struct isl_extract_edge_data
*data
= user
;
1225 struct isl_sched_graph
*graph
= data
->graph
;
1226 struct isl_sched_node
*src
, *dst
;
1227 struct isl_sched_edge
*edge
;
1228 isl_map
*tagged
= NULL
;
1230 if (data
->type
== isl_edge_condition
||
1231 data
->type
== isl_edge_conditional_validity
) {
1232 if (isl_map_can_zip(map
)) {
1233 tagged
= isl_map_copy(map
);
1234 map
= isl_set_unwrap(isl_map_domain(isl_map_zip(map
)));
1236 tagged
= insert_dummy_tags(isl_map_copy(map
));
1240 src
= find_domain_node(ctx
, graph
, map
);
1241 dst
= find_range_node(ctx
, graph
, map
);
1245 isl_map_free(tagged
);
1249 if (src
->compressed
|| dst
->compressed
) {
1251 hull
= extract_hull(src
, dst
);
1253 tagged
= map_intersect_domains(tagged
, hull
);
1254 map
= isl_map_intersect(map
, hull
);
1257 graph
->edge
[graph
->n_edge
].src
= src
;
1258 graph
->edge
[graph
->n_edge
].dst
= dst
;
1259 graph
->edge
[graph
->n_edge
].map
= map
;
1260 graph
->edge
[graph
->n_edge
].types
= 0;
1261 graph
->edge
[graph
->n_edge
].tagged_condition
= NULL
;
1262 graph
->edge
[graph
->n_edge
].tagged_validity
= NULL
;
1263 set_type(&graph
->edge
[graph
->n_edge
], data
->type
);
1264 if (data
->type
== isl_edge_condition
)
1265 graph
->edge
[graph
->n_edge
].tagged_condition
=
1266 isl_union_map_from_map(tagged
);
1267 if (data
->type
== isl_edge_conditional_validity
)
1268 graph
->edge
[graph
->n_edge
].tagged_validity
=
1269 isl_union_map_from_map(tagged
);
1271 edge
= graph_find_matching_edge(graph
, &graph
->edge
[graph
->n_edge
]);
1274 return isl_stat_error
;
1276 if (edge
== &graph
->edge
[graph
->n_edge
])
1277 return graph_edge_table_add(ctx
, graph
, data
->type
,
1278 &graph
->edge
[graph
->n_edge
++]);
1280 if (merge_edge(edge
, &graph
->edge
[graph
->n_edge
]) < 0)
1283 return graph_edge_table_add(ctx
, graph
, data
->type
, edge
);
1286 /* Initialize the schedule graph "graph" from the schedule constraints "sc".
1288 * The context is included in the domain before the nodes of
1289 * the graphs are extracted in order to be able to exploit
1290 * any possible additional equalities.
1291 * Note that this intersection is only performed locally here.
1293 static isl_stat
graph_init(struct isl_sched_graph
*graph
,
1294 __isl_keep isl_schedule_constraints
*sc
)
1297 isl_union_set
*domain
;
1299 struct isl_extract_edge_data data
;
1300 enum isl_edge_type i
;
1304 return isl_stat_error
;
1306 ctx
= isl_schedule_constraints_get_ctx(sc
);
1308 domain
= isl_schedule_constraints_get_domain(sc
);
1309 graph
->n
= isl_union_set_n_set(domain
);
1310 isl_union_set_free(domain
);
1312 if (graph_alloc(ctx
, graph
, graph
->n
,
1313 isl_schedule_constraints_n_map(sc
)) < 0)
1314 return isl_stat_error
;
1316 if (compute_max_row(graph
, sc
) < 0)
1317 return isl_stat_error
;
1320 domain
= isl_schedule_constraints_get_domain(sc
);
1321 domain
= isl_union_set_intersect_params(domain
,
1322 isl_schedule_constraints_get_context(sc
));
1323 r
= isl_union_set_foreach_set(domain
, &extract_node
, graph
);
1324 isl_union_set_free(domain
);
1326 return isl_stat_error
;
1327 if (graph_init_table(ctx
, graph
) < 0)
1328 return isl_stat_error
;
1329 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
1330 c
= isl_schedule_constraints_get(sc
, i
);
1331 graph
->max_edge
[i
] = isl_union_map_n_map(c
);
1332 isl_union_map_free(c
);
1334 return isl_stat_error
;
1336 if (graph_init_edge_tables(ctx
, graph
) < 0)
1337 return isl_stat_error
;
1340 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
1344 c
= isl_schedule_constraints_get(sc
, i
);
1345 r
= isl_union_map_foreach_map(c
, &extract_edge
, &data
);
1346 isl_union_map_free(c
);
1348 return isl_stat_error
;
1354 /* Check whether there is any dependence from node[j] to node[i]
1355 * or from node[i] to node[j].
1357 static isl_bool
node_follows_weak(int i
, int j
, void *user
)
1360 struct isl_sched_graph
*graph
= user
;
1362 f
= graph_has_any_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1365 return graph_has_any_edge(graph
, &graph
->node
[i
], &graph
->node
[j
]);
1368 /* Check whether there is a (conditional) validity dependence from node[j]
1369 * to node[i], forcing node[i] to follow node[j].
1371 static isl_bool
node_follows_strong(int i
, int j
, void *user
)
1373 struct isl_sched_graph
*graph
= user
;
1375 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1378 /* Use Tarjan's algorithm for computing the strongly connected components
1379 * in the dependence graph only considering those edges defined by "follows".
1381 static int detect_ccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
1382 isl_bool (*follows
)(int i
, int j
, void *user
))
1385 struct isl_tarjan_graph
*g
= NULL
;
1387 g
= isl_tarjan_graph_init(ctx
, graph
->n
, follows
, graph
);
1395 while (g
->order
[i
] != -1) {
1396 graph
->node
[g
->order
[i
]].scc
= graph
->scc
;
1404 isl_tarjan_graph_free(g
);
1409 /* Apply Tarjan's algorithm to detect the strongly connected components
1410 * in the dependence graph.
1411 * Only consider the (conditional) validity dependences and clear "weak".
1413 static int detect_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1416 return detect_ccs(ctx
, graph
, &node_follows_strong
);
1419 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1420 * in the dependence graph.
1421 * Consider all dependences and set "weak".
1423 static int detect_wccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1426 return detect_ccs(ctx
, graph
, &node_follows_weak
);
1429 static int cmp_scc(const void *a
, const void *b
, void *data
)
1431 struct isl_sched_graph
*graph
= data
;
1435 return graph
->node
[*i1
].scc
- graph
->node
[*i2
].scc
;
1438 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1440 static int sort_sccs(struct isl_sched_graph
*graph
)
1442 return isl_sort(graph
->sorted
, graph
->n
, sizeof(int), &cmp_scc
, graph
);
1445 /* Given a dependence relation R from "node" to itself,
1446 * construct the set of coefficients of valid constraints for elements
1447 * in that dependence relation.
1448 * In particular, the result contains tuples of coefficients
1449 * c_0, c_n, c_x such that
1451 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1455 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1457 * We choose here to compute the dual of delta R.
1458 * Alternatively, we could have computed the dual of R, resulting
1459 * in a set of tuples c_0, c_n, c_x, c_y, and then
1460 * plugged in (c_0, c_n, c_x, -c_x).
1462 * If "node" has been compressed, then the dependence relation
1463 * is also compressed before the set of coefficients is computed.
1465 static __isl_give isl_basic_set
*intra_coefficients(
1466 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1467 __isl_take isl_map
*map
)
1471 isl_basic_set
*coef
;
1472 isl_maybe_isl_basic_set m
;
1474 m
= isl_map_to_basic_set_try_get(graph
->intra_hmap
, map
);
1475 if (m
.valid
< 0 || m
.valid
) {
1480 key
= isl_map_copy(map
);
1481 if (node
->compressed
) {
1482 map
= isl_map_preimage_domain_multi_aff(map
,
1483 isl_multi_aff_copy(node
->decompress
));
1484 map
= isl_map_preimage_range_multi_aff(map
,
1485 isl_multi_aff_copy(node
->decompress
));
1487 delta
= isl_set_remove_divs(isl_map_deltas(map
));
1488 coef
= isl_set_coefficients(delta
);
1489 graph
->intra_hmap
= isl_map_to_basic_set_set(graph
->intra_hmap
, key
,
1490 isl_basic_set_copy(coef
));
1495 /* Given a dependence relation R, construct the set of coefficients
1496 * of valid constraints for elements in that dependence relation.
1497 * In particular, the result contains tuples of coefficients
1498 * c_0, c_n, c_x, c_y such that
1500 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1502 * If the source or destination nodes of "edge" have been compressed,
1503 * then the dependence relation is also compressed before
1504 * the set of coefficients is computed.
1506 static __isl_give isl_basic_set
*inter_coefficients(
1507 struct isl_sched_graph
*graph
, struct isl_sched_edge
*edge
,
1508 __isl_take isl_map
*map
)
1512 isl_basic_set
*coef
;
1513 isl_maybe_isl_basic_set m
;
1515 m
= isl_map_to_basic_set_try_get(graph
->inter_hmap
, map
);
1516 if (m
.valid
< 0 || m
.valid
) {
1521 key
= isl_map_copy(map
);
1522 if (edge
->src
->compressed
)
1523 map
= isl_map_preimage_domain_multi_aff(map
,
1524 isl_multi_aff_copy(edge
->src
->decompress
));
1525 if (edge
->dst
->compressed
)
1526 map
= isl_map_preimage_range_multi_aff(map
,
1527 isl_multi_aff_copy(edge
->dst
->decompress
));
1528 set
= isl_map_wrap(isl_map_remove_divs(map
));
1529 coef
= isl_set_coefficients(set
);
1530 graph
->inter_hmap
= isl_map_to_basic_set_set(graph
->inter_hmap
, key
,
1531 isl_basic_set_copy(coef
));
1536 /* Return the position of the coefficients of the variables in
1537 * the coefficients constraints "coef".
1539 * The space of "coef" is of the form
1541 * { coefficients[[cst, params] -> S] }
1543 * Return the position of S.
1545 static int coef_var_offset(__isl_keep isl_basic_set
*coef
)
1550 space
= isl_space_unwrap(isl_basic_set_get_space(coef
));
1551 offset
= isl_space_dim(space
, isl_dim_in
);
1552 isl_space_free(space
);
1557 /* Return the offset of the coefficients of the variables of "node"
1560 * Within each node, the coefficients have the following order:
1562 * - c_i_n (if parametric)
1563 * - positive and negative parts of c_i_x
1565 static int node_var_coef_offset(struct isl_sched_node
*node
)
1567 return node
->start
+ 1 + node
->nparam
;
1570 /* Return the position of the pair of variables encoding
1571 * coefficient "i" of "node".
1573 * The order of these variable pairs is the opposite of
1574 * that of the coefficients, with 2 variables per coefficient.
1576 static int node_var_coef_pos(struct isl_sched_node
*node
, int i
)
1578 return node_var_coef_offset(node
) + 2 * (node
->nvar
- 1 - i
);
1581 /* Construct an isl_dim_map for mapping constraints on coefficients
1582 * for "node" to the corresponding positions in graph->lp.
1583 * "offset" is the offset of the coefficients for the variables
1584 * in the input constraints.
1585 * "s" is the sign of the mapping.
1587 * The input constraints are given in terms of the coefficients (c_0, c_n, c_x).
1588 * The mapping produced by this function essentially plugs in
1589 * (0, 0, c_i_x^+ - c_i_x^-) if s = 1 and
1590 * (0, 0, -c_i_x^+ + c_i_x^-) if s = -1.
1591 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1592 * Furthermore, the order of these pairs is the opposite of that
1593 * of the corresponding coefficients.
1595 * The caller can extend the mapping to also map the other coefficients
1596 * (and therefore not plug in 0).
1598 static __isl_give isl_dim_map
*intra_dim_map(isl_ctx
*ctx
,
1599 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1604 isl_dim_map
*dim_map
;
1609 total
= isl_basic_set_total_dim(graph
->lp
);
1610 pos
= node_var_coef_pos(node
, 0);
1611 dim_map
= isl_dim_map_alloc(ctx
, total
);
1612 isl_dim_map_range(dim_map
, pos
, -2, offset
, 1, node
->nvar
, -s
);
1613 isl_dim_map_range(dim_map
, pos
+ 1, -2, offset
, 1, node
->nvar
, s
);
1618 /* Construct an isl_dim_map for mapping constraints on coefficients
1619 * for "src" (node i) and "dst" (node j) to the corresponding positions
1621 * "offset" is the offset of the coefficients for the variables of "src"
1622 * in the input constraints.
1623 * "s" is the sign of the mapping.
1625 * The input constraints are given in terms of the coefficients
1626 * (c_0, c_n, c_x, c_y).
1627 * The mapping produced by this function essentially plugs in
1628 * (c_j_0 - c_i_0, c_j_n - c_i_n,
1629 * -(c_i_x^+ - c_i_x^-), c_j_x^+ - c_j_x^-) if s = 1 and
1630 * (-c_j_0 + c_i_0, -c_j_n + c_i_n,
1631 * c_i_x^+ - c_i_x^-, -(c_j_x^+ - c_j_x^-)) if s = -1.
1632 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1633 * Furthermore, the order of these pairs is the opposite of that
1634 * of the corresponding coefficients.
1636 * The caller can further extend the mapping.
1638 static __isl_give isl_dim_map
*inter_dim_map(isl_ctx
*ctx
,
1639 struct isl_sched_graph
*graph
, struct isl_sched_node
*src
,
1640 struct isl_sched_node
*dst
, int offset
, int s
)
1644 isl_dim_map
*dim_map
;
1649 total
= isl_basic_set_total_dim(graph
->lp
);
1650 dim_map
= isl_dim_map_alloc(ctx
, total
);
1652 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, s
);
1653 isl_dim_map_range(dim_map
, dst
->start
+ 1, 1, 1, 1, dst
->nparam
, s
);
1654 pos
= node_var_coef_pos(dst
, 0);
1655 isl_dim_map_range(dim_map
, pos
, -2, offset
+ src
->nvar
, 1,
1657 isl_dim_map_range(dim_map
, pos
+ 1, -2, offset
+ src
->nvar
, 1,
1660 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, -s
);
1661 isl_dim_map_range(dim_map
, src
->start
+ 1, 1, 1, 1, src
->nparam
, -s
);
1662 pos
= node_var_coef_pos(src
, 0);
1663 isl_dim_map_range(dim_map
, pos
, -2, offset
, 1, src
->nvar
, s
);
1664 isl_dim_map_range(dim_map
, pos
+ 1, -2, offset
, 1, src
->nvar
, -s
);
1669 /* Add the constraints from "src" to "dst" using "dim_map",
1670 * after making sure there is enough room in "dst" for the extra constraints.
1672 static __isl_give isl_basic_set
*add_constraints_dim_map(
1673 __isl_take isl_basic_set
*dst
, __isl_take isl_basic_set
*src
,
1674 __isl_take isl_dim_map
*dim_map
)
1678 n_eq
= isl_basic_set_n_equality(src
);
1679 n_ineq
= isl_basic_set_n_inequality(src
);
1680 dst
= isl_basic_set_extend_constraints(dst
, n_eq
, n_ineq
);
1681 dst
= isl_basic_set_add_constraints_dim_map(dst
, src
, dim_map
);
1685 /* Add constraints to graph->lp that force validity for the given
1686 * dependence from a node i to itself.
1687 * That is, add constraints that enforce
1689 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1690 * = c_i_x (y - x) >= 0
1692 * for each (x,y) in R.
1693 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1694 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1695 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1696 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1698 static isl_stat
add_intra_validity_constraints(struct isl_sched_graph
*graph
,
1699 struct isl_sched_edge
*edge
)
1702 isl_map
*map
= isl_map_copy(edge
->map
);
1703 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1704 isl_dim_map
*dim_map
;
1705 isl_basic_set
*coef
;
1706 struct isl_sched_node
*node
= edge
->src
;
1708 coef
= intra_coefficients(graph
, node
, map
);
1710 offset
= coef_var_offset(coef
);
1713 return isl_stat_error
;
1715 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, 1);
1716 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
1721 /* Add constraints to graph->lp that force validity for the given
1722 * dependence from node i to node j.
1723 * That is, add constraints that enforce
1725 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1727 * for each (x,y) in R.
1728 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1729 * of valid constraints for R and then plug in
1730 * (c_j_0 - c_i_0, c_j_n - c_i_n, -(c_i_x^+ - c_i_x^-), c_j_x^+ - c_j_x^-),
1731 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1732 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1734 static isl_stat
add_inter_validity_constraints(struct isl_sched_graph
*graph
,
1735 struct isl_sched_edge
*edge
)
1740 isl_dim_map
*dim_map
;
1741 isl_basic_set
*coef
;
1742 struct isl_sched_node
*src
= edge
->src
;
1743 struct isl_sched_node
*dst
= edge
->dst
;
1746 return isl_stat_error
;
1748 map
= isl_map_copy(edge
->map
);
1749 ctx
= isl_map_get_ctx(map
);
1750 coef
= inter_coefficients(graph
, edge
, map
);
1752 offset
= coef_var_offset(coef
);
1755 return isl_stat_error
;
1757 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, 1);
1759 edge
->start
= graph
->lp
->n_ineq
;
1760 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
1762 return isl_stat_error
;
1763 edge
->end
= graph
->lp
->n_ineq
;
1768 /* Add constraints to graph->lp that bound the dependence distance for the given
1769 * dependence from a node i to itself.
1770 * If s = 1, we add the constraint
1772 * c_i_x (y - x) <= m_0 + m_n n
1776 * -c_i_x (y - x) + m_0 + m_n n >= 0
1778 * for each (x,y) in R.
1779 * If s = -1, we add the constraint
1781 * -c_i_x (y - x) <= m_0 + m_n n
1785 * c_i_x (y - x) + m_0 + m_n n >= 0
1787 * for each (x,y) in R.
1788 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1789 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1790 * with each coefficient (except m_0) represented as a pair of non-negative
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
);
1824 return isl_stat_error
;
1826 nparam
= isl_space_dim(node
->space
, isl_dim_param
);
1827 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, -s
);
1830 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1831 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1832 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1834 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
1839 /* Add constraints to graph->lp that bound the dependence distance for the given
1840 * dependence from node i to node j.
1841 * If s = 1, we add the constraint
1843 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1848 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1851 * for each (x,y) in R.
1852 * If s = -1, we add the constraint
1854 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1859 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1862 * for each (x,y) in R.
1863 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1864 * of valid constraints for R and then plug in
1865 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1866 * s*c_i_x, -s*c_j_x)
1867 * with each coefficient (except m_0, c_*_0 and c_*_n)
1868 * represented as a pair of non-negative coefficients.
1871 * If "local" is set (and s = 1), then we add constraints
1873 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1877 * -((c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x)) >= 0
1879 * instead, forcing the dependence distance to be (less than or) equal to 0.
1880 * That is, we plug in
1881 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, s*c_i_x, -s*c_j_x).
1882 * Note that dependences marked local are treated as validity constraints
1883 * by add_all_validity_constraints and therefore also have
1884 * their distances bounded by 0 from below.
1886 static isl_stat
add_inter_proximity_constraints(struct isl_sched_graph
*graph
,
1887 struct isl_sched_edge
*edge
, int s
, int local
)
1891 isl_map
*map
= isl_map_copy(edge
->map
);
1892 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1893 isl_dim_map
*dim_map
;
1894 isl_basic_set
*coef
;
1895 struct isl_sched_node
*src
= edge
->src
;
1896 struct isl_sched_node
*dst
= edge
->dst
;
1898 coef
= inter_coefficients(graph
, edge
, map
);
1900 offset
= coef_var_offset(coef
);
1903 return isl_stat_error
;
1905 nparam
= isl_space_dim(src
->space
, isl_dim_param
);
1906 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, -s
);
1909 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1910 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1911 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1914 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
1919 /* Add all validity constraints to graph->lp.
1921 * An edge that is forced to be local needs to have its dependence
1922 * distances equal to zero. We take care of bounding them by 0 from below
1923 * here. add_all_proximity_constraints takes care of bounding them by 0
1926 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1927 * Otherwise, we ignore them.
1929 static int add_all_validity_constraints(struct isl_sched_graph
*graph
,
1930 int use_coincidence
)
1934 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1935 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1938 local
= is_local(edge
) ||
1939 (is_coincidence(edge
) && use_coincidence
);
1940 if (!is_validity(edge
) && !local
)
1942 if (edge
->src
!= edge
->dst
)
1944 if (add_intra_validity_constraints(graph
, edge
) < 0)
1948 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1949 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1952 local
= is_local(edge
) ||
1953 (is_coincidence(edge
) && use_coincidence
);
1954 if (!is_validity(edge
) && !local
)
1956 if (edge
->src
== edge
->dst
)
1958 if (add_inter_validity_constraints(graph
, edge
) < 0)
1965 /* Add constraints to graph->lp that bound the dependence distance
1966 * for all dependence relations.
1967 * If a given proximity dependence is identical to a validity
1968 * dependence, then the dependence distance is already bounded
1969 * from below (by zero), so we only need to bound the distance
1970 * from above. (This includes the case of "local" dependences
1971 * which are treated as validity dependence by add_all_validity_constraints.)
1972 * Otherwise, we need to bound the distance both from above and from below.
1974 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1975 * Otherwise, we ignore them.
1977 static int add_all_proximity_constraints(struct isl_sched_graph
*graph
,
1978 int use_coincidence
)
1982 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1983 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1986 local
= is_local(edge
) ||
1987 (is_coincidence(edge
) && use_coincidence
);
1988 if (!is_proximity(edge
) && !local
)
1990 if (edge
->src
== edge
->dst
&&
1991 add_intra_proximity_constraints(graph
, edge
, 1, local
) < 0)
1993 if (edge
->src
!= edge
->dst
&&
1994 add_inter_proximity_constraints(graph
, edge
, 1, local
) < 0)
1996 if (is_validity(edge
) || local
)
1998 if (edge
->src
== edge
->dst
&&
1999 add_intra_proximity_constraints(graph
, edge
, -1, 0) < 0)
2001 if (edge
->src
!= edge
->dst
&&
2002 add_inter_proximity_constraints(graph
, edge
, -1, 0) < 0)
2009 /* Normalize the rows of "indep" such that all rows are lexicographically
2010 * positive and such that each row contains as many final zeros as possible,
2011 * given the choice for the previous rows.
2012 * Do this by performing elementary row operations.
2014 static __isl_give isl_mat
*normalize_independent(__isl_take isl_mat
*indep
)
2016 indep
= isl_mat_reverse_gauss(indep
);
2017 indep
= isl_mat_lexnonneg_rows(indep
);
2021 /* Compute a basis for the rows in the linear part of the schedule
2022 * and extend this basis to a full basis. The remaining rows
2023 * can then be used to force linear independence from the rows
2026 * In particular, given the schedule rows S, we compute
2031 * with H the Hermite normal form of S. That is, all but the
2032 * first rank columns of H are zero and so each row in S is
2033 * a linear combination of the first rank rows of Q.
2034 * The matrix Q is then transposed because we will write the
2035 * coefficients of the next schedule row as a column vector s
2036 * and express this s as a linear combination s = Q c of the
2038 * Transposing S U = H yields
2042 * with all but the first rank rows of H^T zero.
2043 * The last rows of U^T are therefore linear combinations
2044 * of schedule coefficients that are all zero on schedule
2045 * coefficients that are linearly dependent on the rows of S.
2046 * At least one of these combinations is non-zero on
2047 * linearly independent schedule coefficients.
2048 * The rows are normalized to involve as few of the last
2049 * coefficients as possible and to have a positive initial value.
2051 static int node_update_cmap(struct isl_sched_node
*node
)
2054 int n_row
= isl_mat_rows(node
->sched
);
2056 H
= isl_mat_sub_alloc(node
->sched
, 0, n_row
,
2057 1 + node
->nparam
, node
->nvar
);
2059 H
= isl_mat_left_hermite(H
, 0, &U
, &Q
);
2060 isl_mat_free(node
->cmap
);
2061 isl_mat_free(node
->indep
);
2062 isl_mat_free(node
->ctrans
);
2063 node
->ctrans
= isl_mat_copy(Q
);
2064 node
->cmap
= isl_mat_transpose(Q
);
2065 node
->indep
= isl_mat_transpose(U
);
2066 node
->rank
= isl_mat_initial_non_zero_cols(H
);
2067 node
->indep
= isl_mat_drop_rows(node
->indep
, 0, node
->rank
);
2068 node
->indep
= normalize_independent(node
->indep
);
2071 if (!node
->cmap
|| !node
->indep
|| !node
->ctrans
|| node
->rank
< 0)
2076 /* Is "edge" marked as a validity or a conditional validity edge?
2078 static int is_any_validity(struct isl_sched_edge
*edge
)
2080 return is_validity(edge
) || is_conditional_validity(edge
);
2083 /* How many times should we count the constraints in "edge"?
2085 * We count as follows
2086 * validity -> 1 (>= 0)
2087 * validity+proximity -> 2 (>= 0 and upper bound)
2088 * proximity -> 2 (lower and upper bound)
2089 * local(+any) -> 2 (>= 0 and <= 0)
2091 * If an edge is only marked conditional_validity then it counts
2092 * as zero since it is only checked afterwards.
2094 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2095 * Otherwise, we ignore them.
2097 static int edge_multiplicity(struct isl_sched_edge
*edge
, int use_coincidence
)
2099 if (is_proximity(edge
) || is_local(edge
))
2101 if (use_coincidence
&& is_coincidence(edge
))
2103 if (is_validity(edge
))
2108 /* Count the number of equality and inequality constraints
2109 * that will be added for the given map.
2111 * "use_coincidence" is set if we should take into account coincidence edges.
2113 static isl_stat
count_map_constraints(struct isl_sched_graph
*graph
,
2114 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
,
2115 int *n_eq
, int *n_ineq
, int use_coincidence
)
2117 isl_basic_set
*coef
;
2118 int f
= edge_multiplicity(edge
, use_coincidence
);
2125 if (edge
->src
== edge
->dst
)
2126 coef
= intra_coefficients(graph
, edge
->src
, map
);
2128 coef
= inter_coefficients(graph
, edge
, map
);
2130 return isl_stat_error
;
2131 *n_eq
+= f
* isl_basic_set_n_equality(coef
);
2132 *n_ineq
+= f
* isl_basic_set_n_inequality(coef
);
2133 isl_basic_set_free(coef
);
2138 /* Count the number of equality and inequality constraints
2139 * that will be added to the main lp problem.
2140 * We count as follows
2141 * validity -> 1 (>= 0)
2142 * validity+proximity -> 2 (>= 0 and upper bound)
2143 * proximity -> 2 (lower and upper bound)
2144 * local(+any) -> 2 (>= 0 and <= 0)
2146 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2147 * Otherwise, we ignore them.
2149 static int count_constraints(struct isl_sched_graph
*graph
,
2150 int *n_eq
, int *n_ineq
, int use_coincidence
)
2154 *n_eq
= *n_ineq
= 0;
2155 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2156 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2157 isl_map
*map
= isl_map_copy(edge
->map
);
2159 if (count_map_constraints(graph
, edge
, map
, n_eq
, n_ineq
,
2160 use_coincidence
) < 0)
2167 /* Count the number of constraints that will be added by
2168 * add_bound_constant_constraints to bound the values of the constant terms
2169 * and increment *n_eq and *n_ineq accordingly.
2171 * In practice, add_bound_constant_constraints only adds inequalities.
2173 static isl_stat
count_bound_constant_constraints(isl_ctx
*ctx
,
2174 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
2176 if (isl_options_get_schedule_max_constant_term(ctx
) == -1)
2179 *n_ineq
+= graph
->n
;
2184 /* Add constraints to bound the values of the constant terms in the schedule,
2185 * if requested by the user.
2187 * The maximal value of the constant terms is defined by the option
2188 * "schedule_max_constant_term".
2190 * Within each node, the coefficients have the following order:
2192 * - c_i_n (if parametric)
2193 * - positive and negative parts of c_i_x
2195 static isl_stat
add_bound_constant_constraints(isl_ctx
*ctx
,
2196 struct isl_sched_graph
*graph
)
2202 max
= isl_options_get_schedule_max_constant_term(ctx
);
2206 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2208 for (i
= 0; i
< graph
->n
; ++i
) {
2209 struct isl_sched_node
*node
= &graph
->node
[i
];
2210 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2212 return isl_stat_error
;
2213 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2214 isl_int_set_si(graph
->lp
->ineq
[k
][1 + node
->start
], -1);
2215 isl_int_set_si(graph
->lp
->ineq
[k
][0], max
);
2221 /* Count the number of constraints that will be added by
2222 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
2225 * In practice, add_bound_coefficient_constraints only adds inequalities.
2227 static int count_bound_coefficient_constraints(isl_ctx
*ctx
,
2228 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
2232 if (isl_options_get_schedule_max_coefficient(ctx
) == -1 &&
2233 !isl_options_get_schedule_treat_coalescing(ctx
))
2236 for (i
= 0; i
< graph
->n
; ++i
)
2237 *n_ineq
+= graph
->node
[i
].nparam
+ 2 * graph
->node
[i
].nvar
;
2242 /* Add constraints to graph->lp that bound the values of
2243 * the parameter schedule coefficients of "node" to "max" and
2244 * the variable schedule coefficients to the corresponding entry
2246 * In either case, a negative value means that no bound needs to be imposed.
2248 * For parameter coefficients, this amounts to adding a constraint
2256 * The variables coefficients are, however, not represented directly.
2257 * Instead, the variable coefficients c_x are written as differences
2258 * c_x = c_x^+ - c_x^-.
2261 * -max_i <= c_x_i <= max_i
2265 * -max_i <= c_x_i^+ - c_x_i^- <= max_i
2269 * -(c_x_i^+ - c_x_i^-) + max_i >= 0
2270 * c_x_i^+ - c_x_i^- + max_i >= 0
2272 static isl_stat
node_add_coefficient_constraints(isl_ctx
*ctx
,
2273 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
, int max
)
2279 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2281 for (j
= 0; j
< node
->nparam
; ++j
) {
2287 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2289 return isl_stat_error
;
2290 dim
= 1 + node
->start
+ 1 + j
;
2291 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2292 isl_int_set_si(graph
->lp
->ineq
[k
][dim
], -1);
2293 isl_int_set_si(graph
->lp
->ineq
[k
][0], max
);
2296 ineq
= isl_vec_alloc(ctx
, 1 + total
);
2297 ineq
= isl_vec_clr(ineq
);
2299 return isl_stat_error
;
2300 for (i
= 0; i
< node
->nvar
; ++i
) {
2301 int pos
= 1 + node_var_coef_pos(node
, i
);
2303 if (isl_int_is_neg(node
->max
->el
[i
]))
2306 isl_int_set_si(ineq
->el
[pos
], 1);
2307 isl_int_set_si(ineq
->el
[pos
+ 1], -1);
2308 isl_int_set(ineq
->el
[0], node
->max
->el
[i
]);
2310 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2313 isl_seq_cpy(graph
->lp
->ineq
[k
], ineq
->el
, 1 + total
);
2315 isl_seq_neg(ineq
->el
+ pos
, ineq
->el
+ pos
+ 2 * i
, 2);
2316 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2319 isl_seq_cpy(graph
->lp
->ineq
[k
], ineq
->el
, 1 + total
);
2326 return isl_stat_error
;
2329 /* Add constraints that bound the values of the variable and parameter
2330 * coefficients of the schedule.
2332 * The maximal value of the coefficients is defined by the option
2333 * 'schedule_max_coefficient' and the entries in node->max.
2334 * These latter entries are only set if either the schedule_max_coefficient
2335 * option or the schedule_treat_coalescing option is set.
2337 static isl_stat
add_bound_coefficient_constraints(isl_ctx
*ctx
,
2338 struct isl_sched_graph
*graph
)
2343 max
= isl_options_get_schedule_max_coefficient(ctx
);
2345 if (max
== -1 && !isl_options_get_schedule_treat_coalescing(ctx
))
2348 for (i
= 0; i
< graph
->n
; ++i
) {
2349 struct isl_sched_node
*node
= &graph
->node
[i
];
2351 if (node_add_coefficient_constraints(ctx
, graph
, node
, max
) < 0)
2352 return isl_stat_error
;
2358 /* Add a constraint to graph->lp that equates the value at position
2359 * "sum_pos" to the sum of the "n" values starting at "first".
2361 static isl_stat
add_sum_constraint(struct isl_sched_graph
*graph
,
2362 int sum_pos
, int first
, int n
)
2367 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2369 k
= isl_basic_set_alloc_equality(graph
->lp
);
2371 return isl_stat_error
;
2372 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2373 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2374 for (i
= 0; i
< n
; ++i
)
2375 isl_int_set_si(graph
->lp
->eq
[k
][1 + first
+ i
], 1);
2380 /* Add a constraint to graph->lp that equates the value at position
2381 * "sum_pos" to the sum of the parameter coefficients of all nodes.
2383 * Within each node, the coefficients have the following order:
2385 * - c_i_n (if parametric)
2386 * - positive and negative parts of c_i_x
2388 static isl_stat
add_param_sum_constraint(struct isl_sched_graph
*graph
,
2394 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2396 k
= isl_basic_set_alloc_equality(graph
->lp
);
2398 return isl_stat_error
;
2399 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2400 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2401 for (i
= 0; i
< graph
->n
; ++i
) {
2402 int pos
= 1 + graph
->node
[i
].start
+ 1;
2404 for (j
= 0; j
< graph
->node
[i
].nparam
; ++j
)
2405 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2411 /* Add a constraint to graph->lp that equates the value at position
2412 * "sum_pos" to the sum of the variable coefficients of all nodes.
2414 * Within each node, the coefficients have the following order:
2416 * - c_i_n (if parametric)
2417 * - positive and negative parts of c_i_x
2419 static isl_stat
add_var_sum_constraint(struct isl_sched_graph
*graph
,
2425 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2427 k
= isl_basic_set_alloc_equality(graph
->lp
);
2429 return isl_stat_error
;
2430 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2431 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2432 for (i
= 0; i
< graph
->n
; ++i
) {
2433 struct isl_sched_node
*node
= &graph
->node
[i
];
2434 int pos
= 1 + node_var_coef_offset(node
);
2436 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
2437 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2443 /* Construct an ILP problem for finding schedule coefficients
2444 * that result in non-negative, but small dependence distances
2445 * over all dependences.
2446 * In particular, the dependence distances over proximity edges
2447 * are bounded by m_0 + m_n n and we compute schedule coefficients
2448 * with small values (preferably zero) of m_n and m_0.
2450 * All variables of the ILP are non-negative. The actual coefficients
2451 * may be negative, so each coefficient is represented as the difference
2452 * of two non-negative variables. The negative part always appears
2453 * immediately before the positive part.
2454 * Other than that, the variables have the following order
2456 * - sum of positive and negative parts of m_n coefficients
2458 * - sum of all c_n coefficients
2459 * (unconstrained when computing non-parametric schedules)
2460 * - sum of positive and negative parts of all c_x coefficients
2461 * - positive and negative parts of m_n coefficients
2464 * - c_i_n (if parametric)
2465 * - positive and negative parts of c_i_x, in opposite order
2467 * The constraints are those from the edges plus two or three equalities
2468 * to express the sums.
2470 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2471 * Otherwise, we ignore them.
2473 static isl_stat
setup_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
2474 int use_coincidence
)
2484 parametric
= ctx
->opt
->schedule_parametric
;
2485 nparam
= isl_space_dim(graph
->node
[0].space
, isl_dim_param
);
2487 total
= param_pos
+ 2 * nparam
;
2488 for (i
= 0; i
< graph
->n
; ++i
) {
2489 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
2490 if (node_update_cmap(node
) < 0)
2491 return isl_stat_error
;
2492 node
->start
= total
;
2493 total
+= 1 + node
->nparam
+ 2 * node
->nvar
;
2496 if (count_constraints(graph
, &n_eq
, &n_ineq
, use_coincidence
) < 0)
2497 return isl_stat_error
;
2498 if (count_bound_constant_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2499 return isl_stat_error
;
2500 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2501 return isl_stat_error
;
2503 space
= isl_space_set_alloc(ctx
, 0, total
);
2504 isl_basic_set_free(graph
->lp
);
2505 n_eq
+= 2 + parametric
;
2507 graph
->lp
= isl_basic_set_alloc_space(space
, 0, n_eq
, n_ineq
);
2509 if (add_sum_constraint(graph
, 0, param_pos
, 2 * nparam
) < 0)
2510 return isl_stat_error
;
2511 if (parametric
&& add_param_sum_constraint(graph
, 2) < 0)
2512 return isl_stat_error
;
2513 if (add_var_sum_constraint(graph
, 3) < 0)
2514 return isl_stat_error
;
2515 if (add_bound_constant_constraints(ctx
, graph
) < 0)
2516 return isl_stat_error
;
2517 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
2518 return isl_stat_error
;
2519 if (add_all_validity_constraints(graph
, use_coincidence
) < 0)
2520 return isl_stat_error
;
2521 if (add_all_proximity_constraints(graph
, use_coincidence
) < 0)
2522 return isl_stat_error
;
2527 /* Analyze the conflicting constraint found by
2528 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2529 * constraint of one of the edges between distinct nodes, living, moreover
2530 * in distinct SCCs, then record the source and sink SCC as this may
2531 * be a good place to cut between SCCs.
2533 static int check_conflict(int con
, void *user
)
2536 struct isl_sched_graph
*graph
= user
;
2538 if (graph
->src_scc
>= 0)
2541 con
-= graph
->lp
->n_eq
;
2543 if (con
>= graph
->lp
->n_ineq
)
2546 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2547 if (!is_validity(&graph
->edge
[i
]))
2549 if (graph
->edge
[i
].src
== graph
->edge
[i
].dst
)
2551 if (graph
->edge
[i
].src
->scc
== graph
->edge
[i
].dst
->scc
)
2553 if (graph
->edge
[i
].start
> con
)
2555 if (graph
->edge
[i
].end
<= con
)
2557 graph
->src_scc
= graph
->edge
[i
].src
->scc
;
2558 graph
->dst_scc
= graph
->edge
[i
].dst
->scc
;
2564 /* Check whether the next schedule row of the given node needs to be
2565 * non-trivial. Lower-dimensional domains may have some trivial rows,
2566 * but as soon as the number of remaining required non-trivial rows
2567 * is as large as the number or remaining rows to be computed,
2568 * all remaining rows need to be non-trivial.
2570 static int needs_row(struct isl_sched_graph
*graph
, struct isl_sched_node
*node
)
2572 return node
->nvar
- node
->rank
>= graph
->maxvar
- graph
->n_row
;
2575 /* Construct a non-triviality region with triviality directions
2576 * corresponding to the rows of "indep".
2577 * The rows of "indep" are expressed in terms of the schedule coefficients c_i,
2578 * while the triviality directions are expressed in terms of
2579 * pairs of non-negative variables c^+_i - c^-_i, with c^-_i appearing
2580 * before c^+_i. Furthermore,
2581 * the pairs of non-negative variables representing the coefficients
2582 * are stored in the opposite order.
2584 static __isl_give isl_mat
*construct_trivial(__isl_keep isl_mat
*indep
)
2593 ctx
= isl_mat_get_ctx(indep
);
2594 n
= isl_mat_rows(indep
);
2595 n_var
= isl_mat_cols(indep
);
2596 mat
= isl_mat_alloc(ctx
, n
, 2 * n_var
);
2599 for (i
= 0; i
< n
; ++i
) {
2600 for (j
= 0; j
< n_var
; ++j
) {
2601 int nj
= n_var
- 1 - j
;
2602 isl_int_neg(mat
->row
[i
][2 * nj
], indep
->row
[i
][j
]);
2603 isl_int_set(mat
->row
[i
][2 * nj
+ 1], indep
->row
[i
][j
]);
2610 /* Solve the ILP problem constructed in setup_lp.
2611 * For each node such that all the remaining rows of its schedule
2612 * need to be non-trivial, we construct a non-triviality region.
2613 * This region imposes that the next row is independent of previous rows.
2614 * In particular, the non-triviality region enforces that at least
2615 * one of the linear combinations in the rows of node->indep is non-zero.
2617 static __isl_give isl_vec
*solve_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2623 for (i
= 0; i
< graph
->n
; ++i
) {
2624 struct isl_sched_node
*node
= &graph
->node
[i
];
2627 graph
->region
[i
].pos
= node_var_coef_offset(node
);
2628 if (needs_row(graph
, node
))
2629 trivial
= construct_trivial(node
->indep
);
2631 trivial
= isl_mat_zero(ctx
, 0, 0);
2632 graph
->region
[i
].trivial
= trivial
;
2634 lp
= isl_basic_set_copy(graph
->lp
);
2635 sol
= isl_tab_basic_set_non_trivial_lexmin(lp
, 2, graph
->n
,
2636 graph
->region
, &check_conflict
, graph
);
2637 for (i
= 0; i
< graph
->n
; ++i
)
2638 isl_mat_free(graph
->region
[i
].trivial
);
2642 /* Extract the coefficients for the variables of "node" from "sol".
2644 * Within each node, the coefficients have the following order:
2646 * - c_i_n (if parametric)
2647 * - positive and negative parts of c_i_x
2649 * The c_i_x^- appear before their c_i_x^+ counterpart.
2650 * Furthermore, the order of these pairs is the opposite of that
2651 * of the corresponding coefficients.
2653 * Return c_i_x = c_i_x^+ - c_i_x^-
2655 static __isl_give isl_vec
*extract_var_coef(struct isl_sched_node
*node
,
2656 __isl_keep isl_vec
*sol
)
2664 csol
= isl_vec_alloc(isl_vec_get_ctx(sol
), node
->nvar
);
2668 pos
= 1 + node_var_coef_offset(node
);
2669 for (i
= 0; i
< node
->nvar
; ++i
)
2670 isl_int_sub(csol
->el
[node
->nvar
- 1 - i
],
2671 sol
->el
[pos
+ 2 * i
+ 1], sol
->el
[pos
+ 2 * i
]);
2676 /* Update the schedules of all nodes based on the given solution
2677 * of the LP problem.
2678 * The new row is added to the current band.
2679 * All possibly negative coefficients are encoded as a difference
2680 * of two non-negative variables, so we need to perform the subtraction
2681 * here. Moreover, if use_cmap is set, then the solution does
2682 * not refer to the actual coefficients c_i_x, but instead to variables
2683 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2684 * In this case, we then also need to perform this multiplication
2685 * to obtain the values of c_i_x.
2687 * If coincident is set, then the caller guarantees that the new
2688 * row satisfies the coincidence constraints.
2690 static int update_schedule(struct isl_sched_graph
*graph
,
2691 __isl_take isl_vec
*sol
, int use_cmap
, int coincident
)
2694 isl_vec
*csol
= NULL
;
2699 isl_die(sol
->ctx
, isl_error_internal
,
2700 "no solution found", goto error
);
2701 if (graph
->n_total_row
>= graph
->max_row
)
2702 isl_die(sol
->ctx
, isl_error_internal
,
2703 "too many schedule rows", goto error
);
2705 for (i
= 0; i
< graph
->n
; ++i
) {
2706 struct isl_sched_node
*node
= &graph
->node
[i
];
2707 int pos
= node
->start
;
2708 int row
= isl_mat_rows(node
->sched
);
2711 csol
= extract_var_coef(node
, sol
);
2715 isl_map_free(node
->sched_map
);
2716 node
->sched_map
= NULL
;
2717 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
2720 for (j
= 0; j
< 1 + node
->nparam
; ++j
)
2721 node
->sched
= isl_mat_set_element(node
->sched
,
2722 row
, j
, sol
->el
[1 + pos
+ j
]);
2724 csol
= isl_mat_vec_product(isl_mat_copy(node
->cmap
),
2728 for (j
= 0; j
< node
->nvar
; ++j
)
2729 node
->sched
= isl_mat_set_element(node
->sched
,
2730 row
, 1 + node
->nparam
+ j
, csol
->el
[j
]);
2731 node
->coincident
[graph
->n_total_row
] = coincident
;
2737 graph
->n_total_row
++;
2746 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2747 * and return this isl_aff.
2749 static __isl_give isl_aff
*extract_schedule_row(__isl_take isl_local_space
*ls
,
2750 struct isl_sched_node
*node
, int row
)
2758 aff
= isl_aff_zero_on_domain(ls
);
2759 isl_mat_get_element(node
->sched
, row
, 0, &v
);
2760 aff
= isl_aff_set_constant(aff
, v
);
2761 for (j
= 0; j
< node
->nparam
; ++j
) {
2762 isl_mat_get_element(node
->sched
, row
, 1 + j
, &v
);
2763 aff
= isl_aff_set_coefficient(aff
, isl_dim_param
, j
, v
);
2765 for (j
= 0; j
< node
->nvar
; ++j
) {
2766 isl_mat_get_element(node
->sched
, row
, 1 + node
->nparam
+ j
, &v
);
2767 aff
= isl_aff_set_coefficient(aff
, isl_dim_in
, j
, v
);
2775 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
2776 * and return this multi_aff.
2778 * The result is defined over the uncompressed node domain.
2780 static __isl_give isl_multi_aff
*node_extract_partial_schedule_multi_aff(
2781 struct isl_sched_node
*node
, int first
, int n
)
2785 isl_local_space
*ls
;
2792 nrow
= isl_mat_rows(node
->sched
);
2793 if (node
->compressed
)
2794 space
= isl_multi_aff_get_domain_space(node
->decompress
);
2796 space
= isl_space_copy(node
->space
);
2797 ls
= isl_local_space_from_space(isl_space_copy(space
));
2798 space
= isl_space_from_domain(space
);
2799 space
= isl_space_add_dims(space
, isl_dim_out
, n
);
2800 ma
= isl_multi_aff_zero(space
);
2802 for (i
= first
; i
< first
+ n
; ++i
) {
2803 aff
= extract_schedule_row(isl_local_space_copy(ls
), node
, i
);
2804 ma
= isl_multi_aff_set_aff(ma
, i
- first
, aff
);
2807 isl_local_space_free(ls
);
2809 if (node
->compressed
)
2810 ma
= isl_multi_aff_pullback_multi_aff(ma
,
2811 isl_multi_aff_copy(node
->compress
));
2816 /* Convert node->sched into a multi_aff and return this multi_aff.
2818 * The result is defined over the uncompressed node domain.
2820 static __isl_give isl_multi_aff
*node_extract_schedule_multi_aff(
2821 struct isl_sched_node
*node
)
2825 nrow
= isl_mat_rows(node
->sched
);
2826 return node_extract_partial_schedule_multi_aff(node
, 0, nrow
);
2829 /* Convert node->sched into a map and return this map.
2831 * The result is cached in node->sched_map, which needs to be released
2832 * whenever node->sched is updated.
2833 * It is defined over the uncompressed node domain.
2835 static __isl_give isl_map
*node_extract_schedule(struct isl_sched_node
*node
)
2837 if (!node
->sched_map
) {
2840 ma
= node_extract_schedule_multi_aff(node
);
2841 node
->sched_map
= isl_map_from_multi_aff(ma
);
2844 return isl_map_copy(node
->sched_map
);
2847 /* Construct a map that can be used to update a dependence relation
2848 * based on the current schedule.
2849 * That is, construct a map expressing that source and sink
2850 * are executed within the same iteration of the current schedule.
2851 * This map can then be intersected with the dependence relation.
2852 * This is not the most efficient way, but this shouldn't be a critical
2855 static __isl_give isl_map
*specializer(struct isl_sched_node
*src
,
2856 struct isl_sched_node
*dst
)
2858 isl_map
*src_sched
, *dst_sched
;
2860 src_sched
= node_extract_schedule(src
);
2861 dst_sched
= node_extract_schedule(dst
);
2862 return isl_map_apply_range(src_sched
, isl_map_reverse(dst_sched
));
2865 /* Intersect the domains of the nested relations in domain and range
2866 * of "umap" with "map".
2868 static __isl_give isl_union_map
*intersect_domains(
2869 __isl_take isl_union_map
*umap
, __isl_keep isl_map
*map
)
2871 isl_union_set
*uset
;
2873 umap
= isl_union_map_zip(umap
);
2874 uset
= isl_union_set_from_set(isl_map_wrap(isl_map_copy(map
)));
2875 umap
= isl_union_map_intersect_domain(umap
, uset
);
2876 umap
= isl_union_map_zip(umap
);
2880 /* Update the dependence relation of the given edge based
2881 * on the current schedule.
2882 * If the dependence is carried completely by the current schedule, then
2883 * it is removed from the edge_tables. It is kept in the list of edges
2884 * as otherwise all edge_tables would have to be recomputed.
2886 static int update_edge(struct isl_sched_graph
*graph
,
2887 struct isl_sched_edge
*edge
)
2892 id
= specializer(edge
->src
, edge
->dst
);
2893 edge
->map
= isl_map_intersect(edge
->map
, isl_map_copy(id
));
2897 if (edge
->tagged_condition
) {
2898 edge
->tagged_condition
=
2899 intersect_domains(edge
->tagged_condition
, id
);
2900 if (!edge
->tagged_condition
)
2903 if (edge
->tagged_validity
) {
2904 edge
->tagged_validity
=
2905 intersect_domains(edge
->tagged_validity
, id
);
2906 if (!edge
->tagged_validity
)
2910 empty
= isl_map_plain_is_empty(edge
->map
);
2914 graph_remove_edge(graph
, edge
);
2923 /* Does the domain of "umap" intersect "uset"?
2925 static int domain_intersects(__isl_keep isl_union_map
*umap
,
2926 __isl_keep isl_union_set
*uset
)
2930 umap
= isl_union_map_copy(umap
);
2931 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(uset
));
2932 empty
= isl_union_map_is_empty(umap
);
2933 isl_union_map_free(umap
);
2935 return empty
< 0 ? -1 : !empty
;
2938 /* Does the range of "umap" intersect "uset"?
2940 static int range_intersects(__isl_keep isl_union_map
*umap
,
2941 __isl_keep isl_union_set
*uset
)
2945 umap
= isl_union_map_copy(umap
);
2946 umap
= isl_union_map_intersect_range(umap
, isl_union_set_copy(uset
));
2947 empty
= isl_union_map_is_empty(umap
);
2948 isl_union_map_free(umap
);
2950 return empty
< 0 ? -1 : !empty
;
2953 /* Are the condition dependences of "edge" local with respect to
2954 * the current schedule?
2956 * That is, are domain and range of the condition dependences mapped
2957 * to the same point?
2959 * In other words, is the condition false?
2961 static int is_condition_false(struct isl_sched_edge
*edge
)
2963 isl_union_map
*umap
;
2964 isl_map
*map
, *sched
, *test
;
2967 empty
= isl_union_map_is_empty(edge
->tagged_condition
);
2968 if (empty
< 0 || empty
)
2971 umap
= isl_union_map_copy(edge
->tagged_condition
);
2972 umap
= isl_union_map_zip(umap
);
2973 umap
= isl_union_set_unwrap(isl_union_map_domain(umap
));
2974 map
= isl_map_from_union_map(umap
);
2976 sched
= node_extract_schedule(edge
->src
);
2977 map
= isl_map_apply_domain(map
, sched
);
2978 sched
= node_extract_schedule(edge
->dst
);
2979 map
= isl_map_apply_range(map
, sched
);
2981 test
= isl_map_identity(isl_map_get_space(map
));
2982 local
= isl_map_is_subset(map
, test
);
2989 /* For each conditional validity constraint that is adjacent
2990 * to a condition with domain in condition_source or range in condition_sink,
2991 * turn it into an unconditional validity constraint.
2993 static int unconditionalize_adjacent_validity(struct isl_sched_graph
*graph
,
2994 __isl_take isl_union_set
*condition_source
,
2995 __isl_take isl_union_set
*condition_sink
)
2999 condition_source
= isl_union_set_coalesce(condition_source
);
3000 condition_sink
= isl_union_set_coalesce(condition_sink
);
3002 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3004 isl_union_map
*validity
;
3006 if (!is_conditional_validity(&graph
->edge
[i
]))
3008 if (is_validity(&graph
->edge
[i
]))
3011 validity
= graph
->edge
[i
].tagged_validity
;
3012 adjacent
= domain_intersects(validity
, condition_sink
);
3013 if (adjacent
>= 0 && !adjacent
)
3014 adjacent
= range_intersects(validity
, condition_source
);
3020 set_validity(&graph
->edge
[i
]);
3023 isl_union_set_free(condition_source
);
3024 isl_union_set_free(condition_sink
);
3027 isl_union_set_free(condition_source
);
3028 isl_union_set_free(condition_sink
);
3032 /* Update the dependence relations of all edges based on the current schedule
3033 * and enforce conditional validity constraints that are adjacent
3034 * to satisfied condition constraints.
3036 * First check if any of the condition constraints are satisfied
3037 * (i.e., not local to the outer schedule) and keep track of
3038 * their domain and range.
3039 * Then update all dependence relations (which removes the non-local
3041 * Finally, if any condition constraints turned out to be satisfied,
3042 * then turn all adjacent conditional validity constraints into
3043 * unconditional validity constraints.
3045 static int update_edges(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3049 isl_union_set
*source
, *sink
;
3051 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3052 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3053 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3055 isl_union_set
*uset
;
3056 isl_union_map
*umap
;
3058 if (!is_condition(&graph
->edge
[i
]))
3060 if (is_local(&graph
->edge
[i
]))
3062 local
= is_condition_false(&graph
->edge
[i
]);
3070 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
3071 uset
= isl_union_map_domain(umap
);
3072 source
= isl_union_set_union(source
, uset
);
3074 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
3075 uset
= isl_union_map_range(umap
);
3076 sink
= isl_union_set_union(sink
, uset
);
3079 for (i
= graph
->n_edge
- 1; i
>= 0; --i
) {
3080 if (update_edge(graph
, &graph
->edge
[i
]) < 0)
3085 return unconditionalize_adjacent_validity(graph
, source
, sink
);
3087 isl_union_set_free(source
);
3088 isl_union_set_free(sink
);
3091 isl_union_set_free(source
);
3092 isl_union_set_free(sink
);
3096 static void next_band(struct isl_sched_graph
*graph
)
3098 graph
->band_start
= graph
->n_total_row
;
3101 /* Return the union of the universe domains of the nodes in "graph"
3102 * that satisfy "pred".
3104 static __isl_give isl_union_set
*isl_sched_graph_domain(isl_ctx
*ctx
,
3105 struct isl_sched_graph
*graph
,
3106 int (*pred
)(struct isl_sched_node
*node
, int data
), int data
)
3112 for (i
= 0; i
< graph
->n
; ++i
)
3113 if (pred(&graph
->node
[i
], data
))
3117 isl_die(ctx
, isl_error_internal
,
3118 "empty component", return NULL
);
3120 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
3121 dom
= isl_union_set_from_set(set
);
3123 for (i
= i
+ 1; i
< graph
->n
; ++i
) {
3124 if (!pred(&graph
->node
[i
], data
))
3126 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
3127 dom
= isl_union_set_union(dom
, isl_union_set_from_set(set
));
3133 /* Return a list of unions of universe domains, where each element
3134 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
3136 static __isl_give isl_union_set_list
*extract_sccs(isl_ctx
*ctx
,
3137 struct isl_sched_graph
*graph
)
3140 isl_union_set_list
*filters
;
3142 filters
= isl_union_set_list_alloc(ctx
, graph
->scc
);
3143 for (i
= 0; i
< graph
->scc
; ++i
) {
3146 dom
= isl_sched_graph_domain(ctx
, graph
, &node_scc_exactly
, i
);
3147 filters
= isl_union_set_list_add(filters
, dom
);
3153 /* Return a list of two unions of universe domains, one for the SCCs up
3154 * to and including graph->src_scc and another for the other SCCs.
3156 static __isl_give isl_union_set_list
*extract_split(isl_ctx
*ctx
,
3157 struct isl_sched_graph
*graph
)
3160 isl_union_set_list
*filters
;
3162 filters
= isl_union_set_list_alloc(ctx
, 2);
3163 dom
= isl_sched_graph_domain(ctx
, graph
,
3164 &node_scc_at_most
, graph
->src_scc
);
3165 filters
= isl_union_set_list_add(filters
, dom
);
3166 dom
= isl_sched_graph_domain(ctx
, graph
,
3167 &node_scc_at_least
, graph
->src_scc
+ 1);
3168 filters
= isl_union_set_list_add(filters
, dom
);
3173 /* Copy nodes that satisfy node_pred from the src dependence graph
3174 * to the dst dependence graph.
3176 static int copy_nodes(struct isl_sched_graph
*dst
, struct isl_sched_graph
*src
,
3177 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
3182 for (i
= 0; i
< src
->n
; ++i
) {
3185 if (!node_pred(&src
->node
[i
], data
))
3189 dst
->node
[j
].space
= isl_space_copy(src
->node
[i
].space
);
3190 dst
->node
[j
].compressed
= src
->node
[i
].compressed
;
3191 dst
->node
[j
].hull
= isl_set_copy(src
->node
[i
].hull
);
3192 dst
->node
[j
].compress
=
3193 isl_multi_aff_copy(src
->node
[i
].compress
);
3194 dst
->node
[j
].decompress
=
3195 isl_multi_aff_copy(src
->node
[i
].decompress
);
3196 dst
->node
[j
].nvar
= src
->node
[i
].nvar
;
3197 dst
->node
[j
].nparam
= src
->node
[i
].nparam
;
3198 dst
->node
[j
].sched
= isl_mat_copy(src
->node
[i
].sched
);
3199 dst
->node
[j
].sched_map
= isl_map_copy(src
->node
[i
].sched_map
);
3200 dst
->node
[j
].coincident
= src
->node
[i
].coincident
;
3201 dst
->node
[j
].sizes
= isl_multi_val_copy(src
->node
[i
].sizes
);
3202 dst
->node
[j
].max
= isl_vec_copy(src
->node
[i
].max
);
3205 if (!dst
->node
[j
].space
|| !dst
->node
[j
].sched
)
3207 if (dst
->node
[j
].compressed
&&
3208 (!dst
->node
[j
].hull
|| !dst
->node
[j
].compress
||
3209 !dst
->node
[j
].decompress
))
3216 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
3217 * to the dst dependence graph.
3218 * If the source or destination node of the edge is not in the destination
3219 * graph, then it must be a backward proximity edge and it should simply
3222 static int copy_edges(isl_ctx
*ctx
, struct isl_sched_graph
*dst
,
3223 struct isl_sched_graph
*src
,
3224 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
), int data
)
3227 enum isl_edge_type t
;
3230 for (i
= 0; i
< src
->n_edge
; ++i
) {
3231 struct isl_sched_edge
*edge
= &src
->edge
[i
];
3233 isl_union_map
*tagged_condition
;
3234 isl_union_map
*tagged_validity
;
3235 struct isl_sched_node
*dst_src
, *dst_dst
;
3237 if (!edge_pred(edge
, data
))
3240 if (isl_map_plain_is_empty(edge
->map
))
3243 dst_src
= graph_find_node(ctx
, dst
, edge
->src
->space
);
3244 dst_dst
= graph_find_node(ctx
, dst
, edge
->dst
->space
);
3245 if (!dst_src
|| !dst_dst
) {
3246 if (is_validity(edge
) || is_conditional_validity(edge
))
3247 isl_die(ctx
, isl_error_internal
,
3248 "backward (conditional) validity edge",
3253 map
= isl_map_copy(edge
->map
);
3254 tagged_condition
= isl_union_map_copy(edge
->tagged_condition
);
3255 tagged_validity
= isl_union_map_copy(edge
->tagged_validity
);
3257 dst
->edge
[dst
->n_edge
].src
= dst_src
;
3258 dst
->edge
[dst
->n_edge
].dst
= dst_dst
;
3259 dst
->edge
[dst
->n_edge
].map
= map
;
3260 dst
->edge
[dst
->n_edge
].tagged_condition
= tagged_condition
;
3261 dst
->edge
[dst
->n_edge
].tagged_validity
= tagged_validity
;
3262 dst
->edge
[dst
->n_edge
].types
= edge
->types
;
3265 if (edge
->tagged_condition
&& !tagged_condition
)
3267 if (edge
->tagged_validity
&& !tagged_validity
)
3270 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
3272 graph_find_edge(src
, t
, edge
->src
, edge
->dst
))
3274 if (graph_edge_table_add(ctx
, dst
, t
,
3275 &dst
->edge
[dst
->n_edge
- 1]) < 0)
3283 /* Compute the maximal number of variables over all nodes.
3284 * This is the maximal number of linearly independent schedule
3285 * rows that we need to compute.
3286 * Just in case we end up in a part of the dependence graph
3287 * with only lower-dimensional domains, we make sure we will
3288 * compute the required amount of extra linearly independent rows.
3290 static int compute_maxvar(struct isl_sched_graph
*graph
)
3295 for (i
= 0; i
< graph
->n
; ++i
) {
3296 struct isl_sched_node
*node
= &graph
->node
[i
];
3299 if (node_update_cmap(node
) < 0)
3301 nvar
= node
->nvar
+ graph
->n_row
- node
->rank
;
3302 if (nvar
> graph
->maxvar
)
3303 graph
->maxvar
= nvar
;
3309 /* Extract the subgraph of "graph" that consists of the node satisfying
3310 * "node_pred" and the edges satisfying "edge_pred" and store
3311 * the result in "sub".
3313 static int extract_sub_graph(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
3314 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
3315 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
3316 int data
, struct isl_sched_graph
*sub
)
3318 int i
, n
= 0, n_edge
= 0;
3321 for (i
= 0; i
< graph
->n
; ++i
)
3322 if (node_pred(&graph
->node
[i
], data
))
3324 for (i
= 0; i
< graph
->n_edge
; ++i
)
3325 if (edge_pred(&graph
->edge
[i
], data
))
3327 if (graph_alloc(ctx
, sub
, n
, n_edge
) < 0)
3329 if (copy_nodes(sub
, graph
, node_pred
, data
) < 0)
3331 if (graph_init_table(ctx
, sub
) < 0)
3333 for (t
= 0; t
<= isl_edge_last
; ++t
)
3334 sub
->max_edge
[t
] = graph
->max_edge
[t
];
3335 if (graph_init_edge_tables(ctx
, sub
) < 0)
3337 if (copy_edges(ctx
, sub
, graph
, edge_pred
, data
) < 0)
3339 sub
->n_row
= graph
->n_row
;
3340 sub
->max_row
= graph
->max_row
;
3341 sub
->n_total_row
= graph
->n_total_row
;
3342 sub
->band_start
= graph
->band_start
;
3347 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
3348 struct isl_sched_graph
*graph
);
3349 static __isl_give isl_schedule_node
*compute_schedule_wcc(
3350 isl_schedule_node
*node
, struct isl_sched_graph
*graph
);
3352 /* Compute a schedule for a subgraph of "graph". In particular, for
3353 * the graph composed of nodes that satisfy node_pred and edges that
3354 * that satisfy edge_pred.
3355 * If the subgraph is known to consist of a single component, then wcc should
3356 * be set and then we call compute_schedule_wcc on the constructed subgraph.
3357 * Otherwise, we call compute_schedule, which will check whether the subgraph
3360 * The schedule is inserted at "node" and the updated schedule node
3363 static __isl_give isl_schedule_node
*compute_sub_schedule(
3364 __isl_take isl_schedule_node
*node
, isl_ctx
*ctx
,
3365 struct isl_sched_graph
*graph
,
3366 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
3367 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
3370 struct isl_sched_graph split
= { 0 };
3372 if (extract_sub_graph(ctx
, graph
, node_pred
, edge_pred
, data
,
3377 node
= compute_schedule_wcc(node
, &split
);
3379 node
= compute_schedule(node
, &split
);
3381 graph_free(ctx
, &split
);
3384 graph_free(ctx
, &split
);
3385 return isl_schedule_node_free(node
);
3388 static int edge_scc_exactly(struct isl_sched_edge
*edge
, int scc
)
3390 return edge
->src
->scc
== scc
&& edge
->dst
->scc
== scc
;
3393 static int edge_dst_scc_at_most(struct isl_sched_edge
*edge
, int scc
)
3395 return edge
->dst
->scc
<= scc
;
3398 static int edge_src_scc_at_least(struct isl_sched_edge
*edge
, int scc
)
3400 return edge
->src
->scc
>= scc
;
3403 /* Reset the current band by dropping all its schedule rows.
3405 static int reset_band(struct isl_sched_graph
*graph
)
3410 drop
= graph
->n_total_row
- graph
->band_start
;
3411 graph
->n_total_row
-= drop
;
3412 graph
->n_row
-= drop
;
3414 for (i
= 0; i
< graph
->n
; ++i
) {
3415 struct isl_sched_node
*node
= &graph
->node
[i
];
3417 isl_map_free(node
->sched_map
);
3418 node
->sched_map
= NULL
;
3420 node
->sched
= isl_mat_drop_rows(node
->sched
,
3421 graph
->band_start
, drop
);
3430 /* Split the current graph into two parts and compute a schedule for each
3431 * part individually. In particular, one part consists of all SCCs up
3432 * to and including graph->src_scc, while the other part contains the other
3433 * SCCs. The split is enforced by a sequence node inserted at position "node"
3434 * in the schedule tree. Return the updated schedule node.
3435 * If either of these two parts consists of a sequence, then it is spliced
3436 * into the sequence containing the two parts.
3438 * The current band is reset. It would be possible to reuse
3439 * the previously computed rows as the first rows in the next
3440 * band, but recomputing them may result in better rows as we are looking
3441 * at a smaller part of the dependence graph.
3443 static __isl_give isl_schedule_node
*compute_split_schedule(
3444 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3448 isl_union_set_list
*filters
;
3453 if (reset_band(graph
) < 0)
3454 return isl_schedule_node_free(node
);
3458 ctx
= isl_schedule_node_get_ctx(node
);
3459 filters
= extract_split(ctx
, graph
);
3460 node
= isl_schedule_node_insert_sequence(node
, filters
);
3461 node
= isl_schedule_node_child(node
, 1);
3462 node
= isl_schedule_node_child(node
, 0);
3464 node
= compute_sub_schedule(node
, ctx
, graph
,
3465 &node_scc_at_least
, &edge_src_scc_at_least
,
3466 graph
->src_scc
+ 1, 0);
3467 is_seq
= isl_schedule_node_get_type(node
) == isl_schedule_node_sequence
;
3468 node
= isl_schedule_node_parent(node
);
3469 node
= isl_schedule_node_parent(node
);
3471 node
= isl_schedule_node_sequence_splice_child(node
, 1);
3472 node
= isl_schedule_node_child(node
, 0);
3473 node
= isl_schedule_node_child(node
, 0);
3474 node
= compute_sub_schedule(node
, ctx
, graph
,
3475 &node_scc_at_most
, &edge_dst_scc_at_most
,
3477 is_seq
= isl_schedule_node_get_type(node
) == isl_schedule_node_sequence
;
3478 node
= isl_schedule_node_parent(node
);
3479 node
= isl_schedule_node_parent(node
);
3481 node
= isl_schedule_node_sequence_splice_child(node
, 0);
3486 /* Insert a band node at position "node" in the schedule tree corresponding
3487 * to the current band in "graph". Mark the band node permutable
3488 * if "permutable" is set.
3489 * The partial schedules and the coincidence property are extracted
3490 * from the graph nodes.
3491 * Return the updated schedule node.
3493 static __isl_give isl_schedule_node
*insert_current_band(
3494 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
3500 isl_multi_pw_aff
*mpa
;
3501 isl_multi_union_pw_aff
*mupa
;
3507 isl_die(isl_schedule_node_get_ctx(node
), isl_error_internal
,
3508 "graph should have at least one node",
3509 return isl_schedule_node_free(node
));
3511 start
= graph
->band_start
;
3512 end
= graph
->n_total_row
;
3515 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[0], start
, n
);
3516 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3517 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3519 for (i
= 1; i
< graph
->n
; ++i
) {
3520 isl_multi_union_pw_aff
*mupa_i
;
3522 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[i
],
3524 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3525 mupa_i
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3526 mupa
= isl_multi_union_pw_aff_union_add(mupa
, mupa_i
);
3528 node
= isl_schedule_node_insert_partial_schedule(node
, mupa
);
3530 for (i
= 0; i
< n
; ++i
)
3531 node
= isl_schedule_node_band_member_set_coincident(node
, i
,
3532 graph
->node
[0].coincident
[start
+ i
]);
3533 node
= isl_schedule_node_band_set_permutable(node
, permutable
);
3538 /* Update the dependence relations based on the current schedule,
3539 * add the current band to "node" and then continue with the computation
3541 * Return the updated schedule node.
3543 static __isl_give isl_schedule_node
*compute_next_band(
3544 __isl_take isl_schedule_node
*node
,
3545 struct isl_sched_graph
*graph
, int permutable
)
3552 ctx
= isl_schedule_node_get_ctx(node
);
3553 if (update_edges(ctx
, graph
) < 0)
3554 return isl_schedule_node_free(node
);
3555 node
= insert_current_band(node
, graph
, permutable
);
3558 node
= isl_schedule_node_child(node
, 0);
3559 node
= compute_schedule(node
, graph
);
3560 node
= isl_schedule_node_parent(node
);
3565 /* Add the constraints "coef" derived from an edge from "node" to itself
3566 * to graph->lp in order to respect the dependences and to try and carry them.
3567 * "pos" is the sequence number of the edge that needs to be carried.
3568 * "coef" represents general constraints on coefficients (c_0, c_n, c_x)
3569 * of valid constraints for (y - x) with x and y instances of the node.
3571 * The constraints added to graph->lp need to enforce
3573 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3574 * = c_j_x (y - x) >= e_i
3576 * for each (x,y) in the dependence relation of the edge.
3577 * That is, (-e_i, 0, c_j_x) needs to be plugged in for (c_0, c_n, c_x),
3578 * taking into account that each coefficient in c_j_x is represented
3579 * as a pair of non-negative coefficients.
3581 static isl_stat
add_intra_constraints(struct isl_sched_graph
*graph
,
3582 struct isl_sched_node
*node
, __isl_take isl_basic_set
*coef
, int pos
)
3586 isl_dim_map
*dim_map
;
3589 return isl_stat_error
;
3591 ctx
= isl_basic_set_get_ctx(coef
);
3592 offset
= coef_var_offset(coef
);
3593 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, 1);
3594 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3595 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
3600 /* Add the constraints "coef" derived from an edge from "src" to "dst"
3601 * to graph->lp in order to respect the dependences and to try and carry them.
3602 * "pos" is the sequence number of the edge that needs to be carried.
3603 * "coef" represents general constraints on coefficients (c_0, c_n, c_x, c_y)
3604 * of valid constraints for (x, y) with x and y instances of "src" and "dst".
3606 * The constraints added to graph->lp need to enforce
3608 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3610 * for each (x,y) in the dependence relation of the edge.
3612 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, -c_j_x, c_k_x)
3613 * needs to be plugged in for (c_0, c_n, c_x, c_y),
3614 * taking into account that each coefficient in c_j_x and c_k_x is represented
3615 * as a pair of non-negative coefficients.
3617 static isl_stat
add_inter_constraints(struct isl_sched_graph
*graph
,
3618 struct isl_sched_node
*src
, struct isl_sched_node
*dst
,
3619 __isl_take isl_basic_set
*coef
, int pos
)
3623 isl_dim_map
*dim_map
;
3626 return isl_stat_error
;
3628 ctx
= isl_basic_set_get_ctx(coef
);
3629 offset
= coef_var_offset(coef
);
3630 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, 1);
3631 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3632 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
3637 /* Data structure collecting information used during the construction
3638 * of an LP for carrying dependences.
3640 * "intra" is a sequence of coefficient constraints for intra-node edges.
3641 * "inter" is a sequence of coefficient constraints for inter-node edges.
3644 isl_basic_set_list
*intra
;
3645 isl_basic_set_list
*inter
;
3648 /* Free all the data stored in "carry".
3650 static void isl_carry_clear(struct isl_carry
*carry
)
3652 isl_basic_set_list_free(carry
->intra
);
3653 isl_basic_set_list_free(carry
->inter
);
3656 /* Return a pointer to the node in "graph" that lives in "space".
3657 * If the requested node has been compressed, then "space"
3658 * corresponds to the compressed space.
3660 * First try and see if "space" is the space of an uncompressed node.
3661 * If so, return that node.
3662 * Otherwise, "space" was constructed by construct_compressed_id and
3663 * contains a user pointer pointing to the node in the tuple id.
3665 static struct isl_sched_node
*graph_find_compressed_node(isl_ctx
*ctx
,
3666 struct isl_sched_graph
*graph
, __isl_keep isl_space
*space
)
3669 struct isl_sched_node
*node
;
3674 node
= graph_find_node(ctx
, graph
, space
);
3678 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
3679 node
= isl_id_get_user(id
);
3685 if (!(node
>= &graph
->node
[0] && node
< &graph
->node
[graph
->n
]))
3686 isl_die(ctx
, isl_error_internal
,
3687 "space points to invalid node", return NULL
);
3692 /* Internal data structure for add_all_constraints.
3694 * "graph" is the schedule constraint graph for which an LP problem
3695 * is being constructed.
3696 * "pos" is the position of the next edge that needs to be carried.
3698 struct isl_add_all_constraints_data
{
3700 struct isl_sched_graph
*graph
;
3704 /* Add the constraints "coef" derived from an edge from a node to itself
3705 * to data->graph->lp in order to respect the dependences and
3706 * to try and carry them.
3708 * The space of "coef" is of the form
3710 * coefficients[[c_cst, c_n] -> S[c_x]]
3712 * with S[c_x] the (compressed) space of the node.
3713 * Extract the node from the space and call add_intra_constraints.
3715 static isl_stat
lp_add_intra(__isl_take isl_basic_set
*coef
, void *user
)
3717 struct isl_add_all_constraints_data
*data
= user
;
3719 struct isl_sched_node
*node
;
3721 space
= isl_basic_set_get_space(coef
);
3722 space
= isl_space_range(isl_space_unwrap(space
));
3723 node
= graph_find_compressed_node(data
->ctx
, data
->graph
, space
);
3724 isl_space_free(space
);
3725 return add_intra_constraints(data
->graph
, node
, coef
, data
->pos
++);
3728 /* Add the constraints "coef" derived from an edge from a node j
3729 * to a node k to data->graph->lp in order to respect the dependences and
3730 * to try and carry them.
3732 * The space of "coef" is of the form
3734 * coefficients[[c_cst, c_n] -> [S_j[c_x] -> S_k[c_y]]]
3736 * with S_j[c_x] and S_k[c_y] the (compressed) spaces of the nodes.
3737 * Extract the nodes from the space and call add_inter_constraints.
3739 static isl_stat
lp_add_inter(__isl_take isl_basic_set
*coef
, void *user
)
3741 struct isl_add_all_constraints_data
*data
= user
;
3742 isl_space
*space
, *dom
;
3743 struct isl_sched_node
*src
, *dst
;
3745 space
= isl_basic_set_get_space(coef
);
3746 space
= isl_space_unwrap(isl_space_range(isl_space_unwrap(space
)));
3747 dom
= isl_space_domain(isl_space_copy(space
));
3748 src
= graph_find_compressed_node(data
->ctx
, data
->graph
, dom
);
3749 isl_space_free(dom
);
3750 space
= isl_space_range(space
);
3751 dst
= graph_find_compressed_node(data
->ctx
, data
->graph
, space
);
3752 isl_space_free(space
);
3754 return add_inter_constraints(data
->graph
, src
, dst
, coef
, data
->pos
++);
3757 /* Add constraints to graph->lp that force all (conditional) validity
3758 * dependences to be respected and attempt to carry them.
3759 * "intra" is the sequence of coefficient constraints for intra-node edges.
3760 * "inter" is the sequence of coefficient constraints for inter-node edges.
3762 static isl_stat
add_all_constraints(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
3763 __isl_keep isl_basic_set_list
*intra
,
3764 __isl_keep isl_basic_set_list
*inter
)
3766 struct isl_add_all_constraints_data data
= { ctx
, graph
};
3769 if (isl_basic_set_list_foreach(intra
, &lp_add_intra
, &data
) < 0)
3770 return isl_stat_error
;
3771 if (isl_basic_set_list_foreach(inter
, &lp_add_inter
, &data
) < 0)
3772 return isl_stat_error
;
3776 /* Internal data structure for count_all_constraints
3777 * for keeping track of the number of equality and inequality constraints.
3779 struct isl_sched_count
{
3784 /* Add the number of equality and inequality constraints of "bset"
3785 * to data->n_eq and data->n_ineq.
3787 static isl_stat
bset_update_count(__isl_take isl_basic_set
*bset
, void *user
)
3789 struct isl_sched_count
*data
= user
;
3791 data
->n_eq
+= isl_basic_set_n_equality(bset
);
3792 data
->n_ineq
+= isl_basic_set_n_inequality(bset
);
3793 isl_basic_set_free(bset
);
3798 /* Count the number of equality and inequality constraints
3799 * that will be added to the carry_lp problem.
3800 * We count each edge exactly once.
3801 * "intra" is the sequence of coefficient constraints for intra-node edges.
3802 * "inter" is the sequence of coefficient constraints for inter-node edges.
3804 static isl_stat
count_all_constraints(__isl_keep isl_basic_set_list
*intra
,
3805 __isl_keep isl_basic_set_list
*inter
, int *n_eq
, int *n_ineq
)
3807 struct isl_sched_count data
;
3809 data
.n_eq
= data
.n_ineq
= 0;
3810 if (isl_basic_set_list_foreach(inter
, &bset_update_count
, &data
) < 0)
3811 return isl_stat_error
;
3812 if (isl_basic_set_list_foreach(intra
, &bset_update_count
, &data
) < 0)
3813 return isl_stat_error
;
3816 *n_ineq
= data
.n_ineq
;
3821 /* Construct an LP problem for finding schedule coefficients
3822 * such that the schedule carries as many validity dependences as possible.
3823 * In particular, for each dependence i, we bound the dependence distance
3824 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3825 * of all e_i's. Dependences with e_i = 0 in the solution are simply
3826 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3827 * "intra" is the sequence of coefficient constraints for intra-node edges.
3828 * "inter" is the sequence of coefficient constraints for inter-node edges.
3829 * "n_edge" is the total number of edges.
3831 * All variables of the LP are non-negative. The actual coefficients
3832 * may be negative, so each coefficient is represented as the difference
3833 * of two non-negative variables. The negative part always appears
3834 * immediately before the positive part.
3835 * Other than that, the variables have the following order
3837 * - sum of (1 - e_i) over all edges
3838 * - sum of all c_n coefficients
3839 * (unconstrained when computing non-parametric schedules)
3840 * - sum of positive and negative parts of all c_x coefficients
3845 * - c_i_n (if parametric)
3846 * - positive and negative parts of c_i_x, in opposite order
3848 * The constraints are those from the (validity) edges plus three equalities
3849 * to express the sums and n_edge inequalities to express e_i <= 1.
3851 static isl_stat
setup_carry_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
3852 int n_edge
, __isl_keep isl_basic_set_list
*intra
,
3853 __isl_keep isl_basic_set_list
*inter
)
3862 for (i
= 0; i
< graph
->n
; ++i
) {
3863 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
3864 node
->start
= total
;
3865 total
+= 1 + node
->nparam
+ 2 * node
->nvar
;
3868 if (count_all_constraints(intra
, inter
, &n_eq
, &n_ineq
) < 0)
3869 return isl_stat_error
;
3871 dim
= isl_space_set_alloc(ctx
, 0, total
);
3872 isl_basic_set_free(graph
->lp
);
3875 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
3876 graph
->lp
= isl_basic_set_set_rational(graph
->lp
);
3878 k
= isl_basic_set_alloc_equality(graph
->lp
);
3880 return isl_stat_error
;
3881 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
3882 isl_int_set_si(graph
->lp
->eq
[k
][0], -n_edge
);
3883 isl_int_set_si(graph
->lp
->eq
[k
][1], 1);
3884 for (i
= 0; i
< n_edge
; ++i
)
3885 isl_int_set_si(graph
->lp
->eq
[k
][4 + i
], 1);
3887 if (add_param_sum_constraint(graph
, 1) < 0)
3888 return isl_stat_error
;
3889 if (add_var_sum_constraint(graph
, 2) < 0)
3890 return isl_stat_error
;
3892 for (i
= 0; i
< n_edge
; ++i
) {
3893 k
= isl_basic_set_alloc_inequality(graph
->lp
);
3895 return isl_stat_error
;
3896 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
3897 isl_int_set_si(graph
->lp
->ineq
[k
][4 + i
], -1);
3898 isl_int_set_si(graph
->lp
->ineq
[k
][0], 1);
3901 if (add_all_constraints(ctx
, graph
, intra
, inter
) < 0)
3902 return isl_stat_error
;
3907 static __isl_give isl_schedule_node
*compute_component_schedule(
3908 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
3911 /* Comparison function for sorting the statements based on
3912 * the corresponding value in "r".
3914 static int smaller_value(const void *a
, const void *b
, void *data
)
3920 return isl_int_cmp(r
->el
[*i1
], r
->el
[*i2
]);
3923 /* If the schedule_split_scaled option is set and if the linear
3924 * parts of the scheduling rows for all nodes in the graphs have
3925 * a non-trivial common divisor, then split off the remainder of the
3926 * constant term modulo this common divisor from the linear part.
3927 * Otherwise, insert a band node directly and continue with
3928 * the construction of the schedule.
3930 * If a non-trivial common divisor is found, then
3931 * the linear part is reduced and the remainder is enforced
3932 * by a sequence node with the children placed in the order
3933 * of this remainder.
3934 * In particular, we assign an scc index based on the remainder and
3935 * then rely on compute_component_schedule to insert the sequence and
3936 * to continue the schedule construction on each part.
3938 static __isl_give isl_schedule_node
*split_scaled(
3939 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3952 ctx
= isl_schedule_node_get_ctx(node
);
3953 if (!ctx
->opt
->schedule_split_scaled
)
3954 return compute_next_band(node
, graph
, 0);
3956 return compute_next_band(node
, graph
, 0);
3959 isl_int_init(gcd_i
);
3961 isl_int_set_si(gcd
, 0);
3963 row
= isl_mat_rows(graph
->node
[0].sched
) - 1;
3965 for (i
= 0; i
< graph
->n
; ++i
) {
3966 struct isl_sched_node
*node
= &graph
->node
[i
];
3967 int cols
= isl_mat_cols(node
->sched
);
3969 isl_seq_gcd(node
->sched
->row
[row
] + 1, cols
- 1, &gcd_i
);
3970 isl_int_gcd(gcd
, gcd
, gcd_i
);
3973 isl_int_clear(gcd_i
);
3975 if (isl_int_cmp_si(gcd
, 1) <= 0) {
3977 return compute_next_band(node
, graph
, 0);
3980 r
= isl_vec_alloc(ctx
, graph
->n
);
3981 order
= isl_calloc_array(ctx
, int, graph
->n
);
3985 for (i
= 0; i
< graph
->n
; ++i
) {
3986 struct isl_sched_node
*node
= &graph
->node
[i
];
3989 isl_int_fdiv_r(r
->el
[i
], node
->sched
->row
[row
][0], gcd
);
3990 isl_int_fdiv_q(node
->sched
->row
[row
][0],
3991 node
->sched
->row
[row
][0], gcd
);
3992 isl_int_mul(node
->sched
->row
[row
][0],
3993 node
->sched
->row
[row
][0], gcd
);
3994 node
->sched
= isl_mat_scale_down_row(node
->sched
, row
, gcd
);
3999 if (isl_sort(order
, graph
->n
, sizeof(order
[0]), &smaller_value
, r
) < 0)
4003 for (i
= 0; i
< graph
->n
; ++i
) {
4004 if (i
> 0 && isl_int_ne(r
->el
[order
[i
- 1]], r
->el
[order
[i
]]))
4006 graph
->node
[order
[i
]].scc
= scc
;
4015 if (update_edges(ctx
, graph
) < 0)
4016 return isl_schedule_node_free(node
);
4017 node
= insert_current_band(node
, graph
, 0);
4020 node
= isl_schedule_node_child(node
, 0);
4021 node
= compute_component_schedule(node
, graph
, 0);
4022 node
= isl_schedule_node_parent(node
);
4029 return isl_schedule_node_free(node
);
4032 /* Is the schedule row "sol" trivial on node "node"?
4033 * That is, is the solution zero on the dimensions linearly independent of
4034 * the previously found solutions?
4035 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
4037 * Each coefficient is represented as the difference between
4038 * two non-negative values in "sol".
4039 * We construct the schedule row s and check if it is linearly
4040 * independent of previously computed schedule rows
4041 * by computing T s, with T the linear combinations that are zero
4042 * on linearly dependent schedule rows.
4043 * If the result consists of all zeros, then the solution is trivial.
4045 static int is_trivial(struct isl_sched_node
*node
, __isl_keep isl_vec
*sol
)
4052 if (node
->nvar
== node
->rank
)
4055 node_sol
= extract_var_coef(node
, sol
);
4056 node_sol
= isl_mat_vec_product(isl_mat_copy(node
->indep
), node_sol
);
4060 trivial
= isl_seq_first_non_zero(node_sol
->el
,
4061 node
->nvar
- node
->rank
) == -1;
4063 isl_vec_free(node_sol
);
4068 /* Is the schedule row "sol" trivial on any node where it should
4070 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
4072 static int is_any_trivial(struct isl_sched_graph
*graph
,
4073 __isl_keep isl_vec
*sol
)
4077 for (i
= 0; i
< graph
->n
; ++i
) {
4078 struct isl_sched_node
*node
= &graph
->node
[i
];
4081 if (!needs_row(graph
, node
))
4083 trivial
= is_trivial(node
, sol
);
4084 if (trivial
< 0 || trivial
)
4091 /* Does the schedule represented by "sol" perform loop coalescing on "node"?
4092 * If so, return the position of the coalesced dimension.
4093 * Otherwise, return node->nvar or -1 on error.
4095 * In particular, look for pairs of coefficients c_i and c_j such that
4096 * |c_j/c_i| >= size_i, i.e., |c_j| >= |c_i * size_i|.
4097 * If any such pair is found, then return i.
4098 * If size_i is infinity, then no check on c_i needs to be performed.
4100 static int find_node_coalescing(struct isl_sched_node
*node
,
4101 __isl_keep isl_vec
*sol
)
4107 if (node
->nvar
<= 1)
4110 csol
= extract_var_coef(node
, sol
);
4114 for (i
= 0; i
< node
->nvar
; ++i
) {
4117 if (isl_int_is_zero(csol
->el
[i
]))
4119 v
= isl_multi_val_get_val(node
->sizes
, i
);
4122 if (!isl_val_is_int(v
)) {
4126 isl_int_mul(max
, v
->n
, csol
->el
[i
]);
4129 for (j
= 0; j
< node
->nvar
; ++j
) {
4132 if (isl_int_abs_ge(csol
->el
[j
], max
))
4148 /* Force the schedule coefficient at position "pos" of "node" to be zero
4150 * The coefficient is encoded as the difference between two non-negative
4151 * variables. Force these two variables to have the same value.
4153 static __isl_give isl_tab_lexmin
*zero_out_node_coef(
4154 __isl_take isl_tab_lexmin
*tl
, struct isl_sched_node
*node
, int pos
)
4160 ctx
= isl_space_get_ctx(node
->space
);
4161 dim
= isl_tab_lexmin_dim(tl
);
4163 return isl_tab_lexmin_free(tl
);
4164 eq
= isl_vec_alloc(ctx
, 1 + dim
);
4165 eq
= isl_vec_clr(eq
);
4167 return isl_tab_lexmin_free(tl
);
4169 pos
= 1 + node_var_coef_pos(node
, pos
);
4170 isl_int_set_si(eq
->el
[pos
], 1);
4171 isl_int_set_si(eq
->el
[pos
+ 1], -1);
4172 tl
= isl_tab_lexmin_add_eq(tl
, eq
->el
);
4178 /* Return the lexicographically smallest rational point in the basic set
4179 * from which "tl" was constructed, double checking that this input set
4182 static __isl_give isl_vec
*non_empty_solution(__isl_keep isl_tab_lexmin
*tl
)
4186 sol
= isl_tab_lexmin_get_solution(tl
);
4190 isl_die(isl_vec_get_ctx(sol
), isl_error_internal
,
4191 "error in schedule construction",
4192 return isl_vec_free(sol
));
4196 /* Does the solution "sol" of the LP problem constructed by setup_carry_lp
4197 * carry any of the "n_edge" groups of dependences?
4198 * The value in the first position is the sum of (1 - e_i) over all "n_edge"
4199 * edges, with 0 <= e_i <= 1 equal to 1 when the dependences represented
4200 * by the edge are carried by the solution.
4201 * If the sum of the (1 - e_i) is smaller than "n_edge" then at least
4202 * one of those is carried.
4204 * Note that despite the fact that the problem is solved using a rational
4205 * solver, the solution is guaranteed to be integral.
4206 * Specifically, the dependence distance lower bounds e_i (and therefore
4207 * also their sum) are integers. See Lemma 5 of [1].
4209 * Any potential denominator of the sum is cleared by this function.
4210 * The denominator is not relevant for any of the other elements
4213 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4214 * Problem, Part II: Multi-Dimensional Time.
4215 * In Intl. Journal of Parallel Programming, 1992.
4217 static int carries_dependences(__isl_keep isl_vec
*sol
, int n_edge
)
4219 isl_int_divexact(sol
->el
[1], sol
->el
[1], sol
->el
[0]);
4220 isl_int_set_si(sol
->el
[0], 1);
4221 return isl_int_cmp_si(sol
->el
[1], n_edge
) < 0;
4224 /* Return the lexicographically smallest rational point in "lp",
4225 * assuming that all variables are non-negative and performing some
4226 * additional sanity checks.
4227 * If "want_integral" is set, then compute the lexicographically smallest
4228 * integer point instead.
4229 * In particular, "lp" should not be empty by construction.
4230 * Double check that this is the case.
4231 * If dependences are not carried for any of the "n_edge" edges,
4232 * then return an empty vector.
4234 * If the schedule_treat_coalescing option is set and
4235 * if the computed schedule performs loop coalescing on a given node,
4236 * i.e., if it is of the form
4238 * c_i i + c_j j + ...
4240 * with |c_j/c_i| >= size_i, then force the coefficient c_i to be zero
4241 * to cut out this solution. Repeat this process until no more loop
4242 * coalescing occurs or until no more dependences can be carried.
4243 * In the latter case, revert to the previously computed solution.
4245 * If the caller requests an integral solution and if coalescing should
4246 * be treated, then perform the coalescing treatment first as
4247 * an integral solution computed before coalescing treatment
4248 * would carry the same number of edges and would therefore probably
4249 * also be coalescing.
4251 * To allow the coalescing treatment to be performed first,
4252 * the initial solution is allowed to be rational and it is only
4253 * cut out (if needed) in the next iteration, if no coalescing measures
4256 static __isl_give isl_vec
*non_neg_lexmin(struct isl_sched_graph
*graph
,
4257 __isl_take isl_basic_set
*lp
, int n_edge
, int want_integral
)
4262 isl_vec
*sol
, *prev
= NULL
;
4263 int treat_coalescing
;
4267 ctx
= isl_basic_set_get_ctx(lp
);
4268 treat_coalescing
= isl_options_get_schedule_treat_coalescing(ctx
);
4269 tl
= isl_tab_lexmin_from_basic_set(lp
);
4276 tl
= isl_tab_lexmin_cut_to_integer(tl
);
4277 sol
= non_empty_solution(tl
);
4281 integral
= isl_int_is_one(sol
->el
[0]);
4282 if (!carries_dependences(sol
, n_edge
)) {
4284 prev
= isl_vec_alloc(ctx
, 0);
4289 prev
= isl_vec_free(prev
);
4290 cut
= want_integral
&& !integral
;
4293 if (!treat_coalescing
)
4295 for (i
= 0; i
< graph
->n
; ++i
) {
4296 struct isl_sched_node
*node
= &graph
->node
[i
];
4298 pos
= find_node_coalescing(node
, sol
);
4301 if (pos
< node
->nvar
)
4306 tl
= zero_out_node_coef(tl
, &graph
->node
[i
], pos
);
4311 isl_tab_lexmin_free(tl
);
4315 isl_tab_lexmin_free(tl
);
4321 /* If "edge" is an edge from a node to itself, then add the corresponding
4322 * dependence relation to "umap".
4323 * If "node" has been compressed, then the dependence relation
4324 * is also compressed first.
4326 static __isl_give isl_union_map
*add_intra(__isl_take isl_union_map
*umap
,
4327 struct isl_sched_edge
*edge
)
4330 struct isl_sched_node
*node
= edge
->src
;
4332 if (edge
->src
!= edge
->dst
)
4335 map
= isl_map_copy(edge
->map
);
4336 if (node
->compressed
) {
4337 map
= isl_map_preimage_domain_multi_aff(map
,
4338 isl_multi_aff_copy(node
->decompress
));
4339 map
= isl_map_preimage_range_multi_aff(map
,
4340 isl_multi_aff_copy(node
->decompress
));
4342 umap
= isl_union_map_add_map(umap
, map
);
4346 /* If "edge" is an edge from a node to another node, then add the corresponding
4347 * dependence relation to "umap".
4348 * If the source or destination nodes of "edge" have been compressed,
4349 * then the dependence relation is also compressed first.
4351 static __isl_give isl_union_map
*add_inter(__isl_take isl_union_map
*umap
,
4352 struct isl_sched_edge
*edge
)
4356 if (edge
->src
== edge
->dst
)
4359 map
= isl_map_copy(edge
->map
);
4360 if (edge
->src
->compressed
)
4361 map
= isl_map_preimage_domain_multi_aff(map
,
4362 isl_multi_aff_copy(edge
->src
->decompress
));
4363 if (edge
->dst
->compressed
)
4364 map
= isl_map_preimage_range_multi_aff(map
,
4365 isl_multi_aff_copy(edge
->dst
->decompress
));
4366 umap
= isl_union_map_add_map(umap
, map
);
4370 /* For each (conditional) validity edge in "graph",
4371 * add the corresponding dependence relation using "add"
4372 * to a collection of dependence relations and return the result.
4373 * If "coincidence" is set, then coincidence edges are considered as well.
4375 static __isl_give isl_union_map
*collect_validity(struct isl_sched_graph
*graph
,
4376 __isl_give isl_union_map
*(*add
)(__isl_take isl_union_map
*umap
,
4377 struct isl_sched_edge
*edge
), int coincidence
)
4381 isl_union_map
*umap
;
4383 space
= isl_space_copy(graph
->node
[0].space
);
4384 umap
= isl_union_map_empty(space
);
4386 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4387 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
4389 if (!is_any_validity(edge
) &&
4390 (!coincidence
|| !is_coincidence(edge
)))
4393 umap
= add(umap
, edge
);
4399 /* For each dependence relation on a (conditional) validity edge
4400 * from a node to itself,
4401 * construct the set of coefficients of valid constraints for elements
4402 * in that dependence relation and collect the results.
4403 * If "coincidence" is set, then coincidence edges are considered as well.
4405 * In particular, for each dependence relation R, constraints
4406 * on coefficients (c_0, c_n, c_x) are constructed such that
4408 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
4410 * This computation is essentially the same as that performed
4411 * by intra_coefficients, except that it operates on multiple
4414 * Note that if a dependence relation is a union of basic maps,
4415 * then each basic map needs to be treated individually as it may only
4416 * be possible to carry the dependences expressed by some of those
4417 * basic maps and not all of them.
4418 * The collected validity constraints are therefore not coalesced and
4419 * it is assumed that they are not coalesced automatically.
4420 * Duplicate basic maps can be removed, however.
4421 * In particular, if the same basic map appears as a disjunct
4422 * in multiple edges, then it only needs to be carried once.
4424 static __isl_give isl_basic_set_list
*collect_intra_validity(
4425 struct isl_sched_graph
*graph
, int coincidence
)
4427 isl_union_map
*intra
;
4428 isl_union_set
*delta
;
4429 isl_basic_set_list
*list
;
4431 intra
= collect_validity(graph
, &add_intra
, coincidence
);
4432 delta
= isl_union_map_deltas(intra
);
4433 delta
= isl_union_set_remove_divs(delta
);
4434 list
= isl_union_set_get_basic_set_list(delta
);
4435 isl_union_set_free(delta
);
4437 return isl_basic_set_list_coefficients(list
);
4440 /* For each dependence relation on a (conditional) validity edge
4441 * from a node to some other node,
4442 * construct the set of coefficients of valid constraints for elements
4443 * in that dependence relation and collect the results.
4444 * If "coincidence" is set, then coincidence edges are considered as well.
4446 * In particular, for each dependence relation R, constraints
4447 * on coefficients (c_0, c_n, c_x, c_y) are constructed such that
4449 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
4451 * This computation is essentially the same as that performed
4452 * by inter_coefficients, except that it operates on multiple
4455 * Note that if a dependence relation is a union of basic maps,
4456 * then each basic map needs to be treated individually as it may only
4457 * be possible to carry the dependences expressed by some of those
4458 * basic maps and not all of them.
4459 * The collected validity constraints are therefore not coalesced and
4460 * it is assumed that they are not coalesced automatically.
4461 * Duplicate basic maps can be removed, however.
4462 * In particular, if the same basic map appears as a disjunct
4463 * in multiple edges, then it only needs to be carried once.
4465 static __isl_give isl_basic_set_list
*collect_inter_validity(
4466 struct isl_sched_graph
*graph
, int coincidence
)
4468 isl_union_map
*inter
;
4469 isl_union_set
*wrap
;
4470 isl_basic_set_list
*list
;
4472 inter
= collect_validity(graph
, &add_inter
, coincidence
);
4473 inter
= isl_union_map_remove_divs(inter
);
4474 wrap
= isl_union_map_wrap(inter
);
4475 list
= isl_union_set_get_basic_set_list(wrap
);
4476 isl_union_set_free(wrap
);
4477 return isl_basic_set_list_coefficients(list
);
4480 /* Construct an LP problem for finding schedule coefficients
4481 * such that the schedule carries as many of the validity dependences
4483 * return the lexicographically smallest non-trivial solution.
4484 * If "fallback" is set, then the carrying is performed as a fallback
4485 * for the Pluto-like scheduler.
4486 * If "coincidence" is set, then try and carry coincidence edges as well.
4488 * The variable "n_edge" stores the number of groups that should be carried.
4489 * If none of the "n_edge" groups can be carried
4490 * then return an empty vector.
4491 * If, moreover, "n_edge" is zero, then the LP problem does not even
4492 * need to be constructed.
4494 * If a fallback solution is being computed, then compute an integral solution
4495 * for the coefficients rather than using the numerators
4496 * of a rational solution.
4498 static __isl_give isl_vec
*compute_carrying_sol(isl_ctx
*ctx
,
4499 struct isl_sched_graph
*graph
, int fallback
, int coincidence
)
4501 int n_intra
, n_inter
;
4504 struct isl_carry carry
= { 0 };
4506 carry
.intra
= collect_intra_validity(graph
, coincidence
);
4507 carry
.inter
= collect_inter_validity(graph
, coincidence
);
4508 if (!carry
.intra
|| !carry
.inter
)
4510 n_intra
= isl_basic_set_list_n_basic_set(carry
.intra
);
4511 n_inter
= isl_basic_set_list_n_basic_set(carry
.inter
);
4512 n_edge
= n_intra
+ n_inter
;
4514 isl_carry_clear(&carry
);
4515 return isl_vec_alloc(ctx
, 0);
4518 if (setup_carry_lp(ctx
, graph
, n_edge
, carry
.intra
, carry
.inter
) < 0)
4521 isl_carry_clear(&carry
);
4522 lp
= isl_basic_set_copy(graph
->lp
);
4523 return non_neg_lexmin(graph
, lp
, n_edge
, fallback
);
4525 isl_carry_clear(&carry
);
4529 /* Construct a schedule row for each node such that as many validity dependences
4530 * as possible are carried and then continue with the next band.
4531 * If "fallback" is set, then the carrying is performed as a fallback
4532 * for the Pluto-like scheduler.
4533 * If "coincidence" is set, then try and carry coincidence edges as well.
4535 * If there are no validity dependences, then no dependence can be carried and
4536 * the procedure is guaranteed to fail. If there is more than one component,
4537 * then try computing a schedule on each component separately
4538 * to prevent or at least postpone this failure.
4540 * If a schedule row is computed, then check that dependences are carried
4541 * for at least one of the edges.
4543 * If the computed schedule row turns out to be trivial on one or
4544 * more nodes where it should not be trivial, then we throw it away
4545 * and try again on each component separately.
4547 * If there is only one component, then we accept the schedule row anyway,
4548 * but we do not consider it as a complete row and therefore do not
4549 * increment graph->n_row. Note that the ranks of the nodes that
4550 * do get a non-trivial schedule part will get updated regardless and
4551 * graph->maxvar is computed based on these ranks. The test for
4552 * whether more schedule rows are required in compute_schedule_wcc
4553 * is therefore not affected.
4555 * Insert a band corresponding to the schedule row at position "node"
4556 * of the schedule tree and continue with the construction of the schedule.
4557 * This insertion and the continued construction is performed by split_scaled
4558 * after optionally checking for non-trivial common divisors.
4560 static __isl_give isl_schedule_node
*carry(__isl_take isl_schedule_node
*node
,
4561 struct isl_sched_graph
*graph
, int fallback
, int coincidence
)
4570 ctx
= isl_schedule_node_get_ctx(node
);
4571 sol
= compute_carrying_sol(ctx
, graph
, fallback
, coincidence
);
4573 return isl_schedule_node_free(node
);
4574 if (sol
->size
== 0) {
4577 return compute_component_schedule(node
, graph
, 1);
4578 isl_die(ctx
, isl_error_unknown
, "unable to carry dependences",
4579 return isl_schedule_node_free(node
));
4582 trivial
= is_any_trivial(graph
, sol
);
4584 sol
= isl_vec_free(sol
);
4585 } else if (trivial
&& graph
->scc
> 1) {
4587 return compute_component_schedule(node
, graph
, 1);
4590 if (update_schedule(graph
, sol
, 0, 0) < 0)
4591 return isl_schedule_node_free(node
);
4595 return split_scaled(node
, graph
);
4598 /* Construct a schedule row for each node such that as many validity dependences
4599 * as possible are carried and then continue with the next band.
4600 * Do so as a fallback for the Pluto-like scheduler.
4601 * If "coincidence" is set, then try and carry coincidence edges as well.
4603 static __isl_give isl_schedule_node
*carry_fallback(
4604 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
4607 return carry(node
, graph
, 1, coincidence
);
4610 /* Construct a schedule row for each node such that as many validity dependences
4611 * as possible are carried and then continue with the next band.
4612 * Do so for the case where the Feautrier scheduler was selected
4615 static __isl_give isl_schedule_node
*carry_feautrier(
4616 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4618 return carry(node
, graph
, 0, 0);
4621 /* Construct a schedule row for each node such that as many validity dependences
4622 * as possible are carried and then continue with the next band.
4623 * Do so as a fallback for the Pluto-like scheduler.
4625 static __isl_give isl_schedule_node
*carry_dependences(
4626 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4628 return carry_fallback(node
, graph
, 0);
4631 /* Construct a schedule row for each node such that as many validity or
4632 * coincidence dependences as possible are carried and
4633 * then continue with the next band.
4634 * Do so as a fallback for the Pluto-like scheduler.
4636 static __isl_give isl_schedule_node
*carry_coincidence(
4637 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4639 return carry_fallback(node
, graph
, 1);
4642 /* Topologically sort statements mapped to the same schedule iteration
4643 * and add insert a sequence node in front of "node"
4644 * corresponding to this order.
4645 * If "initialized" is set, then it may be assumed that compute_maxvar
4646 * has been called on the current band. Otherwise, call
4647 * compute_maxvar if and before carry_dependences gets called.
4649 * If it turns out to be impossible to sort the statements apart,
4650 * because different dependences impose different orderings
4651 * on the statements, then we extend the schedule such that
4652 * it carries at least one more dependence.
4654 static __isl_give isl_schedule_node
*sort_statements(
4655 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
4659 isl_union_set_list
*filters
;
4664 ctx
= isl_schedule_node_get_ctx(node
);
4666 isl_die(ctx
, isl_error_internal
,
4667 "graph should have at least one node",
4668 return isl_schedule_node_free(node
));
4673 if (update_edges(ctx
, graph
) < 0)
4674 return isl_schedule_node_free(node
);
4676 if (graph
->n_edge
== 0)
4679 if (detect_sccs(ctx
, graph
) < 0)
4680 return isl_schedule_node_free(node
);
4683 if (graph
->scc
< graph
->n
) {
4684 if (!initialized
&& compute_maxvar(graph
) < 0)
4685 return isl_schedule_node_free(node
);
4686 return carry_dependences(node
, graph
);
4689 filters
= extract_sccs(ctx
, graph
);
4690 node
= isl_schedule_node_insert_sequence(node
, filters
);
4695 /* Are there any (non-empty) (conditional) validity edges in the graph?
4697 static int has_validity_edges(struct isl_sched_graph
*graph
)
4701 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4704 empty
= isl_map_plain_is_empty(graph
->edge
[i
].map
);
4709 if (is_any_validity(&graph
->edge
[i
]))
4716 /* Should we apply a Feautrier step?
4717 * That is, did the user request the Feautrier algorithm and are
4718 * there any validity dependences (left)?
4720 static int need_feautrier_step(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
4722 if (ctx
->opt
->schedule_algorithm
!= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
)
4725 return has_validity_edges(graph
);
4728 /* Compute a schedule for a connected dependence graph using Feautrier's
4729 * multi-dimensional scheduling algorithm and return the updated schedule node.
4731 * The original algorithm is described in [1].
4732 * The main idea is to minimize the number of scheduling dimensions, by
4733 * trying to satisfy as many dependences as possible per scheduling dimension.
4735 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4736 * Problem, Part II: Multi-Dimensional Time.
4737 * In Intl. Journal of Parallel Programming, 1992.
4739 static __isl_give isl_schedule_node
*compute_schedule_wcc_feautrier(
4740 isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4742 return carry_feautrier(node
, graph
);
4745 /* Turn off the "local" bit on all (condition) edges.
4747 static void clear_local_edges(struct isl_sched_graph
*graph
)
4751 for (i
= 0; i
< graph
->n_edge
; ++i
)
4752 if (is_condition(&graph
->edge
[i
]))
4753 clear_local(&graph
->edge
[i
]);
4756 /* Does "graph" have both condition and conditional validity edges?
4758 static int need_condition_check(struct isl_sched_graph
*graph
)
4761 int any_condition
= 0;
4762 int any_conditional_validity
= 0;
4764 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4765 if (is_condition(&graph
->edge
[i
]))
4767 if (is_conditional_validity(&graph
->edge
[i
]))
4768 any_conditional_validity
= 1;
4771 return any_condition
&& any_conditional_validity
;
4774 /* Does "graph" contain any coincidence edge?
4776 static int has_any_coincidence(struct isl_sched_graph
*graph
)
4780 for (i
= 0; i
< graph
->n_edge
; ++i
)
4781 if (is_coincidence(&graph
->edge
[i
]))
4787 /* Extract the final schedule row as a map with the iteration domain
4788 * of "node" as domain.
4790 static __isl_give isl_map
*final_row(struct isl_sched_node
*node
)
4795 row
= isl_mat_rows(node
->sched
) - 1;
4796 ma
= node_extract_partial_schedule_multi_aff(node
, row
, 1);
4797 return isl_map_from_multi_aff(ma
);
4800 /* Is the conditional validity dependence in the edge with index "edge_index"
4801 * violated by the latest (i.e., final) row of the schedule?
4802 * That is, is i scheduled after j
4803 * for any conditional validity dependence i -> j?
4805 static int is_violated(struct isl_sched_graph
*graph
, int edge_index
)
4807 isl_map
*src_sched
, *dst_sched
, *map
;
4808 struct isl_sched_edge
*edge
= &graph
->edge
[edge_index
];
4811 src_sched
= final_row(edge
->src
);
4812 dst_sched
= final_row(edge
->dst
);
4813 map
= isl_map_copy(edge
->map
);
4814 map
= isl_map_apply_domain(map
, src_sched
);
4815 map
= isl_map_apply_range(map
, dst_sched
);
4816 map
= isl_map_order_gt(map
, isl_dim_in
, 0, isl_dim_out
, 0);
4817 empty
= isl_map_is_empty(map
);
4826 /* Does "graph" have any satisfied condition edges that
4827 * are adjacent to the conditional validity constraint with
4828 * domain "conditional_source" and range "conditional_sink"?
4830 * A satisfied condition is one that is not local.
4831 * If a condition was forced to be local already (i.e., marked as local)
4832 * then there is no need to check if it is in fact local.
4834 * Additionally, mark all adjacent condition edges found as local.
4836 static int has_adjacent_true_conditions(struct isl_sched_graph
*graph
,
4837 __isl_keep isl_union_set
*conditional_source
,
4838 __isl_keep isl_union_set
*conditional_sink
)
4843 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4844 int adjacent
, local
;
4845 isl_union_map
*condition
;
4847 if (!is_condition(&graph
->edge
[i
]))
4849 if (is_local(&graph
->edge
[i
]))
4852 condition
= graph
->edge
[i
].tagged_condition
;
4853 adjacent
= domain_intersects(condition
, conditional_sink
);
4854 if (adjacent
>= 0 && !adjacent
)
4855 adjacent
= range_intersects(condition
,
4856 conditional_source
);
4862 set_local(&graph
->edge
[i
]);
4864 local
= is_condition_false(&graph
->edge
[i
]);
4874 /* Are there any violated conditional validity dependences with
4875 * adjacent condition dependences that are not local with respect
4876 * to the current schedule?
4877 * That is, is the conditional validity constraint violated?
4879 * Additionally, mark all those adjacent condition dependences as local.
4880 * We also mark those adjacent condition dependences that were not marked
4881 * as local before, but just happened to be local already. This ensures
4882 * that they remain local if the schedule is recomputed.
4884 * We first collect domain and range of all violated conditional validity
4885 * dependences and then check if there are any adjacent non-local
4886 * condition dependences.
4888 static int has_violated_conditional_constraint(isl_ctx
*ctx
,
4889 struct isl_sched_graph
*graph
)
4893 isl_union_set
*source
, *sink
;
4895 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
4896 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
4897 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4898 isl_union_set
*uset
;
4899 isl_union_map
*umap
;
4902 if (!is_conditional_validity(&graph
->edge
[i
]))
4905 violated
= is_violated(graph
, i
);
4913 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
4914 uset
= isl_union_map_domain(umap
);
4915 source
= isl_union_set_union(source
, uset
);
4916 source
= isl_union_set_coalesce(source
);
4918 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
4919 uset
= isl_union_map_range(umap
);
4920 sink
= isl_union_set_union(sink
, uset
);
4921 sink
= isl_union_set_coalesce(sink
);
4925 any
= has_adjacent_true_conditions(graph
, source
, sink
);
4927 isl_union_set_free(source
);
4928 isl_union_set_free(sink
);
4931 isl_union_set_free(source
);
4932 isl_union_set_free(sink
);
4936 /* Examine the current band (the rows between graph->band_start and
4937 * graph->n_total_row), deciding whether to drop it or add it to "node"
4938 * and then continue with the computation of the next band, if any.
4939 * If "initialized" is set, then it may be assumed that compute_maxvar
4940 * has been called on the current band. Otherwise, call
4941 * compute_maxvar if and before carry_dependences gets called.
4943 * The caller keeps looking for a new row as long as
4944 * graph->n_row < graph->maxvar. If the latest attempt to find
4945 * such a row failed (i.e., we still have graph->n_row < graph->maxvar),
4947 * - split between SCCs and start over (assuming we found an interesting
4948 * pair of SCCs between which to split)
4949 * - continue with the next band (assuming the current band has at least
4951 * - if outer coincidence needs to be enforced, then try to carry as many
4952 * validity or coincidence dependences as possible and
4953 * continue with the next band
4954 * - try to carry as many validity dependences as possible and
4955 * continue with the next band
4956 * In each case, we first insert a band node in the schedule tree
4957 * if any rows have been computed.
4959 * If the caller managed to complete the schedule, we insert a band node
4960 * (if any schedule rows were computed) and we finish off by topologically
4961 * sorting the statements based on the remaining dependences.
4963 static __isl_give isl_schedule_node
*compute_schedule_finish_band(
4964 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
4972 if (graph
->n_row
< graph
->maxvar
) {
4974 int empty
= graph
->n_total_row
== graph
->band_start
;
4976 ctx
= isl_schedule_node_get_ctx(node
);
4977 if (!ctx
->opt
->schedule_maximize_band_depth
&& !empty
)
4978 return compute_next_band(node
, graph
, 1);
4979 if (graph
->src_scc
>= 0)
4980 return compute_split_schedule(node
, graph
);
4982 return compute_next_band(node
, graph
, 1);
4983 if (!initialized
&& compute_maxvar(graph
) < 0)
4984 return isl_schedule_node_free(node
);
4985 if (isl_options_get_schedule_outer_coincidence(ctx
))
4986 return carry_coincidence(node
, graph
);
4987 return carry_dependences(node
, graph
);
4990 insert
= graph
->n_total_row
> graph
->band_start
;
4992 node
= insert_current_band(node
, graph
, 1);
4993 node
= isl_schedule_node_child(node
, 0);
4995 node
= sort_statements(node
, graph
, initialized
);
4997 node
= isl_schedule_node_parent(node
);
5002 /* Construct a band of schedule rows for a connected dependence graph.
5003 * The caller is responsible for determining the strongly connected
5004 * components and calling compute_maxvar first.
5006 * We try to find a sequence of as many schedule rows as possible that result
5007 * in non-negative dependence distances (independent of the previous rows
5008 * in the sequence, i.e., such that the sequence is tilable), with as
5009 * many of the initial rows as possible satisfying the coincidence constraints.
5010 * The computation stops if we can't find any more rows or if we have found
5011 * all the rows we wanted to find.
5013 * If ctx->opt->schedule_outer_coincidence is set, then we force the
5014 * outermost dimension to satisfy the coincidence constraints. If this
5015 * turns out to be impossible, we fall back on the general scheme above
5016 * and try to carry as many dependences as possible.
5018 * If "graph" contains both condition and conditional validity dependences,
5019 * then we need to check that that the conditional schedule constraint
5020 * is satisfied, i.e., there are no violated conditional validity dependences
5021 * that are adjacent to any non-local condition dependences.
5022 * If there are, then we mark all those adjacent condition dependences
5023 * as local and recompute the current band. Those dependences that
5024 * are marked local will then be forced to be local.
5025 * The initial computation is performed with no dependences marked as local.
5026 * If we are lucky, then there will be no violated conditional validity
5027 * dependences adjacent to any non-local condition dependences.
5028 * Otherwise, we mark some additional condition dependences as local and
5029 * recompute. We continue this process until there are no violations left or
5030 * until we are no longer able to compute a schedule.
5031 * Since there are only a finite number of dependences,
5032 * there will only be a finite number of iterations.
5034 static isl_stat
compute_schedule_wcc_band(isl_ctx
*ctx
,
5035 struct isl_sched_graph
*graph
)
5037 int has_coincidence
;
5038 int use_coincidence
;
5039 int force_coincidence
= 0;
5040 int check_conditional
;
5042 if (sort_sccs(graph
) < 0)
5043 return isl_stat_error
;
5045 clear_local_edges(graph
);
5046 check_conditional
= need_condition_check(graph
);
5047 has_coincidence
= has_any_coincidence(graph
);
5049 if (ctx
->opt
->schedule_outer_coincidence
)
5050 force_coincidence
= 1;
5052 use_coincidence
= has_coincidence
;
5053 while (graph
->n_row
< graph
->maxvar
) {
5058 graph
->src_scc
= -1;
5059 graph
->dst_scc
= -1;
5061 if (setup_lp(ctx
, graph
, use_coincidence
) < 0)
5062 return isl_stat_error
;
5063 sol
= solve_lp(ctx
, graph
);
5065 return isl_stat_error
;
5066 if (sol
->size
== 0) {
5067 int empty
= graph
->n_total_row
== graph
->band_start
;
5070 if (use_coincidence
&& (!force_coincidence
|| !empty
)) {
5071 use_coincidence
= 0;
5076 coincident
= !has_coincidence
|| use_coincidence
;
5077 if (update_schedule(graph
, sol
, 0, coincident
) < 0)
5078 return isl_stat_error
;
5080 if (!check_conditional
)
5082 violated
= has_violated_conditional_constraint(ctx
, graph
);
5084 return isl_stat_error
;
5087 if (reset_band(graph
) < 0)
5088 return isl_stat_error
;
5089 use_coincidence
= has_coincidence
;
5095 /* Compute a schedule for a connected dependence graph by considering
5096 * the graph as a whole and return the updated schedule node.
5098 * The actual schedule rows of the current band are computed by
5099 * compute_schedule_wcc_band. compute_schedule_finish_band takes
5100 * care of integrating the band into "node" and continuing
5103 static __isl_give isl_schedule_node
*compute_schedule_wcc_whole(
5104 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
5111 ctx
= isl_schedule_node_get_ctx(node
);
5112 if (compute_schedule_wcc_band(ctx
, graph
) < 0)
5113 return isl_schedule_node_free(node
);
5115 return compute_schedule_finish_band(node
, graph
, 1);
5118 /* Clustering information used by compute_schedule_wcc_clustering.
5120 * "n" is the number of SCCs in the original dependence graph
5121 * "scc" is an array of "n" elements, each representing an SCC
5122 * of the original dependence graph. All entries in the same cluster
5123 * have the same number of schedule rows.
5124 * "scc_cluster" maps each SCC index to the cluster to which it belongs,
5125 * where each cluster is represented by the index of the first SCC
5126 * in the cluster. Initially, each SCC belongs to a cluster containing
5129 * "scc_in_merge" is used by merge_clusters_along_edge to keep
5130 * track of which SCCs need to be merged.
5132 * "cluster" contains the merged clusters of SCCs after the clustering
5135 * "scc_node" is a temporary data structure used inside copy_partial.
5136 * For each SCC, it keeps track of the number of nodes in the SCC
5137 * that have already been copied.
5139 struct isl_clustering
{
5141 struct isl_sched_graph
*scc
;
5142 struct isl_sched_graph
*cluster
;
5148 /* Initialize the clustering data structure "c" from "graph".
5150 * In particular, allocate memory, extract the SCCs from "graph"
5151 * into c->scc, initialize scc_cluster and construct
5152 * a band of schedule rows for each SCC.
5153 * Within each SCC, there is only one SCC by definition.
5154 * Each SCC initially belongs to a cluster containing only that SCC.
5156 static isl_stat
clustering_init(isl_ctx
*ctx
, struct isl_clustering
*c
,
5157 struct isl_sched_graph
*graph
)
5162 c
->scc
= isl_calloc_array(ctx
, struct isl_sched_graph
, c
->n
);
5163 c
->cluster
= isl_calloc_array(ctx
, struct isl_sched_graph
, c
->n
);
5164 c
->scc_cluster
= isl_calloc_array(ctx
, int, c
->n
);
5165 c
->scc_node
= isl_calloc_array(ctx
, int, c
->n
);
5166 c
->scc_in_merge
= isl_calloc_array(ctx
, int, c
->n
);
5167 if (!c
->scc
|| !c
->cluster
||
5168 !c
->scc_cluster
|| !c
->scc_node
|| !c
->scc_in_merge
)
5169 return isl_stat_error
;
5171 for (i
= 0; i
< c
->n
; ++i
) {
5172 if (extract_sub_graph(ctx
, graph
, &node_scc_exactly
,
5173 &edge_scc_exactly
, i
, &c
->scc
[i
]) < 0)
5174 return isl_stat_error
;
5176 if (compute_maxvar(&c
->scc
[i
]) < 0)
5177 return isl_stat_error
;
5178 if (compute_schedule_wcc_band(ctx
, &c
->scc
[i
]) < 0)
5179 return isl_stat_error
;
5180 c
->scc_cluster
[i
] = i
;
5186 /* Free all memory allocated for "c".
5188 static void clustering_free(isl_ctx
*ctx
, struct isl_clustering
*c
)
5193 for (i
= 0; i
< c
->n
; ++i
)
5194 graph_free(ctx
, &c
->scc
[i
]);
5197 for (i
= 0; i
< c
->n
; ++i
)
5198 graph_free(ctx
, &c
->cluster
[i
]);
5200 free(c
->scc_cluster
);
5202 free(c
->scc_in_merge
);
5205 /* Should we refrain from merging the cluster in "graph" with
5206 * any other cluster?
5207 * In particular, is its current schedule band empty and incomplete.
5209 static int bad_cluster(struct isl_sched_graph
*graph
)
5211 return graph
->n_row
< graph
->maxvar
&&
5212 graph
->n_total_row
== graph
->band_start
;
5215 /* Is "edge" a proximity edge with a non-empty dependence relation?
5217 static isl_bool
is_non_empty_proximity(struct isl_sched_edge
*edge
)
5219 if (!is_proximity(edge
))
5220 return isl_bool_false
;
5221 return isl_bool_not(isl_map_plain_is_empty(edge
->map
));
5224 /* Return the index of an edge in "graph" that can be used to merge
5225 * two clusters in "c".
5226 * Return graph->n_edge if no such edge can be found.
5227 * Return -1 on error.
5229 * In particular, return a proximity edge between two clusters
5230 * that is not marked "no_merge" and such that neither of the
5231 * two clusters has an incomplete, empty band.
5233 * If there are multiple such edges, then try and find the most
5234 * appropriate edge to use for merging. In particular, pick the edge
5235 * with the greatest weight. If there are multiple of those,
5236 * then pick one with the shortest distance between
5237 * the two cluster representatives.
5239 static int find_proximity(struct isl_sched_graph
*graph
,
5240 struct isl_clustering
*c
)
5242 int i
, best
= graph
->n_edge
, best_dist
, best_weight
;
5244 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5245 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
5249 prox
= is_non_empty_proximity(edge
);
5256 if (bad_cluster(&c
->scc
[edge
->src
->scc
]) ||
5257 bad_cluster(&c
->scc
[edge
->dst
->scc
]))
5259 dist
= c
->scc_cluster
[edge
->dst
->scc
] -
5260 c
->scc_cluster
[edge
->src
->scc
];
5263 weight
= edge
->weight
;
5264 if (best
< graph
->n_edge
) {
5265 if (best_weight
> weight
)
5267 if (best_weight
== weight
&& best_dist
<= dist
)
5272 best_weight
= weight
;
5278 /* Internal data structure used in mark_merge_sccs.
5280 * "graph" is the dependence graph in which a strongly connected
5281 * component is constructed.
5282 * "scc_cluster" maps each SCC index to the cluster to which it belongs.
5283 * "src" and "dst" are the indices of the nodes that are being merged.
5285 struct isl_mark_merge_sccs_data
{
5286 struct isl_sched_graph
*graph
;
5292 /* Check whether the cluster containing node "i" depends on the cluster
5293 * containing node "j". If "i" and "j" belong to the same cluster,
5294 * then they are taken to depend on each other to ensure that
5295 * the resulting strongly connected component consists of complete
5296 * clusters. Furthermore, if "i" and "j" are the two nodes that
5297 * are being merged, then they are taken to depend on each other as well.
5298 * Otherwise, check if there is a (conditional) validity dependence
5299 * from node[j] to node[i], forcing node[i] to follow node[j].
5301 static isl_bool
cluster_follows(int i
, int j
, void *user
)
5303 struct isl_mark_merge_sccs_data
*data
= user
;
5304 struct isl_sched_graph
*graph
= data
->graph
;
5305 int *scc_cluster
= data
->scc_cluster
;
5307 if (data
->src
== i
&& data
->dst
== j
)
5308 return isl_bool_true
;
5309 if (data
->src
== j
&& data
->dst
== i
)
5310 return isl_bool_true
;
5311 if (scc_cluster
[graph
->node
[i
].scc
] == scc_cluster
[graph
->node
[j
].scc
])
5312 return isl_bool_true
;
5314 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
5317 /* Mark all SCCs that belong to either of the two clusters in "c"
5318 * connected by the edge in "graph" with index "edge", or to any
5319 * of the intermediate clusters.
5320 * The marking is recorded in c->scc_in_merge.
5322 * The given edge has been selected for merging two clusters,
5323 * meaning that there is at least a proximity edge between the two nodes.
5324 * However, there may also be (indirect) validity dependences
5325 * between the two nodes. When merging the two clusters, all clusters
5326 * containing one or more of the intermediate nodes along the
5327 * indirect validity dependences need to be merged in as well.
5329 * First collect all such nodes by computing the strongly connected
5330 * component (SCC) containing the two nodes connected by the edge, where
5331 * the two nodes are considered to depend on each other to make
5332 * sure they end up in the same SCC. Similarly, each node is considered
5333 * to depend on every other node in the same cluster to ensure
5334 * that the SCC consists of complete clusters.
5336 * Then the original SCCs that contain any of these nodes are marked
5337 * in c->scc_in_merge.
5339 static isl_stat
mark_merge_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5340 int edge
, struct isl_clustering
*c
)
5342 struct isl_mark_merge_sccs_data data
;
5343 struct isl_tarjan_graph
*g
;
5346 for (i
= 0; i
< c
->n
; ++i
)
5347 c
->scc_in_merge
[i
] = 0;
5350 data
.scc_cluster
= c
->scc_cluster
;
5351 data
.src
= graph
->edge
[edge
].src
- graph
->node
;
5352 data
.dst
= graph
->edge
[edge
].dst
- graph
->node
;
5354 g
= isl_tarjan_graph_component(ctx
, graph
->n
, data
.dst
,
5355 &cluster_follows
, &data
);
5361 isl_die(ctx
, isl_error_internal
,
5362 "expecting at least two nodes in component",
5364 if (g
->order
[--i
] != -1)
5365 isl_die(ctx
, isl_error_internal
,
5366 "expecting end of component marker", goto error
);
5368 for (--i
; i
>= 0 && g
->order
[i
] != -1; --i
) {
5369 int scc
= graph
->node
[g
->order
[i
]].scc
;
5370 c
->scc_in_merge
[scc
] = 1;
5373 isl_tarjan_graph_free(g
);
5376 isl_tarjan_graph_free(g
);
5377 return isl_stat_error
;
5380 /* Construct the identifier "cluster_i".
5382 static __isl_give isl_id
*cluster_id(isl_ctx
*ctx
, int i
)
5386 snprintf(name
, sizeof(name
), "cluster_%d", i
);
5387 return isl_id_alloc(ctx
, name
, NULL
);
5390 /* Construct the space of the cluster with index "i" containing
5391 * the strongly connected component "scc".
5393 * In particular, construct a space called cluster_i with dimension equal
5394 * to the number of schedule rows in the current band of "scc".
5396 static __isl_give isl_space
*cluster_space(struct isl_sched_graph
*scc
, int i
)
5402 nvar
= scc
->n_total_row
- scc
->band_start
;
5403 space
= isl_space_copy(scc
->node
[0].space
);
5404 space
= isl_space_params(space
);
5405 space
= isl_space_set_from_params(space
);
5406 space
= isl_space_add_dims(space
, isl_dim_set
, nvar
);
5407 id
= cluster_id(isl_space_get_ctx(space
), i
);
5408 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
5413 /* Collect the domain of the graph for merging clusters.
5415 * In particular, for each cluster with first SCC "i", construct
5416 * a set in the space called cluster_i with dimension equal
5417 * to the number of schedule rows in the current band of the cluster.
5419 static __isl_give isl_union_set
*collect_domain(isl_ctx
*ctx
,
5420 struct isl_sched_graph
*graph
, struct isl_clustering
*c
)
5424 isl_union_set
*domain
;
5426 space
= isl_space_params_alloc(ctx
, 0);
5427 domain
= isl_union_set_empty(space
);
5429 for (i
= 0; i
< graph
->scc
; ++i
) {
5432 if (!c
->scc_in_merge
[i
])
5434 if (c
->scc_cluster
[i
] != i
)
5436 space
= cluster_space(&c
->scc
[i
], i
);
5437 domain
= isl_union_set_add_set(domain
, isl_set_universe(space
));
5443 /* Construct a map from the original instances to the corresponding
5444 * cluster instance in the current bands of the clusters in "c".
5446 static __isl_give isl_union_map
*collect_cluster_map(isl_ctx
*ctx
,
5447 struct isl_sched_graph
*graph
, struct isl_clustering
*c
)
5451 isl_union_map
*cluster_map
;
5453 space
= isl_space_params_alloc(ctx
, 0);
5454 cluster_map
= isl_union_map_empty(space
);
5455 for (i
= 0; i
< graph
->scc
; ++i
) {
5459 if (!c
->scc_in_merge
[i
])
5462 id
= cluster_id(ctx
, c
->scc_cluster
[i
]);
5463 start
= c
->scc
[i
].band_start
;
5464 n
= c
->scc
[i
].n_total_row
- start
;
5465 for (j
= 0; j
< c
->scc
[i
].n
; ++j
) {
5468 struct isl_sched_node
*node
= &c
->scc
[i
].node
[j
];
5470 ma
= node_extract_partial_schedule_multi_aff(node
,
5472 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
,
5474 map
= isl_map_from_multi_aff(ma
);
5475 cluster_map
= isl_union_map_add_map(cluster_map
, map
);
5483 /* Add "umap" to the schedule constraints "sc" of all types of "edge"
5484 * that are not isl_edge_condition or isl_edge_conditional_validity.
5486 static __isl_give isl_schedule_constraints
*add_non_conditional_constraints(
5487 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*umap
,
5488 __isl_take isl_schedule_constraints
*sc
)
5490 enum isl_edge_type t
;
5495 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
5496 if (t
== isl_edge_condition
||
5497 t
== isl_edge_conditional_validity
)
5499 if (!is_type(edge
, t
))
5501 sc
= isl_schedule_constraints_add(sc
, t
,
5502 isl_union_map_copy(umap
));
5508 /* Add schedule constraints of types isl_edge_condition and
5509 * isl_edge_conditional_validity to "sc" by applying "umap" to
5510 * the domains of the wrapped relations in domain and range
5511 * of the corresponding tagged constraints of "edge".
5513 static __isl_give isl_schedule_constraints
*add_conditional_constraints(
5514 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*umap
,
5515 __isl_take isl_schedule_constraints
*sc
)
5517 enum isl_edge_type t
;
5518 isl_union_map
*tagged
;
5520 for (t
= isl_edge_condition
; t
<= isl_edge_conditional_validity
; ++t
) {
5521 if (!is_type(edge
, t
))
5523 if (t
== isl_edge_condition
)
5524 tagged
= isl_union_map_copy(edge
->tagged_condition
);
5526 tagged
= isl_union_map_copy(edge
->tagged_validity
);
5527 tagged
= isl_union_map_zip(tagged
);
5528 tagged
= isl_union_map_apply_domain(tagged
,
5529 isl_union_map_copy(umap
));
5530 tagged
= isl_union_map_zip(tagged
);
5531 sc
= isl_schedule_constraints_add(sc
, t
, tagged
);
5539 /* Given a mapping "cluster_map" from the original instances to
5540 * the cluster instances, add schedule constraints on the clusters
5541 * to "sc" corresponding to the original constraints represented by "edge".
5543 * For non-tagged dependence constraints, the cluster constraints
5544 * are obtained by applying "cluster_map" to the edge->map.
5546 * For tagged dependence constraints, "cluster_map" needs to be applied
5547 * to the domains of the wrapped relations in domain and range
5548 * of the tagged dependence constraints. Pick out the mappings
5549 * from these domains from "cluster_map" and construct their product.
5550 * This mapping can then be applied to the pair of domains.
5552 static __isl_give isl_schedule_constraints
*collect_edge_constraints(
5553 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*cluster_map
,
5554 __isl_take isl_schedule_constraints
*sc
)
5556 isl_union_map
*umap
;
5558 isl_union_set
*uset
;
5559 isl_union_map
*umap1
, *umap2
;
5564 umap
= isl_union_map_from_map(isl_map_copy(edge
->map
));
5565 umap
= isl_union_map_apply_domain(umap
,
5566 isl_union_map_copy(cluster_map
));
5567 umap
= isl_union_map_apply_range(umap
,
5568 isl_union_map_copy(cluster_map
));
5569 sc
= add_non_conditional_constraints(edge
, umap
, sc
);
5570 isl_union_map_free(umap
);
5572 if (!sc
|| (!is_condition(edge
) && !is_conditional_validity(edge
)))
5575 space
= isl_space_domain(isl_map_get_space(edge
->map
));
5576 uset
= isl_union_set_from_set(isl_set_universe(space
));
5577 umap1
= isl_union_map_copy(cluster_map
);
5578 umap1
= isl_union_map_intersect_domain(umap1
, uset
);
5579 space
= isl_space_range(isl_map_get_space(edge
->map
));
5580 uset
= isl_union_set_from_set(isl_set_universe(space
));
5581 umap2
= isl_union_map_copy(cluster_map
);
5582 umap2
= isl_union_map_intersect_domain(umap2
, uset
);
5583 umap
= isl_union_map_product(umap1
, umap2
);
5585 sc
= add_conditional_constraints(edge
, umap
, sc
);
5587 isl_union_map_free(umap
);
5591 /* Given a mapping "cluster_map" from the original instances to
5592 * the cluster instances, add schedule constraints on the clusters
5593 * to "sc" corresponding to all edges in "graph" between nodes that
5594 * belong to SCCs that are marked for merging in "scc_in_merge".
5596 static __isl_give isl_schedule_constraints
*collect_constraints(
5597 struct isl_sched_graph
*graph
, int *scc_in_merge
,
5598 __isl_keep isl_union_map
*cluster_map
,
5599 __isl_take isl_schedule_constraints
*sc
)
5603 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5604 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
5606 if (!scc_in_merge
[edge
->src
->scc
])
5608 if (!scc_in_merge
[edge
->dst
->scc
])
5610 sc
= collect_edge_constraints(edge
, cluster_map
, sc
);
5616 /* Construct a dependence graph for scheduling clusters with respect
5617 * to each other and store the result in "merge_graph".
5618 * In particular, the nodes of the graph correspond to the schedule
5619 * dimensions of the current bands of those clusters that have been
5620 * marked for merging in "c".
5622 * First construct an isl_schedule_constraints object for this domain
5623 * by transforming the edges in "graph" to the domain.
5624 * Then initialize a dependence graph for scheduling from these
5627 static isl_stat
init_merge_graph(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5628 struct isl_clustering
*c
, struct isl_sched_graph
*merge_graph
)
5630 isl_union_set
*domain
;
5631 isl_union_map
*cluster_map
;
5632 isl_schedule_constraints
*sc
;
5635 domain
= collect_domain(ctx
, graph
, c
);
5636 sc
= isl_schedule_constraints_on_domain(domain
);
5638 return isl_stat_error
;
5639 cluster_map
= collect_cluster_map(ctx
, graph
, c
);
5640 sc
= collect_constraints(graph
, c
->scc_in_merge
, cluster_map
, sc
);
5641 isl_union_map_free(cluster_map
);
5643 r
= graph_init(merge_graph
, sc
);
5645 isl_schedule_constraints_free(sc
);
5650 /* Compute the maximal number of remaining schedule rows that still need
5651 * to be computed for the nodes that belong to clusters with the maximal
5652 * dimension for the current band (i.e., the band that is to be merged).
5653 * Only clusters that are about to be merged are considered.
5654 * "maxvar" is the maximal dimension for the current band.
5655 * "c" contains information about the clusters.
5657 * Return the maximal number of remaining schedule rows or -1 on error.
5659 static int compute_maxvar_max_slack(int maxvar
, struct isl_clustering
*c
)
5665 for (i
= 0; i
< c
->n
; ++i
) {
5667 struct isl_sched_graph
*scc
;
5669 if (!c
->scc_in_merge
[i
])
5672 nvar
= scc
->n_total_row
- scc
->band_start
;
5675 for (j
= 0; j
< scc
->n
; ++j
) {
5676 struct isl_sched_node
*node
= &scc
->node
[j
];
5679 if (node_update_cmap(node
) < 0)
5681 slack
= node
->nvar
- node
->rank
;
5682 if (slack
> max_slack
)
5690 /* If there are any clusters where the dimension of the current band
5691 * (i.e., the band that is to be merged) is smaller than "maxvar" and
5692 * if there are any nodes in such a cluster where the number
5693 * of remaining schedule rows that still need to be computed
5694 * is greater than "max_slack", then return the smallest current band
5695 * dimension of all these clusters. Otherwise return the original value
5696 * of "maxvar". Return -1 in case of any error.
5697 * Only clusters that are about to be merged are considered.
5698 * "c" contains information about the clusters.
5700 static int limit_maxvar_to_slack(int maxvar
, int max_slack
,
5701 struct isl_clustering
*c
)
5705 for (i
= 0; i
< c
->n
; ++i
) {
5707 struct isl_sched_graph
*scc
;
5709 if (!c
->scc_in_merge
[i
])
5712 nvar
= scc
->n_total_row
- scc
->band_start
;
5715 for (j
= 0; j
< scc
->n
; ++j
) {
5716 struct isl_sched_node
*node
= &scc
->node
[j
];
5719 if (node_update_cmap(node
) < 0)
5721 slack
= node
->nvar
- node
->rank
;
5722 if (slack
> max_slack
) {
5732 /* Adjust merge_graph->maxvar based on the number of remaining schedule rows
5733 * that still need to be computed. In particular, if there is a node
5734 * in a cluster where the dimension of the current band is smaller
5735 * than merge_graph->maxvar, but the number of remaining schedule rows
5736 * is greater than that of any node in a cluster with the maximal
5737 * dimension for the current band (i.e., merge_graph->maxvar),
5738 * then adjust merge_graph->maxvar to the (smallest) current band dimension
5739 * of those clusters. Without this adjustment, the total number of
5740 * schedule dimensions would be increased, resulting in a skewed view
5741 * of the number of coincident dimensions.
5742 * "c" contains information about the clusters.
5744 * If the maximize_band_depth option is set and merge_graph->maxvar is reduced,
5745 * then there is no point in attempting any merge since it will be rejected
5746 * anyway. Set merge_graph->maxvar to zero in such cases.
5748 static isl_stat
adjust_maxvar_to_slack(isl_ctx
*ctx
,
5749 struct isl_sched_graph
*merge_graph
, struct isl_clustering
*c
)
5751 int max_slack
, maxvar
;
5753 max_slack
= compute_maxvar_max_slack(merge_graph
->maxvar
, c
);
5755 return isl_stat_error
;
5756 maxvar
= limit_maxvar_to_slack(merge_graph
->maxvar
, max_slack
, c
);
5758 return isl_stat_error
;
5760 if (maxvar
< merge_graph
->maxvar
) {
5761 if (isl_options_get_schedule_maximize_band_depth(ctx
))
5762 merge_graph
->maxvar
= 0;
5764 merge_graph
->maxvar
= maxvar
;
5770 /* Return the number of coincident dimensions in the current band of "graph",
5771 * where the nodes of "graph" are assumed to be scheduled by a single band.
5773 static int get_n_coincident(struct isl_sched_graph
*graph
)
5777 for (i
= graph
->band_start
; i
< graph
->n_total_row
; ++i
)
5778 if (!graph
->node
[0].coincident
[i
])
5781 return i
- graph
->band_start
;
5784 /* Should the clusters be merged based on the cluster schedule
5785 * in the current (and only) band of "merge_graph", given that
5786 * coincidence should be maximized?
5788 * If the number of coincident schedule dimensions in the merged band
5789 * would be less than the maximal number of coincident schedule dimensions
5790 * in any of the merged clusters, then the clusters should not be merged.
5792 static isl_bool
ok_to_merge_coincident(struct isl_clustering
*c
,
5793 struct isl_sched_graph
*merge_graph
)
5800 for (i
= 0; i
< c
->n
; ++i
) {
5801 if (!c
->scc_in_merge
[i
])
5803 n_coincident
= get_n_coincident(&c
->scc
[i
]);
5804 if (n_coincident
> max_coincident
)
5805 max_coincident
= n_coincident
;
5808 n_coincident
= get_n_coincident(merge_graph
);
5810 return n_coincident
>= max_coincident
;
5813 /* Return the transformation on "node" expressed by the current (and only)
5814 * band of "merge_graph" applied to the clusters in "c".
5816 * First find the representation of "node" in its SCC in "c" and
5817 * extract the transformation expressed by the current band.
5818 * Then extract the transformation applied by "merge_graph"
5819 * to the cluster to which this SCC belongs.
5820 * Combine the two to obtain the complete transformation on the node.
5822 * Note that the range of the first transformation is an anonymous space,
5823 * while the domain of the second is named "cluster_X". The range
5824 * of the former therefore needs to be adjusted before the two
5827 static __isl_give isl_map
*extract_node_transformation(isl_ctx
*ctx
,
5828 struct isl_sched_node
*node
, struct isl_clustering
*c
,
5829 struct isl_sched_graph
*merge_graph
)
5831 struct isl_sched_node
*scc_node
, *cluster_node
;
5835 isl_multi_aff
*ma
, *ma2
;
5837 scc_node
= graph_find_node(ctx
, &c
->scc
[node
->scc
], node
->space
);
5838 start
= c
->scc
[node
->scc
].band_start
;
5839 n
= c
->scc
[node
->scc
].n_total_row
- start
;
5840 ma
= node_extract_partial_schedule_multi_aff(scc_node
, start
, n
);
5841 space
= cluster_space(&c
->scc
[node
->scc
], c
->scc_cluster
[node
->scc
]);
5842 cluster_node
= graph_find_node(ctx
, merge_graph
, space
);
5843 if (space
&& !cluster_node
)
5844 isl_die(ctx
, isl_error_internal
, "unable to find cluster",
5845 space
= isl_space_free(space
));
5846 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
5847 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
, id
);
5848 isl_space_free(space
);
5849 n
= merge_graph
->n_total_row
;
5850 ma2
= node_extract_partial_schedule_multi_aff(cluster_node
, 0, n
);
5851 ma
= isl_multi_aff_pullback_multi_aff(ma2
, ma
);
5853 return isl_map_from_multi_aff(ma
);
5856 /* Give a set of distances "set", are they bounded by a small constant
5857 * in direction "pos"?
5858 * In practice, check if they are bounded by 2 by checking that there
5859 * are no elements with a value greater than or equal to 3 or
5860 * smaller than or equal to -3.
5862 static isl_bool
distance_is_bounded(__isl_keep isl_set
*set
, int pos
)
5868 return isl_bool_error
;
5870 test
= isl_set_copy(set
);
5871 test
= isl_set_lower_bound_si(test
, isl_dim_set
, pos
, 3);
5872 bounded
= isl_set_is_empty(test
);
5875 if (bounded
< 0 || !bounded
)
5878 test
= isl_set_copy(set
);
5879 test
= isl_set_upper_bound_si(test
, isl_dim_set
, pos
, -3);
5880 bounded
= isl_set_is_empty(test
);
5886 /* Does the set "set" have a fixed (but possible parametric) value
5887 * at dimension "pos"?
5889 static isl_bool
has_single_value(__isl_keep isl_set
*set
, int pos
)
5895 return isl_bool_error
;
5896 set
= isl_set_copy(set
);
5897 n
= isl_set_dim(set
, isl_dim_set
);
5898 set
= isl_set_project_out(set
, isl_dim_set
, pos
+ 1, n
- (pos
+ 1));
5899 set
= isl_set_project_out(set
, isl_dim_set
, 0, pos
);
5900 single
= isl_set_is_singleton(set
);
5906 /* Does "map" have a fixed (but possible parametric) value
5907 * at dimension "pos" of either its domain or its range?
5909 static isl_bool
has_singular_src_or_dst(__isl_keep isl_map
*map
, int pos
)
5914 set
= isl_map_domain(isl_map_copy(map
));
5915 single
= has_single_value(set
, pos
);
5918 if (single
< 0 || single
)
5921 set
= isl_map_range(isl_map_copy(map
));
5922 single
= has_single_value(set
, pos
);
5928 /* Does the edge "edge" from "graph" have bounded dependence distances
5929 * in the merged graph "merge_graph" of a selection of clusters in "c"?
5931 * Extract the complete transformations of the source and destination
5932 * nodes of the edge, apply them to the edge constraints and
5933 * compute the differences. Finally, check if these differences are bounded
5934 * in each direction.
5936 * If the dimension of the band is greater than the number of
5937 * dimensions that can be expected to be optimized by the edge
5938 * (based on its weight), then also allow the differences to be unbounded
5939 * in the remaining dimensions, but only if either the source or
5940 * the destination has a fixed value in that direction.
5941 * This allows a statement that produces values that are used by
5942 * several instances of another statement to be merged with that
5944 * However, merging such clusters will introduce an inherently
5945 * large proximity distance inside the merged cluster, meaning
5946 * that proximity distances will no longer be optimized in
5947 * subsequent merges. These merges are therefore only allowed
5948 * after all other possible merges have been tried.
5949 * The first time such a merge is encountered, the weight of the edge
5950 * is replaced by a negative weight. The second time (i.e., after
5951 * all merges over edges with a non-negative weight have been tried),
5952 * the merge is allowed.
5954 static isl_bool
has_bounded_distances(isl_ctx
*ctx
, struct isl_sched_edge
*edge
,
5955 struct isl_sched_graph
*graph
, struct isl_clustering
*c
,
5956 struct isl_sched_graph
*merge_graph
)
5963 map
= isl_map_copy(edge
->map
);
5964 t
= extract_node_transformation(ctx
, edge
->src
, c
, merge_graph
);
5965 map
= isl_map_apply_domain(map
, t
);
5966 t
= extract_node_transformation(ctx
, edge
->dst
, c
, merge_graph
);
5967 map
= isl_map_apply_range(map
, t
);
5968 dist
= isl_map_deltas(isl_map_copy(map
));
5970 bounded
= isl_bool_true
;
5971 n
= isl_set_dim(dist
, isl_dim_set
);
5972 n_slack
= n
- edge
->weight
;
5973 if (edge
->weight
< 0)
5974 n_slack
-= graph
->max_weight
+ 1;
5975 for (i
= 0; i
< n
; ++i
) {
5976 isl_bool bounded_i
, singular_i
;
5978 bounded_i
= distance_is_bounded(dist
, i
);
5983 if (edge
->weight
>= 0)
5984 bounded
= isl_bool_false
;
5988 singular_i
= has_singular_src_or_dst(map
, i
);
5993 bounded
= isl_bool_false
;
5996 if (!bounded
&& i
>= n
&& edge
->weight
>= 0)
5997 edge
->weight
-= graph
->max_weight
+ 1;
6005 return isl_bool_error
;
6008 /* Should the clusters be merged based on the cluster schedule
6009 * in the current (and only) band of "merge_graph"?
6010 * "graph" is the original dependence graph, while "c" records
6011 * which SCCs are involved in the latest merge.
6013 * In particular, is there at least one proximity constraint
6014 * that is optimized by the merge?
6016 * A proximity constraint is considered to be optimized
6017 * if the dependence distances are small.
6019 static isl_bool
ok_to_merge_proximity(isl_ctx
*ctx
,
6020 struct isl_sched_graph
*graph
, struct isl_clustering
*c
,
6021 struct isl_sched_graph
*merge_graph
)
6025 for (i
= 0; i
< graph
->n_edge
; ++i
) {
6026 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
6029 if (!is_proximity(edge
))
6031 if (!c
->scc_in_merge
[edge
->src
->scc
])
6033 if (!c
->scc_in_merge
[edge
->dst
->scc
])
6035 if (c
->scc_cluster
[edge
->dst
->scc
] ==
6036 c
->scc_cluster
[edge
->src
->scc
])
6038 bounded
= has_bounded_distances(ctx
, edge
, graph
, c
,
6040 if (bounded
< 0 || bounded
)
6044 return isl_bool_false
;
6047 /* Should the clusters be merged based on the cluster schedule
6048 * in the current (and only) band of "merge_graph"?
6049 * "graph" is the original dependence graph, while "c" records
6050 * which SCCs are involved in the latest merge.
6052 * If the current band is empty, then the clusters should not be merged.
6054 * If the band depth should be maximized and the merge schedule
6055 * is incomplete (meaning that the dimension of some of the schedule
6056 * bands in the original schedule will be reduced), then the clusters
6057 * should not be merged.
6059 * If the schedule_maximize_coincidence option is set, then check that
6060 * the number of coincident schedule dimensions is not reduced.
6062 * Finally, only allow the merge if at least one proximity
6063 * constraint is optimized.
6065 static isl_bool
ok_to_merge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6066 struct isl_clustering
*c
, struct isl_sched_graph
*merge_graph
)
6068 if (merge_graph
->n_total_row
== merge_graph
->band_start
)
6069 return isl_bool_false
;
6071 if (isl_options_get_schedule_maximize_band_depth(ctx
) &&
6072 merge_graph
->n_total_row
< merge_graph
->maxvar
)
6073 return isl_bool_false
;
6075 if (isl_options_get_schedule_maximize_coincidence(ctx
)) {
6078 ok
= ok_to_merge_coincident(c
, merge_graph
);
6083 return ok_to_merge_proximity(ctx
, graph
, c
, merge_graph
);
6086 /* Apply the schedule in "t_node" to the "n" rows starting at "first"
6087 * of the schedule in "node" and return the result.
6089 * That is, essentially compute
6091 * T * N(first:first+n-1)
6093 * taking into account the constant term and the parameter coefficients
6096 static __isl_give isl_mat
*node_transformation(isl_ctx
*ctx
,
6097 struct isl_sched_node
*t_node
, struct isl_sched_node
*node
,
6102 int n_row
, n_col
, n_param
, n_var
;
6104 n_param
= node
->nparam
;
6106 n_row
= isl_mat_rows(t_node
->sched
);
6107 n_col
= isl_mat_cols(node
->sched
);
6108 t
= isl_mat_alloc(ctx
, n_row
, n_col
);
6111 for (i
= 0; i
< n_row
; ++i
) {
6112 isl_seq_cpy(t
->row
[i
], t_node
->sched
->row
[i
], 1 + n_param
);
6113 isl_seq_clr(t
->row
[i
] + 1 + n_param
, n_var
);
6114 for (j
= 0; j
< n
; ++j
)
6115 isl_seq_addmul(t
->row
[i
],
6116 t_node
->sched
->row
[i
][1 + n_param
+ j
],
6117 node
->sched
->row
[first
+ j
],
6118 1 + n_param
+ n_var
);
6123 /* Apply the cluster schedule in "t_node" to the current band
6124 * schedule of the nodes in "graph".
6126 * In particular, replace the rows starting at band_start
6127 * by the result of applying the cluster schedule in "t_node"
6128 * to the original rows.
6130 * The coincidence of the schedule is determined by the coincidence
6131 * of the cluster schedule.
6133 static isl_stat
transform(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6134 struct isl_sched_node
*t_node
)
6140 start
= graph
->band_start
;
6141 n
= graph
->n_total_row
- start
;
6143 n_new
= isl_mat_rows(t_node
->sched
);
6144 for (i
= 0; i
< graph
->n
; ++i
) {
6145 struct isl_sched_node
*node
= &graph
->node
[i
];
6148 t
= node_transformation(ctx
, t_node
, node
, start
, n
);
6149 node
->sched
= isl_mat_drop_rows(node
->sched
, start
, n
);
6150 node
->sched
= isl_mat_concat(node
->sched
, t
);
6151 node
->sched_map
= isl_map_free(node
->sched_map
);
6153 return isl_stat_error
;
6154 for (j
= 0; j
< n_new
; ++j
)
6155 node
->coincident
[start
+ j
] = t_node
->coincident
[j
];
6157 graph
->n_total_row
-= n
;
6159 graph
->n_total_row
+= n_new
;
6160 graph
->n_row
+= n_new
;
6165 /* Merge the clusters marked for merging in "c" into a single
6166 * cluster using the cluster schedule in the current band of "merge_graph".
6167 * The representative SCC for the new cluster is the SCC with
6168 * the smallest index.
6170 * The current band schedule of each SCC in the new cluster is obtained
6171 * by applying the schedule of the corresponding original cluster
6172 * to the original band schedule.
6173 * All SCCs in the new cluster have the same number of schedule rows.
6175 static isl_stat
merge(isl_ctx
*ctx
, struct isl_clustering
*c
,
6176 struct isl_sched_graph
*merge_graph
)
6182 for (i
= 0; i
< c
->n
; ++i
) {
6183 struct isl_sched_node
*node
;
6185 if (!c
->scc_in_merge
[i
])
6189 space
= cluster_space(&c
->scc
[i
], c
->scc_cluster
[i
]);
6191 return isl_stat_error
;
6192 node
= graph_find_node(ctx
, merge_graph
, space
);
6193 isl_space_free(space
);
6195 isl_die(ctx
, isl_error_internal
,
6196 "unable to find cluster",
6197 return isl_stat_error
);
6198 if (transform(ctx
, &c
->scc
[i
], node
) < 0)
6199 return isl_stat_error
;
6200 c
->scc_cluster
[i
] = cluster
;
6206 /* Try and merge the clusters of SCCs marked in c->scc_in_merge
6207 * by scheduling the current cluster bands with respect to each other.
6209 * Construct a dependence graph with a space for each cluster and
6210 * with the coordinates of each space corresponding to the schedule
6211 * dimensions of the current band of that cluster.
6212 * Construct a cluster schedule in this cluster dependence graph and
6213 * apply it to the current cluster bands if it is applicable
6214 * according to ok_to_merge.
6216 * If the number of remaining schedule dimensions in a cluster
6217 * with a non-maximal current schedule dimension is greater than
6218 * the number of remaining schedule dimensions in clusters
6219 * with a maximal current schedule dimension, then restrict
6220 * the number of rows to be computed in the cluster schedule
6221 * to the minimal such non-maximal current schedule dimension.
6222 * Do this by adjusting merge_graph.maxvar.
6224 * Return isl_bool_true if the clusters have effectively been merged
6225 * into a single cluster.
6227 * Note that since the standard scheduling algorithm minimizes the maximal
6228 * distance over proximity constraints, the proximity constraints between
6229 * the merged clusters may not be optimized any further than what is
6230 * sufficient to bring the distances within the limits of the internal
6231 * proximity constraints inside the individual clusters.
6232 * It may therefore make sense to perform an additional translation step
6233 * to bring the clusters closer to each other, while maintaining
6234 * the linear part of the merging schedule found using the standard
6235 * scheduling algorithm.
6237 static isl_bool
try_merge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6238 struct isl_clustering
*c
)
6240 struct isl_sched_graph merge_graph
= { 0 };
6243 if (init_merge_graph(ctx
, graph
, c
, &merge_graph
) < 0)
6246 if (compute_maxvar(&merge_graph
) < 0)
6248 if (adjust_maxvar_to_slack(ctx
, &merge_graph
,c
) < 0)
6250 if (compute_schedule_wcc_band(ctx
, &merge_graph
) < 0)
6252 merged
= ok_to_merge(ctx
, graph
, c
, &merge_graph
);
6253 if (merged
&& merge(ctx
, c
, &merge_graph
) < 0)
6256 graph_free(ctx
, &merge_graph
);
6259 graph_free(ctx
, &merge_graph
);
6260 return isl_bool_error
;
6263 /* Is there any edge marked "no_merge" between two SCCs that are
6264 * about to be merged (i.e., that are set in "scc_in_merge")?
6265 * "merge_edge" is the proximity edge along which the clusters of SCCs
6266 * are going to be merged.
6268 * If there is any edge between two SCCs with a negative weight,
6269 * while the weight of "merge_edge" is non-negative, then this
6270 * means that the edge was postponed. "merge_edge" should then
6271 * also be postponed since merging along the edge with negative weight should
6272 * be postponed until all edges with non-negative weight have been tried.
6273 * Replace the weight of "merge_edge" by a negative weight as well and
6274 * tell the caller not to attempt a merge.
6276 static int any_no_merge(struct isl_sched_graph
*graph
, int *scc_in_merge
,
6277 struct isl_sched_edge
*merge_edge
)
6281 for (i
= 0; i
< graph
->n_edge
; ++i
) {
6282 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
6284 if (!scc_in_merge
[edge
->src
->scc
])
6286 if (!scc_in_merge
[edge
->dst
->scc
])
6290 if (merge_edge
->weight
>= 0 && edge
->weight
< 0) {
6291 merge_edge
->weight
-= graph
->max_weight
+ 1;
6299 /* Merge the two clusters in "c" connected by the edge in "graph"
6300 * with index "edge" into a single cluster.
6301 * If it turns out to be impossible to merge these two clusters,
6302 * then mark the edge as "no_merge" such that it will not be
6305 * First mark all SCCs that need to be merged. This includes the SCCs
6306 * in the two clusters, but it may also include the SCCs
6307 * of intermediate clusters.
6308 * If there is already a no_merge edge between any pair of such SCCs,
6309 * then simply mark the current edge as no_merge as well.
6310 * Likewise, if any of those edges was postponed by has_bounded_distances,
6311 * then postpone the current edge as well.
6312 * Otherwise, try and merge the clusters and mark "edge" as "no_merge"
6313 * if the clusters did not end up getting merged, unless the non-merge
6314 * is due to the fact that the edge was postponed. This postponement
6315 * can be recognized by a change in weight (from non-negative to negative).
6317 static isl_stat
merge_clusters_along_edge(isl_ctx
*ctx
,
6318 struct isl_sched_graph
*graph
, int edge
, struct isl_clustering
*c
)
6321 int edge_weight
= graph
->edge
[edge
].weight
;
6323 if (mark_merge_sccs(ctx
, graph
, edge
, c
) < 0)
6324 return isl_stat_error
;
6326 if (any_no_merge(graph
, c
->scc_in_merge
, &graph
->edge
[edge
]))
6327 merged
= isl_bool_false
;
6329 merged
= try_merge(ctx
, graph
, c
);
6331 return isl_stat_error
;
6332 if (!merged
&& edge_weight
== graph
->edge
[edge
].weight
)
6333 graph
->edge
[edge
].no_merge
= 1;
6338 /* Does "node" belong to the cluster identified by "cluster"?
6340 static int node_cluster_exactly(struct isl_sched_node
*node
, int cluster
)
6342 return node
->cluster
== cluster
;
6345 /* Does "edge" connect two nodes belonging to the cluster
6346 * identified by "cluster"?
6348 static int edge_cluster_exactly(struct isl_sched_edge
*edge
, int cluster
)
6350 return edge
->src
->cluster
== cluster
&& edge
->dst
->cluster
== cluster
;
6353 /* Swap the schedule of "node1" and "node2".
6354 * Both nodes have been derived from the same node in a common parent graph.
6355 * Since the "coincident" field is shared with that node
6356 * in the parent graph, there is no need to also swap this field.
6358 static void swap_sched(struct isl_sched_node
*node1
,
6359 struct isl_sched_node
*node2
)
6364 sched
= node1
->sched
;
6365 node1
->sched
= node2
->sched
;
6366 node2
->sched
= sched
;
6368 sched_map
= node1
->sched_map
;
6369 node1
->sched_map
= node2
->sched_map
;
6370 node2
->sched_map
= sched_map
;
6373 /* Copy the current band schedule from the SCCs that form the cluster
6374 * with index "pos" to the actual cluster at position "pos".
6375 * By construction, the index of the first SCC that belongs to the cluster
6378 * The order of the nodes inside both the SCCs and the cluster
6379 * is assumed to be same as the order in the original "graph".
6381 * Since the SCC graphs will no longer be used after this function,
6382 * the schedules are actually swapped rather than copied.
6384 static isl_stat
copy_partial(struct isl_sched_graph
*graph
,
6385 struct isl_clustering
*c
, int pos
)
6389 c
->cluster
[pos
].n_total_row
= c
->scc
[pos
].n_total_row
;
6390 c
->cluster
[pos
].n_row
= c
->scc
[pos
].n_row
;
6391 c
->cluster
[pos
].maxvar
= c
->scc
[pos
].maxvar
;
6393 for (i
= 0; i
< graph
->n
; ++i
) {
6397 if (graph
->node
[i
].cluster
!= pos
)
6399 s
= graph
->node
[i
].scc
;
6400 k
= c
->scc_node
[s
]++;
6401 swap_sched(&c
->cluster
[pos
].node
[j
], &c
->scc
[s
].node
[k
]);
6402 if (c
->scc
[s
].maxvar
> c
->cluster
[pos
].maxvar
)
6403 c
->cluster
[pos
].maxvar
= c
->scc
[s
].maxvar
;
6410 /* Is there a (conditional) validity dependence from node[j] to node[i],
6411 * forcing node[i] to follow node[j] or do the nodes belong to the same
6414 static isl_bool
node_follows_strong_or_same_cluster(int i
, int j
, void *user
)
6416 struct isl_sched_graph
*graph
= user
;
6418 if (graph
->node
[i
].cluster
== graph
->node
[j
].cluster
)
6419 return isl_bool_true
;
6420 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
6423 /* Extract the merged clusters of SCCs in "graph", sort them, and
6424 * store them in c->clusters. Update c->scc_cluster accordingly.
6426 * First keep track of the cluster containing the SCC to which a node
6427 * belongs in the node itself.
6428 * Then extract the clusters into c->clusters, copying the current
6429 * band schedule from the SCCs that belong to the cluster.
6430 * Do this only once per cluster.
6432 * Finally, topologically sort the clusters and update c->scc_cluster
6433 * to match the new scc numbering. While the SCCs were originally
6434 * sorted already, some SCCs that depend on some other SCCs may
6435 * have been merged with SCCs that appear before these other SCCs.
6436 * A reordering may therefore be required.
6438 static isl_stat
extract_clusters(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6439 struct isl_clustering
*c
)
6443 for (i
= 0; i
< graph
->n
; ++i
)
6444 graph
->node
[i
].cluster
= c
->scc_cluster
[graph
->node
[i
].scc
];
6446 for (i
= 0; i
< graph
->scc
; ++i
) {
6447 if (c
->scc_cluster
[i
] != i
)
6449 if (extract_sub_graph(ctx
, graph
, &node_cluster_exactly
,
6450 &edge_cluster_exactly
, i
, &c
->cluster
[i
]) < 0)
6451 return isl_stat_error
;
6452 c
->cluster
[i
].src_scc
= -1;
6453 c
->cluster
[i
].dst_scc
= -1;
6454 if (copy_partial(graph
, c
, i
) < 0)
6455 return isl_stat_error
;
6458 if (detect_ccs(ctx
, graph
, &node_follows_strong_or_same_cluster
) < 0)
6459 return isl_stat_error
;
6460 for (i
= 0; i
< graph
->n
; ++i
)
6461 c
->scc_cluster
[graph
->node
[i
].scc
] = graph
->node
[i
].cluster
;
6466 /* Compute weights on the proximity edges of "graph" that can
6467 * be used by find_proximity to find the most appropriate
6468 * proximity edge to use to merge two clusters in "c".
6469 * The weights are also used by has_bounded_distances to determine
6470 * whether the merge should be allowed.
6471 * Store the maximum of the computed weights in graph->max_weight.
6473 * The computed weight is a measure for the number of remaining schedule
6474 * dimensions that can still be completely aligned.
6475 * In particular, compute the number of equalities between
6476 * input dimensions and output dimensions in the proximity constraints.
6477 * The directions that are already handled by outer schedule bands
6478 * are projected out prior to determining this number.
6480 * Edges that will never be considered by find_proximity are ignored.
6482 static isl_stat
compute_weights(struct isl_sched_graph
*graph
,
6483 struct isl_clustering
*c
)
6487 graph
->max_weight
= 0;
6489 for (i
= 0; i
< graph
->n_edge
; ++i
) {
6490 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
6491 struct isl_sched_node
*src
= edge
->src
;
6492 struct isl_sched_node
*dst
= edge
->dst
;
6493 isl_basic_map
*hull
;
6497 prox
= is_non_empty_proximity(edge
);
6499 return isl_stat_error
;
6502 if (bad_cluster(&c
->scc
[edge
->src
->scc
]) ||
6503 bad_cluster(&c
->scc
[edge
->dst
->scc
]))
6505 if (c
->scc_cluster
[edge
->dst
->scc
] ==
6506 c
->scc_cluster
[edge
->src
->scc
])
6509 hull
= isl_map_affine_hull(isl_map_copy(edge
->map
));
6510 hull
= isl_basic_map_transform_dims(hull
, isl_dim_in
, 0,
6511 isl_mat_copy(src
->ctrans
));
6512 hull
= isl_basic_map_transform_dims(hull
, isl_dim_out
, 0,
6513 isl_mat_copy(dst
->ctrans
));
6514 hull
= isl_basic_map_project_out(hull
,
6515 isl_dim_in
, 0, src
->rank
);
6516 hull
= isl_basic_map_project_out(hull
,
6517 isl_dim_out
, 0, dst
->rank
);
6518 hull
= isl_basic_map_remove_divs(hull
);
6519 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
6520 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
6521 hull
= isl_basic_map_drop_constraints_not_involving_dims(hull
,
6522 isl_dim_in
, 0, n_in
);
6523 hull
= isl_basic_map_drop_constraints_not_involving_dims(hull
,
6524 isl_dim_out
, 0, n_out
);
6526 return isl_stat_error
;
6527 edge
->weight
= isl_basic_map_n_equality(hull
);
6528 isl_basic_map_free(hull
);
6530 if (edge
->weight
> graph
->max_weight
)
6531 graph
->max_weight
= edge
->weight
;
6537 /* Call compute_schedule_finish_band on each of the clusters in "c"
6538 * in their topological order. This order is determined by the scc
6539 * fields of the nodes in "graph".
6540 * Combine the results in a sequence expressing the topological order.
6542 * If there is only one cluster left, then there is no need to introduce
6543 * a sequence node. Also, in this case, the cluster necessarily contains
6544 * the SCC at position 0 in the original graph and is therefore also
6545 * stored in the first cluster of "c".
6547 static __isl_give isl_schedule_node
*finish_bands_clustering(
6548 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
6549 struct isl_clustering
*c
)
6553 isl_union_set_list
*filters
;
6555 if (graph
->scc
== 1)
6556 return compute_schedule_finish_band(node
, &c
->cluster
[0], 0);
6558 ctx
= isl_schedule_node_get_ctx(node
);
6560 filters
= extract_sccs(ctx
, graph
);
6561 node
= isl_schedule_node_insert_sequence(node
, filters
);
6563 for (i
= 0; i
< graph
->scc
; ++i
) {
6564 int j
= c
->scc_cluster
[i
];
6565 node
= isl_schedule_node_child(node
, i
);
6566 node
= isl_schedule_node_child(node
, 0);
6567 node
= compute_schedule_finish_band(node
, &c
->cluster
[j
], 0);
6568 node
= isl_schedule_node_parent(node
);
6569 node
= isl_schedule_node_parent(node
);
6575 /* Compute a schedule for a connected dependence graph by first considering
6576 * each strongly connected component (SCC) in the graph separately and then
6577 * incrementally combining them into clusters.
6578 * Return the updated schedule node.
6580 * Initially, each cluster consists of a single SCC, each with its
6581 * own band schedule. The algorithm then tries to merge pairs
6582 * of clusters along a proximity edge until no more suitable
6583 * proximity edges can be found. During this merging, the schedule
6584 * is maintained in the individual SCCs.
6585 * After the merging is completed, the full resulting clusters
6586 * are extracted and in finish_bands_clustering,
6587 * compute_schedule_finish_band is called on each of them to integrate
6588 * the band into "node" and to continue the computation.
6590 * compute_weights initializes the weights that are used by find_proximity.
6592 static __isl_give isl_schedule_node
*compute_schedule_wcc_clustering(
6593 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
6596 struct isl_clustering c
;
6599 ctx
= isl_schedule_node_get_ctx(node
);
6601 if (clustering_init(ctx
, &c
, graph
) < 0)
6604 if (compute_weights(graph
, &c
) < 0)
6608 i
= find_proximity(graph
, &c
);
6611 if (i
>= graph
->n_edge
)
6613 if (merge_clusters_along_edge(ctx
, graph
, i
, &c
) < 0)
6617 if (extract_clusters(ctx
, graph
, &c
) < 0)
6620 node
= finish_bands_clustering(node
, graph
, &c
);
6622 clustering_free(ctx
, &c
);
6625 clustering_free(ctx
, &c
);
6626 return isl_schedule_node_free(node
);
6629 /* Compute a schedule for a connected dependence graph and return
6630 * the updated schedule node.
6632 * If Feautrier's algorithm is selected, we first recursively try to satisfy
6633 * as many validity dependences as possible. When all validity dependences
6634 * are satisfied we extend the schedule to a full-dimensional schedule.
6636 * Call compute_schedule_wcc_whole or compute_schedule_wcc_clustering
6637 * depending on whether the user has selected the option to try and
6638 * compute a schedule for the entire (weakly connected) component first.
6639 * If there is only a single strongly connected component (SCC), then
6640 * there is no point in trying to combine SCCs
6641 * in compute_schedule_wcc_clustering, so compute_schedule_wcc_whole
6642 * is called instead.
6644 static __isl_give isl_schedule_node
*compute_schedule_wcc(
6645 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
6652 ctx
= isl_schedule_node_get_ctx(node
);
6653 if (detect_sccs(ctx
, graph
) < 0)
6654 return isl_schedule_node_free(node
);
6656 if (compute_maxvar(graph
) < 0)
6657 return isl_schedule_node_free(node
);
6659 if (need_feautrier_step(ctx
, graph
))
6660 return compute_schedule_wcc_feautrier(node
, graph
);
6662 if (graph
->scc
<= 1 || isl_options_get_schedule_whole_component(ctx
))
6663 return compute_schedule_wcc_whole(node
, graph
);
6665 return compute_schedule_wcc_clustering(node
, graph
);
6668 /* Compute a schedule for each group of nodes identified by node->scc
6669 * separately and then combine them in a sequence node (or as set node
6670 * if graph->weak is set) inserted at position "node" of the schedule tree.
6671 * Return the updated schedule node.
6673 * If "wcc" is set then each of the groups belongs to a single
6674 * weakly connected component in the dependence graph so that
6675 * there is no need for compute_sub_schedule to look for weakly
6676 * connected components.
6678 static __isl_give isl_schedule_node
*compute_component_schedule(
6679 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
6684 isl_union_set_list
*filters
;
6688 ctx
= isl_schedule_node_get_ctx(node
);
6690 filters
= extract_sccs(ctx
, graph
);
6692 node
= isl_schedule_node_insert_set(node
, filters
);
6694 node
= isl_schedule_node_insert_sequence(node
, filters
);
6696 for (component
= 0; component
< graph
->scc
; ++component
) {
6697 node
= isl_schedule_node_child(node
, component
);
6698 node
= isl_schedule_node_child(node
, 0);
6699 node
= compute_sub_schedule(node
, ctx
, graph
,
6701 &edge_scc_exactly
, component
, wcc
);
6702 node
= isl_schedule_node_parent(node
);
6703 node
= isl_schedule_node_parent(node
);
6709 /* Compute a schedule for the given dependence graph and insert it at "node".
6710 * Return the updated schedule node.
6712 * We first check if the graph is connected (through validity and conditional
6713 * validity dependences) and, if not, compute a schedule
6714 * for each component separately.
6715 * If the schedule_serialize_sccs option is set, then we check for strongly
6716 * connected components instead and compute a separate schedule for
6717 * each such strongly connected component.
6719 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
6720 struct isl_sched_graph
*graph
)
6727 ctx
= isl_schedule_node_get_ctx(node
);
6728 if (isl_options_get_schedule_serialize_sccs(ctx
)) {
6729 if (detect_sccs(ctx
, graph
) < 0)
6730 return isl_schedule_node_free(node
);
6732 if (detect_wccs(ctx
, graph
) < 0)
6733 return isl_schedule_node_free(node
);
6737 return compute_component_schedule(node
, graph
, 1);
6739 return compute_schedule_wcc(node
, graph
);
6742 /* Compute a schedule on sc->domain that respects the given schedule
6745 * In particular, the schedule respects all the validity dependences.
6746 * If the default isl scheduling algorithm is used, it tries to minimize
6747 * the dependence distances over the proximity dependences.
6748 * If Feautrier's scheduling algorithm is used, the proximity dependence
6749 * distances are only minimized during the extension to a full-dimensional
6752 * If there are any condition and conditional validity dependences,
6753 * then the conditional validity dependences may be violated inside
6754 * a tilable band, provided they have no adjacent non-local
6755 * condition dependences.
6757 __isl_give isl_schedule
*isl_schedule_constraints_compute_schedule(
6758 __isl_take isl_schedule_constraints
*sc
)
6760 isl_ctx
*ctx
= isl_schedule_constraints_get_ctx(sc
);
6761 struct isl_sched_graph graph
= { 0 };
6762 isl_schedule
*sched
;
6763 isl_schedule_node
*node
;
6764 isl_union_set
*domain
;
6766 sc
= isl_schedule_constraints_align_params(sc
);
6768 domain
= isl_schedule_constraints_get_domain(sc
);
6769 if (isl_union_set_n_set(domain
) == 0) {
6770 isl_schedule_constraints_free(sc
);
6771 return isl_schedule_from_domain(domain
);
6774 if (graph_init(&graph
, sc
) < 0)
6775 domain
= isl_union_set_free(domain
);
6777 node
= isl_schedule_node_from_domain(domain
);
6778 node
= isl_schedule_node_child(node
, 0);
6780 node
= compute_schedule(node
, &graph
);
6781 sched
= isl_schedule_node_get_schedule(node
);
6782 isl_schedule_node_free(node
);
6784 graph_free(ctx
, &graph
);
6785 isl_schedule_constraints_free(sc
);
6790 /* Compute a schedule for the given union of domains that respects
6791 * all the validity dependences and minimizes
6792 * the dependence distances over the proximity dependences.
6794 * This function is kept for backward compatibility.
6796 __isl_give isl_schedule
*isl_union_set_compute_schedule(
6797 __isl_take isl_union_set
*domain
,
6798 __isl_take isl_union_map
*validity
,
6799 __isl_take isl_union_map
*proximity
)
6801 isl_schedule_constraints
*sc
;
6803 sc
= isl_schedule_constraints_on_domain(domain
);
6804 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
6805 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
6807 return isl_schedule_constraints_compute_schedule(sc
);