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 /* Construct an isl_schedule_constraints object for computing a schedule
40 * on "domain". The initial object does not impose any constraints.
42 __isl_give isl_schedule_constraints
*isl_schedule_constraints_on_domain(
43 __isl_take isl_union_set
*domain
)
47 isl_schedule_constraints
*sc
;
54 ctx
= isl_union_set_get_ctx(domain
);
55 sc
= isl_calloc_type(ctx
, struct isl_schedule_constraints
);
57 return isl_union_set_free(domain
);
59 space
= isl_union_set_get_space(domain
);
61 empty
= isl_union_map_empty(space
);
62 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
63 sc
->constraint
[i
] = isl_union_map_copy(empty
);
64 if (!sc
->constraint
[i
])
65 sc
->domain
= isl_union_set_free(sc
->domain
);
67 isl_union_map_free(empty
);
70 return isl_schedule_constraints_free(sc
);
75 /* Replace the validity constraints of "sc" by "validity".
77 __isl_give isl_schedule_constraints
*isl_schedule_constraints_set_validity(
78 __isl_take isl_schedule_constraints
*sc
,
79 __isl_take isl_union_map
*validity
)
84 isl_union_map_free(sc
->constraint
[isl_edge_validity
]);
85 sc
->constraint
[isl_edge_validity
] = validity
;
89 isl_schedule_constraints_free(sc
);
90 isl_union_map_free(validity
);
94 /* Replace the proximity constraints of "sc" by "proximity".
96 __isl_give isl_schedule_constraints
*isl_schedule_constraints_set_proximity(
97 __isl_take isl_schedule_constraints
*sc
,
98 __isl_take isl_union_map
*proximity
)
100 if (!sc
|| !proximity
)
103 isl_union_map_free(sc
->constraint
[isl_edge_proximity
]);
104 sc
->constraint
[isl_edge_proximity
] = proximity
;
108 isl_schedule_constraints_free(sc
);
109 isl_union_map_free(proximity
);
113 void *isl_schedule_constraints_free(__isl_take isl_schedule_constraints
*sc
)
115 enum isl_edge_type i
;
120 isl_union_set_free(sc
->domain
);
121 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
122 isl_union_map_free(sc
->constraint
[i
]);
129 isl_ctx
*isl_schedule_constraints_get_ctx(
130 __isl_keep isl_schedule_constraints
*sc
)
132 return sc
? isl_union_set_get_ctx(sc
->domain
) : NULL
;
135 void isl_schedule_constraints_dump(__isl_keep isl_schedule_constraints
*sc
)
140 fprintf(stderr
, "domain: ");
141 isl_union_set_dump(sc
->domain
);
142 fprintf(stderr
, "validity: ");
143 isl_union_map_dump(sc
->constraint
[isl_edge_validity
]);
144 fprintf(stderr
, "proximity: ");
145 isl_union_map_dump(sc
->constraint
[isl_edge_proximity
]);
148 /* Align the parameters of the fields of "sc".
150 static __isl_give isl_schedule_constraints
*
151 isl_schedule_constraints_align_params(__isl_take isl_schedule_constraints
*sc
)
154 enum isl_edge_type i
;
159 space
= isl_union_set_get_space(sc
->domain
);
160 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
161 space
= isl_space_align_params(space
,
162 isl_union_map_get_space(sc
->constraint
[i
]));
164 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
165 sc
->constraint
[i
] = isl_union_map_align_params(
166 sc
->constraint
[i
], isl_space_copy(space
));
167 if (!sc
->constraint
[i
])
168 space
= isl_space_free(space
);
170 sc
->domain
= isl_union_set_align_params(sc
->domain
, space
);
172 return isl_schedule_constraints_free(sc
);
177 /* Return the total number of isl_maps in the constraints of "sc".
179 static __isl_give
int isl_schedule_constraints_n_map(
180 __isl_keep isl_schedule_constraints
*sc
)
182 enum isl_edge_type i
;
185 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
186 n
+= isl_union_map_n_map(sc
->constraint
[i
]);
191 /* Internal information about a node that is used during the construction
193 * dim represents the space in which the domain lives
194 * sched is a matrix representation of the schedule being constructed
196 * sched_map is an isl_map representation of the same (partial) schedule
197 * sched_map may be NULL
198 * rank is the number of linearly independent rows in the linear part
200 * the columns of cmap represent a change of basis for the schedule
201 * coefficients; the first rank columns span the linear part of
203 * cinv is the inverse of cmap.
204 * start is the first variable in the LP problem in the sequences that
205 * represents the schedule coefficients of this node
206 * nvar is the dimension of the domain
207 * nparam is the number of parameters or 0 if we are not constructing
208 * a parametric schedule
210 * scc is the index of SCC (or WCC) this node belongs to
212 * band contains the band index for each of the rows of the schedule.
213 * band_id is used to differentiate between separate bands at the same
214 * level within the same parent band, i.e., bands that are separated
215 * by the parent band or bands that are independent of each other.
216 * zero contains a boolean for each of the rows of the schedule,
217 * indicating whether the corresponding scheduling dimension results
218 * in zero dependence distances within its band and with respect
219 * to the proximity edges.
221 struct isl_sched_node
{
239 static int node_has_dim(const void *entry
, const void *val
)
241 struct isl_sched_node
*node
= (struct isl_sched_node
*)entry
;
242 isl_space
*dim
= (isl_space
*)val
;
244 return isl_space_is_equal(node
->dim
, dim
);
247 /* An edge in the dependence graph. An edge may be used to
248 * ensure validity of the generated schedule, to minimize the dependence
251 * map is the dependence relation
252 * src is the source node
253 * dst is the sink node
254 * validity is set if the edge is used to ensure correctness
255 * proximity is set if the edge is used to minimize dependence distances
257 * For validity edges, start and end mark the sequence of inequality
258 * constraints in the LP problem that encode the validity constraint
259 * corresponding to this edge.
261 struct isl_sched_edge
{
264 struct isl_sched_node
*src
;
265 struct isl_sched_node
*dst
;
274 /* Internal information about the dependence graph used during
275 * the construction of the schedule.
277 * intra_hmap is a cache, mapping dependence relations to their dual,
278 * for dependences from a node to itself
279 * inter_hmap is a cache, mapping dependence relations to their dual,
280 * for dependences between distinct nodes
282 * n is the number of nodes
283 * node is the list of nodes
284 * maxvar is the maximal number of variables over all nodes
285 * max_row is the allocated number of rows in the schedule
286 * n_row is the current (maximal) number of linearly independent
287 * rows in the node schedules
288 * n_total_row is the current number of rows in the node schedules
289 * n_band is the current number of completed bands
290 * band_start is the starting row in the node schedules of the current band
291 * root is set if this graph is the original dependence graph,
292 * without any splitting
294 * sorted contains a list of node indices sorted according to the
295 * SCC to which a node belongs
297 * n_edge is the number of edges
298 * edge is the list of edges
299 * max_edge contains the maximal number of edges of each type;
300 * in particular, it contains the number of edges in the inital graph.
301 * edge_table contains pointers into the edge array, hashed on the source
302 * and sink spaces; there is one such table for each type;
303 * a given edge may be referenced from more than one table
304 * if the corresponding relation appears in more than of the
305 * sets of dependences
307 * node_table contains pointers into the node array, hashed on the space
309 * region contains a list of variable sequences that should be non-trivial
311 * lp contains the (I)LP problem used to obtain new schedule rows
313 * src_scc and dst_scc are the source and sink SCCs of an edge with
314 * conflicting constraints
316 * scc represents the number of components
318 struct isl_sched_graph
{
319 isl_map_to_basic_set
*intra_hmap
;
320 isl_map_to_basic_set
*inter_hmap
;
322 struct isl_sched_node
*node
;
336 struct isl_sched_edge
*edge
;
338 int max_edge
[isl_edge_last
+ 1];
339 struct isl_hash_table
*edge_table
[isl_edge_last
+ 1];
341 struct isl_hash_table
*node_table
;
342 struct isl_region
*region
;
352 /* Initialize node_table based on the list of nodes.
354 static int graph_init_table(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
358 graph
->node_table
= isl_hash_table_alloc(ctx
, graph
->n
);
359 if (!graph
->node_table
)
362 for (i
= 0; i
< graph
->n
; ++i
) {
363 struct isl_hash_table_entry
*entry
;
366 hash
= isl_space_get_hash(graph
->node
[i
].dim
);
367 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
369 graph
->node
[i
].dim
, 1);
372 entry
->data
= &graph
->node
[i
];
378 /* Return a pointer to the node that lives within the given space,
379 * or NULL if there is no such node.
381 static struct isl_sched_node
*graph_find_node(isl_ctx
*ctx
,
382 struct isl_sched_graph
*graph
, __isl_keep isl_space
*dim
)
384 struct isl_hash_table_entry
*entry
;
387 hash
= isl_space_get_hash(dim
);
388 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
389 &node_has_dim
, dim
, 0);
391 return entry
? entry
->data
: NULL
;
394 static int edge_has_src_and_dst(const void *entry
, const void *val
)
396 const struct isl_sched_edge
*edge
= entry
;
397 const struct isl_sched_edge
*temp
= val
;
399 return edge
->src
== temp
->src
&& edge
->dst
== temp
->dst
;
402 /* Add the given edge to graph->edge_table[type].
404 static int graph_edge_table_add(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
405 enum isl_edge_type type
, struct isl_sched_edge
*edge
)
407 struct isl_hash_table_entry
*entry
;
410 hash
= isl_hash_init();
411 hash
= isl_hash_builtin(hash
, edge
->src
);
412 hash
= isl_hash_builtin(hash
, edge
->dst
);
413 entry
= isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
414 &edge_has_src_and_dst
, edge
, 1);
422 /* Allocate the edge_tables based on the maximal number of edges of
425 static int graph_init_edge_tables(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
429 for (i
= 0; i
<= isl_edge_last
; ++i
) {
430 graph
->edge_table
[i
] = isl_hash_table_alloc(ctx
,
432 if (!graph
->edge_table
[i
])
439 /* If graph->edge_table[type] contains an edge from the given source
440 * to the given destination, then return the hash table entry of this edge.
441 * Otherwise, return NULL.
443 static struct isl_hash_table_entry
*graph_find_edge_entry(
444 struct isl_sched_graph
*graph
,
445 enum isl_edge_type type
,
446 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
448 isl_ctx
*ctx
= isl_space_get_ctx(src
->dim
);
450 struct isl_sched_edge temp
= { .src
= src
, .dst
= dst
};
452 hash
= isl_hash_init();
453 hash
= isl_hash_builtin(hash
, temp
.src
);
454 hash
= isl_hash_builtin(hash
, temp
.dst
);
455 return isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
456 &edge_has_src_and_dst
, &temp
, 0);
460 /* If graph->edge_table[type] contains an edge from the given source
461 * to the given destination, then return this edge.
462 * Otherwise, return NULL.
464 static struct isl_sched_edge
*graph_find_edge(struct isl_sched_graph
*graph
,
465 enum isl_edge_type type
,
466 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
468 struct isl_hash_table_entry
*entry
;
470 entry
= graph_find_edge_entry(graph
, type
, src
, dst
);
477 /* Check whether the dependence graph has an edge of the given type
478 * between the given two nodes.
480 static int graph_has_edge(struct isl_sched_graph
*graph
,
481 enum isl_edge_type type
,
482 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
484 struct isl_sched_edge
*edge
;
487 edge
= graph_find_edge(graph
, type
, src
, dst
);
491 empty
= isl_map_plain_is_empty(edge
->map
);
498 /* If there is an edge from the given source to the given destination
499 * of any type then return this edge.
500 * Otherwise, return NULL.
502 static struct isl_sched_edge
*graph_find_any_edge(struct isl_sched_graph
*graph
,
503 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
505 enum isl_edge_type i
;
506 struct isl_sched_edge
*edge
;
508 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
509 edge
= graph_find_edge(graph
, i
, src
, dst
);
517 /* Remove the given edge from all the edge_tables that refer to it.
519 static void graph_remove_edge(struct isl_sched_graph
*graph
,
520 struct isl_sched_edge
*edge
)
522 isl_ctx
*ctx
= isl_map_get_ctx(edge
->map
);
523 enum isl_edge_type i
;
525 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
526 struct isl_hash_table_entry
*entry
;
528 entry
= graph_find_edge_entry(graph
, i
, edge
->src
, edge
->dst
);
531 if (entry
->data
!= edge
)
533 isl_hash_table_remove(ctx
, graph
->edge_table
[i
], entry
);
537 /* Check whether the dependence graph has any edge
538 * between the given two nodes.
540 static int graph_has_any_edge(struct isl_sched_graph
*graph
,
541 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
543 enum isl_edge_type i
;
546 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
547 r
= graph_has_edge(graph
, i
, src
, dst
);
555 /* Check whether the dependence graph has a validity edge
556 * between the given two nodes.
558 static int graph_has_validity_edge(struct isl_sched_graph
*graph
,
559 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
561 return graph_has_edge(graph
, isl_edge_validity
, src
, dst
);
564 static int graph_alloc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
565 int n_node
, int n_edge
)
570 graph
->n_edge
= n_edge
;
571 graph
->node
= isl_calloc_array(ctx
, struct isl_sched_node
, graph
->n
);
572 graph
->sorted
= isl_calloc_array(ctx
, int, graph
->n
);
573 graph
->region
= isl_alloc_array(ctx
, struct isl_region
, graph
->n
);
574 graph
->edge
= isl_calloc_array(ctx
,
575 struct isl_sched_edge
, graph
->n_edge
);
577 graph
->intra_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
578 graph
->inter_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
580 if (!graph
->node
|| !graph
->region
|| (graph
->n_edge
&& !graph
->edge
) ||
584 for(i
= 0; i
< graph
->n
; ++i
)
585 graph
->sorted
[i
] = i
;
590 static void graph_free(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
594 isl_map_to_basic_set_free(graph
->intra_hmap
);
595 isl_map_to_basic_set_free(graph
->inter_hmap
);
597 for (i
= 0; i
< graph
->n
; ++i
) {
598 isl_space_free(graph
->node
[i
].dim
);
599 isl_mat_free(graph
->node
[i
].sched
);
600 isl_map_free(graph
->node
[i
].sched_map
);
601 isl_mat_free(graph
->node
[i
].cmap
);
602 isl_mat_free(graph
->node
[i
].cinv
);
604 free(graph
->node
[i
].band
);
605 free(graph
->node
[i
].band_id
);
606 free(graph
->node
[i
].zero
);
611 for (i
= 0; i
< graph
->n_edge
; ++i
)
612 isl_map_free(graph
->edge
[i
].map
);
615 for (i
= 0; i
<= isl_edge_last
; ++i
)
616 isl_hash_table_free(ctx
, graph
->edge_table
[i
]);
617 isl_hash_table_free(ctx
, graph
->node_table
);
618 isl_basic_set_free(graph
->lp
);
621 /* For each "set" on which this function is called, increment
622 * graph->n by one and update graph->maxvar.
624 static int init_n_maxvar(__isl_take isl_set
*set
, void *user
)
626 struct isl_sched_graph
*graph
= user
;
627 int nvar
= isl_set_dim(set
, isl_dim_set
);
630 if (nvar
> graph
->maxvar
)
631 graph
->maxvar
= nvar
;
638 /* Compute the number of rows that should be allocated for the schedule.
639 * The graph can be split at most "n - 1" times, there can be at most
640 * two rows for each dimension in the iteration domains (in particular,
641 * we usually have one row, but it may be split by split_scaled),
642 * and there can be one extra row for ordering the statements.
643 * Note that if we have actually split "n - 1" times, then no ordering
644 * is needed, so in principle we could use "graph->n + 2 * graph->maxvar - 1".
646 static int compute_max_row(struct isl_sched_graph
*graph
,
647 __isl_keep isl_union_set
*domain
)
651 if (isl_union_set_foreach_set(domain
, &init_n_maxvar
, graph
) < 0)
653 graph
->max_row
= graph
->n
+ 2 * graph
->maxvar
;
658 /* Add a new node to the graph representing the given set.
660 static int extract_node(__isl_take isl_set
*set
, void *user
)
666 struct isl_sched_graph
*graph
= user
;
667 int *band
, *band_id
, *zero
;
669 ctx
= isl_set_get_ctx(set
);
670 dim
= isl_set_get_space(set
);
672 nvar
= isl_space_dim(dim
, isl_dim_set
);
673 nparam
= isl_space_dim(dim
, isl_dim_param
);
674 if (!ctx
->opt
->schedule_parametric
)
676 sched
= isl_mat_alloc(ctx
, 0, 1 + nparam
+ nvar
);
677 graph
->node
[graph
->n
].dim
= dim
;
678 graph
->node
[graph
->n
].nvar
= nvar
;
679 graph
->node
[graph
->n
].nparam
= nparam
;
680 graph
->node
[graph
->n
].sched
= sched
;
681 graph
->node
[graph
->n
].sched_map
= NULL
;
682 band
= isl_alloc_array(ctx
, int, graph
->max_row
);
683 graph
->node
[graph
->n
].band
= band
;
684 band_id
= isl_calloc_array(ctx
, int, graph
->max_row
);
685 graph
->node
[graph
->n
].band_id
= band_id
;
686 zero
= isl_calloc_array(ctx
, int, graph
->max_row
);
687 graph
->node
[graph
->n
].zero
= zero
;
690 if (!sched
|| (graph
->max_row
&& (!band
|| !band_id
|| !zero
)))
696 struct isl_extract_edge_data
{
697 enum isl_edge_type type
;
698 struct isl_sched_graph
*graph
;
701 /* Merge edge2 into edge1, freeing the contents of edge2.
702 * "type" is the type of the schedule constraint from which edge2 was
704 * Return 0 on success and -1 on failure.
706 * edge1 and edge2 are assumed to have the same value for the map field.
708 static int merge_edge(enum isl_edge_type type
, struct isl_sched_edge
*edge1
,
709 struct isl_sched_edge
*edge2
)
711 edge1
->validity
|= edge2
->validity
;
712 edge1
->proximity
|= edge2
->proximity
;
713 isl_map_free(edge2
->map
);
718 /* Add a new edge to the graph based on the given map
719 * and add it to data->graph->edge_table[data->type].
720 * If a dependence relation of a given type happens to be identical
721 * to one of the dependence relations of a type that was added before,
722 * then we don't create a new edge, but instead mark the original edge
723 * as also representing a dependence of the current type.
725 static int extract_edge(__isl_take isl_map
*map
, void *user
)
727 isl_ctx
*ctx
= isl_map_get_ctx(map
);
728 struct isl_extract_edge_data
*data
= user
;
729 struct isl_sched_graph
*graph
= data
->graph
;
730 struct isl_sched_node
*src
, *dst
;
732 struct isl_sched_edge
*edge
;
735 dim
= isl_space_domain(isl_map_get_space(map
));
736 src
= graph_find_node(ctx
, graph
, dim
);
738 dim
= isl_space_range(isl_map_get_space(map
));
739 dst
= graph_find_node(ctx
, graph
, dim
);
747 graph
->edge
[graph
->n_edge
].src
= src
;
748 graph
->edge
[graph
->n_edge
].dst
= dst
;
749 graph
->edge
[graph
->n_edge
].map
= map
;
750 if (data
->type
== isl_edge_validity
) {
751 graph
->edge
[graph
->n_edge
].validity
= 1;
752 graph
->edge
[graph
->n_edge
].proximity
= 0;
754 if (data
->type
== isl_edge_proximity
) {
755 graph
->edge
[graph
->n_edge
].validity
= 0;
756 graph
->edge
[graph
->n_edge
].proximity
= 1;
760 edge
= graph_find_any_edge(graph
, src
, dst
);
762 return graph_edge_table_add(ctx
, graph
, data
->type
,
763 &graph
->edge
[graph
->n_edge
- 1]);
764 is_equal
= isl_map_plain_is_equal(map
, edge
->map
);
768 return graph_edge_table_add(ctx
, graph
, data
->type
,
769 &graph
->edge
[graph
->n_edge
- 1]);
772 if (merge_edge(data
->type
, edge
, &graph
->edge
[graph
->n_edge
]) < 0)
775 return graph_edge_table_add(ctx
, graph
, data
->type
, edge
);
778 /* Check whether there is any dependence from node[j] to node[i]
779 * or from node[i] to node[j].
781 static int node_follows_weak(int i
, int j
, void *user
)
784 struct isl_sched_graph
*graph
= user
;
786 f
= graph_has_any_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
789 return graph_has_any_edge(graph
, &graph
->node
[i
], &graph
->node
[j
]);
792 /* Check whether there is a validity dependence from node[j] to node[i],
793 * forcing node[i] to follow node[j].
795 static int node_follows_strong(int i
, int j
, void *user
)
797 struct isl_sched_graph
*graph
= user
;
799 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
802 /* Use Tarjan's algorithm for computing the strongly connected components
803 * in the dependence graph (only validity edges).
804 * If weak is set, we consider the graph to be undirected and
805 * we effectively compute the (weakly) connected components.
806 * Additionally, we also consider other edges when weak is set.
808 static int detect_ccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
, int weak
)
811 struct isl_tarjan_graph
*g
= NULL
;
813 g
= isl_tarjan_graph_init(ctx
, graph
->n
,
814 weak
? &node_follows_weak
: &node_follows_strong
, graph
);
822 while (g
->order
[i
] != -1) {
823 graph
->node
[g
->order
[i
]].scc
= graph
->scc
;
831 isl_tarjan_graph_free(g
);
836 /* Apply Tarjan's algorithm to detect the strongly connected components
837 * in the dependence graph.
839 static int detect_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
841 return detect_ccs(ctx
, graph
, 0);
844 /* Apply Tarjan's algorithm to detect the (weakly) connected components
845 * in the dependence graph.
847 static int detect_wccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
849 return detect_ccs(ctx
, graph
, 1);
852 static int cmp_scc(const void *a
, const void *b
, void *data
)
854 struct isl_sched_graph
*graph
= data
;
858 return graph
->node
[*i1
].scc
- graph
->node
[*i2
].scc
;
861 /* Sort the elements of graph->sorted according to the corresponding SCCs.
863 static int sort_sccs(struct isl_sched_graph
*graph
)
865 return isl_sort(graph
->sorted
, graph
->n
, sizeof(int), &cmp_scc
, graph
);
868 /* Given a dependence relation R from a node to itself,
869 * construct the set of coefficients of valid constraints for elements
870 * in that dependence relation.
871 * In particular, the result contains tuples of coefficients
872 * c_0, c_n, c_x such that
874 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
878 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
880 * We choose here to compute the dual of delta R.
881 * Alternatively, we could have computed the dual of R, resulting
882 * in a set of tuples c_0, c_n, c_x, c_y, and then
883 * plugged in (c_0, c_n, c_x, -c_x).
885 static __isl_give isl_basic_set
*intra_coefficients(
886 struct isl_sched_graph
*graph
, __isl_take isl_map
*map
)
891 if (isl_map_to_basic_set_has(graph
->intra_hmap
, map
))
892 return isl_map_to_basic_set_get(graph
->intra_hmap
, map
);
894 delta
= isl_set_remove_divs(isl_map_deltas(isl_map_copy(map
)));
895 coef
= isl_set_coefficients(delta
);
896 graph
->intra_hmap
= isl_map_to_basic_set_set(graph
->intra_hmap
, map
,
897 isl_basic_set_copy(coef
));
902 /* Given a dependence relation R, * construct the set of coefficients
903 * of valid constraints for elements in that dependence relation.
904 * In particular, the result contains tuples of coefficients
905 * c_0, c_n, c_x, c_y such that
907 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
910 static __isl_give isl_basic_set
*inter_coefficients(
911 struct isl_sched_graph
*graph
, __isl_take isl_map
*map
)
916 if (isl_map_to_basic_set_has(graph
->inter_hmap
, map
))
917 return isl_map_to_basic_set_get(graph
->inter_hmap
, map
);
919 set
= isl_map_wrap(isl_map_remove_divs(isl_map_copy(map
)));
920 coef
= isl_set_coefficients(set
);
921 graph
->inter_hmap
= isl_map_to_basic_set_set(graph
->inter_hmap
, map
,
922 isl_basic_set_copy(coef
));
927 /* Add constraints to graph->lp that force validity for the given
928 * dependence from a node i to itself.
929 * That is, add constraints that enforce
931 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
932 * = c_i_x (y - x) >= 0
934 * for each (x,y) in R.
935 * We obtain general constraints on coefficients (c_0, c_n, c_x)
936 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
937 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
938 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
940 * Actually, we do not construct constraints for the c_i_x themselves,
941 * but for the coefficients of c_i_x written as a linear combination
942 * of the columns in node->cmap.
944 static int add_intra_validity_constraints(struct isl_sched_graph
*graph
,
945 struct isl_sched_edge
*edge
)
948 isl_map
*map
= isl_map_copy(edge
->map
);
949 isl_ctx
*ctx
= isl_map_get_ctx(map
);
951 isl_dim_map
*dim_map
;
953 struct isl_sched_node
*node
= edge
->src
;
955 coef
= intra_coefficients(graph
, map
);
957 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
959 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
960 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(node
->cmap
));
964 total
= isl_basic_set_total_dim(graph
->lp
);
965 dim_map
= isl_dim_map_alloc(ctx
, total
);
966 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
967 isl_space_dim(dim
, isl_dim_set
), 1,
969 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
970 isl_space_dim(dim
, isl_dim_set
), 1,
972 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
973 coef
->n_eq
, coef
->n_ineq
);
974 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
984 /* Add constraints to graph->lp that force validity for the given
985 * dependence from node i to node j.
986 * That is, add constraints that enforce
988 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
990 * for each (x,y) in R.
991 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
992 * of valid constraints for R and then plug in
993 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
994 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
995 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
996 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
998 * Actually, we do not construct constraints for the c_*_x themselves,
999 * but for the coefficients of c_*_x written as a linear combination
1000 * of the columns in node->cmap.
1002 static int add_inter_validity_constraints(struct isl_sched_graph
*graph
,
1003 struct isl_sched_edge
*edge
)
1006 isl_map
*map
= isl_map_copy(edge
->map
);
1007 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1009 isl_dim_map
*dim_map
;
1010 isl_basic_set
*coef
;
1011 struct isl_sched_node
*src
= edge
->src
;
1012 struct isl_sched_node
*dst
= edge
->dst
;
1014 coef
= inter_coefficients(graph
, map
);
1016 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1018 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1019 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(src
->cmap
));
1020 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1021 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
,
1022 isl_mat_copy(dst
->cmap
));
1026 total
= isl_basic_set_total_dim(graph
->lp
);
1027 dim_map
= isl_dim_map_alloc(ctx
, total
);
1029 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, 1);
1030 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, -1);
1031 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, 1);
1032 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
1033 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1035 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
1036 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1039 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, -1);
1040 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, 1);
1041 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, -1);
1042 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
1043 isl_space_dim(dim
, isl_dim_set
), 1,
1045 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
1046 isl_space_dim(dim
, isl_dim_set
), 1,
1049 edge
->start
= graph
->lp
->n_ineq
;
1050 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1051 coef
->n_eq
, coef
->n_ineq
);
1052 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1056 isl_space_free(dim
);
1057 edge
->end
= graph
->lp
->n_ineq
;
1061 isl_space_free(dim
);
1065 /* Add constraints to graph->lp that bound the dependence distance for the given
1066 * dependence from a node i to itself.
1067 * If s = 1, we add the constraint
1069 * c_i_x (y - x) <= m_0 + m_n n
1073 * -c_i_x (y - x) + m_0 + m_n n >= 0
1075 * for each (x,y) in R.
1076 * If s = -1, we add the constraint
1078 * -c_i_x (y - x) <= m_0 + m_n n
1082 * c_i_x (y - x) + m_0 + m_n n >= 0
1084 * for each (x,y) in R.
1085 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1086 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1087 * with each coefficient (except m_0) represented as a pair of non-negative
1090 * Actually, we do not construct constraints for the c_i_x themselves,
1091 * but for the coefficients of c_i_x written as a linear combination
1092 * of the columns in node->cmap.
1094 static int add_intra_proximity_constraints(struct isl_sched_graph
*graph
,
1095 struct isl_sched_edge
*edge
, int s
)
1099 isl_map
*map
= isl_map_copy(edge
->map
);
1100 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1102 isl_dim_map
*dim_map
;
1103 isl_basic_set
*coef
;
1104 struct isl_sched_node
*node
= edge
->src
;
1106 coef
= intra_coefficients(graph
, map
);
1108 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1110 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1111 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(node
->cmap
));
1115 nparam
= isl_space_dim(node
->dim
, isl_dim_param
);
1116 total
= isl_basic_set_total_dim(graph
->lp
);
1117 dim_map
= isl_dim_map_alloc(ctx
, total
);
1118 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1119 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1120 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1121 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
1122 isl_space_dim(dim
, isl_dim_set
), 1,
1124 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
1125 isl_space_dim(dim
, isl_dim_set
), 1,
1127 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1128 coef
->n_eq
, coef
->n_ineq
);
1129 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1131 isl_space_free(dim
);
1135 isl_space_free(dim
);
1139 /* Add constraints to graph->lp that bound the dependence distance for the given
1140 * dependence from node i to node j.
1141 * If s = 1, we add the constraint
1143 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1148 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1151 * for each (x,y) in R.
1152 * If s = -1, we add the constraint
1154 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1159 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1162 * for each (x,y) in R.
1163 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1164 * of valid constraints for R and then plug in
1165 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1167 * with each coefficient (except m_0, c_j_0 and c_i_0)
1168 * represented as a pair of non-negative coefficients.
1170 * Actually, we do not construct constraints for the c_*_x themselves,
1171 * but for the coefficients of c_*_x written as a linear combination
1172 * of the columns in node->cmap.
1174 static int add_inter_proximity_constraints(struct isl_sched_graph
*graph
,
1175 struct isl_sched_edge
*edge
, int s
)
1179 isl_map
*map
= isl_map_copy(edge
->map
);
1180 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1182 isl_dim_map
*dim_map
;
1183 isl_basic_set
*coef
;
1184 struct isl_sched_node
*src
= edge
->src
;
1185 struct isl_sched_node
*dst
= edge
->dst
;
1187 coef
= inter_coefficients(graph
, map
);
1189 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1191 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1192 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(src
->cmap
));
1193 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1194 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
,
1195 isl_mat_copy(dst
->cmap
));
1199 nparam
= isl_space_dim(src
->dim
, isl_dim_param
);
1200 total
= isl_basic_set_total_dim(graph
->lp
);
1201 dim_map
= isl_dim_map_alloc(ctx
, total
);
1203 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1204 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1205 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1207 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, -s
);
1208 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, s
);
1209 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, -s
);
1210 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
1211 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1213 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
1214 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1217 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, s
);
1218 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, -s
);
1219 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, s
);
1220 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
1221 isl_space_dim(dim
, isl_dim_set
), 1,
1223 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
1224 isl_space_dim(dim
, isl_dim_set
), 1,
1227 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1228 coef
->n_eq
, coef
->n_ineq
);
1229 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1231 isl_space_free(dim
);
1235 isl_space_free(dim
);
1239 static int add_all_validity_constraints(struct isl_sched_graph
*graph
)
1243 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1244 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1245 if (!edge
->validity
)
1247 if (edge
->src
!= edge
->dst
)
1249 if (add_intra_validity_constraints(graph
, edge
) < 0)
1253 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1254 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1255 if (!edge
->validity
)
1257 if (edge
->src
== edge
->dst
)
1259 if (add_inter_validity_constraints(graph
, edge
) < 0)
1266 /* Add constraints to graph->lp that bound the dependence distance
1267 * for all dependence relations.
1268 * If a given proximity dependence is identical to a validity
1269 * dependence, then the dependence distance is already bounded
1270 * from below (by zero), so we only need to bound the distance
1272 * Otherwise, we need to bound the distance both from above and from below.
1274 static int add_all_proximity_constraints(struct isl_sched_graph
*graph
)
1278 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1279 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1280 if (!edge
->proximity
)
1282 if (edge
->src
== edge
->dst
&&
1283 add_intra_proximity_constraints(graph
, edge
, 1) < 0)
1285 if (edge
->src
!= edge
->dst
&&
1286 add_inter_proximity_constraints(graph
, edge
, 1) < 0)
1290 if (edge
->src
== edge
->dst
&&
1291 add_intra_proximity_constraints(graph
, edge
, -1) < 0)
1293 if (edge
->src
!= edge
->dst
&&
1294 add_inter_proximity_constraints(graph
, edge
, -1) < 0)
1301 /* Compute a basis for the rows in the linear part of the schedule
1302 * and extend this basis to a full basis. The remaining rows
1303 * can then be used to force linear independence from the rows
1306 * In particular, given the schedule rows S, we compute
1311 * with H the Hermite normal form of S. That is, all but the
1312 * first rank columns of H are zero and so each row in S is
1313 * a linear combination of the first rank rows of Q.
1314 * The matrix Q is then transposed because we will write the
1315 * coefficients of the next schedule row as a column vector s
1316 * and express this s as a linear combination s = Q c of the
1318 * Similarly, the matrix U is transposed such that we can
1319 * compute the coefficients c = U s from a schedule row s.
1321 static int node_update_cmap(struct isl_sched_node
*node
)
1324 int n_row
= isl_mat_rows(node
->sched
);
1326 H
= isl_mat_sub_alloc(node
->sched
, 0, n_row
,
1327 1 + node
->nparam
, node
->nvar
);
1329 H
= isl_mat_left_hermite(H
, 0, &U
, &Q
);
1330 isl_mat_free(node
->cmap
);
1331 isl_mat_free(node
->cinv
);
1332 node
->cmap
= isl_mat_transpose(Q
);
1333 node
->cinv
= isl_mat_transpose(U
);
1334 node
->rank
= isl_mat_initial_non_zero_cols(H
);
1337 if (!node
->cmap
|| !node
->cinv
|| node
->rank
< 0)
1342 /* Count the number of equality and inequality constraints
1343 * that will be added for the given map.
1344 * If carry is set, then we are counting the number of (validity)
1345 * constraints that will be added in setup_carry_lp and we count
1346 * each edge exactly once. Otherwise, we count as follows
1347 * validity -> 1 (>= 0)
1348 * validity+proximity -> 2 (>= 0 and upper bound)
1349 * proximity -> 2 (lower and upper bound)
1351 static int count_map_constraints(struct isl_sched_graph
*graph
,
1352 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
,
1353 int *n_eq
, int *n_ineq
, int carry
)
1355 isl_basic_set
*coef
;
1356 int f
= carry
? 1 : edge
->proximity
? 2 : 1;
1358 if (carry
&& !edge
->validity
) {
1363 if (edge
->src
== edge
->dst
)
1364 coef
= intra_coefficients(graph
, map
);
1366 coef
= inter_coefficients(graph
, map
);
1369 *n_eq
+= f
* coef
->n_eq
;
1370 *n_ineq
+= f
* coef
->n_ineq
;
1371 isl_basic_set_free(coef
);
1376 /* Count the number of equality and inequality constraints
1377 * that will be added to the main lp problem.
1378 * We count as follows
1379 * validity -> 1 (>= 0)
1380 * validity+proximity -> 2 (>= 0 and upper bound)
1381 * proximity -> 2 (lower and upper bound)
1383 static int count_constraints(struct isl_sched_graph
*graph
,
1384 int *n_eq
, int *n_ineq
)
1388 *n_eq
= *n_ineq
= 0;
1389 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1390 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1391 isl_map
*map
= isl_map_copy(edge
->map
);
1393 if (count_map_constraints(graph
, edge
, map
,
1394 n_eq
, n_ineq
, 0) < 0)
1401 /* Count the number of constraints that will be added by
1402 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
1405 * In practice, add_bound_coefficient_constraints only adds inequalities.
1407 static int count_bound_coefficient_constraints(isl_ctx
*ctx
,
1408 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
1412 if (ctx
->opt
->schedule_max_coefficient
== -1)
1415 for (i
= 0; i
< graph
->n
; ++i
)
1416 *n_ineq
+= 2 * graph
->node
[i
].nparam
+ 2 * graph
->node
[i
].nvar
;
1421 /* Add constraints that bound the values of the variable and parameter
1422 * coefficients of the schedule.
1424 * The maximal value of the coefficients is defined by the option
1425 * 'schedule_max_coefficient'.
1427 static int add_bound_coefficient_constraints(isl_ctx
*ctx
,
1428 struct isl_sched_graph
*graph
)
1431 int max_coefficient
;
1434 max_coefficient
= ctx
->opt
->schedule_max_coefficient
;
1436 if (max_coefficient
== -1)
1439 total
= isl_basic_set_total_dim(graph
->lp
);
1441 for (i
= 0; i
< graph
->n
; ++i
) {
1442 struct isl_sched_node
*node
= &graph
->node
[i
];
1443 for (j
= 0; j
< 2 * node
->nparam
+ 2 * node
->nvar
; ++j
) {
1445 k
= isl_basic_set_alloc_inequality(graph
->lp
);
1448 dim
= 1 + node
->start
+ 1 + j
;
1449 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
1450 isl_int_set_si(graph
->lp
->ineq
[k
][dim
], -1);
1451 isl_int_set_si(graph
->lp
->ineq
[k
][0], max_coefficient
);
1458 /* Construct an ILP problem for finding schedule coefficients
1459 * that result in non-negative, but small dependence distances
1460 * over all dependences.
1461 * In particular, the dependence distances over proximity edges
1462 * are bounded by m_0 + m_n n and we compute schedule coefficients
1463 * with small values (preferably zero) of m_n and m_0.
1465 * All variables of the ILP are non-negative. The actual coefficients
1466 * may be negative, so each coefficient is represented as the difference
1467 * of two non-negative variables. The negative part always appears
1468 * immediately before the positive part.
1469 * Other than that, the variables have the following order
1471 * - sum of positive and negative parts of m_n coefficients
1473 * - sum of positive and negative parts of all c_n coefficients
1474 * (unconstrained when computing non-parametric schedules)
1475 * - sum of positive and negative parts of all c_x coefficients
1476 * - positive and negative parts of m_n coefficients
1479 * - positive and negative parts of c_i_n (if parametric)
1480 * - positive and negative parts of c_i_x
1482 * The c_i_x are not represented directly, but through the columns of
1483 * node->cmap. That is, the computed values are for variable t_i_x
1484 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
1486 * The constraints are those from the edges plus two or three equalities
1487 * to express the sums.
1489 * If force_zero is set, then we add equalities to ensure that
1490 * the sum of the m_n coefficients and m_0 are both zero.
1492 static int setup_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
1503 int max_constant_term
;
1505 max_constant_term
= ctx
->opt
->schedule_max_constant_term
;
1507 parametric
= ctx
->opt
->schedule_parametric
;
1508 nparam
= isl_space_dim(graph
->node
[0].dim
, isl_dim_param
);
1510 total
= param_pos
+ 2 * nparam
;
1511 for (i
= 0; i
< graph
->n
; ++i
) {
1512 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
1513 if (node_update_cmap(node
) < 0)
1515 node
->start
= total
;
1516 total
+= 1 + 2 * (node
->nparam
+ node
->nvar
);
1519 if (count_constraints(graph
, &n_eq
, &n_ineq
) < 0)
1521 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
1524 dim
= isl_space_set_alloc(ctx
, 0, total
);
1525 isl_basic_set_free(graph
->lp
);
1526 n_eq
+= 2 + parametric
+ force_zero
;
1527 if (max_constant_term
!= -1)
1530 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
1532 k
= isl_basic_set_alloc_equality(graph
->lp
);
1535 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
1537 isl_int_set_si(graph
->lp
->eq
[k
][1], -1);
1538 for (i
= 0; i
< 2 * nparam
; ++i
)
1539 isl_int_set_si(graph
->lp
->eq
[k
][1 + param_pos
+ i
], 1);
1542 k
= isl_basic_set_alloc_equality(graph
->lp
);
1545 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
1546 isl_int_set_si(graph
->lp
->eq
[k
][2], -1);
1550 k
= isl_basic_set_alloc_equality(graph
->lp
);
1553 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
1554 isl_int_set_si(graph
->lp
->eq
[k
][3], -1);
1555 for (i
= 0; i
< graph
->n
; ++i
) {
1556 int pos
= 1 + graph
->node
[i
].start
+ 1;
1558 for (j
= 0; j
< 2 * graph
->node
[i
].nparam
; ++j
)
1559 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
1563 k
= isl_basic_set_alloc_equality(graph
->lp
);
1566 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
1567 isl_int_set_si(graph
->lp
->eq
[k
][4], -1);
1568 for (i
= 0; i
< graph
->n
; ++i
) {
1569 struct isl_sched_node
*node
= &graph
->node
[i
];
1570 int pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
1572 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
1573 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
1576 if (max_constant_term
!= -1)
1577 for (i
= 0; i
< graph
->n
; ++i
) {
1578 struct isl_sched_node
*node
= &graph
->node
[i
];
1579 k
= isl_basic_set_alloc_inequality(graph
->lp
);
1582 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
1583 isl_int_set_si(graph
->lp
->ineq
[k
][1 + node
->start
], -1);
1584 isl_int_set_si(graph
->lp
->ineq
[k
][0], max_constant_term
);
1587 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
1589 if (add_all_validity_constraints(graph
) < 0)
1591 if (add_all_proximity_constraints(graph
) < 0)
1597 /* Analyze the conflicting constraint found by
1598 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
1599 * constraint of one of the edges between distinct nodes, living, moreover
1600 * in distinct SCCs, then record the source and sink SCC as this may
1601 * be a good place to cut between SCCs.
1603 static int check_conflict(int con
, void *user
)
1606 struct isl_sched_graph
*graph
= user
;
1608 if (graph
->src_scc
>= 0)
1611 con
-= graph
->lp
->n_eq
;
1613 if (con
>= graph
->lp
->n_ineq
)
1616 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1617 if (!graph
->edge
[i
].validity
)
1619 if (graph
->edge
[i
].src
== graph
->edge
[i
].dst
)
1621 if (graph
->edge
[i
].src
->scc
== graph
->edge
[i
].dst
->scc
)
1623 if (graph
->edge
[i
].start
> con
)
1625 if (graph
->edge
[i
].end
<= con
)
1627 graph
->src_scc
= graph
->edge
[i
].src
->scc
;
1628 graph
->dst_scc
= graph
->edge
[i
].dst
->scc
;
1634 /* Check whether the next schedule row of the given node needs to be
1635 * non-trivial. Lower-dimensional domains may have some trivial rows,
1636 * but as soon as the number of remaining required non-trivial rows
1637 * is as large as the number or remaining rows to be computed,
1638 * all remaining rows need to be non-trivial.
1640 static int needs_row(struct isl_sched_graph
*graph
, struct isl_sched_node
*node
)
1642 return node
->nvar
- node
->rank
>= graph
->maxvar
- graph
->n_row
;
1645 /* Solve the ILP problem constructed in setup_lp.
1646 * For each node such that all the remaining rows of its schedule
1647 * need to be non-trivial, we construct a non-triviality region.
1648 * This region imposes that the next row is independent of previous rows.
1649 * In particular the coefficients c_i_x are represented by t_i_x
1650 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
1651 * its first columns span the rows of the previously computed part
1652 * of the schedule. The non-triviality region enforces that at least
1653 * one of the remaining components of t_i_x is non-zero, i.e.,
1654 * that the new schedule row depends on at least one of the remaining
1657 static __isl_give isl_vec
*solve_lp(struct isl_sched_graph
*graph
)
1663 for (i
= 0; i
< graph
->n
; ++i
) {
1664 struct isl_sched_node
*node
= &graph
->node
[i
];
1665 int skip
= node
->rank
;
1666 graph
->region
[i
].pos
= node
->start
+ 1 + 2*(node
->nparam
+skip
);
1667 if (needs_row(graph
, node
))
1668 graph
->region
[i
].len
= 2 * (node
->nvar
- skip
);
1670 graph
->region
[i
].len
= 0;
1672 lp
= isl_basic_set_copy(graph
->lp
);
1673 sol
= isl_tab_basic_set_non_trivial_lexmin(lp
, 2, graph
->n
,
1674 graph
->region
, &check_conflict
, graph
);
1678 /* Update the schedules of all nodes based on the given solution
1679 * of the LP problem.
1680 * The new row is added to the current band.
1681 * All possibly negative coefficients are encoded as a difference
1682 * of two non-negative variables, so we need to perform the subtraction
1683 * here. Moreover, if use_cmap is set, then the solution does
1684 * not refer to the actual coefficients c_i_x, but instead to variables
1685 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
1686 * In this case, we then also need to perform this multiplication
1687 * to obtain the values of c_i_x.
1689 * If check_zero is set, then the first two coordinates of sol are
1690 * assumed to correspond to the dependence distance. If these two
1691 * coordinates are zero, then the corresponding scheduling dimension
1692 * is marked as being zero distance.
1694 static int update_schedule(struct isl_sched_graph
*graph
,
1695 __isl_take isl_vec
*sol
, int use_cmap
, int check_zero
)
1699 isl_vec
*csol
= NULL
;
1704 isl_die(sol
->ctx
, isl_error_internal
,
1705 "no solution found", goto error
);
1706 if (graph
->n_total_row
>= graph
->max_row
)
1707 isl_die(sol
->ctx
, isl_error_internal
,
1708 "too many schedule rows", goto error
);
1711 zero
= isl_int_is_zero(sol
->el
[1]) &&
1712 isl_int_is_zero(sol
->el
[2]);
1714 for (i
= 0; i
< graph
->n
; ++i
) {
1715 struct isl_sched_node
*node
= &graph
->node
[i
];
1716 int pos
= node
->start
;
1717 int row
= isl_mat_rows(node
->sched
);
1720 csol
= isl_vec_alloc(sol
->ctx
, node
->nvar
);
1724 isl_map_free(node
->sched_map
);
1725 node
->sched_map
= NULL
;
1726 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
1729 node
->sched
= isl_mat_set_element(node
->sched
, row
, 0,
1731 for (j
= 0; j
< node
->nparam
+ node
->nvar
; ++j
)
1732 isl_int_sub(sol
->el
[1 + pos
+ 1 + 2 * j
+ 1],
1733 sol
->el
[1 + pos
+ 1 + 2 * j
+ 1],
1734 sol
->el
[1 + pos
+ 1 + 2 * j
]);
1735 for (j
= 0; j
< node
->nparam
; ++j
)
1736 node
->sched
= isl_mat_set_element(node
->sched
,
1737 row
, 1 + j
, sol
->el
[1+pos
+1+2*j
+1]);
1738 for (j
= 0; j
< node
->nvar
; ++j
)
1739 isl_int_set(csol
->el
[j
],
1740 sol
->el
[1+pos
+1+2*(node
->nparam
+j
)+1]);
1742 csol
= isl_mat_vec_product(isl_mat_copy(node
->cmap
),
1746 for (j
= 0; j
< node
->nvar
; ++j
)
1747 node
->sched
= isl_mat_set_element(node
->sched
,
1748 row
, 1 + node
->nparam
+ j
, csol
->el
[j
]);
1749 node
->band
[graph
->n_total_row
] = graph
->n_band
;
1750 node
->zero
[graph
->n_total_row
] = zero
;
1756 graph
->n_total_row
++;
1765 /* Convert row "row" of node->sched into an isl_aff living in "ls"
1766 * and return this isl_aff.
1768 static __isl_give isl_aff
*extract_schedule_row(__isl_take isl_local_space
*ls
,
1769 struct isl_sched_node
*node
, int row
)
1777 aff
= isl_aff_zero_on_domain(ls
);
1778 isl_mat_get_element(node
->sched
, row
, 0, &v
);
1779 aff
= isl_aff_set_constant(aff
, v
);
1780 for (j
= 0; j
< node
->nparam
; ++j
) {
1781 isl_mat_get_element(node
->sched
, row
, 1 + j
, &v
);
1782 aff
= isl_aff_set_coefficient(aff
, isl_dim_param
, j
, v
);
1784 for (j
= 0; j
< node
->nvar
; ++j
) {
1785 isl_mat_get_element(node
->sched
, row
, 1 + node
->nparam
+ j
, &v
);
1786 aff
= isl_aff_set_coefficient(aff
, isl_dim_in
, j
, v
);
1794 /* Convert node->sched into a multi_aff and return this multi_aff.
1796 static __isl_give isl_multi_aff
*node_extract_schedule_multi_aff(
1797 struct isl_sched_node
*node
)
1801 isl_local_space
*ls
;
1806 nrow
= isl_mat_rows(node
->sched
);
1807 ncol
= isl_mat_cols(node
->sched
) - 1;
1808 space
= isl_space_from_domain(isl_space_copy(node
->dim
));
1809 space
= isl_space_add_dims(space
, isl_dim_out
, nrow
);
1810 ma
= isl_multi_aff_zero(space
);
1811 ls
= isl_local_space_from_space(isl_space_copy(node
->dim
));
1813 for (i
= 0; i
< nrow
; ++i
) {
1814 aff
= extract_schedule_row(isl_local_space_copy(ls
), node
, i
);
1815 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
1818 isl_local_space_free(ls
);
1823 /* Convert node->sched into a map and return this map.
1825 * The result is cached in node->sched_map, which needs to be released
1826 * whenever node->sched is updated.
1828 static __isl_give isl_map
*node_extract_schedule(struct isl_sched_node
*node
)
1830 if (!node
->sched_map
) {
1833 ma
= node_extract_schedule_multi_aff(node
);
1834 node
->sched_map
= isl_map_from_multi_aff(ma
);
1837 return isl_map_copy(node
->sched_map
);
1840 /* Update the given dependence relation based on the current schedule.
1841 * That is, intersect the dependence relation with a map expressing
1842 * that source and sink are executed within the same iteration of
1843 * the current schedule.
1844 * This is not the most efficient way, but this shouldn't be a critical
1847 static __isl_give isl_map
*specialize(__isl_take isl_map
*map
,
1848 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
1850 isl_map
*src_sched
, *dst_sched
, *id
;
1852 src_sched
= node_extract_schedule(src
);
1853 dst_sched
= node_extract_schedule(dst
);
1854 id
= isl_map_apply_range(src_sched
, isl_map_reverse(dst_sched
));
1855 return isl_map_intersect(map
, id
);
1858 /* Update the dependence relations of all edges based on the current schedule.
1859 * If a dependence is carried completely by the current schedule, then
1860 * it is removed from the edge_tables. It is kept in the list of edges
1861 * as otherwise all edge_tables would have to be recomputed.
1863 static int update_edges(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1867 for (i
= graph
->n_edge
- 1; i
>= 0; --i
) {
1868 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1869 edge
->map
= specialize(edge
->map
, edge
->src
, edge
->dst
);
1873 if (isl_map_plain_is_empty(edge
->map
))
1874 graph_remove_edge(graph
, edge
);
1880 static void next_band(struct isl_sched_graph
*graph
)
1882 graph
->band_start
= graph
->n_total_row
;
1886 /* Topologically sort statements mapped to the same schedule iteration
1887 * and add a row to the schedule corresponding to this order.
1889 static int sort_statements(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1896 if (update_edges(ctx
, graph
) < 0)
1899 if (graph
->n_edge
== 0)
1902 if (detect_sccs(ctx
, graph
) < 0)
1905 if (graph
->n_total_row
>= graph
->max_row
)
1906 isl_die(ctx
, isl_error_internal
,
1907 "too many schedule rows", return -1);
1909 for (i
= 0; i
< graph
->n
; ++i
) {
1910 struct isl_sched_node
*node
= &graph
->node
[i
];
1911 int row
= isl_mat_rows(node
->sched
);
1912 int cols
= isl_mat_cols(node
->sched
);
1914 isl_map_free(node
->sched_map
);
1915 node
->sched_map
= NULL
;
1916 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
1919 node
->sched
= isl_mat_set_element_si(node
->sched
, row
, 0,
1921 for (j
= 1; j
< cols
; ++j
)
1922 node
->sched
= isl_mat_set_element_si(node
->sched
,
1924 node
->band
[graph
->n_total_row
] = graph
->n_band
;
1927 graph
->n_total_row
++;
1933 /* Construct an isl_schedule based on the computed schedule stored
1934 * in graph and with parameters specified by dim.
1936 static __isl_give isl_schedule
*extract_schedule(struct isl_sched_graph
*graph
,
1937 __isl_take isl_space
*dim
)
1941 isl_schedule
*sched
= NULL
;
1946 ctx
= isl_space_get_ctx(dim
);
1947 sched
= isl_calloc(ctx
, struct isl_schedule
,
1948 sizeof(struct isl_schedule
) +
1949 (graph
->n
- 1) * sizeof(struct isl_schedule_node
));
1954 sched
->n
= graph
->n
;
1955 sched
->n_band
= graph
->n_band
;
1956 sched
->n_total_row
= graph
->n_total_row
;
1958 for (i
= 0; i
< sched
->n
; ++i
) {
1960 int *band_end
, *band_id
, *zero
;
1962 sched
->node
[i
].sched
=
1963 node_extract_schedule_multi_aff(&graph
->node
[i
]);
1964 if (!sched
->node
[i
].sched
)
1967 sched
->node
[i
].n_band
= graph
->n_band
;
1968 if (graph
->n_band
== 0)
1971 band_end
= isl_alloc_array(ctx
, int, graph
->n_band
);
1972 band_id
= isl_alloc_array(ctx
, int, graph
->n_band
);
1973 zero
= isl_alloc_array(ctx
, int, graph
->n_total_row
);
1974 sched
->node
[i
].band_end
= band_end
;
1975 sched
->node
[i
].band_id
= band_id
;
1976 sched
->node
[i
].zero
= zero
;
1977 if (!band_end
|| !band_id
|| !zero
)
1980 for (r
= 0; r
< graph
->n_total_row
; ++r
)
1981 zero
[r
] = graph
->node
[i
].zero
[r
];
1982 for (r
= b
= 0; r
< graph
->n_total_row
; ++r
) {
1983 if (graph
->node
[i
].band
[r
] == b
)
1986 if (graph
->node
[i
].band
[r
] == -1)
1989 if (r
== graph
->n_total_row
)
1991 sched
->node
[i
].n_band
= b
;
1992 for (--b
; b
>= 0; --b
)
1993 band_id
[b
] = graph
->node
[i
].band_id
[b
];
2000 isl_space_free(dim
);
2001 isl_schedule_free(sched
);
2005 /* Copy nodes that satisfy node_pred from the src dependence graph
2006 * to the dst dependence graph.
2008 static int copy_nodes(struct isl_sched_graph
*dst
, struct isl_sched_graph
*src
,
2009 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
2014 for (i
= 0; i
< src
->n
; ++i
) {
2015 if (!node_pred(&src
->node
[i
], data
))
2017 dst
->node
[dst
->n
].dim
= isl_space_copy(src
->node
[i
].dim
);
2018 dst
->node
[dst
->n
].nvar
= src
->node
[i
].nvar
;
2019 dst
->node
[dst
->n
].nparam
= src
->node
[i
].nparam
;
2020 dst
->node
[dst
->n
].sched
= isl_mat_copy(src
->node
[i
].sched
);
2021 dst
->node
[dst
->n
].sched_map
=
2022 isl_map_copy(src
->node
[i
].sched_map
);
2023 dst
->node
[dst
->n
].band
= src
->node
[i
].band
;
2024 dst
->node
[dst
->n
].band_id
= src
->node
[i
].band_id
;
2025 dst
->node
[dst
->n
].zero
= src
->node
[i
].zero
;
2032 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
2033 * to the dst dependence graph.
2034 * If the source or destination node of the edge is not in the destination
2035 * graph, then it must be a backward proximity edge and it should simply
2038 static int copy_edges(isl_ctx
*ctx
, struct isl_sched_graph
*dst
,
2039 struct isl_sched_graph
*src
,
2040 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
), int data
)
2043 enum isl_edge_type t
;
2046 for (i
= 0; i
< src
->n_edge
; ++i
) {
2047 struct isl_sched_edge
*edge
= &src
->edge
[i
];
2049 struct isl_sched_node
*dst_src
, *dst_dst
;
2051 if (!edge_pred(edge
, data
))
2054 if (isl_map_plain_is_empty(edge
->map
))
2057 dst_src
= graph_find_node(ctx
, dst
, edge
->src
->dim
);
2058 dst_dst
= graph_find_node(ctx
, dst
, edge
->dst
->dim
);
2059 if (!dst_src
|| !dst_dst
) {
2061 isl_die(ctx
, isl_error_internal
,
2062 "backward validity edge", return -1);
2066 map
= isl_map_copy(edge
->map
);
2068 dst
->edge
[dst
->n_edge
].src
= dst_src
;
2069 dst
->edge
[dst
->n_edge
].dst
= dst_dst
;
2070 dst
->edge
[dst
->n_edge
].map
= map
;
2071 dst
->edge
[dst
->n_edge
].validity
= edge
->validity
;
2072 dst
->edge
[dst
->n_edge
].proximity
= edge
->proximity
;
2075 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
2077 graph_find_edge(src
, t
, edge
->src
, edge
->dst
))
2079 if (graph_edge_table_add(ctx
, dst
, t
,
2080 &dst
->edge
[dst
->n_edge
- 1]) < 0)
2088 /* Given a "src" dependence graph that contains the nodes from "dst"
2089 * that satisfy node_pred, copy the schedule computed in "src"
2090 * for those nodes back to "dst".
2092 static int copy_schedule(struct isl_sched_graph
*dst
,
2093 struct isl_sched_graph
*src
,
2094 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
2099 for (i
= 0; i
< dst
->n
; ++i
) {
2100 if (!node_pred(&dst
->node
[i
], data
))
2102 isl_mat_free(dst
->node
[i
].sched
);
2103 isl_map_free(dst
->node
[i
].sched_map
);
2104 dst
->node
[i
].sched
= isl_mat_copy(src
->node
[src
->n
].sched
);
2105 dst
->node
[i
].sched_map
=
2106 isl_map_copy(src
->node
[src
->n
].sched_map
);
2110 dst
->max_row
= src
->max_row
;
2111 dst
->n_total_row
= src
->n_total_row
;
2112 dst
->n_band
= src
->n_band
;
2117 /* Compute the maximal number of variables over all nodes.
2118 * This is the maximal number of linearly independent schedule
2119 * rows that we need to compute.
2120 * Just in case we end up in a part of the dependence graph
2121 * with only lower-dimensional domains, we make sure we will
2122 * compute the required amount of extra linearly independent rows.
2124 static int compute_maxvar(struct isl_sched_graph
*graph
)
2129 for (i
= 0; i
< graph
->n
; ++i
) {
2130 struct isl_sched_node
*node
= &graph
->node
[i
];
2133 if (node_update_cmap(node
) < 0)
2135 nvar
= node
->nvar
+ graph
->n_row
- node
->rank
;
2136 if (nvar
> graph
->maxvar
)
2137 graph
->maxvar
= nvar
;
2143 static int compute_schedule(isl_ctx
*ctx
, struct isl_sched_graph
*graph
);
2144 static int compute_schedule_wcc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
);
2146 /* Compute a schedule for a subgraph of "graph". In particular, for
2147 * the graph composed of nodes that satisfy node_pred and edges that
2148 * that satisfy edge_pred. The caller should precompute the number
2149 * of nodes and edges that satisfy these predicates and pass them along
2150 * as "n" and "n_edge".
2151 * If the subgraph is known to consist of a single component, then wcc should
2152 * be set and then we call compute_schedule_wcc on the constructed subgraph.
2153 * Otherwise, we call compute_schedule, which will check whether the subgraph
2156 static int compute_sub_schedule(isl_ctx
*ctx
,
2157 struct isl_sched_graph
*graph
, int n
, int n_edge
,
2158 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
2159 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
2162 struct isl_sched_graph split
= { 0 };
2165 if (graph_alloc(ctx
, &split
, n
, n_edge
) < 0)
2167 if (copy_nodes(&split
, graph
, node_pred
, data
) < 0)
2169 if (graph_init_table(ctx
, &split
) < 0)
2171 for (t
= 0; t
<= isl_edge_last
; ++t
)
2172 split
.max_edge
[t
] = graph
->max_edge
[t
];
2173 if (graph_init_edge_tables(ctx
, &split
) < 0)
2175 if (copy_edges(ctx
, &split
, graph
, edge_pred
, data
) < 0)
2177 split
.n_row
= graph
->n_row
;
2178 split
.max_row
= graph
->max_row
;
2179 split
.n_total_row
= graph
->n_total_row
;
2180 split
.n_band
= graph
->n_band
;
2181 split
.band_start
= graph
->band_start
;
2183 if (wcc
&& compute_schedule_wcc(ctx
, &split
) < 0)
2185 if (!wcc
&& compute_schedule(ctx
, &split
) < 0)
2188 copy_schedule(graph
, &split
, node_pred
, data
);
2190 graph_free(ctx
, &split
);
2193 graph_free(ctx
, &split
);
2197 static int node_scc_exactly(struct isl_sched_node
*node
, int scc
)
2199 return node
->scc
== scc
;
2202 static int node_scc_at_most(struct isl_sched_node
*node
, int scc
)
2204 return node
->scc
<= scc
;
2207 static int node_scc_at_least(struct isl_sched_node
*node
, int scc
)
2209 return node
->scc
>= scc
;
2212 static int edge_scc_exactly(struct isl_sched_edge
*edge
, int scc
)
2214 return edge
->src
->scc
== scc
&& edge
->dst
->scc
== scc
;
2217 static int edge_dst_scc_at_most(struct isl_sched_edge
*edge
, int scc
)
2219 return edge
->dst
->scc
<= scc
;
2222 static int edge_src_scc_at_least(struct isl_sched_edge
*edge
, int scc
)
2224 return edge
->src
->scc
>= scc
;
2227 /* Pad the schedules of all nodes with zero rows such that in the end
2228 * they all have graph->n_total_row rows.
2229 * The extra rows don't belong to any band, so they get assigned band number -1.
2231 static int pad_schedule(struct isl_sched_graph
*graph
)
2235 for (i
= 0; i
< graph
->n
; ++i
) {
2236 struct isl_sched_node
*node
= &graph
->node
[i
];
2237 int row
= isl_mat_rows(node
->sched
);
2238 if (graph
->n_total_row
> row
) {
2239 isl_map_free(node
->sched_map
);
2240 node
->sched_map
= NULL
;
2242 node
->sched
= isl_mat_add_zero_rows(node
->sched
,
2243 graph
->n_total_row
- row
);
2246 for (j
= row
; j
< graph
->n_total_row
; ++j
)
2253 /* Reset the current band by dropping all its schedule rows.
2255 static int reset_band(struct isl_sched_graph
*graph
)
2260 drop
= graph
->n_total_row
- graph
->band_start
;
2261 graph
->n_total_row
-= drop
;
2262 graph
->n_row
-= drop
;
2264 for (i
= 0; i
< graph
->n
; ++i
) {
2265 struct isl_sched_node
*node
= &graph
->node
[i
];
2267 isl_map_free(node
->sched_map
);
2268 node
->sched_map
= NULL
;
2270 node
->sched
= isl_mat_drop_rows(node
->sched
,
2271 graph
->band_start
, drop
);
2280 /* Split the current graph into two parts and compute a schedule for each
2281 * part individually. In particular, one part consists of all SCCs up
2282 * to and including graph->src_scc, while the other part contains the other
2285 * The split is enforced in the schedule by constant rows with two different
2286 * values (0 and 1). These constant rows replace the previously computed rows
2287 * in the current band.
2288 * It would be possible to reuse them as the first rows in the next
2289 * band, but recomputing them may result in better rows as we are looking
2290 * at a smaller part of the dependence graph.
2291 * compute_split_schedule is only called when no zero-distance schedule row
2292 * could be found on the entire graph, so we wark the splitting row as
2293 * non zero-distance.
2295 * The band_id of the second group is set to n, where n is the number
2296 * of nodes in the first group. This ensures that the band_ids over
2297 * the two groups remain disjoint, even if either or both of the two
2298 * groups contain independent components.
2300 static int compute_split_schedule(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2302 int i
, j
, n
, e1
, e2
;
2303 int n_total_row
, orig_total_row
;
2304 int n_band
, orig_band
;
2306 if (graph
->n_total_row
>= graph
->max_row
)
2307 isl_die(ctx
, isl_error_internal
,
2308 "too many schedule rows", return -1);
2310 if (reset_band(graph
) < 0)
2314 for (i
= 0; i
< graph
->n
; ++i
) {
2315 struct isl_sched_node
*node
= &graph
->node
[i
];
2316 int row
= isl_mat_rows(node
->sched
);
2317 int cols
= isl_mat_cols(node
->sched
);
2318 int before
= node
->scc
<= graph
->src_scc
;
2323 isl_map_free(node
->sched_map
);
2324 node
->sched_map
= NULL
;
2325 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
2328 node
->sched
= isl_mat_set_element_si(node
->sched
, row
, 0,
2330 for (j
= 1; j
< cols
; ++j
)
2331 node
->sched
= isl_mat_set_element_si(node
->sched
,
2333 node
->band
[graph
->n_total_row
] = graph
->n_band
;
2334 node
->zero
[graph
->n_total_row
] = 0;
2338 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2339 if (graph
->edge
[i
].dst
->scc
<= graph
->src_scc
)
2341 if (graph
->edge
[i
].src
->scc
> graph
->src_scc
)
2345 graph
->n_total_row
++;
2348 for (i
= 0; i
< graph
->n
; ++i
) {
2349 struct isl_sched_node
*node
= &graph
->node
[i
];
2350 if (node
->scc
> graph
->src_scc
)
2351 node
->band_id
[graph
->n_band
] = n
;
2354 orig_total_row
= graph
->n_total_row
;
2355 orig_band
= graph
->n_band
;
2356 if (compute_sub_schedule(ctx
, graph
, n
, e1
,
2357 &node_scc_at_most
, &edge_dst_scc_at_most
,
2358 graph
->src_scc
, 0) < 0)
2360 n_total_row
= graph
->n_total_row
;
2361 graph
->n_total_row
= orig_total_row
;
2362 n_band
= graph
->n_band
;
2363 graph
->n_band
= orig_band
;
2364 if (compute_sub_schedule(ctx
, graph
, graph
->n
- n
, e2
,
2365 &node_scc_at_least
, &edge_src_scc_at_least
,
2366 graph
->src_scc
+ 1, 0) < 0)
2368 if (n_total_row
> graph
->n_total_row
)
2369 graph
->n_total_row
= n_total_row
;
2370 if (n_band
> graph
->n_band
)
2371 graph
->n_band
= n_band
;
2373 return pad_schedule(graph
);
2376 /* Compute the next band of the schedule after updating the dependence
2377 * relations based on the the current schedule.
2379 static int compute_next_band(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2381 if (update_edges(ctx
, graph
) < 0)
2385 return compute_schedule(ctx
, graph
);
2388 /* Add constraints to graph->lp that force the dependence "map" (which
2389 * is part of the dependence relation of "edge")
2390 * to be respected and attempt to carry it, where the edge is one from
2391 * a node j to itself. "pos" is the sequence number of the given map.
2392 * That is, add constraints that enforce
2394 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
2395 * = c_j_x (y - x) >= e_i
2397 * for each (x,y) in R.
2398 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2399 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
2400 * with each coefficient in c_j_x represented as a pair of non-negative
2403 static int add_intra_constraints(struct isl_sched_graph
*graph
,
2404 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
2407 isl_ctx
*ctx
= isl_map_get_ctx(map
);
2409 isl_dim_map
*dim_map
;
2410 isl_basic_set
*coef
;
2411 struct isl_sched_node
*node
= edge
->src
;
2413 coef
= intra_coefficients(graph
, map
);
2417 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
2419 total
= isl_basic_set_total_dim(graph
->lp
);
2420 dim_map
= isl_dim_map_alloc(ctx
, total
);
2421 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
2422 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
2423 isl_space_dim(dim
, isl_dim_set
), 1,
2425 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
2426 isl_space_dim(dim
, isl_dim_set
), 1,
2428 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
2429 coef
->n_eq
, coef
->n_ineq
);
2430 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
2432 isl_space_free(dim
);
2437 /* Add constraints to graph->lp that force the dependence "map" (which
2438 * is part of the dependence relation of "edge")
2439 * to be respected and attempt to carry it, where the edge is one from
2440 * node j to node k. "pos" is the sequence number of the given map.
2441 * That is, add constraints that enforce
2443 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
2445 * for each (x,y) in R.
2446 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2447 * of valid constraints for R and then plug in
2448 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
2449 * with each coefficient (except e_i, c_k_0 and c_j_0)
2450 * represented as a pair of non-negative coefficients.
2452 static int add_inter_constraints(struct isl_sched_graph
*graph
,
2453 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
2456 isl_ctx
*ctx
= isl_map_get_ctx(map
);
2458 isl_dim_map
*dim_map
;
2459 isl_basic_set
*coef
;
2460 struct isl_sched_node
*src
= edge
->src
;
2461 struct isl_sched_node
*dst
= edge
->dst
;
2463 coef
= inter_coefficients(graph
, map
);
2467 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
2469 total
= isl_basic_set_total_dim(graph
->lp
);
2470 dim_map
= isl_dim_map_alloc(ctx
, total
);
2472 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
2474 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, 1);
2475 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, -1);
2476 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, 1);
2477 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
2478 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
2480 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
2481 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
2484 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, -1);
2485 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, 1);
2486 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, -1);
2487 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
2488 isl_space_dim(dim
, isl_dim_set
), 1,
2490 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
2491 isl_space_dim(dim
, isl_dim_set
), 1,
2494 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
2495 coef
->n_eq
, coef
->n_ineq
);
2496 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
2498 isl_space_free(dim
);
2503 /* Add constraints to graph->lp that force all validity dependences
2504 * to be respected and attempt to carry them.
2506 static int add_all_constraints(struct isl_sched_graph
*graph
)
2512 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2513 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2515 if (!edge
->validity
)
2518 for (j
= 0; j
< edge
->map
->n
; ++j
) {
2519 isl_basic_map
*bmap
;
2522 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
2523 map
= isl_map_from_basic_map(bmap
);
2525 if (edge
->src
== edge
->dst
&&
2526 add_intra_constraints(graph
, edge
, map
, pos
) < 0)
2528 if (edge
->src
!= edge
->dst
&&
2529 add_inter_constraints(graph
, edge
, map
, pos
) < 0)
2538 /* Count the number of equality and inequality constraints
2539 * that will be added to the carry_lp problem.
2540 * We count each edge exactly once.
2542 static int count_all_constraints(struct isl_sched_graph
*graph
,
2543 int *n_eq
, int *n_ineq
)
2547 *n_eq
= *n_ineq
= 0;
2548 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2549 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2550 for (j
= 0; j
< edge
->map
->n
; ++j
) {
2551 isl_basic_map
*bmap
;
2554 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
2555 map
= isl_map_from_basic_map(bmap
);
2557 if (count_map_constraints(graph
, edge
, map
,
2558 n_eq
, n_ineq
, 1) < 0)
2566 /* Construct an LP problem for finding schedule coefficients
2567 * such that the schedule carries as many dependences as possible.
2568 * In particular, for each dependence i, we bound the dependence distance
2569 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
2570 * of all e_i's. Dependence with e_i = 0 in the solution are simply
2571 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
2572 * Note that if the dependence relation is a union of basic maps,
2573 * then we have to consider each basic map individually as it may only
2574 * be possible to carry the dependences expressed by some of those
2575 * basic maps and not all off them.
2576 * Below, we consider each of those basic maps as a separate "edge".
2578 * All variables of the LP are non-negative. The actual coefficients
2579 * may be negative, so each coefficient is represented as the difference
2580 * of two non-negative variables. The negative part always appears
2581 * immediately before the positive part.
2582 * Other than that, the variables have the following order
2584 * - sum of (1 - e_i) over all edges
2585 * - sum of positive and negative parts of all c_n coefficients
2586 * (unconstrained when computing non-parametric schedules)
2587 * - sum of positive and negative parts of all c_x coefficients
2592 * - positive and negative parts of c_i_n (if parametric)
2593 * - positive and negative parts of c_i_x
2595 * The constraints are those from the (validity) edges plus three equalities
2596 * to express the sums and n_edge inequalities to express e_i <= 1.
2598 static int setup_carry_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2608 for (i
= 0; i
< graph
->n_edge
; ++i
)
2609 n_edge
+= graph
->edge
[i
].map
->n
;
2612 for (i
= 0; i
< graph
->n
; ++i
) {
2613 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
2614 node
->start
= total
;
2615 total
+= 1 + 2 * (node
->nparam
+ node
->nvar
);
2618 if (count_all_constraints(graph
, &n_eq
, &n_ineq
) < 0)
2620 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2623 dim
= isl_space_set_alloc(ctx
, 0, total
);
2624 isl_basic_set_free(graph
->lp
);
2627 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
2628 graph
->lp
= isl_basic_set_set_rational(graph
->lp
);
2630 k
= isl_basic_set_alloc_equality(graph
->lp
);
2633 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2634 isl_int_set_si(graph
->lp
->eq
[k
][0], -n_edge
);
2635 isl_int_set_si(graph
->lp
->eq
[k
][1], 1);
2636 for (i
= 0; i
< n_edge
; ++i
)
2637 isl_int_set_si(graph
->lp
->eq
[k
][4 + i
], 1);
2639 k
= isl_basic_set_alloc_equality(graph
->lp
);
2642 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2643 isl_int_set_si(graph
->lp
->eq
[k
][2], -1);
2644 for (i
= 0; i
< graph
->n
; ++i
) {
2645 int pos
= 1 + graph
->node
[i
].start
+ 1;
2647 for (j
= 0; j
< 2 * graph
->node
[i
].nparam
; ++j
)
2648 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2651 k
= isl_basic_set_alloc_equality(graph
->lp
);
2654 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2655 isl_int_set_si(graph
->lp
->eq
[k
][3], -1);
2656 for (i
= 0; i
< graph
->n
; ++i
) {
2657 struct isl_sched_node
*node
= &graph
->node
[i
];
2658 int pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
2660 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
2661 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2664 for (i
= 0; i
< n_edge
; ++i
) {
2665 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2668 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2669 isl_int_set_si(graph
->lp
->ineq
[k
][4 + i
], -1);
2670 isl_int_set_si(graph
->lp
->ineq
[k
][0], 1);
2673 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
2675 if (add_all_constraints(graph
) < 0)
2681 /* If the schedule_split_scaled option is set and if the linear
2682 * parts of the scheduling rows for all nodes in the graphs have
2683 * non-trivial common divisor, then split off the constant term
2684 * from the linear part.
2685 * The constant term is then placed in a separate band and
2686 * the linear part is reduced.
2688 static int split_scaled(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2694 if (!ctx
->opt
->schedule_split_scaled
)
2699 if (graph
->n_total_row
>= graph
->max_row
)
2700 isl_die(ctx
, isl_error_internal
,
2701 "too many schedule rows", return -1);
2704 isl_int_init(gcd_i
);
2706 isl_int_set_si(gcd
, 0);
2708 row
= isl_mat_rows(graph
->node
[0].sched
) - 1;
2710 for (i
= 0; i
< graph
->n
; ++i
) {
2711 struct isl_sched_node
*node
= &graph
->node
[i
];
2712 int cols
= isl_mat_cols(node
->sched
);
2714 isl_seq_gcd(node
->sched
->row
[row
] + 1, cols
- 1, &gcd_i
);
2715 isl_int_gcd(gcd
, gcd
, gcd_i
);
2718 isl_int_clear(gcd_i
);
2720 if (isl_int_cmp_si(gcd
, 1) <= 0) {
2727 for (i
= 0; i
< graph
->n
; ++i
) {
2728 struct isl_sched_node
*node
= &graph
->node
[i
];
2730 isl_map_free(node
->sched_map
);
2731 node
->sched_map
= NULL
;
2732 node
->sched
= isl_mat_add_zero_rows(node
->sched
, 1);
2735 isl_int_fdiv_r(node
->sched
->row
[row
+ 1][0],
2736 node
->sched
->row
[row
][0], gcd
);
2737 isl_int_fdiv_q(node
->sched
->row
[row
][0],
2738 node
->sched
->row
[row
][0], gcd
);
2739 isl_int_mul(node
->sched
->row
[row
][0],
2740 node
->sched
->row
[row
][0], gcd
);
2741 node
->sched
= isl_mat_scale_down_row(node
->sched
, row
, gcd
);
2744 node
->band
[graph
->n_total_row
] = graph
->n_band
;
2747 graph
->n_total_row
++;
2756 static int compute_component_schedule(isl_ctx
*ctx
,
2757 struct isl_sched_graph
*graph
);
2759 /* Is the schedule row "sol" trivial on node "node"?
2760 * That is, is the solution zero on the dimensions orthogonal to
2761 * the previously found solutions?
2762 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
2764 * Each coefficient is represented as the difference between
2765 * two non-negative values in "sol". "sol" has been computed
2766 * in terms of the original iterators (i.e., without use of cmap).
2767 * We construct the schedule row s and write it as a linear
2768 * combination of (linear combinations of) previously computed schedule rows.
2769 * s = Q c or c = U s.
2770 * If the final entries of c are all zero, then the solution is trivial.
2772 static int is_trivial(struct isl_sched_node
*node
, __isl_keep isl_vec
*sol
)
2782 if (node
->nvar
== node
->rank
)
2785 ctx
= isl_vec_get_ctx(sol
);
2786 node_sol
= isl_vec_alloc(ctx
, node
->nvar
);
2790 pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
2792 for (i
= 0; i
< node
->nvar
; ++i
)
2793 isl_int_sub(node_sol
->el
[i
],
2794 sol
->el
[pos
+ 2 * i
+ 1], sol
->el
[pos
+ 2 * i
]);
2796 node_sol
= isl_mat_vec_product(isl_mat_copy(node
->cinv
), node_sol
);
2801 trivial
= isl_seq_first_non_zero(node_sol
->el
+ node
->rank
,
2802 node
->nvar
- node
->rank
) == -1;
2804 isl_vec_free(node_sol
);
2809 /* Is the schedule row "sol" trivial on any node where it should
2811 * "sol" has been computed in terms of the original iterators
2812 * (i.e., without use of cmap).
2813 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
2815 static int is_any_trivial(struct isl_sched_graph
*graph
,
2816 __isl_keep isl_vec
*sol
)
2820 for (i
= 0; i
< graph
->n
; ++i
) {
2821 struct isl_sched_node
*node
= &graph
->node
[i
];
2824 if (!needs_row(graph
, node
))
2826 trivial
= is_trivial(node
, sol
);
2827 if (trivial
< 0 || trivial
)
2834 /* Construct a schedule row for each node such that as many dependences
2835 * as possible are carried and then continue with the next band.
2837 * If the computed schedule row turns out to be trivial on one or
2838 * more nodes where it should not be trivial, then we throw it away
2839 * and try again on each component separately.
2841 static int carry_dependences(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2850 for (i
= 0; i
< graph
->n_edge
; ++i
)
2851 n_edge
+= graph
->edge
[i
].map
->n
;
2853 if (setup_carry_lp(ctx
, graph
) < 0)
2856 lp
= isl_basic_set_copy(graph
->lp
);
2857 sol
= isl_tab_basic_set_non_neg_lexmin(lp
);
2861 if (sol
->size
== 0) {
2863 isl_die(ctx
, isl_error_internal
,
2864 "error in schedule construction", return -1);
2867 isl_int_divexact(sol
->el
[1], sol
->el
[1], sol
->el
[0]);
2868 if (isl_int_cmp_si(sol
->el
[1], n_edge
) >= 0) {
2870 isl_die(ctx
, isl_error_unknown
,
2871 "unable to carry dependences", return -1);
2874 trivial
= is_any_trivial(graph
, sol
);
2876 sol
= isl_vec_free(sol
);
2877 } else if (trivial
) {
2880 return compute_component_schedule(ctx
, graph
);
2881 isl_die(ctx
, isl_error_unknown
,
2882 "unable to construct non-trivial solution", return -1);
2885 if (update_schedule(graph
, sol
, 0, 0) < 0)
2888 if (split_scaled(ctx
, graph
) < 0)
2891 return compute_next_band(ctx
, graph
);
2894 /* Are there any (non-empty) validity edges in the graph?
2896 static int has_validity_edges(struct isl_sched_graph
*graph
)
2900 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2903 empty
= isl_map_plain_is_empty(graph
->edge
[i
].map
);
2908 if (graph
->edge
[i
].validity
)
2915 /* Should we apply a Feautrier step?
2916 * That is, did the user request the Feautrier algorithm and are
2917 * there any validity dependences (left)?
2919 static int need_feautrier_step(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2921 if (ctx
->opt
->schedule_algorithm
!= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
)
2924 return has_validity_edges(graph
);
2927 /* Compute a schedule for a connected dependence graph using Feautrier's
2928 * multi-dimensional scheduling algorithm.
2929 * The original algorithm is described in [1].
2930 * The main idea is to minimize the number of scheduling dimensions, by
2931 * trying to satisfy as many dependences as possible per scheduling dimension.
2933 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
2934 * Problem, Part II: Multi-Dimensional Time.
2935 * In Intl. Journal of Parallel Programming, 1992.
2937 static int compute_schedule_wcc_feautrier(isl_ctx
*ctx
,
2938 struct isl_sched_graph
*graph
)
2940 return carry_dependences(ctx
, graph
);
2943 /* Compute a schedule for a connected dependence graph.
2944 * We try to find a sequence of as many schedule rows as possible that result
2945 * in non-negative dependence distances (independent of the previous rows
2946 * in the sequence, i.e., such that the sequence is tilable).
2947 * If we can't find any more rows we either
2948 * - split between SCCs and start over (assuming we found an interesting
2949 * pair of SCCs between which to split)
2950 * - continue with the next band (assuming the current band has at least
2952 * - try to carry as many dependences as possible and continue with the next
2955 * If Feautrier's algorithm is selected, we first recursively try to satisfy
2956 * as many validity dependences as possible. When all validity dependences
2957 * are satisfied we extend the schedule to a full-dimensional schedule.
2959 * If we manage to complete the schedule, we finish off by topologically
2960 * sorting the statements based on the remaining dependences.
2962 * If ctx->opt->schedule_outer_zero_distance is set, then we force the
2963 * outermost dimension in the current band to be zero distance. If this
2964 * turns out to be impossible, we fall back on the general scheme above
2965 * and try to carry as many dependences as possible.
2967 static int compute_schedule_wcc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2971 if (detect_sccs(ctx
, graph
) < 0)
2973 if (sort_sccs(graph
) < 0)
2976 if (compute_maxvar(graph
) < 0)
2979 if (need_feautrier_step(ctx
, graph
))
2980 return compute_schedule_wcc_feautrier(ctx
, graph
);
2982 if (ctx
->opt
->schedule_outer_zero_distance
)
2985 while (graph
->n_row
< graph
->maxvar
) {
2988 graph
->src_scc
= -1;
2989 graph
->dst_scc
= -1;
2991 if (setup_lp(ctx
, graph
, force_zero
) < 0)
2993 sol
= solve_lp(graph
);
2996 if (sol
->size
== 0) {
2998 if (!ctx
->opt
->schedule_maximize_band_depth
&&
2999 graph
->n_total_row
> graph
->band_start
)
3000 return compute_next_band(ctx
, graph
);
3001 if (graph
->src_scc
>= 0)
3002 return compute_split_schedule(ctx
, graph
);
3003 if (graph
->n_total_row
> graph
->band_start
)
3004 return compute_next_band(ctx
, graph
);
3005 return carry_dependences(ctx
, graph
);
3007 if (update_schedule(graph
, sol
, 1, 1) < 0)
3012 if (graph
->n_total_row
> graph
->band_start
)
3014 return sort_statements(ctx
, graph
);
3017 /* Add a row to the schedules that separates the SCCs and move
3020 static int split_on_scc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3024 if (graph
->n_total_row
>= graph
->max_row
)
3025 isl_die(ctx
, isl_error_internal
,
3026 "too many schedule rows", return -1);
3028 for (i
= 0; i
< graph
->n
; ++i
) {
3029 struct isl_sched_node
*node
= &graph
->node
[i
];
3030 int row
= isl_mat_rows(node
->sched
);
3032 isl_map_free(node
->sched_map
);
3033 node
->sched_map
= NULL
;
3034 node
->sched
= isl_mat_add_zero_rows(node
->sched
, 1);
3035 node
->sched
= isl_mat_set_element_si(node
->sched
, row
, 0,
3039 node
->band
[graph
->n_total_row
] = graph
->n_band
;
3042 graph
->n_total_row
++;
3048 /* Compute a schedule for each component (identified by node->scc)
3049 * of the dependence graph separately and then combine the results.
3050 * Depending on the setting of schedule_fuse, a component may be
3051 * either weakly or strongly connected.
3053 * The band_id is adjusted such that each component has a separate id.
3054 * Note that the band_id may have already been set to a value different
3055 * from zero by compute_split_schedule.
3057 static int compute_component_schedule(isl_ctx
*ctx
,
3058 struct isl_sched_graph
*graph
)
3062 int n_total_row
, orig_total_row
;
3063 int n_band
, orig_band
;
3065 if (ctx
->opt
->schedule_fuse
== ISL_SCHEDULE_FUSE_MIN
||
3066 ctx
->opt
->schedule_separate_components
)
3067 if (split_on_scc(ctx
, graph
) < 0)
3071 orig_total_row
= graph
->n_total_row
;
3073 orig_band
= graph
->n_band
;
3074 for (i
= 0; i
< graph
->n
; ++i
)
3075 graph
->node
[i
].band_id
[graph
->n_band
] += graph
->node
[i
].scc
;
3076 for (wcc
= 0; wcc
< graph
->scc
; ++wcc
) {
3078 for (i
= 0; i
< graph
->n
; ++i
)
3079 if (graph
->node
[i
].scc
== wcc
)
3082 for (i
= 0; i
< graph
->n_edge
; ++i
)
3083 if (graph
->edge
[i
].src
->scc
== wcc
&&
3084 graph
->edge
[i
].dst
->scc
== wcc
)
3087 if (compute_sub_schedule(ctx
, graph
, n
, n_edge
,
3089 &edge_scc_exactly
, wcc
, 1) < 0)
3091 if (graph
->n_total_row
> n_total_row
)
3092 n_total_row
= graph
->n_total_row
;
3093 graph
->n_total_row
= orig_total_row
;
3094 if (graph
->n_band
> n_band
)
3095 n_band
= graph
->n_band
;
3096 graph
->n_band
= orig_band
;
3099 graph
->n_total_row
= n_total_row
;
3100 graph
->n_band
= n_band
;
3102 return pad_schedule(graph
);
3105 /* Compute a schedule for the given dependence graph.
3106 * We first check if the graph is connected (through validity dependences)
3107 * and, if not, compute a schedule for each component separately.
3108 * If schedule_fuse is set to minimal fusion, then we check for strongly
3109 * connected components instead and compute a separate schedule for
3110 * each such strongly connected component.
3112 static int compute_schedule(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3114 if (ctx
->opt
->schedule_fuse
== ISL_SCHEDULE_FUSE_MIN
) {
3115 if (detect_sccs(ctx
, graph
) < 0)
3118 if (detect_wccs(ctx
, graph
) < 0)
3123 return compute_component_schedule(ctx
, graph
);
3125 return compute_schedule_wcc(ctx
, graph
);
3128 /* Compute a schedule on sc->domain that respects the given schedule
3131 * In particular, the schedule respects all the validity dependences.
3132 * If the default isl scheduling algorithm is used, it tries to minimize
3133 * the dependence distances over the proximity dependences.
3134 * If Feautrier's scheduling algorithm is used, the proximity dependence
3135 * distances are only minimized during the extension to a full-dimensional
3138 __isl_give isl_schedule
*isl_schedule_constraints_compute_schedule(
3139 __isl_take isl_schedule_constraints
*sc
)
3141 isl_ctx
*ctx
= isl_schedule_constraints_get_ctx(sc
);
3142 struct isl_sched_graph graph
= { 0 };
3143 isl_schedule
*sched
;
3144 struct isl_extract_edge_data data
;
3145 enum isl_edge_type i
;
3147 sc
= isl_schedule_constraints_align_params(sc
);
3151 graph
.n
= isl_union_set_n_set(sc
->domain
);
3154 if (graph_alloc(ctx
, &graph
, graph
.n
,
3155 isl_schedule_constraints_n_map(sc
)) < 0)
3157 if (compute_max_row(&graph
, sc
->domain
) < 0)
3161 if (isl_union_set_foreach_set(sc
->domain
, &extract_node
, &graph
) < 0)
3163 if (graph_init_table(ctx
, &graph
) < 0)
3165 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
3166 graph
.max_edge
[i
] = isl_union_map_n_map(sc
->constraint
[i
]);
3167 if (graph_init_edge_tables(ctx
, &graph
) < 0)
3170 data
.graph
= &graph
;
3171 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
3173 if (isl_union_map_foreach_map(sc
->constraint
[i
],
3174 &extract_edge
, &data
) < 0)
3178 if (compute_schedule(ctx
, &graph
) < 0)
3182 sched
= extract_schedule(&graph
, isl_union_set_get_space(sc
->domain
));
3184 graph_free(ctx
, &graph
);
3185 isl_schedule_constraints_free(sc
);
3189 graph_free(ctx
, &graph
);
3190 isl_schedule_constraints_free(sc
);
3194 /* Compute a schedule for the given union of domains that respects
3195 * all the validity dependences and minimizes
3196 * the dependence distances over the proximity dependences.
3198 * This function is kept for backward compatibility.
3200 __isl_give isl_schedule
*isl_union_set_compute_schedule(
3201 __isl_take isl_union_set
*domain
,
3202 __isl_take isl_union_map
*validity
,
3203 __isl_take isl_union_map
*proximity
)
3205 isl_schedule_constraints
*sc
;
3207 sc
= isl_schedule_constraints_on_domain(domain
);
3208 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
3209 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
3211 return isl_schedule_constraints_compute_schedule(sc
);
3214 void *isl_schedule_free(__isl_take isl_schedule
*sched
)
3220 if (--sched
->ref
> 0)
3223 for (i
= 0; i
< sched
->n
; ++i
) {
3224 isl_multi_aff_free(sched
->node
[i
].sched
);
3225 free(sched
->node
[i
].band_end
);
3226 free(sched
->node
[i
].band_id
);
3227 free(sched
->node
[i
].zero
);
3229 isl_space_free(sched
->dim
);
3230 isl_band_list_free(sched
->band_forest
);
3235 isl_ctx
*isl_schedule_get_ctx(__isl_keep isl_schedule
*schedule
)
3237 return schedule
? isl_space_get_ctx(schedule
->dim
) : NULL
;
3240 /* Set max_out to the maximal number of output dimensions over
3243 static int update_max_out(__isl_take isl_map
*map
, void *user
)
3245 int *max_out
= user
;
3246 int n_out
= isl_map_dim(map
, isl_dim_out
);
3248 if (n_out
> *max_out
)
3255 /* Internal data structure for map_pad_range.
3257 * "max_out" is the maximal schedule dimension.
3258 * "res" collects the results.
3260 struct isl_pad_schedule_map_data
{
3265 /* Pad the range of the given map with zeros to data->max_out and
3266 * then add the result to data->res.
3268 static int map_pad_range(__isl_take isl_map
*map
, void *user
)
3270 struct isl_pad_schedule_map_data
*data
= user
;
3272 int n_out
= isl_map_dim(map
, isl_dim_out
);
3274 map
= isl_map_add_dims(map
, isl_dim_out
, data
->max_out
- n_out
);
3275 for (i
= n_out
; i
< data
->max_out
; ++i
)
3276 map
= isl_map_fix_si(map
, isl_dim_out
, i
, 0);
3278 data
->res
= isl_union_map_add_map(data
->res
, map
);
3285 /* Pad the ranges of the maps in the union map with zeros such they all have
3286 * the same dimension.
3288 static __isl_give isl_union_map
*pad_schedule_map(
3289 __isl_take isl_union_map
*umap
)
3291 struct isl_pad_schedule_map_data data
;
3295 if (isl_union_map_n_map(umap
) <= 1)
3299 if (isl_union_map_foreach_map(umap
, &update_max_out
, &data
.max_out
) < 0)
3300 return isl_union_map_free(umap
);
3302 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3303 if (isl_union_map_foreach_map(umap
, &map_pad_range
, &data
) < 0)
3304 data
.res
= isl_union_map_free(data
.res
);
3306 isl_union_map_free(umap
);
3310 /* Return an isl_union_map of the schedule. If we have already constructed
3311 * a band forest, then this band forest may have been modified so we need
3312 * to extract the isl_union_map from the forest rather than from
3313 * the originally computed schedule. This reconstructed schedule map
3314 * then needs to be padded with zeros to unify the schedule space
3315 * since the result of isl_band_list_get_suffix_schedule may not have
3316 * a unified schedule space.
3318 __isl_give isl_union_map
*isl_schedule_get_map(__isl_keep isl_schedule
*sched
)
3321 isl_union_map
*umap
;
3326 if (sched
->band_forest
) {
3327 umap
= isl_band_list_get_suffix_schedule(sched
->band_forest
);
3328 return pad_schedule_map(umap
);
3331 umap
= isl_union_map_empty(isl_space_copy(sched
->dim
));
3332 for (i
= 0; i
< sched
->n
; ++i
) {
3335 ma
= isl_multi_aff_copy(sched
->node
[i
].sched
);
3336 umap
= isl_union_map_add_map(umap
, isl_map_from_multi_aff(ma
));
3342 static __isl_give isl_band_list
*construct_band_list(
3343 __isl_keep isl_schedule
*schedule
, __isl_keep isl_band
*parent
,
3344 int band_nr
, int *parent_active
, int n_active
);
3346 /* Construct an isl_band structure for the band in the given schedule
3347 * with sequence number band_nr for the n_active nodes marked by active.
3348 * If the nodes don't have a band with the given sequence number,
3349 * then a band without members is created.
3351 * Because of the way the schedule is constructed, we know that
3352 * the position of the band inside the schedule of a node is the same
3353 * for all active nodes.
3355 * The partial schedule for the band is created before the children
3356 * are created to that construct_band_list can refer to the partial
3357 * schedule of the parent.
3359 static __isl_give isl_band
*construct_band(__isl_keep isl_schedule
*schedule
,
3360 __isl_keep isl_band
*parent
,
3361 int band_nr
, int *active
, int n_active
)
3364 isl_ctx
*ctx
= isl_schedule_get_ctx(schedule
);
3366 unsigned start
, end
;
3368 band
= isl_band_alloc(ctx
);
3372 band
->schedule
= schedule
;
3373 band
->parent
= parent
;
3375 for (i
= 0; i
< schedule
->n
; ++i
)
3379 if (i
>= schedule
->n
)
3380 isl_die(ctx
, isl_error_internal
,
3381 "band without active statements", goto error
);
3383 start
= band_nr
? schedule
->node
[i
].band_end
[band_nr
- 1] : 0;
3384 end
= band_nr
< schedule
->node
[i
].n_band
?
3385 schedule
->node
[i
].band_end
[band_nr
] : start
;
3386 band
->n
= end
- start
;
3388 band
->zero
= isl_alloc_array(ctx
, int, band
->n
);
3389 if (band
->n
&& !band
->zero
)
3392 for (j
= 0; j
< band
->n
; ++j
)
3393 band
->zero
[j
] = schedule
->node
[i
].zero
[start
+ j
];
3395 band
->pma
= isl_union_pw_multi_aff_empty(isl_space_copy(schedule
->dim
));
3396 for (i
= 0; i
< schedule
->n
; ++i
) {
3398 isl_pw_multi_aff
*pma
;
3404 ma
= isl_multi_aff_copy(schedule
->node
[i
].sched
);
3405 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
3406 ma
= isl_multi_aff_drop_dims(ma
, isl_dim_out
, end
, n_out
- end
);
3407 ma
= isl_multi_aff_drop_dims(ma
, isl_dim_out
, 0, start
);
3408 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
3409 band
->pma
= isl_union_pw_multi_aff_add_pw_multi_aff(band
->pma
,
3415 for (i
= 0; i
< schedule
->n
; ++i
)
3416 if (active
[i
] && schedule
->node
[i
].n_band
> band_nr
+ 1)
3419 if (i
< schedule
->n
) {
3420 band
->children
= construct_band_list(schedule
, band
,
3421 band_nr
+ 1, active
, n_active
);
3422 if (!band
->children
)
3428 isl_band_free(band
);
3432 /* Internal data structure used inside cmp_band and pw_multi_aff_extract_int.
3434 * r is set to a negative value if anything goes wrong.
3436 * c1 stores the result of extract_int.
3437 * c2 is a temporary value used inside cmp_band_in_ancestor.
3438 * t is a temporary value used inside extract_int.
3440 * first and equal are used inside extract_int.
3441 * first is set if we are looking at the first isl_multi_aff inside
3442 * the isl_union_pw_multi_aff.
3443 * equal is set if all the isl_multi_affs have been equal so far.
3445 struct isl_cmp_band_data
{
3456 /* Check if "ma" assigns a constant value.
3457 * Note that this function is only called on isl_multi_affs
3458 * with a single output dimension.
3460 * If "ma" assigns a constant value then we compare it to data->c1
3461 * or assign it to data->c1 if this is the first isl_multi_aff we consider.
3462 * If "ma" does not assign a constant value or if it assigns a value
3463 * that is different from data->c1, then we set data->equal to zero
3464 * and terminate the check.
3466 static int multi_aff_extract_int(__isl_take isl_set
*set
,
3467 __isl_take isl_multi_aff
*ma
, void *user
)
3470 struct isl_cmp_band_data
*data
= user
;
3472 aff
= isl_multi_aff_get_aff(ma
, 0);
3473 data
->r
= isl_aff_is_cst(aff
);
3474 if (data
->r
>= 0 && data
->r
) {
3475 isl_aff_get_constant(aff
, &data
->t
);
3477 isl_int_set(data
->c1
, data
->t
);
3479 } else if (!isl_int_eq(data
->c1
, data
->t
))
3481 } else if (data
->r
>= 0 && !data
->r
)
3486 isl_multi_aff_free(ma
);
3495 /* This function is called for each isl_pw_multi_aff in
3496 * the isl_union_pw_multi_aff checked by extract_int.
3497 * Check all the isl_multi_affs inside "pma".
3499 static int pw_multi_aff_extract_int(__isl_take isl_pw_multi_aff
*pma
,
3504 r
= isl_pw_multi_aff_foreach_piece(pma
, &multi_aff_extract_int
, user
);
3505 isl_pw_multi_aff_free(pma
);
3510 /* Check if "upma" assigns a single constant value to its domain.
3511 * If so, return 1 and store the result in data->c1.
3514 * A negative return value from isl_union_pw_multi_aff_foreach_pw_multi_aff
3515 * means that either an error occurred or that we have broken off the check
3516 * because we already know the result is going to be negative.
3517 * In the latter case, data->equal is set to zero.
3519 static int extract_int(__isl_keep isl_union_pw_multi_aff
*upma
,
3520 struct isl_cmp_band_data
*data
)
3525 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
3526 &pw_multi_aff_extract_int
, data
) < 0) {
3532 return !data
->first
&& data
->equal
;
3535 /* Compare "b1" and "b2" based on the parent schedule of their ancestor
3538 * If the parent of "ancestor" also has a single member, then we
3539 * first try to compare the two band based on the partial schedule
3542 * Otherwise, or if the result is inconclusive, we look at the partial schedule
3543 * of "ancestor" itself.
3544 * In particular, we specialize the parent schedule based
3545 * on the domains of the child schedules, check if both assign
3546 * a single constant value and, if so, compare the two constant values.
3547 * If the specialized parent schedules do not assign a constant value,
3548 * then they cannot be used to order the two bands and so in this case
3551 static int cmp_band_in_ancestor(__isl_keep isl_band
*b1
,
3552 __isl_keep isl_band
*b2
, struct isl_cmp_band_data
*data
,
3553 __isl_keep isl_band
*ancestor
)
3555 isl_union_pw_multi_aff
*upma
;
3556 isl_union_set
*domain
;
3562 if (ancestor
->parent
&& ancestor
->parent
->n
== 1) {
3563 r
= cmp_band_in_ancestor(b1
, b2
, data
, ancestor
->parent
);
3570 upma
= isl_union_pw_multi_aff_copy(b1
->pma
);
3571 domain
= isl_union_pw_multi_aff_domain(upma
);
3572 upma
= isl_union_pw_multi_aff_copy(ancestor
->pma
);
3573 upma
= isl_union_pw_multi_aff_intersect_domain(upma
, domain
);
3574 r
= extract_int(upma
, data
);
3575 isl_union_pw_multi_aff_free(upma
);
3582 isl_int_set(data
->c2
, data
->c1
);
3584 upma
= isl_union_pw_multi_aff_copy(b2
->pma
);
3585 domain
= isl_union_pw_multi_aff_domain(upma
);
3586 upma
= isl_union_pw_multi_aff_copy(ancestor
->pma
);
3587 upma
= isl_union_pw_multi_aff_intersect_domain(upma
, domain
);
3588 r
= extract_int(upma
, data
);
3589 isl_union_pw_multi_aff_free(upma
);
3596 return isl_int_cmp(data
->c2
, data
->c1
);
3599 /* Compare "a" and "b" based on the parent schedule of their parent.
3601 static int cmp_band(const void *a
, const void *b
, void *user
)
3603 isl_band
*b1
= *(isl_band
* const *) a
;
3604 isl_band
*b2
= *(isl_band
* const *) b
;
3605 struct isl_cmp_band_data
*data
= user
;
3607 return cmp_band_in_ancestor(b1
, b2
, data
, b1
->parent
);
3610 /* Sort the elements in "list" based on the partial schedules of its parent
3611 * (and ancestors). In particular if the parent assigns constant values
3612 * to the domains of the bands in "list", then the elements are sorted
3613 * according to that order.
3614 * This order should be a more "natural" order for the user, but otherwise
3615 * shouldn't have any effect.
3616 * If we would be constructing an isl_band forest directly in
3617 * isl_schedule_constraints_compute_schedule then there wouldn't be any need
3618 * for a reordering, since the children would be added to the list
3619 * in their natural order automatically.
3621 * If there is only one element in the list, then there is no need to sort
3623 * If the partial schedule of the parent has more than one member
3624 * (or if there is no parent), then it's
3625 * defnitely not assigning constant values to the different children in
3626 * the list and so we wouldn't be able to use it to sort the list.
3628 static __isl_give isl_band_list
*sort_band_list(__isl_take isl_band_list
*list
,
3629 __isl_keep isl_band
*parent
)
3631 struct isl_cmp_band_data data
;
3637 if (!parent
|| parent
->n
!= 1)
3641 isl_int_init(data
.c1
);
3642 isl_int_init(data
.c2
);
3643 isl_int_init(data
.t
);
3644 isl_sort(list
->p
, list
->n
, sizeof(list
->p
[0]), &cmp_band
, &data
);
3646 list
= isl_band_list_free(list
);
3647 isl_int_clear(data
.c1
);
3648 isl_int_clear(data
.c2
);
3649 isl_int_clear(data
.t
);
3654 /* Construct a list of bands that start at the same position (with
3655 * sequence number band_nr) in the schedules of the nodes that
3656 * were active in the parent band.
3658 * A separate isl_band structure is created for each band_id
3659 * and for each node that does not have a band with sequence
3660 * number band_nr. In the latter case, a band without members
3662 * This ensures that if a band has any children, then each node
3663 * that was active in the band is active in exactly one of the children.
3665 static __isl_give isl_band_list
*construct_band_list(
3666 __isl_keep isl_schedule
*schedule
, __isl_keep isl_band
*parent
,
3667 int band_nr
, int *parent_active
, int n_active
)
3670 isl_ctx
*ctx
= isl_schedule_get_ctx(schedule
);
3673 isl_band_list
*list
;
3676 for (i
= 0; i
< n_active
; ++i
) {
3677 for (j
= 0; j
< schedule
->n
; ++j
) {
3678 if (!parent_active
[j
])
3680 if (schedule
->node
[j
].n_band
<= band_nr
)
3682 if (schedule
->node
[j
].band_id
[band_nr
] == i
) {
3688 for (j
= 0; j
< schedule
->n
; ++j
)
3689 if (schedule
->node
[j
].n_band
<= band_nr
)
3694 list
= isl_band_list_alloc(ctx
, n_band
);
3695 band
= construct_band(schedule
, parent
, band_nr
,
3696 parent_active
, n_active
);
3697 return isl_band_list_add(list
, band
);
3700 active
= isl_alloc_array(ctx
, int, schedule
->n
);
3701 if (schedule
->n
&& !active
)
3704 list
= isl_band_list_alloc(ctx
, n_band
);
3706 for (i
= 0; i
< n_active
; ++i
) {
3710 for (j
= 0; j
< schedule
->n
; ++j
) {
3711 active
[j
] = parent_active
[j
] &&
3712 schedule
->node
[j
].n_band
> band_nr
&&
3713 schedule
->node
[j
].band_id
[band_nr
] == i
;
3720 band
= construct_band(schedule
, parent
, band_nr
, active
, n
);
3722 list
= isl_band_list_add(list
, band
);
3724 for (i
= 0; i
< schedule
->n
; ++i
) {
3726 if (!parent_active
[i
])
3728 if (schedule
->node
[i
].n_band
> band_nr
)
3730 for (j
= 0; j
< schedule
->n
; ++j
)
3732 band
= construct_band(schedule
, parent
, band_nr
, active
, 1);
3733 list
= isl_band_list_add(list
, band
);
3738 list
= sort_band_list(list
, parent
);
3743 /* Construct a band forest representation of the schedule and
3744 * return the list of roots.
3746 static __isl_give isl_band_list
*construct_forest(
3747 __isl_keep isl_schedule
*schedule
)
3750 isl_ctx
*ctx
= isl_schedule_get_ctx(schedule
);
3751 isl_band_list
*forest
;
3754 active
= isl_alloc_array(ctx
, int, schedule
->n
);
3755 if (schedule
->n
&& !active
)
3758 for (i
= 0; i
< schedule
->n
; ++i
)
3761 forest
= construct_band_list(schedule
, NULL
, 0, active
, schedule
->n
);
3768 /* Return the roots of a band forest representation of the schedule.
3770 __isl_give isl_band_list
*isl_schedule_get_band_forest(
3771 __isl_keep isl_schedule
*schedule
)
3775 if (!schedule
->band_forest
)
3776 schedule
->band_forest
= construct_forest(schedule
);
3777 return isl_band_list_dup(schedule
->band_forest
);
3780 /* Call "fn" on each band in the schedule in depth-first post-order.
3782 int isl_schedule_foreach_band(__isl_keep isl_schedule
*sched
,
3783 int (*fn
)(__isl_keep isl_band
*band
, void *user
), void *user
)
3786 isl_band_list
*forest
;
3791 forest
= isl_schedule_get_band_forest(sched
);
3792 r
= isl_band_list_foreach_band(forest
, fn
, user
);
3793 isl_band_list_free(forest
);
3798 static __isl_give isl_printer
*print_band_list(__isl_take isl_printer
*p
,
3799 __isl_keep isl_band_list
*list
);
3801 static __isl_give isl_printer
*print_band(__isl_take isl_printer
*p
,
3802 __isl_keep isl_band
*band
)
3804 isl_band_list
*children
;
3806 p
= isl_printer_start_line(p
);
3807 p
= isl_printer_print_union_pw_multi_aff(p
, band
->pma
);
3808 p
= isl_printer_end_line(p
);
3810 if (!isl_band_has_children(band
))
3813 children
= isl_band_get_children(band
);
3815 p
= isl_printer_indent(p
, 4);
3816 p
= print_band_list(p
, children
);
3817 p
= isl_printer_indent(p
, -4);
3819 isl_band_list_free(children
);
3824 static __isl_give isl_printer
*print_band_list(__isl_take isl_printer
*p
,
3825 __isl_keep isl_band_list
*list
)
3829 n
= isl_band_list_n_band(list
);
3830 for (i
= 0; i
< n
; ++i
) {
3832 band
= isl_band_list_get_band(list
, i
);
3833 p
= print_band(p
, band
);
3834 isl_band_free(band
);
3840 __isl_give isl_printer
*isl_printer_print_schedule(__isl_take isl_printer
*p
,
3841 __isl_keep isl_schedule
*schedule
)
3843 isl_band_list
*forest
;
3845 forest
= isl_schedule_get_band_forest(schedule
);
3847 p
= print_band_list(p
, forest
);
3849 isl_band_list_free(forest
);
3854 void isl_schedule_dump(__isl_keep isl_schedule
*schedule
)
3856 isl_printer
*printer
;
3861 printer
= isl_printer_to_file(isl_schedule_get_ctx(schedule
), stderr
);
3862 printer
= isl_printer_print_schedule(printer
, schedule
);
3864 isl_printer_free(printer
);