replace --schedule-fuse option by --schedule-serialize-sccs option
[isl.git] / isl_scheduler.c
blobc1e15b79dcbde61e4a86d6dc485cf31239d5cb5c
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2014 Ecole Normale Superieure
4 * Copyright 2015 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * 91893 Orsay, France
11 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
16 #include <isl_space_private.h>
17 #include <isl_aff_private.h>
18 #include <isl/hash.h>
19 #include <isl/constraint.h>
20 #include <isl/schedule.h>
21 #include <isl/schedule_node.h>
22 #include <isl_mat_private.h>
23 #include <isl_vec_private.h>
24 #include <isl/set.h>
25 #include <isl/union_set.h>
26 #include <isl_seq.h>
27 #include <isl_tab.h>
28 #include <isl_dim_map.h>
29 #include <isl/map_to_basic_set.h>
30 #include <isl_sort.h>
31 #include <isl_options_private.h>
32 #include <isl_tarjan.h>
33 #include <isl_morph.h>
36 * The scheduling algorithm implemented in this file was inspired by
37 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
38 * Parallelization and Locality Optimization in the Polyhedral Model".
41 enum isl_edge_type {
42 isl_edge_validity = 0,
43 isl_edge_first = isl_edge_validity,
44 isl_edge_coincidence,
45 isl_edge_condition,
46 isl_edge_conditional_validity,
47 isl_edge_proximity,
48 isl_edge_last = isl_edge_proximity
51 /* The constraints that need to be satisfied by a schedule on "domain".
53 * "context" specifies extra constraints on the parameters.
55 * "validity" constraints map domain elements i to domain elements
56 * that should be scheduled after i. (Hard constraint)
57 * "proximity" constraints map domain elements i to domains elements
58 * that should be scheduled as early as possible after i (or before i).
59 * (Soft constraint)
61 * "condition" and "conditional_validity" constraints map possibly "tagged"
62 * domain elements i -> s to "tagged" domain elements j -> t.
63 * The elements of the "conditional_validity" constraints, but without the
64 * tags (i.e., the elements i -> j) are treated as validity constraints,
65 * except that during the construction of a tilable band,
66 * the elements of the "conditional_validity" constraints may be violated
67 * provided that all adjacent elements of the "condition" constraints
68 * are local within the band.
69 * A dependence is local within a band if domain and range are mapped
70 * to the same schedule point by the band.
72 struct isl_schedule_constraints {
73 isl_union_set *domain;
74 isl_set *context;
76 isl_union_map *constraint[isl_edge_last + 1];
79 __isl_give isl_schedule_constraints *isl_schedule_constraints_copy(
80 __isl_keep isl_schedule_constraints *sc)
82 isl_ctx *ctx;
83 isl_schedule_constraints *sc_copy;
84 enum isl_edge_type i;
86 ctx = isl_union_set_get_ctx(sc->domain);
87 sc_copy = isl_calloc_type(ctx, struct isl_schedule_constraints);
88 if (!sc_copy)
89 return NULL;
91 sc_copy->domain = isl_union_set_copy(sc->domain);
92 sc_copy->context = isl_set_copy(sc->context);
93 if (!sc_copy->domain || !sc_copy->context)
94 return isl_schedule_constraints_free(sc_copy);
96 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
97 sc_copy->constraint[i] = isl_union_map_copy(sc->constraint[i]);
98 if (!sc_copy->constraint[i])
99 return isl_schedule_constraints_free(sc_copy);
102 return sc_copy;
106 /* Construct an isl_schedule_constraints object for computing a schedule
107 * on "domain". The initial object does not impose any constraints.
109 __isl_give isl_schedule_constraints *isl_schedule_constraints_on_domain(
110 __isl_take isl_union_set *domain)
112 isl_ctx *ctx;
113 isl_space *space;
114 isl_schedule_constraints *sc;
115 isl_union_map *empty;
116 enum isl_edge_type i;
118 if (!domain)
119 return NULL;
121 ctx = isl_union_set_get_ctx(domain);
122 sc = isl_calloc_type(ctx, struct isl_schedule_constraints);
123 if (!sc)
124 goto error;
126 space = isl_union_set_get_space(domain);
127 sc->domain = domain;
128 sc->context = isl_set_universe(isl_space_copy(space));
129 empty = isl_union_map_empty(space);
130 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
131 sc->constraint[i] = isl_union_map_copy(empty);
132 if (!sc->constraint[i])
133 sc->domain = isl_union_set_free(sc->domain);
135 isl_union_map_free(empty);
137 if (!sc->domain || !sc->context)
138 return isl_schedule_constraints_free(sc);
140 return sc;
141 error:
142 isl_union_set_free(domain);
143 return NULL;
146 /* Replace the context of "sc" by "context".
148 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_context(
149 __isl_take isl_schedule_constraints *sc, __isl_take isl_set *context)
151 if (!sc || !context)
152 goto error;
154 isl_set_free(sc->context);
155 sc->context = context;
157 return sc;
158 error:
159 isl_schedule_constraints_free(sc);
160 isl_set_free(context);
161 return NULL;
164 /* Replace the validity constraints of "sc" by "validity".
166 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_validity(
167 __isl_take isl_schedule_constraints *sc,
168 __isl_take isl_union_map *validity)
170 if (!sc || !validity)
171 goto error;
173 isl_union_map_free(sc->constraint[isl_edge_validity]);
174 sc->constraint[isl_edge_validity] = validity;
176 return sc;
177 error:
178 isl_schedule_constraints_free(sc);
179 isl_union_map_free(validity);
180 return NULL;
183 /* Replace the coincidence constraints of "sc" by "coincidence".
185 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_coincidence(
186 __isl_take isl_schedule_constraints *sc,
187 __isl_take isl_union_map *coincidence)
189 if (!sc || !coincidence)
190 goto error;
192 isl_union_map_free(sc->constraint[isl_edge_coincidence]);
193 sc->constraint[isl_edge_coincidence] = coincidence;
195 return sc;
196 error:
197 isl_schedule_constraints_free(sc);
198 isl_union_map_free(coincidence);
199 return NULL;
202 /* Replace the proximity constraints of "sc" by "proximity".
204 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_proximity(
205 __isl_take isl_schedule_constraints *sc,
206 __isl_take isl_union_map *proximity)
208 if (!sc || !proximity)
209 goto error;
211 isl_union_map_free(sc->constraint[isl_edge_proximity]);
212 sc->constraint[isl_edge_proximity] = proximity;
214 return sc;
215 error:
216 isl_schedule_constraints_free(sc);
217 isl_union_map_free(proximity);
218 return NULL;
221 /* Replace the conditional validity constraints of "sc" by "condition"
222 * and "validity".
224 __isl_give isl_schedule_constraints *
225 isl_schedule_constraints_set_conditional_validity(
226 __isl_take isl_schedule_constraints *sc,
227 __isl_take isl_union_map *condition,
228 __isl_take isl_union_map *validity)
230 if (!sc || !condition || !validity)
231 goto error;
233 isl_union_map_free(sc->constraint[isl_edge_condition]);
234 sc->constraint[isl_edge_condition] = condition;
235 isl_union_map_free(sc->constraint[isl_edge_conditional_validity]);
236 sc->constraint[isl_edge_conditional_validity] = validity;
238 return sc;
239 error:
240 isl_schedule_constraints_free(sc);
241 isl_union_map_free(condition);
242 isl_union_map_free(validity);
243 return NULL;
246 __isl_null isl_schedule_constraints *isl_schedule_constraints_free(
247 __isl_take isl_schedule_constraints *sc)
249 enum isl_edge_type i;
251 if (!sc)
252 return NULL;
254 isl_union_set_free(sc->domain);
255 isl_set_free(sc->context);
256 for (i = isl_edge_first; i <= isl_edge_last; ++i)
257 isl_union_map_free(sc->constraint[i]);
259 free(sc);
261 return NULL;
264 isl_ctx *isl_schedule_constraints_get_ctx(
265 __isl_keep isl_schedule_constraints *sc)
267 return sc ? isl_union_set_get_ctx(sc->domain) : NULL;
270 /* Return the validity constraints of "sc".
272 __isl_give isl_union_map *isl_schedule_constraints_get_validity(
273 __isl_keep isl_schedule_constraints *sc)
275 if (!sc)
276 return NULL;
278 return isl_union_map_copy(sc->constraint[isl_edge_validity]);
281 /* Return the coincidence constraints of "sc".
283 __isl_give isl_union_map *isl_schedule_constraints_get_coincidence(
284 __isl_keep isl_schedule_constraints *sc)
286 if (!sc)
287 return NULL;
289 return isl_union_map_copy(sc->constraint[isl_edge_coincidence]);
292 /* Return the conditional validity constraints of "sc".
294 __isl_give isl_union_map *isl_schedule_constraints_get_conditional_validity(
295 __isl_keep isl_schedule_constraints *sc)
297 if (!sc)
298 return NULL;
300 return
301 isl_union_map_copy(sc->constraint[isl_edge_conditional_validity]);
304 /* Return the conditions for the conditional validity constraints of "sc".
306 __isl_give isl_union_map *
307 isl_schedule_constraints_get_conditional_validity_condition(
308 __isl_keep isl_schedule_constraints *sc)
310 if (!sc)
311 return NULL;
313 return isl_union_map_copy(sc->constraint[isl_edge_condition]);
316 void isl_schedule_constraints_dump(__isl_keep isl_schedule_constraints *sc)
318 if (!sc)
319 return;
321 fprintf(stderr, "domain: ");
322 isl_union_set_dump(sc->domain);
323 fprintf(stderr, "context: ");
324 isl_set_dump(sc->context);
325 fprintf(stderr, "validity: ");
326 isl_union_map_dump(sc->constraint[isl_edge_validity]);
327 fprintf(stderr, "proximity: ");
328 isl_union_map_dump(sc->constraint[isl_edge_proximity]);
329 fprintf(stderr, "coincidence: ");
330 isl_union_map_dump(sc->constraint[isl_edge_coincidence]);
331 fprintf(stderr, "condition: ");
332 isl_union_map_dump(sc->constraint[isl_edge_condition]);
333 fprintf(stderr, "conditional_validity: ");
334 isl_union_map_dump(sc->constraint[isl_edge_conditional_validity]);
337 /* Align the parameters of the fields of "sc".
339 static __isl_give isl_schedule_constraints *
340 isl_schedule_constraints_align_params(__isl_take isl_schedule_constraints *sc)
342 isl_space *space;
343 enum isl_edge_type i;
345 if (!sc)
346 return NULL;
348 space = isl_union_set_get_space(sc->domain);
349 space = isl_space_align_params(space, isl_set_get_space(sc->context));
350 for (i = isl_edge_first; i <= isl_edge_last; ++i)
351 space = isl_space_align_params(space,
352 isl_union_map_get_space(sc->constraint[i]));
354 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
355 sc->constraint[i] = isl_union_map_align_params(
356 sc->constraint[i], isl_space_copy(space));
357 if (!sc->constraint[i])
358 space = isl_space_free(space);
360 sc->context = isl_set_align_params(sc->context, isl_space_copy(space));
361 sc->domain = isl_union_set_align_params(sc->domain, space);
362 if (!sc->context || !sc->domain)
363 return isl_schedule_constraints_free(sc);
365 return sc;
368 /* Return the total number of isl_maps in the constraints of "sc".
370 static __isl_give int isl_schedule_constraints_n_map(
371 __isl_keep isl_schedule_constraints *sc)
373 enum isl_edge_type i;
374 int n = 0;
376 for (i = isl_edge_first; i <= isl_edge_last; ++i)
377 n += isl_union_map_n_map(sc->constraint[i]);
379 return n;
382 /* Internal information about a node that is used during the construction
383 * of a schedule.
384 * space represents the space in which the domain lives
385 * sched is a matrix representation of the schedule being constructed
386 * for this node; if compressed is set, then this schedule is
387 * defined over the compressed domain space
388 * sched_map is an isl_map representation of the same (partial) schedule
389 * sched_map may be NULL; if compressed is set, then this map
390 * is defined over the uncompressed domain space
391 * rank is the number of linearly independent rows in the linear part
392 * of sched
393 * the columns of cmap represent a change of basis for the schedule
394 * coefficients; the first rank columns span the linear part of
395 * the schedule rows
396 * cinv is the inverse of cmap.
397 * start is the first variable in the LP problem in the sequences that
398 * represents the schedule coefficients of this node
399 * nvar is the dimension of the domain
400 * nparam is the number of parameters or 0 if we are not constructing
401 * a parametric schedule
403 * If compressed is set, then hull represents the constraints
404 * that were used to derive the compression, while compress and
405 * decompress map the original space to the compressed space and
406 * vice versa.
408 * scc is the index of SCC (or WCC) this node belongs to
410 * coincident contains a boolean for each of the rows of the schedule,
411 * indicating whether the corresponding scheduling dimension satisfies
412 * the coincidence constraints in the sense that the corresponding
413 * dependence distances are zero.
415 struct isl_sched_node {
416 isl_space *space;
417 int compressed;
418 isl_set *hull;
419 isl_multi_aff *compress;
420 isl_multi_aff *decompress;
421 isl_mat *sched;
422 isl_map *sched_map;
423 int rank;
424 isl_mat *cmap;
425 isl_mat *cinv;
426 int start;
427 int nvar;
428 int nparam;
430 int scc;
432 int *coincident;
435 static int node_has_space(const void *entry, const void *val)
437 struct isl_sched_node *node = (struct isl_sched_node *)entry;
438 isl_space *dim = (isl_space *)val;
440 return isl_space_is_equal(node->space, dim);
443 static int node_scc_exactly(struct isl_sched_node *node, int scc)
445 return node->scc == scc;
448 static int node_scc_at_most(struct isl_sched_node *node, int scc)
450 return node->scc <= scc;
453 static int node_scc_at_least(struct isl_sched_node *node, int scc)
455 return node->scc >= scc;
458 /* An edge in the dependence graph. An edge may be used to
459 * ensure validity of the generated schedule, to minimize the dependence
460 * distance or both
462 * map is the dependence relation, with i -> j in the map if j depends on i
463 * tagged_condition and tagged_validity contain the union of all tagged
464 * condition or conditional validity dependence relations that
465 * specialize the dependence relation "map"; that is,
466 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
467 * or "tagged_validity", then i -> j is an element of "map".
468 * If these fields are NULL, then they represent the empty relation.
469 * src is the source node
470 * dst is the sink node
471 * validity is set if the edge is used to ensure correctness
472 * coincidence is used to enforce zero dependence distances
473 * proximity is set if the edge is used to minimize dependence distances
474 * condition is set if the edge represents a condition
475 * for a conditional validity schedule constraint
476 * local can only be set for condition edges and indicates that
477 * the dependence distance over the edge should be zero
478 * conditional_validity is set if the edge is used to conditionally
479 * ensure correctness
481 * For validity edges, start and end mark the sequence of inequality
482 * constraints in the LP problem that encode the validity constraint
483 * corresponding to this edge.
485 struct isl_sched_edge {
486 isl_map *map;
487 isl_union_map *tagged_condition;
488 isl_union_map *tagged_validity;
490 struct isl_sched_node *src;
491 struct isl_sched_node *dst;
493 unsigned validity : 1;
494 unsigned coincidence : 1;
495 unsigned proximity : 1;
496 unsigned local : 1;
497 unsigned condition : 1;
498 unsigned conditional_validity : 1;
500 int start;
501 int end;
504 /* Internal information about the dependence graph used during
505 * the construction of the schedule.
507 * intra_hmap is a cache, mapping dependence relations to their dual,
508 * for dependences from a node to itself
509 * inter_hmap is a cache, mapping dependence relations to their dual,
510 * for dependences between distinct nodes
511 * if compression is involved then the key for these maps
512 * it the original, uncompressed dependence relation, while
513 * the value is the dual of the compressed dependence relation.
515 * n is the number of nodes
516 * node is the list of nodes
517 * maxvar is the maximal number of variables over all nodes
518 * max_row is the allocated number of rows in the schedule
519 * n_row is the current (maximal) number of linearly independent
520 * rows in the node schedules
521 * n_total_row is the current number of rows in the node schedules
522 * band_start is the starting row in the node schedules of the current band
523 * root is set if this graph is the original dependence graph,
524 * without any splitting
526 * sorted contains a list of node indices sorted according to the
527 * SCC to which a node belongs
529 * n_edge is the number of edges
530 * edge is the list of edges
531 * max_edge contains the maximal number of edges of each type;
532 * in particular, it contains the number of edges in the inital graph.
533 * edge_table contains pointers into the edge array, hashed on the source
534 * and sink spaces; there is one such table for each type;
535 * a given edge may be referenced from more than one table
536 * if the corresponding relation appears in more than of the
537 * sets of dependences
539 * node_table contains pointers into the node array, hashed on the space
541 * region contains a list of variable sequences that should be non-trivial
543 * lp contains the (I)LP problem used to obtain new schedule rows
545 * src_scc and dst_scc are the source and sink SCCs of an edge with
546 * conflicting constraints
548 * scc represents the number of components
549 * weak is set if the components are weakly connected
551 struct isl_sched_graph {
552 isl_map_to_basic_set *intra_hmap;
553 isl_map_to_basic_set *inter_hmap;
555 struct isl_sched_node *node;
556 int n;
557 int maxvar;
558 int max_row;
559 int n_row;
561 int *sorted;
563 int n_total_row;
564 int band_start;
566 int root;
568 struct isl_sched_edge *edge;
569 int n_edge;
570 int max_edge[isl_edge_last + 1];
571 struct isl_hash_table *edge_table[isl_edge_last + 1];
573 struct isl_hash_table *node_table;
574 struct isl_region *region;
576 isl_basic_set *lp;
578 int src_scc;
579 int dst_scc;
581 int scc;
582 int weak;
585 /* Initialize node_table based on the list of nodes.
587 static int graph_init_table(isl_ctx *ctx, struct isl_sched_graph *graph)
589 int i;
591 graph->node_table = isl_hash_table_alloc(ctx, graph->n);
592 if (!graph->node_table)
593 return -1;
595 for (i = 0; i < graph->n; ++i) {
596 struct isl_hash_table_entry *entry;
597 uint32_t hash;
599 hash = isl_space_get_hash(graph->node[i].space);
600 entry = isl_hash_table_find(ctx, graph->node_table, hash,
601 &node_has_space,
602 graph->node[i].space, 1);
603 if (!entry)
604 return -1;
605 entry->data = &graph->node[i];
608 return 0;
611 /* Return a pointer to the node that lives within the given space,
612 * or NULL if there is no such node.
614 static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
615 struct isl_sched_graph *graph, __isl_keep isl_space *dim)
617 struct isl_hash_table_entry *entry;
618 uint32_t hash;
620 hash = isl_space_get_hash(dim);
621 entry = isl_hash_table_find(ctx, graph->node_table, hash,
622 &node_has_space, dim, 0);
624 return entry ? entry->data : NULL;
627 static int edge_has_src_and_dst(const void *entry, const void *val)
629 const struct isl_sched_edge *edge = entry;
630 const struct isl_sched_edge *temp = val;
632 return edge->src == temp->src && edge->dst == temp->dst;
635 /* Add the given edge to graph->edge_table[type].
637 static isl_stat graph_edge_table_add(isl_ctx *ctx,
638 struct isl_sched_graph *graph, enum isl_edge_type type,
639 struct isl_sched_edge *edge)
641 struct isl_hash_table_entry *entry;
642 uint32_t hash;
644 hash = isl_hash_init();
645 hash = isl_hash_builtin(hash, edge->src);
646 hash = isl_hash_builtin(hash, edge->dst);
647 entry = isl_hash_table_find(ctx, graph->edge_table[type], hash,
648 &edge_has_src_and_dst, edge, 1);
649 if (!entry)
650 return isl_stat_error;
651 entry->data = edge;
653 return isl_stat_ok;
656 /* Allocate the edge_tables based on the maximal number of edges of
657 * each type.
659 static int graph_init_edge_tables(isl_ctx *ctx, struct isl_sched_graph *graph)
661 int i;
663 for (i = 0; i <= isl_edge_last; ++i) {
664 graph->edge_table[i] = isl_hash_table_alloc(ctx,
665 graph->max_edge[i]);
666 if (!graph->edge_table[i])
667 return -1;
670 return 0;
673 /* If graph->edge_table[type] contains an edge from the given source
674 * to the given destination, then return the hash table entry of this edge.
675 * Otherwise, return NULL.
677 static struct isl_hash_table_entry *graph_find_edge_entry(
678 struct isl_sched_graph *graph,
679 enum isl_edge_type type,
680 struct isl_sched_node *src, struct isl_sched_node *dst)
682 isl_ctx *ctx = isl_space_get_ctx(src->space);
683 uint32_t hash;
684 struct isl_sched_edge temp = { .src = src, .dst = dst };
686 hash = isl_hash_init();
687 hash = isl_hash_builtin(hash, temp.src);
688 hash = isl_hash_builtin(hash, temp.dst);
689 return isl_hash_table_find(ctx, graph->edge_table[type], hash,
690 &edge_has_src_and_dst, &temp, 0);
694 /* If graph->edge_table[type] contains an edge from the given source
695 * to the given destination, then return this edge.
696 * Otherwise, return NULL.
698 static struct isl_sched_edge *graph_find_edge(struct isl_sched_graph *graph,
699 enum isl_edge_type type,
700 struct isl_sched_node *src, struct isl_sched_node *dst)
702 struct isl_hash_table_entry *entry;
704 entry = graph_find_edge_entry(graph, type, src, dst);
705 if (!entry)
706 return NULL;
708 return entry->data;
711 /* Check whether the dependence graph has an edge of the given type
712 * between the given two nodes.
714 static isl_bool graph_has_edge(struct isl_sched_graph *graph,
715 enum isl_edge_type type,
716 struct isl_sched_node *src, struct isl_sched_node *dst)
718 struct isl_sched_edge *edge;
719 isl_bool empty;
721 edge = graph_find_edge(graph, type, src, dst);
722 if (!edge)
723 return 0;
725 empty = isl_map_plain_is_empty(edge->map);
726 if (empty < 0)
727 return isl_bool_error;
729 return !empty;
732 /* Look for any edge with the same src, dst and map fields as "model".
734 * Return the matching edge if one can be found.
735 * Return "model" if no matching edge is found.
736 * Return NULL on error.
738 static struct isl_sched_edge *graph_find_matching_edge(
739 struct isl_sched_graph *graph, struct isl_sched_edge *model)
741 enum isl_edge_type i;
742 struct isl_sched_edge *edge;
744 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
745 int is_equal;
747 edge = graph_find_edge(graph, i, model->src, model->dst);
748 if (!edge)
749 continue;
750 is_equal = isl_map_plain_is_equal(model->map, edge->map);
751 if (is_equal < 0)
752 return NULL;
753 if (is_equal)
754 return edge;
757 return model;
760 /* Remove the given edge from all the edge_tables that refer to it.
762 static void graph_remove_edge(struct isl_sched_graph *graph,
763 struct isl_sched_edge *edge)
765 isl_ctx *ctx = isl_map_get_ctx(edge->map);
766 enum isl_edge_type i;
768 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
769 struct isl_hash_table_entry *entry;
771 entry = graph_find_edge_entry(graph, i, edge->src, edge->dst);
772 if (!entry)
773 continue;
774 if (entry->data != edge)
775 continue;
776 isl_hash_table_remove(ctx, graph->edge_table[i], entry);
780 /* Check whether the dependence graph has any edge
781 * between the given two nodes.
783 static isl_bool graph_has_any_edge(struct isl_sched_graph *graph,
784 struct isl_sched_node *src, struct isl_sched_node *dst)
786 enum isl_edge_type i;
787 isl_bool r;
789 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
790 r = graph_has_edge(graph, i, src, dst);
791 if (r < 0 || r)
792 return r;
795 return r;
798 /* Check whether the dependence graph has a validity edge
799 * between the given two nodes.
801 * Conditional validity edges are essentially validity edges that
802 * can be ignored if the corresponding condition edges are iteration private.
803 * Here, we are only checking for the presence of validity
804 * edges, so we need to consider the conditional validity edges too.
805 * In particular, this function is used during the detection
806 * of strongly connected components and we cannot ignore
807 * conditional validity edges during this detection.
809 static isl_bool graph_has_validity_edge(struct isl_sched_graph *graph,
810 struct isl_sched_node *src, struct isl_sched_node *dst)
812 isl_bool r;
814 r = graph_has_edge(graph, isl_edge_validity, src, dst);
815 if (r < 0 || r)
816 return r;
818 return graph_has_edge(graph, isl_edge_conditional_validity, src, dst);
821 static int graph_alloc(isl_ctx *ctx, struct isl_sched_graph *graph,
822 int n_node, int n_edge)
824 int i;
826 graph->n = n_node;
827 graph->n_edge = n_edge;
828 graph->node = isl_calloc_array(ctx, struct isl_sched_node, graph->n);
829 graph->sorted = isl_calloc_array(ctx, int, graph->n);
830 graph->region = isl_alloc_array(ctx, struct isl_region, graph->n);
831 graph->edge = isl_calloc_array(ctx,
832 struct isl_sched_edge, graph->n_edge);
834 graph->intra_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
835 graph->inter_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
837 if (!graph->node || !graph->region || (graph->n_edge && !graph->edge) ||
838 !graph->sorted)
839 return -1;
841 for(i = 0; i < graph->n; ++i)
842 graph->sorted[i] = i;
844 return 0;
847 static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
849 int i;
851 isl_map_to_basic_set_free(graph->intra_hmap);
852 isl_map_to_basic_set_free(graph->inter_hmap);
854 if (graph->node)
855 for (i = 0; i < graph->n; ++i) {
856 isl_space_free(graph->node[i].space);
857 isl_set_free(graph->node[i].hull);
858 isl_multi_aff_free(graph->node[i].compress);
859 isl_multi_aff_free(graph->node[i].decompress);
860 isl_mat_free(graph->node[i].sched);
861 isl_map_free(graph->node[i].sched_map);
862 isl_mat_free(graph->node[i].cmap);
863 isl_mat_free(graph->node[i].cinv);
864 if (graph->root)
865 free(graph->node[i].coincident);
867 free(graph->node);
868 free(graph->sorted);
869 if (graph->edge)
870 for (i = 0; i < graph->n_edge; ++i) {
871 isl_map_free(graph->edge[i].map);
872 isl_union_map_free(graph->edge[i].tagged_condition);
873 isl_union_map_free(graph->edge[i].tagged_validity);
875 free(graph->edge);
876 free(graph->region);
877 for (i = 0; i <= isl_edge_last; ++i)
878 isl_hash_table_free(ctx, graph->edge_table[i]);
879 isl_hash_table_free(ctx, graph->node_table);
880 isl_basic_set_free(graph->lp);
883 /* For each "set" on which this function is called, increment
884 * graph->n by one and update graph->maxvar.
886 static isl_stat init_n_maxvar(__isl_take isl_set *set, void *user)
888 struct isl_sched_graph *graph = user;
889 int nvar = isl_set_dim(set, isl_dim_set);
891 graph->n++;
892 if (nvar > graph->maxvar)
893 graph->maxvar = nvar;
895 isl_set_free(set);
897 return isl_stat_ok;
900 /* Add the number of basic maps in "map" to *n.
902 static isl_stat add_n_basic_map(__isl_take isl_map *map, void *user)
904 int *n = user;
906 *n += isl_map_n_basic_map(map);
907 isl_map_free(map);
909 return isl_stat_ok;
912 /* Compute the number of rows that should be allocated for the schedule.
913 * In particular, we need one row for each variable or one row
914 * for each basic map in the dependences.
915 * Note that it is practically impossible to exhaust both
916 * the number of dependences and the number of variables.
918 static int compute_max_row(struct isl_sched_graph *graph,
919 __isl_keep isl_schedule_constraints *sc)
921 enum isl_edge_type i;
922 int n_edge;
924 graph->n = 0;
925 graph->maxvar = 0;
926 if (isl_union_set_foreach_set(sc->domain, &init_n_maxvar, graph) < 0)
927 return -1;
928 n_edge = 0;
929 for (i = isl_edge_first; i <= isl_edge_last; ++i)
930 if (isl_union_map_foreach_map(sc->constraint[i],
931 &add_n_basic_map, &n_edge) < 0)
932 return -1;
933 graph->max_row = n_edge + graph->maxvar;
935 return 0;
938 /* Does "bset" have any defining equalities for its set variables?
940 static int has_any_defining_equality(__isl_keep isl_basic_set *bset)
942 int i, n;
944 if (!bset)
945 return -1;
947 n = isl_basic_set_dim(bset, isl_dim_set);
948 for (i = 0; i < n; ++i) {
949 int has;
951 has = isl_basic_set_has_defining_equality(bset, isl_dim_set, i,
952 NULL);
953 if (has < 0 || has)
954 return has;
957 return 0;
960 /* Add a new node to the graph representing the given space.
961 * "nvar" is the (possibly compressed) number of variables and
962 * may be smaller than then number of set variables in "space"
963 * if "compressed" is set.
964 * If "compressed" is set, then "hull" represents the constraints
965 * that were used to derive the compression, while "compress" and
966 * "decompress" map the original space to the compressed space and
967 * vice versa.
968 * If "compressed" is not set, then "hull", "compress" and "decompress"
969 * should be NULL.
971 static isl_stat add_node(struct isl_sched_graph *graph,
972 __isl_take isl_space *space, int nvar, int compressed,
973 __isl_take isl_set *hull, __isl_take isl_multi_aff *compress,
974 __isl_take isl_multi_aff *decompress)
976 int nparam;
977 isl_ctx *ctx;
978 isl_mat *sched;
979 int *coincident;
981 if (!space)
982 return isl_stat_error;
984 ctx = isl_space_get_ctx(space);
985 nparam = isl_space_dim(space, isl_dim_param);
986 if (!ctx->opt->schedule_parametric)
987 nparam = 0;
988 sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
989 graph->node[graph->n].space = space;
990 graph->node[graph->n].nvar = nvar;
991 graph->node[graph->n].nparam = nparam;
992 graph->node[graph->n].sched = sched;
993 graph->node[graph->n].sched_map = NULL;
994 coincident = isl_calloc_array(ctx, int, graph->max_row);
995 graph->node[graph->n].coincident = coincident;
996 graph->node[graph->n].compressed = compressed;
997 graph->node[graph->n].hull = hull;
998 graph->node[graph->n].compress = compress;
999 graph->node[graph->n].decompress = decompress;
1000 graph->n++;
1002 if (!space || !sched || (graph->max_row && !coincident))
1003 return isl_stat_error;
1004 if (compressed && (!hull || !compress || !decompress))
1005 return isl_stat_error;
1007 return isl_stat_ok;
1010 /* Add a new node to the graph representing the given set.
1012 * If any of the set variables is defined by an equality, then
1013 * we perform variable compression such that we can perform
1014 * the scheduling on the compressed domain.
1016 static isl_stat extract_node(__isl_take isl_set *set, void *user)
1018 int nvar;
1019 int has_equality;
1020 isl_space *space;
1021 isl_basic_set *hull;
1022 isl_set *hull_set;
1023 isl_morph *morph;
1024 isl_multi_aff *compress, *decompress;
1025 struct isl_sched_graph *graph = user;
1027 space = isl_set_get_space(set);
1028 hull = isl_set_affine_hull(set);
1029 hull = isl_basic_set_remove_divs(hull);
1030 nvar = isl_space_dim(space, isl_dim_set);
1031 has_equality = has_any_defining_equality(hull);
1033 if (has_equality < 0)
1034 goto error;
1035 if (!has_equality) {
1036 isl_basic_set_free(hull);
1037 return add_node(graph, space, nvar, 0, NULL, NULL, NULL);
1040 morph = isl_basic_set_variable_compression(hull, isl_dim_set);
1041 nvar = isl_morph_ran_dim(morph, isl_dim_set);
1042 compress = isl_morph_get_var_multi_aff(morph);
1043 morph = isl_morph_inverse(morph);
1044 decompress = isl_morph_get_var_multi_aff(morph);
1045 isl_morph_free(morph);
1047 hull_set = isl_set_from_basic_set(hull);
1048 return add_node(graph, space, nvar, 1, hull_set, compress, decompress);
1049 error:
1050 isl_basic_set_free(hull);
1051 isl_space_free(space);
1052 return isl_stat_error;
1055 struct isl_extract_edge_data {
1056 enum isl_edge_type type;
1057 struct isl_sched_graph *graph;
1060 /* Merge edge2 into edge1, freeing the contents of edge2.
1061 * "type" is the type of the schedule constraint from which edge2 was
1062 * extracted.
1063 * Return 0 on success and -1 on failure.
1065 * edge1 and edge2 are assumed to have the same value for the map field.
1067 static int merge_edge(enum isl_edge_type type, struct isl_sched_edge *edge1,
1068 struct isl_sched_edge *edge2)
1070 edge1->validity |= edge2->validity;
1071 edge1->coincidence |= edge2->coincidence;
1072 edge1->proximity |= edge2->proximity;
1073 edge1->condition |= edge2->condition;
1074 edge1->conditional_validity |= edge2->conditional_validity;
1075 isl_map_free(edge2->map);
1077 if (type == isl_edge_condition) {
1078 if (!edge1->tagged_condition)
1079 edge1->tagged_condition = edge2->tagged_condition;
1080 else
1081 edge1->tagged_condition =
1082 isl_union_map_union(edge1->tagged_condition,
1083 edge2->tagged_condition);
1086 if (type == isl_edge_conditional_validity) {
1087 if (!edge1->tagged_validity)
1088 edge1->tagged_validity = edge2->tagged_validity;
1089 else
1090 edge1->tagged_validity =
1091 isl_union_map_union(edge1->tagged_validity,
1092 edge2->tagged_validity);
1095 if (type == isl_edge_condition && !edge1->tagged_condition)
1096 return -1;
1097 if (type == isl_edge_conditional_validity && !edge1->tagged_validity)
1098 return -1;
1100 return 0;
1103 /* Insert dummy tags in domain and range of "map".
1105 * In particular, if "map" is of the form
1107 * A -> B
1109 * then return
1111 * [A -> dummy_tag] -> [B -> dummy_tag]
1113 * where the dummy_tags are identical and equal to any dummy tags
1114 * introduced by any other call to this function.
1116 static __isl_give isl_map *insert_dummy_tags(__isl_take isl_map *map)
1118 static char dummy;
1119 isl_ctx *ctx;
1120 isl_id *id;
1121 isl_space *space;
1122 isl_set *domain, *range;
1124 ctx = isl_map_get_ctx(map);
1126 id = isl_id_alloc(ctx, NULL, &dummy);
1127 space = isl_space_params(isl_map_get_space(map));
1128 space = isl_space_set_from_params(space);
1129 space = isl_space_set_tuple_id(space, isl_dim_set, id);
1130 space = isl_space_map_from_set(space);
1132 domain = isl_map_wrap(map);
1133 range = isl_map_wrap(isl_map_universe(space));
1134 map = isl_map_from_domain_and_range(domain, range);
1135 map = isl_map_zip(map);
1137 return map;
1140 /* Given that at least one of "src" or "dst" is compressed, return
1141 * a map between the spaces of these nodes restricted to the affine
1142 * hull that was used in the compression.
1144 static __isl_give isl_map *extract_hull(struct isl_sched_node *src,
1145 struct isl_sched_node *dst)
1147 isl_set *dom, *ran;
1149 if (src->compressed)
1150 dom = isl_set_copy(src->hull);
1151 else
1152 dom = isl_set_universe(isl_space_copy(src->space));
1153 if (dst->compressed)
1154 ran = isl_set_copy(dst->hull);
1155 else
1156 ran = isl_set_universe(isl_space_copy(dst->space));
1158 return isl_map_from_domain_and_range(dom, ran);
1161 /* Intersect the domains of the nested relations in domain and range
1162 * of "tagged" with "map".
1164 static __isl_give isl_map *map_intersect_domains(__isl_take isl_map *tagged,
1165 __isl_keep isl_map *map)
1167 isl_set *set;
1169 tagged = isl_map_zip(tagged);
1170 set = isl_map_wrap(isl_map_copy(map));
1171 tagged = isl_map_intersect_domain(tagged, set);
1172 tagged = isl_map_zip(tagged);
1173 return tagged;
1176 /* Add a new edge to the graph based on the given map
1177 * and add it to data->graph->edge_table[data->type].
1178 * If a dependence relation of a given type happens to be identical
1179 * to one of the dependence relations of a type that was added before,
1180 * then we don't create a new edge, but instead mark the original edge
1181 * as also representing a dependence of the current type.
1183 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1184 * may be specified as "tagged" dependence relations. That is, "map"
1185 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1186 * the dependence on iterations and a and b are tags.
1187 * edge->map is set to the relation containing the elements i -> j,
1188 * while edge->tagged_condition and edge->tagged_validity contain
1189 * the union of all the "map" relations
1190 * for which extract_edge is called that result in the same edge->map.
1192 * If the source or the destination node is compressed, then
1193 * intersect both "map" and "tagged" with the constraints that
1194 * were used to construct the compression.
1195 * This ensures that there are no schedule constraints defined
1196 * outside of these domains, while the scheduler no longer has
1197 * any control over those outside parts.
1199 static isl_stat extract_edge(__isl_take isl_map *map, void *user)
1201 isl_ctx *ctx = isl_map_get_ctx(map);
1202 struct isl_extract_edge_data *data = user;
1203 struct isl_sched_graph *graph = data->graph;
1204 struct isl_sched_node *src, *dst;
1205 isl_space *dim;
1206 struct isl_sched_edge *edge;
1207 isl_map *tagged = NULL;
1209 if (data->type == isl_edge_condition ||
1210 data->type == isl_edge_conditional_validity) {
1211 if (isl_map_can_zip(map)) {
1212 tagged = isl_map_copy(map);
1213 map = isl_set_unwrap(isl_map_domain(isl_map_zip(map)));
1214 } else {
1215 tagged = insert_dummy_tags(isl_map_copy(map));
1219 dim = isl_space_domain(isl_map_get_space(map));
1220 src = graph_find_node(ctx, graph, dim);
1221 isl_space_free(dim);
1222 dim = isl_space_range(isl_map_get_space(map));
1223 dst = graph_find_node(ctx, graph, dim);
1224 isl_space_free(dim);
1226 if (!src || !dst) {
1227 isl_map_free(map);
1228 isl_map_free(tagged);
1229 return isl_stat_ok;
1232 if (src->compressed || dst->compressed) {
1233 isl_map *hull;
1234 hull = extract_hull(src, dst);
1235 if (tagged)
1236 tagged = map_intersect_domains(tagged, hull);
1237 map = isl_map_intersect(map, hull);
1240 graph->edge[graph->n_edge].src = src;
1241 graph->edge[graph->n_edge].dst = dst;
1242 graph->edge[graph->n_edge].map = map;
1243 graph->edge[graph->n_edge].validity = 0;
1244 graph->edge[graph->n_edge].coincidence = 0;
1245 graph->edge[graph->n_edge].proximity = 0;
1246 graph->edge[graph->n_edge].condition = 0;
1247 graph->edge[graph->n_edge].local = 0;
1248 graph->edge[graph->n_edge].conditional_validity = 0;
1249 graph->edge[graph->n_edge].tagged_condition = NULL;
1250 graph->edge[graph->n_edge].tagged_validity = NULL;
1251 if (data->type == isl_edge_validity)
1252 graph->edge[graph->n_edge].validity = 1;
1253 if (data->type == isl_edge_coincidence)
1254 graph->edge[graph->n_edge].coincidence = 1;
1255 if (data->type == isl_edge_proximity)
1256 graph->edge[graph->n_edge].proximity = 1;
1257 if (data->type == isl_edge_condition) {
1258 graph->edge[graph->n_edge].condition = 1;
1259 graph->edge[graph->n_edge].tagged_condition =
1260 isl_union_map_from_map(tagged);
1262 if (data->type == isl_edge_conditional_validity) {
1263 graph->edge[graph->n_edge].conditional_validity = 1;
1264 graph->edge[graph->n_edge].tagged_validity =
1265 isl_union_map_from_map(tagged);
1268 edge = graph_find_matching_edge(graph, &graph->edge[graph->n_edge]);
1269 if (!edge) {
1270 graph->n_edge++;
1271 return isl_stat_error;
1273 if (edge == &graph->edge[graph->n_edge])
1274 return graph_edge_table_add(ctx, graph, data->type,
1275 &graph->edge[graph->n_edge++]);
1277 if (merge_edge(data->type, edge, &graph->edge[graph->n_edge]) < 0)
1278 return -1;
1280 return graph_edge_table_add(ctx, graph, data->type, edge);
1283 /* Check whether there is any dependence from node[j] to node[i]
1284 * or from node[i] to node[j].
1286 static isl_bool node_follows_weak(int i, int j, void *user)
1288 isl_bool f;
1289 struct isl_sched_graph *graph = user;
1291 f = graph_has_any_edge(graph, &graph->node[j], &graph->node[i]);
1292 if (f < 0 || f)
1293 return f;
1294 return graph_has_any_edge(graph, &graph->node[i], &graph->node[j]);
1297 /* Check whether there is a (conditional) validity dependence from node[j]
1298 * to node[i], forcing node[i] to follow node[j].
1300 static isl_bool node_follows_strong(int i, int j, void *user)
1302 struct isl_sched_graph *graph = user;
1304 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
1307 /* Use Tarjan's algorithm for computing the strongly connected components
1308 * in the dependence graph (only validity edges).
1309 * If weak is set, we consider the graph to be undirected and
1310 * we effectively compute the (weakly) connected components.
1311 * Additionally, we also consider other edges when weak is set.
1313 static int detect_ccs(isl_ctx *ctx, struct isl_sched_graph *graph, int weak)
1315 int i, n;
1316 struct isl_tarjan_graph *g = NULL;
1318 g = isl_tarjan_graph_init(ctx, graph->n,
1319 weak ? &node_follows_weak : &node_follows_strong, graph);
1320 if (!g)
1321 return -1;
1323 graph->weak = weak;
1324 graph->scc = 0;
1325 i = 0;
1326 n = graph->n;
1327 while (n) {
1328 while (g->order[i] != -1) {
1329 graph->node[g->order[i]].scc = graph->scc;
1330 --n;
1331 ++i;
1333 ++i;
1334 graph->scc++;
1337 isl_tarjan_graph_free(g);
1339 return 0;
1342 /* Apply Tarjan's algorithm to detect the strongly connected components
1343 * in the dependence graph.
1345 static int detect_sccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1347 return detect_ccs(ctx, graph, 0);
1350 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1351 * in the dependence graph.
1353 static int detect_wccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1355 return detect_ccs(ctx, graph, 1);
1358 static int cmp_scc(const void *a, const void *b, void *data)
1360 struct isl_sched_graph *graph = data;
1361 const int *i1 = a;
1362 const int *i2 = b;
1364 return graph->node[*i1].scc - graph->node[*i2].scc;
1367 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1369 static int sort_sccs(struct isl_sched_graph *graph)
1371 return isl_sort(graph->sorted, graph->n, sizeof(int), &cmp_scc, graph);
1374 /* Given a dependence relation R from "node" to itself,
1375 * construct the set of coefficients of valid constraints for elements
1376 * in that dependence relation.
1377 * In particular, the result contains tuples of coefficients
1378 * c_0, c_n, c_x such that
1380 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1382 * or, equivalently,
1384 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1386 * We choose here to compute the dual of delta R.
1387 * Alternatively, we could have computed the dual of R, resulting
1388 * in a set of tuples c_0, c_n, c_x, c_y, and then
1389 * plugged in (c_0, c_n, c_x, -c_x).
1391 * If "node" has been compressed, then the dependence relation
1392 * is also compressed before the set of coefficients is computed.
1394 static __isl_give isl_basic_set *intra_coefficients(
1395 struct isl_sched_graph *graph, struct isl_sched_node *node,
1396 __isl_take isl_map *map)
1398 isl_set *delta;
1399 isl_map *key;
1400 isl_basic_set *coef;
1402 if (isl_map_to_basic_set_has(graph->intra_hmap, map))
1403 return isl_map_to_basic_set_get(graph->intra_hmap, map);
1405 key = isl_map_copy(map);
1406 if (node->compressed) {
1407 map = isl_map_preimage_domain_multi_aff(map,
1408 isl_multi_aff_copy(node->decompress));
1409 map = isl_map_preimage_range_multi_aff(map,
1410 isl_multi_aff_copy(node->decompress));
1412 delta = isl_set_remove_divs(isl_map_deltas(map));
1413 coef = isl_set_coefficients(delta);
1414 graph->intra_hmap = isl_map_to_basic_set_set(graph->intra_hmap, key,
1415 isl_basic_set_copy(coef));
1417 return coef;
1420 /* Given a dependence relation R, construct the set of coefficients
1421 * of valid constraints for elements in that dependence relation.
1422 * In particular, the result contains tuples of coefficients
1423 * c_0, c_n, c_x, c_y such that
1425 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1427 * If the source or destination nodes of "edge" have been compressed,
1428 * then the dependence relation is also compressed before
1429 * the set of coefficients is computed.
1431 static __isl_give isl_basic_set *inter_coefficients(
1432 struct isl_sched_graph *graph, struct isl_sched_edge *edge,
1433 __isl_take isl_map *map)
1435 isl_set *set;
1436 isl_map *key;
1437 isl_basic_set *coef;
1439 if (isl_map_to_basic_set_has(graph->inter_hmap, map))
1440 return isl_map_to_basic_set_get(graph->inter_hmap, map);
1442 key = isl_map_copy(map);
1443 if (edge->src->compressed)
1444 map = isl_map_preimage_domain_multi_aff(map,
1445 isl_multi_aff_copy(edge->src->decompress));
1446 if (edge->dst->compressed)
1447 map = isl_map_preimage_range_multi_aff(map,
1448 isl_multi_aff_copy(edge->dst->decompress));
1449 set = isl_map_wrap(isl_map_remove_divs(map));
1450 coef = isl_set_coefficients(set);
1451 graph->inter_hmap = isl_map_to_basic_set_set(graph->inter_hmap, key,
1452 isl_basic_set_copy(coef));
1454 return coef;
1457 /* Add constraints to graph->lp that force validity for the given
1458 * dependence from a node i to itself.
1459 * That is, add constraints that enforce
1461 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1462 * = c_i_x (y - x) >= 0
1464 * for each (x,y) in R.
1465 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1466 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1467 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1468 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1470 * Actually, we do not construct constraints for the c_i_x themselves,
1471 * but for the coefficients of c_i_x written as a linear combination
1472 * of the columns in node->cmap.
1474 static int add_intra_validity_constraints(struct isl_sched_graph *graph,
1475 struct isl_sched_edge *edge)
1477 unsigned total;
1478 isl_map *map = isl_map_copy(edge->map);
1479 isl_ctx *ctx = isl_map_get_ctx(map);
1480 isl_space *dim;
1481 isl_dim_map *dim_map;
1482 isl_basic_set *coef;
1483 struct isl_sched_node *node = edge->src;
1485 coef = intra_coefficients(graph, node, map);
1487 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1489 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1490 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
1491 if (!coef)
1492 goto error;
1494 total = isl_basic_set_total_dim(graph->lp);
1495 dim_map = isl_dim_map_alloc(ctx, total);
1496 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
1497 isl_space_dim(dim, isl_dim_set), 1,
1498 node->nvar, -1);
1499 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
1500 isl_space_dim(dim, isl_dim_set), 1,
1501 node->nvar, 1);
1502 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1503 coef->n_eq, coef->n_ineq);
1504 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1505 coef, dim_map);
1506 isl_space_free(dim);
1508 return 0;
1509 error:
1510 isl_space_free(dim);
1511 return -1;
1514 /* Add constraints to graph->lp that force validity for the given
1515 * dependence from node i to node j.
1516 * That is, add constraints that enforce
1518 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1520 * for each (x,y) in R.
1521 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1522 * of valid constraints for R and then plug in
1523 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
1524 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1525 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1526 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1528 * Actually, we do not construct constraints for the c_*_x themselves,
1529 * but for the coefficients of c_*_x written as a linear combination
1530 * of the columns in node->cmap.
1532 static int add_inter_validity_constraints(struct isl_sched_graph *graph,
1533 struct isl_sched_edge *edge)
1535 unsigned total;
1536 isl_map *map = isl_map_copy(edge->map);
1537 isl_ctx *ctx = isl_map_get_ctx(map);
1538 isl_space *dim;
1539 isl_dim_map *dim_map;
1540 isl_basic_set *coef;
1541 struct isl_sched_node *src = edge->src;
1542 struct isl_sched_node *dst = edge->dst;
1544 coef = inter_coefficients(graph, edge, map);
1546 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1548 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1549 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1550 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1551 isl_space_dim(dim, isl_dim_set) + src->nvar,
1552 isl_mat_copy(dst->cmap));
1553 if (!coef)
1554 goto error;
1556 total = isl_basic_set_total_dim(graph->lp);
1557 dim_map = isl_dim_map_alloc(ctx, total);
1559 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
1560 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
1561 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
1562 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1563 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1564 dst->nvar, -1);
1565 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1566 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1567 dst->nvar, 1);
1569 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
1570 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
1571 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
1572 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1573 isl_space_dim(dim, isl_dim_set), 1,
1574 src->nvar, 1);
1575 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1576 isl_space_dim(dim, isl_dim_set), 1,
1577 src->nvar, -1);
1579 edge->start = graph->lp->n_ineq;
1580 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1581 coef->n_eq, coef->n_ineq);
1582 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1583 coef, dim_map);
1584 if (!graph->lp)
1585 goto error;
1586 isl_space_free(dim);
1587 edge->end = graph->lp->n_ineq;
1589 return 0;
1590 error:
1591 isl_space_free(dim);
1592 return -1;
1595 /* Add constraints to graph->lp that bound the dependence distance for the given
1596 * dependence from a node i to itself.
1597 * If s = 1, we add the constraint
1599 * c_i_x (y - x) <= m_0 + m_n n
1601 * or
1603 * -c_i_x (y - x) + m_0 + m_n n >= 0
1605 * for each (x,y) in R.
1606 * If s = -1, we add the constraint
1608 * -c_i_x (y - x) <= m_0 + m_n n
1610 * or
1612 * c_i_x (y - x) + m_0 + m_n n >= 0
1614 * for each (x,y) in R.
1615 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1616 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1617 * with each coefficient (except m_0) represented as a pair of non-negative
1618 * coefficients.
1620 * Actually, we do not construct constraints for the c_i_x themselves,
1621 * but for the coefficients of c_i_x written as a linear combination
1622 * of the columns in node->cmap.
1625 * If "local" is set, then we add constraints
1627 * c_i_x (y - x) <= 0
1629 * or
1631 * -c_i_x (y - x) <= 0
1633 * instead, forcing the dependence distance to be (less than or) equal to 0.
1634 * That is, we plug in (0, 0, -s * c_i_x),
1635 * Note that dependences marked local are treated as validity constraints
1636 * by add_all_validity_constraints and therefore also have
1637 * their distances bounded by 0 from below.
1639 static int add_intra_proximity_constraints(struct isl_sched_graph *graph,
1640 struct isl_sched_edge *edge, int s, int local)
1642 unsigned total;
1643 unsigned nparam;
1644 isl_map *map = isl_map_copy(edge->map);
1645 isl_ctx *ctx = isl_map_get_ctx(map);
1646 isl_space *dim;
1647 isl_dim_map *dim_map;
1648 isl_basic_set *coef;
1649 struct isl_sched_node *node = edge->src;
1651 coef = intra_coefficients(graph, node, map);
1653 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1655 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1656 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
1657 if (!coef)
1658 goto error;
1660 nparam = isl_space_dim(node->space, isl_dim_param);
1661 total = isl_basic_set_total_dim(graph->lp);
1662 dim_map = isl_dim_map_alloc(ctx, total);
1664 if (!local) {
1665 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1666 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1667 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1669 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
1670 isl_space_dim(dim, isl_dim_set), 1,
1671 node->nvar, s);
1672 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
1673 isl_space_dim(dim, isl_dim_set), 1,
1674 node->nvar, -s);
1675 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1676 coef->n_eq, coef->n_ineq);
1677 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1678 coef, dim_map);
1679 isl_space_free(dim);
1681 return 0;
1682 error:
1683 isl_space_free(dim);
1684 return -1;
1687 /* Add constraints to graph->lp that bound the dependence distance for the given
1688 * dependence from node i to node j.
1689 * If s = 1, we add the constraint
1691 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1692 * <= m_0 + m_n n
1694 * or
1696 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1697 * m_0 + m_n n >= 0
1699 * for each (x,y) in R.
1700 * If s = -1, we add the constraint
1702 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1703 * <= m_0 + m_n n
1705 * or
1707 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1708 * m_0 + m_n n >= 0
1710 * for each (x,y) in R.
1711 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1712 * of valid constraints for R and then plug in
1713 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1714 * -s*c_j_x+s*c_i_x)
1715 * with each coefficient (except m_0, c_j_0 and c_i_0)
1716 * represented as a pair of non-negative coefficients.
1718 * Actually, we do not construct constraints for the c_*_x themselves,
1719 * but for the coefficients of c_*_x written as a linear combination
1720 * of the columns in node->cmap.
1723 * If "local" is set, then we add constraints
1725 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1727 * or
1729 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
1731 * instead, forcing the dependence distance to be (less than or) equal to 0.
1732 * That is, we plug in
1733 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
1734 * Note that dependences marked local are treated as validity constraints
1735 * by add_all_validity_constraints and therefore also have
1736 * their distances bounded by 0 from below.
1738 static int add_inter_proximity_constraints(struct isl_sched_graph *graph,
1739 struct isl_sched_edge *edge, int s, int local)
1741 unsigned total;
1742 unsigned nparam;
1743 isl_map *map = isl_map_copy(edge->map);
1744 isl_ctx *ctx = isl_map_get_ctx(map);
1745 isl_space *dim;
1746 isl_dim_map *dim_map;
1747 isl_basic_set *coef;
1748 struct isl_sched_node *src = edge->src;
1749 struct isl_sched_node *dst = edge->dst;
1751 coef = inter_coefficients(graph, edge, map);
1753 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1755 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1756 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1757 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1758 isl_space_dim(dim, isl_dim_set) + src->nvar,
1759 isl_mat_copy(dst->cmap));
1760 if (!coef)
1761 goto error;
1763 nparam = isl_space_dim(src->space, isl_dim_param);
1764 total = isl_basic_set_total_dim(graph->lp);
1765 dim_map = isl_dim_map_alloc(ctx, total);
1767 if (!local) {
1768 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1769 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1770 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1773 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, -s);
1774 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, s);
1775 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, -s);
1776 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1777 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1778 dst->nvar, s);
1779 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1780 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1781 dst->nvar, -s);
1783 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, s);
1784 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, -s);
1785 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, s);
1786 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1787 isl_space_dim(dim, isl_dim_set), 1,
1788 src->nvar, -s);
1789 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1790 isl_space_dim(dim, isl_dim_set), 1,
1791 src->nvar, s);
1793 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1794 coef->n_eq, coef->n_ineq);
1795 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1796 coef, dim_map);
1797 isl_space_free(dim);
1799 return 0;
1800 error:
1801 isl_space_free(dim);
1802 return -1;
1805 /* Add all validity constraints to graph->lp.
1807 * An edge that is forced to be local needs to have its dependence
1808 * distances equal to zero. We take care of bounding them by 0 from below
1809 * here. add_all_proximity_constraints takes care of bounding them by 0
1810 * from above.
1812 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1813 * Otherwise, we ignore them.
1815 static int add_all_validity_constraints(struct isl_sched_graph *graph,
1816 int use_coincidence)
1818 int i;
1820 for (i = 0; i < graph->n_edge; ++i) {
1821 struct isl_sched_edge *edge= &graph->edge[i];
1822 int local;
1824 local = edge->local || (edge->coincidence && use_coincidence);
1825 if (!edge->validity && !local)
1826 continue;
1827 if (edge->src != edge->dst)
1828 continue;
1829 if (add_intra_validity_constraints(graph, edge) < 0)
1830 return -1;
1833 for (i = 0; i < graph->n_edge; ++i) {
1834 struct isl_sched_edge *edge = &graph->edge[i];
1835 int local;
1837 local = edge->local || (edge->coincidence && use_coincidence);
1838 if (!edge->validity && !local)
1839 continue;
1840 if (edge->src == edge->dst)
1841 continue;
1842 if (add_inter_validity_constraints(graph, edge) < 0)
1843 return -1;
1846 return 0;
1849 /* Add constraints to graph->lp that bound the dependence distance
1850 * for all dependence relations.
1851 * If a given proximity dependence is identical to a validity
1852 * dependence, then the dependence distance is already bounded
1853 * from below (by zero), so we only need to bound the distance
1854 * from above. (This includes the case of "local" dependences
1855 * which are treated as validity dependence by add_all_validity_constraints.)
1856 * Otherwise, we need to bound the distance both from above and from below.
1858 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1859 * Otherwise, we ignore them.
1861 static int add_all_proximity_constraints(struct isl_sched_graph *graph,
1862 int use_coincidence)
1864 int i;
1866 for (i = 0; i < graph->n_edge; ++i) {
1867 struct isl_sched_edge *edge= &graph->edge[i];
1868 int local;
1870 local = edge->local || (edge->coincidence && use_coincidence);
1871 if (!edge->proximity && !local)
1872 continue;
1873 if (edge->src == edge->dst &&
1874 add_intra_proximity_constraints(graph, edge, 1, local) < 0)
1875 return -1;
1876 if (edge->src != edge->dst &&
1877 add_inter_proximity_constraints(graph, edge, 1, local) < 0)
1878 return -1;
1879 if (edge->validity || local)
1880 continue;
1881 if (edge->src == edge->dst &&
1882 add_intra_proximity_constraints(graph, edge, -1, 0) < 0)
1883 return -1;
1884 if (edge->src != edge->dst &&
1885 add_inter_proximity_constraints(graph, edge, -1, 0) < 0)
1886 return -1;
1889 return 0;
1892 /* Compute a basis for the rows in the linear part of the schedule
1893 * and extend this basis to a full basis. The remaining rows
1894 * can then be used to force linear independence from the rows
1895 * in the schedule.
1897 * In particular, given the schedule rows S, we compute
1899 * S = H Q
1900 * S U = H
1902 * with H the Hermite normal form of S. That is, all but the
1903 * first rank columns of H are zero and so each row in S is
1904 * a linear combination of the first rank rows of Q.
1905 * The matrix Q is then transposed because we will write the
1906 * coefficients of the next schedule row as a column vector s
1907 * and express this s as a linear combination s = Q c of the
1908 * computed basis.
1909 * Similarly, the matrix U is transposed such that we can
1910 * compute the coefficients c = U s from a schedule row s.
1912 static int node_update_cmap(struct isl_sched_node *node)
1914 isl_mat *H, *U, *Q;
1915 int n_row = isl_mat_rows(node->sched);
1917 H = isl_mat_sub_alloc(node->sched, 0, n_row,
1918 1 + node->nparam, node->nvar);
1920 H = isl_mat_left_hermite(H, 0, &U, &Q);
1921 isl_mat_free(node->cmap);
1922 isl_mat_free(node->cinv);
1923 node->cmap = isl_mat_transpose(Q);
1924 node->cinv = isl_mat_transpose(U);
1925 node->rank = isl_mat_initial_non_zero_cols(H);
1926 isl_mat_free(H);
1928 if (!node->cmap || !node->cinv || node->rank < 0)
1929 return -1;
1930 return 0;
1933 /* How many times should we count the constraints in "edge"?
1935 * If carry is set, then we are counting the number of
1936 * (validity or conditional validity) constraints that will be added
1937 * in setup_carry_lp and we count each edge exactly once.
1939 * Otherwise, we count as follows
1940 * validity -> 1 (>= 0)
1941 * validity+proximity -> 2 (>= 0 and upper bound)
1942 * proximity -> 2 (lower and upper bound)
1943 * local(+any) -> 2 (>= 0 and <= 0)
1945 * If an edge is only marked conditional_validity then it counts
1946 * as zero since it is only checked afterwards.
1948 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1949 * Otherwise, we ignore them.
1951 static int edge_multiplicity(struct isl_sched_edge *edge, int carry,
1952 int use_coincidence)
1954 if (carry && !edge->validity && !edge->conditional_validity)
1955 return 0;
1956 if (carry)
1957 return 1;
1958 if (edge->proximity || edge->local)
1959 return 2;
1960 if (use_coincidence && edge->coincidence)
1961 return 2;
1962 if (edge->validity)
1963 return 1;
1964 return 0;
1967 /* Count the number of equality and inequality constraints
1968 * that will be added for the given map.
1970 * "use_coincidence" is set if we should take into account coincidence edges.
1972 static int count_map_constraints(struct isl_sched_graph *graph,
1973 struct isl_sched_edge *edge, __isl_take isl_map *map,
1974 int *n_eq, int *n_ineq, int carry, int use_coincidence)
1976 isl_basic_set *coef;
1977 int f = edge_multiplicity(edge, carry, use_coincidence);
1979 if (f == 0) {
1980 isl_map_free(map);
1981 return 0;
1984 if (edge->src == edge->dst)
1985 coef = intra_coefficients(graph, edge->src, map);
1986 else
1987 coef = inter_coefficients(graph, edge, map);
1988 if (!coef)
1989 return -1;
1990 *n_eq += f * coef->n_eq;
1991 *n_ineq += f * coef->n_ineq;
1992 isl_basic_set_free(coef);
1994 return 0;
1997 /* Count the number of equality and inequality constraints
1998 * that will be added to the main lp problem.
1999 * We count as follows
2000 * validity -> 1 (>= 0)
2001 * validity+proximity -> 2 (>= 0 and upper bound)
2002 * proximity -> 2 (lower and upper bound)
2003 * local(+any) -> 2 (>= 0 and <= 0)
2005 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2006 * Otherwise, we ignore them.
2008 static int count_constraints(struct isl_sched_graph *graph,
2009 int *n_eq, int *n_ineq, int use_coincidence)
2011 int i;
2013 *n_eq = *n_ineq = 0;
2014 for (i = 0; i < graph->n_edge; ++i) {
2015 struct isl_sched_edge *edge= &graph->edge[i];
2016 isl_map *map = isl_map_copy(edge->map);
2018 if (count_map_constraints(graph, edge, map, n_eq, n_ineq,
2019 0, use_coincidence) < 0)
2020 return -1;
2023 return 0;
2026 /* Count the number of constraints that will be added by
2027 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
2028 * accordingly.
2030 * In practice, add_bound_coefficient_constraints only adds inequalities.
2032 static int count_bound_coefficient_constraints(isl_ctx *ctx,
2033 struct isl_sched_graph *graph, int *n_eq, int *n_ineq)
2035 int i;
2037 if (ctx->opt->schedule_max_coefficient == -1)
2038 return 0;
2040 for (i = 0; i < graph->n; ++i)
2041 *n_ineq += 2 * graph->node[i].nparam + 2 * graph->node[i].nvar;
2043 return 0;
2046 /* Add constraints that bound the values of the variable and parameter
2047 * coefficients of the schedule.
2049 * The maximal value of the coefficients is defined by the option
2050 * 'schedule_max_coefficient'.
2052 static int add_bound_coefficient_constraints(isl_ctx *ctx,
2053 struct isl_sched_graph *graph)
2055 int i, j, k;
2056 int max_coefficient;
2057 int total;
2059 max_coefficient = ctx->opt->schedule_max_coefficient;
2061 if (max_coefficient == -1)
2062 return 0;
2064 total = isl_basic_set_total_dim(graph->lp);
2066 for (i = 0; i < graph->n; ++i) {
2067 struct isl_sched_node *node = &graph->node[i];
2068 for (j = 0; j < 2 * node->nparam + 2 * node->nvar; ++j) {
2069 int dim;
2070 k = isl_basic_set_alloc_inequality(graph->lp);
2071 if (k < 0)
2072 return -1;
2073 dim = 1 + node->start + 1 + j;
2074 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2075 isl_int_set_si(graph->lp->ineq[k][dim], -1);
2076 isl_int_set_si(graph->lp->ineq[k][0], max_coefficient);
2080 return 0;
2083 /* Construct an ILP problem for finding schedule coefficients
2084 * that result in non-negative, but small dependence distances
2085 * over all dependences.
2086 * In particular, the dependence distances over proximity edges
2087 * are bounded by m_0 + m_n n and we compute schedule coefficients
2088 * with small values (preferably zero) of m_n and m_0.
2090 * All variables of the ILP are non-negative. The actual coefficients
2091 * may be negative, so each coefficient is represented as the difference
2092 * of two non-negative variables. The negative part always appears
2093 * immediately before the positive part.
2094 * Other than that, the variables have the following order
2096 * - sum of positive and negative parts of m_n coefficients
2097 * - m_0
2098 * - sum of positive and negative parts of all c_n coefficients
2099 * (unconstrained when computing non-parametric schedules)
2100 * - sum of positive and negative parts of all c_x coefficients
2101 * - positive and negative parts of m_n coefficients
2102 * - for each node
2103 * - c_i_0
2104 * - positive and negative parts of c_i_n (if parametric)
2105 * - positive and negative parts of c_i_x
2107 * The c_i_x are not represented directly, but through the columns of
2108 * node->cmap. That is, the computed values are for variable t_i_x
2109 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
2111 * The constraints are those from the edges plus two or three equalities
2112 * to express the sums.
2114 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2115 * Otherwise, we ignore them.
2117 static int setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
2118 int use_coincidence)
2120 int i, j;
2121 int k;
2122 unsigned nparam;
2123 unsigned total;
2124 isl_space *dim;
2125 int parametric;
2126 int param_pos;
2127 int n_eq, n_ineq;
2128 int max_constant_term;
2130 max_constant_term = ctx->opt->schedule_max_constant_term;
2132 parametric = ctx->opt->schedule_parametric;
2133 nparam = isl_space_dim(graph->node[0].space, isl_dim_param);
2134 param_pos = 4;
2135 total = param_pos + 2 * nparam;
2136 for (i = 0; i < graph->n; ++i) {
2137 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2138 if (node_update_cmap(node) < 0)
2139 return -1;
2140 node->start = total;
2141 total += 1 + 2 * (node->nparam + node->nvar);
2144 if (count_constraints(graph, &n_eq, &n_ineq, use_coincidence) < 0)
2145 return -1;
2146 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
2147 return -1;
2149 dim = isl_space_set_alloc(ctx, 0, total);
2150 isl_basic_set_free(graph->lp);
2151 n_eq += 2 + parametric;
2152 if (max_constant_term != -1)
2153 n_ineq += graph->n;
2155 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
2157 k = isl_basic_set_alloc_equality(graph->lp);
2158 if (k < 0)
2159 return -1;
2160 isl_seq_clr(graph->lp->eq[k], 1 + total);
2161 isl_int_set_si(graph->lp->eq[k][1], -1);
2162 for (i = 0; i < 2 * nparam; ++i)
2163 isl_int_set_si(graph->lp->eq[k][1 + param_pos + i], 1);
2165 if (parametric) {
2166 k = isl_basic_set_alloc_equality(graph->lp);
2167 if (k < 0)
2168 return -1;
2169 isl_seq_clr(graph->lp->eq[k], 1 + total);
2170 isl_int_set_si(graph->lp->eq[k][3], -1);
2171 for (i = 0; i < graph->n; ++i) {
2172 int pos = 1 + graph->node[i].start + 1;
2174 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
2175 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2179 k = isl_basic_set_alloc_equality(graph->lp);
2180 if (k < 0)
2181 return -1;
2182 isl_seq_clr(graph->lp->eq[k], 1 + total);
2183 isl_int_set_si(graph->lp->eq[k][4], -1);
2184 for (i = 0; i < graph->n; ++i) {
2185 struct isl_sched_node *node = &graph->node[i];
2186 int pos = 1 + node->start + 1 + 2 * node->nparam;
2188 for (j = 0; j < 2 * node->nvar; ++j)
2189 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2192 if (max_constant_term != -1)
2193 for (i = 0; i < graph->n; ++i) {
2194 struct isl_sched_node *node = &graph->node[i];
2195 k = isl_basic_set_alloc_inequality(graph->lp);
2196 if (k < 0)
2197 return -1;
2198 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2199 isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
2200 isl_int_set_si(graph->lp->ineq[k][0], max_constant_term);
2203 if (add_bound_coefficient_constraints(ctx, graph) < 0)
2204 return -1;
2205 if (add_all_validity_constraints(graph, use_coincidence) < 0)
2206 return -1;
2207 if (add_all_proximity_constraints(graph, use_coincidence) < 0)
2208 return -1;
2210 return 0;
2213 /* Analyze the conflicting constraint found by
2214 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2215 * constraint of one of the edges between distinct nodes, living, moreover
2216 * in distinct SCCs, then record the source and sink SCC as this may
2217 * be a good place to cut between SCCs.
2219 static int check_conflict(int con, void *user)
2221 int i;
2222 struct isl_sched_graph *graph = user;
2224 if (graph->src_scc >= 0)
2225 return 0;
2227 con -= graph->lp->n_eq;
2229 if (con >= graph->lp->n_ineq)
2230 return 0;
2232 for (i = 0; i < graph->n_edge; ++i) {
2233 if (!graph->edge[i].validity)
2234 continue;
2235 if (graph->edge[i].src == graph->edge[i].dst)
2236 continue;
2237 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
2238 continue;
2239 if (graph->edge[i].start > con)
2240 continue;
2241 if (graph->edge[i].end <= con)
2242 continue;
2243 graph->src_scc = graph->edge[i].src->scc;
2244 graph->dst_scc = graph->edge[i].dst->scc;
2247 return 0;
2250 /* Check whether the next schedule row of the given node needs to be
2251 * non-trivial. Lower-dimensional domains may have some trivial rows,
2252 * but as soon as the number of remaining required non-trivial rows
2253 * is as large as the number or remaining rows to be computed,
2254 * all remaining rows need to be non-trivial.
2256 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
2258 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
2261 /* Solve the ILP problem constructed in setup_lp.
2262 * For each node such that all the remaining rows of its schedule
2263 * need to be non-trivial, we construct a non-triviality region.
2264 * This region imposes that the next row is independent of previous rows.
2265 * In particular the coefficients c_i_x are represented by t_i_x
2266 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
2267 * its first columns span the rows of the previously computed part
2268 * of the schedule. The non-triviality region enforces that at least
2269 * one of the remaining components of t_i_x is non-zero, i.e.,
2270 * that the new schedule row depends on at least one of the remaining
2271 * columns of Q.
2273 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
2275 int i;
2276 isl_vec *sol;
2277 isl_basic_set *lp;
2279 for (i = 0; i < graph->n; ++i) {
2280 struct isl_sched_node *node = &graph->node[i];
2281 int skip = node->rank;
2282 graph->region[i].pos = node->start + 1 + 2*(node->nparam+skip);
2283 if (needs_row(graph, node))
2284 graph->region[i].len = 2 * (node->nvar - skip);
2285 else
2286 graph->region[i].len = 0;
2288 lp = isl_basic_set_copy(graph->lp);
2289 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
2290 graph->region, &check_conflict, graph);
2291 return sol;
2294 /* Update the schedules of all nodes based on the given solution
2295 * of the LP problem.
2296 * The new row is added to the current band.
2297 * All possibly negative coefficients are encoded as a difference
2298 * of two non-negative variables, so we need to perform the subtraction
2299 * here. Moreover, if use_cmap is set, then the solution does
2300 * not refer to the actual coefficients c_i_x, but instead to variables
2301 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2302 * In this case, we then also need to perform this multiplication
2303 * to obtain the values of c_i_x.
2305 * If coincident is set, then the caller guarantees that the new
2306 * row satisfies the coincidence constraints.
2308 static int update_schedule(struct isl_sched_graph *graph,
2309 __isl_take isl_vec *sol, int use_cmap, int coincident)
2311 int i, j;
2312 isl_vec *csol = NULL;
2314 if (!sol)
2315 goto error;
2316 if (sol->size == 0)
2317 isl_die(sol->ctx, isl_error_internal,
2318 "no solution found", goto error);
2319 if (graph->n_total_row >= graph->max_row)
2320 isl_die(sol->ctx, isl_error_internal,
2321 "too many schedule rows", goto error);
2323 for (i = 0; i < graph->n; ++i) {
2324 struct isl_sched_node *node = &graph->node[i];
2325 int pos = node->start;
2326 int row = isl_mat_rows(node->sched);
2328 isl_vec_free(csol);
2329 csol = isl_vec_alloc(sol->ctx, node->nvar);
2330 if (!csol)
2331 goto error;
2333 isl_map_free(node->sched_map);
2334 node->sched_map = NULL;
2335 node->sched = isl_mat_add_rows(node->sched, 1);
2336 if (!node->sched)
2337 goto error;
2338 node->sched = isl_mat_set_element(node->sched, row, 0,
2339 sol->el[1 + pos]);
2340 for (j = 0; j < node->nparam + node->nvar; ++j)
2341 isl_int_sub(sol->el[1 + pos + 1 + 2 * j + 1],
2342 sol->el[1 + pos + 1 + 2 * j + 1],
2343 sol->el[1 + pos + 1 + 2 * j]);
2344 for (j = 0; j < node->nparam; ++j)
2345 node->sched = isl_mat_set_element(node->sched,
2346 row, 1 + j, sol->el[1+pos+1+2*j+1]);
2347 for (j = 0; j < node->nvar; ++j)
2348 isl_int_set(csol->el[j],
2349 sol->el[1+pos+1+2*(node->nparam+j)+1]);
2350 if (use_cmap)
2351 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
2352 csol);
2353 if (!csol)
2354 goto error;
2355 for (j = 0; j < node->nvar; ++j)
2356 node->sched = isl_mat_set_element(node->sched,
2357 row, 1 + node->nparam + j, csol->el[j]);
2358 node->coincident[graph->n_total_row] = coincident;
2360 isl_vec_free(sol);
2361 isl_vec_free(csol);
2363 graph->n_row++;
2364 graph->n_total_row++;
2366 return 0;
2367 error:
2368 isl_vec_free(sol);
2369 isl_vec_free(csol);
2370 return -1;
2373 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2374 * and return this isl_aff.
2376 static __isl_give isl_aff *extract_schedule_row(__isl_take isl_local_space *ls,
2377 struct isl_sched_node *node, int row)
2379 int j;
2380 isl_int v;
2381 isl_aff *aff;
2383 isl_int_init(v);
2385 aff = isl_aff_zero_on_domain(ls);
2386 isl_mat_get_element(node->sched, row, 0, &v);
2387 aff = isl_aff_set_constant(aff, v);
2388 for (j = 0; j < node->nparam; ++j) {
2389 isl_mat_get_element(node->sched, row, 1 + j, &v);
2390 aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
2392 for (j = 0; j < node->nvar; ++j) {
2393 isl_mat_get_element(node->sched, row, 1 + node->nparam + j, &v);
2394 aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
2397 isl_int_clear(v);
2399 return aff;
2402 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
2403 * and return this multi_aff.
2405 * The result is defined over the uncompressed node domain.
2407 static __isl_give isl_multi_aff *node_extract_partial_schedule_multi_aff(
2408 struct isl_sched_node *node, int first, int n)
2410 int i;
2411 isl_space *space;
2412 isl_local_space *ls;
2413 isl_aff *aff;
2414 isl_multi_aff *ma;
2415 int nrow;
2417 nrow = isl_mat_rows(node->sched);
2418 if (node->compressed)
2419 space = isl_multi_aff_get_domain_space(node->decompress);
2420 else
2421 space = isl_space_copy(node->space);
2422 ls = isl_local_space_from_space(isl_space_copy(space));
2423 space = isl_space_from_domain(space);
2424 space = isl_space_add_dims(space, isl_dim_out, n);
2425 ma = isl_multi_aff_zero(space);
2427 for (i = first; i < first + n; ++i) {
2428 aff = extract_schedule_row(isl_local_space_copy(ls), node, i);
2429 ma = isl_multi_aff_set_aff(ma, i - first, aff);
2432 isl_local_space_free(ls);
2434 if (node->compressed)
2435 ma = isl_multi_aff_pullback_multi_aff(ma,
2436 isl_multi_aff_copy(node->compress));
2438 return ma;
2441 /* Convert node->sched into a multi_aff and return this multi_aff.
2443 * The result is defined over the uncompressed node domain.
2445 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
2446 struct isl_sched_node *node)
2448 int nrow;
2450 nrow = isl_mat_rows(node->sched);
2451 return node_extract_partial_schedule_multi_aff(node, 0, nrow);
2454 /* Convert node->sched into a map and return this map.
2456 * The result is cached in node->sched_map, which needs to be released
2457 * whenever node->sched is updated.
2458 * It is defined over the uncompressed node domain.
2460 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
2462 if (!node->sched_map) {
2463 isl_multi_aff *ma;
2465 ma = node_extract_schedule_multi_aff(node);
2466 node->sched_map = isl_map_from_multi_aff(ma);
2469 return isl_map_copy(node->sched_map);
2472 /* Construct a map that can be used to update a dependence relation
2473 * based on the current schedule.
2474 * That is, construct a map expressing that source and sink
2475 * are executed within the same iteration of the current schedule.
2476 * This map can then be intersected with the dependence relation.
2477 * This is not the most efficient way, but this shouldn't be a critical
2478 * operation.
2480 static __isl_give isl_map *specializer(struct isl_sched_node *src,
2481 struct isl_sched_node *dst)
2483 isl_map *src_sched, *dst_sched;
2485 src_sched = node_extract_schedule(src);
2486 dst_sched = node_extract_schedule(dst);
2487 return isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
2490 /* Intersect the domains of the nested relations in domain and range
2491 * of "umap" with "map".
2493 static __isl_give isl_union_map *intersect_domains(
2494 __isl_take isl_union_map *umap, __isl_keep isl_map *map)
2496 isl_union_set *uset;
2498 umap = isl_union_map_zip(umap);
2499 uset = isl_union_set_from_set(isl_map_wrap(isl_map_copy(map)));
2500 umap = isl_union_map_intersect_domain(umap, uset);
2501 umap = isl_union_map_zip(umap);
2502 return umap;
2505 /* Update the dependence relation of the given edge based
2506 * on the current schedule.
2507 * If the dependence is carried completely by the current schedule, then
2508 * it is removed from the edge_tables. It is kept in the list of edges
2509 * as otherwise all edge_tables would have to be recomputed.
2511 static int update_edge(struct isl_sched_graph *graph,
2512 struct isl_sched_edge *edge)
2514 int empty;
2515 isl_map *id;
2517 id = specializer(edge->src, edge->dst);
2518 edge->map = isl_map_intersect(edge->map, isl_map_copy(id));
2519 if (!edge->map)
2520 goto error;
2522 if (edge->tagged_condition) {
2523 edge->tagged_condition =
2524 intersect_domains(edge->tagged_condition, id);
2525 if (!edge->tagged_condition)
2526 goto error;
2528 if (edge->tagged_validity) {
2529 edge->tagged_validity =
2530 intersect_domains(edge->tagged_validity, id);
2531 if (!edge->tagged_validity)
2532 goto error;
2535 empty = isl_map_plain_is_empty(edge->map);
2536 if (empty < 0)
2537 goto error;
2538 if (empty)
2539 graph_remove_edge(graph, edge);
2541 isl_map_free(id);
2542 return 0;
2543 error:
2544 isl_map_free(id);
2545 return -1;
2548 /* Does the domain of "umap" intersect "uset"?
2550 static int domain_intersects(__isl_keep isl_union_map *umap,
2551 __isl_keep isl_union_set *uset)
2553 int empty;
2555 umap = isl_union_map_copy(umap);
2556 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(uset));
2557 empty = isl_union_map_is_empty(umap);
2558 isl_union_map_free(umap);
2560 return empty < 0 ? -1 : !empty;
2563 /* Does the range of "umap" intersect "uset"?
2565 static int range_intersects(__isl_keep isl_union_map *umap,
2566 __isl_keep isl_union_set *uset)
2568 int empty;
2570 umap = isl_union_map_copy(umap);
2571 umap = isl_union_map_intersect_range(umap, isl_union_set_copy(uset));
2572 empty = isl_union_map_is_empty(umap);
2573 isl_union_map_free(umap);
2575 return empty < 0 ? -1 : !empty;
2578 /* Are the condition dependences of "edge" local with respect to
2579 * the current schedule?
2581 * That is, are domain and range of the condition dependences mapped
2582 * to the same point?
2584 * In other words, is the condition false?
2586 static int is_condition_false(struct isl_sched_edge *edge)
2588 isl_union_map *umap;
2589 isl_map *map, *sched, *test;
2590 int empty, local;
2592 empty = isl_union_map_is_empty(edge->tagged_condition);
2593 if (empty < 0 || empty)
2594 return empty;
2596 umap = isl_union_map_copy(edge->tagged_condition);
2597 umap = isl_union_map_zip(umap);
2598 umap = isl_union_set_unwrap(isl_union_map_domain(umap));
2599 map = isl_map_from_union_map(umap);
2601 sched = node_extract_schedule(edge->src);
2602 map = isl_map_apply_domain(map, sched);
2603 sched = node_extract_schedule(edge->dst);
2604 map = isl_map_apply_range(map, sched);
2606 test = isl_map_identity(isl_map_get_space(map));
2607 local = isl_map_is_subset(map, test);
2608 isl_map_free(map);
2609 isl_map_free(test);
2611 return local;
2614 /* For each conditional validity constraint that is adjacent
2615 * to a condition with domain in condition_source or range in condition_sink,
2616 * turn it into an unconditional validity constraint.
2618 static int unconditionalize_adjacent_validity(struct isl_sched_graph *graph,
2619 __isl_take isl_union_set *condition_source,
2620 __isl_take isl_union_set *condition_sink)
2622 int i;
2624 condition_source = isl_union_set_coalesce(condition_source);
2625 condition_sink = isl_union_set_coalesce(condition_sink);
2627 for (i = 0; i < graph->n_edge; ++i) {
2628 int adjacent;
2629 isl_union_map *validity;
2631 if (!graph->edge[i].conditional_validity)
2632 continue;
2633 if (graph->edge[i].validity)
2634 continue;
2636 validity = graph->edge[i].tagged_validity;
2637 adjacent = domain_intersects(validity, condition_sink);
2638 if (adjacent >= 0 && !adjacent)
2639 adjacent = range_intersects(validity, condition_source);
2640 if (adjacent < 0)
2641 goto error;
2642 if (!adjacent)
2643 continue;
2645 graph->edge[i].validity = 1;
2648 isl_union_set_free(condition_source);
2649 isl_union_set_free(condition_sink);
2650 return 0;
2651 error:
2652 isl_union_set_free(condition_source);
2653 isl_union_set_free(condition_sink);
2654 return -1;
2657 /* Update the dependence relations of all edges based on the current schedule
2658 * and enforce conditional validity constraints that are adjacent
2659 * to satisfied condition constraints.
2661 * First check if any of the condition constraints are satisfied
2662 * (i.e., not local to the outer schedule) and keep track of
2663 * their domain and range.
2664 * Then update all dependence relations (which removes the non-local
2665 * constraints).
2666 * Finally, if any condition constraints turned out to be satisfied,
2667 * then turn all adjacent conditional validity constraints into
2668 * unconditional validity constraints.
2670 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
2672 int i;
2673 int any = 0;
2674 isl_union_set *source, *sink;
2676 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
2677 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
2678 for (i = 0; i < graph->n_edge; ++i) {
2679 int local;
2680 isl_union_set *uset;
2681 isl_union_map *umap;
2683 if (!graph->edge[i].condition)
2684 continue;
2685 if (graph->edge[i].local)
2686 continue;
2687 local = is_condition_false(&graph->edge[i]);
2688 if (local < 0)
2689 goto error;
2690 if (local)
2691 continue;
2693 any = 1;
2695 umap = isl_union_map_copy(graph->edge[i].tagged_condition);
2696 uset = isl_union_map_domain(umap);
2697 source = isl_union_set_union(source, uset);
2699 umap = isl_union_map_copy(graph->edge[i].tagged_condition);
2700 uset = isl_union_map_range(umap);
2701 sink = isl_union_set_union(sink, uset);
2704 for (i = graph->n_edge - 1; i >= 0; --i) {
2705 if (update_edge(graph, &graph->edge[i]) < 0)
2706 goto error;
2709 if (any)
2710 return unconditionalize_adjacent_validity(graph, source, sink);
2712 isl_union_set_free(source);
2713 isl_union_set_free(sink);
2714 return 0;
2715 error:
2716 isl_union_set_free(source);
2717 isl_union_set_free(sink);
2718 return -1;
2721 static void next_band(struct isl_sched_graph *graph)
2723 graph->band_start = graph->n_total_row;
2726 /* Return the union of the universe domains of the nodes in "graph"
2727 * that satisfy "pred".
2729 static __isl_give isl_union_set *isl_sched_graph_domain(isl_ctx *ctx,
2730 struct isl_sched_graph *graph,
2731 int (*pred)(struct isl_sched_node *node, int data), int data)
2733 int i;
2734 isl_set *set;
2735 isl_union_set *dom;
2737 for (i = 0; i < graph->n; ++i)
2738 if (pred(&graph->node[i], data))
2739 break;
2741 if (i >= graph->n)
2742 isl_die(ctx, isl_error_internal,
2743 "empty component", return NULL);
2745 set = isl_set_universe(isl_space_copy(graph->node[i].space));
2746 dom = isl_union_set_from_set(set);
2748 for (i = i + 1; i < graph->n; ++i) {
2749 if (!pred(&graph->node[i], data))
2750 continue;
2751 set = isl_set_universe(isl_space_copy(graph->node[i].space));
2752 dom = isl_union_set_union(dom, isl_union_set_from_set(set));
2755 return dom;
2758 /* Return a list of unions of universe domains, where each element
2759 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
2761 static __isl_give isl_union_set_list *extract_sccs(isl_ctx *ctx,
2762 struct isl_sched_graph *graph)
2764 int i;
2765 isl_union_set_list *filters;
2767 filters = isl_union_set_list_alloc(ctx, graph->scc);
2768 for (i = 0; i < graph->scc; ++i) {
2769 isl_union_set *dom;
2771 dom = isl_sched_graph_domain(ctx, graph, &node_scc_exactly, i);
2772 filters = isl_union_set_list_add(filters, dom);
2775 return filters;
2778 /* Return a list of two unions of universe domains, one for the SCCs up
2779 * to and including graph->src_scc and another for the other SCCS.
2781 static __isl_give isl_union_set_list *extract_split(isl_ctx *ctx,
2782 struct isl_sched_graph *graph)
2784 isl_union_set *dom;
2785 isl_union_set_list *filters;
2787 filters = isl_union_set_list_alloc(ctx, 2);
2788 dom = isl_sched_graph_domain(ctx, graph,
2789 &node_scc_at_most, graph->src_scc);
2790 filters = isl_union_set_list_add(filters, dom);
2791 dom = isl_sched_graph_domain(ctx, graph,
2792 &node_scc_at_least, graph->src_scc + 1);
2793 filters = isl_union_set_list_add(filters, dom);
2795 return filters;
2798 /* Copy nodes that satisfy node_pred from the src dependence graph
2799 * to the dst dependence graph.
2801 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
2802 int (*node_pred)(struct isl_sched_node *node, int data), int data)
2804 int i;
2806 dst->n = 0;
2807 for (i = 0; i < src->n; ++i) {
2808 int j;
2810 if (!node_pred(&src->node[i], data))
2811 continue;
2813 j = dst->n;
2814 dst->node[j].space = isl_space_copy(src->node[i].space);
2815 dst->node[j].compressed = src->node[i].compressed;
2816 dst->node[j].hull = isl_set_copy(src->node[i].hull);
2817 dst->node[j].compress =
2818 isl_multi_aff_copy(src->node[i].compress);
2819 dst->node[j].decompress =
2820 isl_multi_aff_copy(src->node[i].decompress);
2821 dst->node[j].nvar = src->node[i].nvar;
2822 dst->node[j].nparam = src->node[i].nparam;
2823 dst->node[j].sched = isl_mat_copy(src->node[i].sched);
2824 dst->node[j].sched_map = isl_map_copy(src->node[i].sched_map);
2825 dst->node[j].coincident = src->node[i].coincident;
2826 dst->n++;
2828 if (!dst->node[j].space || !dst->node[j].sched)
2829 return -1;
2830 if (dst->node[j].compressed &&
2831 (!dst->node[j].hull || !dst->node[j].compress ||
2832 !dst->node[j].decompress))
2833 return -1;
2836 return 0;
2839 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
2840 * to the dst dependence graph.
2841 * If the source or destination node of the edge is not in the destination
2842 * graph, then it must be a backward proximity edge and it should simply
2843 * be ignored.
2845 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
2846 struct isl_sched_graph *src,
2847 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
2849 int i;
2850 enum isl_edge_type t;
2852 dst->n_edge = 0;
2853 for (i = 0; i < src->n_edge; ++i) {
2854 struct isl_sched_edge *edge = &src->edge[i];
2855 isl_map *map;
2856 isl_union_map *tagged_condition;
2857 isl_union_map *tagged_validity;
2858 struct isl_sched_node *dst_src, *dst_dst;
2860 if (!edge_pred(edge, data))
2861 continue;
2863 if (isl_map_plain_is_empty(edge->map))
2864 continue;
2866 dst_src = graph_find_node(ctx, dst, edge->src->space);
2867 dst_dst = graph_find_node(ctx, dst, edge->dst->space);
2868 if (!dst_src || !dst_dst) {
2869 if (edge->validity || edge->conditional_validity)
2870 isl_die(ctx, isl_error_internal,
2871 "backward (conditional) validity edge",
2872 return -1);
2873 continue;
2876 map = isl_map_copy(edge->map);
2877 tagged_condition = isl_union_map_copy(edge->tagged_condition);
2878 tagged_validity = isl_union_map_copy(edge->tagged_validity);
2880 dst->edge[dst->n_edge].src = dst_src;
2881 dst->edge[dst->n_edge].dst = dst_dst;
2882 dst->edge[dst->n_edge].map = map;
2883 dst->edge[dst->n_edge].tagged_condition = tagged_condition;
2884 dst->edge[dst->n_edge].tagged_validity = tagged_validity;
2885 dst->edge[dst->n_edge].validity = edge->validity;
2886 dst->edge[dst->n_edge].proximity = edge->proximity;
2887 dst->edge[dst->n_edge].coincidence = edge->coincidence;
2888 dst->edge[dst->n_edge].condition = edge->condition;
2889 dst->edge[dst->n_edge].conditional_validity =
2890 edge->conditional_validity;
2891 dst->n_edge++;
2893 if (edge->tagged_condition && !tagged_condition)
2894 return -1;
2895 if (edge->tagged_validity && !tagged_validity)
2896 return -1;
2898 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
2899 if (edge !=
2900 graph_find_edge(src, t, edge->src, edge->dst))
2901 continue;
2902 if (graph_edge_table_add(ctx, dst, t,
2903 &dst->edge[dst->n_edge - 1]) < 0)
2904 return -1;
2908 return 0;
2911 /* Compute the maximal number of variables over all nodes.
2912 * This is the maximal number of linearly independent schedule
2913 * rows that we need to compute.
2914 * Just in case we end up in a part of the dependence graph
2915 * with only lower-dimensional domains, we make sure we will
2916 * compute the required amount of extra linearly independent rows.
2918 static int compute_maxvar(struct isl_sched_graph *graph)
2920 int i;
2922 graph->maxvar = 0;
2923 for (i = 0; i < graph->n; ++i) {
2924 struct isl_sched_node *node = &graph->node[i];
2925 int nvar;
2927 if (node_update_cmap(node) < 0)
2928 return -1;
2929 nvar = node->nvar + graph->n_row - node->rank;
2930 if (nvar > graph->maxvar)
2931 graph->maxvar = nvar;
2934 return 0;
2937 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
2938 struct isl_sched_graph *graph);
2939 static __isl_give isl_schedule_node *compute_schedule_wcc(
2940 isl_schedule_node *node, struct isl_sched_graph *graph);
2942 /* Compute a schedule for a subgraph of "graph". In particular, for
2943 * the graph composed of nodes that satisfy node_pred and edges that
2944 * that satisfy edge_pred. The caller should precompute the number
2945 * of nodes and edges that satisfy these predicates and pass them along
2946 * as "n" and "n_edge".
2947 * If the subgraph is known to consist of a single component, then wcc should
2948 * be set and then we call compute_schedule_wcc on the constructed subgraph.
2949 * Otherwise, we call compute_schedule, which will check whether the subgraph
2950 * is connected.
2952 * The schedule is inserted at "node" and the updated schedule node
2953 * is returned.
2955 static __isl_give isl_schedule_node *compute_sub_schedule(
2956 __isl_take isl_schedule_node *node, isl_ctx *ctx,
2957 struct isl_sched_graph *graph, int n, int n_edge,
2958 int (*node_pred)(struct isl_sched_node *node, int data),
2959 int (*edge_pred)(struct isl_sched_edge *edge, int data),
2960 int data, int wcc)
2962 struct isl_sched_graph split = { 0 };
2963 int t;
2965 if (graph_alloc(ctx, &split, n, n_edge) < 0)
2966 goto error;
2967 if (copy_nodes(&split, graph, node_pred, data) < 0)
2968 goto error;
2969 if (graph_init_table(ctx, &split) < 0)
2970 goto error;
2971 for (t = 0; t <= isl_edge_last; ++t)
2972 split.max_edge[t] = graph->max_edge[t];
2973 if (graph_init_edge_tables(ctx, &split) < 0)
2974 goto error;
2975 if (copy_edges(ctx, &split, graph, edge_pred, data) < 0)
2976 goto error;
2977 split.n_row = graph->n_row;
2978 split.max_row = graph->max_row;
2979 split.n_total_row = graph->n_total_row;
2980 split.band_start = graph->band_start;
2982 if (wcc)
2983 node = compute_schedule_wcc(node, &split);
2984 else
2985 node = compute_schedule(node, &split);
2987 graph_free(ctx, &split);
2988 return node;
2989 error:
2990 graph_free(ctx, &split);
2991 return isl_schedule_node_free(node);
2994 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
2996 return edge->src->scc == scc && edge->dst->scc == scc;
2999 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
3001 return edge->dst->scc <= scc;
3004 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
3006 return edge->src->scc >= scc;
3009 /* Reset the current band by dropping all its schedule rows.
3011 static int reset_band(struct isl_sched_graph *graph)
3013 int i;
3014 int drop;
3016 drop = graph->n_total_row - graph->band_start;
3017 graph->n_total_row -= drop;
3018 graph->n_row -= drop;
3020 for (i = 0; i < graph->n; ++i) {
3021 struct isl_sched_node *node = &graph->node[i];
3023 isl_map_free(node->sched_map);
3024 node->sched_map = NULL;
3026 node->sched = isl_mat_drop_rows(node->sched,
3027 graph->band_start, drop);
3029 if (!node->sched)
3030 return -1;
3033 return 0;
3036 /* Split the current graph into two parts and compute a schedule for each
3037 * part individually. In particular, one part consists of all SCCs up
3038 * to and including graph->src_scc, while the other part contains the other
3039 * SCCS. The split is enforced by a sequence node inserted at position "node"
3040 * in the schedule tree. Return the updated schedule node.
3042 * The current band is reset. It would be possible to reuse
3043 * the previously computed rows as the first rows in the next
3044 * band, but recomputing them may result in better rows as we are looking
3045 * at a smaller part of the dependence graph.
3047 static __isl_give isl_schedule_node *compute_split_schedule(
3048 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3050 int i, n, e1, e2;
3051 isl_ctx *ctx;
3052 isl_union_set_list *filters;
3054 if (!node)
3055 return NULL;
3057 if (reset_band(graph) < 0)
3058 return isl_schedule_node_free(node);
3060 n = 0;
3061 for (i = 0; i < graph->n; ++i) {
3062 struct isl_sched_node *node = &graph->node[i];
3063 int before = node->scc <= graph->src_scc;
3065 if (before)
3066 n++;
3069 e1 = e2 = 0;
3070 for (i = 0; i < graph->n_edge; ++i) {
3071 if (graph->edge[i].dst->scc <= graph->src_scc)
3072 e1++;
3073 if (graph->edge[i].src->scc > graph->src_scc)
3074 e2++;
3077 next_band(graph);
3079 ctx = isl_schedule_node_get_ctx(node);
3080 filters = extract_split(ctx, graph);
3081 node = isl_schedule_node_insert_sequence(node, filters);
3082 node = isl_schedule_node_child(node, 0);
3083 node = isl_schedule_node_child(node, 0);
3085 node = compute_sub_schedule(node, ctx, graph, n, e1,
3086 &node_scc_at_most, &edge_dst_scc_at_most,
3087 graph->src_scc, 0);
3088 node = isl_schedule_node_parent(node);
3089 node = isl_schedule_node_next_sibling(node);
3090 node = isl_schedule_node_child(node, 0);
3091 node = compute_sub_schedule(node, ctx, graph, graph->n - n, e2,
3092 &node_scc_at_least, &edge_src_scc_at_least,
3093 graph->src_scc + 1, 0);
3094 node = isl_schedule_node_parent(node);
3095 node = isl_schedule_node_parent(node);
3097 return node;
3100 /* Insert a band node at position "node" in the schedule tree corresponding
3101 * to the current band in "graph". Mark the band node permutable
3102 * if "permutable" is set.
3103 * The partial schedules and the coincidence property are extracted
3104 * from the graph nodes.
3105 * Return the updated schedule node.
3107 static __isl_give isl_schedule_node *insert_current_band(
3108 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3109 int permutable)
3111 int i;
3112 int start, end, n;
3113 isl_multi_aff *ma;
3114 isl_multi_pw_aff *mpa;
3115 isl_multi_union_pw_aff *mupa;
3117 if (!node)
3118 return NULL;
3120 if (graph->n < 1)
3121 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
3122 "graph should have at least one node",
3123 return isl_schedule_node_free(node));
3125 start = graph->band_start;
3126 end = graph->n_total_row;
3127 n = end - start;
3129 ma = node_extract_partial_schedule_multi_aff(&graph->node[0], start, n);
3130 mpa = isl_multi_pw_aff_from_multi_aff(ma);
3131 mupa = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
3133 for (i = 1; i < graph->n; ++i) {
3134 isl_multi_union_pw_aff *mupa_i;
3136 ma = node_extract_partial_schedule_multi_aff(&graph->node[i],
3137 start, n);
3138 mpa = isl_multi_pw_aff_from_multi_aff(ma);
3139 mupa_i = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
3140 mupa = isl_multi_union_pw_aff_union_add(mupa, mupa_i);
3142 node = isl_schedule_node_insert_partial_schedule(node, mupa);
3144 for (i = 0; i < n; ++i)
3145 node = isl_schedule_node_band_member_set_coincident(node, i,
3146 graph->node[0].coincident[start + i]);
3147 node = isl_schedule_node_band_set_permutable(node, permutable);
3149 return node;
3152 /* Update the dependence relations based on the current schedule,
3153 * add the current band to "node" and the continue with the computation
3154 * of the next band.
3155 * Return the updated schedule node.
3157 static __isl_give isl_schedule_node *compute_next_band(
3158 __isl_take isl_schedule_node *node,
3159 struct isl_sched_graph *graph, int permutable)
3161 isl_ctx *ctx;
3163 if (!node)
3164 return NULL;
3166 ctx = isl_schedule_node_get_ctx(node);
3167 if (update_edges(ctx, graph) < 0)
3168 return isl_schedule_node_free(node);
3169 node = insert_current_band(node, graph, permutable);
3170 next_band(graph);
3172 node = isl_schedule_node_child(node, 0);
3173 node = compute_schedule(node, graph);
3174 node = isl_schedule_node_parent(node);
3176 return node;
3179 /* Add constraints to graph->lp that force the dependence "map" (which
3180 * is part of the dependence relation of "edge")
3181 * to be respected and attempt to carry it, where the edge is one from
3182 * a node j to itself. "pos" is the sequence number of the given map.
3183 * That is, add constraints that enforce
3185 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3186 * = c_j_x (y - x) >= e_i
3188 * for each (x,y) in R.
3189 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3190 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3191 * with each coefficient in c_j_x represented as a pair of non-negative
3192 * coefficients.
3194 static int add_intra_constraints(struct isl_sched_graph *graph,
3195 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3197 unsigned total;
3198 isl_ctx *ctx = isl_map_get_ctx(map);
3199 isl_space *dim;
3200 isl_dim_map *dim_map;
3201 isl_basic_set *coef;
3202 struct isl_sched_node *node = edge->src;
3204 coef = intra_coefficients(graph, node, map);
3205 if (!coef)
3206 return -1;
3208 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3210 total = isl_basic_set_total_dim(graph->lp);
3211 dim_map = isl_dim_map_alloc(ctx, total);
3212 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3213 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
3214 isl_space_dim(dim, isl_dim_set), 1,
3215 node->nvar, -1);
3216 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
3217 isl_space_dim(dim, isl_dim_set), 1,
3218 node->nvar, 1);
3219 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3220 coef->n_eq, coef->n_ineq);
3221 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3222 coef, dim_map);
3223 isl_space_free(dim);
3225 return 0;
3228 /* Add constraints to graph->lp that force the dependence "map" (which
3229 * is part of the dependence relation of "edge")
3230 * to be respected and attempt to carry it, where the edge is one from
3231 * node j to node k. "pos" is the sequence number of the given map.
3232 * That is, add constraints that enforce
3234 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3236 * for each (x,y) in R.
3237 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3238 * of valid constraints for R and then plug in
3239 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3240 * with each coefficient (except e_i, c_k_0 and c_j_0)
3241 * represented as a pair of non-negative coefficients.
3243 static int add_inter_constraints(struct isl_sched_graph *graph,
3244 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3246 unsigned total;
3247 isl_ctx *ctx = isl_map_get_ctx(map);
3248 isl_space *dim;
3249 isl_dim_map *dim_map;
3250 isl_basic_set *coef;
3251 struct isl_sched_node *src = edge->src;
3252 struct isl_sched_node *dst = edge->dst;
3254 coef = inter_coefficients(graph, edge, map);
3255 if (!coef)
3256 return -1;
3258 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3260 total = isl_basic_set_total_dim(graph->lp);
3261 dim_map = isl_dim_map_alloc(ctx, total);
3263 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3265 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
3266 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
3267 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
3268 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
3269 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3270 dst->nvar, -1);
3271 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
3272 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3273 dst->nvar, 1);
3275 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
3276 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
3277 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
3278 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
3279 isl_space_dim(dim, isl_dim_set), 1,
3280 src->nvar, 1);
3281 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
3282 isl_space_dim(dim, isl_dim_set), 1,
3283 src->nvar, -1);
3285 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3286 coef->n_eq, coef->n_ineq);
3287 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3288 coef, dim_map);
3289 isl_space_free(dim);
3291 return 0;
3294 /* Add constraints to graph->lp that force all (conditional) validity
3295 * dependences to be respected and attempt to carry them.
3297 static int add_all_constraints(struct isl_sched_graph *graph)
3299 int i, j;
3300 int pos;
3302 pos = 0;
3303 for (i = 0; i < graph->n_edge; ++i) {
3304 struct isl_sched_edge *edge= &graph->edge[i];
3306 if (!edge->validity && !edge->conditional_validity)
3307 continue;
3309 for (j = 0; j < edge->map->n; ++j) {
3310 isl_basic_map *bmap;
3311 isl_map *map;
3313 bmap = isl_basic_map_copy(edge->map->p[j]);
3314 map = isl_map_from_basic_map(bmap);
3316 if (edge->src == edge->dst &&
3317 add_intra_constraints(graph, edge, map, pos) < 0)
3318 return -1;
3319 if (edge->src != edge->dst &&
3320 add_inter_constraints(graph, edge, map, pos) < 0)
3321 return -1;
3322 ++pos;
3326 return 0;
3329 /* Count the number of equality and inequality constraints
3330 * that will be added to the carry_lp problem.
3331 * We count each edge exactly once.
3333 static int count_all_constraints(struct isl_sched_graph *graph,
3334 int *n_eq, int *n_ineq)
3336 int i, j;
3338 *n_eq = *n_ineq = 0;
3339 for (i = 0; i < graph->n_edge; ++i) {
3340 struct isl_sched_edge *edge= &graph->edge[i];
3341 for (j = 0; j < edge->map->n; ++j) {
3342 isl_basic_map *bmap;
3343 isl_map *map;
3345 bmap = isl_basic_map_copy(edge->map->p[j]);
3346 map = isl_map_from_basic_map(bmap);
3348 if (count_map_constraints(graph, edge, map,
3349 n_eq, n_ineq, 1, 0) < 0)
3350 return -1;
3354 return 0;
3357 /* Construct an LP problem for finding schedule coefficients
3358 * such that the schedule carries as many dependences as possible.
3359 * In particular, for each dependence i, we bound the dependence distance
3360 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3361 * of all e_i's. Dependence with e_i = 0 in the solution are simply
3362 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3363 * Note that if the dependence relation is a union of basic maps,
3364 * then we have to consider each basic map individually as it may only
3365 * be possible to carry the dependences expressed by some of those
3366 * basic maps and not all off them.
3367 * Below, we consider each of those basic maps as a separate "edge".
3369 * All variables of the LP are non-negative. The actual coefficients
3370 * may be negative, so each coefficient is represented as the difference
3371 * of two non-negative variables. The negative part always appears
3372 * immediately before the positive part.
3373 * Other than that, the variables have the following order
3375 * - sum of (1 - e_i) over all edges
3376 * - sum of positive and negative parts of all c_n coefficients
3377 * (unconstrained when computing non-parametric schedules)
3378 * - sum of positive and negative parts of all c_x coefficients
3379 * - for each edge
3380 * - e_i
3381 * - for each node
3382 * - c_i_0
3383 * - positive and negative parts of c_i_n (if parametric)
3384 * - positive and negative parts of c_i_x
3386 * The constraints are those from the (validity) edges plus three equalities
3387 * to express the sums and n_edge inequalities to express e_i <= 1.
3389 static int setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
3391 int i, j;
3392 int k;
3393 isl_space *dim;
3394 unsigned total;
3395 int n_eq, n_ineq;
3396 int n_edge;
3398 n_edge = 0;
3399 for (i = 0; i < graph->n_edge; ++i)
3400 n_edge += graph->edge[i].map->n;
3402 total = 3 + n_edge;
3403 for (i = 0; i < graph->n; ++i) {
3404 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
3405 node->start = total;
3406 total += 1 + 2 * (node->nparam + node->nvar);
3409 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
3410 return -1;
3411 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
3412 return -1;
3414 dim = isl_space_set_alloc(ctx, 0, total);
3415 isl_basic_set_free(graph->lp);
3416 n_eq += 3;
3417 n_ineq += n_edge;
3418 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
3419 graph->lp = isl_basic_set_set_rational(graph->lp);
3421 k = isl_basic_set_alloc_equality(graph->lp);
3422 if (k < 0)
3423 return -1;
3424 isl_seq_clr(graph->lp->eq[k], 1 + total);
3425 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
3426 isl_int_set_si(graph->lp->eq[k][1], 1);
3427 for (i = 0; i < n_edge; ++i)
3428 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
3430 k = isl_basic_set_alloc_equality(graph->lp);
3431 if (k < 0)
3432 return -1;
3433 isl_seq_clr(graph->lp->eq[k], 1 + total);
3434 isl_int_set_si(graph->lp->eq[k][2], -1);
3435 for (i = 0; i < graph->n; ++i) {
3436 int pos = 1 + graph->node[i].start + 1;
3438 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
3439 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3442 k = isl_basic_set_alloc_equality(graph->lp);
3443 if (k < 0)
3444 return -1;
3445 isl_seq_clr(graph->lp->eq[k], 1 + total);
3446 isl_int_set_si(graph->lp->eq[k][3], -1);
3447 for (i = 0; i < graph->n; ++i) {
3448 struct isl_sched_node *node = &graph->node[i];
3449 int pos = 1 + node->start + 1 + 2 * node->nparam;
3451 for (j = 0; j < 2 * node->nvar; ++j)
3452 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3455 for (i = 0; i < n_edge; ++i) {
3456 k = isl_basic_set_alloc_inequality(graph->lp);
3457 if (k < 0)
3458 return -1;
3459 isl_seq_clr(graph->lp->ineq[k], 1 + total);
3460 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
3461 isl_int_set_si(graph->lp->ineq[k][0], 1);
3464 if (add_bound_coefficient_constraints(ctx, graph) < 0)
3465 return -1;
3466 if (add_all_constraints(graph) < 0)
3467 return -1;
3469 return 0;
3472 static __isl_give isl_schedule_node *compute_component_schedule(
3473 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3474 int wcc);
3476 /* Comparison function for sorting the statements based on
3477 * the corresponding value in "r".
3479 static int smaller_value(const void *a, const void *b, void *data)
3481 isl_vec *r = data;
3482 const int *i1 = a;
3483 const int *i2 = b;
3485 return isl_int_cmp(r->el[*i1], r->el[*i2]);
3488 /* If the schedule_split_scaled option is set and if the linear
3489 * parts of the scheduling rows for all nodes in the graphs have
3490 * a non-trivial common divisor, then split off the remainder of the
3491 * constant term modulo this common divisor from the linear part.
3492 * Otherwise, insert a band node directly and continue with
3493 * the construction of the schedule.
3495 * If a non-trivial common divisor is found, then
3496 * the linear part is reduced and the remainder is enforced
3497 * by a sequence node with the children placed in the order
3498 * of this remainder.
3499 * In particular, we assign an scc index based on the remainder and
3500 * then rely on compute_component_schedule to insert the sequence and
3501 * to continue the schedule construction on each part.
3503 static __isl_give isl_schedule_node *split_scaled(
3504 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3506 int i;
3507 int row;
3508 int scc;
3509 isl_ctx *ctx;
3510 isl_int gcd, gcd_i;
3511 isl_vec *r;
3512 int *order;
3514 if (!node)
3515 return NULL;
3517 ctx = isl_schedule_node_get_ctx(node);
3518 if (!ctx->opt->schedule_split_scaled)
3519 return compute_next_band(node, graph, 0);
3520 if (graph->n <= 1)
3521 return compute_next_band(node, graph, 0);
3523 isl_int_init(gcd);
3524 isl_int_init(gcd_i);
3526 isl_int_set_si(gcd, 0);
3528 row = isl_mat_rows(graph->node[0].sched) - 1;
3530 for (i = 0; i < graph->n; ++i) {
3531 struct isl_sched_node *node = &graph->node[i];
3532 int cols = isl_mat_cols(node->sched);
3534 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
3535 isl_int_gcd(gcd, gcd, gcd_i);
3538 isl_int_clear(gcd_i);
3540 if (isl_int_cmp_si(gcd, 1) <= 0) {
3541 isl_int_clear(gcd);
3542 return compute_next_band(node, graph, 0);
3545 r = isl_vec_alloc(ctx, graph->n);
3546 order = isl_calloc_array(ctx, int, graph->n);
3547 if (!r || !order)
3548 goto error;
3550 for (i = 0; i < graph->n; ++i) {
3551 struct isl_sched_node *node = &graph->node[i];
3553 order[i] = i;
3554 isl_int_fdiv_r(r->el[i], node->sched->row[row][0], gcd);
3555 isl_int_fdiv_q(node->sched->row[row][0],
3556 node->sched->row[row][0], gcd);
3557 isl_int_mul(node->sched->row[row][0],
3558 node->sched->row[row][0], gcd);
3559 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
3560 if (!node->sched)
3561 goto error;
3564 if (isl_sort(order, graph->n, sizeof(order[0]), &smaller_value, r) < 0)
3565 goto error;
3567 scc = 0;
3568 for (i = 0; i < graph->n; ++i) {
3569 if (i > 0 && isl_int_ne(r->el[order[i - 1]], r->el[order[i]]))
3570 ++scc;
3571 graph->node[order[i]].scc = scc;
3573 graph->scc = ++scc;
3574 graph->weak = 0;
3576 isl_int_clear(gcd);
3577 isl_vec_free(r);
3578 free(order);
3580 if (update_edges(ctx, graph) < 0)
3581 return isl_schedule_node_free(node);
3582 node = insert_current_band(node, graph, 0);
3583 next_band(graph);
3585 node = isl_schedule_node_child(node, 0);
3586 node = compute_component_schedule(node, graph, 0);
3587 node = isl_schedule_node_parent(node);
3589 return node;
3590 error:
3591 isl_vec_free(r);
3592 free(order);
3593 isl_int_clear(gcd);
3594 return isl_schedule_node_free(node);
3597 /* Is the schedule row "sol" trivial on node "node"?
3598 * That is, is the solution zero on the dimensions orthogonal to
3599 * the previously found solutions?
3600 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3602 * Each coefficient is represented as the difference between
3603 * two non-negative values in "sol". "sol" has been computed
3604 * in terms of the original iterators (i.e., without use of cmap).
3605 * We construct the schedule row s and write it as a linear
3606 * combination of (linear combinations of) previously computed schedule rows.
3607 * s = Q c or c = U s.
3608 * If the final entries of c are all zero, then the solution is trivial.
3610 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
3612 int i;
3613 int pos;
3614 int trivial;
3615 isl_ctx *ctx;
3616 isl_vec *node_sol;
3618 if (!sol)
3619 return -1;
3620 if (node->nvar == node->rank)
3621 return 0;
3623 ctx = isl_vec_get_ctx(sol);
3624 node_sol = isl_vec_alloc(ctx, node->nvar);
3625 if (!node_sol)
3626 return -1;
3628 pos = 1 + node->start + 1 + 2 * node->nparam;
3630 for (i = 0; i < node->nvar; ++i)
3631 isl_int_sub(node_sol->el[i],
3632 sol->el[pos + 2 * i + 1], sol->el[pos + 2 * i]);
3634 node_sol = isl_mat_vec_product(isl_mat_copy(node->cinv), node_sol);
3636 if (!node_sol)
3637 return -1;
3639 trivial = isl_seq_first_non_zero(node_sol->el + node->rank,
3640 node->nvar - node->rank) == -1;
3642 isl_vec_free(node_sol);
3644 return trivial;
3647 /* Is the schedule row "sol" trivial on any node where it should
3648 * not be trivial?
3649 * "sol" has been computed in terms of the original iterators
3650 * (i.e., without use of cmap).
3651 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3653 static int is_any_trivial(struct isl_sched_graph *graph,
3654 __isl_keep isl_vec *sol)
3656 int i;
3658 for (i = 0; i < graph->n; ++i) {
3659 struct isl_sched_node *node = &graph->node[i];
3660 int trivial;
3662 if (!needs_row(graph, node))
3663 continue;
3664 trivial = is_trivial(node, sol);
3665 if (trivial < 0 || trivial)
3666 return trivial;
3669 return 0;
3672 /* Construct a schedule row for each node such that as many dependences
3673 * as possible are carried and then continue with the next band.
3675 * If the computed schedule row turns out to be trivial on one or
3676 * more nodes where it should not be trivial, then we throw it away
3677 * and try again on each component separately.
3679 * If there is only one component, then we accept the schedule row anyway,
3680 * but we do not consider it as a complete row and therefore do not
3681 * increment graph->n_row. Note that the ranks of the nodes that
3682 * do get a non-trivial schedule part will get updated regardless and
3683 * graph->maxvar is computed based on these ranks. The test for
3684 * whether more schedule rows are required in compute_schedule_wcc
3685 * is therefore not affected.
3687 * Insert a band corresponding to the schedule row at position "node"
3688 * of the schedule tree and continue with the construction of the schedule.
3689 * This insertion and the continued construction is performed by split_scaled
3690 * after optionally checking for non-trivial common divisors.
3692 static __isl_give isl_schedule_node *carry_dependences(
3693 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3695 int i;
3696 int n_edge;
3697 int trivial;
3698 isl_ctx *ctx;
3699 isl_vec *sol;
3700 isl_basic_set *lp;
3702 if (!node)
3703 return NULL;
3705 n_edge = 0;
3706 for (i = 0; i < graph->n_edge; ++i)
3707 n_edge += graph->edge[i].map->n;
3709 ctx = isl_schedule_node_get_ctx(node);
3710 if (setup_carry_lp(ctx, graph) < 0)
3711 return isl_schedule_node_free(node);
3713 lp = isl_basic_set_copy(graph->lp);
3714 sol = isl_tab_basic_set_non_neg_lexmin(lp);
3715 if (!sol)
3716 return isl_schedule_node_free(node);
3718 if (sol->size == 0) {
3719 isl_vec_free(sol);
3720 isl_die(ctx, isl_error_internal,
3721 "error in schedule construction",
3722 return isl_schedule_node_free(node));
3725 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
3726 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
3727 isl_vec_free(sol);
3728 isl_die(ctx, isl_error_unknown,
3729 "unable to carry dependences",
3730 return isl_schedule_node_free(node));
3733 trivial = is_any_trivial(graph, sol);
3734 if (trivial < 0) {
3735 sol = isl_vec_free(sol);
3736 } else if (trivial && graph->scc > 1) {
3737 isl_vec_free(sol);
3738 return compute_component_schedule(node, graph, 1);
3741 if (update_schedule(graph, sol, 0, 0) < 0)
3742 return isl_schedule_node_free(node);
3743 if (trivial)
3744 graph->n_row--;
3746 return split_scaled(node, graph);
3749 /* Topologically sort statements mapped to the same schedule iteration
3750 * and add insert a sequence node in front of "node"
3751 * corresponding to this order.
3753 * If it turns out to be impossible to sort the statements apart,
3754 * because different dependences impose different orderings
3755 * on the statements, then we extend the schedule such that
3756 * it carries at least one more dependence.
3758 static __isl_give isl_schedule_node *sort_statements(
3759 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3761 isl_ctx *ctx;
3762 isl_union_set_list *filters;
3764 if (!node)
3765 return NULL;
3767 ctx = isl_schedule_node_get_ctx(node);
3768 if (graph->n < 1)
3769 isl_die(ctx, isl_error_internal,
3770 "graph should have at least one node",
3771 return isl_schedule_node_free(node));
3773 if (graph->n == 1)
3774 return node;
3776 if (update_edges(ctx, graph) < 0)
3777 return isl_schedule_node_free(node);
3779 if (graph->n_edge == 0)
3780 return node;
3782 if (detect_sccs(ctx, graph) < 0)
3783 return isl_schedule_node_free(node);
3785 next_band(graph);
3786 if (graph->scc < graph->n)
3787 return carry_dependences(node, graph);
3789 filters = extract_sccs(ctx, graph);
3790 node = isl_schedule_node_insert_sequence(node, filters);
3792 return node;
3795 /* Are there any (non-empty) (conditional) validity edges in the graph?
3797 static int has_validity_edges(struct isl_sched_graph *graph)
3799 int i;
3801 for (i = 0; i < graph->n_edge; ++i) {
3802 int empty;
3804 empty = isl_map_plain_is_empty(graph->edge[i].map);
3805 if (empty < 0)
3806 return -1;
3807 if (empty)
3808 continue;
3809 if (graph->edge[i].validity ||
3810 graph->edge[i].conditional_validity)
3811 return 1;
3814 return 0;
3817 /* Should we apply a Feautrier step?
3818 * That is, did the user request the Feautrier algorithm and are
3819 * there any validity dependences (left)?
3821 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
3823 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
3824 return 0;
3826 return has_validity_edges(graph);
3829 /* Compute a schedule for a connected dependence graph using Feautrier's
3830 * multi-dimensional scheduling algorithm and return the updated schedule node.
3832 * The original algorithm is described in [1].
3833 * The main idea is to minimize the number of scheduling dimensions, by
3834 * trying to satisfy as many dependences as possible per scheduling dimension.
3836 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
3837 * Problem, Part II: Multi-Dimensional Time.
3838 * In Intl. Journal of Parallel Programming, 1992.
3840 static __isl_give isl_schedule_node *compute_schedule_wcc_feautrier(
3841 isl_schedule_node *node, struct isl_sched_graph *graph)
3843 return carry_dependences(node, graph);
3846 /* Turn off the "local" bit on all (condition) edges.
3848 static void clear_local_edges(struct isl_sched_graph *graph)
3850 int i;
3852 for (i = 0; i < graph->n_edge; ++i)
3853 if (graph->edge[i].condition)
3854 graph->edge[i].local = 0;
3857 /* Does "graph" have both condition and conditional validity edges?
3859 static int need_condition_check(struct isl_sched_graph *graph)
3861 int i;
3862 int any_condition = 0;
3863 int any_conditional_validity = 0;
3865 for (i = 0; i < graph->n_edge; ++i) {
3866 if (graph->edge[i].condition)
3867 any_condition = 1;
3868 if (graph->edge[i].conditional_validity)
3869 any_conditional_validity = 1;
3872 return any_condition && any_conditional_validity;
3875 /* Does "graph" contain any coincidence edge?
3877 static int has_any_coincidence(struct isl_sched_graph *graph)
3879 int i;
3881 for (i = 0; i < graph->n_edge; ++i)
3882 if (graph->edge[i].coincidence)
3883 return 1;
3885 return 0;
3888 /* Extract the final schedule row as a map with the iteration domain
3889 * of "node" as domain.
3891 static __isl_give isl_map *final_row(struct isl_sched_node *node)
3893 isl_local_space *ls;
3894 isl_aff *aff;
3895 int row;
3897 row = isl_mat_rows(node->sched) - 1;
3898 ls = isl_local_space_from_space(isl_space_copy(node->space));
3899 aff = extract_schedule_row(ls, node, row);
3900 return isl_map_from_aff(aff);
3903 /* Is the conditional validity dependence in the edge with index "edge_index"
3904 * violated by the latest (i.e., final) row of the schedule?
3905 * That is, is i scheduled after j
3906 * for any conditional validity dependence i -> j?
3908 static int is_violated(struct isl_sched_graph *graph, int edge_index)
3910 isl_map *src_sched, *dst_sched, *map;
3911 struct isl_sched_edge *edge = &graph->edge[edge_index];
3912 int empty;
3914 src_sched = final_row(edge->src);
3915 dst_sched = final_row(edge->dst);
3916 map = isl_map_copy(edge->map);
3917 map = isl_map_apply_domain(map, src_sched);
3918 map = isl_map_apply_range(map, dst_sched);
3919 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3920 empty = isl_map_is_empty(map);
3921 isl_map_free(map);
3923 if (empty < 0)
3924 return -1;
3926 return !empty;
3929 /* Does "graph" have any satisfied condition edges that
3930 * are adjacent to the conditional validity constraint with
3931 * domain "conditional_source" and range "conditional_sink"?
3933 * A satisfied condition is one that is not local.
3934 * If a condition was forced to be local already (i.e., marked as local)
3935 * then there is no need to check if it is in fact local.
3937 * Additionally, mark all adjacent condition edges found as local.
3939 static int has_adjacent_true_conditions(struct isl_sched_graph *graph,
3940 __isl_keep isl_union_set *conditional_source,
3941 __isl_keep isl_union_set *conditional_sink)
3943 int i;
3944 int any = 0;
3946 for (i = 0; i < graph->n_edge; ++i) {
3947 int adjacent, local;
3948 isl_union_map *condition;
3950 if (!graph->edge[i].condition)
3951 continue;
3952 if (graph->edge[i].local)
3953 continue;
3955 condition = graph->edge[i].tagged_condition;
3956 adjacent = domain_intersects(condition, conditional_sink);
3957 if (adjacent >= 0 && !adjacent)
3958 adjacent = range_intersects(condition,
3959 conditional_source);
3960 if (adjacent < 0)
3961 return -1;
3962 if (!adjacent)
3963 continue;
3965 graph->edge[i].local = 1;
3967 local = is_condition_false(&graph->edge[i]);
3968 if (local < 0)
3969 return -1;
3970 if (!local)
3971 any = 1;
3974 return any;
3977 /* Are there any violated conditional validity dependences with
3978 * adjacent condition dependences that are not local with respect
3979 * to the current schedule?
3980 * That is, is the conditional validity constraint violated?
3982 * Additionally, mark all those adjacent condition dependences as local.
3983 * We also mark those adjacent condition dependences that were not marked
3984 * as local before, but just happened to be local already. This ensures
3985 * that they remain local if the schedule is recomputed.
3987 * We first collect domain and range of all violated conditional validity
3988 * dependences and then check if there are any adjacent non-local
3989 * condition dependences.
3991 static int has_violated_conditional_constraint(isl_ctx *ctx,
3992 struct isl_sched_graph *graph)
3994 int i;
3995 int any = 0;
3996 isl_union_set *source, *sink;
3998 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3999 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
4000 for (i = 0; i < graph->n_edge; ++i) {
4001 isl_union_set *uset;
4002 isl_union_map *umap;
4003 int violated;
4005 if (!graph->edge[i].conditional_validity)
4006 continue;
4008 violated = is_violated(graph, i);
4009 if (violated < 0)
4010 goto error;
4011 if (!violated)
4012 continue;
4014 any = 1;
4016 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
4017 uset = isl_union_map_domain(umap);
4018 source = isl_union_set_union(source, uset);
4019 source = isl_union_set_coalesce(source);
4021 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
4022 uset = isl_union_map_range(umap);
4023 sink = isl_union_set_union(sink, uset);
4024 sink = isl_union_set_coalesce(sink);
4027 if (any)
4028 any = has_adjacent_true_conditions(graph, source, sink);
4030 isl_union_set_free(source);
4031 isl_union_set_free(sink);
4032 return any;
4033 error:
4034 isl_union_set_free(source);
4035 isl_union_set_free(sink);
4036 return -1;
4039 /* Compute a schedule for a connected dependence graph and return
4040 * the updated schedule node.
4042 * We try to find a sequence of as many schedule rows as possible that result
4043 * in non-negative dependence distances (independent of the previous rows
4044 * in the sequence, i.e., such that the sequence is tilable), with as
4045 * many of the initial rows as possible satisfying the coincidence constraints.
4046 * If we can't find any more rows we either
4047 * - split between SCCs and start over (assuming we found an interesting
4048 * pair of SCCs between which to split)
4049 * - continue with the next band (assuming the current band has at least
4050 * one row)
4051 * - try to carry as many dependences as possible and continue with the next
4052 * band
4053 * In each case, we first insert a band node in the schedule tree
4054 * if any rows have been computed.
4056 * If Feautrier's algorithm is selected, we first recursively try to satisfy
4057 * as many validity dependences as possible. When all validity dependences
4058 * are satisfied we extend the schedule to a full-dimensional schedule.
4060 * If we manage to complete the schedule, we insert a band node
4061 * (if any schedule rows were computed) and we finish off by topologically
4062 * sorting the statements based on the remaining dependences.
4064 * If ctx->opt->schedule_outer_coincidence is set, then we force the
4065 * outermost dimension to satisfy the coincidence constraints. If this
4066 * turns out to be impossible, we fall back on the general scheme above
4067 * and try to carry as many dependences as possible.
4069 * If "graph" contains both condition and conditional validity dependences,
4070 * then we need to check that that the conditional schedule constraint
4071 * is satisfied, i.e., there are no violated conditional validity dependences
4072 * that are adjacent to any non-local condition dependences.
4073 * If there are, then we mark all those adjacent condition dependences
4074 * as local and recompute the current band. Those dependences that
4075 * are marked local will then be forced to be local.
4076 * The initial computation is performed with no dependences marked as local.
4077 * If we are lucky, then there will be no violated conditional validity
4078 * dependences adjacent to any non-local condition dependences.
4079 * Otherwise, we mark some additional condition dependences as local and
4080 * recompute. We continue this process until there are no violations left or
4081 * until we are no longer able to compute a schedule.
4082 * Since there are only a finite number of dependences,
4083 * there will only be a finite number of iterations.
4085 static __isl_give isl_schedule_node *compute_schedule_wcc(
4086 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
4088 int has_coincidence;
4089 int use_coincidence;
4090 int force_coincidence = 0;
4091 int check_conditional;
4092 int insert;
4093 isl_ctx *ctx;
4095 if (!node)
4096 return NULL;
4098 ctx = isl_schedule_node_get_ctx(node);
4099 if (detect_sccs(ctx, graph) < 0)
4100 return isl_schedule_node_free(node);
4101 if (sort_sccs(graph) < 0)
4102 return isl_schedule_node_free(node);
4104 if (compute_maxvar(graph) < 0)
4105 return isl_schedule_node_free(node);
4107 if (need_feautrier_step(ctx, graph))
4108 return compute_schedule_wcc_feautrier(node, graph);
4110 clear_local_edges(graph);
4111 check_conditional = need_condition_check(graph);
4112 has_coincidence = has_any_coincidence(graph);
4114 if (ctx->opt->schedule_outer_coincidence)
4115 force_coincidence = 1;
4117 use_coincidence = has_coincidence;
4118 while (graph->n_row < graph->maxvar) {
4119 isl_vec *sol;
4120 int violated;
4121 int coincident;
4123 graph->src_scc = -1;
4124 graph->dst_scc = -1;
4126 if (setup_lp(ctx, graph, use_coincidence) < 0)
4127 return isl_schedule_node_free(node);
4128 sol = solve_lp(graph);
4129 if (!sol)
4130 return isl_schedule_node_free(node);
4131 if (sol->size == 0) {
4132 int empty = graph->n_total_row == graph->band_start;
4134 isl_vec_free(sol);
4135 if (use_coincidence && (!force_coincidence || !empty)) {
4136 use_coincidence = 0;
4137 continue;
4139 if (!ctx->opt->schedule_maximize_band_depth && !empty)
4140 return compute_next_band(node, graph, 1);
4141 if (graph->src_scc >= 0)
4142 return compute_split_schedule(node, graph);
4143 if (!empty)
4144 return compute_next_band(node, graph, 1);
4145 return carry_dependences(node, graph);
4147 coincident = !has_coincidence || use_coincidence;
4148 if (update_schedule(graph, sol, 1, coincident) < 0)
4149 return isl_schedule_node_free(node);
4151 if (!check_conditional)
4152 continue;
4153 violated = has_violated_conditional_constraint(ctx, graph);
4154 if (violated < 0)
4155 return isl_schedule_node_free(node);
4156 if (!violated)
4157 continue;
4158 if (reset_band(graph) < 0)
4159 return isl_schedule_node_free(node);
4160 use_coincidence = has_coincidence;
4163 insert = graph->n_total_row > graph->band_start;
4164 if (insert) {
4165 node = insert_current_band(node, graph, 1);
4166 node = isl_schedule_node_child(node, 0);
4168 node = sort_statements(node, graph);
4169 if (insert)
4170 node = isl_schedule_node_parent(node);
4172 return node;
4175 /* Compute a schedule for each group of nodes identified by node->scc
4176 * separately and then combine them in a sequence node (or as set node
4177 * if graph->weak is set) inserted at position "node" of the schedule tree.
4178 * Return the updated schedule node.
4180 * If "wcc" is set then each of the groups belongs to a single
4181 * weakly connected component in the dependence graph so that
4182 * there is no need for compute_sub_schedule to look for weakly
4183 * connected components.
4185 static __isl_give isl_schedule_node *compute_component_schedule(
4186 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
4187 int wcc)
4189 int component, i;
4190 int n, n_edge;
4191 isl_ctx *ctx;
4192 isl_union_set_list *filters;
4194 if (!node)
4195 return NULL;
4196 ctx = isl_schedule_node_get_ctx(node);
4198 filters = extract_sccs(ctx, graph);
4199 if (graph->weak)
4200 node = isl_schedule_node_insert_set(node, filters);
4201 else
4202 node = isl_schedule_node_insert_sequence(node, filters);
4204 for (component = 0; component < graph->scc; ++component) {
4205 n = 0;
4206 for (i = 0; i < graph->n; ++i)
4207 if (graph->node[i].scc == component)
4208 n++;
4209 n_edge = 0;
4210 for (i = 0; i < graph->n_edge; ++i)
4211 if (graph->edge[i].src->scc == component &&
4212 graph->edge[i].dst->scc == component)
4213 n_edge++;
4215 node = isl_schedule_node_child(node, component);
4216 node = isl_schedule_node_child(node, 0);
4217 node = compute_sub_schedule(node, ctx, graph, n, n_edge,
4218 &node_scc_exactly,
4219 &edge_scc_exactly, component, wcc);
4220 node = isl_schedule_node_parent(node);
4221 node = isl_schedule_node_parent(node);
4224 return node;
4227 /* Compute a schedule for the given dependence graph and insert it at "node".
4228 * Return the updated schedule node.
4230 * We first check if the graph is connected (through validity and conditional
4231 * validity dependences) and, if not, compute a schedule
4232 * for each component separately.
4233 * If the schedule_serialize_sccs option is set, then we check for strongly
4234 * connected components instead and compute a separate schedule for
4235 * each such strongly connected component.
4237 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
4238 struct isl_sched_graph *graph)
4240 isl_ctx *ctx;
4242 if (!node)
4243 return NULL;
4245 ctx = isl_schedule_node_get_ctx(node);
4246 if (isl_options_get_schedule_serialize_sccs(ctx)) {
4247 if (detect_sccs(ctx, graph) < 0)
4248 return isl_schedule_node_free(node);
4249 } else {
4250 if (detect_wccs(ctx, graph) < 0)
4251 return isl_schedule_node_free(node);
4254 if (graph->scc > 1)
4255 return compute_component_schedule(node, graph, 1);
4257 return compute_schedule_wcc(node, graph);
4260 /* Compute a schedule on sc->domain that respects the given schedule
4261 * constraints.
4263 * In particular, the schedule respects all the validity dependences.
4264 * If the default isl scheduling algorithm is used, it tries to minimize
4265 * the dependence distances over the proximity dependences.
4266 * If Feautrier's scheduling algorithm is used, the proximity dependence
4267 * distances are only minimized during the extension to a full-dimensional
4268 * schedule.
4270 * If there are any condition and conditional validity dependences,
4271 * then the conditional validity dependences may be violated inside
4272 * a tilable band, provided they have no adjacent non-local
4273 * condition dependences.
4275 * The context is included in the domain before the nodes of
4276 * the graphs are extracted in order to be able to exploit
4277 * any possible additional equalities.
4278 * However, the returned schedule contains the original domain
4279 * (before this intersection).
4281 __isl_give isl_schedule *isl_schedule_constraints_compute_schedule(
4282 __isl_take isl_schedule_constraints *sc)
4284 isl_ctx *ctx = isl_schedule_constraints_get_ctx(sc);
4285 struct isl_sched_graph graph = { 0 };
4286 isl_schedule *sched;
4287 isl_schedule_node *node;
4288 isl_union_set *domain;
4289 struct isl_extract_edge_data data;
4290 enum isl_edge_type i;
4291 int r;
4293 sc = isl_schedule_constraints_align_params(sc);
4294 if (!sc)
4295 return NULL;
4297 graph.n = isl_union_set_n_set(sc->domain);
4298 if (graph.n == 0) {
4299 isl_union_set *domain = isl_union_set_copy(sc->domain);
4300 sched = isl_schedule_from_domain(domain);
4301 goto done;
4303 if (graph_alloc(ctx, &graph, graph.n,
4304 isl_schedule_constraints_n_map(sc)) < 0)
4305 goto error;
4306 if (compute_max_row(&graph, sc) < 0)
4307 goto error;
4308 graph.root = 1;
4309 graph.n = 0;
4310 domain = isl_union_set_copy(sc->domain);
4311 domain = isl_union_set_intersect_params(domain,
4312 isl_set_copy(sc->context));
4313 r = isl_union_set_foreach_set(domain, &extract_node, &graph);
4314 isl_union_set_free(domain);
4315 if (r < 0)
4316 goto error;
4317 if (graph_init_table(ctx, &graph) < 0)
4318 goto error;
4319 for (i = isl_edge_first; i <= isl_edge_last; ++i)
4320 graph.max_edge[i] = isl_union_map_n_map(sc->constraint[i]);
4321 if (graph_init_edge_tables(ctx, &graph) < 0)
4322 goto error;
4323 graph.n_edge = 0;
4324 data.graph = &graph;
4325 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
4326 data.type = i;
4327 if (isl_union_map_foreach_map(sc->constraint[i],
4328 &extract_edge, &data) < 0)
4329 goto error;
4332 node = isl_schedule_node_from_domain(isl_union_set_copy(sc->domain));
4333 node = isl_schedule_node_child(node, 0);
4334 if (graph.n > 0)
4335 node = compute_schedule(node, &graph);
4336 sched = isl_schedule_node_get_schedule(node);
4337 isl_schedule_node_free(node);
4339 done:
4340 graph_free(ctx, &graph);
4341 isl_schedule_constraints_free(sc);
4343 return sched;
4344 error:
4345 graph_free(ctx, &graph);
4346 isl_schedule_constraints_free(sc);
4347 return NULL;
4350 /* Compute a schedule for the given union of domains that respects
4351 * all the validity dependences and minimizes
4352 * the dependence distances over the proximity dependences.
4354 * This function is kept for backward compatibility.
4356 __isl_give isl_schedule *isl_union_set_compute_schedule(
4357 __isl_take isl_union_set *domain,
4358 __isl_take isl_union_map *validity,
4359 __isl_take isl_union_map *proximity)
4361 isl_schedule_constraints *sc;
4363 sc = isl_schedule_constraints_on_domain(domain);
4364 sc = isl_schedule_constraints_set_validity(sc, validity);
4365 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4367 return isl_schedule_constraints_compute_schedule(sc);