add isl_schedule_constraints_get_coincidence
[isl.git] / isl_scheduler.c
blobabbbce57a5de64843e602eb4ca6301245646fa0f
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_seq.h>
26 #include <isl_tab.h>
27 #include <isl_dim_map.h>
28 #include <isl/map_to_basic_set.h>
29 #include <isl_sort.h>
30 #include <isl_options_private.h>
31 #include <isl_tarjan.h>
32 #include <isl_morph.h>
35 * The scheduling algorithm implemented in this file was inspired by
36 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
37 * Parallelization and Locality Optimization in the Polyhedral Model".
40 enum isl_edge_type {
41 isl_edge_validity = 0,
42 isl_edge_first = isl_edge_validity,
43 isl_edge_coincidence,
44 isl_edge_condition,
45 isl_edge_conditional_validity,
46 isl_edge_proximity,
47 isl_edge_last = isl_edge_proximity
50 /* The constraints that need to be satisfied by a schedule on "domain".
52 * "context" specifies extra constraints on the parameters.
54 * "validity" constraints map domain elements i to domain elements
55 * that should be scheduled after i. (Hard constraint)
56 * "proximity" constraints map domain elements i to domains elements
57 * that should be scheduled as early as possible after i (or before i).
58 * (Soft constraint)
60 * "condition" and "conditional_validity" constraints map possibly "tagged"
61 * domain elements i -> s to "tagged" domain elements j -> t.
62 * The elements of the "conditional_validity" constraints, but without the
63 * tags (i.e., the elements i -> j) are treated as validity constraints,
64 * except that during the construction of a tilable band,
65 * the elements of the "conditional_validity" constraints may be violated
66 * provided that all adjacent elements of the "condition" constraints
67 * are local within the band.
68 * A dependence is local within a band if domain and range are mapped
69 * to the same schedule point by the band.
71 struct isl_schedule_constraints {
72 isl_union_set *domain;
73 isl_set *context;
75 isl_union_map *constraint[isl_edge_last + 1];
78 __isl_give isl_schedule_constraints *isl_schedule_constraints_copy(
79 __isl_keep isl_schedule_constraints *sc)
81 isl_ctx *ctx;
82 isl_schedule_constraints *sc_copy;
83 enum isl_edge_type i;
85 ctx = isl_union_set_get_ctx(sc->domain);
86 sc_copy = isl_calloc_type(ctx, struct isl_schedule_constraints);
87 if (!sc_copy)
88 return NULL;
90 sc_copy->domain = isl_union_set_copy(sc->domain);
91 sc_copy->context = isl_set_copy(sc->context);
92 if (!sc_copy->domain || !sc_copy->context)
93 return isl_schedule_constraints_free(sc_copy);
95 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
96 sc_copy->constraint[i] = isl_union_map_copy(sc->constraint[i]);
97 if (!sc_copy->constraint[i])
98 return isl_schedule_constraints_free(sc_copy);
101 return sc_copy;
105 /* Construct an isl_schedule_constraints object for computing a schedule
106 * on "domain". The initial object does not impose any constraints.
108 __isl_give isl_schedule_constraints *isl_schedule_constraints_on_domain(
109 __isl_take isl_union_set *domain)
111 isl_ctx *ctx;
112 isl_space *space;
113 isl_schedule_constraints *sc;
114 isl_union_map *empty;
115 enum isl_edge_type i;
117 if (!domain)
118 return NULL;
120 ctx = isl_union_set_get_ctx(domain);
121 sc = isl_calloc_type(ctx, struct isl_schedule_constraints);
122 if (!sc)
123 goto error;
125 space = isl_union_set_get_space(domain);
126 sc->domain = domain;
127 sc->context = isl_set_universe(isl_space_copy(space));
128 empty = isl_union_map_empty(space);
129 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
130 sc->constraint[i] = isl_union_map_copy(empty);
131 if (!sc->constraint[i])
132 sc->domain = isl_union_set_free(sc->domain);
134 isl_union_map_free(empty);
136 if (!sc->domain || !sc->context)
137 return isl_schedule_constraints_free(sc);
139 return sc;
140 error:
141 isl_union_set_free(domain);
142 return NULL;
145 /* Replace the context of "sc" by "context".
147 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_context(
148 __isl_take isl_schedule_constraints *sc, __isl_take isl_set *context)
150 if (!sc || !context)
151 goto error;
153 isl_set_free(sc->context);
154 sc->context = context;
156 return sc;
157 error:
158 isl_schedule_constraints_free(sc);
159 isl_set_free(context);
160 return NULL;
163 /* Replace the validity constraints of "sc" by "validity".
165 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_validity(
166 __isl_take isl_schedule_constraints *sc,
167 __isl_take isl_union_map *validity)
169 if (!sc || !validity)
170 goto error;
172 isl_union_map_free(sc->constraint[isl_edge_validity]);
173 sc->constraint[isl_edge_validity] = validity;
175 return sc;
176 error:
177 isl_schedule_constraints_free(sc);
178 isl_union_map_free(validity);
179 return NULL;
182 /* Replace the coincidence constraints of "sc" by "coincidence".
184 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_coincidence(
185 __isl_take isl_schedule_constraints *sc,
186 __isl_take isl_union_map *coincidence)
188 if (!sc || !coincidence)
189 goto error;
191 isl_union_map_free(sc->constraint[isl_edge_coincidence]);
192 sc->constraint[isl_edge_coincidence] = coincidence;
194 return sc;
195 error:
196 isl_schedule_constraints_free(sc);
197 isl_union_map_free(coincidence);
198 return NULL;
201 /* Replace the proximity constraints of "sc" by "proximity".
203 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_proximity(
204 __isl_take isl_schedule_constraints *sc,
205 __isl_take isl_union_map *proximity)
207 if (!sc || !proximity)
208 goto error;
210 isl_union_map_free(sc->constraint[isl_edge_proximity]);
211 sc->constraint[isl_edge_proximity] = proximity;
213 return sc;
214 error:
215 isl_schedule_constraints_free(sc);
216 isl_union_map_free(proximity);
217 return NULL;
220 /* Replace the conditional validity constraints of "sc" by "condition"
221 * and "validity".
223 __isl_give isl_schedule_constraints *
224 isl_schedule_constraints_set_conditional_validity(
225 __isl_take isl_schedule_constraints *sc,
226 __isl_take isl_union_map *condition,
227 __isl_take isl_union_map *validity)
229 if (!sc || !condition || !validity)
230 goto error;
232 isl_union_map_free(sc->constraint[isl_edge_condition]);
233 sc->constraint[isl_edge_condition] = condition;
234 isl_union_map_free(sc->constraint[isl_edge_conditional_validity]);
235 sc->constraint[isl_edge_conditional_validity] = validity;
237 return sc;
238 error:
239 isl_schedule_constraints_free(sc);
240 isl_union_map_free(condition);
241 isl_union_map_free(validity);
242 return NULL;
245 __isl_null isl_schedule_constraints *isl_schedule_constraints_free(
246 __isl_take isl_schedule_constraints *sc)
248 enum isl_edge_type i;
250 if (!sc)
251 return NULL;
253 isl_union_set_free(sc->domain);
254 isl_set_free(sc->context);
255 for (i = isl_edge_first; i <= isl_edge_last; ++i)
256 isl_union_map_free(sc->constraint[i]);
258 free(sc);
260 return NULL;
263 isl_ctx *isl_schedule_constraints_get_ctx(
264 __isl_keep isl_schedule_constraints *sc)
266 return sc ? isl_union_set_get_ctx(sc->domain) : NULL;
269 /* Return the validity constraints of "sc".
271 __isl_give isl_union_map *isl_schedule_constraints_get_validity(
272 __isl_keep isl_schedule_constraints *sc)
274 if (!sc)
275 return NULL;
277 return isl_union_map_copy(sc->constraint[isl_edge_validity]);
280 /* Return the coincidence constraints of "sc".
282 __isl_give isl_union_map *isl_schedule_constraints_get_coincidence(
283 __isl_keep isl_schedule_constraints *sc)
285 if (!sc)
286 return NULL;
288 return isl_union_map_copy(sc->constraint[isl_edge_coincidence]);
291 void isl_schedule_constraints_dump(__isl_keep isl_schedule_constraints *sc)
293 if (!sc)
294 return;
296 fprintf(stderr, "domain: ");
297 isl_union_set_dump(sc->domain);
298 fprintf(stderr, "context: ");
299 isl_set_dump(sc->context);
300 fprintf(stderr, "validity: ");
301 isl_union_map_dump(sc->constraint[isl_edge_validity]);
302 fprintf(stderr, "proximity: ");
303 isl_union_map_dump(sc->constraint[isl_edge_proximity]);
304 fprintf(stderr, "coincidence: ");
305 isl_union_map_dump(sc->constraint[isl_edge_coincidence]);
306 fprintf(stderr, "condition: ");
307 isl_union_map_dump(sc->constraint[isl_edge_condition]);
308 fprintf(stderr, "conditional_validity: ");
309 isl_union_map_dump(sc->constraint[isl_edge_conditional_validity]);
312 /* Align the parameters of the fields of "sc".
314 static __isl_give isl_schedule_constraints *
315 isl_schedule_constraints_align_params(__isl_take isl_schedule_constraints *sc)
317 isl_space *space;
318 enum isl_edge_type i;
320 if (!sc)
321 return NULL;
323 space = isl_union_set_get_space(sc->domain);
324 space = isl_space_align_params(space, isl_set_get_space(sc->context));
325 for (i = isl_edge_first; i <= isl_edge_last; ++i)
326 space = isl_space_align_params(space,
327 isl_union_map_get_space(sc->constraint[i]));
329 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
330 sc->constraint[i] = isl_union_map_align_params(
331 sc->constraint[i], isl_space_copy(space));
332 if (!sc->constraint[i])
333 space = isl_space_free(space);
335 sc->context = isl_set_align_params(sc->context, isl_space_copy(space));
336 sc->domain = isl_union_set_align_params(sc->domain, space);
337 if (!sc->context || !sc->domain)
338 return isl_schedule_constraints_free(sc);
340 return sc;
343 /* Return the total number of isl_maps in the constraints of "sc".
345 static __isl_give int isl_schedule_constraints_n_map(
346 __isl_keep isl_schedule_constraints *sc)
348 enum isl_edge_type i;
349 int n = 0;
351 for (i = isl_edge_first; i <= isl_edge_last; ++i)
352 n += isl_union_map_n_map(sc->constraint[i]);
354 return n;
357 /* Internal information about a node that is used during the construction
358 * of a schedule.
359 * space represents the space in which the domain lives
360 * sched is a matrix representation of the schedule being constructed
361 * for this node; if compressed is set, then this schedule is
362 * defined over the compressed domain space
363 * sched_map is an isl_map representation of the same (partial) schedule
364 * sched_map may be NULL; if compressed is set, then this map
365 * is defined over the uncompressed domain space
366 * rank is the number of linearly independent rows in the linear part
367 * of sched
368 * the columns of cmap represent a change of basis for the schedule
369 * coefficients; the first rank columns span the linear part of
370 * the schedule rows
371 * cinv is the inverse of cmap.
372 * start is the first variable in the LP problem in the sequences that
373 * represents the schedule coefficients of this node
374 * nvar is the dimension of the domain
375 * nparam is the number of parameters or 0 if we are not constructing
376 * a parametric schedule
378 * If compressed is set, then hull represents the constraints
379 * that were used to derive the compression, while compress and
380 * decompress map the original space to the compressed space and
381 * vice versa.
383 * scc is the index of SCC (or WCC) this node belongs to
385 * coincident contains a boolean for each of the rows of the schedule,
386 * indicating whether the corresponding scheduling dimension satisfies
387 * the coincidence constraints in the sense that the corresponding
388 * dependence distances are zero.
390 struct isl_sched_node {
391 isl_space *space;
392 int compressed;
393 isl_set *hull;
394 isl_multi_aff *compress;
395 isl_multi_aff *decompress;
396 isl_mat *sched;
397 isl_map *sched_map;
398 int rank;
399 isl_mat *cmap;
400 isl_mat *cinv;
401 int start;
402 int nvar;
403 int nparam;
405 int scc;
407 int *coincident;
410 static int node_has_space(const void *entry, const void *val)
412 struct isl_sched_node *node = (struct isl_sched_node *)entry;
413 isl_space *dim = (isl_space *)val;
415 return isl_space_is_equal(node->space, dim);
418 static int node_scc_exactly(struct isl_sched_node *node, int scc)
420 return node->scc == scc;
423 static int node_scc_at_most(struct isl_sched_node *node, int scc)
425 return node->scc <= scc;
428 static int node_scc_at_least(struct isl_sched_node *node, int scc)
430 return node->scc >= scc;
433 /* An edge in the dependence graph. An edge may be used to
434 * ensure validity of the generated schedule, to minimize the dependence
435 * distance or both
437 * map is the dependence relation, with i -> j in the map if j depends on i
438 * tagged_condition and tagged_validity contain the union of all tagged
439 * condition or conditional validity dependence relations that
440 * specialize the dependence relation "map"; that is,
441 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
442 * or "tagged_validity", then i -> j is an element of "map".
443 * If these fields are NULL, then they represent the empty relation.
444 * src is the source node
445 * dst is the sink node
446 * validity is set if the edge is used to ensure correctness
447 * coincidence is used to enforce zero dependence distances
448 * proximity is set if the edge is used to minimize dependence distances
449 * condition is set if the edge represents a condition
450 * for a conditional validity schedule constraint
451 * local can only be set for condition edges and indicates that
452 * the dependence distance over the edge should be zero
453 * conditional_validity is set if the edge is used to conditionally
454 * ensure correctness
456 * For validity edges, start and end mark the sequence of inequality
457 * constraints in the LP problem that encode the validity constraint
458 * corresponding to this edge.
460 struct isl_sched_edge {
461 isl_map *map;
462 isl_union_map *tagged_condition;
463 isl_union_map *tagged_validity;
465 struct isl_sched_node *src;
466 struct isl_sched_node *dst;
468 unsigned validity : 1;
469 unsigned coincidence : 1;
470 unsigned proximity : 1;
471 unsigned local : 1;
472 unsigned condition : 1;
473 unsigned conditional_validity : 1;
475 int start;
476 int end;
479 /* Internal information about the dependence graph used during
480 * the construction of the schedule.
482 * intra_hmap is a cache, mapping dependence relations to their dual,
483 * for dependences from a node to itself
484 * inter_hmap is a cache, mapping dependence relations to their dual,
485 * for dependences between distinct nodes
486 * if compression is involved then the key for these maps
487 * it the original, uncompressed dependence relation, while
488 * the value is the dual of the compressed dependence relation.
490 * n is the number of nodes
491 * node is the list of nodes
492 * maxvar is the maximal number of variables over all nodes
493 * max_row is the allocated number of rows in the schedule
494 * n_row is the current (maximal) number of linearly independent
495 * rows in the node schedules
496 * n_total_row is the current number of rows in the node schedules
497 * band_start is the starting row in the node schedules of the current band
498 * root is set if this graph is the original dependence graph,
499 * without any splitting
501 * sorted contains a list of node indices sorted according to the
502 * SCC to which a node belongs
504 * n_edge is the number of edges
505 * edge is the list of edges
506 * max_edge contains the maximal number of edges of each type;
507 * in particular, it contains the number of edges in the inital graph.
508 * edge_table contains pointers into the edge array, hashed on the source
509 * and sink spaces; there is one such table for each type;
510 * a given edge may be referenced from more than one table
511 * if the corresponding relation appears in more than of the
512 * sets of dependences
514 * node_table contains pointers into the node array, hashed on the space
516 * region contains a list of variable sequences that should be non-trivial
518 * lp contains the (I)LP problem used to obtain new schedule rows
520 * src_scc and dst_scc are the source and sink SCCs of an edge with
521 * conflicting constraints
523 * scc represents the number of components
524 * weak is set if the components are weakly connected
526 struct isl_sched_graph {
527 isl_map_to_basic_set *intra_hmap;
528 isl_map_to_basic_set *inter_hmap;
530 struct isl_sched_node *node;
531 int n;
532 int maxvar;
533 int max_row;
534 int n_row;
536 int *sorted;
538 int n_total_row;
539 int band_start;
541 int root;
543 struct isl_sched_edge *edge;
544 int n_edge;
545 int max_edge[isl_edge_last + 1];
546 struct isl_hash_table *edge_table[isl_edge_last + 1];
548 struct isl_hash_table *node_table;
549 struct isl_region *region;
551 isl_basic_set *lp;
553 int src_scc;
554 int dst_scc;
556 int scc;
557 int weak;
560 /* Initialize node_table based on the list of nodes.
562 static int graph_init_table(isl_ctx *ctx, struct isl_sched_graph *graph)
564 int i;
566 graph->node_table = isl_hash_table_alloc(ctx, graph->n);
567 if (!graph->node_table)
568 return -1;
570 for (i = 0; i < graph->n; ++i) {
571 struct isl_hash_table_entry *entry;
572 uint32_t hash;
574 hash = isl_space_get_hash(graph->node[i].space);
575 entry = isl_hash_table_find(ctx, graph->node_table, hash,
576 &node_has_space,
577 graph->node[i].space, 1);
578 if (!entry)
579 return -1;
580 entry->data = &graph->node[i];
583 return 0;
586 /* Return a pointer to the node that lives within the given space,
587 * or NULL if there is no such node.
589 static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
590 struct isl_sched_graph *graph, __isl_keep isl_space *dim)
592 struct isl_hash_table_entry *entry;
593 uint32_t hash;
595 hash = isl_space_get_hash(dim);
596 entry = isl_hash_table_find(ctx, graph->node_table, hash,
597 &node_has_space, dim, 0);
599 return entry ? entry->data : NULL;
602 static int edge_has_src_and_dst(const void *entry, const void *val)
604 const struct isl_sched_edge *edge = entry;
605 const struct isl_sched_edge *temp = val;
607 return edge->src == temp->src && edge->dst == temp->dst;
610 /* Add the given edge to graph->edge_table[type].
612 static int graph_edge_table_add(isl_ctx *ctx, struct isl_sched_graph *graph,
613 enum isl_edge_type type, struct isl_sched_edge *edge)
615 struct isl_hash_table_entry *entry;
616 uint32_t hash;
618 hash = isl_hash_init();
619 hash = isl_hash_builtin(hash, edge->src);
620 hash = isl_hash_builtin(hash, edge->dst);
621 entry = isl_hash_table_find(ctx, graph->edge_table[type], hash,
622 &edge_has_src_and_dst, edge, 1);
623 if (!entry)
624 return -1;
625 entry->data = edge;
627 return 0;
630 /* Allocate the edge_tables based on the maximal number of edges of
631 * each type.
633 static int graph_init_edge_tables(isl_ctx *ctx, struct isl_sched_graph *graph)
635 int i;
637 for (i = 0; i <= isl_edge_last; ++i) {
638 graph->edge_table[i] = isl_hash_table_alloc(ctx,
639 graph->max_edge[i]);
640 if (!graph->edge_table[i])
641 return -1;
644 return 0;
647 /* If graph->edge_table[type] contains an edge from the given source
648 * to the given destination, then return the hash table entry of this edge.
649 * Otherwise, return NULL.
651 static struct isl_hash_table_entry *graph_find_edge_entry(
652 struct isl_sched_graph *graph,
653 enum isl_edge_type type,
654 struct isl_sched_node *src, struct isl_sched_node *dst)
656 isl_ctx *ctx = isl_space_get_ctx(src->space);
657 uint32_t hash;
658 struct isl_sched_edge temp = { .src = src, .dst = dst };
660 hash = isl_hash_init();
661 hash = isl_hash_builtin(hash, temp.src);
662 hash = isl_hash_builtin(hash, temp.dst);
663 return isl_hash_table_find(ctx, graph->edge_table[type], hash,
664 &edge_has_src_and_dst, &temp, 0);
668 /* If graph->edge_table[type] contains an edge from the given source
669 * to the given destination, then return this edge.
670 * Otherwise, return NULL.
672 static struct isl_sched_edge *graph_find_edge(struct isl_sched_graph *graph,
673 enum isl_edge_type type,
674 struct isl_sched_node *src, struct isl_sched_node *dst)
676 struct isl_hash_table_entry *entry;
678 entry = graph_find_edge_entry(graph, type, src, dst);
679 if (!entry)
680 return NULL;
682 return entry->data;
685 /* Check whether the dependence graph has an edge of the given type
686 * between the given two nodes.
688 static int graph_has_edge(struct isl_sched_graph *graph,
689 enum isl_edge_type type,
690 struct isl_sched_node *src, struct isl_sched_node *dst)
692 struct isl_sched_edge *edge;
693 int empty;
695 edge = graph_find_edge(graph, type, src, dst);
696 if (!edge)
697 return 0;
699 empty = isl_map_plain_is_empty(edge->map);
700 if (empty < 0)
701 return -1;
703 return !empty;
706 /* Look for any edge with the same src, dst and map fields as "model".
708 * Return the matching edge if one can be found.
709 * Return "model" if no matching edge is found.
710 * Return NULL on error.
712 static struct isl_sched_edge *graph_find_matching_edge(
713 struct isl_sched_graph *graph, struct isl_sched_edge *model)
715 enum isl_edge_type i;
716 struct isl_sched_edge *edge;
718 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
719 int is_equal;
721 edge = graph_find_edge(graph, i, model->src, model->dst);
722 if (!edge)
723 continue;
724 is_equal = isl_map_plain_is_equal(model->map, edge->map);
725 if (is_equal < 0)
726 return NULL;
727 if (is_equal)
728 return edge;
731 return model;
734 /* Remove the given edge from all the edge_tables that refer to it.
736 static void graph_remove_edge(struct isl_sched_graph *graph,
737 struct isl_sched_edge *edge)
739 isl_ctx *ctx = isl_map_get_ctx(edge->map);
740 enum isl_edge_type i;
742 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
743 struct isl_hash_table_entry *entry;
745 entry = graph_find_edge_entry(graph, i, edge->src, edge->dst);
746 if (!entry)
747 continue;
748 if (entry->data != edge)
749 continue;
750 isl_hash_table_remove(ctx, graph->edge_table[i], entry);
754 /* Check whether the dependence graph has any edge
755 * between the given two nodes.
757 static int graph_has_any_edge(struct isl_sched_graph *graph,
758 struct isl_sched_node *src, struct isl_sched_node *dst)
760 enum isl_edge_type i;
761 int r;
763 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
764 r = graph_has_edge(graph, i, src, dst);
765 if (r < 0 || r)
766 return r;
769 return r;
772 /* Check whether the dependence graph has a validity edge
773 * between the given two nodes.
775 * Conditional validity edges are essentially validity edges that
776 * can be ignored if the corresponding condition edges are iteration private.
777 * Here, we are only checking for the presence of validity
778 * edges, so we need to consider the conditional validity edges too.
779 * In particular, this function is used during the detection
780 * of strongly connected components and we cannot ignore
781 * conditional validity edges during this detection.
783 static int graph_has_validity_edge(struct isl_sched_graph *graph,
784 struct isl_sched_node *src, struct isl_sched_node *dst)
786 int r;
788 r = graph_has_edge(graph, isl_edge_validity, src, dst);
789 if (r < 0 || r)
790 return r;
792 return graph_has_edge(graph, isl_edge_conditional_validity, src, dst);
795 static int graph_alloc(isl_ctx *ctx, struct isl_sched_graph *graph,
796 int n_node, int n_edge)
798 int i;
800 graph->n = n_node;
801 graph->n_edge = n_edge;
802 graph->node = isl_calloc_array(ctx, struct isl_sched_node, graph->n);
803 graph->sorted = isl_calloc_array(ctx, int, graph->n);
804 graph->region = isl_alloc_array(ctx, struct isl_region, graph->n);
805 graph->edge = isl_calloc_array(ctx,
806 struct isl_sched_edge, graph->n_edge);
808 graph->intra_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
809 graph->inter_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
811 if (!graph->node || !graph->region || (graph->n_edge && !graph->edge) ||
812 !graph->sorted)
813 return -1;
815 for(i = 0; i < graph->n; ++i)
816 graph->sorted[i] = i;
818 return 0;
821 static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
823 int i;
825 isl_map_to_basic_set_free(graph->intra_hmap);
826 isl_map_to_basic_set_free(graph->inter_hmap);
828 if (graph->node)
829 for (i = 0; i < graph->n; ++i) {
830 isl_space_free(graph->node[i].space);
831 isl_set_free(graph->node[i].hull);
832 isl_multi_aff_free(graph->node[i].compress);
833 isl_multi_aff_free(graph->node[i].decompress);
834 isl_mat_free(graph->node[i].sched);
835 isl_map_free(graph->node[i].sched_map);
836 isl_mat_free(graph->node[i].cmap);
837 isl_mat_free(graph->node[i].cinv);
838 if (graph->root)
839 free(graph->node[i].coincident);
841 free(graph->node);
842 free(graph->sorted);
843 if (graph->edge)
844 for (i = 0; i < graph->n_edge; ++i) {
845 isl_map_free(graph->edge[i].map);
846 isl_union_map_free(graph->edge[i].tagged_condition);
847 isl_union_map_free(graph->edge[i].tagged_validity);
849 free(graph->edge);
850 free(graph->region);
851 for (i = 0; i <= isl_edge_last; ++i)
852 isl_hash_table_free(ctx, graph->edge_table[i]);
853 isl_hash_table_free(ctx, graph->node_table);
854 isl_basic_set_free(graph->lp);
857 /* For each "set" on which this function is called, increment
858 * graph->n by one and update graph->maxvar.
860 static int init_n_maxvar(__isl_take isl_set *set, void *user)
862 struct isl_sched_graph *graph = user;
863 int nvar = isl_set_dim(set, isl_dim_set);
865 graph->n++;
866 if (nvar > graph->maxvar)
867 graph->maxvar = nvar;
869 isl_set_free(set);
871 return 0;
874 /* Add the number of basic maps in "map" to *n.
876 static int add_n_basic_map(__isl_take isl_map *map, void *user)
878 int *n = user;
880 *n += isl_map_n_basic_map(map);
881 isl_map_free(map);
883 return 0;
886 /* Compute the number of rows that should be allocated for the schedule.
887 * In particular, we need one row for each variable or one row
888 * for each basic map in the dependences.
889 * Note that it is practically impossible to exhaust both
890 * the number of dependences and the number of variables.
892 static int compute_max_row(struct isl_sched_graph *graph,
893 __isl_keep isl_schedule_constraints *sc)
895 enum isl_edge_type i;
896 int n_edge;
898 graph->n = 0;
899 graph->maxvar = 0;
900 if (isl_union_set_foreach_set(sc->domain, &init_n_maxvar, graph) < 0)
901 return -1;
902 n_edge = 0;
903 for (i = isl_edge_first; i <= isl_edge_last; ++i)
904 if (isl_union_map_foreach_map(sc->constraint[i],
905 &add_n_basic_map, &n_edge) < 0)
906 return -1;
907 graph->max_row = n_edge + graph->maxvar;
909 return 0;
912 /* Does "bset" have any defining equalities for its set variables?
914 static int has_any_defining_equality(__isl_keep isl_basic_set *bset)
916 int i, n;
918 if (!bset)
919 return -1;
921 n = isl_basic_set_dim(bset, isl_dim_set);
922 for (i = 0; i < n; ++i) {
923 int has;
925 has = isl_basic_set_has_defining_equality(bset, isl_dim_set, i,
926 NULL);
927 if (has < 0 || has)
928 return has;
931 return 0;
934 /* Add a new node to the graph representing the given space.
935 * "nvar" is the (possibly compressed) number of variables and
936 * may be smaller than then number of set variables in "space"
937 * if "compressed" is set.
938 * If "compressed" is set, then "hull" represents the constraints
939 * that were used to derive the compression, while "compress" and
940 * "decompress" map the original space to the compressed space and
941 * vice versa.
942 * If "compressed" is not set, then "hull", "compress" and "decompress"
943 * should be NULL.
945 static int add_node(struct isl_sched_graph *graph, __isl_take isl_space *space,
946 int nvar, int compressed, __isl_take isl_set *hull,
947 __isl_take isl_multi_aff *compress,
948 __isl_take isl_multi_aff *decompress)
950 int nparam;
951 isl_ctx *ctx;
952 isl_mat *sched;
953 int *coincident;
955 if (!space)
956 return -1;
958 ctx = isl_space_get_ctx(space);
959 nparam = isl_space_dim(space, isl_dim_param);
960 if (!ctx->opt->schedule_parametric)
961 nparam = 0;
962 sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
963 graph->node[graph->n].space = space;
964 graph->node[graph->n].nvar = nvar;
965 graph->node[graph->n].nparam = nparam;
966 graph->node[graph->n].sched = sched;
967 graph->node[graph->n].sched_map = NULL;
968 coincident = isl_calloc_array(ctx, int, graph->max_row);
969 graph->node[graph->n].coincident = coincident;
970 graph->node[graph->n].compressed = compressed;
971 graph->node[graph->n].hull = hull;
972 graph->node[graph->n].compress = compress;
973 graph->node[graph->n].decompress = decompress;
974 graph->n++;
976 if (!space || !sched || (graph->max_row && !coincident))
977 return -1;
978 if (compressed && (!hull || !compress || !decompress))
979 return -1;
981 return 0;
984 /* Add a new node to the graph representing the given set.
986 * If any of the set variables is defined by an equality, then
987 * we perform variable compression such that we can perform
988 * the scheduling on the compressed domain.
990 static int extract_node(__isl_take isl_set *set, void *user)
992 int nvar;
993 int has_equality;
994 isl_space *space;
995 isl_basic_set *hull;
996 isl_set *hull_set;
997 isl_morph *morph;
998 isl_multi_aff *compress, *decompress;
999 struct isl_sched_graph *graph = user;
1001 space = isl_set_get_space(set);
1002 hull = isl_set_affine_hull(set);
1003 hull = isl_basic_set_remove_divs(hull);
1004 nvar = isl_space_dim(space, isl_dim_set);
1005 has_equality = has_any_defining_equality(hull);
1007 if (has_equality < 0)
1008 goto error;
1009 if (!has_equality) {
1010 isl_basic_set_free(hull);
1011 return add_node(graph, space, nvar, 0, NULL, NULL, NULL);
1014 morph = isl_basic_set_variable_compression(hull, isl_dim_set);
1015 nvar = isl_morph_ran_dim(morph, isl_dim_set);
1016 compress = isl_morph_get_var_multi_aff(morph);
1017 morph = isl_morph_inverse(morph);
1018 decompress = isl_morph_get_var_multi_aff(morph);
1019 isl_morph_free(morph);
1021 hull_set = isl_set_from_basic_set(hull);
1022 return add_node(graph, space, nvar, 1, hull_set, compress, decompress);
1023 error:
1024 isl_basic_set_free(hull);
1025 isl_space_free(space);
1026 return -1;
1029 struct isl_extract_edge_data {
1030 enum isl_edge_type type;
1031 struct isl_sched_graph *graph;
1034 /* Merge edge2 into edge1, freeing the contents of edge2.
1035 * "type" is the type of the schedule constraint from which edge2 was
1036 * extracted.
1037 * Return 0 on success and -1 on failure.
1039 * edge1 and edge2 are assumed to have the same value for the map field.
1041 static int merge_edge(enum isl_edge_type type, struct isl_sched_edge *edge1,
1042 struct isl_sched_edge *edge2)
1044 edge1->validity |= edge2->validity;
1045 edge1->coincidence |= edge2->coincidence;
1046 edge1->proximity |= edge2->proximity;
1047 edge1->condition |= edge2->condition;
1048 edge1->conditional_validity |= edge2->conditional_validity;
1049 isl_map_free(edge2->map);
1051 if (type == isl_edge_condition) {
1052 if (!edge1->tagged_condition)
1053 edge1->tagged_condition = edge2->tagged_condition;
1054 else
1055 edge1->tagged_condition =
1056 isl_union_map_union(edge1->tagged_condition,
1057 edge2->tagged_condition);
1060 if (type == isl_edge_conditional_validity) {
1061 if (!edge1->tagged_validity)
1062 edge1->tagged_validity = edge2->tagged_validity;
1063 else
1064 edge1->tagged_validity =
1065 isl_union_map_union(edge1->tagged_validity,
1066 edge2->tagged_validity);
1069 if (type == isl_edge_condition && !edge1->tagged_condition)
1070 return -1;
1071 if (type == isl_edge_conditional_validity && !edge1->tagged_validity)
1072 return -1;
1074 return 0;
1077 /* Insert dummy tags in domain and range of "map".
1079 * In particular, if "map" is of the form
1081 * A -> B
1083 * then return
1085 * [A -> dummy_tag] -> [B -> dummy_tag]
1087 * where the dummy_tags are identical and equal to any dummy tags
1088 * introduced by any other call to this function.
1090 static __isl_give isl_map *insert_dummy_tags(__isl_take isl_map *map)
1092 static char dummy;
1093 isl_ctx *ctx;
1094 isl_id *id;
1095 isl_space *space;
1096 isl_set *domain, *range;
1098 ctx = isl_map_get_ctx(map);
1100 id = isl_id_alloc(ctx, NULL, &dummy);
1101 space = isl_space_params(isl_map_get_space(map));
1102 space = isl_space_set_from_params(space);
1103 space = isl_space_set_tuple_id(space, isl_dim_set, id);
1104 space = isl_space_map_from_set(space);
1106 domain = isl_map_wrap(map);
1107 range = isl_map_wrap(isl_map_universe(space));
1108 map = isl_map_from_domain_and_range(domain, range);
1109 map = isl_map_zip(map);
1111 return map;
1114 /* Given that at least one of "src" or "dst" is compressed, return
1115 * a map between the spaces of these nodes restricted to the affine
1116 * hull that was used in the compression.
1118 static __isl_give isl_map *extract_hull(struct isl_sched_node *src,
1119 struct isl_sched_node *dst)
1121 isl_set *dom, *ran;
1123 if (src->compressed)
1124 dom = isl_set_copy(src->hull);
1125 else
1126 dom = isl_set_universe(isl_space_copy(src->space));
1127 if (dst->compressed)
1128 ran = isl_set_copy(dst->hull);
1129 else
1130 ran = isl_set_universe(isl_space_copy(dst->space));
1132 return isl_map_from_domain_and_range(dom, ran);
1135 /* Intersect the domains of the nested relations in domain and range
1136 * of "tagged" with "map".
1138 static __isl_give isl_map *map_intersect_domains(__isl_take isl_map *tagged,
1139 __isl_keep isl_map *map)
1141 isl_set *set;
1143 tagged = isl_map_zip(tagged);
1144 set = isl_map_wrap(isl_map_copy(map));
1145 tagged = isl_map_intersect_domain(tagged, set);
1146 tagged = isl_map_zip(tagged);
1147 return tagged;
1150 /* Add a new edge to the graph based on the given map
1151 * and add it to data->graph->edge_table[data->type].
1152 * If a dependence relation of a given type happens to be identical
1153 * to one of the dependence relations of a type that was added before,
1154 * then we don't create a new edge, but instead mark the original edge
1155 * as also representing a dependence of the current type.
1157 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1158 * may be specified as "tagged" dependence relations. That is, "map"
1159 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1160 * the dependence on iterations and a and b are tags.
1161 * edge->map is set to the relation containing the elements i -> j,
1162 * while edge->tagged_condition and edge->tagged_validity contain
1163 * the union of all the "map" relations
1164 * for which extract_edge is called that result in the same edge->map.
1166 * If the source or the destination node is compressed, then
1167 * intersect both "map" and "tagged" with the constraints that
1168 * were used to construct the compression.
1169 * This ensures that there are no schedule constraints defined
1170 * outside of these domains, while the scheduler no longer has
1171 * any control over those outside parts.
1173 static int extract_edge(__isl_take isl_map *map, void *user)
1175 isl_ctx *ctx = isl_map_get_ctx(map);
1176 struct isl_extract_edge_data *data = user;
1177 struct isl_sched_graph *graph = data->graph;
1178 struct isl_sched_node *src, *dst;
1179 isl_space *dim;
1180 struct isl_sched_edge *edge;
1181 isl_map *tagged = NULL;
1183 if (data->type == isl_edge_condition ||
1184 data->type == isl_edge_conditional_validity) {
1185 if (isl_map_can_zip(map)) {
1186 tagged = isl_map_copy(map);
1187 map = isl_set_unwrap(isl_map_domain(isl_map_zip(map)));
1188 } else {
1189 tagged = insert_dummy_tags(isl_map_copy(map));
1193 dim = isl_space_domain(isl_map_get_space(map));
1194 src = graph_find_node(ctx, graph, dim);
1195 isl_space_free(dim);
1196 dim = isl_space_range(isl_map_get_space(map));
1197 dst = graph_find_node(ctx, graph, dim);
1198 isl_space_free(dim);
1200 if (!src || !dst) {
1201 isl_map_free(map);
1202 isl_map_free(tagged);
1203 return 0;
1206 if (src->compressed || dst->compressed) {
1207 isl_map *hull;
1208 hull = extract_hull(src, dst);
1209 if (tagged)
1210 tagged = map_intersect_domains(tagged, hull);
1211 map = isl_map_intersect(map, hull);
1214 graph->edge[graph->n_edge].src = src;
1215 graph->edge[graph->n_edge].dst = dst;
1216 graph->edge[graph->n_edge].map = map;
1217 graph->edge[graph->n_edge].validity = 0;
1218 graph->edge[graph->n_edge].coincidence = 0;
1219 graph->edge[graph->n_edge].proximity = 0;
1220 graph->edge[graph->n_edge].condition = 0;
1221 graph->edge[graph->n_edge].local = 0;
1222 graph->edge[graph->n_edge].conditional_validity = 0;
1223 graph->edge[graph->n_edge].tagged_condition = NULL;
1224 graph->edge[graph->n_edge].tagged_validity = NULL;
1225 if (data->type == isl_edge_validity)
1226 graph->edge[graph->n_edge].validity = 1;
1227 if (data->type == isl_edge_coincidence)
1228 graph->edge[graph->n_edge].coincidence = 1;
1229 if (data->type == isl_edge_proximity)
1230 graph->edge[graph->n_edge].proximity = 1;
1231 if (data->type == isl_edge_condition) {
1232 graph->edge[graph->n_edge].condition = 1;
1233 graph->edge[graph->n_edge].tagged_condition =
1234 isl_union_map_from_map(tagged);
1236 if (data->type == isl_edge_conditional_validity) {
1237 graph->edge[graph->n_edge].conditional_validity = 1;
1238 graph->edge[graph->n_edge].tagged_validity =
1239 isl_union_map_from_map(tagged);
1242 edge = graph_find_matching_edge(graph, &graph->edge[graph->n_edge]);
1243 if (!edge) {
1244 graph->n_edge++;
1245 return -1;
1247 if (edge == &graph->edge[graph->n_edge])
1248 return graph_edge_table_add(ctx, graph, data->type,
1249 &graph->edge[graph->n_edge++]);
1251 if (merge_edge(data->type, edge, &graph->edge[graph->n_edge]) < 0)
1252 return -1;
1254 return graph_edge_table_add(ctx, graph, data->type, edge);
1257 /* Check whether there is any dependence from node[j] to node[i]
1258 * or from node[i] to node[j].
1260 static int node_follows_weak(int i, int j, void *user)
1262 int f;
1263 struct isl_sched_graph *graph = user;
1265 f = graph_has_any_edge(graph, &graph->node[j], &graph->node[i]);
1266 if (f < 0 || f)
1267 return f;
1268 return graph_has_any_edge(graph, &graph->node[i], &graph->node[j]);
1271 /* Check whether there is a (conditional) validity dependence from node[j]
1272 * to node[i], forcing node[i] to follow node[j].
1274 static int node_follows_strong(int i, int j, void *user)
1276 struct isl_sched_graph *graph = user;
1278 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
1281 /* Use Tarjan's algorithm for computing the strongly connected components
1282 * in the dependence graph (only validity edges).
1283 * If weak is set, we consider the graph to be undirected and
1284 * we effectively compute the (weakly) connected components.
1285 * Additionally, we also consider other edges when weak is set.
1287 static int detect_ccs(isl_ctx *ctx, struct isl_sched_graph *graph, int weak)
1289 int i, n;
1290 struct isl_tarjan_graph *g = NULL;
1292 g = isl_tarjan_graph_init(ctx, graph->n,
1293 weak ? &node_follows_weak : &node_follows_strong, graph);
1294 if (!g)
1295 return -1;
1297 graph->weak = weak;
1298 graph->scc = 0;
1299 i = 0;
1300 n = graph->n;
1301 while (n) {
1302 while (g->order[i] != -1) {
1303 graph->node[g->order[i]].scc = graph->scc;
1304 --n;
1305 ++i;
1307 ++i;
1308 graph->scc++;
1311 isl_tarjan_graph_free(g);
1313 return 0;
1316 /* Apply Tarjan's algorithm to detect the strongly connected components
1317 * in the dependence graph.
1319 static int detect_sccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1321 return detect_ccs(ctx, graph, 0);
1324 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1325 * in the dependence graph.
1327 static int detect_wccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1329 return detect_ccs(ctx, graph, 1);
1332 static int cmp_scc(const void *a, const void *b, void *data)
1334 struct isl_sched_graph *graph = data;
1335 const int *i1 = a;
1336 const int *i2 = b;
1338 return graph->node[*i1].scc - graph->node[*i2].scc;
1341 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1343 static int sort_sccs(struct isl_sched_graph *graph)
1345 return isl_sort(graph->sorted, graph->n, sizeof(int), &cmp_scc, graph);
1348 /* Given a dependence relation R from "node" to itself,
1349 * construct the set of coefficients of valid constraints for elements
1350 * in that dependence relation.
1351 * In particular, the result contains tuples of coefficients
1352 * c_0, c_n, c_x such that
1354 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1356 * or, equivalently,
1358 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1360 * We choose here to compute the dual of delta R.
1361 * Alternatively, we could have computed the dual of R, resulting
1362 * in a set of tuples c_0, c_n, c_x, c_y, and then
1363 * plugged in (c_0, c_n, c_x, -c_x).
1365 * If "node" has been compressed, then the dependence relation
1366 * is also compressed before the set of coefficients is computed.
1368 static __isl_give isl_basic_set *intra_coefficients(
1369 struct isl_sched_graph *graph, struct isl_sched_node *node,
1370 __isl_take isl_map *map)
1372 isl_set *delta;
1373 isl_map *key;
1374 isl_basic_set *coef;
1376 if (isl_map_to_basic_set_has(graph->intra_hmap, map))
1377 return isl_map_to_basic_set_get(graph->intra_hmap, map);
1379 key = isl_map_copy(map);
1380 if (node->compressed) {
1381 map = isl_map_preimage_domain_multi_aff(map,
1382 isl_multi_aff_copy(node->decompress));
1383 map = isl_map_preimage_range_multi_aff(map,
1384 isl_multi_aff_copy(node->decompress));
1386 delta = isl_set_remove_divs(isl_map_deltas(map));
1387 coef = isl_set_coefficients(delta);
1388 graph->intra_hmap = isl_map_to_basic_set_set(graph->intra_hmap, key,
1389 isl_basic_set_copy(coef));
1391 return coef;
1394 /* Given a dependence relation R, construct the set of coefficients
1395 * of valid constraints for elements in that dependence relation.
1396 * In particular, the result contains tuples of coefficients
1397 * c_0, c_n, c_x, c_y such that
1399 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1401 * If the source or destination nodes of "edge" have been compressed,
1402 * then the dependence relation is also compressed before
1403 * the set of coefficients is computed.
1405 static __isl_give isl_basic_set *inter_coefficients(
1406 struct isl_sched_graph *graph, struct isl_sched_edge *edge,
1407 __isl_take isl_map *map)
1409 isl_set *set;
1410 isl_map *key;
1411 isl_basic_set *coef;
1413 if (isl_map_to_basic_set_has(graph->inter_hmap, map))
1414 return isl_map_to_basic_set_get(graph->inter_hmap, map);
1416 key = isl_map_copy(map);
1417 if (edge->src->compressed)
1418 map = isl_map_preimage_domain_multi_aff(map,
1419 isl_multi_aff_copy(edge->src->decompress));
1420 if (edge->dst->compressed)
1421 map = isl_map_preimage_range_multi_aff(map,
1422 isl_multi_aff_copy(edge->dst->decompress));
1423 set = isl_map_wrap(isl_map_remove_divs(map));
1424 coef = isl_set_coefficients(set);
1425 graph->inter_hmap = isl_map_to_basic_set_set(graph->inter_hmap, key,
1426 isl_basic_set_copy(coef));
1428 return coef;
1431 /* Add constraints to graph->lp that force validity for the given
1432 * dependence from a node i to itself.
1433 * That is, add constraints that enforce
1435 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1436 * = c_i_x (y - x) >= 0
1438 * for each (x,y) in R.
1439 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1440 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1441 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1442 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1444 * Actually, we do not construct constraints for the c_i_x themselves,
1445 * but for the coefficients of c_i_x written as a linear combination
1446 * of the columns in node->cmap.
1448 static int add_intra_validity_constraints(struct isl_sched_graph *graph,
1449 struct isl_sched_edge *edge)
1451 unsigned total;
1452 isl_map *map = isl_map_copy(edge->map);
1453 isl_ctx *ctx = isl_map_get_ctx(map);
1454 isl_space *dim;
1455 isl_dim_map *dim_map;
1456 isl_basic_set *coef;
1457 struct isl_sched_node *node = edge->src;
1459 coef = intra_coefficients(graph, node, map);
1461 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1463 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1464 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
1465 if (!coef)
1466 goto error;
1468 total = isl_basic_set_total_dim(graph->lp);
1469 dim_map = isl_dim_map_alloc(ctx, total);
1470 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
1471 isl_space_dim(dim, isl_dim_set), 1,
1472 node->nvar, -1);
1473 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
1474 isl_space_dim(dim, isl_dim_set), 1,
1475 node->nvar, 1);
1476 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1477 coef->n_eq, coef->n_ineq);
1478 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1479 coef, dim_map);
1480 isl_space_free(dim);
1482 return 0;
1483 error:
1484 isl_space_free(dim);
1485 return -1;
1488 /* Add constraints to graph->lp that force validity for the given
1489 * dependence from node i to node j.
1490 * That is, add constraints that enforce
1492 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1494 * for each (x,y) in R.
1495 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1496 * of valid constraints for R and then plug in
1497 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
1498 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1499 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1500 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1502 * Actually, we do not construct constraints for the c_*_x themselves,
1503 * but for the coefficients of c_*_x written as a linear combination
1504 * of the columns in node->cmap.
1506 static int add_inter_validity_constraints(struct isl_sched_graph *graph,
1507 struct isl_sched_edge *edge)
1509 unsigned total;
1510 isl_map *map = isl_map_copy(edge->map);
1511 isl_ctx *ctx = isl_map_get_ctx(map);
1512 isl_space *dim;
1513 isl_dim_map *dim_map;
1514 isl_basic_set *coef;
1515 struct isl_sched_node *src = edge->src;
1516 struct isl_sched_node *dst = edge->dst;
1518 coef = inter_coefficients(graph, edge, map);
1520 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1522 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1523 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1524 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1525 isl_space_dim(dim, isl_dim_set) + src->nvar,
1526 isl_mat_copy(dst->cmap));
1527 if (!coef)
1528 goto error;
1530 total = isl_basic_set_total_dim(graph->lp);
1531 dim_map = isl_dim_map_alloc(ctx, total);
1533 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
1534 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
1535 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
1536 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1537 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1538 dst->nvar, -1);
1539 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1540 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1541 dst->nvar, 1);
1543 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
1544 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
1545 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
1546 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1547 isl_space_dim(dim, isl_dim_set), 1,
1548 src->nvar, 1);
1549 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1550 isl_space_dim(dim, isl_dim_set), 1,
1551 src->nvar, -1);
1553 edge->start = graph->lp->n_ineq;
1554 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1555 coef->n_eq, coef->n_ineq);
1556 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1557 coef, dim_map);
1558 if (!graph->lp)
1559 goto error;
1560 isl_space_free(dim);
1561 edge->end = graph->lp->n_ineq;
1563 return 0;
1564 error:
1565 isl_space_free(dim);
1566 return -1;
1569 /* Add constraints to graph->lp that bound the dependence distance for the given
1570 * dependence from a node i to itself.
1571 * If s = 1, we add the constraint
1573 * c_i_x (y - x) <= m_0 + m_n n
1575 * or
1577 * -c_i_x (y - x) + m_0 + m_n n >= 0
1579 * for each (x,y) in R.
1580 * If s = -1, we add the constraint
1582 * -c_i_x (y - x) <= m_0 + m_n n
1584 * or
1586 * c_i_x (y - x) + m_0 + m_n n >= 0
1588 * for each (x,y) in R.
1589 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1590 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1591 * with each coefficient (except m_0) represented as a pair of non-negative
1592 * coefficients.
1594 * Actually, we do not construct constraints for the c_i_x themselves,
1595 * but for the coefficients of c_i_x written as a linear combination
1596 * of the columns in node->cmap.
1599 * If "local" is set, then we add constraints
1601 * c_i_x (y - x) <= 0
1603 * or
1605 * -c_i_x (y - x) <= 0
1607 * instead, forcing the dependence distance to be (less than or) equal to 0.
1608 * That is, we plug in (0, 0, -s * c_i_x),
1609 * Note that dependences marked local are treated as validity constraints
1610 * by add_all_validity_constraints and therefore also have
1611 * their distances bounded by 0 from below.
1613 static int add_intra_proximity_constraints(struct isl_sched_graph *graph,
1614 struct isl_sched_edge *edge, int s, int local)
1616 unsigned total;
1617 unsigned nparam;
1618 isl_map *map = isl_map_copy(edge->map);
1619 isl_ctx *ctx = isl_map_get_ctx(map);
1620 isl_space *dim;
1621 isl_dim_map *dim_map;
1622 isl_basic_set *coef;
1623 struct isl_sched_node *node = edge->src;
1625 coef = intra_coefficients(graph, node, map);
1627 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1629 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1630 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
1631 if (!coef)
1632 goto error;
1634 nparam = isl_space_dim(node->space, isl_dim_param);
1635 total = isl_basic_set_total_dim(graph->lp);
1636 dim_map = isl_dim_map_alloc(ctx, total);
1638 if (!local) {
1639 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1640 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1641 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1643 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
1644 isl_space_dim(dim, isl_dim_set), 1,
1645 node->nvar, s);
1646 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
1647 isl_space_dim(dim, isl_dim_set), 1,
1648 node->nvar, -s);
1649 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1650 coef->n_eq, coef->n_ineq);
1651 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1652 coef, dim_map);
1653 isl_space_free(dim);
1655 return 0;
1656 error:
1657 isl_space_free(dim);
1658 return -1;
1661 /* Add constraints to graph->lp that bound the dependence distance for the given
1662 * dependence from node i to node j.
1663 * If s = 1, we add the constraint
1665 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1666 * <= m_0 + m_n n
1668 * or
1670 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1671 * m_0 + m_n n >= 0
1673 * for each (x,y) in R.
1674 * If s = -1, we add the constraint
1676 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1677 * <= m_0 + m_n n
1679 * or
1681 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1682 * m_0 + m_n n >= 0
1684 * for each (x,y) in R.
1685 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1686 * of valid constraints for R and then plug in
1687 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1688 * -s*c_j_x+s*c_i_x)
1689 * with each coefficient (except m_0, c_j_0 and c_i_0)
1690 * represented as a pair of non-negative coefficients.
1692 * Actually, we do not construct constraints for the c_*_x themselves,
1693 * but for the coefficients of c_*_x written as a linear combination
1694 * of the columns in node->cmap.
1697 * If "local" is set, then we add constraints
1699 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1701 * or
1703 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
1705 * instead, forcing the dependence distance to be (less than or) equal to 0.
1706 * That is, we plug in
1707 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
1708 * Note that dependences marked local are treated as validity constraints
1709 * by add_all_validity_constraints and therefore also have
1710 * their distances bounded by 0 from below.
1712 static int add_inter_proximity_constraints(struct isl_sched_graph *graph,
1713 struct isl_sched_edge *edge, int s, int local)
1715 unsigned total;
1716 unsigned nparam;
1717 isl_map *map = isl_map_copy(edge->map);
1718 isl_ctx *ctx = isl_map_get_ctx(map);
1719 isl_space *dim;
1720 isl_dim_map *dim_map;
1721 isl_basic_set *coef;
1722 struct isl_sched_node *src = edge->src;
1723 struct isl_sched_node *dst = edge->dst;
1725 coef = inter_coefficients(graph, edge, map);
1727 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1729 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1730 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1731 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1732 isl_space_dim(dim, isl_dim_set) + src->nvar,
1733 isl_mat_copy(dst->cmap));
1734 if (!coef)
1735 goto error;
1737 nparam = isl_space_dim(src->space, isl_dim_param);
1738 total = isl_basic_set_total_dim(graph->lp);
1739 dim_map = isl_dim_map_alloc(ctx, total);
1741 if (!local) {
1742 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1743 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1744 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1747 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, -s);
1748 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, s);
1749 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, -s);
1750 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1751 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1752 dst->nvar, s);
1753 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1754 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1755 dst->nvar, -s);
1757 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, s);
1758 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, -s);
1759 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, s);
1760 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1761 isl_space_dim(dim, isl_dim_set), 1,
1762 src->nvar, -s);
1763 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1764 isl_space_dim(dim, isl_dim_set), 1,
1765 src->nvar, s);
1767 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1768 coef->n_eq, coef->n_ineq);
1769 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1770 coef, dim_map);
1771 isl_space_free(dim);
1773 return 0;
1774 error:
1775 isl_space_free(dim);
1776 return -1;
1779 /* Add all validity constraints to graph->lp.
1781 * An edge that is forced to be local needs to have its dependence
1782 * distances equal to zero. We take care of bounding them by 0 from below
1783 * here. add_all_proximity_constraints takes care of bounding them by 0
1784 * from above.
1786 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1787 * Otherwise, we ignore them.
1789 static int add_all_validity_constraints(struct isl_sched_graph *graph,
1790 int use_coincidence)
1792 int i;
1794 for (i = 0; i < graph->n_edge; ++i) {
1795 struct isl_sched_edge *edge= &graph->edge[i];
1796 int local;
1798 local = edge->local || (edge->coincidence && use_coincidence);
1799 if (!edge->validity && !local)
1800 continue;
1801 if (edge->src != edge->dst)
1802 continue;
1803 if (add_intra_validity_constraints(graph, edge) < 0)
1804 return -1;
1807 for (i = 0; i < graph->n_edge; ++i) {
1808 struct isl_sched_edge *edge = &graph->edge[i];
1809 int local;
1811 local = edge->local || (edge->coincidence && use_coincidence);
1812 if (!edge->validity && !local)
1813 continue;
1814 if (edge->src == edge->dst)
1815 continue;
1816 if (add_inter_validity_constraints(graph, edge) < 0)
1817 return -1;
1820 return 0;
1823 /* Add constraints to graph->lp that bound the dependence distance
1824 * for all dependence relations.
1825 * If a given proximity dependence is identical to a validity
1826 * dependence, then the dependence distance is already bounded
1827 * from below (by zero), so we only need to bound the distance
1828 * from above. (This includes the case of "local" dependences
1829 * which are treated as validity dependence by add_all_validity_constraints.)
1830 * Otherwise, we need to bound the distance both from above and from below.
1832 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1833 * Otherwise, we ignore them.
1835 static int add_all_proximity_constraints(struct isl_sched_graph *graph,
1836 int use_coincidence)
1838 int i;
1840 for (i = 0; i < graph->n_edge; ++i) {
1841 struct isl_sched_edge *edge= &graph->edge[i];
1842 int local;
1844 local = edge->local || (edge->coincidence && use_coincidence);
1845 if (!edge->proximity && !local)
1846 continue;
1847 if (edge->src == edge->dst &&
1848 add_intra_proximity_constraints(graph, edge, 1, local) < 0)
1849 return -1;
1850 if (edge->src != edge->dst &&
1851 add_inter_proximity_constraints(graph, edge, 1, local) < 0)
1852 return -1;
1853 if (edge->validity || local)
1854 continue;
1855 if (edge->src == edge->dst &&
1856 add_intra_proximity_constraints(graph, edge, -1, 0) < 0)
1857 return -1;
1858 if (edge->src != edge->dst &&
1859 add_inter_proximity_constraints(graph, edge, -1, 0) < 0)
1860 return -1;
1863 return 0;
1866 /* Compute a basis for the rows in the linear part of the schedule
1867 * and extend this basis to a full basis. The remaining rows
1868 * can then be used to force linear independence from the rows
1869 * in the schedule.
1871 * In particular, given the schedule rows S, we compute
1873 * S = H Q
1874 * S U = H
1876 * with H the Hermite normal form of S. That is, all but the
1877 * first rank columns of H are zero and so each row in S is
1878 * a linear combination of the first rank rows of Q.
1879 * The matrix Q is then transposed because we will write the
1880 * coefficients of the next schedule row as a column vector s
1881 * and express this s as a linear combination s = Q c of the
1882 * computed basis.
1883 * Similarly, the matrix U is transposed such that we can
1884 * compute the coefficients c = U s from a schedule row s.
1886 static int node_update_cmap(struct isl_sched_node *node)
1888 isl_mat *H, *U, *Q;
1889 int n_row = isl_mat_rows(node->sched);
1891 H = isl_mat_sub_alloc(node->sched, 0, n_row,
1892 1 + node->nparam, node->nvar);
1894 H = isl_mat_left_hermite(H, 0, &U, &Q);
1895 isl_mat_free(node->cmap);
1896 isl_mat_free(node->cinv);
1897 node->cmap = isl_mat_transpose(Q);
1898 node->cinv = isl_mat_transpose(U);
1899 node->rank = isl_mat_initial_non_zero_cols(H);
1900 isl_mat_free(H);
1902 if (!node->cmap || !node->cinv || node->rank < 0)
1903 return -1;
1904 return 0;
1907 /* How many times should we count the constraints in "edge"?
1909 * If carry is set, then we are counting the number of
1910 * (validity or conditional validity) constraints that will be added
1911 * in setup_carry_lp and we count each edge exactly once.
1913 * Otherwise, we count as follows
1914 * validity -> 1 (>= 0)
1915 * validity+proximity -> 2 (>= 0 and upper bound)
1916 * proximity -> 2 (lower and upper bound)
1917 * local(+any) -> 2 (>= 0 and <= 0)
1919 * If an edge is only marked conditional_validity then it counts
1920 * as zero since it is only checked afterwards.
1922 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1923 * Otherwise, we ignore them.
1925 static int edge_multiplicity(struct isl_sched_edge *edge, int carry,
1926 int use_coincidence)
1928 if (carry && !edge->validity && !edge->conditional_validity)
1929 return 0;
1930 if (carry)
1931 return 1;
1932 if (edge->proximity || edge->local)
1933 return 2;
1934 if (use_coincidence && edge->coincidence)
1935 return 2;
1936 if (edge->validity)
1937 return 1;
1938 return 0;
1941 /* Count the number of equality and inequality constraints
1942 * that will be added for the given map.
1944 * "use_coincidence" is set if we should take into account coincidence edges.
1946 static int count_map_constraints(struct isl_sched_graph *graph,
1947 struct isl_sched_edge *edge, __isl_take isl_map *map,
1948 int *n_eq, int *n_ineq, int carry, int use_coincidence)
1950 isl_basic_set *coef;
1951 int f = edge_multiplicity(edge, carry, use_coincidence);
1953 if (f == 0) {
1954 isl_map_free(map);
1955 return 0;
1958 if (edge->src == edge->dst)
1959 coef = intra_coefficients(graph, edge->src, map);
1960 else
1961 coef = inter_coefficients(graph, edge, map);
1962 if (!coef)
1963 return -1;
1964 *n_eq += f * coef->n_eq;
1965 *n_ineq += f * coef->n_ineq;
1966 isl_basic_set_free(coef);
1968 return 0;
1971 /* Count the number of equality and inequality constraints
1972 * that will be added to the main lp problem.
1973 * We count as follows
1974 * validity -> 1 (>= 0)
1975 * validity+proximity -> 2 (>= 0 and upper bound)
1976 * proximity -> 2 (lower and upper bound)
1977 * local(+any) -> 2 (>= 0 and <= 0)
1979 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1980 * Otherwise, we ignore them.
1982 static int count_constraints(struct isl_sched_graph *graph,
1983 int *n_eq, int *n_ineq, int use_coincidence)
1985 int i;
1987 *n_eq = *n_ineq = 0;
1988 for (i = 0; i < graph->n_edge; ++i) {
1989 struct isl_sched_edge *edge= &graph->edge[i];
1990 isl_map *map = isl_map_copy(edge->map);
1992 if (count_map_constraints(graph, edge, map, n_eq, n_ineq,
1993 0, use_coincidence) < 0)
1994 return -1;
1997 return 0;
2000 /* Count the number of constraints that will be added by
2001 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
2002 * accordingly.
2004 * In practice, add_bound_coefficient_constraints only adds inequalities.
2006 static int count_bound_coefficient_constraints(isl_ctx *ctx,
2007 struct isl_sched_graph *graph, int *n_eq, int *n_ineq)
2009 int i;
2011 if (ctx->opt->schedule_max_coefficient == -1)
2012 return 0;
2014 for (i = 0; i < graph->n; ++i)
2015 *n_ineq += 2 * graph->node[i].nparam + 2 * graph->node[i].nvar;
2017 return 0;
2020 /* Add constraints that bound the values of the variable and parameter
2021 * coefficients of the schedule.
2023 * The maximal value of the coefficients is defined by the option
2024 * 'schedule_max_coefficient'.
2026 static int add_bound_coefficient_constraints(isl_ctx *ctx,
2027 struct isl_sched_graph *graph)
2029 int i, j, k;
2030 int max_coefficient;
2031 int total;
2033 max_coefficient = ctx->opt->schedule_max_coefficient;
2035 if (max_coefficient == -1)
2036 return 0;
2038 total = isl_basic_set_total_dim(graph->lp);
2040 for (i = 0; i < graph->n; ++i) {
2041 struct isl_sched_node *node = &graph->node[i];
2042 for (j = 0; j < 2 * node->nparam + 2 * node->nvar; ++j) {
2043 int dim;
2044 k = isl_basic_set_alloc_inequality(graph->lp);
2045 if (k < 0)
2046 return -1;
2047 dim = 1 + node->start + 1 + j;
2048 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2049 isl_int_set_si(graph->lp->ineq[k][dim], -1);
2050 isl_int_set_si(graph->lp->ineq[k][0], max_coefficient);
2054 return 0;
2057 /* Construct an ILP problem for finding schedule coefficients
2058 * that result in non-negative, but small dependence distances
2059 * over all dependences.
2060 * In particular, the dependence distances over proximity edges
2061 * are bounded by m_0 + m_n n and we compute schedule coefficients
2062 * with small values (preferably zero) of m_n and m_0.
2064 * All variables of the ILP are non-negative. The actual coefficients
2065 * may be negative, so each coefficient is represented as the difference
2066 * of two non-negative variables. The negative part always appears
2067 * immediately before the positive part.
2068 * Other than that, the variables have the following order
2070 * - sum of positive and negative parts of m_n coefficients
2071 * - m_0
2072 * - sum of positive and negative parts of all c_n coefficients
2073 * (unconstrained when computing non-parametric schedules)
2074 * - sum of positive and negative parts of all c_x coefficients
2075 * - positive and negative parts of m_n coefficients
2076 * - for each node
2077 * - c_i_0
2078 * - positive and negative parts of c_i_n (if parametric)
2079 * - positive and negative parts of c_i_x
2081 * The c_i_x are not represented directly, but through the columns of
2082 * node->cmap. That is, the computed values are for variable t_i_x
2083 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
2085 * The constraints are those from the edges plus two or three equalities
2086 * to express the sums.
2088 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2089 * Otherwise, we ignore them.
2091 static int setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
2092 int use_coincidence)
2094 int i, j;
2095 int k;
2096 unsigned nparam;
2097 unsigned total;
2098 isl_space *dim;
2099 int parametric;
2100 int param_pos;
2101 int n_eq, n_ineq;
2102 int max_constant_term;
2104 max_constant_term = ctx->opt->schedule_max_constant_term;
2106 parametric = ctx->opt->schedule_parametric;
2107 nparam = isl_space_dim(graph->node[0].space, isl_dim_param);
2108 param_pos = 4;
2109 total = param_pos + 2 * nparam;
2110 for (i = 0; i < graph->n; ++i) {
2111 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2112 if (node_update_cmap(node) < 0)
2113 return -1;
2114 node->start = total;
2115 total += 1 + 2 * (node->nparam + node->nvar);
2118 if (count_constraints(graph, &n_eq, &n_ineq, use_coincidence) < 0)
2119 return -1;
2120 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
2121 return -1;
2123 dim = isl_space_set_alloc(ctx, 0, total);
2124 isl_basic_set_free(graph->lp);
2125 n_eq += 2 + parametric;
2126 if (max_constant_term != -1)
2127 n_ineq += graph->n;
2129 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
2131 k = isl_basic_set_alloc_equality(graph->lp);
2132 if (k < 0)
2133 return -1;
2134 isl_seq_clr(graph->lp->eq[k], 1 + total);
2135 isl_int_set_si(graph->lp->eq[k][1], -1);
2136 for (i = 0; i < 2 * nparam; ++i)
2137 isl_int_set_si(graph->lp->eq[k][1 + param_pos + i], 1);
2139 if (parametric) {
2140 k = isl_basic_set_alloc_equality(graph->lp);
2141 if (k < 0)
2142 return -1;
2143 isl_seq_clr(graph->lp->eq[k], 1 + total);
2144 isl_int_set_si(graph->lp->eq[k][3], -1);
2145 for (i = 0; i < graph->n; ++i) {
2146 int pos = 1 + graph->node[i].start + 1;
2148 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
2149 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2153 k = isl_basic_set_alloc_equality(graph->lp);
2154 if (k < 0)
2155 return -1;
2156 isl_seq_clr(graph->lp->eq[k], 1 + total);
2157 isl_int_set_si(graph->lp->eq[k][4], -1);
2158 for (i = 0; i < graph->n; ++i) {
2159 struct isl_sched_node *node = &graph->node[i];
2160 int pos = 1 + node->start + 1 + 2 * node->nparam;
2162 for (j = 0; j < 2 * node->nvar; ++j)
2163 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2166 if (max_constant_term != -1)
2167 for (i = 0; i < graph->n; ++i) {
2168 struct isl_sched_node *node = &graph->node[i];
2169 k = isl_basic_set_alloc_inequality(graph->lp);
2170 if (k < 0)
2171 return -1;
2172 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2173 isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
2174 isl_int_set_si(graph->lp->ineq[k][0], max_constant_term);
2177 if (add_bound_coefficient_constraints(ctx, graph) < 0)
2178 return -1;
2179 if (add_all_validity_constraints(graph, use_coincidence) < 0)
2180 return -1;
2181 if (add_all_proximity_constraints(graph, use_coincidence) < 0)
2182 return -1;
2184 return 0;
2187 /* Analyze the conflicting constraint found by
2188 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2189 * constraint of one of the edges between distinct nodes, living, moreover
2190 * in distinct SCCs, then record the source and sink SCC as this may
2191 * be a good place to cut between SCCs.
2193 static int check_conflict(int con, void *user)
2195 int i;
2196 struct isl_sched_graph *graph = user;
2198 if (graph->src_scc >= 0)
2199 return 0;
2201 con -= graph->lp->n_eq;
2203 if (con >= graph->lp->n_ineq)
2204 return 0;
2206 for (i = 0; i < graph->n_edge; ++i) {
2207 if (!graph->edge[i].validity)
2208 continue;
2209 if (graph->edge[i].src == graph->edge[i].dst)
2210 continue;
2211 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
2212 continue;
2213 if (graph->edge[i].start > con)
2214 continue;
2215 if (graph->edge[i].end <= con)
2216 continue;
2217 graph->src_scc = graph->edge[i].src->scc;
2218 graph->dst_scc = graph->edge[i].dst->scc;
2221 return 0;
2224 /* Check whether the next schedule row of the given node needs to be
2225 * non-trivial. Lower-dimensional domains may have some trivial rows,
2226 * but as soon as the number of remaining required non-trivial rows
2227 * is as large as the number or remaining rows to be computed,
2228 * all remaining rows need to be non-trivial.
2230 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
2232 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
2235 /* Solve the ILP problem constructed in setup_lp.
2236 * For each node such that all the remaining rows of its schedule
2237 * need to be non-trivial, we construct a non-triviality region.
2238 * This region imposes that the next row is independent of previous rows.
2239 * In particular the coefficients c_i_x are represented by t_i_x
2240 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
2241 * its first columns span the rows of the previously computed part
2242 * of the schedule. The non-triviality region enforces that at least
2243 * one of the remaining components of t_i_x is non-zero, i.e.,
2244 * that the new schedule row depends on at least one of the remaining
2245 * columns of Q.
2247 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
2249 int i;
2250 isl_vec *sol;
2251 isl_basic_set *lp;
2253 for (i = 0; i < graph->n; ++i) {
2254 struct isl_sched_node *node = &graph->node[i];
2255 int skip = node->rank;
2256 graph->region[i].pos = node->start + 1 + 2*(node->nparam+skip);
2257 if (needs_row(graph, node))
2258 graph->region[i].len = 2 * (node->nvar - skip);
2259 else
2260 graph->region[i].len = 0;
2262 lp = isl_basic_set_copy(graph->lp);
2263 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
2264 graph->region, &check_conflict, graph);
2265 return sol;
2268 /* Update the schedules of all nodes based on the given solution
2269 * of the LP problem.
2270 * The new row is added to the current band.
2271 * All possibly negative coefficients are encoded as a difference
2272 * of two non-negative variables, so we need to perform the subtraction
2273 * here. Moreover, if use_cmap is set, then the solution does
2274 * not refer to the actual coefficients c_i_x, but instead to variables
2275 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2276 * In this case, we then also need to perform this multiplication
2277 * to obtain the values of c_i_x.
2279 * If coincident is set, then the caller guarantees that the new
2280 * row satisfies the coincidence constraints.
2282 static int update_schedule(struct isl_sched_graph *graph,
2283 __isl_take isl_vec *sol, int use_cmap, int coincident)
2285 int i, j;
2286 isl_vec *csol = NULL;
2288 if (!sol)
2289 goto error;
2290 if (sol->size == 0)
2291 isl_die(sol->ctx, isl_error_internal,
2292 "no solution found", goto error);
2293 if (graph->n_total_row >= graph->max_row)
2294 isl_die(sol->ctx, isl_error_internal,
2295 "too many schedule rows", goto error);
2297 for (i = 0; i < graph->n; ++i) {
2298 struct isl_sched_node *node = &graph->node[i];
2299 int pos = node->start;
2300 int row = isl_mat_rows(node->sched);
2302 isl_vec_free(csol);
2303 csol = isl_vec_alloc(sol->ctx, node->nvar);
2304 if (!csol)
2305 goto error;
2307 isl_map_free(node->sched_map);
2308 node->sched_map = NULL;
2309 node->sched = isl_mat_add_rows(node->sched, 1);
2310 if (!node->sched)
2311 goto error;
2312 node->sched = isl_mat_set_element(node->sched, row, 0,
2313 sol->el[1 + pos]);
2314 for (j = 0; j < node->nparam + node->nvar; ++j)
2315 isl_int_sub(sol->el[1 + pos + 1 + 2 * j + 1],
2316 sol->el[1 + pos + 1 + 2 * j + 1],
2317 sol->el[1 + pos + 1 + 2 * j]);
2318 for (j = 0; j < node->nparam; ++j)
2319 node->sched = isl_mat_set_element(node->sched,
2320 row, 1 + j, sol->el[1+pos+1+2*j+1]);
2321 for (j = 0; j < node->nvar; ++j)
2322 isl_int_set(csol->el[j],
2323 sol->el[1+pos+1+2*(node->nparam+j)+1]);
2324 if (use_cmap)
2325 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
2326 csol);
2327 if (!csol)
2328 goto error;
2329 for (j = 0; j < node->nvar; ++j)
2330 node->sched = isl_mat_set_element(node->sched,
2331 row, 1 + node->nparam + j, csol->el[j]);
2332 node->coincident[graph->n_total_row] = coincident;
2334 isl_vec_free(sol);
2335 isl_vec_free(csol);
2337 graph->n_row++;
2338 graph->n_total_row++;
2340 return 0;
2341 error:
2342 isl_vec_free(sol);
2343 isl_vec_free(csol);
2344 return -1;
2347 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2348 * and return this isl_aff.
2350 static __isl_give isl_aff *extract_schedule_row(__isl_take isl_local_space *ls,
2351 struct isl_sched_node *node, int row)
2353 int j;
2354 isl_int v;
2355 isl_aff *aff;
2357 isl_int_init(v);
2359 aff = isl_aff_zero_on_domain(ls);
2360 isl_mat_get_element(node->sched, row, 0, &v);
2361 aff = isl_aff_set_constant(aff, v);
2362 for (j = 0; j < node->nparam; ++j) {
2363 isl_mat_get_element(node->sched, row, 1 + j, &v);
2364 aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
2366 for (j = 0; j < node->nvar; ++j) {
2367 isl_mat_get_element(node->sched, row, 1 + node->nparam + j, &v);
2368 aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
2371 isl_int_clear(v);
2373 return aff;
2376 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
2377 * and return this multi_aff.
2379 * The result is defined over the uncompressed node domain.
2381 static __isl_give isl_multi_aff *node_extract_partial_schedule_multi_aff(
2382 struct isl_sched_node *node, int first, int n)
2384 int i;
2385 isl_space *space;
2386 isl_local_space *ls;
2387 isl_aff *aff;
2388 isl_multi_aff *ma;
2389 int nrow;
2391 nrow = isl_mat_rows(node->sched);
2392 if (node->compressed)
2393 space = isl_multi_aff_get_domain_space(node->decompress);
2394 else
2395 space = isl_space_copy(node->space);
2396 ls = isl_local_space_from_space(isl_space_copy(space));
2397 space = isl_space_from_domain(space);
2398 space = isl_space_add_dims(space, isl_dim_out, n);
2399 ma = isl_multi_aff_zero(space);
2401 for (i = first; i < first + n; ++i) {
2402 aff = extract_schedule_row(isl_local_space_copy(ls), node, i);
2403 ma = isl_multi_aff_set_aff(ma, i - first, aff);
2406 isl_local_space_free(ls);
2408 if (node->compressed)
2409 ma = isl_multi_aff_pullback_multi_aff(ma,
2410 isl_multi_aff_copy(node->compress));
2412 return ma;
2415 /* Convert node->sched into a multi_aff and return this multi_aff.
2417 * The result is defined over the uncompressed node domain.
2419 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
2420 struct isl_sched_node *node)
2422 int nrow;
2424 nrow = isl_mat_rows(node->sched);
2425 return node_extract_partial_schedule_multi_aff(node, 0, nrow);
2428 /* Convert node->sched into a map and return this map.
2430 * The result is cached in node->sched_map, which needs to be released
2431 * whenever node->sched is updated.
2432 * It is defined over the uncompressed node domain.
2434 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
2436 if (!node->sched_map) {
2437 isl_multi_aff *ma;
2439 ma = node_extract_schedule_multi_aff(node);
2440 node->sched_map = isl_map_from_multi_aff(ma);
2443 return isl_map_copy(node->sched_map);
2446 /* Construct a map that can be used to update a dependence relation
2447 * based on the current schedule.
2448 * That is, construct a map expressing that source and sink
2449 * are executed within the same iteration of the current schedule.
2450 * This map can then be intersected with the dependence relation.
2451 * This is not the most efficient way, but this shouldn't be a critical
2452 * operation.
2454 static __isl_give isl_map *specializer(struct isl_sched_node *src,
2455 struct isl_sched_node *dst)
2457 isl_map *src_sched, *dst_sched;
2459 src_sched = node_extract_schedule(src);
2460 dst_sched = node_extract_schedule(dst);
2461 return isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
2464 /* Intersect the domains of the nested relations in domain and range
2465 * of "umap" with "map".
2467 static __isl_give isl_union_map *intersect_domains(
2468 __isl_take isl_union_map *umap, __isl_keep isl_map *map)
2470 isl_union_set *uset;
2472 umap = isl_union_map_zip(umap);
2473 uset = isl_union_set_from_set(isl_map_wrap(isl_map_copy(map)));
2474 umap = isl_union_map_intersect_domain(umap, uset);
2475 umap = isl_union_map_zip(umap);
2476 return umap;
2479 /* Update the dependence relation of the given edge based
2480 * on the current schedule.
2481 * If the dependence is carried completely by the current schedule, then
2482 * it is removed from the edge_tables. It is kept in the list of edges
2483 * as otherwise all edge_tables would have to be recomputed.
2485 static int update_edge(struct isl_sched_graph *graph,
2486 struct isl_sched_edge *edge)
2488 int empty;
2489 isl_map *id;
2491 id = specializer(edge->src, edge->dst);
2492 edge->map = isl_map_intersect(edge->map, isl_map_copy(id));
2493 if (!edge->map)
2494 goto error;
2496 if (edge->tagged_condition) {
2497 edge->tagged_condition =
2498 intersect_domains(edge->tagged_condition, id);
2499 if (!edge->tagged_condition)
2500 goto error;
2502 if (edge->tagged_validity) {
2503 edge->tagged_validity =
2504 intersect_domains(edge->tagged_validity, id);
2505 if (!edge->tagged_validity)
2506 goto error;
2509 empty = isl_map_plain_is_empty(edge->map);
2510 if (empty < 0)
2511 goto error;
2512 if (empty)
2513 graph_remove_edge(graph, edge);
2515 isl_map_free(id);
2516 return 0;
2517 error:
2518 isl_map_free(id);
2519 return -1;
2522 /* Does the domain of "umap" intersect "uset"?
2524 static int domain_intersects(__isl_keep isl_union_map *umap,
2525 __isl_keep isl_union_set *uset)
2527 int empty;
2529 umap = isl_union_map_copy(umap);
2530 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(uset));
2531 empty = isl_union_map_is_empty(umap);
2532 isl_union_map_free(umap);
2534 return empty < 0 ? -1 : !empty;
2537 /* Does the range of "umap" intersect "uset"?
2539 static int range_intersects(__isl_keep isl_union_map *umap,
2540 __isl_keep isl_union_set *uset)
2542 int empty;
2544 umap = isl_union_map_copy(umap);
2545 umap = isl_union_map_intersect_range(umap, isl_union_set_copy(uset));
2546 empty = isl_union_map_is_empty(umap);
2547 isl_union_map_free(umap);
2549 return empty < 0 ? -1 : !empty;
2552 /* Are the condition dependences of "edge" local with respect to
2553 * the current schedule?
2555 * That is, are domain and range of the condition dependences mapped
2556 * to the same point?
2558 * In other words, is the condition false?
2560 static int is_condition_false(struct isl_sched_edge *edge)
2562 isl_union_map *umap;
2563 isl_map *map, *sched, *test;
2564 int empty, local;
2566 empty = isl_union_map_is_empty(edge->tagged_condition);
2567 if (empty < 0 || empty)
2568 return empty;
2570 umap = isl_union_map_copy(edge->tagged_condition);
2571 umap = isl_union_map_zip(umap);
2572 umap = isl_union_set_unwrap(isl_union_map_domain(umap));
2573 map = isl_map_from_union_map(umap);
2575 sched = node_extract_schedule(edge->src);
2576 map = isl_map_apply_domain(map, sched);
2577 sched = node_extract_schedule(edge->dst);
2578 map = isl_map_apply_range(map, sched);
2580 test = isl_map_identity(isl_map_get_space(map));
2581 local = isl_map_is_subset(map, test);
2582 isl_map_free(map);
2583 isl_map_free(test);
2585 return local;
2588 /* For each conditional validity constraint that is adjacent
2589 * to a condition with domain in condition_source or range in condition_sink,
2590 * turn it into an unconditional validity constraint.
2592 static int unconditionalize_adjacent_validity(struct isl_sched_graph *graph,
2593 __isl_take isl_union_set *condition_source,
2594 __isl_take isl_union_set *condition_sink)
2596 int i;
2598 condition_source = isl_union_set_coalesce(condition_source);
2599 condition_sink = isl_union_set_coalesce(condition_sink);
2601 for (i = 0; i < graph->n_edge; ++i) {
2602 int adjacent;
2603 isl_union_map *validity;
2605 if (!graph->edge[i].conditional_validity)
2606 continue;
2607 if (graph->edge[i].validity)
2608 continue;
2610 validity = graph->edge[i].tagged_validity;
2611 adjacent = domain_intersects(validity, condition_sink);
2612 if (adjacent >= 0 && !adjacent)
2613 adjacent = range_intersects(validity, condition_source);
2614 if (adjacent < 0)
2615 goto error;
2616 if (!adjacent)
2617 continue;
2619 graph->edge[i].validity = 1;
2622 isl_union_set_free(condition_source);
2623 isl_union_set_free(condition_sink);
2624 return 0;
2625 error:
2626 isl_union_set_free(condition_source);
2627 isl_union_set_free(condition_sink);
2628 return -1;
2631 /* Update the dependence relations of all edges based on the current schedule
2632 * and enforce conditional validity constraints that are adjacent
2633 * to satisfied condition constraints.
2635 * First check if any of the condition constraints are satisfied
2636 * (i.e., not local to the outer schedule) and keep track of
2637 * their domain and range.
2638 * Then update all dependence relations (which removes the non-local
2639 * constraints).
2640 * Finally, if any condition constraints turned out to be satisfied,
2641 * then turn all adjacent conditional validity constraints into
2642 * unconditional validity constraints.
2644 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
2646 int i;
2647 int any = 0;
2648 isl_union_set *source, *sink;
2650 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
2651 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
2652 for (i = 0; i < graph->n_edge; ++i) {
2653 int local;
2654 isl_union_set *uset;
2655 isl_union_map *umap;
2657 if (!graph->edge[i].condition)
2658 continue;
2659 if (graph->edge[i].local)
2660 continue;
2661 local = is_condition_false(&graph->edge[i]);
2662 if (local < 0)
2663 goto error;
2664 if (local)
2665 continue;
2667 any = 1;
2669 umap = isl_union_map_copy(graph->edge[i].tagged_condition);
2670 uset = isl_union_map_domain(umap);
2671 source = isl_union_set_union(source, uset);
2673 umap = isl_union_map_copy(graph->edge[i].tagged_condition);
2674 uset = isl_union_map_range(umap);
2675 sink = isl_union_set_union(sink, uset);
2678 for (i = graph->n_edge - 1; i >= 0; --i) {
2679 if (update_edge(graph, &graph->edge[i]) < 0)
2680 goto error;
2683 if (any)
2684 return unconditionalize_adjacent_validity(graph, source, sink);
2686 isl_union_set_free(source);
2687 isl_union_set_free(sink);
2688 return 0;
2689 error:
2690 isl_union_set_free(source);
2691 isl_union_set_free(sink);
2692 return -1;
2695 static void next_band(struct isl_sched_graph *graph)
2697 graph->band_start = graph->n_total_row;
2700 /* Return the union of the universe domains of the nodes in "graph"
2701 * that satisfy "pred".
2703 static __isl_give isl_union_set *isl_sched_graph_domain(isl_ctx *ctx,
2704 struct isl_sched_graph *graph,
2705 int (*pred)(struct isl_sched_node *node, int data), int data)
2707 int i;
2708 isl_set *set;
2709 isl_union_set *dom;
2711 for (i = 0; i < graph->n; ++i)
2712 if (pred(&graph->node[i], data))
2713 break;
2715 if (i >= graph->n)
2716 isl_die(ctx, isl_error_internal,
2717 "empty component", return NULL);
2719 set = isl_set_universe(isl_space_copy(graph->node[i].space));
2720 dom = isl_union_set_from_set(set);
2722 for (i = i + 1; i < graph->n; ++i) {
2723 if (!pred(&graph->node[i], data))
2724 continue;
2725 set = isl_set_universe(isl_space_copy(graph->node[i].space));
2726 dom = isl_union_set_union(dom, isl_union_set_from_set(set));
2729 return dom;
2732 /* Return a list of unions of universe domains, where each element
2733 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
2735 static __isl_give isl_union_set_list *extract_sccs(isl_ctx *ctx,
2736 struct isl_sched_graph *graph)
2738 int i;
2739 isl_union_set_list *filters;
2741 filters = isl_union_set_list_alloc(ctx, graph->scc);
2742 for (i = 0; i < graph->scc; ++i) {
2743 isl_union_set *dom;
2745 dom = isl_sched_graph_domain(ctx, graph, &node_scc_exactly, i);
2746 filters = isl_union_set_list_add(filters, dom);
2749 return filters;
2752 /* Return a list of two unions of universe domains, one for the SCCs up
2753 * to and including graph->src_scc and another for the other SCCS.
2755 static __isl_give isl_union_set_list *extract_split(isl_ctx *ctx,
2756 struct isl_sched_graph *graph)
2758 isl_union_set *dom;
2759 isl_union_set_list *filters;
2761 filters = isl_union_set_list_alloc(ctx, 2);
2762 dom = isl_sched_graph_domain(ctx, graph,
2763 &node_scc_at_most, graph->src_scc);
2764 filters = isl_union_set_list_add(filters, dom);
2765 dom = isl_sched_graph_domain(ctx, graph,
2766 &node_scc_at_least, graph->src_scc + 1);
2767 filters = isl_union_set_list_add(filters, dom);
2769 return filters;
2772 /* Topologically sort statements mapped to the same schedule iteration
2773 * and add insert a sequence node in front of "node"
2774 * corresponding to this order.
2776 static __isl_give isl_schedule_node *sort_statements(
2777 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
2779 isl_ctx *ctx;
2780 isl_union_set_list *filters;
2782 if (!node)
2783 return NULL;
2785 ctx = isl_schedule_node_get_ctx(node);
2786 if (graph->n < 1)
2787 isl_die(ctx, isl_error_internal,
2788 "graph should have at least one node",
2789 return isl_schedule_node_free(node));
2791 if (graph->n == 1)
2792 return node;
2794 if (update_edges(ctx, graph) < 0)
2795 return isl_schedule_node_free(node);
2797 if (graph->n_edge == 0)
2798 return node;
2800 if (detect_sccs(ctx, graph) < 0)
2801 return isl_schedule_node_free(node);
2803 filters = extract_sccs(ctx, graph);
2804 node = isl_schedule_node_insert_sequence(node, filters);
2806 return node;
2809 /* Copy nodes that satisfy node_pred from the src dependence graph
2810 * to the dst dependence graph.
2812 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
2813 int (*node_pred)(struct isl_sched_node *node, int data), int data)
2815 int i;
2817 dst->n = 0;
2818 for (i = 0; i < src->n; ++i) {
2819 int j;
2821 if (!node_pred(&src->node[i], data))
2822 continue;
2824 j = dst->n;
2825 dst->node[j].space = isl_space_copy(src->node[i].space);
2826 dst->node[j].compressed = src->node[i].compressed;
2827 dst->node[j].hull = isl_set_copy(src->node[i].hull);
2828 dst->node[j].compress =
2829 isl_multi_aff_copy(src->node[i].compress);
2830 dst->node[j].decompress =
2831 isl_multi_aff_copy(src->node[i].decompress);
2832 dst->node[j].nvar = src->node[i].nvar;
2833 dst->node[j].nparam = src->node[i].nparam;
2834 dst->node[j].sched = isl_mat_copy(src->node[i].sched);
2835 dst->node[j].sched_map = isl_map_copy(src->node[i].sched_map);
2836 dst->node[j].coincident = src->node[i].coincident;
2837 dst->n++;
2839 if (!dst->node[j].space || !dst->node[j].sched)
2840 return -1;
2841 if (dst->node[j].compressed &&
2842 (!dst->node[j].hull || !dst->node[j].compress ||
2843 !dst->node[j].decompress))
2844 return -1;
2847 return 0;
2850 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
2851 * to the dst dependence graph.
2852 * If the source or destination node of the edge is not in the destination
2853 * graph, then it must be a backward proximity edge and it should simply
2854 * be ignored.
2856 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
2857 struct isl_sched_graph *src,
2858 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
2860 int i;
2861 enum isl_edge_type t;
2863 dst->n_edge = 0;
2864 for (i = 0; i < src->n_edge; ++i) {
2865 struct isl_sched_edge *edge = &src->edge[i];
2866 isl_map *map;
2867 isl_union_map *tagged_condition;
2868 isl_union_map *tagged_validity;
2869 struct isl_sched_node *dst_src, *dst_dst;
2871 if (!edge_pred(edge, data))
2872 continue;
2874 if (isl_map_plain_is_empty(edge->map))
2875 continue;
2877 dst_src = graph_find_node(ctx, dst, edge->src->space);
2878 dst_dst = graph_find_node(ctx, dst, edge->dst->space);
2879 if (!dst_src || !dst_dst) {
2880 if (edge->validity || edge->conditional_validity)
2881 isl_die(ctx, isl_error_internal,
2882 "backward (conditional) validity edge",
2883 return -1);
2884 continue;
2887 map = isl_map_copy(edge->map);
2888 tagged_condition = isl_union_map_copy(edge->tagged_condition);
2889 tagged_validity = isl_union_map_copy(edge->tagged_validity);
2891 dst->edge[dst->n_edge].src = dst_src;
2892 dst->edge[dst->n_edge].dst = dst_dst;
2893 dst->edge[dst->n_edge].map = map;
2894 dst->edge[dst->n_edge].tagged_condition = tagged_condition;
2895 dst->edge[dst->n_edge].tagged_validity = tagged_validity;
2896 dst->edge[dst->n_edge].validity = edge->validity;
2897 dst->edge[dst->n_edge].proximity = edge->proximity;
2898 dst->edge[dst->n_edge].coincidence = edge->coincidence;
2899 dst->edge[dst->n_edge].condition = edge->condition;
2900 dst->edge[dst->n_edge].conditional_validity =
2901 edge->conditional_validity;
2902 dst->n_edge++;
2904 if (edge->tagged_condition && !tagged_condition)
2905 return -1;
2906 if (edge->tagged_validity && !tagged_validity)
2907 return -1;
2909 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
2910 if (edge !=
2911 graph_find_edge(src, t, edge->src, edge->dst))
2912 continue;
2913 if (graph_edge_table_add(ctx, dst, t,
2914 &dst->edge[dst->n_edge - 1]) < 0)
2915 return -1;
2919 return 0;
2922 /* Compute the maximal number of variables over all nodes.
2923 * This is the maximal number of linearly independent schedule
2924 * rows that we need to compute.
2925 * Just in case we end up in a part of the dependence graph
2926 * with only lower-dimensional domains, we make sure we will
2927 * compute the required amount of extra linearly independent rows.
2929 static int compute_maxvar(struct isl_sched_graph *graph)
2931 int i;
2933 graph->maxvar = 0;
2934 for (i = 0; i < graph->n; ++i) {
2935 struct isl_sched_node *node = &graph->node[i];
2936 int nvar;
2938 if (node_update_cmap(node) < 0)
2939 return -1;
2940 nvar = node->nvar + graph->n_row - node->rank;
2941 if (nvar > graph->maxvar)
2942 graph->maxvar = nvar;
2945 return 0;
2948 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
2949 struct isl_sched_graph *graph);
2950 static __isl_give isl_schedule_node *compute_schedule_wcc(
2951 isl_schedule_node *node, struct isl_sched_graph *graph);
2953 /* Compute a schedule for a subgraph of "graph". In particular, for
2954 * the graph composed of nodes that satisfy node_pred and edges that
2955 * that satisfy edge_pred. The caller should precompute the number
2956 * of nodes and edges that satisfy these predicates and pass them along
2957 * as "n" and "n_edge".
2958 * If the subgraph is known to consist of a single component, then wcc should
2959 * be set and then we call compute_schedule_wcc on the constructed subgraph.
2960 * Otherwise, we call compute_schedule, which will check whether the subgraph
2961 * is connected.
2963 * The schedule is inserted at "node" and the updated schedule node
2964 * is returned.
2966 static __isl_give isl_schedule_node *compute_sub_schedule(
2967 __isl_take isl_schedule_node *node, isl_ctx *ctx,
2968 struct isl_sched_graph *graph, int n, int n_edge,
2969 int (*node_pred)(struct isl_sched_node *node, int data),
2970 int (*edge_pred)(struct isl_sched_edge *edge, int data),
2971 int data, int wcc)
2973 struct isl_sched_graph split = { 0 };
2974 int t;
2976 if (graph_alloc(ctx, &split, n, n_edge) < 0)
2977 goto error;
2978 if (copy_nodes(&split, graph, node_pred, data) < 0)
2979 goto error;
2980 if (graph_init_table(ctx, &split) < 0)
2981 goto error;
2982 for (t = 0; t <= isl_edge_last; ++t)
2983 split.max_edge[t] = graph->max_edge[t];
2984 if (graph_init_edge_tables(ctx, &split) < 0)
2985 goto error;
2986 if (copy_edges(ctx, &split, graph, edge_pred, data) < 0)
2987 goto error;
2988 split.n_row = graph->n_row;
2989 split.max_row = graph->max_row;
2990 split.n_total_row = graph->n_total_row;
2991 split.band_start = graph->band_start;
2993 if (wcc)
2994 node = compute_schedule_wcc(node, &split);
2995 else
2996 node = compute_schedule(node, &split);
2998 graph_free(ctx, &split);
2999 return node;
3000 error:
3001 graph_free(ctx, &split);
3002 return isl_schedule_node_free(node);
3005 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
3007 return edge->src->scc == scc && edge->dst->scc == scc;
3010 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
3012 return edge->dst->scc <= scc;
3015 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
3017 return edge->src->scc >= scc;
3020 /* Reset the current band by dropping all its schedule rows.
3022 static int reset_band(struct isl_sched_graph *graph)
3024 int i;
3025 int drop;
3027 drop = graph->n_total_row - graph->band_start;
3028 graph->n_total_row -= drop;
3029 graph->n_row -= drop;
3031 for (i = 0; i < graph->n; ++i) {
3032 struct isl_sched_node *node = &graph->node[i];
3034 isl_map_free(node->sched_map);
3035 node->sched_map = NULL;
3037 node->sched = isl_mat_drop_rows(node->sched,
3038 graph->band_start, drop);
3040 if (!node->sched)
3041 return -1;
3044 return 0;
3047 /* Split the current graph into two parts and compute a schedule for each
3048 * part individually. In particular, one part consists of all SCCs up
3049 * to and including graph->src_scc, while the other part contains the other
3050 * SCCS. The split is enforced by a sequence node inserted at position "node"
3051 * in the schedule tree. Return the updated schedule node.
3053 * The current band is reset. It would be possible to reuse
3054 * the previously computed rows as the first rows in the next
3055 * band, but recomputing them may result in better rows as we are looking
3056 * at a smaller part of the dependence graph.
3058 static __isl_give isl_schedule_node *compute_split_schedule(
3059 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3061 int i, n, e1, e2;
3062 int orig_total_row;
3063 isl_ctx *ctx;
3064 isl_union_set_list *filters;
3066 if (!node)
3067 return NULL;
3069 if (reset_band(graph) < 0)
3070 return isl_schedule_node_free(node);
3072 n = 0;
3073 for (i = 0; i < graph->n; ++i) {
3074 struct isl_sched_node *node = &graph->node[i];
3075 int before = node->scc <= graph->src_scc;
3077 if (before)
3078 n++;
3081 e1 = e2 = 0;
3082 for (i = 0; i < graph->n_edge; ++i) {
3083 if (graph->edge[i].dst->scc <= graph->src_scc)
3084 e1++;
3085 if (graph->edge[i].src->scc > graph->src_scc)
3086 e2++;
3089 next_band(graph);
3091 ctx = isl_schedule_node_get_ctx(node);
3092 filters = extract_split(ctx, graph);
3093 node = isl_schedule_node_insert_sequence(node, filters);
3094 node = isl_schedule_node_child(node, 0);
3095 node = isl_schedule_node_child(node, 0);
3097 orig_total_row = graph->n_total_row;
3098 node = compute_sub_schedule(node, ctx, graph, n, e1,
3099 &node_scc_at_most, &edge_dst_scc_at_most,
3100 graph->src_scc, 0);
3101 node = isl_schedule_node_parent(node);
3102 node = isl_schedule_node_next_sibling(node);
3103 node = isl_schedule_node_child(node, 0);
3104 graph->n_total_row = orig_total_row;
3105 node = compute_sub_schedule(node, ctx, graph, graph->n - n, e2,
3106 &node_scc_at_least, &edge_src_scc_at_least,
3107 graph->src_scc + 1, 0);
3108 node = isl_schedule_node_parent(node);
3109 node = isl_schedule_node_parent(node);
3111 return node;
3114 /* Insert a band node at position "node" in the schedule tree corresponding
3115 * to the current band in "graph". Mark the band node permutable
3116 * if "permutable" is set.
3117 * The partial schedules and the coincidence property are extracted
3118 * from the graph nodes.
3119 * Return the updated schedule node.
3121 static __isl_give isl_schedule_node *insert_current_band(
3122 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3123 int permutable)
3125 int i;
3126 int start, end, n;
3127 isl_multi_aff *ma;
3128 isl_multi_pw_aff *mpa;
3129 isl_multi_union_pw_aff *mupa;
3131 if (!node)
3132 return NULL;
3134 if (graph->n < 1)
3135 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
3136 "graph should have at least one node",
3137 return isl_schedule_node_free(node));
3139 start = graph->band_start;
3140 end = graph->n_total_row;
3141 n = end - start;
3143 ma = node_extract_partial_schedule_multi_aff(&graph->node[0], start, n);
3144 mpa = isl_multi_pw_aff_from_multi_aff(ma);
3145 mupa = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
3147 for (i = 1; i < graph->n; ++i) {
3148 isl_multi_union_pw_aff *mupa_i;
3150 ma = node_extract_partial_schedule_multi_aff(&graph->node[i],
3151 start, n);
3152 mpa = isl_multi_pw_aff_from_multi_aff(ma);
3153 mupa_i = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
3154 mupa = isl_multi_union_pw_aff_union_add(mupa, mupa_i);
3156 node = isl_schedule_node_insert_partial_schedule(node, mupa);
3158 for (i = 0; i < n; ++i)
3159 node = isl_schedule_node_band_member_set_coincident(node, i,
3160 graph->node[0].coincident[start + i]);
3161 node = isl_schedule_node_band_set_permutable(node, permutable);
3163 return node;
3166 /* Update the dependence relations based on the current schedule,
3167 * add the current band to "node" and the continue with the computation
3168 * of the next band.
3169 * Return the updated schedule node.
3171 static __isl_give isl_schedule_node *compute_next_band(
3172 __isl_take isl_schedule_node *node,
3173 struct isl_sched_graph *graph, int permutable)
3175 isl_ctx *ctx;
3177 if (!node)
3178 return NULL;
3180 ctx = isl_schedule_node_get_ctx(node);
3181 if (update_edges(ctx, graph) < 0)
3182 return isl_schedule_node_free(node);
3183 node = insert_current_band(node, graph, permutable);
3184 next_band(graph);
3186 node = isl_schedule_node_child(node, 0);
3187 node = compute_schedule(node, graph);
3188 node = isl_schedule_node_parent(node);
3190 return node;
3193 /* Add constraints to graph->lp that force the dependence "map" (which
3194 * is part of the dependence relation of "edge")
3195 * to be respected and attempt to carry it, where the edge is one from
3196 * a node j to itself. "pos" is the sequence number of the given map.
3197 * That is, add constraints that enforce
3199 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3200 * = c_j_x (y - x) >= e_i
3202 * for each (x,y) in R.
3203 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3204 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3205 * with each coefficient in c_j_x represented as a pair of non-negative
3206 * coefficients.
3208 static int add_intra_constraints(struct isl_sched_graph *graph,
3209 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3211 unsigned total;
3212 isl_ctx *ctx = isl_map_get_ctx(map);
3213 isl_space *dim;
3214 isl_dim_map *dim_map;
3215 isl_basic_set *coef;
3216 struct isl_sched_node *node = edge->src;
3218 coef = intra_coefficients(graph, node, map);
3219 if (!coef)
3220 return -1;
3222 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3224 total = isl_basic_set_total_dim(graph->lp);
3225 dim_map = isl_dim_map_alloc(ctx, total);
3226 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3227 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
3228 isl_space_dim(dim, isl_dim_set), 1,
3229 node->nvar, -1);
3230 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
3231 isl_space_dim(dim, isl_dim_set), 1,
3232 node->nvar, 1);
3233 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3234 coef->n_eq, coef->n_ineq);
3235 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3236 coef, dim_map);
3237 isl_space_free(dim);
3239 return 0;
3242 /* Add constraints to graph->lp that force the dependence "map" (which
3243 * is part of the dependence relation of "edge")
3244 * to be respected and attempt to carry it, where the edge is one from
3245 * node j to node k. "pos" is the sequence number of the given map.
3246 * That is, add constraints that enforce
3248 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3250 * for each (x,y) in R.
3251 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3252 * of valid constraints for R and then plug in
3253 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3254 * with each coefficient (except e_i, c_k_0 and c_j_0)
3255 * represented as a pair of non-negative coefficients.
3257 static int add_inter_constraints(struct isl_sched_graph *graph,
3258 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3260 unsigned total;
3261 isl_ctx *ctx = isl_map_get_ctx(map);
3262 isl_space *dim;
3263 isl_dim_map *dim_map;
3264 isl_basic_set *coef;
3265 struct isl_sched_node *src = edge->src;
3266 struct isl_sched_node *dst = edge->dst;
3268 coef = inter_coefficients(graph, edge, map);
3269 if (!coef)
3270 return -1;
3272 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3274 total = isl_basic_set_total_dim(graph->lp);
3275 dim_map = isl_dim_map_alloc(ctx, total);
3277 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3279 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
3280 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
3281 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
3282 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
3283 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3284 dst->nvar, -1);
3285 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
3286 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3287 dst->nvar, 1);
3289 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
3290 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
3291 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
3292 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
3293 isl_space_dim(dim, isl_dim_set), 1,
3294 src->nvar, 1);
3295 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
3296 isl_space_dim(dim, isl_dim_set), 1,
3297 src->nvar, -1);
3299 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3300 coef->n_eq, coef->n_ineq);
3301 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3302 coef, dim_map);
3303 isl_space_free(dim);
3305 return 0;
3308 /* Add constraints to graph->lp that force all (conditional) validity
3309 * dependences to be respected and attempt to carry them.
3311 static int add_all_constraints(struct isl_sched_graph *graph)
3313 int i, j;
3314 int pos;
3316 pos = 0;
3317 for (i = 0; i < graph->n_edge; ++i) {
3318 struct isl_sched_edge *edge= &graph->edge[i];
3320 if (!edge->validity && !edge->conditional_validity)
3321 continue;
3323 for (j = 0; j < edge->map->n; ++j) {
3324 isl_basic_map *bmap;
3325 isl_map *map;
3327 bmap = isl_basic_map_copy(edge->map->p[j]);
3328 map = isl_map_from_basic_map(bmap);
3330 if (edge->src == edge->dst &&
3331 add_intra_constraints(graph, edge, map, pos) < 0)
3332 return -1;
3333 if (edge->src != edge->dst &&
3334 add_inter_constraints(graph, edge, map, pos) < 0)
3335 return -1;
3336 ++pos;
3340 return 0;
3343 /* Count the number of equality and inequality constraints
3344 * that will be added to the carry_lp problem.
3345 * We count each edge exactly once.
3347 static int count_all_constraints(struct isl_sched_graph *graph,
3348 int *n_eq, int *n_ineq)
3350 int i, j;
3352 *n_eq = *n_ineq = 0;
3353 for (i = 0; i < graph->n_edge; ++i) {
3354 struct isl_sched_edge *edge= &graph->edge[i];
3355 for (j = 0; j < edge->map->n; ++j) {
3356 isl_basic_map *bmap;
3357 isl_map *map;
3359 bmap = isl_basic_map_copy(edge->map->p[j]);
3360 map = isl_map_from_basic_map(bmap);
3362 if (count_map_constraints(graph, edge, map,
3363 n_eq, n_ineq, 1, 0) < 0)
3364 return -1;
3368 return 0;
3371 /* Construct an LP problem for finding schedule coefficients
3372 * such that the schedule carries as many dependences as possible.
3373 * In particular, for each dependence i, we bound the dependence distance
3374 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3375 * of all e_i's. Dependence with e_i = 0 in the solution are simply
3376 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3377 * Note that if the dependence relation is a union of basic maps,
3378 * then we have to consider each basic map individually as it may only
3379 * be possible to carry the dependences expressed by some of those
3380 * basic maps and not all off them.
3381 * Below, we consider each of those basic maps as a separate "edge".
3383 * All variables of the LP are non-negative. The actual coefficients
3384 * may be negative, so each coefficient is represented as the difference
3385 * of two non-negative variables. The negative part always appears
3386 * immediately before the positive part.
3387 * Other than that, the variables have the following order
3389 * - sum of (1 - e_i) over all edges
3390 * - sum of positive and negative parts of all c_n coefficients
3391 * (unconstrained when computing non-parametric schedules)
3392 * - sum of positive and negative parts of all c_x coefficients
3393 * - for each edge
3394 * - e_i
3395 * - for each node
3396 * - c_i_0
3397 * - positive and negative parts of c_i_n (if parametric)
3398 * - positive and negative parts of c_i_x
3400 * The constraints are those from the (validity) edges plus three equalities
3401 * to express the sums and n_edge inequalities to express e_i <= 1.
3403 static int setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
3405 int i, j;
3406 int k;
3407 isl_space *dim;
3408 unsigned total;
3409 int n_eq, n_ineq;
3410 int n_edge;
3412 n_edge = 0;
3413 for (i = 0; i < graph->n_edge; ++i)
3414 n_edge += graph->edge[i].map->n;
3416 total = 3 + n_edge;
3417 for (i = 0; i < graph->n; ++i) {
3418 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
3419 node->start = total;
3420 total += 1 + 2 * (node->nparam + node->nvar);
3423 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
3424 return -1;
3425 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
3426 return -1;
3428 dim = isl_space_set_alloc(ctx, 0, total);
3429 isl_basic_set_free(graph->lp);
3430 n_eq += 3;
3431 n_ineq += n_edge;
3432 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
3433 graph->lp = isl_basic_set_set_rational(graph->lp);
3435 k = isl_basic_set_alloc_equality(graph->lp);
3436 if (k < 0)
3437 return -1;
3438 isl_seq_clr(graph->lp->eq[k], 1 + total);
3439 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
3440 isl_int_set_si(graph->lp->eq[k][1], 1);
3441 for (i = 0; i < n_edge; ++i)
3442 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
3444 k = isl_basic_set_alloc_equality(graph->lp);
3445 if (k < 0)
3446 return -1;
3447 isl_seq_clr(graph->lp->eq[k], 1 + total);
3448 isl_int_set_si(graph->lp->eq[k][2], -1);
3449 for (i = 0; i < graph->n; ++i) {
3450 int pos = 1 + graph->node[i].start + 1;
3452 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
3453 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3456 k = isl_basic_set_alloc_equality(graph->lp);
3457 if (k < 0)
3458 return -1;
3459 isl_seq_clr(graph->lp->eq[k], 1 + total);
3460 isl_int_set_si(graph->lp->eq[k][3], -1);
3461 for (i = 0; i < graph->n; ++i) {
3462 struct isl_sched_node *node = &graph->node[i];
3463 int pos = 1 + node->start + 1 + 2 * node->nparam;
3465 for (j = 0; j < 2 * node->nvar; ++j)
3466 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3469 for (i = 0; i < n_edge; ++i) {
3470 k = isl_basic_set_alloc_inequality(graph->lp);
3471 if (k < 0)
3472 return -1;
3473 isl_seq_clr(graph->lp->ineq[k], 1 + total);
3474 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
3475 isl_int_set_si(graph->lp->ineq[k][0], 1);
3478 if (add_bound_coefficient_constraints(ctx, graph) < 0)
3479 return -1;
3480 if (add_all_constraints(graph) < 0)
3481 return -1;
3483 return 0;
3486 static __isl_give isl_schedule_node *compute_component_schedule(
3487 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3488 int wcc);
3490 /* Comparison function for sorting the statements based on
3491 * the corresponding value in "r".
3493 static int smaller_value(const void *a, const void *b, void *data)
3495 isl_vec *r = data;
3496 const int *i1 = a;
3497 const int *i2 = b;
3499 return isl_int_cmp(r->el[*i1], r->el[*i2]);
3502 /* If the schedule_split_scaled option is set and if the linear
3503 * parts of the scheduling rows for all nodes in the graphs have
3504 * a non-trivial common divisor, then split off the remainder of the
3505 * constant term modulo this common divisor from the linear part.
3506 * Otherwise, insert a band node directly and continue with
3507 * the construction of the schedule.
3509 * If a non-trivial common divisor is found, then
3510 * the linear part is reduced and the remainder is enforced
3511 * by a sequence node with the children placed in the order
3512 * of this remainder.
3513 * In particular, we assign an scc index based on the remainder and
3514 * then rely on compute_component_schedule to insert the sequence and
3515 * to continue the schedule construction on each part.
3517 static __isl_give isl_schedule_node *split_scaled(
3518 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3520 int i;
3521 int row;
3522 int scc;
3523 isl_ctx *ctx;
3524 isl_int gcd, gcd_i;
3525 isl_vec *r;
3526 int *order;
3528 if (!node)
3529 return NULL;
3531 ctx = isl_schedule_node_get_ctx(node);
3532 if (!ctx->opt->schedule_split_scaled)
3533 return compute_next_band(node, graph, 0);
3534 if (graph->n <= 1)
3535 return compute_next_band(node, graph, 0);
3537 isl_int_init(gcd);
3538 isl_int_init(gcd_i);
3540 isl_int_set_si(gcd, 0);
3542 row = isl_mat_rows(graph->node[0].sched) - 1;
3544 for (i = 0; i < graph->n; ++i) {
3545 struct isl_sched_node *node = &graph->node[i];
3546 int cols = isl_mat_cols(node->sched);
3548 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
3549 isl_int_gcd(gcd, gcd, gcd_i);
3552 isl_int_clear(gcd_i);
3554 if (isl_int_cmp_si(gcd, 1) <= 0) {
3555 isl_int_clear(gcd);
3556 return compute_next_band(node, graph, 0);
3559 r = isl_vec_alloc(ctx, graph->n);
3560 order = isl_calloc_array(ctx, int, graph->n);
3561 if (!r || !order)
3562 goto error;
3564 for (i = 0; i < graph->n; ++i) {
3565 struct isl_sched_node *node = &graph->node[i];
3567 order[i] = i;
3568 isl_int_fdiv_r(r->el[i], node->sched->row[row][0], gcd);
3569 isl_int_fdiv_q(node->sched->row[row][0],
3570 node->sched->row[row][0], gcd);
3571 isl_int_mul(node->sched->row[row][0],
3572 node->sched->row[row][0], gcd);
3573 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
3574 if (!node->sched)
3575 goto error;
3578 if (isl_sort(order, graph->n, sizeof(order[0]), &smaller_value, r) < 0)
3579 goto error;
3581 scc = 0;
3582 for (i = 0; i < graph->n; ++i) {
3583 if (i > 0 && isl_int_ne(r->el[order[i - 1]], r->el[order[i]]))
3584 ++scc;
3585 graph->node[order[i]].scc = scc;
3587 graph->scc = ++scc;
3588 graph->weak = 0;
3590 isl_int_clear(gcd);
3591 isl_vec_free(r);
3592 free(order);
3594 if (update_edges(ctx, graph) < 0)
3595 return isl_schedule_node_free(node);
3596 node = insert_current_band(node, graph, 0);
3597 next_band(graph);
3599 node = isl_schedule_node_child(node, 0);
3600 node = compute_component_schedule(node, graph, 0);
3601 node = isl_schedule_node_parent(node);
3603 return node;
3604 error:
3605 isl_vec_free(r);
3606 free(order);
3607 isl_int_clear(gcd);
3608 return isl_schedule_node_free(node);
3611 /* Is the schedule row "sol" trivial on node "node"?
3612 * That is, is the solution zero on the dimensions orthogonal to
3613 * the previously found solutions?
3614 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3616 * Each coefficient is represented as the difference between
3617 * two non-negative values in "sol". "sol" has been computed
3618 * in terms of the original iterators (i.e., without use of cmap).
3619 * We construct the schedule row s and write it as a linear
3620 * combination of (linear combinations of) previously computed schedule rows.
3621 * s = Q c or c = U s.
3622 * If the final entries of c are all zero, then the solution is trivial.
3624 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
3626 int i;
3627 int pos;
3628 int trivial;
3629 isl_ctx *ctx;
3630 isl_vec *node_sol;
3632 if (!sol)
3633 return -1;
3634 if (node->nvar == node->rank)
3635 return 0;
3637 ctx = isl_vec_get_ctx(sol);
3638 node_sol = isl_vec_alloc(ctx, node->nvar);
3639 if (!node_sol)
3640 return -1;
3642 pos = 1 + node->start + 1 + 2 * node->nparam;
3644 for (i = 0; i < node->nvar; ++i)
3645 isl_int_sub(node_sol->el[i],
3646 sol->el[pos + 2 * i + 1], sol->el[pos + 2 * i]);
3648 node_sol = isl_mat_vec_product(isl_mat_copy(node->cinv), node_sol);
3650 if (!node_sol)
3651 return -1;
3653 trivial = isl_seq_first_non_zero(node_sol->el + node->rank,
3654 node->nvar - node->rank) == -1;
3656 isl_vec_free(node_sol);
3658 return trivial;
3661 /* Is the schedule row "sol" trivial on any node where it should
3662 * not be trivial?
3663 * "sol" has been computed in terms of the original iterators
3664 * (i.e., without use of cmap).
3665 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3667 static int is_any_trivial(struct isl_sched_graph *graph,
3668 __isl_keep isl_vec *sol)
3670 int i;
3672 for (i = 0; i < graph->n; ++i) {
3673 struct isl_sched_node *node = &graph->node[i];
3674 int trivial;
3676 if (!needs_row(graph, node))
3677 continue;
3678 trivial = is_trivial(node, sol);
3679 if (trivial < 0 || trivial)
3680 return trivial;
3683 return 0;
3686 /* Construct a schedule row for each node such that as many dependences
3687 * as possible are carried and then continue with the next band.
3689 * If the computed schedule row turns out to be trivial on one or
3690 * more nodes where it should not be trivial, then we throw it away
3691 * and try again on each component separately.
3693 * If there is only one component, then we accept the schedule row anyway,
3694 * but we do not consider it as a complete row and therefore do not
3695 * increment graph->n_row. Note that the ranks of the nodes that
3696 * do get a non-trivial schedule part will get updated regardless and
3697 * graph->maxvar is computed based on these ranks. The test for
3698 * whether more schedule rows are required in compute_schedule_wcc
3699 * is therefore not affected.
3701 * Insert a band corresponding to the schedule row at position "node"
3702 * of the schedule tree and continue with the construction of the schedule.
3703 * This insertion and the continued construction is performed by split_scaled
3704 * after optionally checking for non-trivial common divisors.
3706 static __isl_give isl_schedule_node *carry_dependences(
3707 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3709 int i;
3710 int n_edge;
3711 int trivial;
3712 isl_ctx *ctx;
3713 isl_vec *sol;
3714 isl_basic_set *lp;
3716 if (!node)
3717 return NULL;
3719 n_edge = 0;
3720 for (i = 0; i < graph->n_edge; ++i)
3721 n_edge += graph->edge[i].map->n;
3723 ctx = isl_schedule_node_get_ctx(node);
3724 if (setup_carry_lp(ctx, graph) < 0)
3725 return isl_schedule_node_free(node);
3727 lp = isl_basic_set_copy(graph->lp);
3728 sol = isl_tab_basic_set_non_neg_lexmin(lp);
3729 if (!sol)
3730 return isl_schedule_node_free(node);
3732 if (sol->size == 0) {
3733 isl_vec_free(sol);
3734 isl_die(ctx, isl_error_internal,
3735 "error in schedule construction",
3736 return isl_schedule_node_free(node));
3739 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
3740 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
3741 isl_vec_free(sol);
3742 isl_die(ctx, isl_error_unknown,
3743 "unable to carry dependences",
3744 return isl_schedule_node_free(node));
3747 trivial = is_any_trivial(graph, sol);
3748 if (trivial < 0) {
3749 sol = isl_vec_free(sol);
3750 } else if (trivial && graph->scc > 1) {
3751 isl_vec_free(sol);
3752 return compute_component_schedule(node, graph, 1);
3755 if (update_schedule(graph, sol, 0, 0) < 0)
3756 return isl_schedule_node_free(node);
3757 if (trivial)
3758 graph->n_row--;
3760 return split_scaled(node, graph);
3763 /* Are there any (non-empty) (conditional) validity edges in the graph?
3765 static int has_validity_edges(struct isl_sched_graph *graph)
3767 int i;
3769 for (i = 0; i < graph->n_edge; ++i) {
3770 int empty;
3772 empty = isl_map_plain_is_empty(graph->edge[i].map);
3773 if (empty < 0)
3774 return -1;
3775 if (empty)
3776 continue;
3777 if (graph->edge[i].validity ||
3778 graph->edge[i].conditional_validity)
3779 return 1;
3782 return 0;
3785 /* Should we apply a Feautrier step?
3786 * That is, did the user request the Feautrier algorithm and are
3787 * there any validity dependences (left)?
3789 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
3791 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
3792 return 0;
3794 return has_validity_edges(graph);
3797 /* Compute a schedule for a connected dependence graph using Feautrier's
3798 * multi-dimensional scheduling algorithm and return the updated schedule node.
3800 * The original algorithm is described in [1].
3801 * The main idea is to minimize the number of scheduling dimensions, by
3802 * trying to satisfy as many dependences as possible per scheduling dimension.
3804 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
3805 * Problem, Part II: Multi-Dimensional Time.
3806 * In Intl. Journal of Parallel Programming, 1992.
3808 static __isl_give isl_schedule_node *compute_schedule_wcc_feautrier(
3809 isl_schedule_node *node, struct isl_sched_graph *graph)
3811 return carry_dependences(node, graph);
3814 /* Turn off the "local" bit on all (condition) edges.
3816 static void clear_local_edges(struct isl_sched_graph *graph)
3818 int i;
3820 for (i = 0; i < graph->n_edge; ++i)
3821 if (graph->edge[i].condition)
3822 graph->edge[i].local = 0;
3825 /* Does "graph" have both condition and conditional validity edges?
3827 static int need_condition_check(struct isl_sched_graph *graph)
3829 int i;
3830 int any_condition = 0;
3831 int any_conditional_validity = 0;
3833 for (i = 0; i < graph->n_edge; ++i) {
3834 if (graph->edge[i].condition)
3835 any_condition = 1;
3836 if (graph->edge[i].conditional_validity)
3837 any_conditional_validity = 1;
3840 return any_condition && any_conditional_validity;
3843 /* Does "graph" contain any coincidence edge?
3845 static int has_any_coincidence(struct isl_sched_graph *graph)
3847 int i;
3849 for (i = 0; i < graph->n_edge; ++i)
3850 if (graph->edge[i].coincidence)
3851 return 1;
3853 return 0;
3856 /* Extract the final schedule row as a map with the iteration domain
3857 * of "node" as domain.
3859 static __isl_give isl_map *final_row(struct isl_sched_node *node)
3861 isl_local_space *ls;
3862 isl_aff *aff;
3863 int row;
3865 row = isl_mat_rows(node->sched) - 1;
3866 ls = isl_local_space_from_space(isl_space_copy(node->space));
3867 aff = extract_schedule_row(ls, node, row);
3868 return isl_map_from_aff(aff);
3871 /* Is the conditional validity dependence in the edge with index "edge_index"
3872 * violated by the latest (i.e., final) row of the schedule?
3873 * That is, is i scheduled after j
3874 * for any conditional validity dependence i -> j?
3876 static int is_violated(struct isl_sched_graph *graph, int edge_index)
3878 isl_map *src_sched, *dst_sched, *map;
3879 struct isl_sched_edge *edge = &graph->edge[edge_index];
3880 int empty;
3882 src_sched = final_row(edge->src);
3883 dst_sched = final_row(edge->dst);
3884 map = isl_map_copy(edge->map);
3885 map = isl_map_apply_domain(map, src_sched);
3886 map = isl_map_apply_range(map, dst_sched);
3887 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3888 empty = isl_map_is_empty(map);
3889 isl_map_free(map);
3891 if (empty < 0)
3892 return -1;
3894 return !empty;
3897 /* Does "graph" have any satisfied condition edges that
3898 * are adjacent to the conditional validity constraint with
3899 * domain "conditional_source" and range "conditional_sink"?
3901 * A satisfied condition is one that is not local.
3902 * If a condition was forced to be local already (i.e., marked as local)
3903 * then there is no need to check if it is in fact local.
3905 * Additionally, mark all adjacent condition edges found as local.
3907 static int has_adjacent_true_conditions(struct isl_sched_graph *graph,
3908 __isl_keep isl_union_set *conditional_source,
3909 __isl_keep isl_union_set *conditional_sink)
3911 int i;
3912 int any = 0;
3914 for (i = 0; i < graph->n_edge; ++i) {
3915 int adjacent, local;
3916 isl_union_map *condition;
3918 if (!graph->edge[i].condition)
3919 continue;
3920 if (graph->edge[i].local)
3921 continue;
3923 condition = graph->edge[i].tagged_condition;
3924 adjacent = domain_intersects(condition, conditional_sink);
3925 if (adjacent >= 0 && !adjacent)
3926 adjacent = range_intersects(condition,
3927 conditional_source);
3928 if (adjacent < 0)
3929 return -1;
3930 if (!adjacent)
3931 continue;
3933 graph->edge[i].local = 1;
3935 local = is_condition_false(&graph->edge[i]);
3936 if (local < 0)
3937 return -1;
3938 if (!local)
3939 any = 1;
3942 return any;
3945 /* Are there any violated conditional validity dependences with
3946 * adjacent condition dependences that are not local with respect
3947 * to the current schedule?
3948 * That is, is the conditional validity constraint violated?
3950 * Additionally, mark all those adjacent condition dependences as local.
3951 * We also mark those adjacent condition dependences that were not marked
3952 * as local before, but just happened to be local already. This ensures
3953 * that they remain local if the schedule is recomputed.
3955 * We first collect domain and range of all violated conditional validity
3956 * dependences and then check if there are any adjacent non-local
3957 * condition dependences.
3959 static int has_violated_conditional_constraint(isl_ctx *ctx,
3960 struct isl_sched_graph *graph)
3962 int i;
3963 int any = 0;
3964 isl_union_set *source, *sink;
3966 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3967 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3968 for (i = 0; i < graph->n_edge; ++i) {
3969 isl_union_set *uset;
3970 isl_union_map *umap;
3971 int violated;
3973 if (!graph->edge[i].conditional_validity)
3974 continue;
3976 violated = is_violated(graph, i);
3977 if (violated < 0)
3978 goto error;
3979 if (!violated)
3980 continue;
3982 any = 1;
3984 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
3985 uset = isl_union_map_domain(umap);
3986 source = isl_union_set_union(source, uset);
3987 source = isl_union_set_coalesce(source);
3989 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
3990 uset = isl_union_map_range(umap);
3991 sink = isl_union_set_union(sink, uset);
3992 sink = isl_union_set_coalesce(sink);
3995 if (any)
3996 any = has_adjacent_true_conditions(graph, source, sink);
3998 isl_union_set_free(source);
3999 isl_union_set_free(sink);
4000 return any;
4001 error:
4002 isl_union_set_free(source);
4003 isl_union_set_free(sink);
4004 return -1;
4007 /* Compute a schedule for a connected dependence graph and return
4008 * the updated schedule node.
4010 * We try to find a sequence of as many schedule rows as possible that result
4011 * in non-negative dependence distances (independent of the previous rows
4012 * in the sequence, i.e., such that the sequence is tilable), with as
4013 * many of the initial rows as possible satisfying the coincidence constraints.
4014 * If we can't find any more rows we either
4015 * - split between SCCs and start over (assuming we found an interesting
4016 * pair of SCCs between which to split)
4017 * - continue with the next band (assuming the current band has at least
4018 * one row)
4019 * - try to carry as many dependences as possible and continue with the next
4020 * band
4021 * In each case, we first insert a band node in the schedule tree
4022 * if any rows have been computed.
4024 * If Feautrier's algorithm is selected, we first recursively try to satisfy
4025 * as many validity dependences as possible. When all validity dependences
4026 * are satisfied we extend the schedule to a full-dimensional schedule.
4028 * If we manage to complete the schedule, we insert a band node
4029 * (if any schedule rows were computed) and we finish off by topologically
4030 * sorting the statements based on the remaining dependences.
4032 * If ctx->opt->schedule_outer_coincidence is set, then we force the
4033 * outermost dimension to satisfy the coincidence constraints. If this
4034 * turns out to be impossible, we fall back on the general scheme above
4035 * and try to carry as many dependences as possible.
4037 * If "graph" contains both condition and conditional validity dependences,
4038 * then we need to check that that the conditional schedule constraint
4039 * is satisfied, i.e., there are no violated conditional validity dependences
4040 * that are adjacent to any non-local condition dependences.
4041 * If there are, then we mark all those adjacent condition dependences
4042 * as local and recompute the current band. Those dependences that
4043 * are marked local will then be forced to be local.
4044 * The initial computation is performed with no dependences marked as local.
4045 * If we are lucky, then there will be no violated conditional validity
4046 * dependences adjacent to any non-local condition dependences.
4047 * Otherwise, we mark some additional condition dependences as local and
4048 * recompute. We continue this process until there are no violations left or
4049 * until we are no longer able to compute a schedule.
4050 * Since there are only a finite number of dependences,
4051 * there will only be a finite number of iterations.
4053 static __isl_give isl_schedule_node *compute_schedule_wcc(
4054 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
4056 int has_coincidence;
4057 int use_coincidence;
4058 int force_coincidence = 0;
4059 int check_conditional;
4060 isl_ctx *ctx;
4062 if (!node)
4063 return NULL;
4065 ctx = isl_schedule_node_get_ctx(node);
4066 if (detect_sccs(ctx, graph) < 0)
4067 return isl_schedule_node_free(node);
4068 if (sort_sccs(graph) < 0)
4069 return isl_schedule_node_free(node);
4071 if (compute_maxvar(graph) < 0)
4072 return isl_schedule_node_free(node);
4074 if (need_feautrier_step(ctx, graph))
4075 return compute_schedule_wcc_feautrier(node, graph);
4077 clear_local_edges(graph);
4078 check_conditional = need_condition_check(graph);
4079 has_coincidence = has_any_coincidence(graph);
4081 if (ctx->opt->schedule_outer_coincidence)
4082 force_coincidence = 1;
4084 use_coincidence = has_coincidence;
4085 while (graph->n_row < graph->maxvar) {
4086 isl_vec *sol;
4087 int violated;
4088 int coincident;
4090 graph->src_scc = -1;
4091 graph->dst_scc = -1;
4093 if (setup_lp(ctx, graph, use_coincidence) < 0)
4094 return isl_schedule_node_free(node);
4095 sol = solve_lp(graph);
4096 if (!sol)
4097 return isl_schedule_node_free(node);
4098 if (sol->size == 0) {
4099 int empty = graph->n_total_row == graph->band_start;
4101 isl_vec_free(sol);
4102 if (use_coincidence && (!force_coincidence || !empty)) {
4103 use_coincidence = 0;
4104 continue;
4106 if (!ctx->opt->schedule_maximize_band_depth && !empty)
4107 return compute_next_band(node, graph, 1);
4108 if (graph->src_scc >= 0)
4109 return compute_split_schedule(node, graph);
4110 if (!empty)
4111 return compute_next_band(node, graph, 1);
4112 return carry_dependences(node, graph);
4114 coincident = !has_coincidence || use_coincidence;
4115 if (update_schedule(graph, sol, 1, coincident) < 0)
4116 return isl_schedule_node_free(node);
4118 if (!check_conditional)
4119 continue;
4120 violated = has_violated_conditional_constraint(ctx, graph);
4121 if (violated < 0)
4122 return isl_schedule_node_free(node);
4123 if (!violated)
4124 continue;
4125 if (reset_band(graph) < 0)
4126 return isl_schedule_node_free(node);
4127 use_coincidence = has_coincidence;
4130 if (graph->n_total_row > graph->band_start) {
4131 node = insert_current_band(node, graph, 1);
4132 node = isl_schedule_node_child(node, 0);
4134 node = sort_statements(node, graph);
4135 if (graph->n_total_row > graph->band_start)
4136 node = isl_schedule_node_parent(node);
4138 return node;
4141 /* Compute a schedule for each group of nodes identified by node->scc
4142 * separately and then combine them in a sequence node (or as set node
4143 * if graph->weak is set) inserted at position "node" of the schedule tree.
4144 * Return the updated schedule node.
4146 * If "wcc" is set then each of the groups belongs to a single
4147 * weakly connected component in the dependence graph so that
4148 * there is no need for compute_sub_schedule to look for weakly
4149 * connected components.
4151 static __isl_give isl_schedule_node *compute_component_schedule(
4152 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
4153 int wcc)
4155 int component, i;
4156 int n, n_edge;
4157 int orig_total_row;
4158 isl_ctx *ctx;
4159 isl_union_set_list *filters;
4161 if (!node)
4162 return NULL;
4163 ctx = isl_schedule_node_get_ctx(node);
4165 filters = extract_sccs(ctx, graph);
4166 if (graph->weak)
4167 node = isl_schedule_node_insert_set(node, filters);
4168 else
4169 node = isl_schedule_node_insert_sequence(node, filters);
4171 orig_total_row = graph->n_total_row;
4172 for (component = 0; component < graph->scc; ++component) {
4173 n = 0;
4174 for (i = 0; i < graph->n; ++i)
4175 if (graph->node[i].scc == component)
4176 n++;
4177 n_edge = 0;
4178 for (i = 0; i < graph->n_edge; ++i)
4179 if (graph->edge[i].src->scc == component &&
4180 graph->edge[i].dst->scc == component)
4181 n_edge++;
4183 node = isl_schedule_node_child(node, component);
4184 node = isl_schedule_node_child(node, 0);
4185 node = compute_sub_schedule(node, ctx, graph, n, n_edge,
4186 &node_scc_exactly,
4187 &edge_scc_exactly, component, wcc);
4188 node = isl_schedule_node_parent(node);
4189 node = isl_schedule_node_parent(node);
4190 graph->n_total_row = orig_total_row;
4193 return node;
4196 /* Compute a schedule for the given dependence graph and insert it at "node".
4197 * Return the updated schedule node.
4199 * We first check if the graph is connected (through validity and conditional
4200 * validity dependences) and, if not, compute a schedule
4201 * for each component separately.
4202 * If schedule_fuse is set to minimal fusion, then we check for strongly
4203 * connected components instead and compute a separate schedule for
4204 * each such strongly connected component.
4206 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
4207 struct isl_sched_graph *graph)
4209 isl_ctx *ctx;
4211 if (!node)
4212 return NULL;
4214 ctx = isl_schedule_node_get_ctx(node);
4215 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN) {
4216 if (detect_sccs(ctx, graph) < 0)
4217 return isl_schedule_node_free(node);
4218 } else {
4219 if (detect_wccs(ctx, graph) < 0)
4220 return isl_schedule_node_free(node);
4223 if (graph->scc > 1)
4224 return compute_component_schedule(node, graph, 1);
4226 return compute_schedule_wcc(node, graph);
4229 /* Compute a schedule on sc->domain that respects the given schedule
4230 * constraints.
4232 * In particular, the schedule respects all the validity dependences.
4233 * If the default isl scheduling algorithm is used, it tries to minimize
4234 * the dependence distances over the proximity dependences.
4235 * If Feautrier's scheduling algorithm is used, the proximity dependence
4236 * distances are only minimized during the extension to a full-dimensional
4237 * schedule.
4239 * If there are any condition and conditional validity dependences,
4240 * then the conditional validity dependences may be violated inside
4241 * a tilable band, provided they have no adjacent non-local
4242 * condition dependences.
4244 * The context is included in the domain before the nodes of
4245 * the graphs are extracted in order to be able to exploit
4246 * any possible additional equalities.
4247 * However, the returned schedule contains the original domain
4248 * (before this intersection).
4250 __isl_give isl_schedule *isl_schedule_constraints_compute_schedule(
4251 __isl_take isl_schedule_constraints *sc)
4253 isl_ctx *ctx = isl_schedule_constraints_get_ctx(sc);
4254 struct isl_sched_graph graph = { 0 };
4255 isl_schedule *sched;
4256 isl_schedule_node *node;
4257 isl_union_set *domain;
4258 struct isl_extract_edge_data data;
4259 enum isl_edge_type i;
4260 int r;
4262 sc = isl_schedule_constraints_align_params(sc);
4263 if (!sc)
4264 return NULL;
4266 graph.n = isl_union_set_n_set(sc->domain);
4267 if (graph.n == 0) {
4268 isl_union_set *domain = isl_union_set_copy(sc->domain);
4269 sched = isl_schedule_from_domain(domain);
4270 goto done;
4272 if (graph_alloc(ctx, &graph, graph.n,
4273 isl_schedule_constraints_n_map(sc)) < 0)
4274 goto error;
4275 if (compute_max_row(&graph, sc) < 0)
4276 goto error;
4277 graph.root = 1;
4278 graph.n = 0;
4279 domain = isl_union_set_copy(sc->domain);
4280 domain = isl_union_set_intersect_params(domain,
4281 isl_set_copy(sc->context));
4282 r = isl_union_set_foreach_set(domain, &extract_node, &graph);
4283 isl_union_set_free(domain);
4284 if (r < 0)
4285 goto error;
4286 if (graph_init_table(ctx, &graph) < 0)
4287 goto error;
4288 for (i = isl_edge_first; i <= isl_edge_last; ++i)
4289 graph.max_edge[i] = isl_union_map_n_map(sc->constraint[i]);
4290 if (graph_init_edge_tables(ctx, &graph) < 0)
4291 goto error;
4292 graph.n_edge = 0;
4293 data.graph = &graph;
4294 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
4295 data.type = i;
4296 if (isl_union_map_foreach_map(sc->constraint[i],
4297 &extract_edge, &data) < 0)
4298 goto error;
4301 node = isl_schedule_node_from_domain(isl_union_set_copy(sc->domain));
4302 node = isl_schedule_node_child(node, 0);
4303 node = compute_schedule(node, &graph);
4304 sched = isl_schedule_node_get_schedule(node);
4305 isl_schedule_node_free(node);
4307 done:
4308 graph_free(ctx, &graph);
4309 isl_schedule_constraints_free(sc);
4311 return sched;
4312 error:
4313 graph_free(ctx, &graph);
4314 isl_schedule_constraints_free(sc);
4315 return NULL;
4318 /* Compute a schedule for the given union of domains that respects
4319 * all the validity dependences and minimizes
4320 * the dependence distances over the proximity dependences.
4322 * This function is kept for backward compatibility.
4324 __isl_give isl_schedule *isl_union_set_compute_schedule(
4325 __isl_take isl_union_set *domain,
4326 __isl_take isl_union_map *validity,
4327 __isl_take isl_union_map *proximity)
4329 isl_schedule_constraints *sc;
4331 sc = isl_schedule_constraints_on_domain(domain);
4332 sc = isl_schedule_constraints_set_validity(sc, validity);
4333 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4335 return isl_schedule_constraints_compute_schedule(sc);