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 /* Allocate the edge_tables based on the maximal number of edges of
445 static int graph_init_edge_tables(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
449 for (i
= 0; i
<= isl_edge_last
; ++i
) {
450 graph
->edge_table
[i
] = isl_hash_table_alloc(ctx
,
452 if (!graph
->edge_table
[i
])
459 /* If graph->edge_table[type] contains an edge from the given source
460 * to the given destination, then return the hash table entry of this edge.
461 * Otherwise, return NULL.
463 static struct isl_hash_table_entry
*graph_find_edge_entry(
464 struct isl_sched_graph
*graph
,
465 enum isl_edge_type type
,
466 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
468 isl_ctx
*ctx
= isl_space_get_ctx(src
->space
);
470 struct isl_sched_edge temp
= { .src
= src
, .dst
= dst
};
472 hash
= isl_hash_init();
473 hash
= isl_hash_builtin(hash
, temp
.src
);
474 hash
= isl_hash_builtin(hash
, temp
.dst
);
475 return isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
476 &edge_has_src_and_dst
, &temp
, 0);
480 /* If graph->edge_table[type] contains an edge from the given source
481 * to the given destination, then return this edge.
482 * Otherwise, return NULL.
484 static struct isl_sched_edge
*graph_find_edge(struct isl_sched_graph
*graph
,
485 enum isl_edge_type type
,
486 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
488 struct isl_hash_table_entry
*entry
;
490 entry
= graph_find_edge_entry(graph
, type
, src
, dst
);
497 /* Check whether the dependence graph has an edge of the given type
498 * between the given two nodes.
500 static isl_bool
graph_has_edge(struct isl_sched_graph
*graph
,
501 enum isl_edge_type type
,
502 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
504 struct isl_sched_edge
*edge
;
507 edge
= graph_find_edge(graph
, type
, src
, dst
);
511 empty
= isl_map_plain_is_empty(edge
->map
);
513 return isl_bool_error
;
518 /* Look for any edge with the same src, dst and map fields as "model".
520 * Return the matching edge if one can be found.
521 * Return "model" if no matching edge is found.
522 * Return NULL on error.
524 static struct isl_sched_edge
*graph_find_matching_edge(
525 struct isl_sched_graph
*graph
, struct isl_sched_edge
*model
)
527 enum isl_edge_type i
;
528 struct isl_sched_edge
*edge
;
530 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
533 edge
= graph_find_edge(graph
, i
, model
->src
, model
->dst
);
536 is_equal
= isl_map_plain_is_equal(model
->map
, edge
->map
);
546 /* Remove the given edge from all the edge_tables that refer to it.
548 static void graph_remove_edge(struct isl_sched_graph
*graph
,
549 struct isl_sched_edge
*edge
)
551 isl_ctx
*ctx
= isl_map_get_ctx(edge
->map
);
552 enum isl_edge_type i
;
554 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
555 struct isl_hash_table_entry
*entry
;
557 entry
= graph_find_edge_entry(graph
, i
, edge
->src
, edge
->dst
);
560 if (entry
->data
!= edge
)
562 isl_hash_table_remove(ctx
, graph
->edge_table
[i
], entry
);
566 /* Check whether the dependence graph has any edge
567 * between the given two nodes.
569 static isl_bool
graph_has_any_edge(struct isl_sched_graph
*graph
,
570 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
572 enum isl_edge_type i
;
575 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
576 r
= graph_has_edge(graph
, i
, src
, dst
);
584 /* Check whether the dependence graph has a validity edge
585 * between the given two nodes.
587 * Conditional validity edges are essentially validity edges that
588 * can be ignored if the corresponding condition edges are iteration private.
589 * Here, we are only checking for the presence of validity
590 * edges, so we need to consider the conditional validity edges too.
591 * In particular, this function is used during the detection
592 * of strongly connected components and we cannot ignore
593 * conditional validity edges during this detection.
595 static isl_bool
graph_has_validity_edge(struct isl_sched_graph
*graph
,
596 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
600 r
= graph_has_edge(graph
, isl_edge_validity
, src
, dst
);
604 return graph_has_edge(graph
, isl_edge_conditional_validity
, src
, dst
);
607 static int graph_alloc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
608 int n_node
, int n_edge
)
613 graph
->n_edge
= n_edge
;
614 graph
->node
= isl_calloc_array(ctx
, struct isl_sched_node
, graph
->n
);
615 graph
->sorted
= isl_calloc_array(ctx
, int, graph
->n
);
616 graph
->region
= isl_alloc_array(ctx
, struct isl_region
, graph
->n
);
617 graph
->edge
= isl_calloc_array(ctx
,
618 struct isl_sched_edge
, graph
->n_edge
);
620 graph
->intra_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
621 graph
->inter_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
623 if (!graph
->node
|| !graph
->region
|| (graph
->n_edge
&& !graph
->edge
) ||
627 for(i
= 0; i
< graph
->n
; ++i
)
628 graph
->sorted
[i
] = i
;
633 static void graph_free(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
637 isl_map_to_basic_set_free(graph
->intra_hmap
);
638 isl_map_to_basic_set_free(graph
->inter_hmap
);
641 for (i
= 0; i
< graph
->n
; ++i
) {
642 isl_space_free(graph
->node
[i
].space
);
643 isl_set_free(graph
->node
[i
].hull
);
644 isl_multi_aff_free(graph
->node
[i
].compress
);
645 isl_multi_aff_free(graph
->node
[i
].decompress
);
646 isl_mat_free(graph
->node
[i
].sched
);
647 isl_map_free(graph
->node
[i
].sched_map
);
648 isl_mat_free(graph
->node
[i
].cmap
);
649 isl_mat_free(graph
->node
[i
].cinv
);
650 isl_mat_free(graph
->node
[i
].ctrans
);
652 free(graph
->node
[i
].coincident
);
653 isl_multi_val_free(graph
->node
[i
].sizes
);
654 isl_vec_free(graph
->node
[i
].max
);
659 for (i
= 0; i
< graph
->n_edge
; ++i
) {
660 isl_map_free(graph
->edge
[i
].map
);
661 isl_union_map_free(graph
->edge
[i
].tagged_condition
);
662 isl_union_map_free(graph
->edge
[i
].tagged_validity
);
666 for (i
= 0; i
<= isl_edge_last
; ++i
)
667 isl_hash_table_free(ctx
, graph
->edge_table
[i
]);
668 isl_hash_table_free(ctx
, graph
->node_table
);
669 isl_basic_set_free(graph
->lp
);
672 /* For each "set" on which this function is called, increment
673 * graph->n by one and update graph->maxvar.
675 static isl_stat
init_n_maxvar(__isl_take isl_set
*set
, void *user
)
677 struct isl_sched_graph
*graph
= user
;
678 int nvar
= isl_set_dim(set
, isl_dim_set
);
681 if (nvar
> graph
->maxvar
)
682 graph
->maxvar
= nvar
;
689 /* Compute the number of rows that should be allocated for the schedule.
690 * In particular, we need one row for each variable or one row
691 * for each basic map in the dependences.
692 * Note that it is practically impossible to exhaust both
693 * the number of dependences and the number of variables.
695 static isl_stat
compute_max_row(struct isl_sched_graph
*graph
,
696 __isl_keep isl_schedule_constraints
*sc
)
700 isl_union_set
*domain
;
704 domain
= isl_schedule_constraints_get_domain(sc
);
705 r
= isl_union_set_foreach_set(domain
, &init_n_maxvar
, graph
);
706 isl_union_set_free(domain
);
708 return isl_stat_error
;
709 n_edge
= isl_schedule_constraints_n_basic_map(sc
);
711 return isl_stat_error
;
712 graph
->max_row
= n_edge
+ graph
->maxvar
;
717 /* Does "bset" have any defining equalities for its set variables?
719 static int has_any_defining_equality(__isl_keep isl_basic_set
*bset
)
726 n
= isl_basic_set_dim(bset
, isl_dim_set
);
727 for (i
= 0; i
< n
; ++i
) {
730 has
= isl_basic_set_has_defining_equality(bset
, isl_dim_set
, i
,
739 /* Set the entries of node->max to the value of the schedule_max_coefficient
742 static isl_stat
set_max_coefficient(isl_ctx
*ctx
, struct isl_sched_node
*node
)
746 max
= isl_options_get_schedule_max_coefficient(ctx
);
750 node
->max
= isl_vec_alloc(ctx
, node
->nvar
);
751 node
->max
= isl_vec_set_si(node
->max
, max
);
753 return isl_stat_error
;
758 /* Set the entries of node->max to the minimum of the schedule_max_coefficient
759 * option (if set) and half of the minimum of the sizes in the other
760 * dimensions. If the minimum of the sizes is one, half of the size
761 * is zero and this value is reset to one.
762 * If the global minimum is unbounded (i.e., if both
763 * the schedule_max_coefficient is not set and the sizes in the other
764 * dimensions are unbounded), then store a negative value.
765 * If the schedule coefficient is close to the size of the instance set
766 * in another dimension, then the schedule may represent a loop
767 * coalescing transformation (especially if the coefficient
768 * in that other dimension is one). Forcing the coefficient to be
769 * smaller than or equal to half the minimal size should avoid this
772 static isl_stat
compute_max_coefficient(isl_ctx
*ctx
,
773 struct isl_sched_node
*node
)
779 max
= isl_options_get_schedule_max_coefficient(ctx
);
780 v
= isl_vec_alloc(ctx
, node
->nvar
);
782 return isl_stat_error
;
784 for (i
= 0; i
< node
->nvar
; ++i
) {
785 isl_int_set_si(v
->el
[i
], max
);
786 isl_int_mul_si(v
->el
[i
], v
->el
[i
], 2);
789 for (i
= 0; i
< node
->nvar
; ++i
) {
792 size
= isl_multi_val_get_val(node
->sizes
, i
);
795 if (!isl_val_is_int(size
)) {
799 for (j
= 0; j
< node
->nvar
; ++j
) {
802 if (isl_int_is_neg(v
->el
[j
]) ||
803 isl_int_gt(v
->el
[j
], size
->n
))
804 isl_int_set(v
->el
[j
], size
->n
);
809 for (i
= 0; i
< node
->nvar
; ++i
) {
810 isl_int_fdiv_q_ui(v
->el
[i
], v
->el
[i
], 2);
811 if (isl_int_is_zero(v
->el
[i
]))
812 isl_int_set_si(v
->el
[i
], 1);
819 return isl_stat_error
;
822 /* Compute and return the size of "set" in dimension "dim".
823 * The size is taken to be the difference in values for that variable
824 * for fixed values of the other variables.
825 * In particular, the variable is first isolated from the other variables
826 * in the range of a map
828 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [i_dim]
830 * and then duplicated
832 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [[i_dim] -> [i_dim']]
834 * The shared variables are then projected out and the maximal value
835 * of i_dim' - i_dim is computed.
837 static __isl_give isl_val
*compute_size(__isl_take isl_set
*set
, int dim
)
844 map
= isl_set_project_onto_map(set
, isl_dim_set
, dim
, 1);
845 map
= isl_map_project_out(map
, isl_dim_in
, dim
, 1);
846 map
= isl_map_range_product(map
, isl_map_copy(map
));
847 map
= isl_set_unwrap(isl_map_range(map
));
848 set
= isl_map_deltas(map
);
849 ls
= isl_local_space_from_space(isl_set_get_space(set
));
850 obj
= isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
851 v
= isl_set_max_val(set
, obj
);
858 /* Compute the size of the instance set "set" of "node", after compression,
859 * as well as bounds on the corresponding coefficients, if needed.
861 * The sizes are needed when the schedule_treat_coalescing option is set.
862 * The bounds are needed when the schedule_treat_coalescing option or
863 * the schedule_max_coefficient option is set.
865 * If the schedule_treat_coalescing option is not set, then at most
866 * the bounds need to be set and this is done in set_max_coefficient.
867 * Otherwise, compress the domain if needed, compute the size
868 * in each direction and store the results in node->size.
869 * Finally, set the bounds on the coefficients based on the sizes
870 * and the schedule_max_coefficient option in compute_max_coefficient.
872 static isl_stat
compute_sizes_and_max(isl_ctx
*ctx
, struct isl_sched_node
*node
,
873 __isl_take isl_set
*set
)
878 if (!isl_options_get_schedule_treat_coalescing(ctx
)) {
880 return set_max_coefficient(ctx
, node
);
883 if (node
->compressed
)
884 set
= isl_set_preimage_multi_aff(set
,
885 isl_multi_aff_copy(node
->decompress
));
886 mv
= isl_multi_val_zero(isl_set_get_space(set
));
887 n
= isl_set_dim(set
, isl_dim_set
);
888 for (j
= 0; j
< n
; ++j
) {
891 v
= compute_size(isl_set_copy(set
), j
);
892 mv
= isl_multi_val_set_val(mv
, j
, v
);
897 return isl_stat_error
;
898 return compute_max_coefficient(ctx
, node
);
901 /* Add a new node to the graph representing the given instance set.
902 * "nvar" is the (possibly compressed) number of variables and
903 * may be smaller than then number of set variables in "set"
904 * if "compressed" is set.
905 * If "compressed" is set, then "hull" represents the constraints
906 * that were used to derive the compression, while "compress" and
907 * "decompress" map the original space to the compressed space and
909 * If "compressed" is not set, then "hull", "compress" and "decompress"
912 * Compute the size of the instance set and bounds on the coefficients,
915 static isl_stat
add_node(struct isl_sched_graph
*graph
,
916 __isl_take isl_set
*set
, int nvar
, int compressed
,
917 __isl_take isl_set
*hull
, __isl_take isl_multi_aff
*compress
,
918 __isl_take isl_multi_aff
*decompress
)
925 struct isl_sched_node
*node
;
928 return isl_stat_error
;
930 ctx
= isl_set_get_ctx(set
);
931 nparam
= isl_set_dim(set
, isl_dim_param
);
932 if (!ctx
->opt
->schedule_parametric
)
934 sched
= isl_mat_alloc(ctx
, 0, 1 + nparam
+ nvar
);
935 node
= &graph
->node
[graph
->n
];
937 space
= isl_set_get_space(set
);
940 node
->nparam
= nparam
;
942 node
->sched_map
= NULL
;
943 coincident
= isl_calloc_array(ctx
, int, graph
->max_row
);
944 node
->coincident
= coincident
;
945 node
->compressed
= compressed
;
947 node
->compress
= compress
;
948 node
->decompress
= decompress
;
949 if (compute_sizes_and_max(ctx
, node
, set
) < 0)
950 return isl_stat_error
;
952 if (!space
|| !sched
|| (graph
->max_row
&& !coincident
))
953 return isl_stat_error
;
954 if (compressed
&& (!hull
|| !compress
|| !decompress
))
955 return isl_stat_error
;
960 /* Add a new node to the graph representing the given set.
962 * If any of the set variables is defined by an equality, then
963 * we perform variable compression such that we can perform
964 * the scheduling on the compressed domain.
966 static isl_stat
extract_node(__isl_take isl_set
*set
, void *user
)
973 isl_multi_aff
*compress
, *decompress
;
974 struct isl_sched_graph
*graph
= user
;
976 hull
= isl_set_affine_hull(isl_set_copy(set
));
977 hull
= isl_basic_set_remove_divs(hull
);
978 nvar
= isl_set_dim(set
, isl_dim_set
);
979 has_equality
= has_any_defining_equality(hull
);
981 if (has_equality
< 0)
984 isl_basic_set_free(hull
);
985 return add_node(graph
, set
, nvar
, 0, NULL
, NULL
, NULL
);
988 morph
= isl_basic_set_variable_compression(hull
, isl_dim_set
);
989 nvar
= isl_morph_ran_dim(morph
, isl_dim_set
);
990 compress
= isl_morph_get_var_multi_aff(morph
);
991 morph
= isl_morph_inverse(morph
);
992 decompress
= isl_morph_get_var_multi_aff(morph
);
993 isl_morph_free(morph
);
995 hull_set
= isl_set_from_basic_set(hull
);
996 return add_node(graph
, set
, nvar
, 1, hull_set
, compress
, decompress
);
998 isl_basic_set_free(hull
);
1000 return isl_stat_error
;
1003 struct isl_extract_edge_data
{
1004 enum isl_edge_type type
;
1005 struct isl_sched_graph
*graph
;
1008 /* Merge edge2 into edge1, freeing the contents of edge2.
1009 * Return 0 on success and -1 on failure.
1011 * edge1 and edge2 are assumed to have the same value for the map field.
1013 static int merge_edge(struct isl_sched_edge
*edge1
,
1014 struct isl_sched_edge
*edge2
)
1016 edge1
->types
|= edge2
->types
;
1017 isl_map_free(edge2
->map
);
1019 if (is_condition(edge2
)) {
1020 if (!edge1
->tagged_condition
)
1021 edge1
->tagged_condition
= edge2
->tagged_condition
;
1023 edge1
->tagged_condition
=
1024 isl_union_map_union(edge1
->tagged_condition
,
1025 edge2
->tagged_condition
);
1028 if (is_conditional_validity(edge2
)) {
1029 if (!edge1
->tagged_validity
)
1030 edge1
->tagged_validity
= edge2
->tagged_validity
;
1032 edge1
->tagged_validity
=
1033 isl_union_map_union(edge1
->tagged_validity
,
1034 edge2
->tagged_validity
);
1037 if (is_condition(edge2
) && !edge1
->tagged_condition
)
1039 if (is_conditional_validity(edge2
) && !edge1
->tagged_validity
)
1045 /* Insert dummy tags in domain and range of "map".
1047 * In particular, if "map" is of the form
1053 * [A -> dummy_tag] -> [B -> dummy_tag]
1055 * where the dummy_tags are identical and equal to any dummy tags
1056 * introduced by any other call to this function.
1058 static __isl_give isl_map
*insert_dummy_tags(__isl_take isl_map
*map
)
1064 isl_set
*domain
, *range
;
1066 ctx
= isl_map_get_ctx(map
);
1068 id
= isl_id_alloc(ctx
, NULL
, &dummy
);
1069 space
= isl_space_params(isl_map_get_space(map
));
1070 space
= isl_space_set_from_params(space
);
1071 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
1072 space
= isl_space_map_from_set(space
);
1074 domain
= isl_map_wrap(map
);
1075 range
= isl_map_wrap(isl_map_universe(space
));
1076 map
= isl_map_from_domain_and_range(domain
, range
);
1077 map
= isl_map_zip(map
);
1082 /* Given that at least one of "src" or "dst" is compressed, return
1083 * a map between the spaces of these nodes restricted to the affine
1084 * hull that was used in the compression.
1086 static __isl_give isl_map
*extract_hull(struct isl_sched_node
*src
,
1087 struct isl_sched_node
*dst
)
1091 if (src
->compressed
)
1092 dom
= isl_set_copy(src
->hull
);
1094 dom
= isl_set_universe(isl_space_copy(src
->space
));
1095 if (dst
->compressed
)
1096 ran
= isl_set_copy(dst
->hull
);
1098 ran
= isl_set_universe(isl_space_copy(dst
->space
));
1100 return isl_map_from_domain_and_range(dom
, ran
);
1103 /* Intersect the domains of the nested relations in domain and range
1104 * of "tagged" with "map".
1106 static __isl_give isl_map
*map_intersect_domains(__isl_take isl_map
*tagged
,
1107 __isl_keep isl_map
*map
)
1111 tagged
= isl_map_zip(tagged
);
1112 set
= isl_map_wrap(isl_map_copy(map
));
1113 tagged
= isl_map_intersect_domain(tagged
, set
);
1114 tagged
= isl_map_zip(tagged
);
1118 /* Return a pointer to the node that lives in the domain space of "map"
1119 * or NULL if there is no such node.
1121 static struct isl_sched_node
*find_domain_node(isl_ctx
*ctx
,
1122 struct isl_sched_graph
*graph
, __isl_keep isl_map
*map
)
1124 struct isl_sched_node
*node
;
1127 space
= isl_space_domain(isl_map_get_space(map
));
1128 node
= graph_find_node(ctx
, graph
, space
);
1129 isl_space_free(space
);
1134 /* Return a pointer to the node that lives in the range space of "map"
1135 * or NULL if there is no such node.
1137 static struct isl_sched_node
*find_range_node(isl_ctx
*ctx
,
1138 struct isl_sched_graph
*graph
, __isl_keep isl_map
*map
)
1140 struct isl_sched_node
*node
;
1143 space
= isl_space_range(isl_map_get_space(map
));
1144 node
= graph_find_node(ctx
, graph
, space
);
1145 isl_space_free(space
);
1150 /* Add a new edge to the graph based on the given map
1151 * and add it to data->graph->edge_table[data->type].
1152 * If a dependence relation of a given type happens to be identical
1153 * to one of the dependence relations of a type that was added before,
1154 * then we don't create a new edge, but instead mark the original edge
1155 * as also representing a dependence of the current type.
1157 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1158 * may be specified as "tagged" dependence relations. That is, "map"
1159 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1160 * the dependence on iterations and a and b are tags.
1161 * edge->map is set to the relation containing the elements i -> j,
1162 * while edge->tagged_condition and edge->tagged_validity contain
1163 * the union of all the "map" relations
1164 * for which extract_edge is called that result in the same edge->map.
1166 * If the source or the destination node is compressed, then
1167 * intersect both "map" and "tagged" with the constraints that
1168 * were used to construct the compression.
1169 * This ensures that there are no schedule constraints defined
1170 * outside of these domains, while the scheduler no longer has
1171 * any control over those outside parts.
1173 static isl_stat
extract_edge(__isl_take isl_map
*map
, void *user
)
1175 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1176 struct isl_extract_edge_data
*data
= user
;
1177 struct isl_sched_graph
*graph
= data
->graph
;
1178 struct isl_sched_node
*src
, *dst
;
1179 struct isl_sched_edge
*edge
;
1180 isl_map
*tagged
= NULL
;
1182 if (data
->type
== isl_edge_condition
||
1183 data
->type
== isl_edge_conditional_validity
) {
1184 if (isl_map_can_zip(map
)) {
1185 tagged
= isl_map_copy(map
);
1186 map
= isl_set_unwrap(isl_map_domain(isl_map_zip(map
)));
1188 tagged
= insert_dummy_tags(isl_map_copy(map
));
1192 src
= find_domain_node(ctx
, graph
, map
);
1193 dst
= find_range_node(ctx
, graph
, map
);
1197 isl_map_free(tagged
);
1201 if (src
->compressed
|| dst
->compressed
) {
1203 hull
= extract_hull(src
, dst
);
1205 tagged
= map_intersect_domains(tagged
, hull
);
1206 map
= isl_map_intersect(map
, hull
);
1209 graph
->edge
[graph
->n_edge
].src
= src
;
1210 graph
->edge
[graph
->n_edge
].dst
= dst
;
1211 graph
->edge
[graph
->n_edge
].map
= map
;
1212 graph
->edge
[graph
->n_edge
].types
= 0;
1213 graph
->edge
[graph
->n_edge
].tagged_condition
= NULL
;
1214 graph
->edge
[graph
->n_edge
].tagged_validity
= NULL
;
1215 set_type(&graph
->edge
[graph
->n_edge
], data
->type
);
1216 if (data
->type
== isl_edge_condition
)
1217 graph
->edge
[graph
->n_edge
].tagged_condition
=
1218 isl_union_map_from_map(tagged
);
1219 if (data
->type
== isl_edge_conditional_validity
)
1220 graph
->edge
[graph
->n_edge
].tagged_validity
=
1221 isl_union_map_from_map(tagged
);
1223 edge
= graph_find_matching_edge(graph
, &graph
->edge
[graph
->n_edge
]);
1226 return isl_stat_error
;
1228 if (edge
== &graph
->edge
[graph
->n_edge
])
1229 return graph_edge_table_add(ctx
, graph
, data
->type
,
1230 &graph
->edge
[graph
->n_edge
++]);
1232 if (merge_edge(edge
, &graph
->edge
[graph
->n_edge
]) < 0)
1235 return graph_edge_table_add(ctx
, graph
, data
->type
, edge
);
1238 /* Initialize the schedule graph "graph" from the schedule constraints "sc".
1240 * The context is included in the domain before the nodes of
1241 * the graphs are extracted in order to be able to exploit
1242 * any possible additional equalities.
1243 * Note that this intersection is only performed locally here.
1245 static isl_stat
graph_init(struct isl_sched_graph
*graph
,
1246 __isl_keep isl_schedule_constraints
*sc
)
1249 isl_union_set
*domain
;
1251 struct isl_extract_edge_data data
;
1252 enum isl_edge_type i
;
1256 return isl_stat_error
;
1258 ctx
= isl_schedule_constraints_get_ctx(sc
);
1260 domain
= isl_schedule_constraints_get_domain(sc
);
1261 graph
->n
= isl_union_set_n_set(domain
);
1262 isl_union_set_free(domain
);
1264 if (graph_alloc(ctx
, graph
, graph
->n
,
1265 isl_schedule_constraints_n_map(sc
)) < 0)
1266 return isl_stat_error
;
1268 if (compute_max_row(graph
, sc
) < 0)
1269 return isl_stat_error
;
1272 domain
= isl_schedule_constraints_get_domain(sc
);
1273 domain
= isl_union_set_intersect_params(domain
,
1274 isl_schedule_constraints_get_context(sc
));
1275 r
= isl_union_set_foreach_set(domain
, &extract_node
, graph
);
1276 isl_union_set_free(domain
);
1278 return isl_stat_error
;
1279 if (graph_init_table(ctx
, graph
) < 0)
1280 return isl_stat_error
;
1281 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
1282 c
= isl_schedule_constraints_get(sc
, i
);
1283 graph
->max_edge
[i
] = isl_union_map_n_map(c
);
1284 isl_union_map_free(c
);
1286 return isl_stat_error
;
1288 if (graph_init_edge_tables(ctx
, graph
) < 0)
1289 return isl_stat_error
;
1292 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
1296 c
= isl_schedule_constraints_get(sc
, i
);
1297 r
= isl_union_map_foreach_map(c
, &extract_edge
, &data
);
1298 isl_union_map_free(c
);
1300 return isl_stat_error
;
1306 /* Check whether there is any dependence from node[j] to node[i]
1307 * or from node[i] to node[j].
1309 static isl_bool
node_follows_weak(int i
, int j
, void *user
)
1312 struct isl_sched_graph
*graph
= user
;
1314 f
= graph_has_any_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1317 return graph_has_any_edge(graph
, &graph
->node
[i
], &graph
->node
[j
]);
1320 /* Check whether there is a (conditional) validity dependence from node[j]
1321 * to node[i], forcing node[i] to follow node[j].
1323 static isl_bool
node_follows_strong(int i
, int j
, void *user
)
1325 struct isl_sched_graph
*graph
= user
;
1327 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1330 /* Use Tarjan's algorithm for computing the strongly connected components
1331 * in the dependence graph only considering those edges defined by "follows".
1333 static int detect_ccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
1334 isl_bool (*follows
)(int i
, int j
, void *user
))
1337 struct isl_tarjan_graph
*g
= NULL
;
1339 g
= isl_tarjan_graph_init(ctx
, graph
->n
, follows
, graph
);
1347 while (g
->order
[i
] != -1) {
1348 graph
->node
[g
->order
[i
]].scc
= graph
->scc
;
1356 isl_tarjan_graph_free(g
);
1361 /* Apply Tarjan's algorithm to detect the strongly connected components
1362 * in the dependence graph.
1363 * Only consider the (conditional) validity dependences and clear "weak".
1365 static int detect_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1368 return detect_ccs(ctx
, graph
, &node_follows_strong
);
1371 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1372 * in the dependence graph.
1373 * Consider all dependences and set "weak".
1375 static int detect_wccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1378 return detect_ccs(ctx
, graph
, &node_follows_weak
);
1381 static int cmp_scc(const void *a
, const void *b
, void *data
)
1383 struct isl_sched_graph
*graph
= data
;
1387 return graph
->node
[*i1
].scc
- graph
->node
[*i2
].scc
;
1390 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1392 static int sort_sccs(struct isl_sched_graph
*graph
)
1394 return isl_sort(graph
->sorted
, graph
->n
, sizeof(int), &cmp_scc
, graph
);
1397 /* Given a dependence relation R from "node" to itself,
1398 * construct the set of coefficients of valid constraints for elements
1399 * in that dependence relation.
1400 * In particular, the result contains tuples of coefficients
1401 * c_0, c_n, c_x such that
1403 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1407 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1409 * We choose here to compute the dual of delta R.
1410 * Alternatively, we could have computed the dual of R, resulting
1411 * in a set of tuples c_0, c_n, c_x, c_y, and then
1412 * plugged in (c_0, c_n, c_x, -c_x).
1414 * If "node" has been compressed, then the dependence relation
1415 * is also compressed before the set of coefficients is computed.
1417 static __isl_give isl_basic_set
*intra_coefficients(
1418 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1419 __isl_take isl_map
*map
)
1423 isl_basic_set
*coef
;
1424 isl_maybe_isl_basic_set m
;
1426 m
= isl_map_to_basic_set_try_get(graph
->intra_hmap
, map
);
1427 if (m
.valid
< 0 || m
.valid
) {
1432 key
= isl_map_copy(map
);
1433 if (node
->compressed
) {
1434 map
= isl_map_preimage_domain_multi_aff(map
,
1435 isl_multi_aff_copy(node
->decompress
));
1436 map
= isl_map_preimage_range_multi_aff(map
,
1437 isl_multi_aff_copy(node
->decompress
));
1439 delta
= isl_set_remove_divs(isl_map_deltas(map
));
1440 coef
= isl_set_coefficients(delta
);
1441 graph
->intra_hmap
= isl_map_to_basic_set_set(graph
->intra_hmap
, key
,
1442 isl_basic_set_copy(coef
));
1447 /* Given a dependence relation R, construct the set of coefficients
1448 * of valid constraints for elements in that dependence relation.
1449 * In particular, the result contains tuples of coefficients
1450 * c_0, c_n, c_x, c_y such that
1452 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1454 * If the source or destination nodes of "edge" have been compressed,
1455 * then the dependence relation is also compressed before
1456 * the set of coefficients is computed.
1458 static __isl_give isl_basic_set
*inter_coefficients(
1459 struct isl_sched_graph
*graph
, struct isl_sched_edge
*edge
,
1460 __isl_take isl_map
*map
)
1464 isl_basic_set
*coef
;
1465 isl_maybe_isl_basic_set m
;
1467 m
= isl_map_to_basic_set_try_get(graph
->inter_hmap
, map
);
1468 if (m
.valid
< 0 || m
.valid
) {
1473 key
= isl_map_copy(map
);
1474 if (edge
->src
->compressed
)
1475 map
= isl_map_preimage_domain_multi_aff(map
,
1476 isl_multi_aff_copy(edge
->src
->decompress
));
1477 if (edge
->dst
->compressed
)
1478 map
= isl_map_preimage_range_multi_aff(map
,
1479 isl_multi_aff_copy(edge
->dst
->decompress
));
1480 set
= isl_map_wrap(isl_map_remove_divs(map
));
1481 coef
= isl_set_coefficients(set
);
1482 graph
->inter_hmap
= isl_map_to_basic_set_set(graph
->inter_hmap
, key
,
1483 isl_basic_set_copy(coef
));
1488 /* Return the position of the coefficients of the variables in
1489 * the coefficients constraints "coef".
1491 * The space of "coef" is of the form
1493 * { coefficients[[cst, params] -> S] }
1495 * Return the position of S.
1497 static int coef_var_offset(__isl_keep isl_basic_set
*coef
)
1502 space
= isl_space_unwrap(isl_basic_set_get_space(coef
));
1503 offset
= isl_space_dim(space
, isl_dim_in
);
1504 isl_space_free(space
);
1509 /* Return the offset of the coefficients of the variables of "node"
1512 * Within each node, the coefficients have the following order:
1514 * - c_i_n (if parametric)
1515 * - positive and negative parts of c_i_x
1517 static int node_var_coef_offset(struct isl_sched_node
*node
)
1519 return node
->start
+ 1 + node
->nparam
;
1522 /* Construct an isl_dim_map for mapping constraints on coefficients
1523 * for "node" to the corresponding positions in graph->lp.
1524 * "offset" is the offset of the coefficients for the variables
1525 * in the input constraints.
1526 * "s" is the sign of the mapping.
1528 * The input constraints are given in terms of the coefficients (c_0, c_n, c_x).
1529 * The mapping produced by this function essentially plugs in
1530 * (0, 0, c_i_x^+ - c_i_x^-) if s = 1 and
1531 * (0, 0, -c_i_x^+ + c_i_x^-) if s = -1.
1532 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1534 * The caller can extend the mapping to also map the other coefficients
1535 * (and therefore not plug in 0).
1537 static __isl_give isl_dim_map
*intra_dim_map(isl_ctx
*ctx
,
1538 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1543 isl_dim_map
*dim_map
;
1545 total
= isl_basic_set_total_dim(graph
->lp
);
1546 pos
= node_var_coef_offset(node
);
1547 dim_map
= isl_dim_map_alloc(ctx
, total
);
1548 isl_dim_map_range(dim_map
, pos
, 2, offset
, 1, node
->nvar
, -s
);
1549 isl_dim_map_range(dim_map
, pos
+ 1, 2, offset
, 1, node
->nvar
, s
);
1554 /* Construct an isl_dim_map for mapping constraints on coefficients
1555 * for "src" (node i) and "dst" (node j) to the corresponding positions
1557 * "offset" is the offset of the coefficients for the variables of "src"
1558 * in the input constraints.
1559 * "s" is the sign of the mapping.
1561 * The input constraints are given in terms of the coefficients
1562 * (c_0, c_n, c_x, c_y).
1563 * The mapping produced by this function essentially plugs in
1564 * (c_j_0 - c_i_0, c_j_n - c_i_n,
1565 * c_j_x^+ - c_j_x^-, -(c_i_x^+ - c_i_x^-)) if s = 1 and
1566 * (-c_j_0 + c_i_0, -c_j_n + c_i_n,
1567 * - (c_j_x^+ - c_j_x^-), c_i_x^+ - c_i_x^-) if s = -1.
1568 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1570 * The caller can further extend the mapping.
1572 static __isl_give isl_dim_map
*inter_dim_map(isl_ctx
*ctx
,
1573 struct isl_sched_graph
*graph
, struct isl_sched_node
*src
,
1574 struct isl_sched_node
*dst
, int offset
, int s
)
1578 isl_dim_map
*dim_map
;
1580 total
= isl_basic_set_total_dim(graph
->lp
);
1581 dim_map
= isl_dim_map_alloc(ctx
, total
);
1583 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, s
);
1584 isl_dim_map_range(dim_map
, dst
->start
+ 1, 1, 1, 1, dst
->nparam
, s
);
1585 pos
= node_var_coef_offset(dst
);
1586 isl_dim_map_range(dim_map
, pos
, 2, offset
+ src
->nvar
, 1,
1588 isl_dim_map_range(dim_map
, pos
+ 1, 2, offset
+ src
->nvar
, 1,
1591 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, -s
);
1592 isl_dim_map_range(dim_map
, src
->start
+ 1, 1, 1, 1, src
->nparam
, -s
);
1593 pos
= node_var_coef_offset(src
);
1594 isl_dim_map_range(dim_map
, pos
, 2, offset
, 1, src
->nvar
, s
);
1595 isl_dim_map_range(dim_map
, pos
+ 1, 2, offset
, 1, src
->nvar
, -s
);
1600 /* Add constraints to graph->lp that force validity for the given
1601 * dependence from a node i to itself.
1602 * That is, add constraints that enforce
1604 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1605 * = c_i_x (y - x) >= 0
1607 * for each (x,y) in R.
1608 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1609 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1610 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1611 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1613 * Actually, we do not construct constraints for the c_i_x themselves,
1614 * but for the coefficients of c_i_x written as a linear combination
1615 * of the columns in node->cmap.
1617 static isl_stat
add_intra_validity_constraints(struct isl_sched_graph
*graph
,
1618 struct isl_sched_edge
*edge
)
1621 isl_map
*map
= isl_map_copy(edge
->map
);
1622 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1623 isl_dim_map
*dim_map
;
1624 isl_basic_set
*coef
;
1625 struct isl_sched_node
*node
= edge
->src
;
1627 coef
= intra_coefficients(graph
, node
, map
);
1629 offset
= coef_var_offset(coef
);
1631 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1632 offset
, isl_mat_copy(node
->cmap
));
1634 return isl_stat_error
;
1636 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, 1);
1637 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1638 coef
->n_eq
, coef
->n_ineq
);
1639 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1645 /* Add constraints to graph->lp that force validity for the given
1646 * dependence from node i to node j.
1647 * That is, add constraints that enforce
1649 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1651 * for each (x,y) in R.
1652 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1653 * of valid constraints for R and then plug in
1654 * (c_j_0 - c_i_0, c_j_n - c_i_n, c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1655 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1656 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1658 * Actually, we do not construct constraints for the c_*_x themselves,
1659 * but for the coefficients of c_*_x written as a linear combination
1660 * of the columns in node->cmap.
1662 static isl_stat
add_inter_validity_constraints(struct isl_sched_graph
*graph
,
1663 struct isl_sched_edge
*edge
)
1668 isl_dim_map
*dim_map
;
1669 isl_basic_set
*coef
;
1670 struct isl_sched_node
*src
= edge
->src
;
1671 struct isl_sched_node
*dst
= edge
->dst
;
1674 return isl_stat_error
;
1676 map
= isl_map_copy(edge
->map
);
1677 ctx
= isl_map_get_ctx(map
);
1678 coef
= inter_coefficients(graph
, edge
, map
);
1680 offset
= coef_var_offset(coef
);
1682 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1683 offset
, isl_mat_copy(src
->cmap
));
1684 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1685 offset
+ src
->nvar
, isl_mat_copy(dst
->cmap
));
1687 return isl_stat_error
;
1689 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, 1);
1691 edge
->start
= graph
->lp
->n_ineq
;
1692 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1693 coef
->n_eq
, coef
->n_ineq
);
1694 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1697 return isl_stat_error
;
1698 edge
->end
= graph
->lp
->n_ineq
;
1703 /* Add constraints to graph->lp that bound the dependence distance for the given
1704 * dependence from a node i to itself.
1705 * If s = 1, we add the constraint
1707 * c_i_x (y - x) <= m_0 + m_n n
1711 * -c_i_x (y - x) + m_0 + m_n n >= 0
1713 * for each (x,y) in R.
1714 * If s = -1, we add the constraint
1716 * -c_i_x (y - x) <= m_0 + m_n n
1720 * c_i_x (y - x) + m_0 + m_n n >= 0
1722 * for each (x,y) in R.
1723 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1724 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1725 * with each coefficient (except m_0) represented as a pair of non-negative
1728 * Actually, we do not construct constraints for the c_i_x themselves,
1729 * but for the coefficients of c_i_x written as a linear combination
1730 * of the columns in node->cmap.
1733 * If "local" is set, then we add constraints
1735 * c_i_x (y - x) <= 0
1739 * -c_i_x (y - x) <= 0
1741 * instead, forcing the dependence distance to be (less than or) equal to 0.
1742 * That is, we plug in (0, 0, -s * c_i_x),
1743 * Note that dependences marked local are treated as validity constraints
1744 * by add_all_validity_constraints and therefore also have
1745 * their distances bounded by 0 from below.
1747 static isl_stat
add_intra_proximity_constraints(struct isl_sched_graph
*graph
,
1748 struct isl_sched_edge
*edge
, int s
, int local
)
1752 isl_map
*map
= isl_map_copy(edge
->map
);
1753 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1754 isl_dim_map
*dim_map
;
1755 isl_basic_set
*coef
;
1756 struct isl_sched_node
*node
= edge
->src
;
1758 coef
= intra_coefficients(graph
, node
, map
);
1760 offset
= coef_var_offset(coef
);
1762 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1763 offset
, isl_mat_copy(node
->cmap
));
1765 return isl_stat_error
;
1767 nparam
= isl_space_dim(node
->space
, isl_dim_param
);
1768 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, -s
);
1771 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1772 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1773 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1775 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1776 coef
->n_eq
, coef
->n_ineq
);
1777 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1783 /* Add constraints to graph->lp that bound the dependence distance for the given
1784 * dependence from node i to node j.
1785 * If s = 1, we add the constraint
1787 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1792 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1795 * for each (x,y) in R.
1796 * If s = -1, we add the constraint
1798 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1803 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1806 * for each (x,y) in R.
1807 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1808 * of valid constraints for R and then plug in
1809 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1811 * with each coefficient (except m_0, c_*_0 and c_*_n)
1812 * represented as a pair of non-negative coefficients.
1814 * Actually, we do not construct constraints for the c_*_x themselves,
1815 * but for the coefficients of c_*_x written as a linear combination
1816 * of the columns in node->cmap.
1819 * If "local" is set, then we add constraints
1821 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1825 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
1827 * instead, forcing the dependence distance to be (less than or) equal to 0.
1828 * That is, we plug in
1829 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
1830 * Note that dependences marked local are treated as validity constraints
1831 * by add_all_validity_constraints and therefore also have
1832 * their distances bounded by 0 from below.
1834 static isl_stat
add_inter_proximity_constraints(struct isl_sched_graph
*graph
,
1835 struct isl_sched_edge
*edge
, int s
, int local
)
1839 isl_map
*map
= isl_map_copy(edge
->map
);
1840 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1841 isl_dim_map
*dim_map
;
1842 isl_basic_set
*coef
;
1843 struct isl_sched_node
*src
= edge
->src
;
1844 struct isl_sched_node
*dst
= edge
->dst
;
1846 coef
= inter_coefficients(graph
, edge
, map
);
1848 offset
= coef_var_offset(coef
);
1850 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1851 offset
, isl_mat_copy(src
->cmap
));
1852 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1853 offset
+ src
->nvar
, isl_mat_copy(dst
->cmap
));
1855 return isl_stat_error
;
1857 nparam
= isl_space_dim(src
->space
, isl_dim_param
);
1858 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, -s
);
1861 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1862 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1863 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1866 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1867 coef
->n_eq
, coef
->n_ineq
);
1868 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1874 /* Add all validity constraints to graph->lp.
1876 * An edge that is forced to be local needs to have its dependence
1877 * distances equal to zero. We take care of bounding them by 0 from below
1878 * here. add_all_proximity_constraints takes care of bounding them by 0
1881 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1882 * Otherwise, we ignore them.
1884 static int add_all_validity_constraints(struct isl_sched_graph
*graph
,
1885 int use_coincidence
)
1889 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1890 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1893 local
= is_local(edge
) ||
1894 (is_coincidence(edge
) && use_coincidence
);
1895 if (!is_validity(edge
) && !local
)
1897 if (edge
->src
!= edge
->dst
)
1899 if (add_intra_validity_constraints(graph
, edge
) < 0)
1903 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1904 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1907 local
= is_local(edge
) ||
1908 (is_coincidence(edge
) && use_coincidence
);
1909 if (!is_validity(edge
) && !local
)
1911 if (edge
->src
== edge
->dst
)
1913 if (add_inter_validity_constraints(graph
, edge
) < 0)
1920 /* Add constraints to graph->lp that bound the dependence distance
1921 * for all dependence relations.
1922 * If a given proximity dependence is identical to a validity
1923 * dependence, then the dependence distance is already bounded
1924 * from below (by zero), so we only need to bound the distance
1925 * from above. (This includes the case of "local" dependences
1926 * which are treated as validity dependence by add_all_validity_constraints.)
1927 * Otherwise, we need to bound the distance both from above and from below.
1929 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1930 * Otherwise, we ignore them.
1932 static int add_all_proximity_constraints(struct isl_sched_graph
*graph
,
1933 int use_coincidence
)
1937 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1938 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1941 local
= is_local(edge
) ||
1942 (is_coincidence(edge
) && use_coincidence
);
1943 if (!is_proximity(edge
) && !local
)
1945 if (edge
->src
== edge
->dst
&&
1946 add_intra_proximity_constraints(graph
, edge
, 1, local
) < 0)
1948 if (edge
->src
!= edge
->dst
&&
1949 add_inter_proximity_constraints(graph
, edge
, 1, local
) < 0)
1951 if (is_validity(edge
) || local
)
1953 if (edge
->src
== edge
->dst
&&
1954 add_intra_proximity_constraints(graph
, edge
, -1, 0) < 0)
1956 if (edge
->src
!= edge
->dst
&&
1957 add_inter_proximity_constraints(graph
, edge
, -1, 0) < 0)
1964 /* Compute a basis for the rows in the linear part of the schedule
1965 * and extend this basis to a full basis. The remaining rows
1966 * can then be used to force linear independence from the rows
1969 * In particular, given the schedule rows S, we compute
1974 * with H the Hermite normal form of S. That is, all but the
1975 * first rank columns of H are zero and so each row in S is
1976 * a linear combination of the first rank rows of Q.
1977 * The matrix Q is then transposed because we will write the
1978 * coefficients of the next schedule row as a column vector s
1979 * and express this s as a linear combination s = Q c of the
1981 * Similarly, the matrix U is transposed such that we can
1982 * compute the coefficients c = U s from a schedule row s.
1984 static int node_update_cmap(struct isl_sched_node
*node
)
1987 int n_row
= isl_mat_rows(node
->sched
);
1989 H
= isl_mat_sub_alloc(node
->sched
, 0, n_row
,
1990 1 + node
->nparam
, node
->nvar
);
1992 H
= isl_mat_left_hermite(H
, 0, &U
, &Q
);
1993 isl_mat_free(node
->cmap
);
1994 isl_mat_free(node
->cinv
);
1995 isl_mat_free(node
->ctrans
);
1996 node
->ctrans
= isl_mat_copy(Q
);
1997 node
->cmap
= isl_mat_transpose(Q
);
1998 node
->cinv
= isl_mat_transpose(U
);
1999 node
->rank
= isl_mat_initial_non_zero_cols(H
);
2002 if (!node
->cmap
|| !node
->cinv
|| !node
->ctrans
|| node
->rank
< 0)
2007 /* Is "edge" marked as a validity or a conditional validity edge?
2009 static int is_any_validity(struct isl_sched_edge
*edge
)
2011 return is_validity(edge
) || is_conditional_validity(edge
);
2014 /* How many times should we count the constraints in "edge"?
2016 * If carry is set, then we are counting the number of
2017 * (validity or conditional validity) constraints that will be added
2018 * in setup_carry_lp and we count each edge exactly once.
2020 * Otherwise, we count as follows
2021 * validity -> 1 (>= 0)
2022 * validity+proximity -> 2 (>= 0 and upper bound)
2023 * proximity -> 2 (lower and upper bound)
2024 * local(+any) -> 2 (>= 0 and <= 0)
2026 * If an edge is only marked conditional_validity then it counts
2027 * as zero since it is only checked afterwards.
2029 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2030 * Otherwise, we ignore them.
2032 static int edge_multiplicity(struct isl_sched_edge
*edge
, int carry
,
2033 int use_coincidence
)
2037 if (is_proximity(edge
) || is_local(edge
))
2039 if (use_coincidence
&& is_coincidence(edge
))
2041 if (is_validity(edge
))
2046 /* Count the number of equality and inequality constraints
2047 * that will be added for the given map.
2049 * "use_coincidence" is set if we should take into account coincidence edges.
2051 static int count_map_constraints(struct isl_sched_graph
*graph
,
2052 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
,
2053 int *n_eq
, int *n_ineq
, int carry
, int use_coincidence
)
2055 isl_basic_set
*coef
;
2056 int f
= edge_multiplicity(edge
, carry
, use_coincidence
);
2063 if (edge
->src
== edge
->dst
)
2064 coef
= intra_coefficients(graph
, edge
->src
, map
);
2066 coef
= inter_coefficients(graph
, edge
, map
);
2069 *n_eq
+= f
* coef
->n_eq
;
2070 *n_ineq
+= f
* coef
->n_ineq
;
2071 isl_basic_set_free(coef
);
2076 /* Count the number of equality and inequality constraints
2077 * that will be added to the main lp problem.
2078 * We count as follows
2079 * validity -> 1 (>= 0)
2080 * validity+proximity -> 2 (>= 0 and upper bound)
2081 * proximity -> 2 (lower and upper bound)
2082 * local(+any) -> 2 (>= 0 and <= 0)
2084 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2085 * Otherwise, we ignore them.
2087 static int count_constraints(struct isl_sched_graph
*graph
,
2088 int *n_eq
, int *n_ineq
, int use_coincidence
)
2092 *n_eq
= *n_ineq
= 0;
2093 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2094 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2095 isl_map
*map
= isl_map_copy(edge
->map
);
2097 if (count_map_constraints(graph
, edge
, map
, n_eq
, n_ineq
,
2098 0, use_coincidence
) < 0)
2105 /* Count the number of constraints that will be added by
2106 * add_bound_constant_constraints to bound the values of the constant terms
2107 * and increment *n_eq and *n_ineq accordingly.
2109 * In practice, add_bound_constant_constraints only adds inequalities.
2111 static isl_stat
count_bound_constant_constraints(isl_ctx
*ctx
,
2112 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
2114 if (isl_options_get_schedule_max_constant_term(ctx
) == -1)
2117 *n_ineq
+= graph
->n
;
2122 /* Add constraints to bound the values of the constant terms in the schedule,
2123 * if requested by the user.
2125 * The maximal value of the constant terms is defined by the option
2126 * "schedule_max_constant_term".
2128 * Within each node, the coefficients have the following order:
2130 * - c_i_n (if parametric)
2131 * - positive and negative parts of c_i_x
2133 static isl_stat
add_bound_constant_constraints(isl_ctx
*ctx
,
2134 struct isl_sched_graph
*graph
)
2140 max
= isl_options_get_schedule_max_constant_term(ctx
);
2144 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2146 for (i
= 0; i
< graph
->n
; ++i
) {
2147 struct isl_sched_node
*node
= &graph
->node
[i
];
2148 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2150 return isl_stat_error
;
2151 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2152 isl_int_set_si(graph
->lp
->ineq
[k
][1 + node
->start
], -1);
2153 isl_int_set_si(graph
->lp
->ineq
[k
][0], max
);
2159 /* Count the number of constraints that will be added by
2160 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
2163 * In practice, add_bound_coefficient_constraints only adds inequalities.
2165 static int count_bound_coefficient_constraints(isl_ctx
*ctx
,
2166 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
2170 if (isl_options_get_schedule_max_coefficient(ctx
) == -1 &&
2171 !isl_options_get_schedule_treat_coalescing(ctx
))
2174 for (i
= 0; i
< graph
->n
; ++i
)
2175 *n_ineq
+= graph
->node
[i
].nparam
+ 2 * graph
->node
[i
].nvar
;
2180 /* Add constraints to graph->lp that bound the values of
2181 * the parameter schedule coefficients of "node" to "max" and
2182 * the variable schedule coefficients to the corresponding entry
2184 * In either case, a negative value means that no bound needs to be imposed.
2186 * For parameter coefficients, this amounts to adding a constraint
2194 * The variables coefficients are, however, not represented directly.
2195 * Instead, the variables coefficients c_x are written as a linear
2196 * combination c_x = cmap c_z of some other coefficients c_z,
2197 * which are in turn encoded as c_z = c_z^+ - c_z^-.
2198 * Let a_j be the elements of row i of node->cmap, then
2200 * -max_i <= c_x_i <= max_i
2204 * -max_i <= \sum_j a_j (c_z_j^+ - c_z_j^-) <= max_i
2208 * -\sum_j a_j (c_z_j^+ - c_z_j^-) + max_i >= 0
2209 * \sum_j a_j (c_z_j^+ - c_z_j^-) + max_i >= 0
2211 static isl_stat
node_add_coefficient_constraints(isl_ctx
*ctx
,
2212 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
, int max
)
2218 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2220 for (j
= 0; j
< node
->nparam
; ++j
) {
2226 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2228 return isl_stat_error
;
2229 dim
= 1 + node
->start
+ 1 + j
;
2230 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2231 isl_int_set_si(graph
->lp
->ineq
[k
][dim
], -1);
2232 isl_int_set_si(graph
->lp
->ineq
[k
][0], max
);
2235 ineq
= isl_vec_alloc(ctx
, 1 + total
);
2236 ineq
= isl_vec_clr(ineq
);
2238 return isl_stat_error
;
2239 for (i
= 0; i
< node
->nvar
; ++i
) {
2240 int pos
= 1 + node_var_coef_offset(node
);
2242 if (isl_int_is_neg(node
->max
->el
[i
]))
2245 for (j
= 0; j
< node
->nvar
; ++j
) {
2246 isl_int_set(ineq
->el
[pos
+ 2 * j
],
2247 node
->cmap
->row
[i
][j
]);
2248 isl_int_neg(ineq
->el
[pos
+ 2 * j
+ 1],
2249 node
->cmap
->row
[i
][j
]);
2251 isl_int_set(ineq
->el
[0], node
->max
->el
[i
]);
2253 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2256 isl_seq_cpy(graph
->lp
->ineq
[k
], ineq
->el
, 1 + total
);
2258 isl_seq_neg(ineq
->el
+ pos
, ineq
->el
+ pos
, 2 * node
->nvar
);
2259 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2262 isl_seq_cpy(graph
->lp
->ineq
[k
], ineq
->el
, 1 + total
);
2269 return isl_stat_error
;
2272 /* Add constraints that bound the values of the variable and parameter
2273 * coefficients of the schedule.
2275 * The maximal value of the coefficients is defined by the option
2276 * 'schedule_max_coefficient' and the entries in node->max.
2277 * These latter entries are only set if either the schedule_max_coefficient
2278 * option or the schedule_treat_coalescing option is set.
2280 static isl_stat
add_bound_coefficient_constraints(isl_ctx
*ctx
,
2281 struct isl_sched_graph
*graph
)
2286 max
= isl_options_get_schedule_max_coefficient(ctx
);
2288 if (max
== -1 && !isl_options_get_schedule_treat_coalescing(ctx
))
2291 for (i
= 0; i
< graph
->n
; ++i
) {
2292 struct isl_sched_node
*node
= &graph
->node
[i
];
2294 if (node_add_coefficient_constraints(ctx
, graph
, node
, max
) < 0)
2295 return isl_stat_error
;
2301 /* Add a constraint to graph->lp that equates the value at position
2302 * "sum_pos" to the sum of the "n" values starting at "first".
2304 static isl_stat
add_sum_constraint(struct isl_sched_graph
*graph
,
2305 int sum_pos
, int first
, int n
)
2310 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2312 k
= isl_basic_set_alloc_equality(graph
->lp
);
2314 return isl_stat_error
;
2315 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2316 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2317 for (i
= 0; i
< n
; ++i
)
2318 isl_int_set_si(graph
->lp
->eq
[k
][1 + first
+ i
], 1);
2323 /* Add a constraint to graph->lp that equates the value at position
2324 * "sum_pos" to the sum of the parameter coefficients of all nodes.
2326 * Within each node, the coefficients have the following order:
2328 * - c_i_n (if parametric)
2329 * - positive and negative parts of c_i_x
2331 static isl_stat
add_param_sum_constraint(struct isl_sched_graph
*graph
,
2337 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2339 k
= isl_basic_set_alloc_equality(graph
->lp
);
2341 return isl_stat_error
;
2342 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2343 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2344 for (i
= 0; i
< graph
->n
; ++i
) {
2345 int pos
= 1 + graph
->node
[i
].start
+ 1;
2347 for (j
= 0; j
< graph
->node
[i
].nparam
; ++j
)
2348 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2354 /* Add a constraint to graph->lp that equates the value at position
2355 * "sum_pos" to the sum of the variable coefficients of all nodes.
2357 * Within each node, the coefficients have the following order:
2359 * - c_i_n (if parametric)
2360 * - positive and negative parts of c_i_x
2362 static isl_stat
add_var_sum_constraint(struct isl_sched_graph
*graph
,
2368 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2370 k
= isl_basic_set_alloc_equality(graph
->lp
);
2372 return isl_stat_error
;
2373 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2374 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2375 for (i
= 0; i
< graph
->n
; ++i
) {
2376 struct isl_sched_node
*node
= &graph
->node
[i
];
2377 int pos
= 1 + node_var_coef_offset(node
);
2379 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
2380 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2386 /* Construct an ILP problem for finding schedule coefficients
2387 * that result in non-negative, but small dependence distances
2388 * over all dependences.
2389 * In particular, the dependence distances over proximity edges
2390 * are bounded by m_0 + m_n n and we compute schedule coefficients
2391 * with small values (preferably zero) of m_n and m_0.
2393 * All variables of the ILP are non-negative. The actual coefficients
2394 * may be negative, so each coefficient is represented as the difference
2395 * of two non-negative variables. The negative part always appears
2396 * immediately before the positive part.
2397 * Other than that, the variables have the following order
2399 * - sum of positive and negative parts of m_n coefficients
2401 * - sum of all c_n coefficients
2402 * (unconstrained when computing non-parametric schedules)
2403 * - sum of positive and negative parts of all c_x coefficients
2404 * - positive and negative parts of m_n coefficients
2407 * - c_i_n (if parametric)
2408 * - positive and negative parts of c_i_x
2410 * The c_i_x are not represented directly, but through the columns of
2411 * node->cmap. That is, the computed values are for variable t_i_x
2412 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
2414 * The constraints are those from the edges plus two or three equalities
2415 * to express the sums.
2417 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2418 * Otherwise, we ignore them.
2420 static isl_stat
setup_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
2421 int use_coincidence
)
2431 parametric
= ctx
->opt
->schedule_parametric
;
2432 nparam
= isl_space_dim(graph
->node
[0].space
, isl_dim_param
);
2434 total
= param_pos
+ 2 * nparam
;
2435 for (i
= 0; i
< graph
->n
; ++i
) {
2436 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
2437 if (node_update_cmap(node
) < 0)
2438 return isl_stat_error
;
2439 node
->start
= total
;
2440 total
+= 1 + node
->nparam
+ 2 * node
->nvar
;
2443 if (count_constraints(graph
, &n_eq
, &n_ineq
, use_coincidence
) < 0)
2444 return isl_stat_error
;
2445 if (count_bound_constant_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2446 return isl_stat_error
;
2447 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2448 return isl_stat_error
;
2450 space
= isl_space_set_alloc(ctx
, 0, total
);
2451 isl_basic_set_free(graph
->lp
);
2452 n_eq
+= 2 + parametric
;
2454 graph
->lp
= isl_basic_set_alloc_space(space
, 0, n_eq
, n_ineq
);
2456 if (add_sum_constraint(graph
, 0, param_pos
, 2 * nparam
) < 0)
2457 return isl_stat_error
;
2458 if (parametric
&& add_param_sum_constraint(graph
, 2) < 0)
2459 return isl_stat_error
;
2460 if (add_var_sum_constraint(graph
, 3) < 0)
2461 return isl_stat_error
;
2462 if (add_bound_constant_constraints(ctx
, graph
) < 0)
2463 return isl_stat_error
;
2464 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
2465 return isl_stat_error
;
2466 if (add_all_validity_constraints(graph
, use_coincidence
) < 0)
2467 return isl_stat_error
;
2468 if (add_all_proximity_constraints(graph
, use_coincidence
) < 0)
2469 return isl_stat_error
;
2474 /* Analyze the conflicting constraint found by
2475 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2476 * constraint of one of the edges between distinct nodes, living, moreover
2477 * in distinct SCCs, then record the source and sink SCC as this may
2478 * be a good place to cut between SCCs.
2480 static int check_conflict(int con
, void *user
)
2483 struct isl_sched_graph
*graph
= user
;
2485 if (graph
->src_scc
>= 0)
2488 con
-= graph
->lp
->n_eq
;
2490 if (con
>= graph
->lp
->n_ineq
)
2493 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2494 if (!is_validity(&graph
->edge
[i
]))
2496 if (graph
->edge
[i
].src
== graph
->edge
[i
].dst
)
2498 if (graph
->edge
[i
].src
->scc
== graph
->edge
[i
].dst
->scc
)
2500 if (graph
->edge
[i
].start
> con
)
2502 if (graph
->edge
[i
].end
<= con
)
2504 graph
->src_scc
= graph
->edge
[i
].src
->scc
;
2505 graph
->dst_scc
= graph
->edge
[i
].dst
->scc
;
2511 /* Check whether the next schedule row of the given node needs to be
2512 * non-trivial. Lower-dimensional domains may have some trivial rows,
2513 * but as soon as the number of remaining required non-trivial rows
2514 * is as large as the number or remaining rows to be computed,
2515 * all remaining rows need to be non-trivial.
2517 static int needs_row(struct isl_sched_graph
*graph
, struct isl_sched_node
*node
)
2519 return node
->nvar
- node
->rank
>= graph
->maxvar
- graph
->n_row
;
2522 /* Solve the ILP problem constructed in setup_lp.
2523 * For each node such that all the remaining rows of its schedule
2524 * need to be non-trivial, we construct a non-triviality region.
2525 * This region imposes that the next row is independent of previous rows.
2526 * In particular the coefficients c_i_x are represented by t_i_x
2527 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
2528 * its first columns span the rows of the previously computed part
2529 * of the schedule. The non-triviality region enforces that at least
2530 * one of the remaining components of t_i_x is non-zero, i.e.,
2531 * that the new schedule row depends on at least one of the remaining
2534 static __isl_give isl_vec
*solve_lp(struct isl_sched_graph
*graph
)
2540 for (i
= 0; i
< graph
->n
; ++i
) {
2541 struct isl_sched_node
*node
= &graph
->node
[i
];
2542 int skip
= node
->rank
;
2543 graph
->region
[i
].pos
= node_var_coef_offset(node
) + 2 * skip
;
2544 if (needs_row(graph
, node
))
2545 graph
->region
[i
].len
= 2 * (node
->nvar
- skip
);
2547 graph
->region
[i
].len
= 0;
2549 lp
= isl_basic_set_copy(graph
->lp
);
2550 sol
= isl_tab_basic_set_non_trivial_lexmin(lp
, 2, graph
->n
,
2551 graph
->region
, &check_conflict
, graph
);
2555 /* Extract the coefficients for the variables of "node" from "sol".
2557 * Within each node, the coefficients have the following order:
2559 * - c_i_n (if parametric)
2560 * - positive and negative parts of c_i_x
2562 * The c_i_x^- appear before their c_i_x^+ counterpart.
2564 * Return c_i_x = c_i_x^+ - c_i_x^-
2566 static __isl_give isl_vec
*extract_var_coef(struct isl_sched_node
*node
,
2567 __isl_keep isl_vec
*sol
)
2575 csol
= isl_vec_alloc(isl_vec_get_ctx(sol
), node
->nvar
);
2579 pos
= 1 + node_var_coef_offset(node
);
2580 for (i
= 0; i
< node
->nvar
; ++i
)
2581 isl_int_sub(csol
->el
[i
],
2582 sol
->el
[pos
+ 2 * i
+ 1], sol
->el
[pos
+ 2 * i
]);
2587 /* Update the schedules of all nodes based on the given solution
2588 * of the LP problem.
2589 * The new row is added to the current band.
2590 * All possibly negative coefficients are encoded as a difference
2591 * of two non-negative variables, so we need to perform the subtraction
2592 * here. Moreover, if use_cmap is set, then the solution does
2593 * not refer to the actual coefficients c_i_x, but instead to variables
2594 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2595 * In this case, we then also need to perform this multiplication
2596 * to obtain the values of c_i_x.
2598 * If coincident is set, then the caller guarantees that the new
2599 * row satisfies the coincidence constraints.
2601 static int update_schedule(struct isl_sched_graph
*graph
,
2602 __isl_take isl_vec
*sol
, int use_cmap
, int coincident
)
2605 isl_vec
*csol
= NULL
;
2610 isl_die(sol
->ctx
, isl_error_internal
,
2611 "no solution found", goto error
);
2612 if (graph
->n_total_row
>= graph
->max_row
)
2613 isl_die(sol
->ctx
, isl_error_internal
,
2614 "too many schedule rows", goto error
);
2616 for (i
= 0; i
< graph
->n
; ++i
) {
2617 struct isl_sched_node
*node
= &graph
->node
[i
];
2618 int pos
= node
->start
;
2619 int row
= isl_mat_rows(node
->sched
);
2622 csol
= extract_var_coef(node
, sol
);
2626 isl_map_free(node
->sched_map
);
2627 node
->sched_map
= NULL
;
2628 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
2631 for (j
= 0; j
< 1 + node
->nparam
; ++j
)
2632 node
->sched
= isl_mat_set_element(node
->sched
,
2633 row
, j
, sol
->el
[1 + pos
+ j
]);
2635 csol
= isl_mat_vec_product(isl_mat_copy(node
->cmap
),
2639 for (j
= 0; j
< node
->nvar
; ++j
)
2640 node
->sched
= isl_mat_set_element(node
->sched
,
2641 row
, 1 + node
->nparam
+ j
, csol
->el
[j
]);
2642 node
->coincident
[graph
->n_total_row
] = coincident
;
2648 graph
->n_total_row
++;
2657 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2658 * and return this isl_aff.
2660 static __isl_give isl_aff
*extract_schedule_row(__isl_take isl_local_space
*ls
,
2661 struct isl_sched_node
*node
, int row
)
2669 aff
= isl_aff_zero_on_domain(ls
);
2670 isl_mat_get_element(node
->sched
, row
, 0, &v
);
2671 aff
= isl_aff_set_constant(aff
, v
);
2672 for (j
= 0; j
< node
->nparam
; ++j
) {
2673 isl_mat_get_element(node
->sched
, row
, 1 + j
, &v
);
2674 aff
= isl_aff_set_coefficient(aff
, isl_dim_param
, j
, v
);
2676 for (j
= 0; j
< node
->nvar
; ++j
) {
2677 isl_mat_get_element(node
->sched
, row
, 1 + node
->nparam
+ j
, &v
);
2678 aff
= isl_aff_set_coefficient(aff
, isl_dim_in
, j
, v
);
2686 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
2687 * and return this multi_aff.
2689 * The result is defined over the uncompressed node domain.
2691 static __isl_give isl_multi_aff
*node_extract_partial_schedule_multi_aff(
2692 struct isl_sched_node
*node
, int first
, int n
)
2696 isl_local_space
*ls
;
2703 nrow
= isl_mat_rows(node
->sched
);
2704 if (node
->compressed
)
2705 space
= isl_multi_aff_get_domain_space(node
->decompress
);
2707 space
= isl_space_copy(node
->space
);
2708 ls
= isl_local_space_from_space(isl_space_copy(space
));
2709 space
= isl_space_from_domain(space
);
2710 space
= isl_space_add_dims(space
, isl_dim_out
, n
);
2711 ma
= isl_multi_aff_zero(space
);
2713 for (i
= first
; i
< first
+ n
; ++i
) {
2714 aff
= extract_schedule_row(isl_local_space_copy(ls
), node
, i
);
2715 ma
= isl_multi_aff_set_aff(ma
, i
- first
, aff
);
2718 isl_local_space_free(ls
);
2720 if (node
->compressed
)
2721 ma
= isl_multi_aff_pullback_multi_aff(ma
,
2722 isl_multi_aff_copy(node
->compress
));
2727 /* Convert node->sched into a multi_aff and return this multi_aff.
2729 * The result is defined over the uncompressed node domain.
2731 static __isl_give isl_multi_aff
*node_extract_schedule_multi_aff(
2732 struct isl_sched_node
*node
)
2736 nrow
= isl_mat_rows(node
->sched
);
2737 return node_extract_partial_schedule_multi_aff(node
, 0, nrow
);
2740 /* Convert node->sched into a map and return this map.
2742 * The result is cached in node->sched_map, which needs to be released
2743 * whenever node->sched is updated.
2744 * It is defined over the uncompressed node domain.
2746 static __isl_give isl_map
*node_extract_schedule(struct isl_sched_node
*node
)
2748 if (!node
->sched_map
) {
2751 ma
= node_extract_schedule_multi_aff(node
);
2752 node
->sched_map
= isl_map_from_multi_aff(ma
);
2755 return isl_map_copy(node
->sched_map
);
2758 /* Construct a map that can be used to update a dependence relation
2759 * based on the current schedule.
2760 * That is, construct a map expressing that source and sink
2761 * are executed within the same iteration of the current schedule.
2762 * This map can then be intersected with the dependence relation.
2763 * This is not the most efficient way, but this shouldn't be a critical
2766 static __isl_give isl_map
*specializer(struct isl_sched_node
*src
,
2767 struct isl_sched_node
*dst
)
2769 isl_map
*src_sched
, *dst_sched
;
2771 src_sched
= node_extract_schedule(src
);
2772 dst_sched
= node_extract_schedule(dst
);
2773 return isl_map_apply_range(src_sched
, isl_map_reverse(dst_sched
));
2776 /* Intersect the domains of the nested relations in domain and range
2777 * of "umap" with "map".
2779 static __isl_give isl_union_map
*intersect_domains(
2780 __isl_take isl_union_map
*umap
, __isl_keep isl_map
*map
)
2782 isl_union_set
*uset
;
2784 umap
= isl_union_map_zip(umap
);
2785 uset
= isl_union_set_from_set(isl_map_wrap(isl_map_copy(map
)));
2786 umap
= isl_union_map_intersect_domain(umap
, uset
);
2787 umap
= isl_union_map_zip(umap
);
2791 /* Update the dependence relation of the given edge based
2792 * on the current schedule.
2793 * If the dependence is carried completely by the current schedule, then
2794 * it is removed from the edge_tables. It is kept in the list of edges
2795 * as otherwise all edge_tables would have to be recomputed.
2797 static int update_edge(struct isl_sched_graph
*graph
,
2798 struct isl_sched_edge
*edge
)
2803 id
= specializer(edge
->src
, edge
->dst
);
2804 edge
->map
= isl_map_intersect(edge
->map
, isl_map_copy(id
));
2808 if (edge
->tagged_condition
) {
2809 edge
->tagged_condition
=
2810 intersect_domains(edge
->tagged_condition
, id
);
2811 if (!edge
->tagged_condition
)
2814 if (edge
->tagged_validity
) {
2815 edge
->tagged_validity
=
2816 intersect_domains(edge
->tagged_validity
, id
);
2817 if (!edge
->tagged_validity
)
2821 empty
= isl_map_plain_is_empty(edge
->map
);
2825 graph_remove_edge(graph
, edge
);
2834 /* Does the domain of "umap" intersect "uset"?
2836 static int domain_intersects(__isl_keep isl_union_map
*umap
,
2837 __isl_keep isl_union_set
*uset
)
2841 umap
= isl_union_map_copy(umap
);
2842 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(uset
));
2843 empty
= isl_union_map_is_empty(umap
);
2844 isl_union_map_free(umap
);
2846 return empty
< 0 ? -1 : !empty
;
2849 /* Does the range of "umap" intersect "uset"?
2851 static int range_intersects(__isl_keep isl_union_map
*umap
,
2852 __isl_keep isl_union_set
*uset
)
2856 umap
= isl_union_map_copy(umap
);
2857 umap
= isl_union_map_intersect_range(umap
, isl_union_set_copy(uset
));
2858 empty
= isl_union_map_is_empty(umap
);
2859 isl_union_map_free(umap
);
2861 return empty
< 0 ? -1 : !empty
;
2864 /* Are the condition dependences of "edge" local with respect to
2865 * the current schedule?
2867 * That is, are domain and range of the condition dependences mapped
2868 * to the same point?
2870 * In other words, is the condition false?
2872 static int is_condition_false(struct isl_sched_edge
*edge
)
2874 isl_union_map
*umap
;
2875 isl_map
*map
, *sched
, *test
;
2878 empty
= isl_union_map_is_empty(edge
->tagged_condition
);
2879 if (empty
< 0 || empty
)
2882 umap
= isl_union_map_copy(edge
->tagged_condition
);
2883 umap
= isl_union_map_zip(umap
);
2884 umap
= isl_union_set_unwrap(isl_union_map_domain(umap
));
2885 map
= isl_map_from_union_map(umap
);
2887 sched
= node_extract_schedule(edge
->src
);
2888 map
= isl_map_apply_domain(map
, sched
);
2889 sched
= node_extract_schedule(edge
->dst
);
2890 map
= isl_map_apply_range(map
, sched
);
2892 test
= isl_map_identity(isl_map_get_space(map
));
2893 local
= isl_map_is_subset(map
, test
);
2900 /* For each conditional validity constraint that is adjacent
2901 * to a condition with domain in condition_source or range in condition_sink,
2902 * turn it into an unconditional validity constraint.
2904 static int unconditionalize_adjacent_validity(struct isl_sched_graph
*graph
,
2905 __isl_take isl_union_set
*condition_source
,
2906 __isl_take isl_union_set
*condition_sink
)
2910 condition_source
= isl_union_set_coalesce(condition_source
);
2911 condition_sink
= isl_union_set_coalesce(condition_sink
);
2913 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2915 isl_union_map
*validity
;
2917 if (!is_conditional_validity(&graph
->edge
[i
]))
2919 if (is_validity(&graph
->edge
[i
]))
2922 validity
= graph
->edge
[i
].tagged_validity
;
2923 adjacent
= domain_intersects(validity
, condition_sink
);
2924 if (adjacent
>= 0 && !adjacent
)
2925 adjacent
= range_intersects(validity
, condition_source
);
2931 set_validity(&graph
->edge
[i
]);
2934 isl_union_set_free(condition_source
);
2935 isl_union_set_free(condition_sink
);
2938 isl_union_set_free(condition_source
);
2939 isl_union_set_free(condition_sink
);
2943 /* Update the dependence relations of all edges based on the current schedule
2944 * and enforce conditional validity constraints that are adjacent
2945 * to satisfied condition constraints.
2947 * First check if any of the condition constraints are satisfied
2948 * (i.e., not local to the outer schedule) and keep track of
2949 * their domain and range.
2950 * Then update all dependence relations (which removes the non-local
2952 * Finally, if any condition constraints turned out to be satisfied,
2953 * then turn all adjacent conditional validity constraints into
2954 * unconditional validity constraints.
2956 static int update_edges(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2960 isl_union_set
*source
, *sink
;
2962 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
2963 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
2964 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2966 isl_union_set
*uset
;
2967 isl_union_map
*umap
;
2969 if (!is_condition(&graph
->edge
[i
]))
2971 if (is_local(&graph
->edge
[i
]))
2973 local
= is_condition_false(&graph
->edge
[i
]);
2981 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
2982 uset
= isl_union_map_domain(umap
);
2983 source
= isl_union_set_union(source
, uset
);
2985 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
2986 uset
= isl_union_map_range(umap
);
2987 sink
= isl_union_set_union(sink
, uset
);
2990 for (i
= graph
->n_edge
- 1; i
>= 0; --i
) {
2991 if (update_edge(graph
, &graph
->edge
[i
]) < 0)
2996 return unconditionalize_adjacent_validity(graph
, source
, sink
);
2998 isl_union_set_free(source
);
2999 isl_union_set_free(sink
);
3002 isl_union_set_free(source
);
3003 isl_union_set_free(sink
);
3007 static void next_band(struct isl_sched_graph
*graph
)
3009 graph
->band_start
= graph
->n_total_row
;
3012 /* Return the union of the universe domains of the nodes in "graph"
3013 * that satisfy "pred".
3015 static __isl_give isl_union_set
*isl_sched_graph_domain(isl_ctx
*ctx
,
3016 struct isl_sched_graph
*graph
,
3017 int (*pred
)(struct isl_sched_node
*node
, int data
), int data
)
3023 for (i
= 0; i
< graph
->n
; ++i
)
3024 if (pred(&graph
->node
[i
], data
))
3028 isl_die(ctx
, isl_error_internal
,
3029 "empty component", return NULL
);
3031 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
3032 dom
= isl_union_set_from_set(set
);
3034 for (i
= i
+ 1; i
< graph
->n
; ++i
) {
3035 if (!pred(&graph
->node
[i
], data
))
3037 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
3038 dom
= isl_union_set_union(dom
, isl_union_set_from_set(set
));
3044 /* Return a list of unions of universe domains, where each element
3045 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
3047 static __isl_give isl_union_set_list
*extract_sccs(isl_ctx
*ctx
,
3048 struct isl_sched_graph
*graph
)
3051 isl_union_set_list
*filters
;
3053 filters
= isl_union_set_list_alloc(ctx
, graph
->scc
);
3054 for (i
= 0; i
< graph
->scc
; ++i
) {
3057 dom
= isl_sched_graph_domain(ctx
, graph
, &node_scc_exactly
, i
);
3058 filters
= isl_union_set_list_add(filters
, dom
);
3064 /* Return a list of two unions of universe domains, one for the SCCs up
3065 * to and including graph->src_scc and another for the other SCCs.
3067 static __isl_give isl_union_set_list
*extract_split(isl_ctx
*ctx
,
3068 struct isl_sched_graph
*graph
)
3071 isl_union_set_list
*filters
;
3073 filters
= isl_union_set_list_alloc(ctx
, 2);
3074 dom
= isl_sched_graph_domain(ctx
, graph
,
3075 &node_scc_at_most
, graph
->src_scc
);
3076 filters
= isl_union_set_list_add(filters
, dom
);
3077 dom
= isl_sched_graph_domain(ctx
, graph
,
3078 &node_scc_at_least
, graph
->src_scc
+ 1);
3079 filters
= isl_union_set_list_add(filters
, dom
);
3084 /* Copy nodes that satisfy node_pred from the src dependence graph
3085 * to the dst dependence graph.
3087 static int copy_nodes(struct isl_sched_graph
*dst
, struct isl_sched_graph
*src
,
3088 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
3093 for (i
= 0; i
< src
->n
; ++i
) {
3096 if (!node_pred(&src
->node
[i
], data
))
3100 dst
->node
[j
].space
= isl_space_copy(src
->node
[i
].space
);
3101 dst
->node
[j
].compressed
= src
->node
[i
].compressed
;
3102 dst
->node
[j
].hull
= isl_set_copy(src
->node
[i
].hull
);
3103 dst
->node
[j
].compress
=
3104 isl_multi_aff_copy(src
->node
[i
].compress
);
3105 dst
->node
[j
].decompress
=
3106 isl_multi_aff_copy(src
->node
[i
].decompress
);
3107 dst
->node
[j
].nvar
= src
->node
[i
].nvar
;
3108 dst
->node
[j
].nparam
= src
->node
[i
].nparam
;
3109 dst
->node
[j
].sched
= isl_mat_copy(src
->node
[i
].sched
);
3110 dst
->node
[j
].sched_map
= isl_map_copy(src
->node
[i
].sched_map
);
3111 dst
->node
[j
].coincident
= src
->node
[i
].coincident
;
3112 dst
->node
[j
].sizes
= isl_multi_val_copy(src
->node
[i
].sizes
);
3113 dst
->node
[j
].max
= isl_vec_copy(src
->node
[i
].max
);
3116 if (!dst
->node
[j
].space
|| !dst
->node
[j
].sched
)
3118 if (dst
->node
[j
].compressed
&&
3119 (!dst
->node
[j
].hull
|| !dst
->node
[j
].compress
||
3120 !dst
->node
[j
].decompress
))
3127 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
3128 * to the dst dependence graph.
3129 * If the source or destination node of the edge is not in the destination
3130 * graph, then it must be a backward proximity edge and it should simply
3133 static int copy_edges(isl_ctx
*ctx
, struct isl_sched_graph
*dst
,
3134 struct isl_sched_graph
*src
,
3135 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
), int data
)
3138 enum isl_edge_type t
;
3141 for (i
= 0; i
< src
->n_edge
; ++i
) {
3142 struct isl_sched_edge
*edge
= &src
->edge
[i
];
3144 isl_union_map
*tagged_condition
;
3145 isl_union_map
*tagged_validity
;
3146 struct isl_sched_node
*dst_src
, *dst_dst
;
3148 if (!edge_pred(edge
, data
))
3151 if (isl_map_plain_is_empty(edge
->map
))
3154 dst_src
= graph_find_node(ctx
, dst
, edge
->src
->space
);
3155 dst_dst
= graph_find_node(ctx
, dst
, edge
->dst
->space
);
3156 if (!dst_src
|| !dst_dst
) {
3157 if (is_validity(edge
) || is_conditional_validity(edge
))
3158 isl_die(ctx
, isl_error_internal
,
3159 "backward (conditional) validity edge",
3164 map
= isl_map_copy(edge
->map
);
3165 tagged_condition
= isl_union_map_copy(edge
->tagged_condition
);
3166 tagged_validity
= isl_union_map_copy(edge
->tagged_validity
);
3168 dst
->edge
[dst
->n_edge
].src
= dst_src
;
3169 dst
->edge
[dst
->n_edge
].dst
= dst_dst
;
3170 dst
->edge
[dst
->n_edge
].map
= map
;
3171 dst
->edge
[dst
->n_edge
].tagged_condition
= tagged_condition
;
3172 dst
->edge
[dst
->n_edge
].tagged_validity
= tagged_validity
;
3173 dst
->edge
[dst
->n_edge
].types
= edge
->types
;
3176 if (edge
->tagged_condition
&& !tagged_condition
)
3178 if (edge
->tagged_validity
&& !tagged_validity
)
3181 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
3183 graph_find_edge(src
, t
, edge
->src
, edge
->dst
))
3185 if (graph_edge_table_add(ctx
, dst
, t
,
3186 &dst
->edge
[dst
->n_edge
- 1]) < 0)
3194 /* Compute the maximal number of variables over all nodes.
3195 * This is the maximal number of linearly independent schedule
3196 * rows that we need to compute.
3197 * Just in case we end up in a part of the dependence graph
3198 * with only lower-dimensional domains, we make sure we will
3199 * compute the required amount of extra linearly independent rows.
3201 static int compute_maxvar(struct isl_sched_graph
*graph
)
3206 for (i
= 0; i
< graph
->n
; ++i
) {
3207 struct isl_sched_node
*node
= &graph
->node
[i
];
3210 if (node_update_cmap(node
) < 0)
3212 nvar
= node
->nvar
+ graph
->n_row
- node
->rank
;
3213 if (nvar
> graph
->maxvar
)
3214 graph
->maxvar
= nvar
;
3220 /* Extract the subgraph of "graph" that consists of the node satisfying
3221 * "node_pred" and the edges satisfying "edge_pred" and store
3222 * the result in "sub".
3224 static int extract_sub_graph(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
3225 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
3226 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
3227 int data
, struct isl_sched_graph
*sub
)
3229 int i
, n
= 0, n_edge
= 0;
3232 for (i
= 0; i
< graph
->n
; ++i
)
3233 if (node_pred(&graph
->node
[i
], data
))
3235 for (i
= 0; i
< graph
->n_edge
; ++i
)
3236 if (edge_pred(&graph
->edge
[i
], data
))
3238 if (graph_alloc(ctx
, sub
, n
, n_edge
) < 0)
3240 if (copy_nodes(sub
, graph
, node_pred
, data
) < 0)
3242 if (graph_init_table(ctx
, sub
) < 0)
3244 for (t
= 0; t
<= isl_edge_last
; ++t
)
3245 sub
->max_edge
[t
] = graph
->max_edge
[t
];
3246 if (graph_init_edge_tables(ctx
, sub
) < 0)
3248 if (copy_edges(ctx
, sub
, graph
, edge_pred
, data
) < 0)
3250 sub
->n_row
= graph
->n_row
;
3251 sub
->max_row
= graph
->max_row
;
3252 sub
->n_total_row
= graph
->n_total_row
;
3253 sub
->band_start
= graph
->band_start
;
3258 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
3259 struct isl_sched_graph
*graph
);
3260 static __isl_give isl_schedule_node
*compute_schedule_wcc(
3261 isl_schedule_node
*node
, struct isl_sched_graph
*graph
);
3263 /* Compute a schedule for a subgraph of "graph". In particular, for
3264 * the graph composed of nodes that satisfy node_pred and edges that
3265 * that satisfy edge_pred.
3266 * If the subgraph is known to consist of a single component, then wcc should
3267 * be set and then we call compute_schedule_wcc on the constructed subgraph.
3268 * Otherwise, we call compute_schedule, which will check whether the subgraph
3271 * The schedule is inserted at "node" and the updated schedule node
3274 static __isl_give isl_schedule_node
*compute_sub_schedule(
3275 __isl_take isl_schedule_node
*node
, isl_ctx
*ctx
,
3276 struct isl_sched_graph
*graph
,
3277 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
3278 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
3281 struct isl_sched_graph split
= { 0 };
3283 if (extract_sub_graph(ctx
, graph
, node_pred
, edge_pred
, data
,
3288 node
= compute_schedule_wcc(node
, &split
);
3290 node
= compute_schedule(node
, &split
);
3292 graph_free(ctx
, &split
);
3295 graph_free(ctx
, &split
);
3296 return isl_schedule_node_free(node
);
3299 static int edge_scc_exactly(struct isl_sched_edge
*edge
, int scc
)
3301 return edge
->src
->scc
== scc
&& edge
->dst
->scc
== scc
;
3304 static int edge_dst_scc_at_most(struct isl_sched_edge
*edge
, int scc
)
3306 return edge
->dst
->scc
<= scc
;
3309 static int edge_src_scc_at_least(struct isl_sched_edge
*edge
, int scc
)
3311 return edge
->src
->scc
>= scc
;
3314 /* Reset the current band by dropping all its schedule rows.
3316 static int reset_band(struct isl_sched_graph
*graph
)
3321 drop
= graph
->n_total_row
- graph
->band_start
;
3322 graph
->n_total_row
-= drop
;
3323 graph
->n_row
-= drop
;
3325 for (i
= 0; i
< graph
->n
; ++i
) {
3326 struct isl_sched_node
*node
= &graph
->node
[i
];
3328 isl_map_free(node
->sched_map
);
3329 node
->sched_map
= NULL
;
3331 node
->sched
= isl_mat_drop_rows(node
->sched
,
3332 graph
->band_start
, drop
);
3341 /* Split the current graph into two parts and compute a schedule for each
3342 * part individually. In particular, one part consists of all SCCs up
3343 * to and including graph->src_scc, while the other part contains the other
3344 * SCCs. The split is enforced by a sequence node inserted at position "node"
3345 * in the schedule tree. Return the updated schedule node.
3346 * If either of these two parts consists of a sequence, then it is spliced
3347 * into the sequence containing the two parts.
3349 * The current band is reset. It would be possible to reuse
3350 * the previously computed rows as the first rows in the next
3351 * band, but recomputing them may result in better rows as we are looking
3352 * at a smaller part of the dependence graph.
3354 static __isl_give isl_schedule_node
*compute_split_schedule(
3355 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3359 isl_union_set_list
*filters
;
3364 if (reset_band(graph
) < 0)
3365 return isl_schedule_node_free(node
);
3369 ctx
= isl_schedule_node_get_ctx(node
);
3370 filters
= extract_split(ctx
, graph
);
3371 node
= isl_schedule_node_insert_sequence(node
, filters
);
3372 node
= isl_schedule_node_child(node
, 1);
3373 node
= isl_schedule_node_child(node
, 0);
3375 node
= compute_sub_schedule(node
, ctx
, graph
,
3376 &node_scc_at_least
, &edge_src_scc_at_least
,
3377 graph
->src_scc
+ 1, 0);
3378 is_seq
= isl_schedule_node_get_type(node
) == isl_schedule_node_sequence
;
3379 node
= isl_schedule_node_parent(node
);
3380 node
= isl_schedule_node_parent(node
);
3382 node
= isl_schedule_node_sequence_splice_child(node
, 1);
3383 node
= isl_schedule_node_child(node
, 0);
3384 node
= isl_schedule_node_child(node
, 0);
3385 node
= compute_sub_schedule(node
, ctx
, graph
,
3386 &node_scc_at_most
, &edge_dst_scc_at_most
,
3388 is_seq
= isl_schedule_node_get_type(node
) == isl_schedule_node_sequence
;
3389 node
= isl_schedule_node_parent(node
);
3390 node
= isl_schedule_node_parent(node
);
3392 node
= isl_schedule_node_sequence_splice_child(node
, 0);
3397 /* Insert a band node at position "node" in the schedule tree corresponding
3398 * to the current band in "graph". Mark the band node permutable
3399 * if "permutable" is set.
3400 * The partial schedules and the coincidence property are extracted
3401 * from the graph nodes.
3402 * Return the updated schedule node.
3404 static __isl_give isl_schedule_node
*insert_current_band(
3405 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
3411 isl_multi_pw_aff
*mpa
;
3412 isl_multi_union_pw_aff
*mupa
;
3418 isl_die(isl_schedule_node_get_ctx(node
), isl_error_internal
,
3419 "graph should have at least one node",
3420 return isl_schedule_node_free(node
));
3422 start
= graph
->band_start
;
3423 end
= graph
->n_total_row
;
3426 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[0], start
, n
);
3427 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3428 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3430 for (i
= 1; i
< graph
->n
; ++i
) {
3431 isl_multi_union_pw_aff
*mupa_i
;
3433 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[i
],
3435 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3436 mupa_i
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3437 mupa
= isl_multi_union_pw_aff_union_add(mupa
, mupa_i
);
3439 node
= isl_schedule_node_insert_partial_schedule(node
, mupa
);
3441 for (i
= 0; i
< n
; ++i
)
3442 node
= isl_schedule_node_band_member_set_coincident(node
, i
,
3443 graph
->node
[0].coincident
[start
+ i
]);
3444 node
= isl_schedule_node_band_set_permutable(node
, permutable
);
3449 /* Update the dependence relations based on the current schedule,
3450 * add the current band to "node" and then continue with the computation
3452 * Return the updated schedule node.
3454 static __isl_give isl_schedule_node
*compute_next_band(
3455 __isl_take isl_schedule_node
*node
,
3456 struct isl_sched_graph
*graph
, int permutable
)
3463 ctx
= isl_schedule_node_get_ctx(node
);
3464 if (update_edges(ctx
, graph
) < 0)
3465 return isl_schedule_node_free(node
);
3466 node
= insert_current_band(node
, graph
, permutable
);
3469 node
= isl_schedule_node_child(node
, 0);
3470 node
= compute_schedule(node
, graph
);
3471 node
= isl_schedule_node_parent(node
);
3476 /* Add constraints to graph->lp that force the dependence "map" (which
3477 * is part of the dependence relation of "edge")
3478 * to be respected and attempt to carry it, where the edge is one from
3479 * a node j to itself. "pos" is the sequence number of the given map.
3480 * That is, add constraints that enforce
3482 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3483 * = c_j_x (y - x) >= e_i
3485 * for each (x,y) in R.
3486 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3487 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3488 * with each coefficient in c_j_x represented as a pair of non-negative
3491 static int add_intra_constraints(struct isl_sched_graph
*graph
,
3492 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
3495 isl_ctx
*ctx
= isl_map_get_ctx(map
);
3496 isl_dim_map
*dim_map
;
3497 isl_basic_set
*coef
;
3498 struct isl_sched_node
*node
= edge
->src
;
3500 coef
= intra_coefficients(graph
, node
, map
);
3504 offset
= coef_var_offset(coef
);
3505 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, 1);
3506 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3507 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
3508 coef
->n_eq
, coef
->n_ineq
);
3509 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
3515 /* Add constraints to graph->lp that force the dependence "map" (which
3516 * is part of the dependence relation of "edge")
3517 * to be respected and attempt to carry it, where the edge is one from
3518 * node j to node k. "pos" is the sequence number of the given map.
3519 * That is, add constraints that enforce
3521 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3523 * for each (x,y) in R.
3524 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3525 * of valid constraints for R and then plug in
3526 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3527 * with each coefficient (except e_i, c_*_0 and c_*_n)
3528 * represented as a pair of non-negative coefficients.
3530 static int add_inter_constraints(struct isl_sched_graph
*graph
,
3531 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
3534 isl_ctx
*ctx
= isl_map_get_ctx(map
);
3535 isl_dim_map
*dim_map
;
3536 isl_basic_set
*coef
;
3537 struct isl_sched_node
*src
= edge
->src
;
3538 struct isl_sched_node
*dst
= edge
->dst
;
3540 coef
= inter_coefficients(graph
, edge
, map
);
3544 offset
= coef_var_offset(coef
);
3545 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, 1);
3546 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3547 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
3548 coef
->n_eq
, coef
->n_ineq
);
3549 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
3555 /* Add constraints to graph->lp that force all (conditional) validity
3556 * dependences to be respected and attempt to carry them.
3558 static int add_all_constraints(struct isl_sched_graph
*graph
)
3564 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3565 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
3567 if (!is_any_validity(edge
))
3570 for (j
= 0; j
< edge
->map
->n
; ++j
) {
3571 isl_basic_map
*bmap
;
3574 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
3575 map
= isl_map_from_basic_map(bmap
);
3577 if (edge
->src
== edge
->dst
&&
3578 add_intra_constraints(graph
, edge
, map
, pos
) < 0)
3580 if (edge
->src
!= edge
->dst
&&
3581 add_inter_constraints(graph
, edge
, map
, pos
) < 0)
3590 /* Count the number of equality and inequality constraints
3591 * that will be added to the carry_lp problem.
3592 * We count each edge exactly once.
3594 static int count_all_constraints(struct isl_sched_graph
*graph
,
3595 int *n_eq
, int *n_ineq
)
3599 *n_eq
= *n_ineq
= 0;
3600 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3601 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
3603 if (!is_any_validity(edge
))
3606 for (j
= 0; j
< edge
->map
->n
; ++j
) {
3607 isl_basic_map
*bmap
;
3610 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
3611 map
= isl_map_from_basic_map(bmap
);
3613 if (count_map_constraints(graph
, edge
, map
,
3614 n_eq
, n_ineq
, 1, 0) < 0)
3622 /* Return the total number of (validity) edges that carry_dependences will
3625 static int count_carry_edges(struct isl_sched_graph
*graph
)
3631 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3632 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
3634 if (!is_any_validity(edge
))
3637 n_edge
+= isl_map_n_basic_map(edge
->map
);
3643 /* Construct an LP problem for finding schedule coefficients
3644 * such that the schedule carries as many validity dependences as possible.
3645 * In particular, for each dependence i, we bound the dependence distance
3646 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3647 * of all e_i's. Dependences with e_i = 0 in the solution are simply
3648 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3649 * Note that if the dependence relation is a union of basic maps,
3650 * then we have to consider each basic map individually as it may only
3651 * be possible to carry the dependences expressed by some of those
3652 * basic maps and not all of them.
3653 * Below, we consider each of those basic maps as a separate "edge".
3655 * All variables of the LP are non-negative. The actual coefficients
3656 * may be negative, so each coefficient is represented as the difference
3657 * of two non-negative variables. The negative part always appears
3658 * immediately before the positive part.
3659 * Other than that, the variables have the following order
3661 * - sum of (1 - e_i) over all edges
3662 * - sum of all c_n coefficients
3663 * (unconstrained when computing non-parametric schedules)
3664 * - sum of positive and negative parts of all c_x coefficients
3669 * - c_i_n (if parametric)
3670 * - positive and negative parts of c_i_x
3672 * The constraints are those from the (validity) edges plus three equalities
3673 * to express the sums and n_edge inequalities to express e_i <= 1.
3675 static isl_stat
setup_carry_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3684 n_edge
= count_carry_edges(graph
);
3687 for (i
= 0; i
< graph
->n
; ++i
) {
3688 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
3689 node
->start
= total
;
3690 total
+= 1 + node
->nparam
+ 2 * node
->nvar
;
3693 if (count_all_constraints(graph
, &n_eq
, &n_ineq
) < 0)
3694 return isl_stat_error
;
3696 dim
= isl_space_set_alloc(ctx
, 0, total
);
3697 isl_basic_set_free(graph
->lp
);
3700 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
3701 graph
->lp
= isl_basic_set_set_rational(graph
->lp
);
3703 k
= isl_basic_set_alloc_equality(graph
->lp
);
3705 return isl_stat_error
;
3706 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
3707 isl_int_set_si(graph
->lp
->eq
[k
][0], -n_edge
);
3708 isl_int_set_si(graph
->lp
->eq
[k
][1], 1);
3709 for (i
= 0; i
< n_edge
; ++i
)
3710 isl_int_set_si(graph
->lp
->eq
[k
][4 + i
], 1);
3712 if (add_param_sum_constraint(graph
, 1) < 0)
3713 return isl_stat_error
;
3714 if (add_var_sum_constraint(graph
, 2) < 0)
3715 return isl_stat_error
;
3717 for (i
= 0; i
< n_edge
; ++i
) {
3718 k
= isl_basic_set_alloc_inequality(graph
->lp
);
3720 return isl_stat_error
;
3721 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
3722 isl_int_set_si(graph
->lp
->ineq
[k
][4 + i
], -1);
3723 isl_int_set_si(graph
->lp
->ineq
[k
][0], 1);
3726 if (add_all_constraints(graph
) < 0)
3727 return isl_stat_error
;
3732 static __isl_give isl_schedule_node
*compute_component_schedule(
3733 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
3736 /* Comparison function for sorting the statements based on
3737 * the corresponding value in "r".
3739 static int smaller_value(const void *a
, const void *b
, void *data
)
3745 return isl_int_cmp(r
->el
[*i1
], r
->el
[*i2
]);
3748 /* If the schedule_split_scaled option is set and if the linear
3749 * parts of the scheduling rows for all nodes in the graphs have
3750 * a non-trivial common divisor, then split off the remainder of the
3751 * constant term modulo this common divisor from the linear part.
3752 * Otherwise, insert a band node directly and continue with
3753 * the construction of the schedule.
3755 * If a non-trivial common divisor is found, then
3756 * the linear part is reduced and the remainder is enforced
3757 * by a sequence node with the children placed in the order
3758 * of this remainder.
3759 * In particular, we assign an scc index based on the remainder and
3760 * then rely on compute_component_schedule to insert the sequence and
3761 * to continue the schedule construction on each part.
3763 static __isl_give isl_schedule_node
*split_scaled(
3764 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3777 ctx
= isl_schedule_node_get_ctx(node
);
3778 if (!ctx
->opt
->schedule_split_scaled
)
3779 return compute_next_band(node
, graph
, 0);
3781 return compute_next_band(node
, graph
, 0);
3784 isl_int_init(gcd_i
);
3786 isl_int_set_si(gcd
, 0);
3788 row
= isl_mat_rows(graph
->node
[0].sched
) - 1;
3790 for (i
= 0; i
< graph
->n
; ++i
) {
3791 struct isl_sched_node
*node
= &graph
->node
[i
];
3792 int cols
= isl_mat_cols(node
->sched
);
3794 isl_seq_gcd(node
->sched
->row
[row
] + 1, cols
- 1, &gcd_i
);
3795 isl_int_gcd(gcd
, gcd
, gcd_i
);
3798 isl_int_clear(gcd_i
);
3800 if (isl_int_cmp_si(gcd
, 1) <= 0) {
3802 return compute_next_band(node
, graph
, 0);
3805 r
= isl_vec_alloc(ctx
, graph
->n
);
3806 order
= isl_calloc_array(ctx
, int, graph
->n
);
3810 for (i
= 0; i
< graph
->n
; ++i
) {
3811 struct isl_sched_node
*node
= &graph
->node
[i
];
3814 isl_int_fdiv_r(r
->el
[i
], node
->sched
->row
[row
][0], gcd
);
3815 isl_int_fdiv_q(node
->sched
->row
[row
][0],
3816 node
->sched
->row
[row
][0], gcd
);
3817 isl_int_mul(node
->sched
->row
[row
][0],
3818 node
->sched
->row
[row
][0], gcd
);
3819 node
->sched
= isl_mat_scale_down_row(node
->sched
, row
, gcd
);
3824 if (isl_sort(order
, graph
->n
, sizeof(order
[0]), &smaller_value
, r
) < 0)
3828 for (i
= 0; i
< graph
->n
; ++i
) {
3829 if (i
> 0 && isl_int_ne(r
->el
[order
[i
- 1]], r
->el
[order
[i
]]))
3831 graph
->node
[order
[i
]].scc
= scc
;
3840 if (update_edges(ctx
, graph
) < 0)
3841 return isl_schedule_node_free(node
);
3842 node
= insert_current_band(node
, graph
, 0);
3845 node
= isl_schedule_node_child(node
, 0);
3846 node
= compute_component_schedule(node
, graph
, 0);
3847 node
= isl_schedule_node_parent(node
);
3854 return isl_schedule_node_free(node
);
3857 /* Is the schedule row "sol" trivial on node "node"?
3858 * That is, is the solution zero on the dimensions orthogonal to
3859 * the previously found solutions?
3860 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3862 * Each coefficient is represented as the difference between
3863 * two non-negative values in "sol". "sol" has been computed
3864 * in terms of the original iterators (i.e., without use of cmap).
3865 * We construct the schedule row s and write it as a linear
3866 * combination of (linear combinations of) previously computed schedule rows.
3867 * s = Q c or c = U s.
3868 * If the final entries of c are all zero, then the solution is trivial.
3870 static int is_trivial(struct isl_sched_node
*node
, __isl_keep isl_vec
*sol
)
3877 if (node
->nvar
== node
->rank
)
3880 node_sol
= extract_var_coef(node
, sol
);
3881 node_sol
= isl_mat_vec_product(isl_mat_copy(node
->cinv
), node_sol
);
3885 trivial
= isl_seq_first_non_zero(node_sol
->el
+ node
->rank
,
3886 node
->nvar
- node
->rank
) == -1;
3888 isl_vec_free(node_sol
);
3893 /* Is the schedule row "sol" trivial on any node where it should
3895 * "sol" has been computed in terms of the original iterators
3896 * (i.e., without use of cmap).
3897 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3899 static int is_any_trivial(struct isl_sched_graph
*graph
,
3900 __isl_keep isl_vec
*sol
)
3904 for (i
= 0; i
< graph
->n
; ++i
) {
3905 struct isl_sched_node
*node
= &graph
->node
[i
];
3908 if (!needs_row(graph
, node
))
3910 trivial
= is_trivial(node
, sol
);
3911 if (trivial
< 0 || trivial
)
3918 /* Does the schedule represented by "sol" perform loop coalescing on "node"?
3919 * If so, return the position of the coalesced dimension.
3920 * Otherwise, return node->nvar or -1 on error.
3922 * In particular, look for pairs of coefficients c_i and c_j such that
3923 * |c_j/c_i| >= size_i, i.e., |c_j| >= |c_i * size_i|.
3924 * If any such pair is found, then return i.
3925 * If size_i is infinity, then no check on c_i needs to be performed.
3927 static int find_node_coalescing(struct isl_sched_node
*node
,
3928 __isl_keep isl_vec
*sol
)
3934 if (node
->nvar
<= 1)
3937 csol
= extract_var_coef(node
, sol
);
3941 for (i
= 0; i
< node
->nvar
; ++i
) {
3944 if (isl_int_is_zero(csol
->el
[i
]))
3946 v
= isl_multi_val_get_val(node
->sizes
, i
);
3949 if (!isl_val_is_int(v
)) {
3953 isl_int_mul(max
, v
->n
, csol
->el
[i
]);
3956 for (j
= 0; j
< node
->nvar
; ++j
) {
3959 if (isl_int_abs_ge(csol
->el
[j
], max
))
3975 /* Force the schedule coefficient at position "pos" of "node" to be zero
3977 * The coefficient is encoded as the difference between two non-negative
3978 * variables. Force these two variables to have the same value.
3980 static __isl_give isl_tab_lexmin
*zero_out_node_coef(
3981 __isl_take isl_tab_lexmin
*tl
, struct isl_sched_node
*node
, int pos
)
3987 ctx
= isl_space_get_ctx(node
->space
);
3988 dim
= isl_tab_lexmin_dim(tl
);
3990 return isl_tab_lexmin_free(tl
);
3991 eq
= isl_vec_alloc(ctx
, 1 + dim
);
3992 eq
= isl_vec_clr(eq
);
3994 return isl_tab_lexmin_free(tl
);
3996 pos
= 1 + node_var_coef_offset(node
) + 2 * pos
;
3997 isl_int_set_si(eq
->el
[pos
], 1);
3998 isl_int_set_si(eq
->el
[pos
+ 1], -1);
3999 tl
= isl_tab_lexmin_add_eq(tl
, eq
->el
);
4005 /* Return the lexicographically smallest rational point in the basic set
4006 * from which "tl" was constructed, double checking that this input set
4009 static __isl_give isl_vec
*non_empty_solution(__isl_keep isl_tab_lexmin
*tl
)
4013 sol
= isl_tab_lexmin_get_solution(tl
);
4017 isl_die(isl_vec_get_ctx(sol
), isl_error_internal
,
4018 "error in schedule construction",
4019 return isl_vec_free(sol
));
4023 /* Does the solution "sol" of the LP problem constructed by setup_carry_lp
4024 * carry any of the "n_edge" groups of dependences?
4025 * The value in the first position is the sum of (1 - e_i) over all "n_edge"
4026 * edges, with 0 <= e_i <= 1 equal to 1 when the dependences represented
4027 * by the edge are carried by the solution.
4028 * If the sum of the (1 - e_i) is smaller than "n_edge" then at least
4029 * one of those is carried.
4031 * Note that despite the fact that the problem is solved using a rational
4032 * solver, the solution is guaranteed to be integral.
4033 * Specifically, the dependence distance lower bounds e_i (and therefore
4034 * also their sum) are integers. See Lemma 5 of [1].
4036 * Any potential denominator of the sum is cleared by this function.
4037 * The denominator is not relevant for any of the other elements
4040 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4041 * Problem, Part II: Multi-Dimensional Time.
4042 * In Intl. Journal of Parallel Programming, 1992.
4044 static int carries_dependences(__isl_keep isl_vec
*sol
, int n_edge
)
4046 isl_int_divexact(sol
->el
[1], sol
->el
[1], sol
->el
[0]);
4047 isl_int_set_si(sol
->el
[0], 1);
4048 return isl_int_cmp_si(sol
->el
[1], n_edge
) < 0;
4051 /* Return the lexicographically smallest rational point in "lp",
4052 * assuming that all variables are non-negative and performing some
4053 * additional sanity checks.
4054 * In particular, "lp" should not be empty by construction.
4055 * Double check that this is the case.
4056 * Also, check that dependences are carried for at least one of
4057 * the "n_edge" edges.
4059 * If the computed schedule performs loop coalescing on a given node,
4060 * i.e., if it is of the form
4062 * c_i i + c_j j + ...
4064 * with |c_j/c_i| >= size_i, then force the coefficient c_i to be zero
4065 * to cut out this solution. Repeat this process until no more loop
4066 * coalescing occurs or until no more dependences can be carried.
4067 * In the latter case, revert to the previously computed solution.
4069 static __isl_give isl_vec
*non_neg_lexmin(struct isl_sched_graph
*graph
,
4070 __isl_take isl_basic_set
*lp
, int n_edge
)
4075 isl_vec
*sol
, *prev
= NULL
;
4076 int treat_coalescing
;
4080 ctx
= isl_basic_set_get_ctx(lp
);
4081 treat_coalescing
= isl_options_get_schedule_treat_coalescing(ctx
);
4082 tl
= isl_tab_lexmin_from_basic_set(lp
);
4085 sol
= non_empty_solution(tl
);
4089 if (!carries_dependences(sol
, n_edge
)) {
4091 isl_die(ctx
, isl_error_unknown
,
4092 "unable to carry dependences",
4098 prev
= isl_vec_free(prev
);
4099 if (!treat_coalescing
)
4101 for (i
= 0; i
< graph
->n
; ++i
) {
4102 struct isl_sched_node
*node
= &graph
->node
[i
];
4104 pos
= find_node_coalescing(node
, sol
);
4107 if (pos
< node
->nvar
)
4112 tl
= zero_out_node_coef(tl
, &graph
->node
[i
], pos
);
4114 } while (i
< graph
->n
);
4116 isl_tab_lexmin_free(tl
);
4120 isl_tab_lexmin_free(tl
);
4126 /* Construct a schedule row for each node such that as many validity dependences
4127 * as possible are carried and then continue with the next band.
4129 * If there are no validity dependences, then no dependence can be carried and
4130 * the procedure is guaranteed to fail. If there is more than one component,
4131 * then try computing a schedule on each component separately
4132 * to prevent or at least postpone this failure.
4134 * If the computed schedule row turns out to be trivial on one or
4135 * more nodes where it should not be trivial, then we throw it away
4136 * and try again on each component separately.
4138 * If there is only one component, then we accept the schedule row anyway,
4139 * but we do not consider it as a complete row and therefore do not
4140 * increment graph->n_row. Note that the ranks of the nodes that
4141 * do get a non-trivial schedule part will get updated regardless and
4142 * graph->maxvar is computed based on these ranks. The test for
4143 * whether more schedule rows are required in compute_schedule_wcc
4144 * is therefore not affected.
4146 * Insert a band corresponding to the schedule row at position "node"
4147 * of the schedule tree and continue with the construction of the schedule.
4148 * This insertion and the continued construction is performed by split_scaled
4149 * after optionally checking for non-trivial common divisors.
4151 static __isl_give isl_schedule_node
*carry_dependences(
4152 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4163 n_edge
= count_carry_edges(graph
);
4164 if (n_edge
== 0 && graph
->scc
> 1)
4165 return compute_component_schedule(node
, graph
, 1);
4167 ctx
= isl_schedule_node_get_ctx(node
);
4168 if (setup_carry_lp(ctx
, graph
) < 0)
4169 return isl_schedule_node_free(node
);
4171 lp
= isl_basic_set_copy(graph
->lp
);
4172 sol
= non_neg_lexmin(graph
, lp
, n_edge
);
4174 return isl_schedule_node_free(node
);
4176 trivial
= is_any_trivial(graph
, sol
);
4178 sol
= isl_vec_free(sol
);
4179 } else if (trivial
&& graph
->scc
> 1) {
4181 return compute_component_schedule(node
, graph
, 1);
4184 if (update_schedule(graph
, sol
, 0, 0) < 0)
4185 return isl_schedule_node_free(node
);
4189 return split_scaled(node
, graph
);
4192 /* Topologically sort statements mapped to the same schedule iteration
4193 * and add insert a sequence node in front of "node"
4194 * corresponding to this order.
4195 * If "initialized" is set, then it may be assumed that compute_maxvar
4196 * has been called on the current band. Otherwise, call
4197 * compute_maxvar if and before carry_dependences gets called.
4199 * If it turns out to be impossible to sort the statements apart,
4200 * because different dependences impose different orderings
4201 * on the statements, then we extend the schedule such that
4202 * it carries at least one more dependence.
4204 static __isl_give isl_schedule_node
*sort_statements(
4205 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
4209 isl_union_set_list
*filters
;
4214 ctx
= isl_schedule_node_get_ctx(node
);
4216 isl_die(ctx
, isl_error_internal
,
4217 "graph should have at least one node",
4218 return isl_schedule_node_free(node
));
4223 if (update_edges(ctx
, graph
) < 0)
4224 return isl_schedule_node_free(node
);
4226 if (graph
->n_edge
== 0)
4229 if (detect_sccs(ctx
, graph
) < 0)
4230 return isl_schedule_node_free(node
);
4233 if (graph
->scc
< graph
->n
) {
4234 if (!initialized
&& compute_maxvar(graph
) < 0)
4235 return isl_schedule_node_free(node
);
4236 return carry_dependences(node
, graph
);
4239 filters
= extract_sccs(ctx
, graph
);
4240 node
= isl_schedule_node_insert_sequence(node
, filters
);
4245 /* Are there any (non-empty) (conditional) validity edges in the graph?
4247 static int has_validity_edges(struct isl_sched_graph
*graph
)
4251 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4254 empty
= isl_map_plain_is_empty(graph
->edge
[i
].map
);
4259 if (is_any_validity(&graph
->edge
[i
]))
4266 /* Should we apply a Feautrier step?
4267 * That is, did the user request the Feautrier algorithm and are
4268 * there any validity dependences (left)?
4270 static int need_feautrier_step(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
4272 if (ctx
->opt
->schedule_algorithm
!= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
)
4275 return has_validity_edges(graph
);
4278 /* Compute a schedule for a connected dependence graph using Feautrier's
4279 * multi-dimensional scheduling algorithm and return the updated schedule node.
4281 * The original algorithm is described in [1].
4282 * The main idea is to minimize the number of scheduling dimensions, by
4283 * trying to satisfy as many dependences as possible per scheduling dimension.
4285 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4286 * Problem, Part II: Multi-Dimensional Time.
4287 * In Intl. Journal of Parallel Programming, 1992.
4289 static __isl_give isl_schedule_node
*compute_schedule_wcc_feautrier(
4290 isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4292 return carry_dependences(node
, graph
);
4295 /* Turn off the "local" bit on all (condition) edges.
4297 static void clear_local_edges(struct isl_sched_graph
*graph
)
4301 for (i
= 0; i
< graph
->n_edge
; ++i
)
4302 if (is_condition(&graph
->edge
[i
]))
4303 clear_local(&graph
->edge
[i
]);
4306 /* Does "graph" have both condition and conditional validity edges?
4308 static int need_condition_check(struct isl_sched_graph
*graph
)
4311 int any_condition
= 0;
4312 int any_conditional_validity
= 0;
4314 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4315 if (is_condition(&graph
->edge
[i
]))
4317 if (is_conditional_validity(&graph
->edge
[i
]))
4318 any_conditional_validity
= 1;
4321 return any_condition
&& any_conditional_validity
;
4324 /* Does "graph" contain any coincidence edge?
4326 static int has_any_coincidence(struct isl_sched_graph
*graph
)
4330 for (i
= 0; i
< graph
->n_edge
; ++i
)
4331 if (is_coincidence(&graph
->edge
[i
]))
4337 /* Extract the final schedule row as a map with the iteration domain
4338 * of "node" as domain.
4340 static __isl_give isl_map
*final_row(struct isl_sched_node
*node
)
4345 row
= isl_mat_rows(node
->sched
) - 1;
4346 ma
= node_extract_partial_schedule_multi_aff(node
, row
, 1);
4347 return isl_map_from_multi_aff(ma
);
4350 /* Is the conditional validity dependence in the edge with index "edge_index"
4351 * violated by the latest (i.e., final) row of the schedule?
4352 * That is, is i scheduled after j
4353 * for any conditional validity dependence i -> j?
4355 static int is_violated(struct isl_sched_graph
*graph
, int edge_index
)
4357 isl_map
*src_sched
, *dst_sched
, *map
;
4358 struct isl_sched_edge
*edge
= &graph
->edge
[edge_index
];
4361 src_sched
= final_row(edge
->src
);
4362 dst_sched
= final_row(edge
->dst
);
4363 map
= isl_map_copy(edge
->map
);
4364 map
= isl_map_apply_domain(map
, src_sched
);
4365 map
= isl_map_apply_range(map
, dst_sched
);
4366 map
= isl_map_order_gt(map
, isl_dim_in
, 0, isl_dim_out
, 0);
4367 empty
= isl_map_is_empty(map
);
4376 /* Does "graph" have any satisfied condition edges that
4377 * are adjacent to the conditional validity constraint with
4378 * domain "conditional_source" and range "conditional_sink"?
4380 * A satisfied condition is one that is not local.
4381 * If a condition was forced to be local already (i.e., marked as local)
4382 * then there is no need to check if it is in fact local.
4384 * Additionally, mark all adjacent condition edges found as local.
4386 static int has_adjacent_true_conditions(struct isl_sched_graph
*graph
,
4387 __isl_keep isl_union_set
*conditional_source
,
4388 __isl_keep isl_union_set
*conditional_sink
)
4393 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4394 int adjacent
, local
;
4395 isl_union_map
*condition
;
4397 if (!is_condition(&graph
->edge
[i
]))
4399 if (is_local(&graph
->edge
[i
]))
4402 condition
= graph
->edge
[i
].tagged_condition
;
4403 adjacent
= domain_intersects(condition
, conditional_sink
);
4404 if (adjacent
>= 0 && !adjacent
)
4405 adjacent
= range_intersects(condition
,
4406 conditional_source
);
4412 set_local(&graph
->edge
[i
]);
4414 local
= is_condition_false(&graph
->edge
[i
]);
4424 /* Are there any violated conditional validity dependences with
4425 * adjacent condition dependences that are not local with respect
4426 * to the current schedule?
4427 * That is, is the conditional validity constraint violated?
4429 * Additionally, mark all those adjacent condition dependences as local.
4430 * We also mark those adjacent condition dependences that were not marked
4431 * as local before, but just happened to be local already. This ensures
4432 * that they remain local if the schedule is recomputed.
4434 * We first collect domain and range of all violated conditional validity
4435 * dependences and then check if there are any adjacent non-local
4436 * condition dependences.
4438 static int has_violated_conditional_constraint(isl_ctx
*ctx
,
4439 struct isl_sched_graph
*graph
)
4443 isl_union_set
*source
, *sink
;
4445 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
4446 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
4447 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4448 isl_union_set
*uset
;
4449 isl_union_map
*umap
;
4452 if (!is_conditional_validity(&graph
->edge
[i
]))
4455 violated
= is_violated(graph
, i
);
4463 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
4464 uset
= isl_union_map_domain(umap
);
4465 source
= isl_union_set_union(source
, uset
);
4466 source
= isl_union_set_coalesce(source
);
4468 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
4469 uset
= isl_union_map_range(umap
);
4470 sink
= isl_union_set_union(sink
, uset
);
4471 sink
= isl_union_set_coalesce(sink
);
4475 any
= has_adjacent_true_conditions(graph
, source
, sink
);
4477 isl_union_set_free(source
);
4478 isl_union_set_free(sink
);
4481 isl_union_set_free(source
);
4482 isl_union_set_free(sink
);
4486 /* Examine the current band (the rows between graph->band_start and
4487 * graph->n_total_row), deciding whether to drop it or add it to "node"
4488 * and then continue with the computation of the next band, if any.
4489 * If "initialized" is set, then it may be assumed that compute_maxvar
4490 * has been called on the current band. Otherwise, call
4491 * compute_maxvar if and before carry_dependences gets called.
4493 * The caller keeps looking for a new row as long as
4494 * graph->n_row < graph->maxvar. If the latest attempt to find
4495 * such a row failed (i.e., we still have graph->n_row < graph->maxvar),
4497 * - split between SCCs and start over (assuming we found an interesting
4498 * pair of SCCs between which to split)
4499 * - continue with the next band (assuming the current band has at least
4501 * - try to carry as many dependences as possible and continue with the next
4503 * In each case, we first insert a band node in the schedule tree
4504 * if any rows have been computed.
4506 * If the caller managed to complete the schedule, we insert a band node
4507 * (if any schedule rows were computed) and we finish off by topologically
4508 * sorting the statements based on the remaining dependences.
4510 static __isl_give isl_schedule_node
*compute_schedule_finish_band(
4511 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
4519 if (graph
->n_row
< graph
->maxvar
) {
4521 int empty
= graph
->n_total_row
== graph
->band_start
;
4523 ctx
= isl_schedule_node_get_ctx(node
);
4524 if (!ctx
->opt
->schedule_maximize_band_depth
&& !empty
)
4525 return compute_next_band(node
, graph
, 1);
4526 if (graph
->src_scc
>= 0)
4527 return compute_split_schedule(node
, graph
);
4529 return compute_next_band(node
, graph
, 1);
4530 if (!initialized
&& compute_maxvar(graph
) < 0)
4531 return isl_schedule_node_free(node
);
4532 return carry_dependences(node
, graph
);
4535 insert
= graph
->n_total_row
> graph
->band_start
;
4537 node
= insert_current_band(node
, graph
, 1);
4538 node
= isl_schedule_node_child(node
, 0);
4540 node
= sort_statements(node
, graph
, initialized
);
4542 node
= isl_schedule_node_parent(node
);
4547 /* Construct a band of schedule rows for a connected dependence graph.
4548 * The caller is responsible for determining the strongly connected
4549 * components and calling compute_maxvar first.
4551 * We try to find a sequence of as many schedule rows as possible that result
4552 * in non-negative dependence distances (independent of the previous rows
4553 * in the sequence, i.e., such that the sequence is tilable), with as
4554 * many of the initial rows as possible satisfying the coincidence constraints.
4555 * The computation stops if we can't find any more rows or if we have found
4556 * all the rows we wanted to find.
4558 * If ctx->opt->schedule_outer_coincidence is set, then we force the
4559 * outermost dimension to satisfy the coincidence constraints. If this
4560 * turns out to be impossible, we fall back on the general scheme above
4561 * and try to carry as many dependences as possible.
4563 * If "graph" contains both condition and conditional validity dependences,
4564 * then we need to check that that the conditional schedule constraint
4565 * is satisfied, i.e., there are no violated conditional validity dependences
4566 * that are adjacent to any non-local condition dependences.
4567 * If there are, then we mark all those adjacent condition dependences
4568 * as local and recompute the current band. Those dependences that
4569 * are marked local will then be forced to be local.
4570 * The initial computation is performed with no dependences marked as local.
4571 * If we are lucky, then there will be no violated conditional validity
4572 * dependences adjacent to any non-local condition dependences.
4573 * Otherwise, we mark some additional condition dependences as local and
4574 * recompute. We continue this process until there are no violations left or
4575 * until we are no longer able to compute a schedule.
4576 * Since there are only a finite number of dependences,
4577 * there will only be a finite number of iterations.
4579 static isl_stat
compute_schedule_wcc_band(isl_ctx
*ctx
,
4580 struct isl_sched_graph
*graph
)
4582 int has_coincidence
;
4583 int use_coincidence
;
4584 int force_coincidence
= 0;
4585 int check_conditional
;
4587 if (sort_sccs(graph
) < 0)
4588 return isl_stat_error
;
4590 clear_local_edges(graph
);
4591 check_conditional
= need_condition_check(graph
);
4592 has_coincidence
= has_any_coincidence(graph
);
4594 if (ctx
->opt
->schedule_outer_coincidence
)
4595 force_coincidence
= 1;
4597 use_coincidence
= has_coincidence
;
4598 while (graph
->n_row
< graph
->maxvar
) {
4603 graph
->src_scc
= -1;
4604 graph
->dst_scc
= -1;
4606 if (setup_lp(ctx
, graph
, use_coincidence
) < 0)
4607 return isl_stat_error
;
4608 sol
= solve_lp(graph
);
4610 return isl_stat_error
;
4611 if (sol
->size
== 0) {
4612 int empty
= graph
->n_total_row
== graph
->band_start
;
4615 if (use_coincidence
&& (!force_coincidence
|| !empty
)) {
4616 use_coincidence
= 0;
4621 coincident
= !has_coincidence
|| use_coincidence
;
4622 if (update_schedule(graph
, sol
, 1, coincident
) < 0)
4623 return isl_stat_error
;
4625 if (!check_conditional
)
4627 violated
= has_violated_conditional_constraint(ctx
, graph
);
4629 return isl_stat_error
;
4632 if (reset_band(graph
) < 0)
4633 return isl_stat_error
;
4634 use_coincidence
= has_coincidence
;
4640 /* Compute a schedule for a connected dependence graph by considering
4641 * the graph as a whole and return the updated schedule node.
4643 * The actual schedule rows of the current band are computed by
4644 * compute_schedule_wcc_band. compute_schedule_finish_band takes
4645 * care of integrating the band into "node" and continuing
4648 static __isl_give isl_schedule_node
*compute_schedule_wcc_whole(
4649 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4656 ctx
= isl_schedule_node_get_ctx(node
);
4657 if (compute_schedule_wcc_band(ctx
, graph
) < 0)
4658 return isl_schedule_node_free(node
);
4660 return compute_schedule_finish_band(node
, graph
, 1);
4663 /* Clustering information used by compute_schedule_wcc_clustering.
4665 * "n" is the number of SCCs in the original dependence graph
4666 * "scc" is an array of "n" elements, each representing an SCC
4667 * of the original dependence graph. All entries in the same cluster
4668 * have the same number of schedule rows.
4669 * "scc_cluster" maps each SCC index to the cluster to which it belongs,
4670 * where each cluster is represented by the index of the first SCC
4671 * in the cluster. Initially, each SCC belongs to a cluster containing
4674 * "scc_in_merge" is used by merge_clusters_along_edge to keep
4675 * track of which SCCs need to be merged.
4677 * "cluster" contains the merged clusters of SCCs after the clustering
4680 * "scc_node" is a temporary data structure used inside copy_partial.
4681 * For each SCC, it keeps track of the number of nodes in the SCC
4682 * that have already been copied.
4684 struct isl_clustering
{
4686 struct isl_sched_graph
*scc
;
4687 struct isl_sched_graph
*cluster
;
4693 /* Initialize the clustering data structure "c" from "graph".
4695 * In particular, allocate memory, extract the SCCs from "graph"
4696 * into c->scc, initialize scc_cluster and construct
4697 * a band of schedule rows for each SCC.
4698 * Within each SCC, there is only one SCC by definition.
4699 * Each SCC initially belongs to a cluster containing only that SCC.
4701 static isl_stat
clustering_init(isl_ctx
*ctx
, struct isl_clustering
*c
,
4702 struct isl_sched_graph
*graph
)
4707 c
->scc
= isl_calloc_array(ctx
, struct isl_sched_graph
, c
->n
);
4708 c
->cluster
= isl_calloc_array(ctx
, struct isl_sched_graph
, c
->n
);
4709 c
->scc_cluster
= isl_calloc_array(ctx
, int, c
->n
);
4710 c
->scc_node
= isl_calloc_array(ctx
, int, c
->n
);
4711 c
->scc_in_merge
= isl_calloc_array(ctx
, int, c
->n
);
4712 if (!c
->scc
|| !c
->cluster
||
4713 !c
->scc_cluster
|| !c
->scc_node
|| !c
->scc_in_merge
)
4714 return isl_stat_error
;
4716 for (i
= 0; i
< c
->n
; ++i
) {
4717 if (extract_sub_graph(ctx
, graph
, &node_scc_exactly
,
4718 &edge_scc_exactly
, i
, &c
->scc
[i
]) < 0)
4719 return isl_stat_error
;
4721 if (compute_maxvar(&c
->scc
[i
]) < 0)
4722 return isl_stat_error
;
4723 if (compute_schedule_wcc_band(ctx
, &c
->scc
[i
]) < 0)
4724 return isl_stat_error
;
4725 c
->scc_cluster
[i
] = i
;
4731 /* Free all memory allocated for "c".
4733 static void clustering_free(isl_ctx
*ctx
, struct isl_clustering
*c
)
4738 for (i
= 0; i
< c
->n
; ++i
)
4739 graph_free(ctx
, &c
->scc
[i
]);
4742 for (i
= 0; i
< c
->n
; ++i
)
4743 graph_free(ctx
, &c
->cluster
[i
]);
4745 free(c
->scc_cluster
);
4747 free(c
->scc_in_merge
);
4750 /* Should we refrain from merging the cluster in "graph" with
4751 * any other cluster?
4752 * In particular, is its current schedule band empty and incomplete.
4754 static int bad_cluster(struct isl_sched_graph
*graph
)
4756 return graph
->n_row
< graph
->maxvar
&&
4757 graph
->n_total_row
== graph
->band_start
;
4760 /* Return the index of an edge in "graph" that can be used to merge
4761 * two clusters in "c".
4762 * Return graph->n_edge if no such edge can be found.
4763 * Return -1 on error.
4765 * In particular, return a proximity edge between two clusters
4766 * that is not marked "no_merge" and such that neither of the
4767 * two clusters has an incomplete, empty band.
4769 * If there are multiple such edges, then try and find the most
4770 * appropriate edge to use for merging. In particular, pick the edge
4771 * with the greatest weight. If there are multiple of those,
4772 * then pick one with the shortest distance between
4773 * the two cluster representatives.
4775 static int find_proximity(struct isl_sched_graph
*graph
,
4776 struct isl_clustering
*c
)
4778 int i
, best
= graph
->n_edge
, best_dist
, best_weight
;
4780 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4781 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
4784 if (!is_proximity(edge
))
4788 if (bad_cluster(&c
->scc
[edge
->src
->scc
]) ||
4789 bad_cluster(&c
->scc
[edge
->dst
->scc
]))
4791 dist
= c
->scc_cluster
[edge
->dst
->scc
] -
4792 c
->scc_cluster
[edge
->src
->scc
];
4795 weight
= edge
->weight
;
4796 if (best
< graph
->n_edge
) {
4797 if (best_weight
> weight
)
4799 if (best_weight
== weight
&& best_dist
<= dist
)
4804 best_weight
= weight
;
4810 /* Internal data structure used in mark_merge_sccs.
4812 * "graph" is the dependence graph in which a strongly connected
4813 * component is constructed.
4814 * "scc_cluster" maps each SCC index to the cluster to which it belongs.
4815 * "src" and "dst" are the indices of the nodes that are being merged.
4817 struct isl_mark_merge_sccs_data
{
4818 struct isl_sched_graph
*graph
;
4824 /* Check whether the cluster containing node "i" depends on the cluster
4825 * containing node "j". If "i" and "j" belong to the same cluster,
4826 * then they are taken to depend on each other to ensure that
4827 * the resulting strongly connected component consists of complete
4828 * clusters. Furthermore, if "i" and "j" are the two nodes that
4829 * are being merged, then they are taken to depend on each other as well.
4830 * Otherwise, check if there is a (conditional) validity dependence
4831 * from node[j] to node[i], forcing node[i] to follow node[j].
4833 static isl_bool
cluster_follows(int i
, int j
, void *user
)
4835 struct isl_mark_merge_sccs_data
*data
= user
;
4836 struct isl_sched_graph
*graph
= data
->graph
;
4837 int *scc_cluster
= data
->scc_cluster
;
4839 if (data
->src
== i
&& data
->dst
== j
)
4840 return isl_bool_true
;
4841 if (data
->src
== j
&& data
->dst
== i
)
4842 return isl_bool_true
;
4843 if (scc_cluster
[graph
->node
[i
].scc
] == scc_cluster
[graph
->node
[j
].scc
])
4844 return isl_bool_true
;
4846 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
4849 /* Mark all SCCs that belong to either of the two clusters in "c"
4850 * connected by the edge in "graph" with index "edge", or to any
4851 * of the intermediate clusters.
4852 * The marking is recorded in c->scc_in_merge.
4854 * The given edge has been selected for merging two clusters,
4855 * meaning that there is at least a proximity edge between the two nodes.
4856 * However, there may also be (indirect) validity dependences
4857 * between the two nodes. When merging the two clusters, all clusters
4858 * containing one or more of the intermediate nodes along the
4859 * indirect validity dependences need to be merged in as well.
4861 * First collect all such nodes by computing the strongly connected
4862 * component (SCC) containing the two nodes connected by the edge, where
4863 * the two nodes are considered to depend on each other to make
4864 * sure they end up in the same SCC. Similarly, each node is considered
4865 * to depend on every other node in the same cluster to ensure
4866 * that the SCC consists of complete clusters.
4868 * Then the original SCCs that contain any of these nodes are marked
4869 * in c->scc_in_merge.
4871 static isl_stat
mark_merge_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
4872 int edge
, struct isl_clustering
*c
)
4874 struct isl_mark_merge_sccs_data data
;
4875 struct isl_tarjan_graph
*g
;
4878 for (i
= 0; i
< c
->n
; ++i
)
4879 c
->scc_in_merge
[i
] = 0;
4882 data
.scc_cluster
= c
->scc_cluster
;
4883 data
.src
= graph
->edge
[edge
].src
- graph
->node
;
4884 data
.dst
= graph
->edge
[edge
].dst
- graph
->node
;
4886 g
= isl_tarjan_graph_component(ctx
, graph
->n
, data
.dst
,
4887 &cluster_follows
, &data
);
4893 isl_die(ctx
, isl_error_internal
,
4894 "expecting at least two nodes in component",
4896 if (g
->order
[--i
] != -1)
4897 isl_die(ctx
, isl_error_internal
,
4898 "expecting end of component marker", goto error
);
4900 for (--i
; i
>= 0 && g
->order
[i
] != -1; --i
) {
4901 int scc
= graph
->node
[g
->order
[i
]].scc
;
4902 c
->scc_in_merge
[scc
] = 1;
4905 isl_tarjan_graph_free(g
);
4908 isl_tarjan_graph_free(g
);
4909 return isl_stat_error
;
4912 /* Construct the identifier "cluster_i".
4914 static __isl_give isl_id
*cluster_id(isl_ctx
*ctx
, int i
)
4918 snprintf(name
, sizeof(name
), "cluster_%d", i
);
4919 return isl_id_alloc(ctx
, name
, NULL
);
4922 /* Construct the space of the cluster with index "i" containing
4923 * the strongly connected component "scc".
4925 * In particular, construct a space called cluster_i with dimension equal
4926 * to the number of schedule rows in the current band of "scc".
4928 static __isl_give isl_space
*cluster_space(struct isl_sched_graph
*scc
, int i
)
4934 nvar
= scc
->n_total_row
- scc
->band_start
;
4935 space
= isl_space_copy(scc
->node
[0].space
);
4936 space
= isl_space_params(space
);
4937 space
= isl_space_set_from_params(space
);
4938 space
= isl_space_add_dims(space
, isl_dim_set
, nvar
);
4939 id
= cluster_id(isl_space_get_ctx(space
), i
);
4940 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
4945 /* Collect the domain of the graph for merging clusters.
4947 * In particular, for each cluster with first SCC "i", construct
4948 * a set in the space called cluster_i with dimension equal
4949 * to the number of schedule rows in the current band of the cluster.
4951 static __isl_give isl_union_set
*collect_domain(isl_ctx
*ctx
,
4952 struct isl_sched_graph
*graph
, struct isl_clustering
*c
)
4956 isl_union_set
*domain
;
4958 space
= isl_space_params_alloc(ctx
, 0);
4959 domain
= isl_union_set_empty(space
);
4961 for (i
= 0; i
< graph
->scc
; ++i
) {
4964 if (!c
->scc_in_merge
[i
])
4966 if (c
->scc_cluster
[i
] != i
)
4968 space
= cluster_space(&c
->scc
[i
], i
);
4969 domain
= isl_union_set_add_set(domain
, isl_set_universe(space
));
4975 /* Construct a map from the original instances to the corresponding
4976 * cluster instance in the current bands of the clusters in "c".
4978 static __isl_give isl_union_map
*collect_cluster_map(isl_ctx
*ctx
,
4979 struct isl_sched_graph
*graph
, struct isl_clustering
*c
)
4983 isl_union_map
*cluster_map
;
4985 space
= isl_space_params_alloc(ctx
, 0);
4986 cluster_map
= isl_union_map_empty(space
);
4987 for (i
= 0; i
< graph
->scc
; ++i
) {
4991 if (!c
->scc_in_merge
[i
])
4994 id
= cluster_id(ctx
, c
->scc_cluster
[i
]);
4995 start
= c
->scc
[i
].band_start
;
4996 n
= c
->scc
[i
].n_total_row
- start
;
4997 for (j
= 0; j
< c
->scc
[i
].n
; ++j
) {
5000 struct isl_sched_node
*node
= &c
->scc
[i
].node
[j
];
5002 ma
= node_extract_partial_schedule_multi_aff(node
,
5004 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
,
5006 map
= isl_map_from_multi_aff(ma
);
5007 cluster_map
= isl_union_map_add_map(cluster_map
, map
);
5015 /* Add "umap" to the schedule constraints "sc" of all types of "edge"
5016 * that are not isl_edge_condition or isl_edge_conditional_validity.
5018 static __isl_give isl_schedule_constraints
*add_non_conditional_constraints(
5019 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*umap
,
5020 __isl_take isl_schedule_constraints
*sc
)
5022 enum isl_edge_type t
;
5027 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
5028 if (t
== isl_edge_condition
||
5029 t
== isl_edge_conditional_validity
)
5031 if (!is_type(edge
, t
))
5033 sc
= isl_schedule_constraints_add(sc
, t
,
5034 isl_union_map_copy(umap
));
5040 /* Add schedule constraints of types isl_edge_condition and
5041 * isl_edge_conditional_validity to "sc" by applying "umap" to
5042 * the domains of the wrapped relations in domain and range
5043 * of the corresponding tagged constraints of "edge".
5045 static __isl_give isl_schedule_constraints
*add_conditional_constraints(
5046 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*umap
,
5047 __isl_take isl_schedule_constraints
*sc
)
5049 enum isl_edge_type t
;
5050 isl_union_map
*tagged
;
5052 for (t
= isl_edge_condition
; t
<= isl_edge_conditional_validity
; ++t
) {
5053 if (!is_type(edge
, t
))
5055 if (t
== isl_edge_condition
)
5056 tagged
= isl_union_map_copy(edge
->tagged_condition
);
5058 tagged
= isl_union_map_copy(edge
->tagged_validity
);
5059 tagged
= isl_union_map_zip(tagged
);
5060 tagged
= isl_union_map_apply_domain(tagged
,
5061 isl_union_map_copy(umap
));
5062 tagged
= isl_union_map_zip(tagged
);
5063 sc
= isl_schedule_constraints_add(sc
, t
, tagged
);
5071 /* Given a mapping "cluster_map" from the original instances to
5072 * the cluster instances, add schedule constraints on the clusters
5073 * to "sc" corresponding to the original constraints represented by "edge".
5075 * For non-tagged dependence constraints, the cluster constraints
5076 * are obtained by applying "cluster_map" to the edge->map.
5078 * For tagged dependence constraints, "cluster_map" needs to be applied
5079 * to the domains of the wrapped relations in domain and range
5080 * of the tagged dependence constraints. Pick out the mappings
5081 * from these domains from "cluster_map" and construct their product.
5082 * This mapping can then be applied to the pair of domains.
5084 static __isl_give isl_schedule_constraints
*collect_edge_constraints(
5085 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*cluster_map
,
5086 __isl_take isl_schedule_constraints
*sc
)
5088 isl_union_map
*umap
;
5090 isl_union_set
*uset
;
5091 isl_union_map
*umap1
, *umap2
;
5096 umap
= isl_union_map_from_map(isl_map_copy(edge
->map
));
5097 umap
= isl_union_map_apply_domain(umap
,
5098 isl_union_map_copy(cluster_map
));
5099 umap
= isl_union_map_apply_range(umap
,
5100 isl_union_map_copy(cluster_map
));
5101 sc
= add_non_conditional_constraints(edge
, umap
, sc
);
5102 isl_union_map_free(umap
);
5104 if (!sc
|| (!is_condition(edge
) && !is_conditional_validity(edge
)))
5107 space
= isl_space_domain(isl_map_get_space(edge
->map
));
5108 uset
= isl_union_set_from_set(isl_set_universe(space
));
5109 umap1
= isl_union_map_copy(cluster_map
);
5110 umap1
= isl_union_map_intersect_domain(umap1
, uset
);
5111 space
= isl_space_range(isl_map_get_space(edge
->map
));
5112 uset
= isl_union_set_from_set(isl_set_universe(space
));
5113 umap2
= isl_union_map_copy(cluster_map
);
5114 umap2
= isl_union_map_intersect_domain(umap2
, uset
);
5115 umap
= isl_union_map_product(umap1
, umap2
);
5117 sc
= add_conditional_constraints(edge
, umap
, sc
);
5119 isl_union_map_free(umap
);
5123 /* Given a mapping "cluster_map" from the original instances to
5124 * the cluster instances, add schedule constraints on the clusters
5125 * to "sc" corresponding to all edges in "graph" between nodes that
5126 * belong to SCCs that are marked for merging in "scc_in_merge".
5128 static __isl_give isl_schedule_constraints
*collect_constraints(
5129 struct isl_sched_graph
*graph
, int *scc_in_merge
,
5130 __isl_keep isl_union_map
*cluster_map
,
5131 __isl_take isl_schedule_constraints
*sc
)
5135 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5136 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
5138 if (!scc_in_merge
[edge
->src
->scc
])
5140 if (!scc_in_merge
[edge
->dst
->scc
])
5142 sc
= collect_edge_constraints(edge
, cluster_map
, sc
);
5148 /* Construct a dependence graph for scheduling clusters with respect
5149 * to each other and store the result in "merge_graph".
5150 * In particular, the nodes of the graph correspond to the schedule
5151 * dimensions of the current bands of those clusters that have been
5152 * marked for merging in "c".
5154 * First construct an isl_schedule_constraints object for this domain
5155 * by transforming the edges in "graph" to the domain.
5156 * Then initialize a dependence graph for scheduling from these
5159 static isl_stat
init_merge_graph(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5160 struct isl_clustering
*c
, struct isl_sched_graph
*merge_graph
)
5162 isl_union_set
*domain
;
5163 isl_union_map
*cluster_map
;
5164 isl_schedule_constraints
*sc
;
5167 domain
= collect_domain(ctx
, graph
, c
);
5168 sc
= isl_schedule_constraints_on_domain(domain
);
5170 return isl_stat_error
;
5171 cluster_map
= collect_cluster_map(ctx
, graph
, c
);
5172 sc
= collect_constraints(graph
, c
->scc_in_merge
, cluster_map
, sc
);
5173 isl_union_map_free(cluster_map
);
5175 r
= graph_init(merge_graph
, sc
);
5177 isl_schedule_constraints_free(sc
);
5182 /* Compute the maximal number of remaining schedule rows that still need
5183 * to be computed for the nodes that belong to clusters with the maximal
5184 * dimension for the current band (i.e., the band that is to be merged).
5185 * Only clusters that are about to be merged are considered.
5186 * "maxvar" is the maximal dimension for the current band.
5187 * "c" contains information about the clusters.
5189 * Return the maximal number of remaining schedule rows or -1 on error.
5191 static int compute_maxvar_max_slack(int maxvar
, struct isl_clustering
*c
)
5197 for (i
= 0; i
< c
->n
; ++i
) {
5199 struct isl_sched_graph
*scc
;
5201 if (!c
->scc_in_merge
[i
])
5204 nvar
= scc
->n_total_row
- scc
->band_start
;
5207 for (j
= 0; j
< scc
->n
; ++j
) {
5208 struct isl_sched_node
*node
= &scc
->node
[j
];
5211 if (node_update_cmap(node
) < 0)
5213 slack
= node
->nvar
- node
->rank
;
5214 if (slack
> max_slack
)
5222 /* If there are any clusters where the dimension of the current band
5223 * (i.e., the band that is to be merged) is smaller than "maxvar" and
5224 * if there are any nodes in such a cluster where the number
5225 * of remaining schedule rows that still need to be computed
5226 * is greater than "max_slack", then return the smallest current band
5227 * dimension of all these clusters. Otherwise return the original value
5228 * of "maxvar". Return -1 in case of any error.
5229 * Only clusters that are about to be merged are considered.
5230 * "c" contains information about the clusters.
5232 static int limit_maxvar_to_slack(int maxvar
, int max_slack
,
5233 struct isl_clustering
*c
)
5237 for (i
= 0; i
< c
->n
; ++i
) {
5239 struct isl_sched_graph
*scc
;
5241 if (!c
->scc_in_merge
[i
])
5244 nvar
= scc
->n_total_row
- scc
->band_start
;
5247 for (j
= 0; j
< scc
->n
; ++j
) {
5248 struct isl_sched_node
*node
= &scc
->node
[j
];
5251 if (node_update_cmap(node
) < 0)
5253 slack
= node
->nvar
- node
->rank
;
5254 if (slack
> max_slack
) {
5264 /* Adjust merge_graph->maxvar based on the number of remaining schedule rows
5265 * that still need to be computed. In particular, if there is a node
5266 * in a cluster where the dimension of the current band is smaller
5267 * than merge_graph->maxvar, but the number of remaining schedule rows
5268 * is greater than that of any node in a cluster with the maximal
5269 * dimension for the current band (i.e., merge_graph->maxvar),
5270 * then adjust merge_graph->maxvar to the (smallest) current band dimension
5271 * of those clusters. Without this adjustment, the total number of
5272 * schedule dimensions would be increased, resulting in a skewed view
5273 * of the number of coincident dimensions.
5274 * "c" contains information about the clusters.
5276 * If the maximize_band_depth option is set and merge_graph->maxvar is reduced,
5277 * then there is no point in attempting any merge since it will be rejected
5278 * anyway. Set merge_graph->maxvar to zero in such cases.
5280 static isl_stat
adjust_maxvar_to_slack(isl_ctx
*ctx
,
5281 struct isl_sched_graph
*merge_graph
, struct isl_clustering
*c
)
5283 int max_slack
, maxvar
;
5285 max_slack
= compute_maxvar_max_slack(merge_graph
->maxvar
, c
);
5287 return isl_stat_error
;
5288 maxvar
= limit_maxvar_to_slack(merge_graph
->maxvar
, max_slack
, c
);
5290 return isl_stat_error
;
5292 if (maxvar
< merge_graph
->maxvar
) {
5293 if (isl_options_get_schedule_maximize_band_depth(ctx
))
5294 merge_graph
->maxvar
= 0;
5296 merge_graph
->maxvar
= maxvar
;
5302 /* Return the number of coincident dimensions in the current band of "graph",
5303 * where the nodes of "graph" are assumed to be scheduled by a single band.
5305 static int get_n_coincident(struct isl_sched_graph
*graph
)
5309 for (i
= graph
->band_start
; i
< graph
->n_total_row
; ++i
)
5310 if (!graph
->node
[0].coincident
[i
])
5313 return i
- graph
->band_start
;
5316 /* Should the clusters be merged based on the cluster schedule
5317 * in the current (and only) band of "merge_graph", given that
5318 * coincidence should be maximized?
5320 * If the number of coincident schedule dimensions in the merged band
5321 * would be less than the maximal number of coincident schedule dimensions
5322 * in any of the merged clusters, then the clusters should not be merged.
5324 static isl_bool
ok_to_merge_coincident(struct isl_clustering
*c
,
5325 struct isl_sched_graph
*merge_graph
)
5332 for (i
= 0; i
< c
->n
; ++i
) {
5333 if (!c
->scc_in_merge
[i
])
5335 n_coincident
= get_n_coincident(&c
->scc
[i
]);
5336 if (n_coincident
> max_coincident
)
5337 max_coincident
= n_coincident
;
5340 n_coincident
= get_n_coincident(merge_graph
);
5342 return n_coincident
>= max_coincident
;
5345 /* Return the transformation on "node" expressed by the current (and only)
5346 * band of "merge_graph" applied to the clusters in "c".
5348 * First find the representation of "node" in its SCC in "c" and
5349 * extract the transformation expressed by the current band.
5350 * Then extract the transformation applied by "merge_graph"
5351 * to the cluster to which this SCC belongs.
5352 * Combine the two to obtain the complete transformation on the node.
5354 * Note that the range of the first transformation is an anonymous space,
5355 * while the domain of the second is named "cluster_X". The range
5356 * of the former therefore needs to be adjusted before the two
5359 static __isl_give isl_map
*extract_node_transformation(isl_ctx
*ctx
,
5360 struct isl_sched_node
*node
, struct isl_clustering
*c
,
5361 struct isl_sched_graph
*merge_graph
)
5363 struct isl_sched_node
*scc_node
, *cluster_node
;
5367 isl_multi_aff
*ma
, *ma2
;
5369 scc_node
= graph_find_node(ctx
, &c
->scc
[node
->scc
], node
->space
);
5370 start
= c
->scc
[node
->scc
].band_start
;
5371 n
= c
->scc
[node
->scc
].n_total_row
- start
;
5372 ma
= node_extract_partial_schedule_multi_aff(scc_node
, start
, n
);
5373 space
= cluster_space(&c
->scc
[node
->scc
], c
->scc_cluster
[node
->scc
]);
5374 cluster_node
= graph_find_node(ctx
, merge_graph
, space
);
5375 if (space
&& !cluster_node
)
5376 isl_die(ctx
, isl_error_internal
, "unable to find cluster",
5377 space
= isl_space_free(space
));
5378 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
5379 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
, id
);
5380 isl_space_free(space
);
5381 n
= merge_graph
->n_total_row
;
5382 ma2
= node_extract_partial_schedule_multi_aff(cluster_node
, 0, n
);
5383 ma
= isl_multi_aff_pullback_multi_aff(ma2
, ma
);
5385 return isl_map_from_multi_aff(ma
);
5388 /* Give a set of distances "set", are they bounded by a small constant
5389 * in direction "pos"?
5390 * In practice, check if they are bounded by 2 by checking that there
5391 * are no elements with a value greater than or equal to 3 or
5392 * smaller than or equal to -3.
5394 static isl_bool
distance_is_bounded(__isl_keep isl_set
*set
, int pos
)
5400 return isl_bool_error
;
5402 test
= isl_set_copy(set
);
5403 test
= isl_set_lower_bound_si(test
, isl_dim_set
, pos
, 3);
5404 bounded
= isl_set_is_empty(test
);
5407 if (bounded
< 0 || !bounded
)
5410 test
= isl_set_copy(set
);
5411 test
= isl_set_upper_bound_si(test
, isl_dim_set
, pos
, -3);
5412 bounded
= isl_set_is_empty(test
);
5418 /* Does the set "set" have a fixed (but possible parametric) value
5419 * at dimension "pos"?
5421 static isl_bool
has_single_value(__isl_keep isl_set
*set
, int pos
)
5427 return isl_bool_error
;
5428 set
= isl_set_copy(set
);
5429 n
= isl_set_dim(set
, isl_dim_set
);
5430 set
= isl_set_project_out(set
, isl_dim_set
, pos
+ 1, n
- (pos
+ 1));
5431 set
= isl_set_project_out(set
, isl_dim_set
, 0, pos
);
5432 single
= isl_set_is_singleton(set
);
5438 /* Does "map" have a fixed (but possible parametric) value
5439 * at dimension "pos" of either its domain or its range?
5441 static isl_bool
has_singular_src_or_dst(__isl_keep isl_map
*map
, int pos
)
5446 set
= isl_map_domain(isl_map_copy(map
));
5447 single
= has_single_value(set
, pos
);
5450 if (single
< 0 || single
)
5453 set
= isl_map_range(isl_map_copy(map
));
5454 single
= has_single_value(set
, pos
);
5460 /* Does the edge "edge" from "graph" have bounded dependence distances
5461 * in the merged graph "merge_graph" of a selection of clusters in "c"?
5463 * Extract the complete transformations of the source and destination
5464 * nodes of the edge, apply them to the edge constraints and
5465 * compute the differences. Finally, check if these differences are bounded
5466 * in each direction.
5468 * If the dimension of the band is greater than the number of
5469 * dimensions that can be expected to be optimized by the edge
5470 * (based on its weight), then also allow the differences to be unbounded
5471 * in the remaining dimensions, but only if either the source or
5472 * the destination has a fixed value in that direction.
5473 * This allows a statement that produces values that are used by
5474 * several instances of another statement to be merged with that
5476 * However, merging such clusters will introduce an inherently
5477 * large proximity distance inside the merged cluster, meaning
5478 * that proximity distances will no longer be optimized in
5479 * subsequent merges. These merges are therefore only allowed
5480 * after all other possible merges have been tried.
5481 * The first time such a merge is encountered, the weight of the edge
5482 * is replaced by a negative weight. The second time (i.e., after
5483 * all merges over edges with a non-negative weight have been tried),
5484 * the merge is allowed.
5486 static isl_bool
has_bounded_distances(isl_ctx
*ctx
, struct isl_sched_edge
*edge
,
5487 struct isl_sched_graph
*graph
, struct isl_clustering
*c
,
5488 struct isl_sched_graph
*merge_graph
)
5495 map
= isl_map_copy(edge
->map
);
5496 t
= extract_node_transformation(ctx
, edge
->src
, c
, merge_graph
);
5497 map
= isl_map_apply_domain(map
, t
);
5498 t
= extract_node_transformation(ctx
, edge
->dst
, c
, merge_graph
);
5499 map
= isl_map_apply_range(map
, t
);
5500 dist
= isl_map_deltas(isl_map_copy(map
));
5502 bounded
= isl_bool_true
;
5503 n
= isl_set_dim(dist
, isl_dim_set
);
5504 n_slack
= n
- edge
->weight
;
5505 if (edge
->weight
< 0)
5506 n_slack
-= graph
->max_weight
+ 1;
5507 for (i
= 0; i
< n
; ++i
) {
5508 isl_bool bounded_i
, singular_i
;
5510 bounded_i
= distance_is_bounded(dist
, i
);
5515 if (edge
->weight
>= 0)
5516 bounded
= isl_bool_false
;
5520 singular_i
= has_singular_src_or_dst(map
, i
);
5525 bounded
= isl_bool_false
;
5528 if (!bounded
&& i
>= n
&& edge
->weight
>= 0)
5529 edge
->weight
-= graph
->max_weight
+ 1;
5537 return isl_bool_error
;
5540 /* Should the clusters be merged based on the cluster schedule
5541 * in the current (and only) band of "merge_graph"?
5542 * "graph" is the original dependence graph, while "c" records
5543 * which SCCs are involved in the latest merge.
5545 * In particular, is there at least one proximity constraint
5546 * that is optimized by the merge?
5548 * A proximity constraint is considered to be optimized
5549 * if the dependence distances are small.
5551 static isl_bool
ok_to_merge_proximity(isl_ctx
*ctx
,
5552 struct isl_sched_graph
*graph
, struct isl_clustering
*c
,
5553 struct isl_sched_graph
*merge_graph
)
5557 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5558 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
5561 if (!is_proximity(edge
))
5563 if (!c
->scc_in_merge
[edge
->src
->scc
])
5565 if (!c
->scc_in_merge
[edge
->dst
->scc
])
5567 if (c
->scc_cluster
[edge
->dst
->scc
] ==
5568 c
->scc_cluster
[edge
->src
->scc
])
5570 bounded
= has_bounded_distances(ctx
, edge
, graph
, c
,
5572 if (bounded
< 0 || bounded
)
5576 return isl_bool_false
;
5579 /* Should the clusters be merged based on the cluster schedule
5580 * in the current (and only) band of "merge_graph"?
5581 * "graph" is the original dependence graph, while "c" records
5582 * which SCCs are involved in the latest merge.
5584 * If the current band is empty, then the clusters should not be merged.
5586 * If the band depth should be maximized and the merge schedule
5587 * is incomplete (meaning that the dimension of some of the schedule
5588 * bands in the original schedule will be reduced), then the clusters
5589 * should not be merged.
5591 * If the schedule_maximize_coincidence option is set, then check that
5592 * the number of coincident schedule dimensions is not reduced.
5594 * Finally, only allow the merge if at least one proximity
5595 * constraint is optimized.
5597 static isl_bool
ok_to_merge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5598 struct isl_clustering
*c
, struct isl_sched_graph
*merge_graph
)
5600 if (merge_graph
->n_total_row
== merge_graph
->band_start
)
5601 return isl_bool_false
;
5603 if (isl_options_get_schedule_maximize_band_depth(ctx
) &&
5604 merge_graph
->n_total_row
< merge_graph
->maxvar
)
5605 return isl_bool_false
;
5607 if (isl_options_get_schedule_maximize_coincidence(ctx
)) {
5610 ok
= ok_to_merge_coincident(c
, merge_graph
);
5615 return ok_to_merge_proximity(ctx
, graph
, c
, merge_graph
);
5618 /* Apply the schedule in "t_node" to the "n" rows starting at "first"
5619 * of the schedule in "node" and return the result.
5621 * That is, essentially compute
5623 * T * N(first:first+n-1)
5625 * taking into account the constant term and the parameter coefficients
5628 static __isl_give isl_mat
*node_transformation(isl_ctx
*ctx
,
5629 struct isl_sched_node
*t_node
, struct isl_sched_node
*node
,
5634 int n_row
, n_col
, n_param
, n_var
;
5636 n_param
= node
->nparam
;
5638 n_row
= isl_mat_rows(t_node
->sched
);
5639 n_col
= isl_mat_cols(node
->sched
);
5640 t
= isl_mat_alloc(ctx
, n_row
, n_col
);
5643 for (i
= 0; i
< n_row
; ++i
) {
5644 isl_seq_cpy(t
->row
[i
], t_node
->sched
->row
[i
], 1 + n_param
);
5645 isl_seq_clr(t
->row
[i
] + 1 + n_param
, n_var
);
5646 for (j
= 0; j
< n
; ++j
)
5647 isl_seq_addmul(t
->row
[i
],
5648 t_node
->sched
->row
[i
][1 + n_param
+ j
],
5649 node
->sched
->row
[first
+ j
],
5650 1 + n_param
+ n_var
);
5655 /* Apply the cluster schedule in "t_node" to the current band
5656 * schedule of the nodes in "graph".
5658 * In particular, replace the rows starting at band_start
5659 * by the result of applying the cluster schedule in "t_node"
5660 * to the original rows.
5662 * The coincidence of the schedule is determined by the coincidence
5663 * of the cluster schedule.
5665 static isl_stat
transform(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5666 struct isl_sched_node
*t_node
)
5672 start
= graph
->band_start
;
5673 n
= graph
->n_total_row
- start
;
5675 n_new
= isl_mat_rows(t_node
->sched
);
5676 for (i
= 0; i
< graph
->n
; ++i
) {
5677 struct isl_sched_node
*node
= &graph
->node
[i
];
5680 t
= node_transformation(ctx
, t_node
, node
, start
, n
);
5681 node
->sched
= isl_mat_drop_rows(node
->sched
, start
, n
);
5682 node
->sched
= isl_mat_concat(node
->sched
, t
);
5683 node
->sched_map
= isl_map_free(node
->sched_map
);
5685 return isl_stat_error
;
5686 for (j
= 0; j
< n_new
; ++j
)
5687 node
->coincident
[start
+ j
] = t_node
->coincident
[j
];
5689 graph
->n_total_row
-= n
;
5691 graph
->n_total_row
+= n_new
;
5692 graph
->n_row
+= n_new
;
5697 /* Merge the clusters marked for merging in "c" into a single
5698 * cluster using the cluster schedule in the current band of "merge_graph".
5699 * The representative SCC for the new cluster is the SCC with
5700 * the smallest index.
5702 * The current band schedule of each SCC in the new cluster is obtained
5703 * by applying the schedule of the corresponding original cluster
5704 * to the original band schedule.
5705 * All SCCs in the new cluster have the same number of schedule rows.
5707 static isl_stat
merge(isl_ctx
*ctx
, struct isl_clustering
*c
,
5708 struct isl_sched_graph
*merge_graph
)
5714 for (i
= 0; i
< c
->n
; ++i
) {
5715 struct isl_sched_node
*node
;
5717 if (!c
->scc_in_merge
[i
])
5721 space
= cluster_space(&c
->scc
[i
], c
->scc_cluster
[i
]);
5723 return isl_stat_error
;
5724 node
= graph_find_node(ctx
, merge_graph
, space
);
5725 isl_space_free(space
);
5727 isl_die(ctx
, isl_error_internal
,
5728 "unable to find cluster",
5729 return isl_stat_error
);
5730 if (transform(ctx
, &c
->scc
[i
], node
) < 0)
5731 return isl_stat_error
;
5732 c
->scc_cluster
[i
] = cluster
;
5738 /* Try and merge the clusters of SCCs marked in c->scc_in_merge
5739 * by scheduling the current cluster bands with respect to each other.
5741 * Construct a dependence graph with a space for each cluster and
5742 * with the coordinates of each space corresponding to the schedule
5743 * dimensions of the current band of that cluster.
5744 * Construct a cluster schedule in this cluster dependence graph and
5745 * apply it to the current cluster bands if it is applicable
5746 * according to ok_to_merge.
5748 * If the number of remaining schedule dimensions in a cluster
5749 * with a non-maximal current schedule dimension is greater than
5750 * the number of remaining schedule dimensions in clusters
5751 * with a maximal current schedule dimension, then restrict
5752 * the number of rows to be computed in the cluster schedule
5753 * to the minimal such non-maximal current schedule dimension.
5754 * Do this by adjusting merge_graph.maxvar.
5756 * Return isl_bool_true if the clusters have effectively been merged
5757 * into a single cluster.
5759 * Note that since the standard scheduling algorithm minimizes the maximal
5760 * distance over proximity constraints, the proximity constraints between
5761 * the merged clusters may not be optimized any further than what is
5762 * sufficient to bring the distances within the limits of the internal
5763 * proximity constraints inside the individual clusters.
5764 * It may therefore make sense to perform an additional translation step
5765 * to bring the clusters closer to each other, while maintaining
5766 * the linear part of the merging schedule found using the standard
5767 * scheduling algorithm.
5769 static isl_bool
try_merge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5770 struct isl_clustering
*c
)
5772 struct isl_sched_graph merge_graph
= { 0 };
5775 if (init_merge_graph(ctx
, graph
, c
, &merge_graph
) < 0)
5778 if (compute_maxvar(&merge_graph
) < 0)
5780 if (adjust_maxvar_to_slack(ctx
, &merge_graph
,c
) < 0)
5782 if (compute_schedule_wcc_band(ctx
, &merge_graph
) < 0)
5784 merged
= ok_to_merge(ctx
, graph
, c
, &merge_graph
);
5785 if (merged
&& merge(ctx
, c
, &merge_graph
) < 0)
5788 graph_free(ctx
, &merge_graph
);
5791 graph_free(ctx
, &merge_graph
);
5792 return isl_bool_error
;
5795 /* Is there any edge marked "no_merge" between two SCCs that are
5796 * about to be merged (i.e., that are set in "scc_in_merge")?
5797 * "merge_edge" is the proximity edge along which the clusters of SCCs
5798 * are going to be merged.
5800 * If there is any edge between two SCCs with a negative weight,
5801 * while the weight of "merge_edge" is non-negative, then this
5802 * means that the edge was postponed. "merge_edge" should then
5803 * also be postponed since merging along the edge with negative weight should
5804 * be postponed until all edges with non-negative weight have been tried.
5805 * Replace the weight of "merge_edge" by a negative weight as well and
5806 * tell the caller not to attempt a merge.
5808 static int any_no_merge(struct isl_sched_graph
*graph
, int *scc_in_merge
,
5809 struct isl_sched_edge
*merge_edge
)
5813 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5814 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
5816 if (!scc_in_merge
[edge
->src
->scc
])
5818 if (!scc_in_merge
[edge
->dst
->scc
])
5822 if (merge_edge
->weight
>= 0 && edge
->weight
< 0) {
5823 merge_edge
->weight
-= graph
->max_weight
+ 1;
5831 /* Merge the two clusters in "c" connected by the edge in "graph"
5832 * with index "edge" into a single cluster.
5833 * If it turns out to be impossible to merge these two clusters,
5834 * then mark the edge as "no_merge" such that it will not be
5837 * First mark all SCCs that need to be merged. This includes the SCCs
5838 * in the two clusters, but it may also include the SCCs
5839 * of intermediate clusters.
5840 * If there is already a no_merge edge between any pair of such SCCs,
5841 * then simply mark the current edge as no_merge as well.
5842 * Likewise, if any of those edges was postponed by has_bounded_distances,
5843 * then postpone the current edge as well.
5844 * Otherwise, try and merge the clusters and mark "edge" as "no_merge"
5845 * if the clusters did not end up getting merged, unless the non-merge
5846 * is due to the fact that the edge was postponed. This postponement
5847 * can be recognized by a change in weight (from non-negative to negative).
5849 static isl_stat
merge_clusters_along_edge(isl_ctx
*ctx
,
5850 struct isl_sched_graph
*graph
, int edge
, struct isl_clustering
*c
)
5853 int edge_weight
= graph
->edge
[edge
].weight
;
5855 if (mark_merge_sccs(ctx
, graph
, edge
, c
) < 0)
5856 return isl_stat_error
;
5858 if (any_no_merge(graph
, c
->scc_in_merge
, &graph
->edge
[edge
]))
5859 merged
= isl_bool_false
;
5861 merged
= try_merge(ctx
, graph
, c
);
5863 return isl_stat_error
;
5864 if (!merged
&& edge_weight
== graph
->edge
[edge
].weight
)
5865 graph
->edge
[edge
].no_merge
= 1;
5870 /* Does "node" belong to the cluster identified by "cluster"?
5872 static int node_cluster_exactly(struct isl_sched_node
*node
, int cluster
)
5874 return node
->cluster
== cluster
;
5877 /* Does "edge" connect two nodes belonging to the cluster
5878 * identified by "cluster"?
5880 static int edge_cluster_exactly(struct isl_sched_edge
*edge
, int cluster
)
5882 return edge
->src
->cluster
== cluster
&& edge
->dst
->cluster
== cluster
;
5885 /* Swap the schedule of "node1" and "node2".
5886 * Both nodes have been derived from the same node in a common parent graph.
5887 * Since the "coincident" field is shared with that node
5888 * in the parent graph, there is no need to also swap this field.
5890 static void swap_sched(struct isl_sched_node
*node1
,
5891 struct isl_sched_node
*node2
)
5896 sched
= node1
->sched
;
5897 node1
->sched
= node2
->sched
;
5898 node2
->sched
= sched
;
5900 sched_map
= node1
->sched_map
;
5901 node1
->sched_map
= node2
->sched_map
;
5902 node2
->sched_map
= sched_map
;
5905 /* Copy the current band schedule from the SCCs that form the cluster
5906 * with index "pos" to the actual cluster at position "pos".
5907 * By construction, the index of the first SCC that belongs to the cluster
5910 * The order of the nodes inside both the SCCs and the cluster
5911 * is assumed to be same as the order in the original "graph".
5913 * Since the SCC graphs will no longer be used after this function,
5914 * the schedules are actually swapped rather than copied.
5916 static isl_stat
copy_partial(struct isl_sched_graph
*graph
,
5917 struct isl_clustering
*c
, int pos
)
5921 c
->cluster
[pos
].n_total_row
= c
->scc
[pos
].n_total_row
;
5922 c
->cluster
[pos
].n_row
= c
->scc
[pos
].n_row
;
5923 c
->cluster
[pos
].maxvar
= c
->scc
[pos
].maxvar
;
5925 for (i
= 0; i
< graph
->n
; ++i
) {
5929 if (graph
->node
[i
].cluster
!= pos
)
5931 s
= graph
->node
[i
].scc
;
5932 k
= c
->scc_node
[s
]++;
5933 swap_sched(&c
->cluster
[pos
].node
[j
], &c
->scc
[s
].node
[k
]);
5934 if (c
->scc
[s
].maxvar
> c
->cluster
[pos
].maxvar
)
5935 c
->cluster
[pos
].maxvar
= c
->scc
[s
].maxvar
;
5942 /* Is there a (conditional) validity dependence from node[j] to node[i],
5943 * forcing node[i] to follow node[j] or do the nodes belong to the same
5946 static isl_bool
node_follows_strong_or_same_cluster(int i
, int j
, void *user
)
5948 struct isl_sched_graph
*graph
= user
;
5950 if (graph
->node
[i
].cluster
== graph
->node
[j
].cluster
)
5951 return isl_bool_true
;
5952 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
5955 /* Extract the merged clusters of SCCs in "graph", sort them, and
5956 * store them in c->clusters. Update c->scc_cluster accordingly.
5958 * First keep track of the cluster containing the SCC to which a node
5959 * belongs in the node itself.
5960 * Then extract the clusters into c->clusters, copying the current
5961 * band schedule from the SCCs that belong to the cluster.
5962 * Do this only once per cluster.
5964 * Finally, topologically sort the clusters and update c->scc_cluster
5965 * to match the new scc numbering. While the SCCs were originally
5966 * sorted already, some SCCs that depend on some other SCCs may
5967 * have been merged with SCCs that appear before these other SCCs.
5968 * A reordering may therefore be required.
5970 static isl_stat
extract_clusters(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5971 struct isl_clustering
*c
)
5975 for (i
= 0; i
< graph
->n
; ++i
)
5976 graph
->node
[i
].cluster
= c
->scc_cluster
[graph
->node
[i
].scc
];
5978 for (i
= 0; i
< graph
->scc
; ++i
) {
5979 if (c
->scc_cluster
[i
] != i
)
5981 if (extract_sub_graph(ctx
, graph
, &node_cluster_exactly
,
5982 &edge_cluster_exactly
, i
, &c
->cluster
[i
]) < 0)
5983 return isl_stat_error
;
5984 c
->cluster
[i
].src_scc
= -1;
5985 c
->cluster
[i
].dst_scc
= -1;
5986 if (copy_partial(graph
, c
, i
) < 0)
5987 return isl_stat_error
;
5990 if (detect_ccs(ctx
, graph
, &node_follows_strong_or_same_cluster
) < 0)
5991 return isl_stat_error
;
5992 for (i
= 0; i
< graph
->n
; ++i
)
5993 c
->scc_cluster
[graph
->node
[i
].scc
] = graph
->node
[i
].cluster
;
5998 /* Compute weights on the proximity edges of "graph" that can
5999 * be used by find_proximity to find the most appropriate
6000 * proximity edge to use to merge two clusters in "c".
6001 * The weights are also used by has_bounded_distances to determine
6002 * whether the merge should be allowed.
6003 * Store the maximum of the computed weights in graph->max_weight.
6005 * The computed weight is a measure for the number of remaining schedule
6006 * dimensions that can still be completely aligned.
6007 * In particular, compute the number of equalities between
6008 * input dimensions and output dimensions in the proximity constraints.
6009 * The directions that are already handled by outer schedule bands
6010 * are projected out prior to determining this number.
6012 * Edges that will never be considered by find_proximity are ignored.
6014 static isl_stat
compute_weights(struct isl_sched_graph
*graph
,
6015 struct isl_clustering
*c
)
6019 graph
->max_weight
= 0;
6021 for (i
= 0; i
< graph
->n_edge
; ++i
) {
6022 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
6023 struct isl_sched_node
*src
= edge
->src
;
6024 struct isl_sched_node
*dst
= edge
->dst
;
6025 isl_basic_map
*hull
;
6028 if (!is_proximity(edge
))
6030 if (bad_cluster(&c
->scc
[edge
->src
->scc
]) ||
6031 bad_cluster(&c
->scc
[edge
->dst
->scc
]))
6033 if (c
->scc_cluster
[edge
->dst
->scc
] ==
6034 c
->scc_cluster
[edge
->src
->scc
])
6037 hull
= isl_map_affine_hull(isl_map_copy(edge
->map
));
6038 hull
= isl_basic_map_transform_dims(hull
, isl_dim_in
, 0,
6039 isl_mat_copy(src
->ctrans
));
6040 hull
= isl_basic_map_transform_dims(hull
, isl_dim_out
, 0,
6041 isl_mat_copy(dst
->ctrans
));
6042 hull
= isl_basic_map_project_out(hull
,
6043 isl_dim_in
, 0, src
->rank
);
6044 hull
= isl_basic_map_project_out(hull
,
6045 isl_dim_out
, 0, dst
->rank
);
6046 hull
= isl_basic_map_remove_divs(hull
);
6047 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
6048 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
6049 hull
= isl_basic_map_drop_constraints_not_involving_dims(hull
,
6050 isl_dim_in
, 0, n_in
);
6051 hull
= isl_basic_map_drop_constraints_not_involving_dims(hull
,
6052 isl_dim_out
, 0, n_out
);
6054 return isl_stat_error
;
6055 edge
->weight
= hull
->n_eq
;
6056 isl_basic_map_free(hull
);
6058 if (edge
->weight
> graph
->max_weight
)
6059 graph
->max_weight
= edge
->weight
;
6065 /* Call compute_schedule_finish_band on each of the clusters in "c"
6066 * in their topological order. This order is determined by the scc
6067 * fields of the nodes in "graph".
6068 * Combine the results in a sequence expressing the topological order.
6070 * If there is only one cluster left, then there is no need to introduce
6071 * a sequence node. Also, in this case, the cluster necessarily contains
6072 * the SCC at position 0 in the original graph and is therefore also
6073 * stored in the first cluster of "c".
6075 static __isl_give isl_schedule_node
*finish_bands_clustering(
6076 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
6077 struct isl_clustering
*c
)
6081 isl_union_set_list
*filters
;
6083 if (graph
->scc
== 1)
6084 return compute_schedule_finish_band(node
, &c
->cluster
[0], 0);
6086 ctx
= isl_schedule_node_get_ctx(node
);
6088 filters
= extract_sccs(ctx
, graph
);
6089 node
= isl_schedule_node_insert_sequence(node
, filters
);
6091 for (i
= 0; i
< graph
->scc
; ++i
) {
6092 int j
= c
->scc_cluster
[i
];
6093 node
= isl_schedule_node_child(node
, i
);
6094 node
= isl_schedule_node_child(node
, 0);
6095 node
= compute_schedule_finish_band(node
, &c
->cluster
[j
], 0);
6096 node
= isl_schedule_node_parent(node
);
6097 node
= isl_schedule_node_parent(node
);
6103 /* Compute a schedule for a connected dependence graph by first considering
6104 * each strongly connected component (SCC) in the graph separately and then
6105 * incrementally combining them into clusters.
6106 * Return the updated schedule node.
6108 * Initially, each cluster consists of a single SCC, each with its
6109 * own band schedule. The algorithm then tries to merge pairs
6110 * of clusters along a proximity edge until no more suitable
6111 * proximity edges can be found. During this merging, the schedule
6112 * is maintained in the individual SCCs.
6113 * After the merging is completed, the full resulting clusters
6114 * are extracted and in finish_bands_clustering,
6115 * compute_schedule_finish_band is called on each of them to integrate
6116 * the band into "node" and to continue the computation.
6118 * compute_weights initializes the weights that are used by find_proximity.
6120 static __isl_give isl_schedule_node
*compute_schedule_wcc_clustering(
6121 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
6124 struct isl_clustering c
;
6127 ctx
= isl_schedule_node_get_ctx(node
);
6129 if (clustering_init(ctx
, &c
, graph
) < 0)
6132 if (compute_weights(graph
, &c
) < 0)
6136 i
= find_proximity(graph
, &c
);
6139 if (i
>= graph
->n_edge
)
6141 if (merge_clusters_along_edge(ctx
, graph
, i
, &c
) < 0)
6145 if (extract_clusters(ctx
, graph
, &c
) < 0)
6148 node
= finish_bands_clustering(node
, graph
, &c
);
6150 clustering_free(ctx
, &c
);
6153 clustering_free(ctx
, &c
);
6154 return isl_schedule_node_free(node
);
6157 /* Compute a schedule for a connected dependence graph and return
6158 * the updated schedule node.
6160 * If Feautrier's algorithm is selected, we first recursively try to satisfy
6161 * as many validity dependences as possible. When all validity dependences
6162 * are satisfied we extend the schedule to a full-dimensional schedule.
6164 * Call compute_schedule_wcc_whole or compute_schedule_wcc_clustering
6165 * depending on whether the user has selected the option to try and
6166 * compute a schedule for the entire (weakly connected) component first.
6167 * If there is only a single strongly connected component (SCC), then
6168 * there is no point in trying to combine SCCs
6169 * in compute_schedule_wcc_clustering, so compute_schedule_wcc_whole
6170 * is called instead.
6172 static __isl_give isl_schedule_node
*compute_schedule_wcc(
6173 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
6180 ctx
= isl_schedule_node_get_ctx(node
);
6181 if (detect_sccs(ctx
, graph
) < 0)
6182 return isl_schedule_node_free(node
);
6184 if (compute_maxvar(graph
) < 0)
6185 return isl_schedule_node_free(node
);
6187 if (need_feautrier_step(ctx
, graph
))
6188 return compute_schedule_wcc_feautrier(node
, graph
);
6190 if (graph
->scc
<= 1 || isl_options_get_schedule_whole_component(ctx
))
6191 return compute_schedule_wcc_whole(node
, graph
);
6193 return compute_schedule_wcc_clustering(node
, graph
);
6196 /* Compute a schedule for each group of nodes identified by node->scc
6197 * separately and then combine them in a sequence node (or as set node
6198 * if graph->weak is set) inserted at position "node" of the schedule tree.
6199 * Return the updated schedule node.
6201 * If "wcc" is set then each of the groups belongs to a single
6202 * weakly connected component in the dependence graph so that
6203 * there is no need for compute_sub_schedule to look for weakly
6204 * connected components.
6206 static __isl_give isl_schedule_node
*compute_component_schedule(
6207 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
6212 isl_union_set_list
*filters
;
6216 ctx
= isl_schedule_node_get_ctx(node
);
6218 filters
= extract_sccs(ctx
, graph
);
6220 node
= isl_schedule_node_insert_set(node
, filters
);
6222 node
= isl_schedule_node_insert_sequence(node
, filters
);
6224 for (component
= 0; component
< graph
->scc
; ++component
) {
6225 node
= isl_schedule_node_child(node
, component
);
6226 node
= isl_schedule_node_child(node
, 0);
6227 node
= compute_sub_schedule(node
, ctx
, graph
,
6229 &edge_scc_exactly
, component
, wcc
);
6230 node
= isl_schedule_node_parent(node
);
6231 node
= isl_schedule_node_parent(node
);
6237 /* Compute a schedule for the given dependence graph and insert it at "node".
6238 * Return the updated schedule node.
6240 * We first check if the graph is connected (through validity and conditional
6241 * validity dependences) and, if not, compute a schedule
6242 * for each component separately.
6243 * If the schedule_serialize_sccs option is set, then we check for strongly
6244 * connected components instead and compute a separate schedule for
6245 * each such strongly connected component.
6247 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
6248 struct isl_sched_graph
*graph
)
6255 ctx
= isl_schedule_node_get_ctx(node
);
6256 if (isl_options_get_schedule_serialize_sccs(ctx
)) {
6257 if (detect_sccs(ctx
, graph
) < 0)
6258 return isl_schedule_node_free(node
);
6260 if (detect_wccs(ctx
, graph
) < 0)
6261 return isl_schedule_node_free(node
);
6265 return compute_component_schedule(node
, graph
, 1);
6267 return compute_schedule_wcc(node
, graph
);
6270 /* Compute a schedule on sc->domain that respects the given schedule
6273 * In particular, the schedule respects all the validity dependences.
6274 * If the default isl scheduling algorithm is used, it tries to minimize
6275 * the dependence distances over the proximity dependences.
6276 * If Feautrier's scheduling algorithm is used, the proximity dependence
6277 * distances are only minimized during the extension to a full-dimensional
6280 * If there are any condition and conditional validity dependences,
6281 * then the conditional validity dependences may be violated inside
6282 * a tilable band, provided they have no adjacent non-local
6283 * condition dependences.
6285 __isl_give isl_schedule
*isl_schedule_constraints_compute_schedule(
6286 __isl_take isl_schedule_constraints
*sc
)
6288 isl_ctx
*ctx
= isl_schedule_constraints_get_ctx(sc
);
6289 struct isl_sched_graph graph
= { 0 };
6290 isl_schedule
*sched
;
6291 isl_schedule_node
*node
;
6292 isl_union_set
*domain
;
6294 sc
= isl_schedule_constraints_align_params(sc
);
6296 domain
= isl_schedule_constraints_get_domain(sc
);
6297 if (isl_union_set_n_set(domain
) == 0) {
6298 isl_schedule_constraints_free(sc
);
6299 return isl_schedule_from_domain(domain
);
6302 if (graph_init(&graph
, sc
) < 0)
6303 domain
= isl_union_set_free(domain
);
6305 node
= isl_schedule_node_from_domain(domain
);
6306 node
= isl_schedule_node_child(node
, 0);
6308 node
= compute_schedule(node
, &graph
);
6309 sched
= isl_schedule_node_get_schedule(node
);
6310 isl_schedule_node_free(node
);
6312 graph_free(ctx
, &graph
);
6313 isl_schedule_constraints_free(sc
);
6318 /* Compute a schedule for the given union of domains that respects
6319 * all the validity dependences and minimizes
6320 * the dependence distances over the proximity dependences.
6322 * This function is kept for backward compatibility.
6324 __isl_give isl_schedule
*isl_union_set_compute_schedule(
6325 __isl_take isl_union_set
*domain
,
6326 __isl_take isl_union_map
*validity
,
6327 __isl_take isl_union_map
*proximity
)
6329 isl_schedule_constraints
*sc
;
6331 sc
= isl_schedule_constraints_on_domain(domain
);
6332 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
6333 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
6335 return isl_schedule_constraints_compute_schedule(sc
);