isl_map_simplify.c: eliminate_div: improve error handling
[isl.git] / isl_scheduler.c
blobff02711d5b3294695a8dc869f79797aa46337427
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 int orig_total_row;
3052 isl_ctx *ctx;
3053 isl_union_set_list *filters;
3055 if (!node)
3056 return NULL;
3058 if (reset_band(graph) < 0)
3059 return isl_schedule_node_free(node);
3061 n = 0;
3062 for (i = 0; i < graph->n; ++i) {
3063 struct isl_sched_node *node = &graph->node[i];
3064 int before = node->scc <= graph->src_scc;
3066 if (before)
3067 n++;
3070 e1 = e2 = 0;
3071 for (i = 0; i < graph->n_edge; ++i) {
3072 if (graph->edge[i].dst->scc <= graph->src_scc)
3073 e1++;
3074 if (graph->edge[i].src->scc > graph->src_scc)
3075 e2++;
3078 next_band(graph);
3080 ctx = isl_schedule_node_get_ctx(node);
3081 filters = extract_split(ctx, graph);
3082 node = isl_schedule_node_insert_sequence(node, filters);
3083 node = isl_schedule_node_child(node, 0);
3084 node = isl_schedule_node_child(node, 0);
3086 orig_total_row = graph->n_total_row;
3087 node = compute_sub_schedule(node, ctx, graph, n, e1,
3088 &node_scc_at_most, &edge_dst_scc_at_most,
3089 graph->src_scc, 0);
3090 node = isl_schedule_node_parent(node);
3091 node = isl_schedule_node_next_sibling(node);
3092 node = isl_schedule_node_child(node, 0);
3093 graph->n_total_row = orig_total_row;
3094 node = compute_sub_schedule(node, ctx, graph, graph->n - n, e2,
3095 &node_scc_at_least, &edge_src_scc_at_least,
3096 graph->src_scc + 1, 0);
3097 node = isl_schedule_node_parent(node);
3098 node = isl_schedule_node_parent(node);
3100 return node;
3103 /* Insert a band node at position "node" in the schedule tree corresponding
3104 * to the current band in "graph". Mark the band node permutable
3105 * if "permutable" is set.
3106 * The partial schedules and the coincidence property are extracted
3107 * from the graph nodes.
3108 * Return the updated schedule node.
3110 static __isl_give isl_schedule_node *insert_current_band(
3111 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3112 int permutable)
3114 int i;
3115 int start, end, n;
3116 isl_multi_aff *ma;
3117 isl_multi_pw_aff *mpa;
3118 isl_multi_union_pw_aff *mupa;
3120 if (!node)
3121 return NULL;
3123 if (graph->n < 1)
3124 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
3125 "graph should have at least one node",
3126 return isl_schedule_node_free(node));
3128 start = graph->band_start;
3129 end = graph->n_total_row;
3130 n = end - start;
3132 ma = node_extract_partial_schedule_multi_aff(&graph->node[0], start, n);
3133 mpa = isl_multi_pw_aff_from_multi_aff(ma);
3134 mupa = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
3136 for (i = 1; i < graph->n; ++i) {
3137 isl_multi_union_pw_aff *mupa_i;
3139 ma = node_extract_partial_schedule_multi_aff(&graph->node[i],
3140 start, n);
3141 mpa = isl_multi_pw_aff_from_multi_aff(ma);
3142 mupa_i = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
3143 mupa = isl_multi_union_pw_aff_union_add(mupa, mupa_i);
3145 node = isl_schedule_node_insert_partial_schedule(node, mupa);
3147 for (i = 0; i < n; ++i)
3148 node = isl_schedule_node_band_member_set_coincident(node, i,
3149 graph->node[0].coincident[start + i]);
3150 node = isl_schedule_node_band_set_permutable(node, permutable);
3152 return node;
3155 /* Update the dependence relations based on the current schedule,
3156 * add the current band to "node" and the continue with the computation
3157 * of the next band.
3158 * Return the updated schedule node.
3160 static __isl_give isl_schedule_node *compute_next_band(
3161 __isl_take isl_schedule_node *node,
3162 struct isl_sched_graph *graph, int permutable)
3164 isl_ctx *ctx;
3166 if (!node)
3167 return NULL;
3169 ctx = isl_schedule_node_get_ctx(node);
3170 if (update_edges(ctx, graph) < 0)
3171 return isl_schedule_node_free(node);
3172 node = insert_current_band(node, graph, permutable);
3173 next_band(graph);
3175 node = isl_schedule_node_child(node, 0);
3176 node = compute_schedule(node, graph);
3177 node = isl_schedule_node_parent(node);
3179 return node;
3182 /* Add constraints to graph->lp that force the dependence "map" (which
3183 * is part of the dependence relation of "edge")
3184 * to be respected and attempt to carry it, where the edge is one from
3185 * a node j to itself. "pos" is the sequence number of the given map.
3186 * That is, add constraints that enforce
3188 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3189 * = c_j_x (y - x) >= e_i
3191 * for each (x,y) in R.
3192 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3193 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3194 * with each coefficient in c_j_x represented as a pair of non-negative
3195 * coefficients.
3197 static int add_intra_constraints(struct isl_sched_graph *graph,
3198 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3200 unsigned total;
3201 isl_ctx *ctx = isl_map_get_ctx(map);
3202 isl_space *dim;
3203 isl_dim_map *dim_map;
3204 isl_basic_set *coef;
3205 struct isl_sched_node *node = edge->src;
3207 coef = intra_coefficients(graph, node, map);
3208 if (!coef)
3209 return -1;
3211 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3213 total = isl_basic_set_total_dim(graph->lp);
3214 dim_map = isl_dim_map_alloc(ctx, total);
3215 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3216 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
3217 isl_space_dim(dim, isl_dim_set), 1,
3218 node->nvar, -1);
3219 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
3220 isl_space_dim(dim, isl_dim_set), 1,
3221 node->nvar, 1);
3222 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3223 coef->n_eq, coef->n_ineq);
3224 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3225 coef, dim_map);
3226 isl_space_free(dim);
3228 return 0;
3231 /* Add constraints to graph->lp that force the dependence "map" (which
3232 * is part of the dependence relation of "edge")
3233 * to be respected and attempt to carry it, where the edge is one from
3234 * node j to node k. "pos" is the sequence number of the given map.
3235 * That is, add constraints that enforce
3237 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3239 * for each (x,y) in R.
3240 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3241 * of valid constraints for R and then plug in
3242 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3243 * with each coefficient (except e_i, c_k_0 and c_j_0)
3244 * represented as a pair of non-negative coefficients.
3246 static int add_inter_constraints(struct isl_sched_graph *graph,
3247 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3249 unsigned total;
3250 isl_ctx *ctx = isl_map_get_ctx(map);
3251 isl_space *dim;
3252 isl_dim_map *dim_map;
3253 isl_basic_set *coef;
3254 struct isl_sched_node *src = edge->src;
3255 struct isl_sched_node *dst = edge->dst;
3257 coef = inter_coefficients(graph, edge, map);
3258 if (!coef)
3259 return -1;
3261 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3263 total = isl_basic_set_total_dim(graph->lp);
3264 dim_map = isl_dim_map_alloc(ctx, total);
3266 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3268 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
3269 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
3270 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
3271 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
3272 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3273 dst->nvar, -1);
3274 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
3275 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3276 dst->nvar, 1);
3278 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
3279 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
3280 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
3281 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
3282 isl_space_dim(dim, isl_dim_set), 1,
3283 src->nvar, 1);
3284 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
3285 isl_space_dim(dim, isl_dim_set), 1,
3286 src->nvar, -1);
3288 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3289 coef->n_eq, coef->n_ineq);
3290 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3291 coef, dim_map);
3292 isl_space_free(dim);
3294 return 0;
3297 /* Add constraints to graph->lp that force all (conditional) validity
3298 * dependences to be respected and attempt to carry them.
3300 static int add_all_constraints(struct isl_sched_graph *graph)
3302 int i, j;
3303 int pos;
3305 pos = 0;
3306 for (i = 0; i < graph->n_edge; ++i) {
3307 struct isl_sched_edge *edge= &graph->edge[i];
3309 if (!edge->validity && !edge->conditional_validity)
3310 continue;
3312 for (j = 0; j < edge->map->n; ++j) {
3313 isl_basic_map *bmap;
3314 isl_map *map;
3316 bmap = isl_basic_map_copy(edge->map->p[j]);
3317 map = isl_map_from_basic_map(bmap);
3319 if (edge->src == edge->dst &&
3320 add_intra_constraints(graph, edge, map, pos) < 0)
3321 return -1;
3322 if (edge->src != edge->dst &&
3323 add_inter_constraints(graph, edge, map, pos) < 0)
3324 return -1;
3325 ++pos;
3329 return 0;
3332 /* Count the number of equality and inequality constraints
3333 * that will be added to the carry_lp problem.
3334 * We count each edge exactly once.
3336 static int count_all_constraints(struct isl_sched_graph *graph,
3337 int *n_eq, int *n_ineq)
3339 int i, j;
3341 *n_eq = *n_ineq = 0;
3342 for (i = 0; i < graph->n_edge; ++i) {
3343 struct isl_sched_edge *edge= &graph->edge[i];
3344 for (j = 0; j < edge->map->n; ++j) {
3345 isl_basic_map *bmap;
3346 isl_map *map;
3348 bmap = isl_basic_map_copy(edge->map->p[j]);
3349 map = isl_map_from_basic_map(bmap);
3351 if (count_map_constraints(graph, edge, map,
3352 n_eq, n_ineq, 1, 0) < 0)
3353 return -1;
3357 return 0;
3360 /* Construct an LP problem for finding schedule coefficients
3361 * such that the schedule carries as many dependences as possible.
3362 * In particular, for each dependence i, we bound the dependence distance
3363 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3364 * of all e_i's. Dependence with e_i = 0 in the solution are simply
3365 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3366 * Note that if the dependence relation is a union of basic maps,
3367 * then we have to consider each basic map individually as it may only
3368 * be possible to carry the dependences expressed by some of those
3369 * basic maps and not all off them.
3370 * Below, we consider each of those basic maps as a separate "edge".
3372 * All variables of the LP are non-negative. The actual coefficients
3373 * may be negative, so each coefficient is represented as the difference
3374 * of two non-negative variables. The negative part always appears
3375 * immediately before the positive part.
3376 * Other than that, the variables have the following order
3378 * - sum of (1 - e_i) over all edges
3379 * - sum of positive and negative parts of all c_n coefficients
3380 * (unconstrained when computing non-parametric schedules)
3381 * - sum of positive and negative parts of all c_x coefficients
3382 * - for each edge
3383 * - e_i
3384 * - for each node
3385 * - c_i_0
3386 * - positive and negative parts of c_i_n (if parametric)
3387 * - positive and negative parts of c_i_x
3389 * The constraints are those from the (validity) edges plus three equalities
3390 * to express the sums and n_edge inequalities to express e_i <= 1.
3392 static int setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
3394 int i, j;
3395 int k;
3396 isl_space *dim;
3397 unsigned total;
3398 int n_eq, n_ineq;
3399 int n_edge;
3401 n_edge = 0;
3402 for (i = 0; i < graph->n_edge; ++i)
3403 n_edge += graph->edge[i].map->n;
3405 total = 3 + n_edge;
3406 for (i = 0; i < graph->n; ++i) {
3407 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
3408 node->start = total;
3409 total += 1 + 2 * (node->nparam + node->nvar);
3412 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
3413 return -1;
3414 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
3415 return -1;
3417 dim = isl_space_set_alloc(ctx, 0, total);
3418 isl_basic_set_free(graph->lp);
3419 n_eq += 3;
3420 n_ineq += n_edge;
3421 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
3422 graph->lp = isl_basic_set_set_rational(graph->lp);
3424 k = isl_basic_set_alloc_equality(graph->lp);
3425 if (k < 0)
3426 return -1;
3427 isl_seq_clr(graph->lp->eq[k], 1 + total);
3428 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
3429 isl_int_set_si(graph->lp->eq[k][1], 1);
3430 for (i = 0; i < n_edge; ++i)
3431 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
3433 k = isl_basic_set_alloc_equality(graph->lp);
3434 if (k < 0)
3435 return -1;
3436 isl_seq_clr(graph->lp->eq[k], 1 + total);
3437 isl_int_set_si(graph->lp->eq[k][2], -1);
3438 for (i = 0; i < graph->n; ++i) {
3439 int pos = 1 + graph->node[i].start + 1;
3441 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
3442 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3445 k = isl_basic_set_alloc_equality(graph->lp);
3446 if (k < 0)
3447 return -1;
3448 isl_seq_clr(graph->lp->eq[k], 1 + total);
3449 isl_int_set_si(graph->lp->eq[k][3], -1);
3450 for (i = 0; i < graph->n; ++i) {
3451 struct isl_sched_node *node = &graph->node[i];
3452 int pos = 1 + node->start + 1 + 2 * node->nparam;
3454 for (j = 0; j < 2 * node->nvar; ++j)
3455 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3458 for (i = 0; i < n_edge; ++i) {
3459 k = isl_basic_set_alloc_inequality(graph->lp);
3460 if (k < 0)
3461 return -1;
3462 isl_seq_clr(graph->lp->ineq[k], 1 + total);
3463 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
3464 isl_int_set_si(graph->lp->ineq[k][0], 1);
3467 if (add_bound_coefficient_constraints(ctx, graph) < 0)
3468 return -1;
3469 if (add_all_constraints(graph) < 0)
3470 return -1;
3472 return 0;
3475 static __isl_give isl_schedule_node *compute_component_schedule(
3476 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3477 int wcc);
3479 /* Comparison function for sorting the statements based on
3480 * the corresponding value in "r".
3482 static int smaller_value(const void *a, const void *b, void *data)
3484 isl_vec *r = data;
3485 const int *i1 = a;
3486 const int *i2 = b;
3488 return isl_int_cmp(r->el[*i1], r->el[*i2]);
3491 /* If the schedule_split_scaled option is set and if the linear
3492 * parts of the scheduling rows for all nodes in the graphs have
3493 * a non-trivial common divisor, then split off the remainder of the
3494 * constant term modulo this common divisor from the linear part.
3495 * Otherwise, insert a band node directly and continue with
3496 * the construction of the schedule.
3498 * If a non-trivial common divisor is found, then
3499 * the linear part is reduced and the remainder is enforced
3500 * by a sequence node with the children placed in the order
3501 * of this remainder.
3502 * In particular, we assign an scc index based on the remainder and
3503 * then rely on compute_component_schedule to insert the sequence and
3504 * to continue the schedule construction on each part.
3506 static __isl_give isl_schedule_node *split_scaled(
3507 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3509 int i;
3510 int row;
3511 int scc;
3512 isl_ctx *ctx;
3513 isl_int gcd, gcd_i;
3514 isl_vec *r;
3515 int *order;
3517 if (!node)
3518 return NULL;
3520 ctx = isl_schedule_node_get_ctx(node);
3521 if (!ctx->opt->schedule_split_scaled)
3522 return compute_next_band(node, graph, 0);
3523 if (graph->n <= 1)
3524 return compute_next_band(node, graph, 0);
3526 isl_int_init(gcd);
3527 isl_int_init(gcd_i);
3529 isl_int_set_si(gcd, 0);
3531 row = isl_mat_rows(graph->node[0].sched) - 1;
3533 for (i = 0; i < graph->n; ++i) {
3534 struct isl_sched_node *node = &graph->node[i];
3535 int cols = isl_mat_cols(node->sched);
3537 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
3538 isl_int_gcd(gcd, gcd, gcd_i);
3541 isl_int_clear(gcd_i);
3543 if (isl_int_cmp_si(gcd, 1) <= 0) {
3544 isl_int_clear(gcd);
3545 return compute_next_band(node, graph, 0);
3548 r = isl_vec_alloc(ctx, graph->n);
3549 order = isl_calloc_array(ctx, int, graph->n);
3550 if (!r || !order)
3551 goto error;
3553 for (i = 0; i < graph->n; ++i) {
3554 struct isl_sched_node *node = &graph->node[i];
3556 order[i] = i;
3557 isl_int_fdiv_r(r->el[i], node->sched->row[row][0], gcd);
3558 isl_int_fdiv_q(node->sched->row[row][0],
3559 node->sched->row[row][0], gcd);
3560 isl_int_mul(node->sched->row[row][0],
3561 node->sched->row[row][0], gcd);
3562 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
3563 if (!node->sched)
3564 goto error;
3567 if (isl_sort(order, graph->n, sizeof(order[0]), &smaller_value, r) < 0)
3568 goto error;
3570 scc = 0;
3571 for (i = 0; i < graph->n; ++i) {
3572 if (i > 0 && isl_int_ne(r->el[order[i - 1]], r->el[order[i]]))
3573 ++scc;
3574 graph->node[order[i]].scc = scc;
3576 graph->scc = ++scc;
3577 graph->weak = 0;
3579 isl_int_clear(gcd);
3580 isl_vec_free(r);
3581 free(order);
3583 if (update_edges(ctx, graph) < 0)
3584 return isl_schedule_node_free(node);
3585 node = insert_current_band(node, graph, 0);
3586 next_band(graph);
3588 node = isl_schedule_node_child(node, 0);
3589 node = compute_component_schedule(node, graph, 0);
3590 node = isl_schedule_node_parent(node);
3592 return node;
3593 error:
3594 isl_vec_free(r);
3595 free(order);
3596 isl_int_clear(gcd);
3597 return isl_schedule_node_free(node);
3600 /* Is the schedule row "sol" trivial on node "node"?
3601 * That is, is the solution zero on the dimensions orthogonal to
3602 * the previously found solutions?
3603 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3605 * Each coefficient is represented as the difference between
3606 * two non-negative values in "sol". "sol" has been computed
3607 * in terms of the original iterators (i.e., without use of cmap).
3608 * We construct the schedule row s and write it as a linear
3609 * combination of (linear combinations of) previously computed schedule rows.
3610 * s = Q c or c = U s.
3611 * If the final entries of c are all zero, then the solution is trivial.
3613 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
3615 int i;
3616 int pos;
3617 int trivial;
3618 isl_ctx *ctx;
3619 isl_vec *node_sol;
3621 if (!sol)
3622 return -1;
3623 if (node->nvar == node->rank)
3624 return 0;
3626 ctx = isl_vec_get_ctx(sol);
3627 node_sol = isl_vec_alloc(ctx, node->nvar);
3628 if (!node_sol)
3629 return -1;
3631 pos = 1 + node->start + 1 + 2 * node->nparam;
3633 for (i = 0; i < node->nvar; ++i)
3634 isl_int_sub(node_sol->el[i],
3635 sol->el[pos + 2 * i + 1], sol->el[pos + 2 * i]);
3637 node_sol = isl_mat_vec_product(isl_mat_copy(node->cinv), node_sol);
3639 if (!node_sol)
3640 return -1;
3642 trivial = isl_seq_first_non_zero(node_sol->el + node->rank,
3643 node->nvar - node->rank) == -1;
3645 isl_vec_free(node_sol);
3647 return trivial;
3650 /* Is the schedule row "sol" trivial on any node where it should
3651 * not be trivial?
3652 * "sol" has been computed in terms of the original iterators
3653 * (i.e., without use of cmap).
3654 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3656 static int is_any_trivial(struct isl_sched_graph *graph,
3657 __isl_keep isl_vec *sol)
3659 int i;
3661 for (i = 0; i < graph->n; ++i) {
3662 struct isl_sched_node *node = &graph->node[i];
3663 int trivial;
3665 if (!needs_row(graph, node))
3666 continue;
3667 trivial = is_trivial(node, sol);
3668 if (trivial < 0 || trivial)
3669 return trivial;
3672 return 0;
3675 /* Construct a schedule row for each node such that as many dependences
3676 * as possible are carried and then continue with the next band.
3678 * If the computed schedule row turns out to be trivial on one or
3679 * more nodes where it should not be trivial, then we throw it away
3680 * and try again on each component separately.
3682 * If there is only one component, then we accept the schedule row anyway,
3683 * but we do not consider it as a complete row and therefore do not
3684 * increment graph->n_row. Note that the ranks of the nodes that
3685 * do get a non-trivial schedule part will get updated regardless and
3686 * graph->maxvar is computed based on these ranks. The test for
3687 * whether more schedule rows are required in compute_schedule_wcc
3688 * is therefore not affected.
3690 * Insert a band corresponding to the schedule row at position "node"
3691 * of the schedule tree and continue with the construction of the schedule.
3692 * This insertion and the continued construction is performed by split_scaled
3693 * after optionally checking for non-trivial common divisors.
3695 static __isl_give isl_schedule_node *carry_dependences(
3696 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3698 int i;
3699 int n_edge;
3700 int trivial;
3701 isl_ctx *ctx;
3702 isl_vec *sol;
3703 isl_basic_set *lp;
3705 if (!node)
3706 return NULL;
3708 n_edge = 0;
3709 for (i = 0; i < graph->n_edge; ++i)
3710 n_edge += graph->edge[i].map->n;
3712 ctx = isl_schedule_node_get_ctx(node);
3713 if (setup_carry_lp(ctx, graph) < 0)
3714 return isl_schedule_node_free(node);
3716 lp = isl_basic_set_copy(graph->lp);
3717 sol = isl_tab_basic_set_non_neg_lexmin(lp);
3718 if (!sol)
3719 return isl_schedule_node_free(node);
3721 if (sol->size == 0) {
3722 isl_vec_free(sol);
3723 isl_die(ctx, isl_error_internal,
3724 "error in schedule construction",
3725 return isl_schedule_node_free(node));
3728 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
3729 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
3730 isl_vec_free(sol);
3731 isl_die(ctx, isl_error_unknown,
3732 "unable to carry dependences",
3733 return isl_schedule_node_free(node));
3736 trivial = is_any_trivial(graph, sol);
3737 if (trivial < 0) {
3738 sol = isl_vec_free(sol);
3739 } else if (trivial && graph->scc > 1) {
3740 isl_vec_free(sol);
3741 return compute_component_schedule(node, graph, 1);
3744 if (update_schedule(graph, sol, 0, 0) < 0)
3745 return isl_schedule_node_free(node);
3746 if (trivial)
3747 graph->n_row--;
3749 return split_scaled(node, graph);
3752 /* Topologically sort statements mapped to the same schedule iteration
3753 * and add insert a sequence node in front of "node"
3754 * corresponding to this order.
3756 * If it turns out to be impossible to sort the statements apart,
3757 * because different dependences impose different orderings
3758 * on the statements, then we extend the schedule such that
3759 * it carries at least one more dependence.
3761 static __isl_give isl_schedule_node *sort_statements(
3762 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3764 isl_ctx *ctx;
3765 isl_union_set_list *filters;
3767 if (!node)
3768 return NULL;
3770 ctx = isl_schedule_node_get_ctx(node);
3771 if (graph->n < 1)
3772 isl_die(ctx, isl_error_internal,
3773 "graph should have at least one node",
3774 return isl_schedule_node_free(node));
3776 if (graph->n == 1)
3777 return node;
3779 if (update_edges(ctx, graph) < 0)
3780 return isl_schedule_node_free(node);
3782 if (graph->n_edge == 0)
3783 return node;
3785 if (detect_sccs(ctx, graph) < 0)
3786 return isl_schedule_node_free(node);
3788 next_band(graph);
3789 if (graph->scc < graph->n)
3790 return carry_dependences(node, graph);
3792 filters = extract_sccs(ctx, graph);
3793 node = isl_schedule_node_insert_sequence(node, filters);
3795 return node;
3798 /* Are there any (non-empty) (conditional) validity edges in the graph?
3800 static int has_validity_edges(struct isl_sched_graph *graph)
3802 int i;
3804 for (i = 0; i < graph->n_edge; ++i) {
3805 int empty;
3807 empty = isl_map_plain_is_empty(graph->edge[i].map);
3808 if (empty < 0)
3809 return -1;
3810 if (empty)
3811 continue;
3812 if (graph->edge[i].validity ||
3813 graph->edge[i].conditional_validity)
3814 return 1;
3817 return 0;
3820 /* Should we apply a Feautrier step?
3821 * That is, did the user request the Feautrier algorithm and are
3822 * there any validity dependences (left)?
3824 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
3826 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
3827 return 0;
3829 return has_validity_edges(graph);
3832 /* Compute a schedule for a connected dependence graph using Feautrier's
3833 * multi-dimensional scheduling algorithm and return the updated schedule node.
3835 * The original algorithm is described in [1].
3836 * The main idea is to minimize the number of scheduling dimensions, by
3837 * trying to satisfy as many dependences as possible per scheduling dimension.
3839 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
3840 * Problem, Part II: Multi-Dimensional Time.
3841 * In Intl. Journal of Parallel Programming, 1992.
3843 static __isl_give isl_schedule_node *compute_schedule_wcc_feautrier(
3844 isl_schedule_node *node, struct isl_sched_graph *graph)
3846 return carry_dependences(node, graph);
3849 /* Turn off the "local" bit on all (condition) edges.
3851 static void clear_local_edges(struct isl_sched_graph *graph)
3853 int i;
3855 for (i = 0; i < graph->n_edge; ++i)
3856 if (graph->edge[i].condition)
3857 graph->edge[i].local = 0;
3860 /* Does "graph" have both condition and conditional validity edges?
3862 static int need_condition_check(struct isl_sched_graph *graph)
3864 int i;
3865 int any_condition = 0;
3866 int any_conditional_validity = 0;
3868 for (i = 0; i < graph->n_edge; ++i) {
3869 if (graph->edge[i].condition)
3870 any_condition = 1;
3871 if (graph->edge[i].conditional_validity)
3872 any_conditional_validity = 1;
3875 return any_condition && any_conditional_validity;
3878 /* Does "graph" contain any coincidence edge?
3880 static int has_any_coincidence(struct isl_sched_graph *graph)
3882 int i;
3884 for (i = 0; i < graph->n_edge; ++i)
3885 if (graph->edge[i].coincidence)
3886 return 1;
3888 return 0;
3891 /* Extract the final schedule row as a map with the iteration domain
3892 * of "node" as domain.
3894 static __isl_give isl_map *final_row(struct isl_sched_node *node)
3896 isl_local_space *ls;
3897 isl_aff *aff;
3898 int row;
3900 row = isl_mat_rows(node->sched) - 1;
3901 ls = isl_local_space_from_space(isl_space_copy(node->space));
3902 aff = extract_schedule_row(ls, node, row);
3903 return isl_map_from_aff(aff);
3906 /* Is the conditional validity dependence in the edge with index "edge_index"
3907 * violated by the latest (i.e., final) row of the schedule?
3908 * That is, is i scheduled after j
3909 * for any conditional validity dependence i -> j?
3911 static int is_violated(struct isl_sched_graph *graph, int edge_index)
3913 isl_map *src_sched, *dst_sched, *map;
3914 struct isl_sched_edge *edge = &graph->edge[edge_index];
3915 int empty;
3917 src_sched = final_row(edge->src);
3918 dst_sched = final_row(edge->dst);
3919 map = isl_map_copy(edge->map);
3920 map = isl_map_apply_domain(map, src_sched);
3921 map = isl_map_apply_range(map, dst_sched);
3922 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3923 empty = isl_map_is_empty(map);
3924 isl_map_free(map);
3926 if (empty < 0)
3927 return -1;
3929 return !empty;
3932 /* Does "graph" have any satisfied condition edges that
3933 * are adjacent to the conditional validity constraint with
3934 * domain "conditional_source" and range "conditional_sink"?
3936 * A satisfied condition is one that is not local.
3937 * If a condition was forced to be local already (i.e., marked as local)
3938 * then there is no need to check if it is in fact local.
3940 * Additionally, mark all adjacent condition edges found as local.
3942 static int has_adjacent_true_conditions(struct isl_sched_graph *graph,
3943 __isl_keep isl_union_set *conditional_source,
3944 __isl_keep isl_union_set *conditional_sink)
3946 int i;
3947 int any = 0;
3949 for (i = 0; i < graph->n_edge; ++i) {
3950 int adjacent, local;
3951 isl_union_map *condition;
3953 if (!graph->edge[i].condition)
3954 continue;
3955 if (graph->edge[i].local)
3956 continue;
3958 condition = graph->edge[i].tagged_condition;
3959 adjacent = domain_intersects(condition, conditional_sink);
3960 if (adjacent >= 0 && !adjacent)
3961 adjacent = range_intersects(condition,
3962 conditional_source);
3963 if (adjacent < 0)
3964 return -1;
3965 if (!adjacent)
3966 continue;
3968 graph->edge[i].local = 1;
3970 local = is_condition_false(&graph->edge[i]);
3971 if (local < 0)
3972 return -1;
3973 if (!local)
3974 any = 1;
3977 return any;
3980 /* Are there any violated conditional validity dependences with
3981 * adjacent condition dependences that are not local with respect
3982 * to the current schedule?
3983 * That is, is the conditional validity constraint violated?
3985 * Additionally, mark all those adjacent condition dependences as local.
3986 * We also mark those adjacent condition dependences that were not marked
3987 * as local before, but just happened to be local already. This ensures
3988 * that they remain local if the schedule is recomputed.
3990 * We first collect domain and range of all violated conditional validity
3991 * dependences and then check if there are any adjacent non-local
3992 * condition dependences.
3994 static int has_violated_conditional_constraint(isl_ctx *ctx,
3995 struct isl_sched_graph *graph)
3997 int i;
3998 int any = 0;
3999 isl_union_set *source, *sink;
4001 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
4002 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
4003 for (i = 0; i < graph->n_edge; ++i) {
4004 isl_union_set *uset;
4005 isl_union_map *umap;
4006 int violated;
4008 if (!graph->edge[i].conditional_validity)
4009 continue;
4011 violated = is_violated(graph, i);
4012 if (violated < 0)
4013 goto error;
4014 if (!violated)
4015 continue;
4017 any = 1;
4019 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
4020 uset = isl_union_map_domain(umap);
4021 source = isl_union_set_union(source, uset);
4022 source = isl_union_set_coalesce(source);
4024 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
4025 uset = isl_union_map_range(umap);
4026 sink = isl_union_set_union(sink, uset);
4027 sink = isl_union_set_coalesce(sink);
4030 if (any)
4031 any = has_adjacent_true_conditions(graph, source, sink);
4033 isl_union_set_free(source);
4034 isl_union_set_free(sink);
4035 return any;
4036 error:
4037 isl_union_set_free(source);
4038 isl_union_set_free(sink);
4039 return -1;
4042 /* Compute a schedule for a connected dependence graph and return
4043 * the updated schedule node.
4045 * We try to find a sequence of as many schedule rows as possible that result
4046 * in non-negative dependence distances (independent of the previous rows
4047 * in the sequence, i.e., such that the sequence is tilable), with as
4048 * many of the initial rows as possible satisfying the coincidence constraints.
4049 * If we can't find any more rows we either
4050 * - split between SCCs and start over (assuming we found an interesting
4051 * pair of SCCs between which to split)
4052 * - continue with the next band (assuming the current band has at least
4053 * one row)
4054 * - try to carry as many dependences as possible and continue with the next
4055 * band
4056 * In each case, we first insert a band node in the schedule tree
4057 * if any rows have been computed.
4059 * If Feautrier's algorithm is selected, we first recursively try to satisfy
4060 * as many validity dependences as possible. When all validity dependences
4061 * are satisfied we extend the schedule to a full-dimensional schedule.
4063 * If we manage to complete the schedule, we insert a band node
4064 * (if any schedule rows were computed) and we finish off by topologically
4065 * sorting the statements based on the remaining dependences.
4067 * If ctx->opt->schedule_outer_coincidence is set, then we force the
4068 * outermost dimension to satisfy the coincidence constraints. If this
4069 * turns out to be impossible, we fall back on the general scheme above
4070 * and try to carry as many dependences as possible.
4072 * If "graph" contains both condition and conditional validity dependences,
4073 * then we need to check that that the conditional schedule constraint
4074 * is satisfied, i.e., there are no violated conditional validity dependences
4075 * that are adjacent to any non-local condition dependences.
4076 * If there are, then we mark all those adjacent condition dependences
4077 * as local and recompute the current band. Those dependences that
4078 * are marked local will then be forced to be local.
4079 * The initial computation is performed with no dependences marked as local.
4080 * If we are lucky, then there will be no violated conditional validity
4081 * dependences adjacent to any non-local condition dependences.
4082 * Otherwise, we mark some additional condition dependences as local and
4083 * recompute. We continue this process until there are no violations left or
4084 * until we are no longer able to compute a schedule.
4085 * Since there are only a finite number of dependences,
4086 * there will only be a finite number of iterations.
4088 static __isl_give isl_schedule_node *compute_schedule_wcc(
4089 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
4091 int has_coincidence;
4092 int use_coincidence;
4093 int force_coincidence = 0;
4094 int check_conditional;
4095 int insert;
4096 isl_ctx *ctx;
4098 if (!node)
4099 return NULL;
4101 ctx = isl_schedule_node_get_ctx(node);
4102 if (detect_sccs(ctx, graph) < 0)
4103 return isl_schedule_node_free(node);
4104 if (sort_sccs(graph) < 0)
4105 return isl_schedule_node_free(node);
4107 if (compute_maxvar(graph) < 0)
4108 return isl_schedule_node_free(node);
4110 if (need_feautrier_step(ctx, graph))
4111 return compute_schedule_wcc_feautrier(node, graph);
4113 clear_local_edges(graph);
4114 check_conditional = need_condition_check(graph);
4115 has_coincidence = has_any_coincidence(graph);
4117 if (ctx->opt->schedule_outer_coincidence)
4118 force_coincidence = 1;
4120 use_coincidence = has_coincidence;
4121 while (graph->n_row < graph->maxvar) {
4122 isl_vec *sol;
4123 int violated;
4124 int coincident;
4126 graph->src_scc = -1;
4127 graph->dst_scc = -1;
4129 if (setup_lp(ctx, graph, use_coincidence) < 0)
4130 return isl_schedule_node_free(node);
4131 sol = solve_lp(graph);
4132 if (!sol)
4133 return isl_schedule_node_free(node);
4134 if (sol->size == 0) {
4135 int empty = graph->n_total_row == graph->band_start;
4137 isl_vec_free(sol);
4138 if (use_coincidence && (!force_coincidence || !empty)) {
4139 use_coincidence = 0;
4140 continue;
4142 if (!ctx->opt->schedule_maximize_band_depth && !empty)
4143 return compute_next_band(node, graph, 1);
4144 if (graph->src_scc >= 0)
4145 return compute_split_schedule(node, graph);
4146 if (!empty)
4147 return compute_next_band(node, graph, 1);
4148 return carry_dependences(node, graph);
4150 coincident = !has_coincidence || use_coincidence;
4151 if (update_schedule(graph, sol, 1, coincident) < 0)
4152 return isl_schedule_node_free(node);
4154 if (!check_conditional)
4155 continue;
4156 violated = has_violated_conditional_constraint(ctx, graph);
4157 if (violated < 0)
4158 return isl_schedule_node_free(node);
4159 if (!violated)
4160 continue;
4161 if (reset_band(graph) < 0)
4162 return isl_schedule_node_free(node);
4163 use_coincidence = has_coincidence;
4166 insert = graph->n_total_row > graph->band_start;
4167 if (insert) {
4168 node = insert_current_band(node, graph, 1);
4169 node = isl_schedule_node_child(node, 0);
4171 node = sort_statements(node, graph);
4172 if (insert)
4173 node = isl_schedule_node_parent(node);
4175 return node;
4178 /* Compute a schedule for each group of nodes identified by node->scc
4179 * separately and then combine them in a sequence node (or as set node
4180 * if graph->weak is set) inserted at position "node" of the schedule tree.
4181 * Return the updated schedule node.
4183 * If "wcc" is set then each of the groups belongs to a single
4184 * weakly connected component in the dependence graph so that
4185 * there is no need for compute_sub_schedule to look for weakly
4186 * connected components.
4188 static __isl_give isl_schedule_node *compute_component_schedule(
4189 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
4190 int wcc)
4192 int component, i;
4193 int n, n_edge;
4194 int orig_total_row;
4195 isl_ctx *ctx;
4196 isl_union_set_list *filters;
4198 if (!node)
4199 return NULL;
4200 ctx = isl_schedule_node_get_ctx(node);
4202 filters = extract_sccs(ctx, graph);
4203 if (graph->weak)
4204 node = isl_schedule_node_insert_set(node, filters);
4205 else
4206 node = isl_schedule_node_insert_sequence(node, filters);
4208 orig_total_row = graph->n_total_row;
4209 for (component = 0; component < graph->scc; ++component) {
4210 n = 0;
4211 for (i = 0; i < graph->n; ++i)
4212 if (graph->node[i].scc == component)
4213 n++;
4214 n_edge = 0;
4215 for (i = 0; i < graph->n_edge; ++i)
4216 if (graph->edge[i].src->scc == component &&
4217 graph->edge[i].dst->scc == component)
4218 n_edge++;
4220 node = isl_schedule_node_child(node, component);
4221 node = isl_schedule_node_child(node, 0);
4222 node = compute_sub_schedule(node, ctx, graph, n, n_edge,
4223 &node_scc_exactly,
4224 &edge_scc_exactly, component, wcc);
4225 node = isl_schedule_node_parent(node);
4226 node = isl_schedule_node_parent(node);
4227 graph->n_total_row = orig_total_row;
4230 return node;
4233 /* Compute a schedule for the given dependence graph and insert it at "node".
4234 * Return the updated schedule node.
4236 * We first check if the graph is connected (through validity and conditional
4237 * validity dependences) and, if not, compute a schedule
4238 * for each component separately.
4239 * If schedule_fuse is set to minimal fusion, then we check for strongly
4240 * connected components instead and compute a separate schedule for
4241 * each such strongly connected component.
4243 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
4244 struct isl_sched_graph *graph)
4246 isl_ctx *ctx;
4248 if (!node)
4249 return NULL;
4251 ctx = isl_schedule_node_get_ctx(node);
4252 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN) {
4253 if (detect_sccs(ctx, graph) < 0)
4254 return isl_schedule_node_free(node);
4255 } else {
4256 if (detect_wccs(ctx, graph) < 0)
4257 return isl_schedule_node_free(node);
4260 if (graph->scc > 1)
4261 return compute_component_schedule(node, graph, 1);
4263 return compute_schedule_wcc(node, graph);
4266 /* Compute a schedule on sc->domain that respects the given schedule
4267 * constraints.
4269 * In particular, the schedule respects all the validity dependences.
4270 * If the default isl scheduling algorithm is used, it tries to minimize
4271 * the dependence distances over the proximity dependences.
4272 * If Feautrier's scheduling algorithm is used, the proximity dependence
4273 * distances are only minimized during the extension to a full-dimensional
4274 * schedule.
4276 * If there are any condition and conditional validity dependences,
4277 * then the conditional validity dependences may be violated inside
4278 * a tilable band, provided they have no adjacent non-local
4279 * condition dependences.
4281 * The context is included in the domain before the nodes of
4282 * the graphs are extracted in order to be able to exploit
4283 * any possible additional equalities.
4284 * However, the returned schedule contains the original domain
4285 * (before this intersection).
4287 __isl_give isl_schedule *isl_schedule_constraints_compute_schedule(
4288 __isl_take isl_schedule_constraints *sc)
4290 isl_ctx *ctx = isl_schedule_constraints_get_ctx(sc);
4291 struct isl_sched_graph graph = { 0 };
4292 isl_schedule *sched;
4293 isl_schedule_node *node;
4294 isl_union_set *domain;
4295 struct isl_extract_edge_data data;
4296 enum isl_edge_type i;
4297 int r;
4299 sc = isl_schedule_constraints_align_params(sc);
4300 if (!sc)
4301 return NULL;
4303 graph.n = isl_union_set_n_set(sc->domain);
4304 if (graph.n == 0) {
4305 isl_union_set *domain = isl_union_set_copy(sc->domain);
4306 sched = isl_schedule_from_domain(domain);
4307 goto done;
4309 if (graph_alloc(ctx, &graph, graph.n,
4310 isl_schedule_constraints_n_map(sc)) < 0)
4311 goto error;
4312 if (compute_max_row(&graph, sc) < 0)
4313 goto error;
4314 graph.root = 1;
4315 graph.n = 0;
4316 domain = isl_union_set_copy(sc->domain);
4317 domain = isl_union_set_intersect_params(domain,
4318 isl_set_copy(sc->context));
4319 r = isl_union_set_foreach_set(domain, &extract_node, &graph);
4320 isl_union_set_free(domain);
4321 if (r < 0)
4322 goto error;
4323 if (graph_init_table(ctx, &graph) < 0)
4324 goto error;
4325 for (i = isl_edge_first; i <= isl_edge_last; ++i)
4326 graph.max_edge[i] = isl_union_map_n_map(sc->constraint[i]);
4327 if (graph_init_edge_tables(ctx, &graph) < 0)
4328 goto error;
4329 graph.n_edge = 0;
4330 data.graph = &graph;
4331 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
4332 data.type = i;
4333 if (isl_union_map_foreach_map(sc->constraint[i],
4334 &extract_edge, &data) < 0)
4335 goto error;
4338 node = isl_schedule_node_from_domain(isl_union_set_copy(sc->domain));
4339 node = isl_schedule_node_child(node, 0);
4340 node = compute_schedule(node, &graph);
4341 sched = isl_schedule_node_get_schedule(node);
4342 isl_schedule_node_free(node);
4344 done:
4345 graph_free(ctx, &graph);
4346 isl_schedule_constraints_free(sc);
4348 return sched;
4349 error:
4350 graph_free(ctx, &graph);
4351 isl_schedule_constraints_free(sc);
4352 return NULL;
4355 /* Compute a schedule for the given union of domains that respects
4356 * all the validity dependences and minimizes
4357 * the dependence distances over the proximity dependences.
4359 * This function is kept for backward compatibility.
4361 __isl_give isl_schedule *isl_union_set_compute_schedule(
4362 __isl_take isl_union_set *domain,
4363 __isl_take isl_union_map *validity,
4364 __isl_take isl_union_map *proximity)
4366 isl_schedule_constraints *sc;
4368 sc = isl_schedule_constraints_on_domain(domain);
4369 sc = isl_schedule_constraints_set_validity(sc, validity);
4370 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4372 return isl_schedule_constraints_compute_schedule(sc);