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 /* Internal information about the dependence graph used during
284 * the construction of the schedule.
286 * intra_hmap is a cache, mapping dependence relations to their dual,
287 * for dependences from a node to itself
288 * inter_hmap is a cache, mapping dependence relations to their dual,
289 * for dependences between distinct nodes
290 * if compression is involved then the key for these maps
291 * is the original, uncompressed dependence relation, while
292 * the value is the dual of the compressed dependence relation.
294 * n is the number of nodes
295 * node is the list of nodes
296 * maxvar is the maximal number of variables over all nodes
297 * max_row is the allocated number of rows in the schedule
298 * n_row is the current (maximal) number of linearly independent
299 * rows in the node schedules
300 * n_total_row is the current number of rows in the node schedules
301 * band_start is the starting row in the node schedules of the current band
302 * root is set if this graph is the original dependence graph,
303 * without any splitting
305 * sorted contains a list of node indices sorted according to the
306 * SCC to which a node belongs
308 * n_edge is the number of edges
309 * edge is the list of edges
310 * max_edge contains the maximal number of edges of each type;
311 * in particular, it contains the number of edges in the inital graph.
312 * edge_table contains pointers into the edge array, hashed on the source
313 * and sink spaces; there is one such table for each type;
314 * a given edge may be referenced from more than one table
315 * if the corresponding relation appears in more than one of the
316 * sets of dependences; however, for each type there is only
317 * a single edge between a given pair of source and sink space
318 * in the entire graph
320 * node_table contains pointers into the node array, hashed on the space
322 * region contains a list of variable sequences that should be non-trivial
324 * lp contains the (I)LP problem used to obtain new schedule rows
326 * src_scc and dst_scc are the source and sink SCCs of an edge with
327 * conflicting constraints
329 * scc represents the number of components
330 * weak is set if the components are weakly connected
332 * max_weight is used during clustering and represents the maximal
333 * weight of the relevant proximity edges.
335 struct isl_sched_graph
{
336 isl_map_to_basic_set
*intra_hmap
;
337 isl_map_to_basic_set
*inter_hmap
;
339 struct isl_sched_node
*node
;
352 struct isl_sched_edge
*edge
;
354 int max_edge
[isl_edge_last
+ 1];
355 struct isl_hash_table
*edge_table
[isl_edge_last
+ 1];
357 struct isl_hash_table
*node_table
;
358 struct isl_region
*region
;
371 /* Initialize node_table based on the list of nodes.
373 static int graph_init_table(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
377 graph
->node_table
= isl_hash_table_alloc(ctx
, graph
->n
);
378 if (!graph
->node_table
)
381 for (i
= 0; i
< graph
->n
; ++i
) {
382 struct isl_hash_table_entry
*entry
;
385 hash
= isl_space_get_hash(graph
->node
[i
].space
);
386 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
388 graph
->node
[i
].space
, 1);
391 entry
->data
= &graph
->node
[i
];
397 /* Return a pointer to the node that lives within the given space,
398 * or NULL if there is no such node.
400 static struct isl_sched_node
*graph_find_node(isl_ctx
*ctx
,
401 struct isl_sched_graph
*graph
, __isl_keep isl_space
*dim
)
403 struct isl_hash_table_entry
*entry
;
406 hash
= isl_space_get_hash(dim
);
407 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
408 &node_has_space
, dim
, 0);
410 return entry
? entry
->data
: NULL
;
413 static int edge_has_src_and_dst(const void *entry
, const void *val
)
415 const struct isl_sched_edge
*edge
= entry
;
416 const struct isl_sched_edge
*temp
= val
;
418 return edge
->src
== temp
->src
&& edge
->dst
== temp
->dst
;
421 /* Add the given edge to graph->edge_table[type].
423 static isl_stat
graph_edge_table_add(isl_ctx
*ctx
,
424 struct isl_sched_graph
*graph
, enum isl_edge_type type
,
425 struct isl_sched_edge
*edge
)
427 struct isl_hash_table_entry
*entry
;
430 hash
= isl_hash_init();
431 hash
= isl_hash_builtin(hash
, edge
->src
);
432 hash
= isl_hash_builtin(hash
, edge
->dst
);
433 entry
= isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
434 &edge_has_src_and_dst
, edge
, 1);
436 return isl_stat_error
;
442 /* Add "edge" to all relevant edge tables.
443 * That is, for every type of the edge, add it to the corresponding table.
445 static isl_stat
graph_edge_tables_add(isl_ctx
*ctx
,
446 struct isl_sched_graph
*graph
, struct isl_sched_edge
*edge
)
448 enum isl_edge_type t
;
450 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
451 if (!is_type(edge
, t
))
453 if (graph_edge_table_add(ctx
, graph
, t
, edge
) < 0)
454 return isl_stat_error
;
460 /* Allocate the edge_tables based on the maximal number of edges of
463 static int graph_init_edge_tables(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
467 for (i
= 0; i
<= isl_edge_last
; ++i
) {
468 graph
->edge_table
[i
] = isl_hash_table_alloc(ctx
,
470 if (!graph
->edge_table
[i
])
477 /* If graph->edge_table[type] contains an edge from the given source
478 * to the given destination, then return the hash table entry of this edge.
479 * Otherwise, return NULL.
481 static struct isl_hash_table_entry
*graph_find_edge_entry(
482 struct isl_sched_graph
*graph
,
483 enum isl_edge_type type
,
484 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
486 isl_ctx
*ctx
= isl_space_get_ctx(src
->space
);
488 struct isl_sched_edge temp
= { .src
= src
, .dst
= dst
};
490 hash
= isl_hash_init();
491 hash
= isl_hash_builtin(hash
, temp
.src
);
492 hash
= isl_hash_builtin(hash
, temp
.dst
);
493 return isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
494 &edge_has_src_and_dst
, &temp
, 0);
498 /* If graph->edge_table[type] contains an edge from the given source
499 * to the given destination, then return this edge.
500 * Otherwise, return NULL.
502 static struct isl_sched_edge
*graph_find_edge(struct isl_sched_graph
*graph
,
503 enum isl_edge_type type
,
504 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
506 struct isl_hash_table_entry
*entry
;
508 entry
= graph_find_edge_entry(graph
, type
, src
, dst
);
515 /* Check whether the dependence graph has an edge of the given type
516 * between the given two nodes.
518 static isl_bool
graph_has_edge(struct isl_sched_graph
*graph
,
519 enum isl_edge_type type
,
520 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
522 struct isl_sched_edge
*edge
;
525 edge
= graph_find_edge(graph
, type
, src
, dst
);
529 empty
= isl_map_plain_is_empty(edge
->map
);
531 return isl_bool_error
;
536 /* Look for any edge with the same src, dst and map fields as "model".
538 * Return the matching edge if one can be found.
539 * Return "model" if no matching edge is found.
540 * Return NULL on error.
542 static struct isl_sched_edge
*graph_find_matching_edge(
543 struct isl_sched_graph
*graph
, struct isl_sched_edge
*model
)
545 enum isl_edge_type i
;
546 struct isl_sched_edge
*edge
;
548 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
551 edge
= graph_find_edge(graph
, i
, model
->src
, model
->dst
);
554 is_equal
= isl_map_plain_is_equal(model
->map
, edge
->map
);
564 /* Remove the given edge from all the edge_tables that refer to it.
566 static void graph_remove_edge(struct isl_sched_graph
*graph
,
567 struct isl_sched_edge
*edge
)
569 isl_ctx
*ctx
= isl_map_get_ctx(edge
->map
);
570 enum isl_edge_type i
;
572 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
573 struct isl_hash_table_entry
*entry
;
575 entry
= graph_find_edge_entry(graph
, i
, edge
->src
, edge
->dst
);
578 if (entry
->data
!= edge
)
580 isl_hash_table_remove(ctx
, graph
->edge_table
[i
], entry
);
584 /* Check whether the dependence graph has any edge
585 * between the given two nodes.
587 static isl_bool
graph_has_any_edge(struct isl_sched_graph
*graph
,
588 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
590 enum isl_edge_type i
;
593 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
594 r
= graph_has_edge(graph
, i
, src
, dst
);
602 /* Check whether the dependence graph has a validity edge
603 * between the given two nodes.
605 * Conditional validity edges are essentially validity edges that
606 * can be ignored if the corresponding condition edges are iteration private.
607 * Here, we are only checking for the presence of validity
608 * edges, so we need to consider the conditional validity edges too.
609 * In particular, this function is used during the detection
610 * of strongly connected components and we cannot ignore
611 * conditional validity edges during this detection.
613 static isl_bool
graph_has_validity_edge(struct isl_sched_graph
*graph
,
614 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
618 r
= graph_has_edge(graph
, isl_edge_validity
, src
, dst
);
622 return graph_has_edge(graph
, isl_edge_conditional_validity
, src
, dst
);
625 static int graph_alloc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
626 int n_node
, int n_edge
)
631 graph
->n_edge
= n_edge
;
632 graph
->node
= isl_calloc_array(ctx
, struct isl_sched_node
, graph
->n
);
633 graph
->sorted
= isl_calloc_array(ctx
, int, graph
->n
);
634 graph
->region
= isl_alloc_array(ctx
, struct isl_region
, graph
->n
);
635 graph
->edge
= isl_calloc_array(ctx
,
636 struct isl_sched_edge
, graph
->n_edge
);
638 graph
->intra_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
639 graph
->inter_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
641 if (!graph
->node
|| !graph
->region
|| (graph
->n_edge
&& !graph
->edge
) ||
645 for(i
= 0; i
< graph
->n
; ++i
)
646 graph
->sorted
[i
] = i
;
651 static void graph_free(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
655 isl_map_to_basic_set_free(graph
->intra_hmap
);
656 isl_map_to_basic_set_free(graph
->inter_hmap
);
659 for (i
= 0; i
< graph
->n
; ++i
) {
660 isl_space_free(graph
->node
[i
].space
);
661 isl_set_free(graph
->node
[i
].hull
);
662 isl_multi_aff_free(graph
->node
[i
].compress
);
663 isl_multi_aff_free(graph
->node
[i
].decompress
);
664 isl_mat_free(graph
->node
[i
].sched
);
665 isl_map_free(graph
->node
[i
].sched_map
);
666 isl_mat_free(graph
->node
[i
].cmap
);
667 isl_mat_free(graph
->node
[i
].cinv
);
668 isl_mat_free(graph
->node
[i
].ctrans
);
670 free(graph
->node
[i
].coincident
);
671 isl_multi_val_free(graph
->node
[i
].sizes
);
672 isl_vec_free(graph
->node
[i
].max
);
677 for (i
= 0; i
< graph
->n_edge
; ++i
) {
678 isl_map_free(graph
->edge
[i
].map
);
679 isl_union_map_free(graph
->edge
[i
].tagged_condition
);
680 isl_union_map_free(graph
->edge
[i
].tagged_validity
);
684 for (i
= 0; i
<= isl_edge_last
; ++i
)
685 isl_hash_table_free(ctx
, graph
->edge_table
[i
]);
686 isl_hash_table_free(ctx
, graph
->node_table
);
687 isl_basic_set_free(graph
->lp
);
690 /* For each "set" on which this function is called, increment
691 * graph->n by one and update graph->maxvar.
693 static isl_stat
init_n_maxvar(__isl_take isl_set
*set
, void *user
)
695 struct isl_sched_graph
*graph
= user
;
696 int nvar
= isl_set_dim(set
, isl_dim_set
);
699 if (nvar
> graph
->maxvar
)
700 graph
->maxvar
= nvar
;
707 /* Compute the number of rows that should be allocated for the schedule.
708 * In particular, we need one row for each variable or one row
709 * for each basic map in the dependences.
710 * Note that it is practically impossible to exhaust both
711 * the number of dependences and the number of variables.
713 static isl_stat
compute_max_row(struct isl_sched_graph
*graph
,
714 __isl_keep isl_schedule_constraints
*sc
)
718 isl_union_set
*domain
;
722 domain
= isl_schedule_constraints_get_domain(sc
);
723 r
= isl_union_set_foreach_set(domain
, &init_n_maxvar
, graph
);
724 isl_union_set_free(domain
);
726 return isl_stat_error
;
727 n_edge
= isl_schedule_constraints_n_basic_map(sc
);
729 return isl_stat_error
;
730 graph
->max_row
= n_edge
+ graph
->maxvar
;
735 /* Does "bset" have any defining equalities for its set variables?
737 static int has_any_defining_equality(__isl_keep isl_basic_set
*bset
)
744 n
= isl_basic_set_dim(bset
, isl_dim_set
);
745 for (i
= 0; i
< n
; ++i
) {
748 has
= isl_basic_set_has_defining_equality(bset
, isl_dim_set
, i
,
757 /* Set the entries of node->max to the value of the schedule_max_coefficient
760 static isl_stat
set_max_coefficient(isl_ctx
*ctx
, struct isl_sched_node
*node
)
764 max
= isl_options_get_schedule_max_coefficient(ctx
);
768 node
->max
= isl_vec_alloc(ctx
, node
->nvar
);
769 node
->max
= isl_vec_set_si(node
->max
, max
);
771 return isl_stat_error
;
776 /* Set the entries of node->max to the minimum of the schedule_max_coefficient
777 * option (if set) and half of the minimum of the sizes in the other
778 * dimensions. If the minimum of the sizes is one, half of the size
779 * is zero and this value is reset to one.
780 * If the global minimum is unbounded (i.e., if both
781 * the schedule_max_coefficient is not set and the sizes in the other
782 * dimensions are unbounded), then store a negative value.
783 * If the schedule coefficient is close to the size of the instance set
784 * in another dimension, then the schedule may represent a loop
785 * coalescing transformation (especially if the coefficient
786 * in that other dimension is one). Forcing the coefficient to be
787 * smaller than or equal to half the minimal size should avoid this
790 static isl_stat
compute_max_coefficient(isl_ctx
*ctx
,
791 struct isl_sched_node
*node
)
797 max
= isl_options_get_schedule_max_coefficient(ctx
);
798 v
= isl_vec_alloc(ctx
, node
->nvar
);
800 return isl_stat_error
;
802 for (i
= 0; i
< node
->nvar
; ++i
) {
803 isl_int_set_si(v
->el
[i
], max
);
804 isl_int_mul_si(v
->el
[i
], v
->el
[i
], 2);
807 for (i
= 0; i
< node
->nvar
; ++i
) {
810 size
= isl_multi_val_get_val(node
->sizes
, i
);
813 if (!isl_val_is_int(size
)) {
817 for (j
= 0; j
< node
->nvar
; ++j
) {
820 if (isl_int_is_neg(v
->el
[j
]) ||
821 isl_int_gt(v
->el
[j
], size
->n
))
822 isl_int_set(v
->el
[j
], size
->n
);
827 for (i
= 0; i
< node
->nvar
; ++i
) {
828 isl_int_fdiv_q_ui(v
->el
[i
], v
->el
[i
], 2);
829 if (isl_int_is_zero(v
->el
[i
]))
830 isl_int_set_si(v
->el
[i
], 1);
837 return isl_stat_error
;
840 /* Compute and return the size of "set" in dimension "dim".
841 * The size is taken to be the difference in values for that variable
842 * for fixed values of the other variables.
843 * In particular, the variable is first isolated from the other variables
844 * in the range of a map
846 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [i_dim]
848 * and then duplicated
850 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [[i_dim] -> [i_dim']]
852 * The shared variables are then projected out and the maximal value
853 * of i_dim' - i_dim is computed.
855 static __isl_give isl_val
*compute_size(__isl_take isl_set
*set
, int dim
)
862 map
= isl_set_project_onto_map(set
, isl_dim_set
, dim
, 1);
863 map
= isl_map_project_out(map
, isl_dim_in
, dim
, 1);
864 map
= isl_map_range_product(map
, isl_map_copy(map
));
865 map
= isl_set_unwrap(isl_map_range(map
));
866 set
= isl_map_deltas(map
);
867 ls
= isl_local_space_from_space(isl_set_get_space(set
));
868 obj
= isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
869 v
= isl_set_max_val(set
, obj
);
876 /* Compute the size of the instance set "set" of "node", after compression,
877 * as well as bounds on the corresponding coefficients, if needed.
879 * The sizes are needed when the schedule_treat_coalescing option is set.
880 * The bounds are needed when the schedule_treat_coalescing option or
881 * the schedule_max_coefficient option is set.
883 * If the schedule_treat_coalescing option is not set, then at most
884 * the bounds need to be set and this is done in set_max_coefficient.
885 * Otherwise, compress the domain if needed, compute the size
886 * in each direction and store the results in node->size.
887 * Finally, set the bounds on the coefficients based on the sizes
888 * and the schedule_max_coefficient option in compute_max_coefficient.
890 static isl_stat
compute_sizes_and_max(isl_ctx
*ctx
, struct isl_sched_node
*node
,
891 __isl_take isl_set
*set
)
896 if (!isl_options_get_schedule_treat_coalescing(ctx
)) {
898 return set_max_coefficient(ctx
, node
);
901 if (node
->compressed
)
902 set
= isl_set_preimage_multi_aff(set
,
903 isl_multi_aff_copy(node
->decompress
));
904 mv
= isl_multi_val_zero(isl_set_get_space(set
));
905 n
= isl_set_dim(set
, isl_dim_set
);
906 for (j
= 0; j
< n
; ++j
) {
909 v
= compute_size(isl_set_copy(set
), j
);
910 mv
= isl_multi_val_set_val(mv
, j
, v
);
915 return isl_stat_error
;
916 return compute_max_coefficient(ctx
, node
);
919 /* Add a new node to the graph representing the given instance set.
920 * "nvar" is the (possibly compressed) number of variables and
921 * may be smaller than then number of set variables in "set"
922 * if "compressed" is set.
923 * If "compressed" is set, then "hull" represents the constraints
924 * that were used to derive the compression, while "compress" and
925 * "decompress" map the original space to the compressed space and
927 * If "compressed" is not set, then "hull", "compress" and "decompress"
930 * Compute the size of the instance set and bounds on the coefficients,
933 static isl_stat
add_node(struct isl_sched_graph
*graph
,
934 __isl_take isl_set
*set
, int nvar
, int compressed
,
935 __isl_take isl_set
*hull
, __isl_take isl_multi_aff
*compress
,
936 __isl_take isl_multi_aff
*decompress
)
943 struct isl_sched_node
*node
;
946 return isl_stat_error
;
948 ctx
= isl_set_get_ctx(set
);
949 nparam
= isl_set_dim(set
, isl_dim_param
);
950 if (!ctx
->opt
->schedule_parametric
)
952 sched
= isl_mat_alloc(ctx
, 0, 1 + nparam
+ nvar
);
953 node
= &graph
->node
[graph
->n
];
955 space
= isl_set_get_space(set
);
958 node
->nparam
= nparam
;
960 node
->sched_map
= NULL
;
961 coincident
= isl_calloc_array(ctx
, int, graph
->max_row
);
962 node
->coincident
= coincident
;
963 node
->compressed
= compressed
;
965 node
->compress
= compress
;
966 node
->decompress
= decompress
;
967 if (compute_sizes_and_max(ctx
, node
, set
) < 0)
968 return isl_stat_error
;
970 if (!space
|| !sched
|| (graph
->max_row
&& !coincident
))
971 return isl_stat_error
;
972 if (compressed
&& (!hull
|| !compress
|| !decompress
))
973 return isl_stat_error
;
978 /* Add a new node to the graph representing the given set.
980 * If any of the set variables is defined by an equality, then
981 * we perform variable compression such that we can perform
982 * the scheduling on the compressed domain.
984 static isl_stat
extract_node(__isl_take isl_set
*set
, void *user
)
991 isl_multi_aff
*compress
, *decompress
;
992 struct isl_sched_graph
*graph
= user
;
994 hull
= isl_set_affine_hull(isl_set_copy(set
));
995 hull
= isl_basic_set_remove_divs(hull
);
996 nvar
= isl_set_dim(set
, isl_dim_set
);
997 has_equality
= has_any_defining_equality(hull
);
999 if (has_equality
< 0)
1001 if (!has_equality
) {
1002 isl_basic_set_free(hull
);
1003 return add_node(graph
, set
, nvar
, 0, NULL
, NULL
, NULL
);
1006 morph
= isl_basic_set_variable_compression(hull
, isl_dim_set
);
1007 nvar
= isl_morph_ran_dim(morph
, isl_dim_set
);
1008 compress
= isl_morph_get_var_multi_aff(morph
);
1009 morph
= isl_morph_inverse(morph
);
1010 decompress
= isl_morph_get_var_multi_aff(morph
);
1011 isl_morph_free(morph
);
1013 hull_set
= isl_set_from_basic_set(hull
);
1014 return add_node(graph
, set
, nvar
, 1, hull_set
, compress
, decompress
);
1016 isl_basic_set_free(hull
);
1018 return isl_stat_error
;
1021 struct isl_extract_edge_data
{
1022 enum isl_edge_type type
;
1023 struct isl_sched_graph
*graph
;
1026 /* Merge edge2 into edge1, freeing the contents of edge2.
1027 * Return 0 on success and -1 on failure.
1029 * edge1 and edge2 are assumed to have the same value for the map field.
1031 static int merge_edge(struct isl_sched_edge
*edge1
,
1032 struct isl_sched_edge
*edge2
)
1034 edge1
->types
|= edge2
->types
;
1035 isl_map_free(edge2
->map
);
1037 if (is_condition(edge2
)) {
1038 if (!edge1
->tagged_condition
)
1039 edge1
->tagged_condition
= edge2
->tagged_condition
;
1041 edge1
->tagged_condition
=
1042 isl_union_map_union(edge1
->tagged_condition
,
1043 edge2
->tagged_condition
);
1046 if (is_conditional_validity(edge2
)) {
1047 if (!edge1
->tagged_validity
)
1048 edge1
->tagged_validity
= edge2
->tagged_validity
;
1050 edge1
->tagged_validity
=
1051 isl_union_map_union(edge1
->tagged_validity
,
1052 edge2
->tagged_validity
);
1055 if (is_condition(edge2
) && !edge1
->tagged_condition
)
1057 if (is_conditional_validity(edge2
) && !edge1
->tagged_validity
)
1063 /* Insert dummy tags in domain and range of "map".
1065 * In particular, if "map" is of the form
1071 * [A -> dummy_tag] -> [B -> dummy_tag]
1073 * where the dummy_tags are identical and equal to any dummy tags
1074 * introduced by any other call to this function.
1076 static __isl_give isl_map
*insert_dummy_tags(__isl_take isl_map
*map
)
1082 isl_set
*domain
, *range
;
1084 ctx
= isl_map_get_ctx(map
);
1086 id
= isl_id_alloc(ctx
, NULL
, &dummy
);
1087 space
= isl_space_params(isl_map_get_space(map
));
1088 space
= isl_space_set_from_params(space
);
1089 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
1090 space
= isl_space_map_from_set(space
);
1092 domain
= isl_map_wrap(map
);
1093 range
= isl_map_wrap(isl_map_universe(space
));
1094 map
= isl_map_from_domain_and_range(domain
, range
);
1095 map
= isl_map_zip(map
);
1100 /* Given that at least one of "src" or "dst" is compressed, return
1101 * a map between the spaces of these nodes restricted to the affine
1102 * hull that was used in the compression.
1104 static __isl_give isl_map
*extract_hull(struct isl_sched_node
*src
,
1105 struct isl_sched_node
*dst
)
1109 if (src
->compressed
)
1110 dom
= isl_set_copy(src
->hull
);
1112 dom
= isl_set_universe(isl_space_copy(src
->space
));
1113 if (dst
->compressed
)
1114 ran
= isl_set_copy(dst
->hull
);
1116 ran
= isl_set_universe(isl_space_copy(dst
->space
));
1118 return isl_map_from_domain_and_range(dom
, ran
);
1121 /* Intersect the domains of the nested relations in domain and range
1122 * of "tagged" with "map".
1124 static __isl_give isl_map
*map_intersect_domains(__isl_take isl_map
*tagged
,
1125 __isl_keep isl_map
*map
)
1129 tagged
= isl_map_zip(tagged
);
1130 set
= isl_map_wrap(isl_map_copy(map
));
1131 tagged
= isl_map_intersect_domain(tagged
, set
);
1132 tagged
= isl_map_zip(tagged
);
1136 /* Return a pointer to the node that lives in the domain space of "map"
1137 * or NULL if there is no such node.
1139 static struct isl_sched_node
*find_domain_node(isl_ctx
*ctx
,
1140 struct isl_sched_graph
*graph
, __isl_keep isl_map
*map
)
1142 struct isl_sched_node
*node
;
1145 space
= isl_space_domain(isl_map_get_space(map
));
1146 node
= graph_find_node(ctx
, graph
, space
);
1147 isl_space_free(space
);
1152 /* Return a pointer to the node that lives in the range space of "map"
1153 * or NULL if there is no such node.
1155 static struct isl_sched_node
*find_range_node(isl_ctx
*ctx
,
1156 struct isl_sched_graph
*graph
, __isl_keep isl_map
*map
)
1158 struct isl_sched_node
*node
;
1161 space
= isl_space_range(isl_map_get_space(map
));
1162 node
= graph_find_node(ctx
, graph
, space
);
1163 isl_space_free(space
);
1168 /* Add a new edge to the graph based on the given map
1169 * and add it to data->graph->edge_table[data->type].
1170 * If a dependence relation of a given type happens to be identical
1171 * to one of the dependence relations of a type that was added before,
1172 * then we don't create a new edge, but instead mark the original edge
1173 * as also representing a dependence of the current type.
1175 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1176 * may be specified as "tagged" dependence relations. That is, "map"
1177 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1178 * the dependence on iterations and a and b are tags.
1179 * edge->map is set to the relation containing the elements i -> j,
1180 * while edge->tagged_condition and edge->tagged_validity contain
1181 * the union of all the "map" relations
1182 * for which extract_edge is called that result in the same edge->map.
1184 * If the source or the destination node is compressed, then
1185 * intersect both "map" and "tagged" with the constraints that
1186 * were used to construct the compression.
1187 * This ensures that there are no schedule constraints defined
1188 * outside of these domains, while the scheduler no longer has
1189 * any control over those outside parts.
1191 static isl_stat
extract_edge(__isl_take isl_map
*map
, void *user
)
1193 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1194 struct isl_extract_edge_data
*data
= user
;
1195 struct isl_sched_graph
*graph
= data
->graph
;
1196 struct isl_sched_node
*src
, *dst
;
1197 struct isl_sched_edge
*edge
;
1198 isl_map
*tagged
= NULL
;
1200 if (data
->type
== isl_edge_condition
||
1201 data
->type
== isl_edge_conditional_validity
) {
1202 if (isl_map_can_zip(map
)) {
1203 tagged
= isl_map_copy(map
);
1204 map
= isl_set_unwrap(isl_map_domain(isl_map_zip(map
)));
1206 tagged
= insert_dummy_tags(isl_map_copy(map
));
1210 src
= find_domain_node(ctx
, graph
, map
);
1211 dst
= find_range_node(ctx
, graph
, map
);
1215 isl_map_free(tagged
);
1219 if (src
->compressed
|| dst
->compressed
) {
1221 hull
= extract_hull(src
, dst
);
1223 tagged
= map_intersect_domains(tagged
, hull
);
1224 map
= isl_map_intersect(map
, hull
);
1227 graph
->edge
[graph
->n_edge
].src
= src
;
1228 graph
->edge
[graph
->n_edge
].dst
= dst
;
1229 graph
->edge
[graph
->n_edge
].map
= map
;
1230 graph
->edge
[graph
->n_edge
].types
= 0;
1231 graph
->edge
[graph
->n_edge
].tagged_condition
= NULL
;
1232 graph
->edge
[graph
->n_edge
].tagged_validity
= NULL
;
1233 set_type(&graph
->edge
[graph
->n_edge
], data
->type
);
1234 if (data
->type
== isl_edge_condition
)
1235 graph
->edge
[graph
->n_edge
].tagged_condition
=
1236 isl_union_map_from_map(tagged
);
1237 if (data
->type
== isl_edge_conditional_validity
)
1238 graph
->edge
[graph
->n_edge
].tagged_validity
=
1239 isl_union_map_from_map(tagged
);
1241 edge
= graph_find_matching_edge(graph
, &graph
->edge
[graph
->n_edge
]);
1244 return isl_stat_error
;
1246 if (edge
== &graph
->edge
[graph
->n_edge
])
1247 return graph_edge_table_add(ctx
, graph
, data
->type
,
1248 &graph
->edge
[graph
->n_edge
++]);
1250 if (merge_edge(edge
, &graph
->edge
[graph
->n_edge
]) < 0)
1253 return graph_edge_table_add(ctx
, graph
, data
->type
, edge
);
1256 /* Initialize the schedule graph "graph" from the schedule constraints "sc".
1258 * The context is included in the domain before the nodes of
1259 * the graphs are extracted in order to be able to exploit
1260 * any possible additional equalities.
1261 * Note that this intersection is only performed locally here.
1263 static isl_stat
graph_init(struct isl_sched_graph
*graph
,
1264 __isl_keep isl_schedule_constraints
*sc
)
1267 isl_union_set
*domain
;
1269 struct isl_extract_edge_data data
;
1270 enum isl_edge_type i
;
1274 return isl_stat_error
;
1276 ctx
= isl_schedule_constraints_get_ctx(sc
);
1278 domain
= isl_schedule_constraints_get_domain(sc
);
1279 graph
->n
= isl_union_set_n_set(domain
);
1280 isl_union_set_free(domain
);
1282 if (graph_alloc(ctx
, graph
, graph
->n
,
1283 isl_schedule_constraints_n_map(sc
)) < 0)
1284 return isl_stat_error
;
1286 if (compute_max_row(graph
, sc
) < 0)
1287 return isl_stat_error
;
1290 domain
= isl_schedule_constraints_get_domain(sc
);
1291 domain
= isl_union_set_intersect_params(domain
,
1292 isl_schedule_constraints_get_context(sc
));
1293 r
= isl_union_set_foreach_set(domain
, &extract_node
, graph
);
1294 isl_union_set_free(domain
);
1296 return isl_stat_error
;
1297 if (graph_init_table(ctx
, graph
) < 0)
1298 return isl_stat_error
;
1299 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
1300 c
= isl_schedule_constraints_get(sc
, i
);
1301 graph
->max_edge
[i
] = isl_union_map_n_map(c
);
1302 isl_union_map_free(c
);
1304 return isl_stat_error
;
1306 if (graph_init_edge_tables(ctx
, graph
) < 0)
1307 return isl_stat_error
;
1310 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
1314 c
= isl_schedule_constraints_get(sc
, i
);
1315 r
= isl_union_map_foreach_map(c
, &extract_edge
, &data
);
1316 isl_union_map_free(c
);
1318 return isl_stat_error
;
1324 /* Check whether there is any dependence from node[j] to node[i]
1325 * or from node[i] to node[j].
1327 static isl_bool
node_follows_weak(int i
, int j
, void *user
)
1330 struct isl_sched_graph
*graph
= user
;
1332 f
= graph_has_any_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1335 return graph_has_any_edge(graph
, &graph
->node
[i
], &graph
->node
[j
]);
1338 /* Check whether there is a (conditional) validity dependence from node[j]
1339 * to node[i], forcing node[i] to follow node[j].
1341 static isl_bool
node_follows_strong(int i
, int j
, void *user
)
1343 struct isl_sched_graph
*graph
= user
;
1345 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1348 /* Use Tarjan's algorithm for computing the strongly connected components
1349 * in the dependence graph only considering those edges defined by "follows".
1351 static int detect_ccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
1352 isl_bool (*follows
)(int i
, int j
, void *user
))
1355 struct isl_tarjan_graph
*g
= NULL
;
1357 g
= isl_tarjan_graph_init(ctx
, graph
->n
, follows
, graph
);
1365 while (g
->order
[i
] != -1) {
1366 graph
->node
[g
->order
[i
]].scc
= graph
->scc
;
1374 isl_tarjan_graph_free(g
);
1379 /* Apply Tarjan's algorithm to detect the strongly connected components
1380 * in the dependence graph.
1381 * Only consider the (conditional) validity dependences and clear "weak".
1383 static int detect_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1386 return detect_ccs(ctx
, graph
, &node_follows_strong
);
1389 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1390 * in the dependence graph.
1391 * Consider all dependences and set "weak".
1393 static int detect_wccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1396 return detect_ccs(ctx
, graph
, &node_follows_weak
);
1399 static int cmp_scc(const void *a
, const void *b
, void *data
)
1401 struct isl_sched_graph
*graph
= data
;
1405 return graph
->node
[*i1
].scc
- graph
->node
[*i2
].scc
;
1408 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1410 static int sort_sccs(struct isl_sched_graph
*graph
)
1412 return isl_sort(graph
->sorted
, graph
->n
, sizeof(int), &cmp_scc
, graph
);
1415 /* Given a dependence relation R from "node" to itself,
1416 * construct the set of coefficients of valid constraints for elements
1417 * in that dependence relation.
1418 * In particular, the result contains tuples of coefficients
1419 * c_0, c_n, c_x such that
1421 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1425 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1427 * We choose here to compute the dual of delta R.
1428 * Alternatively, we could have computed the dual of R, resulting
1429 * in a set of tuples c_0, c_n, c_x, c_y, and then
1430 * plugged in (c_0, c_n, c_x, -c_x).
1432 * If "node" has been compressed, then the dependence relation
1433 * is also compressed before the set of coefficients is computed.
1435 static __isl_give isl_basic_set
*intra_coefficients(
1436 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1437 __isl_take isl_map
*map
)
1441 isl_basic_set
*coef
;
1442 isl_maybe_isl_basic_set m
;
1444 m
= isl_map_to_basic_set_try_get(graph
->intra_hmap
, map
);
1445 if (m
.valid
< 0 || m
.valid
) {
1450 key
= isl_map_copy(map
);
1451 if (node
->compressed
) {
1452 map
= isl_map_preimage_domain_multi_aff(map
,
1453 isl_multi_aff_copy(node
->decompress
));
1454 map
= isl_map_preimage_range_multi_aff(map
,
1455 isl_multi_aff_copy(node
->decompress
));
1457 delta
= isl_set_remove_divs(isl_map_deltas(map
));
1458 coef
= isl_set_coefficients(delta
);
1459 graph
->intra_hmap
= isl_map_to_basic_set_set(graph
->intra_hmap
, key
,
1460 isl_basic_set_copy(coef
));
1465 /* Given a dependence relation R, construct the set of coefficients
1466 * of valid constraints for elements in that dependence relation.
1467 * In particular, the result contains tuples of coefficients
1468 * c_0, c_n, c_x, c_y such that
1470 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1472 * If the source or destination nodes of "edge" have been compressed,
1473 * then the dependence relation is also compressed before
1474 * the set of coefficients is computed.
1476 static __isl_give isl_basic_set
*inter_coefficients(
1477 struct isl_sched_graph
*graph
, struct isl_sched_edge
*edge
,
1478 __isl_take isl_map
*map
)
1482 isl_basic_set
*coef
;
1483 isl_maybe_isl_basic_set m
;
1485 m
= isl_map_to_basic_set_try_get(graph
->inter_hmap
, map
);
1486 if (m
.valid
< 0 || m
.valid
) {
1491 key
= isl_map_copy(map
);
1492 if (edge
->src
->compressed
)
1493 map
= isl_map_preimage_domain_multi_aff(map
,
1494 isl_multi_aff_copy(edge
->src
->decompress
));
1495 if (edge
->dst
->compressed
)
1496 map
= isl_map_preimage_range_multi_aff(map
,
1497 isl_multi_aff_copy(edge
->dst
->decompress
));
1498 set
= isl_map_wrap(isl_map_remove_divs(map
));
1499 coef
= isl_set_coefficients(set
);
1500 graph
->inter_hmap
= isl_map_to_basic_set_set(graph
->inter_hmap
, key
,
1501 isl_basic_set_copy(coef
));
1506 /* Return the position of the coefficients of the variables in
1507 * the coefficients constraints "coef".
1509 * The space of "coef" is of the form
1511 * { coefficients[[cst, params] -> S] }
1513 * Return the position of S.
1515 static int coef_var_offset(__isl_keep isl_basic_set
*coef
)
1520 space
= isl_space_unwrap(isl_basic_set_get_space(coef
));
1521 offset
= isl_space_dim(space
, isl_dim_in
);
1522 isl_space_free(space
);
1527 /* Return the offset of the coefficients of the variables of "node"
1530 * Within each node, the coefficients have the following order:
1532 * - c_i_n (if parametric)
1533 * - positive and negative parts of c_i_x
1535 static int node_var_coef_offset(struct isl_sched_node
*node
)
1537 return node
->start
+ 1 + node
->nparam
;
1540 /* Construct an isl_dim_map for mapping constraints on coefficients
1541 * for "node" to the corresponding positions in graph->lp.
1542 * "offset" is the offset of the coefficients for the variables
1543 * in the input constraints.
1544 * "s" is the sign of the mapping.
1546 * The input constraints are given in terms of the coefficients (c_0, c_n, c_x).
1547 * The mapping produced by this function essentially plugs in
1548 * (0, 0, c_i_x^+ - c_i_x^-) if s = 1 and
1549 * (0, 0, -c_i_x^+ + c_i_x^-) if s = -1.
1550 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1552 * The caller can extend the mapping to also map the other coefficients
1553 * (and therefore not plug in 0).
1555 static __isl_give isl_dim_map
*intra_dim_map(isl_ctx
*ctx
,
1556 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1561 isl_dim_map
*dim_map
;
1563 if (!node
|| !graph
->lp
)
1566 total
= isl_basic_set_total_dim(graph
->lp
);
1567 pos
= node_var_coef_offset(node
);
1568 dim_map
= isl_dim_map_alloc(ctx
, total
);
1569 isl_dim_map_range(dim_map
, pos
, 2, offset
, 1, node
->nvar
, -s
);
1570 isl_dim_map_range(dim_map
, pos
+ 1, 2, offset
, 1, node
->nvar
, s
);
1575 /* Construct an isl_dim_map for mapping constraints on coefficients
1576 * for "src" (node i) and "dst" (node j) to the corresponding positions
1578 * "offset" is the offset of the coefficients for the variables of "src"
1579 * in the input constraints.
1580 * "s" is the sign of the mapping.
1582 * The input constraints are given in terms of the coefficients
1583 * (c_0, c_n, c_x, c_y).
1584 * The mapping produced by this function essentially plugs in
1585 * (c_j_0 - c_i_0, c_j_n - c_i_n,
1586 * c_j_x^+ - c_j_x^-, -(c_i_x^+ - c_i_x^-)) if s = 1 and
1587 * (-c_j_0 + c_i_0, -c_j_n + c_i_n,
1588 * - (c_j_x^+ - c_j_x^-), c_i_x^+ - c_i_x^-) if s = -1.
1589 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1591 * The caller can further extend the mapping.
1593 static __isl_give isl_dim_map
*inter_dim_map(isl_ctx
*ctx
,
1594 struct isl_sched_graph
*graph
, struct isl_sched_node
*src
,
1595 struct isl_sched_node
*dst
, int offset
, int s
)
1599 isl_dim_map
*dim_map
;
1601 if (!src
|| !dst
|| !graph
->lp
)
1604 total
= isl_basic_set_total_dim(graph
->lp
);
1605 dim_map
= isl_dim_map_alloc(ctx
, total
);
1607 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, s
);
1608 isl_dim_map_range(dim_map
, dst
->start
+ 1, 1, 1, 1, dst
->nparam
, s
);
1609 pos
= node_var_coef_offset(dst
);
1610 isl_dim_map_range(dim_map
, pos
, 2, offset
+ src
->nvar
, 1,
1612 isl_dim_map_range(dim_map
, pos
+ 1, 2, offset
+ src
->nvar
, 1,
1615 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, -s
);
1616 isl_dim_map_range(dim_map
, src
->start
+ 1, 1, 1, 1, src
->nparam
, -s
);
1617 pos
= node_var_coef_offset(src
);
1618 isl_dim_map_range(dim_map
, pos
, 2, offset
, 1, src
->nvar
, s
);
1619 isl_dim_map_range(dim_map
, pos
+ 1, 2, offset
, 1, src
->nvar
, -s
);
1624 /* Add constraints to graph->lp that force validity for the given
1625 * dependence from a node i to itself.
1626 * That is, add constraints that enforce
1628 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1629 * = c_i_x (y - x) >= 0
1631 * for each (x,y) in R.
1632 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1633 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1634 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1635 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1637 * Actually, we do not construct constraints for the c_i_x themselves,
1638 * but for the coefficients of c_i_x written as a linear combination
1639 * of the columns in node->cmap.
1641 static isl_stat
add_intra_validity_constraints(struct isl_sched_graph
*graph
,
1642 struct isl_sched_edge
*edge
)
1645 isl_map
*map
= isl_map_copy(edge
->map
);
1646 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1647 isl_dim_map
*dim_map
;
1648 isl_basic_set
*coef
;
1649 struct isl_sched_node
*node
= edge
->src
;
1651 coef
= intra_coefficients(graph
, node
, map
);
1653 offset
= coef_var_offset(coef
);
1655 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1656 offset
, isl_mat_copy(node
->cmap
));
1658 return isl_stat_error
;
1660 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, 1);
1661 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1662 coef
->n_eq
, coef
->n_ineq
);
1663 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1669 /* Add constraints to graph->lp that force validity for the given
1670 * dependence from node i to node j.
1671 * That is, add constraints that enforce
1673 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1675 * for each (x,y) in R.
1676 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1677 * of valid constraints for R and then plug in
1678 * (c_j_0 - c_i_0, c_j_n - c_i_n, c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1679 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1680 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1682 * Actually, we do not construct constraints for the c_*_x themselves,
1683 * but for the coefficients of c_*_x written as a linear combination
1684 * of the columns in node->cmap.
1686 static isl_stat
add_inter_validity_constraints(struct isl_sched_graph
*graph
,
1687 struct isl_sched_edge
*edge
)
1692 isl_dim_map
*dim_map
;
1693 isl_basic_set
*coef
;
1694 struct isl_sched_node
*src
= edge
->src
;
1695 struct isl_sched_node
*dst
= edge
->dst
;
1698 return isl_stat_error
;
1700 map
= isl_map_copy(edge
->map
);
1701 ctx
= isl_map_get_ctx(map
);
1702 coef
= inter_coefficients(graph
, edge
, map
);
1704 offset
= coef_var_offset(coef
);
1706 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1707 offset
, isl_mat_copy(src
->cmap
));
1708 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1709 offset
+ src
->nvar
, isl_mat_copy(dst
->cmap
));
1711 return isl_stat_error
;
1713 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, 1);
1715 edge
->start
= graph
->lp
->n_ineq
;
1716 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1717 coef
->n_eq
, coef
->n_ineq
);
1718 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1721 return isl_stat_error
;
1722 edge
->end
= graph
->lp
->n_ineq
;
1727 /* Add constraints to graph->lp that bound the dependence distance for the given
1728 * dependence from a node i to itself.
1729 * If s = 1, we add the constraint
1731 * c_i_x (y - x) <= m_0 + m_n n
1735 * -c_i_x (y - x) + m_0 + m_n n >= 0
1737 * for each (x,y) in R.
1738 * If s = -1, we add the constraint
1740 * -c_i_x (y - x) <= m_0 + m_n n
1744 * c_i_x (y - x) + m_0 + m_n n >= 0
1746 * for each (x,y) in R.
1747 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1748 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1749 * with each coefficient (except m_0) represented as a pair of non-negative
1752 * Actually, we do not construct constraints for the c_i_x themselves,
1753 * but for the coefficients of c_i_x written as a linear combination
1754 * of the columns in node->cmap.
1757 * If "local" is set, then we add constraints
1759 * c_i_x (y - x) <= 0
1763 * -c_i_x (y - x) <= 0
1765 * instead, forcing the dependence distance to be (less than or) equal to 0.
1766 * That is, we plug in (0, 0, -s * c_i_x),
1767 * Note that dependences marked local are treated as validity constraints
1768 * by add_all_validity_constraints and therefore also have
1769 * their distances bounded by 0 from below.
1771 static isl_stat
add_intra_proximity_constraints(struct isl_sched_graph
*graph
,
1772 struct isl_sched_edge
*edge
, int s
, int local
)
1776 isl_map
*map
= isl_map_copy(edge
->map
);
1777 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1778 isl_dim_map
*dim_map
;
1779 isl_basic_set
*coef
;
1780 struct isl_sched_node
*node
= edge
->src
;
1782 coef
= intra_coefficients(graph
, node
, map
);
1784 offset
= coef_var_offset(coef
);
1786 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1787 offset
, isl_mat_copy(node
->cmap
));
1789 return isl_stat_error
;
1791 nparam
= isl_space_dim(node
->space
, isl_dim_param
);
1792 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, -s
);
1795 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1796 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1797 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1799 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1800 coef
->n_eq
, coef
->n_ineq
);
1801 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1807 /* Add constraints to graph->lp that bound the dependence distance for the given
1808 * dependence from node i to node j.
1809 * If s = 1, we add the constraint
1811 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1816 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1819 * for each (x,y) in R.
1820 * If s = -1, we add the constraint
1822 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1827 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1830 * for each (x,y) in R.
1831 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1832 * of valid constraints for R and then plug in
1833 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1835 * with each coefficient (except m_0, c_*_0 and c_*_n)
1836 * represented as a pair of non-negative coefficients.
1838 * Actually, we do not construct constraints for the c_*_x themselves,
1839 * but for the coefficients of c_*_x written as a linear combination
1840 * of the columns in node->cmap.
1843 * If "local" is set, then we add constraints
1845 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1849 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
1851 * instead, forcing the dependence distance to be (less than or) equal to 0.
1852 * That is, we plug in
1853 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
1854 * Note that dependences marked local are treated as validity constraints
1855 * by add_all_validity_constraints and therefore also have
1856 * their distances bounded by 0 from below.
1858 static isl_stat
add_inter_proximity_constraints(struct isl_sched_graph
*graph
,
1859 struct isl_sched_edge
*edge
, int s
, int local
)
1863 isl_map
*map
= isl_map_copy(edge
->map
);
1864 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1865 isl_dim_map
*dim_map
;
1866 isl_basic_set
*coef
;
1867 struct isl_sched_node
*src
= edge
->src
;
1868 struct isl_sched_node
*dst
= edge
->dst
;
1870 coef
= inter_coefficients(graph
, edge
, map
);
1872 offset
= coef_var_offset(coef
);
1874 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1875 offset
, isl_mat_copy(src
->cmap
));
1876 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1877 offset
+ src
->nvar
, isl_mat_copy(dst
->cmap
));
1879 return isl_stat_error
;
1881 nparam
= isl_space_dim(src
->space
, isl_dim_param
);
1882 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, -s
);
1885 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1886 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1887 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1890 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1891 coef
->n_eq
, coef
->n_ineq
);
1892 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1898 /* Add all validity constraints to graph->lp.
1900 * An edge that is forced to be local needs to have its dependence
1901 * distances equal to zero. We take care of bounding them by 0 from below
1902 * here. add_all_proximity_constraints takes care of bounding them by 0
1905 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1906 * Otherwise, we ignore them.
1908 static int add_all_validity_constraints(struct isl_sched_graph
*graph
,
1909 int use_coincidence
)
1913 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1914 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1917 local
= is_local(edge
) ||
1918 (is_coincidence(edge
) && use_coincidence
);
1919 if (!is_validity(edge
) && !local
)
1921 if (edge
->src
!= edge
->dst
)
1923 if (add_intra_validity_constraints(graph
, edge
) < 0)
1927 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1928 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1931 local
= is_local(edge
) ||
1932 (is_coincidence(edge
) && use_coincidence
);
1933 if (!is_validity(edge
) && !local
)
1935 if (edge
->src
== edge
->dst
)
1937 if (add_inter_validity_constraints(graph
, edge
) < 0)
1944 /* Add constraints to graph->lp that bound the dependence distance
1945 * for all dependence relations.
1946 * If a given proximity dependence is identical to a validity
1947 * dependence, then the dependence distance is already bounded
1948 * from below (by zero), so we only need to bound the distance
1949 * from above. (This includes the case of "local" dependences
1950 * which are treated as validity dependence by add_all_validity_constraints.)
1951 * Otherwise, we need to bound the distance both from above and from below.
1953 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1954 * Otherwise, we ignore them.
1956 static int add_all_proximity_constraints(struct isl_sched_graph
*graph
,
1957 int use_coincidence
)
1961 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1962 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1965 local
= is_local(edge
) ||
1966 (is_coincidence(edge
) && use_coincidence
);
1967 if (!is_proximity(edge
) && !local
)
1969 if (edge
->src
== edge
->dst
&&
1970 add_intra_proximity_constraints(graph
, edge
, 1, local
) < 0)
1972 if (edge
->src
!= edge
->dst
&&
1973 add_inter_proximity_constraints(graph
, edge
, 1, local
) < 0)
1975 if (is_validity(edge
) || local
)
1977 if (edge
->src
== edge
->dst
&&
1978 add_intra_proximity_constraints(graph
, edge
, -1, 0) < 0)
1980 if (edge
->src
!= edge
->dst
&&
1981 add_inter_proximity_constraints(graph
, edge
, -1, 0) < 0)
1988 /* Compute a basis for the rows in the linear part of the schedule
1989 * and extend this basis to a full basis. The remaining rows
1990 * can then be used to force linear independence from the rows
1993 * In particular, given the schedule rows S, we compute
1998 * with H the Hermite normal form of S. That is, all but the
1999 * first rank columns of H are zero and so each row in S is
2000 * a linear combination of the first rank rows of Q.
2001 * The matrix Q is then transposed because we will write the
2002 * coefficients of the next schedule row as a column vector s
2003 * and express this s as a linear combination s = Q c of the
2005 * Similarly, the matrix U is transposed such that we can
2006 * compute the coefficients c = U s from a schedule row s.
2008 static int node_update_cmap(struct isl_sched_node
*node
)
2011 int n_row
= isl_mat_rows(node
->sched
);
2013 H
= isl_mat_sub_alloc(node
->sched
, 0, n_row
,
2014 1 + node
->nparam
, node
->nvar
);
2016 H
= isl_mat_left_hermite(H
, 0, &U
, &Q
);
2017 isl_mat_free(node
->cmap
);
2018 isl_mat_free(node
->cinv
);
2019 isl_mat_free(node
->ctrans
);
2020 node
->ctrans
= isl_mat_copy(Q
);
2021 node
->cmap
= isl_mat_transpose(Q
);
2022 node
->cinv
= isl_mat_transpose(U
);
2023 node
->rank
= isl_mat_initial_non_zero_cols(H
);
2026 if (!node
->cmap
|| !node
->cinv
|| !node
->ctrans
|| node
->rank
< 0)
2031 /* Is "edge" marked as a validity or a conditional validity edge?
2033 static int is_any_validity(struct isl_sched_edge
*edge
)
2035 return is_validity(edge
) || is_conditional_validity(edge
);
2038 /* How many times should we count the constraints in "edge"?
2040 * If carry is set, then we are counting the number of
2041 * (validity or conditional validity) constraints that will be added
2042 * in setup_carry_lp and we count each edge exactly once.
2044 * Otherwise, we count as follows
2045 * validity -> 1 (>= 0)
2046 * validity+proximity -> 2 (>= 0 and upper bound)
2047 * proximity -> 2 (lower and upper bound)
2048 * local(+any) -> 2 (>= 0 and <= 0)
2050 * If an edge is only marked conditional_validity then it counts
2051 * as zero since it is only checked afterwards.
2053 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2054 * Otherwise, we ignore them.
2056 static int edge_multiplicity(struct isl_sched_edge
*edge
, int carry
,
2057 int use_coincidence
)
2061 if (is_proximity(edge
) || is_local(edge
))
2063 if (use_coincidence
&& is_coincidence(edge
))
2065 if (is_validity(edge
))
2070 /* Count the number of equality and inequality constraints
2071 * that will be added for the given map.
2073 * "use_coincidence" is set if we should take into account coincidence edges.
2075 static int count_map_constraints(struct isl_sched_graph
*graph
,
2076 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
,
2077 int *n_eq
, int *n_ineq
, int carry
, int use_coincidence
)
2079 isl_basic_set
*coef
;
2080 int f
= edge_multiplicity(edge
, carry
, use_coincidence
);
2087 if (edge
->src
== edge
->dst
)
2088 coef
= intra_coefficients(graph
, edge
->src
, map
);
2090 coef
= inter_coefficients(graph
, edge
, map
);
2093 *n_eq
+= f
* coef
->n_eq
;
2094 *n_ineq
+= f
* coef
->n_ineq
;
2095 isl_basic_set_free(coef
);
2100 /* Count the number of equality and inequality constraints
2101 * that will be added to the main lp problem.
2102 * We count as follows
2103 * validity -> 1 (>= 0)
2104 * validity+proximity -> 2 (>= 0 and upper bound)
2105 * proximity -> 2 (lower and upper bound)
2106 * local(+any) -> 2 (>= 0 and <= 0)
2108 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2109 * Otherwise, we ignore them.
2111 static int count_constraints(struct isl_sched_graph
*graph
,
2112 int *n_eq
, int *n_ineq
, int use_coincidence
)
2116 *n_eq
= *n_ineq
= 0;
2117 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2118 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2119 isl_map
*map
= isl_map_copy(edge
->map
);
2121 if (count_map_constraints(graph
, edge
, map
, n_eq
, n_ineq
,
2122 0, use_coincidence
) < 0)
2129 /* Count the number of constraints that will be added by
2130 * add_bound_constant_constraints to bound the values of the constant terms
2131 * and increment *n_eq and *n_ineq accordingly.
2133 * In practice, add_bound_constant_constraints only adds inequalities.
2135 static isl_stat
count_bound_constant_constraints(isl_ctx
*ctx
,
2136 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
2138 if (isl_options_get_schedule_max_constant_term(ctx
) == -1)
2141 *n_ineq
+= graph
->n
;
2146 /* Add constraints to bound the values of the constant terms in the schedule,
2147 * if requested by the user.
2149 * The maximal value of the constant terms is defined by the option
2150 * "schedule_max_constant_term".
2152 * Within each node, the coefficients have the following order:
2154 * - c_i_n (if parametric)
2155 * - positive and negative parts of c_i_x
2157 static isl_stat
add_bound_constant_constraints(isl_ctx
*ctx
,
2158 struct isl_sched_graph
*graph
)
2164 max
= isl_options_get_schedule_max_constant_term(ctx
);
2168 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2170 for (i
= 0; i
< graph
->n
; ++i
) {
2171 struct isl_sched_node
*node
= &graph
->node
[i
];
2172 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2174 return isl_stat_error
;
2175 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2176 isl_int_set_si(graph
->lp
->ineq
[k
][1 + node
->start
], -1);
2177 isl_int_set_si(graph
->lp
->ineq
[k
][0], max
);
2183 /* Count the number of constraints that will be added by
2184 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
2187 * In practice, add_bound_coefficient_constraints only adds inequalities.
2189 static int count_bound_coefficient_constraints(isl_ctx
*ctx
,
2190 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
2194 if (isl_options_get_schedule_max_coefficient(ctx
) == -1 &&
2195 !isl_options_get_schedule_treat_coalescing(ctx
))
2198 for (i
= 0; i
< graph
->n
; ++i
)
2199 *n_ineq
+= graph
->node
[i
].nparam
+ 2 * graph
->node
[i
].nvar
;
2204 /* Add constraints to graph->lp that bound the values of
2205 * the parameter schedule coefficients of "node" to "max" and
2206 * the variable schedule coefficients to the corresponding entry
2208 * In either case, a negative value means that no bound needs to be imposed.
2210 * For parameter coefficients, this amounts to adding a constraint
2218 * The variables coefficients are, however, not represented directly.
2219 * Instead, the variables coefficients c_x are written as a linear
2220 * combination c_x = cmap c_z of some other coefficients c_z,
2221 * which are in turn encoded as c_z = c_z^+ - c_z^-.
2222 * Let a_j be the elements of row i of node->cmap, then
2224 * -max_i <= c_x_i <= max_i
2228 * -max_i <= \sum_j a_j (c_z_j^+ - c_z_j^-) <= max_i
2232 * -\sum_j a_j (c_z_j^+ - c_z_j^-) + max_i >= 0
2233 * \sum_j a_j (c_z_j^+ - c_z_j^-) + max_i >= 0
2235 static isl_stat
node_add_coefficient_constraints(isl_ctx
*ctx
,
2236 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
, int max
)
2242 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2244 for (j
= 0; j
< node
->nparam
; ++j
) {
2250 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2252 return isl_stat_error
;
2253 dim
= 1 + node
->start
+ 1 + j
;
2254 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2255 isl_int_set_si(graph
->lp
->ineq
[k
][dim
], -1);
2256 isl_int_set_si(graph
->lp
->ineq
[k
][0], max
);
2259 ineq
= isl_vec_alloc(ctx
, 1 + total
);
2260 ineq
= isl_vec_clr(ineq
);
2262 return isl_stat_error
;
2263 for (i
= 0; i
< node
->nvar
; ++i
) {
2264 int pos
= 1 + node_var_coef_offset(node
);
2266 if (isl_int_is_neg(node
->max
->el
[i
]))
2269 for (j
= 0; j
< node
->nvar
; ++j
) {
2270 isl_int_set(ineq
->el
[pos
+ 2 * j
],
2271 node
->cmap
->row
[i
][j
]);
2272 isl_int_neg(ineq
->el
[pos
+ 2 * j
+ 1],
2273 node
->cmap
->row
[i
][j
]);
2275 isl_int_set(ineq
->el
[0], node
->max
->el
[i
]);
2277 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2280 isl_seq_cpy(graph
->lp
->ineq
[k
], ineq
->el
, 1 + total
);
2282 isl_seq_neg(ineq
->el
+ pos
, ineq
->el
+ pos
, 2 * node
->nvar
);
2283 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2286 isl_seq_cpy(graph
->lp
->ineq
[k
], ineq
->el
, 1 + total
);
2293 return isl_stat_error
;
2296 /* Add constraints that bound the values of the variable and parameter
2297 * coefficients of the schedule.
2299 * The maximal value of the coefficients is defined by the option
2300 * 'schedule_max_coefficient' and the entries in node->max.
2301 * These latter entries are only set if either the schedule_max_coefficient
2302 * option or the schedule_treat_coalescing option is set.
2304 static isl_stat
add_bound_coefficient_constraints(isl_ctx
*ctx
,
2305 struct isl_sched_graph
*graph
)
2310 max
= isl_options_get_schedule_max_coefficient(ctx
);
2312 if (max
== -1 && !isl_options_get_schedule_treat_coalescing(ctx
))
2315 for (i
= 0; i
< graph
->n
; ++i
) {
2316 struct isl_sched_node
*node
= &graph
->node
[i
];
2318 if (node_add_coefficient_constraints(ctx
, graph
, node
, max
) < 0)
2319 return isl_stat_error
;
2325 /* Add a constraint to graph->lp that equates the value at position
2326 * "sum_pos" to the sum of the "n" values starting at "first".
2328 static isl_stat
add_sum_constraint(struct isl_sched_graph
*graph
,
2329 int sum_pos
, int first
, int n
)
2334 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2336 k
= isl_basic_set_alloc_equality(graph
->lp
);
2338 return isl_stat_error
;
2339 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2340 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2341 for (i
= 0; i
< n
; ++i
)
2342 isl_int_set_si(graph
->lp
->eq
[k
][1 + first
+ i
], 1);
2347 /* Add a constraint to graph->lp that equates the value at position
2348 * "sum_pos" to the sum of the parameter coefficients of all nodes.
2350 * Within each node, the coefficients have the following order:
2352 * - c_i_n (if parametric)
2353 * - positive and negative parts of c_i_x
2355 static isl_stat
add_param_sum_constraint(struct isl_sched_graph
*graph
,
2361 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2363 k
= isl_basic_set_alloc_equality(graph
->lp
);
2365 return isl_stat_error
;
2366 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2367 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2368 for (i
= 0; i
< graph
->n
; ++i
) {
2369 int pos
= 1 + graph
->node
[i
].start
+ 1;
2371 for (j
= 0; j
< graph
->node
[i
].nparam
; ++j
)
2372 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2378 /* Add a constraint to graph->lp that equates the value at position
2379 * "sum_pos" to the sum of the variable coefficients of all nodes.
2381 * Within each node, the coefficients have the following order:
2383 * - c_i_n (if parametric)
2384 * - positive and negative parts of c_i_x
2386 static isl_stat
add_var_sum_constraint(struct isl_sched_graph
*graph
,
2392 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2394 k
= isl_basic_set_alloc_equality(graph
->lp
);
2396 return isl_stat_error
;
2397 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2398 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2399 for (i
= 0; i
< graph
->n
; ++i
) {
2400 struct isl_sched_node
*node
= &graph
->node
[i
];
2401 int pos
= 1 + node_var_coef_offset(node
);
2403 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
2404 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2410 /* Construct an ILP problem for finding schedule coefficients
2411 * that result in non-negative, but small dependence distances
2412 * over all dependences.
2413 * In particular, the dependence distances over proximity edges
2414 * are bounded by m_0 + m_n n and we compute schedule coefficients
2415 * with small values (preferably zero) of m_n and m_0.
2417 * All variables of the ILP are non-negative. The actual coefficients
2418 * may be negative, so each coefficient is represented as the difference
2419 * of two non-negative variables. The negative part always appears
2420 * immediately before the positive part.
2421 * Other than that, the variables have the following order
2423 * - sum of positive and negative parts of m_n coefficients
2425 * - sum of all c_n coefficients
2426 * (unconstrained when computing non-parametric schedules)
2427 * - sum of positive and negative parts of all c_x coefficients
2428 * - positive and negative parts of m_n coefficients
2431 * - c_i_n (if parametric)
2432 * - positive and negative parts of c_i_x
2434 * The c_i_x are not represented directly, but through the columns of
2435 * node->cmap. That is, the computed values are for variable t_i_x
2436 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
2438 * The constraints are those from the edges plus two or three equalities
2439 * to express the sums.
2441 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2442 * Otherwise, we ignore them.
2444 static isl_stat
setup_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
2445 int use_coincidence
)
2455 parametric
= ctx
->opt
->schedule_parametric
;
2456 nparam
= isl_space_dim(graph
->node
[0].space
, isl_dim_param
);
2458 total
= param_pos
+ 2 * nparam
;
2459 for (i
= 0; i
< graph
->n
; ++i
) {
2460 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
2461 if (node_update_cmap(node
) < 0)
2462 return isl_stat_error
;
2463 node
->start
= total
;
2464 total
+= 1 + node
->nparam
+ 2 * node
->nvar
;
2467 if (count_constraints(graph
, &n_eq
, &n_ineq
, use_coincidence
) < 0)
2468 return isl_stat_error
;
2469 if (count_bound_constant_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2470 return isl_stat_error
;
2471 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2472 return isl_stat_error
;
2474 space
= isl_space_set_alloc(ctx
, 0, total
);
2475 isl_basic_set_free(graph
->lp
);
2476 n_eq
+= 2 + parametric
;
2478 graph
->lp
= isl_basic_set_alloc_space(space
, 0, n_eq
, n_ineq
);
2480 if (add_sum_constraint(graph
, 0, param_pos
, 2 * nparam
) < 0)
2481 return isl_stat_error
;
2482 if (parametric
&& add_param_sum_constraint(graph
, 2) < 0)
2483 return isl_stat_error
;
2484 if (add_var_sum_constraint(graph
, 3) < 0)
2485 return isl_stat_error
;
2486 if (add_bound_constant_constraints(ctx
, graph
) < 0)
2487 return isl_stat_error
;
2488 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
2489 return isl_stat_error
;
2490 if (add_all_validity_constraints(graph
, use_coincidence
) < 0)
2491 return isl_stat_error
;
2492 if (add_all_proximity_constraints(graph
, use_coincidence
) < 0)
2493 return isl_stat_error
;
2498 /* Analyze the conflicting constraint found by
2499 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2500 * constraint of one of the edges between distinct nodes, living, moreover
2501 * in distinct SCCs, then record the source and sink SCC as this may
2502 * be a good place to cut between SCCs.
2504 static int check_conflict(int con
, void *user
)
2507 struct isl_sched_graph
*graph
= user
;
2509 if (graph
->src_scc
>= 0)
2512 con
-= graph
->lp
->n_eq
;
2514 if (con
>= graph
->lp
->n_ineq
)
2517 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2518 if (!is_validity(&graph
->edge
[i
]))
2520 if (graph
->edge
[i
].src
== graph
->edge
[i
].dst
)
2522 if (graph
->edge
[i
].src
->scc
== graph
->edge
[i
].dst
->scc
)
2524 if (graph
->edge
[i
].start
> con
)
2526 if (graph
->edge
[i
].end
<= con
)
2528 graph
->src_scc
= graph
->edge
[i
].src
->scc
;
2529 graph
->dst_scc
= graph
->edge
[i
].dst
->scc
;
2535 /* Check whether the next schedule row of the given node needs to be
2536 * non-trivial. Lower-dimensional domains may have some trivial rows,
2537 * but as soon as the number of remaining required non-trivial rows
2538 * is as large as the number or remaining rows to be computed,
2539 * all remaining rows need to be non-trivial.
2541 static int needs_row(struct isl_sched_graph
*graph
, struct isl_sched_node
*node
)
2543 return node
->nvar
- node
->rank
>= graph
->maxvar
- graph
->n_row
;
2546 /* Solve the ILP problem constructed in setup_lp.
2547 * For each node such that all the remaining rows of its schedule
2548 * need to be non-trivial, we construct a non-triviality region.
2549 * This region imposes that the next row is independent of previous rows.
2550 * In particular the coefficients c_i_x are represented by t_i_x
2551 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
2552 * its first columns span the rows of the previously computed part
2553 * of the schedule. The non-triviality region enforces that at least
2554 * one of the remaining components of t_i_x is non-zero, i.e.,
2555 * that the new schedule row depends on at least one of the remaining
2558 static __isl_give isl_vec
*solve_lp(struct isl_sched_graph
*graph
)
2564 for (i
= 0; i
< graph
->n
; ++i
) {
2565 struct isl_sched_node
*node
= &graph
->node
[i
];
2566 int skip
= node
->rank
;
2567 graph
->region
[i
].pos
= node_var_coef_offset(node
) + 2 * skip
;
2568 if (needs_row(graph
, node
))
2569 graph
->region
[i
].len
= 2 * (node
->nvar
- skip
);
2571 graph
->region
[i
].len
= 0;
2573 lp
= isl_basic_set_copy(graph
->lp
);
2574 sol
= isl_tab_basic_set_non_trivial_lexmin(lp
, 2, graph
->n
,
2575 graph
->region
, &check_conflict
, graph
);
2579 /* Extract the coefficients for the variables of "node" from "sol".
2581 * Within each node, the coefficients have the following order:
2583 * - c_i_n (if parametric)
2584 * - positive and negative parts of c_i_x
2586 * The c_i_x^- appear before their c_i_x^+ counterpart.
2588 * Return c_i_x = c_i_x^+ - c_i_x^-
2590 static __isl_give isl_vec
*extract_var_coef(struct isl_sched_node
*node
,
2591 __isl_keep isl_vec
*sol
)
2599 csol
= isl_vec_alloc(isl_vec_get_ctx(sol
), node
->nvar
);
2603 pos
= 1 + node_var_coef_offset(node
);
2604 for (i
= 0; i
< node
->nvar
; ++i
)
2605 isl_int_sub(csol
->el
[i
],
2606 sol
->el
[pos
+ 2 * i
+ 1], sol
->el
[pos
+ 2 * i
]);
2611 /* Update the schedules of all nodes based on the given solution
2612 * of the LP problem.
2613 * The new row is added to the current band.
2614 * All possibly negative coefficients are encoded as a difference
2615 * of two non-negative variables, so we need to perform the subtraction
2616 * here. Moreover, if use_cmap is set, then the solution does
2617 * not refer to the actual coefficients c_i_x, but instead to variables
2618 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2619 * In this case, we then also need to perform this multiplication
2620 * to obtain the values of c_i_x.
2622 * If coincident is set, then the caller guarantees that the new
2623 * row satisfies the coincidence constraints.
2625 static int update_schedule(struct isl_sched_graph
*graph
,
2626 __isl_take isl_vec
*sol
, int use_cmap
, int coincident
)
2629 isl_vec
*csol
= NULL
;
2634 isl_die(sol
->ctx
, isl_error_internal
,
2635 "no solution found", goto error
);
2636 if (graph
->n_total_row
>= graph
->max_row
)
2637 isl_die(sol
->ctx
, isl_error_internal
,
2638 "too many schedule rows", goto error
);
2640 for (i
= 0; i
< graph
->n
; ++i
) {
2641 struct isl_sched_node
*node
= &graph
->node
[i
];
2642 int pos
= node
->start
;
2643 int row
= isl_mat_rows(node
->sched
);
2646 csol
= extract_var_coef(node
, sol
);
2650 isl_map_free(node
->sched_map
);
2651 node
->sched_map
= NULL
;
2652 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
2655 for (j
= 0; j
< 1 + node
->nparam
; ++j
)
2656 node
->sched
= isl_mat_set_element(node
->sched
,
2657 row
, j
, sol
->el
[1 + pos
+ j
]);
2659 csol
= isl_mat_vec_product(isl_mat_copy(node
->cmap
),
2663 for (j
= 0; j
< node
->nvar
; ++j
)
2664 node
->sched
= isl_mat_set_element(node
->sched
,
2665 row
, 1 + node
->nparam
+ j
, csol
->el
[j
]);
2666 node
->coincident
[graph
->n_total_row
] = coincident
;
2672 graph
->n_total_row
++;
2681 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2682 * and return this isl_aff.
2684 static __isl_give isl_aff
*extract_schedule_row(__isl_take isl_local_space
*ls
,
2685 struct isl_sched_node
*node
, int row
)
2693 aff
= isl_aff_zero_on_domain(ls
);
2694 isl_mat_get_element(node
->sched
, row
, 0, &v
);
2695 aff
= isl_aff_set_constant(aff
, v
);
2696 for (j
= 0; j
< node
->nparam
; ++j
) {
2697 isl_mat_get_element(node
->sched
, row
, 1 + j
, &v
);
2698 aff
= isl_aff_set_coefficient(aff
, isl_dim_param
, j
, v
);
2700 for (j
= 0; j
< node
->nvar
; ++j
) {
2701 isl_mat_get_element(node
->sched
, row
, 1 + node
->nparam
+ j
, &v
);
2702 aff
= isl_aff_set_coefficient(aff
, isl_dim_in
, j
, v
);
2710 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
2711 * and return this multi_aff.
2713 * The result is defined over the uncompressed node domain.
2715 static __isl_give isl_multi_aff
*node_extract_partial_schedule_multi_aff(
2716 struct isl_sched_node
*node
, int first
, int n
)
2720 isl_local_space
*ls
;
2727 nrow
= isl_mat_rows(node
->sched
);
2728 if (node
->compressed
)
2729 space
= isl_multi_aff_get_domain_space(node
->decompress
);
2731 space
= isl_space_copy(node
->space
);
2732 ls
= isl_local_space_from_space(isl_space_copy(space
));
2733 space
= isl_space_from_domain(space
);
2734 space
= isl_space_add_dims(space
, isl_dim_out
, n
);
2735 ma
= isl_multi_aff_zero(space
);
2737 for (i
= first
; i
< first
+ n
; ++i
) {
2738 aff
= extract_schedule_row(isl_local_space_copy(ls
), node
, i
);
2739 ma
= isl_multi_aff_set_aff(ma
, i
- first
, aff
);
2742 isl_local_space_free(ls
);
2744 if (node
->compressed
)
2745 ma
= isl_multi_aff_pullback_multi_aff(ma
,
2746 isl_multi_aff_copy(node
->compress
));
2751 /* Convert node->sched into a multi_aff and return this multi_aff.
2753 * The result is defined over the uncompressed node domain.
2755 static __isl_give isl_multi_aff
*node_extract_schedule_multi_aff(
2756 struct isl_sched_node
*node
)
2760 nrow
= isl_mat_rows(node
->sched
);
2761 return node_extract_partial_schedule_multi_aff(node
, 0, nrow
);
2764 /* Convert node->sched into a map and return this map.
2766 * The result is cached in node->sched_map, which needs to be released
2767 * whenever node->sched is updated.
2768 * It is defined over the uncompressed node domain.
2770 static __isl_give isl_map
*node_extract_schedule(struct isl_sched_node
*node
)
2772 if (!node
->sched_map
) {
2775 ma
= node_extract_schedule_multi_aff(node
);
2776 node
->sched_map
= isl_map_from_multi_aff(ma
);
2779 return isl_map_copy(node
->sched_map
);
2782 /* Construct a map that can be used to update a dependence relation
2783 * based on the current schedule.
2784 * That is, construct a map expressing that source and sink
2785 * are executed within the same iteration of the current schedule.
2786 * This map can then be intersected with the dependence relation.
2787 * This is not the most efficient way, but this shouldn't be a critical
2790 static __isl_give isl_map
*specializer(struct isl_sched_node
*src
,
2791 struct isl_sched_node
*dst
)
2793 isl_map
*src_sched
, *dst_sched
;
2795 src_sched
= node_extract_schedule(src
);
2796 dst_sched
= node_extract_schedule(dst
);
2797 return isl_map_apply_range(src_sched
, isl_map_reverse(dst_sched
));
2800 /* Intersect the domains of the nested relations in domain and range
2801 * of "umap" with "map".
2803 static __isl_give isl_union_map
*intersect_domains(
2804 __isl_take isl_union_map
*umap
, __isl_keep isl_map
*map
)
2806 isl_union_set
*uset
;
2808 umap
= isl_union_map_zip(umap
);
2809 uset
= isl_union_set_from_set(isl_map_wrap(isl_map_copy(map
)));
2810 umap
= isl_union_map_intersect_domain(umap
, uset
);
2811 umap
= isl_union_map_zip(umap
);
2815 /* Update the dependence relation of the given edge based
2816 * on the current schedule.
2817 * If the dependence is carried completely by the current schedule, then
2818 * it is removed from the edge_tables. It is kept in the list of edges
2819 * as otherwise all edge_tables would have to be recomputed.
2821 static int update_edge(struct isl_sched_graph
*graph
,
2822 struct isl_sched_edge
*edge
)
2827 id
= specializer(edge
->src
, edge
->dst
);
2828 edge
->map
= isl_map_intersect(edge
->map
, isl_map_copy(id
));
2832 if (edge
->tagged_condition
) {
2833 edge
->tagged_condition
=
2834 intersect_domains(edge
->tagged_condition
, id
);
2835 if (!edge
->tagged_condition
)
2838 if (edge
->tagged_validity
) {
2839 edge
->tagged_validity
=
2840 intersect_domains(edge
->tagged_validity
, id
);
2841 if (!edge
->tagged_validity
)
2845 empty
= isl_map_plain_is_empty(edge
->map
);
2849 graph_remove_edge(graph
, edge
);
2858 /* Does the domain of "umap" intersect "uset"?
2860 static int domain_intersects(__isl_keep isl_union_map
*umap
,
2861 __isl_keep isl_union_set
*uset
)
2865 umap
= isl_union_map_copy(umap
);
2866 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(uset
));
2867 empty
= isl_union_map_is_empty(umap
);
2868 isl_union_map_free(umap
);
2870 return empty
< 0 ? -1 : !empty
;
2873 /* Does the range of "umap" intersect "uset"?
2875 static int range_intersects(__isl_keep isl_union_map
*umap
,
2876 __isl_keep isl_union_set
*uset
)
2880 umap
= isl_union_map_copy(umap
);
2881 umap
= isl_union_map_intersect_range(umap
, isl_union_set_copy(uset
));
2882 empty
= isl_union_map_is_empty(umap
);
2883 isl_union_map_free(umap
);
2885 return empty
< 0 ? -1 : !empty
;
2888 /* Are the condition dependences of "edge" local with respect to
2889 * the current schedule?
2891 * That is, are domain and range of the condition dependences mapped
2892 * to the same point?
2894 * In other words, is the condition false?
2896 static int is_condition_false(struct isl_sched_edge
*edge
)
2898 isl_union_map
*umap
;
2899 isl_map
*map
, *sched
, *test
;
2902 empty
= isl_union_map_is_empty(edge
->tagged_condition
);
2903 if (empty
< 0 || empty
)
2906 umap
= isl_union_map_copy(edge
->tagged_condition
);
2907 umap
= isl_union_map_zip(umap
);
2908 umap
= isl_union_set_unwrap(isl_union_map_domain(umap
));
2909 map
= isl_map_from_union_map(umap
);
2911 sched
= node_extract_schedule(edge
->src
);
2912 map
= isl_map_apply_domain(map
, sched
);
2913 sched
= node_extract_schedule(edge
->dst
);
2914 map
= isl_map_apply_range(map
, sched
);
2916 test
= isl_map_identity(isl_map_get_space(map
));
2917 local
= isl_map_is_subset(map
, test
);
2924 /* For each conditional validity constraint that is adjacent
2925 * to a condition with domain in condition_source or range in condition_sink,
2926 * turn it into an unconditional validity constraint.
2928 static int unconditionalize_adjacent_validity(struct isl_sched_graph
*graph
,
2929 __isl_take isl_union_set
*condition_source
,
2930 __isl_take isl_union_set
*condition_sink
)
2934 condition_source
= isl_union_set_coalesce(condition_source
);
2935 condition_sink
= isl_union_set_coalesce(condition_sink
);
2937 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2939 isl_union_map
*validity
;
2941 if (!is_conditional_validity(&graph
->edge
[i
]))
2943 if (is_validity(&graph
->edge
[i
]))
2946 validity
= graph
->edge
[i
].tagged_validity
;
2947 adjacent
= domain_intersects(validity
, condition_sink
);
2948 if (adjacent
>= 0 && !adjacent
)
2949 adjacent
= range_intersects(validity
, condition_source
);
2955 set_validity(&graph
->edge
[i
]);
2958 isl_union_set_free(condition_source
);
2959 isl_union_set_free(condition_sink
);
2962 isl_union_set_free(condition_source
);
2963 isl_union_set_free(condition_sink
);
2967 /* Update the dependence relations of all edges based on the current schedule
2968 * and enforce conditional validity constraints that are adjacent
2969 * to satisfied condition constraints.
2971 * First check if any of the condition constraints are satisfied
2972 * (i.e., not local to the outer schedule) and keep track of
2973 * their domain and range.
2974 * Then update all dependence relations (which removes the non-local
2976 * Finally, if any condition constraints turned out to be satisfied,
2977 * then turn all adjacent conditional validity constraints into
2978 * unconditional validity constraints.
2980 static int update_edges(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2984 isl_union_set
*source
, *sink
;
2986 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
2987 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
2988 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2990 isl_union_set
*uset
;
2991 isl_union_map
*umap
;
2993 if (!is_condition(&graph
->edge
[i
]))
2995 if (is_local(&graph
->edge
[i
]))
2997 local
= is_condition_false(&graph
->edge
[i
]);
3005 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
3006 uset
= isl_union_map_domain(umap
);
3007 source
= isl_union_set_union(source
, uset
);
3009 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
3010 uset
= isl_union_map_range(umap
);
3011 sink
= isl_union_set_union(sink
, uset
);
3014 for (i
= graph
->n_edge
- 1; i
>= 0; --i
) {
3015 if (update_edge(graph
, &graph
->edge
[i
]) < 0)
3020 return unconditionalize_adjacent_validity(graph
, source
, sink
);
3022 isl_union_set_free(source
);
3023 isl_union_set_free(sink
);
3026 isl_union_set_free(source
);
3027 isl_union_set_free(sink
);
3031 static void next_band(struct isl_sched_graph
*graph
)
3033 graph
->band_start
= graph
->n_total_row
;
3036 /* Return the union of the universe domains of the nodes in "graph"
3037 * that satisfy "pred".
3039 static __isl_give isl_union_set
*isl_sched_graph_domain(isl_ctx
*ctx
,
3040 struct isl_sched_graph
*graph
,
3041 int (*pred
)(struct isl_sched_node
*node
, int data
), int data
)
3047 for (i
= 0; i
< graph
->n
; ++i
)
3048 if (pred(&graph
->node
[i
], data
))
3052 isl_die(ctx
, isl_error_internal
,
3053 "empty component", return NULL
);
3055 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
3056 dom
= isl_union_set_from_set(set
);
3058 for (i
= i
+ 1; i
< graph
->n
; ++i
) {
3059 if (!pred(&graph
->node
[i
], data
))
3061 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
3062 dom
= isl_union_set_union(dom
, isl_union_set_from_set(set
));
3068 /* Return a list of unions of universe domains, where each element
3069 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
3071 static __isl_give isl_union_set_list
*extract_sccs(isl_ctx
*ctx
,
3072 struct isl_sched_graph
*graph
)
3075 isl_union_set_list
*filters
;
3077 filters
= isl_union_set_list_alloc(ctx
, graph
->scc
);
3078 for (i
= 0; i
< graph
->scc
; ++i
) {
3081 dom
= isl_sched_graph_domain(ctx
, graph
, &node_scc_exactly
, i
);
3082 filters
= isl_union_set_list_add(filters
, dom
);
3088 /* Return a list of two unions of universe domains, one for the SCCs up
3089 * to and including graph->src_scc and another for the other SCCs.
3091 static __isl_give isl_union_set_list
*extract_split(isl_ctx
*ctx
,
3092 struct isl_sched_graph
*graph
)
3095 isl_union_set_list
*filters
;
3097 filters
= isl_union_set_list_alloc(ctx
, 2);
3098 dom
= isl_sched_graph_domain(ctx
, graph
,
3099 &node_scc_at_most
, graph
->src_scc
);
3100 filters
= isl_union_set_list_add(filters
, dom
);
3101 dom
= isl_sched_graph_domain(ctx
, graph
,
3102 &node_scc_at_least
, graph
->src_scc
+ 1);
3103 filters
= isl_union_set_list_add(filters
, dom
);
3108 /* Copy nodes that satisfy node_pred from the src dependence graph
3109 * to the dst dependence graph.
3111 static int copy_nodes(struct isl_sched_graph
*dst
, struct isl_sched_graph
*src
,
3112 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
3117 for (i
= 0; i
< src
->n
; ++i
) {
3120 if (!node_pred(&src
->node
[i
], data
))
3124 dst
->node
[j
].space
= isl_space_copy(src
->node
[i
].space
);
3125 dst
->node
[j
].compressed
= src
->node
[i
].compressed
;
3126 dst
->node
[j
].hull
= isl_set_copy(src
->node
[i
].hull
);
3127 dst
->node
[j
].compress
=
3128 isl_multi_aff_copy(src
->node
[i
].compress
);
3129 dst
->node
[j
].decompress
=
3130 isl_multi_aff_copy(src
->node
[i
].decompress
);
3131 dst
->node
[j
].nvar
= src
->node
[i
].nvar
;
3132 dst
->node
[j
].nparam
= src
->node
[i
].nparam
;
3133 dst
->node
[j
].sched
= isl_mat_copy(src
->node
[i
].sched
);
3134 dst
->node
[j
].sched_map
= isl_map_copy(src
->node
[i
].sched_map
);
3135 dst
->node
[j
].coincident
= src
->node
[i
].coincident
;
3136 dst
->node
[j
].sizes
= isl_multi_val_copy(src
->node
[i
].sizes
);
3137 dst
->node
[j
].max
= isl_vec_copy(src
->node
[i
].max
);
3140 if (!dst
->node
[j
].space
|| !dst
->node
[j
].sched
)
3142 if (dst
->node
[j
].compressed
&&
3143 (!dst
->node
[j
].hull
|| !dst
->node
[j
].compress
||
3144 !dst
->node
[j
].decompress
))
3151 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
3152 * to the dst dependence graph.
3153 * If the source or destination node of the edge is not in the destination
3154 * graph, then it must be a backward proximity edge and it should simply
3157 static int copy_edges(isl_ctx
*ctx
, struct isl_sched_graph
*dst
,
3158 struct isl_sched_graph
*src
,
3159 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
), int data
)
3164 for (i
= 0; i
< src
->n_edge
; ++i
) {
3165 struct isl_sched_edge
*edge
= &src
->edge
[i
];
3167 isl_union_map
*tagged_condition
;
3168 isl_union_map
*tagged_validity
;
3169 struct isl_sched_node
*dst_src
, *dst_dst
;
3171 if (!edge_pred(edge
, data
))
3174 if (isl_map_plain_is_empty(edge
->map
))
3177 dst_src
= graph_find_node(ctx
, dst
, edge
->src
->space
);
3178 dst_dst
= graph_find_node(ctx
, dst
, edge
->dst
->space
);
3179 if (!dst_src
|| !dst_dst
) {
3180 if (is_validity(edge
) || is_conditional_validity(edge
))
3181 isl_die(ctx
, isl_error_internal
,
3182 "backward (conditional) validity edge",
3187 map
= isl_map_copy(edge
->map
);
3188 tagged_condition
= isl_union_map_copy(edge
->tagged_condition
);
3189 tagged_validity
= isl_union_map_copy(edge
->tagged_validity
);
3191 dst
->edge
[dst
->n_edge
].src
= dst_src
;
3192 dst
->edge
[dst
->n_edge
].dst
= dst_dst
;
3193 dst
->edge
[dst
->n_edge
].map
= map
;
3194 dst
->edge
[dst
->n_edge
].tagged_condition
= tagged_condition
;
3195 dst
->edge
[dst
->n_edge
].tagged_validity
= tagged_validity
;
3196 dst
->edge
[dst
->n_edge
].types
= edge
->types
;
3199 if (edge
->tagged_condition
&& !tagged_condition
)
3201 if (edge
->tagged_validity
&& !tagged_validity
)
3204 if (graph_edge_tables_add(ctx
, dst
,
3205 &dst
->edge
[dst
->n_edge
- 1]) < 0)
3212 /* Compute the maximal number of variables over all nodes.
3213 * This is the maximal number of linearly independent schedule
3214 * rows that we need to compute.
3215 * Just in case we end up in a part of the dependence graph
3216 * with only lower-dimensional domains, we make sure we will
3217 * compute the required amount of extra linearly independent rows.
3219 static int compute_maxvar(struct isl_sched_graph
*graph
)
3224 for (i
= 0; i
< graph
->n
; ++i
) {
3225 struct isl_sched_node
*node
= &graph
->node
[i
];
3228 if (node_update_cmap(node
) < 0)
3230 nvar
= node
->nvar
+ graph
->n_row
- node
->rank
;
3231 if (nvar
> graph
->maxvar
)
3232 graph
->maxvar
= nvar
;
3238 /* Extract the subgraph of "graph" that consists of the node satisfying
3239 * "node_pred" and the edges satisfying "edge_pred" and store
3240 * the result in "sub".
3242 static int extract_sub_graph(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
3243 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
3244 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
3245 int data
, struct isl_sched_graph
*sub
)
3247 int i
, n
= 0, n_edge
= 0;
3250 for (i
= 0; i
< graph
->n
; ++i
)
3251 if (node_pred(&graph
->node
[i
], data
))
3253 for (i
= 0; i
< graph
->n_edge
; ++i
)
3254 if (edge_pred(&graph
->edge
[i
], data
))
3256 if (graph_alloc(ctx
, sub
, n
, n_edge
) < 0)
3258 if (copy_nodes(sub
, graph
, node_pred
, data
) < 0)
3260 if (graph_init_table(ctx
, sub
) < 0)
3262 for (t
= 0; t
<= isl_edge_last
; ++t
)
3263 sub
->max_edge
[t
] = graph
->max_edge
[t
];
3264 if (graph_init_edge_tables(ctx
, sub
) < 0)
3266 if (copy_edges(ctx
, sub
, graph
, edge_pred
, data
) < 0)
3268 sub
->n_row
= graph
->n_row
;
3269 sub
->max_row
= graph
->max_row
;
3270 sub
->n_total_row
= graph
->n_total_row
;
3271 sub
->band_start
= graph
->band_start
;
3276 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
3277 struct isl_sched_graph
*graph
);
3278 static __isl_give isl_schedule_node
*compute_schedule_wcc(
3279 isl_schedule_node
*node
, struct isl_sched_graph
*graph
);
3281 /* Compute a schedule for a subgraph of "graph". In particular, for
3282 * the graph composed of nodes that satisfy node_pred and edges that
3283 * that satisfy edge_pred.
3284 * If the subgraph is known to consist of a single component, then wcc should
3285 * be set and then we call compute_schedule_wcc on the constructed subgraph.
3286 * Otherwise, we call compute_schedule, which will check whether the subgraph
3289 * The schedule is inserted at "node" and the updated schedule node
3292 static __isl_give isl_schedule_node
*compute_sub_schedule(
3293 __isl_take isl_schedule_node
*node
, isl_ctx
*ctx
,
3294 struct isl_sched_graph
*graph
,
3295 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
3296 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
3299 struct isl_sched_graph split
= { 0 };
3301 if (extract_sub_graph(ctx
, graph
, node_pred
, edge_pred
, data
,
3306 node
= compute_schedule_wcc(node
, &split
);
3308 node
= compute_schedule(node
, &split
);
3310 graph_free(ctx
, &split
);
3313 graph_free(ctx
, &split
);
3314 return isl_schedule_node_free(node
);
3317 static int edge_scc_exactly(struct isl_sched_edge
*edge
, int scc
)
3319 return edge
->src
->scc
== scc
&& edge
->dst
->scc
== scc
;
3322 static int edge_dst_scc_at_most(struct isl_sched_edge
*edge
, int scc
)
3324 return edge
->dst
->scc
<= scc
;
3327 static int edge_src_scc_at_least(struct isl_sched_edge
*edge
, int scc
)
3329 return edge
->src
->scc
>= scc
;
3332 /* Reset the current band by dropping all its schedule rows.
3334 static int reset_band(struct isl_sched_graph
*graph
)
3339 drop
= graph
->n_total_row
- graph
->band_start
;
3340 graph
->n_total_row
-= drop
;
3341 graph
->n_row
-= drop
;
3343 for (i
= 0; i
< graph
->n
; ++i
) {
3344 struct isl_sched_node
*node
= &graph
->node
[i
];
3346 isl_map_free(node
->sched_map
);
3347 node
->sched_map
= NULL
;
3349 node
->sched
= isl_mat_drop_rows(node
->sched
,
3350 graph
->band_start
, drop
);
3359 /* Split the current graph into two parts and compute a schedule for each
3360 * part individually. In particular, one part consists of all SCCs up
3361 * to and including graph->src_scc, while the other part contains the other
3362 * SCCs. The split is enforced by a sequence node inserted at position "node"
3363 * in the schedule tree. Return the updated schedule node.
3364 * If either of these two parts consists of a sequence, then it is spliced
3365 * into the sequence containing the two parts.
3367 * The current band is reset. It would be possible to reuse
3368 * the previously computed rows as the first rows in the next
3369 * band, but recomputing them may result in better rows as we are looking
3370 * at a smaller part of the dependence graph.
3372 static __isl_give isl_schedule_node
*compute_split_schedule(
3373 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3377 isl_union_set_list
*filters
;
3382 if (reset_band(graph
) < 0)
3383 return isl_schedule_node_free(node
);
3387 ctx
= isl_schedule_node_get_ctx(node
);
3388 filters
= extract_split(ctx
, graph
);
3389 node
= isl_schedule_node_insert_sequence(node
, filters
);
3390 node
= isl_schedule_node_child(node
, 1);
3391 node
= isl_schedule_node_child(node
, 0);
3393 node
= compute_sub_schedule(node
, ctx
, graph
,
3394 &node_scc_at_least
, &edge_src_scc_at_least
,
3395 graph
->src_scc
+ 1, 0);
3396 is_seq
= isl_schedule_node_get_type(node
) == isl_schedule_node_sequence
;
3397 node
= isl_schedule_node_parent(node
);
3398 node
= isl_schedule_node_parent(node
);
3400 node
= isl_schedule_node_sequence_splice_child(node
, 1);
3401 node
= isl_schedule_node_child(node
, 0);
3402 node
= isl_schedule_node_child(node
, 0);
3403 node
= compute_sub_schedule(node
, ctx
, graph
,
3404 &node_scc_at_most
, &edge_dst_scc_at_most
,
3406 is_seq
= isl_schedule_node_get_type(node
) == isl_schedule_node_sequence
;
3407 node
= isl_schedule_node_parent(node
);
3408 node
= isl_schedule_node_parent(node
);
3410 node
= isl_schedule_node_sequence_splice_child(node
, 0);
3415 /* Insert a band node at position "node" in the schedule tree corresponding
3416 * to the current band in "graph". Mark the band node permutable
3417 * if "permutable" is set.
3418 * The partial schedules and the coincidence property are extracted
3419 * from the graph nodes.
3420 * Return the updated schedule node.
3422 static __isl_give isl_schedule_node
*insert_current_band(
3423 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
3429 isl_multi_pw_aff
*mpa
;
3430 isl_multi_union_pw_aff
*mupa
;
3436 isl_die(isl_schedule_node_get_ctx(node
), isl_error_internal
,
3437 "graph should have at least one node",
3438 return isl_schedule_node_free(node
));
3440 start
= graph
->band_start
;
3441 end
= graph
->n_total_row
;
3444 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[0], start
, n
);
3445 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3446 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3448 for (i
= 1; i
< graph
->n
; ++i
) {
3449 isl_multi_union_pw_aff
*mupa_i
;
3451 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[i
],
3453 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3454 mupa_i
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3455 mupa
= isl_multi_union_pw_aff_union_add(mupa
, mupa_i
);
3457 node
= isl_schedule_node_insert_partial_schedule(node
, mupa
);
3459 for (i
= 0; i
< n
; ++i
)
3460 node
= isl_schedule_node_band_member_set_coincident(node
, i
,
3461 graph
->node
[0].coincident
[start
+ i
]);
3462 node
= isl_schedule_node_band_set_permutable(node
, permutable
);
3467 /* Update the dependence relations based on the current schedule,
3468 * add the current band to "node" and then continue with the computation
3470 * Return the updated schedule node.
3472 static __isl_give isl_schedule_node
*compute_next_band(
3473 __isl_take isl_schedule_node
*node
,
3474 struct isl_sched_graph
*graph
, int permutable
)
3481 ctx
= isl_schedule_node_get_ctx(node
);
3482 if (update_edges(ctx
, graph
) < 0)
3483 return isl_schedule_node_free(node
);
3484 node
= insert_current_band(node
, graph
, permutable
);
3487 node
= isl_schedule_node_child(node
, 0);
3488 node
= compute_schedule(node
, graph
);
3489 node
= isl_schedule_node_parent(node
);
3494 /* Add constraints to graph->lp that force the dependence "map" (which
3495 * is part of the dependence relation of "edge")
3496 * to be respected and attempt to carry it, where the edge is one from
3497 * a node j to itself. "pos" is the sequence number of the given map.
3498 * That is, add constraints that enforce
3500 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3501 * = c_j_x (y - x) >= e_i
3503 * for each (x,y) in R.
3504 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3505 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3506 * with each coefficient in c_j_x represented as a pair of non-negative
3509 static int add_intra_constraints(struct isl_sched_graph
*graph
,
3510 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
3513 isl_ctx
*ctx
= isl_map_get_ctx(map
);
3514 isl_dim_map
*dim_map
;
3515 isl_basic_set
*coef
;
3516 struct isl_sched_node
*node
= edge
->src
;
3518 coef
= intra_coefficients(graph
, node
, map
);
3522 offset
= coef_var_offset(coef
);
3523 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, 1);
3524 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3525 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
3526 coef
->n_eq
, coef
->n_ineq
);
3527 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
3533 /* Add constraints to graph->lp that force the dependence "map" (which
3534 * is part of the dependence relation of "edge")
3535 * to be respected and attempt to carry it, where the edge is one from
3536 * node j to node k. "pos" is the sequence number of the given map.
3537 * That is, add constraints that enforce
3539 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3541 * for each (x,y) in R.
3542 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3543 * of valid constraints for R and then plug in
3544 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3545 * with each coefficient (except e_i, c_*_0 and c_*_n)
3546 * represented as a pair of non-negative coefficients.
3548 static int add_inter_constraints(struct isl_sched_graph
*graph
,
3549 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
3552 isl_ctx
*ctx
= isl_map_get_ctx(map
);
3553 isl_dim_map
*dim_map
;
3554 isl_basic_set
*coef
;
3555 struct isl_sched_node
*src
= edge
->src
;
3556 struct isl_sched_node
*dst
= edge
->dst
;
3558 coef
= inter_coefficients(graph
, edge
, map
);
3562 offset
= coef_var_offset(coef
);
3563 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, 1);
3564 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3565 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
3566 coef
->n_eq
, coef
->n_ineq
);
3567 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
3573 /* Add constraints to graph->lp that force all (conditional) validity
3574 * dependences to be respected and attempt to carry them.
3576 static int add_all_constraints(struct isl_sched_graph
*graph
)
3582 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3583 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
3585 if (!is_any_validity(edge
))
3588 for (j
= 0; j
< edge
->map
->n
; ++j
) {
3589 isl_basic_map
*bmap
;
3592 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
3593 map
= isl_map_from_basic_map(bmap
);
3595 if (edge
->src
== edge
->dst
&&
3596 add_intra_constraints(graph
, edge
, map
, pos
) < 0)
3598 if (edge
->src
!= edge
->dst
&&
3599 add_inter_constraints(graph
, edge
, map
, pos
) < 0)
3608 /* Count the number of equality and inequality constraints
3609 * that will be added to the carry_lp problem.
3610 * We count each edge exactly once.
3612 static int count_all_constraints(struct isl_sched_graph
*graph
,
3613 int *n_eq
, int *n_ineq
)
3617 *n_eq
= *n_ineq
= 0;
3618 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3619 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
3621 if (!is_any_validity(edge
))
3624 for (j
= 0; j
< edge
->map
->n
; ++j
) {
3625 isl_basic_map
*bmap
;
3628 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
3629 map
= isl_map_from_basic_map(bmap
);
3631 if (count_map_constraints(graph
, edge
, map
,
3632 n_eq
, n_ineq
, 1, 0) < 0)
3640 /* Return the total number of (validity) edges that carry_dependences will
3643 static int count_carry_edges(struct isl_sched_graph
*graph
)
3649 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3650 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
3652 if (!is_any_validity(edge
))
3655 n_edge
+= isl_map_n_basic_map(edge
->map
);
3661 /* Construct an LP problem for finding schedule coefficients
3662 * such that the schedule carries as many validity dependences as possible.
3663 * In particular, for each dependence i, we bound the dependence distance
3664 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3665 * of all e_i's. Dependences with e_i = 0 in the solution are simply
3666 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3667 * Note that if the dependence relation is a union of basic maps,
3668 * then we have to consider each basic map individually as it may only
3669 * be possible to carry the dependences expressed by some of those
3670 * basic maps and not all of them.
3671 * Below, we consider each of those basic maps as a separate "edge".
3673 * All variables of the LP are non-negative. The actual coefficients
3674 * may be negative, so each coefficient is represented as the difference
3675 * of two non-negative variables. The negative part always appears
3676 * immediately before the positive part.
3677 * Other than that, the variables have the following order
3679 * - sum of (1 - e_i) over all edges
3680 * - sum of all c_n coefficients
3681 * (unconstrained when computing non-parametric schedules)
3682 * - sum of positive and negative parts of all c_x coefficients
3687 * - c_i_n (if parametric)
3688 * - positive and negative parts of c_i_x
3690 * The constraints are those from the (validity) edges plus three equalities
3691 * to express the sums and n_edge inequalities to express e_i <= 1.
3693 static isl_stat
setup_carry_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3702 n_edge
= count_carry_edges(graph
);
3705 for (i
= 0; i
< graph
->n
; ++i
) {
3706 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
3707 node
->start
= total
;
3708 total
+= 1 + node
->nparam
+ 2 * node
->nvar
;
3711 if (count_all_constraints(graph
, &n_eq
, &n_ineq
) < 0)
3712 return isl_stat_error
;
3714 dim
= isl_space_set_alloc(ctx
, 0, total
);
3715 isl_basic_set_free(graph
->lp
);
3718 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
3719 graph
->lp
= isl_basic_set_set_rational(graph
->lp
);
3721 k
= isl_basic_set_alloc_equality(graph
->lp
);
3723 return isl_stat_error
;
3724 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
3725 isl_int_set_si(graph
->lp
->eq
[k
][0], -n_edge
);
3726 isl_int_set_si(graph
->lp
->eq
[k
][1], 1);
3727 for (i
= 0; i
< n_edge
; ++i
)
3728 isl_int_set_si(graph
->lp
->eq
[k
][4 + i
], 1);
3730 if (add_param_sum_constraint(graph
, 1) < 0)
3731 return isl_stat_error
;
3732 if (add_var_sum_constraint(graph
, 2) < 0)
3733 return isl_stat_error
;
3735 for (i
= 0; i
< n_edge
; ++i
) {
3736 k
= isl_basic_set_alloc_inequality(graph
->lp
);
3738 return isl_stat_error
;
3739 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
3740 isl_int_set_si(graph
->lp
->ineq
[k
][4 + i
], -1);
3741 isl_int_set_si(graph
->lp
->ineq
[k
][0], 1);
3744 if (add_all_constraints(graph
) < 0)
3745 return isl_stat_error
;
3750 static __isl_give isl_schedule_node
*compute_component_schedule(
3751 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
3754 /* Comparison function for sorting the statements based on
3755 * the corresponding value in "r".
3757 static int smaller_value(const void *a
, const void *b
, void *data
)
3763 return isl_int_cmp(r
->el
[*i1
], r
->el
[*i2
]);
3766 /* If the schedule_split_scaled option is set and if the linear
3767 * parts of the scheduling rows for all nodes in the graphs have
3768 * a non-trivial common divisor, then split off the remainder of the
3769 * constant term modulo this common divisor from the linear part.
3770 * Otherwise, insert a band node directly and continue with
3771 * the construction of the schedule.
3773 * If a non-trivial common divisor is found, then
3774 * the linear part is reduced and the remainder is enforced
3775 * by a sequence node with the children placed in the order
3776 * of this remainder.
3777 * In particular, we assign an scc index based on the remainder and
3778 * then rely on compute_component_schedule to insert the sequence and
3779 * to continue the schedule construction on each part.
3781 static __isl_give isl_schedule_node
*split_scaled(
3782 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3795 ctx
= isl_schedule_node_get_ctx(node
);
3796 if (!ctx
->opt
->schedule_split_scaled
)
3797 return compute_next_band(node
, graph
, 0);
3799 return compute_next_band(node
, graph
, 0);
3802 isl_int_init(gcd_i
);
3804 isl_int_set_si(gcd
, 0);
3806 row
= isl_mat_rows(graph
->node
[0].sched
) - 1;
3808 for (i
= 0; i
< graph
->n
; ++i
) {
3809 struct isl_sched_node
*node
= &graph
->node
[i
];
3810 int cols
= isl_mat_cols(node
->sched
);
3812 isl_seq_gcd(node
->sched
->row
[row
] + 1, cols
- 1, &gcd_i
);
3813 isl_int_gcd(gcd
, gcd
, gcd_i
);
3816 isl_int_clear(gcd_i
);
3818 if (isl_int_cmp_si(gcd
, 1) <= 0) {
3820 return compute_next_band(node
, graph
, 0);
3823 r
= isl_vec_alloc(ctx
, graph
->n
);
3824 order
= isl_calloc_array(ctx
, int, graph
->n
);
3828 for (i
= 0; i
< graph
->n
; ++i
) {
3829 struct isl_sched_node
*node
= &graph
->node
[i
];
3832 isl_int_fdiv_r(r
->el
[i
], node
->sched
->row
[row
][0], gcd
);
3833 isl_int_fdiv_q(node
->sched
->row
[row
][0],
3834 node
->sched
->row
[row
][0], gcd
);
3835 isl_int_mul(node
->sched
->row
[row
][0],
3836 node
->sched
->row
[row
][0], gcd
);
3837 node
->sched
= isl_mat_scale_down_row(node
->sched
, row
, gcd
);
3842 if (isl_sort(order
, graph
->n
, sizeof(order
[0]), &smaller_value
, r
) < 0)
3846 for (i
= 0; i
< graph
->n
; ++i
) {
3847 if (i
> 0 && isl_int_ne(r
->el
[order
[i
- 1]], r
->el
[order
[i
]]))
3849 graph
->node
[order
[i
]].scc
= scc
;
3858 if (update_edges(ctx
, graph
) < 0)
3859 return isl_schedule_node_free(node
);
3860 node
= insert_current_band(node
, graph
, 0);
3863 node
= isl_schedule_node_child(node
, 0);
3864 node
= compute_component_schedule(node
, graph
, 0);
3865 node
= isl_schedule_node_parent(node
);
3872 return isl_schedule_node_free(node
);
3875 /* Is the schedule row "sol" trivial on node "node"?
3876 * That is, is the solution zero on the dimensions orthogonal to
3877 * the previously found solutions?
3878 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3880 * Each coefficient is represented as the difference between
3881 * two non-negative values in "sol". "sol" has been computed
3882 * in terms of the original iterators (i.e., without use of cmap).
3883 * We construct the schedule row s and write it as a linear
3884 * combination of (linear combinations of) previously computed schedule rows.
3885 * s = Q c or c = U s.
3886 * If the final entries of c are all zero, then the solution is trivial.
3888 static int is_trivial(struct isl_sched_node
*node
, __isl_keep isl_vec
*sol
)
3895 if (node
->nvar
== node
->rank
)
3898 node_sol
= extract_var_coef(node
, sol
);
3899 node_sol
= isl_mat_vec_product(isl_mat_copy(node
->cinv
), node_sol
);
3903 trivial
= isl_seq_first_non_zero(node_sol
->el
+ node
->rank
,
3904 node
->nvar
- node
->rank
) == -1;
3906 isl_vec_free(node_sol
);
3911 /* Is the schedule row "sol" trivial on any node where it should
3913 * "sol" has been computed in terms of the original iterators
3914 * (i.e., without use of cmap).
3915 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3917 static int is_any_trivial(struct isl_sched_graph
*graph
,
3918 __isl_keep isl_vec
*sol
)
3922 for (i
= 0; i
< graph
->n
; ++i
) {
3923 struct isl_sched_node
*node
= &graph
->node
[i
];
3926 if (!needs_row(graph
, node
))
3928 trivial
= is_trivial(node
, sol
);
3929 if (trivial
< 0 || trivial
)
3936 /* Does the schedule represented by "sol" perform loop coalescing on "node"?
3937 * If so, return the position of the coalesced dimension.
3938 * Otherwise, return node->nvar or -1 on error.
3940 * In particular, look for pairs of coefficients c_i and c_j such that
3941 * |c_j/c_i| >= size_i, i.e., |c_j| >= |c_i * size_i|.
3942 * If any such pair is found, then return i.
3943 * If size_i is infinity, then no check on c_i needs to be performed.
3945 static int find_node_coalescing(struct isl_sched_node
*node
,
3946 __isl_keep isl_vec
*sol
)
3952 if (node
->nvar
<= 1)
3955 csol
= extract_var_coef(node
, sol
);
3959 for (i
= 0; i
< node
->nvar
; ++i
) {
3962 if (isl_int_is_zero(csol
->el
[i
]))
3964 v
= isl_multi_val_get_val(node
->sizes
, i
);
3967 if (!isl_val_is_int(v
)) {
3971 isl_int_mul(max
, v
->n
, csol
->el
[i
]);
3974 for (j
= 0; j
< node
->nvar
; ++j
) {
3977 if (isl_int_abs_ge(csol
->el
[j
], max
))
3993 /* Force the schedule coefficient at position "pos" of "node" to be zero
3995 * The coefficient is encoded as the difference between two non-negative
3996 * variables. Force these two variables to have the same value.
3998 static __isl_give isl_tab_lexmin
*zero_out_node_coef(
3999 __isl_take isl_tab_lexmin
*tl
, struct isl_sched_node
*node
, int pos
)
4005 ctx
= isl_space_get_ctx(node
->space
);
4006 dim
= isl_tab_lexmin_dim(tl
);
4008 return isl_tab_lexmin_free(tl
);
4009 eq
= isl_vec_alloc(ctx
, 1 + dim
);
4010 eq
= isl_vec_clr(eq
);
4012 return isl_tab_lexmin_free(tl
);
4014 pos
= 1 + node_var_coef_offset(node
) + 2 * pos
;
4015 isl_int_set_si(eq
->el
[pos
], 1);
4016 isl_int_set_si(eq
->el
[pos
+ 1], -1);
4017 tl
= isl_tab_lexmin_add_eq(tl
, eq
->el
);
4023 /* Return the lexicographically smallest rational point in the basic set
4024 * from which "tl" was constructed, double checking that this input set
4027 static __isl_give isl_vec
*non_empty_solution(__isl_keep isl_tab_lexmin
*tl
)
4031 sol
= isl_tab_lexmin_get_solution(tl
);
4035 isl_die(isl_vec_get_ctx(sol
), isl_error_internal
,
4036 "error in schedule construction",
4037 return isl_vec_free(sol
));
4041 /* Does the solution "sol" of the LP problem constructed by setup_carry_lp
4042 * carry any of the "n_edge" groups of dependences?
4043 * The value in the first position is the sum of (1 - e_i) over all "n_edge"
4044 * edges, with 0 <= e_i <= 1 equal to 1 when the dependences represented
4045 * by the edge are carried by the solution.
4046 * If the sum of the (1 - e_i) is smaller than "n_edge" then at least
4047 * one of those is carried.
4049 * Note that despite the fact that the problem is solved using a rational
4050 * solver, the solution is guaranteed to be integral.
4051 * Specifically, the dependence distance lower bounds e_i (and therefore
4052 * also their sum) are integers. See Lemma 5 of [1].
4054 * Any potential denominator of the sum is cleared by this function.
4055 * The denominator is not relevant for any of the other elements
4058 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4059 * Problem, Part II: Multi-Dimensional Time.
4060 * In Intl. Journal of Parallel Programming, 1992.
4062 static int carries_dependences(__isl_keep isl_vec
*sol
, int n_edge
)
4064 isl_int_divexact(sol
->el
[1], sol
->el
[1], sol
->el
[0]);
4065 isl_int_set_si(sol
->el
[0], 1);
4066 return isl_int_cmp_si(sol
->el
[1], n_edge
) < 0;
4069 /* Return the lexicographically smallest rational point in "lp",
4070 * assuming that all variables are non-negative and performing some
4071 * additional sanity checks.
4072 * In particular, "lp" should not be empty by construction.
4073 * Double check that this is the case.
4074 * Also, check that dependences are carried for at least one of
4075 * the "n_edge" edges.
4077 * If the computed schedule performs loop coalescing on a given node,
4078 * i.e., if it is of the form
4080 * c_i i + c_j j + ...
4082 * with |c_j/c_i| >= size_i, then force the coefficient c_i to be zero
4083 * to cut out this solution. Repeat this process until no more loop
4084 * coalescing occurs or until no more dependences can be carried.
4085 * In the latter case, revert to the previously computed solution.
4087 static __isl_give isl_vec
*non_neg_lexmin(struct isl_sched_graph
*graph
,
4088 __isl_take isl_basic_set
*lp
, int n_edge
)
4093 isl_vec
*sol
, *prev
= NULL
;
4094 int treat_coalescing
;
4098 ctx
= isl_basic_set_get_ctx(lp
);
4099 treat_coalescing
= isl_options_get_schedule_treat_coalescing(ctx
);
4100 tl
= isl_tab_lexmin_from_basic_set(lp
);
4103 sol
= non_empty_solution(tl
);
4107 if (!carries_dependences(sol
, n_edge
)) {
4109 isl_die(ctx
, isl_error_unknown
,
4110 "unable to carry dependences",
4116 prev
= isl_vec_free(prev
);
4117 if (!treat_coalescing
)
4119 for (i
= 0; i
< graph
->n
; ++i
) {
4120 struct isl_sched_node
*node
= &graph
->node
[i
];
4122 pos
= find_node_coalescing(node
, sol
);
4125 if (pos
< node
->nvar
)
4130 tl
= zero_out_node_coef(tl
, &graph
->node
[i
], pos
);
4132 } while (i
< graph
->n
);
4134 isl_tab_lexmin_free(tl
);
4138 isl_tab_lexmin_free(tl
);
4144 /* Construct a schedule row for each node such that as many validity dependences
4145 * as possible are carried and then continue with the next band.
4147 * If there are no validity dependences, then no dependence can be carried and
4148 * the procedure is guaranteed to fail. If there is more than one component,
4149 * then try computing a schedule on each component separately
4150 * to prevent or at least postpone this failure.
4152 * If the computed schedule row turns out to be trivial on one or
4153 * more nodes where it should not be trivial, then we throw it away
4154 * and try again on each component separately.
4156 * If there is only one component, then we accept the schedule row anyway,
4157 * but we do not consider it as a complete row and therefore do not
4158 * increment graph->n_row. Note that the ranks of the nodes that
4159 * do get a non-trivial schedule part will get updated regardless and
4160 * graph->maxvar is computed based on these ranks. The test for
4161 * whether more schedule rows are required in compute_schedule_wcc
4162 * is therefore not affected.
4164 * Insert a band corresponding to the schedule row at position "node"
4165 * of the schedule tree and continue with the construction of the schedule.
4166 * This insertion and the continued construction is performed by split_scaled
4167 * after optionally checking for non-trivial common divisors.
4169 static __isl_give isl_schedule_node
*carry_dependences(
4170 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4181 n_edge
= count_carry_edges(graph
);
4182 if (n_edge
== 0 && graph
->scc
> 1)
4183 return compute_component_schedule(node
, graph
, 1);
4185 ctx
= isl_schedule_node_get_ctx(node
);
4186 if (setup_carry_lp(ctx
, graph
) < 0)
4187 return isl_schedule_node_free(node
);
4189 lp
= isl_basic_set_copy(graph
->lp
);
4190 sol
= non_neg_lexmin(graph
, lp
, n_edge
);
4192 return isl_schedule_node_free(node
);
4194 trivial
= is_any_trivial(graph
, sol
);
4196 sol
= isl_vec_free(sol
);
4197 } else if (trivial
&& graph
->scc
> 1) {
4199 return compute_component_schedule(node
, graph
, 1);
4202 if (update_schedule(graph
, sol
, 0, 0) < 0)
4203 return isl_schedule_node_free(node
);
4207 return split_scaled(node
, graph
);
4210 /* Topologically sort statements mapped to the same schedule iteration
4211 * and add insert a sequence node in front of "node"
4212 * corresponding to this order.
4213 * If "initialized" is set, then it may be assumed that compute_maxvar
4214 * has been called on the current band. Otherwise, call
4215 * compute_maxvar if and before carry_dependences gets called.
4217 * If it turns out to be impossible to sort the statements apart,
4218 * because different dependences impose different orderings
4219 * on the statements, then we extend the schedule such that
4220 * it carries at least one more dependence.
4222 static __isl_give isl_schedule_node
*sort_statements(
4223 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
4227 isl_union_set_list
*filters
;
4232 ctx
= isl_schedule_node_get_ctx(node
);
4234 isl_die(ctx
, isl_error_internal
,
4235 "graph should have at least one node",
4236 return isl_schedule_node_free(node
));
4241 if (update_edges(ctx
, graph
) < 0)
4242 return isl_schedule_node_free(node
);
4244 if (graph
->n_edge
== 0)
4247 if (detect_sccs(ctx
, graph
) < 0)
4248 return isl_schedule_node_free(node
);
4251 if (graph
->scc
< graph
->n
) {
4252 if (!initialized
&& compute_maxvar(graph
) < 0)
4253 return isl_schedule_node_free(node
);
4254 return carry_dependences(node
, graph
);
4257 filters
= extract_sccs(ctx
, graph
);
4258 node
= isl_schedule_node_insert_sequence(node
, filters
);
4263 /* Are there any (non-empty) (conditional) validity edges in the graph?
4265 static int has_validity_edges(struct isl_sched_graph
*graph
)
4269 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4272 empty
= isl_map_plain_is_empty(graph
->edge
[i
].map
);
4277 if (is_any_validity(&graph
->edge
[i
]))
4284 /* Should we apply a Feautrier step?
4285 * That is, did the user request the Feautrier algorithm and are
4286 * there any validity dependences (left)?
4288 static int need_feautrier_step(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
4290 if (ctx
->opt
->schedule_algorithm
!= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
)
4293 return has_validity_edges(graph
);
4296 /* Compute a schedule for a connected dependence graph using Feautrier's
4297 * multi-dimensional scheduling algorithm and return the updated schedule node.
4299 * The original algorithm is described in [1].
4300 * The main idea is to minimize the number of scheduling dimensions, by
4301 * trying to satisfy as many dependences as possible per scheduling dimension.
4303 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4304 * Problem, Part II: Multi-Dimensional Time.
4305 * In Intl. Journal of Parallel Programming, 1992.
4307 static __isl_give isl_schedule_node
*compute_schedule_wcc_feautrier(
4308 isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4310 return carry_dependences(node
, graph
);
4313 /* Turn off the "local" bit on all (condition) edges.
4315 static void clear_local_edges(struct isl_sched_graph
*graph
)
4319 for (i
= 0; i
< graph
->n_edge
; ++i
)
4320 if (is_condition(&graph
->edge
[i
]))
4321 clear_local(&graph
->edge
[i
]);
4324 /* Does "graph" have both condition and conditional validity edges?
4326 static int need_condition_check(struct isl_sched_graph
*graph
)
4329 int any_condition
= 0;
4330 int any_conditional_validity
= 0;
4332 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4333 if (is_condition(&graph
->edge
[i
]))
4335 if (is_conditional_validity(&graph
->edge
[i
]))
4336 any_conditional_validity
= 1;
4339 return any_condition
&& any_conditional_validity
;
4342 /* Does "graph" contain any coincidence edge?
4344 static int has_any_coincidence(struct isl_sched_graph
*graph
)
4348 for (i
= 0; i
< graph
->n_edge
; ++i
)
4349 if (is_coincidence(&graph
->edge
[i
]))
4355 /* Extract the final schedule row as a map with the iteration domain
4356 * of "node" as domain.
4358 static __isl_give isl_map
*final_row(struct isl_sched_node
*node
)
4363 row
= isl_mat_rows(node
->sched
) - 1;
4364 ma
= node_extract_partial_schedule_multi_aff(node
, row
, 1);
4365 return isl_map_from_multi_aff(ma
);
4368 /* Is the conditional validity dependence in the edge with index "edge_index"
4369 * violated by the latest (i.e., final) row of the schedule?
4370 * That is, is i scheduled after j
4371 * for any conditional validity dependence i -> j?
4373 static int is_violated(struct isl_sched_graph
*graph
, int edge_index
)
4375 isl_map
*src_sched
, *dst_sched
, *map
;
4376 struct isl_sched_edge
*edge
= &graph
->edge
[edge_index
];
4379 src_sched
= final_row(edge
->src
);
4380 dst_sched
= final_row(edge
->dst
);
4381 map
= isl_map_copy(edge
->map
);
4382 map
= isl_map_apply_domain(map
, src_sched
);
4383 map
= isl_map_apply_range(map
, dst_sched
);
4384 map
= isl_map_order_gt(map
, isl_dim_in
, 0, isl_dim_out
, 0);
4385 empty
= isl_map_is_empty(map
);
4394 /* Does "graph" have any satisfied condition edges that
4395 * are adjacent to the conditional validity constraint with
4396 * domain "conditional_source" and range "conditional_sink"?
4398 * A satisfied condition is one that is not local.
4399 * If a condition was forced to be local already (i.e., marked as local)
4400 * then there is no need to check if it is in fact local.
4402 * Additionally, mark all adjacent condition edges found as local.
4404 static int has_adjacent_true_conditions(struct isl_sched_graph
*graph
,
4405 __isl_keep isl_union_set
*conditional_source
,
4406 __isl_keep isl_union_set
*conditional_sink
)
4411 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4412 int adjacent
, local
;
4413 isl_union_map
*condition
;
4415 if (!is_condition(&graph
->edge
[i
]))
4417 if (is_local(&graph
->edge
[i
]))
4420 condition
= graph
->edge
[i
].tagged_condition
;
4421 adjacent
= domain_intersects(condition
, conditional_sink
);
4422 if (adjacent
>= 0 && !adjacent
)
4423 adjacent
= range_intersects(condition
,
4424 conditional_source
);
4430 set_local(&graph
->edge
[i
]);
4432 local
= is_condition_false(&graph
->edge
[i
]);
4442 /* Are there any violated conditional validity dependences with
4443 * adjacent condition dependences that are not local with respect
4444 * to the current schedule?
4445 * That is, is the conditional validity constraint violated?
4447 * Additionally, mark all those adjacent condition dependences as local.
4448 * We also mark those adjacent condition dependences that were not marked
4449 * as local before, but just happened to be local already. This ensures
4450 * that they remain local if the schedule is recomputed.
4452 * We first collect domain and range of all violated conditional validity
4453 * dependences and then check if there are any adjacent non-local
4454 * condition dependences.
4456 static int has_violated_conditional_constraint(isl_ctx
*ctx
,
4457 struct isl_sched_graph
*graph
)
4461 isl_union_set
*source
, *sink
;
4463 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
4464 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
4465 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4466 isl_union_set
*uset
;
4467 isl_union_map
*umap
;
4470 if (!is_conditional_validity(&graph
->edge
[i
]))
4473 violated
= is_violated(graph
, i
);
4481 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
4482 uset
= isl_union_map_domain(umap
);
4483 source
= isl_union_set_union(source
, uset
);
4484 source
= isl_union_set_coalesce(source
);
4486 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
4487 uset
= isl_union_map_range(umap
);
4488 sink
= isl_union_set_union(sink
, uset
);
4489 sink
= isl_union_set_coalesce(sink
);
4493 any
= has_adjacent_true_conditions(graph
, source
, sink
);
4495 isl_union_set_free(source
);
4496 isl_union_set_free(sink
);
4499 isl_union_set_free(source
);
4500 isl_union_set_free(sink
);
4504 /* Examine the current band (the rows between graph->band_start and
4505 * graph->n_total_row), deciding whether to drop it or add it to "node"
4506 * and then continue with the computation of the next band, if any.
4507 * If "initialized" is set, then it may be assumed that compute_maxvar
4508 * has been called on the current band. Otherwise, call
4509 * compute_maxvar if and before carry_dependences gets called.
4511 * The caller keeps looking for a new row as long as
4512 * graph->n_row < graph->maxvar. If the latest attempt to find
4513 * such a row failed (i.e., we still have graph->n_row < graph->maxvar),
4515 * - split between SCCs and start over (assuming we found an interesting
4516 * pair of SCCs between which to split)
4517 * - continue with the next band (assuming the current band has at least
4519 * - try to carry as many dependences as possible and continue with the next
4521 * In each case, we first insert a band node in the schedule tree
4522 * if any rows have been computed.
4524 * If the caller managed to complete the schedule, we insert a band node
4525 * (if any schedule rows were computed) and we finish off by topologically
4526 * sorting the statements based on the remaining dependences.
4528 static __isl_give isl_schedule_node
*compute_schedule_finish_band(
4529 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
4537 if (graph
->n_row
< graph
->maxvar
) {
4539 int empty
= graph
->n_total_row
== graph
->band_start
;
4541 ctx
= isl_schedule_node_get_ctx(node
);
4542 if (!ctx
->opt
->schedule_maximize_band_depth
&& !empty
)
4543 return compute_next_band(node
, graph
, 1);
4544 if (graph
->src_scc
>= 0)
4545 return compute_split_schedule(node
, graph
);
4547 return compute_next_band(node
, graph
, 1);
4548 if (!initialized
&& compute_maxvar(graph
) < 0)
4549 return isl_schedule_node_free(node
);
4550 return carry_dependences(node
, graph
);
4553 insert
= graph
->n_total_row
> graph
->band_start
;
4555 node
= insert_current_band(node
, graph
, 1);
4556 node
= isl_schedule_node_child(node
, 0);
4558 node
= sort_statements(node
, graph
, initialized
);
4560 node
= isl_schedule_node_parent(node
);
4565 /* Construct a band of schedule rows for a connected dependence graph.
4566 * The caller is responsible for determining the strongly connected
4567 * components and calling compute_maxvar first.
4569 * We try to find a sequence of as many schedule rows as possible that result
4570 * in non-negative dependence distances (independent of the previous rows
4571 * in the sequence, i.e., such that the sequence is tilable), with as
4572 * many of the initial rows as possible satisfying the coincidence constraints.
4573 * The computation stops if we can't find any more rows or if we have found
4574 * all the rows we wanted to find.
4576 * If ctx->opt->schedule_outer_coincidence is set, then we force the
4577 * outermost dimension to satisfy the coincidence constraints. If this
4578 * turns out to be impossible, we fall back on the general scheme above
4579 * and try to carry as many dependences as possible.
4581 * If "graph" contains both condition and conditional validity dependences,
4582 * then we need to check that that the conditional schedule constraint
4583 * is satisfied, i.e., there are no violated conditional validity dependences
4584 * that are adjacent to any non-local condition dependences.
4585 * If there are, then we mark all those adjacent condition dependences
4586 * as local and recompute the current band. Those dependences that
4587 * are marked local will then be forced to be local.
4588 * The initial computation is performed with no dependences marked as local.
4589 * If we are lucky, then there will be no violated conditional validity
4590 * dependences adjacent to any non-local condition dependences.
4591 * Otherwise, we mark some additional condition dependences as local and
4592 * recompute. We continue this process until there are no violations left or
4593 * until we are no longer able to compute a schedule.
4594 * Since there are only a finite number of dependences,
4595 * there will only be a finite number of iterations.
4597 static isl_stat
compute_schedule_wcc_band(isl_ctx
*ctx
,
4598 struct isl_sched_graph
*graph
)
4600 int has_coincidence
;
4601 int use_coincidence
;
4602 int force_coincidence
= 0;
4603 int check_conditional
;
4605 if (sort_sccs(graph
) < 0)
4606 return isl_stat_error
;
4608 clear_local_edges(graph
);
4609 check_conditional
= need_condition_check(graph
);
4610 has_coincidence
= has_any_coincidence(graph
);
4612 if (ctx
->opt
->schedule_outer_coincidence
)
4613 force_coincidence
= 1;
4615 use_coincidence
= has_coincidence
;
4616 while (graph
->n_row
< graph
->maxvar
) {
4621 graph
->src_scc
= -1;
4622 graph
->dst_scc
= -1;
4624 if (setup_lp(ctx
, graph
, use_coincidence
) < 0)
4625 return isl_stat_error
;
4626 sol
= solve_lp(graph
);
4628 return isl_stat_error
;
4629 if (sol
->size
== 0) {
4630 int empty
= graph
->n_total_row
== graph
->band_start
;
4633 if (use_coincidence
&& (!force_coincidence
|| !empty
)) {
4634 use_coincidence
= 0;
4639 coincident
= !has_coincidence
|| use_coincidence
;
4640 if (update_schedule(graph
, sol
, 1, coincident
) < 0)
4641 return isl_stat_error
;
4643 if (!check_conditional
)
4645 violated
= has_violated_conditional_constraint(ctx
, graph
);
4647 return isl_stat_error
;
4650 if (reset_band(graph
) < 0)
4651 return isl_stat_error
;
4652 use_coincidence
= has_coincidence
;
4658 /* Compute a schedule for a connected dependence graph by considering
4659 * the graph as a whole and return the updated schedule node.
4661 * The actual schedule rows of the current band are computed by
4662 * compute_schedule_wcc_band. compute_schedule_finish_band takes
4663 * care of integrating the band into "node" and continuing
4666 static __isl_give isl_schedule_node
*compute_schedule_wcc_whole(
4667 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4674 ctx
= isl_schedule_node_get_ctx(node
);
4675 if (compute_schedule_wcc_band(ctx
, graph
) < 0)
4676 return isl_schedule_node_free(node
);
4678 return compute_schedule_finish_band(node
, graph
, 1);
4681 /* Clustering information used by compute_schedule_wcc_clustering.
4683 * "n" is the number of SCCs in the original dependence graph
4684 * "scc" is an array of "n" elements, each representing an SCC
4685 * of the original dependence graph. All entries in the same cluster
4686 * have the same number of schedule rows.
4687 * "scc_cluster" maps each SCC index to the cluster to which it belongs,
4688 * where each cluster is represented by the index of the first SCC
4689 * in the cluster. Initially, each SCC belongs to a cluster containing
4692 * "scc_in_merge" is used by merge_clusters_along_edge to keep
4693 * track of which SCCs need to be merged.
4695 * "cluster" contains the merged clusters of SCCs after the clustering
4698 * "scc_node" is a temporary data structure used inside copy_partial.
4699 * For each SCC, it keeps track of the number of nodes in the SCC
4700 * that have already been copied.
4702 struct isl_clustering
{
4704 struct isl_sched_graph
*scc
;
4705 struct isl_sched_graph
*cluster
;
4711 /* Initialize the clustering data structure "c" from "graph".
4713 * In particular, allocate memory, extract the SCCs from "graph"
4714 * into c->scc, initialize scc_cluster and construct
4715 * a band of schedule rows for each SCC.
4716 * Within each SCC, there is only one SCC by definition.
4717 * Each SCC initially belongs to a cluster containing only that SCC.
4719 static isl_stat
clustering_init(isl_ctx
*ctx
, struct isl_clustering
*c
,
4720 struct isl_sched_graph
*graph
)
4725 c
->scc
= isl_calloc_array(ctx
, struct isl_sched_graph
, c
->n
);
4726 c
->cluster
= isl_calloc_array(ctx
, struct isl_sched_graph
, c
->n
);
4727 c
->scc_cluster
= isl_calloc_array(ctx
, int, c
->n
);
4728 c
->scc_node
= isl_calloc_array(ctx
, int, c
->n
);
4729 c
->scc_in_merge
= isl_calloc_array(ctx
, int, c
->n
);
4730 if (!c
->scc
|| !c
->cluster
||
4731 !c
->scc_cluster
|| !c
->scc_node
|| !c
->scc_in_merge
)
4732 return isl_stat_error
;
4734 for (i
= 0; i
< c
->n
; ++i
) {
4735 if (extract_sub_graph(ctx
, graph
, &node_scc_exactly
,
4736 &edge_scc_exactly
, i
, &c
->scc
[i
]) < 0)
4737 return isl_stat_error
;
4739 if (compute_maxvar(&c
->scc
[i
]) < 0)
4740 return isl_stat_error
;
4741 if (compute_schedule_wcc_band(ctx
, &c
->scc
[i
]) < 0)
4742 return isl_stat_error
;
4743 c
->scc_cluster
[i
] = i
;
4749 /* Free all memory allocated for "c".
4751 static void clustering_free(isl_ctx
*ctx
, struct isl_clustering
*c
)
4756 for (i
= 0; i
< c
->n
; ++i
)
4757 graph_free(ctx
, &c
->scc
[i
]);
4760 for (i
= 0; i
< c
->n
; ++i
)
4761 graph_free(ctx
, &c
->cluster
[i
]);
4763 free(c
->scc_cluster
);
4765 free(c
->scc_in_merge
);
4768 /* Should we refrain from merging the cluster in "graph" with
4769 * any other cluster?
4770 * In particular, is its current schedule band empty and incomplete.
4772 static int bad_cluster(struct isl_sched_graph
*graph
)
4774 return graph
->n_row
< graph
->maxvar
&&
4775 graph
->n_total_row
== graph
->band_start
;
4778 /* Return the index of an edge in "graph" that can be used to merge
4779 * two clusters in "c".
4780 * Return graph->n_edge if no such edge can be found.
4781 * Return -1 on error.
4783 * In particular, return a proximity edge between two clusters
4784 * that is not marked "no_merge" and such that neither of the
4785 * two clusters has an incomplete, empty band.
4787 * If there are multiple such edges, then try and find the most
4788 * appropriate edge to use for merging. In particular, pick the edge
4789 * with the greatest weight. If there are multiple of those,
4790 * then pick one with the shortest distance between
4791 * the two cluster representatives.
4793 static int find_proximity(struct isl_sched_graph
*graph
,
4794 struct isl_clustering
*c
)
4796 int i
, best
= graph
->n_edge
, best_dist
, best_weight
;
4798 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4799 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
4802 if (!is_proximity(edge
))
4806 if (bad_cluster(&c
->scc
[edge
->src
->scc
]) ||
4807 bad_cluster(&c
->scc
[edge
->dst
->scc
]))
4809 dist
= c
->scc_cluster
[edge
->dst
->scc
] -
4810 c
->scc_cluster
[edge
->src
->scc
];
4813 weight
= edge
->weight
;
4814 if (best
< graph
->n_edge
) {
4815 if (best_weight
> weight
)
4817 if (best_weight
== weight
&& best_dist
<= dist
)
4822 best_weight
= weight
;
4828 /* Internal data structure used in mark_merge_sccs.
4830 * "graph" is the dependence graph in which a strongly connected
4831 * component is constructed.
4832 * "scc_cluster" maps each SCC index to the cluster to which it belongs.
4833 * "src" and "dst" are the indices of the nodes that are being merged.
4835 struct isl_mark_merge_sccs_data
{
4836 struct isl_sched_graph
*graph
;
4842 /* Check whether the cluster containing node "i" depends on the cluster
4843 * containing node "j". If "i" and "j" belong to the same cluster,
4844 * then they are taken to depend on each other to ensure that
4845 * the resulting strongly connected component consists of complete
4846 * clusters. Furthermore, if "i" and "j" are the two nodes that
4847 * are being merged, then they are taken to depend on each other as well.
4848 * Otherwise, check if there is a (conditional) validity dependence
4849 * from node[j] to node[i], forcing node[i] to follow node[j].
4851 static isl_bool
cluster_follows(int i
, int j
, void *user
)
4853 struct isl_mark_merge_sccs_data
*data
= user
;
4854 struct isl_sched_graph
*graph
= data
->graph
;
4855 int *scc_cluster
= data
->scc_cluster
;
4857 if (data
->src
== i
&& data
->dst
== j
)
4858 return isl_bool_true
;
4859 if (data
->src
== j
&& data
->dst
== i
)
4860 return isl_bool_true
;
4861 if (scc_cluster
[graph
->node
[i
].scc
] == scc_cluster
[graph
->node
[j
].scc
])
4862 return isl_bool_true
;
4864 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
4867 /* Mark all SCCs that belong to either of the two clusters in "c"
4868 * connected by the edge in "graph" with index "edge", or to any
4869 * of the intermediate clusters.
4870 * The marking is recorded in c->scc_in_merge.
4872 * The given edge has been selected for merging two clusters,
4873 * meaning that there is at least a proximity edge between the two nodes.
4874 * However, there may also be (indirect) validity dependences
4875 * between the two nodes. When merging the two clusters, all clusters
4876 * containing one or more of the intermediate nodes along the
4877 * indirect validity dependences need to be merged in as well.
4879 * First collect all such nodes by computing the strongly connected
4880 * component (SCC) containing the two nodes connected by the edge, where
4881 * the two nodes are considered to depend on each other to make
4882 * sure they end up in the same SCC. Similarly, each node is considered
4883 * to depend on every other node in the same cluster to ensure
4884 * that the SCC consists of complete clusters.
4886 * Then the original SCCs that contain any of these nodes are marked
4887 * in c->scc_in_merge.
4889 static isl_stat
mark_merge_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
4890 int edge
, struct isl_clustering
*c
)
4892 struct isl_mark_merge_sccs_data data
;
4893 struct isl_tarjan_graph
*g
;
4896 for (i
= 0; i
< c
->n
; ++i
)
4897 c
->scc_in_merge
[i
] = 0;
4900 data
.scc_cluster
= c
->scc_cluster
;
4901 data
.src
= graph
->edge
[edge
].src
- graph
->node
;
4902 data
.dst
= graph
->edge
[edge
].dst
- graph
->node
;
4904 g
= isl_tarjan_graph_component(ctx
, graph
->n
, data
.dst
,
4905 &cluster_follows
, &data
);
4911 isl_die(ctx
, isl_error_internal
,
4912 "expecting at least two nodes in component",
4914 if (g
->order
[--i
] != -1)
4915 isl_die(ctx
, isl_error_internal
,
4916 "expecting end of component marker", goto error
);
4918 for (--i
; i
>= 0 && g
->order
[i
] != -1; --i
) {
4919 int scc
= graph
->node
[g
->order
[i
]].scc
;
4920 c
->scc_in_merge
[scc
] = 1;
4923 isl_tarjan_graph_free(g
);
4926 isl_tarjan_graph_free(g
);
4927 return isl_stat_error
;
4930 /* Construct the identifier "cluster_i".
4932 static __isl_give isl_id
*cluster_id(isl_ctx
*ctx
, int i
)
4936 snprintf(name
, sizeof(name
), "cluster_%d", i
);
4937 return isl_id_alloc(ctx
, name
, NULL
);
4940 /* Construct the space of the cluster with index "i" containing
4941 * the strongly connected component "scc".
4943 * In particular, construct a space called cluster_i with dimension equal
4944 * to the number of schedule rows in the current band of "scc".
4946 static __isl_give isl_space
*cluster_space(struct isl_sched_graph
*scc
, int i
)
4952 nvar
= scc
->n_total_row
- scc
->band_start
;
4953 space
= isl_space_copy(scc
->node
[0].space
);
4954 space
= isl_space_params(space
);
4955 space
= isl_space_set_from_params(space
);
4956 space
= isl_space_add_dims(space
, isl_dim_set
, nvar
);
4957 id
= cluster_id(isl_space_get_ctx(space
), i
);
4958 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
4963 /* Collect the domain of the graph for merging clusters.
4965 * In particular, for each cluster with first SCC "i", construct
4966 * a set in the space called cluster_i with dimension equal
4967 * to the number of schedule rows in the current band of the cluster.
4969 static __isl_give isl_union_set
*collect_domain(isl_ctx
*ctx
,
4970 struct isl_sched_graph
*graph
, struct isl_clustering
*c
)
4974 isl_union_set
*domain
;
4976 space
= isl_space_params_alloc(ctx
, 0);
4977 domain
= isl_union_set_empty(space
);
4979 for (i
= 0; i
< graph
->scc
; ++i
) {
4982 if (!c
->scc_in_merge
[i
])
4984 if (c
->scc_cluster
[i
] != i
)
4986 space
= cluster_space(&c
->scc
[i
], i
);
4987 domain
= isl_union_set_add_set(domain
, isl_set_universe(space
));
4993 /* Construct a map from the original instances to the corresponding
4994 * cluster instance in the current bands of the clusters in "c".
4996 static __isl_give isl_union_map
*collect_cluster_map(isl_ctx
*ctx
,
4997 struct isl_sched_graph
*graph
, struct isl_clustering
*c
)
5001 isl_union_map
*cluster_map
;
5003 space
= isl_space_params_alloc(ctx
, 0);
5004 cluster_map
= isl_union_map_empty(space
);
5005 for (i
= 0; i
< graph
->scc
; ++i
) {
5009 if (!c
->scc_in_merge
[i
])
5012 id
= cluster_id(ctx
, c
->scc_cluster
[i
]);
5013 start
= c
->scc
[i
].band_start
;
5014 n
= c
->scc
[i
].n_total_row
- start
;
5015 for (j
= 0; j
< c
->scc
[i
].n
; ++j
) {
5018 struct isl_sched_node
*node
= &c
->scc
[i
].node
[j
];
5020 ma
= node_extract_partial_schedule_multi_aff(node
,
5022 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
,
5024 map
= isl_map_from_multi_aff(ma
);
5025 cluster_map
= isl_union_map_add_map(cluster_map
, map
);
5033 /* Add "umap" to the schedule constraints "sc" of all types of "edge"
5034 * that are not isl_edge_condition or isl_edge_conditional_validity.
5036 static __isl_give isl_schedule_constraints
*add_non_conditional_constraints(
5037 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*umap
,
5038 __isl_take isl_schedule_constraints
*sc
)
5040 enum isl_edge_type t
;
5045 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
5046 if (t
== isl_edge_condition
||
5047 t
== isl_edge_conditional_validity
)
5049 if (!is_type(edge
, t
))
5051 sc
= isl_schedule_constraints_add(sc
, t
,
5052 isl_union_map_copy(umap
));
5058 /* Add schedule constraints of types isl_edge_condition and
5059 * isl_edge_conditional_validity to "sc" by applying "umap" to
5060 * the domains of the wrapped relations in domain and range
5061 * of the corresponding tagged constraints of "edge".
5063 static __isl_give isl_schedule_constraints
*add_conditional_constraints(
5064 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*umap
,
5065 __isl_take isl_schedule_constraints
*sc
)
5067 enum isl_edge_type t
;
5068 isl_union_map
*tagged
;
5070 for (t
= isl_edge_condition
; t
<= isl_edge_conditional_validity
; ++t
) {
5071 if (!is_type(edge
, t
))
5073 if (t
== isl_edge_condition
)
5074 tagged
= isl_union_map_copy(edge
->tagged_condition
);
5076 tagged
= isl_union_map_copy(edge
->tagged_validity
);
5077 tagged
= isl_union_map_zip(tagged
);
5078 tagged
= isl_union_map_apply_domain(tagged
,
5079 isl_union_map_copy(umap
));
5080 tagged
= isl_union_map_zip(tagged
);
5081 sc
= isl_schedule_constraints_add(sc
, t
, tagged
);
5089 /* Given a mapping "cluster_map" from the original instances to
5090 * the cluster instances, add schedule constraints on the clusters
5091 * to "sc" corresponding to the original constraints represented by "edge".
5093 * For non-tagged dependence constraints, the cluster constraints
5094 * are obtained by applying "cluster_map" to the edge->map.
5096 * For tagged dependence constraints, "cluster_map" needs to be applied
5097 * to the domains of the wrapped relations in domain and range
5098 * of the tagged dependence constraints. Pick out the mappings
5099 * from these domains from "cluster_map" and construct their product.
5100 * This mapping can then be applied to the pair of domains.
5102 static __isl_give isl_schedule_constraints
*collect_edge_constraints(
5103 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*cluster_map
,
5104 __isl_take isl_schedule_constraints
*sc
)
5106 isl_union_map
*umap
;
5108 isl_union_set
*uset
;
5109 isl_union_map
*umap1
, *umap2
;
5114 umap
= isl_union_map_from_map(isl_map_copy(edge
->map
));
5115 umap
= isl_union_map_apply_domain(umap
,
5116 isl_union_map_copy(cluster_map
));
5117 umap
= isl_union_map_apply_range(umap
,
5118 isl_union_map_copy(cluster_map
));
5119 sc
= add_non_conditional_constraints(edge
, umap
, sc
);
5120 isl_union_map_free(umap
);
5122 if (!sc
|| (!is_condition(edge
) && !is_conditional_validity(edge
)))
5125 space
= isl_space_domain(isl_map_get_space(edge
->map
));
5126 uset
= isl_union_set_from_set(isl_set_universe(space
));
5127 umap1
= isl_union_map_copy(cluster_map
);
5128 umap1
= isl_union_map_intersect_domain(umap1
, uset
);
5129 space
= isl_space_range(isl_map_get_space(edge
->map
));
5130 uset
= isl_union_set_from_set(isl_set_universe(space
));
5131 umap2
= isl_union_map_copy(cluster_map
);
5132 umap2
= isl_union_map_intersect_domain(umap2
, uset
);
5133 umap
= isl_union_map_product(umap1
, umap2
);
5135 sc
= add_conditional_constraints(edge
, umap
, sc
);
5137 isl_union_map_free(umap
);
5141 /* Given a mapping "cluster_map" from the original instances to
5142 * the cluster instances, add schedule constraints on the clusters
5143 * to "sc" corresponding to all edges in "graph" between nodes that
5144 * belong to SCCs that are marked for merging in "scc_in_merge".
5146 static __isl_give isl_schedule_constraints
*collect_constraints(
5147 struct isl_sched_graph
*graph
, int *scc_in_merge
,
5148 __isl_keep isl_union_map
*cluster_map
,
5149 __isl_take isl_schedule_constraints
*sc
)
5153 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5154 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
5156 if (!scc_in_merge
[edge
->src
->scc
])
5158 if (!scc_in_merge
[edge
->dst
->scc
])
5160 sc
= collect_edge_constraints(edge
, cluster_map
, sc
);
5166 /* Construct a dependence graph for scheduling clusters with respect
5167 * to each other and store the result in "merge_graph".
5168 * In particular, the nodes of the graph correspond to the schedule
5169 * dimensions of the current bands of those clusters that have been
5170 * marked for merging in "c".
5172 * First construct an isl_schedule_constraints object for this domain
5173 * by transforming the edges in "graph" to the domain.
5174 * Then initialize a dependence graph for scheduling from these
5177 static isl_stat
init_merge_graph(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5178 struct isl_clustering
*c
, struct isl_sched_graph
*merge_graph
)
5180 isl_union_set
*domain
;
5181 isl_union_map
*cluster_map
;
5182 isl_schedule_constraints
*sc
;
5185 domain
= collect_domain(ctx
, graph
, c
);
5186 sc
= isl_schedule_constraints_on_domain(domain
);
5188 return isl_stat_error
;
5189 cluster_map
= collect_cluster_map(ctx
, graph
, c
);
5190 sc
= collect_constraints(graph
, c
->scc_in_merge
, cluster_map
, sc
);
5191 isl_union_map_free(cluster_map
);
5193 r
= graph_init(merge_graph
, sc
);
5195 isl_schedule_constraints_free(sc
);
5200 /* Compute the maximal number of remaining schedule rows that still need
5201 * to be computed for the nodes that belong to clusters with the maximal
5202 * dimension for the current band (i.e., the band that is to be merged).
5203 * Only clusters that are about to be merged are considered.
5204 * "maxvar" is the maximal dimension for the current band.
5205 * "c" contains information about the clusters.
5207 * Return the maximal number of remaining schedule rows or -1 on error.
5209 static int compute_maxvar_max_slack(int maxvar
, struct isl_clustering
*c
)
5215 for (i
= 0; i
< c
->n
; ++i
) {
5217 struct isl_sched_graph
*scc
;
5219 if (!c
->scc_in_merge
[i
])
5222 nvar
= scc
->n_total_row
- scc
->band_start
;
5225 for (j
= 0; j
< scc
->n
; ++j
) {
5226 struct isl_sched_node
*node
= &scc
->node
[j
];
5229 if (node_update_cmap(node
) < 0)
5231 slack
= node
->nvar
- node
->rank
;
5232 if (slack
> max_slack
)
5240 /* If there are any clusters where the dimension of the current band
5241 * (i.e., the band that is to be merged) is smaller than "maxvar" and
5242 * if there are any nodes in such a cluster where the number
5243 * of remaining schedule rows that still need to be computed
5244 * is greater than "max_slack", then return the smallest current band
5245 * dimension of all these clusters. Otherwise return the original value
5246 * of "maxvar". Return -1 in case of any error.
5247 * Only clusters that are about to be merged are considered.
5248 * "c" contains information about the clusters.
5250 static int limit_maxvar_to_slack(int maxvar
, int max_slack
,
5251 struct isl_clustering
*c
)
5255 for (i
= 0; i
< c
->n
; ++i
) {
5257 struct isl_sched_graph
*scc
;
5259 if (!c
->scc_in_merge
[i
])
5262 nvar
= scc
->n_total_row
- scc
->band_start
;
5265 for (j
= 0; j
< scc
->n
; ++j
) {
5266 struct isl_sched_node
*node
= &scc
->node
[j
];
5269 if (node_update_cmap(node
) < 0)
5271 slack
= node
->nvar
- node
->rank
;
5272 if (slack
> max_slack
) {
5282 /* Adjust merge_graph->maxvar based on the number of remaining schedule rows
5283 * that still need to be computed. In particular, if there is a node
5284 * in a cluster where the dimension of the current band is smaller
5285 * than merge_graph->maxvar, but the number of remaining schedule rows
5286 * is greater than that of any node in a cluster with the maximal
5287 * dimension for the current band (i.e., merge_graph->maxvar),
5288 * then adjust merge_graph->maxvar to the (smallest) current band dimension
5289 * of those clusters. Without this adjustment, the total number of
5290 * schedule dimensions would be increased, resulting in a skewed view
5291 * of the number of coincident dimensions.
5292 * "c" contains information about the clusters.
5294 * If the maximize_band_depth option is set and merge_graph->maxvar is reduced,
5295 * then there is no point in attempting any merge since it will be rejected
5296 * anyway. Set merge_graph->maxvar to zero in such cases.
5298 static isl_stat
adjust_maxvar_to_slack(isl_ctx
*ctx
,
5299 struct isl_sched_graph
*merge_graph
, struct isl_clustering
*c
)
5301 int max_slack
, maxvar
;
5303 max_slack
= compute_maxvar_max_slack(merge_graph
->maxvar
, c
);
5305 return isl_stat_error
;
5306 maxvar
= limit_maxvar_to_slack(merge_graph
->maxvar
, max_slack
, c
);
5308 return isl_stat_error
;
5310 if (maxvar
< merge_graph
->maxvar
) {
5311 if (isl_options_get_schedule_maximize_band_depth(ctx
))
5312 merge_graph
->maxvar
= 0;
5314 merge_graph
->maxvar
= maxvar
;
5320 /* Return the number of coincident dimensions in the current band of "graph",
5321 * where the nodes of "graph" are assumed to be scheduled by a single band.
5323 static int get_n_coincident(struct isl_sched_graph
*graph
)
5327 for (i
= graph
->band_start
; i
< graph
->n_total_row
; ++i
)
5328 if (!graph
->node
[0].coincident
[i
])
5331 return i
- graph
->band_start
;
5334 /* Should the clusters be merged based on the cluster schedule
5335 * in the current (and only) band of "merge_graph", given that
5336 * coincidence should be maximized?
5338 * If the number of coincident schedule dimensions in the merged band
5339 * would be less than the maximal number of coincident schedule dimensions
5340 * in any of the merged clusters, then the clusters should not be merged.
5342 static isl_bool
ok_to_merge_coincident(struct isl_clustering
*c
,
5343 struct isl_sched_graph
*merge_graph
)
5350 for (i
= 0; i
< c
->n
; ++i
) {
5351 if (!c
->scc_in_merge
[i
])
5353 n_coincident
= get_n_coincident(&c
->scc
[i
]);
5354 if (n_coincident
> max_coincident
)
5355 max_coincident
= n_coincident
;
5358 n_coincident
= get_n_coincident(merge_graph
);
5360 return n_coincident
>= max_coincident
;
5363 /* Return the transformation on "node" expressed by the current (and only)
5364 * band of "merge_graph" applied to the clusters in "c".
5366 * First find the representation of "node" in its SCC in "c" and
5367 * extract the transformation expressed by the current band.
5368 * Then extract the transformation applied by "merge_graph"
5369 * to the cluster to which this SCC belongs.
5370 * Combine the two to obtain the complete transformation on the node.
5372 * Note that the range of the first transformation is an anonymous space,
5373 * while the domain of the second is named "cluster_X". The range
5374 * of the former therefore needs to be adjusted before the two
5377 static __isl_give isl_map
*extract_node_transformation(isl_ctx
*ctx
,
5378 struct isl_sched_node
*node
, struct isl_clustering
*c
,
5379 struct isl_sched_graph
*merge_graph
)
5381 struct isl_sched_node
*scc_node
, *cluster_node
;
5385 isl_multi_aff
*ma
, *ma2
;
5387 scc_node
= graph_find_node(ctx
, &c
->scc
[node
->scc
], node
->space
);
5388 start
= c
->scc
[node
->scc
].band_start
;
5389 n
= c
->scc
[node
->scc
].n_total_row
- start
;
5390 ma
= node_extract_partial_schedule_multi_aff(scc_node
, start
, n
);
5391 space
= cluster_space(&c
->scc
[node
->scc
], c
->scc_cluster
[node
->scc
]);
5392 cluster_node
= graph_find_node(ctx
, merge_graph
, space
);
5393 if (space
&& !cluster_node
)
5394 isl_die(ctx
, isl_error_internal
, "unable to find cluster",
5395 space
= isl_space_free(space
));
5396 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
5397 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
, id
);
5398 isl_space_free(space
);
5399 n
= merge_graph
->n_total_row
;
5400 ma2
= node_extract_partial_schedule_multi_aff(cluster_node
, 0, n
);
5401 ma
= isl_multi_aff_pullback_multi_aff(ma2
, ma
);
5403 return isl_map_from_multi_aff(ma
);
5406 /* Give a set of distances "set", are they bounded by a small constant
5407 * in direction "pos"?
5408 * In practice, check if they are bounded by 2 by checking that there
5409 * are no elements with a value greater than or equal to 3 or
5410 * smaller than or equal to -3.
5412 static isl_bool
distance_is_bounded(__isl_keep isl_set
*set
, int pos
)
5418 return isl_bool_error
;
5420 test
= isl_set_copy(set
);
5421 test
= isl_set_lower_bound_si(test
, isl_dim_set
, pos
, 3);
5422 bounded
= isl_set_is_empty(test
);
5425 if (bounded
< 0 || !bounded
)
5428 test
= isl_set_copy(set
);
5429 test
= isl_set_upper_bound_si(test
, isl_dim_set
, pos
, -3);
5430 bounded
= isl_set_is_empty(test
);
5436 /* Does the set "set" have a fixed (but possible parametric) value
5437 * at dimension "pos"?
5439 static isl_bool
has_single_value(__isl_keep isl_set
*set
, int pos
)
5445 return isl_bool_error
;
5446 set
= isl_set_copy(set
);
5447 n
= isl_set_dim(set
, isl_dim_set
);
5448 set
= isl_set_project_out(set
, isl_dim_set
, pos
+ 1, n
- (pos
+ 1));
5449 set
= isl_set_project_out(set
, isl_dim_set
, 0, pos
);
5450 single
= isl_set_is_singleton(set
);
5456 /* Does "map" have a fixed (but possible parametric) value
5457 * at dimension "pos" of either its domain or its range?
5459 static isl_bool
has_singular_src_or_dst(__isl_keep isl_map
*map
, int pos
)
5464 set
= isl_map_domain(isl_map_copy(map
));
5465 single
= has_single_value(set
, pos
);
5468 if (single
< 0 || single
)
5471 set
= isl_map_range(isl_map_copy(map
));
5472 single
= has_single_value(set
, pos
);
5478 /* Does the edge "edge" from "graph" have bounded dependence distances
5479 * in the merged graph "merge_graph" of a selection of clusters in "c"?
5481 * Extract the complete transformations of the source and destination
5482 * nodes of the edge, apply them to the edge constraints and
5483 * compute the differences. Finally, check if these differences are bounded
5484 * in each direction.
5486 * If the dimension of the band is greater than the number of
5487 * dimensions that can be expected to be optimized by the edge
5488 * (based on its weight), then also allow the differences to be unbounded
5489 * in the remaining dimensions, but only if either the source or
5490 * the destination has a fixed value in that direction.
5491 * This allows a statement that produces values that are used by
5492 * several instances of another statement to be merged with that
5494 * However, merging such clusters will introduce an inherently
5495 * large proximity distance inside the merged cluster, meaning
5496 * that proximity distances will no longer be optimized in
5497 * subsequent merges. These merges are therefore only allowed
5498 * after all other possible merges have been tried.
5499 * The first time such a merge is encountered, the weight of the edge
5500 * is replaced by a negative weight. The second time (i.e., after
5501 * all merges over edges with a non-negative weight have been tried),
5502 * the merge is allowed.
5504 static isl_bool
has_bounded_distances(isl_ctx
*ctx
, struct isl_sched_edge
*edge
,
5505 struct isl_sched_graph
*graph
, struct isl_clustering
*c
,
5506 struct isl_sched_graph
*merge_graph
)
5513 map
= isl_map_copy(edge
->map
);
5514 t
= extract_node_transformation(ctx
, edge
->src
, c
, merge_graph
);
5515 map
= isl_map_apply_domain(map
, t
);
5516 t
= extract_node_transformation(ctx
, edge
->dst
, c
, merge_graph
);
5517 map
= isl_map_apply_range(map
, t
);
5518 dist
= isl_map_deltas(isl_map_copy(map
));
5520 bounded
= isl_bool_true
;
5521 n
= isl_set_dim(dist
, isl_dim_set
);
5522 n_slack
= n
- edge
->weight
;
5523 if (edge
->weight
< 0)
5524 n_slack
-= graph
->max_weight
+ 1;
5525 for (i
= 0; i
< n
; ++i
) {
5526 isl_bool bounded_i
, singular_i
;
5528 bounded_i
= distance_is_bounded(dist
, i
);
5533 if (edge
->weight
>= 0)
5534 bounded
= isl_bool_false
;
5538 singular_i
= has_singular_src_or_dst(map
, i
);
5543 bounded
= isl_bool_false
;
5546 if (!bounded
&& i
>= n
&& edge
->weight
>= 0)
5547 edge
->weight
-= graph
->max_weight
+ 1;
5555 return isl_bool_error
;
5558 /* Should the clusters be merged based on the cluster schedule
5559 * in the current (and only) band of "merge_graph"?
5560 * "graph" is the original dependence graph, while "c" records
5561 * which SCCs are involved in the latest merge.
5563 * In particular, is there at least one proximity constraint
5564 * that is optimized by the merge?
5566 * A proximity constraint is considered to be optimized
5567 * if the dependence distances are small.
5569 static isl_bool
ok_to_merge_proximity(isl_ctx
*ctx
,
5570 struct isl_sched_graph
*graph
, struct isl_clustering
*c
,
5571 struct isl_sched_graph
*merge_graph
)
5575 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5576 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
5579 if (!is_proximity(edge
))
5581 if (!c
->scc_in_merge
[edge
->src
->scc
])
5583 if (!c
->scc_in_merge
[edge
->dst
->scc
])
5585 if (c
->scc_cluster
[edge
->dst
->scc
] ==
5586 c
->scc_cluster
[edge
->src
->scc
])
5588 bounded
= has_bounded_distances(ctx
, edge
, graph
, c
,
5590 if (bounded
< 0 || bounded
)
5594 return isl_bool_false
;
5597 /* Should the clusters be merged based on the cluster schedule
5598 * in the current (and only) band of "merge_graph"?
5599 * "graph" is the original dependence graph, while "c" records
5600 * which SCCs are involved in the latest merge.
5602 * If the current band is empty, then the clusters should not be merged.
5604 * If the band depth should be maximized and the merge schedule
5605 * is incomplete (meaning that the dimension of some of the schedule
5606 * bands in the original schedule will be reduced), then the clusters
5607 * should not be merged.
5609 * If the schedule_maximize_coincidence option is set, then check that
5610 * the number of coincident schedule dimensions is not reduced.
5612 * Finally, only allow the merge if at least one proximity
5613 * constraint is optimized.
5615 static isl_bool
ok_to_merge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5616 struct isl_clustering
*c
, struct isl_sched_graph
*merge_graph
)
5618 if (merge_graph
->n_total_row
== merge_graph
->band_start
)
5619 return isl_bool_false
;
5621 if (isl_options_get_schedule_maximize_band_depth(ctx
) &&
5622 merge_graph
->n_total_row
< merge_graph
->maxvar
)
5623 return isl_bool_false
;
5625 if (isl_options_get_schedule_maximize_coincidence(ctx
)) {
5628 ok
= ok_to_merge_coincident(c
, merge_graph
);
5633 return ok_to_merge_proximity(ctx
, graph
, c
, merge_graph
);
5636 /* Apply the schedule in "t_node" to the "n" rows starting at "first"
5637 * of the schedule in "node" and return the result.
5639 * That is, essentially compute
5641 * T * N(first:first+n-1)
5643 * taking into account the constant term and the parameter coefficients
5646 static __isl_give isl_mat
*node_transformation(isl_ctx
*ctx
,
5647 struct isl_sched_node
*t_node
, struct isl_sched_node
*node
,
5652 int n_row
, n_col
, n_param
, n_var
;
5654 n_param
= node
->nparam
;
5656 n_row
= isl_mat_rows(t_node
->sched
);
5657 n_col
= isl_mat_cols(node
->sched
);
5658 t
= isl_mat_alloc(ctx
, n_row
, n_col
);
5661 for (i
= 0; i
< n_row
; ++i
) {
5662 isl_seq_cpy(t
->row
[i
], t_node
->sched
->row
[i
], 1 + n_param
);
5663 isl_seq_clr(t
->row
[i
] + 1 + n_param
, n_var
);
5664 for (j
= 0; j
< n
; ++j
)
5665 isl_seq_addmul(t
->row
[i
],
5666 t_node
->sched
->row
[i
][1 + n_param
+ j
],
5667 node
->sched
->row
[first
+ j
],
5668 1 + n_param
+ n_var
);
5673 /* Apply the cluster schedule in "t_node" to the current band
5674 * schedule of the nodes in "graph".
5676 * In particular, replace the rows starting at band_start
5677 * by the result of applying the cluster schedule in "t_node"
5678 * to the original rows.
5680 * The coincidence of the schedule is determined by the coincidence
5681 * of the cluster schedule.
5683 static isl_stat
transform(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5684 struct isl_sched_node
*t_node
)
5690 start
= graph
->band_start
;
5691 n
= graph
->n_total_row
- start
;
5693 n_new
= isl_mat_rows(t_node
->sched
);
5694 for (i
= 0; i
< graph
->n
; ++i
) {
5695 struct isl_sched_node
*node
= &graph
->node
[i
];
5698 t
= node_transformation(ctx
, t_node
, node
, start
, n
);
5699 node
->sched
= isl_mat_drop_rows(node
->sched
, start
, n
);
5700 node
->sched
= isl_mat_concat(node
->sched
, t
);
5701 node
->sched_map
= isl_map_free(node
->sched_map
);
5703 return isl_stat_error
;
5704 for (j
= 0; j
< n_new
; ++j
)
5705 node
->coincident
[start
+ j
] = t_node
->coincident
[j
];
5707 graph
->n_total_row
-= n
;
5709 graph
->n_total_row
+= n_new
;
5710 graph
->n_row
+= n_new
;
5715 /* Merge the clusters marked for merging in "c" into a single
5716 * cluster using the cluster schedule in the current band of "merge_graph".
5717 * The representative SCC for the new cluster is the SCC with
5718 * the smallest index.
5720 * The current band schedule of each SCC in the new cluster is obtained
5721 * by applying the schedule of the corresponding original cluster
5722 * to the original band schedule.
5723 * All SCCs in the new cluster have the same number of schedule rows.
5725 static isl_stat
merge(isl_ctx
*ctx
, struct isl_clustering
*c
,
5726 struct isl_sched_graph
*merge_graph
)
5732 for (i
= 0; i
< c
->n
; ++i
) {
5733 struct isl_sched_node
*node
;
5735 if (!c
->scc_in_merge
[i
])
5739 space
= cluster_space(&c
->scc
[i
], c
->scc_cluster
[i
]);
5741 return isl_stat_error
;
5742 node
= graph_find_node(ctx
, merge_graph
, space
);
5743 isl_space_free(space
);
5745 isl_die(ctx
, isl_error_internal
,
5746 "unable to find cluster",
5747 return isl_stat_error
);
5748 if (transform(ctx
, &c
->scc
[i
], node
) < 0)
5749 return isl_stat_error
;
5750 c
->scc_cluster
[i
] = cluster
;
5756 /* Try and merge the clusters of SCCs marked in c->scc_in_merge
5757 * by scheduling the current cluster bands with respect to each other.
5759 * Construct a dependence graph with a space for each cluster and
5760 * with the coordinates of each space corresponding to the schedule
5761 * dimensions of the current band of that cluster.
5762 * Construct a cluster schedule in this cluster dependence graph and
5763 * apply it to the current cluster bands if it is applicable
5764 * according to ok_to_merge.
5766 * If the number of remaining schedule dimensions in a cluster
5767 * with a non-maximal current schedule dimension is greater than
5768 * the number of remaining schedule dimensions in clusters
5769 * with a maximal current schedule dimension, then restrict
5770 * the number of rows to be computed in the cluster schedule
5771 * to the minimal such non-maximal current schedule dimension.
5772 * Do this by adjusting merge_graph.maxvar.
5774 * Return isl_bool_true if the clusters have effectively been merged
5775 * into a single cluster.
5777 * Note that since the standard scheduling algorithm minimizes the maximal
5778 * distance over proximity constraints, the proximity constraints between
5779 * the merged clusters may not be optimized any further than what is
5780 * sufficient to bring the distances within the limits of the internal
5781 * proximity constraints inside the individual clusters.
5782 * It may therefore make sense to perform an additional translation step
5783 * to bring the clusters closer to each other, while maintaining
5784 * the linear part of the merging schedule found using the standard
5785 * scheduling algorithm.
5787 static isl_bool
try_merge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5788 struct isl_clustering
*c
)
5790 struct isl_sched_graph merge_graph
= { 0 };
5793 if (init_merge_graph(ctx
, graph
, c
, &merge_graph
) < 0)
5796 if (compute_maxvar(&merge_graph
) < 0)
5798 if (adjust_maxvar_to_slack(ctx
, &merge_graph
,c
) < 0)
5800 if (compute_schedule_wcc_band(ctx
, &merge_graph
) < 0)
5802 merged
= ok_to_merge(ctx
, graph
, c
, &merge_graph
);
5803 if (merged
&& merge(ctx
, c
, &merge_graph
) < 0)
5806 graph_free(ctx
, &merge_graph
);
5809 graph_free(ctx
, &merge_graph
);
5810 return isl_bool_error
;
5813 /* Is there any edge marked "no_merge" between two SCCs that are
5814 * about to be merged (i.e., that are set in "scc_in_merge")?
5815 * "merge_edge" is the proximity edge along which the clusters of SCCs
5816 * are going to be merged.
5818 * If there is any edge between two SCCs with a negative weight,
5819 * while the weight of "merge_edge" is non-negative, then this
5820 * means that the edge was postponed. "merge_edge" should then
5821 * also be postponed since merging along the edge with negative weight should
5822 * be postponed until all edges with non-negative weight have been tried.
5823 * Replace the weight of "merge_edge" by a negative weight as well and
5824 * tell the caller not to attempt a merge.
5826 static int any_no_merge(struct isl_sched_graph
*graph
, int *scc_in_merge
,
5827 struct isl_sched_edge
*merge_edge
)
5831 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5832 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
5834 if (!scc_in_merge
[edge
->src
->scc
])
5836 if (!scc_in_merge
[edge
->dst
->scc
])
5840 if (merge_edge
->weight
>= 0 && edge
->weight
< 0) {
5841 merge_edge
->weight
-= graph
->max_weight
+ 1;
5849 /* Merge the two clusters in "c" connected by the edge in "graph"
5850 * with index "edge" into a single cluster.
5851 * If it turns out to be impossible to merge these two clusters,
5852 * then mark the edge as "no_merge" such that it will not be
5855 * First mark all SCCs that need to be merged. This includes the SCCs
5856 * in the two clusters, but it may also include the SCCs
5857 * of intermediate clusters.
5858 * If there is already a no_merge edge between any pair of such SCCs,
5859 * then simply mark the current edge as no_merge as well.
5860 * Likewise, if any of those edges was postponed by has_bounded_distances,
5861 * then postpone the current edge as well.
5862 * Otherwise, try and merge the clusters and mark "edge" as "no_merge"
5863 * if the clusters did not end up getting merged, unless the non-merge
5864 * is due to the fact that the edge was postponed. This postponement
5865 * can be recognized by a change in weight (from non-negative to negative).
5867 static isl_stat
merge_clusters_along_edge(isl_ctx
*ctx
,
5868 struct isl_sched_graph
*graph
, int edge
, struct isl_clustering
*c
)
5871 int edge_weight
= graph
->edge
[edge
].weight
;
5873 if (mark_merge_sccs(ctx
, graph
, edge
, c
) < 0)
5874 return isl_stat_error
;
5876 if (any_no_merge(graph
, c
->scc_in_merge
, &graph
->edge
[edge
]))
5877 merged
= isl_bool_false
;
5879 merged
= try_merge(ctx
, graph
, c
);
5881 return isl_stat_error
;
5882 if (!merged
&& edge_weight
== graph
->edge
[edge
].weight
)
5883 graph
->edge
[edge
].no_merge
= 1;
5888 /* Does "node" belong to the cluster identified by "cluster"?
5890 static int node_cluster_exactly(struct isl_sched_node
*node
, int cluster
)
5892 return node
->cluster
== cluster
;
5895 /* Does "edge" connect two nodes belonging to the cluster
5896 * identified by "cluster"?
5898 static int edge_cluster_exactly(struct isl_sched_edge
*edge
, int cluster
)
5900 return edge
->src
->cluster
== cluster
&& edge
->dst
->cluster
== cluster
;
5903 /* Swap the schedule of "node1" and "node2".
5904 * Both nodes have been derived from the same node in a common parent graph.
5905 * Since the "coincident" field is shared with that node
5906 * in the parent graph, there is no need to also swap this field.
5908 static void swap_sched(struct isl_sched_node
*node1
,
5909 struct isl_sched_node
*node2
)
5914 sched
= node1
->sched
;
5915 node1
->sched
= node2
->sched
;
5916 node2
->sched
= sched
;
5918 sched_map
= node1
->sched_map
;
5919 node1
->sched_map
= node2
->sched_map
;
5920 node2
->sched_map
= sched_map
;
5923 /* Copy the current band schedule from the SCCs that form the cluster
5924 * with index "pos" to the actual cluster at position "pos".
5925 * By construction, the index of the first SCC that belongs to the cluster
5928 * The order of the nodes inside both the SCCs and the cluster
5929 * is assumed to be same as the order in the original "graph".
5931 * Since the SCC graphs will no longer be used after this function,
5932 * the schedules are actually swapped rather than copied.
5934 static isl_stat
copy_partial(struct isl_sched_graph
*graph
,
5935 struct isl_clustering
*c
, int pos
)
5939 c
->cluster
[pos
].n_total_row
= c
->scc
[pos
].n_total_row
;
5940 c
->cluster
[pos
].n_row
= c
->scc
[pos
].n_row
;
5941 c
->cluster
[pos
].maxvar
= c
->scc
[pos
].maxvar
;
5943 for (i
= 0; i
< graph
->n
; ++i
) {
5947 if (graph
->node
[i
].cluster
!= pos
)
5949 s
= graph
->node
[i
].scc
;
5950 k
= c
->scc_node
[s
]++;
5951 swap_sched(&c
->cluster
[pos
].node
[j
], &c
->scc
[s
].node
[k
]);
5952 if (c
->scc
[s
].maxvar
> c
->cluster
[pos
].maxvar
)
5953 c
->cluster
[pos
].maxvar
= c
->scc
[s
].maxvar
;
5960 /* Is there a (conditional) validity dependence from node[j] to node[i],
5961 * forcing node[i] to follow node[j] or do the nodes belong to the same
5964 static isl_bool
node_follows_strong_or_same_cluster(int i
, int j
, void *user
)
5966 struct isl_sched_graph
*graph
= user
;
5968 if (graph
->node
[i
].cluster
== graph
->node
[j
].cluster
)
5969 return isl_bool_true
;
5970 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
5973 /* Extract the merged clusters of SCCs in "graph", sort them, and
5974 * store them in c->clusters. Update c->scc_cluster accordingly.
5976 * First keep track of the cluster containing the SCC to which a node
5977 * belongs in the node itself.
5978 * Then extract the clusters into c->clusters, copying the current
5979 * band schedule from the SCCs that belong to the cluster.
5980 * Do this only once per cluster.
5982 * Finally, topologically sort the clusters and update c->scc_cluster
5983 * to match the new scc numbering. While the SCCs were originally
5984 * sorted already, some SCCs that depend on some other SCCs may
5985 * have been merged with SCCs that appear before these other SCCs.
5986 * A reordering may therefore be required.
5988 static isl_stat
extract_clusters(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5989 struct isl_clustering
*c
)
5993 for (i
= 0; i
< graph
->n
; ++i
)
5994 graph
->node
[i
].cluster
= c
->scc_cluster
[graph
->node
[i
].scc
];
5996 for (i
= 0; i
< graph
->scc
; ++i
) {
5997 if (c
->scc_cluster
[i
] != i
)
5999 if (extract_sub_graph(ctx
, graph
, &node_cluster_exactly
,
6000 &edge_cluster_exactly
, i
, &c
->cluster
[i
]) < 0)
6001 return isl_stat_error
;
6002 c
->cluster
[i
].src_scc
= -1;
6003 c
->cluster
[i
].dst_scc
= -1;
6004 if (copy_partial(graph
, c
, i
) < 0)
6005 return isl_stat_error
;
6008 if (detect_ccs(ctx
, graph
, &node_follows_strong_or_same_cluster
) < 0)
6009 return isl_stat_error
;
6010 for (i
= 0; i
< graph
->n
; ++i
)
6011 c
->scc_cluster
[graph
->node
[i
].scc
] = graph
->node
[i
].cluster
;
6016 /* Compute weights on the proximity edges of "graph" that can
6017 * be used by find_proximity to find the most appropriate
6018 * proximity edge to use to merge two clusters in "c".
6019 * The weights are also used by has_bounded_distances to determine
6020 * whether the merge should be allowed.
6021 * Store the maximum of the computed weights in graph->max_weight.
6023 * The computed weight is a measure for the number of remaining schedule
6024 * dimensions that can still be completely aligned.
6025 * In particular, compute the number of equalities between
6026 * input dimensions and output dimensions in the proximity constraints.
6027 * The directions that are already handled by outer schedule bands
6028 * are projected out prior to determining this number.
6030 * Edges that will never be considered by find_proximity are ignored.
6032 static isl_stat
compute_weights(struct isl_sched_graph
*graph
,
6033 struct isl_clustering
*c
)
6037 graph
->max_weight
= 0;
6039 for (i
= 0; i
< graph
->n_edge
; ++i
) {
6040 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
6041 struct isl_sched_node
*src
= edge
->src
;
6042 struct isl_sched_node
*dst
= edge
->dst
;
6043 isl_basic_map
*hull
;
6046 if (!is_proximity(edge
))
6048 if (bad_cluster(&c
->scc
[edge
->src
->scc
]) ||
6049 bad_cluster(&c
->scc
[edge
->dst
->scc
]))
6051 if (c
->scc_cluster
[edge
->dst
->scc
] ==
6052 c
->scc_cluster
[edge
->src
->scc
])
6055 hull
= isl_map_affine_hull(isl_map_copy(edge
->map
));
6056 hull
= isl_basic_map_transform_dims(hull
, isl_dim_in
, 0,
6057 isl_mat_copy(src
->ctrans
));
6058 hull
= isl_basic_map_transform_dims(hull
, isl_dim_out
, 0,
6059 isl_mat_copy(dst
->ctrans
));
6060 hull
= isl_basic_map_project_out(hull
,
6061 isl_dim_in
, 0, src
->rank
);
6062 hull
= isl_basic_map_project_out(hull
,
6063 isl_dim_out
, 0, dst
->rank
);
6064 hull
= isl_basic_map_remove_divs(hull
);
6065 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
6066 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
6067 hull
= isl_basic_map_drop_constraints_not_involving_dims(hull
,
6068 isl_dim_in
, 0, n_in
);
6069 hull
= isl_basic_map_drop_constraints_not_involving_dims(hull
,
6070 isl_dim_out
, 0, n_out
);
6072 return isl_stat_error
;
6073 edge
->weight
= hull
->n_eq
;
6074 isl_basic_map_free(hull
);
6076 if (edge
->weight
> graph
->max_weight
)
6077 graph
->max_weight
= edge
->weight
;
6083 /* Call compute_schedule_finish_band on each of the clusters in "c"
6084 * in their topological order. This order is determined by the scc
6085 * fields of the nodes in "graph".
6086 * Combine the results in a sequence expressing the topological order.
6088 * If there is only one cluster left, then there is no need to introduce
6089 * a sequence node. Also, in this case, the cluster necessarily contains
6090 * the SCC at position 0 in the original graph and is therefore also
6091 * stored in the first cluster of "c".
6093 static __isl_give isl_schedule_node
*finish_bands_clustering(
6094 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
6095 struct isl_clustering
*c
)
6099 isl_union_set_list
*filters
;
6101 if (graph
->scc
== 1)
6102 return compute_schedule_finish_band(node
, &c
->cluster
[0], 0);
6104 ctx
= isl_schedule_node_get_ctx(node
);
6106 filters
= extract_sccs(ctx
, graph
);
6107 node
= isl_schedule_node_insert_sequence(node
, filters
);
6109 for (i
= 0; i
< graph
->scc
; ++i
) {
6110 int j
= c
->scc_cluster
[i
];
6111 node
= isl_schedule_node_child(node
, i
);
6112 node
= isl_schedule_node_child(node
, 0);
6113 node
= compute_schedule_finish_band(node
, &c
->cluster
[j
], 0);
6114 node
= isl_schedule_node_parent(node
);
6115 node
= isl_schedule_node_parent(node
);
6121 /* Compute a schedule for a connected dependence graph by first considering
6122 * each strongly connected component (SCC) in the graph separately and then
6123 * incrementally combining them into clusters.
6124 * Return the updated schedule node.
6126 * Initially, each cluster consists of a single SCC, each with its
6127 * own band schedule. The algorithm then tries to merge pairs
6128 * of clusters along a proximity edge until no more suitable
6129 * proximity edges can be found. During this merging, the schedule
6130 * is maintained in the individual SCCs.
6131 * After the merging is completed, the full resulting clusters
6132 * are extracted and in finish_bands_clustering,
6133 * compute_schedule_finish_band is called on each of them to integrate
6134 * the band into "node" and to continue the computation.
6136 * compute_weights initializes the weights that are used by find_proximity.
6138 static __isl_give isl_schedule_node
*compute_schedule_wcc_clustering(
6139 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
6142 struct isl_clustering c
;
6145 ctx
= isl_schedule_node_get_ctx(node
);
6147 if (clustering_init(ctx
, &c
, graph
) < 0)
6150 if (compute_weights(graph
, &c
) < 0)
6154 i
= find_proximity(graph
, &c
);
6157 if (i
>= graph
->n_edge
)
6159 if (merge_clusters_along_edge(ctx
, graph
, i
, &c
) < 0)
6163 if (extract_clusters(ctx
, graph
, &c
) < 0)
6166 node
= finish_bands_clustering(node
, graph
, &c
);
6168 clustering_free(ctx
, &c
);
6171 clustering_free(ctx
, &c
);
6172 return isl_schedule_node_free(node
);
6175 /* Compute a schedule for a connected dependence graph and return
6176 * the updated schedule node.
6178 * If Feautrier's algorithm is selected, we first recursively try to satisfy
6179 * as many validity dependences as possible. When all validity dependences
6180 * are satisfied we extend the schedule to a full-dimensional schedule.
6182 * Call compute_schedule_wcc_whole or compute_schedule_wcc_clustering
6183 * depending on whether the user has selected the option to try and
6184 * compute a schedule for the entire (weakly connected) component first.
6185 * If there is only a single strongly connected component (SCC), then
6186 * there is no point in trying to combine SCCs
6187 * in compute_schedule_wcc_clustering, so compute_schedule_wcc_whole
6188 * is called instead.
6190 static __isl_give isl_schedule_node
*compute_schedule_wcc(
6191 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
6198 ctx
= isl_schedule_node_get_ctx(node
);
6199 if (detect_sccs(ctx
, graph
) < 0)
6200 return isl_schedule_node_free(node
);
6202 if (compute_maxvar(graph
) < 0)
6203 return isl_schedule_node_free(node
);
6205 if (need_feautrier_step(ctx
, graph
))
6206 return compute_schedule_wcc_feautrier(node
, graph
);
6208 if (graph
->scc
<= 1 || isl_options_get_schedule_whole_component(ctx
))
6209 return compute_schedule_wcc_whole(node
, graph
);
6211 return compute_schedule_wcc_clustering(node
, graph
);
6214 /* Compute a schedule for each group of nodes identified by node->scc
6215 * separately and then combine them in a sequence node (or as set node
6216 * if graph->weak is set) inserted at position "node" of the schedule tree.
6217 * Return the updated schedule node.
6219 * If "wcc" is set then each of the groups belongs to a single
6220 * weakly connected component in the dependence graph so that
6221 * there is no need for compute_sub_schedule to look for weakly
6222 * connected components.
6224 static __isl_give isl_schedule_node
*compute_component_schedule(
6225 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
6230 isl_union_set_list
*filters
;
6234 ctx
= isl_schedule_node_get_ctx(node
);
6236 filters
= extract_sccs(ctx
, graph
);
6238 node
= isl_schedule_node_insert_set(node
, filters
);
6240 node
= isl_schedule_node_insert_sequence(node
, filters
);
6242 for (component
= 0; component
< graph
->scc
; ++component
) {
6243 node
= isl_schedule_node_child(node
, component
);
6244 node
= isl_schedule_node_child(node
, 0);
6245 node
= compute_sub_schedule(node
, ctx
, graph
,
6247 &edge_scc_exactly
, component
, wcc
);
6248 node
= isl_schedule_node_parent(node
);
6249 node
= isl_schedule_node_parent(node
);
6255 /* Compute a schedule for the given dependence graph and insert it at "node".
6256 * Return the updated schedule node.
6258 * We first check if the graph is connected (through validity and conditional
6259 * validity dependences) and, if not, compute a schedule
6260 * for each component separately.
6261 * If the schedule_serialize_sccs option is set, then we check for strongly
6262 * connected components instead and compute a separate schedule for
6263 * each such strongly connected component.
6265 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
6266 struct isl_sched_graph
*graph
)
6273 ctx
= isl_schedule_node_get_ctx(node
);
6274 if (isl_options_get_schedule_serialize_sccs(ctx
)) {
6275 if (detect_sccs(ctx
, graph
) < 0)
6276 return isl_schedule_node_free(node
);
6278 if (detect_wccs(ctx
, graph
) < 0)
6279 return isl_schedule_node_free(node
);
6283 return compute_component_schedule(node
, graph
, 1);
6285 return compute_schedule_wcc(node
, graph
);
6288 /* Compute a schedule on sc->domain that respects the given schedule
6291 * In particular, the schedule respects all the validity dependences.
6292 * If the default isl scheduling algorithm is used, it tries to minimize
6293 * the dependence distances over the proximity dependences.
6294 * If Feautrier's scheduling algorithm is used, the proximity dependence
6295 * distances are only minimized during the extension to a full-dimensional
6298 * If there are any condition and conditional validity dependences,
6299 * then the conditional validity dependences may be violated inside
6300 * a tilable band, provided they have no adjacent non-local
6301 * condition dependences.
6303 __isl_give isl_schedule
*isl_schedule_constraints_compute_schedule(
6304 __isl_take isl_schedule_constraints
*sc
)
6306 isl_ctx
*ctx
= isl_schedule_constraints_get_ctx(sc
);
6307 struct isl_sched_graph graph
= { 0 };
6308 isl_schedule
*sched
;
6309 isl_schedule_node
*node
;
6310 isl_union_set
*domain
;
6312 sc
= isl_schedule_constraints_align_params(sc
);
6314 domain
= isl_schedule_constraints_get_domain(sc
);
6315 if (isl_union_set_n_set(domain
) == 0) {
6316 isl_schedule_constraints_free(sc
);
6317 return isl_schedule_from_domain(domain
);
6320 if (graph_init(&graph
, sc
) < 0)
6321 domain
= isl_union_set_free(domain
);
6323 node
= isl_schedule_node_from_domain(domain
);
6324 node
= isl_schedule_node_child(node
, 0);
6326 node
= compute_schedule(node
, &graph
);
6327 sched
= isl_schedule_node_get_schedule(node
);
6328 isl_schedule_node_free(node
);
6330 graph_free(ctx
, &graph
);
6331 isl_schedule_constraints_free(sc
);
6336 /* Compute a schedule for the given union of domains that respects
6337 * all the validity dependences and minimizes
6338 * the dependence distances over the proximity dependences.
6340 * This function is kept for backward compatibility.
6342 __isl_give isl_schedule
*isl_union_set_compute_schedule(
6343 __isl_take isl_union_set
*domain
,
6344 __isl_take isl_union_map
*validity
,
6345 __isl_take isl_union_map
*proximity
)
6347 isl_schedule_constraints
*sc
;
6349 sc
= isl_schedule_constraints_on_domain(domain
);
6350 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
6351 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
6353 return isl_schedule_constraints_compute_schedule(sc
);