2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_space_private.h>
16 #include <isl_aff_private.h>
18 #include <isl/constraint.h>
19 #include <isl/schedule.h>
20 #include <isl_mat_private.h>
21 #include <isl_vec_private.h>
25 #include <isl_dim_map.h>
26 #include <isl/map_to_basic_set.h>
28 #include <isl_schedule_private.h>
29 #include <isl_band_private.h>
30 #include <isl_options_private.h>
31 #include <isl_tarjan.h>
34 * The scheduling algorithm implemented in this file was inspired by
35 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
36 * Parallelization and Locality Optimization in the Polyhedral Model".
39 __isl_give isl_schedule_constraints
*isl_schedule_constraints_copy(
40 __isl_keep isl_schedule_constraints
*sc
)
43 isl_schedule_constraints
*sc_copy
;
46 ctx
= isl_union_set_get_ctx(sc
->domain
);
47 sc_copy
= isl_calloc_type(ctx
, struct isl_schedule_constraints
);
51 sc_copy
->domain
= isl_union_set_copy(sc
->domain
);
53 return isl_schedule_constraints_free(sc_copy
);
55 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
56 sc_copy
->constraint
[i
] = isl_union_map_copy(sc
->constraint
[i
]);
57 if (!sc_copy
->constraint
[i
])
58 return isl_schedule_constraints_free(sc_copy
);
65 /* Construct an isl_schedule_constraints object for computing a schedule
66 * on "domain". The initial object does not impose any constraints.
68 __isl_give isl_schedule_constraints
*isl_schedule_constraints_on_domain(
69 __isl_take isl_union_set
*domain
)
73 isl_schedule_constraints
*sc
;
80 ctx
= isl_union_set_get_ctx(domain
);
81 sc
= isl_calloc_type(ctx
, struct isl_schedule_constraints
);
85 space
= isl_union_set_get_space(domain
);
87 empty
= isl_union_map_empty(space
);
88 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
89 sc
->constraint
[i
] = isl_union_map_copy(empty
);
90 if (!sc
->constraint
[i
])
91 sc
->domain
= isl_union_set_free(sc
->domain
);
93 isl_union_map_free(empty
);
96 return isl_schedule_constraints_free(sc
);
100 isl_union_set_free(domain
);
104 /* Replace the validity constraints of "sc" by "validity".
106 __isl_give isl_schedule_constraints
*isl_schedule_constraints_set_validity(
107 __isl_take isl_schedule_constraints
*sc
,
108 __isl_take isl_union_map
*validity
)
110 if (!sc
|| !validity
)
113 isl_union_map_free(sc
->constraint
[isl_edge_validity
]);
114 sc
->constraint
[isl_edge_validity
] = validity
;
118 isl_schedule_constraints_free(sc
);
119 isl_union_map_free(validity
);
123 /* Replace the coincidence constraints of "sc" by "coincidence".
125 __isl_give isl_schedule_constraints
*isl_schedule_constraints_set_coincidence(
126 __isl_take isl_schedule_constraints
*sc
,
127 __isl_take isl_union_map
*coincidence
)
129 if (!sc
|| !coincidence
)
132 isl_union_map_free(sc
->constraint
[isl_edge_coincidence
]);
133 sc
->constraint
[isl_edge_coincidence
] = coincidence
;
137 isl_schedule_constraints_free(sc
);
138 isl_union_map_free(coincidence
);
142 /* Replace the proximity constraints of "sc" by "proximity".
144 __isl_give isl_schedule_constraints
*isl_schedule_constraints_set_proximity(
145 __isl_take isl_schedule_constraints
*sc
,
146 __isl_take isl_union_map
*proximity
)
148 if (!sc
|| !proximity
)
151 isl_union_map_free(sc
->constraint
[isl_edge_proximity
]);
152 sc
->constraint
[isl_edge_proximity
] = proximity
;
156 isl_schedule_constraints_free(sc
);
157 isl_union_map_free(proximity
);
161 /* Replace the conditional validity constraints of "sc" by "condition"
164 __isl_give isl_schedule_constraints
*
165 isl_schedule_constraints_set_conditional_validity(
166 __isl_take isl_schedule_constraints
*sc
,
167 __isl_take isl_union_map
*condition
,
168 __isl_take isl_union_map
*validity
)
170 if (!sc
|| !condition
|| !validity
)
173 isl_union_map_free(sc
->constraint
[isl_edge_condition
]);
174 sc
->constraint
[isl_edge_condition
] = condition
;
175 isl_union_map_free(sc
->constraint
[isl_edge_conditional_validity
]);
176 sc
->constraint
[isl_edge_conditional_validity
] = validity
;
180 isl_schedule_constraints_free(sc
);
181 isl_union_map_free(condition
);
182 isl_union_map_free(validity
);
186 __isl_null isl_schedule_constraints
*isl_schedule_constraints_free(
187 __isl_take isl_schedule_constraints
*sc
)
189 enum isl_edge_type i
;
194 isl_union_set_free(sc
->domain
);
195 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
196 isl_union_map_free(sc
->constraint
[i
]);
203 isl_ctx
*isl_schedule_constraints_get_ctx(
204 __isl_keep isl_schedule_constraints
*sc
)
206 return sc
? isl_union_set_get_ctx(sc
->domain
) : NULL
;
209 void isl_schedule_constraints_dump(__isl_keep isl_schedule_constraints
*sc
)
214 fprintf(stderr
, "domain: ");
215 isl_union_set_dump(sc
->domain
);
216 fprintf(stderr
, "validity: ");
217 isl_union_map_dump(sc
->constraint
[isl_edge_validity
]);
218 fprintf(stderr
, "proximity: ");
219 isl_union_map_dump(sc
->constraint
[isl_edge_proximity
]);
220 fprintf(stderr
, "coincidence: ");
221 isl_union_map_dump(sc
->constraint
[isl_edge_coincidence
]);
222 fprintf(stderr
, "condition: ");
223 isl_union_map_dump(sc
->constraint
[isl_edge_condition
]);
224 fprintf(stderr
, "conditional_validity: ");
225 isl_union_map_dump(sc
->constraint
[isl_edge_conditional_validity
]);
228 /* Align the parameters of the fields of "sc".
230 static __isl_give isl_schedule_constraints
*
231 isl_schedule_constraints_align_params(__isl_take isl_schedule_constraints
*sc
)
234 enum isl_edge_type i
;
239 space
= isl_union_set_get_space(sc
->domain
);
240 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
241 space
= isl_space_align_params(space
,
242 isl_union_map_get_space(sc
->constraint
[i
]));
244 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
245 sc
->constraint
[i
] = isl_union_map_align_params(
246 sc
->constraint
[i
], isl_space_copy(space
));
247 if (!sc
->constraint
[i
])
248 space
= isl_space_free(space
);
250 sc
->domain
= isl_union_set_align_params(sc
->domain
, space
);
252 return isl_schedule_constraints_free(sc
);
257 /* Return the total number of isl_maps in the constraints of "sc".
259 static __isl_give
int isl_schedule_constraints_n_map(
260 __isl_keep isl_schedule_constraints
*sc
)
262 enum isl_edge_type i
;
265 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
266 n
+= isl_union_map_n_map(sc
->constraint
[i
]);
271 /* Internal information about a node that is used during the construction
273 * dim represents the space in which the domain lives
274 * sched is a matrix representation of the schedule being constructed
276 * sched_map is an isl_map representation of the same (partial) schedule
277 * sched_map may be NULL
278 * rank is the number of linearly independent rows in the linear part
280 * the columns of cmap represent a change of basis for the schedule
281 * coefficients; the first rank columns span the linear part of
283 * cinv is the inverse of cmap.
284 * start is the first variable in the LP problem in the sequences that
285 * represents the schedule coefficients of this node
286 * nvar is the dimension of the domain
287 * nparam is the number of parameters or 0 if we are not constructing
288 * a parametric schedule
290 * scc is the index of SCC (or WCC) this node belongs to
292 * band contains the band index for each of the rows of the schedule.
293 * band_id is used to differentiate between separate bands at the same
294 * level within the same parent band, i.e., bands that are separated
295 * by the parent band or bands that are independent of each other.
296 * coincident contains a boolean for each of the rows of the schedule,
297 * indicating whether the corresponding scheduling dimension satisfies
298 * the coincidence constraints in the sense that the corresponding
299 * dependence distances are zero.
301 struct isl_sched_node
{
319 static int node_has_dim(const void *entry
, const void *val
)
321 struct isl_sched_node
*node
= (struct isl_sched_node
*)entry
;
322 isl_space
*dim
= (isl_space
*)val
;
324 return isl_space_is_equal(node
->dim
, dim
);
327 /* An edge in the dependence graph. An edge may be used to
328 * ensure validity of the generated schedule, to minimize the dependence
331 * map is the dependence relation, with i -> j in the map if j depends on i
332 * tagged_condition and tagged_validity contain the union of all tagged
333 * condition or conditional validity dependence relations that
334 * specialize the dependence relation "map"; that is,
335 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
336 * or "tagged_validity", then i -> j is an element of "map".
337 * If these fields are NULL, then they represent the empty relation.
338 * src is the source node
339 * dst is the sink node
340 * validity is set if the edge is used to ensure correctness
341 * coincidence is used to enforce zero dependence distances
342 * proximity is set if the edge is used to minimize dependence distances
343 * condition is set if the edge represents a condition
344 * for a conditional validity schedule constraint
345 * local can only be set for condition edges and indicates that
346 * the dependence distance over the edge should be zero
347 * conditional_validity is set if the edge is used to conditionally
350 * For validity edges, start and end mark the sequence of inequality
351 * constraints in the LP problem that encode the validity constraint
352 * corresponding to this edge.
354 struct isl_sched_edge
{
356 isl_union_map
*tagged_condition
;
357 isl_union_map
*tagged_validity
;
359 struct isl_sched_node
*src
;
360 struct isl_sched_node
*dst
;
362 unsigned validity
: 1;
363 unsigned coincidence
: 1;
364 unsigned proximity
: 1;
366 unsigned condition
: 1;
367 unsigned conditional_validity
: 1;
373 /* Internal information about the dependence graph used during
374 * the construction of the schedule.
376 * intra_hmap is a cache, mapping dependence relations to their dual,
377 * for dependences from a node to itself
378 * inter_hmap is a cache, mapping dependence relations to their dual,
379 * for dependences between distinct nodes
381 * n is the number of nodes
382 * node is the list of nodes
383 * maxvar is the maximal number of variables over all nodes
384 * max_row is the allocated number of rows in the schedule
385 * n_row is the current (maximal) number of linearly independent
386 * rows in the node schedules
387 * n_total_row is the current number of rows in the node schedules
388 * n_band is the current number of completed bands
389 * band_start is the starting row in the node schedules of the current band
390 * root is set if this graph is the original dependence graph,
391 * without any splitting
393 * sorted contains a list of node indices sorted according to the
394 * SCC to which a node belongs
396 * n_edge is the number of edges
397 * edge is the list of edges
398 * max_edge contains the maximal number of edges of each type;
399 * in particular, it contains the number of edges in the inital graph.
400 * edge_table contains pointers into the edge array, hashed on the source
401 * and sink spaces; there is one such table for each type;
402 * a given edge may be referenced from more than one table
403 * if the corresponding relation appears in more than of the
404 * sets of dependences
406 * node_table contains pointers into the node array, hashed on the space
408 * region contains a list of variable sequences that should be non-trivial
410 * lp contains the (I)LP problem used to obtain new schedule rows
412 * src_scc and dst_scc are the source and sink SCCs of an edge with
413 * conflicting constraints
415 * scc represents the number of components
417 struct isl_sched_graph
{
418 isl_map_to_basic_set
*intra_hmap
;
419 isl_map_to_basic_set
*inter_hmap
;
421 struct isl_sched_node
*node
;
435 struct isl_sched_edge
*edge
;
437 int max_edge
[isl_edge_last
+ 1];
438 struct isl_hash_table
*edge_table
[isl_edge_last
+ 1];
440 struct isl_hash_table
*node_table
;
441 struct isl_region
*region
;
451 /* Initialize node_table based on the list of nodes.
453 static int graph_init_table(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
457 graph
->node_table
= isl_hash_table_alloc(ctx
, graph
->n
);
458 if (!graph
->node_table
)
461 for (i
= 0; i
< graph
->n
; ++i
) {
462 struct isl_hash_table_entry
*entry
;
465 hash
= isl_space_get_hash(graph
->node
[i
].dim
);
466 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
468 graph
->node
[i
].dim
, 1);
471 entry
->data
= &graph
->node
[i
];
477 /* Return a pointer to the node that lives within the given space,
478 * or NULL if there is no such node.
480 static struct isl_sched_node
*graph_find_node(isl_ctx
*ctx
,
481 struct isl_sched_graph
*graph
, __isl_keep isl_space
*dim
)
483 struct isl_hash_table_entry
*entry
;
486 hash
= isl_space_get_hash(dim
);
487 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
488 &node_has_dim
, dim
, 0);
490 return entry
? entry
->data
: NULL
;
493 static int edge_has_src_and_dst(const void *entry
, const void *val
)
495 const struct isl_sched_edge
*edge
= entry
;
496 const struct isl_sched_edge
*temp
= val
;
498 return edge
->src
== temp
->src
&& edge
->dst
== temp
->dst
;
501 /* Add the given edge to graph->edge_table[type].
503 static int graph_edge_table_add(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
504 enum isl_edge_type type
, struct isl_sched_edge
*edge
)
506 struct isl_hash_table_entry
*entry
;
509 hash
= isl_hash_init();
510 hash
= isl_hash_builtin(hash
, edge
->src
);
511 hash
= isl_hash_builtin(hash
, edge
->dst
);
512 entry
= isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
513 &edge_has_src_and_dst
, edge
, 1);
521 /* Allocate the edge_tables based on the maximal number of edges of
524 static int graph_init_edge_tables(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
528 for (i
= 0; i
<= isl_edge_last
; ++i
) {
529 graph
->edge_table
[i
] = isl_hash_table_alloc(ctx
,
531 if (!graph
->edge_table
[i
])
538 /* If graph->edge_table[type] contains an edge from the given source
539 * to the given destination, then return the hash table entry of this edge.
540 * Otherwise, return NULL.
542 static struct isl_hash_table_entry
*graph_find_edge_entry(
543 struct isl_sched_graph
*graph
,
544 enum isl_edge_type type
,
545 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
547 isl_ctx
*ctx
= isl_space_get_ctx(src
->dim
);
549 struct isl_sched_edge temp
= { .src
= src
, .dst
= dst
};
551 hash
= isl_hash_init();
552 hash
= isl_hash_builtin(hash
, temp
.src
);
553 hash
= isl_hash_builtin(hash
, temp
.dst
);
554 return isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
555 &edge_has_src_and_dst
, &temp
, 0);
559 /* If graph->edge_table[type] contains an edge from the given source
560 * to the given destination, then return this edge.
561 * Otherwise, return NULL.
563 static struct isl_sched_edge
*graph_find_edge(struct isl_sched_graph
*graph
,
564 enum isl_edge_type type
,
565 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
567 struct isl_hash_table_entry
*entry
;
569 entry
= graph_find_edge_entry(graph
, type
, src
, dst
);
576 /* Check whether the dependence graph has an edge of the given type
577 * between the given two nodes.
579 static int graph_has_edge(struct isl_sched_graph
*graph
,
580 enum isl_edge_type type
,
581 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
583 struct isl_sched_edge
*edge
;
586 edge
= graph_find_edge(graph
, type
, src
, dst
);
590 empty
= isl_map_plain_is_empty(edge
->map
);
597 /* Look for any edge with the same src, dst and map fields as "model".
599 * Return the matching edge if one can be found.
600 * Return "model" if no matching edge is found.
601 * Return NULL on error.
603 static struct isl_sched_edge
*graph_find_matching_edge(
604 struct isl_sched_graph
*graph
, struct isl_sched_edge
*model
)
606 enum isl_edge_type i
;
607 struct isl_sched_edge
*edge
;
609 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
612 edge
= graph_find_edge(graph
, i
, model
->src
, model
->dst
);
615 is_equal
= isl_map_plain_is_equal(model
->map
, edge
->map
);
625 /* Remove the given edge from all the edge_tables that refer to it.
627 static void graph_remove_edge(struct isl_sched_graph
*graph
,
628 struct isl_sched_edge
*edge
)
630 isl_ctx
*ctx
= isl_map_get_ctx(edge
->map
);
631 enum isl_edge_type i
;
633 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
634 struct isl_hash_table_entry
*entry
;
636 entry
= graph_find_edge_entry(graph
, i
, edge
->src
, edge
->dst
);
639 if (entry
->data
!= edge
)
641 isl_hash_table_remove(ctx
, graph
->edge_table
[i
], entry
);
645 /* Check whether the dependence graph has any edge
646 * between the given two nodes.
648 static int graph_has_any_edge(struct isl_sched_graph
*graph
,
649 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
651 enum isl_edge_type i
;
654 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
655 r
= graph_has_edge(graph
, i
, src
, dst
);
663 /* Check whether the dependence graph has a validity edge
664 * between the given two nodes.
666 * Conditional validity edges are essentially validity edges that
667 * can be ignored if the corresponding condition edges are iteration private.
668 * Here, we are only checking for the presence of validity
669 * edges, so we need to consider the conditional validity edges too.
670 * In particular, this function is used during the detection
671 * of strongly connected components and we cannot ignore
672 * conditional validity edges during this detection.
674 static int graph_has_validity_edge(struct isl_sched_graph
*graph
,
675 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
679 r
= graph_has_edge(graph
, isl_edge_validity
, src
, dst
);
683 return graph_has_edge(graph
, isl_edge_conditional_validity
, src
, dst
);
686 static int graph_alloc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
687 int n_node
, int n_edge
)
692 graph
->n_edge
= n_edge
;
693 graph
->node
= isl_calloc_array(ctx
, struct isl_sched_node
, graph
->n
);
694 graph
->sorted
= isl_calloc_array(ctx
, int, graph
->n
);
695 graph
->region
= isl_alloc_array(ctx
, struct isl_region
, graph
->n
);
696 graph
->edge
= isl_calloc_array(ctx
,
697 struct isl_sched_edge
, graph
->n_edge
);
699 graph
->intra_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
700 graph
->inter_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
702 if (!graph
->node
|| !graph
->region
|| (graph
->n_edge
&& !graph
->edge
) ||
706 for(i
= 0; i
< graph
->n
; ++i
)
707 graph
->sorted
[i
] = i
;
712 static void graph_free(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
716 isl_map_to_basic_set_free(graph
->intra_hmap
);
717 isl_map_to_basic_set_free(graph
->inter_hmap
);
719 for (i
= 0; i
< graph
->n
; ++i
) {
720 isl_space_free(graph
->node
[i
].dim
);
721 isl_mat_free(graph
->node
[i
].sched
);
722 isl_map_free(graph
->node
[i
].sched_map
);
723 isl_mat_free(graph
->node
[i
].cmap
);
724 isl_mat_free(graph
->node
[i
].cinv
);
726 free(graph
->node
[i
].band
);
727 free(graph
->node
[i
].band_id
);
728 free(graph
->node
[i
].coincident
);
733 for (i
= 0; i
< graph
->n_edge
; ++i
) {
734 isl_map_free(graph
->edge
[i
].map
);
735 isl_union_map_free(graph
->edge
[i
].tagged_condition
);
736 isl_union_map_free(graph
->edge
[i
].tagged_validity
);
740 for (i
= 0; i
<= isl_edge_last
; ++i
)
741 isl_hash_table_free(ctx
, graph
->edge_table
[i
]);
742 isl_hash_table_free(ctx
, graph
->node_table
);
743 isl_basic_set_free(graph
->lp
);
746 /* For each "set" on which this function is called, increment
747 * graph->n by one and update graph->maxvar.
749 static int init_n_maxvar(__isl_take isl_set
*set
, void *user
)
751 struct isl_sched_graph
*graph
= user
;
752 int nvar
= isl_set_dim(set
, isl_dim_set
);
755 if (nvar
> graph
->maxvar
)
756 graph
->maxvar
= nvar
;
763 /* Compute the number of rows that should be allocated for the schedule.
764 * The graph can be split at most "n - 1" times, there can be at most
765 * two rows for each dimension in the iteration domains (in particular,
766 * we usually have one row, but it may be split by split_scaled),
767 * and there can be one extra row for ordering the statements.
768 * Note that if we have actually split "n - 1" times, then no ordering
769 * is needed, so in principle we could use "graph->n + 2 * graph->maxvar - 1".
771 static int compute_max_row(struct isl_sched_graph
*graph
,
772 __isl_keep isl_union_set
*domain
)
776 if (isl_union_set_foreach_set(domain
, &init_n_maxvar
, graph
) < 0)
778 graph
->max_row
= graph
->n
+ 2 * graph
->maxvar
;
783 /* Add a new node to the graph representing the given set.
785 static int extract_node(__isl_take isl_set
*set
, void *user
)
791 struct isl_sched_graph
*graph
= user
;
792 int *band
, *band_id
, *coincident
;
794 ctx
= isl_set_get_ctx(set
);
795 dim
= isl_set_get_space(set
);
797 nvar
= isl_space_dim(dim
, isl_dim_set
);
798 nparam
= isl_space_dim(dim
, isl_dim_param
);
799 if (!ctx
->opt
->schedule_parametric
)
801 sched
= isl_mat_alloc(ctx
, 0, 1 + nparam
+ nvar
);
802 graph
->node
[graph
->n
].dim
= dim
;
803 graph
->node
[graph
->n
].nvar
= nvar
;
804 graph
->node
[graph
->n
].nparam
= nparam
;
805 graph
->node
[graph
->n
].sched
= sched
;
806 graph
->node
[graph
->n
].sched_map
= NULL
;
807 band
= isl_alloc_array(ctx
, int, graph
->max_row
);
808 graph
->node
[graph
->n
].band
= band
;
809 band_id
= isl_calloc_array(ctx
, int, graph
->max_row
);
810 graph
->node
[graph
->n
].band_id
= band_id
;
811 coincident
= isl_calloc_array(ctx
, int, graph
->max_row
);
812 graph
->node
[graph
->n
].coincident
= coincident
;
815 if (!sched
|| (graph
->max_row
&& (!band
|| !band_id
|| !coincident
)))
821 struct isl_extract_edge_data
{
822 enum isl_edge_type type
;
823 struct isl_sched_graph
*graph
;
826 /* Merge edge2 into edge1, freeing the contents of edge2.
827 * "type" is the type of the schedule constraint from which edge2 was
829 * Return 0 on success and -1 on failure.
831 * edge1 and edge2 are assumed to have the same value for the map field.
833 static int merge_edge(enum isl_edge_type type
, struct isl_sched_edge
*edge1
,
834 struct isl_sched_edge
*edge2
)
836 edge1
->validity
|= edge2
->validity
;
837 edge1
->coincidence
|= edge2
->coincidence
;
838 edge1
->proximity
|= edge2
->proximity
;
839 edge1
->condition
|= edge2
->condition
;
840 edge1
->conditional_validity
|= edge2
->conditional_validity
;
841 isl_map_free(edge2
->map
);
843 if (type
== isl_edge_condition
) {
844 if (!edge1
->tagged_condition
)
845 edge1
->tagged_condition
= edge2
->tagged_condition
;
847 edge1
->tagged_condition
=
848 isl_union_map_union(edge1
->tagged_condition
,
849 edge2
->tagged_condition
);
852 if (type
== isl_edge_conditional_validity
) {
853 if (!edge1
->tagged_validity
)
854 edge1
->tagged_validity
= edge2
->tagged_validity
;
856 edge1
->tagged_validity
=
857 isl_union_map_union(edge1
->tagged_validity
,
858 edge2
->tagged_validity
);
861 if (type
== isl_edge_condition
&& !edge1
->tagged_condition
)
863 if (type
== isl_edge_conditional_validity
&& !edge1
->tagged_validity
)
869 /* Insert dummy tags in domain and range of "map".
871 * In particular, if "map" is of the form
877 * [A -> dummy_tag] -> [B -> dummy_tag]
879 * where the dummy_tags are identical and equal to any dummy tags
880 * introduced by any other call to this function.
882 static __isl_give isl_map
*insert_dummy_tags(__isl_take isl_map
*map
)
888 isl_set
*domain
, *range
;
890 ctx
= isl_map_get_ctx(map
);
892 id
= isl_id_alloc(ctx
, NULL
, &dummy
);
893 space
= isl_space_params(isl_map_get_space(map
));
894 space
= isl_space_set_from_params(space
);
895 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
896 space
= isl_space_map_from_set(space
);
898 domain
= isl_map_wrap(map
);
899 range
= isl_map_wrap(isl_map_universe(space
));
900 map
= isl_map_from_domain_and_range(domain
, range
);
901 map
= isl_map_zip(map
);
906 /* Add a new edge to the graph based on the given map
907 * and add it to data->graph->edge_table[data->type].
908 * If a dependence relation of a given type happens to be identical
909 * to one of the dependence relations of a type that was added before,
910 * then we don't create a new edge, but instead mark the original edge
911 * as also representing a dependence of the current type.
913 * Edges of type isl_edge_condition or isl_edge_conditional_validity
914 * may be specified as "tagged" dependence relations. That is, "map"
915 * may contain elements * (i -> a) -> (j -> b), where i -> j denotes
916 * the dependence on iterations and a and b are tags.
917 * edge->map is set to the relation containing the elements i -> j,
918 * while edge->tagged_condition and edge->tagged_validity contain
919 * the union of all the "map" relations
920 * for which extract_edge is called that result in the same edge->map.
922 static int extract_edge(__isl_take isl_map
*map
, void *user
)
924 isl_ctx
*ctx
= isl_map_get_ctx(map
);
925 struct isl_extract_edge_data
*data
= user
;
926 struct isl_sched_graph
*graph
= data
->graph
;
927 struct isl_sched_node
*src
, *dst
;
929 struct isl_sched_edge
*edge
;
930 isl_map
*tagged
= NULL
;
932 if (data
->type
== isl_edge_condition
||
933 data
->type
== isl_edge_conditional_validity
) {
934 if (isl_map_can_zip(map
)) {
935 tagged
= isl_map_copy(map
);
936 map
= isl_set_unwrap(isl_map_domain(isl_map_zip(map
)));
938 tagged
= insert_dummy_tags(isl_map_copy(map
));
942 dim
= isl_space_domain(isl_map_get_space(map
));
943 src
= graph_find_node(ctx
, graph
, dim
);
945 dim
= isl_space_range(isl_map_get_space(map
));
946 dst
= graph_find_node(ctx
, graph
, dim
);
951 isl_map_free(tagged
);
955 graph
->edge
[graph
->n_edge
].src
= src
;
956 graph
->edge
[graph
->n_edge
].dst
= dst
;
957 graph
->edge
[graph
->n_edge
].map
= map
;
958 graph
->edge
[graph
->n_edge
].validity
= 0;
959 graph
->edge
[graph
->n_edge
].coincidence
= 0;
960 graph
->edge
[graph
->n_edge
].proximity
= 0;
961 graph
->edge
[graph
->n_edge
].condition
= 0;
962 graph
->edge
[graph
->n_edge
].local
= 0;
963 graph
->edge
[graph
->n_edge
].conditional_validity
= 0;
964 graph
->edge
[graph
->n_edge
].tagged_condition
= NULL
;
965 graph
->edge
[graph
->n_edge
].tagged_validity
= NULL
;
966 if (data
->type
== isl_edge_validity
)
967 graph
->edge
[graph
->n_edge
].validity
= 1;
968 if (data
->type
== isl_edge_coincidence
)
969 graph
->edge
[graph
->n_edge
].coincidence
= 1;
970 if (data
->type
== isl_edge_proximity
)
971 graph
->edge
[graph
->n_edge
].proximity
= 1;
972 if (data
->type
== isl_edge_condition
) {
973 graph
->edge
[graph
->n_edge
].condition
= 1;
974 graph
->edge
[graph
->n_edge
].tagged_condition
=
975 isl_union_map_from_map(tagged
);
977 if (data
->type
== isl_edge_conditional_validity
) {
978 graph
->edge
[graph
->n_edge
].conditional_validity
= 1;
979 graph
->edge
[graph
->n_edge
].tagged_validity
=
980 isl_union_map_from_map(tagged
);
983 edge
= graph_find_matching_edge(graph
, &graph
->edge
[graph
->n_edge
]);
984 if (edge
== &graph
->edge
[graph
->n_edge
])
985 return graph_edge_table_add(ctx
, graph
, data
->type
,
986 &graph
->edge
[graph
->n_edge
++]);
988 if (merge_edge(data
->type
, edge
, &graph
->edge
[graph
->n_edge
]) < 0)
991 return graph_edge_table_add(ctx
, graph
, data
->type
, edge
);
994 /* Check whether there is any dependence from node[j] to node[i]
995 * or from node[i] to node[j].
997 static int node_follows_weak(int i
, int j
, void *user
)
1000 struct isl_sched_graph
*graph
= user
;
1002 f
= graph_has_any_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1005 return graph_has_any_edge(graph
, &graph
->node
[i
], &graph
->node
[j
]);
1008 /* Check whether there is a (conditional) validity dependence from node[j]
1009 * to node[i], forcing node[i] to follow node[j].
1011 static int node_follows_strong(int i
, int j
, void *user
)
1013 struct isl_sched_graph
*graph
= user
;
1015 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1018 /* Use Tarjan's algorithm for computing the strongly connected components
1019 * in the dependence graph (only validity edges).
1020 * If weak is set, we consider the graph to be undirected and
1021 * we effectively compute the (weakly) connected components.
1022 * Additionally, we also consider other edges when weak is set.
1024 static int detect_ccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
, int weak
)
1027 struct isl_tarjan_graph
*g
= NULL
;
1029 g
= isl_tarjan_graph_init(ctx
, graph
->n
,
1030 weak
? &node_follows_weak
: &node_follows_strong
, graph
);
1038 while (g
->order
[i
] != -1) {
1039 graph
->node
[g
->order
[i
]].scc
= graph
->scc
;
1047 isl_tarjan_graph_free(g
);
1052 /* Apply Tarjan's algorithm to detect the strongly connected components
1053 * in the dependence graph.
1055 static int detect_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1057 return detect_ccs(ctx
, graph
, 0);
1060 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1061 * in the dependence graph.
1063 static int detect_wccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1065 return detect_ccs(ctx
, graph
, 1);
1068 static int cmp_scc(const void *a
, const void *b
, void *data
)
1070 struct isl_sched_graph
*graph
= data
;
1074 return graph
->node
[*i1
].scc
- graph
->node
[*i2
].scc
;
1077 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1079 static int sort_sccs(struct isl_sched_graph
*graph
)
1081 return isl_sort(graph
->sorted
, graph
->n
, sizeof(int), &cmp_scc
, graph
);
1084 /* Given a dependence relation R from a node to itself,
1085 * construct the set of coefficients of valid constraints for elements
1086 * in that dependence relation.
1087 * In particular, the result contains tuples of coefficients
1088 * c_0, c_n, c_x such that
1090 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1094 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1096 * We choose here to compute the dual of delta R.
1097 * Alternatively, we could have computed the dual of R, resulting
1098 * in a set of tuples c_0, c_n, c_x, c_y, and then
1099 * plugged in (c_0, c_n, c_x, -c_x).
1101 static __isl_give isl_basic_set
*intra_coefficients(
1102 struct isl_sched_graph
*graph
, __isl_take isl_map
*map
)
1105 isl_basic_set
*coef
;
1107 if (isl_map_to_basic_set_has(graph
->intra_hmap
, map
))
1108 return isl_map_to_basic_set_get(graph
->intra_hmap
, map
);
1110 delta
= isl_set_remove_divs(isl_map_deltas(isl_map_copy(map
)));
1111 coef
= isl_set_coefficients(delta
);
1112 graph
->intra_hmap
= isl_map_to_basic_set_set(graph
->intra_hmap
, map
,
1113 isl_basic_set_copy(coef
));
1118 /* Given a dependence relation R, * construct the set of coefficients
1119 * of valid constraints for elements in that dependence relation.
1120 * In particular, the result contains tuples of coefficients
1121 * c_0, c_n, c_x, c_y such that
1123 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1126 static __isl_give isl_basic_set
*inter_coefficients(
1127 struct isl_sched_graph
*graph
, __isl_take isl_map
*map
)
1130 isl_basic_set
*coef
;
1132 if (isl_map_to_basic_set_has(graph
->inter_hmap
, map
))
1133 return isl_map_to_basic_set_get(graph
->inter_hmap
, map
);
1135 set
= isl_map_wrap(isl_map_remove_divs(isl_map_copy(map
)));
1136 coef
= isl_set_coefficients(set
);
1137 graph
->inter_hmap
= isl_map_to_basic_set_set(graph
->inter_hmap
, map
,
1138 isl_basic_set_copy(coef
));
1143 /* Add constraints to graph->lp that force validity for the given
1144 * dependence from a node i to itself.
1145 * That is, add constraints that enforce
1147 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1148 * = c_i_x (y - x) >= 0
1150 * for each (x,y) in R.
1151 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1152 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1153 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1154 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1156 * Actually, we do not construct constraints for the c_i_x themselves,
1157 * but for the coefficients of c_i_x written as a linear combination
1158 * of the columns in node->cmap.
1160 static int add_intra_validity_constraints(struct isl_sched_graph
*graph
,
1161 struct isl_sched_edge
*edge
)
1164 isl_map
*map
= isl_map_copy(edge
->map
);
1165 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1167 isl_dim_map
*dim_map
;
1168 isl_basic_set
*coef
;
1169 struct isl_sched_node
*node
= edge
->src
;
1171 coef
= intra_coefficients(graph
, map
);
1173 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1175 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1176 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(node
->cmap
));
1180 total
= isl_basic_set_total_dim(graph
->lp
);
1181 dim_map
= isl_dim_map_alloc(ctx
, total
);
1182 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
1183 isl_space_dim(dim
, isl_dim_set
), 1,
1185 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
1186 isl_space_dim(dim
, isl_dim_set
), 1,
1188 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1189 coef
->n_eq
, coef
->n_ineq
);
1190 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1192 isl_space_free(dim
);
1196 isl_space_free(dim
);
1200 /* Add constraints to graph->lp that force validity for the given
1201 * dependence from node i to node j.
1202 * That is, add constraints that enforce
1204 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1206 * for each (x,y) in R.
1207 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1208 * of valid constraints for R and then plug in
1209 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
1210 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1211 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1212 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1214 * Actually, we do not construct constraints for the c_*_x themselves,
1215 * but for the coefficients of c_*_x written as a linear combination
1216 * of the columns in node->cmap.
1218 static int add_inter_validity_constraints(struct isl_sched_graph
*graph
,
1219 struct isl_sched_edge
*edge
)
1222 isl_map
*map
= isl_map_copy(edge
->map
);
1223 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1225 isl_dim_map
*dim_map
;
1226 isl_basic_set
*coef
;
1227 struct isl_sched_node
*src
= edge
->src
;
1228 struct isl_sched_node
*dst
= edge
->dst
;
1230 coef
= inter_coefficients(graph
, map
);
1232 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1234 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1235 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(src
->cmap
));
1236 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1237 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
,
1238 isl_mat_copy(dst
->cmap
));
1242 total
= isl_basic_set_total_dim(graph
->lp
);
1243 dim_map
= isl_dim_map_alloc(ctx
, total
);
1245 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, 1);
1246 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, -1);
1247 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, 1);
1248 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
1249 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1251 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
1252 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1255 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, -1);
1256 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, 1);
1257 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, -1);
1258 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
1259 isl_space_dim(dim
, isl_dim_set
), 1,
1261 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
1262 isl_space_dim(dim
, isl_dim_set
), 1,
1265 edge
->start
= graph
->lp
->n_ineq
;
1266 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1267 coef
->n_eq
, coef
->n_ineq
);
1268 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1272 isl_space_free(dim
);
1273 edge
->end
= graph
->lp
->n_ineq
;
1277 isl_space_free(dim
);
1281 /* Add constraints to graph->lp that bound the dependence distance for the given
1282 * dependence from a node i to itself.
1283 * If s = 1, we add the constraint
1285 * c_i_x (y - x) <= m_0 + m_n n
1289 * -c_i_x (y - x) + m_0 + m_n n >= 0
1291 * for each (x,y) in R.
1292 * If s = -1, we add the constraint
1294 * -c_i_x (y - x) <= m_0 + m_n n
1298 * c_i_x (y - x) + m_0 + m_n n >= 0
1300 * for each (x,y) in R.
1301 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1302 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1303 * with each coefficient (except m_0) represented as a pair of non-negative
1306 * Actually, we do not construct constraints for the c_i_x themselves,
1307 * but for the coefficients of c_i_x written as a linear combination
1308 * of the columns in node->cmap.
1311 * If "local" is set, then we add constraints
1313 * c_i_x (y - x) <= 0
1317 * -c_i_x (y - x) <= 0
1319 * instead, forcing the dependence distance to be (less than or) equal to 0.
1320 * That is, we plug in (0, 0, -s * c_i_x),
1321 * Note that dependences marked local are treated as validity constraints
1322 * by add_all_validity_constraints and therefore also have
1323 * their distances bounded by 0 from below.
1325 static int add_intra_proximity_constraints(struct isl_sched_graph
*graph
,
1326 struct isl_sched_edge
*edge
, int s
, int local
)
1330 isl_map
*map
= isl_map_copy(edge
->map
);
1331 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1333 isl_dim_map
*dim_map
;
1334 isl_basic_set
*coef
;
1335 struct isl_sched_node
*node
= edge
->src
;
1337 coef
= intra_coefficients(graph
, map
);
1339 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1341 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1342 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(node
->cmap
));
1346 nparam
= isl_space_dim(node
->dim
, isl_dim_param
);
1347 total
= isl_basic_set_total_dim(graph
->lp
);
1348 dim_map
= isl_dim_map_alloc(ctx
, total
);
1351 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1352 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1353 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1355 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
1356 isl_space_dim(dim
, isl_dim_set
), 1,
1358 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
1359 isl_space_dim(dim
, isl_dim_set
), 1,
1361 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1362 coef
->n_eq
, coef
->n_ineq
);
1363 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1365 isl_space_free(dim
);
1369 isl_space_free(dim
);
1373 /* Add constraints to graph->lp that bound the dependence distance for the given
1374 * dependence from node i to node j.
1375 * If s = 1, we add the constraint
1377 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1382 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1385 * for each (x,y) in R.
1386 * If s = -1, we add the constraint
1388 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1393 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1396 * for each (x,y) in R.
1397 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1398 * of valid constraints for R and then plug in
1399 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1401 * with each coefficient (except m_0, c_j_0 and c_i_0)
1402 * represented as a pair of non-negative coefficients.
1404 * Actually, we do not construct constraints for the c_*_x themselves,
1405 * but for the coefficients of c_*_x written as a linear combination
1406 * of the columns in node->cmap.
1409 * If "local" is set, then we add constraints
1411 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1415 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
1417 * instead, forcing the dependence distance to be (less than or) equal to 0.
1418 * That is, we plug in
1419 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
1420 * Note that dependences marked local are treated as validity constraints
1421 * by add_all_validity_constraints and therefore also have
1422 * their distances bounded by 0 from below.
1424 static int add_inter_proximity_constraints(struct isl_sched_graph
*graph
,
1425 struct isl_sched_edge
*edge
, int s
, int local
)
1429 isl_map
*map
= isl_map_copy(edge
->map
);
1430 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1432 isl_dim_map
*dim_map
;
1433 isl_basic_set
*coef
;
1434 struct isl_sched_node
*src
= edge
->src
;
1435 struct isl_sched_node
*dst
= edge
->dst
;
1437 coef
= inter_coefficients(graph
, map
);
1439 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1441 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1442 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(src
->cmap
));
1443 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1444 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
,
1445 isl_mat_copy(dst
->cmap
));
1449 nparam
= isl_space_dim(src
->dim
, isl_dim_param
);
1450 total
= isl_basic_set_total_dim(graph
->lp
);
1451 dim_map
= isl_dim_map_alloc(ctx
, total
);
1454 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1455 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1456 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1459 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, -s
);
1460 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, s
);
1461 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, -s
);
1462 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
1463 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1465 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
1466 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1469 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, s
);
1470 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, -s
);
1471 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, s
);
1472 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
1473 isl_space_dim(dim
, isl_dim_set
), 1,
1475 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
1476 isl_space_dim(dim
, isl_dim_set
), 1,
1479 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1480 coef
->n_eq
, coef
->n_ineq
);
1481 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1483 isl_space_free(dim
);
1487 isl_space_free(dim
);
1491 /* Add all validity constraints to graph->lp.
1493 * An edge that is forced to be local needs to have its dependence
1494 * distances equal to zero. We take care of bounding them by 0 from below
1495 * here. add_all_proximity_constraints takes care of bounding them by 0
1498 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1499 * Otherwise, we ignore them.
1501 static int add_all_validity_constraints(struct isl_sched_graph
*graph
,
1502 int use_coincidence
)
1506 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1507 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1510 local
= edge
->local
|| (edge
->coincidence
&& use_coincidence
);
1511 if (!edge
->validity
&& !local
)
1513 if (edge
->src
!= edge
->dst
)
1515 if (add_intra_validity_constraints(graph
, edge
) < 0)
1519 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1520 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1523 local
= edge
->local
|| (edge
->coincidence
&& use_coincidence
);
1524 if (!edge
->validity
&& !local
)
1526 if (edge
->src
== edge
->dst
)
1528 if (add_inter_validity_constraints(graph
, edge
) < 0)
1535 /* Add constraints to graph->lp that bound the dependence distance
1536 * for all dependence relations.
1537 * If a given proximity dependence is identical to a validity
1538 * dependence, then the dependence distance is already bounded
1539 * from below (by zero), so we only need to bound the distance
1540 * from above. (This includes the case of "local" dependences
1541 * which are treated as validity dependence by add_all_validity_constraints.)
1542 * Otherwise, we need to bound the distance both from above and from below.
1544 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1545 * Otherwise, we ignore them.
1547 static int add_all_proximity_constraints(struct isl_sched_graph
*graph
,
1548 int use_coincidence
)
1552 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1553 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1556 local
= edge
->local
|| (edge
->coincidence
&& use_coincidence
);
1557 if (!edge
->proximity
&& !local
)
1559 if (edge
->src
== edge
->dst
&&
1560 add_intra_proximity_constraints(graph
, edge
, 1, local
) < 0)
1562 if (edge
->src
!= edge
->dst
&&
1563 add_inter_proximity_constraints(graph
, edge
, 1, local
) < 0)
1565 if (edge
->validity
|| local
)
1567 if (edge
->src
== edge
->dst
&&
1568 add_intra_proximity_constraints(graph
, edge
, -1, 0) < 0)
1570 if (edge
->src
!= edge
->dst
&&
1571 add_inter_proximity_constraints(graph
, edge
, -1, 0) < 0)
1578 /* Compute a basis for the rows in the linear part of the schedule
1579 * and extend this basis to a full basis. The remaining rows
1580 * can then be used to force linear independence from the rows
1583 * In particular, given the schedule rows S, we compute
1588 * with H the Hermite normal form of S. That is, all but the
1589 * first rank columns of H are zero and so each row in S is
1590 * a linear combination of the first rank rows of Q.
1591 * The matrix Q is then transposed because we will write the
1592 * coefficients of the next schedule row as a column vector s
1593 * and express this s as a linear combination s = Q c of the
1595 * Similarly, the matrix U is transposed such that we can
1596 * compute the coefficients c = U s from a schedule row s.
1598 static int node_update_cmap(struct isl_sched_node
*node
)
1601 int n_row
= isl_mat_rows(node
->sched
);
1603 H
= isl_mat_sub_alloc(node
->sched
, 0, n_row
,
1604 1 + node
->nparam
, node
->nvar
);
1606 H
= isl_mat_left_hermite(H
, 0, &U
, &Q
);
1607 isl_mat_free(node
->cmap
);
1608 isl_mat_free(node
->cinv
);
1609 node
->cmap
= isl_mat_transpose(Q
);
1610 node
->cinv
= isl_mat_transpose(U
);
1611 node
->rank
= isl_mat_initial_non_zero_cols(H
);
1614 if (!node
->cmap
|| !node
->cinv
|| node
->rank
< 0)
1619 /* How many times should we count the constraints in "edge"?
1621 * If carry is set, then we are counting the number of
1622 * (validity or conditional validity) constraints that will be added
1623 * in setup_carry_lp and we count each edge exactly once.
1625 * Otherwise, we count as follows
1626 * validity -> 1 (>= 0)
1627 * validity+proximity -> 2 (>= 0 and upper bound)
1628 * proximity -> 2 (lower and upper bound)
1629 * local(+any) -> 2 (>= 0 and <= 0)
1631 * If an edge is only marked conditional_validity then it counts
1632 * as zero since it is only checked afterwards.
1634 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1635 * Otherwise, we ignore them.
1637 static int edge_multiplicity(struct isl_sched_edge
*edge
, int carry
,
1638 int use_coincidence
)
1640 if (carry
&& !edge
->validity
&& !edge
->conditional_validity
)
1644 if (edge
->proximity
|| edge
->local
)
1646 if (use_coincidence
&& edge
->coincidence
)
1653 /* Count the number of equality and inequality constraints
1654 * that will be added for the given map.
1656 * "use_coincidence" is set if we should take into account coincidence edges.
1658 static int count_map_constraints(struct isl_sched_graph
*graph
,
1659 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
,
1660 int *n_eq
, int *n_ineq
, int carry
, int use_coincidence
)
1662 isl_basic_set
*coef
;
1663 int f
= edge_multiplicity(edge
, carry
, use_coincidence
);
1670 if (edge
->src
== edge
->dst
)
1671 coef
= intra_coefficients(graph
, map
);
1673 coef
= inter_coefficients(graph
, map
);
1676 *n_eq
+= f
* coef
->n_eq
;
1677 *n_ineq
+= f
* coef
->n_ineq
;
1678 isl_basic_set_free(coef
);
1683 /* Count the number of equality and inequality constraints
1684 * that will be added to the main lp problem.
1685 * We count as follows
1686 * validity -> 1 (>= 0)
1687 * validity+proximity -> 2 (>= 0 and upper bound)
1688 * proximity -> 2 (lower and upper bound)
1689 * local(+any) -> 2 (>= 0 and <= 0)
1691 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1692 * Otherwise, we ignore them.
1694 static int count_constraints(struct isl_sched_graph
*graph
,
1695 int *n_eq
, int *n_ineq
, int use_coincidence
)
1699 *n_eq
= *n_ineq
= 0;
1700 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1701 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1702 isl_map
*map
= isl_map_copy(edge
->map
);
1704 if (count_map_constraints(graph
, edge
, map
, n_eq
, n_ineq
,
1705 0, use_coincidence
) < 0)
1712 /* Count the number of constraints that will be added by
1713 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
1716 * In practice, add_bound_coefficient_constraints only adds inequalities.
1718 static int count_bound_coefficient_constraints(isl_ctx
*ctx
,
1719 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
1723 if (ctx
->opt
->schedule_max_coefficient
== -1)
1726 for (i
= 0; i
< graph
->n
; ++i
)
1727 *n_ineq
+= 2 * graph
->node
[i
].nparam
+ 2 * graph
->node
[i
].nvar
;
1732 /* Add constraints that bound the values of the variable and parameter
1733 * coefficients of the schedule.
1735 * The maximal value of the coefficients is defined by the option
1736 * 'schedule_max_coefficient'.
1738 static int add_bound_coefficient_constraints(isl_ctx
*ctx
,
1739 struct isl_sched_graph
*graph
)
1742 int max_coefficient
;
1745 max_coefficient
= ctx
->opt
->schedule_max_coefficient
;
1747 if (max_coefficient
== -1)
1750 total
= isl_basic_set_total_dim(graph
->lp
);
1752 for (i
= 0; i
< graph
->n
; ++i
) {
1753 struct isl_sched_node
*node
= &graph
->node
[i
];
1754 for (j
= 0; j
< 2 * node
->nparam
+ 2 * node
->nvar
; ++j
) {
1756 k
= isl_basic_set_alloc_inequality(graph
->lp
);
1759 dim
= 1 + node
->start
+ 1 + j
;
1760 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
1761 isl_int_set_si(graph
->lp
->ineq
[k
][dim
], -1);
1762 isl_int_set_si(graph
->lp
->ineq
[k
][0], max_coefficient
);
1769 /* Construct an ILP problem for finding schedule coefficients
1770 * that result in non-negative, but small dependence distances
1771 * over all dependences.
1772 * In particular, the dependence distances over proximity edges
1773 * are bounded by m_0 + m_n n and we compute schedule coefficients
1774 * with small values (preferably zero) of m_n and m_0.
1776 * All variables of the ILP are non-negative. The actual coefficients
1777 * may be negative, so each coefficient is represented as the difference
1778 * of two non-negative variables. The negative part always appears
1779 * immediately before the positive part.
1780 * Other than that, the variables have the following order
1782 * - sum of positive and negative parts of m_n coefficients
1784 * - sum of positive and negative parts of all c_n coefficients
1785 * (unconstrained when computing non-parametric schedules)
1786 * - sum of positive and negative parts of all c_x coefficients
1787 * - positive and negative parts of m_n coefficients
1790 * - positive and negative parts of c_i_n (if parametric)
1791 * - positive and negative parts of c_i_x
1793 * The c_i_x are not represented directly, but through the columns of
1794 * node->cmap. That is, the computed values are for variable t_i_x
1795 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
1797 * The constraints are those from the edges plus two or three equalities
1798 * to express the sums.
1800 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1801 * Otherwise, we ignore them.
1803 static int setup_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
1804 int use_coincidence
)
1814 int max_constant_term
;
1816 max_constant_term
= ctx
->opt
->schedule_max_constant_term
;
1818 parametric
= ctx
->opt
->schedule_parametric
;
1819 nparam
= isl_space_dim(graph
->node
[0].dim
, isl_dim_param
);
1821 total
= param_pos
+ 2 * nparam
;
1822 for (i
= 0; i
< graph
->n
; ++i
) {
1823 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
1824 if (node_update_cmap(node
) < 0)
1826 node
->start
= total
;
1827 total
+= 1 + 2 * (node
->nparam
+ node
->nvar
);
1830 if (count_constraints(graph
, &n_eq
, &n_ineq
, use_coincidence
) < 0)
1832 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
1835 dim
= isl_space_set_alloc(ctx
, 0, total
);
1836 isl_basic_set_free(graph
->lp
);
1837 n_eq
+= 2 + parametric
;
1838 if (max_constant_term
!= -1)
1841 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
1843 k
= isl_basic_set_alloc_equality(graph
->lp
);
1846 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
1847 isl_int_set_si(graph
->lp
->eq
[k
][1], -1);
1848 for (i
= 0; i
< 2 * nparam
; ++i
)
1849 isl_int_set_si(graph
->lp
->eq
[k
][1 + param_pos
+ i
], 1);
1852 k
= isl_basic_set_alloc_equality(graph
->lp
);
1855 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
1856 isl_int_set_si(graph
->lp
->eq
[k
][3], -1);
1857 for (i
= 0; i
< graph
->n
; ++i
) {
1858 int pos
= 1 + graph
->node
[i
].start
+ 1;
1860 for (j
= 0; j
< 2 * graph
->node
[i
].nparam
; ++j
)
1861 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
1865 k
= isl_basic_set_alloc_equality(graph
->lp
);
1868 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
1869 isl_int_set_si(graph
->lp
->eq
[k
][4], -1);
1870 for (i
= 0; i
< graph
->n
; ++i
) {
1871 struct isl_sched_node
*node
= &graph
->node
[i
];
1872 int pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
1874 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
1875 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
1878 if (max_constant_term
!= -1)
1879 for (i
= 0; i
< graph
->n
; ++i
) {
1880 struct isl_sched_node
*node
= &graph
->node
[i
];
1881 k
= isl_basic_set_alloc_inequality(graph
->lp
);
1884 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
1885 isl_int_set_si(graph
->lp
->ineq
[k
][1 + node
->start
], -1);
1886 isl_int_set_si(graph
->lp
->ineq
[k
][0], max_constant_term
);
1889 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
1891 if (add_all_validity_constraints(graph
, use_coincidence
) < 0)
1893 if (add_all_proximity_constraints(graph
, use_coincidence
) < 0)
1899 /* Analyze the conflicting constraint found by
1900 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
1901 * constraint of one of the edges between distinct nodes, living, moreover
1902 * in distinct SCCs, then record the source and sink SCC as this may
1903 * be a good place to cut between SCCs.
1905 static int check_conflict(int con
, void *user
)
1908 struct isl_sched_graph
*graph
= user
;
1910 if (graph
->src_scc
>= 0)
1913 con
-= graph
->lp
->n_eq
;
1915 if (con
>= graph
->lp
->n_ineq
)
1918 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1919 if (!graph
->edge
[i
].validity
)
1921 if (graph
->edge
[i
].src
== graph
->edge
[i
].dst
)
1923 if (graph
->edge
[i
].src
->scc
== graph
->edge
[i
].dst
->scc
)
1925 if (graph
->edge
[i
].start
> con
)
1927 if (graph
->edge
[i
].end
<= con
)
1929 graph
->src_scc
= graph
->edge
[i
].src
->scc
;
1930 graph
->dst_scc
= graph
->edge
[i
].dst
->scc
;
1936 /* Check whether the next schedule row of the given node needs to be
1937 * non-trivial. Lower-dimensional domains may have some trivial rows,
1938 * but as soon as the number of remaining required non-trivial rows
1939 * is as large as the number or remaining rows to be computed,
1940 * all remaining rows need to be non-trivial.
1942 static int needs_row(struct isl_sched_graph
*graph
, struct isl_sched_node
*node
)
1944 return node
->nvar
- node
->rank
>= graph
->maxvar
- graph
->n_row
;
1947 /* Solve the ILP problem constructed in setup_lp.
1948 * For each node such that all the remaining rows of its schedule
1949 * need to be non-trivial, we construct a non-triviality region.
1950 * This region imposes that the next row is independent of previous rows.
1951 * In particular the coefficients c_i_x are represented by t_i_x
1952 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
1953 * its first columns span the rows of the previously computed part
1954 * of the schedule. The non-triviality region enforces that at least
1955 * one of the remaining components of t_i_x is non-zero, i.e.,
1956 * that the new schedule row depends on at least one of the remaining
1959 static __isl_give isl_vec
*solve_lp(struct isl_sched_graph
*graph
)
1965 for (i
= 0; i
< graph
->n
; ++i
) {
1966 struct isl_sched_node
*node
= &graph
->node
[i
];
1967 int skip
= node
->rank
;
1968 graph
->region
[i
].pos
= node
->start
+ 1 + 2*(node
->nparam
+skip
);
1969 if (needs_row(graph
, node
))
1970 graph
->region
[i
].len
= 2 * (node
->nvar
- skip
);
1972 graph
->region
[i
].len
= 0;
1974 lp
= isl_basic_set_copy(graph
->lp
);
1975 sol
= isl_tab_basic_set_non_trivial_lexmin(lp
, 2, graph
->n
,
1976 graph
->region
, &check_conflict
, graph
);
1980 /* Update the schedules of all nodes based on the given solution
1981 * of the LP problem.
1982 * The new row is added to the current band.
1983 * All possibly negative coefficients are encoded as a difference
1984 * of two non-negative variables, so we need to perform the subtraction
1985 * here. Moreover, if use_cmap is set, then the solution does
1986 * not refer to the actual coefficients c_i_x, but instead to variables
1987 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
1988 * In this case, we then also need to perform this multiplication
1989 * to obtain the values of c_i_x.
1991 * If coincident is set, then the caller guarantees that the new
1992 * row satisfies the coincidence constraints.
1994 static int update_schedule(struct isl_sched_graph
*graph
,
1995 __isl_take isl_vec
*sol
, int use_cmap
, int coincident
)
1998 isl_vec
*csol
= NULL
;
2003 isl_die(sol
->ctx
, isl_error_internal
,
2004 "no solution found", goto error
);
2005 if (graph
->n_total_row
>= graph
->max_row
)
2006 isl_die(sol
->ctx
, isl_error_internal
,
2007 "too many schedule rows", goto error
);
2009 for (i
= 0; i
< graph
->n
; ++i
) {
2010 struct isl_sched_node
*node
= &graph
->node
[i
];
2011 int pos
= node
->start
;
2012 int row
= isl_mat_rows(node
->sched
);
2015 csol
= isl_vec_alloc(sol
->ctx
, node
->nvar
);
2019 isl_map_free(node
->sched_map
);
2020 node
->sched_map
= NULL
;
2021 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
2024 node
->sched
= isl_mat_set_element(node
->sched
, row
, 0,
2026 for (j
= 0; j
< node
->nparam
+ node
->nvar
; ++j
)
2027 isl_int_sub(sol
->el
[1 + pos
+ 1 + 2 * j
+ 1],
2028 sol
->el
[1 + pos
+ 1 + 2 * j
+ 1],
2029 sol
->el
[1 + pos
+ 1 + 2 * j
]);
2030 for (j
= 0; j
< node
->nparam
; ++j
)
2031 node
->sched
= isl_mat_set_element(node
->sched
,
2032 row
, 1 + j
, sol
->el
[1+pos
+1+2*j
+1]);
2033 for (j
= 0; j
< node
->nvar
; ++j
)
2034 isl_int_set(csol
->el
[j
],
2035 sol
->el
[1+pos
+1+2*(node
->nparam
+j
)+1]);
2037 csol
= isl_mat_vec_product(isl_mat_copy(node
->cmap
),
2041 for (j
= 0; j
< node
->nvar
; ++j
)
2042 node
->sched
= isl_mat_set_element(node
->sched
,
2043 row
, 1 + node
->nparam
+ j
, csol
->el
[j
]);
2044 node
->band
[graph
->n_total_row
] = graph
->n_band
;
2045 node
->coincident
[graph
->n_total_row
] = coincident
;
2051 graph
->n_total_row
++;
2060 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2061 * and return this isl_aff.
2063 static __isl_give isl_aff
*extract_schedule_row(__isl_take isl_local_space
*ls
,
2064 struct isl_sched_node
*node
, int row
)
2072 aff
= isl_aff_zero_on_domain(ls
);
2073 isl_mat_get_element(node
->sched
, row
, 0, &v
);
2074 aff
= isl_aff_set_constant(aff
, v
);
2075 for (j
= 0; j
< node
->nparam
; ++j
) {
2076 isl_mat_get_element(node
->sched
, row
, 1 + j
, &v
);
2077 aff
= isl_aff_set_coefficient(aff
, isl_dim_param
, j
, v
);
2079 for (j
= 0; j
< node
->nvar
; ++j
) {
2080 isl_mat_get_element(node
->sched
, row
, 1 + node
->nparam
+ j
, &v
);
2081 aff
= isl_aff_set_coefficient(aff
, isl_dim_in
, j
, v
);
2089 /* Convert node->sched into a multi_aff and return this multi_aff.
2091 static __isl_give isl_multi_aff
*node_extract_schedule_multi_aff(
2092 struct isl_sched_node
*node
)
2096 isl_local_space
*ls
;
2101 nrow
= isl_mat_rows(node
->sched
);
2102 ncol
= isl_mat_cols(node
->sched
) - 1;
2103 space
= isl_space_from_domain(isl_space_copy(node
->dim
));
2104 space
= isl_space_add_dims(space
, isl_dim_out
, nrow
);
2105 ma
= isl_multi_aff_zero(space
);
2106 ls
= isl_local_space_from_space(isl_space_copy(node
->dim
));
2108 for (i
= 0; i
< nrow
; ++i
) {
2109 aff
= extract_schedule_row(isl_local_space_copy(ls
), node
, i
);
2110 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
2113 isl_local_space_free(ls
);
2118 /* Convert node->sched into a map and return this map.
2120 * The result is cached in node->sched_map, which needs to be released
2121 * whenever node->sched is updated.
2123 static __isl_give isl_map
*node_extract_schedule(struct isl_sched_node
*node
)
2125 if (!node
->sched_map
) {
2128 ma
= node_extract_schedule_multi_aff(node
);
2129 node
->sched_map
= isl_map_from_multi_aff(ma
);
2132 return isl_map_copy(node
->sched_map
);
2135 /* Construct a map that can be used to update dependence relation
2136 * based on the current schedule.
2137 * That is, construct a map expressing that source and sink
2138 * are executed within the same iteration of the current schedule.
2139 * This map can then be intersected with the dependence relation.
2140 * This is not the most efficient way, but this shouldn't be a critical
2143 static __isl_give isl_map
*specializer(struct isl_sched_node
*src
,
2144 struct isl_sched_node
*dst
)
2146 isl_map
*src_sched
, *dst_sched
;
2148 src_sched
= node_extract_schedule(src
);
2149 dst_sched
= node_extract_schedule(dst
);
2150 return isl_map_apply_range(src_sched
, isl_map_reverse(dst_sched
));
2153 /* Intersect the domains of the nested relations in domain and range
2154 * of "umap" with "map".
2156 static __isl_give isl_union_map
*intersect_domains(
2157 __isl_take isl_union_map
*umap
, __isl_keep isl_map
*map
)
2159 isl_union_set
*uset
;
2161 umap
= isl_union_map_zip(umap
);
2162 uset
= isl_union_set_from_set(isl_map_wrap(isl_map_copy(map
)));
2163 umap
= isl_union_map_intersect_domain(umap
, uset
);
2164 umap
= isl_union_map_zip(umap
);
2168 /* Update the dependence relation of the given edge based
2169 * on the current schedule.
2170 * If the dependence is carried completely by the current schedule, then
2171 * it is removed from the edge_tables. It is kept in the list of edges
2172 * as otherwise all edge_tables would have to be recomputed.
2174 static int update_edge(struct isl_sched_graph
*graph
,
2175 struct isl_sched_edge
*edge
)
2179 id
= specializer(edge
->src
, edge
->dst
);
2180 edge
->map
= isl_map_intersect(edge
->map
, isl_map_copy(id
));
2184 if (edge
->tagged_condition
) {
2185 edge
->tagged_condition
=
2186 intersect_domains(edge
->tagged_condition
, id
);
2187 if (!edge
->tagged_condition
)
2190 if (edge
->tagged_validity
) {
2191 edge
->tagged_validity
=
2192 intersect_domains(edge
->tagged_validity
, id
);
2193 if (!edge
->tagged_validity
)
2198 if (isl_map_plain_is_empty(edge
->map
))
2199 graph_remove_edge(graph
, edge
);
2207 /* Update the dependence relations of all edges based on the current schedule.
2209 static int update_edges(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2213 for (i
= graph
->n_edge
- 1; i
>= 0; --i
) {
2214 if (update_edge(graph
, &graph
->edge
[i
]) < 0)
2221 static void next_band(struct isl_sched_graph
*graph
)
2223 graph
->band_start
= graph
->n_total_row
;
2227 /* Topologically sort statements mapped to the same schedule iteration
2228 * and add a row to the schedule corresponding to this order.
2230 static int sort_statements(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2237 if (update_edges(ctx
, graph
) < 0)
2240 if (graph
->n_edge
== 0)
2243 if (detect_sccs(ctx
, graph
) < 0)
2246 if (graph
->n_total_row
>= graph
->max_row
)
2247 isl_die(ctx
, isl_error_internal
,
2248 "too many schedule rows", return -1);
2250 for (i
= 0; i
< graph
->n
; ++i
) {
2251 struct isl_sched_node
*node
= &graph
->node
[i
];
2252 int row
= isl_mat_rows(node
->sched
);
2253 int cols
= isl_mat_cols(node
->sched
);
2255 isl_map_free(node
->sched_map
);
2256 node
->sched_map
= NULL
;
2257 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
2260 node
->sched
= isl_mat_set_element_si(node
->sched
, row
, 0,
2262 for (j
= 1; j
< cols
; ++j
)
2263 node
->sched
= isl_mat_set_element_si(node
->sched
,
2265 node
->band
[graph
->n_total_row
] = graph
->n_band
;
2268 graph
->n_total_row
++;
2274 /* Construct an isl_schedule based on the computed schedule stored
2275 * in graph and with parameters specified by dim.
2277 static __isl_give isl_schedule
*extract_schedule(struct isl_sched_graph
*graph
,
2278 __isl_take isl_space
*dim
)
2282 isl_schedule
*sched
= NULL
;
2287 ctx
= isl_space_get_ctx(dim
);
2288 sched
= isl_calloc(ctx
, struct isl_schedule
,
2289 sizeof(struct isl_schedule
) +
2290 (graph
->n
- 1) * sizeof(struct isl_schedule_node
));
2295 sched
->n
= graph
->n
;
2296 sched
->n_band
= graph
->n_band
;
2297 sched
->n_total_row
= graph
->n_total_row
;
2299 for (i
= 0; i
< sched
->n
; ++i
) {
2301 int *band_end
, *band_id
, *coincident
;
2303 sched
->node
[i
].sched
=
2304 node_extract_schedule_multi_aff(&graph
->node
[i
]);
2305 if (!sched
->node
[i
].sched
)
2308 sched
->node
[i
].n_band
= graph
->n_band
;
2309 if (graph
->n_band
== 0)
2312 band_end
= isl_alloc_array(ctx
, int, graph
->n_band
);
2313 band_id
= isl_alloc_array(ctx
, int, graph
->n_band
);
2314 coincident
= isl_alloc_array(ctx
, int, graph
->n_total_row
);
2315 sched
->node
[i
].band_end
= band_end
;
2316 sched
->node
[i
].band_id
= band_id
;
2317 sched
->node
[i
].coincident
= coincident
;
2318 if (!band_end
|| !band_id
|| !coincident
)
2321 for (r
= 0; r
< graph
->n_total_row
; ++r
)
2322 coincident
[r
] = graph
->node
[i
].coincident
[r
];
2323 for (r
= b
= 0; r
< graph
->n_total_row
; ++r
) {
2324 if (graph
->node
[i
].band
[r
] == b
)
2327 if (graph
->node
[i
].band
[r
] == -1)
2330 if (r
== graph
->n_total_row
)
2332 sched
->node
[i
].n_band
= b
;
2333 for (--b
; b
>= 0; --b
)
2334 band_id
[b
] = graph
->node
[i
].band_id
[b
];
2341 isl_space_free(dim
);
2342 isl_schedule_free(sched
);
2346 /* Copy nodes that satisfy node_pred from the src dependence graph
2347 * to the dst dependence graph.
2349 static int copy_nodes(struct isl_sched_graph
*dst
, struct isl_sched_graph
*src
,
2350 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
2355 for (i
= 0; i
< src
->n
; ++i
) {
2356 if (!node_pred(&src
->node
[i
], data
))
2358 dst
->node
[dst
->n
].dim
= isl_space_copy(src
->node
[i
].dim
);
2359 dst
->node
[dst
->n
].nvar
= src
->node
[i
].nvar
;
2360 dst
->node
[dst
->n
].nparam
= src
->node
[i
].nparam
;
2361 dst
->node
[dst
->n
].sched
= isl_mat_copy(src
->node
[i
].sched
);
2362 dst
->node
[dst
->n
].sched_map
=
2363 isl_map_copy(src
->node
[i
].sched_map
);
2364 dst
->node
[dst
->n
].band
= src
->node
[i
].band
;
2365 dst
->node
[dst
->n
].band_id
= src
->node
[i
].band_id
;
2366 dst
->node
[dst
->n
].coincident
= src
->node
[i
].coincident
;
2373 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
2374 * to the dst dependence graph.
2375 * If the source or destination node of the edge is not in the destination
2376 * graph, then it must be a backward proximity edge and it should simply
2379 static int copy_edges(isl_ctx
*ctx
, struct isl_sched_graph
*dst
,
2380 struct isl_sched_graph
*src
,
2381 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
), int data
)
2384 enum isl_edge_type t
;
2387 for (i
= 0; i
< src
->n_edge
; ++i
) {
2388 struct isl_sched_edge
*edge
= &src
->edge
[i
];
2390 isl_union_map
*tagged_condition
;
2391 isl_union_map
*tagged_validity
;
2392 struct isl_sched_node
*dst_src
, *dst_dst
;
2394 if (!edge_pred(edge
, data
))
2397 if (isl_map_plain_is_empty(edge
->map
))
2400 dst_src
= graph_find_node(ctx
, dst
, edge
->src
->dim
);
2401 dst_dst
= graph_find_node(ctx
, dst
, edge
->dst
->dim
);
2402 if (!dst_src
|| !dst_dst
) {
2403 if (edge
->validity
|| edge
->conditional_validity
)
2404 isl_die(ctx
, isl_error_internal
,
2405 "backward (conditional) validity edge",
2410 map
= isl_map_copy(edge
->map
);
2411 tagged_condition
= isl_union_map_copy(edge
->tagged_condition
);
2412 tagged_validity
= isl_union_map_copy(edge
->tagged_validity
);
2414 dst
->edge
[dst
->n_edge
].src
= dst_src
;
2415 dst
->edge
[dst
->n_edge
].dst
= dst_dst
;
2416 dst
->edge
[dst
->n_edge
].map
= map
;
2417 dst
->edge
[dst
->n_edge
].tagged_condition
= tagged_condition
;
2418 dst
->edge
[dst
->n_edge
].tagged_validity
= tagged_validity
;
2419 dst
->edge
[dst
->n_edge
].validity
= edge
->validity
;
2420 dst
->edge
[dst
->n_edge
].proximity
= edge
->proximity
;
2421 dst
->edge
[dst
->n_edge
].coincidence
= edge
->coincidence
;
2422 dst
->edge
[dst
->n_edge
].condition
= edge
->condition
;
2423 dst
->edge
[dst
->n_edge
].conditional_validity
=
2424 edge
->conditional_validity
;
2427 if (edge
->tagged_condition
&& !tagged_condition
)
2429 if (edge
->tagged_validity
&& !tagged_validity
)
2432 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
2434 graph_find_edge(src
, t
, edge
->src
, edge
->dst
))
2436 if (graph_edge_table_add(ctx
, dst
, t
,
2437 &dst
->edge
[dst
->n_edge
- 1]) < 0)
2445 /* Given a "src" dependence graph that contains the nodes from "dst"
2446 * that satisfy node_pred, copy the schedule computed in "src"
2447 * for those nodes back to "dst".
2449 static int copy_schedule(struct isl_sched_graph
*dst
,
2450 struct isl_sched_graph
*src
,
2451 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
2456 for (i
= 0; i
< dst
->n
; ++i
) {
2457 if (!node_pred(&dst
->node
[i
], data
))
2459 isl_mat_free(dst
->node
[i
].sched
);
2460 isl_map_free(dst
->node
[i
].sched_map
);
2461 dst
->node
[i
].sched
= isl_mat_copy(src
->node
[src
->n
].sched
);
2462 dst
->node
[i
].sched_map
=
2463 isl_map_copy(src
->node
[src
->n
].sched_map
);
2467 dst
->max_row
= src
->max_row
;
2468 dst
->n_total_row
= src
->n_total_row
;
2469 dst
->n_band
= src
->n_band
;
2474 /* Compute the maximal number of variables over all nodes.
2475 * This is the maximal number of linearly independent schedule
2476 * rows that we need to compute.
2477 * Just in case we end up in a part of the dependence graph
2478 * with only lower-dimensional domains, we make sure we will
2479 * compute the required amount of extra linearly independent rows.
2481 static int compute_maxvar(struct isl_sched_graph
*graph
)
2486 for (i
= 0; i
< graph
->n
; ++i
) {
2487 struct isl_sched_node
*node
= &graph
->node
[i
];
2490 if (node_update_cmap(node
) < 0)
2492 nvar
= node
->nvar
+ graph
->n_row
- node
->rank
;
2493 if (nvar
> graph
->maxvar
)
2494 graph
->maxvar
= nvar
;
2500 static int compute_schedule(isl_ctx
*ctx
, struct isl_sched_graph
*graph
);
2501 static int compute_schedule_wcc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
);
2503 /* Compute a schedule for a subgraph of "graph". In particular, for
2504 * the graph composed of nodes that satisfy node_pred and edges that
2505 * that satisfy edge_pred. The caller should precompute the number
2506 * of nodes and edges that satisfy these predicates and pass them along
2507 * as "n" and "n_edge".
2508 * If the subgraph is known to consist of a single component, then wcc should
2509 * be set and then we call compute_schedule_wcc on the constructed subgraph.
2510 * Otherwise, we call compute_schedule, which will check whether the subgraph
2513 static int compute_sub_schedule(isl_ctx
*ctx
,
2514 struct isl_sched_graph
*graph
, int n
, int n_edge
,
2515 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
2516 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
2519 struct isl_sched_graph split
= { 0 };
2522 if (graph_alloc(ctx
, &split
, n
, n_edge
) < 0)
2524 if (copy_nodes(&split
, graph
, node_pred
, data
) < 0)
2526 if (graph_init_table(ctx
, &split
) < 0)
2528 for (t
= 0; t
<= isl_edge_last
; ++t
)
2529 split
.max_edge
[t
] = graph
->max_edge
[t
];
2530 if (graph_init_edge_tables(ctx
, &split
) < 0)
2532 if (copy_edges(ctx
, &split
, graph
, edge_pred
, data
) < 0)
2534 split
.n_row
= graph
->n_row
;
2535 split
.max_row
= graph
->max_row
;
2536 split
.n_total_row
= graph
->n_total_row
;
2537 split
.n_band
= graph
->n_band
;
2538 split
.band_start
= graph
->band_start
;
2540 if (wcc
&& compute_schedule_wcc(ctx
, &split
) < 0)
2542 if (!wcc
&& compute_schedule(ctx
, &split
) < 0)
2545 copy_schedule(graph
, &split
, node_pred
, data
);
2547 graph_free(ctx
, &split
);
2550 graph_free(ctx
, &split
);
2554 static int node_scc_exactly(struct isl_sched_node
*node
, int scc
)
2556 return node
->scc
== scc
;
2559 static int node_scc_at_most(struct isl_sched_node
*node
, int scc
)
2561 return node
->scc
<= scc
;
2564 static int node_scc_at_least(struct isl_sched_node
*node
, int scc
)
2566 return node
->scc
>= scc
;
2569 static int edge_scc_exactly(struct isl_sched_edge
*edge
, int scc
)
2571 return edge
->src
->scc
== scc
&& edge
->dst
->scc
== scc
;
2574 static int edge_dst_scc_at_most(struct isl_sched_edge
*edge
, int scc
)
2576 return edge
->dst
->scc
<= scc
;
2579 static int edge_src_scc_at_least(struct isl_sched_edge
*edge
, int scc
)
2581 return edge
->src
->scc
>= scc
;
2584 /* Pad the schedules of all nodes with zero rows such that in the end
2585 * they all have graph->n_total_row rows.
2586 * The extra rows don't belong to any band, so they get assigned band number -1.
2588 static int pad_schedule(struct isl_sched_graph
*graph
)
2592 for (i
= 0; i
< graph
->n
; ++i
) {
2593 struct isl_sched_node
*node
= &graph
->node
[i
];
2594 int row
= isl_mat_rows(node
->sched
);
2595 if (graph
->n_total_row
> row
) {
2596 isl_map_free(node
->sched_map
);
2597 node
->sched_map
= NULL
;
2599 node
->sched
= isl_mat_add_zero_rows(node
->sched
,
2600 graph
->n_total_row
- row
);
2603 for (j
= row
; j
< graph
->n_total_row
; ++j
)
2610 /* Reset the current band by dropping all its schedule rows.
2612 static int reset_band(struct isl_sched_graph
*graph
)
2617 drop
= graph
->n_total_row
- graph
->band_start
;
2618 graph
->n_total_row
-= drop
;
2619 graph
->n_row
-= drop
;
2621 for (i
= 0; i
< graph
->n
; ++i
) {
2622 struct isl_sched_node
*node
= &graph
->node
[i
];
2624 isl_map_free(node
->sched_map
);
2625 node
->sched_map
= NULL
;
2627 node
->sched
= isl_mat_drop_rows(node
->sched
,
2628 graph
->band_start
, drop
);
2637 /* Split the current graph into two parts and compute a schedule for each
2638 * part individually. In particular, one part consists of all SCCs up
2639 * to and including graph->src_scc, while the other part contains the other
2642 * The split is enforced in the schedule by constant rows with two different
2643 * values (0 and 1). These constant rows replace the previously computed rows
2644 * in the current band.
2645 * It would be possible to reuse them as the first rows in the next
2646 * band, but recomputing them may result in better rows as we are looking
2647 * at a smaller part of the dependence graph.
2649 * Since we do not enforce coincidence, we conservatively mark the
2650 * splitting row as not coincident.
2652 * The band_id of the second group is set to n, where n is the number
2653 * of nodes in the first group. This ensures that the band_ids over
2654 * the two groups remain disjoint, even if either or both of the two
2655 * groups contain independent components.
2657 static int compute_split_schedule(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2659 int i
, j
, n
, e1
, e2
;
2660 int n_total_row
, orig_total_row
;
2661 int n_band
, orig_band
;
2663 if (graph
->n_total_row
>= graph
->max_row
)
2664 isl_die(ctx
, isl_error_internal
,
2665 "too many schedule rows", return -1);
2667 if (reset_band(graph
) < 0)
2671 for (i
= 0; i
< graph
->n
; ++i
) {
2672 struct isl_sched_node
*node
= &graph
->node
[i
];
2673 int row
= isl_mat_rows(node
->sched
);
2674 int cols
= isl_mat_cols(node
->sched
);
2675 int before
= node
->scc
<= graph
->src_scc
;
2680 isl_map_free(node
->sched_map
);
2681 node
->sched_map
= NULL
;
2682 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
2685 node
->sched
= isl_mat_set_element_si(node
->sched
, row
, 0,
2687 for (j
= 1; j
< cols
; ++j
)
2688 node
->sched
= isl_mat_set_element_si(node
->sched
,
2690 node
->band
[graph
->n_total_row
] = graph
->n_band
;
2691 node
->coincident
[graph
->n_total_row
] = 0;
2695 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2696 if (graph
->edge
[i
].dst
->scc
<= graph
->src_scc
)
2698 if (graph
->edge
[i
].src
->scc
> graph
->src_scc
)
2702 graph
->n_total_row
++;
2705 for (i
= 0; i
< graph
->n
; ++i
) {
2706 struct isl_sched_node
*node
= &graph
->node
[i
];
2707 if (node
->scc
> graph
->src_scc
)
2708 node
->band_id
[graph
->n_band
] = n
;
2711 orig_total_row
= graph
->n_total_row
;
2712 orig_band
= graph
->n_band
;
2713 if (compute_sub_schedule(ctx
, graph
, n
, e1
,
2714 &node_scc_at_most
, &edge_dst_scc_at_most
,
2715 graph
->src_scc
, 0) < 0)
2717 n_total_row
= graph
->n_total_row
;
2718 graph
->n_total_row
= orig_total_row
;
2719 n_band
= graph
->n_band
;
2720 graph
->n_band
= orig_band
;
2721 if (compute_sub_schedule(ctx
, graph
, graph
->n
- n
, e2
,
2722 &node_scc_at_least
, &edge_src_scc_at_least
,
2723 graph
->src_scc
+ 1, 0) < 0)
2725 if (n_total_row
> graph
->n_total_row
)
2726 graph
->n_total_row
= n_total_row
;
2727 if (n_band
> graph
->n_band
)
2728 graph
->n_band
= n_band
;
2730 return pad_schedule(graph
);
2733 /* Compute the next band of the schedule after updating the dependence
2734 * relations based on the the current schedule.
2736 static int compute_next_band(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2738 if (update_edges(ctx
, graph
) < 0)
2742 return compute_schedule(ctx
, graph
);
2745 /* Add constraints to graph->lp that force the dependence "map" (which
2746 * is part of the dependence relation of "edge")
2747 * to be respected and attempt to carry it, where the edge is one from
2748 * a node j to itself. "pos" is the sequence number of the given map.
2749 * That is, add constraints that enforce
2751 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
2752 * = c_j_x (y - x) >= e_i
2754 * for each (x,y) in R.
2755 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2756 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
2757 * with each coefficient in c_j_x represented as a pair of non-negative
2760 static int add_intra_constraints(struct isl_sched_graph
*graph
,
2761 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
2764 isl_ctx
*ctx
= isl_map_get_ctx(map
);
2766 isl_dim_map
*dim_map
;
2767 isl_basic_set
*coef
;
2768 struct isl_sched_node
*node
= edge
->src
;
2770 coef
= intra_coefficients(graph
, map
);
2774 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
2776 total
= isl_basic_set_total_dim(graph
->lp
);
2777 dim_map
= isl_dim_map_alloc(ctx
, total
);
2778 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
2779 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
2780 isl_space_dim(dim
, isl_dim_set
), 1,
2782 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
2783 isl_space_dim(dim
, isl_dim_set
), 1,
2785 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
2786 coef
->n_eq
, coef
->n_ineq
);
2787 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
2789 isl_space_free(dim
);
2794 /* Add constraints to graph->lp that force the dependence "map" (which
2795 * is part of the dependence relation of "edge")
2796 * to be respected and attempt to carry it, where the edge is one from
2797 * node j to node k. "pos" is the sequence number of the given map.
2798 * That is, add constraints that enforce
2800 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
2802 * for each (x,y) in R.
2803 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2804 * of valid constraints for R and then plug in
2805 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
2806 * with each coefficient (except e_i, c_k_0 and c_j_0)
2807 * represented as a pair of non-negative coefficients.
2809 static int add_inter_constraints(struct isl_sched_graph
*graph
,
2810 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
2813 isl_ctx
*ctx
= isl_map_get_ctx(map
);
2815 isl_dim_map
*dim_map
;
2816 isl_basic_set
*coef
;
2817 struct isl_sched_node
*src
= edge
->src
;
2818 struct isl_sched_node
*dst
= edge
->dst
;
2820 coef
= inter_coefficients(graph
, map
);
2824 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
2826 total
= isl_basic_set_total_dim(graph
->lp
);
2827 dim_map
= isl_dim_map_alloc(ctx
, total
);
2829 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
2831 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, 1);
2832 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, -1);
2833 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, 1);
2834 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
2835 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
2837 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
2838 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
2841 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, -1);
2842 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, 1);
2843 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, -1);
2844 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
2845 isl_space_dim(dim
, isl_dim_set
), 1,
2847 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
2848 isl_space_dim(dim
, isl_dim_set
), 1,
2851 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
2852 coef
->n_eq
, coef
->n_ineq
);
2853 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
2855 isl_space_free(dim
);
2860 /* Add constraints to graph->lp that force all (conditional) validity
2861 * dependences to be respected and attempt to carry them.
2863 static int add_all_constraints(struct isl_sched_graph
*graph
)
2869 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2870 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2872 if (!edge
->validity
&& !edge
->conditional_validity
)
2875 for (j
= 0; j
< edge
->map
->n
; ++j
) {
2876 isl_basic_map
*bmap
;
2879 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
2880 map
= isl_map_from_basic_map(bmap
);
2882 if (edge
->src
== edge
->dst
&&
2883 add_intra_constraints(graph
, edge
, map
, pos
) < 0)
2885 if (edge
->src
!= edge
->dst
&&
2886 add_inter_constraints(graph
, edge
, map
, pos
) < 0)
2895 /* Count the number of equality and inequality constraints
2896 * that will be added to the carry_lp problem.
2897 * We count each edge exactly once.
2899 static int count_all_constraints(struct isl_sched_graph
*graph
,
2900 int *n_eq
, int *n_ineq
)
2904 *n_eq
= *n_ineq
= 0;
2905 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2906 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2907 for (j
= 0; j
< edge
->map
->n
; ++j
) {
2908 isl_basic_map
*bmap
;
2911 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
2912 map
= isl_map_from_basic_map(bmap
);
2914 if (count_map_constraints(graph
, edge
, map
,
2915 n_eq
, n_ineq
, 1, 0) < 0)
2923 /* Construct an LP problem for finding schedule coefficients
2924 * such that the schedule carries as many dependences as possible.
2925 * In particular, for each dependence i, we bound the dependence distance
2926 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
2927 * of all e_i's. Dependence with e_i = 0 in the solution are simply
2928 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
2929 * Note that if the dependence relation is a union of basic maps,
2930 * then we have to consider each basic map individually as it may only
2931 * be possible to carry the dependences expressed by some of those
2932 * basic maps and not all off them.
2933 * Below, we consider each of those basic maps as a separate "edge".
2935 * All variables of the LP are non-negative. The actual coefficients
2936 * may be negative, so each coefficient is represented as the difference
2937 * of two non-negative variables. The negative part always appears
2938 * immediately before the positive part.
2939 * Other than that, the variables have the following order
2941 * - sum of (1 - e_i) over all edges
2942 * - sum of positive and negative parts of all c_n coefficients
2943 * (unconstrained when computing non-parametric schedules)
2944 * - sum of positive and negative parts of all c_x coefficients
2949 * - positive and negative parts of c_i_n (if parametric)
2950 * - positive and negative parts of c_i_x
2952 * The constraints are those from the (validity) edges plus three equalities
2953 * to express the sums and n_edge inequalities to express e_i <= 1.
2955 static int setup_carry_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2965 for (i
= 0; i
< graph
->n_edge
; ++i
)
2966 n_edge
+= graph
->edge
[i
].map
->n
;
2969 for (i
= 0; i
< graph
->n
; ++i
) {
2970 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
2971 node
->start
= total
;
2972 total
+= 1 + 2 * (node
->nparam
+ node
->nvar
);
2975 if (count_all_constraints(graph
, &n_eq
, &n_ineq
) < 0)
2977 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2980 dim
= isl_space_set_alloc(ctx
, 0, total
);
2981 isl_basic_set_free(graph
->lp
);
2984 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
2985 graph
->lp
= isl_basic_set_set_rational(graph
->lp
);
2987 k
= isl_basic_set_alloc_equality(graph
->lp
);
2990 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2991 isl_int_set_si(graph
->lp
->eq
[k
][0], -n_edge
);
2992 isl_int_set_si(graph
->lp
->eq
[k
][1], 1);
2993 for (i
= 0; i
< n_edge
; ++i
)
2994 isl_int_set_si(graph
->lp
->eq
[k
][4 + i
], 1);
2996 k
= isl_basic_set_alloc_equality(graph
->lp
);
2999 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
3000 isl_int_set_si(graph
->lp
->eq
[k
][2], -1);
3001 for (i
= 0; i
< graph
->n
; ++i
) {
3002 int pos
= 1 + graph
->node
[i
].start
+ 1;
3004 for (j
= 0; j
< 2 * graph
->node
[i
].nparam
; ++j
)
3005 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
3008 k
= isl_basic_set_alloc_equality(graph
->lp
);
3011 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
3012 isl_int_set_si(graph
->lp
->eq
[k
][3], -1);
3013 for (i
= 0; i
< graph
->n
; ++i
) {
3014 struct isl_sched_node
*node
= &graph
->node
[i
];
3015 int pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
3017 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
3018 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
3021 for (i
= 0; i
< n_edge
; ++i
) {
3022 k
= isl_basic_set_alloc_inequality(graph
->lp
);
3025 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
3026 isl_int_set_si(graph
->lp
->ineq
[k
][4 + i
], -1);
3027 isl_int_set_si(graph
->lp
->ineq
[k
][0], 1);
3030 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
3032 if (add_all_constraints(graph
) < 0)
3038 /* If the schedule_split_scaled option is set and if the linear
3039 * parts of the scheduling rows for all nodes in the graphs have
3040 * non-trivial common divisor, then split off the constant term
3041 * from the linear part.
3042 * The constant term is then placed in a separate band and
3043 * the linear part is reduced.
3045 static int split_scaled(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3051 if (!ctx
->opt
->schedule_split_scaled
)
3056 if (graph
->n_total_row
>= graph
->max_row
)
3057 isl_die(ctx
, isl_error_internal
,
3058 "too many schedule rows", return -1);
3061 isl_int_init(gcd_i
);
3063 isl_int_set_si(gcd
, 0);
3065 row
= isl_mat_rows(graph
->node
[0].sched
) - 1;
3067 for (i
= 0; i
< graph
->n
; ++i
) {
3068 struct isl_sched_node
*node
= &graph
->node
[i
];
3069 int cols
= isl_mat_cols(node
->sched
);
3071 isl_seq_gcd(node
->sched
->row
[row
] + 1, cols
- 1, &gcd_i
);
3072 isl_int_gcd(gcd
, gcd
, gcd_i
);
3075 isl_int_clear(gcd_i
);
3077 if (isl_int_cmp_si(gcd
, 1) <= 0) {
3084 for (i
= 0; i
< graph
->n
; ++i
) {
3085 struct isl_sched_node
*node
= &graph
->node
[i
];
3087 isl_map_free(node
->sched_map
);
3088 node
->sched_map
= NULL
;
3089 node
->sched
= isl_mat_add_zero_rows(node
->sched
, 1);
3092 isl_int_fdiv_r(node
->sched
->row
[row
+ 1][0],
3093 node
->sched
->row
[row
][0], gcd
);
3094 isl_int_fdiv_q(node
->sched
->row
[row
][0],
3095 node
->sched
->row
[row
][0], gcd
);
3096 isl_int_mul(node
->sched
->row
[row
][0],
3097 node
->sched
->row
[row
][0], gcd
);
3098 node
->sched
= isl_mat_scale_down_row(node
->sched
, row
, gcd
);
3101 node
->band
[graph
->n_total_row
] = graph
->n_band
;
3104 graph
->n_total_row
++;
3113 static int compute_component_schedule(isl_ctx
*ctx
,
3114 struct isl_sched_graph
*graph
);
3116 /* Is the schedule row "sol" trivial on node "node"?
3117 * That is, is the solution zero on the dimensions orthogonal to
3118 * the previously found solutions?
3119 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3121 * Each coefficient is represented as the difference between
3122 * two non-negative values in "sol". "sol" has been computed
3123 * in terms of the original iterators (i.e., without use of cmap).
3124 * We construct the schedule row s and write it as a linear
3125 * combination of (linear combinations of) previously computed schedule rows.
3126 * s = Q c or c = U s.
3127 * If the final entries of c are all zero, then the solution is trivial.
3129 static int is_trivial(struct isl_sched_node
*node
, __isl_keep isl_vec
*sol
)
3139 if (node
->nvar
== node
->rank
)
3142 ctx
= isl_vec_get_ctx(sol
);
3143 node_sol
= isl_vec_alloc(ctx
, node
->nvar
);
3147 pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
3149 for (i
= 0; i
< node
->nvar
; ++i
)
3150 isl_int_sub(node_sol
->el
[i
],
3151 sol
->el
[pos
+ 2 * i
+ 1], sol
->el
[pos
+ 2 * i
]);
3153 node_sol
= isl_mat_vec_product(isl_mat_copy(node
->cinv
), node_sol
);
3158 trivial
= isl_seq_first_non_zero(node_sol
->el
+ node
->rank
,
3159 node
->nvar
- node
->rank
) == -1;
3161 isl_vec_free(node_sol
);
3166 /* Is the schedule row "sol" trivial on any node where it should
3168 * "sol" has been computed in terms of the original iterators
3169 * (i.e., without use of cmap).
3170 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3172 static int is_any_trivial(struct isl_sched_graph
*graph
,
3173 __isl_keep isl_vec
*sol
)
3177 for (i
= 0; i
< graph
->n
; ++i
) {
3178 struct isl_sched_node
*node
= &graph
->node
[i
];
3181 if (!needs_row(graph
, node
))
3183 trivial
= is_trivial(node
, sol
);
3184 if (trivial
< 0 || trivial
)
3191 /* Construct a schedule row for each node such that as many dependences
3192 * as possible are carried and then continue with the next band.
3194 * If the computed schedule row turns out to be trivial on one or
3195 * more nodes where it should not be trivial, then we throw it away
3196 * and try again on each component separately.
3198 static int carry_dependences(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3207 for (i
= 0; i
< graph
->n_edge
; ++i
)
3208 n_edge
+= graph
->edge
[i
].map
->n
;
3210 if (setup_carry_lp(ctx
, graph
) < 0)
3213 lp
= isl_basic_set_copy(graph
->lp
);
3214 sol
= isl_tab_basic_set_non_neg_lexmin(lp
);
3218 if (sol
->size
== 0) {
3220 isl_die(ctx
, isl_error_internal
,
3221 "error in schedule construction", return -1);
3224 isl_int_divexact(sol
->el
[1], sol
->el
[1], sol
->el
[0]);
3225 if (isl_int_cmp_si(sol
->el
[1], n_edge
) >= 0) {
3227 isl_die(ctx
, isl_error_unknown
,
3228 "unable to carry dependences", return -1);
3231 trivial
= is_any_trivial(graph
, sol
);
3233 sol
= isl_vec_free(sol
);
3234 } else if (trivial
) {
3237 return compute_component_schedule(ctx
, graph
);
3238 isl_die(ctx
, isl_error_unknown
,
3239 "unable to construct non-trivial solution", return -1);
3242 if (update_schedule(graph
, sol
, 0, 0) < 0)
3245 if (split_scaled(ctx
, graph
) < 0)
3248 return compute_next_band(ctx
, graph
);
3251 /* Are there any (non-empty) (conditional) validity edges in the graph?
3253 static int has_validity_edges(struct isl_sched_graph
*graph
)
3257 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3260 empty
= isl_map_plain_is_empty(graph
->edge
[i
].map
);
3265 if (graph
->edge
[i
].validity
||
3266 graph
->edge
[i
].conditional_validity
)
3273 /* Should we apply a Feautrier step?
3274 * That is, did the user request the Feautrier algorithm and are
3275 * there any validity dependences (left)?
3277 static int need_feautrier_step(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3279 if (ctx
->opt
->schedule_algorithm
!= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
)
3282 return has_validity_edges(graph
);
3285 /* Compute a schedule for a connected dependence graph using Feautrier's
3286 * multi-dimensional scheduling algorithm.
3287 * The original algorithm is described in [1].
3288 * The main idea is to minimize the number of scheduling dimensions, by
3289 * trying to satisfy as many dependences as possible per scheduling dimension.
3291 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
3292 * Problem, Part II: Multi-Dimensional Time.
3293 * In Intl. Journal of Parallel Programming, 1992.
3295 static int compute_schedule_wcc_feautrier(isl_ctx
*ctx
,
3296 struct isl_sched_graph
*graph
)
3298 return carry_dependences(ctx
, graph
);
3301 /* Turn off the "local" bit on all (condition) edges.
3303 static void clear_local_edges(struct isl_sched_graph
*graph
)
3307 for (i
= 0; i
< graph
->n_edge
; ++i
)
3308 if (graph
->edge
[i
].condition
)
3309 graph
->edge
[i
].local
= 0;
3312 /* Does "graph" have both condition and conditional validity edges?
3314 static int need_condition_check(struct isl_sched_graph
*graph
)
3317 int any_condition
= 0;
3318 int any_conditional_validity
= 0;
3320 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3321 if (graph
->edge
[i
].condition
)
3323 if (graph
->edge
[i
].conditional_validity
)
3324 any_conditional_validity
= 1;
3327 return any_condition
&& any_conditional_validity
;
3330 /* Does "graph" contain any coincidence edge?
3332 static int has_any_coincidence(struct isl_sched_graph
*graph
)
3336 for (i
= 0; i
< graph
->n_edge
; ++i
)
3337 if (graph
->edge
[i
].coincidence
)
3343 /* Extract the final schedule row as a map with the iteration domain
3344 * of "node" as domain.
3346 static __isl_give isl_map
*final_row(struct isl_sched_node
*node
)
3348 isl_local_space
*ls
;
3352 row
= isl_mat_rows(node
->sched
) - 1;
3353 ls
= isl_local_space_from_space(isl_space_copy(node
->dim
));
3354 aff
= extract_schedule_row(ls
, node
, row
);
3355 return isl_map_from_aff(aff
);
3358 /* Is the conditional validity dependence in the edge with index "edge_index"
3359 * violated by the latest (i.e., final) row of the schedule?
3360 * That is, is i scheduled after j
3361 * for any conditional validity dependence i -> j?
3363 static int is_violated(struct isl_sched_graph
*graph
, int edge_index
)
3365 isl_map
*src_sched
, *dst_sched
, *map
;
3366 struct isl_sched_edge
*edge
= &graph
->edge
[edge_index
];
3369 src_sched
= final_row(edge
->src
);
3370 dst_sched
= final_row(edge
->dst
);
3371 map
= isl_map_copy(edge
->map
);
3372 map
= isl_map_apply_domain(map
, src_sched
);
3373 map
= isl_map_apply_range(map
, dst_sched
);
3374 map
= isl_map_order_gt(map
, isl_dim_in
, 0, isl_dim_out
, 0);
3375 empty
= isl_map_is_empty(map
);
3384 /* Does the domain of "umap" intersect "uset"?
3386 static int domain_intersects(__isl_keep isl_union_map
*umap
,
3387 __isl_keep isl_union_set
*uset
)
3391 umap
= isl_union_map_copy(umap
);
3392 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(uset
));
3393 empty
= isl_union_map_is_empty(umap
);
3394 isl_union_map_free(umap
);
3396 return empty
< 0 ? -1 : !empty
;
3399 /* Does the range of "umap" intersect "uset"?
3401 static int range_intersects(__isl_keep isl_union_map
*umap
,
3402 __isl_keep isl_union_set
*uset
)
3406 umap
= isl_union_map_copy(umap
);
3407 umap
= isl_union_map_intersect_range(umap
, isl_union_set_copy(uset
));
3408 empty
= isl_union_map_is_empty(umap
);
3409 isl_union_map_free(umap
);
3411 return empty
< 0 ? -1 : !empty
;
3414 /* Are the condition dependences of "edge" local with respect to
3415 * the current schedule?
3417 * That is, are domain and range of the condition dependences mapped
3418 * to the same point?
3420 * In other words, is the condition false?
3422 static int is_condition_false(struct isl_sched_edge
*edge
)
3424 isl_union_map
*umap
;
3425 isl_map
*map
, *sched
, *test
;
3428 umap
= isl_union_map_copy(edge
->tagged_condition
);
3429 umap
= isl_union_map_zip(umap
);
3430 umap
= isl_union_set_unwrap(isl_union_map_domain(umap
));
3431 map
= isl_map_from_union_map(umap
);
3433 sched
= node_extract_schedule(edge
->src
);
3434 map
= isl_map_apply_domain(map
, sched
);
3435 sched
= node_extract_schedule(edge
->dst
);
3436 map
= isl_map_apply_range(map
, sched
);
3438 test
= isl_map_identity(isl_map_get_space(map
));
3439 local
= isl_map_is_subset(map
, test
);
3446 /* Does "graph" have any satisfied condition edges that
3447 * are adjacent to the conditional validity constraint with
3448 * domain "conditional_source" and range "conditional_sink"?
3450 * A satisfied condition is one that is not local.
3451 * If a condition was forced to be local already (i.e., marked as local)
3452 * then there is no need to check if it is in fact local.
3454 * Additionally, mark all adjacent condition edges found as local.
3456 static int has_adjacent_true_conditions(struct isl_sched_graph
*graph
,
3457 __isl_keep isl_union_set
*conditional_source
,
3458 __isl_keep isl_union_set
*conditional_sink
)
3463 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3464 int adjacent
, local
;
3465 isl_union_map
*condition
;
3467 if (!graph
->edge
[i
].condition
)
3469 if (graph
->edge
[i
].local
)
3472 condition
= graph
->edge
[i
].tagged_condition
;
3473 adjacent
= domain_intersects(condition
, conditional_sink
);
3474 if (adjacent
>= 0 && !adjacent
)
3475 adjacent
= range_intersects(condition
,
3476 conditional_source
);
3482 graph
->edge
[i
].local
= 1;
3484 local
= is_condition_false(&graph
->edge
[i
]);
3494 /* Are there any violated conditional validity dependences with
3495 * adjacent condition dependences that are not local with respect
3496 * to the current schedule?
3497 * That is, is the conditional validity constraint violated?
3499 * Additionally, mark all those adjacent condition dependences as local.
3500 * We also mark those adjacent condition dependences that were not marked
3501 * as local before, but just happened to be local already. This ensures
3502 * that they remain local if the schedule is recomputed.
3504 * We first collect domain and range of all violated conditional validity
3505 * dependences and then check if there are any adjacent non-local
3506 * condition dependences.
3508 static int has_violated_conditional_constraint(isl_ctx
*ctx
,
3509 struct isl_sched_graph
*graph
)
3513 isl_union_set
*source
, *sink
;
3515 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3516 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3517 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3518 isl_union_set
*uset
;
3519 isl_union_map
*umap
;
3522 if (!graph
->edge
[i
].conditional_validity
)
3525 violated
= is_violated(graph
, i
);
3533 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
3534 uset
= isl_union_map_domain(umap
);
3535 source
= isl_union_set_union(source
, uset
);
3536 source
= isl_union_set_coalesce(source
);
3538 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
3539 uset
= isl_union_map_range(umap
);
3540 sink
= isl_union_set_union(sink
, uset
);
3541 sink
= isl_union_set_coalesce(sink
);
3545 any
= has_adjacent_true_conditions(graph
, source
, sink
);
3547 isl_union_set_free(source
);
3548 isl_union_set_free(sink
);
3551 isl_union_set_free(source
);
3552 isl_union_set_free(sink
);
3556 /* Compute a schedule for a connected dependence graph.
3557 * We try to find a sequence of as many schedule rows as possible that result
3558 * in non-negative dependence distances (independent of the previous rows
3559 * in the sequence, i.e., such that the sequence is tilable), with as
3560 * many of the initial rows as possible satisfying the coincidence constraints.
3561 * If we can't find any more rows we either
3562 * - split between SCCs and start over (assuming we found an interesting
3563 * pair of SCCs between which to split)
3564 * - continue with the next band (assuming the current band has at least
3566 * - try to carry as many dependences as possible and continue with the next
3569 * If Feautrier's algorithm is selected, we first recursively try to satisfy
3570 * as many validity dependences as possible. When all validity dependences
3571 * are satisfied we extend the schedule to a full-dimensional schedule.
3573 * If we manage to complete the schedule, we finish off by topologically
3574 * sorting the statements based on the remaining dependences.
3576 * If ctx->opt->schedule_outer_coincidence is set, then we force the
3577 * outermost dimension to satisfy the coincidence constraints. If this
3578 * turns out to be impossible, we fall back on the general scheme above
3579 * and try to carry as many dependences as possible.
3581 * If "graph" contains both condition and conditional validity dependences,
3582 * then we need to check that that the conditional schedule constraint
3583 * is satisfied, i.e., there are no violated conditional validity dependences
3584 * that are adjacent to any non-local condition dependences.
3585 * If there are, then we mark all those adjacent condition dependences
3586 * as local and recompute the current band. Those dependences that
3587 * are marked local will then be forced to be local.
3588 * The initial computation is performed with no dependences marked as local.
3589 * If we are lucky, then there will be no violated conditional validity
3590 * dependences adjacent to any non-local condition dependences.
3591 * Otherwise, we mark some additional condition dependences as local and
3592 * recompute. We continue this process until there are no violations left or
3593 * until we are no longer able to compute a schedule.
3594 * Since there are only a finite number of dependences,
3595 * there will only be a finite number of iterations.
3597 static int compute_schedule_wcc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3599 int has_coincidence
;
3600 int use_coincidence
;
3601 int force_coincidence
= 0;
3602 int check_conditional
;
3604 if (detect_sccs(ctx
, graph
) < 0)
3606 if (sort_sccs(graph
) < 0)
3609 if (compute_maxvar(graph
) < 0)
3612 if (need_feautrier_step(ctx
, graph
))
3613 return compute_schedule_wcc_feautrier(ctx
, graph
);
3615 clear_local_edges(graph
);
3616 check_conditional
= need_condition_check(graph
);
3617 has_coincidence
= has_any_coincidence(graph
);
3619 if (ctx
->opt
->schedule_outer_coincidence
)
3620 force_coincidence
= 1;
3622 use_coincidence
= has_coincidence
;
3623 while (graph
->n_row
< graph
->maxvar
) {
3628 graph
->src_scc
= -1;
3629 graph
->dst_scc
= -1;
3631 if (setup_lp(ctx
, graph
, use_coincidence
) < 0)
3633 sol
= solve_lp(graph
);
3636 if (sol
->size
== 0) {
3637 int empty
= graph
->n_total_row
== graph
->band_start
;
3640 if (use_coincidence
&& (!force_coincidence
|| !empty
)) {
3641 use_coincidence
= 0;
3644 if (!ctx
->opt
->schedule_maximize_band_depth
&& !empty
)
3645 return compute_next_band(ctx
, graph
);
3646 if (graph
->src_scc
>= 0)
3647 return compute_split_schedule(ctx
, graph
);
3649 return compute_next_band(ctx
, graph
);
3650 return carry_dependences(ctx
, graph
);
3652 coincident
= !has_coincidence
|| use_coincidence
;
3653 if (update_schedule(graph
, sol
, 1, coincident
) < 0)
3656 if (!check_conditional
)
3658 violated
= has_violated_conditional_constraint(ctx
, graph
);
3663 if (reset_band(graph
) < 0)
3665 use_coincidence
= has_coincidence
;
3668 if (graph
->n_total_row
> graph
->band_start
)
3670 return sort_statements(ctx
, graph
);
3673 /* Add a row to the schedules that separates the SCCs and move
3676 static int split_on_scc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3680 if (graph
->n_total_row
>= graph
->max_row
)
3681 isl_die(ctx
, isl_error_internal
,
3682 "too many schedule rows", return -1);
3684 for (i
= 0; i
< graph
->n
; ++i
) {
3685 struct isl_sched_node
*node
= &graph
->node
[i
];
3686 int row
= isl_mat_rows(node
->sched
);
3688 isl_map_free(node
->sched_map
);
3689 node
->sched_map
= NULL
;
3690 node
->sched
= isl_mat_add_zero_rows(node
->sched
, 1);
3691 node
->sched
= isl_mat_set_element_si(node
->sched
, row
, 0,
3695 node
->band
[graph
->n_total_row
] = graph
->n_band
;
3698 graph
->n_total_row
++;
3704 /* Compute a schedule for each component (identified by node->scc)
3705 * of the dependence graph separately and then combine the results.
3706 * Depending on the setting of schedule_fuse, a component may be
3707 * either weakly or strongly connected.
3709 * The band_id is adjusted such that each component has a separate id.
3710 * Note that the band_id may have already been set to a value different
3711 * from zero by compute_split_schedule.
3713 static int compute_component_schedule(isl_ctx
*ctx
,
3714 struct isl_sched_graph
*graph
)
3718 int n_total_row
, orig_total_row
;
3719 int n_band
, orig_band
;
3721 if (ctx
->opt
->schedule_fuse
== ISL_SCHEDULE_FUSE_MIN
||
3722 ctx
->opt
->schedule_separate_components
)
3723 if (split_on_scc(ctx
, graph
) < 0)
3727 orig_total_row
= graph
->n_total_row
;
3729 orig_band
= graph
->n_band
;
3730 for (i
= 0; i
< graph
->n
; ++i
)
3731 graph
->node
[i
].band_id
[graph
->n_band
] += graph
->node
[i
].scc
;
3732 for (wcc
= 0; wcc
< graph
->scc
; ++wcc
) {
3734 for (i
= 0; i
< graph
->n
; ++i
)
3735 if (graph
->node
[i
].scc
== wcc
)
3738 for (i
= 0; i
< graph
->n_edge
; ++i
)
3739 if (graph
->edge
[i
].src
->scc
== wcc
&&
3740 graph
->edge
[i
].dst
->scc
== wcc
)
3743 if (compute_sub_schedule(ctx
, graph
, n
, n_edge
,
3745 &edge_scc_exactly
, wcc
, 1) < 0)
3747 if (graph
->n_total_row
> n_total_row
)
3748 n_total_row
= graph
->n_total_row
;
3749 graph
->n_total_row
= orig_total_row
;
3750 if (graph
->n_band
> n_band
)
3751 n_band
= graph
->n_band
;
3752 graph
->n_band
= orig_band
;
3755 graph
->n_total_row
= n_total_row
;
3756 graph
->n_band
= n_band
;
3758 return pad_schedule(graph
);
3761 /* Compute a schedule for the given dependence graph.
3762 * We first check if the graph is connected (through validity and conditional
3763 * validity dependences) and, if not, compute a schedule
3764 * for each component separately.
3765 * If schedule_fuse is set to minimal fusion, then we check for strongly
3766 * connected components instead and compute a separate schedule for
3767 * each such strongly connected component.
3769 static int compute_schedule(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3771 if (ctx
->opt
->schedule_fuse
== ISL_SCHEDULE_FUSE_MIN
) {
3772 if (detect_sccs(ctx
, graph
) < 0)
3775 if (detect_wccs(ctx
, graph
) < 0)
3780 return compute_component_schedule(ctx
, graph
);
3782 return compute_schedule_wcc(ctx
, graph
);
3785 /* Compute a schedule on sc->domain that respects the given schedule
3788 * In particular, the schedule respects all the validity dependences.
3789 * If the default isl scheduling algorithm is used, it tries to minimize
3790 * the dependence distances over the proximity dependences.
3791 * If Feautrier's scheduling algorithm is used, the proximity dependence
3792 * distances are only minimized during the extension to a full-dimensional
3795 * If there are any condition and conditional validity dependences,
3796 * then the conditional validity dependences may be violated inside
3797 * a tilable band, provided they have no adjacent non-local
3798 * condition dependences.
3800 __isl_give isl_schedule
*isl_schedule_constraints_compute_schedule(
3801 __isl_take isl_schedule_constraints
*sc
)
3803 isl_ctx
*ctx
= isl_schedule_constraints_get_ctx(sc
);
3804 struct isl_sched_graph graph
= { 0 };
3805 isl_schedule
*sched
;
3806 struct isl_extract_edge_data data
;
3807 enum isl_edge_type i
;
3809 sc
= isl_schedule_constraints_align_params(sc
);
3813 graph
.n
= isl_union_set_n_set(sc
->domain
);
3816 if (graph_alloc(ctx
, &graph
, graph
.n
,
3817 isl_schedule_constraints_n_map(sc
)) < 0)
3819 if (compute_max_row(&graph
, sc
->domain
) < 0)
3823 if (isl_union_set_foreach_set(sc
->domain
, &extract_node
, &graph
) < 0)
3825 if (graph_init_table(ctx
, &graph
) < 0)
3827 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
3828 graph
.max_edge
[i
] = isl_union_map_n_map(sc
->constraint
[i
]);
3829 if (graph_init_edge_tables(ctx
, &graph
) < 0)
3832 data
.graph
= &graph
;
3833 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
3835 if (isl_union_map_foreach_map(sc
->constraint
[i
],
3836 &extract_edge
, &data
) < 0)
3840 if (compute_schedule(ctx
, &graph
) < 0)
3844 sched
= extract_schedule(&graph
, isl_union_set_get_space(sc
->domain
));
3846 graph_free(ctx
, &graph
);
3847 isl_schedule_constraints_free(sc
);
3851 graph_free(ctx
, &graph
);
3852 isl_schedule_constraints_free(sc
);
3856 /* Compute a schedule for the given union of domains that respects
3857 * all the validity dependences and minimizes
3858 * the dependence distances over the proximity dependences.
3860 * This function is kept for backward compatibility.
3862 __isl_give isl_schedule
*isl_union_set_compute_schedule(
3863 __isl_take isl_union_set
*domain
,
3864 __isl_take isl_union_map
*validity
,
3865 __isl_take isl_union_map
*proximity
)
3867 isl_schedule_constraints
*sc
;
3869 sc
= isl_schedule_constraints_on_domain(domain
);
3870 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
3871 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
3873 return isl_schedule_constraints_compute_schedule(sc
);
3876 __isl_null isl_schedule
*isl_schedule_free(__isl_take isl_schedule
*sched
)
3882 if (--sched
->ref
> 0)
3885 for (i
= 0; i
< sched
->n
; ++i
) {
3886 isl_multi_aff_free(sched
->node
[i
].sched
);
3887 free(sched
->node
[i
].band_end
);
3888 free(sched
->node
[i
].band_id
);
3889 free(sched
->node
[i
].coincident
);
3891 isl_space_free(sched
->dim
);
3892 isl_band_list_free(sched
->band_forest
);
3897 isl_ctx
*isl_schedule_get_ctx(__isl_keep isl_schedule
*schedule
)
3899 return schedule
? isl_space_get_ctx(schedule
->dim
) : NULL
;
3902 /* Set max_out to the maximal number of output dimensions over
3905 static int update_max_out(__isl_take isl_map
*map
, void *user
)
3907 int *max_out
= user
;
3908 int n_out
= isl_map_dim(map
, isl_dim_out
);
3910 if (n_out
> *max_out
)
3917 /* Internal data structure for map_pad_range.
3919 * "max_out" is the maximal schedule dimension.
3920 * "res" collects the results.
3922 struct isl_pad_schedule_map_data
{
3927 /* Pad the range of the given map with zeros to data->max_out and
3928 * then add the result to data->res.
3930 static int map_pad_range(__isl_take isl_map
*map
, void *user
)
3932 struct isl_pad_schedule_map_data
*data
= user
;
3934 int n_out
= isl_map_dim(map
, isl_dim_out
);
3936 map
= isl_map_add_dims(map
, isl_dim_out
, data
->max_out
- n_out
);
3937 for (i
= n_out
; i
< data
->max_out
; ++i
)
3938 map
= isl_map_fix_si(map
, isl_dim_out
, i
, 0);
3940 data
->res
= isl_union_map_add_map(data
->res
, map
);
3947 /* Pad the ranges of the maps in the union map with zeros such they all have
3948 * the same dimension.
3950 static __isl_give isl_union_map
*pad_schedule_map(
3951 __isl_take isl_union_map
*umap
)
3953 struct isl_pad_schedule_map_data data
;
3957 if (isl_union_map_n_map(umap
) <= 1)
3961 if (isl_union_map_foreach_map(umap
, &update_max_out
, &data
.max_out
) < 0)
3962 return isl_union_map_free(umap
);
3964 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3965 if (isl_union_map_foreach_map(umap
, &map_pad_range
, &data
) < 0)
3966 data
.res
= isl_union_map_free(data
.res
);
3968 isl_union_map_free(umap
);
3972 /* Return an isl_union_map of the schedule. If we have already constructed
3973 * a band forest, then this band forest may have been modified so we need
3974 * to extract the isl_union_map from the forest rather than from
3975 * the originally computed schedule. This reconstructed schedule map
3976 * then needs to be padded with zeros to unify the schedule space
3977 * since the result of isl_band_list_get_suffix_schedule may not have
3978 * a unified schedule space.
3980 __isl_give isl_union_map
*isl_schedule_get_map(__isl_keep isl_schedule
*sched
)
3983 isl_union_map
*umap
;
3988 if (sched
->band_forest
) {
3989 umap
= isl_band_list_get_suffix_schedule(sched
->band_forest
);
3990 return pad_schedule_map(umap
);
3993 umap
= isl_union_map_empty(isl_space_copy(sched
->dim
));
3994 for (i
= 0; i
< sched
->n
; ++i
) {
3997 ma
= isl_multi_aff_copy(sched
->node
[i
].sched
);
3998 umap
= isl_union_map_add_map(umap
, isl_map_from_multi_aff(ma
));
4004 static __isl_give isl_band_list
*construct_band_list(
4005 __isl_keep isl_schedule
*schedule
, __isl_keep isl_band
*parent
,
4006 int band_nr
, int *parent_active
, int n_active
);
4008 /* Construct an isl_band structure for the band in the given schedule
4009 * with sequence number band_nr for the n_active nodes marked by active.
4010 * If the nodes don't have a band with the given sequence number,
4011 * then a band without members is created.
4013 * Because of the way the schedule is constructed, we know that
4014 * the position of the band inside the schedule of a node is the same
4015 * for all active nodes.
4017 * The partial schedule for the band is created before the children
4018 * are created to that construct_band_list can refer to the partial
4019 * schedule of the parent.
4021 static __isl_give isl_band
*construct_band(__isl_keep isl_schedule
*schedule
,
4022 __isl_keep isl_band
*parent
,
4023 int band_nr
, int *active
, int n_active
)
4026 isl_ctx
*ctx
= isl_schedule_get_ctx(schedule
);
4028 unsigned start
, end
;
4030 band
= isl_band_alloc(ctx
);
4034 band
->schedule
= schedule
;
4035 band
->parent
= parent
;
4037 for (i
= 0; i
< schedule
->n
; ++i
)
4041 if (i
>= schedule
->n
)
4042 isl_die(ctx
, isl_error_internal
,
4043 "band without active statements", goto error
);
4045 start
= band_nr
? schedule
->node
[i
].band_end
[band_nr
- 1] : 0;
4046 end
= band_nr
< schedule
->node
[i
].n_band
?
4047 schedule
->node
[i
].band_end
[band_nr
] : start
;
4048 band
->n
= end
- start
;
4050 band
->coincident
= isl_alloc_array(ctx
, int, band
->n
);
4051 if (band
->n
&& !band
->coincident
)
4054 for (j
= 0; j
< band
->n
; ++j
)
4055 band
->coincident
[j
] = schedule
->node
[i
].coincident
[start
+ j
];
4057 band
->pma
= isl_union_pw_multi_aff_empty(isl_space_copy(schedule
->dim
));
4058 for (i
= 0; i
< schedule
->n
; ++i
) {
4060 isl_pw_multi_aff
*pma
;
4066 ma
= isl_multi_aff_copy(schedule
->node
[i
].sched
);
4067 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
4068 ma
= isl_multi_aff_drop_dims(ma
, isl_dim_out
, end
, n_out
- end
);
4069 ma
= isl_multi_aff_drop_dims(ma
, isl_dim_out
, 0, start
);
4070 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
4071 band
->pma
= isl_union_pw_multi_aff_add_pw_multi_aff(band
->pma
,
4077 for (i
= 0; i
< schedule
->n
; ++i
)
4078 if (active
[i
] && schedule
->node
[i
].n_band
> band_nr
+ 1)
4081 if (i
< schedule
->n
) {
4082 band
->children
= construct_band_list(schedule
, band
,
4083 band_nr
+ 1, active
, n_active
);
4084 if (!band
->children
)
4090 isl_band_free(band
);
4094 /* Internal data structure used inside cmp_band and pw_multi_aff_extract_int.
4096 * r is set to a negative value if anything goes wrong.
4098 * c1 stores the result of extract_int.
4099 * c2 is a temporary value used inside cmp_band_in_ancestor.
4100 * t is a temporary value used inside extract_int.
4102 * first and equal are used inside extract_int.
4103 * first is set if we are looking at the first isl_multi_aff inside
4104 * the isl_union_pw_multi_aff.
4105 * equal is set if all the isl_multi_affs have been equal so far.
4107 struct isl_cmp_band_data
{
4118 /* Check if "ma" assigns a constant value.
4119 * Note that this function is only called on isl_multi_affs
4120 * with a single output dimension.
4122 * If "ma" assigns a constant value then we compare it to data->c1
4123 * or assign it to data->c1 if this is the first isl_multi_aff we consider.
4124 * If "ma" does not assign a constant value or if it assigns a value
4125 * that is different from data->c1, then we set data->equal to zero
4126 * and terminate the check.
4128 static int multi_aff_extract_int(__isl_take isl_set
*set
,
4129 __isl_take isl_multi_aff
*ma
, void *user
)
4132 struct isl_cmp_band_data
*data
= user
;
4134 aff
= isl_multi_aff_get_aff(ma
, 0);
4135 data
->r
= isl_aff_is_cst(aff
);
4136 if (data
->r
>= 0 && data
->r
) {
4137 isl_aff_get_constant(aff
, &data
->t
);
4139 isl_int_set(data
->c1
, data
->t
);
4141 } else if (!isl_int_eq(data
->c1
, data
->t
))
4143 } else if (data
->r
>= 0 && !data
->r
)
4148 isl_multi_aff_free(ma
);
4157 /* This function is called for each isl_pw_multi_aff in
4158 * the isl_union_pw_multi_aff checked by extract_int.
4159 * Check all the isl_multi_affs inside "pma".
4161 static int pw_multi_aff_extract_int(__isl_take isl_pw_multi_aff
*pma
,
4166 r
= isl_pw_multi_aff_foreach_piece(pma
, &multi_aff_extract_int
, user
);
4167 isl_pw_multi_aff_free(pma
);
4172 /* Check if "upma" assigns a single constant value to its domain.
4173 * If so, return 1 and store the result in data->c1.
4176 * A negative return value from isl_union_pw_multi_aff_foreach_pw_multi_aff
4177 * means that either an error occurred or that we have broken off the check
4178 * because we already know the result is going to be negative.
4179 * In the latter case, data->equal is set to zero.
4181 static int extract_int(__isl_keep isl_union_pw_multi_aff
*upma
,
4182 struct isl_cmp_band_data
*data
)
4187 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
4188 &pw_multi_aff_extract_int
, data
) < 0) {
4194 return !data
->first
&& data
->equal
;
4197 /* Compare "b1" and "b2" based on the parent schedule of their ancestor
4200 * If the parent of "ancestor" also has a single member, then we
4201 * first try to compare the two band based on the partial schedule
4204 * Otherwise, or if the result is inconclusive, we look at the partial schedule
4205 * of "ancestor" itself.
4206 * In particular, we specialize the parent schedule based
4207 * on the domains of the child schedules, check if both assign
4208 * a single constant value and, if so, compare the two constant values.
4209 * If the specialized parent schedules do not assign a constant value,
4210 * then they cannot be used to order the two bands and so in this case
4213 static int cmp_band_in_ancestor(__isl_keep isl_band
*b1
,
4214 __isl_keep isl_band
*b2
, struct isl_cmp_band_data
*data
,
4215 __isl_keep isl_band
*ancestor
)
4217 isl_union_pw_multi_aff
*upma
;
4218 isl_union_set
*domain
;
4224 if (ancestor
->parent
&& ancestor
->parent
->n
== 1) {
4225 r
= cmp_band_in_ancestor(b1
, b2
, data
, ancestor
->parent
);
4232 upma
= isl_union_pw_multi_aff_copy(b1
->pma
);
4233 domain
= isl_union_pw_multi_aff_domain(upma
);
4234 upma
= isl_union_pw_multi_aff_copy(ancestor
->pma
);
4235 upma
= isl_union_pw_multi_aff_intersect_domain(upma
, domain
);
4236 r
= extract_int(upma
, data
);
4237 isl_union_pw_multi_aff_free(upma
);
4244 isl_int_set(data
->c2
, data
->c1
);
4246 upma
= isl_union_pw_multi_aff_copy(b2
->pma
);
4247 domain
= isl_union_pw_multi_aff_domain(upma
);
4248 upma
= isl_union_pw_multi_aff_copy(ancestor
->pma
);
4249 upma
= isl_union_pw_multi_aff_intersect_domain(upma
, domain
);
4250 r
= extract_int(upma
, data
);
4251 isl_union_pw_multi_aff_free(upma
);
4258 return isl_int_cmp(data
->c2
, data
->c1
);
4261 /* Compare "a" and "b" based on the parent schedule of their parent.
4263 static int cmp_band(const void *a
, const void *b
, void *user
)
4265 isl_band
*b1
= *(isl_band
* const *) a
;
4266 isl_band
*b2
= *(isl_band
* const *) b
;
4267 struct isl_cmp_band_data
*data
= user
;
4269 return cmp_band_in_ancestor(b1
, b2
, data
, b1
->parent
);
4272 /* Sort the elements in "list" based on the partial schedules of its parent
4273 * (and ancestors). In particular if the parent assigns constant values
4274 * to the domains of the bands in "list", then the elements are sorted
4275 * according to that order.
4276 * This order should be a more "natural" order for the user, but otherwise
4277 * shouldn't have any effect.
4278 * If we would be constructing an isl_band forest directly in
4279 * isl_schedule_constraints_compute_schedule then there wouldn't be any need
4280 * for a reordering, since the children would be added to the list
4281 * in their natural order automatically.
4283 * If there is only one element in the list, then there is no need to sort
4285 * If the partial schedule of the parent has more than one member
4286 * (or if there is no parent), then it's
4287 * defnitely not assigning constant values to the different children in
4288 * the list and so we wouldn't be able to use it to sort the list.
4290 static __isl_give isl_band_list
*sort_band_list(__isl_take isl_band_list
*list
,
4291 __isl_keep isl_band
*parent
)
4293 struct isl_cmp_band_data data
;
4299 if (!parent
|| parent
->n
!= 1)
4303 isl_int_init(data
.c1
);
4304 isl_int_init(data
.c2
);
4305 isl_int_init(data
.t
);
4306 isl_sort(list
->p
, list
->n
, sizeof(list
->p
[0]), &cmp_band
, &data
);
4308 list
= isl_band_list_free(list
);
4309 isl_int_clear(data
.c1
);
4310 isl_int_clear(data
.c2
);
4311 isl_int_clear(data
.t
);
4316 /* Construct a list of bands that start at the same position (with
4317 * sequence number band_nr) in the schedules of the nodes that
4318 * were active in the parent band.
4320 * A separate isl_band structure is created for each band_id
4321 * and for each node that does not have a band with sequence
4322 * number band_nr. In the latter case, a band without members
4324 * This ensures that if a band has any children, then each node
4325 * that was active in the band is active in exactly one of the children.
4327 static __isl_give isl_band_list
*construct_band_list(
4328 __isl_keep isl_schedule
*schedule
, __isl_keep isl_band
*parent
,
4329 int band_nr
, int *parent_active
, int n_active
)
4332 isl_ctx
*ctx
= isl_schedule_get_ctx(schedule
);
4335 isl_band_list
*list
;
4338 for (i
= 0; i
< n_active
; ++i
) {
4339 for (j
= 0; j
< schedule
->n
; ++j
) {
4340 if (!parent_active
[j
])
4342 if (schedule
->node
[j
].n_band
<= band_nr
)
4344 if (schedule
->node
[j
].band_id
[band_nr
] == i
) {
4350 for (j
= 0; j
< schedule
->n
; ++j
)
4351 if (schedule
->node
[j
].n_band
<= band_nr
)
4356 list
= isl_band_list_alloc(ctx
, n_band
);
4357 band
= construct_band(schedule
, parent
, band_nr
,
4358 parent_active
, n_active
);
4359 return isl_band_list_add(list
, band
);
4362 active
= isl_alloc_array(ctx
, int, schedule
->n
);
4363 if (schedule
->n
&& !active
)
4366 list
= isl_band_list_alloc(ctx
, n_band
);
4368 for (i
= 0; i
< n_active
; ++i
) {
4372 for (j
= 0; j
< schedule
->n
; ++j
) {
4373 active
[j
] = parent_active
[j
] &&
4374 schedule
->node
[j
].n_band
> band_nr
&&
4375 schedule
->node
[j
].band_id
[band_nr
] == i
;
4382 band
= construct_band(schedule
, parent
, band_nr
, active
, n
);
4384 list
= isl_band_list_add(list
, band
);
4386 for (i
= 0; i
< schedule
->n
; ++i
) {
4388 if (!parent_active
[i
])
4390 if (schedule
->node
[i
].n_band
> band_nr
)
4392 for (j
= 0; j
< schedule
->n
; ++j
)
4394 band
= construct_band(schedule
, parent
, band_nr
, active
, 1);
4395 list
= isl_band_list_add(list
, band
);
4400 list
= sort_band_list(list
, parent
);
4405 /* Construct a band forest representation of the schedule and
4406 * return the list of roots.
4408 static __isl_give isl_band_list
*construct_forest(
4409 __isl_keep isl_schedule
*schedule
)
4412 isl_ctx
*ctx
= isl_schedule_get_ctx(schedule
);
4413 isl_band_list
*forest
;
4416 active
= isl_alloc_array(ctx
, int, schedule
->n
);
4417 if (schedule
->n
&& !active
)
4420 for (i
= 0; i
< schedule
->n
; ++i
)
4423 forest
= construct_band_list(schedule
, NULL
, 0, active
, schedule
->n
);
4430 /* Return the roots of a band forest representation of the schedule.
4432 __isl_give isl_band_list
*isl_schedule_get_band_forest(
4433 __isl_keep isl_schedule
*schedule
)
4437 if (!schedule
->band_forest
)
4438 schedule
->band_forest
= construct_forest(schedule
);
4439 return isl_band_list_dup(schedule
->band_forest
);
4442 /* Call "fn" on each band in the schedule in depth-first post-order.
4444 int isl_schedule_foreach_band(__isl_keep isl_schedule
*sched
,
4445 int (*fn
)(__isl_keep isl_band
*band
, void *user
), void *user
)
4448 isl_band_list
*forest
;
4453 forest
= isl_schedule_get_band_forest(sched
);
4454 r
= isl_band_list_foreach_band(forest
, fn
, user
);
4455 isl_band_list_free(forest
);
4460 static __isl_give isl_printer
*print_band_list(__isl_take isl_printer
*p
,
4461 __isl_keep isl_band_list
*list
);
4463 static __isl_give isl_printer
*print_band(__isl_take isl_printer
*p
,
4464 __isl_keep isl_band
*band
)
4466 isl_band_list
*children
;
4468 p
= isl_printer_start_line(p
);
4469 p
= isl_printer_print_union_pw_multi_aff(p
, band
->pma
);
4470 p
= isl_printer_end_line(p
);
4472 if (!isl_band_has_children(band
))
4475 children
= isl_band_get_children(band
);
4477 p
= isl_printer_indent(p
, 4);
4478 p
= print_band_list(p
, children
);
4479 p
= isl_printer_indent(p
, -4);
4481 isl_band_list_free(children
);
4486 static __isl_give isl_printer
*print_band_list(__isl_take isl_printer
*p
,
4487 __isl_keep isl_band_list
*list
)
4491 n
= isl_band_list_n_band(list
);
4492 for (i
= 0; i
< n
; ++i
) {
4494 band
= isl_band_list_get_band(list
, i
);
4495 p
= print_band(p
, band
);
4496 isl_band_free(band
);
4502 __isl_give isl_printer
*isl_printer_print_schedule(__isl_take isl_printer
*p
,
4503 __isl_keep isl_schedule
*schedule
)
4505 isl_band_list
*forest
;
4507 forest
= isl_schedule_get_band_forest(schedule
);
4509 p
= print_band_list(p
, forest
);
4511 isl_band_list_free(forest
);
4516 void isl_schedule_dump(__isl_keep isl_schedule
*schedule
)
4518 isl_printer
*printer
;
4523 printer
= isl_printer_to_file(isl_schedule_get_ctx(schedule
), stderr
);
4524 printer
= isl_printer_print_schedule(printer
, schedule
);
4526 isl_printer_free(printer
);