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 original space in which the domain lives;
51 * that is, the space is not affected by compression
52 * sched is a matrix representation of the schedule being constructed
53 * for this node; if compressed is set, then this schedule is
54 * defined over the compressed domain space
55 * sched_map is an isl_map representation of the same (partial) schedule
56 * sched_map may be NULL; if compressed is set, then this map
57 * is defined over the uncompressed domain space
58 * rank is the number of linearly independent rows in the linear part
60 * the columns of cmap represent a change of basis for the schedule
61 * coefficients; the first rank columns span the linear part of
63 * cinv is the inverse of cmap.
64 * ctrans is the transpose of cmap.
65 * start is the first variable in the LP problem in the sequences that
66 * represents the schedule coefficients of this node
67 * nvar is the dimension of the domain
68 * nparam is the number of parameters or 0 if we are not constructing
69 * a parametric schedule
71 * If compressed is set, then hull represents the constraints
72 * that were used to derive the compression, while compress and
73 * decompress map the original space to the compressed space and
76 * scc is the index of SCC (or WCC) this node belongs to
78 * "cluster" is only used inside extract_clusters and identifies
79 * the cluster of SCCs that the node belongs to.
81 * coincident contains a boolean for each of the rows of the schedule,
82 * indicating whether the corresponding scheduling dimension satisfies
83 * the coincidence constraints in the sense that the corresponding
84 * dependence distances are zero.
86 * If the schedule_treat_coalescing option is set, then
87 * "sizes" contains the sizes of the (compressed) instance set
88 * in each direction. If there is no fixed size in a given direction,
89 * then the corresponding size value is set to infinity.
90 * If the schedule_treat_coalescing option or the schedule_max_coefficient
91 * option is set, then "max" contains the maximal values for
92 * schedule coefficients of the (compressed) variables. If no bound
93 * needs to be imposed on a particular variable, then the corresponding
96 struct isl_sched_node
{
100 isl_multi_aff
*compress
;
101 isl_multi_aff
*decompress
;
117 isl_multi_val
*sizes
;
121 static int node_has_space(const void *entry
, const void *val
)
123 struct isl_sched_node
*node
= (struct isl_sched_node
*)entry
;
124 isl_space
*dim
= (isl_space
*)val
;
126 return isl_space_is_equal(node
->space
, dim
);
129 static int node_scc_exactly(struct isl_sched_node
*node
, int scc
)
131 return node
->scc
== scc
;
134 static int node_scc_at_most(struct isl_sched_node
*node
, int scc
)
136 return node
->scc
<= scc
;
139 static int node_scc_at_least(struct isl_sched_node
*node
, int scc
)
141 return node
->scc
>= scc
;
144 /* An edge in the dependence graph. An edge may be used to
145 * ensure validity of the generated schedule, to minimize the dependence
148 * map is the dependence relation, with i -> j in the map if j depends on i
149 * tagged_condition and tagged_validity contain the union of all tagged
150 * condition or conditional validity dependence relations that
151 * specialize the dependence relation "map"; that is,
152 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
153 * or "tagged_validity", then i -> j is an element of "map".
154 * If these fields are NULL, then they represent the empty relation.
155 * src is the source node
156 * dst is the sink node
158 * types is a bit vector containing the types of this edge.
159 * validity is set if the edge is used to ensure correctness
160 * coincidence is used to enforce zero dependence distances
161 * proximity is set if the edge is used to minimize dependence distances
162 * condition is set if the edge represents a condition
163 * for a conditional validity schedule constraint
164 * local can only be set for condition edges and indicates that
165 * the dependence distance over the edge should be zero
166 * conditional_validity is set if the edge is used to conditionally
169 * For validity edges, start and end mark the sequence of inequality
170 * constraints in the LP problem that encode the validity constraint
171 * corresponding to this edge.
173 * During clustering, an edge may be marked "no_merge" if it should
174 * not be used to merge clusters.
175 * The weight is also only used during clustering and it is
176 * an indication of how many schedule dimensions on either side
177 * of the schedule constraints can be aligned.
178 * If the weight is negative, then this means that this edge was postponed
179 * by has_bounded_distances or any_no_merge. The original weight can
180 * be retrieved by adding 1 + graph->max_weight, with "graph"
181 * the graph containing this edge.
183 struct isl_sched_edge
{
185 isl_union_map
*tagged_condition
;
186 isl_union_map
*tagged_validity
;
188 struct isl_sched_node
*src
;
189 struct isl_sched_node
*dst
;
200 /* Is "edge" marked as being of type "type"?
202 static int is_type(struct isl_sched_edge
*edge
, enum isl_edge_type type
)
204 return ISL_FL_ISSET(edge
->types
, 1 << type
);
207 /* Mark "edge" as being of type "type".
209 static void set_type(struct isl_sched_edge
*edge
, enum isl_edge_type type
)
211 ISL_FL_SET(edge
->types
, 1 << type
);
214 /* No longer mark "edge" as being of type "type"?
216 static void clear_type(struct isl_sched_edge
*edge
, enum isl_edge_type type
)
218 ISL_FL_CLR(edge
->types
, 1 << type
);
221 /* Is "edge" marked as a validity edge?
223 static int is_validity(struct isl_sched_edge
*edge
)
225 return is_type(edge
, isl_edge_validity
);
228 /* Mark "edge" as a validity edge.
230 static void set_validity(struct isl_sched_edge
*edge
)
232 set_type(edge
, isl_edge_validity
);
235 /* Is "edge" marked as a proximity edge?
237 static int is_proximity(struct isl_sched_edge
*edge
)
239 return is_type(edge
, isl_edge_proximity
);
242 /* Is "edge" marked as a local edge?
244 static int is_local(struct isl_sched_edge
*edge
)
246 return is_type(edge
, isl_edge_local
);
249 /* Mark "edge" as a local edge.
251 static void set_local(struct isl_sched_edge
*edge
)
253 set_type(edge
, isl_edge_local
);
256 /* No longer mark "edge" as a local edge.
258 static void clear_local(struct isl_sched_edge
*edge
)
260 clear_type(edge
, isl_edge_local
);
263 /* Is "edge" marked as a coincidence edge?
265 static int is_coincidence(struct isl_sched_edge
*edge
)
267 return is_type(edge
, isl_edge_coincidence
);
270 /* Is "edge" marked as a condition edge?
272 static int is_condition(struct isl_sched_edge
*edge
)
274 return is_type(edge
, isl_edge_condition
);
277 /* Is "edge" marked as a conditional validity edge?
279 static int is_conditional_validity(struct isl_sched_edge
*edge
)
281 return is_type(edge
, isl_edge_conditional_validity
);
284 /* Internal information about the dependence graph used during
285 * the construction of the schedule.
287 * intra_hmap is a cache, mapping dependence relations to their dual,
288 * for dependences from a node to itself
289 * inter_hmap is a cache, mapping dependence relations to their dual,
290 * for dependences between distinct nodes
291 * if compression is involved then the key for these maps
292 * is the original, uncompressed dependence relation, while
293 * the value is the dual of the compressed dependence relation.
295 * n is the number of nodes
296 * node is the list of nodes
297 * maxvar is the maximal number of variables over all nodes
298 * max_row is the allocated number of rows in the schedule
299 * n_row is the current (maximal) number of linearly independent
300 * rows in the node schedules
301 * n_total_row is the current number of rows in the node schedules
302 * band_start is the starting row in the node schedules of the current band
303 * root is set if this graph is the original dependence graph,
304 * without any splitting
306 * sorted contains a list of node indices sorted according to the
307 * SCC to which a node belongs
309 * n_edge is the number of edges
310 * edge is the list of edges
311 * max_edge contains the maximal number of edges of each type;
312 * in particular, it contains the number of edges in the inital graph.
313 * edge_table contains pointers into the edge array, hashed on the source
314 * and sink spaces; there is one such table for each type;
315 * a given edge may be referenced from more than one table
316 * if the corresponding relation appears in more than one of the
317 * sets of dependences; however, for each type there is only
318 * a single edge between a given pair of source and sink space
319 * in the entire graph
321 * node_table contains pointers into the node array, hashed on the space
323 * region contains a list of variable sequences that should be non-trivial
325 * lp contains the (I)LP problem used to obtain new schedule rows
327 * src_scc and dst_scc are the source and sink SCCs of an edge with
328 * conflicting constraints
330 * scc represents the number of components
331 * weak is set if the components are weakly connected
333 * max_weight is used during clustering and represents the maximal
334 * weight of the relevant proximity edges.
336 struct isl_sched_graph
{
337 isl_map_to_basic_set
*intra_hmap
;
338 isl_map_to_basic_set
*inter_hmap
;
340 struct isl_sched_node
*node
;
353 struct isl_sched_edge
*edge
;
355 int max_edge
[isl_edge_last
+ 1];
356 struct isl_hash_table
*edge_table
[isl_edge_last
+ 1];
358 struct isl_hash_table
*node_table
;
359 struct isl_region
*region
;
372 /* Initialize node_table based on the list of nodes.
374 static int graph_init_table(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
378 graph
->node_table
= isl_hash_table_alloc(ctx
, graph
->n
);
379 if (!graph
->node_table
)
382 for (i
= 0; i
< graph
->n
; ++i
) {
383 struct isl_hash_table_entry
*entry
;
386 hash
= isl_space_get_hash(graph
->node
[i
].space
);
387 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
389 graph
->node
[i
].space
, 1);
392 entry
->data
= &graph
->node
[i
];
398 /* Return a pointer to the node that lives within the given space,
399 * or NULL if there is no such node.
401 static struct isl_sched_node
*graph_find_node(isl_ctx
*ctx
,
402 struct isl_sched_graph
*graph
, __isl_keep isl_space
*dim
)
404 struct isl_hash_table_entry
*entry
;
407 hash
= isl_space_get_hash(dim
);
408 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
409 &node_has_space
, dim
, 0);
411 return entry
? entry
->data
: NULL
;
414 static int edge_has_src_and_dst(const void *entry
, const void *val
)
416 const struct isl_sched_edge
*edge
= entry
;
417 const struct isl_sched_edge
*temp
= val
;
419 return edge
->src
== temp
->src
&& edge
->dst
== temp
->dst
;
422 /* Add the given edge to graph->edge_table[type].
424 static isl_stat
graph_edge_table_add(isl_ctx
*ctx
,
425 struct isl_sched_graph
*graph
, enum isl_edge_type type
,
426 struct isl_sched_edge
*edge
)
428 struct isl_hash_table_entry
*entry
;
431 hash
= isl_hash_init();
432 hash
= isl_hash_builtin(hash
, edge
->src
);
433 hash
= isl_hash_builtin(hash
, edge
->dst
);
434 entry
= isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
435 &edge_has_src_and_dst
, edge
, 1);
437 return isl_stat_error
;
443 /* Allocate the edge_tables based on the maximal number of edges of
446 static int graph_init_edge_tables(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
450 for (i
= 0; i
<= isl_edge_last
; ++i
) {
451 graph
->edge_table
[i
] = isl_hash_table_alloc(ctx
,
453 if (!graph
->edge_table
[i
])
460 /* If graph->edge_table[type] contains an edge from the given source
461 * to the given destination, then return the hash table entry of this edge.
462 * Otherwise, return NULL.
464 static struct isl_hash_table_entry
*graph_find_edge_entry(
465 struct isl_sched_graph
*graph
,
466 enum isl_edge_type type
,
467 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
469 isl_ctx
*ctx
= isl_space_get_ctx(src
->space
);
471 struct isl_sched_edge temp
= { .src
= src
, .dst
= dst
};
473 hash
= isl_hash_init();
474 hash
= isl_hash_builtin(hash
, temp
.src
);
475 hash
= isl_hash_builtin(hash
, temp
.dst
);
476 return isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
477 &edge_has_src_and_dst
, &temp
, 0);
481 /* If graph->edge_table[type] contains an edge from the given source
482 * to the given destination, then return this edge.
483 * Otherwise, return NULL.
485 static struct isl_sched_edge
*graph_find_edge(struct isl_sched_graph
*graph
,
486 enum isl_edge_type type
,
487 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
489 struct isl_hash_table_entry
*entry
;
491 entry
= graph_find_edge_entry(graph
, type
, src
, dst
);
498 /* Check whether the dependence graph has an edge of the given type
499 * between the given two nodes.
501 static isl_bool
graph_has_edge(struct isl_sched_graph
*graph
,
502 enum isl_edge_type type
,
503 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
505 struct isl_sched_edge
*edge
;
508 edge
= graph_find_edge(graph
, type
, src
, dst
);
512 empty
= isl_map_plain_is_empty(edge
->map
);
514 return isl_bool_error
;
519 /* Look for any edge with the same src, dst and map fields as "model".
521 * Return the matching edge if one can be found.
522 * Return "model" if no matching edge is found.
523 * Return NULL on error.
525 static struct isl_sched_edge
*graph_find_matching_edge(
526 struct isl_sched_graph
*graph
, struct isl_sched_edge
*model
)
528 enum isl_edge_type i
;
529 struct isl_sched_edge
*edge
;
531 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
534 edge
= graph_find_edge(graph
, i
, model
->src
, model
->dst
);
537 is_equal
= isl_map_plain_is_equal(model
->map
, edge
->map
);
547 /* Remove the given edge from all the edge_tables that refer to it.
549 static void graph_remove_edge(struct isl_sched_graph
*graph
,
550 struct isl_sched_edge
*edge
)
552 isl_ctx
*ctx
= isl_map_get_ctx(edge
->map
);
553 enum isl_edge_type i
;
555 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
556 struct isl_hash_table_entry
*entry
;
558 entry
= graph_find_edge_entry(graph
, i
, edge
->src
, edge
->dst
);
561 if (entry
->data
!= edge
)
563 isl_hash_table_remove(ctx
, graph
->edge_table
[i
], entry
);
567 /* Check whether the dependence graph has any edge
568 * between the given two nodes.
570 static isl_bool
graph_has_any_edge(struct isl_sched_graph
*graph
,
571 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
573 enum isl_edge_type i
;
576 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
577 r
= graph_has_edge(graph
, i
, src
, dst
);
585 /* Check whether the dependence graph has a validity edge
586 * between the given two nodes.
588 * Conditional validity edges are essentially validity edges that
589 * can be ignored if the corresponding condition edges are iteration private.
590 * Here, we are only checking for the presence of validity
591 * edges, so we need to consider the conditional validity edges too.
592 * In particular, this function is used during the detection
593 * of strongly connected components and we cannot ignore
594 * conditional validity edges during this detection.
596 static isl_bool
graph_has_validity_edge(struct isl_sched_graph
*graph
,
597 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
601 r
= graph_has_edge(graph
, isl_edge_validity
, src
, dst
);
605 return graph_has_edge(graph
, isl_edge_conditional_validity
, src
, dst
);
608 static int graph_alloc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
609 int n_node
, int n_edge
)
614 graph
->n_edge
= n_edge
;
615 graph
->node
= isl_calloc_array(ctx
, struct isl_sched_node
, graph
->n
);
616 graph
->sorted
= isl_calloc_array(ctx
, int, graph
->n
);
617 graph
->region
= isl_alloc_array(ctx
, struct isl_region
, graph
->n
);
618 graph
->edge
= isl_calloc_array(ctx
,
619 struct isl_sched_edge
, graph
->n_edge
);
621 graph
->intra_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
622 graph
->inter_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
624 if (!graph
->node
|| !graph
->region
|| (graph
->n_edge
&& !graph
->edge
) ||
628 for(i
= 0; i
< graph
->n
; ++i
)
629 graph
->sorted
[i
] = i
;
634 static void graph_free(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
638 isl_map_to_basic_set_free(graph
->intra_hmap
);
639 isl_map_to_basic_set_free(graph
->inter_hmap
);
642 for (i
= 0; i
< graph
->n
; ++i
) {
643 isl_space_free(graph
->node
[i
].space
);
644 isl_set_free(graph
->node
[i
].hull
);
645 isl_multi_aff_free(graph
->node
[i
].compress
);
646 isl_multi_aff_free(graph
->node
[i
].decompress
);
647 isl_mat_free(graph
->node
[i
].sched
);
648 isl_map_free(graph
->node
[i
].sched_map
);
649 isl_mat_free(graph
->node
[i
].cmap
);
650 isl_mat_free(graph
->node
[i
].cinv
);
651 isl_mat_free(graph
->node
[i
].ctrans
);
653 free(graph
->node
[i
].coincident
);
654 isl_multi_val_free(graph
->node
[i
].sizes
);
655 isl_vec_free(graph
->node
[i
].max
);
660 for (i
= 0; i
< graph
->n_edge
; ++i
) {
661 isl_map_free(graph
->edge
[i
].map
);
662 isl_union_map_free(graph
->edge
[i
].tagged_condition
);
663 isl_union_map_free(graph
->edge
[i
].tagged_validity
);
667 for (i
= 0; i
<= isl_edge_last
; ++i
)
668 isl_hash_table_free(ctx
, graph
->edge_table
[i
]);
669 isl_hash_table_free(ctx
, graph
->node_table
);
670 isl_basic_set_free(graph
->lp
);
673 /* For each "set" on which this function is called, increment
674 * graph->n by one and update graph->maxvar.
676 static isl_stat
init_n_maxvar(__isl_take isl_set
*set
, void *user
)
678 struct isl_sched_graph
*graph
= user
;
679 int nvar
= isl_set_dim(set
, isl_dim_set
);
682 if (nvar
> graph
->maxvar
)
683 graph
->maxvar
= nvar
;
690 /* Compute the number of rows that should be allocated for the schedule.
691 * In particular, we need one row for each variable or one row
692 * for each basic map in the dependences.
693 * Note that it is practically impossible to exhaust both
694 * the number of dependences and the number of variables.
696 static isl_stat
compute_max_row(struct isl_sched_graph
*graph
,
697 __isl_keep isl_schedule_constraints
*sc
)
701 isl_union_set
*domain
;
705 domain
= isl_schedule_constraints_get_domain(sc
);
706 r
= isl_union_set_foreach_set(domain
, &init_n_maxvar
, graph
);
707 isl_union_set_free(domain
);
709 return isl_stat_error
;
710 n_edge
= isl_schedule_constraints_n_basic_map(sc
);
712 return isl_stat_error
;
713 graph
->max_row
= n_edge
+ graph
->maxvar
;
718 /* Does "bset" have any defining equalities for its set variables?
720 static isl_bool
has_any_defining_equality(__isl_keep isl_basic_set
*bset
)
725 return isl_bool_error
;
727 n
= isl_basic_set_dim(bset
, isl_dim_set
);
728 for (i
= 0; i
< n
; ++i
) {
731 has
= isl_basic_set_has_defining_equality(bset
, isl_dim_set
, i
,
737 return isl_bool_false
;
740 /* Set the entries of node->max to the value of the schedule_max_coefficient
743 static isl_stat
set_max_coefficient(isl_ctx
*ctx
, struct isl_sched_node
*node
)
747 max
= isl_options_get_schedule_max_coefficient(ctx
);
751 node
->max
= isl_vec_alloc(ctx
, node
->nvar
);
752 node
->max
= isl_vec_set_si(node
->max
, max
);
754 return isl_stat_error
;
759 /* Set the entries of node->max to the minimum of the schedule_max_coefficient
760 * option (if set) and half of the minimum of the sizes in the other
761 * dimensions. If the minimum of the sizes is one, half of the size
762 * is zero and this value is reset to one.
763 * If the global minimum is unbounded (i.e., if both
764 * the schedule_max_coefficient is not set and the sizes in the other
765 * dimensions are unbounded), then store a negative value.
766 * If the schedule coefficient is close to the size of the instance set
767 * in another dimension, then the schedule may represent a loop
768 * coalescing transformation (especially if the coefficient
769 * in that other dimension is one). Forcing the coefficient to be
770 * smaller than or equal to half the minimal size should avoid this
773 static isl_stat
compute_max_coefficient(isl_ctx
*ctx
,
774 struct isl_sched_node
*node
)
780 max
= isl_options_get_schedule_max_coefficient(ctx
);
781 v
= isl_vec_alloc(ctx
, node
->nvar
);
783 return isl_stat_error
;
785 for (i
= 0; i
< node
->nvar
; ++i
) {
786 isl_int_set_si(v
->el
[i
], max
);
787 isl_int_mul_si(v
->el
[i
], v
->el
[i
], 2);
790 for (i
= 0; i
< node
->nvar
; ++i
) {
793 size
= isl_multi_val_get_val(node
->sizes
, i
);
796 if (!isl_val_is_int(size
)) {
800 for (j
= 0; j
< node
->nvar
; ++j
) {
803 if (isl_int_is_neg(v
->el
[j
]) ||
804 isl_int_gt(v
->el
[j
], size
->n
))
805 isl_int_set(v
->el
[j
], size
->n
);
810 for (i
= 0; i
< node
->nvar
; ++i
) {
811 isl_int_fdiv_q_ui(v
->el
[i
], v
->el
[i
], 2);
812 if (isl_int_is_zero(v
->el
[i
]))
813 isl_int_set_si(v
->el
[i
], 1);
820 return isl_stat_error
;
823 /* Compute and return the size of "set" in dimension "dim".
824 * The size is taken to be the difference in values for that variable
825 * for fixed values of the other variables.
826 * In particular, the variable is first isolated from the other variables
827 * in the range of a map
829 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [i_dim]
831 * and then duplicated
833 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [[i_dim] -> [i_dim']]
835 * The shared variables are then projected out and the maximal value
836 * of i_dim' - i_dim is computed.
838 static __isl_give isl_val
*compute_size(__isl_take isl_set
*set
, int dim
)
845 map
= isl_set_project_onto_map(set
, isl_dim_set
, dim
, 1);
846 map
= isl_map_project_out(map
, isl_dim_in
, dim
, 1);
847 map
= isl_map_range_product(map
, isl_map_copy(map
));
848 map
= isl_set_unwrap(isl_map_range(map
));
849 set
= isl_map_deltas(map
);
850 ls
= isl_local_space_from_space(isl_set_get_space(set
));
851 obj
= isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
852 v
= isl_set_max_val(set
, obj
);
859 /* Compute the size of the instance set "set" of "node", after compression,
860 * as well as bounds on the corresponding coefficients, if needed.
862 * The sizes are needed when the schedule_treat_coalescing option is set.
863 * The bounds are needed when the schedule_treat_coalescing option or
864 * the schedule_max_coefficient option is set.
866 * If the schedule_treat_coalescing option is not set, then at most
867 * the bounds need to be set and this is done in set_max_coefficient.
868 * Otherwise, compress the domain if needed, compute the size
869 * in each direction and store the results in node->size.
870 * Finally, set the bounds on the coefficients based on the sizes
871 * and the schedule_max_coefficient option in compute_max_coefficient.
873 static isl_stat
compute_sizes_and_max(isl_ctx
*ctx
, struct isl_sched_node
*node
,
874 __isl_take isl_set
*set
)
879 if (!isl_options_get_schedule_treat_coalescing(ctx
)) {
881 return set_max_coefficient(ctx
, node
);
884 if (node
->compressed
)
885 set
= isl_set_preimage_multi_aff(set
,
886 isl_multi_aff_copy(node
->decompress
));
887 mv
= isl_multi_val_zero(isl_set_get_space(set
));
888 n
= isl_set_dim(set
, isl_dim_set
);
889 for (j
= 0; j
< n
; ++j
) {
892 v
= compute_size(isl_set_copy(set
), j
);
893 mv
= isl_multi_val_set_val(mv
, j
, v
);
898 return isl_stat_error
;
899 return compute_max_coefficient(ctx
, node
);
902 /* Add a new node to the graph representing the given instance set.
903 * "nvar" is the (possibly compressed) number of variables and
904 * may be smaller than then number of set variables in "set"
905 * if "compressed" is set.
906 * If "compressed" is set, then "hull" represents the constraints
907 * that were used to derive the compression, while "compress" and
908 * "decompress" map the original space to the compressed space and
910 * If "compressed" is not set, then "hull", "compress" and "decompress"
913 * Compute the size of the instance set and bounds on the coefficients,
916 static isl_stat
add_node(struct isl_sched_graph
*graph
,
917 __isl_take isl_set
*set
, int nvar
, int compressed
,
918 __isl_take isl_set
*hull
, __isl_take isl_multi_aff
*compress
,
919 __isl_take isl_multi_aff
*decompress
)
926 struct isl_sched_node
*node
;
929 return isl_stat_error
;
931 ctx
= isl_set_get_ctx(set
);
932 nparam
= isl_set_dim(set
, isl_dim_param
);
933 if (!ctx
->opt
->schedule_parametric
)
935 sched
= isl_mat_alloc(ctx
, 0, 1 + nparam
+ nvar
);
936 node
= &graph
->node
[graph
->n
];
938 space
= isl_set_get_space(set
);
941 node
->nparam
= nparam
;
943 node
->sched_map
= NULL
;
944 coincident
= isl_calloc_array(ctx
, int, graph
->max_row
);
945 node
->coincident
= coincident
;
946 node
->compressed
= compressed
;
948 node
->compress
= compress
;
949 node
->decompress
= decompress
;
950 if (compute_sizes_and_max(ctx
, node
, set
) < 0)
951 return isl_stat_error
;
953 if (!space
|| !sched
|| (graph
->max_row
&& !coincident
))
954 return isl_stat_error
;
955 if (compressed
&& (!hull
|| !compress
|| !decompress
))
956 return isl_stat_error
;
961 /* Add a new node to the graph representing the given set.
963 * If any of the set variables is defined by an equality, then
964 * we perform variable compression such that we can perform
965 * the scheduling on the compressed domain.
967 static isl_stat
extract_node(__isl_take isl_set
*set
, void *user
)
970 isl_bool has_equality
;
974 isl_multi_aff
*compress
, *decompress
;
975 struct isl_sched_graph
*graph
= user
;
977 hull
= isl_set_affine_hull(isl_set_copy(set
));
978 hull
= isl_basic_set_remove_divs(hull
);
979 nvar
= isl_set_dim(set
, isl_dim_set
);
980 has_equality
= has_any_defining_equality(hull
);
982 if (has_equality
< 0)
985 isl_basic_set_free(hull
);
986 return add_node(graph
, set
, nvar
, 0, NULL
, NULL
, NULL
);
989 morph
= isl_basic_set_variable_compression(hull
, isl_dim_set
);
990 nvar
= isl_morph_ran_dim(morph
, isl_dim_set
);
991 compress
= isl_morph_get_var_multi_aff(morph
);
992 morph
= isl_morph_inverse(morph
);
993 decompress
= isl_morph_get_var_multi_aff(morph
);
994 isl_morph_free(morph
);
996 hull_set
= isl_set_from_basic_set(hull
);
997 return add_node(graph
, set
, nvar
, 1, hull_set
, compress
, decompress
);
999 isl_basic_set_free(hull
);
1001 return isl_stat_error
;
1004 struct isl_extract_edge_data
{
1005 enum isl_edge_type type
;
1006 struct isl_sched_graph
*graph
;
1009 /* Merge edge2 into edge1, freeing the contents of edge2.
1010 * Return 0 on success and -1 on failure.
1012 * edge1 and edge2 are assumed to have the same value for the map field.
1014 static int merge_edge(struct isl_sched_edge
*edge1
,
1015 struct isl_sched_edge
*edge2
)
1017 edge1
->types
|= edge2
->types
;
1018 isl_map_free(edge2
->map
);
1020 if (is_condition(edge2
)) {
1021 if (!edge1
->tagged_condition
)
1022 edge1
->tagged_condition
= edge2
->tagged_condition
;
1024 edge1
->tagged_condition
=
1025 isl_union_map_union(edge1
->tagged_condition
,
1026 edge2
->tagged_condition
);
1029 if (is_conditional_validity(edge2
)) {
1030 if (!edge1
->tagged_validity
)
1031 edge1
->tagged_validity
= edge2
->tagged_validity
;
1033 edge1
->tagged_validity
=
1034 isl_union_map_union(edge1
->tagged_validity
,
1035 edge2
->tagged_validity
);
1038 if (is_condition(edge2
) && !edge1
->tagged_condition
)
1040 if (is_conditional_validity(edge2
) && !edge1
->tagged_validity
)
1046 /* Insert dummy tags in domain and range of "map".
1048 * In particular, if "map" is of the form
1054 * [A -> dummy_tag] -> [B -> dummy_tag]
1056 * where the dummy_tags are identical and equal to any dummy tags
1057 * introduced by any other call to this function.
1059 static __isl_give isl_map
*insert_dummy_tags(__isl_take isl_map
*map
)
1065 isl_set
*domain
, *range
;
1067 ctx
= isl_map_get_ctx(map
);
1069 id
= isl_id_alloc(ctx
, NULL
, &dummy
);
1070 space
= isl_space_params(isl_map_get_space(map
));
1071 space
= isl_space_set_from_params(space
);
1072 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
1073 space
= isl_space_map_from_set(space
);
1075 domain
= isl_map_wrap(map
);
1076 range
= isl_map_wrap(isl_map_universe(space
));
1077 map
= isl_map_from_domain_and_range(domain
, range
);
1078 map
= isl_map_zip(map
);
1083 /* Given that at least one of "src" or "dst" is compressed, return
1084 * a map between the spaces of these nodes restricted to the affine
1085 * hull that was used in the compression.
1087 static __isl_give isl_map
*extract_hull(struct isl_sched_node
*src
,
1088 struct isl_sched_node
*dst
)
1092 if (src
->compressed
)
1093 dom
= isl_set_copy(src
->hull
);
1095 dom
= isl_set_universe(isl_space_copy(src
->space
));
1096 if (dst
->compressed
)
1097 ran
= isl_set_copy(dst
->hull
);
1099 ran
= isl_set_universe(isl_space_copy(dst
->space
));
1101 return isl_map_from_domain_and_range(dom
, ran
);
1104 /* Intersect the domains of the nested relations in domain and range
1105 * of "tagged" with "map".
1107 static __isl_give isl_map
*map_intersect_domains(__isl_take isl_map
*tagged
,
1108 __isl_keep isl_map
*map
)
1112 tagged
= isl_map_zip(tagged
);
1113 set
= isl_map_wrap(isl_map_copy(map
));
1114 tagged
= isl_map_intersect_domain(tagged
, set
);
1115 tagged
= isl_map_zip(tagged
);
1119 /* Return a pointer to the node that lives in the domain space of "map"
1120 * or NULL if there is no such node.
1122 static struct isl_sched_node
*find_domain_node(isl_ctx
*ctx
,
1123 struct isl_sched_graph
*graph
, __isl_keep isl_map
*map
)
1125 struct isl_sched_node
*node
;
1128 space
= isl_space_domain(isl_map_get_space(map
));
1129 node
= graph_find_node(ctx
, graph
, space
);
1130 isl_space_free(space
);
1135 /* Return a pointer to the node that lives in the range space of "map"
1136 * or NULL if there is no such node.
1138 static struct isl_sched_node
*find_range_node(isl_ctx
*ctx
,
1139 struct isl_sched_graph
*graph
, __isl_keep isl_map
*map
)
1141 struct isl_sched_node
*node
;
1144 space
= isl_space_range(isl_map_get_space(map
));
1145 node
= graph_find_node(ctx
, graph
, space
);
1146 isl_space_free(space
);
1151 /* Add a new edge to the graph based on the given map
1152 * and add it to data->graph->edge_table[data->type].
1153 * If a dependence relation of a given type happens to be identical
1154 * to one of the dependence relations of a type that was added before,
1155 * then we don't create a new edge, but instead mark the original edge
1156 * as also representing a dependence of the current type.
1158 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1159 * may be specified as "tagged" dependence relations. That is, "map"
1160 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1161 * the dependence on iterations and a and b are tags.
1162 * edge->map is set to the relation containing the elements i -> j,
1163 * while edge->tagged_condition and edge->tagged_validity contain
1164 * the union of all the "map" relations
1165 * for which extract_edge is called that result in the same edge->map.
1167 * If the source or the destination node is compressed, then
1168 * intersect both "map" and "tagged" with the constraints that
1169 * were used to construct the compression.
1170 * This ensures that there are no schedule constraints defined
1171 * outside of these domains, while the scheduler no longer has
1172 * any control over those outside parts.
1174 static isl_stat
extract_edge(__isl_take isl_map
*map
, void *user
)
1176 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1177 struct isl_extract_edge_data
*data
= user
;
1178 struct isl_sched_graph
*graph
= data
->graph
;
1179 struct isl_sched_node
*src
, *dst
;
1180 struct isl_sched_edge
*edge
;
1181 isl_map
*tagged
= NULL
;
1183 if (data
->type
== isl_edge_condition
||
1184 data
->type
== isl_edge_conditional_validity
) {
1185 if (isl_map_can_zip(map
)) {
1186 tagged
= isl_map_copy(map
);
1187 map
= isl_set_unwrap(isl_map_domain(isl_map_zip(map
)));
1189 tagged
= insert_dummy_tags(isl_map_copy(map
));
1193 src
= find_domain_node(ctx
, graph
, map
);
1194 dst
= find_range_node(ctx
, graph
, map
);
1198 isl_map_free(tagged
);
1202 if (src
->compressed
|| dst
->compressed
) {
1204 hull
= extract_hull(src
, dst
);
1206 tagged
= map_intersect_domains(tagged
, hull
);
1207 map
= isl_map_intersect(map
, hull
);
1210 graph
->edge
[graph
->n_edge
].src
= src
;
1211 graph
->edge
[graph
->n_edge
].dst
= dst
;
1212 graph
->edge
[graph
->n_edge
].map
= map
;
1213 graph
->edge
[graph
->n_edge
].types
= 0;
1214 graph
->edge
[graph
->n_edge
].tagged_condition
= NULL
;
1215 graph
->edge
[graph
->n_edge
].tagged_validity
= NULL
;
1216 set_type(&graph
->edge
[graph
->n_edge
], data
->type
);
1217 if (data
->type
== isl_edge_condition
)
1218 graph
->edge
[graph
->n_edge
].tagged_condition
=
1219 isl_union_map_from_map(tagged
);
1220 if (data
->type
== isl_edge_conditional_validity
)
1221 graph
->edge
[graph
->n_edge
].tagged_validity
=
1222 isl_union_map_from_map(tagged
);
1224 edge
= graph_find_matching_edge(graph
, &graph
->edge
[graph
->n_edge
]);
1227 return isl_stat_error
;
1229 if (edge
== &graph
->edge
[graph
->n_edge
])
1230 return graph_edge_table_add(ctx
, graph
, data
->type
,
1231 &graph
->edge
[graph
->n_edge
++]);
1233 if (merge_edge(edge
, &graph
->edge
[graph
->n_edge
]) < 0)
1236 return graph_edge_table_add(ctx
, graph
, data
->type
, edge
);
1239 /* Initialize the schedule graph "graph" from the schedule constraints "sc".
1241 * The context is included in the domain before the nodes of
1242 * the graphs are extracted in order to be able to exploit
1243 * any possible additional equalities.
1244 * Note that this intersection is only performed locally here.
1246 static isl_stat
graph_init(struct isl_sched_graph
*graph
,
1247 __isl_keep isl_schedule_constraints
*sc
)
1250 isl_union_set
*domain
;
1252 struct isl_extract_edge_data data
;
1253 enum isl_edge_type i
;
1257 return isl_stat_error
;
1259 ctx
= isl_schedule_constraints_get_ctx(sc
);
1261 domain
= isl_schedule_constraints_get_domain(sc
);
1262 graph
->n
= isl_union_set_n_set(domain
);
1263 isl_union_set_free(domain
);
1265 if (graph_alloc(ctx
, graph
, graph
->n
,
1266 isl_schedule_constraints_n_map(sc
)) < 0)
1267 return isl_stat_error
;
1269 if (compute_max_row(graph
, sc
) < 0)
1270 return isl_stat_error
;
1273 domain
= isl_schedule_constraints_get_domain(sc
);
1274 domain
= isl_union_set_intersect_params(domain
,
1275 isl_schedule_constraints_get_context(sc
));
1276 r
= isl_union_set_foreach_set(domain
, &extract_node
, graph
);
1277 isl_union_set_free(domain
);
1279 return isl_stat_error
;
1280 if (graph_init_table(ctx
, graph
) < 0)
1281 return isl_stat_error
;
1282 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
1283 c
= isl_schedule_constraints_get(sc
, i
);
1284 graph
->max_edge
[i
] = isl_union_map_n_map(c
);
1285 isl_union_map_free(c
);
1287 return isl_stat_error
;
1289 if (graph_init_edge_tables(ctx
, graph
) < 0)
1290 return isl_stat_error
;
1293 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
1297 c
= isl_schedule_constraints_get(sc
, i
);
1298 r
= isl_union_map_foreach_map(c
, &extract_edge
, &data
);
1299 isl_union_map_free(c
);
1301 return isl_stat_error
;
1307 /* Check whether there is any dependence from node[j] to node[i]
1308 * or from node[i] to node[j].
1310 static isl_bool
node_follows_weak(int i
, int j
, void *user
)
1313 struct isl_sched_graph
*graph
= user
;
1315 f
= graph_has_any_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1318 return graph_has_any_edge(graph
, &graph
->node
[i
], &graph
->node
[j
]);
1321 /* Check whether there is a (conditional) validity dependence from node[j]
1322 * to node[i], forcing node[i] to follow node[j].
1324 static isl_bool
node_follows_strong(int i
, int j
, void *user
)
1326 struct isl_sched_graph
*graph
= user
;
1328 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1331 /* Use Tarjan's algorithm for computing the strongly connected components
1332 * in the dependence graph only considering those edges defined by "follows".
1334 static int detect_ccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
1335 isl_bool (*follows
)(int i
, int j
, void *user
))
1338 struct isl_tarjan_graph
*g
= NULL
;
1340 g
= isl_tarjan_graph_init(ctx
, graph
->n
, follows
, graph
);
1348 while (g
->order
[i
] != -1) {
1349 graph
->node
[g
->order
[i
]].scc
= graph
->scc
;
1357 isl_tarjan_graph_free(g
);
1362 /* Apply Tarjan's algorithm to detect the strongly connected components
1363 * in the dependence graph.
1364 * Only consider the (conditional) validity dependences and clear "weak".
1366 static int detect_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1369 return detect_ccs(ctx
, graph
, &node_follows_strong
);
1372 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1373 * in the dependence graph.
1374 * Consider all dependences and set "weak".
1376 static int detect_wccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1379 return detect_ccs(ctx
, graph
, &node_follows_weak
);
1382 static int cmp_scc(const void *a
, const void *b
, void *data
)
1384 struct isl_sched_graph
*graph
= data
;
1388 return graph
->node
[*i1
].scc
- graph
->node
[*i2
].scc
;
1391 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1393 static int sort_sccs(struct isl_sched_graph
*graph
)
1395 return isl_sort(graph
->sorted
, graph
->n
, sizeof(int), &cmp_scc
, graph
);
1398 /* Given a dependence relation R from "node" to itself,
1399 * construct the set of coefficients of valid constraints for elements
1400 * in that dependence relation.
1401 * In particular, the result contains tuples of coefficients
1402 * c_0, c_n, c_x such that
1404 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1408 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1410 * We choose here to compute the dual of delta R.
1411 * Alternatively, we could have computed the dual of R, resulting
1412 * in a set of tuples c_0, c_n, c_x, c_y, and then
1413 * plugged in (c_0, c_n, c_x, -c_x).
1415 * If "node" has been compressed, then the dependence relation
1416 * is also compressed before the set of coefficients is computed.
1418 static __isl_give isl_basic_set
*intra_coefficients(
1419 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1420 __isl_take isl_map
*map
)
1424 isl_basic_set
*coef
;
1425 isl_maybe_isl_basic_set m
;
1427 m
= isl_map_to_basic_set_try_get(graph
->intra_hmap
, map
);
1428 if (m
.valid
< 0 || m
.valid
) {
1433 key
= isl_map_copy(map
);
1434 if (node
->compressed
) {
1435 map
= isl_map_preimage_domain_multi_aff(map
,
1436 isl_multi_aff_copy(node
->decompress
));
1437 map
= isl_map_preimage_range_multi_aff(map
,
1438 isl_multi_aff_copy(node
->decompress
));
1440 delta
= isl_set_remove_divs(isl_map_deltas(map
));
1441 coef
= isl_set_coefficients(delta
);
1442 graph
->intra_hmap
= isl_map_to_basic_set_set(graph
->intra_hmap
, key
,
1443 isl_basic_set_copy(coef
));
1448 /* Given a dependence relation R, construct the set of coefficients
1449 * of valid constraints for elements in that dependence relation.
1450 * In particular, the result contains tuples of coefficients
1451 * c_0, c_n, c_x, c_y such that
1453 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1455 * If the source or destination nodes of "edge" have been compressed,
1456 * then the dependence relation is also compressed before
1457 * the set of coefficients is computed.
1459 static __isl_give isl_basic_set
*inter_coefficients(
1460 struct isl_sched_graph
*graph
, struct isl_sched_edge
*edge
,
1461 __isl_take isl_map
*map
)
1465 isl_basic_set
*coef
;
1466 isl_maybe_isl_basic_set m
;
1468 m
= isl_map_to_basic_set_try_get(graph
->inter_hmap
, map
);
1469 if (m
.valid
< 0 || m
.valid
) {
1474 key
= isl_map_copy(map
);
1475 if (edge
->src
->compressed
)
1476 map
= isl_map_preimage_domain_multi_aff(map
,
1477 isl_multi_aff_copy(edge
->src
->decompress
));
1478 if (edge
->dst
->compressed
)
1479 map
= isl_map_preimage_range_multi_aff(map
,
1480 isl_multi_aff_copy(edge
->dst
->decompress
));
1481 set
= isl_map_wrap(isl_map_remove_divs(map
));
1482 coef
= isl_set_coefficients(set
);
1483 graph
->inter_hmap
= isl_map_to_basic_set_set(graph
->inter_hmap
, key
,
1484 isl_basic_set_copy(coef
));
1489 /* Return the position of the coefficients of the variables in
1490 * the coefficients constraints "coef".
1492 * The space of "coef" is of the form
1494 * { coefficients[[cst, params] -> S] }
1496 * Return the position of S.
1498 static int coef_var_offset(__isl_keep isl_basic_set
*coef
)
1503 space
= isl_space_unwrap(isl_basic_set_get_space(coef
));
1504 offset
= isl_space_dim(space
, isl_dim_in
);
1505 isl_space_free(space
);
1510 /* Return the offset of the coefficients of the variables of "node"
1513 * Within each node, the coefficients have the following order:
1515 * - c_i_n (if parametric)
1516 * - positive and negative parts of c_i_x
1518 static int node_var_coef_offset(struct isl_sched_node
*node
)
1520 return node
->start
+ 1 + node
->nparam
;
1523 /* Construct an isl_dim_map for mapping constraints on coefficients
1524 * for "node" to the corresponding positions in graph->lp.
1525 * "offset" is the offset of the coefficients for the variables
1526 * in the input constraints.
1527 * "s" is the sign of the mapping.
1529 * The input constraints are given in terms of the coefficients (c_0, c_n, c_x).
1530 * The mapping produced by this function essentially plugs in
1531 * (0, 0, c_i_x^+ - c_i_x^-) if s = 1 and
1532 * (0, 0, -c_i_x^+ + c_i_x^-) if s = -1.
1533 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1535 * The caller can extend the mapping to also map the other coefficients
1536 * (and therefore not plug in 0).
1538 static __isl_give isl_dim_map
*intra_dim_map(isl_ctx
*ctx
,
1539 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1544 isl_dim_map
*dim_map
;
1546 total
= isl_basic_set_total_dim(graph
->lp
);
1547 pos
= node_var_coef_offset(node
);
1548 dim_map
= isl_dim_map_alloc(ctx
, total
);
1549 isl_dim_map_range(dim_map
, pos
, 2, offset
, 1, node
->nvar
, -s
);
1550 isl_dim_map_range(dim_map
, pos
+ 1, 2, offset
, 1, node
->nvar
, s
);
1555 /* Construct an isl_dim_map for mapping constraints on coefficients
1556 * for "src" (node i) and "dst" (node j) to the corresponding positions
1558 * "offset" is the offset of the coefficients for the variables of "src"
1559 * in the input constraints.
1560 * "s" is the sign of the mapping.
1562 * The input constraints are given in terms of the coefficients
1563 * (c_0, c_n, c_x, c_y).
1564 * The mapping produced by this function essentially plugs in
1565 * (c_j_0 - c_i_0, c_j_n - c_i_n,
1566 * -(c_i_x^+ - c_i_x^-), c_j_x^+ - c_j_x^-) if s = 1 and
1567 * (-c_j_0 + c_i_0, -c_j_n + c_i_n,
1568 * c_i_x^+ - c_i_x^-, -(c_j_x^+ - c_j_x^-)) if s = -1.
1569 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1571 * The caller can further extend the mapping.
1573 static __isl_give isl_dim_map
*inter_dim_map(isl_ctx
*ctx
,
1574 struct isl_sched_graph
*graph
, struct isl_sched_node
*src
,
1575 struct isl_sched_node
*dst
, int offset
, int s
)
1579 isl_dim_map
*dim_map
;
1581 total
= isl_basic_set_total_dim(graph
->lp
);
1582 dim_map
= isl_dim_map_alloc(ctx
, total
);
1584 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, s
);
1585 isl_dim_map_range(dim_map
, dst
->start
+ 1, 1, 1, 1, dst
->nparam
, s
);
1586 pos
= node_var_coef_offset(dst
);
1587 isl_dim_map_range(dim_map
, pos
, 2, offset
+ src
->nvar
, 1,
1589 isl_dim_map_range(dim_map
, pos
+ 1, 2, offset
+ src
->nvar
, 1,
1592 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, -s
);
1593 isl_dim_map_range(dim_map
, src
->start
+ 1, 1, 1, 1, src
->nparam
, -s
);
1594 pos
= node_var_coef_offset(src
);
1595 isl_dim_map_range(dim_map
, pos
, 2, offset
, 1, src
->nvar
, s
);
1596 isl_dim_map_range(dim_map
, pos
+ 1, 2, offset
, 1, src
->nvar
, -s
);
1601 /* Add the constraints from "src" to "dst" using "dim_map",
1602 * after making sure there is enough room in "dst" for the extra constraints.
1604 static __isl_give isl_basic_set
*add_constraints_dim_map(
1605 __isl_take isl_basic_set
*dst
, __isl_take isl_basic_set
*src
,
1606 __isl_take isl_dim_map
*dim_map
)
1610 n_eq
= isl_basic_set_n_equality(src
);
1611 n_ineq
= isl_basic_set_n_inequality(src
);
1612 dst
= isl_basic_set_extend_constraints(dst
, n_eq
, n_ineq
);
1613 dst
= isl_basic_set_add_constraints_dim_map(dst
, src
, dim_map
);
1617 /* Add constraints to graph->lp that force validity for the given
1618 * dependence from a node i to itself.
1619 * That is, add constraints that enforce
1621 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1622 * = c_i_x (y - x) >= 0
1624 * for each (x,y) in R.
1625 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1626 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1627 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1628 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1630 * Actually, we do not construct constraints for the c_i_x themselves,
1631 * but for the coefficients of c_i_x written as a linear combination
1632 * of the columns in node->cmap.
1634 static isl_stat
add_intra_validity_constraints(struct isl_sched_graph
*graph
,
1635 struct isl_sched_edge
*edge
)
1638 isl_map
*map
= isl_map_copy(edge
->map
);
1639 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1640 isl_dim_map
*dim_map
;
1641 isl_basic_set
*coef
;
1642 struct isl_sched_node
*node
= edge
->src
;
1644 coef
= intra_coefficients(graph
, node
, map
);
1646 offset
= coef_var_offset(coef
);
1648 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1649 offset
, isl_mat_copy(node
->cmap
));
1651 return isl_stat_error
;
1653 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, 1);
1654 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
1659 /* Add constraints to graph->lp that force validity for the given
1660 * dependence from node i to node j.
1661 * That is, add constraints that enforce
1663 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1665 * for each (x,y) in R.
1666 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1667 * of valid constraints for R and then plug in
1668 * (c_j_0 - c_i_0, c_j_n - c_i_n, -(c_i_x^+ - c_i_x^-), c_j_x^+ - c_j_x^-),
1669 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1670 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1672 * Actually, we do not construct constraints for the c_*_x themselves,
1673 * but for the coefficients of c_*_x written as a linear combination
1674 * of the columns in node->cmap.
1676 static isl_stat
add_inter_validity_constraints(struct isl_sched_graph
*graph
,
1677 struct isl_sched_edge
*edge
)
1682 isl_dim_map
*dim_map
;
1683 isl_basic_set
*coef
;
1684 struct isl_sched_node
*src
= edge
->src
;
1685 struct isl_sched_node
*dst
= edge
->dst
;
1688 return isl_stat_error
;
1690 map
= isl_map_copy(edge
->map
);
1691 ctx
= isl_map_get_ctx(map
);
1692 coef
= inter_coefficients(graph
, edge
, map
);
1694 offset
= coef_var_offset(coef
);
1696 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1697 offset
, isl_mat_copy(src
->cmap
));
1698 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1699 offset
+ src
->nvar
, isl_mat_copy(dst
->cmap
));
1701 return isl_stat_error
;
1703 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, 1);
1705 edge
->start
= graph
->lp
->n_ineq
;
1706 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
1708 return isl_stat_error
;
1709 edge
->end
= graph
->lp
->n_ineq
;
1714 /* Add constraints to graph->lp that bound the dependence distance for the given
1715 * dependence from a node i to itself.
1716 * If s = 1, we add the constraint
1718 * c_i_x (y - x) <= m_0 + m_n n
1722 * -c_i_x (y - x) + m_0 + m_n n >= 0
1724 * for each (x,y) in R.
1725 * If s = -1, we add the constraint
1727 * -c_i_x (y - x) <= m_0 + m_n n
1731 * c_i_x (y - x) + m_0 + m_n n >= 0
1733 * for each (x,y) in R.
1734 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1735 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1736 * with each coefficient (except m_0) represented as a pair of non-negative
1739 * Actually, we do not construct constraints for the c_i_x themselves,
1740 * but for the coefficients of c_i_x written as a linear combination
1741 * of the columns in node->cmap.
1744 * If "local" is set, then we add constraints
1746 * c_i_x (y - x) <= 0
1750 * -c_i_x (y - x) <= 0
1752 * instead, forcing the dependence distance to be (less than or) equal to 0.
1753 * That is, we plug in (0, 0, -s * c_i_x),
1754 * Note that dependences marked local are treated as validity constraints
1755 * by add_all_validity_constraints and therefore also have
1756 * their distances bounded by 0 from below.
1758 static isl_stat
add_intra_proximity_constraints(struct isl_sched_graph
*graph
,
1759 struct isl_sched_edge
*edge
, int s
, int local
)
1763 isl_map
*map
= isl_map_copy(edge
->map
);
1764 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1765 isl_dim_map
*dim_map
;
1766 isl_basic_set
*coef
;
1767 struct isl_sched_node
*node
= edge
->src
;
1769 coef
= intra_coefficients(graph
, node
, map
);
1771 offset
= coef_var_offset(coef
);
1773 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1774 offset
, isl_mat_copy(node
->cmap
));
1776 return isl_stat_error
;
1778 nparam
= isl_space_dim(node
->space
, isl_dim_param
);
1779 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, -s
);
1782 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1783 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1784 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1786 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
1791 /* Add constraints to graph->lp that bound the dependence distance for the given
1792 * dependence from node i to node j.
1793 * If s = 1, we add the constraint
1795 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1800 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1803 * for each (x,y) in R.
1804 * If s = -1, we add the constraint
1806 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1811 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1814 * for each (x,y) in R.
1815 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1816 * of valid constraints for R and then plug in
1817 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1818 * s*c_i_x, -s*c_j_x)
1819 * with each coefficient (except m_0, c_*_0 and c_*_n)
1820 * represented as a pair of non-negative coefficients.
1822 * Actually, we do not construct constraints for the c_*_x themselves,
1823 * but for the coefficients of c_*_x written as a linear combination
1824 * of the columns in node->cmap.
1827 * If "local" is set (and s = 1), then we add constraints
1829 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1833 * -((c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x)) >= 0
1835 * instead, forcing the dependence distance to be (less than or) equal to 0.
1836 * That is, we plug in
1837 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, s*c_i_x, -s*c_j_x).
1838 * Note that dependences marked local are treated as validity constraints
1839 * by add_all_validity_constraints and therefore also have
1840 * their distances bounded by 0 from below.
1842 static isl_stat
add_inter_proximity_constraints(struct isl_sched_graph
*graph
,
1843 struct isl_sched_edge
*edge
, int s
, int local
)
1847 isl_map
*map
= isl_map_copy(edge
->map
);
1848 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1849 isl_dim_map
*dim_map
;
1850 isl_basic_set
*coef
;
1851 struct isl_sched_node
*src
= edge
->src
;
1852 struct isl_sched_node
*dst
= edge
->dst
;
1854 coef
= inter_coefficients(graph
, edge
, map
);
1856 offset
= coef_var_offset(coef
);
1858 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1859 offset
, isl_mat_copy(src
->cmap
));
1860 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1861 offset
+ src
->nvar
, isl_mat_copy(dst
->cmap
));
1863 return isl_stat_error
;
1865 nparam
= isl_space_dim(src
->space
, isl_dim_param
);
1866 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, -s
);
1869 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1870 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1871 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1874 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
1879 /* Add all validity constraints to graph->lp.
1881 * An edge that is forced to be local needs to have its dependence
1882 * distances equal to zero. We take care of bounding them by 0 from below
1883 * here. add_all_proximity_constraints takes care of bounding them by 0
1886 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1887 * Otherwise, we ignore them.
1889 static int add_all_validity_constraints(struct isl_sched_graph
*graph
,
1890 int use_coincidence
)
1894 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1895 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1898 local
= is_local(edge
) ||
1899 (is_coincidence(edge
) && use_coincidence
);
1900 if (!is_validity(edge
) && !local
)
1902 if (edge
->src
!= edge
->dst
)
1904 if (add_intra_validity_constraints(graph
, edge
) < 0)
1908 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1909 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1912 local
= is_local(edge
) ||
1913 (is_coincidence(edge
) && use_coincidence
);
1914 if (!is_validity(edge
) && !local
)
1916 if (edge
->src
== edge
->dst
)
1918 if (add_inter_validity_constraints(graph
, edge
) < 0)
1925 /* Add constraints to graph->lp that bound the dependence distance
1926 * for all dependence relations.
1927 * If a given proximity dependence is identical to a validity
1928 * dependence, then the dependence distance is already bounded
1929 * from below (by zero), so we only need to bound the distance
1930 * from above. (This includes the case of "local" dependences
1931 * which are treated as validity dependence by add_all_validity_constraints.)
1932 * Otherwise, we need to bound the distance both from above and from below.
1934 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1935 * Otherwise, we ignore them.
1937 static int add_all_proximity_constraints(struct isl_sched_graph
*graph
,
1938 int use_coincidence
)
1942 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1943 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1946 local
= is_local(edge
) ||
1947 (is_coincidence(edge
) && use_coincidence
);
1948 if (!is_proximity(edge
) && !local
)
1950 if (edge
->src
== edge
->dst
&&
1951 add_intra_proximity_constraints(graph
, edge
, 1, local
) < 0)
1953 if (edge
->src
!= edge
->dst
&&
1954 add_inter_proximity_constraints(graph
, edge
, 1, local
) < 0)
1956 if (is_validity(edge
) || local
)
1958 if (edge
->src
== edge
->dst
&&
1959 add_intra_proximity_constraints(graph
, edge
, -1, 0) < 0)
1961 if (edge
->src
!= edge
->dst
&&
1962 add_inter_proximity_constraints(graph
, edge
, -1, 0) < 0)
1969 /* Compute a basis for the rows in the linear part of the schedule
1970 * and extend this basis to a full basis. The remaining rows
1971 * can then be used to force linear independence from the rows
1974 * In particular, given the schedule rows S, we compute
1979 * with H the Hermite normal form of S. That is, all but the
1980 * first rank columns of H are zero and so each row in S is
1981 * a linear combination of the first rank rows of Q.
1982 * The matrix Q is then transposed because we will write the
1983 * coefficients of the next schedule row as a column vector s
1984 * and express this s as a linear combination s = Q c of the
1986 * Similarly, the matrix U is transposed such that we can
1987 * compute the coefficients c = U s from a schedule row s.
1989 static int node_update_cmap(struct isl_sched_node
*node
)
1992 int n_row
= isl_mat_rows(node
->sched
);
1994 H
= isl_mat_sub_alloc(node
->sched
, 0, n_row
,
1995 1 + node
->nparam
, node
->nvar
);
1997 H
= isl_mat_left_hermite(H
, 0, &U
, &Q
);
1998 isl_mat_free(node
->cmap
);
1999 isl_mat_free(node
->cinv
);
2000 isl_mat_free(node
->ctrans
);
2001 node
->ctrans
= isl_mat_copy(Q
);
2002 node
->cmap
= isl_mat_transpose(Q
);
2003 node
->cinv
= isl_mat_transpose(U
);
2004 node
->rank
= isl_mat_initial_non_zero_cols(H
);
2007 if (!node
->cmap
|| !node
->cinv
|| !node
->ctrans
|| node
->rank
< 0)
2012 /* Is "edge" marked as a validity or a conditional validity edge?
2014 static int is_any_validity(struct isl_sched_edge
*edge
)
2016 return is_validity(edge
) || is_conditional_validity(edge
);
2019 /* How many times should we count the constraints in "edge"?
2021 * If carry is set, then we are counting the number of
2022 * (validity or conditional validity) constraints that will be added
2023 * in setup_carry_lp and we count each edge exactly once.
2025 * Otherwise, we count as follows
2026 * validity -> 1 (>= 0)
2027 * validity+proximity -> 2 (>= 0 and upper bound)
2028 * proximity -> 2 (lower and upper bound)
2029 * local(+any) -> 2 (>= 0 and <= 0)
2031 * If an edge is only marked conditional_validity then it counts
2032 * as zero since it is only checked afterwards.
2034 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2035 * Otherwise, we ignore them.
2037 static int edge_multiplicity(struct isl_sched_edge
*edge
, int carry
,
2038 int use_coincidence
)
2042 if (is_proximity(edge
) || is_local(edge
))
2044 if (use_coincidence
&& is_coincidence(edge
))
2046 if (is_validity(edge
))
2051 /* Count the number of equality and inequality constraints
2052 * that will be added for the given map.
2054 * "use_coincidence" is set if we should take into account coincidence edges.
2056 static isl_stat
count_map_constraints(struct isl_sched_graph
*graph
,
2057 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
,
2058 int *n_eq
, int *n_ineq
, int carry
, int use_coincidence
)
2060 isl_basic_set
*coef
;
2061 int f
= edge_multiplicity(edge
, carry
, use_coincidence
);
2068 if (edge
->src
== edge
->dst
)
2069 coef
= intra_coefficients(graph
, edge
->src
, map
);
2071 coef
= inter_coefficients(graph
, edge
, map
);
2073 return isl_stat_error
;
2074 *n_eq
+= f
* isl_basic_set_n_equality(coef
);
2075 *n_ineq
+= f
* isl_basic_set_n_inequality(coef
);
2076 isl_basic_set_free(coef
);
2081 /* Count the number of equality and inequality constraints
2082 * that will be added to the main lp problem.
2083 * We count as follows
2084 * validity -> 1 (>= 0)
2085 * validity+proximity -> 2 (>= 0 and upper bound)
2086 * proximity -> 2 (lower and upper bound)
2087 * local(+any) -> 2 (>= 0 and <= 0)
2089 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2090 * Otherwise, we ignore them.
2092 static int count_constraints(struct isl_sched_graph
*graph
,
2093 int *n_eq
, int *n_ineq
, int use_coincidence
)
2097 *n_eq
= *n_ineq
= 0;
2098 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2099 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2100 isl_map
*map
= isl_map_copy(edge
->map
);
2102 if (count_map_constraints(graph
, edge
, map
, n_eq
, n_ineq
,
2103 0, use_coincidence
) < 0)
2110 /* Count the number of constraints that will be added by
2111 * add_bound_constant_constraints to bound the values of the constant terms
2112 * and increment *n_eq and *n_ineq accordingly.
2114 * In practice, add_bound_constant_constraints only adds inequalities.
2116 static isl_stat
count_bound_constant_constraints(isl_ctx
*ctx
,
2117 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
2119 if (isl_options_get_schedule_max_constant_term(ctx
) == -1)
2122 *n_ineq
+= graph
->n
;
2127 /* Add constraints to bound the values of the constant terms in the schedule,
2128 * if requested by the user.
2130 * The maximal value of the constant terms is defined by the option
2131 * "schedule_max_constant_term".
2133 * Within each node, the coefficients have the following order:
2135 * - c_i_n (if parametric)
2136 * - positive and negative parts of c_i_x
2138 static isl_stat
add_bound_constant_constraints(isl_ctx
*ctx
,
2139 struct isl_sched_graph
*graph
)
2145 max
= isl_options_get_schedule_max_constant_term(ctx
);
2149 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2151 for (i
= 0; i
< graph
->n
; ++i
) {
2152 struct isl_sched_node
*node
= &graph
->node
[i
];
2153 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2155 return isl_stat_error
;
2156 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2157 isl_int_set_si(graph
->lp
->ineq
[k
][1 + node
->start
], -1);
2158 isl_int_set_si(graph
->lp
->ineq
[k
][0], max
);
2164 /* Count the number of constraints that will be added by
2165 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
2168 * In practice, add_bound_coefficient_constraints only adds inequalities.
2170 static int count_bound_coefficient_constraints(isl_ctx
*ctx
,
2171 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
2175 if (isl_options_get_schedule_max_coefficient(ctx
) == -1 &&
2176 !isl_options_get_schedule_treat_coalescing(ctx
))
2179 for (i
= 0; i
< graph
->n
; ++i
)
2180 *n_ineq
+= graph
->node
[i
].nparam
+ 2 * graph
->node
[i
].nvar
;
2185 /* Add constraints to graph->lp that bound the values of
2186 * the parameter schedule coefficients of "node" to "max" and
2187 * the variable schedule coefficients to the corresponding entry
2189 * In either case, a negative value means that no bound needs to be imposed.
2191 * For parameter coefficients, this amounts to adding a constraint
2199 * The variables coefficients are, however, not represented directly.
2200 * Instead, the variables coefficients c_x are written as a linear
2201 * combination c_x = cmap c_z of some other coefficients c_z,
2202 * which are in turn encoded as c_z = c_z^+ - c_z^-.
2203 * Let a_j be the elements of row i of node->cmap, then
2205 * -max_i <= c_x_i <= max_i
2209 * -max_i <= \sum_j a_j (c_z_j^+ - c_z_j^-) <= max_i
2213 * -\sum_j a_j (c_z_j^+ - c_z_j^-) + max_i >= 0
2214 * \sum_j a_j (c_z_j^+ - c_z_j^-) + max_i >= 0
2216 static isl_stat
node_add_coefficient_constraints(isl_ctx
*ctx
,
2217 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
, int max
)
2223 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2225 for (j
= 0; j
< node
->nparam
; ++j
) {
2231 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2233 return isl_stat_error
;
2234 dim
= 1 + node
->start
+ 1 + j
;
2235 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2236 isl_int_set_si(graph
->lp
->ineq
[k
][dim
], -1);
2237 isl_int_set_si(graph
->lp
->ineq
[k
][0], max
);
2240 ineq
= isl_vec_alloc(ctx
, 1 + total
);
2241 ineq
= isl_vec_clr(ineq
);
2243 return isl_stat_error
;
2244 for (i
= 0; i
< node
->nvar
; ++i
) {
2245 int pos
= 1 + node_var_coef_offset(node
);
2247 if (isl_int_is_neg(node
->max
->el
[i
]))
2250 for (j
= 0; j
< node
->nvar
; ++j
) {
2251 isl_int_set(ineq
->el
[pos
+ 2 * j
],
2252 node
->cmap
->row
[i
][j
]);
2253 isl_int_neg(ineq
->el
[pos
+ 2 * j
+ 1],
2254 node
->cmap
->row
[i
][j
]);
2256 isl_int_set(ineq
->el
[0], node
->max
->el
[i
]);
2258 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2261 isl_seq_cpy(graph
->lp
->ineq
[k
], ineq
->el
, 1 + total
);
2263 isl_seq_neg(ineq
->el
+ pos
, ineq
->el
+ pos
, 2 * node
->nvar
);
2264 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2267 isl_seq_cpy(graph
->lp
->ineq
[k
], ineq
->el
, 1 + total
);
2274 return isl_stat_error
;
2277 /* Add constraints that bound the values of the variable and parameter
2278 * coefficients of the schedule.
2280 * The maximal value of the coefficients is defined by the option
2281 * 'schedule_max_coefficient' and the entries in node->max.
2282 * These latter entries are only set if either the schedule_max_coefficient
2283 * option or the schedule_treat_coalescing option is set.
2285 static isl_stat
add_bound_coefficient_constraints(isl_ctx
*ctx
,
2286 struct isl_sched_graph
*graph
)
2291 max
= isl_options_get_schedule_max_coefficient(ctx
);
2293 if (max
== -1 && !isl_options_get_schedule_treat_coalescing(ctx
))
2296 for (i
= 0; i
< graph
->n
; ++i
) {
2297 struct isl_sched_node
*node
= &graph
->node
[i
];
2299 if (node_add_coefficient_constraints(ctx
, graph
, node
, max
) < 0)
2300 return isl_stat_error
;
2306 /* Add a constraint to graph->lp that equates the value at position
2307 * "sum_pos" to the sum of the "n" values starting at "first".
2309 static isl_stat
add_sum_constraint(struct isl_sched_graph
*graph
,
2310 int sum_pos
, int first
, int n
)
2315 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2317 k
= isl_basic_set_alloc_equality(graph
->lp
);
2319 return isl_stat_error
;
2320 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2321 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2322 for (i
= 0; i
< n
; ++i
)
2323 isl_int_set_si(graph
->lp
->eq
[k
][1 + first
+ i
], 1);
2328 /* Add a constraint to graph->lp that equates the value at position
2329 * "sum_pos" to the sum of the parameter coefficients of all nodes.
2331 * Within each node, the coefficients have the following order:
2333 * - c_i_n (if parametric)
2334 * - positive and negative parts of c_i_x
2336 static isl_stat
add_param_sum_constraint(struct isl_sched_graph
*graph
,
2342 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2344 k
= isl_basic_set_alloc_equality(graph
->lp
);
2346 return isl_stat_error
;
2347 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2348 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2349 for (i
= 0; i
< graph
->n
; ++i
) {
2350 int pos
= 1 + graph
->node
[i
].start
+ 1;
2352 for (j
= 0; j
< graph
->node
[i
].nparam
; ++j
)
2353 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2359 /* Add a constraint to graph->lp that equates the value at position
2360 * "sum_pos" to the sum of the variable coefficients of all nodes.
2362 * Within each node, the coefficients have the following order:
2364 * - c_i_n (if parametric)
2365 * - positive and negative parts of c_i_x
2367 static isl_stat
add_var_sum_constraint(struct isl_sched_graph
*graph
,
2373 total
= isl_basic_set_dim(graph
->lp
, isl_dim_set
);
2375 k
= isl_basic_set_alloc_equality(graph
->lp
);
2377 return isl_stat_error
;
2378 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2379 isl_int_set_si(graph
->lp
->eq
[k
][1 + sum_pos
], -1);
2380 for (i
= 0; i
< graph
->n
; ++i
) {
2381 struct isl_sched_node
*node
= &graph
->node
[i
];
2382 int pos
= 1 + node_var_coef_offset(node
);
2384 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
2385 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2391 /* Construct an ILP problem for finding schedule coefficients
2392 * that result in non-negative, but small dependence distances
2393 * over all dependences.
2394 * In particular, the dependence distances over proximity edges
2395 * are bounded by m_0 + m_n n and we compute schedule coefficients
2396 * with small values (preferably zero) of m_n and m_0.
2398 * All variables of the ILP are non-negative. The actual coefficients
2399 * may be negative, so each coefficient is represented as the difference
2400 * of two non-negative variables. The negative part always appears
2401 * immediately before the positive part.
2402 * Other than that, the variables have the following order
2404 * - sum of positive and negative parts of m_n coefficients
2406 * - sum of all c_n coefficients
2407 * (unconstrained when computing non-parametric schedules)
2408 * - sum of positive and negative parts of all c_x coefficients
2409 * - positive and negative parts of m_n coefficients
2412 * - c_i_n (if parametric)
2413 * - positive and negative parts of c_i_x
2415 * The c_i_x are not represented directly, but through the columns of
2416 * node->cmap. That is, the computed values are for variable t_i_x
2417 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
2419 * The constraints are those from the edges plus two or three equalities
2420 * to express the sums.
2422 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2423 * Otherwise, we ignore them.
2425 static isl_stat
setup_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
2426 int use_coincidence
)
2436 parametric
= ctx
->opt
->schedule_parametric
;
2437 nparam
= isl_space_dim(graph
->node
[0].space
, isl_dim_param
);
2439 total
= param_pos
+ 2 * nparam
;
2440 for (i
= 0; i
< graph
->n
; ++i
) {
2441 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
2442 if (node_update_cmap(node
) < 0)
2443 return isl_stat_error
;
2444 node
->start
= total
;
2445 total
+= 1 + node
->nparam
+ 2 * node
->nvar
;
2448 if (count_constraints(graph
, &n_eq
, &n_ineq
, use_coincidence
) < 0)
2449 return isl_stat_error
;
2450 if (count_bound_constant_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2451 return isl_stat_error
;
2452 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2453 return isl_stat_error
;
2455 space
= isl_space_set_alloc(ctx
, 0, total
);
2456 isl_basic_set_free(graph
->lp
);
2457 n_eq
+= 2 + parametric
;
2459 graph
->lp
= isl_basic_set_alloc_space(space
, 0, n_eq
, n_ineq
);
2461 if (add_sum_constraint(graph
, 0, param_pos
, 2 * nparam
) < 0)
2462 return isl_stat_error
;
2463 if (parametric
&& add_param_sum_constraint(graph
, 2) < 0)
2464 return isl_stat_error
;
2465 if (add_var_sum_constraint(graph
, 3) < 0)
2466 return isl_stat_error
;
2467 if (add_bound_constant_constraints(ctx
, graph
) < 0)
2468 return isl_stat_error
;
2469 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
2470 return isl_stat_error
;
2471 if (add_all_validity_constraints(graph
, use_coincidence
) < 0)
2472 return isl_stat_error
;
2473 if (add_all_proximity_constraints(graph
, use_coincidence
) < 0)
2474 return isl_stat_error
;
2479 /* Analyze the conflicting constraint found by
2480 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2481 * constraint of one of the edges between distinct nodes, living, moreover
2482 * in distinct SCCs, then record the source and sink SCC as this may
2483 * be a good place to cut between SCCs.
2485 static int check_conflict(int con
, void *user
)
2488 struct isl_sched_graph
*graph
= user
;
2490 if (graph
->src_scc
>= 0)
2493 con
-= graph
->lp
->n_eq
;
2495 if (con
>= graph
->lp
->n_ineq
)
2498 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2499 if (!is_validity(&graph
->edge
[i
]))
2501 if (graph
->edge
[i
].src
== graph
->edge
[i
].dst
)
2503 if (graph
->edge
[i
].src
->scc
== graph
->edge
[i
].dst
->scc
)
2505 if (graph
->edge
[i
].start
> con
)
2507 if (graph
->edge
[i
].end
<= con
)
2509 graph
->src_scc
= graph
->edge
[i
].src
->scc
;
2510 graph
->dst_scc
= graph
->edge
[i
].dst
->scc
;
2516 /* Check whether the next schedule row of the given node needs to be
2517 * non-trivial. Lower-dimensional domains may have some trivial rows,
2518 * but as soon as the number of remaining required non-trivial rows
2519 * is as large as the number or remaining rows to be computed,
2520 * all remaining rows need to be non-trivial.
2522 static int needs_row(struct isl_sched_graph
*graph
, struct isl_sched_node
*node
)
2524 return node
->nvar
- node
->rank
>= graph
->maxvar
- graph
->n_row
;
2527 /* Solve the ILP problem constructed in setup_lp.
2528 * For each node such that all the remaining rows of its schedule
2529 * need to be non-trivial, we construct a non-triviality region.
2530 * This region imposes that the next row is independent of previous rows.
2531 * In particular the coefficients c_i_x are represented by t_i_x
2532 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
2533 * its first columns span the rows of the previously computed part
2534 * of the schedule. The non-triviality region enforces that at least
2535 * one of the remaining components of t_i_x is non-zero, i.e.,
2536 * that the new schedule row depends on at least one of the remaining
2539 static __isl_give isl_vec
*solve_lp(struct isl_sched_graph
*graph
)
2545 for (i
= 0; i
< graph
->n
; ++i
) {
2546 struct isl_sched_node
*node
= &graph
->node
[i
];
2547 int skip
= node
->rank
;
2548 graph
->region
[i
].pos
= node_var_coef_offset(node
) + 2 * skip
;
2549 if (needs_row(graph
, node
))
2550 graph
->region
[i
].len
= 2 * (node
->nvar
- skip
);
2552 graph
->region
[i
].len
= 0;
2554 lp
= isl_basic_set_copy(graph
->lp
);
2555 sol
= isl_tab_basic_set_non_trivial_lexmin(lp
, 2, graph
->n
,
2556 graph
->region
, &check_conflict
, graph
);
2560 /* Extract the coefficients for the variables of "node" from "sol".
2562 * Within each node, the coefficients have the following order:
2564 * - c_i_n (if parametric)
2565 * - positive and negative parts of c_i_x
2567 * The c_i_x^- appear before their c_i_x^+ counterpart.
2569 * Return c_i_x = c_i_x^+ - c_i_x^-
2571 static __isl_give isl_vec
*extract_var_coef(struct isl_sched_node
*node
,
2572 __isl_keep isl_vec
*sol
)
2580 csol
= isl_vec_alloc(isl_vec_get_ctx(sol
), node
->nvar
);
2584 pos
= 1 + node_var_coef_offset(node
);
2585 for (i
= 0; i
< node
->nvar
; ++i
)
2586 isl_int_sub(csol
->el
[i
],
2587 sol
->el
[pos
+ 2 * i
+ 1], sol
->el
[pos
+ 2 * i
]);
2592 /* Update the schedules of all nodes based on the given solution
2593 * of the LP problem.
2594 * The new row is added to the current band.
2595 * All possibly negative coefficients are encoded as a difference
2596 * of two non-negative variables, so we need to perform the subtraction
2597 * here. Moreover, if use_cmap is set, then the solution does
2598 * not refer to the actual coefficients c_i_x, but instead to variables
2599 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2600 * In this case, we then also need to perform this multiplication
2601 * to obtain the values of c_i_x.
2603 * If coincident is set, then the caller guarantees that the new
2604 * row satisfies the coincidence constraints.
2606 static int update_schedule(struct isl_sched_graph
*graph
,
2607 __isl_take isl_vec
*sol
, int use_cmap
, int coincident
)
2610 isl_vec
*csol
= NULL
;
2615 isl_die(sol
->ctx
, isl_error_internal
,
2616 "no solution found", goto error
);
2617 if (graph
->n_total_row
>= graph
->max_row
)
2618 isl_die(sol
->ctx
, isl_error_internal
,
2619 "too many schedule rows", goto error
);
2621 for (i
= 0; i
< graph
->n
; ++i
) {
2622 struct isl_sched_node
*node
= &graph
->node
[i
];
2623 int pos
= node
->start
;
2624 int row
= isl_mat_rows(node
->sched
);
2627 csol
= extract_var_coef(node
, sol
);
2631 isl_map_free(node
->sched_map
);
2632 node
->sched_map
= NULL
;
2633 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
2636 for (j
= 0; j
< 1 + node
->nparam
; ++j
)
2637 node
->sched
= isl_mat_set_element(node
->sched
,
2638 row
, j
, sol
->el
[1 + pos
+ j
]);
2640 csol
= isl_mat_vec_product(isl_mat_copy(node
->cmap
),
2644 for (j
= 0; j
< node
->nvar
; ++j
)
2645 node
->sched
= isl_mat_set_element(node
->sched
,
2646 row
, 1 + node
->nparam
+ j
, csol
->el
[j
]);
2647 node
->coincident
[graph
->n_total_row
] = coincident
;
2653 graph
->n_total_row
++;
2662 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2663 * and return this isl_aff.
2665 static __isl_give isl_aff
*extract_schedule_row(__isl_take isl_local_space
*ls
,
2666 struct isl_sched_node
*node
, int row
)
2674 aff
= isl_aff_zero_on_domain(ls
);
2675 isl_mat_get_element(node
->sched
, row
, 0, &v
);
2676 aff
= isl_aff_set_constant(aff
, v
);
2677 for (j
= 0; j
< node
->nparam
; ++j
) {
2678 isl_mat_get_element(node
->sched
, row
, 1 + j
, &v
);
2679 aff
= isl_aff_set_coefficient(aff
, isl_dim_param
, j
, v
);
2681 for (j
= 0; j
< node
->nvar
; ++j
) {
2682 isl_mat_get_element(node
->sched
, row
, 1 + node
->nparam
+ j
, &v
);
2683 aff
= isl_aff_set_coefficient(aff
, isl_dim_in
, j
, v
);
2691 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
2692 * and return this multi_aff.
2694 * The result is defined over the uncompressed node domain.
2696 static __isl_give isl_multi_aff
*node_extract_partial_schedule_multi_aff(
2697 struct isl_sched_node
*node
, int first
, int n
)
2701 isl_local_space
*ls
;
2708 nrow
= isl_mat_rows(node
->sched
);
2709 if (node
->compressed
)
2710 space
= isl_multi_aff_get_domain_space(node
->decompress
);
2712 space
= isl_space_copy(node
->space
);
2713 ls
= isl_local_space_from_space(isl_space_copy(space
));
2714 space
= isl_space_from_domain(space
);
2715 space
= isl_space_add_dims(space
, isl_dim_out
, n
);
2716 ma
= isl_multi_aff_zero(space
);
2718 for (i
= first
; i
< first
+ n
; ++i
) {
2719 aff
= extract_schedule_row(isl_local_space_copy(ls
), node
, i
);
2720 ma
= isl_multi_aff_set_aff(ma
, i
- first
, aff
);
2723 isl_local_space_free(ls
);
2725 if (node
->compressed
)
2726 ma
= isl_multi_aff_pullback_multi_aff(ma
,
2727 isl_multi_aff_copy(node
->compress
));
2732 /* Convert node->sched into a multi_aff and return this multi_aff.
2734 * The result is defined over the uncompressed node domain.
2736 static __isl_give isl_multi_aff
*node_extract_schedule_multi_aff(
2737 struct isl_sched_node
*node
)
2741 nrow
= isl_mat_rows(node
->sched
);
2742 return node_extract_partial_schedule_multi_aff(node
, 0, nrow
);
2745 /* Convert node->sched into a map and return this map.
2747 * The result is cached in node->sched_map, which needs to be released
2748 * whenever node->sched is updated.
2749 * It is defined over the uncompressed node domain.
2751 static __isl_give isl_map
*node_extract_schedule(struct isl_sched_node
*node
)
2753 if (!node
->sched_map
) {
2756 ma
= node_extract_schedule_multi_aff(node
);
2757 node
->sched_map
= isl_map_from_multi_aff(ma
);
2760 return isl_map_copy(node
->sched_map
);
2763 /* Construct a map that can be used to update a dependence relation
2764 * based on the current schedule.
2765 * That is, construct a map expressing that source and sink
2766 * are executed within the same iteration of the current schedule.
2767 * This map can then be intersected with the dependence relation.
2768 * This is not the most efficient way, but this shouldn't be a critical
2771 static __isl_give isl_map
*specializer(struct isl_sched_node
*src
,
2772 struct isl_sched_node
*dst
)
2774 isl_map
*src_sched
, *dst_sched
;
2776 src_sched
= node_extract_schedule(src
);
2777 dst_sched
= node_extract_schedule(dst
);
2778 return isl_map_apply_range(src_sched
, isl_map_reverse(dst_sched
));
2781 /* Intersect the domains of the nested relations in domain and range
2782 * of "umap" with "map".
2784 static __isl_give isl_union_map
*intersect_domains(
2785 __isl_take isl_union_map
*umap
, __isl_keep isl_map
*map
)
2787 isl_union_set
*uset
;
2789 umap
= isl_union_map_zip(umap
);
2790 uset
= isl_union_set_from_set(isl_map_wrap(isl_map_copy(map
)));
2791 umap
= isl_union_map_intersect_domain(umap
, uset
);
2792 umap
= isl_union_map_zip(umap
);
2796 /* Update the dependence relation of the given edge based
2797 * on the current schedule.
2798 * If the dependence is carried completely by the current schedule, then
2799 * it is removed from the edge_tables. It is kept in the list of edges
2800 * as otherwise all edge_tables would have to be recomputed.
2802 static int update_edge(struct isl_sched_graph
*graph
,
2803 struct isl_sched_edge
*edge
)
2808 id
= specializer(edge
->src
, edge
->dst
);
2809 edge
->map
= isl_map_intersect(edge
->map
, isl_map_copy(id
));
2813 if (edge
->tagged_condition
) {
2814 edge
->tagged_condition
=
2815 intersect_domains(edge
->tagged_condition
, id
);
2816 if (!edge
->tagged_condition
)
2819 if (edge
->tagged_validity
) {
2820 edge
->tagged_validity
=
2821 intersect_domains(edge
->tagged_validity
, id
);
2822 if (!edge
->tagged_validity
)
2826 empty
= isl_map_plain_is_empty(edge
->map
);
2830 graph_remove_edge(graph
, edge
);
2839 /* Does the domain of "umap" intersect "uset"?
2841 static int domain_intersects(__isl_keep isl_union_map
*umap
,
2842 __isl_keep isl_union_set
*uset
)
2846 umap
= isl_union_map_copy(umap
);
2847 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(uset
));
2848 empty
= isl_union_map_is_empty(umap
);
2849 isl_union_map_free(umap
);
2851 return empty
< 0 ? -1 : !empty
;
2854 /* Does the range of "umap" intersect "uset"?
2856 static int range_intersects(__isl_keep isl_union_map
*umap
,
2857 __isl_keep isl_union_set
*uset
)
2861 umap
= isl_union_map_copy(umap
);
2862 umap
= isl_union_map_intersect_range(umap
, isl_union_set_copy(uset
));
2863 empty
= isl_union_map_is_empty(umap
);
2864 isl_union_map_free(umap
);
2866 return empty
< 0 ? -1 : !empty
;
2869 /* Are the condition dependences of "edge" local with respect to
2870 * the current schedule?
2872 * That is, are domain and range of the condition dependences mapped
2873 * to the same point?
2875 * In other words, is the condition false?
2877 static int is_condition_false(struct isl_sched_edge
*edge
)
2879 isl_union_map
*umap
;
2880 isl_map
*map
, *sched
, *test
;
2883 empty
= isl_union_map_is_empty(edge
->tagged_condition
);
2884 if (empty
< 0 || empty
)
2887 umap
= isl_union_map_copy(edge
->tagged_condition
);
2888 umap
= isl_union_map_zip(umap
);
2889 umap
= isl_union_set_unwrap(isl_union_map_domain(umap
));
2890 map
= isl_map_from_union_map(umap
);
2892 sched
= node_extract_schedule(edge
->src
);
2893 map
= isl_map_apply_domain(map
, sched
);
2894 sched
= node_extract_schedule(edge
->dst
);
2895 map
= isl_map_apply_range(map
, sched
);
2897 test
= isl_map_identity(isl_map_get_space(map
));
2898 local
= isl_map_is_subset(map
, test
);
2905 /* For each conditional validity constraint that is adjacent
2906 * to a condition with domain in condition_source or range in condition_sink,
2907 * turn it into an unconditional validity constraint.
2909 static int unconditionalize_adjacent_validity(struct isl_sched_graph
*graph
,
2910 __isl_take isl_union_set
*condition_source
,
2911 __isl_take isl_union_set
*condition_sink
)
2915 condition_source
= isl_union_set_coalesce(condition_source
);
2916 condition_sink
= isl_union_set_coalesce(condition_sink
);
2918 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2920 isl_union_map
*validity
;
2922 if (!is_conditional_validity(&graph
->edge
[i
]))
2924 if (is_validity(&graph
->edge
[i
]))
2927 validity
= graph
->edge
[i
].tagged_validity
;
2928 adjacent
= domain_intersects(validity
, condition_sink
);
2929 if (adjacent
>= 0 && !adjacent
)
2930 adjacent
= range_intersects(validity
, condition_source
);
2936 set_validity(&graph
->edge
[i
]);
2939 isl_union_set_free(condition_source
);
2940 isl_union_set_free(condition_sink
);
2943 isl_union_set_free(condition_source
);
2944 isl_union_set_free(condition_sink
);
2948 /* Update the dependence relations of all edges based on the current schedule
2949 * and enforce conditional validity constraints that are adjacent
2950 * to satisfied condition constraints.
2952 * First check if any of the condition constraints are satisfied
2953 * (i.e., not local to the outer schedule) and keep track of
2954 * their domain and range.
2955 * Then update all dependence relations (which removes the non-local
2957 * Finally, if any condition constraints turned out to be satisfied,
2958 * then turn all adjacent conditional validity constraints into
2959 * unconditional validity constraints.
2961 static int update_edges(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2965 isl_union_set
*source
, *sink
;
2967 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
2968 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
2969 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2971 isl_union_set
*uset
;
2972 isl_union_map
*umap
;
2974 if (!is_condition(&graph
->edge
[i
]))
2976 if (is_local(&graph
->edge
[i
]))
2978 local
= is_condition_false(&graph
->edge
[i
]);
2986 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
2987 uset
= isl_union_map_domain(umap
);
2988 source
= isl_union_set_union(source
, uset
);
2990 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
2991 uset
= isl_union_map_range(umap
);
2992 sink
= isl_union_set_union(sink
, uset
);
2995 for (i
= graph
->n_edge
- 1; i
>= 0; --i
) {
2996 if (update_edge(graph
, &graph
->edge
[i
]) < 0)
3001 return unconditionalize_adjacent_validity(graph
, source
, sink
);
3003 isl_union_set_free(source
);
3004 isl_union_set_free(sink
);
3007 isl_union_set_free(source
);
3008 isl_union_set_free(sink
);
3012 static void next_band(struct isl_sched_graph
*graph
)
3014 graph
->band_start
= graph
->n_total_row
;
3017 /* Return the union of the universe domains of the nodes in "graph"
3018 * that satisfy "pred".
3020 static __isl_give isl_union_set
*isl_sched_graph_domain(isl_ctx
*ctx
,
3021 struct isl_sched_graph
*graph
,
3022 int (*pred
)(struct isl_sched_node
*node
, int data
), int data
)
3028 for (i
= 0; i
< graph
->n
; ++i
)
3029 if (pred(&graph
->node
[i
], data
))
3033 isl_die(ctx
, isl_error_internal
,
3034 "empty component", return NULL
);
3036 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
3037 dom
= isl_union_set_from_set(set
);
3039 for (i
= i
+ 1; i
< graph
->n
; ++i
) {
3040 if (!pred(&graph
->node
[i
], data
))
3042 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
3043 dom
= isl_union_set_union(dom
, isl_union_set_from_set(set
));
3049 /* Return a list of unions of universe domains, where each element
3050 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
3052 static __isl_give isl_union_set_list
*extract_sccs(isl_ctx
*ctx
,
3053 struct isl_sched_graph
*graph
)
3056 isl_union_set_list
*filters
;
3058 filters
= isl_union_set_list_alloc(ctx
, graph
->scc
);
3059 for (i
= 0; i
< graph
->scc
; ++i
) {
3062 dom
= isl_sched_graph_domain(ctx
, graph
, &node_scc_exactly
, i
);
3063 filters
= isl_union_set_list_add(filters
, dom
);
3069 /* Return a list of two unions of universe domains, one for the SCCs up
3070 * to and including graph->src_scc and another for the other SCCs.
3072 static __isl_give isl_union_set_list
*extract_split(isl_ctx
*ctx
,
3073 struct isl_sched_graph
*graph
)
3076 isl_union_set_list
*filters
;
3078 filters
= isl_union_set_list_alloc(ctx
, 2);
3079 dom
= isl_sched_graph_domain(ctx
, graph
,
3080 &node_scc_at_most
, graph
->src_scc
);
3081 filters
= isl_union_set_list_add(filters
, dom
);
3082 dom
= isl_sched_graph_domain(ctx
, graph
,
3083 &node_scc_at_least
, graph
->src_scc
+ 1);
3084 filters
= isl_union_set_list_add(filters
, dom
);
3089 /* Copy nodes that satisfy node_pred from the src dependence graph
3090 * to the dst dependence graph.
3092 static int copy_nodes(struct isl_sched_graph
*dst
, struct isl_sched_graph
*src
,
3093 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
3098 for (i
= 0; i
< src
->n
; ++i
) {
3101 if (!node_pred(&src
->node
[i
], data
))
3105 dst
->node
[j
].space
= isl_space_copy(src
->node
[i
].space
);
3106 dst
->node
[j
].compressed
= src
->node
[i
].compressed
;
3107 dst
->node
[j
].hull
= isl_set_copy(src
->node
[i
].hull
);
3108 dst
->node
[j
].compress
=
3109 isl_multi_aff_copy(src
->node
[i
].compress
);
3110 dst
->node
[j
].decompress
=
3111 isl_multi_aff_copy(src
->node
[i
].decompress
);
3112 dst
->node
[j
].nvar
= src
->node
[i
].nvar
;
3113 dst
->node
[j
].nparam
= src
->node
[i
].nparam
;
3114 dst
->node
[j
].sched
= isl_mat_copy(src
->node
[i
].sched
);
3115 dst
->node
[j
].sched_map
= isl_map_copy(src
->node
[i
].sched_map
);
3116 dst
->node
[j
].coincident
= src
->node
[i
].coincident
;
3117 dst
->node
[j
].sizes
= isl_multi_val_copy(src
->node
[i
].sizes
);
3118 dst
->node
[j
].max
= isl_vec_copy(src
->node
[i
].max
);
3121 if (!dst
->node
[j
].space
|| !dst
->node
[j
].sched
)
3123 if (dst
->node
[j
].compressed
&&
3124 (!dst
->node
[j
].hull
|| !dst
->node
[j
].compress
||
3125 !dst
->node
[j
].decompress
))
3132 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
3133 * to the dst dependence graph.
3134 * If the source or destination node of the edge is not in the destination
3135 * graph, then it must be a backward proximity edge and it should simply
3138 static int copy_edges(isl_ctx
*ctx
, struct isl_sched_graph
*dst
,
3139 struct isl_sched_graph
*src
,
3140 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
), int data
)
3143 enum isl_edge_type t
;
3146 for (i
= 0; i
< src
->n_edge
; ++i
) {
3147 struct isl_sched_edge
*edge
= &src
->edge
[i
];
3149 isl_union_map
*tagged_condition
;
3150 isl_union_map
*tagged_validity
;
3151 struct isl_sched_node
*dst_src
, *dst_dst
;
3153 if (!edge_pred(edge
, data
))
3156 if (isl_map_plain_is_empty(edge
->map
))
3159 dst_src
= graph_find_node(ctx
, dst
, edge
->src
->space
);
3160 dst_dst
= graph_find_node(ctx
, dst
, edge
->dst
->space
);
3161 if (!dst_src
|| !dst_dst
) {
3162 if (is_validity(edge
) || is_conditional_validity(edge
))
3163 isl_die(ctx
, isl_error_internal
,
3164 "backward (conditional) validity edge",
3169 map
= isl_map_copy(edge
->map
);
3170 tagged_condition
= isl_union_map_copy(edge
->tagged_condition
);
3171 tagged_validity
= isl_union_map_copy(edge
->tagged_validity
);
3173 dst
->edge
[dst
->n_edge
].src
= dst_src
;
3174 dst
->edge
[dst
->n_edge
].dst
= dst_dst
;
3175 dst
->edge
[dst
->n_edge
].map
= map
;
3176 dst
->edge
[dst
->n_edge
].tagged_condition
= tagged_condition
;
3177 dst
->edge
[dst
->n_edge
].tagged_validity
= tagged_validity
;
3178 dst
->edge
[dst
->n_edge
].types
= edge
->types
;
3181 if (edge
->tagged_condition
&& !tagged_condition
)
3183 if (edge
->tagged_validity
&& !tagged_validity
)
3186 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
3188 graph_find_edge(src
, t
, edge
->src
, edge
->dst
))
3190 if (graph_edge_table_add(ctx
, dst
, t
,
3191 &dst
->edge
[dst
->n_edge
- 1]) < 0)
3199 /* Compute the maximal number of variables over all nodes.
3200 * This is the maximal number of linearly independent schedule
3201 * rows that we need to compute.
3202 * Just in case we end up in a part of the dependence graph
3203 * with only lower-dimensional domains, we make sure we will
3204 * compute the required amount of extra linearly independent rows.
3206 static int compute_maxvar(struct isl_sched_graph
*graph
)
3211 for (i
= 0; i
< graph
->n
; ++i
) {
3212 struct isl_sched_node
*node
= &graph
->node
[i
];
3215 if (node_update_cmap(node
) < 0)
3217 nvar
= node
->nvar
+ graph
->n_row
- node
->rank
;
3218 if (nvar
> graph
->maxvar
)
3219 graph
->maxvar
= nvar
;
3225 /* Extract the subgraph of "graph" that consists of the node satisfying
3226 * "node_pred" and the edges satisfying "edge_pred" and store
3227 * the result in "sub".
3229 static int extract_sub_graph(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
3230 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
3231 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
3232 int data
, struct isl_sched_graph
*sub
)
3234 int i
, n
= 0, n_edge
= 0;
3237 for (i
= 0; i
< graph
->n
; ++i
)
3238 if (node_pred(&graph
->node
[i
], data
))
3240 for (i
= 0; i
< graph
->n_edge
; ++i
)
3241 if (edge_pred(&graph
->edge
[i
], data
))
3243 if (graph_alloc(ctx
, sub
, n
, n_edge
) < 0)
3245 if (copy_nodes(sub
, graph
, node_pred
, data
) < 0)
3247 if (graph_init_table(ctx
, sub
) < 0)
3249 for (t
= 0; t
<= isl_edge_last
; ++t
)
3250 sub
->max_edge
[t
] = graph
->max_edge
[t
];
3251 if (graph_init_edge_tables(ctx
, sub
) < 0)
3253 if (copy_edges(ctx
, sub
, graph
, edge_pred
, data
) < 0)
3255 sub
->n_row
= graph
->n_row
;
3256 sub
->max_row
= graph
->max_row
;
3257 sub
->n_total_row
= graph
->n_total_row
;
3258 sub
->band_start
= graph
->band_start
;
3263 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
3264 struct isl_sched_graph
*graph
);
3265 static __isl_give isl_schedule_node
*compute_schedule_wcc(
3266 isl_schedule_node
*node
, struct isl_sched_graph
*graph
);
3268 /* Compute a schedule for a subgraph of "graph". In particular, for
3269 * the graph composed of nodes that satisfy node_pred and edges that
3270 * that satisfy edge_pred.
3271 * If the subgraph is known to consist of a single component, then wcc should
3272 * be set and then we call compute_schedule_wcc on the constructed subgraph.
3273 * Otherwise, we call compute_schedule, which will check whether the subgraph
3276 * The schedule is inserted at "node" and the updated schedule node
3279 static __isl_give isl_schedule_node
*compute_sub_schedule(
3280 __isl_take isl_schedule_node
*node
, isl_ctx
*ctx
,
3281 struct isl_sched_graph
*graph
,
3282 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
3283 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
3286 struct isl_sched_graph split
= { 0 };
3288 if (extract_sub_graph(ctx
, graph
, node_pred
, edge_pred
, data
,
3293 node
= compute_schedule_wcc(node
, &split
);
3295 node
= compute_schedule(node
, &split
);
3297 graph_free(ctx
, &split
);
3300 graph_free(ctx
, &split
);
3301 return isl_schedule_node_free(node
);
3304 static int edge_scc_exactly(struct isl_sched_edge
*edge
, int scc
)
3306 return edge
->src
->scc
== scc
&& edge
->dst
->scc
== scc
;
3309 static int edge_dst_scc_at_most(struct isl_sched_edge
*edge
, int scc
)
3311 return edge
->dst
->scc
<= scc
;
3314 static int edge_src_scc_at_least(struct isl_sched_edge
*edge
, int scc
)
3316 return edge
->src
->scc
>= scc
;
3319 /* Reset the current band by dropping all its schedule rows.
3321 static int reset_band(struct isl_sched_graph
*graph
)
3326 drop
= graph
->n_total_row
- graph
->band_start
;
3327 graph
->n_total_row
-= drop
;
3328 graph
->n_row
-= drop
;
3330 for (i
= 0; i
< graph
->n
; ++i
) {
3331 struct isl_sched_node
*node
= &graph
->node
[i
];
3333 isl_map_free(node
->sched_map
);
3334 node
->sched_map
= NULL
;
3336 node
->sched
= isl_mat_drop_rows(node
->sched
,
3337 graph
->band_start
, drop
);
3346 /* Split the current graph into two parts and compute a schedule for each
3347 * part individually. In particular, one part consists of all SCCs up
3348 * to and including graph->src_scc, while the other part contains the other
3349 * SCCs. The split is enforced by a sequence node inserted at position "node"
3350 * in the schedule tree. Return the updated schedule node.
3351 * If either of these two parts consists of a sequence, then it is spliced
3352 * into the sequence containing the two parts.
3354 * The current band is reset. It would be possible to reuse
3355 * the previously computed rows as the first rows in the next
3356 * band, but recomputing them may result in better rows as we are looking
3357 * at a smaller part of the dependence graph.
3359 static __isl_give isl_schedule_node
*compute_split_schedule(
3360 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3364 isl_union_set_list
*filters
;
3369 if (reset_band(graph
) < 0)
3370 return isl_schedule_node_free(node
);
3374 ctx
= isl_schedule_node_get_ctx(node
);
3375 filters
= extract_split(ctx
, graph
);
3376 node
= isl_schedule_node_insert_sequence(node
, filters
);
3377 node
= isl_schedule_node_child(node
, 1);
3378 node
= isl_schedule_node_child(node
, 0);
3380 node
= compute_sub_schedule(node
, ctx
, graph
,
3381 &node_scc_at_least
, &edge_src_scc_at_least
,
3382 graph
->src_scc
+ 1, 0);
3383 is_seq
= isl_schedule_node_get_type(node
) == isl_schedule_node_sequence
;
3384 node
= isl_schedule_node_parent(node
);
3385 node
= isl_schedule_node_parent(node
);
3387 node
= isl_schedule_node_sequence_splice_child(node
, 1);
3388 node
= isl_schedule_node_child(node
, 0);
3389 node
= isl_schedule_node_child(node
, 0);
3390 node
= compute_sub_schedule(node
, ctx
, graph
,
3391 &node_scc_at_most
, &edge_dst_scc_at_most
,
3393 is_seq
= isl_schedule_node_get_type(node
) == isl_schedule_node_sequence
;
3394 node
= isl_schedule_node_parent(node
);
3395 node
= isl_schedule_node_parent(node
);
3397 node
= isl_schedule_node_sequence_splice_child(node
, 0);
3402 /* Insert a band node at position "node" in the schedule tree corresponding
3403 * to the current band in "graph". Mark the band node permutable
3404 * if "permutable" is set.
3405 * The partial schedules and the coincidence property are extracted
3406 * from the graph nodes.
3407 * Return the updated schedule node.
3409 static __isl_give isl_schedule_node
*insert_current_band(
3410 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
3416 isl_multi_pw_aff
*mpa
;
3417 isl_multi_union_pw_aff
*mupa
;
3423 isl_die(isl_schedule_node_get_ctx(node
), isl_error_internal
,
3424 "graph should have at least one node",
3425 return isl_schedule_node_free(node
));
3427 start
= graph
->band_start
;
3428 end
= graph
->n_total_row
;
3431 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[0], start
, n
);
3432 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3433 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3435 for (i
= 1; i
< graph
->n
; ++i
) {
3436 isl_multi_union_pw_aff
*mupa_i
;
3438 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[i
],
3440 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3441 mupa_i
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3442 mupa
= isl_multi_union_pw_aff_union_add(mupa
, mupa_i
);
3444 node
= isl_schedule_node_insert_partial_schedule(node
, mupa
);
3446 for (i
= 0; i
< n
; ++i
)
3447 node
= isl_schedule_node_band_member_set_coincident(node
, i
,
3448 graph
->node
[0].coincident
[start
+ i
]);
3449 node
= isl_schedule_node_band_set_permutable(node
, permutable
);
3454 /* Update the dependence relations based on the current schedule,
3455 * add the current band to "node" and then continue with the computation
3457 * Return the updated schedule node.
3459 static __isl_give isl_schedule_node
*compute_next_band(
3460 __isl_take isl_schedule_node
*node
,
3461 struct isl_sched_graph
*graph
, int permutable
)
3468 ctx
= isl_schedule_node_get_ctx(node
);
3469 if (update_edges(ctx
, graph
) < 0)
3470 return isl_schedule_node_free(node
);
3471 node
= insert_current_band(node
, graph
, permutable
);
3474 node
= isl_schedule_node_child(node
, 0);
3475 node
= compute_schedule(node
, graph
);
3476 node
= isl_schedule_node_parent(node
);
3481 /* Add constraints to graph->lp that force the dependence "map" (which
3482 * is part of the dependence relation of "edge")
3483 * to be respected and attempt to carry it, where the edge is one from
3484 * a node j to itself. "pos" is the sequence number of the given map.
3485 * That is, add constraints that enforce
3487 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3488 * = c_j_x (y - x) >= e_i
3490 * for each (x,y) in R.
3491 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3492 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3493 * with each coefficient in c_j_x represented as a pair of non-negative
3496 static int add_intra_constraints(struct isl_sched_graph
*graph
,
3497 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
3500 isl_ctx
*ctx
= isl_map_get_ctx(map
);
3501 isl_dim_map
*dim_map
;
3502 isl_basic_set
*coef
;
3503 struct isl_sched_node
*node
= edge
->src
;
3505 coef
= intra_coefficients(graph
, node
, map
);
3509 offset
= coef_var_offset(coef
);
3510 dim_map
= intra_dim_map(ctx
, graph
, node
, offset
, 1);
3511 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3512 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
3517 /* Add constraints to graph->lp that force the dependence "map" (which
3518 * is part of the dependence relation of "edge")
3519 * to be respected and attempt to carry it, where the edge is one from
3520 * node j to node k. "pos" is the sequence number of the given map.
3521 * That is, add constraints that enforce
3523 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3525 * for each (x,y) in R.
3526 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
3527 * of valid constraints for R and then plug in
3528 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, -c_j_x, c_k_x)
3529 * with each coefficient (except e_i, c_*_0 and c_*_n)
3530 * represented as a pair of non-negative coefficients.
3532 static int add_inter_constraints(struct isl_sched_graph
*graph
,
3533 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
3536 isl_ctx
*ctx
= isl_map_get_ctx(map
);
3537 isl_dim_map
*dim_map
;
3538 isl_basic_set
*coef
;
3539 struct isl_sched_node
*src
= edge
->src
;
3540 struct isl_sched_node
*dst
= edge
->dst
;
3542 coef
= inter_coefficients(graph
, edge
, map
);
3546 offset
= coef_var_offset(coef
);
3547 dim_map
= inter_dim_map(ctx
, graph
, src
, dst
, offset
, 1);
3548 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3549 graph
->lp
= add_constraints_dim_map(graph
->lp
, coef
, dim_map
);
3554 /* Add constraints to graph->lp that force all (conditional) validity
3555 * dependences to be respected and attempt to carry them.
3557 static isl_stat
add_all_constraints(struct isl_sched_graph
*graph
)
3563 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3564 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
3566 if (!is_any_validity(edge
))
3569 for (j
= 0; j
< edge
->map
->n
; ++j
) {
3570 isl_basic_map
*bmap
;
3573 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
3574 map
= isl_map_from_basic_map(bmap
);
3576 if (edge
->src
== edge
->dst
&&
3577 add_intra_constraints(graph
, edge
, map
, pos
) < 0)
3578 return isl_stat_error
;
3579 if (edge
->src
!= edge
->dst
&&
3580 add_inter_constraints(graph
, edge
, map
, pos
) < 0)
3581 return isl_stat_error
;
3589 /* Count the number of equality and inequality constraints
3590 * that will be added to the carry_lp problem.
3591 * We count each edge exactly once.
3593 static isl_stat
count_all_constraints(struct isl_sched_graph
*graph
,
3594 int *n_eq
, int *n_ineq
)
3598 *n_eq
= *n_ineq
= 0;
3599 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3600 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
3602 if (!is_any_validity(edge
))
3605 for (j
= 0; j
< edge
->map
->n
; ++j
) {
3606 isl_basic_map
*bmap
;
3609 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
3610 map
= isl_map_from_basic_map(bmap
);
3612 if (count_map_constraints(graph
, edge
, map
,
3613 n_eq
, n_ineq
, 1, 0) < 0)
3614 return isl_stat_error
;
3621 /* Return the total number of (validity) edges that carry_dependences will
3624 static int count_carry_edges(struct isl_sched_graph
*graph
)
3630 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3631 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
3633 if (!is_any_validity(edge
))
3636 n_edge
+= isl_map_n_basic_map(edge
->map
);
3642 /* Construct an LP problem for finding schedule coefficients
3643 * such that the schedule carries as many validity dependences as possible.
3644 * In particular, for each dependence i, we bound the dependence distance
3645 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3646 * of all e_i's. Dependences with e_i = 0 in the solution are simply
3647 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3648 * Note that if the dependence relation is a union of basic maps,
3649 * then we have to consider each basic map individually as it may only
3650 * be possible to carry the dependences expressed by some of those
3651 * basic maps and not all of them.
3652 * Below, we consider each of those basic maps as a separate "edge".
3653 * "n_edge" is the number of these edges.
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
,
3685 for (i
= 0; i
< graph
->n
; ++i
) {
3686 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
3687 node
->start
= total
;
3688 total
+= 1 + node
->nparam
+ 2 * node
->nvar
;
3691 if (count_all_constraints(graph
, &n_eq
, &n_ineq
) < 0)
3692 return isl_stat_error
;
3694 dim
= isl_space_set_alloc(ctx
, 0, total
);
3695 isl_basic_set_free(graph
->lp
);
3698 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
3699 graph
->lp
= isl_basic_set_set_rational(graph
->lp
);
3701 k
= isl_basic_set_alloc_equality(graph
->lp
);
3703 return isl_stat_error
;
3704 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
3705 isl_int_set_si(graph
->lp
->eq
[k
][0], -n_edge
);
3706 isl_int_set_si(graph
->lp
->eq
[k
][1], 1);
3707 for (i
= 0; i
< n_edge
; ++i
)
3708 isl_int_set_si(graph
->lp
->eq
[k
][4 + i
], 1);
3710 if (add_param_sum_constraint(graph
, 1) < 0)
3711 return isl_stat_error
;
3712 if (add_var_sum_constraint(graph
, 2) < 0)
3713 return isl_stat_error
;
3715 for (i
= 0; i
< n_edge
; ++i
) {
3716 k
= isl_basic_set_alloc_inequality(graph
->lp
);
3718 return isl_stat_error
;
3719 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
3720 isl_int_set_si(graph
->lp
->ineq
[k
][4 + i
], -1);
3721 isl_int_set_si(graph
->lp
->ineq
[k
][0], 1);
3724 if (add_all_constraints(graph
) < 0)
3725 return isl_stat_error
;
3730 static __isl_give isl_schedule_node
*compute_component_schedule(
3731 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
3734 /* Comparison function for sorting the statements based on
3735 * the corresponding value in "r".
3737 static int smaller_value(const void *a
, const void *b
, void *data
)
3743 return isl_int_cmp(r
->el
[*i1
], r
->el
[*i2
]);
3746 /* If the schedule_split_scaled option is set and if the linear
3747 * parts of the scheduling rows for all nodes in the graphs have
3748 * a non-trivial common divisor, then split off the remainder of the
3749 * constant term modulo this common divisor from the linear part.
3750 * Otherwise, insert a band node directly and continue with
3751 * the construction of the schedule.
3753 * If a non-trivial common divisor is found, then
3754 * the linear part is reduced and the remainder is enforced
3755 * by a sequence node with the children placed in the order
3756 * of this remainder.
3757 * In particular, we assign an scc index based on the remainder and
3758 * then rely on compute_component_schedule to insert the sequence and
3759 * to continue the schedule construction on each part.
3761 static __isl_give isl_schedule_node
*split_scaled(
3762 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3775 ctx
= isl_schedule_node_get_ctx(node
);
3776 if (!ctx
->opt
->schedule_split_scaled
)
3777 return compute_next_band(node
, graph
, 0);
3779 return compute_next_band(node
, graph
, 0);
3782 isl_int_init(gcd_i
);
3784 isl_int_set_si(gcd
, 0);
3786 row
= isl_mat_rows(graph
->node
[0].sched
) - 1;
3788 for (i
= 0; i
< graph
->n
; ++i
) {
3789 struct isl_sched_node
*node
= &graph
->node
[i
];
3790 int cols
= isl_mat_cols(node
->sched
);
3792 isl_seq_gcd(node
->sched
->row
[row
] + 1, cols
- 1, &gcd_i
);
3793 isl_int_gcd(gcd
, gcd
, gcd_i
);
3796 isl_int_clear(gcd_i
);
3798 if (isl_int_cmp_si(gcd
, 1) <= 0) {
3800 return compute_next_band(node
, graph
, 0);
3803 r
= isl_vec_alloc(ctx
, graph
->n
);
3804 order
= isl_calloc_array(ctx
, int, graph
->n
);
3808 for (i
= 0; i
< graph
->n
; ++i
) {
3809 struct isl_sched_node
*node
= &graph
->node
[i
];
3812 isl_int_fdiv_r(r
->el
[i
], node
->sched
->row
[row
][0], gcd
);
3813 isl_int_fdiv_q(node
->sched
->row
[row
][0],
3814 node
->sched
->row
[row
][0], gcd
);
3815 isl_int_mul(node
->sched
->row
[row
][0],
3816 node
->sched
->row
[row
][0], gcd
);
3817 node
->sched
= isl_mat_scale_down_row(node
->sched
, row
, gcd
);
3822 if (isl_sort(order
, graph
->n
, sizeof(order
[0]), &smaller_value
, r
) < 0)
3826 for (i
= 0; i
< graph
->n
; ++i
) {
3827 if (i
> 0 && isl_int_ne(r
->el
[order
[i
- 1]], r
->el
[order
[i
]]))
3829 graph
->node
[order
[i
]].scc
= scc
;
3838 if (update_edges(ctx
, graph
) < 0)
3839 return isl_schedule_node_free(node
);
3840 node
= insert_current_band(node
, graph
, 0);
3843 node
= isl_schedule_node_child(node
, 0);
3844 node
= compute_component_schedule(node
, graph
, 0);
3845 node
= isl_schedule_node_parent(node
);
3852 return isl_schedule_node_free(node
);
3855 /* Is the schedule row "sol" trivial on node "node"?
3856 * That is, is the solution zero on the dimensions linearly independent of
3857 * the previously found solutions?
3858 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3860 * Each coefficient is represented as the difference between
3861 * two non-negative values in "sol". "sol" has been computed
3862 * in terms of the original iterators (i.e., without use of cmap).
3863 * We construct the schedule row s and write it as a linear
3864 * combination of (linear combinations of) previously computed schedule rows.
3865 * s = Q c or c = U s.
3866 * If the final entries of c are all zero, then the solution is trivial.
3868 static int is_trivial(struct isl_sched_node
*node
, __isl_keep isl_vec
*sol
)
3875 if (node
->nvar
== node
->rank
)
3878 node_sol
= extract_var_coef(node
, sol
);
3879 node_sol
= isl_mat_vec_product(isl_mat_copy(node
->cinv
), node_sol
);
3883 trivial
= isl_seq_first_non_zero(node_sol
->el
+ node
->rank
,
3884 node
->nvar
- node
->rank
) == -1;
3886 isl_vec_free(node_sol
);
3891 /* Is the schedule row "sol" trivial on any node where it should
3893 * "sol" has been computed in terms of the original iterators
3894 * (i.e., without use of cmap).
3895 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3897 static int is_any_trivial(struct isl_sched_graph
*graph
,
3898 __isl_keep isl_vec
*sol
)
3902 for (i
= 0; i
< graph
->n
; ++i
) {
3903 struct isl_sched_node
*node
= &graph
->node
[i
];
3906 if (!needs_row(graph
, node
))
3908 trivial
= is_trivial(node
, sol
);
3909 if (trivial
< 0 || trivial
)
3916 /* Does the schedule represented by "sol" perform loop coalescing on "node"?
3917 * If so, return the position of the coalesced dimension.
3918 * Otherwise, return node->nvar or -1 on error.
3920 * In particular, look for pairs of coefficients c_i and c_j such that
3921 * |c_j/c_i| >= size_i, i.e., |c_j| >= |c_i * size_i|.
3922 * If any such pair is found, then return i.
3923 * If size_i is infinity, then no check on c_i needs to be performed.
3925 static int find_node_coalescing(struct isl_sched_node
*node
,
3926 __isl_keep isl_vec
*sol
)
3932 if (node
->nvar
<= 1)
3935 csol
= extract_var_coef(node
, sol
);
3939 for (i
= 0; i
< node
->nvar
; ++i
) {
3942 if (isl_int_is_zero(csol
->el
[i
]))
3944 v
= isl_multi_val_get_val(node
->sizes
, i
);
3947 if (!isl_val_is_int(v
)) {
3951 isl_int_mul(max
, v
->n
, csol
->el
[i
]);
3954 for (j
= 0; j
< node
->nvar
; ++j
) {
3957 if (isl_int_abs_ge(csol
->el
[j
], max
))
3973 /* Force the schedule coefficient at position "pos" of "node" to be zero
3975 * The coefficient is encoded as the difference between two non-negative
3976 * variables. Force these two variables to have the same value.
3978 static __isl_give isl_tab_lexmin
*zero_out_node_coef(
3979 __isl_take isl_tab_lexmin
*tl
, struct isl_sched_node
*node
, int pos
)
3985 ctx
= isl_space_get_ctx(node
->space
);
3986 dim
= isl_tab_lexmin_dim(tl
);
3988 return isl_tab_lexmin_free(tl
);
3989 eq
= isl_vec_alloc(ctx
, 1 + dim
);
3990 eq
= isl_vec_clr(eq
);
3992 return isl_tab_lexmin_free(tl
);
3994 pos
= 1 + node_var_coef_offset(node
) + 2 * pos
;
3995 isl_int_set_si(eq
->el
[pos
], 1);
3996 isl_int_set_si(eq
->el
[pos
+ 1], -1);
3997 tl
= isl_tab_lexmin_add_eq(tl
, eq
->el
);
4003 /* Return the lexicographically smallest rational point in the basic set
4004 * from which "tl" was constructed, double checking that this input set
4007 static __isl_give isl_vec
*non_empty_solution(__isl_keep isl_tab_lexmin
*tl
)
4011 sol
= isl_tab_lexmin_get_solution(tl
);
4015 isl_die(isl_vec_get_ctx(sol
), isl_error_internal
,
4016 "error in schedule construction",
4017 return isl_vec_free(sol
));
4021 /* Does the solution "sol" of the LP problem constructed by setup_carry_lp
4022 * carry any of the "n_edge" groups of dependences?
4023 * The value in the first position is the sum of (1 - e_i) over all "n_edge"
4024 * edges, with 0 <= e_i <= 1 equal to 1 when the dependences represented
4025 * by the edge are carried by the solution.
4026 * If the sum of the (1 - e_i) is smaller than "n_edge" then at least
4027 * one of those is carried.
4029 * Note that despite the fact that the problem is solved using a rational
4030 * solver, the solution is guaranteed to be integral.
4031 * Specifically, the dependence distance lower bounds e_i (and therefore
4032 * also their sum) are integers. See Lemma 5 of [1].
4034 * Any potential denominator of the sum is cleared by this function.
4035 * The denominator is not relevant for any of the other elements
4038 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4039 * Problem, Part II: Multi-Dimensional Time.
4040 * In Intl. Journal of Parallel Programming, 1992.
4042 static int carries_dependences(__isl_keep isl_vec
*sol
, int n_edge
)
4044 isl_int_divexact(sol
->el
[1], sol
->el
[1], sol
->el
[0]);
4045 isl_int_set_si(sol
->el
[0], 1);
4046 return isl_int_cmp_si(sol
->el
[1], n_edge
) < 0;
4049 /* Return the lexicographically smallest rational point in "lp",
4050 * assuming that all variables are non-negative and performing some
4051 * additional sanity checks.
4052 * In particular, "lp" should not be empty by construction.
4053 * Double check that this is the case.
4054 * Also, check that dependences are carried for at least one of
4055 * the "n_edge" edges.
4057 * If the schedule_treat_coalescing option is set and
4058 * if the computed schedule performs loop coalescing on a given node,
4059 * i.e., if it is of the form
4061 * c_i i + c_j j + ...
4063 * with |c_j/c_i| >= size_i, then force the coefficient c_i to be zero
4064 * to cut out this solution. Repeat this process until no more loop
4065 * coalescing occurs or until no more dependences can be carried.
4066 * In the latter case, revert to the previously computed solution.
4068 static __isl_give isl_vec
*non_neg_lexmin(struct isl_sched_graph
*graph
,
4069 __isl_take isl_basic_set
*lp
, int n_edge
)
4074 isl_vec
*sol
, *prev
= NULL
;
4075 int treat_coalescing
;
4079 ctx
= isl_basic_set_get_ctx(lp
);
4080 treat_coalescing
= isl_options_get_schedule_treat_coalescing(ctx
);
4081 tl
= isl_tab_lexmin_from_basic_set(lp
);
4084 sol
= non_empty_solution(tl
);
4088 if (!carries_dependences(sol
, n_edge
)) {
4090 isl_die(ctx
, isl_error_unknown
,
4091 "unable to carry dependences",
4097 prev
= isl_vec_free(prev
);
4098 if (!treat_coalescing
)
4100 for (i
= 0; i
< graph
->n
; ++i
) {
4101 struct isl_sched_node
*node
= &graph
->node
[i
];
4103 pos
= find_node_coalescing(node
, sol
);
4106 if (pos
< node
->nvar
)
4111 tl
= zero_out_node_coef(tl
, &graph
->node
[i
], pos
);
4113 } while (i
< graph
->n
);
4115 isl_tab_lexmin_free(tl
);
4119 isl_tab_lexmin_free(tl
);
4125 /* Construct a schedule row for each node such that as many validity dependences
4126 * as possible are carried and then continue with the next band.
4128 * If there are no validity dependences, then no dependence can be carried and
4129 * the procedure is guaranteed to fail. If there is more than one component,
4130 * then try computing a schedule on each component separately
4131 * to prevent or at least postpone this failure.
4133 * If the computed schedule row turns out to be trivial on one or
4134 * more nodes where it should not be trivial, then we throw it away
4135 * and try again on each component separately.
4137 * If there is only one component, then we accept the schedule row anyway,
4138 * but we do not consider it as a complete row and therefore do not
4139 * increment graph->n_row. Note that the ranks of the nodes that
4140 * do get a non-trivial schedule part will get updated regardless and
4141 * graph->maxvar is computed based on these ranks. The test for
4142 * whether more schedule rows are required in compute_schedule_wcc
4143 * is therefore not affected.
4145 * Insert a band corresponding to the schedule row at position "node"
4146 * of the schedule tree and continue with the construction of the schedule.
4147 * This insertion and the continued construction is performed by split_scaled
4148 * after optionally checking for non-trivial common divisors.
4150 static __isl_give isl_schedule_node
*carry_dependences(
4151 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4162 n_edge
= count_carry_edges(graph
);
4163 if (n_edge
== 0 && graph
->scc
> 1)
4164 return compute_component_schedule(node
, graph
, 1);
4166 ctx
= isl_schedule_node_get_ctx(node
);
4167 if (setup_carry_lp(ctx
, graph
, n_edge
) < 0)
4168 return isl_schedule_node_free(node
);
4170 lp
= isl_basic_set_copy(graph
->lp
);
4171 sol
= non_neg_lexmin(graph
, lp
, n_edge
);
4173 return isl_schedule_node_free(node
);
4175 trivial
= is_any_trivial(graph
, sol
);
4177 sol
= isl_vec_free(sol
);
4178 } else if (trivial
&& graph
->scc
> 1) {
4180 return compute_component_schedule(node
, graph
, 1);
4183 if (update_schedule(graph
, sol
, 0, 0) < 0)
4184 return isl_schedule_node_free(node
);
4188 return split_scaled(node
, graph
);
4191 /* Topologically sort statements mapped to the same schedule iteration
4192 * and add insert a sequence node in front of "node"
4193 * corresponding to this order.
4194 * If "initialized" is set, then it may be assumed that compute_maxvar
4195 * has been called on the current band. Otherwise, call
4196 * compute_maxvar if and before carry_dependences gets called.
4198 * If it turns out to be impossible to sort the statements apart,
4199 * because different dependences impose different orderings
4200 * on the statements, then we extend the schedule such that
4201 * it carries at least one more dependence.
4203 static __isl_give isl_schedule_node
*sort_statements(
4204 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
4208 isl_union_set_list
*filters
;
4213 ctx
= isl_schedule_node_get_ctx(node
);
4215 isl_die(ctx
, isl_error_internal
,
4216 "graph should have at least one node",
4217 return isl_schedule_node_free(node
));
4222 if (update_edges(ctx
, graph
) < 0)
4223 return isl_schedule_node_free(node
);
4225 if (graph
->n_edge
== 0)
4228 if (detect_sccs(ctx
, graph
) < 0)
4229 return isl_schedule_node_free(node
);
4232 if (graph
->scc
< graph
->n
) {
4233 if (!initialized
&& compute_maxvar(graph
) < 0)
4234 return isl_schedule_node_free(node
);
4235 return carry_dependences(node
, graph
);
4238 filters
= extract_sccs(ctx
, graph
);
4239 node
= isl_schedule_node_insert_sequence(node
, filters
);
4244 /* Are there any (non-empty) (conditional) validity edges in the graph?
4246 static int has_validity_edges(struct isl_sched_graph
*graph
)
4250 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4253 empty
= isl_map_plain_is_empty(graph
->edge
[i
].map
);
4258 if (is_any_validity(&graph
->edge
[i
]))
4265 /* Should we apply a Feautrier step?
4266 * That is, did the user request the Feautrier algorithm and are
4267 * there any validity dependences (left)?
4269 static int need_feautrier_step(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
4271 if (ctx
->opt
->schedule_algorithm
!= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
)
4274 return has_validity_edges(graph
);
4277 /* Compute a schedule for a connected dependence graph using Feautrier's
4278 * multi-dimensional scheduling algorithm and return the updated schedule node.
4280 * The original algorithm is described in [1].
4281 * The main idea is to minimize the number of scheduling dimensions, by
4282 * trying to satisfy as many dependences as possible per scheduling dimension.
4284 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4285 * Problem, Part II: Multi-Dimensional Time.
4286 * In Intl. Journal of Parallel Programming, 1992.
4288 static __isl_give isl_schedule_node
*compute_schedule_wcc_feautrier(
4289 isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4291 return carry_dependences(node
, graph
);
4294 /* Turn off the "local" bit on all (condition) edges.
4296 static void clear_local_edges(struct isl_sched_graph
*graph
)
4300 for (i
= 0; i
< graph
->n_edge
; ++i
)
4301 if (is_condition(&graph
->edge
[i
]))
4302 clear_local(&graph
->edge
[i
]);
4305 /* Does "graph" have both condition and conditional validity edges?
4307 static int need_condition_check(struct isl_sched_graph
*graph
)
4310 int any_condition
= 0;
4311 int any_conditional_validity
= 0;
4313 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4314 if (is_condition(&graph
->edge
[i
]))
4316 if (is_conditional_validity(&graph
->edge
[i
]))
4317 any_conditional_validity
= 1;
4320 return any_condition
&& any_conditional_validity
;
4323 /* Does "graph" contain any coincidence edge?
4325 static int has_any_coincidence(struct isl_sched_graph
*graph
)
4329 for (i
= 0; i
< graph
->n_edge
; ++i
)
4330 if (is_coincidence(&graph
->edge
[i
]))
4336 /* Extract the final schedule row as a map with the iteration domain
4337 * of "node" as domain.
4339 static __isl_give isl_map
*final_row(struct isl_sched_node
*node
)
4344 row
= isl_mat_rows(node
->sched
) - 1;
4345 ma
= node_extract_partial_schedule_multi_aff(node
, row
, 1);
4346 return isl_map_from_multi_aff(ma
);
4349 /* Is the conditional validity dependence in the edge with index "edge_index"
4350 * violated by the latest (i.e., final) row of the schedule?
4351 * That is, is i scheduled after j
4352 * for any conditional validity dependence i -> j?
4354 static int is_violated(struct isl_sched_graph
*graph
, int edge_index
)
4356 isl_map
*src_sched
, *dst_sched
, *map
;
4357 struct isl_sched_edge
*edge
= &graph
->edge
[edge_index
];
4360 src_sched
= final_row(edge
->src
);
4361 dst_sched
= final_row(edge
->dst
);
4362 map
= isl_map_copy(edge
->map
);
4363 map
= isl_map_apply_domain(map
, src_sched
);
4364 map
= isl_map_apply_range(map
, dst_sched
);
4365 map
= isl_map_order_gt(map
, isl_dim_in
, 0, isl_dim_out
, 0);
4366 empty
= isl_map_is_empty(map
);
4375 /* Does "graph" have any satisfied condition edges that
4376 * are adjacent to the conditional validity constraint with
4377 * domain "conditional_source" and range "conditional_sink"?
4379 * A satisfied condition is one that is not local.
4380 * If a condition was forced to be local already (i.e., marked as local)
4381 * then there is no need to check if it is in fact local.
4383 * Additionally, mark all adjacent condition edges found as local.
4385 static int has_adjacent_true_conditions(struct isl_sched_graph
*graph
,
4386 __isl_keep isl_union_set
*conditional_source
,
4387 __isl_keep isl_union_set
*conditional_sink
)
4392 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4393 int adjacent
, local
;
4394 isl_union_map
*condition
;
4396 if (!is_condition(&graph
->edge
[i
]))
4398 if (is_local(&graph
->edge
[i
]))
4401 condition
= graph
->edge
[i
].tagged_condition
;
4402 adjacent
= domain_intersects(condition
, conditional_sink
);
4403 if (adjacent
>= 0 && !adjacent
)
4404 adjacent
= range_intersects(condition
,
4405 conditional_source
);
4411 set_local(&graph
->edge
[i
]);
4413 local
= is_condition_false(&graph
->edge
[i
]);
4423 /* Are there any violated conditional validity dependences with
4424 * adjacent condition dependences that are not local with respect
4425 * to the current schedule?
4426 * That is, is the conditional validity constraint violated?
4428 * Additionally, mark all those adjacent condition dependences as local.
4429 * We also mark those adjacent condition dependences that were not marked
4430 * as local before, but just happened to be local already. This ensures
4431 * that they remain local if the schedule is recomputed.
4433 * We first collect domain and range of all violated conditional validity
4434 * dependences and then check if there are any adjacent non-local
4435 * condition dependences.
4437 static int has_violated_conditional_constraint(isl_ctx
*ctx
,
4438 struct isl_sched_graph
*graph
)
4442 isl_union_set
*source
, *sink
;
4444 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
4445 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
4446 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4447 isl_union_set
*uset
;
4448 isl_union_map
*umap
;
4451 if (!is_conditional_validity(&graph
->edge
[i
]))
4454 violated
= is_violated(graph
, i
);
4462 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
4463 uset
= isl_union_map_domain(umap
);
4464 source
= isl_union_set_union(source
, uset
);
4465 source
= isl_union_set_coalesce(source
);
4467 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
4468 uset
= isl_union_map_range(umap
);
4469 sink
= isl_union_set_union(sink
, uset
);
4470 sink
= isl_union_set_coalesce(sink
);
4474 any
= has_adjacent_true_conditions(graph
, source
, sink
);
4476 isl_union_set_free(source
);
4477 isl_union_set_free(sink
);
4480 isl_union_set_free(source
);
4481 isl_union_set_free(sink
);
4485 /* Examine the current band (the rows between graph->band_start and
4486 * graph->n_total_row), deciding whether to drop it or add it to "node"
4487 * and then continue with the computation of the next band, if any.
4488 * If "initialized" is set, then it may be assumed that compute_maxvar
4489 * has been called on the current band. Otherwise, call
4490 * compute_maxvar if and before carry_dependences gets called.
4492 * The caller keeps looking for a new row as long as
4493 * graph->n_row < graph->maxvar. If the latest attempt to find
4494 * such a row failed (i.e., we still have graph->n_row < graph->maxvar),
4496 * - split between SCCs and start over (assuming we found an interesting
4497 * pair of SCCs between which to split)
4498 * - continue with the next band (assuming the current band has at least
4500 * - try to carry as many dependences as possible and continue with the next
4502 * In each case, we first insert a band node in the schedule tree
4503 * if any rows have been computed.
4505 * If the caller managed to complete the schedule, we insert a band node
4506 * (if any schedule rows were computed) and we finish off by topologically
4507 * sorting the statements based on the remaining dependences.
4509 static __isl_give isl_schedule_node
*compute_schedule_finish_band(
4510 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
4518 if (graph
->n_row
< graph
->maxvar
) {
4520 int empty
= graph
->n_total_row
== graph
->band_start
;
4522 ctx
= isl_schedule_node_get_ctx(node
);
4523 if (!ctx
->opt
->schedule_maximize_band_depth
&& !empty
)
4524 return compute_next_band(node
, graph
, 1);
4525 if (graph
->src_scc
>= 0)
4526 return compute_split_schedule(node
, graph
);
4528 return compute_next_band(node
, graph
, 1);
4529 if (!initialized
&& compute_maxvar(graph
) < 0)
4530 return isl_schedule_node_free(node
);
4531 return carry_dependences(node
, graph
);
4534 insert
= graph
->n_total_row
> graph
->band_start
;
4536 node
= insert_current_band(node
, graph
, 1);
4537 node
= isl_schedule_node_child(node
, 0);
4539 node
= sort_statements(node
, graph
, initialized
);
4541 node
= isl_schedule_node_parent(node
);
4546 /* Construct a band of schedule rows for a connected dependence graph.
4547 * The caller is responsible for determining the strongly connected
4548 * components and calling compute_maxvar first.
4550 * We try to find a sequence of as many schedule rows as possible that result
4551 * in non-negative dependence distances (independent of the previous rows
4552 * in the sequence, i.e., such that the sequence is tilable), with as
4553 * many of the initial rows as possible satisfying the coincidence constraints.
4554 * The computation stops if we can't find any more rows or if we have found
4555 * all the rows we wanted to find.
4557 * If ctx->opt->schedule_outer_coincidence is set, then we force the
4558 * outermost dimension to satisfy the coincidence constraints. If this
4559 * turns out to be impossible, we fall back on the general scheme above
4560 * and try to carry as many dependences as possible.
4562 * If "graph" contains both condition and conditional validity dependences,
4563 * then we need to check that that the conditional schedule constraint
4564 * is satisfied, i.e., there are no violated conditional validity dependences
4565 * that are adjacent to any non-local condition dependences.
4566 * If there are, then we mark all those adjacent condition dependences
4567 * as local and recompute the current band. Those dependences that
4568 * are marked local will then be forced to be local.
4569 * The initial computation is performed with no dependences marked as local.
4570 * If we are lucky, then there will be no violated conditional validity
4571 * dependences adjacent to any non-local condition dependences.
4572 * Otherwise, we mark some additional condition dependences as local and
4573 * recompute. We continue this process until there are no violations left or
4574 * until we are no longer able to compute a schedule.
4575 * Since there are only a finite number of dependences,
4576 * there will only be a finite number of iterations.
4578 static isl_stat
compute_schedule_wcc_band(isl_ctx
*ctx
,
4579 struct isl_sched_graph
*graph
)
4581 int has_coincidence
;
4582 int use_coincidence
;
4583 int force_coincidence
= 0;
4584 int check_conditional
;
4586 if (sort_sccs(graph
) < 0)
4587 return isl_stat_error
;
4589 clear_local_edges(graph
);
4590 check_conditional
= need_condition_check(graph
);
4591 has_coincidence
= has_any_coincidence(graph
);
4593 if (ctx
->opt
->schedule_outer_coincidence
)
4594 force_coincidence
= 1;
4596 use_coincidence
= has_coincidence
;
4597 while (graph
->n_row
< graph
->maxvar
) {
4602 graph
->src_scc
= -1;
4603 graph
->dst_scc
= -1;
4605 if (setup_lp(ctx
, graph
, use_coincidence
) < 0)
4606 return isl_stat_error
;
4607 sol
= solve_lp(graph
);
4609 return isl_stat_error
;
4610 if (sol
->size
== 0) {
4611 int empty
= graph
->n_total_row
== graph
->band_start
;
4614 if (use_coincidence
&& (!force_coincidence
|| !empty
)) {
4615 use_coincidence
= 0;
4620 coincident
= !has_coincidence
|| use_coincidence
;
4621 if (update_schedule(graph
, sol
, 1, coincident
) < 0)
4622 return isl_stat_error
;
4624 if (!check_conditional
)
4626 violated
= has_violated_conditional_constraint(ctx
, graph
);
4628 return isl_stat_error
;
4631 if (reset_band(graph
) < 0)
4632 return isl_stat_error
;
4633 use_coincidence
= has_coincidence
;
4639 /* Compute a schedule for a connected dependence graph by considering
4640 * the graph as a whole and return the updated schedule node.
4642 * The actual schedule rows of the current band are computed by
4643 * compute_schedule_wcc_band. compute_schedule_finish_band takes
4644 * care of integrating the band into "node" and continuing
4647 static __isl_give isl_schedule_node
*compute_schedule_wcc_whole(
4648 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4655 ctx
= isl_schedule_node_get_ctx(node
);
4656 if (compute_schedule_wcc_band(ctx
, graph
) < 0)
4657 return isl_schedule_node_free(node
);
4659 return compute_schedule_finish_band(node
, graph
, 1);
4662 /* Clustering information used by compute_schedule_wcc_clustering.
4664 * "n" is the number of SCCs in the original dependence graph
4665 * "scc" is an array of "n" elements, each representing an SCC
4666 * of the original dependence graph. All entries in the same cluster
4667 * have the same number of schedule rows.
4668 * "scc_cluster" maps each SCC index to the cluster to which it belongs,
4669 * where each cluster is represented by the index of the first SCC
4670 * in the cluster. Initially, each SCC belongs to a cluster containing
4673 * "scc_in_merge" is used by merge_clusters_along_edge to keep
4674 * track of which SCCs need to be merged.
4676 * "cluster" contains the merged clusters of SCCs after the clustering
4679 * "scc_node" is a temporary data structure used inside copy_partial.
4680 * For each SCC, it keeps track of the number of nodes in the SCC
4681 * that have already been copied.
4683 struct isl_clustering
{
4685 struct isl_sched_graph
*scc
;
4686 struct isl_sched_graph
*cluster
;
4692 /* Initialize the clustering data structure "c" from "graph".
4694 * In particular, allocate memory, extract the SCCs from "graph"
4695 * into c->scc, initialize scc_cluster and construct
4696 * a band of schedule rows for each SCC.
4697 * Within each SCC, there is only one SCC by definition.
4698 * Each SCC initially belongs to a cluster containing only that SCC.
4700 static isl_stat
clustering_init(isl_ctx
*ctx
, struct isl_clustering
*c
,
4701 struct isl_sched_graph
*graph
)
4706 c
->scc
= isl_calloc_array(ctx
, struct isl_sched_graph
, c
->n
);
4707 c
->cluster
= isl_calloc_array(ctx
, struct isl_sched_graph
, c
->n
);
4708 c
->scc_cluster
= isl_calloc_array(ctx
, int, c
->n
);
4709 c
->scc_node
= isl_calloc_array(ctx
, int, c
->n
);
4710 c
->scc_in_merge
= isl_calloc_array(ctx
, int, c
->n
);
4711 if (!c
->scc
|| !c
->cluster
||
4712 !c
->scc_cluster
|| !c
->scc_node
|| !c
->scc_in_merge
)
4713 return isl_stat_error
;
4715 for (i
= 0; i
< c
->n
; ++i
) {
4716 if (extract_sub_graph(ctx
, graph
, &node_scc_exactly
,
4717 &edge_scc_exactly
, i
, &c
->scc
[i
]) < 0)
4718 return isl_stat_error
;
4720 if (compute_maxvar(&c
->scc
[i
]) < 0)
4721 return isl_stat_error
;
4722 if (compute_schedule_wcc_band(ctx
, &c
->scc
[i
]) < 0)
4723 return isl_stat_error
;
4724 c
->scc_cluster
[i
] = i
;
4730 /* Free all memory allocated for "c".
4732 static void clustering_free(isl_ctx
*ctx
, struct isl_clustering
*c
)
4737 for (i
= 0; i
< c
->n
; ++i
)
4738 graph_free(ctx
, &c
->scc
[i
]);
4741 for (i
= 0; i
< c
->n
; ++i
)
4742 graph_free(ctx
, &c
->cluster
[i
]);
4744 free(c
->scc_cluster
);
4746 free(c
->scc_in_merge
);
4749 /* Should we refrain from merging the cluster in "graph" with
4750 * any other cluster?
4751 * In particular, is its current schedule band empty and incomplete.
4753 static int bad_cluster(struct isl_sched_graph
*graph
)
4755 return graph
->n_row
< graph
->maxvar
&&
4756 graph
->n_total_row
== graph
->band_start
;
4759 /* Is "edge" a proximity edge with a non-empty dependence relation?
4761 static isl_bool
is_non_empty_proximity(struct isl_sched_edge
*edge
)
4763 if (!is_proximity(edge
))
4764 return isl_bool_false
;
4765 return isl_bool_not(isl_map_plain_is_empty(edge
->map
));
4768 /* Return the index of an edge in "graph" that can be used to merge
4769 * two clusters in "c".
4770 * Return graph->n_edge if no such edge can be found.
4771 * Return -1 on error.
4773 * In particular, return a proximity edge between two clusters
4774 * that is not marked "no_merge" and such that neither of the
4775 * two clusters has an incomplete, empty band.
4777 * If there are multiple such edges, then try and find the most
4778 * appropriate edge to use for merging. In particular, pick the edge
4779 * with the greatest weight. If there are multiple of those,
4780 * then pick one with the shortest distance between
4781 * the two cluster representatives.
4783 static int find_proximity(struct isl_sched_graph
*graph
,
4784 struct isl_clustering
*c
)
4786 int i
, best
= graph
->n_edge
, best_dist
, best_weight
;
4788 for (i
= 0; i
< graph
->n_edge
; ++i
) {
4789 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
4793 prox
= is_non_empty_proximity(edge
);
4800 if (bad_cluster(&c
->scc
[edge
->src
->scc
]) ||
4801 bad_cluster(&c
->scc
[edge
->dst
->scc
]))
4803 dist
= c
->scc_cluster
[edge
->dst
->scc
] -
4804 c
->scc_cluster
[edge
->src
->scc
];
4807 weight
= edge
->weight
;
4808 if (best
< graph
->n_edge
) {
4809 if (best_weight
> weight
)
4811 if (best_weight
== weight
&& best_dist
<= dist
)
4816 best_weight
= weight
;
4822 /* Internal data structure used in mark_merge_sccs.
4824 * "graph" is the dependence graph in which a strongly connected
4825 * component is constructed.
4826 * "scc_cluster" maps each SCC index to the cluster to which it belongs.
4827 * "src" and "dst" are the indices of the nodes that are being merged.
4829 struct isl_mark_merge_sccs_data
{
4830 struct isl_sched_graph
*graph
;
4836 /* Check whether the cluster containing node "i" depends on the cluster
4837 * containing node "j". If "i" and "j" belong to the same cluster,
4838 * then they are taken to depend on each other to ensure that
4839 * the resulting strongly connected component consists of complete
4840 * clusters. Furthermore, if "i" and "j" are the two nodes that
4841 * are being merged, then they are taken to depend on each other as well.
4842 * Otherwise, check if there is a (conditional) validity dependence
4843 * from node[j] to node[i], forcing node[i] to follow node[j].
4845 static isl_bool
cluster_follows(int i
, int j
, void *user
)
4847 struct isl_mark_merge_sccs_data
*data
= user
;
4848 struct isl_sched_graph
*graph
= data
->graph
;
4849 int *scc_cluster
= data
->scc_cluster
;
4851 if (data
->src
== i
&& data
->dst
== j
)
4852 return isl_bool_true
;
4853 if (data
->src
== j
&& data
->dst
== i
)
4854 return isl_bool_true
;
4855 if (scc_cluster
[graph
->node
[i
].scc
] == scc_cluster
[graph
->node
[j
].scc
])
4856 return isl_bool_true
;
4858 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
4861 /* Mark all SCCs that belong to either of the two clusters in "c"
4862 * connected by the edge in "graph" with index "edge", or to any
4863 * of the intermediate clusters.
4864 * The marking is recorded in c->scc_in_merge.
4866 * The given edge has been selected for merging two clusters,
4867 * meaning that there is at least a proximity edge between the two nodes.
4868 * However, there may also be (indirect) validity dependences
4869 * between the two nodes. When merging the two clusters, all clusters
4870 * containing one or more of the intermediate nodes along the
4871 * indirect validity dependences need to be merged in as well.
4873 * First collect all such nodes by computing the strongly connected
4874 * component (SCC) containing the two nodes connected by the edge, where
4875 * the two nodes are considered to depend on each other to make
4876 * sure they end up in the same SCC. Similarly, each node is considered
4877 * to depend on every other node in the same cluster to ensure
4878 * that the SCC consists of complete clusters.
4880 * Then the original SCCs that contain any of these nodes are marked
4881 * in c->scc_in_merge.
4883 static isl_stat
mark_merge_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
4884 int edge
, struct isl_clustering
*c
)
4886 struct isl_mark_merge_sccs_data data
;
4887 struct isl_tarjan_graph
*g
;
4890 for (i
= 0; i
< c
->n
; ++i
)
4891 c
->scc_in_merge
[i
] = 0;
4894 data
.scc_cluster
= c
->scc_cluster
;
4895 data
.src
= graph
->edge
[edge
].src
- graph
->node
;
4896 data
.dst
= graph
->edge
[edge
].dst
- graph
->node
;
4898 g
= isl_tarjan_graph_component(ctx
, graph
->n
, data
.dst
,
4899 &cluster_follows
, &data
);
4905 isl_die(ctx
, isl_error_internal
,
4906 "expecting at least two nodes in component",
4908 if (g
->order
[--i
] != -1)
4909 isl_die(ctx
, isl_error_internal
,
4910 "expecting end of component marker", goto error
);
4912 for (--i
; i
>= 0 && g
->order
[i
] != -1; --i
) {
4913 int scc
= graph
->node
[g
->order
[i
]].scc
;
4914 c
->scc_in_merge
[scc
] = 1;
4917 isl_tarjan_graph_free(g
);
4920 isl_tarjan_graph_free(g
);
4921 return isl_stat_error
;
4924 /* Construct the identifier "cluster_i".
4926 static __isl_give isl_id
*cluster_id(isl_ctx
*ctx
, int i
)
4930 snprintf(name
, sizeof(name
), "cluster_%d", i
);
4931 return isl_id_alloc(ctx
, name
, NULL
);
4934 /* Construct the space of the cluster with index "i" containing
4935 * the strongly connected component "scc".
4937 * In particular, construct a space called cluster_i with dimension equal
4938 * to the number of schedule rows in the current band of "scc".
4940 static __isl_give isl_space
*cluster_space(struct isl_sched_graph
*scc
, int i
)
4946 nvar
= scc
->n_total_row
- scc
->band_start
;
4947 space
= isl_space_copy(scc
->node
[0].space
);
4948 space
= isl_space_params(space
);
4949 space
= isl_space_set_from_params(space
);
4950 space
= isl_space_add_dims(space
, isl_dim_set
, nvar
);
4951 id
= cluster_id(isl_space_get_ctx(space
), i
);
4952 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
4957 /* Collect the domain of the graph for merging clusters.
4959 * In particular, for each cluster with first SCC "i", construct
4960 * a set in the space called cluster_i with dimension equal
4961 * to the number of schedule rows in the current band of the cluster.
4963 static __isl_give isl_union_set
*collect_domain(isl_ctx
*ctx
,
4964 struct isl_sched_graph
*graph
, struct isl_clustering
*c
)
4968 isl_union_set
*domain
;
4970 space
= isl_space_params_alloc(ctx
, 0);
4971 domain
= isl_union_set_empty(space
);
4973 for (i
= 0; i
< graph
->scc
; ++i
) {
4976 if (!c
->scc_in_merge
[i
])
4978 if (c
->scc_cluster
[i
] != i
)
4980 space
= cluster_space(&c
->scc
[i
], i
);
4981 domain
= isl_union_set_add_set(domain
, isl_set_universe(space
));
4987 /* Construct a map from the original instances to the corresponding
4988 * cluster instance in the current bands of the clusters in "c".
4990 static __isl_give isl_union_map
*collect_cluster_map(isl_ctx
*ctx
,
4991 struct isl_sched_graph
*graph
, struct isl_clustering
*c
)
4995 isl_union_map
*cluster_map
;
4997 space
= isl_space_params_alloc(ctx
, 0);
4998 cluster_map
= isl_union_map_empty(space
);
4999 for (i
= 0; i
< graph
->scc
; ++i
) {
5003 if (!c
->scc_in_merge
[i
])
5006 id
= cluster_id(ctx
, c
->scc_cluster
[i
]);
5007 start
= c
->scc
[i
].band_start
;
5008 n
= c
->scc
[i
].n_total_row
- start
;
5009 for (j
= 0; j
< c
->scc
[i
].n
; ++j
) {
5012 struct isl_sched_node
*node
= &c
->scc
[i
].node
[j
];
5014 ma
= node_extract_partial_schedule_multi_aff(node
,
5016 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
,
5018 map
= isl_map_from_multi_aff(ma
);
5019 cluster_map
= isl_union_map_add_map(cluster_map
, map
);
5027 /* Add "umap" to the schedule constraints "sc" of all types of "edge"
5028 * that are not isl_edge_condition or isl_edge_conditional_validity.
5030 static __isl_give isl_schedule_constraints
*add_non_conditional_constraints(
5031 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*umap
,
5032 __isl_take isl_schedule_constraints
*sc
)
5034 enum isl_edge_type t
;
5039 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
5040 if (t
== isl_edge_condition
||
5041 t
== isl_edge_conditional_validity
)
5043 if (!is_type(edge
, t
))
5045 sc
= isl_schedule_constraints_add(sc
, t
,
5046 isl_union_map_copy(umap
));
5052 /* Add schedule constraints of types isl_edge_condition and
5053 * isl_edge_conditional_validity to "sc" by applying "umap" to
5054 * the domains of the wrapped relations in domain and range
5055 * of the corresponding tagged constraints of "edge".
5057 static __isl_give isl_schedule_constraints
*add_conditional_constraints(
5058 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*umap
,
5059 __isl_take isl_schedule_constraints
*sc
)
5061 enum isl_edge_type t
;
5062 isl_union_map
*tagged
;
5064 for (t
= isl_edge_condition
; t
<= isl_edge_conditional_validity
; ++t
) {
5065 if (!is_type(edge
, t
))
5067 if (t
== isl_edge_condition
)
5068 tagged
= isl_union_map_copy(edge
->tagged_condition
);
5070 tagged
= isl_union_map_copy(edge
->tagged_validity
);
5071 tagged
= isl_union_map_zip(tagged
);
5072 tagged
= isl_union_map_apply_domain(tagged
,
5073 isl_union_map_copy(umap
));
5074 tagged
= isl_union_map_zip(tagged
);
5075 sc
= isl_schedule_constraints_add(sc
, t
, tagged
);
5083 /* Given a mapping "cluster_map" from the original instances to
5084 * the cluster instances, add schedule constraints on the clusters
5085 * to "sc" corresponding to the original constraints represented by "edge".
5087 * For non-tagged dependence constraints, the cluster constraints
5088 * are obtained by applying "cluster_map" to the edge->map.
5090 * For tagged dependence constraints, "cluster_map" needs to be applied
5091 * to the domains of the wrapped relations in domain and range
5092 * of the tagged dependence constraints. Pick out the mappings
5093 * from these domains from "cluster_map" and construct their product.
5094 * This mapping can then be applied to the pair of domains.
5096 static __isl_give isl_schedule_constraints
*collect_edge_constraints(
5097 struct isl_sched_edge
*edge
, __isl_keep isl_union_map
*cluster_map
,
5098 __isl_take isl_schedule_constraints
*sc
)
5100 isl_union_map
*umap
;
5102 isl_union_set
*uset
;
5103 isl_union_map
*umap1
, *umap2
;
5108 umap
= isl_union_map_from_map(isl_map_copy(edge
->map
));
5109 umap
= isl_union_map_apply_domain(umap
,
5110 isl_union_map_copy(cluster_map
));
5111 umap
= isl_union_map_apply_range(umap
,
5112 isl_union_map_copy(cluster_map
));
5113 sc
= add_non_conditional_constraints(edge
, umap
, sc
);
5114 isl_union_map_free(umap
);
5116 if (!sc
|| (!is_condition(edge
) && !is_conditional_validity(edge
)))
5119 space
= isl_space_domain(isl_map_get_space(edge
->map
));
5120 uset
= isl_union_set_from_set(isl_set_universe(space
));
5121 umap1
= isl_union_map_copy(cluster_map
);
5122 umap1
= isl_union_map_intersect_domain(umap1
, uset
);
5123 space
= isl_space_range(isl_map_get_space(edge
->map
));
5124 uset
= isl_union_set_from_set(isl_set_universe(space
));
5125 umap2
= isl_union_map_copy(cluster_map
);
5126 umap2
= isl_union_map_intersect_domain(umap2
, uset
);
5127 umap
= isl_union_map_product(umap1
, umap2
);
5129 sc
= add_conditional_constraints(edge
, umap
, sc
);
5131 isl_union_map_free(umap
);
5135 /* Given a mapping "cluster_map" from the original instances to
5136 * the cluster instances, add schedule constraints on the clusters
5137 * to "sc" corresponding to all edges in "graph" between nodes that
5138 * belong to SCCs that are marked for merging in "scc_in_merge".
5140 static __isl_give isl_schedule_constraints
*collect_constraints(
5141 struct isl_sched_graph
*graph
, int *scc_in_merge
,
5142 __isl_keep isl_union_map
*cluster_map
,
5143 __isl_take isl_schedule_constraints
*sc
)
5147 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5148 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
5150 if (!scc_in_merge
[edge
->src
->scc
])
5152 if (!scc_in_merge
[edge
->dst
->scc
])
5154 sc
= collect_edge_constraints(edge
, cluster_map
, sc
);
5160 /* Construct a dependence graph for scheduling clusters with respect
5161 * to each other and store the result in "merge_graph".
5162 * In particular, the nodes of the graph correspond to the schedule
5163 * dimensions of the current bands of those clusters that have been
5164 * marked for merging in "c".
5166 * First construct an isl_schedule_constraints object for this domain
5167 * by transforming the edges in "graph" to the domain.
5168 * Then initialize a dependence graph for scheduling from these
5171 static isl_stat
init_merge_graph(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5172 struct isl_clustering
*c
, struct isl_sched_graph
*merge_graph
)
5174 isl_union_set
*domain
;
5175 isl_union_map
*cluster_map
;
5176 isl_schedule_constraints
*sc
;
5179 domain
= collect_domain(ctx
, graph
, c
);
5180 sc
= isl_schedule_constraints_on_domain(domain
);
5182 return isl_stat_error
;
5183 cluster_map
= collect_cluster_map(ctx
, graph
, c
);
5184 sc
= collect_constraints(graph
, c
->scc_in_merge
, cluster_map
, sc
);
5185 isl_union_map_free(cluster_map
);
5187 r
= graph_init(merge_graph
, sc
);
5189 isl_schedule_constraints_free(sc
);
5194 /* Compute the maximal number of remaining schedule rows that still need
5195 * to be computed for the nodes that belong to clusters with the maximal
5196 * dimension for the current band (i.e., the band that is to be merged).
5197 * Only clusters that are about to be merged are considered.
5198 * "maxvar" is the maximal dimension for the current band.
5199 * "c" contains information about the clusters.
5201 * Return the maximal number of remaining schedule rows or -1 on error.
5203 static int compute_maxvar_max_slack(int maxvar
, struct isl_clustering
*c
)
5209 for (i
= 0; i
< c
->n
; ++i
) {
5211 struct isl_sched_graph
*scc
;
5213 if (!c
->scc_in_merge
[i
])
5216 nvar
= scc
->n_total_row
- scc
->band_start
;
5219 for (j
= 0; j
< scc
->n
; ++j
) {
5220 struct isl_sched_node
*node
= &scc
->node
[j
];
5223 if (node_update_cmap(node
) < 0)
5225 slack
= node
->nvar
- node
->rank
;
5226 if (slack
> max_slack
)
5234 /* If there are any clusters where the dimension of the current band
5235 * (i.e., the band that is to be merged) is smaller than "maxvar" and
5236 * if there are any nodes in such a cluster where the number
5237 * of remaining schedule rows that still need to be computed
5238 * is greater than "max_slack", then return the smallest current band
5239 * dimension of all these clusters. Otherwise return the original value
5240 * of "maxvar". Return -1 in case of any error.
5241 * Only clusters that are about to be merged are considered.
5242 * "c" contains information about the clusters.
5244 static int limit_maxvar_to_slack(int maxvar
, int max_slack
,
5245 struct isl_clustering
*c
)
5249 for (i
= 0; i
< c
->n
; ++i
) {
5251 struct isl_sched_graph
*scc
;
5253 if (!c
->scc_in_merge
[i
])
5256 nvar
= scc
->n_total_row
- scc
->band_start
;
5259 for (j
= 0; j
< scc
->n
; ++j
) {
5260 struct isl_sched_node
*node
= &scc
->node
[j
];
5263 if (node_update_cmap(node
) < 0)
5265 slack
= node
->nvar
- node
->rank
;
5266 if (slack
> max_slack
) {
5276 /* Adjust merge_graph->maxvar based on the number of remaining schedule rows
5277 * that still need to be computed. In particular, if there is a node
5278 * in a cluster where the dimension of the current band is smaller
5279 * than merge_graph->maxvar, but the number of remaining schedule rows
5280 * is greater than that of any node in a cluster with the maximal
5281 * dimension for the current band (i.e., merge_graph->maxvar),
5282 * then adjust merge_graph->maxvar to the (smallest) current band dimension
5283 * of those clusters. Without this adjustment, the total number of
5284 * schedule dimensions would be increased, resulting in a skewed view
5285 * of the number of coincident dimensions.
5286 * "c" contains information about the clusters.
5288 * If the maximize_band_depth option is set and merge_graph->maxvar is reduced,
5289 * then there is no point in attempting any merge since it will be rejected
5290 * anyway. Set merge_graph->maxvar to zero in such cases.
5292 static isl_stat
adjust_maxvar_to_slack(isl_ctx
*ctx
,
5293 struct isl_sched_graph
*merge_graph
, struct isl_clustering
*c
)
5295 int max_slack
, maxvar
;
5297 max_slack
= compute_maxvar_max_slack(merge_graph
->maxvar
, c
);
5299 return isl_stat_error
;
5300 maxvar
= limit_maxvar_to_slack(merge_graph
->maxvar
, max_slack
, c
);
5302 return isl_stat_error
;
5304 if (maxvar
< merge_graph
->maxvar
) {
5305 if (isl_options_get_schedule_maximize_band_depth(ctx
))
5306 merge_graph
->maxvar
= 0;
5308 merge_graph
->maxvar
= maxvar
;
5314 /* Return the number of coincident dimensions in the current band of "graph",
5315 * where the nodes of "graph" are assumed to be scheduled by a single band.
5317 static int get_n_coincident(struct isl_sched_graph
*graph
)
5321 for (i
= graph
->band_start
; i
< graph
->n_total_row
; ++i
)
5322 if (!graph
->node
[0].coincident
[i
])
5325 return i
- graph
->band_start
;
5328 /* Should the clusters be merged based on the cluster schedule
5329 * in the current (and only) band of "merge_graph", given that
5330 * coincidence should be maximized?
5332 * If the number of coincident schedule dimensions in the merged band
5333 * would be less than the maximal number of coincident schedule dimensions
5334 * in any of the merged clusters, then the clusters should not be merged.
5336 static isl_bool
ok_to_merge_coincident(struct isl_clustering
*c
,
5337 struct isl_sched_graph
*merge_graph
)
5344 for (i
= 0; i
< c
->n
; ++i
) {
5345 if (!c
->scc_in_merge
[i
])
5347 n_coincident
= get_n_coincident(&c
->scc
[i
]);
5348 if (n_coincident
> max_coincident
)
5349 max_coincident
= n_coincident
;
5352 n_coincident
= get_n_coincident(merge_graph
);
5354 return n_coincident
>= max_coincident
;
5357 /* Return the transformation on "node" expressed by the current (and only)
5358 * band of "merge_graph" applied to the clusters in "c".
5360 * First find the representation of "node" in its SCC in "c" and
5361 * extract the transformation expressed by the current band.
5362 * Then extract the transformation applied by "merge_graph"
5363 * to the cluster to which this SCC belongs.
5364 * Combine the two to obtain the complete transformation on the node.
5366 * Note that the range of the first transformation is an anonymous space,
5367 * while the domain of the second is named "cluster_X". The range
5368 * of the former therefore needs to be adjusted before the two
5371 static __isl_give isl_map
*extract_node_transformation(isl_ctx
*ctx
,
5372 struct isl_sched_node
*node
, struct isl_clustering
*c
,
5373 struct isl_sched_graph
*merge_graph
)
5375 struct isl_sched_node
*scc_node
, *cluster_node
;
5379 isl_multi_aff
*ma
, *ma2
;
5381 scc_node
= graph_find_node(ctx
, &c
->scc
[node
->scc
], node
->space
);
5382 start
= c
->scc
[node
->scc
].band_start
;
5383 n
= c
->scc
[node
->scc
].n_total_row
- start
;
5384 ma
= node_extract_partial_schedule_multi_aff(scc_node
, start
, n
);
5385 space
= cluster_space(&c
->scc
[node
->scc
], c
->scc_cluster
[node
->scc
]);
5386 cluster_node
= graph_find_node(ctx
, merge_graph
, space
);
5387 if (space
&& !cluster_node
)
5388 isl_die(ctx
, isl_error_internal
, "unable to find cluster",
5389 space
= isl_space_free(space
));
5390 id
= isl_space_get_tuple_id(space
, isl_dim_set
);
5391 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
, id
);
5392 isl_space_free(space
);
5393 n
= merge_graph
->n_total_row
;
5394 ma2
= node_extract_partial_schedule_multi_aff(cluster_node
, 0, n
);
5395 ma
= isl_multi_aff_pullback_multi_aff(ma2
, ma
);
5397 return isl_map_from_multi_aff(ma
);
5400 /* Give a set of distances "set", are they bounded by a small constant
5401 * in direction "pos"?
5402 * In practice, check if they are bounded by 2 by checking that there
5403 * are no elements with a value greater than or equal to 3 or
5404 * smaller than or equal to -3.
5406 static isl_bool
distance_is_bounded(__isl_keep isl_set
*set
, int pos
)
5412 return isl_bool_error
;
5414 test
= isl_set_copy(set
);
5415 test
= isl_set_lower_bound_si(test
, isl_dim_set
, pos
, 3);
5416 bounded
= isl_set_is_empty(test
);
5419 if (bounded
< 0 || !bounded
)
5422 test
= isl_set_copy(set
);
5423 test
= isl_set_upper_bound_si(test
, isl_dim_set
, pos
, -3);
5424 bounded
= isl_set_is_empty(test
);
5430 /* Does the set "set" have a fixed (but possible parametric) value
5431 * at dimension "pos"?
5433 static isl_bool
has_single_value(__isl_keep isl_set
*set
, int pos
)
5439 return isl_bool_error
;
5440 set
= isl_set_copy(set
);
5441 n
= isl_set_dim(set
, isl_dim_set
);
5442 set
= isl_set_project_out(set
, isl_dim_set
, pos
+ 1, n
- (pos
+ 1));
5443 set
= isl_set_project_out(set
, isl_dim_set
, 0, pos
);
5444 single
= isl_set_is_singleton(set
);
5450 /* Does "map" have a fixed (but possible parametric) value
5451 * at dimension "pos" of either its domain or its range?
5453 static isl_bool
has_singular_src_or_dst(__isl_keep isl_map
*map
, int pos
)
5458 set
= isl_map_domain(isl_map_copy(map
));
5459 single
= has_single_value(set
, pos
);
5462 if (single
< 0 || single
)
5465 set
= isl_map_range(isl_map_copy(map
));
5466 single
= has_single_value(set
, pos
);
5472 /* Does the edge "edge" from "graph" have bounded dependence distances
5473 * in the merged graph "merge_graph" of a selection of clusters in "c"?
5475 * Extract the complete transformations of the source and destination
5476 * nodes of the edge, apply them to the edge constraints and
5477 * compute the differences. Finally, check if these differences are bounded
5478 * in each direction.
5480 * If the dimension of the band is greater than the number of
5481 * dimensions that can be expected to be optimized by the edge
5482 * (based on its weight), then also allow the differences to be unbounded
5483 * in the remaining dimensions, but only if either the source or
5484 * the destination has a fixed value in that direction.
5485 * This allows a statement that produces values that are used by
5486 * several instances of another statement to be merged with that
5488 * However, merging such clusters will introduce an inherently
5489 * large proximity distance inside the merged cluster, meaning
5490 * that proximity distances will no longer be optimized in
5491 * subsequent merges. These merges are therefore only allowed
5492 * after all other possible merges have been tried.
5493 * The first time such a merge is encountered, the weight of the edge
5494 * is replaced by a negative weight. The second time (i.e., after
5495 * all merges over edges with a non-negative weight have been tried),
5496 * the merge is allowed.
5498 static isl_bool
has_bounded_distances(isl_ctx
*ctx
, struct isl_sched_edge
*edge
,
5499 struct isl_sched_graph
*graph
, struct isl_clustering
*c
,
5500 struct isl_sched_graph
*merge_graph
)
5507 map
= isl_map_copy(edge
->map
);
5508 t
= extract_node_transformation(ctx
, edge
->src
, c
, merge_graph
);
5509 map
= isl_map_apply_domain(map
, t
);
5510 t
= extract_node_transformation(ctx
, edge
->dst
, c
, merge_graph
);
5511 map
= isl_map_apply_range(map
, t
);
5512 dist
= isl_map_deltas(isl_map_copy(map
));
5514 bounded
= isl_bool_true
;
5515 n
= isl_set_dim(dist
, isl_dim_set
);
5516 n_slack
= n
- edge
->weight
;
5517 if (edge
->weight
< 0)
5518 n_slack
-= graph
->max_weight
+ 1;
5519 for (i
= 0; i
< n
; ++i
) {
5520 isl_bool bounded_i
, singular_i
;
5522 bounded_i
= distance_is_bounded(dist
, i
);
5527 if (edge
->weight
>= 0)
5528 bounded
= isl_bool_false
;
5532 singular_i
= has_singular_src_or_dst(map
, i
);
5537 bounded
= isl_bool_false
;
5540 if (!bounded
&& i
>= n
&& edge
->weight
>= 0)
5541 edge
->weight
-= graph
->max_weight
+ 1;
5549 return isl_bool_error
;
5552 /* Should the clusters be merged based on the cluster schedule
5553 * in the current (and only) band of "merge_graph"?
5554 * "graph" is the original dependence graph, while "c" records
5555 * which SCCs are involved in the latest merge.
5557 * In particular, is there at least one proximity constraint
5558 * that is optimized by the merge?
5560 * A proximity constraint is considered to be optimized
5561 * if the dependence distances are small.
5563 static isl_bool
ok_to_merge_proximity(isl_ctx
*ctx
,
5564 struct isl_sched_graph
*graph
, struct isl_clustering
*c
,
5565 struct isl_sched_graph
*merge_graph
)
5569 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5570 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
5573 if (!is_proximity(edge
))
5575 if (!c
->scc_in_merge
[edge
->src
->scc
])
5577 if (!c
->scc_in_merge
[edge
->dst
->scc
])
5579 if (c
->scc_cluster
[edge
->dst
->scc
] ==
5580 c
->scc_cluster
[edge
->src
->scc
])
5582 bounded
= has_bounded_distances(ctx
, edge
, graph
, c
,
5584 if (bounded
< 0 || bounded
)
5588 return isl_bool_false
;
5591 /* Should the clusters be merged based on the cluster schedule
5592 * in the current (and only) band of "merge_graph"?
5593 * "graph" is the original dependence graph, while "c" records
5594 * which SCCs are involved in the latest merge.
5596 * If the current band is empty, then the clusters should not be merged.
5598 * If the band depth should be maximized and the merge schedule
5599 * is incomplete (meaning that the dimension of some of the schedule
5600 * bands in the original schedule will be reduced), then the clusters
5601 * should not be merged.
5603 * If the schedule_maximize_coincidence option is set, then check that
5604 * the number of coincident schedule dimensions is not reduced.
5606 * Finally, only allow the merge if at least one proximity
5607 * constraint is optimized.
5609 static isl_bool
ok_to_merge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5610 struct isl_clustering
*c
, struct isl_sched_graph
*merge_graph
)
5612 if (merge_graph
->n_total_row
== merge_graph
->band_start
)
5613 return isl_bool_false
;
5615 if (isl_options_get_schedule_maximize_band_depth(ctx
) &&
5616 merge_graph
->n_total_row
< merge_graph
->maxvar
)
5617 return isl_bool_false
;
5619 if (isl_options_get_schedule_maximize_coincidence(ctx
)) {
5622 ok
= ok_to_merge_coincident(c
, merge_graph
);
5627 return ok_to_merge_proximity(ctx
, graph
, c
, merge_graph
);
5630 /* Apply the schedule in "t_node" to the "n" rows starting at "first"
5631 * of the schedule in "node" and return the result.
5633 * That is, essentially compute
5635 * T * N(first:first+n-1)
5637 * taking into account the constant term and the parameter coefficients
5640 static __isl_give isl_mat
*node_transformation(isl_ctx
*ctx
,
5641 struct isl_sched_node
*t_node
, struct isl_sched_node
*node
,
5646 int n_row
, n_col
, n_param
, n_var
;
5648 n_param
= node
->nparam
;
5650 n_row
= isl_mat_rows(t_node
->sched
);
5651 n_col
= isl_mat_cols(node
->sched
);
5652 t
= isl_mat_alloc(ctx
, n_row
, n_col
);
5655 for (i
= 0; i
< n_row
; ++i
) {
5656 isl_seq_cpy(t
->row
[i
], t_node
->sched
->row
[i
], 1 + n_param
);
5657 isl_seq_clr(t
->row
[i
] + 1 + n_param
, n_var
);
5658 for (j
= 0; j
< n
; ++j
)
5659 isl_seq_addmul(t
->row
[i
],
5660 t_node
->sched
->row
[i
][1 + n_param
+ j
],
5661 node
->sched
->row
[first
+ j
],
5662 1 + n_param
+ n_var
);
5667 /* Apply the cluster schedule in "t_node" to the current band
5668 * schedule of the nodes in "graph".
5670 * In particular, replace the rows starting at band_start
5671 * by the result of applying the cluster schedule in "t_node"
5672 * to the original rows.
5674 * The coincidence of the schedule is determined by the coincidence
5675 * of the cluster schedule.
5677 static isl_stat
transform(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5678 struct isl_sched_node
*t_node
)
5684 start
= graph
->band_start
;
5685 n
= graph
->n_total_row
- start
;
5687 n_new
= isl_mat_rows(t_node
->sched
);
5688 for (i
= 0; i
< graph
->n
; ++i
) {
5689 struct isl_sched_node
*node
= &graph
->node
[i
];
5692 t
= node_transformation(ctx
, t_node
, node
, start
, n
);
5693 node
->sched
= isl_mat_drop_rows(node
->sched
, start
, n
);
5694 node
->sched
= isl_mat_concat(node
->sched
, t
);
5695 node
->sched_map
= isl_map_free(node
->sched_map
);
5697 return isl_stat_error
;
5698 for (j
= 0; j
< n_new
; ++j
)
5699 node
->coincident
[start
+ j
] = t_node
->coincident
[j
];
5701 graph
->n_total_row
-= n
;
5703 graph
->n_total_row
+= n_new
;
5704 graph
->n_row
+= n_new
;
5709 /* Merge the clusters marked for merging in "c" into a single
5710 * cluster using the cluster schedule in the current band of "merge_graph".
5711 * The representative SCC for the new cluster is the SCC with
5712 * the smallest index.
5714 * The current band schedule of each SCC in the new cluster is obtained
5715 * by applying the schedule of the corresponding original cluster
5716 * to the original band schedule.
5717 * All SCCs in the new cluster have the same number of schedule rows.
5719 static isl_stat
merge(isl_ctx
*ctx
, struct isl_clustering
*c
,
5720 struct isl_sched_graph
*merge_graph
)
5726 for (i
= 0; i
< c
->n
; ++i
) {
5727 struct isl_sched_node
*node
;
5729 if (!c
->scc_in_merge
[i
])
5733 space
= cluster_space(&c
->scc
[i
], c
->scc_cluster
[i
]);
5735 return isl_stat_error
;
5736 node
= graph_find_node(ctx
, merge_graph
, space
);
5737 isl_space_free(space
);
5739 isl_die(ctx
, isl_error_internal
,
5740 "unable to find cluster",
5741 return isl_stat_error
);
5742 if (transform(ctx
, &c
->scc
[i
], node
) < 0)
5743 return isl_stat_error
;
5744 c
->scc_cluster
[i
] = cluster
;
5750 /* Try and merge the clusters of SCCs marked in c->scc_in_merge
5751 * by scheduling the current cluster bands with respect to each other.
5753 * Construct a dependence graph with a space for each cluster and
5754 * with the coordinates of each space corresponding to the schedule
5755 * dimensions of the current band of that cluster.
5756 * Construct a cluster schedule in this cluster dependence graph and
5757 * apply it to the current cluster bands if it is applicable
5758 * according to ok_to_merge.
5760 * If the number of remaining schedule dimensions in a cluster
5761 * with a non-maximal current schedule dimension is greater than
5762 * the number of remaining schedule dimensions in clusters
5763 * with a maximal current schedule dimension, then restrict
5764 * the number of rows to be computed in the cluster schedule
5765 * to the minimal such non-maximal current schedule dimension.
5766 * Do this by adjusting merge_graph.maxvar.
5768 * Return isl_bool_true if the clusters have effectively been merged
5769 * into a single cluster.
5771 * Note that since the standard scheduling algorithm minimizes the maximal
5772 * distance over proximity constraints, the proximity constraints between
5773 * the merged clusters may not be optimized any further than what is
5774 * sufficient to bring the distances within the limits of the internal
5775 * proximity constraints inside the individual clusters.
5776 * It may therefore make sense to perform an additional translation step
5777 * to bring the clusters closer to each other, while maintaining
5778 * the linear part of the merging schedule found using the standard
5779 * scheduling algorithm.
5781 static isl_bool
try_merge(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5782 struct isl_clustering
*c
)
5784 struct isl_sched_graph merge_graph
= { 0 };
5787 if (init_merge_graph(ctx
, graph
, c
, &merge_graph
) < 0)
5790 if (compute_maxvar(&merge_graph
) < 0)
5792 if (adjust_maxvar_to_slack(ctx
, &merge_graph
,c
) < 0)
5794 if (compute_schedule_wcc_band(ctx
, &merge_graph
) < 0)
5796 merged
= ok_to_merge(ctx
, graph
, c
, &merge_graph
);
5797 if (merged
&& merge(ctx
, c
, &merge_graph
) < 0)
5800 graph_free(ctx
, &merge_graph
);
5803 graph_free(ctx
, &merge_graph
);
5804 return isl_bool_error
;
5807 /* Is there any edge marked "no_merge" between two SCCs that are
5808 * about to be merged (i.e., that are set in "scc_in_merge")?
5809 * "merge_edge" is the proximity edge along which the clusters of SCCs
5810 * are going to be merged.
5812 * If there is any edge between two SCCs with a negative weight,
5813 * while the weight of "merge_edge" is non-negative, then this
5814 * means that the edge was postponed. "merge_edge" should then
5815 * also be postponed since merging along the edge with negative weight should
5816 * be postponed until all edges with non-negative weight have been tried.
5817 * Replace the weight of "merge_edge" by a negative weight as well and
5818 * tell the caller not to attempt a merge.
5820 static int any_no_merge(struct isl_sched_graph
*graph
, int *scc_in_merge
,
5821 struct isl_sched_edge
*merge_edge
)
5825 for (i
= 0; i
< graph
->n_edge
; ++i
) {
5826 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
5828 if (!scc_in_merge
[edge
->src
->scc
])
5830 if (!scc_in_merge
[edge
->dst
->scc
])
5834 if (merge_edge
->weight
>= 0 && edge
->weight
< 0) {
5835 merge_edge
->weight
-= graph
->max_weight
+ 1;
5843 /* Merge the two clusters in "c" connected by the edge in "graph"
5844 * with index "edge" into a single cluster.
5845 * If it turns out to be impossible to merge these two clusters,
5846 * then mark the edge as "no_merge" such that it will not be
5849 * First mark all SCCs that need to be merged. This includes the SCCs
5850 * in the two clusters, but it may also include the SCCs
5851 * of intermediate clusters.
5852 * If there is already a no_merge edge between any pair of such SCCs,
5853 * then simply mark the current edge as no_merge as well.
5854 * Likewise, if any of those edges was postponed by has_bounded_distances,
5855 * then postpone the current edge as well.
5856 * Otherwise, try and merge the clusters and mark "edge" as "no_merge"
5857 * if the clusters did not end up getting merged, unless the non-merge
5858 * is due to the fact that the edge was postponed. This postponement
5859 * can be recognized by a change in weight (from non-negative to negative).
5861 static isl_stat
merge_clusters_along_edge(isl_ctx
*ctx
,
5862 struct isl_sched_graph
*graph
, int edge
, struct isl_clustering
*c
)
5865 int edge_weight
= graph
->edge
[edge
].weight
;
5867 if (mark_merge_sccs(ctx
, graph
, edge
, c
) < 0)
5868 return isl_stat_error
;
5870 if (any_no_merge(graph
, c
->scc_in_merge
, &graph
->edge
[edge
]))
5871 merged
= isl_bool_false
;
5873 merged
= try_merge(ctx
, graph
, c
);
5875 return isl_stat_error
;
5876 if (!merged
&& edge_weight
== graph
->edge
[edge
].weight
)
5877 graph
->edge
[edge
].no_merge
= 1;
5882 /* Does "node" belong to the cluster identified by "cluster"?
5884 static int node_cluster_exactly(struct isl_sched_node
*node
, int cluster
)
5886 return node
->cluster
== cluster
;
5889 /* Does "edge" connect two nodes belonging to the cluster
5890 * identified by "cluster"?
5892 static int edge_cluster_exactly(struct isl_sched_edge
*edge
, int cluster
)
5894 return edge
->src
->cluster
== cluster
&& edge
->dst
->cluster
== cluster
;
5897 /* Swap the schedule of "node1" and "node2".
5898 * Both nodes have been derived from the same node in a common parent graph.
5899 * Since the "coincident" field is shared with that node
5900 * in the parent graph, there is no need to also swap this field.
5902 static void swap_sched(struct isl_sched_node
*node1
,
5903 struct isl_sched_node
*node2
)
5908 sched
= node1
->sched
;
5909 node1
->sched
= node2
->sched
;
5910 node2
->sched
= sched
;
5912 sched_map
= node1
->sched_map
;
5913 node1
->sched_map
= node2
->sched_map
;
5914 node2
->sched_map
= sched_map
;
5917 /* Copy the current band schedule from the SCCs that form the cluster
5918 * with index "pos" to the actual cluster at position "pos".
5919 * By construction, the index of the first SCC that belongs to the cluster
5922 * The order of the nodes inside both the SCCs and the cluster
5923 * is assumed to be same as the order in the original "graph".
5925 * Since the SCC graphs will no longer be used after this function,
5926 * the schedules are actually swapped rather than copied.
5928 static isl_stat
copy_partial(struct isl_sched_graph
*graph
,
5929 struct isl_clustering
*c
, int pos
)
5933 c
->cluster
[pos
].n_total_row
= c
->scc
[pos
].n_total_row
;
5934 c
->cluster
[pos
].n_row
= c
->scc
[pos
].n_row
;
5935 c
->cluster
[pos
].maxvar
= c
->scc
[pos
].maxvar
;
5937 for (i
= 0; i
< graph
->n
; ++i
) {
5941 if (graph
->node
[i
].cluster
!= pos
)
5943 s
= graph
->node
[i
].scc
;
5944 k
= c
->scc_node
[s
]++;
5945 swap_sched(&c
->cluster
[pos
].node
[j
], &c
->scc
[s
].node
[k
]);
5946 if (c
->scc
[s
].maxvar
> c
->cluster
[pos
].maxvar
)
5947 c
->cluster
[pos
].maxvar
= c
->scc
[s
].maxvar
;
5954 /* Is there a (conditional) validity dependence from node[j] to node[i],
5955 * forcing node[i] to follow node[j] or do the nodes belong to the same
5958 static isl_bool
node_follows_strong_or_same_cluster(int i
, int j
, void *user
)
5960 struct isl_sched_graph
*graph
= user
;
5962 if (graph
->node
[i
].cluster
== graph
->node
[j
].cluster
)
5963 return isl_bool_true
;
5964 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
5967 /* Extract the merged clusters of SCCs in "graph", sort them, and
5968 * store them in c->clusters. Update c->scc_cluster accordingly.
5970 * First keep track of the cluster containing the SCC to which a node
5971 * belongs in the node itself.
5972 * Then extract the clusters into c->clusters, copying the current
5973 * band schedule from the SCCs that belong to the cluster.
5974 * Do this only once per cluster.
5976 * Finally, topologically sort the clusters and update c->scc_cluster
5977 * to match the new scc numbering. While the SCCs were originally
5978 * sorted already, some SCCs that depend on some other SCCs may
5979 * have been merged with SCCs that appear before these other SCCs.
5980 * A reordering may therefore be required.
5982 static isl_stat
extract_clusters(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
5983 struct isl_clustering
*c
)
5987 for (i
= 0; i
< graph
->n
; ++i
)
5988 graph
->node
[i
].cluster
= c
->scc_cluster
[graph
->node
[i
].scc
];
5990 for (i
= 0; i
< graph
->scc
; ++i
) {
5991 if (c
->scc_cluster
[i
] != i
)
5993 if (extract_sub_graph(ctx
, graph
, &node_cluster_exactly
,
5994 &edge_cluster_exactly
, i
, &c
->cluster
[i
]) < 0)
5995 return isl_stat_error
;
5996 c
->cluster
[i
].src_scc
= -1;
5997 c
->cluster
[i
].dst_scc
= -1;
5998 if (copy_partial(graph
, c
, i
) < 0)
5999 return isl_stat_error
;
6002 if (detect_ccs(ctx
, graph
, &node_follows_strong_or_same_cluster
) < 0)
6003 return isl_stat_error
;
6004 for (i
= 0; i
< graph
->n
; ++i
)
6005 c
->scc_cluster
[graph
->node
[i
].scc
] = graph
->node
[i
].cluster
;
6010 /* Compute weights on the proximity edges of "graph" that can
6011 * be used by find_proximity to find the most appropriate
6012 * proximity edge to use to merge two clusters in "c".
6013 * The weights are also used by has_bounded_distances to determine
6014 * whether the merge should be allowed.
6015 * Store the maximum of the computed weights in graph->max_weight.
6017 * The computed weight is a measure for the number of remaining schedule
6018 * dimensions that can still be completely aligned.
6019 * In particular, compute the number of equalities between
6020 * input dimensions and output dimensions in the proximity constraints.
6021 * The directions that are already handled by outer schedule bands
6022 * are projected out prior to determining this number.
6024 * Edges that will never be considered by find_proximity are ignored.
6026 static isl_stat
compute_weights(struct isl_sched_graph
*graph
,
6027 struct isl_clustering
*c
)
6031 graph
->max_weight
= 0;
6033 for (i
= 0; i
< graph
->n_edge
; ++i
) {
6034 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
6035 struct isl_sched_node
*src
= edge
->src
;
6036 struct isl_sched_node
*dst
= edge
->dst
;
6037 isl_basic_map
*hull
;
6041 prox
= is_non_empty_proximity(edge
);
6043 return isl_stat_error
;
6046 if (bad_cluster(&c
->scc
[edge
->src
->scc
]) ||
6047 bad_cluster(&c
->scc
[edge
->dst
->scc
]))
6049 if (c
->scc_cluster
[edge
->dst
->scc
] ==
6050 c
->scc_cluster
[edge
->src
->scc
])
6053 hull
= isl_map_affine_hull(isl_map_copy(edge
->map
));
6054 hull
= isl_basic_map_transform_dims(hull
, isl_dim_in
, 0,
6055 isl_mat_copy(src
->ctrans
));
6056 hull
= isl_basic_map_transform_dims(hull
, isl_dim_out
, 0,
6057 isl_mat_copy(dst
->ctrans
));
6058 hull
= isl_basic_map_project_out(hull
,
6059 isl_dim_in
, 0, src
->rank
);
6060 hull
= isl_basic_map_project_out(hull
,
6061 isl_dim_out
, 0, dst
->rank
);
6062 hull
= isl_basic_map_remove_divs(hull
);
6063 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
6064 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
6065 hull
= isl_basic_map_drop_constraints_not_involving_dims(hull
,
6066 isl_dim_in
, 0, n_in
);
6067 hull
= isl_basic_map_drop_constraints_not_involving_dims(hull
,
6068 isl_dim_out
, 0, n_out
);
6070 return isl_stat_error
;
6071 edge
->weight
= isl_basic_map_n_equality(hull
);
6072 isl_basic_map_free(hull
);
6074 if (edge
->weight
> graph
->max_weight
)
6075 graph
->max_weight
= edge
->weight
;
6081 /* Call compute_schedule_finish_band on each of the clusters in "c"
6082 * in their topological order. This order is determined by the scc
6083 * fields of the nodes in "graph".
6084 * Combine the results in a sequence expressing the topological order.
6086 * If there is only one cluster left, then there is no need to introduce
6087 * a sequence node. Also, in this case, the cluster necessarily contains
6088 * the SCC at position 0 in the original graph and is therefore also
6089 * stored in the first cluster of "c".
6091 static __isl_give isl_schedule_node
*finish_bands_clustering(
6092 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
6093 struct isl_clustering
*c
)
6097 isl_union_set_list
*filters
;
6099 if (graph
->scc
== 1)
6100 return compute_schedule_finish_band(node
, &c
->cluster
[0], 0);
6102 ctx
= isl_schedule_node_get_ctx(node
);
6104 filters
= extract_sccs(ctx
, graph
);
6105 node
= isl_schedule_node_insert_sequence(node
, filters
);
6107 for (i
= 0; i
< graph
->scc
; ++i
) {
6108 int j
= c
->scc_cluster
[i
];
6109 node
= isl_schedule_node_child(node
, i
);
6110 node
= isl_schedule_node_child(node
, 0);
6111 node
= compute_schedule_finish_band(node
, &c
->cluster
[j
], 0);
6112 node
= isl_schedule_node_parent(node
);
6113 node
= isl_schedule_node_parent(node
);
6119 /* Compute a schedule for a connected dependence graph by first considering
6120 * each strongly connected component (SCC) in the graph separately and then
6121 * incrementally combining them into clusters.
6122 * Return the updated schedule node.
6124 * Initially, each cluster consists of a single SCC, each with its
6125 * own band schedule. The algorithm then tries to merge pairs
6126 * of clusters along a proximity edge until no more suitable
6127 * proximity edges can be found. During this merging, the schedule
6128 * is maintained in the individual SCCs.
6129 * After the merging is completed, the full resulting clusters
6130 * are extracted and in finish_bands_clustering,
6131 * compute_schedule_finish_band is called on each of them to integrate
6132 * the band into "node" and to continue the computation.
6134 * compute_weights initializes the weights that are used by find_proximity.
6136 static __isl_give isl_schedule_node
*compute_schedule_wcc_clustering(
6137 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
6140 struct isl_clustering c
;
6143 ctx
= isl_schedule_node_get_ctx(node
);
6145 if (clustering_init(ctx
, &c
, graph
) < 0)
6148 if (compute_weights(graph
, &c
) < 0)
6152 i
= find_proximity(graph
, &c
);
6155 if (i
>= graph
->n_edge
)
6157 if (merge_clusters_along_edge(ctx
, graph
, i
, &c
) < 0)
6161 if (extract_clusters(ctx
, graph
, &c
) < 0)
6164 node
= finish_bands_clustering(node
, graph
, &c
);
6166 clustering_free(ctx
, &c
);
6169 clustering_free(ctx
, &c
);
6170 return isl_schedule_node_free(node
);
6173 /* Compute a schedule for a connected dependence graph and return
6174 * the updated schedule node.
6176 * If Feautrier's algorithm is selected, we first recursively try to satisfy
6177 * as many validity dependences as possible. When all validity dependences
6178 * are satisfied we extend the schedule to a full-dimensional schedule.
6180 * Call compute_schedule_wcc_whole or compute_schedule_wcc_clustering
6181 * depending on whether the user has selected the option to try and
6182 * compute a schedule for the entire (weakly connected) component first.
6183 * If there is only a single strongly connected component (SCC), then
6184 * there is no point in trying to combine SCCs
6185 * in compute_schedule_wcc_clustering, so compute_schedule_wcc_whole
6186 * is called instead.
6188 static __isl_give isl_schedule_node
*compute_schedule_wcc(
6189 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
6196 ctx
= isl_schedule_node_get_ctx(node
);
6197 if (detect_sccs(ctx
, graph
) < 0)
6198 return isl_schedule_node_free(node
);
6200 if (compute_maxvar(graph
) < 0)
6201 return isl_schedule_node_free(node
);
6203 if (need_feautrier_step(ctx
, graph
))
6204 return compute_schedule_wcc_feautrier(node
, graph
);
6206 if (graph
->scc
<= 1 || isl_options_get_schedule_whole_component(ctx
))
6207 return compute_schedule_wcc_whole(node
, graph
);
6209 return compute_schedule_wcc_clustering(node
, graph
);
6212 /* Compute a schedule for each group of nodes identified by node->scc
6213 * separately and then combine them in a sequence node (or as set node
6214 * if graph->weak is set) inserted at position "node" of the schedule tree.
6215 * Return the updated schedule node.
6217 * If "wcc" is set then each of the groups belongs to a single
6218 * weakly connected component in the dependence graph so that
6219 * there is no need for compute_sub_schedule to look for weakly
6220 * connected components.
6222 static __isl_give isl_schedule_node
*compute_component_schedule(
6223 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
6228 isl_union_set_list
*filters
;
6232 ctx
= isl_schedule_node_get_ctx(node
);
6234 filters
= extract_sccs(ctx
, graph
);
6236 node
= isl_schedule_node_insert_set(node
, filters
);
6238 node
= isl_schedule_node_insert_sequence(node
, filters
);
6240 for (component
= 0; component
< graph
->scc
; ++component
) {
6241 node
= isl_schedule_node_child(node
, component
);
6242 node
= isl_schedule_node_child(node
, 0);
6243 node
= compute_sub_schedule(node
, ctx
, graph
,
6245 &edge_scc_exactly
, component
, wcc
);
6246 node
= isl_schedule_node_parent(node
);
6247 node
= isl_schedule_node_parent(node
);
6253 /* Compute a schedule for the given dependence graph and insert it at "node".
6254 * Return the updated schedule node.
6256 * We first check if the graph is connected (through validity and conditional
6257 * validity dependences) and, if not, compute a schedule
6258 * for each component separately.
6259 * If the schedule_serialize_sccs option is set, then we check for strongly
6260 * connected components instead and compute a separate schedule for
6261 * each such strongly connected component.
6263 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
6264 struct isl_sched_graph
*graph
)
6271 ctx
= isl_schedule_node_get_ctx(node
);
6272 if (isl_options_get_schedule_serialize_sccs(ctx
)) {
6273 if (detect_sccs(ctx
, graph
) < 0)
6274 return isl_schedule_node_free(node
);
6276 if (detect_wccs(ctx
, graph
) < 0)
6277 return isl_schedule_node_free(node
);
6281 return compute_component_schedule(node
, graph
, 1);
6283 return compute_schedule_wcc(node
, graph
);
6286 /* Compute a schedule on sc->domain that respects the given schedule
6289 * In particular, the schedule respects all the validity dependences.
6290 * If the default isl scheduling algorithm is used, it tries to minimize
6291 * the dependence distances over the proximity dependences.
6292 * If Feautrier's scheduling algorithm is used, the proximity dependence
6293 * distances are only minimized during the extension to a full-dimensional
6296 * If there are any condition and conditional validity dependences,
6297 * then the conditional validity dependences may be violated inside
6298 * a tilable band, provided they have no adjacent non-local
6299 * condition dependences.
6301 __isl_give isl_schedule
*isl_schedule_constraints_compute_schedule(
6302 __isl_take isl_schedule_constraints
*sc
)
6304 isl_ctx
*ctx
= isl_schedule_constraints_get_ctx(sc
);
6305 struct isl_sched_graph graph
= { 0 };
6306 isl_schedule
*sched
;
6307 isl_schedule_node
*node
;
6308 isl_union_set
*domain
;
6310 sc
= isl_schedule_constraints_align_params(sc
);
6312 domain
= isl_schedule_constraints_get_domain(sc
);
6313 if (isl_union_set_n_set(domain
) == 0) {
6314 isl_schedule_constraints_free(sc
);
6315 return isl_schedule_from_domain(domain
);
6318 if (graph_init(&graph
, sc
) < 0)
6319 domain
= isl_union_set_free(domain
);
6321 node
= isl_schedule_node_from_domain(domain
);
6322 node
= isl_schedule_node_child(node
, 0);
6324 node
= compute_schedule(node
, &graph
);
6325 sched
= isl_schedule_node_get_schedule(node
);
6326 isl_schedule_node_free(node
);
6328 graph_free(ctx
, &graph
);
6329 isl_schedule_constraints_free(sc
);
6334 /* Compute a schedule for the given union of domains that respects
6335 * all the validity dependences and minimizes
6336 * the dependence distances over the proximity dependences.
6338 * This function is kept for backward compatibility.
6340 __isl_give isl_schedule
*isl_union_set_compute_schedule(
6341 __isl_take isl_union_set
*domain
,
6342 __isl_take isl_union_map
*validity
,
6343 __isl_take isl_union_map
*proximity
)
6345 isl_schedule_constraints
*sc
;
6347 sc
= isl_schedule_constraints_on_domain(domain
);
6348 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
6349 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
6351 return isl_schedule_constraints_compute_schedule(sc
);