2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2014 Ecole Normale Superieure
4 * Copyright 2015-2016 Sven Verdoolaege
5 * Copyright 2016 INRIA Paris
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
14 * CS 42112, 75589 Paris Cedex 12, France
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
19 #include <isl_space_private.h>
20 #include <isl_aff_private.h>
22 #include <isl/constraint.h>
23 #include <isl/schedule.h>
24 #include <isl_schedule_constraints.h>
25 #include <isl/schedule_node.h>
26 #include <isl_mat_private.h>
27 #include <isl_vec_private.h>
29 #include <isl/union_set.h>
32 #include <isl_dim_map.h>
33 #include <isl/map_to_basic_set.h>
35 #include <isl_options_private.h>
36 #include <isl_tarjan.h>
37 #include <isl_morph.h>
39 #include <isl_val_private.h>
42 * The scheduling algorithm implemented in this file was inspired by
43 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
44 * Parallelization and Locality Optimization in the Polyhedral Model".
48 /* Internal information about a node that is used during the construction
50 * space represents the space in which the domain lives
51 * sched is a matrix representation of the schedule being constructed
52 * for this node; if compressed is set, then this schedule is
53 * defined over the compressed domain space
54 * sched_map is an isl_map representation of the same (partial) schedule
55 * sched_map may be NULL; if compressed is set, then this map
56 * is defined over the uncompressed domain space
57 * rank is the number of linearly independent rows in the linear part
59 * the columns of cmap represent a change of basis for the schedule
60 * coefficients; the first rank columns span the linear part of
62 * cinv is the inverse of cmap.
63 * ctrans is the transpose of cmap.
64 * start is the first variable in the LP problem in the sequences that
65 * represents the schedule coefficients of this node
66 * nvar is the dimension of the domain
67 * nparam is the number of parameters or 0 if we are not constructing
68 * a parametric schedule
70 * If compressed is set, then hull represents the constraints
71 * that were used to derive the compression, while compress and
72 * decompress map the original space to the compressed space and
75 * scc is the index of SCC (or WCC) this node belongs to
77 * "cluster" is only used inside extract_clusters and identifies
78 * the cluster of SCCs that the node belongs to.
80 * coincident contains a boolean for each of the rows of the schedule,
81 * indicating whether the corresponding scheduling dimension satisfies
82 * the coincidence constraints in the sense that the corresponding
83 * dependence distances are zero.
85 * If the schedule_treat_coalescing option is set, then
86 * "sizes" contains the sizes of the (compressed) instance set
87 * in each direction. If there is no fixed size in a given direction,
88 * then the corresponding size value is set to infinity.
89 * If the schedule_treat_coalescing option or the schedule_max_coefficient
90 * option is set, then "max" contains the maximal values for
91 * schedule coefficients of the (compressed) variables. If no bound
92 * needs to be imposed on a particular variable, then the corresponding
95 struct isl_sched_node
{
99 isl_multi_aff
*compress
;
100 isl_multi_aff
*decompress
;
116 isl_multi_val
*sizes
;
120 static int node_has_space(const void *entry
, const void *val
)
122 struct isl_sched_node
*node
= (struct isl_sched_node
*)entry
;
123 isl_space
*dim
= (isl_space
*)val
;
125 return isl_space_is_equal(node
->space
, dim
);
128 static int node_scc_exactly(struct isl_sched_node
*node
, int scc
)
130 return node
->scc
== scc
;
133 static int node_scc_at_most(struct isl_sched_node
*node
, int scc
)
135 return node
->scc
<= scc
;
138 static int node_scc_at_least(struct isl_sched_node
*node
, int scc
)
140 return node
->scc
>= scc
;
143 /* An edge in the dependence graph. An edge may be used to
144 * ensure validity of the generated schedule, to minimize the dependence
147 * map is the dependence relation, with i -> j in the map if j depends on i
148 * tagged_condition and tagged_validity contain the union of all tagged
149 * condition or conditional validity dependence relations that
150 * specialize the dependence relation "map"; that is,
151 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
152 * or "tagged_validity", then i -> j is an element of "map".
153 * If these fields are NULL, then they represent the empty relation.
154 * src is the source node
155 * dst is the sink node
157 * types is a bit vector containing the types of this edge.
158 * validity is set if the edge is used to ensure correctness
159 * coincidence is used to enforce zero dependence distances
160 * proximity is set if the edge is used to minimize dependence distances
161 * condition is set if the edge represents a condition
162 * for a conditional validity schedule constraint
163 * local can only be set for condition edges and indicates that
164 * the dependence distance over the edge should be zero
165 * conditional_validity is set if the edge is used to conditionally
168 * For validity edges, start and end mark the sequence of inequality
169 * constraints in the LP problem that encode the validity constraint
170 * corresponding to this edge.
172 * During clustering, an edge may be marked "no_merge" if it should
173 * not be used to merge clusters.
174 * The weight is also only used during clustering and it is
175 * an indication of how many schedule dimensions on either side
176 * of the schedule constraints can be aligned.
177 * If the weight is negative, then this means that this edge was postponed
178 * by has_bounded_distances or any_no_merge. The original weight can
179 * be retrieved by adding 1 + graph->max_weight, with "graph"
180 * the graph containing this edge.
182 struct isl_sched_edge
{
184 isl_union_map
*tagged_condition
;
185 isl_union_map
*tagged_validity
;
187 struct isl_sched_node
*src
;
188 struct isl_sched_node
*dst
;
199 /* Is "edge" marked as being of type "type"?
201 static int is_type(struct isl_sched_edge
*edge
, enum isl_edge_type type
)
203 return ISL_FL_ISSET(edge
->types
, 1 << type
);
206 /* Mark "edge" as being of type "type".
208 static void set_type(struct isl_sched_edge
*edge
, enum isl_edge_type type
)
210 ISL_FL_SET(edge
->types
, 1 << type
);
213 /* No longer mark "edge" as being of type "type"?
215 static void clear_type(struct isl_sched_edge
*edge
, enum isl_edge_type type
)
217 ISL_FL_CLR(edge
->types
, 1 << type
);
220 /* Is "edge" marked as a validity edge?
222 static int is_validity(struct isl_sched_edge
*edge
)
224 return is_type(edge
, isl_edge_validity
);
227 /* Mark "edge" as a validity edge.
229 static void set_validity(struct isl_sched_edge
*edge
)
231 set_type(edge
, isl_edge_validity
);
234 /* Is "edge" marked as a proximity edge?
236 static int is_proximity(struct isl_sched_edge
*edge
)
238 return is_type(edge
, isl_edge_proximity
);
241 /* Is "edge" marked as a local edge?
243 static int is_local(struct isl_sched_edge
*edge
)
245 return is_type(edge
, isl_edge_local
);
248 /* Mark "edge" as a local edge.
250 static void set_local(struct isl_sched_edge
*edge
)
252 set_type(edge
, isl_edge_local
);
255 /* No longer mark "edge" as a local edge.
257 static void clear_local(struct isl_sched_edge
*edge
)
259 clear_type(edge
, isl_edge_local
);
262 /* Is "edge" marked as a coincidence edge?
264 static int is_coincidence(struct isl_sched_edge
*edge
)
266 return is_type(edge
, isl_edge_coincidence
);
269 /* Is "edge" marked as a condition edge?
271 static int is_condition(struct isl_sched_edge
*edge
)
273 return is_type(edge
, isl_edge_condition
);
276 /* Is "edge" marked as a conditional validity edge?
278 static int is_conditional_validity(struct isl_sched_edge
*edge
)
280 return is_type(edge
, isl_edge_conditional_validity
);
283 /* Is "edge" of a type that can appear multiple times between
284 * the same pair of nodes?
286 * Condition edges and conditional validity edges may have tagged
287 * dependence relations, in which case an edge is added for each
290 static int is_multi_edge_type(struct isl_sched_edge
*edge
)
292 return is_condition(edge
) || is_conditional_validity(edge
);
295 /* Internal information about the dependence graph used during
296 * the construction of the schedule.
298 * intra_hmap is a cache, mapping dependence relations to their dual,
299 * for dependences from a node to itself
300 * inter_hmap is a cache, mapping dependence relations to their dual,
301 * for dependences between distinct nodes
302 * if compression is involved then the key for these maps
303 * is the original, uncompressed dependence relation, while
304 * the value is the dual of the compressed dependence relation.
306 * n is the number of nodes
307 * node is the list of nodes
308 * maxvar is the maximal number of variables over all nodes
309 * max_row is the allocated number of rows in the schedule
310 * n_row is the current (maximal) number of linearly independent
311 * rows in the node schedules
312 * n_total_row is the current number of rows in the node schedules
313 * band_start is the starting row in the node schedules of the current band
314 * root is set if this graph is the original dependence graph,
315 * without any splitting
317 * sorted contains a list of node indices sorted according to the
318 * SCC to which a node belongs
320 * n_edge is the number of edges
321 * edge is the list of edges
322 * max_edge contains the maximal number of edges of each type;
323 * in particular, it contains the number of edges in the inital graph.
324 * edge_table contains pointers into the edge array, hashed on the source
325 * and sink spaces; there is one such table for each type;
326 * a given edge may be referenced from more than one table
327 * if the corresponding relation appears in more than one of the
328 * sets of dependences; however, for each type there is only
329 * a single edge between a given pair of source and sink space
330 * in the entire graph
332 * node_table contains pointers into the node array, hashed on the space
334 * region contains a list of variable sequences that should be non-trivial
336 * lp contains the (I)LP problem used to obtain new schedule rows
338 * src_scc and dst_scc are the source and sink SCCs of an edge with
339 * conflicting constraints
341 * scc represents the number of components
342 * weak is set if the components are weakly connected
344 * max_weight is used during clustering and represents the maximal
345 * weight of the relevant proximity edges.
347 struct isl_sched_graph
{
348 isl_map_to_basic_set
*intra_hmap
;
349 isl_map_to_basic_set
*inter_hmap
;
351 struct isl_sched_node
*node
;
364 struct isl_sched_edge
*edge
;
366 int max_edge
[isl_edge_last
+ 1];
367 struct isl_hash_table
*edge_table
[isl_edge_last
+ 1];
369 struct isl_hash_table
*node_table
;
370 struct isl_region
*region
;
383 /* Initialize node_table based on the list of nodes.
385 static int graph_init_table(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
389 graph
->node_table
= isl_hash_table_alloc(ctx
, graph
->n
);
390 if (!graph
->node_table
)
393 for (i
= 0; i
< graph
->n
; ++i
) {
394 struct isl_hash_table_entry
*entry
;
397 hash
= isl_space_get_hash(graph
->node
[i
].space
);
398 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
400 graph
->node
[i
].space
, 1);
403 entry
->data
= &graph
->node
[i
];
409 /* Return a pointer to the node that lives within the given space,
410 * or NULL if there is no such node.
412 static struct isl_sched_node
*graph_find_node(isl_ctx
*ctx
,
413 struct isl_sched_graph
*graph
, __isl_keep isl_space
*dim
)
415 struct isl_hash_table_entry
*entry
;
418 hash
= isl_space_get_hash(dim
);
419 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
420 &node_has_space
, dim
, 0);
422 return entry
? entry
->data
: NULL
;
425 static int edge_has_src_and_dst(const void *entry
, const void *val
)
427 const struct isl_sched_edge
*edge
= entry
;
428 const struct isl_sched_edge
*temp
= val
;
430 return edge
->src
== temp
->src
&& edge
->dst
== temp
->dst
;
433 /* Add the given edge to graph->edge_table[type].
435 static isl_stat
graph_edge_table_add(isl_ctx
*ctx
,
436 struct isl_sched_graph
*graph
, enum isl_edge_type type
,
437 struct isl_sched_edge
*edge
)
439 struct isl_hash_table_entry
*entry
;
442 hash
= isl_hash_init();
443 hash
= isl_hash_builtin(hash
, edge
->src
);
444 hash
= isl_hash_builtin(hash
, edge
->dst
);
445 entry
= isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
446 &edge_has_src_and_dst
, edge
, 1);
448 return isl_stat_error
;
454 /* Add "edge" to all relevant edge tables.
455 * That is, for every type of the edge, add it to the corresponding table.
457 static isl_stat
graph_edge_tables_add(isl_ctx
*ctx
,
458 struct isl_sched_graph
*graph
, struct isl_sched_edge
*edge
)
460 enum isl_edge_type t
;
462 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
463 if (!is_type(edge
, t
))
465 if (graph_edge_table_add(ctx
, graph
, t
, edge
) < 0)
466 return isl_stat_error
;
472 /* Allocate the edge_tables based on the maximal number of edges of
475 static int graph_init_edge_tables(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
479 for (i
= 0; i
<= isl_edge_last
; ++i
) {
480 graph
->edge_table
[i
] = isl_hash_table_alloc(ctx
,
482 if (!graph
->edge_table
[i
])
489 /* If graph->edge_table[type] contains an edge from the given source
490 * to the given destination, then return the hash table entry of this edge.
491 * Otherwise, return NULL.
493 static struct isl_hash_table_entry
*graph_find_edge_entry(
494 struct isl_sched_graph
*graph
,
495 enum isl_edge_type type
,
496 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
498 isl_ctx
*ctx
= isl_space_get_ctx(src
->space
);
500 struct isl_sched_edge temp
= { .src
= src
, .dst
= dst
};
502 hash
= isl_hash_init();
503 hash
= isl_hash_builtin(hash
, temp
.src
);
504 hash
= isl_hash_builtin(hash
, temp
.dst
);
505 return isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
506 &edge_has_src_and_dst
, &temp
, 0);
510 /* If graph->edge_table[type] contains an edge from the given source
511 * to the given destination, then return this edge.
512 * Otherwise, return NULL.
514 static struct isl_sched_edge
*graph_find_edge(struct isl_sched_graph
*graph
,
515 enum isl_edge_type type
,
516 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
518 struct isl_hash_table_entry
*entry
;
520 entry
= graph_find_edge_entry(graph
, type
, src
, dst
);
527 /* Check whether the dependence graph has an edge of the given type
528 * between the given two nodes.
530 static isl_bool
graph_has_edge(struct isl_sched_graph
*graph
,
531 enum isl_edge_type type
,
532 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
534 struct isl_sched_edge
*edge
;
537 edge
= graph_find_edge(graph
, type
, src
, dst
);
541 empty
= isl_map_plain_is_empty(edge
->map
);
543 return isl_bool_error
;
548 /* Look for any edge with the same src, dst and map fields as "model".
550 * Return the matching edge if one can be found.
551 * Return "model" if no matching edge is found.
552 * Return NULL on error.
554 static struct isl_sched_edge
*graph_find_matching_edge(
555 struct isl_sched_graph
*graph
, struct isl_sched_edge
*model
)
557 enum isl_edge_type i
;
558 struct isl_sched_edge
*edge
;
560 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
563 edge
= graph_find_edge(graph
, i
, model
->src
, model
->dst
);
566 is_equal
= isl_map_plain_is_equal(model
->map
, edge
->map
);
576 /* Remove the given edge from all the edge_tables that refer to it.
578 static void graph_remove_edge(struct isl_sched_graph
*graph
,
579 struct isl_sched_edge
*edge
)
581 isl_ctx
*ctx
= isl_map_get_ctx(edge
->map
);
582 enum isl_edge_type i
;
584 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
585 struct isl_hash_table_entry
*entry
;
587 entry
= graph_find_edge_entry(graph
, i
, edge
->src
, edge
->dst
);
590 if (entry
->data
!= edge
)
592 isl_hash_table_remove(ctx
, graph
->edge_table
[i
], entry
);
596 /* Check whether the dependence graph has any edge
597 * between the given two nodes.
599 static isl_bool
graph_has_any_edge(struct isl_sched_graph
*graph
,
600 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
602 enum isl_edge_type i
;
605 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
606 r
= graph_has_edge(graph
, i
, src
, dst
);
614 /* Check whether the dependence graph has a validity edge
615 * between the given two nodes.
617 * Conditional validity edges are essentially validity edges that
618 * can be ignored if the corresponding condition edges are iteration private.
619 * Here, we are only checking for the presence of validity
620 * edges, so we need to consider the conditional validity edges too.
621 * In particular, this function is used during the detection
622 * of strongly connected components and we cannot ignore
623 * conditional validity edges during this detection.
625 static isl_bool
graph_has_validity_edge(struct isl_sched_graph
*graph
,
626 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
630 r
= graph_has_edge(graph
, isl_edge_validity
, src
, dst
);
634 return graph_has_edge(graph
, isl_edge_conditional_validity
, src
, dst
);
637 static int graph_alloc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
638 int n_node
, int n_edge
)
643 graph
->n_edge
= n_edge
;
644 graph
->node
= isl_calloc_array(ctx
, struct isl_sched_node
, graph
->n
);
645 graph
->sorted
= isl_calloc_array(ctx
, int, graph
->n
);
646 graph
->region
= isl_alloc_array(ctx
, struct isl_region
, graph
->n
);
647 graph
->edge
= isl_calloc_array(ctx
,
648 struct isl_sched_edge
, graph
->n_edge
);
650 graph
->intra_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
651 graph
->inter_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
653 if (!graph
->node
|| !graph
->region
|| (graph
->n_edge
&& !graph
->edge
) ||
657 for(i
= 0; i
< graph
->n
; ++i
)
658 graph
->sorted
[i
] = i
;
663 static void graph_free(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
667 isl_map_to_basic_set_free(graph
->intra_hmap
);
668 isl_map_to_basic_set_free(graph
->inter_hmap
);
671 for (i
= 0; i
< graph
->n
; ++i
) {
672 isl_space_free(graph
->node
[i
].space
);
673 isl_set_free(graph
->node
[i
].hull
);
674 isl_multi_aff_free(graph
->node
[i
].compress
);
675 isl_multi_aff_free(graph
->node
[i
].decompress
);
676 isl_mat_free(graph
->node
[i
].sched
);
677 isl_map_free(graph
->node
[i
].sched_map
);
678 isl_mat_free(graph
->node
[i
].cmap
);
679 isl_mat_free(graph
->node
[i
].cinv
);
680 isl_mat_free(graph
->node
[i
].ctrans
);
682 free(graph
->node
[i
].coincident
);
683 isl_multi_val_free(graph
->node
[i
].sizes
);
684 isl_vec_free(graph
->node
[i
].max
);
689 for (i
= 0; i
< graph
->n_edge
; ++i
) {
690 isl_map_free(graph
->edge
[i
].map
);
691 isl_union_map_free(graph
->edge
[i
].tagged_condition
);
692 isl_union_map_free(graph
->edge
[i
].tagged_validity
);
696 for (i
= 0; i
<= isl_edge_last
; ++i
)
697 isl_hash_table_free(ctx
, graph
->edge_table
[i
]);
698 isl_hash_table_free(ctx
, graph
->node_table
);
699 isl_basic_set_free(graph
->lp
);
702 /* For each "set" on which this function is called, increment
703 * graph->n by one and update graph->maxvar.
705 static isl_stat
init_n_maxvar(__isl_take isl_set
*set
, void *user
)
707 struct isl_sched_graph
*graph
= user
;
708 int nvar
= isl_set_dim(set
, isl_dim_set
);
711 if (nvar
> graph
->maxvar
)
712 graph
->maxvar
= nvar
;
719 /* Compute the number of rows that should be allocated for the schedule.
720 * In particular, we need one row for each variable or one row
721 * for each basic map in the dependences.
722 * Note that it is practically impossible to exhaust both
723 * the number of dependences and the number of variables.
725 static isl_stat
compute_max_row(struct isl_sched_graph
*graph
,
726 __isl_keep isl_schedule_constraints
*sc
)
730 isl_union_set
*domain
;
734 domain
= isl_schedule_constraints_get_domain(sc
);
735 r
= isl_union_set_foreach_set(domain
, &init_n_maxvar
, graph
);
736 isl_union_set_free(domain
);
738 return isl_stat_error
;
739 n_edge
= isl_schedule_constraints_n_basic_map(sc
);
741 return isl_stat_error
;
742 graph
->max_row
= n_edge
+ graph
->maxvar
;
747 /* Does "bset" have any defining equalities for its set variables?
749 static int has_any_defining_equality(__isl_keep isl_basic_set
*bset
)
756 n
= isl_basic_set_dim(bset
, isl_dim_set
);
757 for (i
= 0; i
< n
; ++i
) {
760 has
= isl_basic_set_has_defining_equality(bset
, isl_dim_set
, i
,
769 /* Set the entries of node->max to the value of the schedule_max_coefficient
772 static isl_stat
set_max_coefficient(isl_ctx
*ctx
, struct isl_sched_node
*node
)
776 max
= isl_options_get_schedule_max_coefficient(ctx
);
780 node
->max
= isl_vec_alloc(ctx
, node
->nvar
);
781 node
->max
= isl_vec_set_si(node
->max
, max
);
783 return isl_stat_error
;
788 /* Set the entries of node->max to the minimum of the schedule_max_coefficient
789 * option (if set) and half of the minimum of the sizes in the other
790 * dimensions. If the minimum of the sizes is one, half of the size
791 * is zero and this value is reset to one.
792 * If the global minimum is unbounded (i.e., if both
793 * the schedule_max_coefficient is not set and the sizes in the other
794 * dimensions are unbounded), then store a negative value.
795 * If the schedule coefficient is close to the size of the instance set
796 * in another dimension, then the schedule may represent a loop
797 * coalescing transformation (especially if the coefficient
798 * in that other dimension is one). Forcing the coefficient to be
799 * smaller than or equal to half the minimal size should avoid this
802 static isl_stat
compute_max_coefficient(isl_ctx
*ctx
,
803 struct isl_sched_node
*node
)
809 max
= isl_options_get_schedule_max_coefficient(ctx
);
810 v
= isl_vec_alloc(ctx
, node
->nvar
);
812 return isl_stat_error
;
814 for (i
= 0; i
< node
->nvar
; ++i
) {
815 isl_int_set_si(v
->el
[i
], max
);
816 isl_int_mul_si(v
->el
[i
], v
->el
[i
], 2);
819 for (i
= 0; i
< node
->nvar
; ++i
) {
822 size
= isl_multi_val_get_val(node
->sizes
, i
);
825 if (!isl_val_is_int(size
)) {
829 for (j
= 0; j
< node
->nvar
; ++j
) {
832 if (isl_int_is_neg(v
->el
[j
]) ||
833 isl_int_gt(v
->el
[j
], size
->n
))
834 isl_int_set(v
->el
[j
], size
->n
);
839 for (i
= 0; i
< node
->nvar
; ++i
) {
840 isl_int_fdiv_q_ui(v
->el
[i
], v
->el
[i
], 2);
841 if (isl_int_is_zero(v
->el
[i
]))
842 isl_int_set_si(v
->el
[i
], 1);
849 return isl_stat_error
;
852 /* Compute and return the size of "set" in dimension "dim".
853 * The size is taken to be the difference in values for that variable
854 * for fixed values of the other variables.
855 * In particular, the variable is first isolated from the other variables
856 * in the range of a map
858 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [i_dim]
860 * and then duplicated
862 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [[i_dim] -> [i_dim']]
864 * The shared variables are then projected out and the maximal value
865 * of i_dim' - i_dim is computed.
867 static __isl_give isl_val
*compute_size(__isl_take isl_set
*set
, int dim
)
874 map
= isl_set_project_onto_map(set
, isl_dim_set
, dim
, 1);
875 map
= isl_map_project_out(map
, isl_dim_in
, dim
, 1);
876 map
= isl_map_range_product(map
, isl_map_copy(map
));
877 map
= isl_set_unwrap(isl_map_range(map
));
878 set
= isl_map_deltas(map
);
879 ls
= isl_local_space_from_space(isl_set_get_space(set
));
880 obj
= isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
881 v
= isl_set_max_val(set
, obj
);
888 /* Compute the size of the instance set "set" of "node", after compression,
889 * as well as bounds on the corresponding coefficients, if needed.
891 * The sizes are needed when the schedule_treat_coalescing option is set.
892 * The bounds are needed when the schedule_treat_coalescing option or
893 * the schedule_max_coefficient option is set.
895 * If the schedule_treat_coalescing option is not set, then at most
896 * the bounds need to be set and this is done in set_max_coefficient.
897 * Otherwise, compress the domain if needed, compute the size
898 * in each direction and store the results in node->size.
899 * Finally, set the bounds on the coefficients based on the sizes
900 * and the schedule_max_coefficient option in compute_max_coefficient.
902 static isl_stat
compute_sizes_and_max(isl_ctx
*ctx
, struct isl_sched_node
*node
,
903 __isl_take isl_set
*set
)
908 if (!isl_options_get_schedule_treat_coalescing(ctx
)) {
910 return set_max_coefficient(ctx
, node
);
913 if (node
->compressed
)
914 set
= isl_set_preimage_multi_aff(set
,
915 isl_multi_aff_copy(node
->decompress
));
916 mv
= isl_multi_val_zero(isl_set_get_space(set
));
917 n
= isl_set_dim(set
, isl_dim_set
);
918 for (j
= 0; j
< n
; ++j
) {
921 v
= compute_size(isl_set_copy(set
), j
);
922 mv
= isl_multi_val_set_val(mv
, j
, v
);
927 return isl_stat_error
;
928 return compute_max_coefficient(ctx
, node
);
931 /* Add a new node to the graph representing the given instance set.
932 * "nvar" is the (possibly compressed) number of variables and
933 * may be smaller than then number of set variables in "set"
934 * if "compressed" is set.
935 * If "compressed" is set, then "hull" represents the constraints
936 * that were used to derive the compression, while "compress" and
937 * "decompress" map the original space to the compressed space and
939 * If "compressed" is not set, then "hull", "compress" and "decompress"
942 * Compute the size of the instance set and bounds on the coefficients,
945 static isl_stat
add_node(struct isl_sched_graph
*graph
,
946 __isl_take isl_set
*set
, int nvar
, int compressed
,
947 __isl_take isl_set
*hull
, __isl_take isl_multi_aff
*compress
,
948 __isl_take isl_multi_aff
*decompress
)
955 struct isl_sched_node
*node
;
958 return isl_stat_error
;
960 ctx
= isl_set_get_ctx(set
);
961 nparam
= isl_set_dim(set
, isl_dim_param
);
962 if (!ctx
->opt
->schedule_parametric
)
964 sched
= isl_mat_alloc(ctx
, 0, 1 + nparam
+ nvar
);
965 node
= &graph
->node
[graph
->n
];
967 space
= isl_set_get_space(set
);
970 node
->nparam
= nparam
;
972 node
->sched_map
= NULL
;
973 coincident
= isl_calloc_array(ctx
, int, graph
->max_row
);
974 node
->coincident
= coincident
;
975 node
->compressed
= compressed
;
977 node
->compress
= compress
;
978 node
->decompress
= decompress
;
979 if (compute_sizes_and_max(ctx
, node
, set
) < 0)
980 return isl_stat_error
;
982 if (!space
|| !sched
|| (graph
->max_row
&& !coincident
))
983 return isl_stat_error
;
984 if (compressed
&& (!hull
|| !compress
|| !decompress
))
985 return isl_stat_error
;
990 /* Add a new node to the graph representing the given set.
992 * If any of the set variables is defined by an equality, then
993 * we perform variable compression such that we can perform
994 * the scheduling on the compressed domain.
996 static isl_stat
extract_node(__isl_take isl_set
*set
, void *user
)
1000 isl_basic_set
*hull
;
1003 isl_multi_aff
*compress
, *decompress
;
1004 struct isl_sched_graph
*graph
= user
;
1006 hull
= isl_set_affine_hull(isl_set_copy(set
));
1007 hull
= isl_basic_set_remove_divs(hull
);
1008 nvar
= isl_set_dim(set
, isl_dim_set
);
1009 has_equality
= has_any_defining_equality(hull
);
1011 if (has_equality
< 0)
1013 if (!has_equality
) {
1014 isl_basic_set_free(hull
);
1015 return add_node(graph
, set
, nvar
, 0, NULL
, NULL
, NULL
);
1018 morph
= isl_basic_set_variable_compression(hull
, isl_dim_set
);
1019 nvar
= isl_morph_ran_dim(morph
, isl_dim_set
);
1020 compress
= isl_morph_get_var_multi_aff(morph
);
1021 morph
= isl_morph_inverse(morph
);
1022 decompress
= isl_morph_get_var_multi_aff(morph
);
1023 isl_morph_free(morph
);
1025 hull_set
= isl_set_from_basic_set(hull
);
1026 return add_node(graph
, set
, nvar
, 1, hull_set
, compress
, decompress
);
1028 isl_basic_set_free(hull
);
1030 return isl_stat_error
;
1033 struct isl_extract_edge_data
{
1034 enum isl_edge_type type
;
1035 struct isl_sched_graph
*graph
;
1038 /* Merge edge2 into edge1, freeing the contents of edge2.
1039 * Return 0 on success and -1 on failure.
1041 * edge1 and edge2 are assumed to have the same value for the map field.
1043 static int merge_edge(struct isl_sched_edge
*edge1
,
1044 struct isl_sched_edge
*edge2
)
1046 edge1
->types
|= edge2
->types
;
1047 isl_map_free(edge2
->map
);
1049 if (is_condition(edge2
)) {
1050 if (!edge1
->tagged_condition
)
1051 edge1
->tagged_condition
= edge2
->tagged_condition
;
1053 edge1
->tagged_condition
=
1054 isl_union_map_union(edge1
->tagged_condition
,
1055 edge2
->tagged_condition
);
1058 if (is_conditional_validity(edge2
)) {
1059 if (!edge1
->tagged_validity
)
1060 edge1
->tagged_validity
= edge2
->tagged_validity
;
1062 edge1
->tagged_validity
=
1063 isl_union_map_union(edge1
->tagged_validity
,
1064 edge2
->tagged_validity
);
1067 if (is_condition(edge2
) && !edge1
->tagged_condition
)
1069 if (is_conditional_validity(edge2
) && !edge1
->tagged_validity
)
1075 /* Insert dummy tags in domain and range of "map".
1077 * In particular, if "map" is of the form
1083 * [A -> dummy_tag] -> [B -> dummy_tag]
1085 * where the dummy_tags are identical and equal to any dummy tags
1086 * introduced by any other call to this function.
1088 static __isl_give isl_map
*insert_dummy_tags(__isl_take isl_map
*map
)
1094 isl_set
*domain
, *range
;
1096 ctx
= isl_map_get_ctx(map
);
1098 id
= isl_id_alloc(ctx
, NULL
, &dummy
);
1099 space
= isl_space_params(isl_map_get_space(map
));
1100 space
= isl_space_set_from_params(space
);
1101 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
1102 space
= isl_space_map_from_set(space
);
1104 domain
= isl_map_wrap(map
);
1105 range
= isl_map_wrap(isl_map_universe(space
));
1106 map
= isl_map_from_domain_and_range(domain
, range
);
1107 map
= isl_map_zip(map
);
1112 /* Given that at least one of "src" or "dst" is compressed, return
1113 * a map between the spaces of these nodes restricted to the affine
1114 * hull that was used in the compression.
1116 static __isl_give isl_map
*extract_hull(struct isl_sched_node
*src
,
1117 struct isl_sched_node
*dst
)
1121 if (src
->compressed
)
1122 dom
= isl_set_copy(src
->hull
);
1124 dom
= isl_set_universe(isl_space_copy(src
->space
));
1125 if (dst
->compressed
)
1126 ran
= isl_set_copy(dst
->hull
);
1128 ran
= isl_set_universe(isl_space_copy(dst
->space
));
1130 return isl_map_from_domain_and_range(dom
, ran
);
1133 /* Intersect the domains of the nested relations in domain and range
1134 * of "tagged" with "map".
1136 static __isl_give isl_map
*map_intersect_domains(__isl_take isl_map
*tagged
,
1137 __isl_keep isl_map
*map
)
1141 tagged
= isl_map_zip(tagged
);
1142 set
= isl_map_wrap(isl_map_copy(map
));
1143 tagged
= isl_map_intersect_domain(tagged
, set
);
1144 tagged
= isl_map_zip(tagged
);
1148 /* Return a pointer to the node that lives in the domain space of "map"
1149 * or NULL if there is no such node.
1151 static struct isl_sched_node
*find_domain_node(isl_ctx
*ctx
,
1152 struct isl_sched_graph
*graph
, __isl_keep isl_map
*map
)
1154 struct isl_sched_node
*node
;
1157 space
= isl_space_domain(isl_map_get_space(map
));
1158 node
= graph_find_node(ctx
, graph
, space
);
1159 isl_space_free(space
);
1164 /* Return a pointer to the node that lives in the range space of "map"
1165 * or NULL if there is no such node.
1167 static struct isl_sched_node
*find_range_node(isl_ctx
*ctx
,
1168 struct isl_sched_graph
*graph
, __isl_keep isl_map
*map
)
1170 struct isl_sched_node
*node
;
1173 space
= isl_space_range(isl_map_get_space(map
));
1174 node
= graph_find_node(ctx
, graph
, space
);
1175 isl_space_free(space
);
1180 /* Refrain from adding a new edge based on "map".
1181 * Instead, just free the map.
1182 * "tagged" is either a copy of "map" with additional tags or NULL.
1184 static isl_stat
skip_edge(__isl_take isl_map
*map
, __isl_take isl_map
*tagged
)
1187 isl_map_free(tagged
);
1192 /* Add a new edge to the graph based on the given map
1193 * and add it to data->graph->edge_table[data->type].
1194 * If a dependence relation of a given type happens to be identical
1195 * to one of the dependence relations of a type that was added before,
1196 * then we don't create a new edge, but instead mark the original edge
1197 * as also representing a dependence of the current type.
1199 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1200 * may be specified as "tagged" dependence relations. That is, "map"
1201 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1202 * the dependence on iterations and a and b are tags.
1203 * edge->map is set to the relation containing the elements i -> j,
1204 * while edge->tagged_condition and edge->tagged_validity contain
1205 * the union of all the "map" relations
1206 * for which extract_edge is called that result in the same edge->map.
1208 * If the source or the destination node is compressed, then
1209 * intersect both "map" and "tagged" with the constraints that
1210 * were used to construct the compression.
1211 * This ensures that there are no schedule constraints defined
1212 * outside of these domains, while the scheduler no longer has
1213 * any control over those outside parts.
1215 static isl_stat
extract_edge(__isl_take isl_map
*map
, void *user
)
1218 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1219 struct isl_extract_edge_data
*data
= user
;
1220 struct isl_sched_graph
*graph
= data
->graph
;
1221 struct isl_sched_node
*src
, *dst
;
1222 struct isl_sched_edge
*edge
;
1223 isl_map
*tagged
= NULL
;
1225 if (data
->type
== isl_edge_condition
||
1226 data
->type
== isl_edge_conditional_validity
) {
1227 if (isl_map_can_zip(map
)) {
1228 tagged
= isl_map_copy(map
);
1229 map
= isl_set_unwrap(isl_map_domain(isl_map_zip(map
)));
1231 tagged
= insert_dummy_tags(isl_map_copy(map
));
1235 src
= find_domain_node(ctx
, graph
, map
);
1236 dst
= find_range_node(ctx
, graph
, map
);
1239 return skip_edge(map
, tagged
);
1241 if (src
->compressed
|| dst
->compressed
) {
1243 hull
= extract_hull(src
, dst
);
1245 tagged
= map_intersect_domains(tagged
, hull
);
1246 map
= isl_map_intersect(map
, hull
);
1249 empty
= isl_map_plain_is_empty(map
);
1253 return skip_edge(map
, tagged
);
1255 graph
->edge
[graph
->n_edge
].src
= src
;
1256 graph
->edge
[graph
->n_edge
].dst
= dst
;
1257 graph
->edge
[graph
->n_edge
].map
= map
;
1258 graph
->edge
[graph
->n_edge
].types
= 0;
1259 graph
->edge
[graph
->n_edge
].tagged_condition
= NULL
;
1260 graph
->edge
[graph
->n_edge
].tagged_validity
= NULL
;
1261 set_type(&graph
->edge
[graph
->n_edge
], data
->type
);
1262 if (data
->type
== isl_edge_condition
)
1263 graph
->edge
[graph
->n_edge
].tagged_condition
=
1264 isl_union_map_from_map(tagged
);
1265 if (data
->type
== isl_edge_conditional_validity
)
1266 graph
->edge
[graph
->n_edge
].tagged_validity
=
1267 isl_union_map_from_map(tagged
);
1269 edge
= graph_find_matching_edge(graph
, &graph
->edge
[graph
->n_edge
]);
1272 return isl_stat_error
;
1274 if (edge
== &graph
->edge
[graph
->n_edge
])
1275 return graph_edge_table_add(ctx
, graph
, data
->type
,
1276 &graph
->edge
[graph
->n_edge
++]);
1278 if (merge_edge(edge
, &graph
->edge
[graph
->n_edge
]) < 0)
1281 return graph_edge_table_add(ctx
, graph
, data
->type
, edge
);
1284 isl_map_free(tagged
);
1285 return isl_stat_error
;
1288 /* Initialize the schedule graph "graph" from the schedule constraints "sc".
1290 * The context is included in the domain before the nodes of
1291 * the graphs are extracted in order to be able to exploit
1292 * any possible additional equalities.
1293 * Note that this intersection is only performed locally here.
1295 static isl_stat
graph_init(struct isl_sched_graph
*graph
,
1296 __isl_keep isl_schedule_constraints
*sc
)
1299 isl_union_set
*domain
;
1301 struct isl_extract_edge_data data
;
1302 enum isl_edge_type i
;
1306 return isl_stat_error
;
1308 ctx
= isl_schedule_constraints_get_ctx(sc
);
1310 domain
= isl_schedule_constraints_get_domain(sc
);
1311 graph
->n
= isl_union_set_n_set(domain
);
1312 isl_union_set_free(domain
);
1314 if (graph_alloc(ctx
, graph
, graph
->n
,
1315 isl_schedule_constraints_n_map(sc
)) < 0)
1316 return isl_stat_error
;
1318 if (compute_max_row(graph
, sc
) < 0)
1319 return isl_stat_error
;
1322 domain
= isl_schedule_constraints_get_domain(sc
);
1323 domain
= isl_union_set_intersect_params(domain
,
1324 isl_schedule_constraints_get_context(sc
));
1325 r
= isl_union_set_foreach_set(domain
, &extract_node
, graph
);
1326 isl_union_set_free(domain
);
1328 return isl_stat_error
;
1329 if (graph_init_table(ctx
, graph
) < 0)
1330 return isl_stat_error
;
1331 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
1332 c
= isl_schedule_constraints_get(sc
, i
);
1333 graph
->max_edge
[i
] = isl_union_map_n_map(c
);
1334 isl_union_map_free(c
);
1336 return isl_stat_error
;
1338 if (graph_init_edge_tables(ctx
, graph
) < 0)
1339 return isl_stat_error
;
1342 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
1346 c
= isl_schedule_constraints_get(sc
, i
);
1347 r
= isl_union_map_foreach_map(c
, &extract_edge
, &data
);
1348 isl_union_map_free(c
);
1350 return isl_stat_error
;
1356 /* Check whether there is any dependence from node[j] to node[i]
1357 * or from node[i] to node[j].
1359 static isl_bool
node_follows_weak(int i
, int j
, void *user
)
1362 struct isl_sched_graph
*graph
= user
;
1364 f
= graph_has_any_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1367 return graph_has_any_edge(graph
, &graph
->node
[i
], &graph
->node
[j
]);
1370 /* Check whether there is a (conditional) validity dependence from node[j]
1371 * to node[i], forcing node[i] to follow node[j].
1373 static isl_bool
node_follows_strong(int i
, int j
, void *user
)
1375 struct isl_sched_graph
*graph
= user
;
1377 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1380 /* Use Tarjan's algorithm for computing the strongly connected components
1381 * in the dependence graph only considering those edges defined by "follows".
1383 static int detect_ccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
1384 isl_bool (*follows
)(int i
, int j
, void *user
))
1387 struct isl_tarjan_graph
*g
= NULL
;
1389 g
= isl_tarjan_graph_init(ctx
, graph
->n
, follows
, graph
);
1397 while (g
->order
[i
] != -1) {
1398 graph
->node
[g
->order
[i
]].scc
= graph
->scc
;
1406 isl_tarjan_graph_free(g
);
1411 /* Apply Tarjan's algorithm to detect the strongly connected components
1412 * in the dependence graph.
1413 * Only consider the (conditional) validity dependences and clear "weak".
1415 static int detect_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1418 return detect_ccs(ctx
, graph
, &node_follows_strong
);
1421 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1422 * in the dependence graph.
1423 * Consider all dependences and set "weak".
1425 static int detect_wccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1428 return detect_ccs(ctx
, graph
, &node_follows_weak
);
1431 static int cmp_scc(const void *a
, const void *b
, void *data
)
1433 struct isl_sched_graph
*graph
= data
;
1437 return graph
->node
[*i1
].scc
- graph
->node
[*i2
].scc
;
1440 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1442 static int sort_sccs(struct isl_sched_graph
*graph
)
1444 return isl_sort(graph
->sorted
, graph
->n
, sizeof(int), &cmp_scc
, graph
);
1447 /* Given a dependence relation R from "node" to itself,
1448 * construct the set of coefficients of valid constraints for elements
1449 * in that dependence relation.
1450 * In particular, the result contains tuples of coefficients
1451 * c_0, c_n, c_x such that
1453 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1457 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1459 * We choose here to compute the dual of delta R.
1460 * Alternatively, we could have computed the dual of R, resulting
1461 * in a set of tuples c_0, c_n, c_x, c_y, and then
1462 * plugged in (c_0, c_n, c_x, -c_x).
1464 * If "node" has been compressed, then the dependence relation
1465 * is also compressed before the set of coefficients is computed.
1467 static __isl_give isl_basic_set
*intra_coefficients(
1468 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1469 __isl_take isl_map
*map
)
1473 isl_basic_set
*coef
;
1474 isl_maybe_isl_basic_set m
;
1476 m
= isl_map_to_basic_set_try_get(graph
->intra_hmap
, map
);
1477 if (m
.valid
< 0 || m
.valid
) {
1482 key
= isl_map_copy(map
);
1483 if (node
->compressed
) {
1484 map
= isl_map_preimage_domain_multi_aff(map
,
1485 isl_multi_aff_copy(node
->decompress
));
1486 map
= isl_map_preimage_range_multi_aff(map
,
1487 isl_multi_aff_copy(node
->decompress
));
1489 delta
= isl_set_remove_divs(isl_map_deltas(map
));
1490 coef
= isl_set_coefficients(delta
);
1491 graph
->intra_hmap
= isl_map_to_basic_set_set(graph
->intra_hmap
, key
,
1492 isl_basic_set_copy(coef
));
1497 /* Given a dependence relation R, construct the set of coefficients
1498 * of valid constraints for elements in that dependence relation.
1499 * In particular, the result contains tuples of coefficients
1500 * c_0, c_n, c_x, c_y such that
1502 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1504 * If the source or destination nodes of "edge" have been compressed,
1505 * then the dependence relation is also compressed before
1506 * the set of coefficients is computed.
1508 static __isl_give isl_basic_set
*inter_coefficients(
1509 struct isl_sched_graph
*graph
, struct isl_sched_edge
*edge
,
1510 __isl_take isl_map
*map
)
1514 isl_basic_set
*coef
;
1515 isl_maybe_isl_basic_set m
;
1517 m
= isl_map_to_basic_set_try_get(graph
->inter_hmap
, map
);
1518 if (m
.valid
< 0 || m
.valid
) {
1523 key
= isl_map_copy(map
);
1524 if (edge
->src
->compressed
)
1525 map
= isl_map_preimage_domain_multi_aff(map
,
1526 isl_multi_aff_copy(edge
->src
->decompress
));
1527 if (edge
->dst
->compressed
)
1528 map
= isl_map_preimage_range_multi_aff(map
,
1529 isl_multi_aff_copy(edge
->dst
->decompress
));
1530 set
= isl_map_wrap(isl_map_remove_divs(map
));
1531 coef
= isl_set_coefficients(set
);
1532 graph
->inter_hmap
= isl_map_to_basic_set_set(graph
->inter_hmap
, key
,
1533 isl_basic_set_copy(coef
));
1538 /* Return the position of the coefficients of the variables in
1539 * the coefficients constraints "coef".
1541 * The space of "coef" is of the form
1543 * { coefficients[[cst, params] -> S] }
1545 * Return the position of S.
1547 static int coef_var_offset(__isl_keep isl_basic_set
*coef
)
1552 space
= isl_space_unwrap(isl_basic_set_get_space(coef
));
1553 offset
= isl_space_dim(space
, isl_dim_in
);
1554 isl_space_free(space
);
1559 /* Return the offset of the coefficients of the variables of "node"
1562 * Within each node, the coefficients have the following order:
1564 * - c_i_n (if parametric)
1565 * - positive and negative parts of c_i_x
1567 static int node_var_coef_offset(struct isl_sched_node
*node
)
1569 return node
->start
+ 1 + node
->nparam
;
1572 /* Construct an isl_dim_map for mapping constraints on coefficients
1573 * for "node" to the corresponding positions in graph->lp.
1574 * "offset" is the offset of the coefficients for the variables
1575 * in the input constraints.
1576 * "s" is the sign of the mapping.
1578 * The input constraints are given in terms of the coefficients (c_0, c_n, c_x).
1579 * The mapping produced by this function essentially plugs in
1580 * (0, 0, c_i_x^+ - c_i_x^-) if s = 1 and
1581 * (0, 0, -c_i_x^+ + c_i_x^-) if s = -1.
1582 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1584 * The caller can extend the mapping to also map the other coefficients
1585 * (and therefore not plug in 0).
1587 static __isl_give isl_dim_map
*intra_dim_map(isl_ctx
*ctx
,
1588 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1593 isl_dim_map
*dim_map
;
1595 if (!node
|| !graph
->lp
)
1598 total
= isl_basic_set_total_dim(graph
->lp
);
1599 pos
= node_var_coef_offset(node
);
1600 dim_map
= isl_dim_map_alloc(ctx
, total
);
1601 isl_dim_map_range(dim_map
, pos
, 2, offset
, 1, node
->nvar
, -s
);
1602 isl_dim_map_range(dim_map
, pos
+ 1, 2, offset
, 1, node
->nvar
, s
);
1607 /* Construct an isl_dim_map for mapping constraints on coefficients
1608 * for "src" (node i) and "dst" (node j) to the corresponding positions
1610 * "offset" is the offset of the coefficients for the variables of "src"
1611 * in the input constraints.
1612 * "s" is the sign of the mapping.
1614 * The input constraints are given in terms of the coefficients
1615 * (c_0, c_n, c_x, c_y).
1616 * The mapping produced by this function essentially plugs in
1617 * (c_j_0 - c_i_0, c_j_n - c_i_n,
1618 * c_j_x^+ - c_j_x^-, -(c_i_x^+ - c_i_x^-)) if s = 1 and
1619 * (-c_j_0 + c_i_0, -c_j_n + c_i_n,
1620 * - (c_j_x^+ - c_j_x^-), c_i_x^+ - c_i_x^-) if s = -1.
1621 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1623 * The caller can further extend the mapping.
1625 static __isl_give isl_dim_map
*inter_dim_map(isl_ctx
*ctx
,
1626 struct isl_sched_graph
*graph
, struct isl_sched_node
*src
,
1627 struct isl_sched_node
*dst
, int offset
, int s
)
1631 isl_dim_map
*dim_map
;
1633 if (!src
|| !dst
|| !graph
->lp
)
1636 total
= isl_basic_set_total_dim(graph
->lp
);
1637 dim_map
= isl_dim_map_alloc(ctx
, total
);
1639 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, s
);
1640 isl_dim_map_range(dim_map
, dst
->start
+ 1, 1, 1, 1, dst
->nparam
, s
);
1641 pos
= node_var_coef_offset(dst
);
1642 isl_dim_map_range(dim_map
, pos
, 2, offset
+ src
->nvar
, 1,
1644 isl_dim_map_range(dim_map
, pos
+ 1, 2, offset
+ src
->nvar
, 1,
1647 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, -s
);
1648 isl_dim_map_range(dim_map
, src
->start
+ 1, 1, 1, 1, src
->nparam
, -s
);
1649 pos
= node_var_coef_offset(src
);
1650 isl_dim_map_range(dim_map
, pos
, 2, offset
, 1, src
->nvar
, s
);
1651 isl_dim_map_range(dim_map
, pos
+ 1, 2, offset
, 1, src
->nvar
, -s
);
1656 /* Add constraints to graph->lp that force validity for the given
1657 * dependence from a node i to itself.
1658 * That is, add constraints that enforce
1660 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1661 * = c_i_x (y - x) >= 0
1663 * for each (x,y) in R.
1664 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1665 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1666 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1667 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1669 * Actually, we do not construct constraints for the c_i_x themselves,
1670 * but for the coefficients of c_i_x written as a linear combination
1671 * of the columns in node->cmap.
1673 static isl_stat
add_intra_validity_constraints(struct isl_sched_graph
*graph
,
1674 struct isl_sched_edge
*edge
)
1677 isl_map
*map
= isl_map_copy(edge
->map
);
1678 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1679 isl_dim_map
*dim_map
;
1680 isl_basic_set
*coef
;
1681 struct isl_sched_node
*node
= edge
->src
;
1683 coef
= intra_coefficients(graph
, node
, map
);
1685 offset
= coef_var_offset(coef
);
1687 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1688 offset
, isl_mat_copy(node
->cmap
));
1690 return isl_stat_error
;
1692 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, 1);
1693 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1694 coef
->n_eq
, coef
->n_ineq
);
1695 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1701 /* Add constraints to graph->lp that force validity for the given
1702 * dependence from node i to node j.
1703 * That is, add constraints that enforce
1705 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1707 * for each (x,y) in R.
1708 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1709 * of valid constraints for R and then plug in
1710 * (c_j_0 - c_i_0, c_j_n - c_i_n, c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1711 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1712 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1714 * Actually, we do not construct constraints for the c_*_x themselves,
1715 * but for the coefficients of c_*_x written as a linear combination
1716 * of the columns in node->cmap.
1718 static isl_stat
add_inter_validity_constraints(struct isl_sched_graph
*graph
,
1719 struct isl_sched_edge
*edge
)
1724 isl_dim_map
*dim_map
;
1725 isl_basic_set
*coef
;
1726 struct isl_sched_node
*src
= edge
->src
;
1727 struct isl_sched_node
*dst
= edge
->dst
;
1730 return isl_stat_error
;
1732 map
= isl_map_copy(edge
->map
);
1733 ctx
= isl_map_get_ctx(map
);
1734 coef
= inter_coefficients(graph
, edge
, map
);
1736 offset
= coef_var_offset(coef
);
1738 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1739 offset
, isl_mat_copy(src
->cmap
));
1740 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1741 offset
+ src
->nvar
, isl_mat_copy(dst
->cmap
));
1743 return isl_stat_error
;
1745 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, 1);
1747 edge
->start
= graph
->lp
->n_ineq
;
1748 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1749 coef
->n_eq
, coef
->n_ineq
);
1750 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1753 return isl_stat_error
;
1754 edge
->end
= graph
->lp
->n_ineq
;
1759 /* Add constraints to graph->lp that bound the dependence distance for the given
1760 * dependence from a node i to itself.
1761 * If s = 1, we add the constraint
1763 * c_i_x (y - x) <= m_0 + m_n n
1767 * -c_i_x (y - x) + m_0 + m_n n >= 0
1769 * for each (x,y) in R.
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 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1780 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1781 * with each coefficient (except m_0) represented as a pair of non-negative
1784 * Actually, we do not construct constraints for the c_i_x themselves,
1785 * but for the coefficients of c_i_x written as a linear combination
1786 * of the columns in node->cmap.
1789 * If "local" is set, then we add constraints
1791 * c_i_x (y - x) <= 0
1795 * -c_i_x (y - x) <= 0
1797 * instead, forcing the dependence distance to be (less than or) equal to 0.
1798 * That is, we plug in (0, 0, -s * c_i_x),
1799 * Note that dependences marked local are treated as validity constraints
1800 * by add_all_validity_constraints and therefore also have
1801 * their distances bounded by 0 from below.
1803 static isl_stat
add_intra_proximity_constraints(struct isl_sched_graph
*graph
,
1804 struct isl_sched_edge
*edge
, int s
, int local
)
1808 isl_map
*map
= isl_map_copy(edge
->map
);
1809 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1810 isl_dim_map
*dim_map
;
1811 isl_basic_set
*coef
;
1812 struct isl_sched_node
*node
= edge
->src
;
1814 coef
= intra_coefficients(graph
, node
, map
);
1816 offset
= coef_var_offset(coef
);
1818 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1819 offset
, isl_mat_copy(node
->cmap
));
1821 return isl_stat_error
;
1823 nparam
= isl_space_dim(node
->space
, isl_dim_param
);
1824 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, -s
);
1827 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1828 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1829 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1831 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1832 coef
->n_eq
, coef
->n_ineq
);
1833 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
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,
1867 * with each coefficient (except m_0, c_*_0 and c_*_n)
1868 * represented as a pair of non-negative coefficients.
1870 * Actually, we do not construct constraints for the c_*_x themselves,
1871 * but for the coefficients of c_*_x written as a linear combination
1872 * of the columns in node->cmap.
1875 * If "local" is set, then we add constraints
1877 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1881 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
1883 * instead, forcing the dependence distance to be (less than or) equal to 0.
1884 * That is, we plug in
1885 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
1886 * Note that dependences marked local are treated as validity constraints
1887 * by add_all_validity_constraints and therefore also have
1888 * their distances bounded by 0 from below.
1890 static isl_stat
add_inter_proximity_constraints(struct isl_sched_graph
*graph
,
1891 struct isl_sched_edge
*edge
, int s
, int local
)
1895 isl_map
*map
= isl_map_copy(edge
->map
);
1896 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1897 isl_dim_map
*dim_map
;
1898 isl_basic_set
*coef
;
1899 struct isl_sched_node
*src
= edge
->src
;
1900 struct isl_sched_node
*dst
= edge
->dst
;
1902 coef
= inter_coefficients(graph
, edge
, map
);
1904 offset
= coef_var_offset(coef
);
1906 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1907 offset
, isl_mat_copy(src
->cmap
));
1908 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1909 offset
+ src
->nvar
, isl_mat_copy(dst
->cmap
));
1911 return isl_stat_error
;
1913 nparam
= isl_space_dim(src
->space
, isl_dim_param
);
1914 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, -s
);
1917 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1918 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1919 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1922 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1923 coef
->n_eq
, coef
->n_ineq
);
1924 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1930 /* Add all validity constraints to graph->lp.
1932 * An edge that is forced to be local needs to have its dependence
1933 * distances equal to zero. We take care of bounding them by 0 from below
1934 * here. add_all_proximity_constraints takes care of bounding them by 0
1937 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1938 * Otherwise, we ignore them.
1940 static int add_all_validity_constraints(struct isl_sched_graph
*graph
,
1941 int use_coincidence
)
1945 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1946 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1949 local
= is_local(edge
) ||
1950 (is_coincidence(edge
) && use_coincidence
);
1951 if (!is_validity(edge
) && !local
)
1953 if (edge
->src
!= edge
->dst
)
1955 if (add_intra_validity_constraints(graph
, edge
) < 0)
1959 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1960 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1963 local
= is_local(edge
) ||
1964 (is_coincidence(edge
) && use_coincidence
);
1965 if (!is_validity(edge
) && !local
)
1967 if (edge
->src
== edge
->dst
)
1969 if (add_inter_validity_constraints(graph
, edge
) < 0)
1976 /* Add constraints to graph->lp that bound the dependence distance
1977 * for all dependence relations.
1978 * If a given proximity dependence is identical to a validity
1979 * dependence, then the dependence distance is already bounded
1980 * from below (by zero), so we only need to bound the distance
1981 * from above. (This includes the case of "local" dependences
1982 * which are treated as validity dependence by add_all_validity_constraints.)
1983 * Otherwise, we need to bound the distance both from above and from below.
1985 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1986 * Otherwise, we ignore them.
1988 static int add_all_proximity_constraints(struct isl_sched_graph
*graph
,
1989 int use_coincidence
)
1993 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1994 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1997 local
= is_local(edge
) ||
1998 (is_coincidence(edge
) && use_coincidence
);
1999 if (!is_proximity(edge
) && !local
)
2001 if (edge
->src
== edge
->dst
&&
2002 add_intra_proximity_constraints(graph
, edge
, 1, local
) < 0)
2004 if (edge
->src
!= edge
->dst
&&
2005 add_inter_proximity_constraints(graph
, edge
, 1, local
) < 0)
2007 if (is_validity(edge
) || local
)
2009 if (edge
->src
== edge
->dst
&&
2010 add_intra_proximity_constraints(graph
, edge
, -1, 0) < 0)
2012 if (edge
->src
!= edge
->dst
&&
2013 add_inter_proximity_constraints(graph
, edge
, -1, 0) < 0)
2020 /* Compute a basis for the rows in the linear part of the schedule
2021 * and extend this basis to a full basis. The remaining rows
2022 * can then be used to force linear independence from the rows
2025 * In particular, given the schedule rows S, we compute
2030 * with H the Hermite normal form of S. That is, all but the
2031 * first rank columns of H are zero and so each row in S is
2032 * a linear combination of the first rank rows of Q.
2033 * The matrix Q is then transposed because we will write the
2034 * coefficients of the next schedule row as a column vector s
2035 * and express this s as a linear combination s = Q c of the
2037 * Similarly, the matrix U is transposed such that we can
2038 * compute the coefficients c = U s from a schedule row s.
2040 static int node_update_cmap(struct isl_sched_node
*node
)
2043 int n_row
= isl_mat_rows(node
->sched
);
2045 H
= isl_mat_sub_alloc(node
->sched
, 0, n_row
,
2046 1 + node
->nparam
, node
->nvar
);
2048 H
= isl_mat_left_hermite(H
, 0, &U
, &Q
);
2049 isl_mat_free(node
->cmap
);
2050 isl_mat_free(node
->cinv
);
2051 isl_mat_free(node
->ctrans
);
2052 node
->ctrans
= isl_mat_copy(Q
);
2053 node
->cmap
= isl_mat_transpose(Q
);
2054 node
->cinv
= isl_mat_transpose(U
);
2055 node
->rank
= isl_mat_initial_non_zero_cols(H
);
2058 if (!node
->cmap
|| !node
->cinv
|| !node
->ctrans
|| node
->rank
< 0)
2063 /* Is "edge" marked as a validity or a conditional validity edge?
2065 static int is_any_validity(struct isl_sched_edge
*edge
)
2067 return is_validity(edge
) || is_conditional_validity(edge
);
2070 /* How many times should we count the constraints in "edge"?
2072 * If carry is set, then we are counting the number of
2073 * (validity or conditional validity) constraints that will be added
2074 * in setup_carry_lp and we count each edge exactly once.
2076 * Otherwise, we count as follows
2077 * validity -> 1 (>= 0)
2078 * validity+proximity -> 2 (>= 0 and upper bound)
2079 * proximity -> 2 (lower and upper bound)
2080 * local(+any) -> 2 (>= 0 and <= 0)
2082 * If an edge is only marked conditional_validity then it counts
2083 * as zero since it is only checked afterwards.
2085 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2086 * Otherwise, we ignore them.
2088 static int edge_multiplicity(struct isl_sched_edge
*edge
, int carry
,
2089 int use_coincidence
)
2093 if (is_proximity(edge
) || is_local(edge
))
2095 if (use_coincidence
&& is_coincidence(edge
))
2097 if (is_validity(edge
))
2102 /* Count the number of equality and inequality constraints
2103 * that will be added for the given map.
2105 * "use_coincidence" is set if we should take into account coincidence edges.
2107 static int count_map_constraints(struct isl_sched_graph
*graph
,
2108 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
,
2109 int *n_eq
, int *n_ineq
, int carry
, int use_coincidence
)
2111 isl_basic_set
*coef
;
2112 int f
= edge_multiplicity(edge
, carry
, use_coincidence
);
2119 if (edge
->src
== edge
->dst
)
2120 coef
= intra_coefficients(graph
, edge
->src
, map
);
2122 coef
= inter_coefficients(graph
, edge
, map
);
2125 *n_eq
+= f
* coef
->n_eq
;
2126 *n_ineq
+= f
* coef
->n_ineq
;
2127 isl_basic_set_free(coef
);
2132 /* Count the number of equality and inequality constraints
2133 * that will be added to the main lp problem.
2134 * We count as follows
2135 * validity -> 1 (>= 0)
2136 * validity+proximity -> 2 (>= 0 and upper bound)
2137 * proximity -> 2 (lower and upper bound)
2138 * local(+any) -> 2 (>= 0 and <= 0)
2140 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2141 * Otherwise, we ignore them.
2143 static int count_constraints(struct isl_sched_graph
*graph
,
2144 int *n_eq
, int *n_ineq
, int use_coincidence
)
2148 *n_eq
= *n_ineq
= 0;
2149 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2150 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2151 isl_map
*map
= isl_map_copy(edge
->map
);
2153 if (count_map_constraints(graph
, edge
, map
, n_eq
, n_ineq
,
2154 0, use_coincidence
) < 0)
2161 /* Count the number of constraints that will be added by
2162 * add_bound_constant_constraints to bound the values of the constant terms
2163 * and increment *n_eq and *n_ineq accordingly.
2165 * In practice, add_bound_constant_constraints only adds inequalities.
2167 static isl_stat
count_bound_constant_constraints(isl_ctx
*ctx
,
2168 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
2170 if (isl_options_get_schedule_max_constant_term(ctx
) == -1)
2173 *n_ineq
+= graph
->n
;
2178 /* Add constraints to bound the values of the constant terms in the schedule,
2179 * if requested by the user.
2181 * The maximal value of the constant terms is defined by the option
2182 * "schedule_max_constant_term".
2184 * Within each node, the coefficients have the following order:
2186 * - c_i_n (if parametric)
2187 * - positive and negative parts of c_i_x
2189 static isl_stat
add_bound_constant_constraints(isl_ctx
*ctx
,
2190 struct isl_sched_graph
*graph
)
2196 max
= isl_options_get_schedule_max_constant_term(ctx
);
2200 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2202 for (i
= 0; i
< graph
->n
; ++i
) {
2203 struct isl_sched_node
*node
= &graph
->node
[i
];
2204 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2206 return isl_stat_error
;
2207 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2208 isl_int_set_si(graph
->lp
->ineq
[k
][1 + node
->start
], -1);
2209 isl_int_set_si(graph
->lp
->ineq
[k
][0], max
);
2215 /* Count the number of constraints that will be added by
2216 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
2219 * In practice, add_bound_coefficient_constraints only adds inequalities.
2221 static int count_bound_coefficient_constraints(isl_ctx
*ctx
,
2222 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
2226 if (isl_options_get_schedule_max_coefficient(ctx
) == -1 &&
2227 !isl_options_get_schedule_treat_coalescing(ctx
))
2230 for (i
= 0; i
< graph
->n
; ++i
)
2231 *n_ineq
+= graph
->node
[i
].nparam
+ 2 * graph
->node
[i
].nvar
;
2236 /* Add constraints to graph->lp that bound the values of
2237 * the parameter schedule coefficients of "node" to "max" and
2238 * the variable schedule coefficients to the corresponding entry
2240 * In either case, a negative value means that no bound needs to be imposed.
2242 * For parameter coefficients, this amounts to adding a constraint
2250 * The variables coefficients are, however, not represented directly.
2251 * Instead, the variables coefficients c_x are written as a linear
2252 * combination c_x = cmap c_z of some other coefficients c_z,
2253 * which are in turn encoded as c_z = c_z^+ - c_z^-.
2254 * Let a_j be the elements of row i of node->cmap, then
2256 * -max_i <= c_x_i <= max_i
2260 * -max_i <= \sum_j a_j (c_z_j^+ - c_z_j^-) <= max_i
2264 * -\sum_j a_j (c_z_j^+ - c_z_j^-) + max_i >= 0
2265 * \sum_j a_j (c_z_j^+ - c_z_j^-) + max_i >= 0
2267 static isl_stat
node_add_coefficient_constraints(isl_ctx
*ctx
,
2268 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
, int max
)
2274 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2276 for (j
= 0; j
< node
->nparam
; ++j
) {
2282 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2284 return isl_stat_error
;
2285 dim
= 1 + node
->start
+ 1 + j
;
2286 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2287 isl_int_set_si(graph
->lp
->ineq
[k
][dim
], -1);
2288 isl_int_set_si(graph
->lp
->ineq
[k
][0], max
);
2291 ineq
= isl_vec_alloc(ctx
, 1 + total
);
2292 ineq
= isl_vec_clr(ineq
);
2294 return isl_stat_error
;
2295 for (i
= 0; i
< node
->nvar
; ++i
) {
2296 int pos
= 1 + node_var_coef_offset(node
);
2298 if (isl_int_is_neg(node
->max
->el
[i
]))
2301 for (j
= 0; j
< node
->nvar
; ++j
) {
2302 isl_int_set(ineq
->el
[pos
+ 2 * j
],
2303 node
->cmap
->row
[i
][j
]);
2304 isl_int_neg(ineq
->el
[pos
+ 2 * j
+ 1],
2305 node
->cmap
->row
[i
][j
]);
2307 isl_int_set(ineq
->el
[0], node
->max
->el
[i
]);
2309 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2312 isl_seq_cpy(graph
->lp
->ineq
[k
], ineq
->el
, 1 + total
);
2314 isl_seq_neg(ineq
->el
+ pos
, ineq
->el
+ pos
, 2 * node
->nvar
);
2315 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2318 isl_seq_cpy(graph
->lp
->ineq
[k
], ineq
->el
, 1 + total
);
2325 return isl_stat_error
;
2328 /* Add constraints that bound the values of the variable and parameter
2329 * coefficients of the schedule.
2331 * The maximal value of the coefficients is defined by the option
2332 * 'schedule_max_coefficient' and the entries in node->max.
2333 * These latter entries are only set if either the schedule_max_coefficient
2334 * option or the schedule_treat_coalescing option is set.
2336 static isl_stat
add_bound_coefficient_constraints(isl_ctx
*ctx
,
2337 struct isl_sched_graph
*graph
)
2342 max
= isl_options_get_schedule_max_coefficient(ctx
);
2344 if (max
== -1 && !isl_options_get_schedule_treat_coalescing(ctx
))
2347 for (i
= 0; i
< graph
->n
; ++i
) {
2348 struct isl_sched_node
*node
= &graph
->node
[i
];
2350 if (node_add_coefficient_constraints(ctx
, graph
, node
, max
) < 0)
2351 return isl_stat_error
;
2357 /* Add a constraint to graph->lp that equates the value at position
2358 * "sum_pos" to the sum of the "n" values starting at "first".
2360 static isl_stat
add_sum_constraint(struct isl_sched_graph
*graph
,
2361 int sum_pos
, int first
, int n
)
2366 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2368 k
= isl_basic_set_alloc_equality(graph
->lp
);
2370 return isl_stat_error
;
2371 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2372 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2373 for (i
= 0; i
< n
; ++i
)
2374 isl_int_set_si(graph
->lp
->eq
[k
][1 + first
+ i
], 1);
2379 /* Add a constraint to graph->lp that equates the value at position
2380 * "sum_pos" to the sum of the parameter coefficients of all nodes.
2382 * Within each node, the coefficients have the following order:
2384 * - c_i_n (if parametric)
2385 * - positive and negative parts of c_i_x
2387 static isl_stat
add_param_sum_constraint(struct isl_sched_graph
*graph
,
2393 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2395 k
= isl_basic_set_alloc_equality(graph
->lp
);
2397 return isl_stat_error
;
2398 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2399 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2400 for (i
= 0; i
< graph
->n
; ++i
) {
2401 int pos
= 1 + graph
->node
[i
].start
+ 1;
2403 for (j
= 0; j
< graph
->node
[i
].nparam
; ++j
)
2404 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2410 /* Add a constraint to graph->lp that equates the value at position
2411 * "sum_pos" to the sum of the variable coefficients of all nodes.
2413 * Within each node, the coefficients have the following order:
2415 * - c_i_n (if parametric)
2416 * - positive and negative parts of c_i_x
2418 static isl_stat
add_var_sum_constraint(struct isl_sched_graph
*graph
,
2424 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2426 k
= isl_basic_set_alloc_equality(graph
->lp
);
2428 return isl_stat_error
;
2429 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2430 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2431 for (i
= 0; i
< graph
->n
; ++i
) {
2432 struct isl_sched_node
*node
= &graph
->node
[i
];
2433 int pos
= 1 + node_var_coef_offset(node
);
2435 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
2436 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2442 /* Construct an ILP problem for finding schedule coefficients
2443 * that result in non-negative, but small dependence distances
2444 * over all dependences.
2445 * In particular, the dependence distances over proximity edges
2446 * are bounded by m_0 + m_n n and we compute schedule coefficients
2447 * with small values (preferably zero) of m_n and m_0.
2449 * All variables of the ILP are non-negative. The actual coefficients
2450 * may be negative, so each coefficient is represented as the difference
2451 * of two non-negative variables. The negative part always appears
2452 * immediately before the positive part.
2453 * Other than that, the variables have the following order
2455 * - sum of positive and negative parts of m_n coefficients
2457 * - sum of all c_n coefficients
2458 * (unconstrained when computing non-parametric schedules)
2459 * - sum of positive and negative parts of all c_x coefficients
2460 * - positive and negative parts of m_n coefficients
2463 * - c_i_n (if parametric)
2464 * - positive and negative parts of c_i_x
2466 * The c_i_x are not represented directly, but through the columns of
2467 * node->cmap. That is, the computed values are for variable t_i_x
2468 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
2470 * The constraints are those from the edges plus two or three equalities
2471 * to express the sums.
2473 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2474 * Otherwise, we ignore them.
2476 static isl_stat
setup_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
2477 int use_coincidence
)
2487 parametric
= ctx
->opt
->schedule_parametric
;
2488 nparam
= isl_space_dim(graph
->node
[0].space
, isl_dim_param
);
2490 total
= param_pos
+ 2 * nparam
;
2491 for (i
= 0; i
< graph
->n
; ++i
) {
2492 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
2493 if (node_update_cmap(node
) < 0)
2494 return isl_stat_error
;
2495 node
->start
= total
;
2496 total
+= 1 + node
->nparam
+ 2 * node
->nvar
;
2499 if (count_constraints(graph
, &n_eq
, &n_ineq
, use_coincidence
) < 0)
2500 return isl_stat_error
;
2501 if (count_bound_constant_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2502 return isl_stat_error
;
2503 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2504 return isl_stat_error
;
2506 space
= isl_space_set_alloc(ctx
, 0, total
);
2507 isl_basic_set_free(graph
->lp
);
2508 n_eq
+= 2 + parametric
;
2510 graph
->lp
= isl_basic_set_alloc_space(space
, 0, n_eq
, n_ineq
);
2512 if (add_sum_constraint(graph
, 0, param_pos
, 2 * nparam
) < 0)
2513 return isl_stat_error
;
2514 if (parametric
&& add_param_sum_constraint(graph
, 2) < 0)
2515 return isl_stat_error
;
2516 if (add_var_sum_constraint(graph
, 3) < 0)
2517 return isl_stat_error
;
2518 if (add_bound_constant_constraints(ctx
, graph
) < 0)
2519 return isl_stat_error
;
2520 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
2521 return isl_stat_error
;
2522 if (add_all_validity_constraints(graph
, use_coincidence
) < 0)
2523 return isl_stat_error
;
2524 if (add_all_proximity_constraints(graph
, use_coincidence
) < 0)
2525 return isl_stat_error
;
2530 /* Analyze the conflicting constraint found by
2531 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2532 * constraint of one of the edges between distinct nodes, living, moreover
2533 * in distinct SCCs, then record the source and sink SCC as this may
2534 * be a good place to cut between SCCs.
2536 static int check_conflict(int con
, void *user
)
2539 struct isl_sched_graph
*graph
= user
;
2541 if (graph
->src_scc
>= 0)
2544 con
-= graph
->lp
->n_eq
;
2546 if (con
>= graph
->lp
->n_ineq
)
2549 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2550 if (!is_validity(&graph
->edge
[i
]))
2552 if (graph
->edge
[i
].src
== graph
->edge
[i
].dst
)
2554 if (graph
->edge
[i
].src
->scc
== graph
->edge
[i
].dst
->scc
)
2556 if (graph
->edge
[i
].start
> con
)
2558 if (graph
->edge
[i
].end
<= con
)
2560 graph
->src_scc
= graph
->edge
[i
].src
->scc
;
2561 graph
->dst_scc
= graph
->edge
[i
].dst
->scc
;
2567 /* Check whether the next schedule row of the given node needs to be
2568 * non-trivial. Lower-dimensional domains may have some trivial rows,
2569 * but as soon as the number of remaining required non-trivial rows
2570 * is as large as the number or remaining rows to be computed,
2571 * all remaining rows need to be non-trivial.
2573 static int needs_row(struct isl_sched_graph
*graph
, struct isl_sched_node
*node
)
2575 return node
->nvar
- node
->rank
>= graph
->maxvar
- graph
->n_row
;
2578 /* Solve the ILP problem constructed in setup_lp.
2579 * For each node such that all the remaining rows of its schedule
2580 * need to be non-trivial, we construct a non-triviality region.
2581 * This region imposes that the next row is independent of previous rows.
2582 * In particular the coefficients c_i_x are represented by t_i_x
2583 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
2584 * its first columns span the rows of the previously computed part
2585 * of the schedule. The non-triviality region enforces that at least
2586 * one of the remaining components of t_i_x is non-zero, i.e.,
2587 * that the new schedule row depends on at least one of the remaining
2590 static __isl_give isl_vec
*solve_lp(struct isl_sched_graph
*graph
)
2596 for (i
= 0; i
< graph
->n
; ++i
) {
2597 struct isl_sched_node
*node
= &graph
->node
[i
];
2598 int skip
= node
->rank
;
2599 graph
->region
[i
].pos
= node_var_coef_offset(node
) + 2 * skip
;
2600 if (needs_row(graph
, node
))
2601 graph
->region
[i
].len
= 2 * (node
->nvar
- skip
);
2603 graph
->region
[i
].len
= 0;
2605 lp
= isl_basic_set_copy(graph
->lp
);
2606 sol
= isl_tab_basic_set_non_trivial_lexmin(lp
, 2, graph
->n
,
2607 graph
->region
, &check_conflict
, graph
);
2611 /* Extract the coefficients for the variables of "node" from "sol".
2613 * Within each node, the coefficients have the following order:
2615 * - c_i_n (if parametric)
2616 * - positive and negative parts of c_i_x
2618 * The c_i_x^- appear before their c_i_x^+ counterpart.
2620 * Return c_i_x = c_i_x^+ - c_i_x^-
2622 static __isl_give isl_vec
*extract_var_coef(struct isl_sched_node
*node
,
2623 __isl_keep isl_vec
*sol
)
2631 csol
= isl_vec_alloc(isl_vec_get_ctx(sol
), node
->nvar
);
2635 pos
= 1 + node_var_coef_offset(node
);
2636 for (i
= 0; i
< node
->nvar
; ++i
)
2637 isl_int_sub(csol
->el
[i
],
2638 sol
->el
[pos
+ 2 * i
+ 1], sol
->el
[pos
+ 2 * i
]);
2643 /* Update the schedules of all nodes based on the given solution
2644 * of the LP problem.
2645 * The new row is added to the current band.
2646 * All possibly negative coefficients are encoded as a difference
2647 * of two non-negative variables, so we need to perform the subtraction
2648 * here. Moreover, if use_cmap is set, then the solution does
2649 * not refer to the actual coefficients c_i_x, but instead to variables
2650 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2651 * In this case, we then also need to perform this multiplication
2652 * to obtain the values of c_i_x.
2654 * If coincident is set, then the caller guarantees that the new
2655 * row satisfies the coincidence constraints.
2657 static int update_schedule(struct isl_sched_graph
*graph
,
2658 __isl_take isl_vec
*sol
, int use_cmap
, int coincident
)
2661 isl_vec
*csol
= NULL
;
2666 isl_die(sol
->ctx
, isl_error_internal
,
2667 "no solution found", goto error
);
2668 if (graph
->n_total_row
>= graph
->max_row
)
2669 isl_die(sol
->ctx
, isl_error_internal
,
2670 "too many schedule rows", goto error
);
2672 for (i
= 0; i
< graph
->n
; ++i
) {
2673 struct isl_sched_node
*node
= &graph
->node
[i
];
2674 int pos
= node
->start
;
2675 int row
= isl_mat_rows(node
->sched
);
2678 csol
= extract_var_coef(node
, sol
);
2682 isl_map_free(node
->sched_map
);
2683 node
->sched_map
= NULL
;
2684 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
2687 for (j
= 0; j
< 1 + node
->nparam
; ++j
)
2688 node
->sched
= isl_mat_set_element(node
->sched
,
2689 row
, j
, sol
->el
[1 + pos
+ j
]);
2691 csol
= isl_mat_vec_product(isl_mat_copy(node
->cmap
),
2695 for (j
= 0; j
< node
->nvar
; ++j
)
2696 node
->sched
= isl_mat_set_element(node
->sched
,
2697 row
, 1 + node
->nparam
+ j
, csol
->el
[j
]);
2698 node
->coincident
[graph
->n_total_row
] = coincident
;
2704 graph
->n_total_row
++;
2713 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2714 * and return this isl_aff.
2716 static __isl_give isl_aff
*extract_schedule_row(__isl_take isl_local_space
*ls
,
2717 struct isl_sched_node
*node
, int row
)
2725 aff
= isl_aff_zero_on_domain(ls
);
2726 isl_mat_get_element(node
->sched
, row
, 0, &v
);
2727 aff
= isl_aff_set_constant(aff
, v
);
2728 for (j
= 0; j
< node
->nparam
; ++j
) {
2729 isl_mat_get_element(node
->sched
, row
, 1 + j
, &v
);
2730 aff
= isl_aff_set_coefficient(aff
, isl_dim_param
, j
, v
);
2732 for (j
= 0; j
< node
->nvar
; ++j
) {
2733 isl_mat_get_element(node
->sched
, row
, 1 + node
->nparam
+ j
, &v
);
2734 aff
= isl_aff_set_coefficient(aff
, isl_dim_in
, j
, v
);
2742 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
2743 * and return this multi_aff.
2745 * The result is defined over the uncompressed node domain.
2747 static __isl_give isl_multi_aff
*node_extract_partial_schedule_multi_aff(
2748 struct isl_sched_node
*node
, int first
, int n
)
2752 isl_local_space
*ls
;
2759 nrow
= isl_mat_rows(node
->sched
);
2760 if (node
->compressed
)
2761 space
= isl_multi_aff_get_domain_space(node
->decompress
);
2763 space
= isl_space_copy(node
->space
);
2764 ls
= isl_local_space_from_space(isl_space_copy(space
));
2765 space
= isl_space_from_domain(space
);
2766 space
= isl_space_add_dims(space
, isl_dim_out
, n
);
2767 ma
= isl_multi_aff_zero(space
);
2769 for (i
= first
; i
< first
+ n
; ++i
) {
2770 aff
= extract_schedule_row(isl_local_space_copy(ls
), node
, i
);
2771 ma
= isl_multi_aff_set_aff(ma
, i
- first
, aff
);
2774 isl_local_space_free(ls
);
2776 if (node
->compressed
)
2777 ma
= isl_multi_aff_pullback_multi_aff(ma
,
2778 isl_multi_aff_copy(node
->compress
));
2783 /* Convert node->sched into a multi_aff and return this multi_aff.
2785 * The result is defined over the uncompressed node domain.
2787 static __isl_give isl_multi_aff
*node_extract_schedule_multi_aff(
2788 struct isl_sched_node
*node
)
2792 nrow
= isl_mat_rows(node
->sched
);
2793 return node_extract_partial_schedule_multi_aff(node
, 0, nrow
);
2796 /* Convert node->sched into a map and return this map.
2798 * The result is cached in node->sched_map, which needs to be released
2799 * whenever node->sched is updated.
2800 * It is defined over the uncompressed node domain.
2802 static __isl_give isl_map
*node_extract_schedule(struct isl_sched_node
*node
)
2804 if (!node
->sched_map
) {
2807 ma
= node_extract_schedule_multi_aff(node
);
2808 node
->sched_map
= isl_map_from_multi_aff(ma
);
2811 return isl_map_copy(node
->sched_map
);
2814 /* Construct a map that can be used to update a dependence relation
2815 * based on the current schedule.
2816 * That is, construct a map expressing that source and sink
2817 * are executed within the same iteration of the current schedule.
2818 * This map can then be intersected with the dependence relation.
2819 * This is not the most efficient way, but this shouldn't be a critical
2822 static __isl_give isl_map
*specializer(struct isl_sched_node
*src
,
2823 struct isl_sched_node
*dst
)
2825 isl_map
*src_sched
, *dst_sched
;
2827 src_sched
= node_extract_schedule(src
);
2828 dst_sched
= node_extract_schedule(dst
);
2829 return isl_map_apply_range(src_sched
, isl_map_reverse(dst_sched
));
2832 /* Intersect the domains of the nested relations in domain and range
2833 * of "umap" with "map".
2835 static __isl_give isl_union_map
*intersect_domains(
2836 __isl_take isl_union_map
*umap
, __isl_keep isl_map
*map
)
2838 isl_union_set
*uset
;
2840 umap
= isl_union_map_zip(umap
);
2841 uset
= isl_union_set_from_set(isl_map_wrap(isl_map_copy(map
)));
2842 umap
= isl_union_map_intersect_domain(umap
, uset
);
2843 umap
= isl_union_map_zip(umap
);
2847 /* Update the dependence relation of the given edge based
2848 * on the current schedule.
2849 * If the dependence is carried completely by the current schedule, then
2850 * it is removed from the edge_tables. It is kept in the list of edges
2851 * as otherwise all edge_tables would have to be recomputed.
2853 * If the edge is of a type that can appear multiple times
2854 * between the same pair of nodes, then it is added to
2855 * the edge table (again). This prevents the situation
2856 * where none of these edges is referenced from the edge table
2857 * because the one that was referenced turned out to be empty and
2858 * was therefore removed from the table.
2860 static int update_edge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
2861 struct isl_sched_edge
*edge
)
2866 id
= specializer(edge
->src
, edge
->dst
);
2867 edge
->map
= isl_map_intersect(edge
->map
, isl_map_copy(id
));
2871 if (edge
->tagged_condition
) {
2872 edge
->tagged_condition
=
2873 intersect_domains(edge
->tagged_condition
, id
);
2874 if (!edge
->tagged_condition
)
2877 if (edge
->tagged_validity
) {
2878 edge
->tagged_validity
=
2879 intersect_domains(edge
->tagged_validity
, id
);
2880 if (!edge
->tagged_validity
)
2884 empty
= isl_map_plain_is_empty(edge
->map
);
2888 graph_remove_edge(graph
, edge
);
2889 } else if (is_multi_edge_type(edge
)) {
2890 if (graph_edge_tables_add(ctx
, graph
, edge
) < 0)
2901 /* Does the domain of "umap" intersect "uset"?
2903 static int domain_intersects(__isl_keep isl_union_map
*umap
,
2904 __isl_keep isl_union_set
*uset
)
2908 umap
= isl_union_map_copy(umap
);
2909 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(uset
));
2910 empty
= isl_union_map_is_empty(umap
);
2911 isl_union_map_free(umap
);
2913 return empty
< 0 ? -1 : !empty
;
2916 /* Does the range of "umap" intersect "uset"?
2918 static int range_intersects(__isl_keep isl_union_map
*umap
,
2919 __isl_keep isl_union_set
*uset
)
2923 umap
= isl_union_map_copy(umap
);
2924 umap
= isl_union_map_intersect_range(umap
, isl_union_set_copy(uset
));
2925 empty
= isl_union_map_is_empty(umap
);
2926 isl_union_map_free(umap
);
2928 return empty
< 0 ? -1 : !empty
;
2931 /* Are the condition dependences of "edge" local with respect to
2932 * the current schedule?
2934 * That is, are domain and range of the condition dependences mapped
2935 * to the same point?
2937 * In other words, is the condition false?
2939 static int is_condition_false(struct isl_sched_edge
*edge
)
2941 isl_union_map
*umap
;
2942 isl_map
*map
, *sched
, *test
;
2945 empty
= isl_union_map_is_empty(edge
->tagged_condition
);
2946 if (empty
< 0 || empty
)
2949 umap
= isl_union_map_copy(edge
->tagged_condition
);
2950 umap
= isl_union_map_zip(umap
);
2951 umap
= isl_union_set_unwrap(isl_union_map_domain(umap
));
2952 map
= isl_map_from_union_map(umap
);
2954 sched
= node_extract_schedule(edge
->src
);
2955 map
= isl_map_apply_domain(map
, sched
);
2956 sched
= node_extract_schedule(edge
->dst
);
2957 map
= isl_map_apply_range(map
, sched
);
2959 test
= isl_map_identity(isl_map_get_space(map
));
2960 local
= isl_map_is_subset(map
, test
);
2967 /* For each conditional validity constraint that is adjacent
2968 * to a condition with domain in condition_source or range in condition_sink,
2969 * turn it into an unconditional validity constraint.
2971 static int unconditionalize_adjacent_validity(struct isl_sched_graph
*graph
,
2972 __isl_take isl_union_set
*condition_source
,
2973 __isl_take isl_union_set
*condition_sink
)
2977 condition_source
= isl_union_set_coalesce(condition_source
);
2978 condition_sink
= isl_union_set_coalesce(condition_sink
);
2980 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2982 isl_union_map
*validity
;
2984 if (!is_conditional_validity(&graph
->edge
[i
]))
2986 if (is_validity(&graph
->edge
[i
]))
2989 validity
= graph
->edge
[i
].tagged_validity
;
2990 adjacent
= domain_intersects(validity
, condition_sink
);
2991 if (adjacent
>= 0 && !adjacent
)
2992 adjacent
= range_intersects(validity
, condition_source
);
2998 set_validity(&graph
->edge
[i
]);
3001 isl_union_set_free(condition_source
);
3002 isl_union_set_free(condition_sink
);
3005 isl_union_set_free(condition_source
);
3006 isl_union_set_free(condition_sink
);
3010 /* Update the dependence relations of all edges based on the current schedule
3011 * and enforce conditional validity constraints that are adjacent
3012 * to satisfied condition constraints.
3014 * First check if any of the condition constraints are satisfied
3015 * (i.e., not local to the outer schedule) and keep track of
3016 * their domain and range.
3017 * Then update all dependence relations (which removes the non-local
3019 * Finally, if any condition constraints turned out to be satisfied,
3020 * then turn all adjacent conditional validity constraints into
3021 * unconditional validity constraints.
3023 static int update_edges(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3027 isl_union_set
*source
, *sink
;
3029 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3030 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3031 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3033 isl_union_set
*uset
;
3034 isl_union_map
*umap
;
3036 if (!is_condition(&graph
->edge
[i
]))
3038 if (is_local(&graph
->edge
[i
]))
3040 local
= is_condition_false(&graph
->edge
[i
]);
3048 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
3049 uset
= isl_union_map_domain(umap
);
3050 source
= isl_union_set_union(source
, uset
);
3052 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
3053 uset
= isl_union_map_range(umap
);
3054 sink
= isl_union_set_union(sink
, uset
);
3057 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3058 if (update_edge(ctx
, graph
, &graph
->edge
[i
]) < 0)
3063 return unconditionalize_adjacent_validity(graph
, source
, sink
);
3065 isl_union_set_free(source
);
3066 isl_union_set_free(sink
);
3069 isl_union_set_free(source
);
3070 isl_union_set_free(sink
);
3074 static void next_band(struct isl_sched_graph
*graph
)
3076 graph
->band_start
= graph
->n_total_row
;
3079 /* Return the union of the universe domains of the nodes in "graph"
3080 * that satisfy "pred".
3082 static __isl_give isl_union_set
*isl_sched_graph_domain(isl_ctx
*ctx
,
3083 struct isl_sched_graph
*graph
,
3084 int (*pred
)(struct isl_sched_node
*node
, int data
), int data
)
3090 for (i
= 0; i
< graph
->n
; ++i
)
3091 if (pred(&graph
->node
[i
], data
))
3095 isl_die(ctx
, isl_error_internal
,
3096 "empty component", return NULL
);
3098 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
3099 dom
= isl_union_set_from_set(set
);
3101 for (i
= i
+ 1; i
< graph
->n
; ++i
) {
3102 if (!pred(&graph
->node
[i
], data
))
3104 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
3105 dom
= isl_union_set_union(dom
, isl_union_set_from_set(set
));
3111 /* Return a list of unions of universe domains, where each element
3112 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
3114 static __isl_give isl_union_set_list
*extract_sccs(isl_ctx
*ctx
,
3115 struct isl_sched_graph
*graph
)
3118 isl_union_set_list
*filters
;
3120 filters
= isl_union_set_list_alloc(ctx
, graph
->scc
);
3121 for (i
= 0; i
< graph
->scc
; ++i
) {
3124 dom
= isl_sched_graph_domain(ctx
, graph
, &node_scc_exactly
, i
);
3125 filters
= isl_union_set_list_add(filters
, dom
);
3131 /* Return a list of two unions of universe domains, one for the SCCs up
3132 * to and including graph->src_scc and another for the other SCCs.
3134 static __isl_give isl_union_set_list
*extract_split(isl_ctx
*ctx
,
3135 struct isl_sched_graph
*graph
)
3138 isl_union_set_list
*filters
;
3140 filters
= isl_union_set_list_alloc(ctx
, 2);
3141 dom
= isl_sched_graph_domain(ctx
, graph
,
3142 &node_scc_at_most
, graph
->src_scc
);
3143 filters
= isl_union_set_list_add(filters
, dom
);
3144 dom
= isl_sched_graph_domain(ctx
, graph
,
3145 &node_scc_at_least
, graph
->src_scc
+ 1);
3146 filters
= isl_union_set_list_add(filters
, dom
);
3151 /* Copy nodes that satisfy node_pred from the src dependence graph
3152 * to the dst dependence graph.
3154 static int copy_nodes(struct isl_sched_graph
*dst
, struct isl_sched_graph
*src
,
3155 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
3160 for (i
= 0; i
< src
->n
; ++i
) {
3163 if (!node_pred(&src
->node
[i
], data
))
3167 dst
->node
[j
].space
= isl_space_copy(src
->node
[i
].space
);
3168 dst
->node
[j
].compressed
= src
->node
[i
].compressed
;
3169 dst
->node
[j
].hull
= isl_set_copy(src
->node
[i
].hull
);
3170 dst
->node
[j
].compress
=
3171 isl_multi_aff_copy(src
->node
[i
].compress
);
3172 dst
->node
[j
].decompress
=
3173 isl_multi_aff_copy(src
->node
[i
].decompress
);
3174 dst
->node
[j
].nvar
= src
->node
[i
].nvar
;
3175 dst
->node
[j
].nparam
= src
->node
[i
].nparam
;
3176 dst
->node
[j
].sched
= isl_mat_copy(src
->node
[i
].sched
);
3177 dst
->node
[j
].sched_map
= isl_map_copy(src
->node
[i
].sched_map
);
3178 dst
->node
[j
].coincident
= src
->node
[i
].coincident
;
3179 dst
->node
[j
].sizes
= isl_multi_val_copy(src
->node
[i
].sizes
);
3180 dst
->node
[j
].max
= isl_vec_copy(src
->node
[i
].max
);
3183 if (!dst
->node
[j
].space
|| !dst
->node
[j
].sched
)
3185 if (dst
->node
[j
].compressed
&&
3186 (!dst
->node
[j
].hull
|| !dst
->node
[j
].compress
||
3187 !dst
->node
[j
].decompress
))
3194 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
3195 * to the dst dependence graph.
3196 * If the source or destination node of the edge is not in the destination
3197 * graph, then it must be a backward proximity edge and it should simply
3200 static int copy_edges(isl_ctx
*ctx
, struct isl_sched_graph
*dst
,
3201 struct isl_sched_graph
*src
,
3202 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
), int data
)
3207 for (i
= 0; i
< src
->n_edge
; ++i
) {
3208 struct isl_sched_edge
*edge
= &src
->edge
[i
];
3210 isl_union_map
*tagged_condition
;
3211 isl_union_map
*tagged_validity
;
3212 struct isl_sched_node
*dst_src
, *dst_dst
;
3214 if (!edge_pred(edge
, data
))
3217 if (isl_map_plain_is_empty(edge
->map
))
3220 dst_src
= graph_find_node(ctx
, dst
, edge
->src
->space
);
3221 dst_dst
= graph_find_node(ctx
, dst
, edge
->dst
->space
);
3222 if (!dst_src
|| !dst_dst
) {
3223 if (is_validity(edge
) || is_conditional_validity(edge
))
3224 isl_die(ctx
, isl_error_internal
,
3225 "backward (conditional) validity edge",
3230 map
= isl_map_copy(edge
->map
);
3231 tagged_condition
= isl_union_map_copy(edge
->tagged_condition
);
3232 tagged_validity
= isl_union_map_copy(edge
->tagged_validity
);
3234 dst
->edge
[dst
->n_edge
].src
= dst_src
;
3235 dst
->edge
[dst
->n_edge
].dst
= dst_dst
;
3236 dst
->edge
[dst
->n_edge
].map
= map
;
3237 dst
->edge
[dst
->n_edge
].tagged_condition
= tagged_condition
;
3238 dst
->edge
[dst
->n_edge
].tagged_validity
= tagged_validity
;
3239 dst
->edge
[dst
->n_edge
].types
= edge
->types
;
3242 if (edge
->tagged_condition
&& !tagged_condition
)
3244 if (edge
->tagged_validity
&& !tagged_validity
)
3247 if (graph_edge_tables_add(ctx
, dst
,
3248 &dst
->edge
[dst
->n_edge
- 1]) < 0)
3255 /* Compute the maximal number of variables over all nodes.
3256 * This is the maximal number of linearly independent schedule
3257 * rows that we need to compute.
3258 * Just in case we end up in a part of the dependence graph
3259 * with only lower-dimensional domains, we make sure we will
3260 * compute the required amount of extra linearly independent rows.
3262 static int compute_maxvar(struct isl_sched_graph
*graph
)
3267 for (i
= 0; i
< graph
->n
; ++i
) {
3268 struct isl_sched_node
*node
= &graph
->node
[i
];
3271 if (node_update_cmap(node
) < 0)
3273 nvar
= node
->nvar
+ graph
->n_row
- node
->rank
;
3274 if (nvar
> graph
->maxvar
)
3275 graph
->maxvar
= nvar
;
3281 /* Extract the subgraph of "graph" that consists of the node satisfying
3282 * "node_pred" and the edges satisfying "edge_pred" and store
3283 * the result in "sub".
3285 static int extract_sub_graph(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
3286 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
3287 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
3288 int data
, struct isl_sched_graph
*sub
)
3290 int i
, n
= 0, n_edge
= 0;
3293 for (i
= 0; i
< graph
->n
; ++i
)
3294 if (node_pred(&graph
->node
[i
], data
))
3296 for (i
= 0; i
< graph
->n_edge
; ++i
)
3297 if (edge_pred(&graph
->edge
[i
], data
))
3299 if (graph_alloc(ctx
, sub
, n
, n_edge
) < 0)
3301 if (copy_nodes(sub
, graph
, node_pred
, data
) < 0)
3303 if (graph_init_table(ctx
, sub
) < 0)
3305 for (t
= 0; t
<= isl_edge_last
; ++t
)
3306 sub
->max_edge
[t
] = graph
->max_edge
[t
];
3307 if (graph_init_edge_tables(ctx
, sub
) < 0)
3309 if (copy_edges(ctx
, sub
, graph
, edge_pred
, data
) < 0)
3311 sub
->n_row
= graph
->n_row
;
3312 sub
->max_row
= graph
->max_row
;
3313 sub
->n_total_row
= graph
->n_total_row
;
3314 sub
->band_start
= graph
->band_start
;
3319 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
3320 struct isl_sched_graph
*graph
);
3321 static __isl_give isl_schedule_node
*compute_schedule_wcc(
3322 isl_schedule_node
*node
, struct isl_sched_graph
*graph
);
3324 /* Compute a schedule for a subgraph of "graph". In particular, for
3325 * the graph composed of nodes that satisfy node_pred and edges that
3326 * that satisfy edge_pred.
3327 * If the subgraph is known to consist of a single component, then wcc should
3328 * be set and then we call compute_schedule_wcc on the constructed subgraph.
3329 * Otherwise, we call compute_schedule, which will check whether the subgraph
3332 * The schedule is inserted at "node" and the updated schedule node
3335 static __isl_give isl_schedule_node
*compute_sub_schedule(
3336 __isl_take isl_schedule_node
*node
, isl_ctx
*ctx
,
3337 struct isl_sched_graph
*graph
,
3338 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
3339 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
3342 struct isl_sched_graph split
= { 0 };
3344 if (extract_sub_graph(ctx
, graph
, node_pred
, edge_pred
, data
,
3349 node
= compute_schedule_wcc(node
, &split
);
3351 node
= compute_schedule(node
, &split
);
3353 graph_free(ctx
, &split
);
3356 graph_free(ctx
, &split
);
3357 return isl_schedule_node_free(node
);
3360 static int edge_scc_exactly(struct isl_sched_edge
*edge
, int scc
)
3362 return edge
->src
->scc
== scc
&& edge
->dst
->scc
== scc
;
3365 static int edge_dst_scc_at_most(struct isl_sched_edge
*edge
, int scc
)
3367 return edge
->dst
->scc
<= scc
;
3370 static int edge_src_scc_at_least(struct isl_sched_edge
*edge
, int scc
)
3372 return edge
->src
->scc
>= scc
;
3375 /* Reset the current band by dropping all its schedule rows.
3377 static int reset_band(struct isl_sched_graph
*graph
)
3382 drop
= graph
->n_total_row
- graph
->band_start
;
3383 graph
->n_total_row
-= drop
;
3384 graph
->n_row
-= drop
;
3386 for (i
= 0; i
< graph
->n
; ++i
) {
3387 struct isl_sched_node
*node
= &graph
->node
[i
];
3389 isl_map_free(node
->sched_map
);
3390 node
->sched_map
= NULL
;
3392 node
->sched
= isl_mat_drop_rows(node
->sched
,
3393 graph
->band_start
, drop
);
3402 /* Split the current graph into two parts and compute a schedule for each
3403 * part individually. In particular, one part consists of all SCCs up
3404 * to and including graph->src_scc, while the other part contains the other
3405 * SCCs. The split is enforced by a sequence node inserted at position "node"
3406 * in the schedule tree. Return the updated schedule node.
3407 * If either of these two parts consists of a sequence, then it is spliced
3408 * into the sequence containing the two parts.
3410 * The current band is reset. It would be possible to reuse
3411 * the previously computed rows as the first rows in the next
3412 * band, but recomputing them may result in better rows as we are looking
3413 * at a smaller part of the dependence graph.
3415 static __isl_give isl_schedule_node
*compute_split_schedule(
3416 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3420 isl_union_set_list
*filters
;
3425 if (reset_band(graph
) < 0)
3426 return isl_schedule_node_free(node
);
3430 ctx
= isl_schedule_node_get_ctx(node
);
3431 filters
= extract_split(ctx
, graph
);
3432 node
= isl_schedule_node_insert_sequence(node
, filters
);
3433 node
= isl_schedule_node_child(node
, 1);
3434 node
= isl_schedule_node_child(node
, 0);
3436 node
= compute_sub_schedule(node
, ctx
, graph
,
3437 &node_scc_at_least
, &edge_src_scc_at_least
,
3438 graph
->src_scc
+ 1, 0);
3439 is_seq
= isl_schedule_node_get_type(node
) == isl_schedule_node_sequence
;
3440 node
= isl_schedule_node_parent(node
);
3441 node
= isl_schedule_node_parent(node
);
3443 node
= isl_schedule_node_sequence_splice_child(node
, 1);
3444 node
= isl_schedule_node_child(node
, 0);
3445 node
= isl_schedule_node_child(node
, 0);
3446 node
= compute_sub_schedule(node
, ctx
, graph
,
3447 &node_scc_at_most
, &edge_dst_scc_at_most
,
3449 is_seq
= isl_schedule_node_get_type(node
) == isl_schedule_node_sequence
;
3450 node
= isl_schedule_node_parent(node
);
3451 node
= isl_schedule_node_parent(node
);
3453 node
= isl_schedule_node_sequence_splice_child(node
, 0);
3458 /* Insert a band node at position "node" in the schedule tree corresponding
3459 * to the current band in "graph". Mark the band node permutable
3460 * if "permutable" is set.
3461 * The partial schedules and the coincidence property are extracted
3462 * from the graph nodes.
3463 * Return the updated schedule node.
3465 static __isl_give isl_schedule_node
*insert_current_band(
3466 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
3472 isl_multi_pw_aff
*mpa
;
3473 isl_multi_union_pw_aff
*mupa
;
3479 isl_die(isl_schedule_node_get_ctx(node
), isl_error_internal
,
3480 "graph should have at least one node",
3481 return isl_schedule_node_free(node
));
3483 start
= graph
->band_start
;
3484 end
= graph
->n_total_row
;
3487 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[0], start
, n
);
3488 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3489 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3491 for (i
= 1; i
< graph
->n
; ++i
) {
3492 isl_multi_union_pw_aff
*mupa_i
;
3494 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[i
],
3496 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3497 mupa_i
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3498 mupa
= isl_multi_union_pw_aff_union_add(mupa
, mupa_i
);
3500 node
= isl_schedule_node_insert_partial_schedule(node
, mupa
);
3502 for (i
= 0; i
< n
; ++i
)
3503 node
= isl_schedule_node_band_member_set_coincident(node
, i
,
3504 graph
->node
[0].coincident
[start
+ i
]);
3505 node
= isl_schedule_node_band_set_permutable(node
, permutable
);
3510 /* Update the dependence relations based on the current schedule,
3511 * add the current band to "node" and then continue with the computation
3513 * Return the updated schedule node.
3515 static __isl_give isl_schedule_node
*compute_next_band(
3516 __isl_take isl_schedule_node
*node
,
3517 struct isl_sched_graph
*graph
, int permutable
)
3524 ctx
= isl_schedule_node_get_ctx(node
);
3525 if (update_edges(ctx
, graph
) < 0)
3526 return isl_schedule_node_free(node
);
3527 node
= insert_current_band(node
, graph
, permutable
);
3530 node
= isl_schedule_node_child(node
, 0);
3531 node
= compute_schedule(node
, graph
);
3532 node
= isl_schedule_node_parent(node
);
3537 /* Add constraints to graph->lp that force the dependence "map" (which
3538 * is part of the dependence relation of "edge")
3539 * to be respected and attempt to carry it, where the edge is one from
3540 * a node j to itself. "pos" is the sequence number of the given map.
3541 * That is, add constraints that enforce
3543 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3544 * = c_j_x (y - x) >= e_i
3546 * for each (x,y) in R.
3547 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3548 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3549 * with each coefficient in c_j_x represented as a pair of non-negative
3552 static int add_intra_constraints(struct isl_sched_graph
*graph
,
3553 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
3556 isl_ctx
*ctx
= isl_map_get_ctx(map
);
3557 isl_dim_map
*dim_map
;
3558 isl_basic_set
*coef
;
3559 struct isl_sched_node
*node
= edge
->src
;
3561 coef
= intra_coefficients(graph
, node
, map
);
3565 offset
= coef_var_offset(coef
);
3566 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, 1);
3567 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3568 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
3569 coef
->n_eq
, coef
->n_ineq
);
3570 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
3576 /* Add constraints to graph->lp that force the dependence "map" (which
3577 * is part of the dependence relation of "edge")
3578 * to be respected and attempt to carry it, where the edge is one from
3579 * node j to node k. "pos" is the sequence number of the given map.
3580 * That is, add constraints that enforce
3582 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3584 * for each (x,y) in R.
3585 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3586 * of valid constraints for R and then plug in
3587 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3588 * with each coefficient (except e_i, c_*_0 and c_*_n)
3589 * represented as a pair of non-negative coefficients.
3591 static int add_inter_constraints(struct isl_sched_graph
*graph
,
3592 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
3595 isl_ctx
*ctx
= isl_map_get_ctx(map
);
3596 isl_dim_map
*dim_map
;
3597 isl_basic_set
*coef
;
3598 struct isl_sched_node
*src
= edge
->src
;
3599 struct isl_sched_node
*dst
= edge
->dst
;
3601 coef
= inter_coefficients(graph
, edge
, map
);
3605 offset
= coef_var_offset(coef
);
3606 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, 1);
3607 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3608 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
3609 coef
->n_eq
, coef
->n_ineq
);
3610 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
3616 /* Add constraints to graph->lp that force all (conditional) validity
3617 * dependences to be respected and attempt to carry them.
3619 static int add_all_constraints(struct isl_sched_graph
*graph
)
3625 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3626 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
3628 if (!is_any_validity(edge
))
3631 for (j
= 0; j
< edge
->map
->n
; ++j
) {
3632 isl_basic_map
*bmap
;
3635 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
3636 map
= isl_map_from_basic_map(bmap
);
3638 if (edge
->src
== edge
->dst
&&
3639 add_intra_constraints(graph
, edge
, map
, pos
) < 0)
3641 if (edge
->src
!= edge
->dst
&&
3642 add_inter_constraints(graph
, edge
, map
, pos
) < 0)
3651 /* Count the number of equality and inequality constraints
3652 * that will be added to the carry_lp problem.
3653 * We count each edge exactly once.
3655 static int count_all_constraints(struct isl_sched_graph
*graph
,
3656 int *n_eq
, int *n_ineq
)
3660 *n_eq
= *n_ineq
= 0;
3661 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3662 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
3664 if (!is_any_validity(edge
))
3667 for (j
= 0; j
< edge
->map
->n
; ++j
) {
3668 isl_basic_map
*bmap
;
3671 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
3672 map
= isl_map_from_basic_map(bmap
);
3674 if (count_map_constraints(graph
, edge
, map
,
3675 n_eq
, n_ineq
, 1, 0) < 0)
3683 /* Return the total number of (validity) edges that carry_dependences will
3686 static int count_carry_edges(struct isl_sched_graph
*graph
)
3692 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3693 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
3695 if (!is_any_validity(edge
))
3698 n_edge
+= isl_map_n_basic_map(edge
->map
);
3704 /* Construct an LP problem for finding schedule coefficients
3705 * such that the schedule carries as many validity dependences as possible.
3706 * In particular, for each dependence i, we bound the dependence distance
3707 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3708 * of all e_i's. Dependences with e_i = 0 in the solution are simply
3709 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3710 * Note that if the dependence relation is a union of basic maps,
3711 * then we have to consider each basic map individually as it may only
3712 * be possible to carry the dependences expressed by some of those
3713 * basic maps and not all of them.
3714 * Below, we consider each of those basic maps as a separate "edge".
3716 * All variables of the LP are non-negative. The actual coefficients
3717 * may be negative, so each coefficient is represented as the difference
3718 * of two non-negative variables. The negative part always appears
3719 * immediately before the positive part.
3720 * Other than that, the variables have the following order
3722 * - sum of (1 - e_i) over all edges
3723 * - sum of all c_n coefficients
3724 * (unconstrained when computing non-parametric schedules)
3725 * - sum of positive and negative parts of all c_x coefficients
3730 * - c_i_n (if parametric)
3731 * - positive and negative parts of c_i_x
3733 * The constraints are those from the (validity) edges plus three equalities
3734 * to express the sums and n_edge inequalities to express e_i <= 1.
3736 static isl_stat
setup_carry_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3745 n_edge
= count_carry_edges(graph
);
3748 for (i
= 0; i
< graph
->n
; ++i
) {
3749 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
3750 node
->start
= total
;
3751 total
+= 1 + node
->nparam
+ 2 * node
->nvar
;
3754 if (count_all_constraints(graph
, &n_eq
, &n_ineq
) < 0)
3755 return isl_stat_error
;
3757 dim
= isl_space_set_alloc(ctx
, 0, total
);
3758 isl_basic_set_free(graph
->lp
);
3761 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
3762 graph
->lp
= isl_basic_set_set_rational(graph
->lp
);
3764 k
= isl_basic_set_alloc_equality(graph
->lp
);
3766 return isl_stat_error
;
3767 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
3768 isl_int_set_si(graph
->lp
->eq
[k
][0], -n_edge
);
3769 isl_int_set_si(graph
->lp
->eq
[k
][1], 1);
3770 for (i
= 0; i
< n_edge
; ++i
)
3771 isl_int_set_si(graph
->lp
->eq
[k
][4 + i
], 1);
3773 if (add_param_sum_constraint(graph
, 1) < 0)
3774 return isl_stat_error
;
3775 if (add_var_sum_constraint(graph
, 2) < 0)
3776 return isl_stat_error
;
3778 for (i
= 0; i
< n_edge
; ++i
) {
3779 k
= isl_basic_set_alloc_inequality(graph
->lp
);
3781 return isl_stat_error
;
3782 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
3783 isl_int_set_si(graph
->lp
->ineq
[k
][4 + i
], -1);
3784 isl_int_set_si(graph
->lp
->ineq
[k
][0], 1);
3787 if (add_all_constraints(graph
) < 0)
3788 return isl_stat_error
;
3793 static __isl_give isl_schedule_node
*compute_component_schedule(
3794 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
3797 /* Comparison function for sorting the statements based on
3798 * the corresponding value in "r".
3800 static int smaller_value(const void *a
, const void *b
, void *data
)
3806 return isl_int_cmp(r
->el
[*i1
], r
->el
[*i2
]);
3809 /* If the schedule_split_scaled option is set and if the linear
3810 * parts of the scheduling rows for all nodes in the graphs have
3811 * a non-trivial common divisor, then split off the remainder of the
3812 * constant term modulo this common divisor from the linear part.
3813 * Otherwise, insert a band node directly and continue with
3814 * the construction of the schedule.
3816 * If a non-trivial common divisor is found, then
3817 * the linear part is reduced and the remainder is enforced
3818 * by a sequence node with the children placed in the order
3819 * of this remainder.
3820 * In particular, we assign an scc index based on the remainder and
3821 * then rely on compute_component_schedule to insert the sequence and
3822 * to continue the schedule construction on each part.
3824 static __isl_give isl_schedule_node
*split_scaled(
3825 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3838 ctx
= isl_schedule_node_get_ctx(node
);
3839 if (!ctx
->opt
->schedule_split_scaled
)
3840 return compute_next_band(node
, graph
, 0);
3842 return compute_next_band(node
, graph
, 0);
3845 isl_int_init(gcd_i
);
3847 isl_int_set_si(gcd
, 0);
3849 row
= isl_mat_rows(graph
->node
[0].sched
) - 1;
3851 for (i
= 0; i
< graph
->n
; ++i
) {
3852 struct isl_sched_node
*node
= &graph
->node
[i
];
3853 int cols
= isl_mat_cols(node
->sched
);
3855 isl_seq_gcd(node
->sched
->row
[row
] + 1, cols
- 1, &gcd_i
);
3856 isl_int_gcd(gcd
, gcd
, gcd_i
);
3859 isl_int_clear(gcd_i
);
3861 if (isl_int_cmp_si(gcd
, 1) <= 0) {
3863 return compute_next_band(node
, graph
, 0);
3866 r
= isl_vec_alloc(ctx
, graph
->n
);
3867 order
= isl_calloc_array(ctx
, int, graph
->n
);
3871 for (i
= 0; i
< graph
->n
; ++i
) {
3872 struct isl_sched_node
*node
= &graph
->node
[i
];
3875 isl_int_fdiv_r(r
->el
[i
], node
->sched
->row
[row
][0], gcd
);
3876 isl_int_fdiv_q(node
->sched
->row
[row
][0],
3877 node
->sched
->row
[row
][0], gcd
);
3878 isl_int_mul(node
->sched
->row
[row
][0],
3879 node
->sched
->row
[row
][0], gcd
);
3880 node
->sched
= isl_mat_scale_down_row(node
->sched
, row
, gcd
);
3885 if (isl_sort(order
, graph
->n
, sizeof(order
[0]), &smaller_value
, r
) < 0)
3889 for (i
= 0; i
< graph
->n
; ++i
) {
3890 if (i
> 0 && isl_int_ne(r
->el
[order
[i
- 1]], r
->el
[order
[i
]]))
3892 graph
->node
[order
[i
]].scc
= scc
;
3901 if (update_edges(ctx
, graph
) < 0)
3902 return isl_schedule_node_free(node
);
3903 node
= insert_current_band(node
, graph
, 0);
3906 node
= isl_schedule_node_child(node
, 0);
3907 node
= compute_component_schedule(node
, graph
, 0);
3908 node
= isl_schedule_node_parent(node
);
3915 return isl_schedule_node_free(node
);
3918 /* Is the schedule row "sol" trivial on node "node"?
3919 * That is, is the solution zero on the dimensions orthogonal to
3920 * the previously found solutions?
3921 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3923 * Each coefficient is represented as the difference between
3924 * two non-negative values in "sol". "sol" has been computed
3925 * in terms of the original iterators (i.e., without use of cmap).
3926 * We construct the schedule row s and write it as a linear
3927 * combination of (linear combinations of) previously computed schedule rows.
3928 * s = Q c or c = U s.
3929 * If the final entries of c are all zero, then the solution is trivial.
3931 static int is_trivial(struct isl_sched_node
*node
, __isl_keep isl_vec
*sol
)
3938 if (node
->nvar
== node
->rank
)
3941 node_sol
= extract_var_coef(node
, sol
);
3942 node_sol
= isl_mat_vec_product(isl_mat_copy(node
->cinv
), node_sol
);
3946 trivial
= isl_seq_first_non_zero(node_sol
->el
+ node
->rank
,
3947 node
->nvar
- node
->rank
) == -1;
3949 isl_vec_free(node_sol
);
3954 /* Is the schedule row "sol" trivial on any node where it should
3956 * "sol" has been computed in terms of the original iterators
3957 * (i.e., without use of cmap).
3958 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3960 static int is_any_trivial(struct isl_sched_graph
*graph
,
3961 __isl_keep isl_vec
*sol
)
3965 for (i
= 0; i
< graph
->n
; ++i
) {
3966 struct isl_sched_node
*node
= &graph
->node
[i
];
3969 if (!needs_row(graph
, node
))
3971 trivial
= is_trivial(node
, sol
);
3972 if (trivial
< 0 || trivial
)
3979 /* Does the schedule represented by "sol" perform loop coalescing on "node"?
3980 * If so, return the position of the coalesced dimension.
3981 * Otherwise, return node->nvar or -1 on error.
3983 * In particular, look for pairs of coefficients c_i and c_j such that
3984 * |c_j/c_i| >= size_i, i.e., |c_j| >= |c_i * size_i|.
3985 * If any such pair is found, then return i.
3986 * If size_i is infinity, then no check on c_i needs to be performed.
3988 static int find_node_coalescing(struct isl_sched_node
*node
,
3989 __isl_keep isl_vec
*sol
)
3995 if (node
->nvar
<= 1)
3998 csol
= extract_var_coef(node
, sol
);
4002 for (i
= 0; i
< node
->nvar
; ++i
) {
4005 if (isl_int_is_zero(csol
->el
[i
]))
4007 v
= isl_multi_val_get_val(node
->sizes
, i
);
4010 if (!isl_val_is_int(v
)) {
4014 isl_int_mul(max
, v
->n
, csol
->el
[i
]);
4017 for (j
= 0; j
< node
->nvar
; ++j
) {
4020 if (isl_int_abs_ge(csol
->el
[j
], max
))
4036 /* Force the schedule coefficient at position "pos" of "node" to be zero
4038 * The coefficient is encoded as the difference between two non-negative
4039 * variables. Force these two variables to have the same value.
4041 static __isl_give isl_tab_lexmin
*zero_out_node_coef(
4042 __isl_take isl_tab_lexmin
*tl
, struct isl_sched_node
*node
, int pos
)
4048 ctx
= isl_space_get_ctx(node
->space
);
4049 dim
= isl_tab_lexmin_dim(tl
);
4051 return isl_tab_lexmin_free(tl
);
4052 eq
= isl_vec_alloc(ctx
, 1 + dim
);
4053 eq
= isl_vec_clr(eq
);
4055 return isl_tab_lexmin_free(tl
);
4057 pos
= 1 + node_var_coef_offset(node
) + 2 * pos
;
4058 isl_int_set_si(eq
->el
[pos
], 1);
4059 isl_int_set_si(eq
->el
[pos
+ 1], -1);
4060 tl
= isl_tab_lexmin_add_eq(tl
, eq
->el
);
4066 /* Return the lexicographically smallest rational point in the basic set
4067 * from which "tl" was constructed, double checking that this input set
4070 static __isl_give isl_vec
*non_empty_solution(__isl_keep isl_tab_lexmin
*tl
)
4074 sol
= isl_tab_lexmin_get_solution(tl
);
4078 isl_die(isl_vec_get_ctx(sol
), isl_error_internal
,
4079 "error in schedule construction",
4080 return isl_vec_free(sol
));
4084 /* Does the solution "sol" of the LP problem constructed by setup_carry_lp
4085 * carry any of the "n_edge" groups of dependences?
4086 * The value in the first position is the sum of (1 - e_i) over all "n_edge"
4087 * edges, with 0 <= e_i <= 1 equal to 1 when the dependences represented
4088 * by the edge are carried by the solution.
4089 * If the sum of the (1 - e_i) is smaller than "n_edge" then at least
4090 * one of those is carried.
4092 * Note that despite the fact that the problem is solved using a rational
4093 * solver, the solution is guaranteed to be integral.
4094 * Specifically, the dependence distance lower bounds e_i (and therefore
4095 * also their sum) are integers. See Lemma 5 of [1].
4097 * Any potential denominator of the sum is cleared by this function.
4098 * The denominator is not relevant for any of the other elements
4101 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4102 * Problem, Part II: Multi-Dimensional Time.
4103 * In Intl. Journal of Parallel Programming, 1992.
4105 static int carries_dependences(__isl_keep isl_vec
*sol
, int n_edge
)
4107 isl_int_divexact(sol
->el
[1], sol
->el
[1], sol
->el
[0]);
4108 isl_int_set_si(sol
->el
[0], 1);
4109 return isl_int_cmp_si(sol
->el
[1], n_edge
) < 0;
4112 /* Return the lexicographically smallest rational point in "lp",
4113 * assuming that all variables are non-negative and performing some
4114 * additional sanity checks.
4115 * In particular, "lp" should not be empty by construction.
4116 * Double check that this is the case.
4117 * Also, check that dependences are carried for at least one of
4118 * the "n_edge" edges.
4120 * If the computed schedule performs loop coalescing on a given node,
4121 * i.e., if it is of the form
4123 * c_i i + c_j j + ...
4125 * with |c_j/c_i| >= size_i, then force the coefficient c_i to be zero
4126 * to cut out this solution. Repeat this process until no more loop
4127 * coalescing occurs or until no more dependences can be carried.
4128 * In the latter case, revert to the previously computed solution.
4130 static __isl_give isl_vec
*non_neg_lexmin(struct isl_sched_graph
*graph
,
4131 __isl_take isl_basic_set
*lp
, int n_edge
)
4136 isl_vec
*sol
, *prev
= NULL
;
4137 int treat_coalescing
;
4141 ctx
= isl_basic_set_get_ctx(lp
);
4142 treat_coalescing
= isl_options_get_schedule_treat_coalescing(ctx
);
4143 tl
= isl_tab_lexmin_from_basic_set(lp
);
4146 sol
= non_empty_solution(tl
);
4150 if (!carries_dependences(sol
, n_edge
)) {
4152 isl_die(ctx
, isl_error_unknown
,
4153 "unable to carry dependences",
4159 prev
= isl_vec_free(prev
);
4160 if (!treat_coalescing
)
4162 for (i
= 0; i
< graph
->n
; ++i
) {
4163 struct isl_sched_node
*node
= &graph
->node
[i
];
4165 pos
= find_node_coalescing(node
, sol
);
4168 if (pos
< node
->nvar
)
4173 tl
= zero_out_node_coef(tl
, &graph
->node
[i
], pos
);
4175 } while (i
< graph
->n
);
4177 isl_tab_lexmin_free(tl
);
4181 isl_tab_lexmin_free(tl
);
4187 /* Construct a schedule row for each node such that as many validity dependences
4188 * as possible are carried and then continue with the next band.
4190 * If there are no validity dependences, then no dependence can be carried and
4191 * the procedure is guaranteed to fail. If there is more than one component,
4192 * then try computing a schedule on each component separately
4193 * to prevent or at least postpone this failure.
4195 * If the computed schedule row turns out to be trivial on one or
4196 * more nodes where it should not be trivial, then we throw it away
4197 * and try again on each component separately.
4199 * If there is only one component, then we accept the schedule row anyway,
4200 * but we do not consider it as a complete row and therefore do not
4201 * increment graph->n_row. Note that the ranks of the nodes that
4202 * do get a non-trivial schedule part will get updated regardless and
4203 * graph->maxvar is computed based on these ranks. The test for
4204 * whether more schedule rows are required in compute_schedule_wcc
4205 * is therefore not affected.
4207 * Insert a band corresponding to the schedule row at position "node"
4208 * of the schedule tree and continue with the construction of the schedule.
4209 * This insertion and the continued construction is performed by split_scaled
4210 * after optionally checking for non-trivial common divisors.
4212 static __isl_give isl_schedule_node
*carry_dependences(
4213 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4224 n_edge
= count_carry_edges(graph
);
4225 if (n_edge
== 0 && graph
->scc
> 1)
4226 return compute_component_schedule(node
, graph
, 1);
4228 ctx
= isl_schedule_node_get_ctx(node
);
4229 if (setup_carry_lp(ctx
, graph
) < 0)
4230 return isl_schedule_node_free(node
);
4232 lp
= isl_basic_set_copy(graph
->lp
);
4233 sol
= non_neg_lexmin(graph
, lp
, n_edge
);
4235 return isl_schedule_node_free(node
);
4237 trivial
= is_any_trivial(graph
, sol
);
4239 sol
= isl_vec_free(sol
);
4240 } else if (trivial
&& graph
->scc
> 1) {
4242 return compute_component_schedule(node
, graph
, 1);
4245 if (update_schedule(graph
, sol
, 0, 0) < 0)
4246 return isl_schedule_node_free(node
);
4250 return split_scaled(node
, graph
);
4253 /* Topologically sort statements mapped to the same schedule iteration
4254 * and add insert a sequence node in front of "node"
4255 * corresponding to this order.
4256 * If "initialized" is set, then it may be assumed that compute_maxvar
4257 * has been called on the current band. Otherwise, call
4258 * compute_maxvar if and before carry_dependences gets called.
4260 * If it turns out to be impossible to sort the statements apart,
4261 * because different dependences impose different orderings
4262 * on the statements, then we extend the schedule such that
4263 * it carries at least one more dependence.
4265 static __isl_give isl_schedule_node
*sort_statements(
4266 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
4270 isl_union_set_list
*filters
;
4275 ctx
= isl_schedule_node_get_ctx(node
);
4277 isl_die(ctx
, isl_error_internal
,
4278 "graph should have at least one node",
4279 return isl_schedule_node_free(node
));
4284 if (update_edges(ctx
, graph
) < 0)
4285 return isl_schedule_node_free(node
);
4287 if (graph
->n_edge
== 0)
4290 if (detect_sccs(ctx
, graph
) < 0)
4291 return isl_schedule_node_free(node
);
4294 if (graph
->scc
< graph
->n
) {
4295 if (!initialized
&& compute_maxvar(graph
) < 0)
4296 return isl_schedule_node_free(node
);
4297 return carry_dependences(node
, graph
);
4300 filters
= extract_sccs(ctx
, graph
);
4301 node
= isl_schedule_node_insert_sequence(node
, filters
);
4306 /* Are there any (non-empty) (conditional) validity edges in the graph?
4308 static int has_validity_edges(struct isl_sched_graph
*graph
)
4312 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4315 empty
= isl_map_plain_is_empty(graph
->edge
[i
].map
);
4320 if (is_any_validity(&graph
->edge
[i
]))
4327 /* Should we apply a Feautrier step?
4328 * That is, did the user request the Feautrier algorithm and are
4329 * there any validity dependences (left)?
4331 static int need_feautrier_step(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
4333 if (ctx
->opt
->schedule_algorithm
!= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
)
4336 return has_validity_edges(graph
);
4339 /* Compute a schedule for a connected dependence graph using Feautrier's
4340 * multi-dimensional scheduling algorithm and return the updated schedule node.
4342 * The original algorithm is described in [1].
4343 * The main idea is to minimize the number of scheduling dimensions, by
4344 * trying to satisfy as many dependences as possible per scheduling dimension.
4346 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4347 * Problem, Part II: Multi-Dimensional Time.
4348 * In Intl. Journal of Parallel Programming, 1992.
4350 static __isl_give isl_schedule_node
*compute_schedule_wcc_feautrier(
4351 isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4353 return carry_dependences(node
, graph
);
4356 /* Turn off the "local" bit on all (condition) edges.
4358 static void clear_local_edges(struct isl_sched_graph
*graph
)
4362 for (i
= 0; i
< graph
->n_edge
; ++i
)
4363 if (is_condition(&graph
->edge
[i
]))
4364 clear_local(&graph
->edge
[i
]);
4367 /* Does "graph" have both condition and conditional validity edges?
4369 static int need_condition_check(struct isl_sched_graph
*graph
)
4372 int any_condition
= 0;
4373 int any_conditional_validity
= 0;
4375 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4376 if (is_condition(&graph
->edge
[i
]))
4378 if (is_conditional_validity(&graph
->edge
[i
]))
4379 any_conditional_validity
= 1;
4382 return any_condition
&& any_conditional_validity
;
4385 /* Does "graph" contain any coincidence edge?
4387 static int has_any_coincidence(struct isl_sched_graph
*graph
)
4391 for (i
= 0; i
< graph
->n_edge
; ++i
)
4392 if (is_coincidence(&graph
->edge
[i
]))
4398 /* Extract the final schedule row as a map with the iteration domain
4399 * of "node" as domain.
4401 static __isl_give isl_map
*final_row(struct isl_sched_node
*node
)
4406 row
= isl_mat_rows(node
->sched
) - 1;
4407 ma
= node_extract_partial_schedule_multi_aff(node
, row
, 1);
4408 return isl_map_from_multi_aff(ma
);
4411 /* Is the conditional validity dependence in the edge with index "edge_index"
4412 * violated by the latest (i.e., final) row of the schedule?
4413 * That is, is i scheduled after j
4414 * for any conditional validity dependence i -> j?
4416 static int is_violated(struct isl_sched_graph
*graph
, int edge_index
)
4418 isl_map
*src_sched
, *dst_sched
, *map
;
4419 struct isl_sched_edge
*edge
= &graph
->edge
[edge_index
];
4422 src_sched
= final_row(edge
->src
);
4423 dst_sched
= final_row(edge
->dst
);
4424 map
= isl_map_copy(edge
->map
);
4425 map
= isl_map_apply_domain(map
, src_sched
);
4426 map
= isl_map_apply_range(map
, dst_sched
);
4427 map
= isl_map_order_gt(map
, isl_dim_in
, 0, isl_dim_out
, 0);
4428 empty
= isl_map_is_empty(map
);
4437 /* Does "graph" have any satisfied condition edges that
4438 * are adjacent to the conditional validity constraint with
4439 * domain "conditional_source" and range "conditional_sink"?
4441 * A satisfied condition is one that is not local.
4442 * If a condition was forced to be local already (i.e., marked as local)
4443 * then there is no need to check if it is in fact local.
4445 * Additionally, mark all adjacent condition edges found as local.
4447 static int has_adjacent_true_conditions(struct isl_sched_graph
*graph
,
4448 __isl_keep isl_union_set
*conditional_source
,
4449 __isl_keep isl_union_set
*conditional_sink
)
4454 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4455 int adjacent
, local
;
4456 isl_union_map
*condition
;
4458 if (!is_condition(&graph
->edge
[i
]))
4460 if (is_local(&graph
->edge
[i
]))
4463 condition
= graph
->edge
[i
].tagged_condition
;
4464 adjacent
= domain_intersects(condition
, conditional_sink
);
4465 if (adjacent
>= 0 && !adjacent
)
4466 adjacent
= range_intersects(condition
,
4467 conditional_source
);
4473 set_local(&graph
->edge
[i
]);
4475 local
= is_condition_false(&graph
->edge
[i
]);
4485 /* Are there any violated conditional validity dependences with
4486 * adjacent condition dependences that are not local with respect
4487 * to the current schedule?
4488 * That is, is the conditional validity constraint violated?
4490 * Additionally, mark all those adjacent condition dependences as local.
4491 * We also mark those adjacent condition dependences that were not marked
4492 * as local before, but just happened to be local already. This ensures
4493 * that they remain local if the schedule is recomputed.
4495 * We first collect domain and range of all violated conditional validity
4496 * dependences and then check if there are any adjacent non-local
4497 * condition dependences.
4499 static int has_violated_conditional_constraint(isl_ctx
*ctx
,
4500 struct isl_sched_graph
*graph
)
4504 isl_union_set
*source
, *sink
;
4506 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
4507 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
4508 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4509 isl_union_set
*uset
;
4510 isl_union_map
*umap
;
4513 if (!is_conditional_validity(&graph
->edge
[i
]))
4516 violated
= is_violated(graph
, i
);
4524 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
4525 uset
= isl_union_map_domain(umap
);
4526 source
= isl_union_set_union(source
, uset
);
4527 source
= isl_union_set_coalesce(source
);
4529 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
4530 uset
= isl_union_map_range(umap
);
4531 sink
= isl_union_set_union(sink
, uset
);
4532 sink
= isl_union_set_coalesce(sink
);
4536 any
= has_adjacent_true_conditions(graph
, source
, sink
);
4538 isl_union_set_free(source
);
4539 isl_union_set_free(sink
);
4542 isl_union_set_free(source
);
4543 isl_union_set_free(sink
);
4547 /* Examine the current band (the rows between graph->band_start and
4548 * graph->n_total_row), deciding whether to drop it or add it to "node"
4549 * and then continue with the computation of the next band, if any.
4550 * If "initialized" is set, then it may be assumed that compute_maxvar
4551 * has been called on the current band. Otherwise, call
4552 * compute_maxvar if and before carry_dependences gets called.
4554 * The caller keeps looking for a new row as long as
4555 * graph->n_row < graph->maxvar. If the latest attempt to find
4556 * such a row failed (i.e., we still have graph->n_row < graph->maxvar),
4558 * - split between SCCs and start over (assuming we found an interesting
4559 * pair of SCCs between which to split)
4560 * - continue with the next band (assuming the current band has at least
4562 * - try to carry as many dependences as possible and continue with the next
4564 * In each case, we first insert a band node in the schedule tree
4565 * if any rows have been computed.
4567 * If the caller managed to complete the schedule, we insert a band node
4568 * (if any schedule rows were computed) and we finish off by topologically
4569 * sorting the statements based on the remaining dependences.
4571 static __isl_give isl_schedule_node
*compute_schedule_finish_band(
4572 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
4580 if (graph
->n_row
< graph
->maxvar
) {
4582 int empty
= graph
->n_total_row
== graph
->band_start
;
4584 ctx
= isl_schedule_node_get_ctx(node
);
4585 if (!ctx
->opt
->schedule_maximize_band_depth
&& !empty
)
4586 return compute_next_band(node
, graph
, 1);
4587 if (graph
->src_scc
>= 0)
4588 return compute_split_schedule(node
, graph
);
4590 return compute_next_band(node
, graph
, 1);
4591 if (!initialized
&& compute_maxvar(graph
) < 0)
4592 return isl_schedule_node_free(node
);
4593 return carry_dependences(node
, graph
);
4596 insert
= graph
->n_total_row
> graph
->band_start
;
4598 node
= insert_current_band(node
, graph
, 1);
4599 node
= isl_schedule_node_child(node
, 0);
4601 node
= sort_statements(node
, graph
, initialized
);
4603 node
= isl_schedule_node_parent(node
);
4608 /* Construct a band of schedule rows for a connected dependence graph.
4609 * The caller is responsible for determining the strongly connected
4610 * components and calling compute_maxvar first.
4612 * We try to find a sequence of as many schedule rows as possible that result
4613 * in non-negative dependence distances (independent of the previous rows
4614 * in the sequence, i.e., such that the sequence is tilable), with as
4615 * many of the initial rows as possible satisfying the coincidence constraints.
4616 * The computation stops if we can't find any more rows or if we have found
4617 * all the rows we wanted to find.
4619 * If ctx->opt->schedule_outer_coincidence is set, then we force the
4620 * outermost dimension to satisfy the coincidence constraints. If this
4621 * turns out to be impossible, we fall back on the general scheme above
4622 * and try to carry as many dependences as possible.
4624 * If "graph" contains both condition and conditional validity dependences,
4625 * then we need to check that that the conditional schedule constraint
4626 * is satisfied, i.e., there are no violated conditional validity dependences
4627 * that are adjacent to any non-local condition dependences.
4628 * If there are, then we mark all those adjacent condition dependences
4629 * as local and recompute the current band. Those dependences that
4630 * are marked local will then be forced to be local.
4631 * The initial computation is performed with no dependences marked as local.
4632 * If we are lucky, then there will be no violated conditional validity
4633 * dependences adjacent to any non-local condition dependences.
4634 * Otherwise, we mark some additional condition dependences as local and
4635 * recompute. We continue this process until there are no violations left or
4636 * until we are no longer able to compute a schedule.
4637 * Since there are only a finite number of dependences,
4638 * there will only be a finite number of iterations.
4640 static isl_stat
compute_schedule_wcc_band(isl_ctx
*ctx
,
4641 struct isl_sched_graph
*graph
)
4643 int has_coincidence
;
4644 int use_coincidence
;
4645 int force_coincidence
= 0;
4646 int check_conditional
;
4648 if (sort_sccs(graph
) < 0)
4649 return isl_stat_error
;
4651 clear_local_edges(graph
);
4652 check_conditional
= need_condition_check(graph
);
4653 has_coincidence
= has_any_coincidence(graph
);
4655 if (ctx
->opt
->schedule_outer_coincidence
)
4656 force_coincidence
= 1;
4658 use_coincidence
= has_coincidence
;
4659 while (graph
->n_row
< graph
->maxvar
) {
4664 graph
->src_scc
= -1;
4665 graph
->dst_scc
= -1;
4667 if (setup_lp(ctx
, graph
, use_coincidence
) < 0)
4668 return isl_stat_error
;
4669 sol
= solve_lp(graph
);
4671 return isl_stat_error
;
4672 if (sol
->size
== 0) {
4673 int empty
= graph
->n_total_row
== graph
->band_start
;
4676 if (use_coincidence
&& (!force_coincidence
|| !empty
)) {
4677 use_coincidence
= 0;
4682 coincident
= !has_coincidence
|| use_coincidence
;
4683 if (update_schedule(graph
, sol
, 1, coincident
) < 0)
4684 return isl_stat_error
;
4686 if (!check_conditional
)
4688 violated
= has_violated_conditional_constraint(ctx
, graph
);
4690 return isl_stat_error
;
4693 if (reset_band(graph
) < 0)
4694 return isl_stat_error
;
4695 use_coincidence
= has_coincidence
;
4701 /* Compute a schedule for a connected dependence graph by considering
4702 * the graph as a whole and return the updated schedule node.
4704 * The actual schedule rows of the current band are computed by
4705 * compute_schedule_wcc_band. compute_schedule_finish_band takes
4706 * care of integrating the band into "node" and continuing
4709 static __isl_give isl_schedule_node
*compute_schedule_wcc_whole(
4710 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4717 ctx
= isl_schedule_node_get_ctx(node
);
4718 if (compute_schedule_wcc_band(ctx
, graph
) < 0)
4719 return isl_schedule_node_free(node
);
4721 return compute_schedule_finish_band(node
, graph
, 1);
4724 /* Clustering information used by compute_schedule_wcc_clustering.
4726 * "n" is the number of SCCs in the original dependence graph
4727 * "scc" is an array of "n" elements, each representing an SCC
4728 * of the original dependence graph. All entries in the same cluster
4729 * have the same number of schedule rows.
4730 * "scc_cluster" maps each SCC index to the cluster to which it belongs,
4731 * where each cluster is represented by the index of the first SCC
4732 * in the cluster. Initially, each SCC belongs to a cluster containing
4735 * "scc_in_merge" is used by merge_clusters_along_edge to keep
4736 * track of which SCCs need to be merged.
4738 * "cluster" contains the merged clusters of SCCs after the clustering
4741 * "scc_node" is a temporary data structure used inside copy_partial.
4742 * For each SCC, it keeps track of the number of nodes in the SCC
4743 * that have already been copied.
4745 struct isl_clustering
{
4747 struct isl_sched_graph
*scc
;
4748 struct isl_sched_graph
*cluster
;
4754 /* Initialize the clustering data structure "c" from "graph".
4756 * In particular, allocate memory, extract the SCCs from "graph"
4757 * into c->scc, initialize scc_cluster and construct
4758 * a band of schedule rows for each SCC.
4759 * Within each SCC, there is only one SCC by definition.
4760 * Each SCC initially belongs to a cluster containing only that SCC.
4762 static isl_stat
clustering_init(isl_ctx
*ctx
, struct isl_clustering
*c
,
4763 struct isl_sched_graph
*graph
)
4768 c
->scc
= isl_calloc_array(ctx
, struct isl_sched_graph
, c
->n
);
4769 c
->cluster
= isl_calloc_array(ctx
, struct isl_sched_graph
, c
->n
);
4770 c
->scc_cluster
= isl_calloc_array(ctx
, int, c
->n
);
4771 c
->scc_node
= isl_calloc_array(ctx
, int, c
->n
);
4772 c
->scc_in_merge
= isl_calloc_array(ctx
, int, c
->n
);
4773 if (!c
->scc
|| !c
->cluster
||
4774 !c
->scc_cluster
|| !c
->scc_node
|| !c
->scc_in_merge
)
4775 return isl_stat_error
;
4777 for (i
= 0; i
< c
->n
; ++i
) {
4778 if (extract_sub_graph(ctx
, graph
, &node_scc_exactly
,
4779 &edge_scc_exactly
, i
, &c
->scc
[i
]) < 0)
4780 return isl_stat_error
;
4782 if (compute_maxvar(&c
->scc
[i
]) < 0)
4783 return isl_stat_error
;
4784 if (compute_schedule_wcc_band(ctx
, &c
->scc
[i
]) < 0)
4785 return isl_stat_error
;
4786 c
->scc_cluster
[i
] = i
;
4792 /* Free all memory allocated for "c".
4794 static void clustering_free(isl_ctx
*ctx
, struct isl_clustering
*c
)
4799 for (i
= 0; i
< c
->n
; ++i
)
4800 graph_free(ctx
, &c
->scc
[i
]);
4803 for (i
= 0; i
< c
->n
; ++i
)
4804 graph_free(ctx
, &c
->cluster
[i
]);
4806 free(c
->scc_cluster
);
4808 free(c
->scc_in_merge
);
4811 /* Should we refrain from merging the cluster in "graph" with
4812 * any other cluster?
4813 * In particular, is its current schedule band empty and incomplete.
4815 static int bad_cluster(struct isl_sched_graph
*graph
)
4817 return graph
->n_row
< graph
->maxvar
&&
4818 graph
->n_total_row
== graph
->band_start
;
4821 /* Return the index of an edge in "graph" that can be used to merge
4822 * two clusters in "c".
4823 * Return graph->n_edge if no such edge can be found.
4824 * Return -1 on error.
4826 * In particular, return a proximity edge between two clusters
4827 * that is not marked "no_merge" and such that neither of the
4828 * two clusters has an incomplete, empty band.
4830 * If there are multiple such edges, then try and find the most
4831 * appropriate edge to use for merging. In particular, pick the edge
4832 * with the greatest weight. If there are multiple of those,
4833 * then pick one with the shortest distance between
4834 * the two cluster representatives.
4836 static int find_proximity(struct isl_sched_graph
*graph
,
4837 struct isl_clustering
*c
)
4839 int i
, best
= graph
->n_edge
, best_dist
, best_weight
;
4841 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4842 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
4845 if (!is_proximity(edge
))
4849 if (bad_cluster(&c
->scc
[edge
->src
->scc
]) ||
4850 bad_cluster(&c
->scc
[edge
->dst
->scc
]))
4852 dist
= c
->scc_cluster
[edge
->dst
->scc
] -
4853 c
->scc_cluster
[edge
->src
->scc
];
4856 weight
= edge
->weight
;
4857 if (best
< graph
->n_edge
) {
4858 if (best_weight
> weight
)
4860 if (best_weight
== weight
&& best_dist
<= dist
)
4865 best_weight
= weight
;
4871 /* Internal data structure used in mark_merge_sccs.
4873 * "graph" is the dependence graph in which a strongly connected
4874 * component is constructed.
4875 * "scc_cluster" maps each SCC index to the cluster to which it belongs.
4876 * "src" and "dst" are the indices of the nodes that are being merged.
4878 struct isl_mark_merge_sccs_data
{
4879 struct isl_sched_graph
*graph
;
4885 /* Check whether the cluster containing node "i" depends on the cluster
4886 * containing node "j". If "i" and "j" belong to the same cluster,
4887 * then they are taken to depend on each other to ensure that
4888 * the resulting strongly connected component consists of complete
4889 * clusters. Furthermore, if "i" and "j" are the two nodes that
4890 * are being merged, then they are taken to depend on each other as well.
4891 * Otherwise, check if there is a (conditional) validity dependence
4892 * from node[j] to node[i], forcing node[i] to follow node[j].
4894 static isl_bool
cluster_follows(int i
, int j
, void *user
)
4896 struct isl_mark_merge_sccs_data
*data
= user
;
4897 struct isl_sched_graph
*graph
= data
->graph
;
4898 int *scc_cluster
= data
->scc_cluster
;
4900 if (data
->src
== i
&& data
->dst
== j
)
4901 return isl_bool_true
;
4902 if (data
->src
== j
&& data
->dst
== i
)
4903 return isl_bool_true
;
4904 if (scc_cluster
[graph
->node
[i
].scc
] == scc_cluster
[graph
->node
[j
].scc
])
4905 return isl_bool_true
;
4907 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
4910 /* Mark all SCCs that belong to either of the two clusters in "c"
4911 * connected by the edge in "graph" with index "edge", or to any
4912 * of the intermediate clusters.
4913 * The marking is recorded in c->scc_in_merge.
4915 * The given edge has been selected for merging two clusters,
4916 * meaning that there is at least a proximity edge between the two nodes.
4917 * However, there may also be (indirect) validity dependences
4918 * between the two nodes. When merging the two clusters, all clusters
4919 * containing one or more of the intermediate nodes along the
4920 * indirect validity dependences need to be merged in as well.
4922 * First collect all such nodes by computing the strongly connected
4923 * component (SCC) containing the two nodes connected by the edge, where
4924 * the two nodes are considered to depend on each other to make
4925 * sure they end up in the same SCC. Similarly, each node is considered
4926 * to depend on every other node in the same cluster to ensure
4927 * that the SCC consists of complete clusters.
4929 * Then the original SCCs that contain any of these nodes are marked
4930 * in c->scc_in_merge.
4932 static isl_stat
mark_merge_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
4933 int edge
, struct isl_clustering
*c
)
4935 struct isl_mark_merge_sccs_data data
;
4936 struct isl_tarjan_graph
*g
;
4939 for (i
= 0; i
< c
->n
; ++i
)
4940 c
->scc_in_merge
[i
] = 0;
4943 data
.scc_cluster
= c
->scc_cluster
;
4944 data
.src
= graph
->edge
[edge
].src
- graph
->node
;
4945 data
.dst
= graph
->edge
[edge
].dst
- graph
->node
;
4947 g
= isl_tarjan_graph_component(ctx
, graph
->n
, data
.dst
,
4948 &cluster_follows
, &data
);
4954 isl_die(ctx
, isl_error_internal
,
4955 "expecting at least two nodes in component",
4957 if (g
->order
[--i
] != -1)
4958 isl_die(ctx
, isl_error_internal
,
4959 "expecting end of component marker", goto error
);
4961 for (--i
; i
>= 0 && g
->order
[i
] != -1; --i
) {
4962 int scc
= graph
->node
[g
->order
[i
]].scc
;
4963 c
->scc_in_merge
[scc
] = 1;
4966 isl_tarjan_graph_free(g
);
4969 isl_tarjan_graph_free(g
);
4970 return isl_stat_error
;
4973 /* Construct the identifier "cluster_i".
4975 static __isl_give isl_id
*cluster_id(isl_ctx
*ctx
, int i
)
4979 snprintf(name
, sizeof(name
), "cluster_%d", i
);
4980 return isl_id_alloc(ctx
, name
, NULL
);
4983 /* Construct the space of the cluster with index "i" containing
4984 * the strongly connected component "scc".
4986 * In particular, construct a space called cluster_i with dimension equal
4987 * to the number of schedule rows in the current band of "scc".
4989 static __isl_give isl_space
*cluster_space(struct isl_sched_graph
*scc
, int i
)
4995 nvar
= scc
->n_total_row
- scc
->band_start
;
4996 space
= isl_space_copy(scc
->node
[0].space
);
4997 space
= isl_space_params(space
);
4998 space
= isl_space_set_from_params(space
);
4999 space
= isl_space_add_dims(space
, isl_dim_set
, nvar
);
5000 id
= cluster_id(isl_space_get_ctx(space
), i
);
5001 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
5006 /* Collect the domain of the graph for merging clusters.
5008 * In particular, for each cluster with first SCC "i", construct
5009 * a set in the space called cluster_i with dimension equal
5010 * to the number of schedule rows in the current band of the cluster.
5012 static __isl_give isl_union_set
*collect_domain(isl_ctx
*ctx
,
5013 struct isl_sched_graph
*graph
, struct isl_clustering
*c
)
5017 isl_union_set
*domain
;
5019 space
= isl_space_params_alloc(ctx
, 0);
5020 domain
= isl_union_set_empty(space
);
5022 for (i
= 0; i
< graph
->scc
; ++i
) {
5025 if (!c
->scc_in_merge
[i
])
5027 if (c
->scc_cluster
[i
] != i
)
5029 space
= cluster_space(&c
->scc
[i
], i
);
5030 domain
= isl_union_set_add_set(domain
, isl_set_universe(space
));
5036 /* Construct a map from the original instances to the corresponding
5037 * cluster instance in the current bands of the clusters in "c".
5039 static __isl_give isl_union_map
*collect_cluster_map(isl_ctx
*ctx
,
5040 struct isl_sched_graph
*graph
, struct isl_clustering
*c
)
5044 isl_union_map
*cluster_map
;
5046 space
= isl_space_params_alloc(ctx
, 0);
5047 cluster_map
= isl_union_map_empty(space
);
5048 for (i
= 0; i
< graph
->scc
; ++i
) {
5052 if (!c
->scc_in_merge
[i
])
5055 id
= cluster_id(ctx
, c
->scc_cluster
[i
]);
5056 start
= c
->scc
[i
].band_start
;
5057 n
= c
->scc
[i
].n_total_row
- start
;
5058 for (j
= 0; j
< c
->scc
[i
].n
; ++j
) {
5061 struct isl_sched_node
*node
= &c
->scc
[i
].node
[j
];
5063 ma
= node_extract_partial_schedule_multi_aff(node
,
5065 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
,
5067 map
= isl_map_from_multi_aff(ma
);
5068 cluster_map
= isl_union_map_add_map(cluster_map
, map
);
5076 /* Add "umap" to the schedule constraints "sc" of all types of "edge"
5077 * that are not isl_edge_condition or isl_edge_conditional_validity.
5079 static __isl_give isl_schedule_constraints
*add_non_conditional_constraints(
5080 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*umap
,
5081 __isl_take isl_schedule_constraints
*sc
)
5083 enum isl_edge_type t
;
5088 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
5089 if (t
== isl_edge_condition
||
5090 t
== isl_edge_conditional_validity
)
5092 if (!is_type(edge
, t
))
5094 sc
= isl_schedule_constraints_add(sc
, t
,
5095 isl_union_map_copy(umap
));
5101 /* Add schedule constraints of types isl_edge_condition and
5102 * isl_edge_conditional_validity to "sc" by applying "umap" to
5103 * the domains of the wrapped relations in domain and range
5104 * of the corresponding tagged constraints of "edge".
5106 static __isl_give isl_schedule_constraints
*add_conditional_constraints(
5107 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*umap
,
5108 __isl_take isl_schedule_constraints
*sc
)
5110 enum isl_edge_type t
;
5111 isl_union_map
*tagged
;
5113 for (t
= isl_edge_condition
; t
<= isl_edge_conditional_validity
; ++t
) {
5114 if (!is_type(edge
, t
))
5116 if (t
== isl_edge_condition
)
5117 tagged
= isl_union_map_copy(edge
->tagged_condition
);
5119 tagged
= isl_union_map_copy(edge
->tagged_validity
);
5120 tagged
= isl_union_map_zip(tagged
);
5121 tagged
= isl_union_map_apply_domain(tagged
,
5122 isl_union_map_copy(umap
));
5123 tagged
= isl_union_map_zip(tagged
);
5124 sc
= isl_schedule_constraints_add(sc
, t
, tagged
);
5132 /* Given a mapping "cluster_map" from the original instances to
5133 * the cluster instances, add schedule constraints on the clusters
5134 * to "sc" corresponding to the original constraints represented by "edge".
5136 * For non-tagged dependence constraints, the cluster constraints
5137 * are obtained by applying "cluster_map" to the edge->map.
5139 * For tagged dependence constraints, "cluster_map" needs to be applied
5140 * to the domains of the wrapped relations in domain and range
5141 * of the tagged dependence constraints. Pick out the mappings
5142 * from these domains from "cluster_map" and construct their product.
5143 * This mapping can then be applied to the pair of domains.
5145 static __isl_give isl_schedule_constraints
*collect_edge_constraints(
5146 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*cluster_map
,
5147 __isl_take isl_schedule_constraints
*sc
)
5149 isl_union_map
*umap
;
5151 isl_union_set
*uset
;
5152 isl_union_map
*umap1
, *umap2
;
5157 umap
= isl_union_map_from_map(isl_map_copy(edge
->map
));
5158 umap
= isl_union_map_apply_domain(umap
,
5159 isl_union_map_copy(cluster_map
));
5160 umap
= isl_union_map_apply_range(umap
,
5161 isl_union_map_copy(cluster_map
));
5162 sc
= add_non_conditional_constraints(edge
, umap
, sc
);
5163 isl_union_map_free(umap
);
5165 if (!sc
|| (!is_condition(edge
) && !is_conditional_validity(edge
)))
5168 space
= isl_space_domain(isl_map_get_space(edge
->map
));
5169 uset
= isl_union_set_from_set(isl_set_universe(space
));
5170 umap1
= isl_union_map_copy(cluster_map
);
5171 umap1
= isl_union_map_intersect_domain(umap1
, uset
);
5172 space
= isl_space_range(isl_map_get_space(edge
->map
));
5173 uset
= isl_union_set_from_set(isl_set_universe(space
));
5174 umap2
= isl_union_map_copy(cluster_map
);
5175 umap2
= isl_union_map_intersect_domain(umap2
, uset
);
5176 umap
= isl_union_map_product(umap1
, umap2
);
5178 sc
= add_conditional_constraints(edge
, umap
, sc
);
5180 isl_union_map_free(umap
);
5184 /* Given a mapping "cluster_map" from the original instances to
5185 * the cluster instances, add schedule constraints on the clusters
5186 * to "sc" corresponding to all edges in "graph" between nodes that
5187 * belong to SCCs that are marked for merging in "scc_in_merge".
5189 static __isl_give isl_schedule_constraints
*collect_constraints(
5190 struct isl_sched_graph
*graph
, int *scc_in_merge
,
5191 __isl_keep isl_union_map
*cluster_map
,
5192 __isl_take isl_schedule_constraints
*sc
)
5196 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5197 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
5199 if (!scc_in_merge
[edge
->src
->scc
])
5201 if (!scc_in_merge
[edge
->dst
->scc
])
5203 sc
= collect_edge_constraints(edge
, cluster_map
, sc
);
5209 /* Construct a dependence graph for scheduling clusters with respect
5210 * to each other and store the result in "merge_graph".
5211 * In particular, the nodes of the graph correspond to the schedule
5212 * dimensions of the current bands of those clusters that have been
5213 * marked for merging in "c".
5215 * First construct an isl_schedule_constraints object for this domain
5216 * by transforming the edges in "graph" to the domain.
5217 * Then initialize a dependence graph for scheduling from these
5220 static isl_stat
init_merge_graph(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5221 struct isl_clustering
*c
, struct isl_sched_graph
*merge_graph
)
5223 isl_union_set
*domain
;
5224 isl_union_map
*cluster_map
;
5225 isl_schedule_constraints
*sc
;
5228 domain
= collect_domain(ctx
, graph
, c
);
5229 sc
= isl_schedule_constraints_on_domain(domain
);
5231 return isl_stat_error
;
5232 cluster_map
= collect_cluster_map(ctx
, graph
, c
);
5233 sc
= collect_constraints(graph
, c
->scc_in_merge
, cluster_map
, sc
);
5234 isl_union_map_free(cluster_map
);
5236 r
= graph_init(merge_graph
, sc
);
5238 isl_schedule_constraints_free(sc
);
5243 /* Compute the maximal number of remaining schedule rows that still need
5244 * to be computed for the nodes that belong to clusters with the maximal
5245 * dimension for the current band (i.e., the band that is to be merged).
5246 * Only clusters that are about to be merged are considered.
5247 * "maxvar" is the maximal dimension for the current band.
5248 * "c" contains information about the clusters.
5250 * Return the maximal number of remaining schedule rows or -1 on error.
5252 static int compute_maxvar_max_slack(int maxvar
, struct isl_clustering
*c
)
5258 for (i
= 0; i
< c
->n
; ++i
) {
5260 struct isl_sched_graph
*scc
;
5262 if (!c
->scc_in_merge
[i
])
5265 nvar
= scc
->n_total_row
- scc
->band_start
;
5268 for (j
= 0; j
< scc
->n
; ++j
) {
5269 struct isl_sched_node
*node
= &scc
->node
[j
];
5272 if (node_update_cmap(node
) < 0)
5274 slack
= node
->nvar
- node
->rank
;
5275 if (slack
> max_slack
)
5283 /* If there are any clusters where the dimension of the current band
5284 * (i.e., the band that is to be merged) is smaller than "maxvar" and
5285 * if there are any nodes in such a cluster where the number
5286 * of remaining schedule rows that still need to be computed
5287 * is greater than "max_slack", then return the smallest current band
5288 * dimension of all these clusters. Otherwise return the original value
5289 * of "maxvar". Return -1 in case of any error.
5290 * Only clusters that are about to be merged are considered.
5291 * "c" contains information about the clusters.
5293 static int limit_maxvar_to_slack(int maxvar
, int max_slack
,
5294 struct isl_clustering
*c
)
5298 for (i
= 0; i
< c
->n
; ++i
) {
5300 struct isl_sched_graph
*scc
;
5302 if (!c
->scc_in_merge
[i
])
5305 nvar
= scc
->n_total_row
- scc
->band_start
;
5308 for (j
= 0; j
< scc
->n
; ++j
) {
5309 struct isl_sched_node
*node
= &scc
->node
[j
];
5312 if (node_update_cmap(node
) < 0)
5314 slack
= node
->nvar
- node
->rank
;
5315 if (slack
> max_slack
) {
5325 /* Adjust merge_graph->maxvar based on the number of remaining schedule rows
5326 * that still need to be computed. In particular, if there is a node
5327 * in a cluster where the dimension of the current band is smaller
5328 * than merge_graph->maxvar, but the number of remaining schedule rows
5329 * is greater than that of any node in a cluster with the maximal
5330 * dimension for the current band (i.e., merge_graph->maxvar),
5331 * then adjust merge_graph->maxvar to the (smallest) current band dimension
5332 * of those clusters. Without this adjustment, the total number of
5333 * schedule dimensions would be increased, resulting in a skewed view
5334 * of the number of coincident dimensions.
5335 * "c" contains information about the clusters.
5337 * If the maximize_band_depth option is set and merge_graph->maxvar is reduced,
5338 * then there is no point in attempting any merge since it will be rejected
5339 * anyway. Set merge_graph->maxvar to zero in such cases.
5341 static isl_stat
adjust_maxvar_to_slack(isl_ctx
*ctx
,
5342 struct isl_sched_graph
*merge_graph
, struct isl_clustering
*c
)
5344 int max_slack
, maxvar
;
5346 max_slack
= compute_maxvar_max_slack(merge_graph
->maxvar
, c
);
5348 return isl_stat_error
;
5349 maxvar
= limit_maxvar_to_slack(merge_graph
->maxvar
, max_slack
, c
);
5351 return isl_stat_error
;
5353 if (maxvar
< merge_graph
->maxvar
) {
5354 if (isl_options_get_schedule_maximize_band_depth(ctx
))
5355 merge_graph
->maxvar
= 0;
5357 merge_graph
->maxvar
= maxvar
;
5363 /* Return the number of coincident dimensions in the current band of "graph",
5364 * where the nodes of "graph" are assumed to be scheduled by a single band.
5366 static int get_n_coincident(struct isl_sched_graph
*graph
)
5370 for (i
= graph
->band_start
; i
< graph
->n_total_row
; ++i
)
5371 if (!graph
->node
[0].coincident
[i
])
5374 return i
- graph
->band_start
;
5377 /* Should the clusters be merged based on the cluster schedule
5378 * in the current (and only) band of "merge_graph", given that
5379 * coincidence should be maximized?
5381 * If the number of coincident schedule dimensions in the merged band
5382 * would be less than the maximal number of coincident schedule dimensions
5383 * in any of the merged clusters, then the clusters should not be merged.
5385 static isl_bool
ok_to_merge_coincident(struct isl_clustering
*c
,
5386 struct isl_sched_graph
*merge_graph
)
5393 for (i
= 0; i
< c
->n
; ++i
) {
5394 if (!c
->scc_in_merge
[i
])
5396 n_coincident
= get_n_coincident(&c
->scc
[i
]);
5397 if (n_coincident
> max_coincident
)
5398 max_coincident
= n_coincident
;
5401 n_coincident
= get_n_coincident(merge_graph
);
5403 return n_coincident
>= max_coincident
;
5406 /* Return the transformation on "node" expressed by the current (and only)
5407 * band of "merge_graph" applied to the clusters in "c".
5409 * First find the representation of "node" in its SCC in "c" and
5410 * extract the transformation expressed by the current band.
5411 * Then extract the transformation applied by "merge_graph"
5412 * to the cluster to which this SCC belongs.
5413 * Combine the two to obtain the complete transformation on the node.
5415 * Note that the range of the first transformation is an anonymous space,
5416 * while the domain of the second is named "cluster_X". The range
5417 * of the former therefore needs to be adjusted before the two
5420 static __isl_give isl_map
*extract_node_transformation(isl_ctx
*ctx
,
5421 struct isl_sched_node
*node
, struct isl_clustering
*c
,
5422 struct isl_sched_graph
*merge_graph
)
5424 struct isl_sched_node
*scc_node
, *cluster_node
;
5428 isl_multi_aff
*ma
, *ma2
;
5430 scc_node
= graph_find_node(ctx
, &c
->scc
[node
->scc
], node
->space
);
5431 start
= c
->scc
[node
->scc
].band_start
;
5432 n
= c
->scc
[node
->scc
].n_total_row
- start
;
5433 ma
= node_extract_partial_schedule_multi_aff(scc_node
, start
, n
);
5434 space
= cluster_space(&c
->scc
[node
->scc
], c
->scc_cluster
[node
->scc
]);
5435 cluster_node
= graph_find_node(ctx
, merge_graph
, space
);
5436 if (space
&& !cluster_node
)
5437 isl_die(ctx
, isl_error_internal
, "unable to find cluster",
5438 space
= isl_space_free(space
));
5439 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
5440 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
, id
);
5441 isl_space_free(space
);
5442 n
= merge_graph
->n_total_row
;
5443 ma2
= node_extract_partial_schedule_multi_aff(cluster_node
, 0, n
);
5444 ma
= isl_multi_aff_pullback_multi_aff(ma2
, ma
);
5446 return isl_map_from_multi_aff(ma
);
5449 /* Give a set of distances "set", are they bounded by a small constant
5450 * in direction "pos"?
5451 * In practice, check if they are bounded by 2 by checking that there
5452 * are no elements with a value greater than or equal to 3 or
5453 * smaller than or equal to -3.
5455 static isl_bool
distance_is_bounded(__isl_keep isl_set
*set
, int pos
)
5461 return isl_bool_error
;
5463 test
= isl_set_copy(set
);
5464 test
= isl_set_lower_bound_si(test
, isl_dim_set
, pos
, 3);
5465 bounded
= isl_set_is_empty(test
);
5468 if (bounded
< 0 || !bounded
)
5471 test
= isl_set_copy(set
);
5472 test
= isl_set_upper_bound_si(test
, isl_dim_set
, pos
, -3);
5473 bounded
= isl_set_is_empty(test
);
5479 /* Does the set "set" have a fixed (but possible parametric) value
5480 * at dimension "pos"?
5482 static isl_bool
has_single_value(__isl_keep isl_set
*set
, int pos
)
5488 return isl_bool_error
;
5489 set
= isl_set_copy(set
);
5490 n
= isl_set_dim(set
, isl_dim_set
);
5491 set
= isl_set_project_out(set
, isl_dim_set
, pos
+ 1, n
- (pos
+ 1));
5492 set
= isl_set_project_out(set
, isl_dim_set
, 0, pos
);
5493 single
= isl_set_is_singleton(set
);
5499 /* Does "map" have a fixed (but possible parametric) value
5500 * at dimension "pos" of either its domain or its range?
5502 static isl_bool
has_singular_src_or_dst(__isl_keep isl_map
*map
, int pos
)
5507 set
= isl_map_domain(isl_map_copy(map
));
5508 single
= has_single_value(set
, pos
);
5511 if (single
< 0 || single
)
5514 set
= isl_map_range(isl_map_copy(map
));
5515 single
= has_single_value(set
, pos
);
5521 /* Does the edge "edge" from "graph" have bounded dependence distances
5522 * in the merged graph "merge_graph" of a selection of clusters in "c"?
5524 * Extract the complete transformations of the source and destination
5525 * nodes of the edge, apply them to the edge constraints and
5526 * compute the differences. Finally, check if these differences are bounded
5527 * in each direction.
5529 * If the dimension of the band is greater than the number of
5530 * dimensions that can be expected to be optimized by the edge
5531 * (based on its weight), then also allow the differences to be unbounded
5532 * in the remaining dimensions, but only if either the source or
5533 * the destination has a fixed value in that direction.
5534 * This allows a statement that produces values that are used by
5535 * several instances of another statement to be merged with that
5537 * However, merging such clusters will introduce an inherently
5538 * large proximity distance inside the merged cluster, meaning
5539 * that proximity distances will no longer be optimized in
5540 * subsequent merges. These merges are therefore only allowed
5541 * after all other possible merges have been tried.
5542 * The first time such a merge is encountered, the weight of the edge
5543 * is replaced by a negative weight. The second time (i.e., after
5544 * all merges over edges with a non-negative weight have been tried),
5545 * the merge is allowed.
5547 static isl_bool
has_bounded_distances(isl_ctx
*ctx
, struct isl_sched_edge
*edge
,
5548 struct isl_sched_graph
*graph
, struct isl_clustering
*c
,
5549 struct isl_sched_graph
*merge_graph
)
5556 map
= isl_map_copy(edge
->map
);
5557 t
= extract_node_transformation(ctx
, edge
->src
, c
, merge_graph
);
5558 map
= isl_map_apply_domain(map
, t
);
5559 t
= extract_node_transformation(ctx
, edge
->dst
, c
, merge_graph
);
5560 map
= isl_map_apply_range(map
, t
);
5561 dist
= isl_map_deltas(isl_map_copy(map
));
5563 bounded
= isl_bool_true
;
5564 n
= isl_set_dim(dist
, isl_dim_set
);
5565 n_slack
= n
- edge
->weight
;
5566 if (edge
->weight
< 0)
5567 n_slack
-= graph
->max_weight
+ 1;
5568 for (i
= 0; i
< n
; ++i
) {
5569 isl_bool bounded_i
, singular_i
;
5571 bounded_i
= distance_is_bounded(dist
, i
);
5576 if (edge
->weight
>= 0)
5577 bounded
= isl_bool_false
;
5581 singular_i
= has_singular_src_or_dst(map
, i
);
5586 bounded
= isl_bool_false
;
5589 if (!bounded
&& i
>= n
&& edge
->weight
>= 0)
5590 edge
->weight
-= graph
->max_weight
+ 1;
5598 return isl_bool_error
;
5601 /* Should the clusters be merged based on the cluster schedule
5602 * in the current (and only) band of "merge_graph"?
5603 * "graph" is the original dependence graph, while "c" records
5604 * which SCCs are involved in the latest merge.
5606 * In particular, is there at least one proximity constraint
5607 * that is optimized by the merge?
5609 * A proximity constraint is considered to be optimized
5610 * if the dependence distances are small.
5612 static isl_bool
ok_to_merge_proximity(isl_ctx
*ctx
,
5613 struct isl_sched_graph
*graph
, struct isl_clustering
*c
,
5614 struct isl_sched_graph
*merge_graph
)
5618 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5619 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
5622 if (!is_proximity(edge
))
5624 if (!c
->scc_in_merge
[edge
->src
->scc
])
5626 if (!c
->scc_in_merge
[edge
->dst
->scc
])
5628 if (c
->scc_cluster
[edge
->dst
->scc
] ==
5629 c
->scc_cluster
[edge
->src
->scc
])
5631 bounded
= has_bounded_distances(ctx
, edge
, graph
, c
,
5633 if (bounded
< 0 || bounded
)
5637 return isl_bool_false
;
5640 /* Should the clusters be merged based on the cluster schedule
5641 * in the current (and only) band of "merge_graph"?
5642 * "graph" is the original dependence graph, while "c" records
5643 * which SCCs are involved in the latest merge.
5645 * If the current band is empty, then the clusters should not be merged.
5647 * If the band depth should be maximized and the merge schedule
5648 * is incomplete (meaning that the dimension of some of the schedule
5649 * bands in the original schedule will be reduced), then the clusters
5650 * should not be merged.
5652 * If the schedule_maximize_coincidence option is set, then check that
5653 * the number of coincident schedule dimensions is not reduced.
5655 * Finally, only allow the merge if at least one proximity
5656 * constraint is optimized.
5658 static isl_bool
ok_to_merge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5659 struct isl_clustering
*c
, struct isl_sched_graph
*merge_graph
)
5661 if (merge_graph
->n_total_row
== merge_graph
->band_start
)
5662 return isl_bool_false
;
5664 if (isl_options_get_schedule_maximize_band_depth(ctx
) &&
5665 merge_graph
->n_total_row
< merge_graph
->maxvar
)
5666 return isl_bool_false
;
5668 if (isl_options_get_schedule_maximize_coincidence(ctx
)) {
5671 ok
= ok_to_merge_coincident(c
, merge_graph
);
5676 return ok_to_merge_proximity(ctx
, graph
, c
, merge_graph
);
5679 /* Apply the schedule in "t_node" to the "n" rows starting at "first"
5680 * of the schedule in "node" and return the result.
5682 * That is, essentially compute
5684 * T * N(first:first+n-1)
5686 * taking into account the constant term and the parameter coefficients
5689 static __isl_give isl_mat
*node_transformation(isl_ctx
*ctx
,
5690 struct isl_sched_node
*t_node
, struct isl_sched_node
*node
,
5695 int n_row
, n_col
, n_param
, n_var
;
5697 n_param
= node
->nparam
;
5699 n_row
= isl_mat_rows(t_node
->sched
);
5700 n_col
= isl_mat_cols(node
->sched
);
5701 t
= isl_mat_alloc(ctx
, n_row
, n_col
);
5704 for (i
= 0; i
< n_row
; ++i
) {
5705 isl_seq_cpy(t
->row
[i
], t_node
->sched
->row
[i
], 1 + n_param
);
5706 isl_seq_clr(t
->row
[i
] + 1 + n_param
, n_var
);
5707 for (j
= 0; j
< n
; ++j
)
5708 isl_seq_addmul(t
->row
[i
],
5709 t_node
->sched
->row
[i
][1 + n_param
+ j
],
5710 node
->sched
->row
[first
+ j
],
5711 1 + n_param
+ n_var
);
5716 /* Apply the cluster schedule in "t_node" to the current band
5717 * schedule of the nodes in "graph".
5719 * In particular, replace the rows starting at band_start
5720 * by the result of applying the cluster schedule in "t_node"
5721 * to the original rows.
5723 * The coincidence of the schedule is determined by the coincidence
5724 * of the cluster schedule.
5726 static isl_stat
transform(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5727 struct isl_sched_node
*t_node
)
5733 start
= graph
->band_start
;
5734 n
= graph
->n_total_row
- start
;
5736 n_new
= isl_mat_rows(t_node
->sched
);
5737 for (i
= 0; i
< graph
->n
; ++i
) {
5738 struct isl_sched_node
*node
= &graph
->node
[i
];
5741 t
= node_transformation(ctx
, t_node
, node
, start
, n
);
5742 node
->sched
= isl_mat_drop_rows(node
->sched
, start
, n
);
5743 node
->sched
= isl_mat_concat(node
->sched
, t
);
5744 node
->sched_map
= isl_map_free(node
->sched_map
);
5746 return isl_stat_error
;
5747 for (j
= 0; j
< n_new
; ++j
)
5748 node
->coincident
[start
+ j
] = t_node
->coincident
[j
];
5750 graph
->n_total_row
-= n
;
5752 graph
->n_total_row
+= n_new
;
5753 graph
->n_row
+= n_new
;
5758 /* Merge the clusters marked for merging in "c" into a single
5759 * cluster using the cluster schedule in the current band of "merge_graph".
5760 * The representative SCC for the new cluster is the SCC with
5761 * the smallest index.
5763 * The current band schedule of each SCC in the new cluster is obtained
5764 * by applying the schedule of the corresponding original cluster
5765 * to the original band schedule.
5766 * All SCCs in the new cluster have the same number of schedule rows.
5768 static isl_stat
merge(isl_ctx
*ctx
, struct isl_clustering
*c
,
5769 struct isl_sched_graph
*merge_graph
)
5775 for (i
= 0; i
< c
->n
; ++i
) {
5776 struct isl_sched_node
*node
;
5778 if (!c
->scc_in_merge
[i
])
5782 space
= cluster_space(&c
->scc
[i
], c
->scc_cluster
[i
]);
5784 return isl_stat_error
;
5785 node
= graph_find_node(ctx
, merge_graph
, space
);
5786 isl_space_free(space
);
5788 isl_die(ctx
, isl_error_internal
,
5789 "unable to find cluster",
5790 return isl_stat_error
);
5791 if (transform(ctx
, &c
->scc
[i
], node
) < 0)
5792 return isl_stat_error
;
5793 c
->scc_cluster
[i
] = cluster
;
5799 /* Try and merge the clusters of SCCs marked in c->scc_in_merge
5800 * by scheduling the current cluster bands with respect to each other.
5802 * Construct a dependence graph with a space for each cluster and
5803 * with the coordinates of each space corresponding to the schedule
5804 * dimensions of the current band of that cluster.
5805 * Construct a cluster schedule in this cluster dependence graph and
5806 * apply it to the current cluster bands if it is applicable
5807 * according to ok_to_merge.
5809 * If the number of remaining schedule dimensions in a cluster
5810 * with a non-maximal current schedule dimension is greater than
5811 * the number of remaining schedule dimensions in clusters
5812 * with a maximal current schedule dimension, then restrict
5813 * the number of rows to be computed in the cluster schedule
5814 * to the minimal such non-maximal current schedule dimension.
5815 * Do this by adjusting merge_graph.maxvar.
5817 * Return isl_bool_true if the clusters have effectively been merged
5818 * into a single cluster.
5820 * Note that since the standard scheduling algorithm minimizes the maximal
5821 * distance over proximity constraints, the proximity constraints between
5822 * the merged clusters may not be optimized any further than what is
5823 * sufficient to bring the distances within the limits of the internal
5824 * proximity constraints inside the individual clusters.
5825 * It may therefore make sense to perform an additional translation step
5826 * to bring the clusters closer to each other, while maintaining
5827 * the linear part of the merging schedule found using the standard
5828 * scheduling algorithm.
5830 static isl_bool
try_merge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5831 struct isl_clustering
*c
)
5833 struct isl_sched_graph merge_graph
= { 0 };
5836 if (init_merge_graph(ctx
, graph
, c
, &merge_graph
) < 0)
5839 if (compute_maxvar(&merge_graph
) < 0)
5841 if (adjust_maxvar_to_slack(ctx
, &merge_graph
,c
) < 0)
5843 if (compute_schedule_wcc_band(ctx
, &merge_graph
) < 0)
5845 merged
= ok_to_merge(ctx
, graph
, c
, &merge_graph
);
5846 if (merged
&& merge(ctx
, c
, &merge_graph
) < 0)
5849 graph_free(ctx
, &merge_graph
);
5852 graph_free(ctx
, &merge_graph
);
5853 return isl_bool_error
;
5856 /* Is there any edge marked "no_merge" between two SCCs that are
5857 * about to be merged (i.e., that are set in "scc_in_merge")?
5858 * "merge_edge" is the proximity edge along which the clusters of SCCs
5859 * are going to be merged.
5861 * If there is any edge between two SCCs with a negative weight,
5862 * while the weight of "merge_edge" is non-negative, then this
5863 * means that the edge was postponed. "merge_edge" should then
5864 * also be postponed since merging along the edge with negative weight should
5865 * be postponed until all edges with non-negative weight have been tried.
5866 * Replace the weight of "merge_edge" by a negative weight as well and
5867 * tell the caller not to attempt a merge.
5869 static int any_no_merge(struct isl_sched_graph
*graph
, int *scc_in_merge
,
5870 struct isl_sched_edge
*merge_edge
)
5874 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5875 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
5877 if (!scc_in_merge
[edge
->src
->scc
])
5879 if (!scc_in_merge
[edge
->dst
->scc
])
5883 if (merge_edge
->weight
>= 0 && edge
->weight
< 0) {
5884 merge_edge
->weight
-= graph
->max_weight
+ 1;
5892 /* Merge the two clusters in "c" connected by the edge in "graph"
5893 * with index "edge" into a single cluster.
5894 * If it turns out to be impossible to merge these two clusters,
5895 * then mark the edge as "no_merge" such that it will not be
5898 * First mark all SCCs that need to be merged. This includes the SCCs
5899 * in the two clusters, but it may also include the SCCs
5900 * of intermediate clusters.
5901 * If there is already a no_merge edge between any pair of such SCCs,
5902 * then simply mark the current edge as no_merge as well.
5903 * Likewise, if any of those edges was postponed by has_bounded_distances,
5904 * then postpone the current edge as well.
5905 * Otherwise, try and merge the clusters and mark "edge" as "no_merge"
5906 * if the clusters did not end up getting merged, unless the non-merge
5907 * is due to the fact that the edge was postponed. This postponement
5908 * can be recognized by a change in weight (from non-negative to negative).
5910 static isl_stat
merge_clusters_along_edge(isl_ctx
*ctx
,
5911 struct isl_sched_graph
*graph
, int edge
, struct isl_clustering
*c
)
5914 int edge_weight
= graph
->edge
[edge
].weight
;
5916 if (mark_merge_sccs(ctx
, graph
, edge
, c
) < 0)
5917 return isl_stat_error
;
5919 if (any_no_merge(graph
, c
->scc_in_merge
, &graph
->edge
[edge
]))
5920 merged
= isl_bool_false
;
5922 merged
= try_merge(ctx
, graph
, c
);
5924 return isl_stat_error
;
5925 if (!merged
&& edge_weight
== graph
->edge
[edge
].weight
)
5926 graph
->edge
[edge
].no_merge
= 1;
5931 /* Does "node" belong to the cluster identified by "cluster"?
5933 static int node_cluster_exactly(struct isl_sched_node
*node
, int cluster
)
5935 return node
->cluster
== cluster
;
5938 /* Does "edge" connect two nodes belonging to the cluster
5939 * identified by "cluster"?
5941 static int edge_cluster_exactly(struct isl_sched_edge
*edge
, int cluster
)
5943 return edge
->src
->cluster
== cluster
&& edge
->dst
->cluster
== cluster
;
5946 /* Swap the schedule of "node1" and "node2".
5947 * Both nodes have been derived from the same node in a common parent graph.
5948 * Since the "coincident" field is shared with that node
5949 * in the parent graph, there is no need to also swap this field.
5951 static void swap_sched(struct isl_sched_node
*node1
,
5952 struct isl_sched_node
*node2
)
5957 sched
= node1
->sched
;
5958 node1
->sched
= node2
->sched
;
5959 node2
->sched
= sched
;
5961 sched_map
= node1
->sched_map
;
5962 node1
->sched_map
= node2
->sched_map
;
5963 node2
->sched_map
= sched_map
;
5966 /* Copy the current band schedule from the SCCs that form the cluster
5967 * with index "pos" to the actual cluster at position "pos".
5968 * By construction, the index of the first SCC that belongs to the cluster
5971 * The order of the nodes inside both the SCCs and the cluster
5972 * is assumed to be same as the order in the original "graph".
5974 * Since the SCC graphs will no longer be used after this function,
5975 * the schedules are actually swapped rather than copied.
5977 static isl_stat
copy_partial(struct isl_sched_graph
*graph
,
5978 struct isl_clustering
*c
, int pos
)
5982 c
->cluster
[pos
].n_total_row
= c
->scc
[pos
].n_total_row
;
5983 c
->cluster
[pos
].n_row
= c
->scc
[pos
].n_row
;
5984 c
->cluster
[pos
].maxvar
= c
->scc
[pos
].maxvar
;
5986 for (i
= 0; i
< graph
->n
; ++i
) {
5990 if (graph
->node
[i
].cluster
!= pos
)
5992 s
= graph
->node
[i
].scc
;
5993 k
= c
->scc_node
[s
]++;
5994 swap_sched(&c
->cluster
[pos
].node
[j
], &c
->scc
[s
].node
[k
]);
5995 if (c
->scc
[s
].maxvar
> c
->cluster
[pos
].maxvar
)
5996 c
->cluster
[pos
].maxvar
= c
->scc
[s
].maxvar
;
6003 /* Is there a (conditional) validity dependence from node[j] to node[i],
6004 * forcing node[i] to follow node[j] or do the nodes belong to the same
6007 static isl_bool
node_follows_strong_or_same_cluster(int i
, int j
, void *user
)
6009 struct isl_sched_graph
*graph
= user
;
6011 if (graph
->node
[i
].cluster
== graph
->node
[j
].cluster
)
6012 return isl_bool_true
;
6013 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
6016 /* Extract the merged clusters of SCCs in "graph", sort them, and
6017 * store them in c->clusters. Update c->scc_cluster accordingly.
6019 * First keep track of the cluster containing the SCC to which a node
6020 * belongs in the node itself.
6021 * Then extract the clusters into c->clusters, copying the current
6022 * band schedule from the SCCs that belong to the cluster.
6023 * Do this only once per cluster.
6025 * Finally, topologically sort the clusters and update c->scc_cluster
6026 * to match the new scc numbering. While the SCCs were originally
6027 * sorted already, some SCCs that depend on some other SCCs may
6028 * have been merged with SCCs that appear before these other SCCs.
6029 * A reordering may therefore be required.
6031 static isl_stat
extract_clusters(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
6032 struct isl_clustering
*c
)
6036 for (i
= 0; i
< graph
->n
; ++i
)
6037 graph
->node
[i
].cluster
= c
->scc_cluster
[graph
->node
[i
].scc
];
6039 for (i
= 0; i
< graph
->scc
; ++i
) {
6040 if (c
->scc_cluster
[i
] != i
)
6042 if (extract_sub_graph(ctx
, graph
, &node_cluster_exactly
,
6043 &edge_cluster_exactly
, i
, &c
->cluster
[i
]) < 0)
6044 return isl_stat_error
;
6045 c
->cluster
[i
].src_scc
= -1;
6046 c
->cluster
[i
].dst_scc
= -1;
6047 if (copy_partial(graph
, c
, i
) < 0)
6048 return isl_stat_error
;
6051 if (detect_ccs(ctx
, graph
, &node_follows_strong_or_same_cluster
) < 0)
6052 return isl_stat_error
;
6053 for (i
= 0; i
< graph
->n
; ++i
)
6054 c
->scc_cluster
[graph
->node
[i
].scc
] = graph
->node
[i
].cluster
;
6059 /* Compute weights on the proximity edges of "graph" that can
6060 * be used by find_proximity to find the most appropriate
6061 * proximity edge to use to merge two clusters in "c".
6062 * The weights are also used by has_bounded_distances to determine
6063 * whether the merge should be allowed.
6064 * Store the maximum of the computed weights in graph->max_weight.
6066 * The computed weight is a measure for the number of remaining schedule
6067 * dimensions that can still be completely aligned.
6068 * In particular, compute the number of equalities between
6069 * input dimensions and output dimensions in the proximity constraints.
6070 * The directions that are already handled by outer schedule bands
6071 * are projected out prior to determining this number.
6073 * Edges that will never be considered by find_proximity are ignored.
6075 static isl_stat
compute_weights(struct isl_sched_graph
*graph
,
6076 struct isl_clustering
*c
)
6080 graph
->max_weight
= 0;
6082 for (i
= 0; i
< graph
->n_edge
; ++i
) {
6083 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
6084 struct isl_sched_node
*src
= edge
->src
;
6085 struct isl_sched_node
*dst
= edge
->dst
;
6086 isl_basic_map
*hull
;
6089 if (!is_proximity(edge
))
6091 if (bad_cluster(&c
->scc
[edge
->src
->scc
]) ||
6092 bad_cluster(&c
->scc
[edge
->dst
->scc
]))
6094 if (c
->scc_cluster
[edge
->dst
->scc
] ==
6095 c
->scc_cluster
[edge
->src
->scc
])
6098 hull
= isl_map_affine_hull(isl_map_copy(edge
->map
));
6099 hull
= isl_basic_map_transform_dims(hull
, isl_dim_in
, 0,
6100 isl_mat_copy(src
->ctrans
));
6101 hull
= isl_basic_map_transform_dims(hull
, isl_dim_out
, 0,
6102 isl_mat_copy(dst
->ctrans
));
6103 hull
= isl_basic_map_project_out(hull
,
6104 isl_dim_in
, 0, src
->rank
);
6105 hull
= isl_basic_map_project_out(hull
,
6106 isl_dim_out
, 0, dst
->rank
);
6107 hull
= isl_basic_map_remove_divs(hull
);
6108 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
6109 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
6110 hull
= isl_basic_map_drop_constraints_not_involving_dims(hull
,
6111 isl_dim_in
, 0, n_in
);
6112 hull
= isl_basic_map_drop_constraints_not_involving_dims(hull
,
6113 isl_dim_out
, 0, n_out
);
6115 return isl_stat_error
;
6116 edge
->weight
= hull
->n_eq
;
6117 isl_basic_map_free(hull
);
6119 if (edge
->weight
> graph
->max_weight
)
6120 graph
->max_weight
= edge
->weight
;
6126 /* Call compute_schedule_finish_band on each of the clusters in "c"
6127 * in their topological order. This order is determined by the scc
6128 * fields of the nodes in "graph".
6129 * Combine the results in a sequence expressing the topological order.
6131 * If there is only one cluster left, then there is no need to introduce
6132 * a sequence node. Also, in this case, the cluster necessarily contains
6133 * the SCC at position 0 in the original graph and is therefore also
6134 * stored in the first cluster of "c".
6136 static __isl_give isl_schedule_node
*finish_bands_clustering(
6137 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
6138 struct isl_clustering
*c
)
6142 isl_union_set_list
*filters
;
6144 if (graph
->scc
== 1)
6145 return compute_schedule_finish_band(node
, &c
->cluster
[0], 0);
6147 ctx
= isl_schedule_node_get_ctx(node
);
6149 filters
= extract_sccs(ctx
, graph
);
6150 node
= isl_schedule_node_insert_sequence(node
, filters
);
6152 for (i
= 0; i
< graph
->scc
; ++i
) {
6153 int j
= c
->scc_cluster
[i
];
6154 node
= isl_schedule_node_child(node
, i
);
6155 node
= isl_schedule_node_child(node
, 0);
6156 node
= compute_schedule_finish_band(node
, &c
->cluster
[j
], 0);
6157 node
= isl_schedule_node_parent(node
);
6158 node
= isl_schedule_node_parent(node
);
6164 /* Compute a schedule for a connected dependence graph by first considering
6165 * each strongly connected component (SCC) in the graph separately and then
6166 * incrementally combining them into clusters.
6167 * Return the updated schedule node.
6169 * Initially, each cluster consists of a single SCC, each with its
6170 * own band schedule. The algorithm then tries to merge pairs
6171 * of clusters along a proximity edge until no more suitable
6172 * proximity edges can be found. During this merging, the schedule
6173 * is maintained in the individual SCCs.
6174 * After the merging is completed, the full resulting clusters
6175 * are extracted and in finish_bands_clustering,
6176 * compute_schedule_finish_band is called on each of them to integrate
6177 * the band into "node" and to continue the computation.
6179 * compute_weights initializes the weights that are used by find_proximity.
6181 static __isl_give isl_schedule_node
*compute_schedule_wcc_clustering(
6182 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
6185 struct isl_clustering c
;
6188 ctx
= isl_schedule_node_get_ctx(node
);
6190 if (clustering_init(ctx
, &c
, graph
) < 0)
6193 if (compute_weights(graph
, &c
) < 0)
6197 i
= find_proximity(graph
, &c
);
6200 if (i
>= graph
->n_edge
)
6202 if (merge_clusters_along_edge(ctx
, graph
, i
, &c
) < 0)
6206 if (extract_clusters(ctx
, graph
, &c
) < 0)
6209 node
= finish_bands_clustering(node
, graph
, &c
);
6211 clustering_free(ctx
, &c
);
6214 clustering_free(ctx
, &c
);
6215 return isl_schedule_node_free(node
);
6218 /* Compute a schedule for a connected dependence graph and return
6219 * the updated schedule node.
6221 * If Feautrier's algorithm is selected, we first recursively try to satisfy
6222 * as many validity dependences as possible. When all validity dependences
6223 * are satisfied we extend the schedule to a full-dimensional schedule.
6225 * Call compute_schedule_wcc_whole or compute_schedule_wcc_clustering
6226 * depending on whether the user has selected the option to try and
6227 * compute a schedule for the entire (weakly connected) component first.
6228 * If there is only a single strongly connected component (SCC), then
6229 * there is no point in trying to combine SCCs
6230 * in compute_schedule_wcc_clustering, so compute_schedule_wcc_whole
6231 * is called instead.
6233 static __isl_give isl_schedule_node
*compute_schedule_wcc(
6234 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
6241 ctx
= isl_schedule_node_get_ctx(node
);
6242 if (detect_sccs(ctx
, graph
) < 0)
6243 return isl_schedule_node_free(node
);
6245 if (compute_maxvar(graph
) < 0)
6246 return isl_schedule_node_free(node
);
6248 if (need_feautrier_step(ctx
, graph
))
6249 return compute_schedule_wcc_feautrier(node
, graph
);
6251 if (graph
->scc
<= 1 || isl_options_get_schedule_whole_component(ctx
))
6252 return compute_schedule_wcc_whole(node
, graph
);
6254 return compute_schedule_wcc_clustering(node
, graph
);
6257 /* Compute a schedule for each group of nodes identified by node->scc
6258 * separately and then combine them in a sequence node (or as set node
6259 * if graph->weak is set) inserted at position "node" of the schedule tree.
6260 * Return the updated schedule node.
6262 * If "wcc" is set then each of the groups belongs to a single
6263 * weakly connected component in the dependence graph so that
6264 * there is no need for compute_sub_schedule to look for weakly
6265 * connected components.
6267 static __isl_give isl_schedule_node
*compute_component_schedule(
6268 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
6273 isl_union_set_list
*filters
;
6277 ctx
= isl_schedule_node_get_ctx(node
);
6279 filters
= extract_sccs(ctx
, graph
);
6281 node
= isl_schedule_node_insert_set(node
, filters
);
6283 node
= isl_schedule_node_insert_sequence(node
, filters
);
6285 for (component
= 0; component
< graph
->scc
; ++component
) {
6286 node
= isl_schedule_node_child(node
, component
);
6287 node
= isl_schedule_node_child(node
, 0);
6288 node
= compute_sub_schedule(node
, ctx
, graph
,
6290 &edge_scc_exactly
, component
, wcc
);
6291 node
= isl_schedule_node_parent(node
);
6292 node
= isl_schedule_node_parent(node
);
6298 /* Compute a schedule for the given dependence graph and insert it at "node".
6299 * Return the updated schedule node.
6301 * We first check if the graph is connected (through validity and conditional
6302 * validity dependences) and, if not, compute a schedule
6303 * for each component separately.
6304 * If the schedule_serialize_sccs option is set, then we check for strongly
6305 * connected components instead and compute a separate schedule for
6306 * each such strongly connected component.
6308 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
6309 struct isl_sched_graph
*graph
)
6316 ctx
= isl_schedule_node_get_ctx(node
);
6317 if (isl_options_get_schedule_serialize_sccs(ctx
)) {
6318 if (detect_sccs(ctx
, graph
) < 0)
6319 return isl_schedule_node_free(node
);
6321 if (detect_wccs(ctx
, graph
) < 0)
6322 return isl_schedule_node_free(node
);
6326 return compute_component_schedule(node
, graph
, 1);
6328 return compute_schedule_wcc(node
, graph
);
6331 /* Compute a schedule on sc->domain that respects the given schedule
6334 * In particular, the schedule respects all the validity dependences.
6335 * If the default isl scheduling algorithm is used, it tries to minimize
6336 * the dependence distances over the proximity dependences.
6337 * If Feautrier's scheduling algorithm is used, the proximity dependence
6338 * distances are only minimized during the extension to a full-dimensional
6341 * If there are any condition and conditional validity dependences,
6342 * then the conditional validity dependences may be violated inside
6343 * a tilable band, provided they have no adjacent non-local
6344 * condition dependences.
6346 __isl_give isl_schedule
*isl_schedule_constraints_compute_schedule(
6347 __isl_take isl_schedule_constraints
*sc
)
6349 isl_ctx
*ctx
= isl_schedule_constraints_get_ctx(sc
);
6350 struct isl_sched_graph graph
= { 0 };
6351 isl_schedule
*sched
;
6352 isl_schedule_node
*node
;
6353 isl_union_set
*domain
;
6355 sc
= isl_schedule_constraints_align_params(sc
);
6357 domain
= isl_schedule_constraints_get_domain(sc
);
6358 if (isl_union_set_n_set(domain
) == 0) {
6359 isl_schedule_constraints_free(sc
);
6360 return isl_schedule_from_domain(domain
);
6363 if (graph_init(&graph
, sc
) < 0)
6364 domain
= isl_union_set_free(domain
);
6366 node
= isl_schedule_node_from_domain(domain
);
6367 node
= isl_schedule_node_child(node
, 0);
6369 node
= compute_schedule(node
, &graph
);
6370 sched
= isl_schedule_node_get_schedule(node
);
6371 isl_schedule_node_free(node
);
6373 graph_free(ctx
, &graph
);
6374 isl_schedule_constraints_free(sc
);
6379 /* Compute a schedule for the given union of domains that respects
6380 * all the validity dependences and minimizes
6381 * the dependence distances over the proximity dependences.
6383 * This function is kept for backward compatibility.
6385 __isl_give isl_schedule
*isl_union_set_compute_schedule(
6386 __isl_take isl_union_set
*domain
,
6387 __isl_take isl_union_map
*validity
,
6388 __isl_take isl_union_map
*proximity
)
6390 isl_schedule_constraints
*sc
;
6392 sc
= isl_schedule_constraints_on_domain(domain
);
6393 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
6394 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
6396 return isl_schedule_constraints_compute_schedule(sc
);