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 coincidence constraints of "sc" by "coincidence".
96 __isl_give isl_schedule_constraints
*isl_schedule_constraints_set_coincidence(
97 __isl_take isl_schedule_constraints
*sc
,
98 __isl_take isl_union_map
*coincidence
)
100 if (!sc
|| !coincidence
)
103 isl_union_map_free(sc
->constraint
[isl_edge_coincidence
]);
104 sc
->constraint
[isl_edge_coincidence
] = coincidence
;
108 isl_schedule_constraints_free(sc
);
109 isl_union_map_free(coincidence
);
113 /* Replace the proximity constraints of "sc" by "proximity".
115 __isl_give isl_schedule_constraints
*isl_schedule_constraints_set_proximity(
116 __isl_take isl_schedule_constraints
*sc
,
117 __isl_take isl_union_map
*proximity
)
119 if (!sc
|| !proximity
)
122 isl_union_map_free(sc
->constraint
[isl_edge_proximity
]);
123 sc
->constraint
[isl_edge_proximity
] = proximity
;
127 isl_schedule_constraints_free(sc
);
128 isl_union_map_free(proximity
);
132 /* Replace the conditional validity constraints of "sc" by "condition"
135 __isl_give isl_schedule_constraints
*
136 isl_schedule_constraints_set_conditional_validity(
137 __isl_take isl_schedule_constraints
*sc
,
138 __isl_take isl_union_map
*condition
,
139 __isl_take isl_union_map
*validity
)
141 if (!sc
|| !condition
|| !validity
)
144 isl_union_map_free(sc
->constraint
[isl_edge_condition
]);
145 sc
->constraint
[isl_edge_condition
] = condition
;
146 isl_union_map_free(sc
->constraint
[isl_edge_conditional_validity
]);
147 sc
->constraint
[isl_edge_conditional_validity
] = validity
;
151 isl_schedule_constraints_free(sc
);
152 isl_union_map_free(condition
);
153 isl_union_map_free(validity
);
157 void *isl_schedule_constraints_free(__isl_take isl_schedule_constraints
*sc
)
159 enum isl_edge_type i
;
164 isl_union_set_free(sc
->domain
);
165 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
166 isl_union_map_free(sc
->constraint
[i
]);
173 isl_ctx
*isl_schedule_constraints_get_ctx(
174 __isl_keep isl_schedule_constraints
*sc
)
176 return sc
? isl_union_set_get_ctx(sc
->domain
) : NULL
;
179 void isl_schedule_constraints_dump(__isl_keep isl_schedule_constraints
*sc
)
184 fprintf(stderr
, "domain: ");
185 isl_union_set_dump(sc
->domain
);
186 fprintf(stderr
, "validity: ");
187 isl_union_map_dump(sc
->constraint
[isl_edge_validity
]);
188 fprintf(stderr
, "proximity: ");
189 isl_union_map_dump(sc
->constraint
[isl_edge_proximity
]);
190 fprintf(stderr
, "coincidence: ");
191 isl_union_map_dump(sc
->constraint
[isl_edge_coincidence
]);
192 fprintf(stderr
, "condition: ");
193 isl_union_map_dump(sc
->constraint
[isl_edge_condition
]);
194 fprintf(stderr
, "conditional_validity: ");
195 isl_union_map_dump(sc
->constraint
[isl_edge_conditional_validity
]);
198 /* Align the parameters of the fields of "sc".
200 static __isl_give isl_schedule_constraints
*
201 isl_schedule_constraints_align_params(__isl_take isl_schedule_constraints
*sc
)
204 enum isl_edge_type i
;
209 space
= isl_union_set_get_space(sc
->domain
);
210 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
211 space
= isl_space_align_params(space
,
212 isl_union_map_get_space(sc
->constraint
[i
]));
214 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
215 sc
->constraint
[i
] = isl_union_map_align_params(
216 sc
->constraint
[i
], isl_space_copy(space
));
217 if (!sc
->constraint
[i
])
218 space
= isl_space_free(space
);
220 sc
->domain
= isl_union_set_align_params(sc
->domain
, space
);
222 return isl_schedule_constraints_free(sc
);
227 /* Return the total number of isl_maps in the constraints of "sc".
229 static __isl_give
int isl_schedule_constraints_n_map(
230 __isl_keep isl_schedule_constraints
*sc
)
232 enum isl_edge_type i
;
235 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
236 n
+= isl_union_map_n_map(sc
->constraint
[i
]);
241 /* Internal information about a node that is used during the construction
243 * dim represents the space in which the domain lives
244 * sched is a matrix representation of the schedule being constructed
246 * sched_map is an isl_map representation of the same (partial) schedule
247 * sched_map may be NULL
248 * rank is the number of linearly independent rows in the linear part
250 * the columns of cmap represent a change of basis for the schedule
251 * coefficients; the first rank columns span the linear part of
253 * cinv is the inverse of cmap.
254 * start is the first variable in the LP problem in the sequences that
255 * represents the schedule coefficients of this node
256 * nvar is the dimension of the domain
257 * nparam is the number of parameters or 0 if we are not constructing
258 * a parametric schedule
260 * scc is the index of SCC (or WCC) this node belongs to
262 * band contains the band index for each of the rows of the schedule.
263 * band_id is used to differentiate between separate bands at the same
264 * level within the same parent band, i.e., bands that are separated
265 * by the parent band or bands that are independent of each other.
266 * coincident contains a boolean for each of the rows of the schedule,
267 * indicating whether the corresponding scheduling dimension satisfies
268 * the coincidence constraints in the sense that the corresponding
269 * dependence distances are zero.
271 struct isl_sched_node
{
289 static int node_has_dim(const void *entry
, const void *val
)
291 struct isl_sched_node
*node
= (struct isl_sched_node
*)entry
;
292 isl_space
*dim
= (isl_space
*)val
;
294 return isl_space_is_equal(node
->dim
, dim
);
297 /* An edge in the dependence graph. An edge may be used to
298 * ensure validity of the generated schedule, to minimize the dependence
301 * map is the dependence relation, with i -> j in the map if j depends on i
302 * tagged_condition and tagged_validity contain the union of all tagged
303 * condition or conditional validity dependence relations that
304 * specialize the dependence relation "map"; that is,
305 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
306 * or "tagged_validity", then i -> j is an element of "map".
307 * If these fields are NULL, then they represent the empty relation.
308 * src is the source node
309 * dst is the sink node
310 * validity is set if the edge is used to ensure correctness
311 * coincidence is used to enforce zero dependence distances
312 * proximity is set if the edge is used to minimize dependence distances
313 * condition is set if the edge represents a condition
314 * for a conditional validity schedule constraint
315 * local can only be set for condition edges and indicates that
316 * the dependence distance over the edge should be zero
317 * conditional_validity is set if the edge is used to conditionally
320 * For validity edges, start and end mark the sequence of inequality
321 * constraints in the LP problem that encode the validity constraint
322 * corresponding to this edge.
324 struct isl_sched_edge
{
326 isl_union_map
*tagged_condition
;
327 isl_union_map
*tagged_validity
;
329 struct isl_sched_node
*src
;
330 struct isl_sched_node
*dst
;
332 unsigned validity
: 1;
333 unsigned coincidence
: 1;
334 unsigned proximity
: 1;
336 unsigned condition
: 1;
337 unsigned conditional_validity
: 1;
343 /* Internal information about the dependence graph used during
344 * the construction of the schedule.
346 * intra_hmap is a cache, mapping dependence relations to their dual,
347 * for dependences from a node to itself
348 * inter_hmap is a cache, mapping dependence relations to their dual,
349 * for dependences between distinct nodes
351 * n is the number of nodes
352 * node is the list of nodes
353 * maxvar is the maximal number of variables over all nodes
354 * max_row is the allocated number of rows in the schedule
355 * n_row is the current (maximal) number of linearly independent
356 * rows in the node schedules
357 * n_total_row is the current number of rows in the node schedules
358 * n_band is the current number of completed bands
359 * band_start is the starting row in the node schedules of the current band
360 * root is set if this graph is the original dependence graph,
361 * without any splitting
363 * sorted contains a list of node indices sorted according to the
364 * SCC to which a node belongs
366 * n_edge is the number of edges
367 * edge is the list of edges
368 * max_edge contains the maximal number of edges of each type;
369 * in particular, it contains the number of edges in the inital graph.
370 * edge_table contains pointers into the edge array, hashed on the source
371 * and sink spaces; there is one such table for each type;
372 * a given edge may be referenced from more than one table
373 * if the corresponding relation appears in more than of the
374 * sets of dependences
376 * node_table contains pointers into the node array, hashed on the space
378 * region contains a list of variable sequences that should be non-trivial
380 * lp contains the (I)LP problem used to obtain new schedule rows
382 * src_scc and dst_scc are the source and sink SCCs of an edge with
383 * conflicting constraints
385 * scc represents the number of components
387 struct isl_sched_graph
{
388 isl_map_to_basic_set
*intra_hmap
;
389 isl_map_to_basic_set
*inter_hmap
;
391 struct isl_sched_node
*node
;
405 struct isl_sched_edge
*edge
;
407 int max_edge
[isl_edge_last
+ 1];
408 struct isl_hash_table
*edge_table
[isl_edge_last
+ 1];
410 struct isl_hash_table
*node_table
;
411 struct isl_region
*region
;
421 /* Initialize node_table based on the list of nodes.
423 static int graph_init_table(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
427 graph
->node_table
= isl_hash_table_alloc(ctx
, graph
->n
);
428 if (!graph
->node_table
)
431 for (i
= 0; i
< graph
->n
; ++i
) {
432 struct isl_hash_table_entry
*entry
;
435 hash
= isl_space_get_hash(graph
->node
[i
].dim
);
436 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
438 graph
->node
[i
].dim
, 1);
441 entry
->data
= &graph
->node
[i
];
447 /* Return a pointer to the node that lives within the given space,
448 * or NULL if there is no such node.
450 static struct isl_sched_node
*graph_find_node(isl_ctx
*ctx
,
451 struct isl_sched_graph
*graph
, __isl_keep isl_space
*dim
)
453 struct isl_hash_table_entry
*entry
;
456 hash
= isl_space_get_hash(dim
);
457 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
458 &node_has_dim
, dim
, 0);
460 return entry
? entry
->data
: NULL
;
463 static int edge_has_src_and_dst(const void *entry
, const void *val
)
465 const struct isl_sched_edge
*edge
= entry
;
466 const struct isl_sched_edge
*temp
= val
;
468 return edge
->src
== temp
->src
&& edge
->dst
== temp
->dst
;
471 /* Add the given edge to graph->edge_table[type].
473 static int graph_edge_table_add(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
474 enum isl_edge_type type
, struct isl_sched_edge
*edge
)
476 struct isl_hash_table_entry
*entry
;
479 hash
= isl_hash_init();
480 hash
= isl_hash_builtin(hash
, edge
->src
);
481 hash
= isl_hash_builtin(hash
, edge
->dst
);
482 entry
= isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
483 &edge_has_src_and_dst
, edge
, 1);
491 /* Allocate the edge_tables based on the maximal number of edges of
494 static int graph_init_edge_tables(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
498 for (i
= 0; i
<= isl_edge_last
; ++i
) {
499 graph
->edge_table
[i
] = isl_hash_table_alloc(ctx
,
501 if (!graph
->edge_table
[i
])
508 /* If graph->edge_table[type] contains an edge from the given source
509 * to the given destination, then return the hash table entry of this edge.
510 * Otherwise, return NULL.
512 static struct isl_hash_table_entry
*graph_find_edge_entry(
513 struct isl_sched_graph
*graph
,
514 enum isl_edge_type type
,
515 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
517 isl_ctx
*ctx
= isl_space_get_ctx(src
->dim
);
519 struct isl_sched_edge temp
= { .src
= src
, .dst
= dst
};
521 hash
= isl_hash_init();
522 hash
= isl_hash_builtin(hash
, temp
.src
);
523 hash
= isl_hash_builtin(hash
, temp
.dst
);
524 return isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
525 &edge_has_src_and_dst
, &temp
, 0);
529 /* If graph->edge_table[type] contains an edge from the given source
530 * to the given destination, then return this edge.
531 * Otherwise, return NULL.
533 static struct isl_sched_edge
*graph_find_edge(struct isl_sched_graph
*graph
,
534 enum isl_edge_type type
,
535 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
537 struct isl_hash_table_entry
*entry
;
539 entry
= graph_find_edge_entry(graph
, type
, src
, dst
);
546 /* Check whether the dependence graph has an edge of the given type
547 * between the given two nodes.
549 static int graph_has_edge(struct isl_sched_graph
*graph
,
550 enum isl_edge_type type
,
551 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
553 struct isl_sched_edge
*edge
;
556 edge
= graph_find_edge(graph
, type
, src
, dst
);
560 empty
= isl_map_plain_is_empty(edge
->map
);
567 /* Look for any edge with the same src, dst and map fields as "model".
569 * Return the matching edge if one can be found.
570 * Return "model" if no matching edge is found.
571 * Return NULL on error.
573 static struct isl_sched_edge
*graph_find_matching_edge(
574 struct isl_sched_graph
*graph
, struct isl_sched_edge
*model
)
576 enum isl_edge_type i
;
577 struct isl_sched_edge
*edge
;
579 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
582 edge
= graph_find_edge(graph
, i
, model
->src
, model
->dst
);
585 is_equal
= isl_map_plain_is_equal(model
->map
, edge
->map
);
595 /* Remove the given edge from all the edge_tables that refer to it.
597 static void graph_remove_edge(struct isl_sched_graph
*graph
,
598 struct isl_sched_edge
*edge
)
600 isl_ctx
*ctx
= isl_map_get_ctx(edge
->map
);
601 enum isl_edge_type i
;
603 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
604 struct isl_hash_table_entry
*entry
;
606 entry
= graph_find_edge_entry(graph
, i
, edge
->src
, edge
->dst
);
609 if (entry
->data
!= edge
)
611 isl_hash_table_remove(ctx
, graph
->edge_table
[i
], entry
);
615 /* Check whether the dependence graph has any edge
616 * between the given two nodes.
618 static int graph_has_any_edge(struct isl_sched_graph
*graph
,
619 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
621 enum isl_edge_type i
;
624 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
625 r
= graph_has_edge(graph
, i
, src
, dst
);
633 /* Check whether the dependence graph has a validity edge
634 * between the given two nodes.
636 * Conditional validity edges are essentially validity edges that
637 * can be ignored if the corresponding condition edges are iteration private.
638 * Here, we are only checking for the presence of validity
639 * edges, so we need to consider the conditional validity edges too.
640 * In particular, this function is used during the detection
641 * of strongly connected components and we cannot ignore
642 * conditional validity edges during this detection.
644 static int graph_has_validity_edge(struct isl_sched_graph
*graph
,
645 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
649 r
= graph_has_edge(graph
, isl_edge_validity
, src
, dst
);
653 return graph_has_edge(graph
, isl_edge_conditional_validity
, src
, dst
);
656 static int graph_alloc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
657 int n_node
, int n_edge
)
662 graph
->n_edge
= n_edge
;
663 graph
->node
= isl_calloc_array(ctx
, struct isl_sched_node
, graph
->n
);
664 graph
->sorted
= isl_calloc_array(ctx
, int, graph
->n
);
665 graph
->region
= isl_alloc_array(ctx
, struct isl_region
, graph
->n
);
666 graph
->edge
= isl_calloc_array(ctx
,
667 struct isl_sched_edge
, graph
->n_edge
);
669 graph
->intra_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
670 graph
->inter_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
672 if (!graph
->node
|| !graph
->region
|| (graph
->n_edge
&& !graph
->edge
) ||
676 for(i
= 0; i
< graph
->n
; ++i
)
677 graph
->sorted
[i
] = i
;
682 static void graph_free(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
686 isl_map_to_basic_set_free(graph
->intra_hmap
);
687 isl_map_to_basic_set_free(graph
->inter_hmap
);
689 for (i
= 0; i
< graph
->n
; ++i
) {
690 isl_space_free(graph
->node
[i
].dim
);
691 isl_mat_free(graph
->node
[i
].sched
);
692 isl_map_free(graph
->node
[i
].sched_map
);
693 isl_mat_free(graph
->node
[i
].cmap
);
694 isl_mat_free(graph
->node
[i
].cinv
);
696 free(graph
->node
[i
].band
);
697 free(graph
->node
[i
].band_id
);
698 free(graph
->node
[i
].coincident
);
703 for (i
= 0; i
< graph
->n_edge
; ++i
) {
704 isl_map_free(graph
->edge
[i
].map
);
705 isl_union_map_free(graph
->edge
[i
].tagged_condition
);
706 isl_union_map_free(graph
->edge
[i
].tagged_validity
);
710 for (i
= 0; i
<= isl_edge_last
; ++i
)
711 isl_hash_table_free(ctx
, graph
->edge_table
[i
]);
712 isl_hash_table_free(ctx
, graph
->node_table
);
713 isl_basic_set_free(graph
->lp
);
716 /* For each "set" on which this function is called, increment
717 * graph->n by one and update graph->maxvar.
719 static int init_n_maxvar(__isl_take isl_set
*set
, void *user
)
721 struct isl_sched_graph
*graph
= user
;
722 int nvar
= isl_set_dim(set
, isl_dim_set
);
725 if (nvar
> graph
->maxvar
)
726 graph
->maxvar
= nvar
;
733 /* Compute the number of rows that should be allocated for the schedule.
734 * The graph can be split at most "n - 1" times, there can be at most
735 * two rows for each dimension in the iteration domains (in particular,
736 * we usually have one row, but it may be split by split_scaled),
737 * and there can be one extra row for ordering the statements.
738 * Note that if we have actually split "n - 1" times, then no ordering
739 * is needed, so in principle we could use "graph->n + 2 * graph->maxvar - 1".
741 static int compute_max_row(struct isl_sched_graph
*graph
,
742 __isl_keep isl_union_set
*domain
)
746 if (isl_union_set_foreach_set(domain
, &init_n_maxvar
, graph
) < 0)
748 graph
->max_row
= graph
->n
+ 2 * graph
->maxvar
;
753 /* Add a new node to the graph representing the given set.
755 static int extract_node(__isl_take isl_set
*set
, void *user
)
761 struct isl_sched_graph
*graph
= user
;
762 int *band
, *band_id
, *coincident
;
764 ctx
= isl_set_get_ctx(set
);
765 dim
= isl_set_get_space(set
);
767 nvar
= isl_space_dim(dim
, isl_dim_set
);
768 nparam
= isl_space_dim(dim
, isl_dim_param
);
769 if (!ctx
->opt
->schedule_parametric
)
771 sched
= isl_mat_alloc(ctx
, 0, 1 + nparam
+ nvar
);
772 graph
->node
[graph
->n
].dim
= dim
;
773 graph
->node
[graph
->n
].nvar
= nvar
;
774 graph
->node
[graph
->n
].nparam
= nparam
;
775 graph
->node
[graph
->n
].sched
= sched
;
776 graph
->node
[graph
->n
].sched_map
= NULL
;
777 band
= isl_alloc_array(ctx
, int, graph
->max_row
);
778 graph
->node
[graph
->n
].band
= band
;
779 band_id
= isl_calloc_array(ctx
, int, graph
->max_row
);
780 graph
->node
[graph
->n
].band_id
= band_id
;
781 coincident
= isl_calloc_array(ctx
, int, graph
->max_row
);
782 graph
->node
[graph
->n
].coincident
= coincident
;
785 if (!sched
|| (graph
->max_row
&& (!band
|| !band_id
|| !coincident
)))
791 struct isl_extract_edge_data
{
792 enum isl_edge_type type
;
793 struct isl_sched_graph
*graph
;
796 /* Merge edge2 into edge1, freeing the contents of edge2.
797 * "type" is the type of the schedule constraint from which edge2 was
799 * Return 0 on success and -1 on failure.
801 * edge1 and edge2 are assumed to have the same value for the map field.
803 static int merge_edge(enum isl_edge_type type
, struct isl_sched_edge
*edge1
,
804 struct isl_sched_edge
*edge2
)
806 edge1
->validity
|= edge2
->validity
;
807 edge1
->coincidence
|= edge2
->coincidence
;
808 edge1
->proximity
|= edge2
->proximity
;
809 edge1
->condition
|= edge2
->condition
;
810 edge1
->conditional_validity
|= edge2
->conditional_validity
;
811 isl_map_free(edge2
->map
);
813 if (type
== isl_edge_condition
) {
814 if (!edge1
->tagged_condition
)
815 edge1
->tagged_condition
= edge2
->tagged_condition
;
817 edge1
->tagged_condition
=
818 isl_union_map_union(edge1
->tagged_condition
,
819 edge2
->tagged_condition
);
822 if (type
== isl_edge_conditional_validity
) {
823 if (!edge1
->tagged_validity
)
824 edge1
->tagged_validity
= edge2
->tagged_validity
;
826 edge1
->tagged_validity
=
827 isl_union_map_union(edge1
->tagged_validity
,
828 edge2
->tagged_validity
);
831 if (type
== isl_edge_condition
&& !edge1
->tagged_condition
)
833 if (type
== isl_edge_conditional_validity
&& !edge1
->tagged_validity
)
839 /* Insert dummy tags in domain and range of "map".
841 * In particular, if "map" is of the form
847 * [A -> dummy_tag] -> [B -> dummy_tag]
849 * where the dummy_tags are identical and equal to any dummy tags
850 * introduced by any other call to this function.
852 static __isl_give isl_map
*insert_dummy_tags(__isl_take isl_map
*map
)
858 isl_set
*domain
, *range
;
860 ctx
= isl_map_get_ctx(map
);
862 id
= isl_id_alloc(ctx
, NULL
, &dummy
);
863 space
= isl_space_params(isl_map_get_space(map
));
864 space
= isl_space_set_from_params(space
);
865 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
866 space
= isl_space_map_from_set(space
);
868 domain
= isl_map_wrap(map
);
869 range
= isl_map_wrap(isl_map_universe(space
));
870 map
= isl_map_from_domain_and_range(domain
, range
);
871 map
= isl_map_zip(map
);
876 /* Add a new edge to the graph based on the given map
877 * and add it to data->graph->edge_table[data->type].
878 * If a dependence relation of a given type happens to be identical
879 * to one of the dependence relations of a type that was added before,
880 * then we don't create a new edge, but instead mark the original edge
881 * as also representing a dependence of the current type.
883 * Edges of type isl_edge_condition or isl_edge_conditional_validity
884 * may be specified as "tagged" dependence relations. That is, "map"
885 * may contain elements * (i -> a) -> (j -> b), where i -> j denotes
886 * the dependence on iterations and a and b are tags.
887 * edge->map is set to the relation containing the elements i -> j,
888 * while edge->tagged_condition and edge->tagged_validity contain
889 * the union of all the "map" relations
890 * for which extract_edge is called that result in the same edge->map.
892 static int extract_edge(__isl_take isl_map
*map
, void *user
)
894 isl_ctx
*ctx
= isl_map_get_ctx(map
);
895 struct isl_extract_edge_data
*data
= user
;
896 struct isl_sched_graph
*graph
= data
->graph
;
897 struct isl_sched_node
*src
, *dst
;
899 struct isl_sched_edge
*edge
;
900 isl_map
*tagged
= NULL
;
902 if (data
->type
== isl_edge_condition
||
903 data
->type
== isl_edge_conditional_validity
) {
904 if (isl_map_can_zip(map
)) {
905 tagged
= isl_map_copy(map
);
906 map
= isl_set_unwrap(isl_map_domain(isl_map_zip(map
)));
908 tagged
= insert_dummy_tags(isl_map_copy(map
));
912 dim
= isl_space_domain(isl_map_get_space(map
));
913 src
= graph_find_node(ctx
, graph
, dim
);
915 dim
= isl_space_range(isl_map_get_space(map
));
916 dst
= graph_find_node(ctx
, graph
, dim
);
921 isl_map_free(tagged
);
925 graph
->edge
[graph
->n_edge
].src
= src
;
926 graph
->edge
[graph
->n_edge
].dst
= dst
;
927 graph
->edge
[graph
->n_edge
].map
= map
;
928 graph
->edge
[graph
->n_edge
].validity
= 0;
929 graph
->edge
[graph
->n_edge
].coincidence
= 0;
930 graph
->edge
[graph
->n_edge
].proximity
= 0;
931 graph
->edge
[graph
->n_edge
].condition
= 0;
932 graph
->edge
[graph
->n_edge
].local
= 0;
933 graph
->edge
[graph
->n_edge
].conditional_validity
= 0;
934 graph
->edge
[graph
->n_edge
].tagged_condition
= NULL
;
935 graph
->edge
[graph
->n_edge
].tagged_validity
= NULL
;
936 if (data
->type
== isl_edge_validity
)
937 graph
->edge
[graph
->n_edge
].validity
= 1;
938 if (data
->type
== isl_edge_coincidence
)
939 graph
->edge
[graph
->n_edge
].coincidence
= 1;
940 if (data
->type
== isl_edge_proximity
)
941 graph
->edge
[graph
->n_edge
].proximity
= 1;
942 if (data
->type
== isl_edge_condition
) {
943 graph
->edge
[graph
->n_edge
].condition
= 1;
944 graph
->edge
[graph
->n_edge
].tagged_condition
=
945 isl_union_map_from_map(tagged
);
947 if (data
->type
== isl_edge_conditional_validity
) {
948 graph
->edge
[graph
->n_edge
].conditional_validity
= 1;
949 graph
->edge
[graph
->n_edge
].tagged_validity
=
950 isl_union_map_from_map(tagged
);
953 edge
= graph_find_matching_edge(graph
, &graph
->edge
[graph
->n_edge
]);
954 if (edge
== &graph
->edge
[graph
->n_edge
])
955 return graph_edge_table_add(ctx
, graph
, data
->type
,
956 &graph
->edge
[graph
->n_edge
++]);
958 if (merge_edge(data
->type
, edge
, &graph
->edge
[graph
->n_edge
]) < 0)
961 return graph_edge_table_add(ctx
, graph
, data
->type
, edge
);
964 /* Check whether there is any dependence from node[j] to node[i]
965 * or from node[i] to node[j].
967 static int node_follows_weak(int i
, int j
, void *user
)
970 struct isl_sched_graph
*graph
= user
;
972 f
= graph_has_any_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
975 return graph_has_any_edge(graph
, &graph
->node
[i
], &graph
->node
[j
]);
978 /* Check whether there is a (conditional) validity dependence from node[j]
979 * to node[i], forcing node[i] to follow node[j].
981 static int node_follows_strong(int i
, int j
, void *user
)
983 struct isl_sched_graph
*graph
= user
;
985 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
988 /* Use Tarjan's algorithm for computing the strongly connected components
989 * in the dependence graph (only validity edges).
990 * If weak is set, we consider the graph to be undirected and
991 * we effectively compute the (weakly) connected components.
992 * Additionally, we also consider other edges when weak is set.
994 static int detect_ccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
, int weak
)
997 struct isl_tarjan_graph
*g
= NULL
;
999 g
= isl_tarjan_graph_init(ctx
, graph
->n
,
1000 weak
? &node_follows_weak
: &node_follows_strong
, graph
);
1008 while (g
->order
[i
] != -1) {
1009 graph
->node
[g
->order
[i
]].scc
= graph
->scc
;
1017 isl_tarjan_graph_free(g
);
1022 /* Apply Tarjan's algorithm to detect the strongly connected components
1023 * in the dependence graph.
1025 static int detect_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1027 return detect_ccs(ctx
, graph
, 0);
1030 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1031 * in the dependence graph.
1033 static int detect_wccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1035 return detect_ccs(ctx
, graph
, 1);
1038 static int cmp_scc(const void *a
, const void *b
, void *data
)
1040 struct isl_sched_graph
*graph
= data
;
1044 return graph
->node
[*i1
].scc
- graph
->node
[*i2
].scc
;
1047 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1049 static int sort_sccs(struct isl_sched_graph
*graph
)
1051 return isl_sort(graph
->sorted
, graph
->n
, sizeof(int), &cmp_scc
, graph
);
1054 /* Given a dependence relation R from a node to itself,
1055 * construct the set of coefficients of valid constraints for elements
1056 * in that dependence relation.
1057 * In particular, the result contains tuples of coefficients
1058 * c_0, c_n, c_x such that
1060 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1064 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1066 * We choose here to compute the dual of delta R.
1067 * Alternatively, we could have computed the dual of R, resulting
1068 * in a set of tuples c_0, c_n, c_x, c_y, and then
1069 * plugged in (c_0, c_n, c_x, -c_x).
1071 static __isl_give isl_basic_set
*intra_coefficients(
1072 struct isl_sched_graph
*graph
, __isl_take isl_map
*map
)
1075 isl_basic_set
*coef
;
1077 if (isl_map_to_basic_set_has(graph
->intra_hmap
, map
))
1078 return isl_map_to_basic_set_get(graph
->intra_hmap
, map
);
1080 delta
= isl_set_remove_divs(isl_map_deltas(isl_map_copy(map
)));
1081 coef
= isl_set_coefficients(delta
);
1082 graph
->intra_hmap
= isl_map_to_basic_set_set(graph
->intra_hmap
, map
,
1083 isl_basic_set_copy(coef
));
1088 /* Given a dependence relation R, * construct the set of coefficients
1089 * of valid constraints for elements in that dependence relation.
1090 * In particular, the result contains tuples of coefficients
1091 * c_0, c_n, c_x, c_y such that
1093 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1096 static __isl_give isl_basic_set
*inter_coefficients(
1097 struct isl_sched_graph
*graph
, __isl_take isl_map
*map
)
1100 isl_basic_set
*coef
;
1102 if (isl_map_to_basic_set_has(graph
->inter_hmap
, map
))
1103 return isl_map_to_basic_set_get(graph
->inter_hmap
, map
);
1105 set
= isl_map_wrap(isl_map_remove_divs(isl_map_copy(map
)));
1106 coef
= isl_set_coefficients(set
);
1107 graph
->inter_hmap
= isl_map_to_basic_set_set(graph
->inter_hmap
, map
,
1108 isl_basic_set_copy(coef
));
1113 /* Add constraints to graph->lp that force validity for the given
1114 * dependence from a node i to itself.
1115 * That is, add constraints that enforce
1117 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1118 * = c_i_x (y - x) >= 0
1120 * for each (x,y) in R.
1121 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1122 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1123 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1124 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1126 * Actually, we do not construct constraints for the c_i_x themselves,
1127 * but for the coefficients of c_i_x written as a linear combination
1128 * of the columns in node->cmap.
1130 static int add_intra_validity_constraints(struct isl_sched_graph
*graph
,
1131 struct isl_sched_edge
*edge
)
1134 isl_map
*map
= isl_map_copy(edge
->map
);
1135 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1137 isl_dim_map
*dim_map
;
1138 isl_basic_set
*coef
;
1139 struct isl_sched_node
*node
= edge
->src
;
1141 coef
= intra_coefficients(graph
, map
);
1143 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1145 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1146 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(node
->cmap
));
1150 total
= isl_basic_set_total_dim(graph
->lp
);
1151 dim_map
= isl_dim_map_alloc(ctx
, total
);
1152 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
1153 isl_space_dim(dim
, isl_dim_set
), 1,
1155 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
1156 isl_space_dim(dim
, isl_dim_set
), 1,
1158 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1159 coef
->n_eq
, coef
->n_ineq
);
1160 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1162 isl_space_free(dim
);
1166 isl_space_free(dim
);
1170 /* Add constraints to graph->lp that force validity for the given
1171 * dependence from node i to node j.
1172 * That is, add constraints that enforce
1174 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1176 * for each (x,y) in R.
1177 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1178 * of valid constraints for R and then plug in
1179 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
1180 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1181 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1182 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1184 * Actually, we do not construct constraints for the c_*_x themselves,
1185 * but for the coefficients of c_*_x written as a linear combination
1186 * of the columns in node->cmap.
1188 static int add_inter_validity_constraints(struct isl_sched_graph
*graph
,
1189 struct isl_sched_edge
*edge
)
1192 isl_map
*map
= isl_map_copy(edge
->map
);
1193 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1195 isl_dim_map
*dim_map
;
1196 isl_basic_set
*coef
;
1197 struct isl_sched_node
*src
= edge
->src
;
1198 struct isl_sched_node
*dst
= edge
->dst
;
1200 coef
= inter_coefficients(graph
, map
);
1202 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1204 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1205 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(src
->cmap
));
1206 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1207 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
,
1208 isl_mat_copy(dst
->cmap
));
1212 total
= isl_basic_set_total_dim(graph
->lp
);
1213 dim_map
= isl_dim_map_alloc(ctx
, total
);
1215 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, 1);
1216 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, -1);
1217 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, 1);
1218 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
1219 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1221 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
1222 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1225 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, -1);
1226 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, 1);
1227 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, -1);
1228 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
1229 isl_space_dim(dim
, isl_dim_set
), 1,
1231 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
1232 isl_space_dim(dim
, isl_dim_set
), 1,
1235 edge
->start
= graph
->lp
->n_ineq
;
1236 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1237 coef
->n_eq
, coef
->n_ineq
);
1238 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1242 isl_space_free(dim
);
1243 edge
->end
= graph
->lp
->n_ineq
;
1247 isl_space_free(dim
);
1251 /* Add constraints to graph->lp that bound the dependence distance for the given
1252 * dependence from a node i to itself.
1253 * If s = 1, we add the constraint
1255 * c_i_x (y - x) <= m_0 + m_n n
1259 * -c_i_x (y - x) + m_0 + m_n n >= 0
1261 * for each (x,y) in R.
1262 * If s = -1, we add the constraint
1264 * -c_i_x (y - x) <= m_0 + m_n n
1268 * c_i_x (y - x) + m_0 + m_n n >= 0
1270 * for each (x,y) in R.
1271 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1272 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1273 * with each coefficient (except m_0) represented as a pair of non-negative
1276 * Actually, we do not construct constraints for the c_i_x themselves,
1277 * but for the coefficients of c_i_x written as a linear combination
1278 * of the columns in node->cmap.
1281 * If "local" is set, then we add constraints
1283 * c_i_x (y - x) <= 0
1287 * -c_i_x (y - x) <= 0
1289 * instead, forcing the dependence distance to be (less than or) equal to 0.
1290 * That is, we plug in (0, 0, -s * c_i_x),
1291 * Note that dependences marked local are treated as validity constraints
1292 * by add_all_validity_constraints and therefore also have
1293 * their distances bounded by 0 from below.
1295 static int add_intra_proximity_constraints(struct isl_sched_graph
*graph
,
1296 struct isl_sched_edge
*edge
, int s
, int local
)
1300 isl_map
*map
= isl_map_copy(edge
->map
);
1301 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1303 isl_dim_map
*dim_map
;
1304 isl_basic_set
*coef
;
1305 struct isl_sched_node
*node
= edge
->src
;
1307 coef
= intra_coefficients(graph
, map
);
1309 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1311 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1312 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(node
->cmap
));
1316 nparam
= isl_space_dim(node
->dim
, isl_dim_param
);
1317 total
= isl_basic_set_total_dim(graph
->lp
);
1318 dim_map
= isl_dim_map_alloc(ctx
, total
);
1321 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1322 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1323 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1325 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
1326 isl_space_dim(dim
, isl_dim_set
), 1,
1328 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
1329 isl_space_dim(dim
, isl_dim_set
), 1,
1331 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1332 coef
->n_eq
, coef
->n_ineq
);
1333 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1335 isl_space_free(dim
);
1339 isl_space_free(dim
);
1343 /* Add constraints to graph->lp that bound the dependence distance for the given
1344 * dependence from node i to node j.
1345 * If s = 1, we add the constraint
1347 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1352 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1355 * for each (x,y) in R.
1356 * If s = -1, we add the constraint
1358 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1363 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1366 * for each (x,y) in R.
1367 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1368 * of valid constraints for R and then plug in
1369 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1371 * with each coefficient (except m_0, c_j_0 and c_i_0)
1372 * represented as a pair of non-negative coefficients.
1374 * Actually, we do not construct constraints for the c_*_x themselves,
1375 * but for the coefficients of c_*_x written as a linear combination
1376 * of the columns in node->cmap.
1379 * If "local" is set, then we add constraints
1381 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1385 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
1387 * instead, forcing the dependence distance to be (less than or) equal to 0.
1388 * That is, we plug in
1389 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
1390 * Note that dependences marked local are treated as validity constraints
1391 * by add_all_validity_constraints and therefore also have
1392 * their distances bounded by 0 from below.
1394 static int add_inter_proximity_constraints(struct isl_sched_graph
*graph
,
1395 struct isl_sched_edge
*edge
, int s
, int local
)
1399 isl_map
*map
= isl_map_copy(edge
->map
);
1400 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1402 isl_dim_map
*dim_map
;
1403 isl_basic_set
*coef
;
1404 struct isl_sched_node
*src
= edge
->src
;
1405 struct isl_sched_node
*dst
= edge
->dst
;
1407 coef
= inter_coefficients(graph
, map
);
1409 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1411 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1412 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(src
->cmap
));
1413 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1414 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
,
1415 isl_mat_copy(dst
->cmap
));
1419 nparam
= isl_space_dim(src
->dim
, isl_dim_param
);
1420 total
= isl_basic_set_total_dim(graph
->lp
);
1421 dim_map
= isl_dim_map_alloc(ctx
, total
);
1424 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1425 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1426 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1429 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, -s
);
1430 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, s
);
1431 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, -s
);
1432 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
1433 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1435 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
1436 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1439 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, s
);
1440 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, -s
);
1441 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, s
);
1442 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
1443 isl_space_dim(dim
, isl_dim_set
), 1,
1445 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
1446 isl_space_dim(dim
, isl_dim_set
), 1,
1449 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1450 coef
->n_eq
, coef
->n_ineq
);
1451 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1453 isl_space_free(dim
);
1457 isl_space_free(dim
);
1461 /* Add all validity constraints to graph->lp.
1463 * An edge that is forced to be local needs to have its dependence
1464 * distances equal to zero. We take care of bounding them by 0 from below
1465 * here. add_all_proximity_constraints takes care of bounding them by 0
1468 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1469 * Otherwise, we ignore them.
1471 static int add_all_validity_constraints(struct isl_sched_graph
*graph
,
1472 int use_coincidence
)
1476 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1477 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1480 local
= edge
->local
|| (edge
->coincidence
&& use_coincidence
);
1481 if (!edge
->validity
&& !local
)
1483 if (edge
->src
!= edge
->dst
)
1485 if (add_intra_validity_constraints(graph
, edge
) < 0)
1489 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1490 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1493 local
= edge
->local
|| (edge
->coincidence
&& use_coincidence
);
1494 if (!edge
->validity
&& !local
)
1496 if (edge
->src
== edge
->dst
)
1498 if (add_inter_validity_constraints(graph
, edge
) < 0)
1505 /* Add constraints to graph->lp that bound the dependence distance
1506 * for all dependence relations.
1507 * If a given proximity dependence is identical to a validity
1508 * dependence, then the dependence distance is already bounded
1509 * from below (by zero), so we only need to bound the distance
1510 * from above. (This includes the case of "local" dependences
1511 * which are treated as validity dependence by add_all_validity_constraints.)
1512 * Otherwise, we need to bound the distance both from above and from below.
1514 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1515 * Otherwise, we ignore them.
1517 static int add_all_proximity_constraints(struct isl_sched_graph
*graph
,
1518 int use_coincidence
)
1522 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1523 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1526 local
= edge
->local
|| (edge
->coincidence
&& use_coincidence
);
1527 if (!edge
->proximity
&& !local
)
1529 if (edge
->src
== edge
->dst
&&
1530 add_intra_proximity_constraints(graph
, edge
, 1, local
) < 0)
1532 if (edge
->src
!= edge
->dst
&&
1533 add_inter_proximity_constraints(graph
, edge
, 1, local
) < 0)
1535 if (edge
->validity
|| local
)
1537 if (edge
->src
== edge
->dst
&&
1538 add_intra_proximity_constraints(graph
, edge
, -1, 0) < 0)
1540 if (edge
->src
!= edge
->dst
&&
1541 add_inter_proximity_constraints(graph
, edge
, -1, 0) < 0)
1548 /* Compute a basis for the rows in the linear part of the schedule
1549 * and extend this basis to a full basis. The remaining rows
1550 * can then be used to force linear independence from the rows
1553 * In particular, given the schedule rows S, we compute
1558 * with H the Hermite normal form of S. That is, all but the
1559 * first rank columns of H are zero and so each row in S is
1560 * a linear combination of the first rank rows of Q.
1561 * The matrix Q is then transposed because we will write the
1562 * coefficients of the next schedule row as a column vector s
1563 * and express this s as a linear combination s = Q c of the
1565 * Similarly, the matrix U is transposed such that we can
1566 * compute the coefficients c = U s from a schedule row s.
1568 static int node_update_cmap(struct isl_sched_node
*node
)
1571 int n_row
= isl_mat_rows(node
->sched
);
1573 H
= isl_mat_sub_alloc(node
->sched
, 0, n_row
,
1574 1 + node
->nparam
, node
->nvar
);
1576 H
= isl_mat_left_hermite(H
, 0, &U
, &Q
);
1577 isl_mat_free(node
->cmap
);
1578 isl_mat_free(node
->cinv
);
1579 node
->cmap
= isl_mat_transpose(Q
);
1580 node
->cinv
= isl_mat_transpose(U
);
1581 node
->rank
= isl_mat_initial_non_zero_cols(H
);
1584 if (!node
->cmap
|| !node
->cinv
|| node
->rank
< 0)
1589 /* How many times should we count the constraints in "edge"?
1591 * If carry is set, then we are counting the number of
1592 * (validity or conditional validity) constraints that will be added
1593 * in setup_carry_lp and we count each edge exactly once.
1595 * Otherwise, we count as follows
1596 * validity -> 1 (>= 0)
1597 * validity+proximity -> 2 (>= 0 and upper bound)
1598 * proximity -> 2 (lower and upper bound)
1599 * local(+any) -> 2 (>= 0 and <= 0)
1601 * If an edge is only marked conditional_validity then it counts
1602 * as zero since it is only checked afterwards.
1604 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1605 * Otherwise, we ignore them.
1607 static int edge_multiplicity(struct isl_sched_edge
*edge
, int carry
,
1608 int use_coincidence
)
1610 if (carry
&& !edge
->validity
&& !edge
->conditional_validity
)
1614 if (edge
->proximity
|| edge
->local
)
1616 if (use_coincidence
&& edge
->coincidence
)
1623 /* Count the number of equality and inequality constraints
1624 * that will be added for the given map.
1626 * "use_coincidence" is set if we should take into account coincidence edges.
1628 static int count_map_constraints(struct isl_sched_graph
*graph
,
1629 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
,
1630 int *n_eq
, int *n_ineq
, int carry
, int use_coincidence
)
1632 isl_basic_set
*coef
;
1633 int f
= edge_multiplicity(edge
, carry
, use_coincidence
);
1640 if (edge
->src
== edge
->dst
)
1641 coef
= intra_coefficients(graph
, map
);
1643 coef
= inter_coefficients(graph
, map
);
1646 *n_eq
+= f
* coef
->n_eq
;
1647 *n_ineq
+= f
* coef
->n_ineq
;
1648 isl_basic_set_free(coef
);
1653 /* Count the number of equality and inequality constraints
1654 * that will be added to the main lp problem.
1655 * We count as follows
1656 * validity -> 1 (>= 0)
1657 * validity+proximity -> 2 (>= 0 and upper bound)
1658 * proximity -> 2 (lower and upper bound)
1659 * local(+any) -> 2 (>= 0 and <= 0)
1661 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1662 * Otherwise, we ignore them.
1664 static int count_constraints(struct isl_sched_graph
*graph
,
1665 int *n_eq
, int *n_ineq
, int use_coincidence
)
1669 *n_eq
= *n_ineq
= 0;
1670 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1671 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1672 isl_map
*map
= isl_map_copy(edge
->map
);
1674 if (count_map_constraints(graph
, edge
, map
, n_eq
, n_ineq
,
1675 0, use_coincidence
) < 0)
1682 /* Count the number of constraints that will be added by
1683 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
1686 * In practice, add_bound_coefficient_constraints only adds inequalities.
1688 static int count_bound_coefficient_constraints(isl_ctx
*ctx
,
1689 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
1693 if (ctx
->opt
->schedule_max_coefficient
== -1)
1696 for (i
= 0; i
< graph
->n
; ++i
)
1697 *n_ineq
+= 2 * graph
->node
[i
].nparam
+ 2 * graph
->node
[i
].nvar
;
1702 /* Add constraints that bound the values of the variable and parameter
1703 * coefficients of the schedule.
1705 * The maximal value of the coefficients is defined by the option
1706 * 'schedule_max_coefficient'.
1708 static int add_bound_coefficient_constraints(isl_ctx
*ctx
,
1709 struct isl_sched_graph
*graph
)
1712 int max_coefficient
;
1715 max_coefficient
= ctx
->opt
->schedule_max_coefficient
;
1717 if (max_coefficient
== -1)
1720 total
= isl_basic_set_total_dim(graph
->lp
);
1722 for (i
= 0; i
< graph
->n
; ++i
) {
1723 struct isl_sched_node
*node
= &graph
->node
[i
];
1724 for (j
= 0; j
< 2 * node
->nparam
+ 2 * node
->nvar
; ++j
) {
1726 k
= isl_basic_set_alloc_inequality(graph
->lp
);
1729 dim
= 1 + node
->start
+ 1 + j
;
1730 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
1731 isl_int_set_si(graph
->lp
->ineq
[k
][dim
], -1);
1732 isl_int_set_si(graph
->lp
->ineq
[k
][0], max_coefficient
);
1739 /* Construct an ILP problem for finding schedule coefficients
1740 * that result in non-negative, but small dependence distances
1741 * over all dependences.
1742 * In particular, the dependence distances over proximity edges
1743 * are bounded by m_0 + m_n n and we compute schedule coefficients
1744 * with small values (preferably zero) of m_n and m_0.
1746 * All variables of the ILP are non-negative. The actual coefficients
1747 * may be negative, so each coefficient is represented as the difference
1748 * of two non-negative variables. The negative part always appears
1749 * immediately before the positive part.
1750 * Other than that, the variables have the following order
1752 * - sum of positive and negative parts of m_n coefficients
1754 * - sum of positive and negative parts of all c_n coefficients
1755 * (unconstrained when computing non-parametric schedules)
1756 * - sum of positive and negative parts of all c_x coefficients
1757 * - positive and negative parts of m_n coefficients
1760 * - positive and negative parts of c_i_n (if parametric)
1761 * - positive and negative parts of c_i_x
1763 * The c_i_x are not represented directly, but through the columns of
1764 * node->cmap. That is, the computed values are for variable t_i_x
1765 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
1767 * The constraints are those from the edges plus two or three equalities
1768 * to express the sums.
1770 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1771 * Otherwise, we ignore them.
1773 static int setup_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
1774 int use_coincidence
)
1784 int max_constant_term
;
1786 max_constant_term
= ctx
->opt
->schedule_max_constant_term
;
1788 parametric
= ctx
->opt
->schedule_parametric
;
1789 nparam
= isl_space_dim(graph
->node
[0].dim
, isl_dim_param
);
1791 total
= param_pos
+ 2 * nparam
;
1792 for (i
= 0; i
< graph
->n
; ++i
) {
1793 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
1794 if (node_update_cmap(node
) < 0)
1796 node
->start
= total
;
1797 total
+= 1 + 2 * (node
->nparam
+ node
->nvar
);
1800 if (count_constraints(graph
, &n_eq
, &n_ineq
, use_coincidence
) < 0)
1802 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
1805 dim
= isl_space_set_alloc(ctx
, 0, total
);
1806 isl_basic_set_free(graph
->lp
);
1807 n_eq
+= 2 + parametric
;
1808 if (max_constant_term
!= -1)
1811 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
1813 k
= isl_basic_set_alloc_equality(graph
->lp
);
1816 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
1817 isl_int_set_si(graph
->lp
->eq
[k
][1], -1);
1818 for (i
= 0; i
< 2 * nparam
; ++i
)
1819 isl_int_set_si(graph
->lp
->eq
[k
][1 + param_pos
+ i
], 1);
1822 k
= isl_basic_set_alloc_equality(graph
->lp
);
1825 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
1826 isl_int_set_si(graph
->lp
->eq
[k
][3], -1);
1827 for (i
= 0; i
< graph
->n
; ++i
) {
1828 int pos
= 1 + graph
->node
[i
].start
+ 1;
1830 for (j
= 0; j
< 2 * graph
->node
[i
].nparam
; ++j
)
1831 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
1835 k
= isl_basic_set_alloc_equality(graph
->lp
);
1838 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
1839 isl_int_set_si(graph
->lp
->eq
[k
][4], -1);
1840 for (i
= 0; i
< graph
->n
; ++i
) {
1841 struct isl_sched_node
*node
= &graph
->node
[i
];
1842 int pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
1844 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
1845 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
1848 if (max_constant_term
!= -1)
1849 for (i
= 0; i
< graph
->n
; ++i
) {
1850 struct isl_sched_node
*node
= &graph
->node
[i
];
1851 k
= isl_basic_set_alloc_inequality(graph
->lp
);
1854 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
1855 isl_int_set_si(graph
->lp
->ineq
[k
][1 + node
->start
], -1);
1856 isl_int_set_si(graph
->lp
->ineq
[k
][0], max_constant_term
);
1859 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
1861 if (add_all_validity_constraints(graph
, use_coincidence
) < 0)
1863 if (add_all_proximity_constraints(graph
, use_coincidence
) < 0)
1869 /* Analyze the conflicting constraint found by
1870 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
1871 * constraint of one of the edges between distinct nodes, living, moreover
1872 * in distinct SCCs, then record the source and sink SCC as this may
1873 * be a good place to cut between SCCs.
1875 static int check_conflict(int con
, void *user
)
1878 struct isl_sched_graph
*graph
= user
;
1880 if (graph
->src_scc
>= 0)
1883 con
-= graph
->lp
->n_eq
;
1885 if (con
>= graph
->lp
->n_ineq
)
1888 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1889 if (!graph
->edge
[i
].validity
)
1891 if (graph
->edge
[i
].src
== graph
->edge
[i
].dst
)
1893 if (graph
->edge
[i
].src
->scc
== graph
->edge
[i
].dst
->scc
)
1895 if (graph
->edge
[i
].start
> con
)
1897 if (graph
->edge
[i
].end
<= con
)
1899 graph
->src_scc
= graph
->edge
[i
].src
->scc
;
1900 graph
->dst_scc
= graph
->edge
[i
].dst
->scc
;
1906 /* Check whether the next schedule row of the given node needs to be
1907 * non-trivial. Lower-dimensional domains may have some trivial rows,
1908 * but as soon as the number of remaining required non-trivial rows
1909 * is as large as the number or remaining rows to be computed,
1910 * all remaining rows need to be non-trivial.
1912 static int needs_row(struct isl_sched_graph
*graph
, struct isl_sched_node
*node
)
1914 return node
->nvar
- node
->rank
>= graph
->maxvar
- graph
->n_row
;
1917 /* Solve the ILP problem constructed in setup_lp.
1918 * For each node such that all the remaining rows of its schedule
1919 * need to be non-trivial, we construct a non-triviality region.
1920 * This region imposes that the next row is independent of previous rows.
1921 * In particular the coefficients c_i_x are represented by t_i_x
1922 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
1923 * its first columns span the rows of the previously computed part
1924 * of the schedule. The non-triviality region enforces that at least
1925 * one of the remaining components of t_i_x is non-zero, i.e.,
1926 * that the new schedule row depends on at least one of the remaining
1929 static __isl_give isl_vec
*solve_lp(struct isl_sched_graph
*graph
)
1935 for (i
= 0; i
< graph
->n
; ++i
) {
1936 struct isl_sched_node
*node
= &graph
->node
[i
];
1937 int skip
= node
->rank
;
1938 graph
->region
[i
].pos
= node
->start
+ 1 + 2*(node
->nparam
+skip
);
1939 if (needs_row(graph
, node
))
1940 graph
->region
[i
].len
= 2 * (node
->nvar
- skip
);
1942 graph
->region
[i
].len
= 0;
1944 lp
= isl_basic_set_copy(graph
->lp
);
1945 sol
= isl_tab_basic_set_non_trivial_lexmin(lp
, 2, graph
->n
,
1946 graph
->region
, &check_conflict
, graph
);
1950 /* Update the schedules of all nodes based on the given solution
1951 * of the LP problem.
1952 * The new row is added to the current band.
1953 * All possibly negative coefficients are encoded as a difference
1954 * of two non-negative variables, so we need to perform the subtraction
1955 * here. Moreover, if use_cmap is set, then the solution does
1956 * not refer to the actual coefficients c_i_x, but instead to variables
1957 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
1958 * In this case, we then also need to perform this multiplication
1959 * to obtain the values of c_i_x.
1961 * If coincident is set, then the caller guarantees that the new
1962 * row satisfies the coincidence constraints.
1964 static int update_schedule(struct isl_sched_graph
*graph
,
1965 __isl_take isl_vec
*sol
, int use_cmap
, int coincident
)
1968 isl_vec
*csol
= NULL
;
1973 isl_die(sol
->ctx
, isl_error_internal
,
1974 "no solution found", goto error
);
1975 if (graph
->n_total_row
>= graph
->max_row
)
1976 isl_die(sol
->ctx
, isl_error_internal
,
1977 "too many schedule rows", goto error
);
1979 for (i
= 0; i
< graph
->n
; ++i
) {
1980 struct isl_sched_node
*node
= &graph
->node
[i
];
1981 int pos
= node
->start
;
1982 int row
= isl_mat_rows(node
->sched
);
1985 csol
= isl_vec_alloc(sol
->ctx
, node
->nvar
);
1989 isl_map_free(node
->sched_map
);
1990 node
->sched_map
= NULL
;
1991 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
1994 node
->sched
= isl_mat_set_element(node
->sched
, row
, 0,
1996 for (j
= 0; j
< node
->nparam
+ node
->nvar
; ++j
)
1997 isl_int_sub(sol
->el
[1 + pos
+ 1 + 2 * j
+ 1],
1998 sol
->el
[1 + pos
+ 1 + 2 * j
+ 1],
1999 sol
->el
[1 + pos
+ 1 + 2 * j
]);
2000 for (j
= 0; j
< node
->nparam
; ++j
)
2001 node
->sched
= isl_mat_set_element(node
->sched
,
2002 row
, 1 + j
, sol
->el
[1+pos
+1+2*j
+1]);
2003 for (j
= 0; j
< node
->nvar
; ++j
)
2004 isl_int_set(csol
->el
[j
],
2005 sol
->el
[1+pos
+1+2*(node
->nparam
+j
)+1]);
2007 csol
= isl_mat_vec_product(isl_mat_copy(node
->cmap
),
2011 for (j
= 0; j
< node
->nvar
; ++j
)
2012 node
->sched
= isl_mat_set_element(node
->sched
,
2013 row
, 1 + node
->nparam
+ j
, csol
->el
[j
]);
2014 node
->band
[graph
->n_total_row
] = graph
->n_band
;
2015 node
->coincident
[graph
->n_total_row
] = coincident
;
2021 graph
->n_total_row
++;
2030 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2031 * and return this isl_aff.
2033 static __isl_give isl_aff
*extract_schedule_row(__isl_take isl_local_space
*ls
,
2034 struct isl_sched_node
*node
, int row
)
2042 aff
= isl_aff_zero_on_domain(ls
);
2043 isl_mat_get_element(node
->sched
, row
, 0, &v
);
2044 aff
= isl_aff_set_constant(aff
, v
);
2045 for (j
= 0; j
< node
->nparam
; ++j
) {
2046 isl_mat_get_element(node
->sched
, row
, 1 + j
, &v
);
2047 aff
= isl_aff_set_coefficient(aff
, isl_dim_param
, j
, v
);
2049 for (j
= 0; j
< node
->nvar
; ++j
) {
2050 isl_mat_get_element(node
->sched
, row
, 1 + node
->nparam
+ j
, &v
);
2051 aff
= isl_aff_set_coefficient(aff
, isl_dim_in
, j
, v
);
2059 /* Convert node->sched into a multi_aff and return this multi_aff.
2061 static __isl_give isl_multi_aff
*node_extract_schedule_multi_aff(
2062 struct isl_sched_node
*node
)
2066 isl_local_space
*ls
;
2071 nrow
= isl_mat_rows(node
->sched
);
2072 ncol
= isl_mat_cols(node
->sched
) - 1;
2073 space
= isl_space_from_domain(isl_space_copy(node
->dim
));
2074 space
= isl_space_add_dims(space
, isl_dim_out
, nrow
);
2075 ma
= isl_multi_aff_zero(space
);
2076 ls
= isl_local_space_from_space(isl_space_copy(node
->dim
));
2078 for (i
= 0; i
< nrow
; ++i
) {
2079 aff
= extract_schedule_row(isl_local_space_copy(ls
), node
, i
);
2080 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
2083 isl_local_space_free(ls
);
2088 /* Convert node->sched into a map and return this map.
2090 * The result is cached in node->sched_map, which needs to be released
2091 * whenever node->sched is updated.
2093 static __isl_give isl_map
*node_extract_schedule(struct isl_sched_node
*node
)
2095 if (!node
->sched_map
) {
2098 ma
= node_extract_schedule_multi_aff(node
);
2099 node
->sched_map
= isl_map_from_multi_aff(ma
);
2102 return isl_map_copy(node
->sched_map
);
2105 /* Construct a map that can be used to update dependence relation
2106 * based on the current schedule.
2107 * That is, construct a map expressing that source and sink
2108 * are executed within the same iteration of the current schedule.
2109 * This map can then be intersected with the dependence relation.
2110 * This is not the most efficient way, but this shouldn't be a critical
2113 static __isl_give isl_map
*specializer(struct isl_sched_node
*src
,
2114 struct isl_sched_node
*dst
)
2116 isl_map
*src_sched
, *dst_sched
;
2118 src_sched
= node_extract_schedule(src
);
2119 dst_sched
= node_extract_schedule(dst
);
2120 return isl_map_apply_range(src_sched
, isl_map_reverse(dst_sched
));
2123 /* Intersect the domains of the nested relations in domain and range
2124 * of "umap" with "map".
2126 static __isl_give isl_union_map
*intersect_domains(
2127 __isl_take isl_union_map
*umap
, __isl_keep isl_map
*map
)
2129 isl_union_set
*uset
;
2131 umap
= isl_union_map_zip(umap
);
2132 uset
= isl_union_set_from_set(isl_map_wrap(isl_map_copy(map
)));
2133 umap
= isl_union_map_intersect_domain(umap
, uset
);
2134 umap
= isl_union_map_zip(umap
);
2138 /* Update the dependence relation of the given edge based
2139 * on the current schedule.
2140 * If the dependence is carried completely by the current schedule, then
2141 * it is removed from the edge_tables. It is kept in the list of edges
2142 * as otherwise all edge_tables would have to be recomputed.
2144 static int update_edge(struct isl_sched_graph
*graph
,
2145 struct isl_sched_edge
*edge
)
2149 id
= specializer(edge
->src
, edge
->dst
);
2150 edge
->map
= isl_map_intersect(edge
->map
, isl_map_copy(id
));
2154 if (edge
->tagged_condition
) {
2155 edge
->tagged_condition
=
2156 intersect_domains(edge
->tagged_condition
, id
);
2157 if (!edge
->tagged_condition
)
2160 if (edge
->tagged_validity
) {
2161 edge
->tagged_validity
=
2162 intersect_domains(edge
->tagged_validity
, id
);
2163 if (!edge
->tagged_validity
)
2168 if (isl_map_plain_is_empty(edge
->map
))
2169 graph_remove_edge(graph
, edge
);
2177 /* Update the dependence relations of all edges based on the current schedule.
2179 static int update_edges(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2183 for (i
= graph
->n_edge
- 1; i
>= 0; --i
) {
2184 if (update_edge(graph
, &graph
->edge
[i
]) < 0)
2191 static void next_band(struct isl_sched_graph
*graph
)
2193 graph
->band_start
= graph
->n_total_row
;
2197 /* Topologically sort statements mapped to the same schedule iteration
2198 * and add a row to the schedule corresponding to this order.
2200 static int sort_statements(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2207 if (update_edges(ctx
, graph
) < 0)
2210 if (graph
->n_edge
== 0)
2213 if (detect_sccs(ctx
, graph
) < 0)
2216 if (graph
->n_total_row
>= graph
->max_row
)
2217 isl_die(ctx
, isl_error_internal
,
2218 "too many schedule rows", return -1);
2220 for (i
= 0; i
< graph
->n
; ++i
) {
2221 struct isl_sched_node
*node
= &graph
->node
[i
];
2222 int row
= isl_mat_rows(node
->sched
);
2223 int cols
= isl_mat_cols(node
->sched
);
2225 isl_map_free(node
->sched_map
);
2226 node
->sched_map
= NULL
;
2227 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
2230 node
->sched
= isl_mat_set_element_si(node
->sched
, row
, 0,
2232 for (j
= 1; j
< cols
; ++j
)
2233 node
->sched
= isl_mat_set_element_si(node
->sched
,
2235 node
->band
[graph
->n_total_row
] = graph
->n_band
;
2238 graph
->n_total_row
++;
2244 /* Construct an isl_schedule based on the computed schedule stored
2245 * in graph and with parameters specified by dim.
2247 static __isl_give isl_schedule
*extract_schedule(struct isl_sched_graph
*graph
,
2248 __isl_take isl_space
*dim
)
2252 isl_schedule
*sched
= NULL
;
2257 ctx
= isl_space_get_ctx(dim
);
2258 sched
= isl_calloc(ctx
, struct isl_schedule
,
2259 sizeof(struct isl_schedule
) +
2260 (graph
->n
- 1) * sizeof(struct isl_schedule_node
));
2265 sched
->n
= graph
->n
;
2266 sched
->n_band
= graph
->n_band
;
2267 sched
->n_total_row
= graph
->n_total_row
;
2269 for (i
= 0; i
< sched
->n
; ++i
) {
2271 int *band_end
, *band_id
, *coincident
;
2273 sched
->node
[i
].sched
=
2274 node_extract_schedule_multi_aff(&graph
->node
[i
]);
2275 if (!sched
->node
[i
].sched
)
2278 sched
->node
[i
].n_band
= graph
->n_band
;
2279 if (graph
->n_band
== 0)
2282 band_end
= isl_alloc_array(ctx
, int, graph
->n_band
);
2283 band_id
= isl_alloc_array(ctx
, int, graph
->n_band
);
2284 coincident
= isl_alloc_array(ctx
, int, graph
->n_total_row
);
2285 sched
->node
[i
].band_end
= band_end
;
2286 sched
->node
[i
].band_id
= band_id
;
2287 sched
->node
[i
].coincident
= coincident
;
2288 if (!band_end
|| !band_id
|| !coincident
)
2291 for (r
= 0; r
< graph
->n_total_row
; ++r
)
2292 coincident
[r
] = graph
->node
[i
].coincident
[r
];
2293 for (r
= b
= 0; r
< graph
->n_total_row
; ++r
) {
2294 if (graph
->node
[i
].band
[r
] == b
)
2297 if (graph
->node
[i
].band
[r
] == -1)
2300 if (r
== graph
->n_total_row
)
2302 sched
->node
[i
].n_band
= b
;
2303 for (--b
; b
>= 0; --b
)
2304 band_id
[b
] = graph
->node
[i
].band_id
[b
];
2311 isl_space_free(dim
);
2312 isl_schedule_free(sched
);
2316 /* Copy nodes that satisfy node_pred from the src dependence graph
2317 * to the dst dependence graph.
2319 static int copy_nodes(struct isl_sched_graph
*dst
, struct isl_sched_graph
*src
,
2320 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
2325 for (i
= 0; i
< src
->n
; ++i
) {
2326 if (!node_pred(&src
->node
[i
], data
))
2328 dst
->node
[dst
->n
].dim
= isl_space_copy(src
->node
[i
].dim
);
2329 dst
->node
[dst
->n
].nvar
= src
->node
[i
].nvar
;
2330 dst
->node
[dst
->n
].nparam
= src
->node
[i
].nparam
;
2331 dst
->node
[dst
->n
].sched
= isl_mat_copy(src
->node
[i
].sched
);
2332 dst
->node
[dst
->n
].sched_map
=
2333 isl_map_copy(src
->node
[i
].sched_map
);
2334 dst
->node
[dst
->n
].band
= src
->node
[i
].band
;
2335 dst
->node
[dst
->n
].band_id
= src
->node
[i
].band_id
;
2336 dst
->node
[dst
->n
].coincident
= src
->node
[i
].coincident
;
2343 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
2344 * to the dst dependence graph.
2345 * If the source or destination node of the edge is not in the destination
2346 * graph, then it must be a backward proximity edge and it should simply
2349 static int copy_edges(isl_ctx
*ctx
, struct isl_sched_graph
*dst
,
2350 struct isl_sched_graph
*src
,
2351 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
), int data
)
2354 enum isl_edge_type t
;
2357 for (i
= 0; i
< src
->n_edge
; ++i
) {
2358 struct isl_sched_edge
*edge
= &src
->edge
[i
];
2360 isl_union_map
*tagged_condition
;
2361 isl_union_map
*tagged_validity
;
2362 struct isl_sched_node
*dst_src
, *dst_dst
;
2364 if (!edge_pred(edge
, data
))
2367 if (isl_map_plain_is_empty(edge
->map
))
2370 dst_src
= graph_find_node(ctx
, dst
, edge
->src
->dim
);
2371 dst_dst
= graph_find_node(ctx
, dst
, edge
->dst
->dim
);
2372 if (!dst_src
|| !dst_dst
) {
2373 if (edge
->validity
|| edge
->conditional_validity
)
2374 isl_die(ctx
, isl_error_internal
,
2375 "backward (conditional) validity edge",
2380 map
= isl_map_copy(edge
->map
);
2381 tagged_condition
= isl_union_map_copy(edge
->tagged_condition
);
2382 tagged_validity
= isl_union_map_copy(edge
->tagged_validity
);
2384 dst
->edge
[dst
->n_edge
].src
= dst_src
;
2385 dst
->edge
[dst
->n_edge
].dst
= dst_dst
;
2386 dst
->edge
[dst
->n_edge
].map
= map
;
2387 dst
->edge
[dst
->n_edge
].tagged_condition
= tagged_condition
;
2388 dst
->edge
[dst
->n_edge
].tagged_validity
= tagged_validity
;
2389 dst
->edge
[dst
->n_edge
].validity
= edge
->validity
;
2390 dst
->edge
[dst
->n_edge
].proximity
= edge
->proximity
;
2391 dst
->edge
[dst
->n_edge
].coincidence
= edge
->coincidence
;
2392 dst
->edge
[dst
->n_edge
].condition
= edge
->condition
;
2393 dst
->edge
[dst
->n_edge
].conditional_validity
=
2394 edge
->conditional_validity
;
2397 if (edge
->tagged_condition
&& !tagged_condition
)
2399 if (edge
->tagged_validity
&& !tagged_validity
)
2402 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
2404 graph_find_edge(src
, t
, edge
->src
, edge
->dst
))
2406 if (graph_edge_table_add(ctx
, dst
, t
,
2407 &dst
->edge
[dst
->n_edge
- 1]) < 0)
2415 /* Given a "src" dependence graph that contains the nodes from "dst"
2416 * that satisfy node_pred, copy the schedule computed in "src"
2417 * for those nodes back to "dst".
2419 static int copy_schedule(struct isl_sched_graph
*dst
,
2420 struct isl_sched_graph
*src
,
2421 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
2426 for (i
= 0; i
< dst
->n
; ++i
) {
2427 if (!node_pred(&dst
->node
[i
], data
))
2429 isl_mat_free(dst
->node
[i
].sched
);
2430 isl_map_free(dst
->node
[i
].sched_map
);
2431 dst
->node
[i
].sched
= isl_mat_copy(src
->node
[src
->n
].sched
);
2432 dst
->node
[i
].sched_map
=
2433 isl_map_copy(src
->node
[src
->n
].sched_map
);
2437 dst
->max_row
= src
->max_row
;
2438 dst
->n_total_row
= src
->n_total_row
;
2439 dst
->n_band
= src
->n_band
;
2444 /* Compute the maximal number of variables over all nodes.
2445 * This is the maximal number of linearly independent schedule
2446 * rows that we need to compute.
2447 * Just in case we end up in a part of the dependence graph
2448 * with only lower-dimensional domains, we make sure we will
2449 * compute the required amount of extra linearly independent rows.
2451 static int compute_maxvar(struct isl_sched_graph
*graph
)
2456 for (i
= 0; i
< graph
->n
; ++i
) {
2457 struct isl_sched_node
*node
= &graph
->node
[i
];
2460 if (node_update_cmap(node
) < 0)
2462 nvar
= node
->nvar
+ graph
->n_row
- node
->rank
;
2463 if (nvar
> graph
->maxvar
)
2464 graph
->maxvar
= nvar
;
2470 static int compute_schedule(isl_ctx
*ctx
, struct isl_sched_graph
*graph
);
2471 static int compute_schedule_wcc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
);
2473 /* Compute a schedule for a subgraph of "graph". In particular, for
2474 * the graph composed of nodes that satisfy node_pred and edges that
2475 * that satisfy edge_pred. The caller should precompute the number
2476 * of nodes and edges that satisfy these predicates and pass them along
2477 * as "n" and "n_edge".
2478 * If the subgraph is known to consist of a single component, then wcc should
2479 * be set and then we call compute_schedule_wcc on the constructed subgraph.
2480 * Otherwise, we call compute_schedule, which will check whether the subgraph
2483 static int compute_sub_schedule(isl_ctx
*ctx
,
2484 struct isl_sched_graph
*graph
, int n
, int n_edge
,
2485 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
2486 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
2489 struct isl_sched_graph split
= { 0 };
2492 if (graph_alloc(ctx
, &split
, n
, n_edge
) < 0)
2494 if (copy_nodes(&split
, graph
, node_pred
, data
) < 0)
2496 if (graph_init_table(ctx
, &split
) < 0)
2498 for (t
= 0; t
<= isl_edge_last
; ++t
)
2499 split
.max_edge
[t
] = graph
->max_edge
[t
];
2500 if (graph_init_edge_tables(ctx
, &split
) < 0)
2502 if (copy_edges(ctx
, &split
, graph
, edge_pred
, data
) < 0)
2504 split
.n_row
= graph
->n_row
;
2505 split
.max_row
= graph
->max_row
;
2506 split
.n_total_row
= graph
->n_total_row
;
2507 split
.n_band
= graph
->n_band
;
2508 split
.band_start
= graph
->band_start
;
2510 if (wcc
&& compute_schedule_wcc(ctx
, &split
) < 0)
2512 if (!wcc
&& compute_schedule(ctx
, &split
) < 0)
2515 copy_schedule(graph
, &split
, node_pred
, data
);
2517 graph_free(ctx
, &split
);
2520 graph_free(ctx
, &split
);
2524 static int node_scc_exactly(struct isl_sched_node
*node
, int scc
)
2526 return node
->scc
== scc
;
2529 static int node_scc_at_most(struct isl_sched_node
*node
, int scc
)
2531 return node
->scc
<= scc
;
2534 static int node_scc_at_least(struct isl_sched_node
*node
, int scc
)
2536 return node
->scc
>= scc
;
2539 static int edge_scc_exactly(struct isl_sched_edge
*edge
, int scc
)
2541 return edge
->src
->scc
== scc
&& edge
->dst
->scc
== scc
;
2544 static int edge_dst_scc_at_most(struct isl_sched_edge
*edge
, int scc
)
2546 return edge
->dst
->scc
<= scc
;
2549 static int edge_src_scc_at_least(struct isl_sched_edge
*edge
, int scc
)
2551 return edge
->src
->scc
>= scc
;
2554 /* Pad the schedules of all nodes with zero rows such that in the end
2555 * they all have graph->n_total_row rows.
2556 * The extra rows don't belong to any band, so they get assigned band number -1.
2558 static int pad_schedule(struct isl_sched_graph
*graph
)
2562 for (i
= 0; i
< graph
->n
; ++i
) {
2563 struct isl_sched_node
*node
= &graph
->node
[i
];
2564 int row
= isl_mat_rows(node
->sched
);
2565 if (graph
->n_total_row
> row
) {
2566 isl_map_free(node
->sched_map
);
2567 node
->sched_map
= NULL
;
2569 node
->sched
= isl_mat_add_zero_rows(node
->sched
,
2570 graph
->n_total_row
- row
);
2573 for (j
= row
; j
< graph
->n_total_row
; ++j
)
2580 /* Reset the current band by dropping all its schedule rows.
2582 static int reset_band(struct isl_sched_graph
*graph
)
2587 drop
= graph
->n_total_row
- graph
->band_start
;
2588 graph
->n_total_row
-= drop
;
2589 graph
->n_row
-= drop
;
2591 for (i
= 0; i
< graph
->n
; ++i
) {
2592 struct isl_sched_node
*node
= &graph
->node
[i
];
2594 isl_map_free(node
->sched_map
);
2595 node
->sched_map
= NULL
;
2597 node
->sched
= isl_mat_drop_rows(node
->sched
,
2598 graph
->band_start
, drop
);
2607 /* Split the current graph into two parts and compute a schedule for each
2608 * part individually. In particular, one part consists of all SCCs up
2609 * to and including graph->src_scc, while the other part contains the other
2612 * The split is enforced in the schedule by constant rows with two different
2613 * values (0 and 1). These constant rows replace the previously computed rows
2614 * in the current band.
2615 * It would be possible to reuse them as the first rows in the next
2616 * band, but recomputing them may result in better rows as we are looking
2617 * at a smaller part of the dependence graph.
2619 * Since we do not enforce coincidence, we conservatively mark the
2620 * splitting row as not coincident.
2622 * The band_id of the second group is set to n, where n is the number
2623 * of nodes in the first group. This ensures that the band_ids over
2624 * the two groups remain disjoint, even if either or both of the two
2625 * groups contain independent components.
2627 static int compute_split_schedule(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2629 int i
, j
, n
, e1
, e2
;
2630 int n_total_row
, orig_total_row
;
2631 int n_band
, orig_band
;
2633 if (graph
->n_total_row
>= graph
->max_row
)
2634 isl_die(ctx
, isl_error_internal
,
2635 "too many schedule rows", return -1);
2637 if (reset_band(graph
) < 0)
2641 for (i
= 0; i
< graph
->n
; ++i
) {
2642 struct isl_sched_node
*node
= &graph
->node
[i
];
2643 int row
= isl_mat_rows(node
->sched
);
2644 int cols
= isl_mat_cols(node
->sched
);
2645 int before
= node
->scc
<= graph
->src_scc
;
2650 isl_map_free(node
->sched_map
);
2651 node
->sched_map
= NULL
;
2652 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
2655 node
->sched
= isl_mat_set_element_si(node
->sched
, row
, 0,
2657 for (j
= 1; j
< cols
; ++j
)
2658 node
->sched
= isl_mat_set_element_si(node
->sched
,
2660 node
->band
[graph
->n_total_row
] = graph
->n_band
;
2661 node
->coincident
[graph
->n_total_row
] = 0;
2665 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2666 if (graph
->edge
[i
].dst
->scc
<= graph
->src_scc
)
2668 if (graph
->edge
[i
].src
->scc
> graph
->src_scc
)
2672 graph
->n_total_row
++;
2675 for (i
= 0; i
< graph
->n
; ++i
) {
2676 struct isl_sched_node
*node
= &graph
->node
[i
];
2677 if (node
->scc
> graph
->src_scc
)
2678 node
->band_id
[graph
->n_band
] = n
;
2681 orig_total_row
= graph
->n_total_row
;
2682 orig_band
= graph
->n_band
;
2683 if (compute_sub_schedule(ctx
, graph
, n
, e1
,
2684 &node_scc_at_most
, &edge_dst_scc_at_most
,
2685 graph
->src_scc
, 0) < 0)
2687 n_total_row
= graph
->n_total_row
;
2688 graph
->n_total_row
= orig_total_row
;
2689 n_band
= graph
->n_band
;
2690 graph
->n_band
= orig_band
;
2691 if (compute_sub_schedule(ctx
, graph
, graph
->n
- n
, e2
,
2692 &node_scc_at_least
, &edge_src_scc_at_least
,
2693 graph
->src_scc
+ 1, 0) < 0)
2695 if (n_total_row
> graph
->n_total_row
)
2696 graph
->n_total_row
= n_total_row
;
2697 if (n_band
> graph
->n_band
)
2698 graph
->n_band
= n_band
;
2700 return pad_schedule(graph
);
2703 /* Compute the next band of the schedule after updating the dependence
2704 * relations based on the the current schedule.
2706 static int compute_next_band(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2708 if (update_edges(ctx
, graph
) < 0)
2712 return compute_schedule(ctx
, graph
);
2715 /* Add constraints to graph->lp that force the dependence "map" (which
2716 * is part of the dependence relation of "edge")
2717 * to be respected and attempt to carry it, where the edge is one from
2718 * a node j to itself. "pos" is the sequence number of the given map.
2719 * That is, add constraints that enforce
2721 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
2722 * = c_j_x (y - x) >= e_i
2724 * for each (x,y) in R.
2725 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2726 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
2727 * with each coefficient in c_j_x represented as a pair of non-negative
2730 static int add_intra_constraints(struct isl_sched_graph
*graph
,
2731 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
2734 isl_ctx
*ctx
= isl_map_get_ctx(map
);
2736 isl_dim_map
*dim_map
;
2737 isl_basic_set
*coef
;
2738 struct isl_sched_node
*node
= edge
->src
;
2740 coef
= intra_coefficients(graph
, map
);
2744 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
2746 total
= isl_basic_set_total_dim(graph
->lp
);
2747 dim_map
= isl_dim_map_alloc(ctx
, total
);
2748 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
2749 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
2750 isl_space_dim(dim
, isl_dim_set
), 1,
2752 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
2753 isl_space_dim(dim
, isl_dim_set
), 1,
2755 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
2756 coef
->n_eq
, coef
->n_ineq
);
2757 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
2759 isl_space_free(dim
);
2764 /* Add constraints to graph->lp that force the dependence "map" (which
2765 * is part of the dependence relation of "edge")
2766 * to be respected and attempt to carry it, where the edge is one from
2767 * node j to node k. "pos" is the sequence number of the given map.
2768 * That is, add constraints that enforce
2770 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
2772 * for each (x,y) in R.
2773 * We obtain general constraints on coefficients (c_0, c_n, c_x)
2774 * of valid constraints for R and then plug in
2775 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
2776 * with each coefficient (except e_i, c_k_0 and c_j_0)
2777 * represented as a pair of non-negative coefficients.
2779 static int add_inter_constraints(struct isl_sched_graph
*graph
,
2780 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
2783 isl_ctx
*ctx
= isl_map_get_ctx(map
);
2785 isl_dim_map
*dim_map
;
2786 isl_basic_set
*coef
;
2787 struct isl_sched_node
*src
= edge
->src
;
2788 struct isl_sched_node
*dst
= edge
->dst
;
2790 coef
= inter_coefficients(graph
, map
);
2794 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
2796 total
= isl_basic_set_total_dim(graph
->lp
);
2797 dim_map
= isl_dim_map_alloc(ctx
, total
);
2799 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
2801 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, 1);
2802 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, -1);
2803 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, 1);
2804 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
2805 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
2807 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
2808 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
2811 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, -1);
2812 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, 1);
2813 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, -1);
2814 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
2815 isl_space_dim(dim
, isl_dim_set
), 1,
2817 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
2818 isl_space_dim(dim
, isl_dim_set
), 1,
2821 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
2822 coef
->n_eq
, coef
->n_ineq
);
2823 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
2825 isl_space_free(dim
);
2830 /* Add constraints to graph->lp that force all (conditional) validity
2831 * dependences to be respected and attempt to carry them.
2833 static int add_all_constraints(struct isl_sched_graph
*graph
)
2839 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2840 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2842 if (!edge
->validity
&& !edge
->conditional_validity
)
2845 for (j
= 0; j
< edge
->map
->n
; ++j
) {
2846 isl_basic_map
*bmap
;
2849 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
2850 map
= isl_map_from_basic_map(bmap
);
2852 if (edge
->src
== edge
->dst
&&
2853 add_intra_constraints(graph
, edge
, map
, pos
) < 0)
2855 if (edge
->src
!= edge
->dst
&&
2856 add_inter_constraints(graph
, edge
, map
, pos
) < 0)
2865 /* Count the number of equality and inequality constraints
2866 * that will be added to the carry_lp problem.
2867 * We count each edge exactly once.
2869 static int count_all_constraints(struct isl_sched_graph
*graph
,
2870 int *n_eq
, int *n_ineq
)
2874 *n_eq
= *n_ineq
= 0;
2875 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2876 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2877 for (j
= 0; j
< edge
->map
->n
; ++j
) {
2878 isl_basic_map
*bmap
;
2881 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
2882 map
= isl_map_from_basic_map(bmap
);
2884 if (count_map_constraints(graph
, edge
, map
,
2885 n_eq
, n_ineq
, 1, 0) < 0)
2893 /* Construct an LP problem for finding schedule coefficients
2894 * such that the schedule carries as many dependences as possible.
2895 * In particular, for each dependence i, we bound the dependence distance
2896 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
2897 * of all e_i's. Dependence with e_i = 0 in the solution are simply
2898 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
2899 * Note that if the dependence relation is a union of basic maps,
2900 * then we have to consider each basic map individually as it may only
2901 * be possible to carry the dependences expressed by some of those
2902 * basic maps and not all off them.
2903 * Below, we consider each of those basic maps as a separate "edge".
2905 * All variables of the LP are non-negative. The actual coefficients
2906 * may be negative, so each coefficient is represented as the difference
2907 * of two non-negative variables. The negative part always appears
2908 * immediately before the positive part.
2909 * Other than that, the variables have the following order
2911 * - sum of (1 - e_i) over all edges
2912 * - sum of positive and negative parts of all c_n coefficients
2913 * (unconstrained when computing non-parametric schedules)
2914 * - sum of positive and negative parts of all c_x coefficients
2919 * - positive and negative parts of c_i_n (if parametric)
2920 * - positive and negative parts of c_i_x
2922 * The constraints are those from the (validity) edges plus three equalities
2923 * to express the sums and n_edge inequalities to express e_i <= 1.
2925 static int setup_carry_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2935 for (i
= 0; i
< graph
->n_edge
; ++i
)
2936 n_edge
+= graph
->edge
[i
].map
->n
;
2939 for (i
= 0; i
< graph
->n
; ++i
) {
2940 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
2941 node
->start
= total
;
2942 total
+= 1 + 2 * (node
->nparam
+ node
->nvar
);
2945 if (count_all_constraints(graph
, &n_eq
, &n_ineq
) < 0)
2947 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2950 dim
= isl_space_set_alloc(ctx
, 0, total
);
2951 isl_basic_set_free(graph
->lp
);
2954 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
2955 graph
->lp
= isl_basic_set_set_rational(graph
->lp
);
2957 k
= isl_basic_set_alloc_equality(graph
->lp
);
2960 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2961 isl_int_set_si(graph
->lp
->eq
[k
][0], -n_edge
);
2962 isl_int_set_si(graph
->lp
->eq
[k
][1], 1);
2963 for (i
= 0; i
< n_edge
; ++i
)
2964 isl_int_set_si(graph
->lp
->eq
[k
][4 + i
], 1);
2966 k
= isl_basic_set_alloc_equality(graph
->lp
);
2969 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2970 isl_int_set_si(graph
->lp
->eq
[k
][2], -1);
2971 for (i
= 0; i
< graph
->n
; ++i
) {
2972 int pos
= 1 + graph
->node
[i
].start
+ 1;
2974 for (j
= 0; j
< 2 * graph
->node
[i
].nparam
; ++j
)
2975 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2978 k
= isl_basic_set_alloc_equality(graph
->lp
);
2981 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2982 isl_int_set_si(graph
->lp
->eq
[k
][3], -1);
2983 for (i
= 0; i
< graph
->n
; ++i
) {
2984 struct isl_sched_node
*node
= &graph
->node
[i
];
2985 int pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
2987 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
2988 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2991 for (i
= 0; i
< n_edge
; ++i
) {
2992 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2995 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2996 isl_int_set_si(graph
->lp
->ineq
[k
][4 + i
], -1);
2997 isl_int_set_si(graph
->lp
->ineq
[k
][0], 1);
3000 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
3002 if (add_all_constraints(graph
) < 0)
3008 /* If the schedule_split_scaled option is set and if the linear
3009 * parts of the scheduling rows for all nodes in the graphs have
3010 * non-trivial common divisor, then split off the constant term
3011 * from the linear part.
3012 * The constant term is then placed in a separate band and
3013 * the linear part is reduced.
3015 static int split_scaled(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3021 if (!ctx
->opt
->schedule_split_scaled
)
3026 if (graph
->n_total_row
>= graph
->max_row
)
3027 isl_die(ctx
, isl_error_internal
,
3028 "too many schedule rows", return -1);
3031 isl_int_init(gcd_i
);
3033 isl_int_set_si(gcd
, 0);
3035 row
= isl_mat_rows(graph
->node
[0].sched
) - 1;
3037 for (i
= 0; i
< graph
->n
; ++i
) {
3038 struct isl_sched_node
*node
= &graph
->node
[i
];
3039 int cols
= isl_mat_cols(node
->sched
);
3041 isl_seq_gcd(node
->sched
->row
[row
] + 1, cols
- 1, &gcd_i
);
3042 isl_int_gcd(gcd
, gcd
, gcd_i
);
3045 isl_int_clear(gcd_i
);
3047 if (isl_int_cmp_si(gcd
, 1) <= 0) {
3054 for (i
= 0; i
< graph
->n
; ++i
) {
3055 struct isl_sched_node
*node
= &graph
->node
[i
];
3057 isl_map_free(node
->sched_map
);
3058 node
->sched_map
= NULL
;
3059 node
->sched
= isl_mat_add_zero_rows(node
->sched
, 1);
3062 isl_int_fdiv_r(node
->sched
->row
[row
+ 1][0],
3063 node
->sched
->row
[row
][0], gcd
);
3064 isl_int_fdiv_q(node
->sched
->row
[row
][0],
3065 node
->sched
->row
[row
][0], gcd
);
3066 isl_int_mul(node
->sched
->row
[row
][0],
3067 node
->sched
->row
[row
][0], gcd
);
3068 node
->sched
= isl_mat_scale_down_row(node
->sched
, row
, gcd
);
3071 node
->band
[graph
->n_total_row
] = graph
->n_band
;
3074 graph
->n_total_row
++;
3083 static int compute_component_schedule(isl_ctx
*ctx
,
3084 struct isl_sched_graph
*graph
);
3086 /* Is the schedule row "sol" trivial on node "node"?
3087 * That is, is the solution zero on the dimensions orthogonal to
3088 * the previously found solutions?
3089 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3091 * Each coefficient is represented as the difference between
3092 * two non-negative values in "sol". "sol" has been computed
3093 * in terms of the original iterators (i.e., without use of cmap).
3094 * We construct the schedule row s and write it as a linear
3095 * combination of (linear combinations of) previously computed schedule rows.
3096 * s = Q c or c = U s.
3097 * If the final entries of c are all zero, then the solution is trivial.
3099 static int is_trivial(struct isl_sched_node
*node
, __isl_keep isl_vec
*sol
)
3109 if (node
->nvar
== node
->rank
)
3112 ctx
= isl_vec_get_ctx(sol
);
3113 node_sol
= isl_vec_alloc(ctx
, node
->nvar
);
3117 pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
3119 for (i
= 0; i
< node
->nvar
; ++i
)
3120 isl_int_sub(node_sol
->el
[i
],
3121 sol
->el
[pos
+ 2 * i
+ 1], sol
->el
[pos
+ 2 * i
]);
3123 node_sol
= isl_mat_vec_product(isl_mat_copy(node
->cinv
), node_sol
);
3128 trivial
= isl_seq_first_non_zero(node_sol
->el
+ node
->rank
,
3129 node
->nvar
- node
->rank
) == -1;
3131 isl_vec_free(node_sol
);
3136 /* Is the schedule row "sol" trivial on any node where it should
3138 * "sol" has been computed in terms of the original iterators
3139 * (i.e., without use of cmap).
3140 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3142 static int is_any_trivial(struct isl_sched_graph
*graph
,
3143 __isl_keep isl_vec
*sol
)
3147 for (i
= 0; i
< graph
->n
; ++i
) {
3148 struct isl_sched_node
*node
= &graph
->node
[i
];
3151 if (!needs_row(graph
, node
))
3153 trivial
= is_trivial(node
, sol
);
3154 if (trivial
< 0 || trivial
)
3161 /* Construct a schedule row for each node such that as many dependences
3162 * as possible are carried and then continue with the next band.
3164 * If the computed schedule row turns out to be trivial on one or
3165 * more nodes where it should not be trivial, then we throw it away
3166 * and try again on each component separately.
3168 static int carry_dependences(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3177 for (i
= 0; i
< graph
->n_edge
; ++i
)
3178 n_edge
+= graph
->edge
[i
].map
->n
;
3180 if (setup_carry_lp(ctx
, graph
) < 0)
3183 lp
= isl_basic_set_copy(graph
->lp
);
3184 sol
= isl_tab_basic_set_non_neg_lexmin(lp
);
3188 if (sol
->size
== 0) {
3190 isl_die(ctx
, isl_error_internal
,
3191 "error in schedule construction", return -1);
3194 isl_int_divexact(sol
->el
[1], sol
->el
[1], sol
->el
[0]);
3195 if (isl_int_cmp_si(sol
->el
[1], n_edge
) >= 0) {
3197 isl_die(ctx
, isl_error_unknown
,
3198 "unable to carry dependences", return -1);
3201 trivial
= is_any_trivial(graph
, sol
);
3203 sol
= isl_vec_free(sol
);
3204 } else if (trivial
) {
3207 return compute_component_schedule(ctx
, graph
);
3208 isl_die(ctx
, isl_error_unknown
,
3209 "unable to construct non-trivial solution", return -1);
3212 if (update_schedule(graph
, sol
, 0, 0) < 0)
3215 if (split_scaled(ctx
, graph
) < 0)
3218 return compute_next_band(ctx
, graph
);
3221 /* Are there any (non-empty) (conditional) validity edges in the graph?
3223 static int has_validity_edges(struct isl_sched_graph
*graph
)
3227 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3230 empty
= isl_map_plain_is_empty(graph
->edge
[i
].map
);
3235 if (graph
->edge
[i
].validity
||
3236 graph
->edge
[i
].conditional_validity
)
3243 /* Should we apply a Feautrier step?
3244 * That is, did the user request the Feautrier algorithm and are
3245 * there any validity dependences (left)?
3247 static int need_feautrier_step(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3249 if (ctx
->opt
->schedule_algorithm
!= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
)
3252 return has_validity_edges(graph
);
3255 /* Compute a schedule for a connected dependence graph using Feautrier's
3256 * multi-dimensional scheduling algorithm.
3257 * The original algorithm is described in [1].
3258 * The main idea is to minimize the number of scheduling dimensions, by
3259 * trying to satisfy as many dependences as possible per scheduling dimension.
3261 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
3262 * Problem, Part II: Multi-Dimensional Time.
3263 * In Intl. Journal of Parallel Programming, 1992.
3265 static int compute_schedule_wcc_feautrier(isl_ctx
*ctx
,
3266 struct isl_sched_graph
*graph
)
3268 return carry_dependences(ctx
, graph
);
3271 /* Turn off the "local" bit on all (condition) edges.
3273 static void clear_local_edges(struct isl_sched_graph
*graph
)
3277 for (i
= 0; i
< graph
->n_edge
; ++i
)
3278 if (graph
->edge
[i
].condition
)
3279 graph
->edge
[i
].local
= 0;
3282 /* Does "graph" have both condition and conditional validity edges?
3284 static int need_condition_check(struct isl_sched_graph
*graph
)
3287 int any_condition
= 0;
3288 int any_conditional_validity
= 0;
3290 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3291 if (graph
->edge
[i
].condition
)
3293 if (graph
->edge
[i
].conditional_validity
)
3294 any_conditional_validity
= 1;
3297 return any_condition
&& any_conditional_validity
;
3300 /* Does "graph" contain any coincidence edge?
3302 static int has_any_coincidence(struct isl_sched_graph
*graph
)
3306 for (i
= 0; i
< graph
->n_edge
; ++i
)
3307 if (graph
->edge
[i
].coincidence
)
3313 /* Extract the final schedule row as a map with the iteration domain
3314 * of "node" as domain.
3316 static __isl_give isl_map
*final_row(struct isl_sched_node
*node
)
3318 isl_local_space
*ls
;
3322 row
= isl_mat_rows(node
->sched
) - 1;
3323 ls
= isl_local_space_from_space(isl_space_copy(node
->dim
));
3324 aff
= extract_schedule_row(ls
, node
, row
);
3325 return isl_map_from_aff(aff
);
3328 /* Is the conditional validity dependence in the edge with index "edge_index"
3329 * violated by the latest (i.e., final) row of the schedule?
3330 * That is, is i scheduled after j
3331 * for any conditional validity dependence i -> j?
3333 static int is_violated(struct isl_sched_graph
*graph
, int edge_index
)
3335 isl_map
*src_sched
, *dst_sched
, *map
;
3336 struct isl_sched_edge
*edge
= &graph
->edge
[edge_index
];
3339 src_sched
= final_row(edge
->src
);
3340 dst_sched
= final_row(edge
->dst
);
3341 map
= isl_map_copy(edge
->map
);
3342 map
= isl_map_apply_domain(map
, src_sched
);
3343 map
= isl_map_apply_range(map
, dst_sched
);
3344 map
= isl_map_order_gt(map
, isl_dim_in
, 0, isl_dim_out
, 0);
3345 empty
= isl_map_is_empty(map
);
3354 /* Does the domain of "umap" intersect "uset"?
3356 static int domain_intersects(__isl_keep isl_union_map
*umap
,
3357 __isl_keep isl_union_set
*uset
)
3361 umap
= isl_union_map_copy(umap
);
3362 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(uset
));
3363 empty
= isl_union_map_is_empty(umap
);
3364 isl_union_map_free(umap
);
3366 return empty
< 0 ? -1 : !empty
;
3369 /* Does the range of "umap" intersect "uset"?
3371 static int range_intersects(__isl_keep isl_union_map
*umap
,
3372 __isl_keep isl_union_set
*uset
)
3376 umap
= isl_union_map_copy(umap
);
3377 umap
= isl_union_map_intersect_range(umap
, isl_union_set_copy(uset
));
3378 empty
= isl_union_map_is_empty(umap
);
3379 isl_union_map_free(umap
);
3381 return empty
< 0 ? -1 : !empty
;
3384 /* Are the condition dependences of "edge" local with respect to
3385 * the current schedule?
3387 * That is, are domain and range of the condition dependences mapped
3388 * to the same point?
3390 * In other words, is the condition false?
3392 static int is_condition_false(struct isl_sched_edge
*edge
)
3394 isl_union_map
*umap
;
3395 isl_map
*map
, *sched
, *test
;
3398 umap
= isl_union_map_copy(edge
->tagged_condition
);
3399 umap
= isl_union_map_zip(umap
);
3400 umap
= isl_union_set_unwrap(isl_union_map_domain(umap
));
3401 map
= isl_map_from_union_map(umap
);
3403 sched
= node_extract_schedule(edge
->src
);
3404 map
= isl_map_apply_domain(map
, sched
);
3405 sched
= node_extract_schedule(edge
->dst
);
3406 map
= isl_map_apply_range(map
, sched
);
3408 test
= isl_map_identity(isl_map_get_space(map
));
3409 local
= isl_map_is_subset(map
, test
);
3416 /* Does "graph" have any satisfied condition edges that
3417 * are adjacent to the conditional validity constraint with
3418 * domain "conditional_source" and range "conditional_sink"?
3420 * A satisfied condition is one that is not local.
3421 * If a condition was forced to be local already (i.e., marked as local)
3422 * then there is no need to check if it is in fact local.
3424 * Additionally, mark all adjacent condition edges found as local.
3426 static int has_adjacent_true_conditions(struct isl_sched_graph
*graph
,
3427 __isl_keep isl_union_set
*conditional_source
,
3428 __isl_keep isl_union_set
*conditional_sink
)
3433 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3434 int adjacent
, local
;
3435 isl_union_map
*condition
;
3437 if (!graph
->edge
[i
].condition
)
3439 if (graph
->edge
[i
].local
)
3442 condition
= graph
->edge
[i
].tagged_condition
;
3443 adjacent
= domain_intersects(condition
, conditional_sink
);
3444 if (adjacent
>= 0 && !adjacent
)
3445 adjacent
= range_intersects(condition
,
3446 conditional_source
);
3452 graph
->edge
[i
].local
= 1;
3454 local
= is_condition_false(&graph
->edge
[i
]);
3464 /* Are there any violated conditional validity dependences with
3465 * adjacent condition dependences that are not local with respect
3466 * to the current schedule?
3467 * That is, is the conditional validity constraint violated?
3469 * Additionally, mark all those adjacent condition dependences as local.
3470 * We also mark those adjacent condition dependences that were not marked
3471 * as local before, but just happened to be local already. This ensures
3472 * that they remain local if the schedule is recomputed.
3474 * We first collect domain and range of all violated conditional validity
3475 * dependences and then check if there are any adjacent non-local
3476 * condition dependences.
3478 static int has_violated_conditional_constraint(isl_ctx
*ctx
,
3479 struct isl_sched_graph
*graph
)
3483 isl_union_set
*source
, *sink
;
3485 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3486 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3487 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3488 isl_union_set
*uset
;
3489 isl_union_map
*umap
;
3492 if (!graph
->edge
[i
].conditional_validity
)
3495 violated
= is_violated(graph
, i
);
3503 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
3504 uset
= isl_union_map_domain(umap
);
3505 source
= isl_union_set_union(source
, uset
);
3506 source
= isl_union_set_coalesce(source
);
3508 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
3509 uset
= isl_union_map_range(umap
);
3510 sink
= isl_union_set_union(sink
, uset
);
3511 sink
= isl_union_set_coalesce(sink
);
3515 any
= has_adjacent_true_conditions(graph
, source
, sink
);
3517 isl_union_set_free(source
);
3518 isl_union_set_free(sink
);
3521 isl_union_set_free(source
);
3522 isl_union_set_free(sink
);
3526 /* Compute a schedule for a connected dependence graph.
3527 * We try to find a sequence of as many schedule rows as possible that result
3528 * in non-negative dependence distances (independent of the previous rows
3529 * in the sequence, i.e., such that the sequence is tilable), with as
3530 * many of the initial rows as possible satisfying the coincidence constraints.
3531 * If we can't find any more rows we either
3532 * - split between SCCs and start over (assuming we found an interesting
3533 * pair of SCCs between which to split)
3534 * - continue with the next band (assuming the current band has at least
3536 * - try to carry as many dependences as possible and continue with the next
3539 * If Feautrier's algorithm is selected, we first recursively try to satisfy
3540 * as many validity dependences as possible. When all validity dependences
3541 * are satisfied we extend the schedule to a full-dimensional schedule.
3543 * If we manage to complete the schedule, we finish off by topologically
3544 * sorting the statements based on the remaining dependences.
3546 * If ctx->opt->schedule_outer_coincidence is set, then we force the
3547 * outermost dimension to satisfy the coincidence constraints. If this
3548 * turns out to be impossible, we fall back on the general scheme above
3549 * and try to carry as many dependences as possible.
3551 * If "graph" contains both condition and conditional validity dependences,
3552 * then we need to check that that the conditional schedule constraint
3553 * is satisfied, i.e., there are no violated conditional validity dependences
3554 * that are adjacent to any non-local condition dependences.
3555 * If there are, then we mark all those adjacent condition dependences
3556 * as local and recompute the current band. Those dependences that
3557 * are marked local will then be forced to be local.
3558 * The initial computation is performed with no dependences marked as local.
3559 * If we are lucky, then there will be no violated conditional validity
3560 * dependences adjacent to any non-local condition dependences.
3561 * Otherwise, we mark some additional condition dependences as local and
3562 * recompute. We continue this process until there are no violations left or
3563 * until we are no longer able to compute a schedule.
3564 * Since there are only a finite number of dependences,
3565 * there will only be a finite number of iterations.
3567 static int compute_schedule_wcc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3569 int has_coincidence
;
3570 int use_coincidence
;
3571 int force_coincidence
= 0;
3572 int check_conditional
;
3574 if (detect_sccs(ctx
, graph
) < 0)
3576 if (sort_sccs(graph
) < 0)
3579 if (compute_maxvar(graph
) < 0)
3582 if (need_feautrier_step(ctx
, graph
))
3583 return compute_schedule_wcc_feautrier(ctx
, graph
);
3585 clear_local_edges(graph
);
3586 check_conditional
= need_condition_check(graph
);
3587 has_coincidence
= has_any_coincidence(graph
);
3589 if (ctx
->opt
->schedule_outer_coincidence
)
3590 force_coincidence
= 1;
3592 use_coincidence
= has_coincidence
;
3593 while (graph
->n_row
< graph
->maxvar
) {
3598 graph
->src_scc
= -1;
3599 graph
->dst_scc
= -1;
3601 if (setup_lp(ctx
, graph
, use_coincidence
) < 0)
3603 sol
= solve_lp(graph
);
3606 if (sol
->size
== 0) {
3607 int empty
= graph
->n_total_row
== graph
->band_start
;
3610 if (use_coincidence
&& (!force_coincidence
|| !empty
)) {
3611 use_coincidence
= 0;
3614 if (!ctx
->opt
->schedule_maximize_band_depth
&& !empty
)
3615 return compute_next_band(ctx
, graph
);
3616 if (graph
->src_scc
>= 0)
3617 return compute_split_schedule(ctx
, graph
);
3619 return compute_next_band(ctx
, graph
);
3620 return carry_dependences(ctx
, graph
);
3622 coincident
= !has_coincidence
|| use_coincidence
;
3623 if (update_schedule(graph
, sol
, 1, coincident
) < 0)
3626 if (!check_conditional
)
3628 violated
= has_violated_conditional_constraint(ctx
, graph
);
3633 if (reset_band(graph
) < 0)
3635 use_coincidence
= has_coincidence
;
3638 if (graph
->n_total_row
> graph
->band_start
)
3640 return sort_statements(ctx
, graph
);
3643 /* Add a row to the schedules that separates the SCCs and move
3646 static int split_on_scc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3650 if (graph
->n_total_row
>= graph
->max_row
)
3651 isl_die(ctx
, isl_error_internal
,
3652 "too many schedule rows", return -1);
3654 for (i
= 0; i
< graph
->n
; ++i
) {
3655 struct isl_sched_node
*node
= &graph
->node
[i
];
3656 int row
= isl_mat_rows(node
->sched
);
3658 isl_map_free(node
->sched_map
);
3659 node
->sched_map
= NULL
;
3660 node
->sched
= isl_mat_add_zero_rows(node
->sched
, 1);
3661 node
->sched
= isl_mat_set_element_si(node
->sched
, row
, 0,
3665 node
->band
[graph
->n_total_row
] = graph
->n_band
;
3668 graph
->n_total_row
++;
3674 /* Compute a schedule for each component (identified by node->scc)
3675 * of the dependence graph separately and then combine the results.
3676 * Depending on the setting of schedule_fuse, a component may be
3677 * either weakly or strongly connected.
3679 * The band_id is adjusted such that each component has a separate id.
3680 * Note that the band_id may have already been set to a value different
3681 * from zero by compute_split_schedule.
3683 static int compute_component_schedule(isl_ctx
*ctx
,
3684 struct isl_sched_graph
*graph
)
3688 int n_total_row
, orig_total_row
;
3689 int n_band
, orig_band
;
3691 if (ctx
->opt
->schedule_fuse
== ISL_SCHEDULE_FUSE_MIN
||
3692 ctx
->opt
->schedule_separate_components
)
3693 if (split_on_scc(ctx
, graph
) < 0)
3697 orig_total_row
= graph
->n_total_row
;
3699 orig_band
= graph
->n_band
;
3700 for (i
= 0; i
< graph
->n
; ++i
)
3701 graph
->node
[i
].band_id
[graph
->n_band
] += graph
->node
[i
].scc
;
3702 for (wcc
= 0; wcc
< graph
->scc
; ++wcc
) {
3704 for (i
= 0; i
< graph
->n
; ++i
)
3705 if (graph
->node
[i
].scc
== wcc
)
3708 for (i
= 0; i
< graph
->n_edge
; ++i
)
3709 if (graph
->edge
[i
].src
->scc
== wcc
&&
3710 graph
->edge
[i
].dst
->scc
== wcc
)
3713 if (compute_sub_schedule(ctx
, graph
, n
, n_edge
,
3715 &edge_scc_exactly
, wcc
, 1) < 0)
3717 if (graph
->n_total_row
> n_total_row
)
3718 n_total_row
= graph
->n_total_row
;
3719 graph
->n_total_row
= orig_total_row
;
3720 if (graph
->n_band
> n_band
)
3721 n_band
= graph
->n_band
;
3722 graph
->n_band
= orig_band
;
3725 graph
->n_total_row
= n_total_row
;
3726 graph
->n_band
= n_band
;
3728 return pad_schedule(graph
);
3731 /* Compute a schedule for the given dependence graph.
3732 * We first check if the graph is connected (through validity and conditional
3733 * validity dependences) and, if not, compute a schedule
3734 * for each component separately.
3735 * If schedule_fuse is set to minimal fusion, then we check for strongly
3736 * connected components instead and compute a separate schedule for
3737 * each such strongly connected component.
3739 static int compute_schedule(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3741 if (ctx
->opt
->schedule_fuse
== ISL_SCHEDULE_FUSE_MIN
) {
3742 if (detect_sccs(ctx
, graph
) < 0)
3745 if (detect_wccs(ctx
, graph
) < 0)
3750 return compute_component_schedule(ctx
, graph
);
3752 return compute_schedule_wcc(ctx
, graph
);
3755 /* Compute a schedule on sc->domain that respects the given schedule
3758 * In particular, the schedule respects all the validity dependences.
3759 * If the default isl scheduling algorithm is used, it tries to minimize
3760 * the dependence distances over the proximity dependences.
3761 * If Feautrier's scheduling algorithm is used, the proximity dependence
3762 * distances are only minimized during the extension to a full-dimensional
3765 * If there are any condition and conditional validity dependences,
3766 * then the conditional validity dependences may be violated inside
3767 * a tilable band, provided they have no adjacent non-local
3768 * condition dependences.
3770 __isl_give isl_schedule
*isl_schedule_constraints_compute_schedule(
3771 __isl_take isl_schedule_constraints
*sc
)
3773 isl_ctx
*ctx
= isl_schedule_constraints_get_ctx(sc
);
3774 struct isl_sched_graph graph
= { 0 };
3775 isl_schedule
*sched
;
3776 struct isl_extract_edge_data data
;
3777 enum isl_edge_type i
;
3779 sc
= isl_schedule_constraints_align_params(sc
);
3783 graph
.n
= isl_union_set_n_set(sc
->domain
);
3786 if (graph_alloc(ctx
, &graph
, graph
.n
,
3787 isl_schedule_constraints_n_map(sc
)) < 0)
3789 if (compute_max_row(&graph
, sc
->domain
) < 0)
3793 if (isl_union_set_foreach_set(sc
->domain
, &extract_node
, &graph
) < 0)
3795 if (graph_init_table(ctx
, &graph
) < 0)
3797 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
3798 graph
.max_edge
[i
] = isl_union_map_n_map(sc
->constraint
[i
]);
3799 if (graph_init_edge_tables(ctx
, &graph
) < 0)
3802 data
.graph
= &graph
;
3803 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
3805 if (isl_union_map_foreach_map(sc
->constraint
[i
],
3806 &extract_edge
, &data
) < 0)
3810 if (compute_schedule(ctx
, &graph
) < 0)
3814 sched
= extract_schedule(&graph
, isl_union_set_get_space(sc
->domain
));
3816 graph_free(ctx
, &graph
);
3817 isl_schedule_constraints_free(sc
);
3821 graph_free(ctx
, &graph
);
3822 isl_schedule_constraints_free(sc
);
3826 /* Compute a schedule for the given union of domains that respects
3827 * all the validity dependences and minimizes
3828 * the dependence distances over the proximity dependences.
3830 * This function is kept for backward compatibility.
3832 __isl_give isl_schedule
*isl_union_set_compute_schedule(
3833 __isl_take isl_union_set
*domain
,
3834 __isl_take isl_union_map
*validity
,
3835 __isl_take isl_union_map
*proximity
)
3837 isl_schedule_constraints
*sc
;
3839 sc
= isl_schedule_constraints_on_domain(domain
);
3840 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
3841 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
3843 return isl_schedule_constraints_compute_schedule(sc
);
3846 void *isl_schedule_free(__isl_take isl_schedule
*sched
)
3852 if (--sched
->ref
> 0)
3855 for (i
= 0; i
< sched
->n
; ++i
) {
3856 isl_multi_aff_free(sched
->node
[i
].sched
);
3857 free(sched
->node
[i
].band_end
);
3858 free(sched
->node
[i
].band_id
);
3859 free(sched
->node
[i
].coincident
);
3861 isl_space_free(sched
->dim
);
3862 isl_band_list_free(sched
->band_forest
);
3867 isl_ctx
*isl_schedule_get_ctx(__isl_keep isl_schedule
*schedule
)
3869 return schedule
? isl_space_get_ctx(schedule
->dim
) : NULL
;
3872 /* Set max_out to the maximal number of output dimensions over
3875 static int update_max_out(__isl_take isl_map
*map
, void *user
)
3877 int *max_out
= user
;
3878 int n_out
= isl_map_dim(map
, isl_dim_out
);
3880 if (n_out
> *max_out
)
3887 /* Internal data structure for map_pad_range.
3889 * "max_out" is the maximal schedule dimension.
3890 * "res" collects the results.
3892 struct isl_pad_schedule_map_data
{
3897 /* Pad the range of the given map with zeros to data->max_out and
3898 * then add the result to data->res.
3900 static int map_pad_range(__isl_take isl_map
*map
, void *user
)
3902 struct isl_pad_schedule_map_data
*data
= user
;
3904 int n_out
= isl_map_dim(map
, isl_dim_out
);
3906 map
= isl_map_add_dims(map
, isl_dim_out
, data
->max_out
- n_out
);
3907 for (i
= n_out
; i
< data
->max_out
; ++i
)
3908 map
= isl_map_fix_si(map
, isl_dim_out
, i
, 0);
3910 data
->res
= isl_union_map_add_map(data
->res
, map
);
3917 /* Pad the ranges of the maps in the union map with zeros such they all have
3918 * the same dimension.
3920 static __isl_give isl_union_map
*pad_schedule_map(
3921 __isl_take isl_union_map
*umap
)
3923 struct isl_pad_schedule_map_data data
;
3927 if (isl_union_map_n_map(umap
) <= 1)
3931 if (isl_union_map_foreach_map(umap
, &update_max_out
, &data
.max_out
) < 0)
3932 return isl_union_map_free(umap
);
3934 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3935 if (isl_union_map_foreach_map(umap
, &map_pad_range
, &data
) < 0)
3936 data
.res
= isl_union_map_free(data
.res
);
3938 isl_union_map_free(umap
);
3942 /* Return an isl_union_map of the schedule. If we have already constructed
3943 * a band forest, then this band forest may have been modified so we need
3944 * to extract the isl_union_map from the forest rather than from
3945 * the originally computed schedule. This reconstructed schedule map
3946 * then needs to be padded with zeros to unify the schedule space
3947 * since the result of isl_band_list_get_suffix_schedule may not have
3948 * a unified schedule space.
3950 __isl_give isl_union_map
*isl_schedule_get_map(__isl_keep isl_schedule
*sched
)
3953 isl_union_map
*umap
;
3958 if (sched
->band_forest
) {
3959 umap
= isl_band_list_get_suffix_schedule(sched
->band_forest
);
3960 return pad_schedule_map(umap
);
3963 umap
= isl_union_map_empty(isl_space_copy(sched
->dim
));
3964 for (i
= 0; i
< sched
->n
; ++i
) {
3967 ma
= isl_multi_aff_copy(sched
->node
[i
].sched
);
3968 umap
= isl_union_map_add_map(umap
, isl_map_from_multi_aff(ma
));
3974 static __isl_give isl_band_list
*construct_band_list(
3975 __isl_keep isl_schedule
*schedule
, __isl_keep isl_band
*parent
,
3976 int band_nr
, int *parent_active
, int n_active
);
3978 /* Construct an isl_band structure for the band in the given schedule
3979 * with sequence number band_nr for the n_active nodes marked by active.
3980 * If the nodes don't have a band with the given sequence number,
3981 * then a band without members is created.
3983 * Because of the way the schedule is constructed, we know that
3984 * the position of the band inside the schedule of a node is the same
3985 * for all active nodes.
3987 * The partial schedule for the band is created before the children
3988 * are created to that construct_band_list can refer to the partial
3989 * schedule of the parent.
3991 static __isl_give isl_band
*construct_band(__isl_keep isl_schedule
*schedule
,
3992 __isl_keep isl_band
*parent
,
3993 int band_nr
, int *active
, int n_active
)
3996 isl_ctx
*ctx
= isl_schedule_get_ctx(schedule
);
3998 unsigned start
, end
;
4000 band
= isl_band_alloc(ctx
);
4004 band
->schedule
= schedule
;
4005 band
->parent
= parent
;
4007 for (i
= 0; i
< schedule
->n
; ++i
)
4011 if (i
>= schedule
->n
)
4012 isl_die(ctx
, isl_error_internal
,
4013 "band without active statements", goto error
);
4015 start
= band_nr
? schedule
->node
[i
].band_end
[band_nr
- 1] : 0;
4016 end
= band_nr
< schedule
->node
[i
].n_band
?
4017 schedule
->node
[i
].band_end
[band_nr
] : start
;
4018 band
->n
= end
- start
;
4020 band
->coincident
= isl_alloc_array(ctx
, int, band
->n
);
4021 if (band
->n
&& !band
->coincident
)
4024 for (j
= 0; j
< band
->n
; ++j
)
4025 band
->coincident
[j
] = schedule
->node
[i
].coincident
[start
+ j
];
4027 band
->pma
= isl_union_pw_multi_aff_empty(isl_space_copy(schedule
->dim
));
4028 for (i
= 0; i
< schedule
->n
; ++i
) {
4030 isl_pw_multi_aff
*pma
;
4036 ma
= isl_multi_aff_copy(schedule
->node
[i
].sched
);
4037 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
4038 ma
= isl_multi_aff_drop_dims(ma
, isl_dim_out
, end
, n_out
- end
);
4039 ma
= isl_multi_aff_drop_dims(ma
, isl_dim_out
, 0, start
);
4040 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
4041 band
->pma
= isl_union_pw_multi_aff_add_pw_multi_aff(band
->pma
,
4047 for (i
= 0; i
< schedule
->n
; ++i
)
4048 if (active
[i
] && schedule
->node
[i
].n_band
> band_nr
+ 1)
4051 if (i
< schedule
->n
) {
4052 band
->children
= construct_band_list(schedule
, band
,
4053 band_nr
+ 1, active
, n_active
);
4054 if (!band
->children
)
4060 isl_band_free(band
);
4064 /* Internal data structure used inside cmp_band and pw_multi_aff_extract_int.
4066 * r is set to a negative value if anything goes wrong.
4068 * c1 stores the result of extract_int.
4069 * c2 is a temporary value used inside cmp_band_in_ancestor.
4070 * t is a temporary value used inside extract_int.
4072 * first and equal are used inside extract_int.
4073 * first is set if we are looking at the first isl_multi_aff inside
4074 * the isl_union_pw_multi_aff.
4075 * equal is set if all the isl_multi_affs have been equal so far.
4077 struct isl_cmp_band_data
{
4088 /* Check if "ma" assigns a constant value.
4089 * Note that this function is only called on isl_multi_affs
4090 * with a single output dimension.
4092 * If "ma" assigns a constant value then we compare it to data->c1
4093 * or assign it to data->c1 if this is the first isl_multi_aff we consider.
4094 * If "ma" does not assign a constant value or if it assigns a value
4095 * that is different from data->c1, then we set data->equal to zero
4096 * and terminate the check.
4098 static int multi_aff_extract_int(__isl_take isl_set
*set
,
4099 __isl_take isl_multi_aff
*ma
, void *user
)
4102 struct isl_cmp_band_data
*data
= user
;
4104 aff
= isl_multi_aff_get_aff(ma
, 0);
4105 data
->r
= isl_aff_is_cst(aff
);
4106 if (data
->r
>= 0 && data
->r
) {
4107 isl_aff_get_constant(aff
, &data
->t
);
4109 isl_int_set(data
->c1
, data
->t
);
4111 } else if (!isl_int_eq(data
->c1
, data
->t
))
4113 } else if (data
->r
>= 0 && !data
->r
)
4118 isl_multi_aff_free(ma
);
4127 /* This function is called for each isl_pw_multi_aff in
4128 * the isl_union_pw_multi_aff checked by extract_int.
4129 * Check all the isl_multi_affs inside "pma".
4131 static int pw_multi_aff_extract_int(__isl_take isl_pw_multi_aff
*pma
,
4136 r
= isl_pw_multi_aff_foreach_piece(pma
, &multi_aff_extract_int
, user
);
4137 isl_pw_multi_aff_free(pma
);
4142 /* Check if "upma" assigns a single constant value to its domain.
4143 * If so, return 1 and store the result in data->c1.
4146 * A negative return value from isl_union_pw_multi_aff_foreach_pw_multi_aff
4147 * means that either an error occurred or that we have broken off the check
4148 * because we already know the result is going to be negative.
4149 * In the latter case, data->equal is set to zero.
4151 static int extract_int(__isl_keep isl_union_pw_multi_aff
*upma
,
4152 struct isl_cmp_band_data
*data
)
4157 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
4158 &pw_multi_aff_extract_int
, data
) < 0) {
4164 return !data
->first
&& data
->equal
;
4167 /* Compare "b1" and "b2" based on the parent schedule of their ancestor
4170 * If the parent of "ancestor" also has a single member, then we
4171 * first try to compare the two band based on the partial schedule
4174 * Otherwise, or if the result is inconclusive, we look at the partial schedule
4175 * of "ancestor" itself.
4176 * In particular, we specialize the parent schedule based
4177 * on the domains of the child schedules, check if both assign
4178 * a single constant value and, if so, compare the two constant values.
4179 * If the specialized parent schedules do not assign a constant value,
4180 * then they cannot be used to order the two bands and so in this case
4183 static int cmp_band_in_ancestor(__isl_keep isl_band
*b1
,
4184 __isl_keep isl_band
*b2
, struct isl_cmp_band_data
*data
,
4185 __isl_keep isl_band
*ancestor
)
4187 isl_union_pw_multi_aff
*upma
;
4188 isl_union_set
*domain
;
4194 if (ancestor
->parent
&& ancestor
->parent
->n
== 1) {
4195 r
= cmp_band_in_ancestor(b1
, b2
, data
, ancestor
->parent
);
4202 upma
= isl_union_pw_multi_aff_copy(b1
->pma
);
4203 domain
= isl_union_pw_multi_aff_domain(upma
);
4204 upma
= isl_union_pw_multi_aff_copy(ancestor
->pma
);
4205 upma
= isl_union_pw_multi_aff_intersect_domain(upma
, domain
);
4206 r
= extract_int(upma
, data
);
4207 isl_union_pw_multi_aff_free(upma
);
4214 isl_int_set(data
->c2
, data
->c1
);
4216 upma
= isl_union_pw_multi_aff_copy(b2
->pma
);
4217 domain
= isl_union_pw_multi_aff_domain(upma
);
4218 upma
= isl_union_pw_multi_aff_copy(ancestor
->pma
);
4219 upma
= isl_union_pw_multi_aff_intersect_domain(upma
, domain
);
4220 r
= extract_int(upma
, data
);
4221 isl_union_pw_multi_aff_free(upma
);
4228 return isl_int_cmp(data
->c2
, data
->c1
);
4231 /* Compare "a" and "b" based on the parent schedule of their parent.
4233 static int cmp_band(const void *a
, const void *b
, void *user
)
4235 isl_band
*b1
= *(isl_band
* const *) a
;
4236 isl_band
*b2
= *(isl_band
* const *) b
;
4237 struct isl_cmp_band_data
*data
= user
;
4239 return cmp_band_in_ancestor(b1
, b2
, data
, b1
->parent
);
4242 /* Sort the elements in "list" based on the partial schedules of its parent
4243 * (and ancestors). In particular if the parent assigns constant values
4244 * to the domains of the bands in "list", then the elements are sorted
4245 * according to that order.
4246 * This order should be a more "natural" order for the user, but otherwise
4247 * shouldn't have any effect.
4248 * If we would be constructing an isl_band forest directly in
4249 * isl_schedule_constraints_compute_schedule then there wouldn't be any need
4250 * for a reordering, since the children would be added to the list
4251 * in their natural order automatically.
4253 * If there is only one element in the list, then there is no need to sort
4255 * If the partial schedule of the parent has more than one member
4256 * (or if there is no parent), then it's
4257 * defnitely not assigning constant values to the different children in
4258 * the list and so we wouldn't be able to use it to sort the list.
4260 static __isl_give isl_band_list
*sort_band_list(__isl_take isl_band_list
*list
,
4261 __isl_keep isl_band
*parent
)
4263 struct isl_cmp_band_data data
;
4269 if (!parent
|| parent
->n
!= 1)
4273 isl_int_init(data
.c1
);
4274 isl_int_init(data
.c2
);
4275 isl_int_init(data
.t
);
4276 isl_sort(list
->p
, list
->n
, sizeof(list
->p
[0]), &cmp_band
, &data
);
4278 list
= isl_band_list_free(list
);
4279 isl_int_clear(data
.c1
);
4280 isl_int_clear(data
.c2
);
4281 isl_int_clear(data
.t
);
4286 /* Construct a list of bands that start at the same position (with
4287 * sequence number band_nr) in the schedules of the nodes that
4288 * were active in the parent band.
4290 * A separate isl_band structure is created for each band_id
4291 * and for each node that does not have a band with sequence
4292 * number band_nr. In the latter case, a band without members
4294 * This ensures that if a band has any children, then each node
4295 * that was active in the band is active in exactly one of the children.
4297 static __isl_give isl_band_list
*construct_band_list(
4298 __isl_keep isl_schedule
*schedule
, __isl_keep isl_band
*parent
,
4299 int band_nr
, int *parent_active
, int n_active
)
4302 isl_ctx
*ctx
= isl_schedule_get_ctx(schedule
);
4305 isl_band_list
*list
;
4308 for (i
= 0; i
< n_active
; ++i
) {
4309 for (j
= 0; j
< schedule
->n
; ++j
) {
4310 if (!parent_active
[j
])
4312 if (schedule
->node
[j
].n_band
<= band_nr
)
4314 if (schedule
->node
[j
].band_id
[band_nr
] == i
) {
4320 for (j
= 0; j
< schedule
->n
; ++j
)
4321 if (schedule
->node
[j
].n_band
<= band_nr
)
4326 list
= isl_band_list_alloc(ctx
, n_band
);
4327 band
= construct_band(schedule
, parent
, band_nr
,
4328 parent_active
, n_active
);
4329 return isl_band_list_add(list
, band
);
4332 active
= isl_alloc_array(ctx
, int, schedule
->n
);
4333 if (schedule
->n
&& !active
)
4336 list
= isl_band_list_alloc(ctx
, n_band
);
4338 for (i
= 0; i
< n_active
; ++i
) {
4342 for (j
= 0; j
< schedule
->n
; ++j
) {
4343 active
[j
] = parent_active
[j
] &&
4344 schedule
->node
[j
].n_band
> band_nr
&&
4345 schedule
->node
[j
].band_id
[band_nr
] == i
;
4352 band
= construct_band(schedule
, parent
, band_nr
, active
, n
);
4354 list
= isl_band_list_add(list
, band
);
4356 for (i
= 0; i
< schedule
->n
; ++i
) {
4358 if (!parent_active
[i
])
4360 if (schedule
->node
[i
].n_band
> band_nr
)
4362 for (j
= 0; j
< schedule
->n
; ++j
)
4364 band
= construct_band(schedule
, parent
, band_nr
, active
, 1);
4365 list
= isl_band_list_add(list
, band
);
4370 list
= sort_band_list(list
, parent
);
4375 /* Construct a band forest representation of the schedule and
4376 * return the list of roots.
4378 static __isl_give isl_band_list
*construct_forest(
4379 __isl_keep isl_schedule
*schedule
)
4382 isl_ctx
*ctx
= isl_schedule_get_ctx(schedule
);
4383 isl_band_list
*forest
;
4386 active
= isl_alloc_array(ctx
, int, schedule
->n
);
4387 if (schedule
->n
&& !active
)
4390 for (i
= 0; i
< schedule
->n
; ++i
)
4393 forest
= construct_band_list(schedule
, NULL
, 0, active
, schedule
->n
);
4400 /* Return the roots of a band forest representation of the schedule.
4402 __isl_give isl_band_list
*isl_schedule_get_band_forest(
4403 __isl_keep isl_schedule
*schedule
)
4407 if (!schedule
->band_forest
)
4408 schedule
->band_forest
= construct_forest(schedule
);
4409 return isl_band_list_dup(schedule
->band_forest
);
4412 /* Call "fn" on each band in the schedule in depth-first post-order.
4414 int isl_schedule_foreach_band(__isl_keep isl_schedule
*sched
,
4415 int (*fn
)(__isl_keep isl_band
*band
, void *user
), void *user
)
4418 isl_band_list
*forest
;
4423 forest
= isl_schedule_get_band_forest(sched
);
4424 r
= isl_band_list_foreach_band(forest
, fn
, user
);
4425 isl_band_list_free(forest
);
4430 static __isl_give isl_printer
*print_band_list(__isl_take isl_printer
*p
,
4431 __isl_keep isl_band_list
*list
);
4433 static __isl_give isl_printer
*print_band(__isl_take isl_printer
*p
,
4434 __isl_keep isl_band
*band
)
4436 isl_band_list
*children
;
4438 p
= isl_printer_start_line(p
);
4439 p
= isl_printer_print_union_pw_multi_aff(p
, band
->pma
);
4440 p
= isl_printer_end_line(p
);
4442 if (!isl_band_has_children(band
))
4445 children
= isl_band_get_children(band
);
4447 p
= isl_printer_indent(p
, 4);
4448 p
= print_band_list(p
, children
);
4449 p
= isl_printer_indent(p
, -4);
4451 isl_band_list_free(children
);
4456 static __isl_give isl_printer
*print_band_list(__isl_take isl_printer
*p
,
4457 __isl_keep isl_band_list
*list
)
4461 n
= isl_band_list_n_band(list
);
4462 for (i
= 0; i
< n
; ++i
) {
4464 band
= isl_band_list_get_band(list
, i
);
4465 p
= print_band(p
, band
);
4466 isl_band_free(band
);
4472 __isl_give isl_printer
*isl_printer_print_schedule(__isl_take isl_printer
*p
,
4473 __isl_keep isl_schedule
*schedule
)
4475 isl_band_list
*forest
;
4477 forest
= isl_schedule_get_band_forest(schedule
);
4479 p
= print_band_list(p
, forest
);
4481 isl_band_list_free(forest
);
4486 void isl_schedule_dump(__isl_keep isl_schedule
*schedule
)
4488 isl_printer
*printer
;
4493 printer
= isl_printer_to_file(isl_schedule_get_ctx(schedule
), stderr
);
4494 printer
= isl_printer_print_schedule(printer
, schedule
);
4496 isl_printer_free(printer
);