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
);
720 for (i
= 0; i
< graph
->n
; ++i
) {
721 isl_space_free(graph
->node
[i
].dim
);
722 isl_mat_free(graph
->node
[i
].sched
);
723 isl_map_free(graph
->node
[i
].sched_map
);
724 isl_mat_free(graph
->node
[i
].cmap
);
725 isl_mat_free(graph
->node
[i
].cinv
);
727 free(graph
->node
[i
].band
);
728 free(graph
->node
[i
].band_id
);
729 free(graph
->node
[i
].coincident
);
735 for (i
= 0; i
< graph
->n_edge
; ++i
) {
736 isl_map_free(graph
->edge
[i
].map
);
737 isl_union_map_free(graph
->edge
[i
].tagged_condition
);
738 isl_union_map_free(graph
->edge
[i
].tagged_validity
);
742 for (i
= 0; i
<= isl_edge_last
; ++i
)
743 isl_hash_table_free(ctx
, graph
->edge_table
[i
]);
744 isl_hash_table_free(ctx
, graph
->node_table
);
745 isl_basic_set_free(graph
->lp
);
748 /* For each "set" on which this function is called, increment
749 * graph->n by one and update graph->maxvar.
751 static int init_n_maxvar(__isl_take isl_set
*set
, void *user
)
753 struct isl_sched_graph
*graph
= user
;
754 int nvar
= isl_set_dim(set
, isl_dim_set
);
757 if (nvar
> graph
->maxvar
)
758 graph
->maxvar
= nvar
;
765 /* Compute the number of rows that should be allocated for the schedule.
766 * The graph can be split at most "n - 1" times, there can be at most
767 * two rows for each dimension in the iteration domains (in particular,
768 * we usually have one row, but it may be split by split_scaled),
769 * and there can be one extra row for ordering the statements.
770 * Note that if we have actually split "n - 1" times, then no ordering
771 * is needed, so in principle we could use "graph->n + 2 * graph->maxvar - 1".
773 static int compute_max_row(struct isl_sched_graph
*graph
,
774 __isl_keep isl_union_set
*domain
)
778 if (isl_union_set_foreach_set(domain
, &init_n_maxvar
, graph
) < 0)
780 graph
->max_row
= graph
->n
+ 2 * graph
->maxvar
;
785 /* Add a new node to the graph representing the given set.
787 static int extract_node(__isl_take isl_set
*set
, void *user
)
793 struct isl_sched_graph
*graph
= user
;
794 int *band
, *band_id
, *coincident
;
796 ctx
= isl_set_get_ctx(set
);
797 dim
= isl_set_get_space(set
);
799 nvar
= isl_space_dim(dim
, isl_dim_set
);
800 nparam
= isl_space_dim(dim
, isl_dim_param
);
801 if (!ctx
->opt
->schedule_parametric
)
803 sched
= isl_mat_alloc(ctx
, 0, 1 + nparam
+ nvar
);
804 graph
->node
[graph
->n
].dim
= dim
;
805 graph
->node
[graph
->n
].nvar
= nvar
;
806 graph
->node
[graph
->n
].nparam
= nparam
;
807 graph
->node
[graph
->n
].sched
= sched
;
808 graph
->node
[graph
->n
].sched_map
= NULL
;
809 band
= isl_alloc_array(ctx
, int, graph
->max_row
);
810 graph
->node
[graph
->n
].band
= band
;
811 band_id
= isl_calloc_array(ctx
, int, graph
->max_row
);
812 graph
->node
[graph
->n
].band_id
= band_id
;
813 coincident
= isl_calloc_array(ctx
, int, graph
->max_row
);
814 graph
->node
[graph
->n
].coincident
= coincident
;
817 if (!sched
|| (graph
->max_row
&& (!band
|| !band_id
|| !coincident
)))
823 struct isl_extract_edge_data
{
824 enum isl_edge_type type
;
825 struct isl_sched_graph
*graph
;
828 /* Merge edge2 into edge1, freeing the contents of edge2.
829 * "type" is the type of the schedule constraint from which edge2 was
831 * Return 0 on success and -1 on failure.
833 * edge1 and edge2 are assumed to have the same value for the map field.
835 static int merge_edge(enum isl_edge_type type
, struct isl_sched_edge
*edge1
,
836 struct isl_sched_edge
*edge2
)
838 edge1
->validity
|= edge2
->validity
;
839 edge1
->coincidence
|= edge2
->coincidence
;
840 edge1
->proximity
|= edge2
->proximity
;
841 edge1
->condition
|= edge2
->condition
;
842 edge1
->conditional_validity
|= edge2
->conditional_validity
;
843 isl_map_free(edge2
->map
);
845 if (type
== isl_edge_condition
) {
846 if (!edge1
->tagged_condition
)
847 edge1
->tagged_condition
= edge2
->tagged_condition
;
849 edge1
->tagged_condition
=
850 isl_union_map_union(edge1
->tagged_condition
,
851 edge2
->tagged_condition
);
854 if (type
== isl_edge_conditional_validity
) {
855 if (!edge1
->tagged_validity
)
856 edge1
->tagged_validity
= edge2
->tagged_validity
;
858 edge1
->tagged_validity
=
859 isl_union_map_union(edge1
->tagged_validity
,
860 edge2
->tagged_validity
);
863 if (type
== isl_edge_condition
&& !edge1
->tagged_condition
)
865 if (type
== isl_edge_conditional_validity
&& !edge1
->tagged_validity
)
871 /* Insert dummy tags in domain and range of "map".
873 * In particular, if "map" is of the form
879 * [A -> dummy_tag] -> [B -> dummy_tag]
881 * where the dummy_tags are identical and equal to any dummy tags
882 * introduced by any other call to this function.
884 static __isl_give isl_map
*insert_dummy_tags(__isl_take isl_map
*map
)
890 isl_set
*domain
, *range
;
892 ctx
= isl_map_get_ctx(map
);
894 id
= isl_id_alloc(ctx
, NULL
, &dummy
);
895 space
= isl_space_params(isl_map_get_space(map
));
896 space
= isl_space_set_from_params(space
);
897 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
898 space
= isl_space_map_from_set(space
);
900 domain
= isl_map_wrap(map
);
901 range
= isl_map_wrap(isl_map_universe(space
));
902 map
= isl_map_from_domain_and_range(domain
, range
);
903 map
= isl_map_zip(map
);
908 /* Add a new edge to the graph based on the given map
909 * and add it to data->graph->edge_table[data->type].
910 * If a dependence relation of a given type happens to be identical
911 * to one of the dependence relations of a type that was added before,
912 * then we don't create a new edge, but instead mark the original edge
913 * as also representing a dependence of the current type.
915 * Edges of type isl_edge_condition or isl_edge_conditional_validity
916 * may be specified as "tagged" dependence relations. That is, "map"
917 * may contain elements * (i -> a) -> (j -> b), where i -> j denotes
918 * the dependence on iterations and a and b are tags.
919 * edge->map is set to the relation containing the elements i -> j,
920 * while edge->tagged_condition and edge->tagged_validity contain
921 * the union of all the "map" relations
922 * for which extract_edge is called that result in the same edge->map.
924 static int extract_edge(__isl_take isl_map
*map
, void *user
)
926 isl_ctx
*ctx
= isl_map_get_ctx(map
);
927 struct isl_extract_edge_data
*data
= user
;
928 struct isl_sched_graph
*graph
= data
->graph
;
929 struct isl_sched_node
*src
, *dst
;
931 struct isl_sched_edge
*edge
;
932 isl_map
*tagged
= NULL
;
934 if (data
->type
== isl_edge_condition
||
935 data
->type
== isl_edge_conditional_validity
) {
936 if (isl_map_can_zip(map
)) {
937 tagged
= isl_map_copy(map
);
938 map
= isl_set_unwrap(isl_map_domain(isl_map_zip(map
)));
940 tagged
= insert_dummy_tags(isl_map_copy(map
));
944 dim
= isl_space_domain(isl_map_get_space(map
));
945 src
= graph_find_node(ctx
, graph
, dim
);
947 dim
= isl_space_range(isl_map_get_space(map
));
948 dst
= graph_find_node(ctx
, graph
, dim
);
953 isl_map_free(tagged
);
957 graph
->edge
[graph
->n_edge
].src
= src
;
958 graph
->edge
[graph
->n_edge
].dst
= dst
;
959 graph
->edge
[graph
->n_edge
].map
= map
;
960 graph
->edge
[graph
->n_edge
].validity
= 0;
961 graph
->edge
[graph
->n_edge
].coincidence
= 0;
962 graph
->edge
[graph
->n_edge
].proximity
= 0;
963 graph
->edge
[graph
->n_edge
].condition
= 0;
964 graph
->edge
[graph
->n_edge
].local
= 0;
965 graph
->edge
[graph
->n_edge
].conditional_validity
= 0;
966 graph
->edge
[graph
->n_edge
].tagged_condition
= NULL
;
967 graph
->edge
[graph
->n_edge
].tagged_validity
= NULL
;
968 if (data
->type
== isl_edge_validity
)
969 graph
->edge
[graph
->n_edge
].validity
= 1;
970 if (data
->type
== isl_edge_coincidence
)
971 graph
->edge
[graph
->n_edge
].coincidence
= 1;
972 if (data
->type
== isl_edge_proximity
)
973 graph
->edge
[graph
->n_edge
].proximity
= 1;
974 if (data
->type
== isl_edge_condition
) {
975 graph
->edge
[graph
->n_edge
].condition
= 1;
976 graph
->edge
[graph
->n_edge
].tagged_condition
=
977 isl_union_map_from_map(tagged
);
979 if (data
->type
== isl_edge_conditional_validity
) {
980 graph
->edge
[graph
->n_edge
].conditional_validity
= 1;
981 graph
->edge
[graph
->n_edge
].tagged_validity
=
982 isl_union_map_from_map(tagged
);
985 edge
= graph_find_matching_edge(graph
, &graph
->edge
[graph
->n_edge
]);
990 if (edge
== &graph
->edge
[graph
->n_edge
])
991 return graph_edge_table_add(ctx
, graph
, data
->type
,
992 &graph
->edge
[graph
->n_edge
++]);
994 if (merge_edge(data
->type
, edge
, &graph
->edge
[graph
->n_edge
]) < 0)
997 return graph_edge_table_add(ctx
, graph
, data
->type
, edge
);
1000 /* Check whether there is any dependence from node[j] to node[i]
1001 * or from node[i] to node[j].
1003 static int node_follows_weak(int i
, int j
, void *user
)
1006 struct isl_sched_graph
*graph
= user
;
1008 f
= graph_has_any_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1011 return graph_has_any_edge(graph
, &graph
->node
[i
], &graph
->node
[j
]);
1014 /* Check whether there is a (conditional) validity dependence from node[j]
1015 * to node[i], forcing node[i] to follow node[j].
1017 static int node_follows_strong(int i
, int j
, void *user
)
1019 struct isl_sched_graph
*graph
= user
;
1021 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1024 /* Use Tarjan's algorithm for computing the strongly connected components
1025 * in the dependence graph (only validity edges).
1026 * If weak is set, we consider the graph to be undirected and
1027 * we effectively compute the (weakly) connected components.
1028 * Additionally, we also consider other edges when weak is set.
1030 static int detect_ccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
, int weak
)
1033 struct isl_tarjan_graph
*g
= NULL
;
1035 g
= isl_tarjan_graph_init(ctx
, graph
->n
,
1036 weak
? &node_follows_weak
: &node_follows_strong
, graph
);
1044 while (g
->order
[i
] != -1) {
1045 graph
->node
[g
->order
[i
]].scc
= graph
->scc
;
1053 isl_tarjan_graph_free(g
);
1058 /* Apply Tarjan's algorithm to detect the strongly connected components
1059 * in the dependence graph.
1061 static int detect_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1063 return detect_ccs(ctx
, graph
, 0);
1066 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1067 * in the dependence graph.
1069 static int detect_wccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1071 return detect_ccs(ctx
, graph
, 1);
1074 static int cmp_scc(const void *a
, const void *b
, void *data
)
1076 struct isl_sched_graph
*graph
= data
;
1080 return graph
->node
[*i1
].scc
- graph
->node
[*i2
].scc
;
1083 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1085 static int sort_sccs(struct isl_sched_graph
*graph
)
1087 return isl_sort(graph
->sorted
, graph
->n
, sizeof(int), &cmp_scc
, graph
);
1090 /* Given a dependence relation R from a node to itself,
1091 * construct the set of coefficients of valid constraints for elements
1092 * in that dependence relation.
1093 * In particular, the result contains tuples of coefficients
1094 * c_0, c_n, c_x such that
1096 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1100 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1102 * We choose here to compute the dual of delta R.
1103 * Alternatively, we could have computed the dual of R, resulting
1104 * in a set of tuples c_0, c_n, c_x, c_y, and then
1105 * plugged in (c_0, c_n, c_x, -c_x).
1107 static __isl_give isl_basic_set
*intra_coefficients(
1108 struct isl_sched_graph
*graph
, __isl_take isl_map
*map
)
1111 isl_basic_set
*coef
;
1113 if (isl_map_to_basic_set_has(graph
->intra_hmap
, map
))
1114 return isl_map_to_basic_set_get(graph
->intra_hmap
, map
);
1116 delta
= isl_set_remove_divs(isl_map_deltas(isl_map_copy(map
)));
1117 coef
= isl_set_coefficients(delta
);
1118 graph
->intra_hmap
= isl_map_to_basic_set_set(graph
->intra_hmap
, map
,
1119 isl_basic_set_copy(coef
));
1124 /* Given a dependence relation R, * construct the set of coefficients
1125 * of valid constraints for elements in that dependence relation.
1126 * In particular, the result contains tuples of coefficients
1127 * c_0, c_n, c_x, c_y such that
1129 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1132 static __isl_give isl_basic_set
*inter_coefficients(
1133 struct isl_sched_graph
*graph
, __isl_take isl_map
*map
)
1136 isl_basic_set
*coef
;
1138 if (isl_map_to_basic_set_has(graph
->inter_hmap
, map
))
1139 return isl_map_to_basic_set_get(graph
->inter_hmap
, map
);
1141 set
= isl_map_wrap(isl_map_remove_divs(isl_map_copy(map
)));
1142 coef
= isl_set_coefficients(set
);
1143 graph
->inter_hmap
= isl_map_to_basic_set_set(graph
->inter_hmap
, map
,
1144 isl_basic_set_copy(coef
));
1149 /* Add constraints to graph->lp that force validity for the given
1150 * dependence from a node i to itself.
1151 * That is, add constraints that enforce
1153 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1154 * = c_i_x (y - x) >= 0
1156 * for each (x,y) in R.
1157 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1158 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1159 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1160 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1162 * Actually, we do not construct constraints for the c_i_x themselves,
1163 * but for the coefficients of c_i_x written as a linear combination
1164 * of the columns in node->cmap.
1166 static int add_intra_validity_constraints(struct isl_sched_graph
*graph
,
1167 struct isl_sched_edge
*edge
)
1170 isl_map
*map
= isl_map_copy(edge
->map
);
1171 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1173 isl_dim_map
*dim_map
;
1174 isl_basic_set
*coef
;
1175 struct isl_sched_node
*node
= edge
->src
;
1177 coef
= intra_coefficients(graph
, map
);
1179 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1181 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1182 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(node
->cmap
));
1186 total
= isl_basic_set_total_dim(graph
->lp
);
1187 dim_map
= isl_dim_map_alloc(ctx
, total
);
1188 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
1189 isl_space_dim(dim
, isl_dim_set
), 1,
1191 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
1192 isl_space_dim(dim
, isl_dim_set
), 1,
1194 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1195 coef
->n_eq
, coef
->n_ineq
);
1196 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1198 isl_space_free(dim
);
1202 isl_space_free(dim
);
1206 /* Add constraints to graph->lp that force validity for the given
1207 * dependence from node i to node j.
1208 * That is, add constraints that enforce
1210 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1212 * for each (x,y) in R.
1213 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1214 * of valid constraints for R and then plug in
1215 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
1216 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1217 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1218 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1220 * Actually, we do not construct constraints for the c_*_x themselves,
1221 * but for the coefficients of c_*_x written as a linear combination
1222 * of the columns in node->cmap.
1224 static int add_inter_validity_constraints(struct isl_sched_graph
*graph
,
1225 struct isl_sched_edge
*edge
)
1228 isl_map
*map
= isl_map_copy(edge
->map
);
1229 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1231 isl_dim_map
*dim_map
;
1232 isl_basic_set
*coef
;
1233 struct isl_sched_node
*src
= edge
->src
;
1234 struct isl_sched_node
*dst
= edge
->dst
;
1236 coef
= inter_coefficients(graph
, map
);
1238 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1240 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1241 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(src
->cmap
));
1242 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1243 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
,
1244 isl_mat_copy(dst
->cmap
));
1248 total
= isl_basic_set_total_dim(graph
->lp
);
1249 dim_map
= isl_dim_map_alloc(ctx
, total
);
1251 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, 1);
1252 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, -1);
1253 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, 1);
1254 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
1255 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1257 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
1258 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1261 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, -1);
1262 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, 1);
1263 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, -1);
1264 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
1265 isl_space_dim(dim
, isl_dim_set
), 1,
1267 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
1268 isl_space_dim(dim
, isl_dim_set
), 1,
1271 edge
->start
= graph
->lp
->n_ineq
;
1272 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1273 coef
->n_eq
, coef
->n_ineq
);
1274 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1278 isl_space_free(dim
);
1279 edge
->end
= graph
->lp
->n_ineq
;
1283 isl_space_free(dim
);
1287 /* Add constraints to graph->lp that bound the dependence distance for the given
1288 * dependence from a node i to itself.
1289 * If s = 1, we add the constraint
1291 * c_i_x (y - x) <= m_0 + m_n n
1295 * -c_i_x (y - x) + m_0 + m_n n >= 0
1297 * for each (x,y) in R.
1298 * If s = -1, we add the constraint
1300 * -c_i_x (y - x) <= m_0 + m_n n
1304 * c_i_x (y - x) + m_0 + m_n n >= 0
1306 * for each (x,y) in R.
1307 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1308 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1309 * with each coefficient (except m_0) represented as a pair of non-negative
1312 * Actually, we do not construct constraints for the c_i_x themselves,
1313 * but for the coefficients of c_i_x written as a linear combination
1314 * of the columns in node->cmap.
1317 * If "local" is set, then we add constraints
1319 * c_i_x (y - x) <= 0
1323 * -c_i_x (y - x) <= 0
1325 * instead, forcing the dependence distance to be (less than or) equal to 0.
1326 * That is, we plug in (0, 0, -s * c_i_x),
1327 * Note that dependences marked local are treated as validity constraints
1328 * by add_all_validity_constraints and therefore also have
1329 * their distances bounded by 0 from below.
1331 static int add_intra_proximity_constraints(struct isl_sched_graph
*graph
,
1332 struct isl_sched_edge
*edge
, int s
, int local
)
1336 isl_map
*map
= isl_map_copy(edge
->map
);
1337 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1339 isl_dim_map
*dim_map
;
1340 isl_basic_set
*coef
;
1341 struct isl_sched_node
*node
= edge
->src
;
1343 coef
= intra_coefficients(graph
, map
);
1345 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1347 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1348 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(node
->cmap
));
1352 nparam
= isl_space_dim(node
->dim
, isl_dim_param
);
1353 total
= isl_basic_set_total_dim(graph
->lp
);
1354 dim_map
= isl_dim_map_alloc(ctx
, total
);
1357 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1358 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1359 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1361 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
1362 isl_space_dim(dim
, isl_dim_set
), 1,
1364 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
1365 isl_space_dim(dim
, isl_dim_set
), 1,
1367 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1368 coef
->n_eq
, coef
->n_ineq
);
1369 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1371 isl_space_free(dim
);
1375 isl_space_free(dim
);
1379 /* Add constraints to graph->lp that bound the dependence distance for the given
1380 * dependence from node i to node j.
1381 * If s = 1, we add the constraint
1383 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1388 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1391 * for each (x,y) in R.
1392 * If s = -1, we add the constraint
1394 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1399 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1402 * for each (x,y) in R.
1403 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1404 * of valid constraints for R and then plug in
1405 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1407 * with each coefficient (except m_0, c_j_0 and c_i_0)
1408 * represented as a pair of non-negative coefficients.
1410 * Actually, we do not construct constraints for the c_*_x themselves,
1411 * but for the coefficients of c_*_x written as a linear combination
1412 * of the columns in node->cmap.
1415 * If "local" is set, then we add constraints
1417 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1421 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
1423 * instead, forcing the dependence distance to be (less than or) equal to 0.
1424 * That is, we plug in
1425 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
1426 * Note that dependences marked local are treated as validity constraints
1427 * by add_all_validity_constraints and therefore also have
1428 * their distances bounded by 0 from below.
1430 static int add_inter_proximity_constraints(struct isl_sched_graph
*graph
,
1431 struct isl_sched_edge
*edge
, int s
, int local
)
1435 isl_map
*map
= isl_map_copy(edge
->map
);
1436 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1438 isl_dim_map
*dim_map
;
1439 isl_basic_set
*coef
;
1440 struct isl_sched_node
*src
= edge
->src
;
1441 struct isl_sched_node
*dst
= edge
->dst
;
1443 coef
= inter_coefficients(graph
, map
);
1445 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1447 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1448 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(src
->cmap
));
1449 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1450 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
,
1451 isl_mat_copy(dst
->cmap
));
1455 nparam
= isl_space_dim(src
->dim
, isl_dim_param
);
1456 total
= isl_basic_set_total_dim(graph
->lp
);
1457 dim_map
= isl_dim_map_alloc(ctx
, total
);
1460 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1461 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1462 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1465 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, -s
);
1466 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, s
);
1467 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, -s
);
1468 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
1469 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1471 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
1472 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1475 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, s
);
1476 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, -s
);
1477 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, s
);
1478 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
1479 isl_space_dim(dim
, isl_dim_set
), 1,
1481 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
1482 isl_space_dim(dim
, isl_dim_set
), 1,
1485 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1486 coef
->n_eq
, coef
->n_ineq
);
1487 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1489 isl_space_free(dim
);
1493 isl_space_free(dim
);
1497 /* Add all validity constraints to graph->lp.
1499 * An edge that is forced to be local needs to have its dependence
1500 * distances equal to zero. We take care of bounding them by 0 from below
1501 * here. add_all_proximity_constraints takes care of bounding them by 0
1504 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1505 * Otherwise, we ignore them.
1507 static int add_all_validity_constraints(struct isl_sched_graph
*graph
,
1508 int use_coincidence
)
1512 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1513 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1516 local
= edge
->local
|| (edge
->coincidence
&& use_coincidence
);
1517 if (!edge
->validity
&& !local
)
1519 if (edge
->src
!= edge
->dst
)
1521 if (add_intra_validity_constraints(graph
, edge
) < 0)
1525 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1526 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1529 local
= edge
->local
|| (edge
->coincidence
&& use_coincidence
);
1530 if (!edge
->validity
&& !local
)
1532 if (edge
->src
== edge
->dst
)
1534 if (add_inter_validity_constraints(graph
, edge
) < 0)
1541 /* Add constraints to graph->lp that bound the dependence distance
1542 * for all dependence relations.
1543 * If a given proximity dependence is identical to a validity
1544 * dependence, then the dependence distance is already bounded
1545 * from below (by zero), so we only need to bound the distance
1546 * from above. (This includes the case of "local" dependences
1547 * which are treated as validity dependence by add_all_validity_constraints.)
1548 * Otherwise, we need to bound the distance both from above and from below.
1550 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1551 * Otherwise, we ignore them.
1553 static int add_all_proximity_constraints(struct isl_sched_graph
*graph
,
1554 int use_coincidence
)
1558 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1559 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1562 local
= edge
->local
|| (edge
->coincidence
&& use_coincidence
);
1563 if (!edge
->proximity
&& !local
)
1565 if (edge
->src
== edge
->dst
&&
1566 add_intra_proximity_constraints(graph
, edge
, 1, local
) < 0)
1568 if (edge
->src
!= edge
->dst
&&
1569 add_inter_proximity_constraints(graph
, edge
, 1, local
) < 0)
1571 if (edge
->validity
|| local
)
1573 if (edge
->src
== edge
->dst
&&
1574 add_intra_proximity_constraints(graph
, edge
, -1, 0) < 0)
1576 if (edge
->src
!= edge
->dst
&&
1577 add_inter_proximity_constraints(graph
, edge
, -1, 0) < 0)
1584 /* Compute a basis for the rows in the linear part of the schedule
1585 * and extend this basis to a full basis. The remaining rows
1586 * can then be used to force linear independence from the rows
1589 * In particular, given the schedule rows S, we compute
1594 * with H the Hermite normal form of S. That is, all but the
1595 * first rank columns of H are zero and so each row in S is
1596 * a linear combination of the first rank rows of Q.
1597 * The matrix Q is then transposed because we will write the
1598 * coefficients of the next schedule row as a column vector s
1599 * and express this s as a linear combination s = Q c of the
1601 * Similarly, the matrix U is transposed such that we can
1602 * compute the coefficients c = U s from a schedule row s.
1604 static int node_update_cmap(struct isl_sched_node
*node
)
1607 int n_row
= isl_mat_rows(node
->sched
);
1609 H
= isl_mat_sub_alloc(node
->sched
, 0, n_row
,
1610 1 + node
->nparam
, node
->nvar
);
1612 H
= isl_mat_left_hermite(H
, 0, &U
, &Q
);
1613 isl_mat_free(node
->cmap
);
1614 isl_mat_free(node
->cinv
);
1615 node
->cmap
= isl_mat_transpose(Q
);
1616 node
->cinv
= isl_mat_transpose(U
);
1617 node
->rank
= isl_mat_initial_non_zero_cols(H
);
1620 if (!node
->cmap
|| !node
->cinv
|| node
->rank
< 0)
1625 /* How many times should we count the constraints in "edge"?
1627 * If carry is set, then we are counting the number of
1628 * (validity or conditional validity) constraints that will be added
1629 * in setup_carry_lp and we count each edge exactly once.
1631 * Otherwise, we count as follows
1632 * validity -> 1 (>= 0)
1633 * validity+proximity -> 2 (>= 0 and upper bound)
1634 * proximity -> 2 (lower and upper bound)
1635 * local(+any) -> 2 (>= 0 and <= 0)
1637 * If an edge is only marked conditional_validity then it counts
1638 * as zero since it is only checked afterwards.
1640 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1641 * Otherwise, we ignore them.
1643 static int edge_multiplicity(struct isl_sched_edge
*edge
, int carry
,
1644 int use_coincidence
)
1646 if (carry
&& !edge
->validity
&& !edge
->conditional_validity
)
1650 if (edge
->proximity
|| edge
->local
)
1652 if (use_coincidence
&& edge
->coincidence
)
1659 /* Count the number of equality and inequality constraints
1660 * that will be added for the given map.
1662 * "use_coincidence" is set if we should take into account coincidence edges.
1664 static int count_map_constraints(struct isl_sched_graph
*graph
,
1665 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
,
1666 int *n_eq
, int *n_ineq
, int carry
, int use_coincidence
)
1668 isl_basic_set
*coef
;
1669 int f
= edge_multiplicity(edge
, carry
, use_coincidence
);
1676 if (edge
->src
== edge
->dst
)
1677 coef
= intra_coefficients(graph
, map
);
1679 coef
= inter_coefficients(graph
, map
);
1682 *n_eq
+= f
* coef
->n_eq
;
1683 *n_ineq
+= f
* coef
->n_ineq
;
1684 isl_basic_set_free(coef
);
1689 /* Count the number of equality and inequality constraints
1690 * that will be added to the main lp problem.
1691 * We count as follows
1692 * validity -> 1 (>= 0)
1693 * validity+proximity -> 2 (>= 0 and upper bound)
1694 * proximity -> 2 (lower and upper bound)
1695 * local(+any) -> 2 (>= 0 and <= 0)
1697 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1698 * Otherwise, we ignore them.
1700 static int count_constraints(struct isl_sched_graph
*graph
,
1701 int *n_eq
, int *n_ineq
, int use_coincidence
)
1705 *n_eq
= *n_ineq
= 0;
1706 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1707 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1708 isl_map
*map
= isl_map_copy(edge
->map
);
1710 if (count_map_constraints(graph
, edge
, map
, n_eq
, n_ineq
,
1711 0, use_coincidence
) < 0)
1718 /* Count the number of constraints that will be added by
1719 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
1722 * In practice, add_bound_coefficient_constraints only adds inequalities.
1724 static int count_bound_coefficient_constraints(isl_ctx
*ctx
,
1725 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
1729 if (ctx
->opt
->schedule_max_coefficient
== -1)
1732 for (i
= 0; i
< graph
->n
; ++i
)
1733 *n_ineq
+= 2 * graph
->node
[i
].nparam
+ 2 * graph
->node
[i
].nvar
;
1738 /* Add constraints that bound the values of the variable and parameter
1739 * coefficients of the schedule.
1741 * The maximal value of the coefficients is defined by the option
1742 * 'schedule_max_coefficient'.
1744 static int add_bound_coefficient_constraints(isl_ctx
*ctx
,
1745 struct isl_sched_graph
*graph
)
1748 int max_coefficient
;
1751 max_coefficient
= ctx
->opt
->schedule_max_coefficient
;
1753 if (max_coefficient
== -1)
1756 total
= isl_basic_set_total_dim(graph
->lp
);
1758 for (i
= 0; i
< graph
->n
; ++i
) {
1759 struct isl_sched_node
*node
= &graph
->node
[i
];
1760 for (j
= 0; j
< 2 * node
->nparam
+ 2 * node
->nvar
; ++j
) {
1762 k
= isl_basic_set_alloc_inequality(graph
->lp
);
1765 dim
= 1 + node
->start
+ 1 + j
;
1766 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
1767 isl_int_set_si(graph
->lp
->ineq
[k
][dim
], -1);
1768 isl_int_set_si(graph
->lp
->ineq
[k
][0], max_coefficient
);
1775 /* Construct an ILP problem for finding schedule coefficients
1776 * that result in non-negative, but small dependence distances
1777 * over all dependences.
1778 * In particular, the dependence distances over proximity edges
1779 * are bounded by m_0 + m_n n and we compute schedule coefficients
1780 * with small values (preferably zero) of m_n and m_0.
1782 * All variables of the ILP are non-negative. The actual coefficients
1783 * may be negative, so each coefficient is represented as the difference
1784 * of two non-negative variables. The negative part always appears
1785 * immediately before the positive part.
1786 * Other than that, the variables have the following order
1788 * - sum of positive and negative parts of m_n coefficients
1790 * - sum of positive and negative parts of all c_n coefficients
1791 * (unconstrained when computing non-parametric schedules)
1792 * - sum of positive and negative parts of all c_x coefficients
1793 * - positive and negative parts of m_n coefficients
1796 * - positive and negative parts of c_i_n (if parametric)
1797 * - positive and negative parts of c_i_x
1799 * The c_i_x are not represented directly, but through the columns of
1800 * node->cmap. That is, the computed values are for variable t_i_x
1801 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
1803 * The constraints are those from the edges plus two or three equalities
1804 * to express the sums.
1806 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1807 * Otherwise, we ignore them.
1809 static int setup_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
1810 int use_coincidence
)
1820 int max_constant_term
;
1822 max_constant_term
= ctx
->opt
->schedule_max_constant_term
;
1824 parametric
= ctx
->opt
->schedule_parametric
;
1825 nparam
= isl_space_dim(graph
->node
[0].dim
, isl_dim_param
);
1827 total
= param_pos
+ 2 * nparam
;
1828 for (i
= 0; i
< graph
->n
; ++i
) {
1829 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
1830 if (node_update_cmap(node
) < 0)
1832 node
->start
= total
;
1833 total
+= 1 + 2 * (node
->nparam
+ node
->nvar
);
1836 if (count_constraints(graph
, &n_eq
, &n_ineq
, use_coincidence
) < 0)
1838 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
1841 dim
= isl_space_set_alloc(ctx
, 0, total
);
1842 isl_basic_set_free(graph
->lp
);
1843 n_eq
+= 2 + parametric
;
1844 if (max_constant_term
!= -1)
1847 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
1849 k
= isl_basic_set_alloc_equality(graph
->lp
);
1852 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
1853 isl_int_set_si(graph
->lp
->eq
[k
][1], -1);
1854 for (i
= 0; i
< 2 * nparam
; ++i
)
1855 isl_int_set_si(graph
->lp
->eq
[k
][1 + param_pos
+ i
], 1);
1858 k
= isl_basic_set_alloc_equality(graph
->lp
);
1861 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
1862 isl_int_set_si(graph
->lp
->eq
[k
][3], -1);
1863 for (i
= 0; i
< graph
->n
; ++i
) {
1864 int pos
= 1 + graph
->node
[i
].start
+ 1;
1866 for (j
= 0; j
< 2 * graph
->node
[i
].nparam
; ++j
)
1867 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
1871 k
= isl_basic_set_alloc_equality(graph
->lp
);
1874 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
1875 isl_int_set_si(graph
->lp
->eq
[k
][4], -1);
1876 for (i
= 0; i
< graph
->n
; ++i
) {
1877 struct isl_sched_node
*node
= &graph
->node
[i
];
1878 int pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
1880 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
1881 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
1884 if (max_constant_term
!= -1)
1885 for (i
= 0; i
< graph
->n
; ++i
) {
1886 struct isl_sched_node
*node
= &graph
->node
[i
];
1887 k
= isl_basic_set_alloc_inequality(graph
->lp
);
1890 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
1891 isl_int_set_si(graph
->lp
->ineq
[k
][1 + node
->start
], -1);
1892 isl_int_set_si(graph
->lp
->ineq
[k
][0], max_constant_term
);
1895 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
1897 if (add_all_validity_constraints(graph
, use_coincidence
) < 0)
1899 if (add_all_proximity_constraints(graph
, use_coincidence
) < 0)
1905 /* Analyze the conflicting constraint found by
1906 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
1907 * constraint of one of the edges between distinct nodes, living, moreover
1908 * in distinct SCCs, then record the source and sink SCC as this may
1909 * be a good place to cut between SCCs.
1911 static int check_conflict(int con
, void *user
)
1914 struct isl_sched_graph
*graph
= user
;
1916 if (graph
->src_scc
>= 0)
1919 con
-= graph
->lp
->n_eq
;
1921 if (con
>= graph
->lp
->n_ineq
)
1924 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1925 if (!graph
->edge
[i
].validity
)
1927 if (graph
->edge
[i
].src
== graph
->edge
[i
].dst
)
1929 if (graph
->edge
[i
].src
->scc
== graph
->edge
[i
].dst
->scc
)
1931 if (graph
->edge
[i
].start
> con
)
1933 if (graph
->edge
[i
].end
<= con
)
1935 graph
->src_scc
= graph
->edge
[i
].src
->scc
;
1936 graph
->dst_scc
= graph
->edge
[i
].dst
->scc
;
1942 /* Check whether the next schedule row of the given node needs to be
1943 * non-trivial. Lower-dimensional domains may have some trivial rows,
1944 * but as soon as the number of remaining required non-trivial rows
1945 * is as large as the number or remaining rows to be computed,
1946 * all remaining rows need to be non-trivial.
1948 static int needs_row(struct isl_sched_graph
*graph
, struct isl_sched_node
*node
)
1950 return node
->nvar
- node
->rank
>= graph
->maxvar
- graph
->n_row
;
1953 /* Solve the ILP problem constructed in setup_lp.
1954 * For each node such that all the remaining rows of its schedule
1955 * need to be non-trivial, we construct a non-triviality region.
1956 * This region imposes that the next row is independent of previous rows.
1957 * In particular the coefficients c_i_x are represented by t_i_x
1958 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
1959 * its first columns span the rows of the previously computed part
1960 * of the schedule. The non-triviality region enforces that at least
1961 * one of the remaining components of t_i_x is non-zero, i.e.,
1962 * that the new schedule row depends on at least one of the remaining
1965 static __isl_give isl_vec
*solve_lp(struct isl_sched_graph
*graph
)
1971 for (i
= 0; i
< graph
->n
; ++i
) {
1972 struct isl_sched_node
*node
= &graph
->node
[i
];
1973 int skip
= node
->rank
;
1974 graph
->region
[i
].pos
= node
->start
+ 1 + 2*(node
->nparam
+skip
);
1975 if (needs_row(graph
, node
))
1976 graph
->region
[i
].len
= 2 * (node
->nvar
- skip
);
1978 graph
->region
[i
].len
= 0;
1980 lp
= isl_basic_set_copy(graph
->lp
);
1981 sol
= isl_tab_basic_set_non_trivial_lexmin(lp
, 2, graph
->n
,
1982 graph
->region
, &check_conflict
, graph
);
1986 /* Update the schedules of all nodes based on the given solution
1987 * of the LP problem.
1988 * The new row is added to the current band.
1989 * All possibly negative coefficients are encoded as a difference
1990 * of two non-negative variables, so we need to perform the subtraction
1991 * here. Moreover, if use_cmap is set, then the solution does
1992 * not refer to the actual coefficients c_i_x, but instead to variables
1993 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
1994 * In this case, we then also need to perform this multiplication
1995 * to obtain the values of c_i_x.
1997 * If coincident is set, then the caller guarantees that the new
1998 * row satisfies the coincidence constraints.
2000 static int update_schedule(struct isl_sched_graph
*graph
,
2001 __isl_take isl_vec
*sol
, int use_cmap
, int coincident
)
2004 isl_vec
*csol
= NULL
;
2009 isl_die(sol
->ctx
, isl_error_internal
,
2010 "no solution found", goto error
);
2011 if (graph
->n_total_row
>= graph
->max_row
)
2012 isl_die(sol
->ctx
, isl_error_internal
,
2013 "too many schedule rows", goto error
);
2015 for (i
= 0; i
< graph
->n
; ++i
) {
2016 struct isl_sched_node
*node
= &graph
->node
[i
];
2017 int pos
= node
->start
;
2018 int row
= isl_mat_rows(node
->sched
);
2021 csol
= isl_vec_alloc(sol
->ctx
, node
->nvar
);
2025 isl_map_free(node
->sched_map
);
2026 node
->sched_map
= NULL
;
2027 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
2030 node
->sched
= isl_mat_set_element(node
->sched
, row
, 0,
2032 for (j
= 0; j
< node
->nparam
+ node
->nvar
; ++j
)
2033 isl_int_sub(sol
->el
[1 + pos
+ 1 + 2 * j
+ 1],
2034 sol
->el
[1 + pos
+ 1 + 2 * j
+ 1],
2035 sol
->el
[1 + pos
+ 1 + 2 * j
]);
2036 for (j
= 0; j
< node
->nparam
; ++j
)
2037 node
->sched
= isl_mat_set_element(node
->sched
,
2038 row
, 1 + j
, sol
->el
[1+pos
+1+2*j
+1]);
2039 for (j
= 0; j
< node
->nvar
; ++j
)
2040 isl_int_set(csol
->el
[j
],
2041 sol
->el
[1+pos
+1+2*(node
->nparam
+j
)+1]);
2043 csol
= isl_mat_vec_product(isl_mat_copy(node
->cmap
),
2047 for (j
= 0; j
< node
->nvar
; ++j
)
2048 node
->sched
= isl_mat_set_element(node
->sched
,
2049 row
, 1 + node
->nparam
+ j
, csol
->el
[j
]);
2050 node
->band
[graph
->n_total_row
] = graph
->n_band
;
2051 node
->coincident
[graph
->n_total_row
] = coincident
;
2057 graph
->n_total_row
++;
2066 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2067 * and return this isl_aff.
2069 static __isl_give isl_aff
*extract_schedule_row(__isl_take isl_local_space
*ls
,
2070 struct isl_sched_node
*node
, int row
)
2078 aff
= isl_aff_zero_on_domain(ls
);
2079 isl_mat_get_element(node
->sched
, row
, 0, &v
);
2080 aff
= isl_aff_set_constant(aff
, v
);
2081 for (j
= 0; j
< node
->nparam
; ++j
) {
2082 isl_mat_get_element(node
->sched
, row
, 1 + j
, &v
);
2083 aff
= isl_aff_set_coefficient(aff
, isl_dim_param
, j
, v
);
2085 for (j
= 0; j
< node
->nvar
; ++j
) {
2086 isl_mat_get_element(node
->sched
, row
, 1 + node
->nparam
+ j
, &v
);
2087 aff
= isl_aff_set_coefficient(aff
, isl_dim_in
, j
, v
);
2095 /* Convert node->sched into a multi_aff and return this multi_aff.
2097 static __isl_give isl_multi_aff
*node_extract_schedule_multi_aff(
2098 struct isl_sched_node
*node
)
2102 isl_local_space
*ls
;
2107 nrow
= isl_mat_rows(node
->sched
);
2108 ncol
= isl_mat_cols(node
->sched
) - 1;
2109 space
= isl_space_from_domain(isl_space_copy(node
->dim
));
2110 space
= isl_space_add_dims(space
, isl_dim_out
, nrow
);
2111 ma
= isl_multi_aff_zero(space
);
2112 ls
= isl_local_space_from_space(isl_space_copy(node
->dim
));
2114 for (i
= 0; i
< nrow
; ++i
) {
2115 aff
= extract_schedule_row(isl_local_space_copy(ls
), node
, i
);
2116 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
2119 isl_local_space_free(ls
);
2124 /* Convert node->sched into a map and return this map.
2126 * The result is cached in node->sched_map, which needs to be released
2127 * whenever node->sched is updated.
2129 static __isl_give isl_map
*node_extract_schedule(struct isl_sched_node
*node
)
2131 if (!node
->sched_map
) {
2134 ma
= node_extract_schedule_multi_aff(node
);
2135 node
->sched_map
= isl_map_from_multi_aff(ma
);
2138 return isl_map_copy(node
->sched_map
);
2141 /* Construct a map that can be used to update dependence relation
2142 * based on the current schedule.
2143 * That is, construct a map expressing that source and sink
2144 * are executed within the same iteration of the current schedule.
2145 * This map can then be intersected with the dependence relation.
2146 * This is not the most efficient way, but this shouldn't be a critical
2149 static __isl_give isl_map
*specializer(struct isl_sched_node
*src
,
2150 struct isl_sched_node
*dst
)
2152 isl_map
*src_sched
, *dst_sched
;
2154 src_sched
= node_extract_schedule(src
);
2155 dst_sched
= node_extract_schedule(dst
);
2156 return isl_map_apply_range(src_sched
, isl_map_reverse(dst_sched
));
2159 /* Intersect the domains of the nested relations in domain and range
2160 * of "umap" with "map".
2162 static __isl_give isl_union_map
*intersect_domains(
2163 __isl_take isl_union_map
*umap
, __isl_keep isl_map
*map
)
2165 isl_union_set
*uset
;
2167 umap
= isl_union_map_zip(umap
);
2168 uset
= isl_union_set_from_set(isl_map_wrap(isl_map_copy(map
)));
2169 umap
= isl_union_map_intersect_domain(umap
, uset
);
2170 umap
= isl_union_map_zip(umap
);
2174 /* Update the dependence relation of the given edge based
2175 * on the current schedule.
2176 * If the dependence is carried completely by the current schedule, then
2177 * it is removed from the edge_tables. It is kept in the list of edges
2178 * as otherwise all edge_tables would have to be recomputed.
2180 static int update_edge(struct isl_sched_graph
*graph
,
2181 struct isl_sched_edge
*edge
)
2185 id
= specializer(edge
->src
, edge
->dst
);
2186 edge
->map
= isl_map_intersect(edge
->map
, isl_map_copy(id
));
2190 if (edge
->tagged_condition
) {
2191 edge
->tagged_condition
=
2192 intersect_domains(edge
->tagged_condition
, id
);
2193 if (!edge
->tagged_condition
)
2196 if (edge
->tagged_validity
) {
2197 edge
->tagged_validity
=
2198 intersect_domains(edge
->tagged_validity
, id
);
2199 if (!edge
->tagged_validity
)
2204 if (isl_map_plain_is_empty(edge
->map
))
2205 graph_remove_edge(graph
, edge
);
2213 /* Update the dependence relations of all edges based on the current schedule.
2215 static int update_edges(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2219 for (i
= graph
->n_edge
- 1; i
>= 0; --i
) {
2220 if (update_edge(graph
, &graph
->edge
[i
]) < 0)
2227 static void next_band(struct isl_sched_graph
*graph
)
2229 graph
->band_start
= graph
->n_total_row
;
2233 /* Topologically sort statements mapped to the same schedule iteration
2234 * and add a row to the schedule corresponding to this order.
2236 static int sort_statements(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2243 if (update_edges(ctx
, graph
) < 0)
2246 if (graph
->n_edge
== 0)
2249 if (detect_sccs(ctx
, graph
) < 0)
2252 if (graph
->n_total_row
>= graph
->max_row
)
2253 isl_die(ctx
, isl_error_internal
,
2254 "too many schedule rows", return -1);
2256 for (i
= 0; i
< graph
->n
; ++i
) {
2257 struct isl_sched_node
*node
= &graph
->node
[i
];
2258 int row
= isl_mat_rows(node
->sched
);
2259 int cols
= isl_mat_cols(node
->sched
);
2261 isl_map_free(node
->sched_map
);
2262 node
->sched_map
= NULL
;
2263 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
2266 node
->sched
= isl_mat_set_element_si(node
->sched
, row
, 0,
2268 for (j
= 1; j
< cols
; ++j
)
2269 node
->sched
= isl_mat_set_element_si(node
->sched
,
2271 node
->band
[graph
->n_total_row
] = graph
->n_band
;
2274 graph
->n_total_row
++;
2280 /* Construct an isl_schedule based on the computed schedule stored
2281 * in graph and with parameters specified by dim.
2283 static __isl_give isl_schedule
*extract_schedule(struct isl_sched_graph
*graph
,
2284 __isl_take isl_space
*dim
)
2288 isl_schedule
*sched
= NULL
;
2293 ctx
= isl_space_get_ctx(dim
);
2294 sched
= isl_calloc(ctx
, struct isl_schedule
,
2295 sizeof(struct isl_schedule
) +
2296 (graph
->n
- 1) * sizeof(struct isl_schedule_node
));
2301 sched
->n
= graph
->n
;
2302 sched
->n_band
= graph
->n_band
;
2303 sched
->n_total_row
= graph
->n_total_row
;
2305 for (i
= 0; i
< sched
->n
; ++i
) {
2307 int *band_end
, *band_id
, *coincident
;
2309 sched
->node
[i
].sched
=
2310 node_extract_schedule_multi_aff(&graph
->node
[i
]);
2311 if (!sched
->node
[i
].sched
)
2314 sched
->node
[i
].n_band
= graph
->n_band
;
2315 if (graph
->n_band
== 0)
2318 band_end
= isl_alloc_array(ctx
, int, graph
->n_band
);
2319 band_id
= isl_alloc_array(ctx
, int, graph
->n_band
);
2320 coincident
= isl_alloc_array(ctx
, int, graph
->n_total_row
);
2321 sched
->node
[i
].band_end
= band_end
;
2322 sched
->node
[i
].band_id
= band_id
;
2323 sched
->node
[i
].coincident
= coincident
;
2324 if (!band_end
|| !band_id
|| !coincident
)
2327 for (r
= 0; r
< graph
->n_total_row
; ++r
)
2328 coincident
[r
] = graph
->node
[i
].coincident
[r
];
2329 for (r
= b
= 0; r
< graph
->n_total_row
; ++r
) {
2330 if (graph
->node
[i
].band
[r
] == b
)
2333 if (graph
->node
[i
].band
[r
] == -1)
2336 if (r
== graph
->n_total_row
)
2338 sched
->node
[i
].n_band
= b
;
2339 for (--b
; b
>= 0; --b
)
2340 band_id
[b
] = graph
->node
[i
].band_id
[b
];
2347 isl_space_free(dim
);
2348 isl_schedule_free(sched
);
2352 /* Copy nodes that satisfy node_pred from the src dependence graph
2353 * to the dst dependence graph.
2355 static int copy_nodes(struct isl_sched_graph
*dst
, struct isl_sched_graph
*src
,
2356 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
2361 for (i
= 0; i
< src
->n
; ++i
) {
2362 if (!node_pred(&src
->node
[i
], data
))
2364 dst
->node
[dst
->n
].dim
= isl_space_copy(src
->node
[i
].dim
);
2365 dst
->node
[dst
->n
].nvar
= src
->node
[i
].nvar
;
2366 dst
->node
[dst
->n
].nparam
= src
->node
[i
].nparam
;
2367 dst
->node
[dst
->n
].sched
= isl_mat_copy(src
->node
[i
].sched
);
2368 dst
->node
[dst
->n
].sched_map
=
2369 isl_map_copy(src
->node
[i
].sched_map
);
2370 dst
->node
[dst
->n
].band
= src
->node
[i
].band
;
2371 dst
->node
[dst
->n
].band_id
= src
->node
[i
].band_id
;
2372 dst
->node
[dst
->n
].coincident
= src
->node
[i
].coincident
;
2379 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
2380 * to the dst dependence graph.
2381 * If the source or destination node of the edge is not in the destination
2382 * graph, then it must be a backward proximity edge and it should simply
2385 static int copy_edges(isl_ctx
*ctx
, struct isl_sched_graph
*dst
,
2386 struct isl_sched_graph
*src
,
2387 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
), int data
)
2390 enum isl_edge_type t
;
2393 for (i
= 0; i
< src
->n_edge
; ++i
) {
2394 struct isl_sched_edge
*edge
= &src
->edge
[i
];
2396 isl_union_map
*tagged_condition
;
2397 isl_union_map
*tagged_validity
;
2398 struct isl_sched_node
*dst_src
, *dst_dst
;
2400 if (!edge_pred(edge
, data
))
2403 if (isl_map_plain_is_empty(edge
->map
))
2406 dst_src
= graph_find_node(ctx
, dst
, edge
->src
->dim
);
2407 dst_dst
= graph_find_node(ctx
, dst
, edge
->dst
->dim
);
2408 if (!dst_src
|| !dst_dst
) {
2409 if (edge
->validity
|| edge
->conditional_validity
)
2410 isl_die(ctx
, isl_error_internal
,
2411 "backward (conditional) validity edge",
2416 map
= isl_map_copy(edge
->map
);
2417 tagged_condition
= isl_union_map_copy(edge
->tagged_condition
);
2418 tagged_validity
= isl_union_map_copy(edge
->tagged_validity
);
2420 dst
->edge
[dst
->n_edge
].src
= dst_src
;
2421 dst
->edge
[dst
->n_edge
].dst
= dst_dst
;
2422 dst
->edge
[dst
->n_edge
].map
= map
;
2423 dst
->edge
[dst
->n_edge
].tagged_condition
= tagged_condition
;
2424 dst
->edge
[dst
->n_edge
].tagged_validity
= tagged_validity
;
2425 dst
->edge
[dst
->n_edge
].validity
= edge
->validity
;
2426 dst
->edge
[dst
->n_edge
].proximity
= edge
->proximity
;
2427 dst
->edge
[dst
->n_edge
].coincidence
= edge
->coincidence
;
2428 dst
->edge
[dst
->n_edge
].condition
= edge
->condition
;
2429 dst
->edge
[dst
->n_edge
].conditional_validity
=
2430 edge
->conditional_validity
;
2433 if (edge
->tagged_condition
&& !tagged_condition
)
2435 if (edge
->tagged_validity
&& !tagged_validity
)
2438 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
2440 graph_find_edge(src
, t
, edge
->src
, edge
->dst
))
2442 if (graph_edge_table_add(ctx
, dst
, t
,
2443 &dst
->edge
[dst
->n_edge
- 1]) < 0)
2451 /* Given a "src" dependence graph that contains the nodes from "dst"
2452 * that satisfy node_pred, copy the schedule computed in "src"
2453 * for those nodes back to "dst".
2455 static int copy_schedule(struct isl_sched_graph
*dst
,
2456 struct isl_sched_graph
*src
,
2457 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
2462 for (i
= 0; i
< dst
->n
; ++i
) {
2463 if (!node_pred(&dst
->node
[i
], data
))
2465 isl_mat_free(dst
->node
[i
].sched
);
2466 isl_map_free(dst
->node
[i
].sched_map
);
2467 dst
->node
[i
].sched
= isl_mat_copy(src
->node
[src
->n
].sched
);
2468 dst
->node
[i
].sched_map
=
2469 isl_map_copy(src
->node
[src
->n
].sched_map
);
2473 dst
->max_row
= src
->max_row
;
2474 dst
->n_total_row
= src
->n_total_row
;
2475 dst
->n_band
= src
->n_band
;
2480 /* Compute the maximal number of variables over all nodes.
2481 * This is the maximal number of linearly independent schedule
2482 * rows that we need to compute.
2483 * Just in case we end up in a part of the dependence graph
2484 * with only lower-dimensional domains, we make sure we will
2485 * compute the required amount of extra linearly independent rows.
2487 static int compute_maxvar(struct isl_sched_graph
*graph
)
2492 for (i
= 0; i
< graph
->n
; ++i
) {
2493 struct isl_sched_node
*node
= &graph
->node
[i
];
2496 if (node_update_cmap(node
) < 0)
2498 nvar
= node
->nvar
+ graph
->n_row
- node
->rank
;
2499 if (nvar
> graph
->maxvar
)
2500 graph
->maxvar
= nvar
;
2506 static int compute_schedule(isl_ctx
*ctx
, struct isl_sched_graph
*graph
);
2507 static int compute_schedule_wcc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
);
2509 /* Compute a schedule for a subgraph of "graph". In particular, for
2510 * the graph composed of nodes that satisfy node_pred and edges that
2511 * that satisfy edge_pred. The caller should precompute the number
2512 * of nodes and edges that satisfy these predicates and pass them along
2513 * as "n" and "n_edge".
2514 * If the subgraph is known to consist of a single component, then wcc should
2515 * be set and then we call compute_schedule_wcc on the constructed subgraph.
2516 * Otherwise, we call compute_schedule, which will check whether the subgraph
2519 static int compute_sub_schedule(isl_ctx
*ctx
,
2520 struct isl_sched_graph
*graph
, int n
, int n_edge
,
2521 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
2522 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
2525 struct isl_sched_graph split
= { 0 };
2528 if (graph_alloc(ctx
, &split
, n
, n_edge
) < 0)
2530 if (copy_nodes(&split
, graph
, node_pred
, data
) < 0)
2532 if (graph_init_table(ctx
, &split
) < 0)
2534 for (t
= 0; t
<= isl_edge_last
; ++t
)
2535 split
.max_edge
[t
] = graph
->max_edge
[t
];
2536 if (graph_init_edge_tables(ctx
, &split
) < 0)
2538 if (copy_edges(ctx
, &split
, graph
, edge_pred
, data
) < 0)
2540 split
.n_row
= graph
->n_row
;
2541 split
.max_row
= graph
->max_row
;
2542 split
.n_total_row
= graph
->n_total_row
;
2543 split
.n_band
= graph
->n_band
;
2544 split
.band_start
= graph
->band_start
;
2546 if (wcc
&& compute_schedule_wcc(ctx
, &split
) < 0)
2548 if (!wcc
&& compute_schedule(ctx
, &split
) < 0)
2551 copy_schedule(graph
, &split
, node_pred
, data
);
2553 graph_free(ctx
, &split
);
2556 graph_free(ctx
, &split
);
2560 static int node_scc_exactly(struct isl_sched_node
*node
, int scc
)
2562 return node
->scc
== scc
;
2565 static int node_scc_at_most(struct isl_sched_node
*node
, int scc
)
2567 return node
->scc
<= scc
;
2570 static int node_scc_at_least(struct isl_sched_node
*node
, int scc
)
2572 return node
->scc
>= scc
;
2575 static int edge_scc_exactly(struct isl_sched_edge
*edge
, int scc
)
2577 return edge
->src
->scc
== scc
&& edge
->dst
->scc
== scc
;
2580 static int edge_dst_scc_at_most(struct isl_sched_edge
*edge
, int scc
)
2582 return edge
->dst
->scc
<= scc
;
2585 static int edge_src_scc_at_least(struct isl_sched_edge
*edge
, int scc
)
2587 return edge
->src
->scc
>= scc
;
2590 /* Pad the schedules of all nodes with zero rows such that in the end
2591 * they all have graph->n_total_row rows.
2592 * The extra rows don't belong to any band, so they get assigned band number -1.
2594 static int pad_schedule(struct isl_sched_graph
*graph
)
2598 for (i
= 0; i
< graph
->n
; ++i
) {
2599 struct isl_sched_node
*node
= &graph
->node
[i
];
2600 int row
= isl_mat_rows(node
->sched
);
2601 if (graph
->n_total_row
> row
) {
2602 isl_map_free(node
->sched_map
);
2603 node
->sched_map
= NULL
;
2605 node
->sched
= isl_mat_add_zero_rows(node
->sched
,
2606 graph
->n_total_row
- row
);
2609 for (j
= row
; j
< graph
->n_total_row
; ++j
)
2616 /* Reset the current band by dropping all its schedule rows.
2618 static int reset_band(struct isl_sched_graph
*graph
)
2623 drop
= graph
->n_total_row
- graph
->band_start
;
2624 graph
->n_total_row
-= drop
;
2625 graph
->n_row
-= drop
;
2627 for (i
= 0; i
< graph
->n
; ++i
) {
2628 struct isl_sched_node
*node
= &graph
->node
[i
];
2630 isl_map_free(node
->sched_map
);
2631 node
->sched_map
= NULL
;
2633 node
->sched
= isl_mat_drop_rows(node
->sched
,
2634 graph
->band_start
, drop
);
2643 /* Split the current graph into two parts and compute a schedule for each
2644 * part individually. In particular, one part consists of all SCCs up
2645 * to and including graph->src_scc, while the other part contains the other
2648 * The split is enforced in the schedule by constant rows with two different
2649 * values (0 and 1). These constant rows replace the previously computed rows
2650 * in the current band.
2651 * It would be possible to reuse them as the first rows in the next
2652 * band, but recomputing them may result in better rows as we are looking
2653 * at a smaller part of the dependence graph.
2655 * Since we do not enforce coincidence, we conservatively mark the
2656 * splitting row as not coincident.
2658 * The band_id of the second group is set to n, where n is the number
2659 * of nodes in the first group. This ensures that the band_ids over
2660 * the two groups remain disjoint, even if either or both of the two
2661 * groups contain independent components.
2663 static int compute_split_schedule(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2665 int i
, j
, n
, e1
, e2
;
2666 int n_total_row
, orig_total_row
;
2667 int n_band
, orig_band
;
2669 if (graph
->n_total_row
>= graph
->max_row
)
2670 isl_die(ctx
, isl_error_internal
,
2671 "too many schedule rows", return -1);
2673 if (reset_band(graph
) < 0)
2677 for (i
= 0; i
< graph
->n
; ++i
) {
2678 struct isl_sched_node
*node
= &graph
->node
[i
];
2679 int row
= isl_mat_rows(node
->sched
);
2680 int cols
= isl_mat_cols(node
->sched
);
2681 int before
= node
->scc
<= graph
->src_scc
;
2686 isl_map_free(node
->sched_map
);
2687 node
->sched_map
= NULL
;
2688 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
2691 node
->sched
= isl_mat_set_element_si(node
->sched
, row
, 0,
2693 for (j
= 1; j
< cols
; ++j
)
2694 node
->sched
= isl_mat_set_element_si(node
->sched
,
2696 node
->band
[graph
->n_total_row
] = graph
->n_band
;
2697 node
->coincident
[graph
->n_total_row
] = 0;
2701 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2702 if (graph
->edge
[i
].dst
->scc
<= graph
->src_scc
)
2704 if (graph
->edge
[i
].src
->scc
> graph
->src_scc
)
2708 graph
->n_total_row
++;
2711 for (i
= 0; i
< graph
->n
; ++i
) {
2712 struct isl_sched_node
*node
= &graph
->node
[i
];
2713 if (node
->scc
> graph
->src_scc
)
2714 node
->band_id
[graph
->n_band
] = n
;
2717 orig_total_row
= graph
->n_total_row
;
2718 orig_band
= graph
->n_band
;
2719 if (compute_sub_schedule(ctx
, graph
, n
, e1
,
2720 &node_scc_at_most
, &edge_dst_scc_at_most
,
2721 graph
->src_scc
, 0) < 0)
2723 n_total_row
= graph
->n_total_row
;
2724 graph
->n_total_row
= orig_total_row
;
2725 n_band
= graph
->n_band
;
2726 graph
->n_band
= orig_band
;
2727 if (compute_sub_schedule(ctx
, graph
, graph
->n
- n
, e2
,
2728 &node_scc_at_least
, &edge_src_scc_at_least
,
2729 graph
->src_scc
+ 1, 0) < 0)
2731 if (n_total_row
> graph
->n_total_row
)
2732 graph
->n_total_row
= n_total_row
;
2733 if (n_band
> graph
->n_band
)
2734 graph
->n_band
= n_band
;
2736 return pad_schedule(graph
);
2739 /* Compute the next band of the schedule after updating the dependence
2740 * relations based on the the current schedule.
2742 static int compute_next_band(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2744 if (update_edges(ctx
, graph
) < 0)
2748 return compute_schedule(ctx
, graph
);
2751 /* Add constraints to graph->lp that force the dependence "map" (which
2752 * is part of the dependence relation of "edge")
2753 * to be respected and attempt to carry it, where the edge is one from
2754 * a node j to itself. "pos" is the sequence number of the given map.
2755 * That is, add constraints that enforce
2757 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
2758 * = c_j_x (y - x) >= e_i
2760 * for each (x,y) in R.
2761 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2762 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
2763 * with each coefficient in c_j_x represented as a pair of non-negative
2766 static int add_intra_constraints(struct isl_sched_graph
*graph
,
2767 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
2770 isl_ctx
*ctx
= isl_map_get_ctx(map
);
2772 isl_dim_map
*dim_map
;
2773 isl_basic_set
*coef
;
2774 struct isl_sched_node
*node
= edge
->src
;
2776 coef
= intra_coefficients(graph
, map
);
2780 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
2782 total
= isl_basic_set_total_dim(graph
->lp
);
2783 dim_map
= isl_dim_map_alloc(ctx
, total
);
2784 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
2785 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
2786 isl_space_dim(dim
, isl_dim_set
), 1,
2788 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
2789 isl_space_dim(dim
, isl_dim_set
), 1,
2791 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
2792 coef
->n_eq
, coef
->n_ineq
);
2793 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
2795 isl_space_free(dim
);
2800 /* Add constraints to graph->lp that force the dependence "map" (which
2801 * is part of the dependence relation of "edge")
2802 * to be respected and attempt to carry it, where the edge is one from
2803 * node j to node k. "pos" is the sequence number of the given map.
2804 * That is, add constraints that enforce
2806 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
2808 * for each (x,y) in R.
2809 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2810 * of valid constraints for R and then plug in
2811 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
2812 * with each coefficient (except e_i, c_k_0 and c_j_0)
2813 * represented as a pair of non-negative coefficients.
2815 static int add_inter_constraints(struct isl_sched_graph
*graph
,
2816 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
2819 isl_ctx
*ctx
= isl_map_get_ctx(map
);
2821 isl_dim_map
*dim_map
;
2822 isl_basic_set
*coef
;
2823 struct isl_sched_node
*src
= edge
->src
;
2824 struct isl_sched_node
*dst
= edge
->dst
;
2826 coef
= inter_coefficients(graph
, map
);
2830 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
2832 total
= isl_basic_set_total_dim(graph
->lp
);
2833 dim_map
= isl_dim_map_alloc(ctx
, total
);
2835 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
2837 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, 1);
2838 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, -1);
2839 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, 1);
2840 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
2841 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
2843 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
2844 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
2847 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, -1);
2848 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, 1);
2849 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, -1);
2850 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
2851 isl_space_dim(dim
, isl_dim_set
), 1,
2853 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
2854 isl_space_dim(dim
, isl_dim_set
), 1,
2857 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
2858 coef
->n_eq
, coef
->n_ineq
);
2859 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
2861 isl_space_free(dim
);
2866 /* Add constraints to graph->lp that force all (conditional) validity
2867 * dependences to be respected and attempt to carry them.
2869 static int add_all_constraints(struct isl_sched_graph
*graph
)
2875 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2876 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2878 if (!edge
->validity
&& !edge
->conditional_validity
)
2881 for (j
= 0; j
< edge
->map
->n
; ++j
) {
2882 isl_basic_map
*bmap
;
2885 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
2886 map
= isl_map_from_basic_map(bmap
);
2888 if (edge
->src
== edge
->dst
&&
2889 add_intra_constraints(graph
, edge
, map
, pos
) < 0)
2891 if (edge
->src
!= edge
->dst
&&
2892 add_inter_constraints(graph
, edge
, map
, pos
) < 0)
2901 /* Count the number of equality and inequality constraints
2902 * that will be added to the carry_lp problem.
2903 * We count each edge exactly once.
2905 static int count_all_constraints(struct isl_sched_graph
*graph
,
2906 int *n_eq
, int *n_ineq
)
2910 *n_eq
= *n_ineq
= 0;
2911 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2912 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2913 for (j
= 0; j
< edge
->map
->n
; ++j
) {
2914 isl_basic_map
*bmap
;
2917 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
2918 map
= isl_map_from_basic_map(bmap
);
2920 if (count_map_constraints(graph
, edge
, map
,
2921 n_eq
, n_ineq
, 1, 0) < 0)
2929 /* Construct an LP problem for finding schedule coefficients
2930 * such that the schedule carries as many dependences as possible.
2931 * In particular, for each dependence i, we bound the dependence distance
2932 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
2933 * of all e_i's. Dependence with e_i = 0 in the solution are simply
2934 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
2935 * Note that if the dependence relation is a union of basic maps,
2936 * then we have to consider each basic map individually as it may only
2937 * be possible to carry the dependences expressed by some of those
2938 * basic maps and not all off them.
2939 * Below, we consider each of those basic maps as a separate "edge".
2941 * All variables of the LP are non-negative. The actual coefficients
2942 * may be negative, so each coefficient is represented as the difference
2943 * of two non-negative variables. The negative part always appears
2944 * immediately before the positive part.
2945 * Other than that, the variables have the following order
2947 * - sum of (1 - e_i) over all edges
2948 * - sum of positive and negative parts of all c_n coefficients
2949 * (unconstrained when computing non-parametric schedules)
2950 * - sum of positive and negative parts of all c_x coefficients
2955 * - positive and negative parts of c_i_n (if parametric)
2956 * - positive and negative parts of c_i_x
2958 * The constraints are those from the (validity) edges plus three equalities
2959 * to express the sums and n_edge inequalities to express e_i <= 1.
2961 static int setup_carry_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2971 for (i
= 0; i
< graph
->n_edge
; ++i
)
2972 n_edge
+= graph
->edge
[i
].map
->n
;
2975 for (i
= 0; i
< graph
->n
; ++i
) {
2976 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
2977 node
->start
= total
;
2978 total
+= 1 + 2 * (node
->nparam
+ node
->nvar
);
2981 if (count_all_constraints(graph
, &n_eq
, &n_ineq
) < 0)
2983 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2986 dim
= isl_space_set_alloc(ctx
, 0, total
);
2987 isl_basic_set_free(graph
->lp
);
2990 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
2991 graph
->lp
= isl_basic_set_set_rational(graph
->lp
);
2993 k
= isl_basic_set_alloc_equality(graph
->lp
);
2996 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2997 isl_int_set_si(graph
->lp
->eq
[k
][0], -n_edge
);
2998 isl_int_set_si(graph
->lp
->eq
[k
][1], 1);
2999 for (i
= 0; i
< n_edge
; ++i
)
3000 isl_int_set_si(graph
->lp
->eq
[k
][4 + i
], 1);
3002 k
= isl_basic_set_alloc_equality(graph
->lp
);
3005 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
3006 isl_int_set_si(graph
->lp
->eq
[k
][2], -1);
3007 for (i
= 0; i
< graph
->n
; ++i
) {
3008 int pos
= 1 + graph
->node
[i
].start
+ 1;
3010 for (j
= 0; j
< 2 * graph
->node
[i
].nparam
; ++j
)
3011 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
3014 k
= isl_basic_set_alloc_equality(graph
->lp
);
3017 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
3018 isl_int_set_si(graph
->lp
->eq
[k
][3], -1);
3019 for (i
= 0; i
< graph
->n
; ++i
) {
3020 struct isl_sched_node
*node
= &graph
->node
[i
];
3021 int pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
3023 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
3024 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
3027 for (i
= 0; i
< n_edge
; ++i
) {
3028 k
= isl_basic_set_alloc_inequality(graph
->lp
);
3031 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
3032 isl_int_set_si(graph
->lp
->ineq
[k
][4 + i
], -1);
3033 isl_int_set_si(graph
->lp
->ineq
[k
][0], 1);
3036 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
3038 if (add_all_constraints(graph
) < 0)
3044 /* If the schedule_split_scaled option is set and if the linear
3045 * parts of the scheduling rows for all nodes in the graphs have
3046 * non-trivial common divisor, then split off the constant term
3047 * from the linear part.
3048 * The constant term is then placed in a separate band and
3049 * the linear part is reduced.
3051 static int split_scaled(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3057 if (!ctx
->opt
->schedule_split_scaled
)
3062 if (graph
->n_total_row
>= graph
->max_row
)
3063 isl_die(ctx
, isl_error_internal
,
3064 "too many schedule rows", return -1);
3067 isl_int_init(gcd_i
);
3069 isl_int_set_si(gcd
, 0);
3071 row
= isl_mat_rows(graph
->node
[0].sched
) - 1;
3073 for (i
= 0; i
< graph
->n
; ++i
) {
3074 struct isl_sched_node
*node
= &graph
->node
[i
];
3075 int cols
= isl_mat_cols(node
->sched
);
3077 isl_seq_gcd(node
->sched
->row
[row
] + 1, cols
- 1, &gcd_i
);
3078 isl_int_gcd(gcd
, gcd
, gcd_i
);
3081 isl_int_clear(gcd_i
);
3083 if (isl_int_cmp_si(gcd
, 1) <= 0) {
3090 for (i
= 0; i
< graph
->n
; ++i
) {
3091 struct isl_sched_node
*node
= &graph
->node
[i
];
3093 isl_map_free(node
->sched_map
);
3094 node
->sched_map
= NULL
;
3095 node
->sched
= isl_mat_add_zero_rows(node
->sched
, 1);
3098 isl_int_fdiv_r(node
->sched
->row
[row
+ 1][0],
3099 node
->sched
->row
[row
][0], gcd
);
3100 isl_int_fdiv_q(node
->sched
->row
[row
][0],
3101 node
->sched
->row
[row
][0], gcd
);
3102 isl_int_mul(node
->sched
->row
[row
][0],
3103 node
->sched
->row
[row
][0], gcd
);
3104 node
->sched
= isl_mat_scale_down_row(node
->sched
, row
, gcd
);
3107 node
->band
[graph
->n_total_row
] = graph
->n_band
;
3110 graph
->n_total_row
++;
3119 static int compute_component_schedule(isl_ctx
*ctx
,
3120 struct isl_sched_graph
*graph
);
3122 /* Is the schedule row "sol" trivial on node "node"?
3123 * That is, is the solution zero on the dimensions orthogonal to
3124 * the previously found solutions?
3125 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3127 * Each coefficient is represented as the difference between
3128 * two non-negative values in "sol". "sol" has been computed
3129 * in terms of the original iterators (i.e., without use of cmap).
3130 * We construct the schedule row s and write it as a linear
3131 * combination of (linear combinations of) previously computed schedule rows.
3132 * s = Q c or c = U s.
3133 * If the final entries of c are all zero, then the solution is trivial.
3135 static int is_trivial(struct isl_sched_node
*node
, __isl_keep isl_vec
*sol
)
3145 if (node
->nvar
== node
->rank
)
3148 ctx
= isl_vec_get_ctx(sol
);
3149 node_sol
= isl_vec_alloc(ctx
, node
->nvar
);
3153 pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
3155 for (i
= 0; i
< node
->nvar
; ++i
)
3156 isl_int_sub(node_sol
->el
[i
],
3157 sol
->el
[pos
+ 2 * i
+ 1], sol
->el
[pos
+ 2 * i
]);
3159 node_sol
= isl_mat_vec_product(isl_mat_copy(node
->cinv
), node_sol
);
3164 trivial
= isl_seq_first_non_zero(node_sol
->el
+ node
->rank
,
3165 node
->nvar
- node
->rank
) == -1;
3167 isl_vec_free(node_sol
);
3172 /* Is the schedule row "sol" trivial on any node where it should
3174 * "sol" has been computed in terms of the original iterators
3175 * (i.e., without use of cmap).
3176 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3178 static int is_any_trivial(struct isl_sched_graph
*graph
,
3179 __isl_keep isl_vec
*sol
)
3183 for (i
= 0; i
< graph
->n
; ++i
) {
3184 struct isl_sched_node
*node
= &graph
->node
[i
];
3187 if (!needs_row(graph
, node
))
3189 trivial
= is_trivial(node
, sol
);
3190 if (trivial
< 0 || trivial
)
3197 /* Construct a schedule row for each node such that as many dependences
3198 * as possible are carried and then continue with the next band.
3200 * If the computed schedule row turns out to be trivial on one or
3201 * more nodes where it should not be trivial, then we throw it away
3202 * and try again on each component separately.
3204 static int carry_dependences(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3213 for (i
= 0; i
< graph
->n_edge
; ++i
)
3214 n_edge
+= graph
->edge
[i
].map
->n
;
3216 if (setup_carry_lp(ctx
, graph
) < 0)
3219 lp
= isl_basic_set_copy(graph
->lp
);
3220 sol
= isl_tab_basic_set_non_neg_lexmin(lp
);
3224 if (sol
->size
== 0) {
3226 isl_die(ctx
, isl_error_internal
,
3227 "error in schedule construction", return -1);
3230 isl_int_divexact(sol
->el
[1], sol
->el
[1], sol
->el
[0]);
3231 if (isl_int_cmp_si(sol
->el
[1], n_edge
) >= 0) {
3233 isl_die(ctx
, isl_error_unknown
,
3234 "unable to carry dependences", return -1);
3237 trivial
= is_any_trivial(graph
, sol
);
3239 sol
= isl_vec_free(sol
);
3240 } else if (trivial
) {
3243 return compute_component_schedule(ctx
, graph
);
3244 isl_die(ctx
, isl_error_unknown
,
3245 "unable to construct non-trivial solution", return -1);
3248 if (update_schedule(graph
, sol
, 0, 0) < 0)
3251 if (split_scaled(ctx
, graph
) < 0)
3254 return compute_next_band(ctx
, graph
);
3257 /* Are there any (non-empty) (conditional) validity edges in the graph?
3259 static int has_validity_edges(struct isl_sched_graph
*graph
)
3263 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3266 empty
= isl_map_plain_is_empty(graph
->edge
[i
].map
);
3271 if (graph
->edge
[i
].validity
||
3272 graph
->edge
[i
].conditional_validity
)
3279 /* Should we apply a Feautrier step?
3280 * That is, did the user request the Feautrier algorithm and are
3281 * there any validity dependences (left)?
3283 static int need_feautrier_step(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3285 if (ctx
->opt
->schedule_algorithm
!= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
)
3288 return has_validity_edges(graph
);
3291 /* Compute a schedule for a connected dependence graph using Feautrier's
3292 * multi-dimensional scheduling algorithm.
3293 * The original algorithm is described in [1].
3294 * The main idea is to minimize the number of scheduling dimensions, by
3295 * trying to satisfy as many dependences as possible per scheduling dimension.
3297 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
3298 * Problem, Part II: Multi-Dimensional Time.
3299 * In Intl. Journal of Parallel Programming, 1992.
3301 static int compute_schedule_wcc_feautrier(isl_ctx
*ctx
,
3302 struct isl_sched_graph
*graph
)
3304 return carry_dependences(ctx
, graph
);
3307 /* Turn off the "local" bit on all (condition) edges.
3309 static void clear_local_edges(struct isl_sched_graph
*graph
)
3313 for (i
= 0; i
< graph
->n_edge
; ++i
)
3314 if (graph
->edge
[i
].condition
)
3315 graph
->edge
[i
].local
= 0;
3318 /* Does "graph" have both condition and conditional validity edges?
3320 static int need_condition_check(struct isl_sched_graph
*graph
)
3323 int any_condition
= 0;
3324 int any_conditional_validity
= 0;
3326 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3327 if (graph
->edge
[i
].condition
)
3329 if (graph
->edge
[i
].conditional_validity
)
3330 any_conditional_validity
= 1;
3333 return any_condition
&& any_conditional_validity
;
3336 /* Does "graph" contain any coincidence edge?
3338 static int has_any_coincidence(struct isl_sched_graph
*graph
)
3342 for (i
= 0; i
< graph
->n_edge
; ++i
)
3343 if (graph
->edge
[i
].coincidence
)
3349 /* Extract the final schedule row as a map with the iteration domain
3350 * of "node" as domain.
3352 static __isl_give isl_map
*final_row(struct isl_sched_node
*node
)
3354 isl_local_space
*ls
;
3358 row
= isl_mat_rows(node
->sched
) - 1;
3359 ls
= isl_local_space_from_space(isl_space_copy(node
->dim
));
3360 aff
= extract_schedule_row(ls
, node
, row
);
3361 return isl_map_from_aff(aff
);
3364 /* Is the conditional validity dependence in the edge with index "edge_index"
3365 * violated by the latest (i.e., final) row of the schedule?
3366 * That is, is i scheduled after j
3367 * for any conditional validity dependence i -> j?
3369 static int is_violated(struct isl_sched_graph
*graph
, int edge_index
)
3371 isl_map
*src_sched
, *dst_sched
, *map
;
3372 struct isl_sched_edge
*edge
= &graph
->edge
[edge_index
];
3375 src_sched
= final_row(edge
->src
);
3376 dst_sched
= final_row(edge
->dst
);
3377 map
= isl_map_copy(edge
->map
);
3378 map
= isl_map_apply_domain(map
, src_sched
);
3379 map
= isl_map_apply_range(map
, dst_sched
);
3380 map
= isl_map_order_gt(map
, isl_dim_in
, 0, isl_dim_out
, 0);
3381 empty
= isl_map_is_empty(map
);
3390 /* Does the domain of "umap" intersect "uset"?
3392 static int domain_intersects(__isl_keep isl_union_map
*umap
,
3393 __isl_keep isl_union_set
*uset
)
3397 umap
= isl_union_map_copy(umap
);
3398 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(uset
));
3399 empty
= isl_union_map_is_empty(umap
);
3400 isl_union_map_free(umap
);
3402 return empty
< 0 ? -1 : !empty
;
3405 /* Does the range of "umap" intersect "uset"?
3407 static int range_intersects(__isl_keep isl_union_map
*umap
,
3408 __isl_keep isl_union_set
*uset
)
3412 umap
= isl_union_map_copy(umap
);
3413 umap
= isl_union_map_intersect_range(umap
, isl_union_set_copy(uset
));
3414 empty
= isl_union_map_is_empty(umap
);
3415 isl_union_map_free(umap
);
3417 return empty
< 0 ? -1 : !empty
;
3420 /* Are the condition dependences of "edge" local with respect to
3421 * the current schedule?
3423 * That is, are domain and range of the condition dependences mapped
3424 * to the same point?
3426 * In other words, is the condition false?
3428 static int is_condition_false(struct isl_sched_edge
*edge
)
3430 isl_union_map
*umap
;
3431 isl_map
*map
, *sched
, *test
;
3434 umap
= isl_union_map_copy(edge
->tagged_condition
);
3435 umap
= isl_union_map_zip(umap
);
3436 umap
= isl_union_set_unwrap(isl_union_map_domain(umap
));
3437 map
= isl_map_from_union_map(umap
);
3439 sched
= node_extract_schedule(edge
->src
);
3440 map
= isl_map_apply_domain(map
, sched
);
3441 sched
= node_extract_schedule(edge
->dst
);
3442 map
= isl_map_apply_range(map
, sched
);
3444 test
= isl_map_identity(isl_map_get_space(map
));
3445 local
= isl_map_is_subset(map
, test
);
3452 /* Does "graph" have any satisfied condition edges that
3453 * are adjacent to the conditional validity constraint with
3454 * domain "conditional_source" and range "conditional_sink"?
3456 * A satisfied condition is one that is not local.
3457 * If a condition was forced to be local already (i.e., marked as local)
3458 * then there is no need to check if it is in fact local.
3460 * Additionally, mark all adjacent condition edges found as local.
3462 static int has_adjacent_true_conditions(struct isl_sched_graph
*graph
,
3463 __isl_keep isl_union_set
*conditional_source
,
3464 __isl_keep isl_union_set
*conditional_sink
)
3469 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3470 int adjacent
, local
;
3471 isl_union_map
*condition
;
3473 if (!graph
->edge
[i
].condition
)
3475 if (graph
->edge
[i
].local
)
3478 condition
= graph
->edge
[i
].tagged_condition
;
3479 adjacent
= domain_intersects(condition
, conditional_sink
);
3480 if (adjacent
>= 0 && !adjacent
)
3481 adjacent
= range_intersects(condition
,
3482 conditional_source
);
3488 graph
->edge
[i
].local
= 1;
3490 local
= is_condition_false(&graph
->edge
[i
]);
3500 /* Are there any violated conditional validity dependences with
3501 * adjacent condition dependences that are not local with respect
3502 * to the current schedule?
3503 * That is, is the conditional validity constraint violated?
3505 * Additionally, mark all those adjacent condition dependences as local.
3506 * We also mark those adjacent condition dependences that were not marked
3507 * as local before, but just happened to be local already. This ensures
3508 * that they remain local if the schedule is recomputed.
3510 * We first collect domain and range of all violated conditional validity
3511 * dependences and then check if there are any adjacent non-local
3512 * condition dependences.
3514 static int has_violated_conditional_constraint(isl_ctx
*ctx
,
3515 struct isl_sched_graph
*graph
)
3519 isl_union_set
*source
, *sink
;
3521 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3522 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3523 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3524 isl_union_set
*uset
;
3525 isl_union_map
*umap
;
3528 if (!graph
->edge
[i
].conditional_validity
)
3531 violated
= is_violated(graph
, i
);
3539 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
3540 uset
= isl_union_map_domain(umap
);
3541 source
= isl_union_set_union(source
, uset
);
3542 source
= isl_union_set_coalesce(source
);
3544 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
3545 uset
= isl_union_map_range(umap
);
3546 sink
= isl_union_set_union(sink
, uset
);
3547 sink
= isl_union_set_coalesce(sink
);
3551 any
= has_adjacent_true_conditions(graph
, source
, sink
);
3553 isl_union_set_free(source
);
3554 isl_union_set_free(sink
);
3557 isl_union_set_free(source
);
3558 isl_union_set_free(sink
);
3562 /* Compute a schedule for a connected dependence graph.
3563 * We try to find a sequence of as many schedule rows as possible that result
3564 * in non-negative dependence distances (independent of the previous rows
3565 * in the sequence, i.e., such that the sequence is tilable), with as
3566 * many of the initial rows as possible satisfying the coincidence constraints.
3567 * If we can't find any more rows we either
3568 * - split between SCCs and start over (assuming we found an interesting
3569 * pair of SCCs between which to split)
3570 * - continue with the next band (assuming the current band has at least
3572 * - try to carry as many dependences as possible and continue with the next
3575 * If Feautrier's algorithm is selected, we first recursively try to satisfy
3576 * as many validity dependences as possible. When all validity dependences
3577 * are satisfied we extend the schedule to a full-dimensional schedule.
3579 * If we manage to complete the schedule, we finish off by topologically
3580 * sorting the statements based on the remaining dependences.
3582 * If ctx->opt->schedule_outer_coincidence is set, then we force the
3583 * outermost dimension to satisfy the coincidence constraints. If this
3584 * turns out to be impossible, we fall back on the general scheme above
3585 * and try to carry as many dependences as possible.
3587 * If "graph" contains both condition and conditional validity dependences,
3588 * then we need to check that that the conditional schedule constraint
3589 * is satisfied, i.e., there are no violated conditional validity dependences
3590 * that are adjacent to any non-local condition dependences.
3591 * If there are, then we mark all those adjacent condition dependences
3592 * as local and recompute the current band. Those dependences that
3593 * are marked local will then be forced to be local.
3594 * The initial computation is performed with no dependences marked as local.
3595 * If we are lucky, then there will be no violated conditional validity
3596 * dependences adjacent to any non-local condition dependences.
3597 * Otherwise, we mark some additional condition dependences as local and
3598 * recompute. We continue this process until there are no violations left or
3599 * until we are no longer able to compute a schedule.
3600 * Since there are only a finite number of dependences,
3601 * there will only be a finite number of iterations.
3603 static int compute_schedule_wcc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3605 int has_coincidence
;
3606 int use_coincidence
;
3607 int force_coincidence
= 0;
3608 int check_conditional
;
3610 if (detect_sccs(ctx
, graph
) < 0)
3612 if (sort_sccs(graph
) < 0)
3615 if (compute_maxvar(graph
) < 0)
3618 if (need_feautrier_step(ctx
, graph
))
3619 return compute_schedule_wcc_feautrier(ctx
, graph
);
3621 clear_local_edges(graph
);
3622 check_conditional
= need_condition_check(graph
);
3623 has_coincidence
= has_any_coincidence(graph
);
3625 if (ctx
->opt
->schedule_outer_coincidence
)
3626 force_coincidence
= 1;
3628 use_coincidence
= has_coincidence
;
3629 while (graph
->n_row
< graph
->maxvar
) {
3634 graph
->src_scc
= -1;
3635 graph
->dst_scc
= -1;
3637 if (setup_lp(ctx
, graph
, use_coincidence
) < 0)
3639 sol
= solve_lp(graph
);
3642 if (sol
->size
== 0) {
3643 int empty
= graph
->n_total_row
== graph
->band_start
;
3646 if (use_coincidence
&& (!force_coincidence
|| !empty
)) {
3647 use_coincidence
= 0;
3650 if (!ctx
->opt
->schedule_maximize_band_depth
&& !empty
)
3651 return compute_next_band(ctx
, graph
);
3652 if (graph
->src_scc
>= 0)
3653 return compute_split_schedule(ctx
, graph
);
3655 return compute_next_band(ctx
, graph
);
3656 return carry_dependences(ctx
, graph
);
3658 coincident
= !has_coincidence
|| use_coincidence
;
3659 if (update_schedule(graph
, sol
, 1, coincident
) < 0)
3662 if (!check_conditional
)
3664 violated
= has_violated_conditional_constraint(ctx
, graph
);
3669 if (reset_band(graph
) < 0)
3671 use_coincidence
= has_coincidence
;
3674 if (graph
->n_total_row
> graph
->band_start
)
3676 return sort_statements(ctx
, graph
);
3679 /* Add a row to the schedules that separates the SCCs and move
3682 static int split_on_scc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3686 if (graph
->n_total_row
>= graph
->max_row
)
3687 isl_die(ctx
, isl_error_internal
,
3688 "too many schedule rows", return -1);
3690 for (i
= 0; i
< graph
->n
; ++i
) {
3691 struct isl_sched_node
*node
= &graph
->node
[i
];
3692 int row
= isl_mat_rows(node
->sched
);
3694 isl_map_free(node
->sched_map
);
3695 node
->sched_map
= NULL
;
3696 node
->sched
= isl_mat_add_zero_rows(node
->sched
, 1);
3697 node
->sched
= isl_mat_set_element_si(node
->sched
, row
, 0,
3701 node
->band
[graph
->n_total_row
] = graph
->n_band
;
3704 graph
->n_total_row
++;
3710 /* Compute a schedule for each component (identified by node->scc)
3711 * of the dependence graph separately and then combine the results.
3712 * Depending on the setting of schedule_fuse, a component may be
3713 * either weakly or strongly connected.
3715 * The band_id is adjusted such that each component has a separate id.
3716 * Note that the band_id may have already been set to a value different
3717 * from zero by compute_split_schedule.
3719 static int compute_component_schedule(isl_ctx
*ctx
,
3720 struct isl_sched_graph
*graph
)
3724 int n_total_row
, orig_total_row
;
3725 int n_band
, orig_band
;
3727 if (ctx
->opt
->schedule_fuse
== ISL_SCHEDULE_FUSE_MIN
||
3728 ctx
->opt
->schedule_separate_components
)
3729 if (split_on_scc(ctx
, graph
) < 0)
3733 orig_total_row
= graph
->n_total_row
;
3735 orig_band
= graph
->n_band
;
3736 for (i
= 0; i
< graph
->n
; ++i
)
3737 graph
->node
[i
].band_id
[graph
->n_band
] += graph
->node
[i
].scc
;
3738 for (wcc
= 0; wcc
< graph
->scc
; ++wcc
) {
3740 for (i
= 0; i
< graph
->n
; ++i
)
3741 if (graph
->node
[i
].scc
== wcc
)
3744 for (i
= 0; i
< graph
->n_edge
; ++i
)
3745 if (graph
->edge
[i
].src
->scc
== wcc
&&
3746 graph
->edge
[i
].dst
->scc
== wcc
)
3749 if (compute_sub_schedule(ctx
, graph
, n
, n_edge
,
3751 &edge_scc_exactly
, wcc
, 1) < 0)
3753 if (graph
->n_total_row
> n_total_row
)
3754 n_total_row
= graph
->n_total_row
;
3755 graph
->n_total_row
= orig_total_row
;
3756 if (graph
->n_band
> n_band
)
3757 n_band
= graph
->n_band
;
3758 graph
->n_band
= orig_band
;
3761 graph
->n_total_row
= n_total_row
;
3762 graph
->n_band
= n_band
;
3764 return pad_schedule(graph
);
3767 /* Compute a schedule for the given dependence graph.
3768 * We first check if the graph is connected (through validity and conditional
3769 * validity dependences) and, if not, compute a schedule
3770 * for each component separately.
3771 * If schedule_fuse is set to minimal fusion, then we check for strongly
3772 * connected components instead and compute a separate schedule for
3773 * each such strongly connected component.
3775 static int compute_schedule(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3777 if (ctx
->opt
->schedule_fuse
== ISL_SCHEDULE_FUSE_MIN
) {
3778 if (detect_sccs(ctx
, graph
) < 0)
3781 if (detect_wccs(ctx
, graph
) < 0)
3786 return compute_component_schedule(ctx
, graph
);
3788 return compute_schedule_wcc(ctx
, graph
);
3791 /* Compute a schedule on sc->domain that respects the given schedule
3794 * In particular, the schedule respects all the validity dependences.
3795 * If the default isl scheduling algorithm is used, it tries to minimize
3796 * the dependence distances over the proximity dependences.
3797 * If Feautrier's scheduling algorithm is used, the proximity dependence
3798 * distances are only minimized during the extension to a full-dimensional
3801 * If there are any condition and conditional validity dependences,
3802 * then the conditional validity dependences may be violated inside
3803 * a tilable band, provided they have no adjacent non-local
3804 * condition dependences.
3806 __isl_give isl_schedule
*isl_schedule_constraints_compute_schedule(
3807 __isl_take isl_schedule_constraints
*sc
)
3809 isl_ctx
*ctx
= isl_schedule_constraints_get_ctx(sc
);
3810 struct isl_sched_graph graph
= { 0 };
3811 isl_schedule
*sched
;
3812 struct isl_extract_edge_data data
;
3813 enum isl_edge_type i
;
3815 sc
= isl_schedule_constraints_align_params(sc
);
3819 graph
.n
= isl_union_set_n_set(sc
->domain
);
3822 if (graph_alloc(ctx
, &graph
, graph
.n
,
3823 isl_schedule_constraints_n_map(sc
)) < 0)
3825 if (compute_max_row(&graph
, sc
->domain
) < 0)
3829 if (isl_union_set_foreach_set(sc
->domain
, &extract_node
, &graph
) < 0)
3831 if (graph_init_table(ctx
, &graph
) < 0)
3833 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
3834 graph
.max_edge
[i
] = isl_union_map_n_map(sc
->constraint
[i
]);
3835 if (graph_init_edge_tables(ctx
, &graph
) < 0)
3838 data
.graph
= &graph
;
3839 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
3841 if (isl_union_map_foreach_map(sc
->constraint
[i
],
3842 &extract_edge
, &data
) < 0)
3846 if (compute_schedule(ctx
, &graph
) < 0)
3850 sched
= extract_schedule(&graph
, isl_union_set_get_space(sc
->domain
));
3852 graph_free(ctx
, &graph
);
3853 isl_schedule_constraints_free(sc
);
3857 graph_free(ctx
, &graph
);
3858 isl_schedule_constraints_free(sc
);
3862 /* Compute a schedule for the given union of domains that respects
3863 * all the validity dependences and minimizes
3864 * the dependence distances over the proximity dependences.
3866 * This function is kept for backward compatibility.
3868 __isl_give isl_schedule
*isl_union_set_compute_schedule(
3869 __isl_take isl_union_set
*domain
,
3870 __isl_take isl_union_map
*validity
,
3871 __isl_take isl_union_map
*proximity
)
3873 isl_schedule_constraints
*sc
;
3875 sc
= isl_schedule_constraints_on_domain(domain
);
3876 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
3877 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
3879 return isl_schedule_constraints_compute_schedule(sc
);
3882 __isl_null isl_schedule
*isl_schedule_free(__isl_take isl_schedule
*sched
)
3888 if (--sched
->ref
> 0)
3891 for (i
= 0; i
< sched
->n
; ++i
) {
3892 isl_multi_aff_free(sched
->node
[i
].sched
);
3893 free(sched
->node
[i
].band_end
);
3894 free(sched
->node
[i
].band_id
);
3895 free(sched
->node
[i
].coincident
);
3897 isl_space_free(sched
->dim
);
3898 isl_band_list_free(sched
->band_forest
);
3903 isl_ctx
*isl_schedule_get_ctx(__isl_keep isl_schedule
*schedule
)
3905 return schedule
? isl_space_get_ctx(schedule
->dim
) : NULL
;
3908 /* Set max_out to the maximal number of output dimensions over
3911 static int update_max_out(__isl_take isl_map
*map
, void *user
)
3913 int *max_out
= user
;
3914 int n_out
= isl_map_dim(map
, isl_dim_out
);
3916 if (n_out
> *max_out
)
3923 /* Internal data structure for map_pad_range.
3925 * "max_out" is the maximal schedule dimension.
3926 * "res" collects the results.
3928 struct isl_pad_schedule_map_data
{
3933 /* Pad the range of the given map with zeros to data->max_out and
3934 * then add the result to data->res.
3936 static int map_pad_range(__isl_take isl_map
*map
, void *user
)
3938 struct isl_pad_schedule_map_data
*data
= user
;
3940 int n_out
= isl_map_dim(map
, isl_dim_out
);
3942 map
= isl_map_add_dims(map
, isl_dim_out
, data
->max_out
- n_out
);
3943 for (i
= n_out
; i
< data
->max_out
; ++i
)
3944 map
= isl_map_fix_si(map
, isl_dim_out
, i
, 0);
3946 data
->res
= isl_union_map_add_map(data
->res
, map
);
3953 /* Pad the ranges of the maps in the union map with zeros such they all have
3954 * the same dimension.
3956 static __isl_give isl_union_map
*pad_schedule_map(
3957 __isl_take isl_union_map
*umap
)
3959 struct isl_pad_schedule_map_data data
;
3963 if (isl_union_map_n_map(umap
) <= 1)
3967 if (isl_union_map_foreach_map(umap
, &update_max_out
, &data
.max_out
) < 0)
3968 return isl_union_map_free(umap
);
3970 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3971 if (isl_union_map_foreach_map(umap
, &map_pad_range
, &data
) < 0)
3972 data
.res
= isl_union_map_free(data
.res
);
3974 isl_union_map_free(umap
);
3978 /* Return an isl_union_map of the schedule. If we have already constructed
3979 * a band forest, then this band forest may have been modified so we need
3980 * to extract the isl_union_map from the forest rather than from
3981 * the originally computed schedule. This reconstructed schedule map
3982 * then needs to be padded with zeros to unify the schedule space
3983 * since the result of isl_band_list_get_suffix_schedule may not have
3984 * a unified schedule space.
3986 __isl_give isl_union_map
*isl_schedule_get_map(__isl_keep isl_schedule
*sched
)
3989 isl_union_map
*umap
;
3994 if (sched
->band_forest
) {
3995 umap
= isl_band_list_get_suffix_schedule(sched
->band_forest
);
3996 return pad_schedule_map(umap
);
3999 umap
= isl_union_map_empty(isl_space_copy(sched
->dim
));
4000 for (i
= 0; i
< sched
->n
; ++i
) {
4003 ma
= isl_multi_aff_copy(sched
->node
[i
].sched
);
4004 umap
= isl_union_map_add_map(umap
, isl_map_from_multi_aff(ma
));
4010 static __isl_give isl_band_list
*construct_band_list(
4011 __isl_keep isl_schedule
*schedule
, __isl_keep isl_band
*parent
,
4012 int band_nr
, int *parent_active
, int n_active
);
4014 /* Construct an isl_band structure for the band in the given schedule
4015 * with sequence number band_nr for the n_active nodes marked by active.
4016 * If the nodes don't have a band with the given sequence number,
4017 * then a band without members is created.
4019 * Because of the way the schedule is constructed, we know that
4020 * the position of the band inside the schedule of a node is the same
4021 * for all active nodes.
4023 * The partial schedule for the band is created before the children
4024 * are created to that construct_band_list can refer to the partial
4025 * schedule of the parent.
4027 static __isl_give isl_band
*construct_band(__isl_keep isl_schedule
*schedule
,
4028 __isl_keep isl_band
*parent
,
4029 int band_nr
, int *active
, int n_active
)
4032 isl_ctx
*ctx
= isl_schedule_get_ctx(schedule
);
4034 unsigned start
, end
;
4036 band
= isl_band_alloc(ctx
);
4040 band
->schedule
= schedule
;
4041 band
->parent
= parent
;
4043 for (i
= 0; i
< schedule
->n
; ++i
)
4047 if (i
>= schedule
->n
)
4048 isl_die(ctx
, isl_error_internal
,
4049 "band without active statements", goto error
);
4051 start
= band_nr
? schedule
->node
[i
].band_end
[band_nr
- 1] : 0;
4052 end
= band_nr
< schedule
->node
[i
].n_band
?
4053 schedule
->node
[i
].band_end
[band_nr
] : start
;
4054 band
->n
= end
- start
;
4056 band
->coincident
= isl_alloc_array(ctx
, int, band
->n
);
4057 if (band
->n
&& !band
->coincident
)
4060 for (j
= 0; j
< band
->n
; ++j
)
4061 band
->coincident
[j
] = schedule
->node
[i
].coincident
[start
+ j
];
4063 band
->pma
= isl_union_pw_multi_aff_empty(isl_space_copy(schedule
->dim
));
4064 for (i
= 0; i
< schedule
->n
; ++i
) {
4066 isl_pw_multi_aff
*pma
;
4072 ma
= isl_multi_aff_copy(schedule
->node
[i
].sched
);
4073 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
4074 ma
= isl_multi_aff_drop_dims(ma
, isl_dim_out
, end
, n_out
- end
);
4075 ma
= isl_multi_aff_drop_dims(ma
, isl_dim_out
, 0, start
);
4076 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
4077 band
->pma
= isl_union_pw_multi_aff_add_pw_multi_aff(band
->pma
,
4083 for (i
= 0; i
< schedule
->n
; ++i
)
4084 if (active
[i
] && schedule
->node
[i
].n_band
> band_nr
+ 1)
4087 if (i
< schedule
->n
) {
4088 band
->children
= construct_band_list(schedule
, band
,
4089 band_nr
+ 1, active
, n_active
);
4090 if (!band
->children
)
4096 isl_band_free(band
);
4100 /* Internal data structure used inside cmp_band and pw_multi_aff_extract_int.
4102 * r is set to a negative value if anything goes wrong.
4104 * c1 stores the result of extract_int.
4105 * c2 is a temporary value used inside cmp_band_in_ancestor.
4106 * t is a temporary value used inside extract_int.
4108 * first and equal are used inside extract_int.
4109 * first is set if we are looking at the first isl_multi_aff inside
4110 * the isl_union_pw_multi_aff.
4111 * equal is set if all the isl_multi_affs have been equal so far.
4113 struct isl_cmp_band_data
{
4124 /* Check if "ma" assigns a constant value.
4125 * Note that this function is only called on isl_multi_affs
4126 * with a single output dimension.
4128 * If "ma" assigns a constant value then we compare it to data->c1
4129 * or assign it to data->c1 if this is the first isl_multi_aff we consider.
4130 * If "ma" does not assign a constant value or if it assigns a value
4131 * that is different from data->c1, then we set data->equal to zero
4132 * and terminate the check.
4134 static int multi_aff_extract_int(__isl_take isl_set
*set
,
4135 __isl_take isl_multi_aff
*ma
, void *user
)
4138 struct isl_cmp_band_data
*data
= user
;
4140 aff
= isl_multi_aff_get_aff(ma
, 0);
4141 data
->r
= isl_aff_is_cst(aff
);
4142 if (data
->r
>= 0 && data
->r
) {
4143 isl_aff_get_constant(aff
, &data
->t
);
4145 isl_int_set(data
->c1
, data
->t
);
4147 } else if (!isl_int_eq(data
->c1
, data
->t
))
4149 } else if (data
->r
>= 0 && !data
->r
)
4154 isl_multi_aff_free(ma
);
4163 /* This function is called for each isl_pw_multi_aff in
4164 * the isl_union_pw_multi_aff checked by extract_int.
4165 * Check all the isl_multi_affs inside "pma".
4167 static int pw_multi_aff_extract_int(__isl_take isl_pw_multi_aff
*pma
,
4172 r
= isl_pw_multi_aff_foreach_piece(pma
, &multi_aff_extract_int
, user
);
4173 isl_pw_multi_aff_free(pma
);
4178 /* Check if "upma" assigns a single constant value to its domain.
4179 * If so, return 1 and store the result in data->c1.
4182 * A negative return value from isl_union_pw_multi_aff_foreach_pw_multi_aff
4183 * means that either an error occurred or that we have broken off the check
4184 * because we already know the result is going to be negative.
4185 * In the latter case, data->equal is set to zero.
4187 static int extract_int(__isl_keep isl_union_pw_multi_aff
*upma
,
4188 struct isl_cmp_band_data
*data
)
4193 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
4194 &pw_multi_aff_extract_int
, data
) < 0) {
4200 return !data
->first
&& data
->equal
;
4203 /* Compare "b1" and "b2" based on the parent schedule of their ancestor
4206 * If the parent of "ancestor" also has a single member, then we
4207 * first try to compare the two band based on the partial schedule
4210 * Otherwise, or if the result is inconclusive, we look at the partial schedule
4211 * of "ancestor" itself.
4212 * In particular, we specialize the parent schedule based
4213 * on the domains of the child schedules, check if both assign
4214 * a single constant value and, if so, compare the two constant values.
4215 * If the specialized parent schedules do not assign a constant value,
4216 * then they cannot be used to order the two bands and so in this case
4219 static int cmp_band_in_ancestor(__isl_keep isl_band
*b1
,
4220 __isl_keep isl_band
*b2
, struct isl_cmp_band_data
*data
,
4221 __isl_keep isl_band
*ancestor
)
4223 isl_union_pw_multi_aff
*upma
;
4224 isl_union_set
*domain
;
4230 if (ancestor
->parent
&& ancestor
->parent
->n
== 1) {
4231 r
= cmp_band_in_ancestor(b1
, b2
, data
, ancestor
->parent
);
4238 upma
= isl_union_pw_multi_aff_copy(b1
->pma
);
4239 domain
= isl_union_pw_multi_aff_domain(upma
);
4240 upma
= isl_union_pw_multi_aff_copy(ancestor
->pma
);
4241 upma
= isl_union_pw_multi_aff_intersect_domain(upma
, domain
);
4242 r
= extract_int(upma
, data
);
4243 isl_union_pw_multi_aff_free(upma
);
4250 isl_int_set(data
->c2
, data
->c1
);
4252 upma
= isl_union_pw_multi_aff_copy(b2
->pma
);
4253 domain
= isl_union_pw_multi_aff_domain(upma
);
4254 upma
= isl_union_pw_multi_aff_copy(ancestor
->pma
);
4255 upma
= isl_union_pw_multi_aff_intersect_domain(upma
, domain
);
4256 r
= extract_int(upma
, data
);
4257 isl_union_pw_multi_aff_free(upma
);
4264 return isl_int_cmp(data
->c2
, data
->c1
);
4267 /* Compare "a" and "b" based on the parent schedule of their parent.
4269 static int cmp_band(const void *a
, const void *b
, void *user
)
4271 isl_band
*b1
= *(isl_band
* const *) a
;
4272 isl_band
*b2
= *(isl_band
* const *) b
;
4273 struct isl_cmp_band_data
*data
= user
;
4275 return cmp_band_in_ancestor(b1
, b2
, data
, b1
->parent
);
4278 /* Sort the elements in "list" based on the partial schedules of its parent
4279 * (and ancestors). In particular if the parent assigns constant values
4280 * to the domains of the bands in "list", then the elements are sorted
4281 * according to that order.
4282 * This order should be a more "natural" order for the user, but otherwise
4283 * shouldn't have any effect.
4284 * If we would be constructing an isl_band forest directly in
4285 * isl_schedule_constraints_compute_schedule then there wouldn't be any need
4286 * for a reordering, since the children would be added to the list
4287 * in their natural order automatically.
4289 * If there is only one element in the list, then there is no need to sort
4291 * If the partial schedule of the parent has more than one member
4292 * (or if there is no parent), then it's
4293 * defnitely not assigning constant values to the different children in
4294 * the list and so we wouldn't be able to use it to sort the list.
4296 static __isl_give isl_band_list
*sort_band_list(__isl_take isl_band_list
*list
,
4297 __isl_keep isl_band
*parent
)
4299 struct isl_cmp_band_data data
;
4305 if (!parent
|| parent
->n
!= 1)
4309 isl_int_init(data
.c1
);
4310 isl_int_init(data
.c2
);
4311 isl_int_init(data
.t
);
4312 isl_sort(list
->p
, list
->n
, sizeof(list
->p
[0]), &cmp_band
, &data
);
4314 list
= isl_band_list_free(list
);
4315 isl_int_clear(data
.c1
);
4316 isl_int_clear(data
.c2
);
4317 isl_int_clear(data
.t
);
4322 /* Construct a list of bands that start at the same position (with
4323 * sequence number band_nr) in the schedules of the nodes that
4324 * were active in the parent band.
4326 * A separate isl_band structure is created for each band_id
4327 * and for each node that does not have a band with sequence
4328 * number band_nr. In the latter case, a band without members
4330 * This ensures that if a band has any children, then each node
4331 * that was active in the band is active in exactly one of the children.
4333 static __isl_give isl_band_list
*construct_band_list(
4334 __isl_keep isl_schedule
*schedule
, __isl_keep isl_band
*parent
,
4335 int band_nr
, int *parent_active
, int n_active
)
4338 isl_ctx
*ctx
= isl_schedule_get_ctx(schedule
);
4341 isl_band_list
*list
;
4344 for (i
= 0; i
< n_active
; ++i
) {
4345 for (j
= 0; j
< schedule
->n
; ++j
) {
4346 if (!parent_active
[j
])
4348 if (schedule
->node
[j
].n_band
<= band_nr
)
4350 if (schedule
->node
[j
].band_id
[band_nr
] == i
) {
4356 for (j
= 0; j
< schedule
->n
; ++j
)
4357 if (schedule
->node
[j
].n_band
<= band_nr
)
4362 list
= isl_band_list_alloc(ctx
, n_band
);
4363 band
= construct_band(schedule
, parent
, band_nr
,
4364 parent_active
, n_active
);
4365 return isl_band_list_add(list
, band
);
4368 active
= isl_alloc_array(ctx
, int, schedule
->n
);
4369 if (schedule
->n
&& !active
)
4372 list
= isl_band_list_alloc(ctx
, n_band
);
4374 for (i
= 0; i
< n_active
; ++i
) {
4378 for (j
= 0; j
< schedule
->n
; ++j
) {
4379 active
[j
] = parent_active
[j
] &&
4380 schedule
->node
[j
].n_band
> band_nr
&&
4381 schedule
->node
[j
].band_id
[band_nr
] == i
;
4388 band
= construct_band(schedule
, parent
, band_nr
, active
, n
);
4390 list
= isl_band_list_add(list
, band
);
4392 for (i
= 0; i
< schedule
->n
; ++i
) {
4394 if (!parent_active
[i
])
4396 if (schedule
->node
[i
].n_band
> band_nr
)
4398 for (j
= 0; j
< schedule
->n
; ++j
)
4400 band
= construct_band(schedule
, parent
, band_nr
, active
, 1);
4401 list
= isl_band_list_add(list
, band
);
4406 list
= sort_band_list(list
, parent
);
4411 /* Construct a band forest representation of the schedule and
4412 * return the list of roots.
4414 static __isl_give isl_band_list
*construct_forest(
4415 __isl_keep isl_schedule
*schedule
)
4418 isl_ctx
*ctx
= isl_schedule_get_ctx(schedule
);
4419 isl_band_list
*forest
;
4422 active
= isl_alloc_array(ctx
, int, schedule
->n
);
4423 if (schedule
->n
&& !active
)
4426 for (i
= 0; i
< schedule
->n
; ++i
)
4429 forest
= construct_band_list(schedule
, NULL
, 0, active
, schedule
->n
);
4436 /* Return the roots of a band forest representation of the schedule.
4438 __isl_give isl_band_list
*isl_schedule_get_band_forest(
4439 __isl_keep isl_schedule
*schedule
)
4443 if (!schedule
->band_forest
)
4444 schedule
->band_forest
= construct_forest(schedule
);
4445 return isl_band_list_dup(schedule
->band_forest
);
4448 /* Call "fn" on each band in the schedule in depth-first post-order.
4450 int isl_schedule_foreach_band(__isl_keep isl_schedule
*sched
,
4451 int (*fn
)(__isl_keep isl_band
*band
, void *user
), void *user
)
4454 isl_band_list
*forest
;
4459 forest
= isl_schedule_get_band_forest(sched
);
4460 r
= isl_band_list_foreach_band(forest
, fn
, user
);
4461 isl_band_list_free(forest
);
4466 static __isl_give isl_printer
*print_band_list(__isl_take isl_printer
*p
,
4467 __isl_keep isl_band_list
*list
);
4469 static __isl_give isl_printer
*print_band(__isl_take isl_printer
*p
,
4470 __isl_keep isl_band
*band
)
4472 isl_band_list
*children
;
4474 p
= isl_printer_start_line(p
);
4475 p
= isl_printer_print_union_pw_multi_aff(p
, band
->pma
);
4476 p
= isl_printer_end_line(p
);
4478 if (!isl_band_has_children(band
))
4481 children
= isl_band_get_children(band
);
4483 p
= isl_printer_indent(p
, 4);
4484 p
= print_band_list(p
, children
);
4485 p
= isl_printer_indent(p
, -4);
4487 isl_band_list_free(children
);
4492 static __isl_give isl_printer
*print_band_list(__isl_take isl_printer
*p
,
4493 __isl_keep isl_band_list
*list
)
4497 n
= isl_band_list_n_band(list
);
4498 for (i
= 0; i
< n
; ++i
) {
4500 band
= isl_band_list_get_band(list
, i
);
4501 p
= print_band(p
, band
);
4502 isl_band_free(band
);
4508 __isl_give isl_printer
*isl_printer_print_schedule(__isl_take isl_printer
*p
,
4509 __isl_keep isl_schedule
*schedule
)
4511 isl_band_list
*forest
;
4513 forest
= isl_schedule_get_band_forest(schedule
);
4515 p
= print_band_list(p
, forest
);
4517 isl_band_list_free(forest
);
4522 void isl_schedule_dump(__isl_keep isl_schedule
*schedule
)
4524 isl_printer
*printer
;
4529 printer
= isl_printer_to_file(isl_schedule_get_ctx(schedule
), stderr
);
4530 printer
= isl_printer_print_schedule(printer
, schedule
);
4532 isl_printer_free(printer
);