isl_scheduler.c: copy_edges: extract out graph_edge_tables_add
[isl.git] / isl_scheduler.c
blobe14f041106f4624264f39f48e6b38585aea46314
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2014 Ecole Normale Superieure
4 * Copyright 2015-2016 Sven Verdoolaege
5 * Copyright 2016 INRIA Paris
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
14 * CS 42112, 75589 Paris Cedex 12, France
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
19 #include <isl_space_private.h>
20 #include <isl_aff_private.h>
21 #include <isl/hash.h>
22 #include <isl/constraint.h>
23 #include <isl/schedule.h>
24 #include <isl_schedule_constraints.h>
25 #include <isl/schedule_node.h>
26 #include <isl_mat_private.h>
27 #include <isl_vec_private.h>
28 #include <isl/set.h>
29 #include <isl/union_set.h>
30 #include <isl_seq.h>
31 #include <isl_tab.h>
32 #include <isl_dim_map.h>
33 #include <isl/map_to_basic_set.h>
34 #include <isl_sort.h>
35 #include <isl_options_private.h>
36 #include <isl_tarjan.h>
37 #include <isl_morph.h>
38 #include <isl/ilp.h>
39 #include <isl_val_private.h>
42 * The scheduling algorithm implemented in this file was inspired by
43 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
44 * Parallelization and Locality Optimization in the Polyhedral Model".
48 /* Internal information about a node that is used during the construction
49 * of a schedule.
50 * space represents the space in which the domain lives
51 * sched is a matrix representation of the schedule being constructed
52 * for this node; if compressed is set, then this schedule is
53 * defined over the compressed domain space
54 * sched_map is an isl_map representation of the same (partial) schedule
55 * sched_map may be NULL; if compressed is set, then this map
56 * is defined over the uncompressed domain space
57 * rank is the number of linearly independent rows in the linear part
58 * of sched
59 * the columns of cmap represent a change of basis for the schedule
60 * coefficients; the first rank columns span the linear part of
61 * the schedule rows
62 * cinv is the inverse of cmap.
63 * ctrans is the transpose of cmap.
64 * start is the first variable in the LP problem in the sequences that
65 * represents the schedule coefficients of this node
66 * nvar is the dimension of the domain
67 * nparam is the number of parameters or 0 if we are not constructing
68 * a parametric schedule
70 * If compressed is set, then hull represents the constraints
71 * that were used to derive the compression, while compress and
72 * decompress map the original space to the compressed space and
73 * vice versa.
75 * scc is the index of SCC (or WCC) this node belongs to
77 * "cluster" is only used inside extract_clusters and identifies
78 * the cluster of SCCs that the node belongs to.
80 * coincident contains a boolean for each of the rows of the schedule,
81 * indicating whether the corresponding scheduling dimension satisfies
82 * the coincidence constraints in the sense that the corresponding
83 * dependence distances are zero.
85 * If the schedule_treat_coalescing option is set, then
86 * "sizes" contains the sizes of the (compressed) instance set
87 * in each direction. If there is no fixed size in a given direction,
88 * then the corresponding size value is set to infinity.
89 * If the schedule_treat_coalescing option or the schedule_max_coefficient
90 * option is set, then "max" contains the maximal values for
91 * schedule coefficients of the (compressed) variables. If no bound
92 * needs to be imposed on a particular variable, then the corresponding
93 * value is negative.
95 struct isl_sched_node {
96 isl_space *space;
97 int compressed;
98 isl_set *hull;
99 isl_multi_aff *compress;
100 isl_multi_aff *decompress;
101 isl_mat *sched;
102 isl_map *sched_map;
103 int rank;
104 isl_mat *cmap;
105 isl_mat *cinv;
106 isl_mat *ctrans;
107 int start;
108 int nvar;
109 int nparam;
111 int scc;
112 int cluster;
114 int *coincident;
116 isl_multi_val *sizes;
117 isl_vec *max;
120 static int node_has_space(const void *entry, const void *val)
122 struct isl_sched_node *node = (struct isl_sched_node *)entry;
123 isl_space *dim = (isl_space *)val;
125 return isl_space_is_equal(node->space, dim);
128 static int node_scc_exactly(struct isl_sched_node *node, int scc)
130 return node->scc == scc;
133 static int node_scc_at_most(struct isl_sched_node *node, int scc)
135 return node->scc <= scc;
138 static int node_scc_at_least(struct isl_sched_node *node, int scc)
140 return node->scc >= scc;
143 /* An edge in the dependence graph. An edge may be used to
144 * ensure validity of the generated schedule, to minimize the dependence
145 * distance or both
147 * map is the dependence relation, with i -> j in the map if j depends on i
148 * tagged_condition and tagged_validity contain the union of all tagged
149 * condition or conditional validity dependence relations that
150 * specialize the dependence relation "map"; that is,
151 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
152 * or "tagged_validity", then i -> j is an element of "map".
153 * If these fields are NULL, then they represent the empty relation.
154 * src is the source node
155 * dst is the sink node
157 * types is a bit vector containing the types of this edge.
158 * validity is set if the edge is used to ensure correctness
159 * coincidence is used to enforce zero dependence distances
160 * proximity is set if the edge is used to minimize dependence distances
161 * condition is set if the edge represents a condition
162 * for a conditional validity schedule constraint
163 * local can only be set for condition edges and indicates that
164 * the dependence distance over the edge should be zero
165 * conditional_validity is set if the edge is used to conditionally
166 * ensure correctness
168 * For validity edges, start and end mark the sequence of inequality
169 * constraints in the LP problem that encode the validity constraint
170 * corresponding to this edge.
172 * During clustering, an edge may be marked "no_merge" if it should
173 * not be used to merge clusters.
174 * The weight is also only used during clustering and it is
175 * an indication of how many schedule dimensions on either side
176 * of the schedule constraints can be aligned.
177 * If the weight is negative, then this means that this edge was postponed
178 * by has_bounded_distances or any_no_merge. The original weight can
179 * be retrieved by adding 1 + graph->max_weight, with "graph"
180 * the graph containing this edge.
182 struct isl_sched_edge {
183 isl_map *map;
184 isl_union_map *tagged_condition;
185 isl_union_map *tagged_validity;
187 struct isl_sched_node *src;
188 struct isl_sched_node *dst;
190 unsigned types;
192 int start;
193 int end;
195 int no_merge;
196 int weight;
199 /* Is "edge" marked as being of type "type"?
201 static int is_type(struct isl_sched_edge *edge, enum isl_edge_type type)
203 return ISL_FL_ISSET(edge->types, 1 << type);
206 /* Mark "edge" as being of type "type".
208 static void set_type(struct isl_sched_edge *edge, enum isl_edge_type type)
210 ISL_FL_SET(edge->types, 1 << type);
213 /* No longer mark "edge" as being of type "type"?
215 static void clear_type(struct isl_sched_edge *edge, enum isl_edge_type type)
217 ISL_FL_CLR(edge->types, 1 << type);
220 /* Is "edge" marked as a validity edge?
222 static int is_validity(struct isl_sched_edge *edge)
224 return is_type(edge, isl_edge_validity);
227 /* Mark "edge" as a validity edge.
229 static void set_validity(struct isl_sched_edge *edge)
231 set_type(edge, isl_edge_validity);
234 /* Is "edge" marked as a proximity edge?
236 static int is_proximity(struct isl_sched_edge *edge)
238 return is_type(edge, isl_edge_proximity);
241 /* Is "edge" marked as a local edge?
243 static int is_local(struct isl_sched_edge *edge)
245 return is_type(edge, isl_edge_local);
248 /* Mark "edge" as a local edge.
250 static void set_local(struct isl_sched_edge *edge)
252 set_type(edge, isl_edge_local);
255 /* No longer mark "edge" as a local edge.
257 static void clear_local(struct isl_sched_edge *edge)
259 clear_type(edge, isl_edge_local);
262 /* Is "edge" marked as a coincidence edge?
264 static int is_coincidence(struct isl_sched_edge *edge)
266 return is_type(edge, isl_edge_coincidence);
269 /* Is "edge" marked as a condition edge?
271 static int is_condition(struct isl_sched_edge *edge)
273 return is_type(edge, isl_edge_condition);
276 /* Is "edge" marked as a conditional validity edge?
278 static int is_conditional_validity(struct isl_sched_edge *edge)
280 return is_type(edge, isl_edge_conditional_validity);
283 /* Internal information about the dependence graph used during
284 * the construction of the schedule.
286 * intra_hmap is a cache, mapping dependence relations to their dual,
287 * for dependences from a node to itself
288 * inter_hmap is a cache, mapping dependence relations to their dual,
289 * for dependences between distinct nodes
290 * if compression is involved then the key for these maps
291 * is the original, uncompressed dependence relation, while
292 * the value is the dual of the compressed dependence relation.
294 * n is the number of nodes
295 * node is the list of nodes
296 * maxvar is the maximal number of variables over all nodes
297 * max_row is the allocated number of rows in the schedule
298 * n_row is the current (maximal) number of linearly independent
299 * rows in the node schedules
300 * n_total_row is the current number of rows in the node schedules
301 * band_start is the starting row in the node schedules of the current band
302 * root is set if this graph is the original dependence graph,
303 * without any splitting
305 * sorted contains a list of node indices sorted according to the
306 * SCC to which a node belongs
308 * n_edge is the number of edges
309 * edge is the list of edges
310 * max_edge contains the maximal number of edges of each type;
311 * in particular, it contains the number of edges in the inital graph.
312 * edge_table contains pointers into the edge array, hashed on the source
313 * and sink spaces; there is one such table for each type;
314 * a given edge may be referenced from more than one table
315 * if the corresponding relation appears in more than one of the
316 * sets of dependences; however, for each type there is only
317 * a single edge between a given pair of source and sink space
318 * in the entire graph
320 * node_table contains pointers into the node array, hashed on the space
322 * region contains a list of variable sequences that should be non-trivial
324 * lp contains the (I)LP problem used to obtain new schedule rows
326 * src_scc and dst_scc are the source and sink SCCs of an edge with
327 * conflicting constraints
329 * scc represents the number of components
330 * weak is set if the components are weakly connected
332 * max_weight is used during clustering and represents the maximal
333 * weight of the relevant proximity edges.
335 struct isl_sched_graph {
336 isl_map_to_basic_set *intra_hmap;
337 isl_map_to_basic_set *inter_hmap;
339 struct isl_sched_node *node;
340 int n;
341 int maxvar;
342 int max_row;
343 int n_row;
345 int *sorted;
347 int n_total_row;
348 int band_start;
350 int root;
352 struct isl_sched_edge *edge;
353 int n_edge;
354 int max_edge[isl_edge_last + 1];
355 struct isl_hash_table *edge_table[isl_edge_last + 1];
357 struct isl_hash_table *node_table;
358 struct isl_region *region;
360 isl_basic_set *lp;
362 int src_scc;
363 int dst_scc;
365 int scc;
366 int weak;
368 int max_weight;
371 /* Initialize node_table based on the list of nodes.
373 static int graph_init_table(isl_ctx *ctx, struct isl_sched_graph *graph)
375 int i;
377 graph->node_table = isl_hash_table_alloc(ctx, graph->n);
378 if (!graph->node_table)
379 return -1;
381 for (i = 0; i < graph->n; ++i) {
382 struct isl_hash_table_entry *entry;
383 uint32_t hash;
385 hash = isl_space_get_hash(graph->node[i].space);
386 entry = isl_hash_table_find(ctx, graph->node_table, hash,
387 &node_has_space,
388 graph->node[i].space, 1);
389 if (!entry)
390 return -1;
391 entry->data = &graph->node[i];
394 return 0;
397 /* Return a pointer to the node that lives within the given space,
398 * or NULL if there is no such node.
400 static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
401 struct isl_sched_graph *graph, __isl_keep isl_space *dim)
403 struct isl_hash_table_entry *entry;
404 uint32_t hash;
406 hash = isl_space_get_hash(dim);
407 entry = isl_hash_table_find(ctx, graph->node_table, hash,
408 &node_has_space, dim, 0);
410 return entry ? entry->data : NULL;
413 static int edge_has_src_and_dst(const void *entry, const void *val)
415 const struct isl_sched_edge *edge = entry;
416 const struct isl_sched_edge *temp = val;
418 return edge->src == temp->src && edge->dst == temp->dst;
421 /* Add the given edge to graph->edge_table[type].
423 static isl_stat graph_edge_table_add(isl_ctx *ctx,
424 struct isl_sched_graph *graph, enum isl_edge_type type,
425 struct isl_sched_edge *edge)
427 struct isl_hash_table_entry *entry;
428 uint32_t hash;
430 hash = isl_hash_init();
431 hash = isl_hash_builtin(hash, edge->src);
432 hash = isl_hash_builtin(hash, edge->dst);
433 entry = isl_hash_table_find(ctx, graph->edge_table[type], hash,
434 &edge_has_src_and_dst, edge, 1);
435 if (!entry)
436 return isl_stat_error;
437 entry->data = edge;
439 return isl_stat_ok;
442 /* Add "edge" to all relevant edge tables.
443 * That is, for every type of the edge, add it to the corresponding table.
445 static isl_stat graph_edge_tables_add(isl_ctx *ctx,
446 struct isl_sched_graph *graph, struct isl_sched_edge *edge)
448 enum isl_edge_type t;
450 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
451 if (!is_type(edge, t))
452 continue;
453 if (graph_edge_table_add(ctx, graph, t, edge) < 0)
454 return isl_stat_error;
457 return isl_stat_ok;
460 /* Allocate the edge_tables based on the maximal number of edges of
461 * each type.
463 static int graph_init_edge_tables(isl_ctx *ctx, struct isl_sched_graph *graph)
465 int i;
467 for (i = 0; i <= isl_edge_last; ++i) {
468 graph->edge_table[i] = isl_hash_table_alloc(ctx,
469 graph->max_edge[i]);
470 if (!graph->edge_table[i])
471 return -1;
474 return 0;
477 /* If graph->edge_table[type] contains an edge from the given source
478 * to the given destination, then return the hash table entry of this edge.
479 * Otherwise, return NULL.
481 static struct isl_hash_table_entry *graph_find_edge_entry(
482 struct isl_sched_graph *graph,
483 enum isl_edge_type type,
484 struct isl_sched_node *src, struct isl_sched_node *dst)
486 isl_ctx *ctx = isl_space_get_ctx(src->space);
487 uint32_t hash;
488 struct isl_sched_edge temp = { .src = src, .dst = dst };
490 hash = isl_hash_init();
491 hash = isl_hash_builtin(hash, temp.src);
492 hash = isl_hash_builtin(hash, temp.dst);
493 return isl_hash_table_find(ctx, graph->edge_table[type], hash,
494 &edge_has_src_and_dst, &temp, 0);
498 /* If graph->edge_table[type] contains an edge from the given source
499 * to the given destination, then return this edge.
500 * Otherwise, return NULL.
502 static struct isl_sched_edge *graph_find_edge(struct isl_sched_graph *graph,
503 enum isl_edge_type type,
504 struct isl_sched_node *src, struct isl_sched_node *dst)
506 struct isl_hash_table_entry *entry;
508 entry = graph_find_edge_entry(graph, type, src, dst);
509 if (!entry)
510 return NULL;
512 return entry->data;
515 /* Check whether the dependence graph has an edge of the given type
516 * between the given two nodes.
518 static isl_bool graph_has_edge(struct isl_sched_graph *graph,
519 enum isl_edge_type type,
520 struct isl_sched_node *src, struct isl_sched_node *dst)
522 struct isl_sched_edge *edge;
523 isl_bool empty;
525 edge = graph_find_edge(graph, type, src, dst);
526 if (!edge)
527 return 0;
529 empty = isl_map_plain_is_empty(edge->map);
530 if (empty < 0)
531 return isl_bool_error;
533 return !empty;
536 /* Look for any edge with the same src, dst and map fields as "model".
538 * Return the matching edge if one can be found.
539 * Return "model" if no matching edge is found.
540 * Return NULL on error.
542 static struct isl_sched_edge *graph_find_matching_edge(
543 struct isl_sched_graph *graph, struct isl_sched_edge *model)
545 enum isl_edge_type i;
546 struct isl_sched_edge *edge;
548 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
549 int is_equal;
551 edge = graph_find_edge(graph, i, model->src, model->dst);
552 if (!edge)
553 continue;
554 is_equal = isl_map_plain_is_equal(model->map, edge->map);
555 if (is_equal < 0)
556 return NULL;
557 if (is_equal)
558 return edge;
561 return model;
564 /* Remove the given edge from all the edge_tables that refer to it.
566 static void graph_remove_edge(struct isl_sched_graph *graph,
567 struct isl_sched_edge *edge)
569 isl_ctx *ctx = isl_map_get_ctx(edge->map);
570 enum isl_edge_type i;
572 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
573 struct isl_hash_table_entry *entry;
575 entry = graph_find_edge_entry(graph, i, edge->src, edge->dst);
576 if (!entry)
577 continue;
578 if (entry->data != edge)
579 continue;
580 isl_hash_table_remove(ctx, graph->edge_table[i], entry);
584 /* Check whether the dependence graph has any edge
585 * between the given two nodes.
587 static isl_bool graph_has_any_edge(struct isl_sched_graph *graph,
588 struct isl_sched_node *src, struct isl_sched_node *dst)
590 enum isl_edge_type i;
591 isl_bool r;
593 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
594 r = graph_has_edge(graph, i, src, dst);
595 if (r < 0 || r)
596 return r;
599 return r;
602 /* Check whether the dependence graph has a validity edge
603 * between the given two nodes.
605 * Conditional validity edges are essentially validity edges that
606 * can be ignored if the corresponding condition edges are iteration private.
607 * Here, we are only checking for the presence of validity
608 * edges, so we need to consider the conditional validity edges too.
609 * In particular, this function is used during the detection
610 * of strongly connected components and we cannot ignore
611 * conditional validity edges during this detection.
613 static isl_bool graph_has_validity_edge(struct isl_sched_graph *graph,
614 struct isl_sched_node *src, struct isl_sched_node *dst)
616 isl_bool r;
618 r = graph_has_edge(graph, isl_edge_validity, src, dst);
619 if (r < 0 || r)
620 return r;
622 return graph_has_edge(graph, isl_edge_conditional_validity, src, dst);
625 static int graph_alloc(isl_ctx *ctx, struct isl_sched_graph *graph,
626 int n_node, int n_edge)
628 int i;
630 graph->n = n_node;
631 graph->n_edge = n_edge;
632 graph->node = isl_calloc_array(ctx, struct isl_sched_node, graph->n);
633 graph->sorted = isl_calloc_array(ctx, int, graph->n);
634 graph->region = isl_alloc_array(ctx, struct isl_region, graph->n);
635 graph->edge = isl_calloc_array(ctx,
636 struct isl_sched_edge, graph->n_edge);
638 graph->intra_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
639 graph->inter_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
641 if (!graph->node || !graph->region || (graph->n_edge && !graph->edge) ||
642 !graph->sorted)
643 return -1;
645 for(i = 0; i < graph->n; ++i)
646 graph->sorted[i] = i;
648 return 0;
651 static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
653 int i;
655 isl_map_to_basic_set_free(graph->intra_hmap);
656 isl_map_to_basic_set_free(graph->inter_hmap);
658 if (graph->node)
659 for (i = 0; i < graph->n; ++i) {
660 isl_space_free(graph->node[i].space);
661 isl_set_free(graph->node[i].hull);
662 isl_multi_aff_free(graph->node[i].compress);
663 isl_multi_aff_free(graph->node[i].decompress);
664 isl_mat_free(graph->node[i].sched);
665 isl_map_free(graph->node[i].sched_map);
666 isl_mat_free(graph->node[i].cmap);
667 isl_mat_free(graph->node[i].cinv);
668 isl_mat_free(graph->node[i].ctrans);
669 if (graph->root)
670 free(graph->node[i].coincident);
671 isl_multi_val_free(graph->node[i].sizes);
672 isl_vec_free(graph->node[i].max);
674 free(graph->node);
675 free(graph->sorted);
676 if (graph->edge)
677 for (i = 0; i < graph->n_edge; ++i) {
678 isl_map_free(graph->edge[i].map);
679 isl_union_map_free(graph->edge[i].tagged_condition);
680 isl_union_map_free(graph->edge[i].tagged_validity);
682 free(graph->edge);
683 free(graph->region);
684 for (i = 0; i <= isl_edge_last; ++i)
685 isl_hash_table_free(ctx, graph->edge_table[i]);
686 isl_hash_table_free(ctx, graph->node_table);
687 isl_basic_set_free(graph->lp);
690 /* For each "set" on which this function is called, increment
691 * graph->n by one and update graph->maxvar.
693 static isl_stat init_n_maxvar(__isl_take isl_set *set, void *user)
695 struct isl_sched_graph *graph = user;
696 int nvar = isl_set_dim(set, isl_dim_set);
698 graph->n++;
699 if (nvar > graph->maxvar)
700 graph->maxvar = nvar;
702 isl_set_free(set);
704 return isl_stat_ok;
707 /* Compute the number of rows that should be allocated for the schedule.
708 * In particular, we need one row for each variable or one row
709 * for each basic map in the dependences.
710 * Note that it is practically impossible to exhaust both
711 * the number of dependences and the number of variables.
713 static isl_stat compute_max_row(struct isl_sched_graph *graph,
714 __isl_keep isl_schedule_constraints *sc)
716 int n_edge;
717 isl_stat r;
718 isl_union_set *domain;
720 graph->n = 0;
721 graph->maxvar = 0;
722 domain = isl_schedule_constraints_get_domain(sc);
723 r = isl_union_set_foreach_set(domain, &init_n_maxvar, graph);
724 isl_union_set_free(domain);
725 if (r < 0)
726 return isl_stat_error;
727 n_edge = isl_schedule_constraints_n_basic_map(sc);
728 if (n_edge < 0)
729 return isl_stat_error;
730 graph->max_row = n_edge + graph->maxvar;
732 return isl_stat_ok;
735 /* Does "bset" have any defining equalities for its set variables?
737 static int has_any_defining_equality(__isl_keep isl_basic_set *bset)
739 int i, n;
741 if (!bset)
742 return -1;
744 n = isl_basic_set_dim(bset, isl_dim_set);
745 for (i = 0; i < n; ++i) {
746 int has;
748 has = isl_basic_set_has_defining_equality(bset, isl_dim_set, i,
749 NULL);
750 if (has < 0 || has)
751 return has;
754 return 0;
757 /* Set the entries of node->max to the value of the schedule_max_coefficient
758 * option, if set.
760 static isl_stat set_max_coefficient(isl_ctx *ctx, struct isl_sched_node *node)
762 int max;
764 max = isl_options_get_schedule_max_coefficient(ctx);
765 if (max == -1)
766 return isl_stat_ok;
768 node->max = isl_vec_alloc(ctx, node->nvar);
769 node->max = isl_vec_set_si(node->max, max);
770 if (!node->max)
771 return isl_stat_error;
773 return isl_stat_ok;
776 /* Set the entries of node->max to the minimum of the schedule_max_coefficient
777 * option (if set) and half of the minimum of the sizes in the other
778 * dimensions. If the minimum of the sizes is one, half of the size
779 * is zero and this value is reset to one.
780 * If the global minimum is unbounded (i.e., if both
781 * the schedule_max_coefficient is not set and the sizes in the other
782 * dimensions are unbounded), then store a negative value.
783 * If the schedule coefficient is close to the size of the instance set
784 * in another dimension, then the schedule may represent a loop
785 * coalescing transformation (especially if the coefficient
786 * in that other dimension is one). Forcing the coefficient to be
787 * smaller than or equal to half the minimal size should avoid this
788 * situation.
790 static isl_stat compute_max_coefficient(isl_ctx *ctx,
791 struct isl_sched_node *node)
793 int max;
794 int i, j;
795 isl_vec *v;
797 max = isl_options_get_schedule_max_coefficient(ctx);
798 v = isl_vec_alloc(ctx, node->nvar);
799 if (!v)
800 return isl_stat_error;
802 for (i = 0; i < node->nvar; ++i) {
803 isl_int_set_si(v->el[i], max);
804 isl_int_mul_si(v->el[i], v->el[i], 2);
807 for (i = 0; i < node->nvar; ++i) {
808 isl_val *size;
810 size = isl_multi_val_get_val(node->sizes, i);
811 if (!size)
812 goto error;
813 if (!isl_val_is_int(size)) {
814 isl_val_free(size);
815 continue;
817 for (j = 0; j < node->nvar; ++j) {
818 if (j == i)
819 continue;
820 if (isl_int_is_neg(v->el[j]) ||
821 isl_int_gt(v->el[j], size->n))
822 isl_int_set(v->el[j], size->n);
824 isl_val_free(size);
827 for (i = 0; i < node->nvar; ++i) {
828 isl_int_fdiv_q_ui(v->el[i], v->el[i], 2);
829 if (isl_int_is_zero(v->el[i]))
830 isl_int_set_si(v->el[i], 1);
833 node->max = v;
834 return isl_stat_ok;
835 error:
836 isl_vec_free(v);
837 return isl_stat_error;
840 /* Compute and return the size of "set" in dimension "dim".
841 * The size is taken to be the difference in values for that variable
842 * for fixed values of the other variables.
843 * In particular, the variable is first isolated from the other variables
844 * in the range of a map
846 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [i_dim]
848 * and then duplicated
850 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [[i_dim] -> [i_dim']]
852 * The shared variables are then projected out and the maximal value
853 * of i_dim' - i_dim is computed.
855 static __isl_give isl_val *compute_size(__isl_take isl_set *set, int dim)
857 isl_map *map;
858 isl_local_space *ls;
859 isl_aff *obj;
860 isl_val *v;
862 map = isl_set_project_onto_map(set, isl_dim_set, dim, 1);
863 map = isl_map_project_out(map, isl_dim_in, dim, 1);
864 map = isl_map_range_product(map, isl_map_copy(map));
865 map = isl_set_unwrap(isl_map_range(map));
866 set = isl_map_deltas(map);
867 ls = isl_local_space_from_space(isl_set_get_space(set));
868 obj = isl_aff_var_on_domain(ls, isl_dim_set, 0);
869 v = isl_set_max_val(set, obj);
870 isl_aff_free(obj);
871 isl_set_free(set);
873 return v;
876 /* Compute the size of the instance set "set" of "node", after compression,
877 * as well as bounds on the corresponding coefficients, if needed.
879 * The sizes are needed when the schedule_treat_coalescing option is set.
880 * The bounds are needed when the schedule_treat_coalescing option or
881 * the schedule_max_coefficient option is set.
883 * If the schedule_treat_coalescing option is not set, then at most
884 * the bounds need to be set and this is done in set_max_coefficient.
885 * Otherwise, compress the domain if needed, compute the size
886 * in each direction and store the results in node->size.
887 * Finally, set the bounds on the coefficients based on the sizes
888 * and the schedule_max_coefficient option in compute_max_coefficient.
890 static isl_stat compute_sizes_and_max(isl_ctx *ctx, struct isl_sched_node *node,
891 __isl_take isl_set *set)
893 int j, n;
894 isl_multi_val *mv;
896 if (!isl_options_get_schedule_treat_coalescing(ctx)) {
897 isl_set_free(set);
898 return set_max_coefficient(ctx, node);
901 if (node->compressed)
902 set = isl_set_preimage_multi_aff(set,
903 isl_multi_aff_copy(node->decompress));
904 mv = isl_multi_val_zero(isl_set_get_space(set));
905 n = isl_set_dim(set, isl_dim_set);
906 for (j = 0; j < n; ++j) {
907 isl_val *v;
909 v = compute_size(isl_set_copy(set), j);
910 mv = isl_multi_val_set_val(mv, j, v);
912 node->sizes = mv;
913 isl_set_free(set);
914 if (!node->sizes)
915 return isl_stat_error;
916 return compute_max_coefficient(ctx, node);
919 /* Add a new node to the graph representing the given instance set.
920 * "nvar" is the (possibly compressed) number of variables and
921 * may be smaller than then number of set variables in "set"
922 * if "compressed" is set.
923 * If "compressed" is set, then "hull" represents the constraints
924 * that were used to derive the compression, while "compress" and
925 * "decompress" map the original space to the compressed space and
926 * vice versa.
927 * If "compressed" is not set, then "hull", "compress" and "decompress"
928 * should be NULL.
930 * Compute the size of the instance set and bounds on the coefficients,
931 * if needed.
933 static isl_stat add_node(struct isl_sched_graph *graph,
934 __isl_take isl_set *set, int nvar, int compressed,
935 __isl_take isl_set *hull, __isl_take isl_multi_aff *compress,
936 __isl_take isl_multi_aff *decompress)
938 int nparam;
939 isl_ctx *ctx;
940 isl_mat *sched;
941 isl_space *space;
942 int *coincident;
943 struct isl_sched_node *node;
945 if (!set)
946 return isl_stat_error;
948 ctx = isl_set_get_ctx(set);
949 nparam = isl_set_dim(set, isl_dim_param);
950 if (!ctx->opt->schedule_parametric)
951 nparam = 0;
952 sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
953 node = &graph->node[graph->n];
954 graph->n++;
955 space = isl_set_get_space(set);
956 node->space = space;
957 node->nvar = nvar;
958 node->nparam = nparam;
959 node->sched = sched;
960 node->sched_map = NULL;
961 coincident = isl_calloc_array(ctx, int, graph->max_row);
962 node->coincident = coincident;
963 node->compressed = compressed;
964 node->hull = hull;
965 node->compress = compress;
966 node->decompress = decompress;
967 if (compute_sizes_and_max(ctx, node, set) < 0)
968 return isl_stat_error;
970 if (!space || !sched || (graph->max_row && !coincident))
971 return isl_stat_error;
972 if (compressed && (!hull || !compress || !decompress))
973 return isl_stat_error;
975 return isl_stat_ok;
978 /* Add a new node to the graph representing the given set.
980 * If any of the set variables is defined by an equality, then
981 * we perform variable compression such that we can perform
982 * the scheduling on the compressed domain.
984 static isl_stat extract_node(__isl_take isl_set *set, void *user)
986 int nvar;
987 int has_equality;
988 isl_basic_set *hull;
989 isl_set *hull_set;
990 isl_morph *morph;
991 isl_multi_aff *compress, *decompress;
992 struct isl_sched_graph *graph = user;
994 hull = isl_set_affine_hull(isl_set_copy(set));
995 hull = isl_basic_set_remove_divs(hull);
996 nvar = isl_set_dim(set, isl_dim_set);
997 has_equality = has_any_defining_equality(hull);
999 if (has_equality < 0)
1000 goto error;
1001 if (!has_equality) {
1002 isl_basic_set_free(hull);
1003 return add_node(graph, set, nvar, 0, NULL, NULL, NULL);
1006 morph = isl_basic_set_variable_compression(hull, isl_dim_set);
1007 nvar = isl_morph_ran_dim(morph, isl_dim_set);
1008 compress = isl_morph_get_var_multi_aff(morph);
1009 morph = isl_morph_inverse(morph);
1010 decompress = isl_morph_get_var_multi_aff(morph);
1011 isl_morph_free(morph);
1013 hull_set = isl_set_from_basic_set(hull);
1014 return add_node(graph, set, nvar, 1, hull_set, compress, decompress);
1015 error:
1016 isl_basic_set_free(hull);
1017 isl_set_free(set);
1018 return isl_stat_error;
1021 struct isl_extract_edge_data {
1022 enum isl_edge_type type;
1023 struct isl_sched_graph *graph;
1026 /* Merge edge2 into edge1, freeing the contents of edge2.
1027 * Return 0 on success and -1 on failure.
1029 * edge1 and edge2 are assumed to have the same value for the map field.
1031 static int merge_edge(struct isl_sched_edge *edge1,
1032 struct isl_sched_edge *edge2)
1034 edge1->types |= edge2->types;
1035 isl_map_free(edge2->map);
1037 if (is_condition(edge2)) {
1038 if (!edge1->tagged_condition)
1039 edge1->tagged_condition = edge2->tagged_condition;
1040 else
1041 edge1->tagged_condition =
1042 isl_union_map_union(edge1->tagged_condition,
1043 edge2->tagged_condition);
1046 if (is_conditional_validity(edge2)) {
1047 if (!edge1->tagged_validity)
1048 edge1->tagged_validity = edge2->tagged_validity;
1049 else
1050 edge1->tagged_validity =
1051 isl_union_map_union(edge1->tagged_validity,
1052 edge2->tagged_validity);
1055 if (is_condition(edge2) && !edge1->tagged_condition)
1056 return -1;
1057 if (is_conditional_validity(edge2) && !edge1->tagged_validity)
1058 return -1;
1060 return 0;
1063 /* Insert dummy tags in domain and range of "map".
1065 * In particular, if "map" is of the form
1067 * A -> B
1069 * then return
1071 * [A -> dummy_tag] -> [B -> dummy_tag]
1073 * where the dummy_tags are identical and equal to any dummy tags
1074 * introduced by any other call to this function.
1076 static __isl_give isl_map *insert_dummy_tags(__isl_take isl_map *map)
1078 static char dummy;
1079 isl_ctx *ctx;
1080 isl_id *id;
1081 isl_space *space;
1082 isl_set *domain, *range;
1084 ctx = isl_map_get_ctx(map);
1086 id = isl_id_alloc(ctx, NULL, &dummy);
1087 space = isl_space_params(isl_map_get_space(map));
1088 space = isl_space_set_from_params(space);
1089 space = isl_space_set_tuple_id(space, isl_dim_set, id);
1090 space = isl_space_map_from_set(space);
1092 domain = isl_map_wrap(map);
1093 range = isl_map_wrap(isl_map_universe(space));
1094 map = isl_map_from_domain_and_range(domain, range);
1095 map = isl_map_zip(map);
1097 return map;
1100 /* Given that at least one of "src" or "dst" is compressed, return
1101 * a map between the spaces of these nodes restricted to the affine
1102 * hull that was used in the compression.
1104 static __isl_give isl_map *extract_hull(struct isl_sched_node *src,
1105 struct isl_sched_node *dst)
1107 isl_set *dom, *ran;
1109 if (src->compressed)
1110 dom = isl_set_copy(src->hull);
1111 else
1112 dom = isl_set_universe(isl_space_copy(src->space));
1113 if (dst->compressed)
1114 ran = isl_set_copy(dst->hull);
1115 else
1116 ran = isl_set_universe(isl_space_copy(dst->space));
1118 return isl_map_from_domain_and_range(dom, ran);
1121 /* Intersect the domains of the nested relations in domain and range
1122 * of "tagged" with "map".
1124 static __isl_give isl_map *map_intersect_domains(__isl_take isl_map *tagged,
1125 __isl_keep isl_map *map)
1127 isl_set *set;
1129 tagged = isl_map_zip(tagged);
1130 set = isl_map_wrap(isl_map_copy(map));
1131 tagged = isl_map_intersect_domain(tagged, set);
1132 tagged = isl_map_zip(tagged);
1133 return tagged;
1136 /* Return a pointer to the node that lives in the domain space of "map"
1137 * or NULL if there is no such node.
1139 static struct isl_sched_node *find_domain_node(isl_ctx *ctx,
1140 struct isl_sched_graph *graph, __isl_keep isl_map *map)
1142 struct isl_sched_node *node;
1143 isl_space *space;
1145 space = isl_space_domain(isl_map_get_space(map));
1146 node = graph_find_node(ctx, graph, space);
1147 isl_space_free(space);
1149 return node;
1152 /* Return a pointer to the node that lives in the range space of "map"
1153 * or NULL if there is no such node.
1155 static struct isl_sched_node *find_range_node(isl_ctx *ctx,
1156 struct isl_sched_graph *graph, __isl_keep isl_map *map)
1158 struct isl_sched_node *node;
1159 isl_space *space;
1161 space = isl_space_range(isl_map_get_space(map));
1162 node = graph_find_node(ctx, graph, space);
1163 isl_space_free(space);
1165 return node;
1168 /* Add a new edge to the graph based on the given map
1169 * and add it to data->graph->edge_table[data->type].
1170 * If a dependence relation of a given type happens to be identical
1171 * to one of the dependence relations of a type that was added before,
1172 * then we don't create a new edge, but instead mark the original edge
1173 * as also representing a dependence of the current type.
1175 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1176 * may be specified as "tagged" dependence relations. That is, "map"
1177 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1178 * the dependence on iterations and a and b are tags.
1179 * edge->map is set to the relation containing the elements i -> j,
1180 * while edge->tagged_condition and edge->tagged_validity contain
1181 * the union of all the "map" relations
1182 * for which extract_edge is called that result in the same edge->map.
1184 * If the source or the destination node is compressed, then
1185 * intersect both "map" and "tagged" with the constraints that
1186 * were used to construct the compression.
1187 * This ensures that there are no schedule constraints defined
1188 * outside of these domains, while the scheduler no longer has
1189 * any control over those outside parts.
1191 static isl_stat extract_edge(__isl_take isl_map *map, void *user)
1193 isl_ctx *ctx = isl_map_get_ctx(map);
1194 struct isl_extract_edge_data *data = user;
1195 struct isl_sched_graph *graph = data->graph;
1196 struct isl_sched_node *src, *dst;
1197 struct isl_sched_edge *edge;
1198 isl_map *tagged = NULL;
1200 if (data->type == isl_edge_condition ||
1201 data->type == isl_edge_conditional_validity) {
1202 if (isl_map_can_zip(map)) {
1203 tagged = isl_map_copy(map);
1204 map = isl_set_unwrap(isl_map_domain(isl_map_zip(map)));
1205 } else {
1206 tagged = insert_dummy_tags(isl_map_copy(map));
1210 src = find_domain_node(ctx, graph, map);
1211 dst = find_range_node(ctx, graph, map);
1213 if (!src || !dst) {
1214 isl_map_free(map);
1215 isl_map_free(tagged);
1216 return isl_stat_ok;
1219 if (src->compressed || dst->compressed) {
1220 isl_map *hull;
1221 hull = extract_hull(src, dst);
1222 if (tagged)
1223 tagged = map_intersect_domains(tagged, hull);
1224 map = isl_map_intersect(map, hull);
1227 graph->edge[graph->n_edge].src = src;
1228 graph->edge[graph->n_edge].dst = dst;
1229 graph->edge[graph->n_edge].map = map;
1230 graph->edge[graph->n_edge].types = 0;
1231 graph->edge[graph->n_edge].tagged_condition = NULL;
1232 graph->edge[graph->n_edge].tagged_validity = NULL;
1233 set_type(&graph->edge[graph->n_edge], data->type);
1234 if (data->type == isl_edge_condition)
1235 graph->edge[graph->n_edge].tagged_condition =
1236 isl_union_map_from_map(tagged);
1237 if (data->type == isl_edge_conditional_validity)
1238 graph->edge[graph->n_edge].tagged_validity =
1239 isl_union_map_from_map(tagged);
1241 edge = graph_find_matching_edge(graph, &graph->edge[graph->n_edge]);
1242 if (!edge) {
1243 graph->n_edge++;
1244 return isl_stat_error;
1246 if (edge == &graph->edge[graph->n_edge])
1247 return graph_edge_table_add(ctx, graph, data->type,
1248 &graph->edge[graph->n_edge++]);
1250 if (merge_edge(edge, &graph->edge[graph->n_edge]) < 0)
1251 return -1;
1253 return graph_edge_table_add(ctx, graph, data->type, edge);
1256 /* Initialize the schedule graph "graph" from the schedule constraints "sc".
1258 * The context is included in the domain before the nodes of
1259 * the graphs are extracted in order to be able to exploit
1260 * any possible additional equalities.
1261 * Note that this intersection is only performed locally here.
1263 static isl_stat graph_init(struct isl_sched_graph *graph,
1264 __isl_keep isl_schedule_constraints *sc)
1266 isl_ctx *ctx;
1267 isl_union_set *domain;
1268 isl_union_map *c;
1269 struct isl_extract_edge_data data;
1270 enum isl_edge_type i;
1271 isl_stat r;
1273 if (!sc)
1274 return isl_stat_error;
1276 ctx = isl_schedule_constraints_get_ctx(sc);
1278 domain = isl_schedule_constraints_get_domain(sc);
1279 graph->n = isl_union_set_n_set(domain);
1280 isl_union_set_free(domain);
1282 if (graph_alloc(ctx, graph, graph->n,
1283 isl_schedule_constraints_n_map(sc)) < 0)
1284 return isl_stat_error;
1286 if (compute_max_row(graph, sc) < 0)
1287 return isl_stat_error;
1288 graph->root = 1;
1289 graph->n = 0;
1290 domain = isl_schedule_constraints_get_domain(sc);
1291 domain = isl_union_set_intersect_params(domain,
1292 isl_schedule_constraints_get_context(sc));
1293 r = isl_union_set_foreach_set(domain, &extract_node, graph);
1294 isl_union_set_free(domain);
1295 if (r < 0)
1296 return isl_stat_error;
1297 if (graph_init_table(ctx, graph) < 0)
1298 return isl_stat_error;
1299 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
1300 c = isl_schedule_constraints_get(sc, i);
1301 graph->max_edge[i] = isl_union_map_n_map(c);
1302 isl_union_map_free(c);
1303 if (!c)
1304 return isl_stat_error;
1306 if (graph_init_edge_tables(ctx, graph) < 0)
1307 return isl_stat_error;
1308 graph->n_edge = 0;
1309 data.graph = graph;
1310 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
1311 isl_stat r;
1313 data.type = i;
1314 c = isl_schedule_constraints_get(sc, i);
1315 r = isl_union_map_foreach_map(c, &extract_edge, &data);
1316 isl_union_map_free(c);
1317 if (r < 0)
1318 return isl_stat_error;
1321 return isl_stat_ok;
1324 /* Check whether there is any dependence from node[j] to node[i]
1325 * or from node[i] to node[j].
1327 static isl_bool node_follows_weak(int i, int j, void *user)
1329 isl_bool f;
1330 struct isl_sched_graph *graph = user;
1332 f = graph_has_any_edge(graph, &graph->node[j], &graph->node[i]);
1333 if (f < 0 || f)
1334 return f;
1335 return graph_has_any_edge(graph, &graph->node[i], &graph->node[j]);
1338 /* Check whether there is a (conditional) validity dependence from node[j]
1339 * to node[i], forcing node[i] to follow node[j].
1341 static isl_bool node_follows_strong(int i, int j, void *user)
1343 struct isl_sched_graph *graph = user;
1345 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
1348 /* Use Tarjan's algorithm for computing the strongly connected components
1349 * in the dependence graph only considering those edges defined by "follows".
1351 static int detect_ccs(isl_ctx *ctx, struct isl_sched_graph *graph,
1352 isl_bool (*follows)(int i, int j, void *user))
1354 int i, n;
1355 struct isl_tarjan_graph *g = NULL;
1357 g = isl_tarjan_graph_init(ctx, graph->n, follows, graph);
1358 if (!g)
1359 return -1;
1361 graph->scc = 0;
1362 i = 0;
1363 n = graph->n;
1364 while (n) {
1365 while (g->order[i] != -1) {
1366 graph->node[g->order[i]].scc = graph->scc;
1367 --n;
1368 ++i;
1370 ++i;
1371 graph->scc++;
1374 isl_tarjan_graph_free(g);
1376 return 0;
1379 /* Apply Tarjan's algorithm to detect the strongly connected components
1380 * in the dependence graph.
1381 * Only consider the (conditional) validity dependences and clear "weak".
1383 static int detect_sccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1385 graph->weak = 0;
1386 return detect_ccs(ctx, graph, &node_follows_strong);
1389 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1390 * in the dependence graph.
1391 * Consider all dependences and set "weak".
1393 static int detect_wccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1395 graph->weak = 1;
1396 return detect_ccs(ctx, graph, &node_follows_weak);
1399 static int cmp_scc(const void *a, const void *b, void *data)
1401 struct isl_sched_graph *graph = data;
1402 const int *i1 = a;
1403 const int *i2 = b;
1405 return graph->node[*i1].scc - graph->node[*i2].scc;
1408 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1410 static int sort_sccs(struct isl_sched_graph *graph)
1412 return isl_sort(graph->sorted, graph->n, sizeof(int), &cmp_scc, graph);
1415 /* Given a dependence relation R from "node" to itself,
1416 * construct the set of coefficients of valid constraints for elements
1417 * in that dependence relation.
1418 * In particular, the result contains tuples of coefficients
1419 * c_0, c_n, c_x such that
1421 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1423 * or, equivalently,
1425 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1427 * We choose here to compute the dual of delta R.
1428 * Alternatively, we could have computed the dual of R, resulting
1429 * in a set of tuples c_0, c_n, c_x, c_y, and then
1430 * plugged in (c_0, c_n, c_x, -c_x).
1432 * If "node" has been compressed, then the dependence relation
1433 * is also compressed before the set of coefficients is computed.
1435 static __isl_give isl_basic_set *intra_coefficients(
1436 struct isl_sched_graph *graph, struct isl_sched_node *node,
1437 __isl_take isl_map *map)
1439 isl_set *delta;
1440 isl_map *key;
1441 isl_basic_set *coef;
1442 isl_maybe_isl_basic_set m;
1444 m = isl_map_to_basic_set_try_get(graph->intra_hmap, map);
1445 if (m.valid < 0 || m.valid) {
1446 isl_map_free(map);
1447 return m.value;
1450 key = isl_map_copy(map);
1451 if (node->compressed) {
1452 map = isl_map_preimage_domain_multi_aff(map,
1453 isl_multi_aff_copy(node->decompress));
1454 map = isl_map_preimage_range_multi_aff(map,
1455 isl_multi_aff_copy(node->decompress));
1457 delta = isl_set_remove_divs(isl_map_deltas(map));
1458 coef = isl_set_coefficients(delta);
1459 graph->intra_hmap = isl_map_to_basic_set_set(graph->intra_hmap, key,
1460 isl_basic_set_copy(coef));
1462 return coef;
1465 /* Given a dependence relation R, construct the set of coefficients
1466 * of valid constraints for elements in that dependence relation.
1467 * In particular, the result contains tuples of coefficients
1468 * c_0, c_n, c_x, c_y such that
1470 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1472 * If the source or destination nodes of "edge" have been compressed,
1473 * then the dependence relation is also compressed before
1474 * the set of coefficients is computed.
1476 static __isl_give isl_basic_set *inter_coefficients(
1477 struct isl_sched_graph *graph, struct isl_sched_edge *edge,
1478 __isl_take isl_map *map)
1480 isl_set *set;
1481 isl_map *key;
1482 isl_basic_set *coef;
1483 isl_maybe_isl_basic_set m;
1485 m = isl_map_to_basic_set_try_get(graph->inter_hmap, map);
1486 if (m.valid < 0 || m.valid) {
1487 isl_map_free(map);
1488 return m.value;
1491 key = isl_map_copy(map);
1492 if (edge->src->compressed)
1493 map = isl_map_preimage_domain_multi_aff(map,
1494 isl_multi_aff_copy(edge->src->decompress));
1495 if (edge->dst->compressed)
1496 map = isl_map_preimage_range_multi_aff(map,
1497 isl_multi_aff_copy(edge->dst->decompress));
1498 set = isl_map_wrap(isl_map_remove_divs(map));
1499 coef = isl_set_coefficients(set);
1500 graph->inter_hmap = isl_map_to_basic_set_set(graph->inter_hmap, key,
1501 isl_basic_set_copy(coef));
1503 return coef;
1506 /* Return the position of the coefficients of the variables in
1507 * the coefficients constraints "coef".
1509 * The space of "coef" is of the form
1511 * { coefficients[[cst, params] -> S] }
1513 * Return the position of S.
1515 static int coef_var_offset(__isl_keep isl_basic_set *coef)
1517 int offset;
1518 isl_space *space;
1520 space = isl_space_unwrap(isl_basic_set_get_space(coef));
1521 offset = isl_space_dim(space, isl_dim_in);
1522 isl_space_free(space);
1524 return offset;
1527 /* Return the offset of the coefficients of the variables of "node"
1528 * within the (I)LP.
1530 * Within each node, the coefficients have the following order:
1531 * - c_i_0
1532 * - c_i_n (if parametric)
1533 * - positive and negative parts of c_i_x
1535 static int node_var_coef_offset(struct isl_sched_node *node)
1537 return node->start + 1 + node->nparam;
1540 /* Construct an isl_dim_map for mapping constraints on coefficients
1541 * for "node" to the corresponding positions in graph->lp.
1542 * "offset" is the offset of the coefficients for the variables
1543 * in the input constraints.
1544 * "s" is the sign of the mapping.
1546 * The input constraints are given in terms of the coefficients (c_0, c_n, c_x).
1547 * The mapping produced by this function essentially plugs in
1548 * (0, 0, c_i_x^+ - c_i_x^-) if s = 1 and
1549 * (0, 0, -c_i_x^+ + c_i_x^-) if s = -1.
1550 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1552 * The caller can extend the mapping to also map the other coefficients
1553 * (and therefore not plug in 0).
1555 static __isl_give isl_dim_map *intra_dim_map(isl_ctx *ctx,
1556 struct isl_sched_graph *graph, struct isl_sched_node *node,
1557 int offset, int s)
1559 int pos;
1560 unsigned total;
1561 isl_dim_map *dim_map;
1563 if (!node || !graph->lp)
1564 return NULL;
1566 total = isl_basic_set_total_dim(graph->lp);
1567 pos = node_var_coef_offset(node);
1568 dim_map = isl_dim_map_alloc(ctx, total);
1569 isl_dim_map_range(dim_map, pos, 2, offset, 1, node->nvar, -s);
1570 isl_dim_map_range(dim_map, pos + 1, 2, offset, 1, node->nvar, s);
1572 return dim_map;
1575 /* Construct an isl_dim_map for mapping constraints on coefficients
1576 * for "src" (node i) and "dst" (node j) to the corresponding positions
1577 * in graph->lp.
1578 * "offset" is the offset of the coefficients for the variables of "src"
1579 * in the input constraints.
1580 * "s" is the sign of the mapping.
1582 * The input constraints are given in terms of the coefficients
1583 * (c_0, c_n, c_x, c_y).
1584 * The mapping produced by this function essentially plugs in
1585 * (c_j_0 - c_i_0, c_j_n - c_i_n,
1586 * c_j_x^+ - c_j_x^-, -(c_i_x^+ - c_i_x^-)) if s = 1 and
1587 * (-c_j_0 + c_i_0, -c_j_n + c_i_n,
1588 * - (c_j_x^+ - c_j_x^-), c_i_x^+ - c_i_x^-) if s = -1.
1589 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1591 * The caller can further extend the mapping.
1593 static __isl_give isl_dim_map *inter_dim_map(isl_ctx *ctx,
1594 struct isl_sched_graph *graph, struct isl_sched_node *src,
1595 struct isl_sched_node *dst, int offset, int s)
1597 int pos;
1598 unsigned total;
1599 isl_dim_map *dim_map;
1601 if (!src || !dst || !graph->lp)
1602 return NULL;
1604 total = isl_basic_set_total_dim(graph->lp);
1605 dim_map = isl_dim_map_alloc(ctx, total);
1607 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, s);
1608 isl_dim_map_range(dim_map, dst->start + 1, 1, 1, 1, dst->nparam, s);
1609 pos = node_var_coef_offset(dst);
1610 isl_dim_map_range(dim_map, pos, 2, offset + src->nvar, 1,
1611 dst->nvar, -s);
1612 isl_dim_map_range(dim_map, pos + 1, 2, offset + src->nvar, 1,
1613 dst->nvar, s);
1615 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -s);
1616 isl_dim_map_range(dim_map, src->start + 1, 1, 1, 1, src->nparam, -s);
1617 pos = node_var_coef_offset(src);
1618 isl_dim_map_range(dim_map, pos, 2, offset, 1, src->nvar, s);
1619 isl_dim_map_range(dim_map, pos + 1, 2, offset, 1, src->nvar, -s);
1621 return dim_map;
1624 /* Add constraints to graph->lp that force validity for the given
1625 * dependence from a node i to itself.
1626 * That is, add constraints that enforce
1628 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1629 * = c_i_x (y - x) >= 0
1631 * for each (x,y) in R.
1632 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1633 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1634 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1635 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1637 * Actually, we do not construct constraints for the c_i_x themselves,
1638 * but for the coefficients of c_i_x written as a linear combination
1639 * of the columns in node->cmap.
1641 static isl_stat add_intra_validity_constraints(struct isl_sched_graph *graph,
1642 struct isl_sched_edge *edge)
1644 int offset;
1645 isl_map *map = isl_map_copy(edge->map);
1646 isl_ctx *ctx = isl_map_get_ctx(map);
1647 isl_dim_map *dim_map;
1648 isl_basic_set *coef;
1649 struct isl_sched_node *node = edge->src;
1651 coef = intra_coefficients(graph, node, map);
1653 offset = coef_var_offset(coef);
1655 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1656 offset, isl_mat_copy(node->cmap));
1657 if (!coef)
1658 return isl_stat_error;
1660 dim_map = intra_dim_map(ctx, graph, node, offset, 1);
1661 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1662 coef->n_eq, coef->n_ineq);
1663 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1664 coef, dim_map);
1666 return isl_stat_ok;
1669 /* Add constraints to graph->lp that force validity for the given
1670 * dependence from node i to node j.
1671 * That is, add constraints that enforce
1673 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1675 * for each (x,y) in R.
1676 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1677 * of valid constraints for R and then plug in
1678 * (c_j_0 - c_i_0, c_j_n - c_i_n, c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1679 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1680 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1682 * Actually, we do not construct constraints for the c_*_x themselves,
1683 * but for the coefficients of c_*_x written as a linear combination
1684 * of the columns in node->cmap.
1686 static isl_stat add_inter_validity_constraints(struct isl_sched_graph *graph,
1687 struct isl_sched_edge *edge)
1689 int offset;
1690 isl_map *map;
1691 isl_ctx *ctx;
1692 isl_dim_map *dim_map;
1693 isl_basic_set *coef;
1694 struct isl_sched_node *src = edge->src;
1695 struct isl_sched_node *dst = edge->dst;
1697 if (!graph->lp)
1698 return isl_stat_error;
1700 map = isl_map_copy(edge->map);
1701 ctx = isl_map_get_ctx(map);
1702 coef = inter_coefficients(graph, edge, map);
1704 offset = coef_var_offset(coef);
1706 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1707 offset, isl_mat_copy(src->cmap));
1708 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1709 offset + src->nvar, isl_mat_copy(dst->cmap));
1710 if (!coef)
1711 return isl_stat_error;
1713 dim_map = inter_dim_map(ctx, graph, src, dst, offset, 1);
1715 edge->start = graph->lp->n_ineq;
1716 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1717 coef->n_eq, coef->n_ineq);
1718 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1719 coef, dim_map);
1720 if (!graph->lp)
1721 return isl_stat_error;
1722 edge->end = graph->lp->n_ineq;
1724 return isl_stat_ok;
1727 /* Add constraints to graph->lp that bound the dependence distance for the given
1728 * dependence from a node i to itself.
1729 * If s = 1, we add the constraint
1731 * c_i_x (y - x) <= m_0 + m_n n
1733 * or
1735 * -c_i_x (y - x) + m_0 + m_n n >= 0
1737 * for each (x,y) in R.
1738 * If s = -1, we add the constraint
1740 * -c_i_x (y - x) <= m_0 + m_n n
1742 * or
1744 * c_i_x (y - x) + m_0 + m_n n >= 0
1746 * for each (x,y) in R.
1747 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1748 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1749 * with each coefficient (except m_0) represented as a pair of non-negative
1750 * coefficients.
1752 * Actually, we do not construct constraints for the c_i_x themselves,
1753 * but for the coefficients of c_i_x written as a linear combination
1754 * of the columns in node->cmap.
1757 * If "local" is set, then we add constraints
1759 * c_i_x (y - x) <= 0
1761 * or
1763 * -c_i_x (y - x) <= 0
1765 * instead, forcing the dependence distance to be (less than or) equal to 0.
1766 * That is, we plug in (0, 0, -s * c_i_x),
1767 * Note that dependences marked local are treated as validity constraints
1768 * by add_all_validity_constraints and therefore also have
1769 * their distances bounded by 0 from below.
1771 static isl_stat add_intra_proximity_constraints(struct isl_sched_graph *graph,
1772 struct isl_sched_edge *edge, int s, int local)
1774 int offset;
1775 unsigned nparam;
1776 isl_map *map = isl_map_copy(edge->map);
1777 isl_ctx *ctx = isl_map_get_ctx(map);
1778 isl_dim_map *dim_map;
1779 isl_basic_set *coef;
1780 struct isl_sched_node *node = edge->src;
1782 coef = intra_coefficients(graph, node, map);
1784 offset = coef_var_offset(coef);
1786 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1787 offset, isl_mat_copy(node->cmap));
1788 if (!coef)
1789 return isl_stat_error;
1791 nparam = isl_space_dim(node->space, isl_dim_param);
1792 dim_map = intra_dim_map(ctx, graph, node, offset, -s);
1794 if (!local) {
1795 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1796 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1797 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1799 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1800 coef->n_eq, coef->n_ineq);
1801 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1802 coef, dim_map);
1804 return isl_stat_ok;
1807 /* Add constraints to graph->lp that bound the dependence distance for the given
1808 * dependence from node i to node j.
1809 * If s = 1, we add the constraint
1811 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1812 * <= m_0 + m_n n
1814 * or
1816 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1817 * m_0 + m_n n >= 0
1819 * for each (x,y) in R.
1820 * If s = -1, we add the constraint
1822 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1823 * <= m_0 + m_n n
1825 * or
1827 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1828 * m_0 + m_n n >= 0
1830 * for each (x,y) in R.
1831 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1832 * of valid constraints for R and then plug in
1833 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1834 * -s*c_j_x+s*c_i_x)
1835 * with each coefficient (except m_0, c_*_0 and c_*_n)
1836 * represented as a pair of non-negative coefficients.
1838 * Actually, we do not construct constraints for the c_*_x themselves,
1839 * but for the coefficients of c_*_x written as a linear combination
1840 * of the columns in node->cmap.
1843 * If "local" is set, then we add constraints
1845 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1847 * or
1849 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
1851 * instead, forcing the dependence distance to be (less than or) equal to 0.
1852 * That is, we plug in
1853 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
1854 * Note that dependences marked local are treated as validity constraints
1855 * by add_all_validity_constraints and therefore also have
1856 * their distances bounded by 0 from below.
1858 static isl_stat add_inter_proximity_constraints(struct isl_sched_graph *graph,
1859 struct isl_sched_edge *edge, int s, int local)
1861 int offset;
1862 unsigned nparam;
1863 isl_map *map = isl_map_copy(edge->map);
1864 isl_ctx *ctx = isl_map_get_ctx(map);
1865 isl_dim_map *dim_map;
1866 isl_basic_set *coef;
1867 struct isl_sched_node *src = edge->src;
1868 struct isl_sched_node *dst = edge->dst;
1870 coef = inter_coefficients(graph, edge, map);
1872 offset = coef_var_offset(coef);
1874 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1875 offset, isl_mat_copy(src->cmap));
1876 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1877 offset + src->nvar, isl_mat_copy(dst->cmap));
1878 if (!coef)
1879 return isl_stat_error;
1881 nparam = isl_space_dim(src->space, isl_dim_param);
1882 dim_map = inter_dim_map(ctx, graph, src, dst, offset, -s);
1884 if (!local) {
1885 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1886 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1887 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1890 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1891 coef->n_eq, coef->n_ineq);
1892 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1893 coef, dim_map);
1895 return isl_stat_ok;
1898 /* Add all validity constraints to graph->lp.
1900 * An edge that is forced to be local needs to have its dependence
1901 * distances equal to zero. We take care of bounding them by 0 from below
1902 * here. add_all_proximity_constraints takes care of bounding them by 0
1903 * from above.
1905 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1906 * Otherwise, we ignore them.
1908 static int add_all_validity_constraints(struct isl_sched_graph *graph,
1909 int use_coincidence)
1911 int i;
1913 for (i = 0; i < graph->n_edge; ++i) {
1914 struct isl_sched_edge *edge= &graph->edge[i];
1915 int local;
1917 local = is_local(edge) ||
1918 (is_coincidence(edge) && use_coincidence);
1919 if (!is_validity(edge) && !local)
1920 continue;
1921 if (edge->src != edge->dst)
1922 continue;
1923 if (add_intra_validity_constraints(graph, edge) < 0)
1924 return -1;
1927 for (i = 0; i < graph->n_edge; ++i) {
1928 struct isl_sched_edge *edge = &graph->edge[i];
1929 int local;
1931 local = is_local(edge) ||
1932 (is_coincidence(edge) && use_coincidence);
1933 if (!is_validity(edge) && !local)
1934 continue;
1935 if (edge->src == edge->dst)
1936 continue;
1937 if (add_inter_validity_constraints(graph, edge) < 0)
1938 return -1;
1941 return 0;
1944 /* Add constraints to graph->lp that bound the dependence distance
1945 * for all dependence relations.
1946 * If a given proximity dependence is identical to a validity
1947 * dependence, then the dependence distance is already bounded
1948 * from below (by zero), so we only need to bound the distance
1949 * from above. (This includes the case of "local" dependences
1950 * which are treated as validity dependence by add_all_validity_constraints.)
1951 * Otherwise, we need to bound the distance both from above and from below.
1953 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1954 * Otherwise, we ignore them.
1956 static int add_all_proximity_constraints(struct isl_sched_graph *graph,
1957 int use_coincidence)
1959 int i;
1961 for (i = 0; i < graph->n_edge; ++i) {
1962 struct isl_sched_edge *edge= &graph->edge[i];
1963 int local;
1965 local = is_local(edge) ||
1966 (is_coincidence(edge) && use_coincidence);
1967 if (!is_proximity(edge) && !local)
1968 continue;
1969 if (edge->src == edge->dst &&
1970 add_intra_proximity_constraints(graph, edge, 1, local) < 0)
1971 return -1;
1972 if (edge->src != edge->dst &&
1973 add_inter_proximity_constraints(graph, edge, 1, local) < 0)
1974 return -1;
1975 if (is_validity(edge) || local)
1976 continue;
1977 if (edge->src == edge->dst &&
1978 add_intra_proximity_constraints(graph, edge, -1, 0) < 0)
1979 return -1;
1980 if (edge->src != edge->dst &&
1981 add_inter_proximity_constraints(graph, edge, -1, 0) < 0)
1982 return -1;
1985 return 0;
1988 /* Compute a basis for the rows in the linear part of the schedule
1989 * and extend this basis to a full basis. The remaining rows
1990 * can then be used to force linear independence from the rows
1991 * in the schedule.
1993 * In particular, given the schedule rows S, we compute
1995 * S = H Q
1996 * S U = H
1998 * with H the Hermite normal form of S. That is, all but the
1999 * first rank columns of H are zero and so each row in S is
2000 * a linear combination of the first rank rows of Q.
2001 * The matrix Q is then transposed because we will write the
2002 * coefficients of the next schedule row as a column vector s
2003 * and express this s as a linear combination s = Q c of the
2004 * computed basis.
2005 * Similarly, the matrix U is transposed such that we can
2006 * compute the coefficients c = U s from a schedule row s.
2008 static int node_update_cmap(struct isl_sched_node *node)
2010 isl_mat *H, *U, *Q;
2011 int n_row = isl_mat_rows(node->sched);
2013 H = isl_mat_sub_alloc(node->sched, 0, n_row,
2014 1 + node->nparam, node->nvar);
2016 H = isl_mat_left_hermite(H, 0, &U, &Q);
2017 isl_mat_free(node->cmap);
2018 isl_mat_free(node->cinv);
2019 isl_mat_free(node->ctrans);
2020 node->ctrans = isl_mat_copy(Q);
2021 node->cmap = isl_mat_transpose(Q);
2022 node->cinv = isl_mat_transpose(U);
2023 node->rank = isl_mat_initial_non_zero_cols(H);
2024 isl_mat_free(H);
2026 if (!node->cmap || !node->cinv || !node->ctrans || node->rank < 0)
2027 return -1;
2028 return 0;
2031 /* Is "edge" marked as a validity or a conditional validity edge?
2033 static int is_any_validity(struct isl_sched_edge *edge)
2035 return is_validity(edge) || is_conditional_validity(edge);
2038 /* How many times should we count the constraints in "edge"?
2040 * If carry is set, then we are counting the number of
2041 * (validity or conditional validity) constraints that will be added
2042 * in setup_carry_lp and we count each edge exactly once.
2044 * Otherwise, we count as follows
2045 * validity -> 1 (>= 0)
2046 * validity+proximity -> 2 (>= 0 and upper bound)
2047 * proximity -> 2 (lower and upper bound)
2048 * local(+any) -> 2 (>= 0 and <= 0)
2050 * If an edge is only marked conditional_validity then it counts
2051 * as zero since it is only checked afterwards.
2053 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2054 * Otherwise, we ignore them.
2056 static int edge_multiplicity(struct isl_sched_edge *edge, int carry,
2057 int use_coincidence)
2059 if (carry)
2060 return 1;
2061 if (is_proximity(edge) || is_local(edge))
2062 return 2;
2063 if (use_coincidence && is_coincidence(edge))
2064 return 2;
2065 if (is_validity(edge))
2066 return 1;
2067 return 0;
2070 /* Count the number of equality and inequality constraints
2071 * that will be added for the given map.
2073 * "use_coincidence" is set if we should take into account coincidence edges.
2075 static int count_map_constraints(struct isl_sched_graph *graph,
2076 struct isl_sched_edge *edge, __isl_take isl_map *map,
2077 int *n_eq, int *n_ineq, int carry, int use_coincidence)
2079 isl_basic_set *coef;
2080 int f = edge_multiplicity(edge, carry, use_coincidence);
2082 if (f == 0) {
2083 isl_map_free(map);
2084 return 0;
2087 if (edge->src == edge->dst)
2088 coef = intra_coefficients(graph, edge->src, map);
2089 else
2090 coef = inter_coefficients(graph, edge, map);
2091 if (!coef)
2092 return -1;
2093 *n_eq += f * coef->n_eq;
2094 *n_ineq += f * coef->n_ineq;
2095 isl_basic_set_free(coef);
2097 return 0;
2100 /* Count the number of equality and inequality constraints
2101 * that will be added to the main lp problem.
2102 * We count as follows
2103 * validity -> 1 (>= 0)
2104 * validity+proximity -> 2 (>= 0 and upper bound)
2105 * proximity -> 2 (lower and upper bound)
2106 * local(+any) -> 2 (>= 0 and <= 0)
2108 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2109 * Otherwise, we ignore them.
2111 static int count_constraints(struct isl_sched_graph *graph,
2112 int *n_eq, int *n_ineq, int use_coincidence)
2114 int i;
2116 *n_eq = *n_ineq = 0;
2117 for (i = 0; i < graph->n_edge; ++i) {
2118 struct isl_sched_edge *edge= &graph->edge[i];
2119 isl_map *map = isl_map_copy(edge->map);
2121 if (count_map_constraints(graph, edge, map, n_eq, n_ineq,
2122 0, use_coincidence) < 0)
2123 return -1;
2126 return 0;
2129 /* Count the number of constraints that will be added by
2130 * add_bound_constant_constraints to bound the values of the constant terms
2131 * and increment *n_eq and *n_ineq accordingly.
2133 * In practice, add_bound_constant_constraints only adds inequalities.
2135 static isl_stat count_bound_constant_constraints(isl_ctx *ctx,
2136 struct isl_sched_graph *graph, int *n_eq, int *n_ineq)
2138 if (isl_options_get_schedule_max_constant_term(ctx) == -1)
2139 return isl_stat_ok;
2141 *n_ineq += graph->n;
2143 return isl_stat_ok;
2146 /* Add constraints to bound the values of the constant terms in the schedule,
2147 * if requested by the user.
2149 * The maximal value of the constant terms is defined by the option
2150 * "schedule_max_constant_term".
2152 * Within each node, the coefficients have the following order:
2153 * - c_i_0
2154 * - c_i_n (if parametric)
2155 * - positive and negative parts of c_i_x
2157 static isl_stat add_bound_constant_constraints(isl_ctx *ctx,
2158 struct isl_sched_graph *graph)
2160 int i, k;
2161 int max;
2162 int total;
2164 max = isl_options_get_schedule_max_constant_term(ctx);
2165 if (max == -1)
2166 return isl_stat_ok;
2168 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2170 for (i = 0; i < graph->n; ++i) {
2171 struct isl_sched_node *node = &graph->node[i];
2172 k = isl_basic_set_alloc_inequality(graph->lp);
2173 if (k < 0)
2174 return isl_stat_error;
2175 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2176 isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
2177 isl_int_set_si(graph->lp->ineq[k][0], max);
2180 return isl_stat_ok;
2183 /* Count the number of constraints that will be added by
2184 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
2185 * accordingly.
2187 * In practice, add_bound_coefficient_constraints only adds inequalities.
2189 static int count_bound_coefficient_constraints(isl_ctx *ctx,
2190 struct isl_sched_graph *graph, int *n_eq, int *n_ineq)
2192 int i;
2194 if (isl_options_get_schedule_max_coefficient(ctx) == -1 &&
2195 !isl_options_get_schedule_treat_coalescing(ctx))
2196 return 0;
2198 for (i = 0; i < graph->n; ++i)
2199 *n_ineq += graph->node[i].nparam + 2 * graph->node[i].nvar;
2201 return 0;
2204 /* Add constraints to graph->lp that bound the values of
2205 * the parameter schedule coefficients of "node" to "max" and
2206 * the variable schedule coefficients to the corresponding entry
2207 * in node->max.
2208 * In either case, a negative value means that no bound needs to be imposed.
2210 * For parameter coefficients, this amounts to adding a constraint
2212 * c_n <= max
2214 * i.e.,
2216 * -c_n + max >= 0
2218 * The variables coefficients are, however, not represented directly.
2219 * Instead, the variables coefficients c_x are written as a linear
2220 * combination c_x = cmap c_z of some other coefficients c_z,
2221 * which are in turn encoded as c_z = c_z^+ - c_z^-.
2222 * Let a_j be the elements of row i of node->cmap, then
2224 * -max_i <= c_x_i <= max_i
2226 * is encoded as
2228 * -max_i <= \sum_j a_j (c_z_j^+ - c_z_j^-) <= max_i
2230 * or
2232 * -\sum_j a_j (c_z_j^+ - c_z_j^-) + max_i >= 0
2233 * \sum_j a_j (c_z_j^+ - c_z_j^-) + max_i >= 0
2235 static isl_stat node_add_coefficient_constraints(isl_ctx *ctx,
2236 struct isl_sched_graph *graph, struct isl_sched_node *node, int max)
2238 int i, j, k;
2239 int total;
2240 isl_vec *ineq;
2242 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2244 for (j = 0; j < node->nparam; ++j) {
2245 int dim;
2247 if (max < 0)
2248 continue;
2250 k = isl_basic_set_alloc_inequality(graph->lp);
2251 if (k < 0)
2252 return isl_stat_error;
2253 dim = 1 + node->start + 1 + j;
2254 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2255 isl_int_set_si(graph->lp->ineq[k][dim], -1);
2256 isl_int_set_si(graph->lp->ineq[k][0], max);
2259 ineq = isl_vec_alloc(ctx, 1 + total);
2260 ineq = isl_vec_clr(ineq);
2261 if (!ineq)
2262 return isl_stat_error;
2263 for (i = 0; i < node->nvar; ++i) {
2264 int pos = 1 + node_var_coef_offset(node);
2266 if (isl_int_is_neg(node->max->el[i]))
2267 continue;
2269 for (j = 0; j < node->nvar; ++j) {
2270 isl_int_set(ineq->el[pos + 2 * j],
2271 node->cmap->row[i][j]);
2272 isl_int_neg(ineq->el[pos + 2 * j + 1],
2273 node->cmap->row[i][j]);
2275 isl_int_set(ineq->el[0], node->max->el[i]);
2277 k = isl_basic_set_alloc_inequality(graph->lp);
2278 if (k < 0)
2279 goto error;
2280 isl_seq_cpy(graph->lp->ineq[k], ineq->el, 1 + total);
2282 isl_seq_neg(ineq->el + pos, ineq->el + pos, 2 * node->nvar);
2283 k = isl_basic_set_alloc_inequality(graph->lp);
2284 if (k < 0)
2285 goto error;
2286 isl_seq_cpy(graph->lp->ineq[k], ineq->el, 1 + total);
2288 isl_vec_free(ineq);
2290 return isl_stat_ok;
2291 error:
2292 isl_vec_free(ineq);
2293 return isl_stat_error;
2296 /* Add constraints that bound the values of the variable and parameter
2297 * coefficients of the schedule.
2299 * The maximal value of the coefficients is defined by the option
2300 * 'schedule_max_coefficient' and the entries in node->max.
2301 * These latter entries are only set if either the schedule_max_coefficient
2302 * option or the schedule_treat_coalescing option is set.
2304 static isl_stat add_bound_coefficient_constraints(isl_ctx *ctx,
2305 struct isl_sched_graph *graph)
2307 int i;
2308 int max;
2310 max = isl_options_get_schedule_max_coefficient(ctx);
2312 if (max == -1 && !isl_options_get_schedule_treat_coalescing(ctx))
2313 return isl_stat_ok;
2315 for (i = 0; i < graph->n; ++i) {
2316 struct isl_sched_node *node = &graph->node[i];
2318 if (node_add_coefficient_constraints(ctx, graph, node, max) < 0)
2319 return isl_stat_error;
2322 return isl_stat_ok;
2325 /* Add a constraint to graph->lp that equates the value at position
2326 * "sum_pos" to the sum of the "n" values starting at "first".
2328 static isl_stat add_sum_constraint(struct isl_sched_graph *graph,
2329 int sum_pos, int first, int n)
2331 int i, k;
2332 int total;
2334 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2336 k = isl_basic_set_alloc_equality(graph->lp);
2337 if (k < 0)
2338 return isl_stat_error;
2339 isl_seq_clr(graph->lp->eq[k], 1 + total);
2340 isl_int_set_si(graph->lp->eq[k][1 + sum_pos], -1);
2341 for (i = 0; i < n; ++i)
2342 isl_int_set_si(graph->lp->eq[k][1 + first + i], 1);
2344 return isl_stat_ok;
2347 /* Add a constraint to graph->lp that equates the value at position
2348 * "sum_pos" to the sum of the parameter coefficients of all nodes.
2350 * Within each node, the coefficients have the following order:
2351 * - c_i_0
2352 * - c_i_n (if parametric)
2353 * - positive and negative parts of c_i_x
2355 static isl_stat add_param_sum_constraint(struct isl_sched_graph *graph,
2356 int sum_pos)
2358 int i, j, k;
2359 int total;
2361 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2363 k = isl_basic_set_alloc_equality(graph->lp);
2364 if (k < 0)
2365 return isl_stat_error;
2366 isl_seq_clr(graph->lp->eq[k], 1 + total);
2367 isl_int_set_si(graph->lp->eq[k][1 + sum_pos], -1);
2368 for (i = 0; i < graph->n; ++i) {
2369 int pos = 1 + graph->node[i].start + 1;
2371 for (j = 0; j < graph->node[i].nparam; ++j)
2372 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2375 return isl_stat_ok;
2378 /* Add a constraint to graph->lp that equates the value at position
2379 * "sum_pos" to the sum of the variable coefficients of all nodes.
2381 * Within each node, the coefficients have the following order:
2382 * - c_i_0
2383 * - c_i_n (if parametric)
2384 * - positive and negative parts of c_i_x
2386 static isl_stat add_var_sum_constraint(struct isl_sched_graph *graph,
2387 int sum_pos)
2389 int i, j, k;
2390 int total;
2392 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2394 k = isl_basic_set_alloc_equality(graph->lp);
2395 if (k < 0)
2396 return isl_stat_error;
2397 isl_seq_clr(graph->lp->eq[k], 1 + total);
2398 isl_int_set_si(graph->lp->eq[k][1 + sum_pos], -1);
2399 for (i = 0; i < graph->n; ++i) {
2400 struct isl_sched_node *node = &graph->node[i];
2401 int pos = 1 + node_var_coef_offset(node);
2403 for (j = 0; j < 2 * node->nvar; ++j)
2404 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2407 return isl_stat_ok;
2410 /* Construct an ILP problem for finding schedule coefficients
2411 * that result in non-negative, but small dependence distances
2412 * over all dependences.
2413 * In particular, the dependence distances over proximity edges
2414 * are bounded by m_0 + m_n n and we compute schedule coefficients
2415 * with small values (preferably zero) of m_n and m_0.
2417 * All variables of the ILP are non-negative. The actual coefficients
2418 * may be negative, so each coefficient is represented as the difference
2419 * of two non-negative variables. The negative part always appears
2420 * immediately before the positive part.
2421 * Other than that, the variables have the following order
2423 * - sum of positive and negative parts of m_n coefficients
2424 * - m_0
2425 * - sum of all c_n coefficients
2426 * (unconstrained when computing non-parametric schedules)
2427 * - sum of positive and negative parts of all c_x coefficients
2428 * - positive and negative parts of m_n coefficients
2429 * - for each node
2430 * - c_i_0
2431 * - c_i_n (if parametric)
2432 * - positive and negative parts of c_i_x
2434 * The c_i_x are not represented directly, but through the columns of
2435 * node->cmap. That is, the computed values are for variable t_i_x
2436 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
2438 * The constraints are those from the edges plus two or three equalities
2439 * to express the sums.
2441 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2442 * Otherwise, we ignore them.
2444 static isl_stat setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
2445 int use_coincidence)
2447 int i;
2448 unsigned nparam;
2449 unsigned total;
2450 isl_space *space;
2451 int parametric;
2452 int param_pos;
2453 int n_eq, n_ineq;
2455 parametric = ctx->opt->schedule_parametric;
2456 nparam = isl_space_dim(graph->node[0].space, isl_dim_param);
2457 param_pos = 4;
2458 total = param_pos + 2 * nparam;
2459 for (i = 0; i < graph->n; ++i) {
2460 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2461 if (node_update_cmap(node) < 0)
2462 return isl_stat_error;
2463 node->start = total;
2464 total += 1 + node->nparam + 2 * node->nvar;
2467 if (count_constraints(graph, &n_eq, &n_ineq, use_coincidence) < 0)
2468 return isl_stat_error;
2469 if (count_bound_constant_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
2470 return isl_stat_error;
2471 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
2472 return isl_stat_error;
2474 space = isl_space_set_alloc(ctx, 0, total);
2475 isl_basic_set_free(graph->lp);
2476 n_eq += 2 + parametric;
2478 graph->lp = isl_basic_set_alloc_space(space, 0, n_eq, n_ineq);
2480 if (add_sum_constraint(graph, 0, param_pos, 2 * nparam) < 0)
2481 return isl_stat_error;
2482 if (parametric && add_param_sum_constraint(graph, 2) < 0)
2483 return isl_stat_error;
2484 if (add_var_sum_constraint(graph, 3) < 0)
2485 return isl_stat_error;
2486 if (add_bound_constant_constraints(ctx, graph) < 0)
2487 return isl_stat_error;
2488 if (add_bound_coefficient_constraints(ctx, graph) < 0)
2489 return isl_stat_error;
2490 if (add_all_validity_constraints(graph, use_coincidence) < 0)
2491 return isl_stat_error;
2492 if (add_all_proximity_constraints(graph, use_coincidence) < 0)
2493 return isl_stat_error;
2495 return isl_stat_ok;
2498 /* Analyze the conflicting constraint found by
2499 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2500 * constraint of one of the edges between distinct nodes, living, moreover
2501 * in distinct SCCs, then record the source and sink SCC as this may
2502 * be a good place to cut between SCCs.
2504 static int check_conflict(int con, void *user)
2506 int i;
2507 struct isl_sched_graph *graph = user;
2509 if (graph->src_scc >= 0)
2510 return 0;
2512 con -= graph->lp->n_eq;
2514 if (con >= graph->lp->n_ineq)
2515 return 0;
2517 for (i = 0; i < graph->n_edge; ++i) {
2518 if (!is_validity(&graph->edge[i]))
2519 continue;
2520 if (graph->edge[i].src == graph->edge[i].dst)
2521 continue;
2522 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
2523 continue;
2524 if (graph->edge[i].start > con)
2525 continue;
2526 if (graph->edge[i].end <= con)
2527 continue;
2528 graph->src_scc = graph->edge[i].src->scc;
2529 graph->dst_scc = graph->edge[i].dst->scc;
2532 return 0;
2535 /* Check whether the next schedule row of the given node needs to be
2536 * non-trivial. Lower-dimensional domains may have some trivial rows,
2537 * but as soon as the number of remaining required non-trivial rows
2538 * is as large as the number or remaining rows to be computed,
2539 * all remaining rows need to be non-trivial.
2541 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
2543 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
2546 /* Solve the ILP problem constructed in setup_lp.
2547 * For each node such that all the remaining rows of its schedule
2548 * need to be non-trivial, we construct a non-triviality region.
2549 * This region imposes that the next row is independent of previous rows.
2550 * In particular the coefficients c_i_x are represented by t_i_x
2551 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
2552 * its first columns span the rows of the previously computed part
2553 * of the schedule. The non-triviality region enforces that at least
2554 * one of the remaining components of t_i_x is non-zero, i.e.,
2555 * that the new schedule row depends on at least one of the remaining
2556 * columns of Q.
2558 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
2560 int i;
2561 isl_vec *sol;
2562 isl_basic_set *lp;
2564 for (i = 0; i < graph->n; ++i) {
2565 struct isl_sched_node *node = &graph->node[i];
2566 int skip = node->rank;
2567 graph->region[i].pos = node_var_coef_offset(node) + 2 * skip;
2568 if (needs_row(graph, node))
2569 graph->region[i].len = 2 * (node->nvar - skip);
2570 else
2571 graph->region[i].len = 0;
2573 lp = isl_basic_set_copy(graph->lp);
2574 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
2575 graph->region, &check_conflict, graph);
2576 return sol;
2579 /* Extract the coefficients for the variables of "node" from "sol".
2581 * Within each node, the coefficients have the following order:
2582 * - c_i_0
2583 * - c_i_n (if parametric)
2584 * - positive and negative parts of c_i_x
2586 * The c_i_x^- appear before their c_i_x^+ counterpart.
2588 * Return c_i_x = c_i_x^+ - c_i_x^-
2590 static __isl_give isl_vec *extract_var_coef(struct isl_sched_node *node,
2591 __isl_keep isl_vec *sol)
2593 int i;
2594 int pos;
2595 isl_vec *csol;
2597 if (!sol)
2598 return NULL;
2599 csol = isl_vec_alloc(isl_vec_get_ctx(sol), node->nvar);
2600 if (!csol)
2601 return NULL;
2603 pos = 1 + node_var_coef_offset(node);
2604 for (i = 0; i < node->nvar; ++i)
2605 isl_int_sub(csol->el[i],
2606 sol->el[pos + 2 * i + 1], sol->el[pos + 2 * i]);
2608 return csol;
2611 /* Update the schedules of all nodes based on the given solution
2612 * of the LP problem.
2613 * The new row is added to the current band.
2614 * All possibly negative coefficients are encoded as a difference
2615 * of two non-negative variables, so we need to perform the subtraction
2616 * here. Moreover, if use_cmap is set, then the solution does
2617 * not refer to the actual coefficients c_i_x, but instead to variables
2618 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2619 * In this case, we then also need to perform this multiplication
2620 * to obtain the values of c_i_x.
2622 * If coincident is set, then the caller guarantees that the new
2623 * row satisfies the coincidence constraints.
2625 static int update_schedule(struct isl_sched_graph *graph,
2626 __isl_take isl_vec *sol, int use_cmap, int coincident)
2628 int i, j;
2629 isl_vec *csol = NULL;
2631 if (!sol)
2632 goto error;
2633 if (sol->size == 0)
2634 isl_die(sol->ctx, isl_error_internal,
2635 "no solution found", goto error);
2636 if (graph->n_total_row >= graph->max_row)
2637 isl_die(sol->ctx, isl_error_internal,
2638 "too many schedule rows", goto error);
2640 for (i = 0; i < graph->n; ++i) {
2641 struct isl_sched_node *node = &graph->node[i];
2642 int pos = node->start;
2643 int row = isl_mat_rows(node->sched);
2645 isl_vec_free(csol);
2646 csol = extract_var_coef(node, sol);
2647 if (!csol)
2648 goto error;
2650 isl_map_free(node->sched_map);
2651 node->sched_map = NULL;
2652 node->sched = isl_mat_add_rows(node->sched, 1);
2653 if (!node->sched)
2654 goto error;
2655 for (j = 0; j < 1 + node->nparam; ++j)
2656 node->sched = isl_mat_set_element(node->sched,
2657 row, j, sol->el[1 + pos + j]);
2658 if (use_cmap)
2659 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
2660 csol);
2661 if (!csol)
2662 goto error;
2663 for (j = 0; j < node->nvar; ++j)
2664 node->sched = isl_mat_set_element(node->sched,
2665 row, 1 + node->nparam + j, csol->el[j]);
2666 node->coincident[graph->n_total_row] = coincident;
2668 isl_vec_free(sol);
2669 isl_vec_free(csol);
2671 graph->n_row++;
2672 graph->n_total_row++;
2674 return 0;
2675 error:
2676 isl_vec_free(sol);
2677 isl_vec_free(csol);
2678 return -1;
2681 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2682 * and return this isl_aff.
2684 static __isl_give isl_aff *extract_schedule_row(__isl_take isl_local_space *ls,
2685 struct isl_sched_node *node, int row)
2687 int j;
2688 isl_int v;
2689 isl_aff *aff;
2691 isl_int_init(v);
2693 aff = isl_aff_zero_on_domain(ls);
2694 isl_mat_get_element(node->sched, row, 0, &v);
2695 aff = isl_aff_set_constant(aff, v);
2696 for (j = 0; j < node->nparam; ++j) {
2697 isl_mat_get_element(node->sched, row, 1 + j, &v);
2698 aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
2700 for (j = 0; j < node->nvar; ++j) {
2701 isl_mat_get_element(node->sched, row, 1 + node->nparam + j, &v);
2702 aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
2705 isl_int_clear(v);
2707 return aff;
2710 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
2711 * and return this multi_aff.
2713 * The result is defined over the uncompressed node domain.
2715 static __isl_give isl_multi_aff *node_extract_partial_schedule_multi_aff(
2716 struct isl_sched_node *node, int first, int n)
2718 int i;
2719 isl_space *space;
2720 isl_local_space *ls;
2721 isl_aff *aff;
2722 isl_multi_aff *ma;
2723 int nrow;
2725 if (!node)
2726 return NULL;
2727 nrow = isl_mat_rows(node->sched);
2728 if (node->compressed)
2729 space = isl_multi_aff_get_domain_space(node->decompress);
2730 else
2731 space = isl_space_copy(node->space);
2732 ls = isl_local_space_from_space(isl_space_copy(space));
2733 space = isl_space_from_domain(space);
2734 space = isl_space_add_dims(space, isl_dim_out, n);
2735 ma = isl_multi_aff_zero(space);
2737 for (i = first; i < first + n; ++i) {
2738 aff = extract_schedule_row(isl_local_space_copy(ls), node, i);
2739 ma = isl_multi_aff_set_aff(ma, i - first, aff);
2742 isl_local_space_free(ls);
2744 if (node->compressed)
2745 ma = isl_multi_aff_pullback_multi_aff(ma,
2746 isl_multi_aff_copy(node->compress));
2748 return ma;
2751 /* Convert node->sched into a multi_aff and return this multi_aff.
2753 * The result is defined over the uncompressed node domain.
2755 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
2756 struct isl_sched_node *node)
2758 int nrow;
2760 nrow = isl_mat_rows(node->sched);
2761 return node_extract_partial_schedule_multi_aff(node, 0, nrow);
2764 /* Convert node->sched into a map and return this map.
2766 * The result is cached in node->sched_map, which needs to be released
2767 * whenever node->sched is updated.
2768 * It is defined over the uncompressed node domain.
2770 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
2772 if (!node->sched_map) {
2773 isl_multi_aff *ma;
2775 ma = node_extract_schedule_multi_aff(node);
2776 node->sched_map = isl_map_from_multi_aff(ma);
2779 return isl_map_copy(node->sched_map);
2782 /* Construct a map that can be used to update a dependence relation
2783 * based on the current schedule.
2784 * That is, construct a map expressing that source and sink
2785 * are executed within the same iteration of the current schedule.
2786 * This map can then be intersected with the dependence relation.
2787 * This is not the most efficient way, but this shouldn't be a critical
2788 * operation.
2790 static __isl_give isl_map *specializer(struct isl_sched_node *src,
2791 struct isl_sched_node *dst)
2793 isl_map *src_sched, *dst_sched;
2795 src_sched = node_extract_schedule(src);
2796 dst_sched = node_extract_schedule(dst);
2797 return isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
2800 /* Intersect the domains of the nested relations in domain and range
2801 * of "umap" with "map".
2803 static __isl_give isl_union_map *intersect_domains(
2804 __isl_take isl_union_map *umap, __isl_keep isl_map *map)
2806 isl_union_set *uset;
2808 umap = isl_union_map_zip(umap);
2809 uset = isl_union_set_from_set(isl_map_wrap(isl_map_copy(map)));
2810 umap = isl_union_map_intersect_domain(umap, uset);
2811 umap = isl_union_map_zip(umap);
2812 return umap;
2815 /* Update the dependence relation of the given edge based
2816 * on the current schedule.
2817 * If the dependence is carried completely by the current schedule, then
2818 * it is removed from the edge_tables. It is kept in the list of edges
2819 * as otherwise all edge_tables would have to be recomputed.
2821 static int update_edge(struct isl_sched_graph *graph,
2822 struct isl_sched_edge *edge)
2824 int empty;
2825 isl_map *id;
2827 id = specializer(edge->src, edge->dst);
2828 edge->map = isl_map_intersect(edge->map, isl_map_copy(id));
2829 if (!edge->map)
2830 goto error;
2832 if (edge->tagged_condition) {
2833 edge->tagged_condition =
2834 intersect_domains(edge->tagged_condition, id);
2835 if (!edge->tagged_condition)
2836 goto error;
2838 if (edge->tagged_validity) {
2839 edge->tagged_validity =
2840 intersect_domains(edge->tagged_validity, id);
2841 if (!edge->tagged_validity)
2842 goto error;
2845 empty = isl_map_plain_is_empty(edge->map);
2846 if (empty < 0)
2847 goto error;
2848 if (empty)
2849 graph_remove_edge(graph, edge);
2851 isl_map_free(id);
2852 return 0;
2853 error:
2854 isl_map_free(id);
2855 return -1;
2858 /* Does the domain of "umap" intersect "uset"?
2860 static int domain_intersects(__isl_keep isl_union_map *umap,
2861 __isl_keep isl_union_set *uset)
2863 int empty;
2865 umap = isl_union_map_copy(umap);
2866 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(uset));
2867 empty = isl_union_map_is_empty(umap);
2868 isl_union_map_free(umap);
2870 return empty < 0 ? -1 : !empty;
2873 /* Does the range of "umap" intersect "uset"?
2875 static int range_intersects(__isl_keep isl_union_map *umap,
2876 __isl_keep isl_union_set *uset)
2878 int empty;
2880 umap = isl_union_map_copy(umap);
2881 umap = isl_union_map_intersect_range(umap, isl_union_set_copy(uset));
2882 empty = isl_union_map_is_empty(umap);
2883 isl_union_map_free(umap);
2885 return empty < 0 ? -1 : !empty;
2888 /* Are the condition dependences of "edge" local with respect to
2889 * the current schedule?
2891 * That is, are domain and range of the condition dependences mapped
2892 * to the same point?
2894 * In other words, is the condition false?
2896 static int is_condition_false(struct isl_sched_edge *edge)
2898 isl_union_map *umap;
2899 isl_map *map, *sched, *test;
2900 int empty, local;
2902 empty = isl_union_map_is_empty(edge->tagged_condition);
2903 if (empty < 0 || empty)
2904 return empty;
2906 umap = isl_union_map_copy(edge->tagged_condition);
2907 umap = isl_union_map_zip(umap);
2908 umap = isl_union_set_unwrap(isl_union_map_domain(umap));
2909 map = isl_map_from_union_map(umap);
2911 sched = node_extract_schedule(edge->src);
2912 map = isl_map_apply_domain(map, sched);
2913 sched = node_extract_schedule(edge->dst);
2914 map = isl_map_apply_range(map, sched);
2916 test = isl_map_identity(isl_map_get_space(map));
2917 local = isl_map_is_subset(map, test);
2918 isl_map_free(map);
2919 isl_map_free(test);
2921 return local;
2924 /* For each conditional validity constraint that is adjacent
2925 * to a condition with domain in condition_source or range in condition_sink,
2926 * turn it into an unconditional validity constraint.
2928 static int unconditionalize_adjacent_validity(struct isl_sched_graph *graph,
2929 __isl_take isl_union_set *condition_source,
2930 __isl_take isl_union_set *condition_sink)
2932 int i;
2934 condition_source = isl_union_set_coalesce(condition_source);
2935 condition_sink = isl_union_set_coalesce(condition_sink);
2937 for (i = 0; i < graph->n_edge; ++i) {
2938 int adjacent;
2939 isl_union_map *validity;
2941 if (!is_conditional_validity(&graph->edge[i]))
2942 continue;
2943 if (is_validity(&graph->edge[i]))
2944 continue;
2946 validity = graph->edge[i].tagged_validity;
2947 adjacent = domain_intersects(validity, condition_sink);
2948 if (adjacent >= 0 && !adjacent)
2949 adjacent = range_intersects(validity, condition_source);
2950 if (adjacent < 0)
2951 goto error;
2952 if (!adjacent)
2953 continue;
2955 set_validity(&graph->edge[i]);
2958 isl_union_set_free(condition_source);
2959 isl_union_set_free(condition_sink);
2960 return 0;
2961 error:
2962 isl_union_set_free(condition_source);
2963 isl_union_set_free(condition_sink);
2964 return -1;
2967 /* Update the dependence relations of all edges based on the current schedule
2968 * and enforce conditional validity constraints that are adjacent
2969 * to satisfied condition constraints.
2971 * First check if any of the condition constraints are satisfied
2972 * (i.e., not local to the outer schedule) and keep track of
2973 * their domain and range.
2974 * Then update all dependence relations (which removes the non-local
2975 * constraints).
2976 * Finally, if any condition constraints turned out to be satisfied,
2977 * then turn all adjacent conditional validity constraints into
2978 * unconditional validity constraints.
2980 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
2982 int i;
2983 int any = 0;
2984 isl_union_set *source, *sink;
2986 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
2987 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
2988 for (i = 0; i < graph->n_edge; ++i) {
2989 int local;
2990 isl_union_set *uset;
2991 isl_union_map *umap;
2993 if (!is_condition(&graph->edge[i]))
2994 continue;
2995 if (is_local(&graph->edge[i]))
2996 continue;
2997 local = is_condition_false(&graph->edge[i]);
2998 if (local < 0)
2999 goto error;
3000 if (local)
3001 continue;
3003 any = 1;
3005 umap = isl_union_map_copy(graph->edge[i].tagged_condition);
3006 uset = isl_union_map_domain(umap);
3007 source = isl_union_set_union(source, uset);
3009 umap = isl_union_map_copy(graph->edge[i].tagged_condition);
3010 uset = isl_union_map_range(umap);
3011 sink = isl_union_set_union(sink, uset);
3014 for (i = graph->n_edge - 1; i >= 0; --i) {
3015 if (update_edge(graph, &graph->edge[i]) < 0)
3016 goto error;
3019 if (any)
3020 return unconditionalize_adjacent_validity(graph, source, sink);
3022 isl_union_set_free(source);
3023 isl_union_set_free(sink);
3024 return 0;
3025 error:
3026 isl_union_set_free(source);
3027 isl_union_set_free(sink);
3028 return -1;
3031 static void next_band(struct isl_sched_graph *graph)
3033 graph->band_start = graph->n_total_row;
3036 /* Return the union of the universe domains of the nodes in "graph"
3037 * that satisfy "pred".
3039 static __isl_give isl_union_set *isl_sched_graph_domain(isl_ctx *ctx,
3040 struct isl_sched_graph *graph,
3041 int (*pred)(struct isl_sched_node *node, int data), int data)
3043 int i;
3044 isl_set *set;
3045 isl_union_set *dom;
3047 for (i = 0; i < graph->n; ++i)
3048 if (pred(&graph->node[i], data))
3049 break;
3051 if (i >= graph->n)
3052 isl_die(ctx, isl_error_internal,
3053 "empty component", return NULL);
3055 set = isl_set_universe(isl_space_copy(graph->node[i].space));
3056 dom = isl_union_set_from_set(set);
3058 for (i = i + 1; i < graph->n; ++i) {
3059 if (!pred(&graph->node[i], data))
3060 continue;
3061 set = isl_set_universe(isl_space_copy(graph->node[i].space));
3062 dom = isl_union_set_union(dom, isl_union_set_from_set(set));
3065 return dom;
3068 /* Return a list of unions of universe domains, where each element
3069 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
3071 static __isl_give isl_union_set_list *extract_sccs(isl_ctx *ctx,
3072 struct isl_sched_graph *graph)
3074 int i;
3075 isl_union_set_list *filters;
3077 filters = isl_union_set_list_alloc(ctx, graph->scc);
3078 for (i = 0; i < graph->scc; ++i) {
3079 isl_union_set *dom;
3081 dom = isl_sched_graph_domain(ctx, graph, &node_scc_exactly, i);
3082 filters = isl_union_set_list_add(filters, dom);
3085 return filters;
3088 /* Return a list of two unions of universe domains, one for the SCCs up
3089 * to and including graph->src_scc and another for the other SCCs.
3091 static __isl_give isl_union_set_list *extract_split(isl_ctx *ctx,
3092 struct isl_sched_graph *graph)
3094 isl_union_set *dom;
3095 isl_union_set_list *filters;
3097 filters = isl_union_set_list_alloc(ctx, 2);
3098 dom = isl_sched_graph_domain(ctx, graph,
3099 &node_scc_at_most, graph->src_scc);
3100 filters = isl_union_set_list_add(filters, dom);
3101 dom = isl_sched_graph_domain(ctx, graph,
3102 &node_scc_at_least, graph->src_scc + 1);
3103 filters = isl_union_set_list_add(filters, dom);
3105 return filters;
3108 /* Copy nodes that satisfy node_pred from the src dependence graph
3109 * to the dst dependence graph.
3111 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
3112 int (*node_pred)(struct isl_sched_node *node, int data), int data)
3114 int i;
3116 dst->n = 0;
3117 for (i = 0; i < src->n; ++i) {
3118 int j;
3120 if (!node_pred(&src->node[i], data))
3121 continue;
3123 j = dst->n;
3124 dst->node[j].space = isl_space_copy(src->node[i].space);
3125 dst->node[j].compressed = src->node[i].compressed;
3126 dst->node[j].hull = isl_set_copy(src->node[i].hull);
3127 dst->node[j].compress =
3128 isl_multi_aff_copy(src->node[i].compress);
3129 dst->node[j].decompress =
3130 isl_multi_aff_copy(src->node[i].decompress);
3131 dst->node[j].nvar = src->node[i].nvar;
3132 dst->node[j].nparam = src->node[i].nparam;
3133 dst->node[j].sched = isl_mat_copy(src->node[i].sched);
3134 dst->node[j].sched_map = isl_map_copy(src->node[i].sched_map);
3135 dst->node[j].coincident = src->node[i].coincident;
3136 dst->node[j].sizes = isl_multi_val_copy(src->node[i].sizes);
3137 dst->node[j].max = isl_vec_copy(src->node[i].max);
3138 dst->n++;
3140 if (!dst->node[j].space || !dst->node[j].sched)
3141 return -1;
3142 if (dst->node[j].compressed &&
3143 (!dst->node[j].hull || !dst->node[j].compress ||
3144 !dst->node[j].decompress))
3145 return -1;
3148 return 0;
3151 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
3152 * to the dst dependence graph.
3153 * If the source or destination node of the edge is not in the destination
3154 * graph, then it must be a backward proximity edge and it should simply
3155 * be ignored.
3157 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
3158 struct isl_sched_graph *src,
3159 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
3161 int i;
3163 dst->n_edge = 0;
3164 for (i = 0; i < src->n_edge; ++i) {
3165 struct isl_sched_edge *edge = &src->edge[i];
3166 isl_map *map;
3167 isl_union_map *tagged_condition;
3168 isl_union_map *tagged_validity;
3169 struct isl_sched_node *dst_src, *dst_dst;
3171 if (!edge_pred(edge, data))
3172 continue;
3174 if (isl_map_plain_is_empty(edge->map))
3175 continue;
3177 dst_src = graph_find_node(ctx, dst, edge->src->space);
3178 dst_dst = graph_find_node(ctx, dst, edge->dst->space);
3179 if (!dst_src || !dst_dst) {
3180 if (is_validity(edge) || is_conditional_validity(edge))
3181 isl_die(ctx, isl_error_internal,
3182 "backward (conditional) validity edge",
3183 return -1);
3184 continue;
3187 map = isl_map_copy(edge->map);
3188 tagged_condition = isl_union_map_copy(edge->tagged_condition);
3189 tagged_validity = isl_union_map_copy(edge->tagged_validity);
3191 dst->edge[dst->n_edge].src = dst_src;
3192 dst->edge[dst->n_edge].dst = dst_dst;
3193 dst->edge[dst->n_edge].map = map;
3194 dst->edge[dst->n_edge].tagged_condition = tagged_condition;
3195 dst->edge[dst->n_edge].tagged_validity = tagged_validity;
3196 dst->edge[dst->n_edge].types = edge->types;
3197 dst->n_edge++;
3199 if (edge->tagged_condition && !tagged_condition)
3200 return -1;
3201 if (edge->tagged_validity && !tagged_validity)
3202 return -1;
3204 if (graph_edge_tables_add(ctx, dst,
3205 &dst->edge[dst->n_edge - 1]) < 0)
3206 return -1;
3209 return 0;
3212 /* Compute the maximal number of variables over all nodes.
3213 * This is the maximal number of linearly independent schedule
3214 * rows that we need to compute.
3215 * Just in case we end up in a part of the dependence graph
3216 * with only lower-dimensional domains, we make sure we will
3217 * compute the required amount of extra linearly independent rows.
3219 static int compute_maxvar(struct isl_sched_graph *graph)
3221 int i;
3223 graph->maxvar = 0;
3224 for (i = 0; i < graph->n; ++i) {
3225 struct isl_sched_node *node = &graph->node[i];
3226 int nvar;
3228 if (node_update_cmap(node) < 0)
3229 return -1;
3230 nvar = node->nvar + graph->n_row - node->rank;
3231 if (nvar > graph->maxvar)
3232 graph->maxvar = nvar;
3235 return 0;
3238 /* Extract the subgraph of "graph" that consists of the node satisfying
3239 * "node_pred" and the edges satisfying "edge_pred" and store
3240 * the result in "sub".
3242 static int extract_sub_graph(isl_ctx *ctx, struct isl_sched_graph *graph,
3243 int (*node_pred)(struct isl_sched_node *node, int data),
3244 int (*edge_pred)(struct isl_sched_edge *edge, int data),
3245 int data, struct isl_sched_graph *sub)
3247 int i, n = 0, n_edge = 0;
3248 int t;
3250 for (i = 0; i < graph->n; ++i)
3251 if (node_pred(&graph->node[i], data))
3252 ++n;
3253 for (i = 0; i < graph->n_edge; ++i)
3254 if (edge_pred(&graph->edge[i], data))
3255 ++n_edge;
3256 if (graph_alloc(ctx, sub, n, n_edge) < 0)
3257 return -1;
3258 if (copy_nodes(sub, graph, node_pred, data) < 0)
3259 return -1;
3260 if (graph_init_table(ctx, sub) < 0)
3261 return -1;
3262 for (t = 0; t <= isl_edge_last; ++t)
3263 sub->max_edge[t] = graph->max_edge[t];
3264 if (graph_init_edge_tables(ctx, sub) < 0)
3265 return -1;
3266 if (copy_edges(ctx, sub, graph, edge_pred, data) < 0)
3267 return -1;
3268 sub->n_row = graph->n_row;
3269 sub->max_row = graph->max_row;
3270 sub->n_total_row = graph->n_total_row;
3271 sub->band_start = graph->band_start;
3273 return 0;
3276 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
3277 struct isl_sched_graph *graph);
3278 static __isl_give isl_schedule_node *compute_schedule_wcc(
3279 isl_schedule_node *node, struct isl_sched_graph *graph);
3281 /* Compute a schedule for a subgraph of "graph". In particular, for
3282 * the graph composed of nodes that satisfy node_pred and edges that
3283 * that satisfy edge_pred.
3284 * If the subgraph is known to consist of a single component, then wcc should
3285 * be set and then we call compute_schedule_wcc on the constructed subgraph.
3286 * Otherwise, we call compute_schedule, which will check whether the subgraph
3287 * is connected.
3289 * The schedule is inserted at "node" and the updated schedule node
3290 * is returned.
3292 static __isl_give isl_schedule_node *compute_sub_schedule(
3293 __isl_take isl_schedule_node *node, isl_ctx *ctx,
3294 struct isl_sched_graph *graph,
3295 int (*node_pred)(struct isl_sched_node *node, int data),
3296 int (*edge_pred)(struct isl_sched_edge *edge, int data),
3297 int data, int wcc)
3299 struct isl_sched_graph split = { 0 };
3301 if (extract_sub_graph(ctx, graph, node_pred, edge_pred, data,
3302 &split) < 0)
3303 goto error;
3305 if (wcc)
3306 node = compute_schedule_wcc(node, &split);
3307 else
3308 node = compute_schedule(node, &split);
3310 graph_free(ctx, &split);
3311 return node;
3312 error:
3313 graph_free(ctx, &split);
3314 return isl_schedule_node_free(node);
3317 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
3319 return edge->src->scc == scc && edge->dst->scc == scc;
3322 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
3324 return edge->dst->scc <= scc;
3327 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
3329 return edge->src->scc >= scc;
3332 /* Reset the current band by dropping all its schedule rows.
3334 static int reset_band(struct isl_sched_graph *graph)
3336 int i;
3337 int drop;
3339 drop = graph->n_total_row - graph->band_start;
3340 graph->n_total_row -= drop;
3341 graph->n_row -= drop;
3343 for (i = 0; i < graph->n; ++i) {
3344 struct isl_sched_node *node = &graph->node[i];
3346 isl_map_free(node->sched_map);
3347 node->sched_map = NULL;
3349 node->sched = isl_mat_drop_rows(node->sched,
3350 graph->band_start, drop);
3352 if (!node->sched)
3353 return -1;
3356 return 0;
3359 /* Split the current graph into two parts and compute a schedule for each
3360 * part individually. In particular, one part consists of all SCCs up
3361 * to and including graph->src_scc, while the other part contains the other
3362 * SCCs. The split is enforced by a sequence node inserted at position "node"
3363 * in the schedule tree. Return the updated schedule node.
3364 * If either of these two parts consists of a sequence, then it is spliced
3365 * into the sequence containing the two parts.
3367 * The current band is reset. It would be possible to reuse
3368 * the previously computed rows as the first rows in the next
3369 * band, but recomputing them may result in better rows as we are looking
3370 * at a smaller part of the dependence graph.
3372 static __isl_give isl_schedule_node *compute_split_schedule(
3373 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3375 int is_seq;
3376 isl_ctx *ctx;
3377 isl_union_set_list *filters;
3379 if (!node)
3380 return NULL;
3382 if (reset_band(graph) < 0)
3383 return isl_schedule_node_free(node);
3385 next_band(graph);
3387 ctx = isl_schedule_node_get_ctx(node);
3388 filters = extract_split(ctx, graph);
3389 node = isl_schedule_node_insert_sequence(node, filters);
3390 node = isl_schedule_node_child(node, 1);
3391 node = isl_schedule_node_child(node, 0);
3393 node = compute_sub_schedule(node, ctx, graph,
3394 &node_scc_at_least, &edge_src_scc_at_least,
3395 graph->src_scc + 1, 0);
3396 is_seq = isl_schedule_node_get_type(node) == isl_schedule_node_sequence;
3397 node = isl_schedule_node_parent(node);
3398 node = isl_schedule_node_parent(node);
3399 if (is_seq)
3400 node = isl_schedule_node_sequence_splice_child(node, 1);
3401 node = isl_schedule_node_child(node, 0);
3402 node = isl_schedule_node_child(node, 0);
3403 node = compute_sub_schedule(node, ctx, graph,
3404 &node_scc_at_most, &edge_dst_scc_at_most,
3405 graph->src_scc, 0);
3406 is_seq = isl_schedule_node_get_type(node) == isl_schedule_node_sequence;
3407 node = isl_schedule_node_parent(node);
3408 node = isl_schedule_node_parent(node);
3409 if (is_seq)
3410 node = isl_schedule_node_sequence_splice_child(node, 0);
3412 return node;
3415 /* Insert a band node at position "node" in the schedule tree corresponding
3416 * to the current band in "graph". Mark the band node permutable
3417 * if "permutable" is set.
3418 * The partial schedules and the coincidence property are extracted
3419 * from the graph nodes.
3420 * Return the updated schedule node.
3422 static __isl_give isl_schedule_node *insert_current_band(
3423 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3424 int permutable)
3426 int i;
3427 int start, end, n;
3428 isl_multi_aff *ma;
3429 isl_multi_pw_aff *mpa;
3430 isl_multi_union_pw_aff *mupa;
3432 if (!node)
3433 return NULL;
3435 if (graph->n < 1)
3436 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
3437 "graph should have at least one node",
3438 return isl_schedule_node_free(node));
3440 start = graph->band_start;
3441 end = graph->n_total_row;
3442 n = end - start;
3444 ma = node_extract_partial_schedule_multi_aff(&graph->node[0], start, n);
3445 mpa = isl_multi_pw_aff_from_multi_aff(ma);
3446 mupa = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
3448 for (i = 1; i < graph->n; ++i) {
3449 isl_multi_union_pw_aff *mupa_i;
3451 ma = node_extract_partial_schedule_multi_aff(&graph->node[i],
3452 start, n);
3453 mpa = isl_multi_pw_aff_from_multi_aff(ma);
3454 mupa_i = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
3455 mupa = isl_multi_union_pw_aff_union_add(mupa, mupa_i);
3457 node = isl_schedule_node_insert_partial_schedule(node, mupa);
3459 for (i = 0; i < n; ++i)
3460 node = isl_schedule_node_band_member_set_coincident(node, i,
3461 graph->node[0].coincident[start + i]);
3462 node = isl_schedule_node_band_set_permutable(node, permutable);
3464 return node;
3467 /* Update the dependence relations based on the current schedule,
3468 * add the current band to "node" and then continue with the computation
3469 * of the next band.
3470 * Return the updated schedule node.
3472 static __isl_give isl_schedule_node *compute_next_band(
3473 __isl_take isl_schedule_node *node,
3474 struct isl_sched_graph *graph, int permutable)
3476 isl_ctx *ctx;
3478 if (!node)
3479 return NULL;
3481 ctx = isl_schedule_node_get_ctx(node);
3482 if (update_edges(ctx, graph) < 0)
3483 return isl_schedule_node_free(node);
3484 node = insert_current_band(node, graph, permutable);
3485 next_band(graph);
3487 node = isl_schedule_node_child(node, 0);
3488 node = compute_schedule(node, graph);
3489 node = isl_schedule_node_parent(node);
3491 return node;
3494 /* Add constraints to graph->lp that force the dependence "map" (which
3495 * is part of the dependence relation of "edge")
3496 * to be respected and attempt to carry it, where the edge is one from
3497 * a node j to itself. "pos" is the sequence number of the given map.
3498 * That is, add constraints that enforce
3500 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3501 * = c_j_x (y - x) >= e_i
3503 * for each (x,y) in R.
3504 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3505 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3506 * with each coefficient in c_j_x represented as a pair of non-negative
3507 * coefficients.
3509 static int add_intra_constraints(struct isl_sched_graph *graph,
3510 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3512 int offset;
3513 isl_ctx *ctx = isl_map_get_ctx(map);
3514 isl_dim_map *dim_map;
3515 isl_basic_set *coef;
3516 struct isl_sched_node *node = edge->src;
3518 coef = intra_coefficients(graph, node, map);
3519 if (!coef)
3520 return -1;
3522 offset = coef_var_offset(coef);
3523 dim_map = intra_dim_map(ctx, graph, node, offset, 1);
3524 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3525 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3526 coef->n_eq, coef->n_ineq);
3527 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3528 coef, dim_map);
3530 return 0;
3533 /* Add constraints to graph->lp that force the dependence "map" (which
3534 * is part of the dependence relation of "edge")
3535 * to be respected and attempt to carry it, where the edge is one from
3536 * node j to node k. "pos" is the sequence number of the given map.
3537 * That is, add constraints that enforce
3539 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3541 * for each (x,y) in R.
3542 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3543 * of valid constraints for R and then plug in
3544 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3545 * with each coefficient (except e_i, c_*_0 and c_*_n)
3546 * represented as a pair of non-negative coefficients.
3548 static int add_inter_constraints(struct isl_sched_graph *graph,
3549 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3551 int offset;
3552 isl_ctx *ctx = isl_map_get_ctx(map);
3553 isl_dim_map *dim_map;
3554 isl_basic_set *coef;
3555 struct isl_sched_node *src = edge->src;
3556 struct isl_sched_node *dst = edge->dst;
3558 coef = inter_coefficients(graph, edge, map);
3559 if (!coef)
3560 return -1;
3562 offset = coef_var_offset(coef);
3563 dim_map = inter_dim_map(ctx, graph, src, dst, offset, 1);
3564 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3565 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3566 coef->n_eq, coef->n_ineq);
3567 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3568 coef, dim_map);
3570 return 0;
3573 /* Add constraints to graph->lp that force all (conditional) validity
3574 * dependences to be respected and attempt to carry them.
3576 static int add_all_constraints(struct isl_sched_graph *graph)
3578 int i, j;
3579 int pos;
3581 pos = 0;
3582 for (i = 0; i < graph->n_edge; ++i) {
3583 struct isl_sched_edge *edge= &graph->edge[i];
3585 if (!is_any_validity(edge))
3586 continue;
3588 for (j = 0; j < edge->map->n; ++j) {
3589 isl_basic_map *bmap;
3590 isl_map *map;
3592 bmap = isl_basic_map_copy(edge->map->p[j]);
3593 map = isl_map_from_basic_map(bmap);
3595 if (edge->src == edge->dst &&
3596 add_intra_constraints(graph, edge, map, pos) < 0)
3597 return -1;
3598 if (edge->src != edge->dst &&
3599 add_inter_constraints(graph, edge, map, pos) < 0)
3600 return -1;
3601 ++pos;
3605 return 0;
3608 /* Count the number of equality and inequality constraints
3609 * that will be added to the carry_lp problem.
3610 * We count each edge exactly once.
3612 static int count_all_constraints(struct isl_sched_graph *graph,
3613 int *n_eq, int *n_ineq)
3615 int i, j;
3617 *n_eq = *n_ineq = 0;
3618 for (i = 0; i < graph->n_edge; ++i) {
3619 struct isl_sched_edge *edge= &graph->edge[i];
3621 if (!is_any_validity(edge))
3622 continue;
3624 for (j = 0; j < edge->map->n; ++j) {
3625 isl_basic_map *bmap;
3626 isl_map *map;
3628 bmap = isl_basic_map_copy(edge->map->p[j]);
3629 map = isl_map_from_basic_map(bmap);
3631 if (count_map_constraints(graph, edge, map,
3632 n_eq, n_ineq, 1, 0) < 0)
3633 return -1;
3637 return 0;
3640 /* Return the total number of (validity) edges that carry_dependences will
3641 * attempt to carry.
3643 static int count_carry_edges(struct isl_sched_graph *graph)
3645 int i;
3646 int n_edge;
3648 n_edge = 0;
3649 for (i = 0; i < graph->n_edge; ++i) {
3650 struct isl_sched_edge *edge = &graph->edge[i];
3652 if (!is_any_validity(edge))
3653 continue;
3655 n_edge += isl_map_n_basic_map(edge->map);
3658 return n_edge;
3661 /* Construct an LP problem for finding schedule coefficients
3662 * such that the schedule carries as many validity dependences as possible.
3663 * In particular, for each dependence i, we bound the dependence distance
3664 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3665 * of all e_i's. Dependences with e_i = 0 in the solution are simply
3666 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3667 * Note that if the dependence relation is a union of basic maps,
3668 * then we have to consider each basic map individually as it may only
3669 * be possible to carry the dependences expressed by some of those
3670 * basic maps and not all of them.
3671 * Below, we consider each of those basic maps as a separate "edge".
3673 * All variables of the LP are non-negative. The actual coefficients
3674 * may be negative, so each coefficient is represented as the difference
3675 * of two non-negative variables. The negative part always appears
3676 * immediately before the positive part.
3677 * Other than that, the variables have the following order
3679 * - sum of (1 - e_i) over all edges
3680 * - sum of all c_n coefficients
3681 * (unconstrained when computing non-parametric schedules)
3682 * - sum of positive and negative parts of all c_x coefficients
3683 * - for each edge
3684 * - e_i
3685 * - for each node
3686 * - c_i_0
3687 * - c_i_n (if parametric)
3688 * - positive and negative parts of c_i_x
3690 * The constraints are those from the (validity) edges plus three equalities
3691 * to express the sums and n_edge inequalities to express e_i <= 1.
3693 static isl_stat setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
3695 int i;
3696 int k;
3697 isl_space *dim;
3698 unsigned total;
3699 int n_eq, n_ineq;
3700 int n_edge;
3702 n_edge = count_carry_edges(graph);
3704 total = 3 + n_edge;
3705 for (i = 0; i < graph->n; ++i) {
3706 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
3707 node->start = total;
3708 total += 1 + node->nparam + 2 * node->nvar;
3711 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
3712 return isl_stat_error;
3714 dim = isl_space_set_alloc(ctx, 0, total);
3715 isl_basic_set_free(graph->lp);
3716 n_eq += 3;
3717 n_ineq += n_edge;
3718 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
3719 graph->lp = isl_basic_set_set_rational(graph->lp);
3721 k = isl_basic_set_alloc_equality(graph->lp);
3722 if (k < 0)
3723 return isl_stat_error;
3724 isl_seq_clr(graph->lp->eq[k], 1 + total);
3725 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
3726 isl_int_set_si(graph->lp->eq[k][1], 1);
3727 for (i = 0; i < n_edge; ++i)
3728 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
3730 if (add_param_sum_constraint(graph, 1) < 0)
3731 return isl_stat_error;
3732 if (add_var_sum_constraint(graph, 2) < 0)
3733 return isl_stat_error;
3735 for (i = 0; i < n_edge; ++i) {
3736 k = isl_basic_set_alloc_inequality(graph->lp);
3737 if (k < 0)
3738 return isl_stat_error;
3739 isl_seq_clr(graph->lp->ineq[k], 1 + total);
3740 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
3741 isl_int_set_si(graph->lp->ineq[k][0], 1);
3744 if (add_all_constraints(graph) < 0)
3745 return isl_stat_error;
3747 return isl_stat_ok;
3750 static __isl_give isl_schedule_node *compute_component_schedule(
3751 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3752 int wcc);
3754 /* Comparison function for sorting the statements based on
3755 * the corresponding value in "r".
3757 static int smaller_value(const void *a, const void *b, void *data)
3759 isl_vec *r = data;
3760 const int *i1 = a;
3761 const int *i2 = b;
3763 return isl_int_cmp(r->el[*i1], r->el[*i2]);
3766 /* If the schedule_split_scaled option is set and if the linear
3767 * parts of the scheduling rows for all nodes in the graphs have
3768 * a non-trivial common divisor, then split off the remainder of the
3769 * constant term modulo this common divisor from the linear part.
3770 * Otherwise, insert a band node directly and continue with
3771 * the construction of the schedule.
3773 * If a non-trivial common divisor is found, then
3774 * the linear part is reduced and the remainder is enforced
3775 * by a sequence node with the children placed in the order
3776 * of this remainder.
3777 * In particular, we assign an scc index based on the remainder and
3778 * then rely on compute_component_schedule to insert the sequence and
3779 * to continue the schedule construction on each part.
3781 static __isl_give isl_schedule_node *split_scaled(
3782 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3784 int i;
3785 int row;
3786 int scc;
3787 isl_ctx *ctx;
3788 isl_int gcd, gcd_i;
3789 isl_vec *r;
3790 int *order;
3792 if (!node)
3793 return NULL;
3795 ctx = isl_schedule_node_get_ctx(node);
3796 if (!ctx->opt->schedule_split_scaled)
3797 return compute_next_band(node, graph, 0);
3798 if (graph->n <= 1)
3799 return compute_next_band(node, graph, 0);
3801 isl_int_init(gcd);
3802 isl_int_init(gcd_i);
3804 isl_int_set_si(gcd, 0);
3806 row = isl_mat_rows(graph->node[0].sched) - 1;
3808 for (i = 0; i < graph->n; ++i) {
3809 struct isl_sched_node *node = &graph->node[i];
3810 int cols = isl_mat_cols(node->sched);
3812 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
3813 isl_int_gcd(gcd, gcd, gcd_i);
3816 isl_int_clear(gcd_i);
3818 if (isl_int_cmp_si(gcd, 1) <= 0) {
3819 isl_int_clear(gcd);
3820 return compute_next_band(node, graph, 0);
3823 r = isl_vec_alloc(ctx, graph->n);
3824 order = isl_calloc_array(ctx, int, graph->n);
3825 if (!r || !order)
3826 goto error;
3828 for (i = 0; i < graph->n; ++i) {
3829 struct isl_sched_node *node = &graph->node[i];
3831 order[i] = i;
3832 isl_int_fdiv_r(r->el[i], node->sched->row[row][0], gcd);
3833 isl_int_fdiv_q(node->sched->row[row][0],
3834 node->sched->row[row][0], gcd);
3835 isl_int_mul(node->sched->row[row][0],
3836 node->sched->row[row][0], gcd);
3837 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
3838 if (!node->sched)
3839 goto error;
3842 if (isl_sort(order, graph->n, sizeof(order[0]), &smaller_value, r) < 0)
3843 goto error;
3845 scc = 0;
3846 for (i = 0; i < graph->n; ++i) {
3847 if (i > 0 && isl_int_ne(r->el[order[i - 1]], r->el[order[i]]))
3848 ++scc;
3849 graph->node[order[i]].scc = scc;
3851 graph->scc = ++scc;
3852 graph->weak = 0;
3854 isl_int_clear(gcd);
3855 isl_vec_free(r);
3856 free(order);
3858 if (update_edges(ctx, graph) < 0)
3859 return isl_schedule_node_free(node);
3860 node = insert_current_band(node, graph, 0);
3861 next_band(graph);
3863 node = isl_schedule_node_child(node, 0);
3864 node = compute_component_schedule(node, graph, 0);
3865 node = isl_schedule_node_parent(node);
3867 return node;
3868 error:
3869 isl_vec_free(r);
3870 free(order);
3871 isl_int_clear(gcd);
3872 return isl_schedule_node_free(node);
3875 /* Is the schedule row "sol" trivial on node "node"?
3876 * That is, is the solution zero on the dimensions orthogonal to
3877 * the previously found solutions?
3878 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3880 * Each coefficient is represented as the difference between
3881 * two non-negative values in "sol". "sol" has been computed
3882 * in terms of the original iterators (i.e., without use of cmap).
3883 * We construct the schedule row s and write it as a linear
3884 * combination of (linear combinations of) previously computed schedule rows.
3885 * s = Q c or c = U s.
3886 * If the final entries of c are all zero, then the solution is trivial.
3888 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
3890 int trivial;
3891 isl_vec *node_sol;
3893 if (!sol)
3894 return -1;
3895 if (node->nvar == node->rank)
3896 return 0;
3898 node_sol = extract_var_coef(node, sol);
3899 node_sol = isl_mat_vec_product(isl_mat_copy(node->cinv), node_sol);
3900 if (!node_sol)
3901 return -1;
3903 trivial = isl_seq_first_non_zero(node_sol->el + node->rank,
3904 node->nvar - node->rank) == -1;
3906 isl_vec_free(node_sol);
3908 return trivial;
3911 /* Is the schedule row "sol" trivial on any node where it should
3912 * not be trivial?
3913 * "sol" has been computed in terms of the original iterators
3914 * (i.e., without use of cmap).
3915 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3917 static int is_any_trivial(struct isl_sched_graph *graph,
3918 __isl_keep isl_vec *sol)
3920 int i;
3922 for (i = 0; i < graph->n; ++i) {
3923 struct isl_sched_node *node = &graph->node[i];
3924 int trivial;
3926 if (!needs_row(graph, node))
3927 continue;
3928 trivial = is_trivial(node, sol);
3929 if (trivial < 0 || trivial)
3930 return trivial;
3933 return 0;
3936 /* Does the schedule represented by "sol" perform loop coalescing on "node"?
3937 * If so, return the position of the coalesced dimension.
3938 * Otherwise, return node->nvar or -1 on error.
3940 * In particular, look for pairs of coefficients c_i and c_j such that
3941 * |c_j/c_i| >= size_i, i.e., |c_j| >= |c_i * size_i|.
3942 * If any such pair is found, then return i.
3943 * If size_i is infinity, then no check on c_i needs to be performed.
3945 static int find_node_coalescing(struct isl_sched_node *node,
3946 __isl_keep isl_vec *sol)
3948 int i, j;
3949 isl_int max;
3950 isl_vec *csol;
3952 if (node->nvar <= 1)
3953 return node->nvar;
3955 csol = extract_var_coef(node, sol);
3956 if (!csol)
3957 return -1;
3958 isl_int_init(max);
3959 for (i = 0; i < node->nvar; ++i) {
3960 isl_val *v;
3962 if (isl_int_is_zero(csol->el[i]))
3963 continue;
3964 v = isl_multi_val_get_val(node->sizes, i);
3965 if (!v)
3966 goto error;
3967 if (!isl_val_is_int(v)) {
3968 isl_val_free(v);
3969 continue;
3971 isl_int_mul(max, v->n, csol->el[i]);
3972 isl_val_free(v);
3974 for (j = 0; j < node->nvar; ++j) {
3975 if (j == i)
3976 continue;
3977 if (isl_int_abs_ge(csol->el[j], max))
3978 break;
3980 if (j < node->nvar)
3981 break;
3984 isl_int_clear(max);
3985 isl_vec_free(csol);
3986 return i;
3987 error:
3988 isl_int_clear(max);
3989 isl_vec_free(csol);
3990 return -1;
3993 /* Force the schedule coefficient at position "pos" of "node" to be zero
3994 * in "tl".
3995 * The coefficient is encoded as the difference between two non-negative
3996 * variables. Force these two variables to have the same value.
3998 static __isl_give isl_tab_lexmin *zero_out_node_coef(
3999 __isl_take isl_tab_lexmin *tl, struct isl_sched_node *node, int pos)
4001 int dim;
4002 isl_ctx *ctx;
4003 isl_vec *eq;
4005 ctx = isl_space_get_ctx(node->space);
4006 dim = isl_tab_lexmin_dim(tl);
4007 if (dim < 0)
4008 return isl_tab_lexmin_free(tl);
4009 eq = isl_vec_alloc(ctx, 1 + dim);
4010 eq = isl_vec_clr(eq);
4011 if (!eq)
4012 return isl_tab_lexmin_free(tl);
4014 pos = 1 + node_var_coef_offset(node) + 2 * pos;
4015 isl_int_set_si(eq->el[pos], 1);
4016 isl_int_set_si(eq->el[pos + 1], -1);
4017 tl = isl_tab_lexmin_add_eq(tl, eq->el);
4018 isl_vec_free(eq);
4020 return tl;
4023 /* Return the lexicographically smallest rational point in the basic set
4024 * from which "tl" was constructed, double checking that this input set
4025 * was not empty.
4027 static __isl_give isl_vec *non_empty_solution(__isl_keep isl_tab_lexmin *tl)
4029 isl_vec *sol;
4031 sol = isl_tab_lexmin_get_solution(tl);
4032 if (!sol)
4033 return NULL;
4034 if (sol->size == 0)
4035 isl_die(isl_vec_get_ctx(sol), isl_error_internal,
4036 "error in schedule construction",
4037 return isl_vec_free(sol));
4038 return sol;
4041 /* Does the solution "sol" of the LP problem constructed by setup_carry_lp
4042 * carry any of the "n_edge" groups of dependences?
4043 * The value in the first position is the sum of (1 - e_i) over all "n_edge"
4044 * edges, with 0 <= e_i <= 1 equal to 1 when the dependences represented
4045 * by the edge are carried by the solution.
4046 * If the sum of the (1 - e_i) is smaller than "n_edge" then at least
4047 * one of those is carried.
4049 * Note that despite the fact that the problem is solved using a rational
4050 * solver, the solution is guaranteed to be integral.
4051 * Specifically, the dependence distance lower bounds e_i (and therefore
4052 * also their sum) are integers. See Lemma 5 of [1].
4054 * Any potential denominator of the sum is cleared by this function.
4055 * The denominator is not relevant for any of the other elements
4056 * in the solution.
4058 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4059 * Problem, Part II: Multi-Dimensional Time.
4060 * In Intl. Journal of Parallel Programming, 1992.
4062 static int carries_dependences(__isl_keep isl_vec *sol, int n_edge)
4064 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
4065 isl_int_set_si(sol->el[0], 1);
4066 return isl_int_cmp_si(sol->el[1], n_edge) < 0;
4069 /* Return the lexicographically smallest rational point in "lp",
4070 * assuming that all variables are non-negative and performing some
4071 * additional sanity checks.
4072 * In particular, "lp" should not be empty by construction.
4073 * Double check that this is the case.
4074 * Also, check that dependences are carried for at least one of
4075 * the "n_edge" edges.
4077 * If the computed schedule performs loop coalescing on a given node,
4078 * i.e., if it is of the form
4080 * c_i i + c_j j + ...
4082 * with |c_j/c_i| >= size_i, then force the coefficient c_i to be zero
4083 * to cut out this solution. Repeat this process until no more loop
4084 * coalescing occurs or until no more dependences can be carried.
4085 * In the latter case, revert to the previously computed solution.
4087 static __isl_give isl_vec *non_neg_lexmin(struct isl_sched_graph *graph,
4088 __isl_take isl_basic_set *lp, int n_edge)
4090 int i, pos;
4091 isl_ctx *ctx;
4092 isl_tab_lexmin *tl;
4093 isl_vec *sol, *prev = NULL;
4094 int treat_coalescing;
4096 if (!lp)
4097 return NULL;
4098 ctx = isl_basic_set_get_ctx(lp);
4099 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4100 tl = isl_tab_lexmin_from_basic_set(lp);
4102 do {
4103 sol = non_empty_solution(tl);
4104 if (!sol)
4105 goto error;
4107 if (!carries_dependences(sol, n_edge)) {
4108 if (!prev)
4109 isl_die(ctx, isl_error_unknown,
4110 "unable to carry dependences",
4111 goto error);
4112 isl_vec_free(sol);
4113 sol = prev;
4114 break;
4116 prev = isl_vec_free(prev);
4117 if (!treat_coalescing)
4118 break;
4119 for (i = 0; i < graph->n; ++i) {
4120 struct isl_sched_node *node = &graph->node[i];
4122 pos = find_node_coalescing(node, sol);
4123 if (pos < 0)
4124 goto error;
4125 if (pos < node->nvar)
4126 break;
4128 if (i < graph->n) {
4129 prev = sol;
4130 tl = zero_out_node_coef(tl, &graph->node[i], pos);
4132 } while (i < graph->n);
4134 isl_tab_lexmin_free(tl);
4136 return sol;
4137 error:
4138 isl_tab_lexmin_free(tl);
4139 isl_vec_free(prev);
4140 isl_vec_free(sol);
4141 return NULL;
4144 /* Construct a schedule row for each node such that as many validity dependences
4145 * as possible are carried and then continue with the next band.
4147 * If there are no validity dependences, then no dependence can be carried and
4148 * the procedure is guaranteed to fail. If there is more than one component,
4149 * then try computing a schedule on each component separately
4150 * to prevent or at least postpone this failure.
4152 * If the computed schedule row turns out to be trivial on one or
4153 * more nodes where it should not be trivial, then we throw it away
4154 * and try again on each component separately.
4156 * If there is only one component, then we accept the schedule row anyway,
4157 * but we do not consider it as a complete row and therefore do not
4158 * increment graph->n_row. Note that the ranks of the nodes that
4159 * do get a non-trivial schedule part will get updated regardless and
4160 * graph->maxvar is computed based on these ranks. The test for
4161 * whether more schedule rows are required in compute_schedule_wcc
4162 * is therefore not affected.
4164 * Insert a band corresponding to the schedule row at position "node"
4165 * of the schedule tree and continue with the construction of the schedule.
4166 * This insertion and the continued construction is performed by split_scaled
4167 * after optionally checking for non-trivial common divisors.
4169 static __isl_give isl_schedule_node *carry_dependences(
4170 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
4172 int n_edge;
4173 int trivial;
4174 isl_ctx *ctx;
4175 isl_vec *sol;
4176 isl_basic_set *lp;
4178 if (!node)
4179 return NULL;
4181 n_edge = count_carry_edges(graph);
4182 if (n_edge == 0 && graph->scc > 1)
4183 return compute_component_schedule(node, graph, 1);
4185 ctx = isl_schedule_node_get_ctx(node);
4186 if (setup_carry_lp(ctx, graph) < 0)
4187 return isl_schedule_node_free(node);
4189 lp = isl_basic_set_copy(graph->lp);
4190 sol = non_neg_lexmin(graph, lp, n_edge);
4191 if (!sol)
4192 return isl_schedule_node_free(node);
4194 trivial = is_any_trivial(graph, sol);
4195 if (trivial < 0) {
4196 sol = isl_vec_free(sol);
4197 } else if (trivial && graph->scc > 1) {
4198 isl_vec_free(sol);
4199 return compute_component_schedule(node, graph, 1);
4202 if (update_schedule(graph, sol, 0, 0) < 0)
4203 return isl_schedule_node_free(node);
4204 if (trivial)
4205 graph->n_row--;
4207 return split_scaled(node, graph);
4210 /* Topologically sort statements mapped to the same schedule iteration
4211 * and add insert a sequence node in front of "node"
4212 * corresponding to this order.
4213 * If "initialized" is set, then it may be assumed that compute_maxvar
4214 * has been called on the current band. Otherwise, call
4215 * compute_maxvar if and before carry_dependences gets called.
4217 * If it turns out to be impossible to sort the statements apart,
4218 * because different dependences impose different orderings
4219 * on the statements, then we extend the schedule such that
4220 * it carries at least one more dependence.
4222 static __isl_give isl_schedule_node *sort_statements(
4223 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
4224 int initialized)
4226 isl_ctx *ctx;
4227 isl_union_set_list *filters;
4229 if (!node)
4230 return NULL;
4232 ctx = isl_schedule_node_get_ctx(node);
4233 if (graph->n < 1)
4234 isl_die(ctx, isl_error_internal,
4235 "graph should have at least one node",
4236 return isl_schedule_node_free(node));
4238 if (graph->n == 1)
4239 return node;
4241 if (update_edges(ctx, graph) < 0)
4242 return isl_schedule_node_free(node);
4244 if (graph->n_edge == 0)
4245 return node;
4247 if (detect_sccs(ctx, graph) < 0)
4248 return isl_schedule_node_free(node);
4250 next_band(graph);
4251 if (graph->scc < graph->n) {
4252 if (!initialized && compute_maxvar(graph) < 0)
4253 return isl_schedule_node_free(node);
4254 return carry_dependences(node, graph);
4257 filters = extract_sccs(ctx, graph);
4258 node = isl_schedule_node_insert_sequence(node, filters);
4260 return node;
4263 /* Are there any (non-empty) (conditional) validity edges in the graph?
4265 static int has_validity_edges(struct isl_sched_graph *graph)
4267 int i;
4269 for (i = 0; i < graph->n_edge; ++i) {
4270 int empty;
4272 empty = isl_map_plain_is_empty(graph->edge[i].map);
4273 if (empty < 0)
4274 return -1;
4275 if (empty)
4276 continue;
4277 if (is_any_validity(&graph->edge[i]))
4278 return 1;
4281 return 0;
4284 /* Should we apply a Feautrier step?
4285 * That is, did the user request the Feautrier algorithm and are
4286 * there any validity dependences (left)?
4288 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
4290 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
4291 return 0;
4293 return has_validity_edges(graph);
4296 /* Compute a schedule for a connected dependence graph using Feautrier's
4297 * multi-dimensional scheduling algorithm and return the updated schedule node.
4299 * The original algorithm is described in [1].
4300 * The main idea is to minimize the number of scheduling dimensions, by
4301 * trying to satisfy as many dependences as possible per scheduling dimension.
4303 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4304 * Problem, Part II: Multi-Dimensional Time.
4305 * In Intl. Journal of Parallel Programming, 1992.
4307 static __isl_give isl_schedule_node *compute_schedule_wcc_feautrier(
4308 isl_schedule_node *node, struct isl_sched_graph *graph)
4310 return carry_dependences(node, graph);
4313 /* Turn off the "local" bit on all (condition) edges.
4315 static void clear_local_edges(struct isl_sched_graph *graph)
4317 int i;
4319 for (i = 0; i < graph->n_edge; ++i)
4320 if (is_condition(&graph->edge[i]))
4321 clear_local(&graph->edge[i]);
4324 /* Does "graph" have both condition and conditional validity edges?
4326 static int need_condition_check(struct isl_sched_graph *graph)
4328 int i;
4329 int any_condition = 0;
4330 int any_conditional_validity = 0;
4332 for (i = 0; i < graph->n_edge; ++i) {
4333 if (is_condition(&graph->edge[i]))
4334 any_condition = 1;
4335 if (is_conditional_validity(&graph->edge[i]))
4336 any_conditional_validity = 1;
4339 return any_condition && any_conditional_validity;
4342 /* Does "graph" contain any coincidence edge?
4344 static int has_any_coincidence(struct isl_sched_graph *graph)
4346 int i;
4348 for (i = 0; i < graph->n_edge; ++i)
4349 if (is_coincidence(&graph->edge[i]))
4350 return 1;
4352 return 0;
4355 /* Extract the final schedule row as a map with the iteration domain
4356 * of "node" as domain.
4358 static __isl_give isl_map *final_row(struct isl_sched_node *node)
4360 isl_multi_aff *ma;
4361 int row;
4363 row = isl_mat_rows(node->sched) - 1;
4364 ma = node_extract_partial_schedule_multi_aff(node, row, 1);
4365 return isl_map_from_multi_aff(ma);
4368 /* Is the conditional validity dependence in the edge with index "edge_index"
4369 * violated by the latest (i.e., final) row of the schedule?
4370 * That is, is i scheduled after j
4371 * for any conditional validity dependence i -> j?
4373 static int is_violated(struct isl_sched_graph *graph, int edge_index)
4375 isl_map *src_sched, *dst_sched, *map;
4376 struct isl_sched_edge *edge = &graph->edge[edge_index];
4377 int empty;
4379 src_sched = final_row(edge->src);
4380 dst_sched = final_row(edge->dst);
4381 map = isl_map_copy(edge->map);
4382 map = isl_map_apply_domain(map, src_sched);
4383 map = isl_map_apply_range(map, dst_sched);
4384 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
4385 empty = isl_map_is_empty(map);
4386 isl_map_free(map);
4388 if (empty < 0)
4389 return -1;
4391 return !empty;
4394 /* Does "graph" have any satisfied condition edges that
4395 * are adjacent to the conditional validity constraint with
4396 * domain "conditional_source" and range "conditional_sink"?
4398 * A satisfied condition is one that is not local.
4399 * If a condition was forced to be local already (i.e., marked as local)
4400 * then there is no need to check if it is in fact local.
4402 * Additionally, mark all adjacent condition edges found as local.
4404 static int has_adjacent_true_conditions(struct isl_sched_graph *graph,
4405 __isl_keep isl_union_set *conditional_source,
4406 __isl_keep isl_union_set *conditional_sink)
4408 int i;
4409 int any = 0;
4411 for (i = 0; i < graph->n_edge; ++i) {
4412 int adjacent, local;
4413 isl_union_map *condition;
4415 if (!is_condition(&graph->edge[i]))
4416 continue;
4417 if (is_local(&graph->edge[i]))
4418 continue;
4420 condition = graph->edge[i].tagged_condition;
4421 adjacent = domain_intersects(condition, conditional_sink);
4422 if (adjacent >= 0 && !adjacent)
4423 adjacent = range_intersects(condition,
4424 conditional_source);
4425 if (adjacent < 0)
4426 return -1;
4427 if (!adjacent)
4428 continue;
4430 set_local(&graph->edge[i]);
4432 local = is_condition_false(&graph->edge[i]);
4433 if (local < 0)
4434 return -1;
4435 if (!local)
4436 any = 1;
4439 return any;
4442 /* Are there any violated conditional validity dependences with
4443 * adjacent condition dependences that are not local with respect
4444 * to the current schedule?
4445 * That is, is the conditional validity constraint violated?
4447 * Additionally, mark all those adjacent condition dependences as local.
4448 * We also mark those adjacent condition dependences that were not marked
4449 * as local before, but just happened to be local already. This ensures
4450 * that they remain local if the schedule is recomputed.
4452 * We first collect domain and range of all violated conditional validity
4453 * dependences and then check if there are any adjacent non-local
4454 * condition dependences.
4456 static int has_violated_conditional_constraint(isl_ctx *ctx,
4457 struct isl_sched_graph *graph)
4459 int i;
4460 int any = 0;
4461 isl_union_set *source, *sink;
4463 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
4464 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
4465 for (i = 0; i < graph->n_edge; ++i) {
4466 isl_union_set *uset;
4467 isl_union_map *umap;
4468 int violated;
4470 if (!is_conditional_validity(&graph->edge[i]))
4471 continue;
4473 violated = is_violated(graph, i);
4474 if (violated < 0)
4475 goto error;
4476 if (!violated)
4477 continue;
4479 any = 1;
4481 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
4482 uset = isl_union_map_domain(umap);
4483 source = isl_union_set_union(source, uset);
4484 source = isl_union_set_coalesce(source);
4486 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
4487 uset = isl_union_map_range(umap);
4488 sink = isl_union_set_union(sink, uset);
4489 sink = isl_union_set_coalesce(sink);
4492 if (any)
4493 any = has_adjacent_true_conditions(graph, source, sink);
4495 isl_union_set_free(source);
4496 isl_union_set_free(sink);
4497 return any;
4498 error:
4499 isl_union_set_free(source);
4500 isl_union_set_free(sink);
4501 return -1;
4504 /* Examine the current band (the rows between graph->band_start and
4505 * graph->n_total_row), deciding whether to drop it or add it to "node"
4506 * and then continue with the computation of the next band, if any.
4507 * If "initialized" is set, then it may be assumed that compute_maxvar
4508 * has been called on the current band. Otherwise, call
4509 * compute_maxvar if and before carry_dependences gets called.
4511 * The caller keeps looking for a new row as long as
4512 * graph->n_row < graph->maxvar. If the latest attempt to find
4513 * such a row failed (i.e., we still have graph->n_row < graph->maxvar),
4514 * then we either
4515 * - split between SCCs and start over (assuming we found an interesting
4516 * pair of SCCs between which to split)
4517 * - continue with the next band (assuming the current band has at least
4518 * one row)
4519 * - try to carry as many dependences as possible and continue with the next
4520 * band
4521 * In each case, we first insert a band node in the schedule tree
4522 * if any rows have been computed.
4524 * If the caller managed to complete the schedule, we insert a band node
4525 * (if any schedule rows were computed) and we finish off by topologically
4526 * sorting the statements based on the remaining dependences.
4528 static __isl_give isl_schedule_node *compute_schedule_finish_band(
4529 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
4530 int initialized)
4532 int insert;
4534 if (!node)
4535 return NULL;
4537 if (graph->n_row < graph->maxvar) {
4538 isl_ctx *ctx;
4539 int empty = graph->n_total_row == graph->band_start;
4541 ctx = isl_schedule_node_get_ctx(node);
4542 if (!ctx->opt->schedule_maximize_band_depth && !empty)
4543 return compute_next_band(node, graph, 1);
4544 if (graph->src_scc >= 0)
4545 return compute_split_schedule(node, graph);
4546 if (!empty)
4547 return compute_next_band(node, graph, 1);
4548 if (!initialized && compute_maxvar(graph) < 0)
4549 return isl_schedule_node_free(node);
4550 return carry_dependences(node, graph);
4553 insert = graph->n_total_row > graph->band_start;
4554 if (insert) {
4555 node = insert_current_band(node, graph, 1);
4556 node = isl_schedule_node_child(node, 0);
4558 node = sort_statements(node, graph, initialized);
4559 if (insert)
4560 node = isl_schedule_node_parent(node);
4562 return node;
4565 /* Construct a band of schedule rows for a connected dependence graph.
4566 * The caller is responsible for determining the strongly connected
4567 * components and calling compute_maxvar first.
4569 * We try to find a sequence of as many schedule rows as possible that result
4570 * in non-negative dependence distances (independent of the previous rows
4571 * in the sequence, i.e., such that the sequence is tilable), with as
4572 * many of the initial rows as possible satisfying the coincidence constraints.
4573 * The computation stops if we can't find any more rows or if we have found
4574 * all the rows we wanted to find.
4576 * If ctx->opt->schedule_outer_coincidence is set, then we force the
4577 * outermost dimension to satisfy the coincidence constraints. If this
4578 * turns out to be impossible, we fall back on the general scheme above
4579 * and try to carry as many dependences as possible.
4581 * If "graph" contains both condition and conditional validity dependences,
4582 * then we need to check that that the conditional schedule constraint
4583 * is satisfied, i.e., there are no violated conditional validity dependences
4584 * that are adjacent to any non-local condition dependences.
4585 * If there are, then we mark all those adjacent condition dependences
4586 * as local and recompute the current band. Those dependences that
4587 * are marked local will then be forced to be local.
4588 * The initial computation is performed with no dependences marked as local.
4589 * If we are lucky, then there will be no violated conditional validity
4590 * dependences adjacent to any non-local condition dependences.
4591 * Otherwise, we mark some additional condition dependences as local and
4592 * recompute. We continue this process until there are no violations left or
4593 * until we are no longer able to compute a schedule.
4594 * Since there are only a finite number of dependences,
4595 * there will only be a finite number of iterations.
4597 static isl_stat compute_schedule_wcc_band(isl_ctx *ctx,
4598 struct isl_sched_graph *graph)
4600 int has_coincidence;
4601 int use_coincidence;
4602 int force_coincidence = 0;
4603 int check_conditional;
4605 if (sort_sccs(graph) < 0)
4606 return isl_stat_error;
4608 clear_local_edges(graph);
4609 check_conditional = need_condition_check(graph);
4610 has_coincidence = has_any_coincidence(graph);
4612 if (ctx->opt->schedule_outer_coincidence)
4613 force_coincidence = 1;
4615 use_coincidence = has_coincidence;
4616 while (graph->n_row < graph->maxvar) {
4617 isl_vec *sol;
4618 int violated;
4619 int coincident;
4621 graph->src_scc = -1;
4622 graph->dst_scc = -1;
4624 if (setup_lp(ctx, graph, use_coincidence) < 0)
4625 return isl_stat_error;
4626 sol = solve_lp(graph);
4627 if (!sol)
4628 return isl_stat_error;
4629 if (sol->size == 0) {
4630 int empty = graph->n_total_row == graph->band_start;
4632 isl_vec_free(sol);
4633 if (use_coincidence && (!force_coincidence || !empty)) {
4634 use_coincidence = 0;
4635 continue;
4637 return isl_stat_ok;
4639 coincident = !has_coincidence || use_coincidence;
4640 if (update_schedule(graph, sol, 1, coincident) < 0)
4641 return isl_stat_error;
4643 if (!check_conditional)
4644 continue;
4645 violated = has_violated_conditional_constraint(ctx, graph);
4646 if (violated < 0)
4647 return isl_stat_error;
4648 if (!violated)
4649 continue;
4650 if (reset_band(graph) < 0)
4651 return isl_stat_error;
4652 use_coincidence = has_coincidence;
4655 return isl_stat_ok;
4658 /* Compute a schedule for a connected dependence graph by considering
4659 * the graph as a whole and return the updated schedule node.
4661 * The actual schedule rows of the current band are computed by
4662 * compute_schedule_wcc_band. compute_schedule_finish_band takes
4663 * care of integrating the band into "node" and continuing
4664 * the computation.
4666 static __isl_give isl_schedule_node *compute_schedule_wcc_whole(
4667 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
4669 isl_ctx *ctx;
4671 if (!node)
4672 return NULL;
4674 ctx = isl_schedule_node_get_ctx(node);
4675 if (compute_schedule_wcc_band(ctx, graph) < 0)
4676 return isl_schedule_node_free(node);
4678 return compute_schedule_finish_band(node, graph, 1);
4681 /* Clustering information used by compute_schedule_wcc_clustering.
4683 * "n" is the number of SCCs in the original dependence graph
4684 * "scc" is an array of "n" elements, each representing an SCC
4685 * of the original dependence graph. All entries in the same cluster
4686 * have the same number of schedule rows.
4687 * "scc_cluster" maps each SCC index to the cluster to which it belongs,
4688 * where each cluster is represented by the index of the first SCC
4689 * in the cluster. Initially, each SCC belongs to a cluster containing
4690 * only that SCC.
4692 * "scc_in_merge" is used by merge_clusters_along_edge to keep
4693 * track of which SCCs need to be merged.
4695 * "cluster" contains the merged clusters of SCCs after the clustering
4696 * has completed.
4698 * "scc_node" is a temporary data structure used inside copy_partial.
4699 * For each SCC, it keeps track of the number of nodes in the SCC
4700 * that have already been copied.
4702 struct isl_clustering {
4703 int n;
4704 struct isl_sched_graph *scc;
4705 struct isl_sched_graph *cluster;
4706 int *scc_cluster;
4707 int *scc_node;
4708 int *scc_in_merge;
4711 /* Initialize the clustering data structure "c" from "graph".
4713 * In particular, allocate memory, extract the SCCs from "graph"
4714 * into c->scc, initialize scc_cluster and construct
4715 * a band of schedule rows for each SCC.
4716 * Within each SCC, there is only one SCC by definition.
4717 * Each SCC initially belongs to a cluster containing only that SCC.
4719 static isl_stat clustering_init(isl_ctx *ctx, struct isl_clustering *c,
4720 struct isl_sched_graph *graph)
4722 int i;
4724 c->n = graph->scc;
4725 c->scc = isl_calloc_array(ctx, struct isl_sched_graph, c->n);
4726 c->cluster = isl_calloc_array(ctx, struct isl_sched_graph, c->n);
4727 c->scc_cluster = isl_calloc_array(ctx, int, c->n);
4728 c->scc_node = isl_calloc_array(ctx, int, c->n);
4729 c->scc_in_merge = isl_calloc_array(ctx, int, c->n);
4730 if (!c->scc || !c->cluster ||
4731 !c->scc_cluster || !c->scc_node || !c->scc_in_merge)
4732 return isl_stat_error;
4734 for (i = 0; i < c->n; ++i) {
4735 if (extract_sub_graph(ctx, graph, &node_scc_exactly,
4736 &edge_scc_exactly, i, &c->scc[i]) < 0)
4737 return isl_stat_error;
4738 c->scc[i].scc = 1;
4739 if (compute_maxvar(&c->scc[i]) < 0)
4740 return isl_stat_error;
4741 if (compute_schedule_wcc_band(ctx, &c->scc[i]) < 0)
4742 return isl_stat_error;
4743 c->scc_cluster[i] = i;
4746 return isl_stat_ok;
4749 /* Free all memory allocated for "c".
4751 static void clustering_free(isl_ctx *ctx, struct isl_clustering *c)
4753 int i;
4755 if (c->scc)
4756 for (i = 0; i < c->n; ++i)
4757 graph_free(ctx, &c->scc[i]);
4758 free(c->scc);
4759 if (c->cluster)
4760 for (i = 0; i < c->n; ++i)
4761 graph_free(ctx, &c->cluster[i]);
4762 free(c->cluster);
4763 free(c->scc_cluster);
4764 free(c->scc_node);
4765 free(c->scc_in_merge);
4768 /* Should we refrain from merging the cluster in "graph" with
4769 * any other cluster?
4770 * In particular, is its current schedule band empty and incomplete.
4772 static int bad_cluster(struct isl_sched_graph *graph)
4774 return graph->n_row < graph->maxvar &&
4775 graph->n_total_row == graph->band_start;
4778 /* Return the index of an edge in "graph" that can be used to merge
4779 * two clusters in "c".
4780 * Return graph->n_edge if no such edge can be found.
4781 * Return -1 on error.
4783 * In particular, return a proximity edge between two clusters
4784 * that is not marked "no_merge" and such that neither of the
4785 * two clusters has an incomplete, empty band.
4787 * If there are multiple such edges, then try and find the most
4788 * appropriate edge to use for merging. In particular, pick the edge
4789 * with the greatest weight. If there are multiple of those,
4790 * then pick one with the shortest distance between
4791 * the two cluster representatives.
4793 static int find_proximity(struct isl_sched_graph *graph,
4794 struct isl_clustering *c)
4796 int i, best = graph->n_edge, best_dist, best_weight;
4798 for (i = 0; i < graph->n_edge; ++i) {
4799 struct isl_sched_edge *edge = &graph->edge[i];
4800 int dist, weight;
4802 if (!is_proximity(edge))
4803 continue;
4804 if (edge->no_merge)
4805 continue;
4806 if (bad_cluster(&c->scc[edge->src->scc]) ||
4807 bad_cluster(&c->scc[edge->dst->scc]))
4808 continue;
4809 dist = c->scc_cluster[edge->dst->scc] -
4810 c->scc_cluster[edge->src->scc];
4811 if (dist == 0)
4812 continue;
4813 weight = edge->weight;
4814 if (best < graph->n_edge) {
4815 if (best_weight > weight)
4816 continue;
4817 if (best_weight == weight && best_dist <= dist)
4818 continue;
4820 best = i;
4821 best_dist = dist;
4822 best_weight = weight;
4825 return best;
4828 /* Internal data structure used in mark_merge_sccs.
4830 * "graph" is the dependence graph in which a strongly connected
4831 * component is constructed.
4832 * "scc_cluster" maps each SCC index to the cluster to which it belongs.
4833 * "src" and "dst" are the indices of the nodes that are being merged.
4835 struct isl_mark_merge_sccs_data {
4836 struct isl_sched_graph *graph;
4837 int *scc_cluster;
4838 int src;
4839 int dst;
4842 /* Check whether the cluster containing node "i" depends on the cluster
4843 * containing node "j". If "i" and "j" belong to the same cluster,
4844 * then they are taken to depend on each other to ensure that
4845 * the resulting strongly connected component consists of complete
4846 * clusters. Furthermore, if "i" and "j" are the two nodes that
4847 * are being merged, then they are taken to depend on each other as well.
4848 * Otherwise, check if there is a (conditional) validity dependence
4849 * from node[j] to node[i], forcing node[i] to follow node[j].
4851 static isl_bool cluster_follows(int i, int j, void *user)
4853 struct isl_mark_merge_sccs_data *data = user;
4854 struct isl_sched_graph *graph = data->graph;
4855 int *scc_cluster = data->scc_cluster;
4857 if (data->src == i && data->dst == j)
4858 return isl_bool_true;
4859 if (data->src == j && data->dst == i)
4860 return isl_bool_true;
4861 if (scc_cluster[graph->node[i].scc] == scc_cluster[graph->node[j].scc])
4862 return isl_bool_true;
4864 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
4867 /* Mark all SCCs that belong to either of the two clusters in "c"
4868 * connected by the edge in "graph" with index "edge", or to any
4869 * of the intermediate clusters.
4870 * The marking is recorded in c->scc_in_merge.
4872 * The given edge has been selected for merging two clusters,
4873 * meaning that there is at least a proximity edge between the two nodes.
4874 * However, there may also be (indirect) validity dependences
4875 * between the two nodes. When merging the two clusters, all clusters
4876 * containing one or more of the intermediate nodes along the
4877 * indirect validity dependences need to be merged in as well.
4879 * First collect all such nodes by computing the strongly connected
4880 * component (SCC) containing the two nodes connected by the edge, where
4881 * the two nodes are considered to depend on each other to make
4882 * sure they end up in the same SCC. Similarly, each node is considered
4883 * to depend on every other node in the same cluster to ensure
4884 * that the SCC consists of complete clusters.
4886 * Then the original SCCs that contain any of these nodes are marked
4887 * in c->scc_in_merge.
4889 static isl_stat mark_merge_sccs(isl_ctx *ctx, struct isl_sched_graph *graph,
4890 int edge, struct isl_clustering *c)
4892 struct isl_mark_merge_sccs_data data;
4893 struct isl_tarjan_graph *g;
4894 int i;
4896 for (i = 0; i < c->n; ++i)
4897 c->scc_in_merge[i] = 0;
4899 data.graph = graph;
4900 data.scc_cluster = c->scc_cluster;
4901 data.src = graph->edge[edge].src - graph->node;
4902 data.dst = graph->edge[edge].dst - graph->node;
4904 g = isl_tarjan_graph_component(ctx, graph->n, data.dst,
4905 &cluster_follows, &data);
4906 if (!g)
4907 goto error;
4909 i = g->op;
4910 if (i < 3)
4911 isl_die(ctx, isl_error_internal,
4912 "expecting at least two nodes in component",
4913 goto error);
4914 if (g->order[--i] != -1)
4915 isl_die(ctx, isl_error_internal,
4916 "expecting end of component marker", goto error);
4918 for (--i; i >= 0 && g->order[i] != -1; --i) {
4919 int scc = graph->node[g->order[i]].scc;
4920 c->scc_in_merge[scc] = 1;
4923 isl_tarjan_graph_free(g);
4924 return isl_stat_ok;
4925 error:
4926 isl_tarjan_graph_free(g);
4927 return isl_stat_error;
4930 /* Construct the identifier "cluster_i".
4932 static __isl_give isl_id *cluster_id(isl_ctx *ctx, int i)
4934 char name[40];
4936 snprintf(name, sizeof(name), "cluster_%d", i);
4937 return isl_id_alloc(ctx, name, NULL);
4940 /* Construct the space of the cluster with index "i" containing
4941 * the strongly connected component "scc".
4943 * In particular, construct a space called cluster_i with dimension equal
4944 * to the number of schedule rows in the current band of "scc".
4946 static __isl_give isl_space *cluster_space(struct isl_sched_graph *scc, int i)
4948 int nvar;
4949 isl_space *space;
4950 isl_id *id;
4952 nvar = scc->n_total_row - scc->band_start;
4953 space = isl_space_copy(scc->node[0].space);
4954 space = isl_space_params(space);
4955 space = isl_space_set_from_params(space);
4956 space = isl_space_add_dims(space, isl_dim_set, nvar);
4957 id = cluster_id(isl_space_get_ctx(space), i);
4958 space = isl_space_set_tuple_id(space, isl_dim_set, id);
4960 return space;
4963 /* Collect the domain of the graph for merging clusters.
4965 * In particular, for each cluster with first SCC "i", construct
4966 * a set in the space called cluster_i with dimension equal
4967 * to the number of schedule rows in the current band of the cluster.
4969 static __isl_give isl_union_set *collect_domain(isl_ctx *ctx,
4970 struct isl_sched_graph *graph, struct isl_clustering *c)
4972 int i;
4973 isl_space *space;
4974 isl_union_set *domain;
4976 space = isl_space_params_alloc(ctx, 0);
4977 domain = isl_union_set_empty(space);
4979 for (i = 0; i < graph->scc; ++i) {
4980 isl_space *space;
4982 if (!c->scc_in_merge[i])
4983 continue;
4984 if (c->scc_cluster[i] != i)
4985 continue;
4986 space = cluster_space(&c->scc[i], i);
4987 domain = isl_union_set_add_set(domain, isl_set_universe(space));
4990 return domain;
4993 /* Construct a map from the original instances to the corresponding
4994 * cluster instance in the current bands of the clusters in "c".
4996 static __isl_give isl_union_map *collect_cluster_map(isl_ctx *ctx,
4997 struct isl_sched_graph *graph, struct isl_clustering *c)
4999 int i, j;
5000 isl_space *space;
5001 isl_union_map *cluster_map;
5003 space = isl_space_params_alloc(ctx, 0);
5004 cluster_map = isl_union_map_empty(space);
5005 for (i = 0; i < graph->scc; ++i) {
5006 int start, n;
5007 isl_id *id;
5009 if (!c->scc_in_merge[i])
5010 continue;
5012 id = cluster_id(ctx, c->scc_cluster[i]);
5013 start = c->scc[i].band_start;
5014 n = c->scc[i].n_total_row - start;
5015 for (j = 0; j < c->scc[i].n; ++j) {
5016 isl_multi_aff *ma;
5017 isl_map *map;
5018 struct isl_sched_node *node = &c->scc[i].node[j];
5020 ma = node_extract_partial_schedule_multi_aff(node,
5021 start, n);
5022 ma = isl_multi_aff_set_tuple_id(ma, isl_dim_out,
5023 isl_id_copy(id));
5024 map = isl_map_from_multi_aff(ma);
5025 cluster_map = isl_union_map_add_map(cluster_map, map);
5027 isl_id_free(id);
5030 return cluster_map;
5033 /* Add "umap" to the schedule constraints "sc" of all types of "edge"
5034 * that are not isl_edge_condition or isl_edge_conditional_validity.
5036 static __isl_give isl_schedule_constraints *add_non_conditional_constraints(
5037 struct isl_sched_edge *edge, __isl_keep isl_union_map *umap,
5038 __isl_take isl_schedule_constraints *sc)
5040 enum isl_edge_type t;
5042 if (!sc)
5043 return NULL;
5045 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
5046 if (t == isl_edge_condition ||
5047 t == isl_edge_conditional_validity)
5048 continue;
5049 if (!is_type(edge, t))
5050 continue;
5051 sc = isl_schedule_constraints_add(sc, t,
5052 isl_union_map_copy(umap));
5055 return sc;
5058 /* Add schedule constraints of types isl_edge_condition and
5059 * isl_edge_conditional_validity to "sc" by applying "umap" to
5060 * the domains of the wrapped relations in domain and range
5061 * of the corresponding tagged constraints of "edge".
5063 static __isl_give isl_schedule_constraints *add_conditional_constraints(
5064 struct isl_sched_edge *edge, __isl_keep isl_union_map *umap,
5065 __isl_take isl_schedule_constraints *sc)
5067 enum isl_edge_type t;
5068 isl_union_map *tagged;
5070 for (t = isl_edge_condition; t <= isl_edge_conditional_validity; ++t) {
5071 if (!is_type(edge, t))
5072 continue;
5073 if (t == isl_edge_condition)
5074 tagged = isl_union_map_copy(edge->tagged_condition);
5075 else
5076 tagged = isl_union_map_copy(edge->tagged_validity);
5077 tagged = isl_union_map_zip(tagged);
5078 tagged = isl_union_map_apply_domain(tagged,
5079 isl_union_map_copy(umap));
5080 tagged = isl_union_map_zip(tagged);
5081 sc = isl_schedule_constraints_add(sc, t, tagged);
5082 if (!sc)
5083 return NULL;
5086 return sc;
5089 /* Given a mapping "cluster_map" from the original instances to
5090 * the cluster instances, add schedule constraints on the clusters
5091 * to "sc" corresponding to the original constraints represented by "edge".
5093 * For non-tagged dependence constraints, the cluster constraints
5094 * are obtained by applying "cluster_map" to the edge->map.
5096 * For tagged dependence constraints, "cluster_map" needs to be applied
5097 * to the domains of the wrapped relations in domain and range
5098 * of the tagged dependence constraints. Pick out the mappings
5099 * from these domains from "cluster_map" and construct their product.
5100 * This mapping can then be applied to the pair of domains.
5102 static __isl_give isl_schedule_constraints *collect_edge_constraints(
5103 struct isl_sched_edge *edge, __isl_keep isl_union_map *cluster_map,
5104 __isl_take isl_schedule_constraints *sc)
5106 isl_union_map *umap;
5107 isl_space *space;
5108 isl_union_set *uset;
5109 isl_union_map *umap1, *umap2;
5111 if (!sc)
5112 return NULL;
5114 umap = isl_union_map_from_map(isl_map_copy(edge->map));
5115 umap = isl_union_map_apply_domain(umap,
5116 isl_union_map_copy(cluster_map));
5117 umap = isl_union_map_apply_range(umap,
5118 isl_union_map_copy(cluster_map));
5119 sc = add_non_conditional_constraints(edge, umap, sc);
5120 isl_union_map_free(umap);
5122 if (!sc || (!is_condition(edge) && !is_conditional_validity(edge)))
5123 return sc;
5125 space = isl_space_domain(isl_map_get_space(edge->map));
5126 uset = isl_union_set_from_set(isl_set_universe(space));
5127 umap1 = isl_union_map_copy(cluster_map);
5128 umap1 = isl_union_map_intersect_domain(umap1, uset);
5129 space = isl_space_range(isl_map_get_space(edge->map));
5130 uset = isl_union_set_from_set(isl_set_universe(space));
5131 umap2 = isl_union_map_copy(cluster_map);
5132 umap2 = isl_union_map_intersect_domain(umap2, uset);
5133 umap = isl_union_map_product(umap1, umap2);
5135 sc = add_conditional_constraints(edge, umap, sc);
5137 isl_union_map_free(umap);
5138 return sc;
5141 /* Given a mapping "cluster_map" from the original instances to
5142 * the cluster instances, add schedule constraints on the clusters
5143 * to "sc" corresponding to all edges in "graph" between nodes that
5144 * belong to SCCs that are marked for merging in "scc_in_merge".
5146 static __isl_give isl_schedule_constraints *collect_constraints(
5147 struct isl_sched_graph *graph, int *scc_in_merge,
5148 __isl_keep isl_union_map *cluster_map,
5149 __isl_take isl_schedule_constraints *sc)
5151 int i;
5153 for (i = 0; i < graph->n_edge; ++i) {
5154 struct isl_sched_edge *edge = &graph->edge[i];
5156 if (!scc_in_merge[edge->src->scc])
5157 continue;
5158 if (!scc_in_merge[edge->dst->scc])
5159 continue;
5160 sc = collect_edge_constraints(edge, cluster_map, sc);
5163 return sc;
5166 /* Construct a dependence graph for scheduling clusters with respect
5167 * to each other and store the result in "merge_graph".
5168 * In particular, the nodes of the graph correspond to the schedule
5169 * dimensions of the current bands of those clusters that have been
5170 * marked for merging in "c".
5172 * First construct an isl_schedule_constraints object for this domain
5173 * by transforming the edges in "graph" to the domain.
5174 * Then initialize a dependence graph for scheduling from these
5175 * constraints.
5177 static isl_stat init_merge_graph(isl_ctx *ctx, struct isl_sched_graph *graph,
5178 struct isl_clustering *c, struct isl_sched_graph *merge_graph)
5180 isl_union_set *domain;
5181 isl_union_map *cluster_map;
5182 isl_schedule_constraints *sc;
5183 isl_stat r;
5185 domain = collect_domain(ctx, graph, c);
5186 sc = isl_schedule_constraints_on_domain(domain);
5187 if (!sc)
5188 return isl_stat_error;
5189 cluster_map = collect_cluster_map(ctx, graph, c);
5190 sc = collect_constraints(graph, c->scc_in_merge, cluster_map, sc);
5191 isl_union_map_free(cluster_map);
5193 r = graph_init(merge_graph, sc);
5195 isl_schedule_constraints_free(sc);
5197 return r;
5200 /* Compute the maximal number of remaining schedule rows that still need
5201 * to be computed for the nodes that belong to clusters with the maximal
5202 * dimension for the current band (i.e., the band that is to be merged).
5203 * Only clusters that are about to be merged are considered.
5204 * "maxvar" is the maximal dimension for the current band.
5205 * "c" contains information about the clusters.
5207 * Return the maximal number of remaining schedule rows or -1 on error.
5209 static int compute_maxvar_max_slack(int maxvar, struct isl_clustering *c)
5211 int i, j;
5212 int max_slack;
5214 max_slack = 0;
5215 for (i = 0; i < c->n; ++i) {
5216 int nvar;
5217 struct isl_sched_graph *scc;
5219 if (!c->scc_in_merge[i])
5220 continue;
5221 scc = &c->scc[i];
5222 nvar = scc->n_total_row - scc->band_start;
5223 if (nvar != maxvar)
5224 continue;
5225 for (j = 0; j < scc->n; ++j) {
5226 struct isl_sched_node *node = &scc->node[j];
5227 int slack;
5229 if (node_update_cmap(node) < 0)
5230 return -1;
5231 slack = node->nvar - node->rank;
5232 if (slack > max_slack)
5233 max_slack = slack;
5237 return max_slack;
5240 /* If there are any clusters where the dimension of the current band
5241 * (i.e., the band that is to be merged) is smaller than "maxvar" and
5242 * if there are any nodes in such a cluster where the number
5243 * of remaining schedule rows that still need to be computed
5244 * is greater than "max_slack", then return the smallest current band
5245 * dimension of all these clusters. Otherwise return the original value
5246 * of "maxvar". Return -1 in case of any error.
5247 * Only clusters that are about to be merged are considered.
5248 * "c" contains information about the clusters.
5250 static int limit_maxvar_to_slack(int maxvar, int max_slack,
5251 struct isl_clustering *c)
5253 int i, j;
5255 for (i = 0; i < c->n; ++i) {
5256 int nvar;
5257 struct isl_sched_graph *scc;
5259 if (!c->scc_in_merge[i])
5260 continue;
5261 scc = &c->scc[i];
5262 nvar = scc->n_total_row - scc->band_start;
5263 if (nvar >= maxvar)
5264 continue;
5265 for (j = 0; j < scc->n; ++j) {
5266 struct isl_sched_node *node = &scc->node[j];
5267 int slack;
5269 if (node_update_cmap(node) < 0)
5270 return -1;
5271 slack = node->nvar - node->rank;
5272 if (slack > max_slack) {
5273 maxvar = nvar;
5274 break;
5279 return maxvar;
5282 /* Adjust merge_graph->maxvar based on the number of remaining schedule rows
5283 * that still need to be computed. In particular, if there is a node
5284 * in a cluster where the dimension of the current band is smaller
5285 * than merge_graph->maxvar, but the number of remaining schedule rows
5286 * is greater than that of any node in a cluster with the maximal
5287 * dimension for the current band (i.e., merge_graph->maxvar),
5288 * then adjust merge_graph->maxvar to the (smallest) current band dimension
5289 * of those clusters. Without this adjustment, the total number of
5290 * schedule dimensions would be increased, resulting in a skewed view
5291 * of the number of coincident dimensions.
5292 * "c" contains information about the clusters.
5294 * If the maximize_band_depth option is set and merge_graph->maxvar is reduced,
5295 * then there is no point in attempting any merge since it will be rejected
5296 * anyway. Set merge_graph->maxvar to zero in such cases.
5298 static isl_stat adjust_maxvar_to_slack(isl_ctx *ctx,
5299 struct isl_sched_graph *merge_graph, struct isl_clustering *c)
5301 int max_slack, maxvar;
5303 max_slack = compute_maxvar_max_slack(merge_graph->maxvar, c);
5304 if (max_slack < 0)
5305 return isl_stat_error;
5306 maxvar = limit_maxvar_to_slack(merge_graph->maxvar, max_slack, c);
5307 if (maxvar < 0)
5308 return isl_stat_error;
5310 if (maxvar < merge_graph->maxvar) {
5311 if (isl_options_get_schedule_maximize_band_depth(ctx))
5312 merge_graph->maxvar = 0;
5313 else
5314 merge_graph->maxvar = maxvar;
5317 return isl_stat_ok;
5320 /* Return the number of coincident dimensions in the current band of "graph",
5321 * where the nodes of "graph" are assumed to be scheduled by a single band.
5323 static int get_n_coincident(struct isl_sched_graph *graph)
5325 int i;
5327 for (i = graph->band_start; i < graph->n_total_row; ++i)
5328 if (!graph->node[0].coincident[i])
5329 break;
5331 return i - graph->band_start;
5334 /* Should the clusters be merged based on the cluster schedule
5335 * in the current (and only) band of "merge_graph", given that
5336 * coincidence should be maximized?
5338 * If the number of coincident schedule dimensions in the merged band
5339 * would be less than the maximal number of coincident schedule dimensions
5340 * in any of the merged clusters, then the clusters should not be merged.
5342 static isl_bool ok_to_merge_coincident(struct isl_clustering *c,
5343 struct isl_sched_graph *merge_graph)
5345 int i;
5346 int n_coincident;
5347 int max_coincident;
5349 max_coincident = 0;
5350 for (i = 0; i < c->n; ++i) {
5351 if (!c->scc_in_merge[i])
5352 continue;
5353 n_coincident = get_n_coincident(&c->scc[i]);
5354 if (n_coincident > max_coincident)
5355 max_coincident = n_coincident;
5358 n_coincident = get_n_coincident(merge_graph);
5360 return n_coincident >= max_coincident;
5363 /* Return the transformation on "node" expressed by the current (and only)
5364 * band of "merge_graph" applied to the clusters in "c".
5366 * First find the representation of "node" in its SCC in "c" and
5367 * extract the transformation expressed by the current band.
5368 * Then extract the transformation applied by "merge_graph"
5369 * to the cluster to which this SCC belongs.
5370 * Combine the two to obtain the complete transformation on the node.
5372 * Note that the range of the first transformation is an anonymous space,
5373 * while the domain of the second is named "cluster_X". The range
5374 * of the former therefore needs to be adjusted before the two
5375 * can be combined.
5377 static __isl_give isl_map *extract_node_transformation(isl_ctx *ctx,
5378 struct isl_sched_node *node, struct isl_clustering *c,
5379 struct isl_sched_graph *merge_graph)
5381 struct isl_sched_node *scc_node, *cluster_node;
5382 int start, n;
5383 isl_id *id;
5384 isl_space *space;
5385 isl_multi_aff *ma, *ma2;
5387 scc_node = graph_find_node(ctx, &c->scc[node->scc], node->space);
5388 start = c->scc[node->scc].band_start;
5389 n = c->scc[node->scc].n_total_row - start;
5390 ma = node_extract_partial_schedule_multi_aff(scc_node, start, n);
5391 space = cluster_space(&c->scc[node->scc], c->scc_cluster[node->scc]);
5392 cluster_node = graph_find_node(ctx, merge_graph, space);
5393 if (space && !cluster_node)
5394 isl_die(ctx, isl_error_internal, "unable to find cluster",
5395 space = isl_space_free(space));
5396 id = isl_space_get_tuple_id(space, isl_dim_set);
5397 ma = isl_multi_aff_set_tuple_id(ma, isl_dim_out, id);
5398 isl_space_free(space);
5399 n = merge_graph->n_total_row;
5400 ma2 = node_extract_partial_schedule_multi_aff(cluster_node, 0, n);
5401 ma = isl_multi_aff_pullback_multi_aff(ma2, ma);
5403 return isl_map_from_multi_aff(ma);
5406 /* Give a set of distances "set", are they bounded by a small constant
5407 * in direction "pos"?
5408 * In practice, check if they are bounded by 2 by checking that there
5409 * are no elements with a value greater than or equal to 3 or
5410 * smaller than or equal to -3.
5412 static isl_bool distance_is_bounded(__isl_keep isl_set *set, int pos)
5414 isl_bool bounded;
5415 isl_set *test;
5417 if (!set)
5418 return isl_bool_error;
5420 test = isl_set_copy(set);
5421 test = isl_set_lower_bound_si(test, isl_dim_set, pos, 3);
5422 bounded = isl_set_is_empty(test);
5423 isl_set_free(test);
5425 if (bounded < 0 || !bounded)
5426 return bounded;
5428 test = isl_set_copy(set);
5429 test = isl_set_upper_bound_si(test, isl_dim_set, pos, -3);
5430 bounded = isl_set_is_empty(test);
5431 isl_set_free(test);
5433 return bounded;
5436 /* Does the set "set" have a fixed (but possible parametric) value
5437 * at dimension "pos"?
5439 static isl_bool has_single_value(__isl_keep isl_set *set, int pos)
5441 int n;
5442 isl_bool single;
5444 if (!set)
5445 return isl_bool_error;
5446 set = isl_set_copy(set);
5447 n = isl_set_dim(set, isl_dim_set);
5448 set = isl_set_project_out(set, isl_dim_set, pos + 1, n - (pos + 1));
5449 set = isl_set_project_out(set, isl_dim_set, 0, pos);
5450 single = isl_set_is_singleton(set);
5451 isl_set_free(set);
5453 return single;
5456 /* Does "map" have a fixed (but possible parametric) value
5457 * at dimension "pos" of either its domain or its range?
5459 static isl_bool has_singular_src_or_dst(__isl_keep isl_map *map, int pos)
5461 isl_set *set;
5462 isl_bool single;
5464 set = isl_map_domain(isl_map_copy(map));
5465 single = has_single_value(set, pos);
5466 isl_set_free(set);
5468 if (single < 0 || single)
5469 return single;
5471 set = isl_map_range(isl_map_copy(map));
5472 single = has_single_value(set, pos);
5473 isl_set_free(set);
5475 return single;
5478 /* Does the edge "edge" from "graph" have bounded dependence distances
5479 * in the merged graph "merge_graph" of a selection of clusters in "c"?
5481 * Extract the complete transformations of the source and destination
5482 * nodes of the edge, apply them to the edge constraints and
5483 * compute the differences. Finally, check if these differences are bounded
5484 * in each direction.
5486 * If the dimension of the band is greater than the number of
5487 * dimensions that can be expected to be optimized by the edge
5488 * (based on its weight), then also allow the differences to be unbounded
5489 * in the remaining dimensions, but only if either the source or
5490 * the destination has a fixed value in that direction.
5491 * This allows a statement that produces values that are used by
5492 * several instances of another statement to be merged with that
5493 * other statement.
5494 * However, merging such clusters will introduce an inherently
5495 * large proximity distance inside the merged cluster, meaning
5496 * that proximity distances will no longer be optimized in
5497 * subsequent merges. These merges are therefore only allowed
5498 * after all other possible merges have been tried.
5499 * The first time such a merge is encountered, the weight of the edge
5500 * is replaced by a negative weight. The second time (i.e., after
5501 * all merges over edges with a non-negative weight have been tried),
5502 * the merge is allowed.
5504 static isl_bool has_bounded_distances(isl_ctx *ctx, struct isl_sched_edge *edge,
5505 struct isl_sched_graph *graph, struct isl_clustering *c,
5506 struct isl_sched_graph *merge_graph)
5508 int i, n, n_slack;
5509 isl_bool bounded;
5510 isl_map *map, *t;
5511 isl_set *dist;
5513 map = isl_map_copy(edge->map);
5514 t = extract_node_transformation(ctx, edge->src, c, merge_graph);
5515 map = isl_map_apply_domain(map, t);
5516 t = extract_node_transformation(ctx, edge->dst, c, merge_graph);
5517 map = isl_map_apply_range(map, t);
5518 dist = isl_map_deltas(isl_map_copy(map));
5520 bounded = isl_bool_true;
5521 n = isl_set_dim(dist, isl_dim_set);
5522 n_slack = n - edge->weight;
5523 if (edge->weight < 0)
5524 n_slack -= graph->max_weight + 1;
5525 for (i = 0; i < n; ++i) {
5526 isl_bool bounded_i, singular_i;
5528 bounded_i = distance_is_bounded(dist, i);
5529 if (bounded_i < 0)
5530 goto error;
5531 if (bounded_i)
5532 continue;
5533 if (edge->weight >= 0)
5534 bounded = isl_bool_false;
5535 n_slack--;
5536 if (n_slack < 0)
5537 break;
5538 singular_i = has_singular_src_or_dst(map, i);
5539 if (singular_i < 0)
5540 goto error;
5541 if (singular_i)
5542 continue;
5543 bounded = isl_bool_false;
5544 break;
5546 if (!bounded && i >= n && edge->weight >= 0)
5547 edge->weight -= graph->max_weight + 1;
5548 isl_map_free(map);
5549 isl_set_free(dist);
5551 return bounded;
5552 error:
5553 isl_map_free(map);
5554 isl_set_free(dist);
5555 return isl_bool_error;
5558 /* Should the clusters be merged based on the cluster schedule
5559 * in the current (and only) band of "merge_graph"?
5560 * "graph" is the original dependence graph, while "c" records
5561 * which SCCs are involved in the latest merge.
5563 * In particular, is there at least one proximity constraint
5564 * that is optimized by the merge?
5566 * A proximity constraint is considered to be optimized
5567 * if the dependence distances are small.
5569 static isl_bool ok_to_merge_proximity(isl_ctx *ctx,
5570 struct isl_sched_graph *graph, struct isl_clustering *c,
5571 struct isl_sched_graph *merge_graph)
5573 int i;
5575 for (i = 0; i < graph->n_edge; ++i) {
5576 struct isl_sched_edge *edge = &graph->edge[i];
5577 isl_bool bounded;
5579 if (!is_proximity(edge))
5580 continue;
5581 if (!c->scc_in_merge[edge->src->scc])
5582 continue;
5583 if (!c->scc_in_merge[edge->dst->scc])
5584 continue;
5585 if (c->scc_cluster[edge->dst->scc] ==
5586 c->scc_cluster[edge->src->scc])
5587 continue;
5588 bounded = has_bounded_distances(ctx, edge, graph, c,
5589 merge_graph);
5590 if (bounded < 0 || bounded)
5591 return bounded;
5594 return isl_bool_false;
5597 /* Should the clusters be merged based on the cluster schedule
5598 * in the current (and only) band of "merge_graph"?
5599 * "graph" is the original dependence graph, while "c" records
5600 * which SCCs are involved in the latest merge.
5602 * If the current band is empty, then the clusters should not be merged.
5604 * If the band depth should be maximized and the merge schedule
5605 * is incomplete (meaning that the dimension of some of the schedule
5606 * bands in the original schedule will be reduced), then the clusters
5607 * should not be merged.
5609 * If the schedule_maximize_coincidence option is set, then check that
5610 * the number of coincident schedule dimensions is not reduced.
5612 * Finally, only allow the merge if at least one proximity
5613 * constraint is optimized.
5615 static isl_bool ok_to_merge(isl_ctx *ctx, struct isl_sched_graph *graph,
5616 struct isl_clustering *c, struct isl_sched_graph *merge_graph)
5618 if (merge_graph->n_total_row == merge_graph->band_start)
5619 return isl_bool_false;
5621 if (isl_options_get_schedule_maximize_band_depth(ctx) &&
5622 merge_graph->n_total_row < merge_graph->maxvar)
5623 return isl_bool_false;
5625 if (isl_options_get_schedule_maximize_coincidence(ctx)) {
5626 isl_bool ok;
5628 ok = ok_to_merge_coincident(c, merge_graph);
5629 if (ok < 0 || !ok)
5630 return ok;
5633 return ok_to_merge_proximity(ctx, graph, c, merge_graph);
5636 /* Apply the schedule in "t_node" to the "n" rows starting at "first"
5637 * of the schedule in "node" and return the result.
5639 * That is, essentially compute
5641 * T * N(first:first+n-1)
5643 * taking into account the constant term and the parameter coefficients
5644 * in "t_node".
5646 static __isl_give isl_mat *node_transformation(isl_ctx *ctx,
5647 struct isl_sched_node *t_node, struct isl_sched_node *node,
5648 int first, int n)
5650 int i, j;
5651 isl_mat *t;
5652 int n_row, n_col, n_param, n_var;
5654 n_param = node->nparam;
5655 n_var = node->nvar;
5656 n_row = isl_mat_rows(t_node->sched);
5657 n_col = isl_mat_cols(node->sched);
5658 t = isl_mat_alloc(ctx, n_row, n_col);
5659 if (!t)
5660 return NULL;
5661 for (i = 0; i < n_row; ++i) {
5662 isl_seq_cpy(t->row[i], t_node->sched->row[i], 1 + n_param);
5663 isl_seq_clr(t->row[i] + 1 + n_param, n_var);
5664 for (j = 0; j < n; ++j)
5665 isl_seq_addmul(t->row[i],
5666 t_node->sched->row[i][1 + n_param + j],
5667 node->sched->row[first + j],
5668 1 + n_param + n_var);
5670 return t;
5673 /* Apply the cluster schedule in "t_node" to the current band
5674 * schedule of the nodes in "graph".
5676 * In particular, replace the rows starting at band_start
5677 * by the result of applying the cluster schedule in "t_node"
5678 * to the original rows.
5680 * The coincidence of the schedule is determined by the coincidence
5681 * of the cluster schedule.
5683 static isl_stat transform(isl_ctx *ctx, struct isl_sched_graph *graph,
5684 struct isl_sched_node *t_node)
5686 int i, j;
5687 int n_new;
5688 int start, n;
5690 start = graph->band_start;
5691 n = graph->n_total_row - start;
5693 n_new = isl_mat_rows(t_node->sched);
5694 for (i = 0; i < graph->n; ++i) {
5695 struct isl_sched_node *node = &graph->node[i];
5696 isl_mat *t;
5698 t = node_transformation(ctx, t_node, node, start, n);
5699 node->sched = isl_mat_drop_rows(node->sched, start, n);
5700 node->sched = isl_mat_concat(node->sched, t);
5701 node->sched_map = isl_map_free(node->sched_map);
5702 if (!node->sched)
5703 return isl_stat_error;
5704 for (j = 0; j < n_new; ++j)
5705 node->coincident[start + j] = t_node->coincident[j];
5707 graph->n_total_row -= n;
5708 graph->n_row -= n;
5709 graph->n_total_row += n_new;
5710 graph->n_row += n_new;
5712 return isl_stat_ok;
5715 /* Merge the clusters marked for merging in "c" into a single
5716 * cluster using the cluster schedule in the current band of "merge_graph".
5717 * The representative SCC for the new cluster is the SCC with
5718 * the smallest index.
5720 * The current band schedule of each SCC in the new cluster is obtained
5721 * by applying the schedule of the corresponding original cluster
5722 * to the original band schedule.
5723 * All SCCs in the new cluster have the same number of schedule rows.
5725 static isl_stat merge(isl_ctx *ctx, struct isl_clustering *c,
5726 struct isl_sched_graph *merge_graph)
5728 int i;
5729 int cluster = -1;
5730 isl_space *space;
5732 for (i = 0; i < c->n; ++i) {
5733 struct isl_sched_node *node;
5735 if (!c->scc_in_merge[i])
5736 continue;
5737 if (cluster < 0)
5738 cluster = i;
5739 space = cluster_space(&c->scc[i], c->scc_cluster[i]);
5740 if (!space)
5741 return isl_stat_error;
5742 node = graph_find_node(ctx, merge_graph, space);
5743 isl_space_free(space);
5744 if (!node)
5745 isl_die(ctx, isl_error_internal,
5746 "unable to find cluster",
5747 return isl_stat_error);
5748 if (transform(ctx, &c->scc[i], node) < 0)
5749 return isl_stat_error;
5750 c->scc_cluster[i] = cluster;
5753 return isl_stat_ok;
5756 /* Try and merge the clusters of SCCs marked in c->scc_in_merge
5757 * by scheduling the current cluster bands with respect to each other.
5759 * Construct a dependence graph with a space for each cluster and
5760 * with the coordinates of each space corresponding to the schedule
5761 * dimensions of the current band of that cluster.
5762 * Construct a cluster schedule in this cluster dependence graph and
5763 * apply it to the current cluster bands if it is applicable
5764 * according to ok_to_merge.
5766 * If the number of remaining schedule dimensions in a cluster
5767 * with a non-maximal current schedule dimension is greater than
5768 * the number of remaining schedule dimensions in clusters
5769 * with a maximal current schedule dimension, then restrict
5770 * the number of rows to be computed in the cluster schedule
5771 * to the minimal such non-maximal current schedule dimension.
5772 * Do this by adjusting merge_graph.maxvar.
5774 * Return isl_bool_true if the clusters have effectively been merged
5775 * into a single cluster.
5777 * Note that since the standard scheduling algorithm minimizes the maximal
5778 * distance over proximity constraints, the proximity constraints between
5779 * the merged clusters may not be optimized any further than what is
5780 * sufficient to bring the distances within the limits of the internal
5781 * proximity constraints inside the individual clusters.
5782 * It may therefore make sense to perform an additional translation step
5783 * to bring the clusters closer to each other, while maintaining
5784 * the linear part of the merging schedule found using the standard
5785 * scheduling algorithm.
5787 static isl_bool try_merge(isl_ctx *ctx, struct isl_sched_graph *graph,
5788 struct isl_clustering *c)
5790 struct isl_sched_graph merge_graph = { 0 };
5791 isl_bool merged;
5793 if (init_merge_graph(ctx, graph, c, &merge_graph) < 0)
5794 goto error;
5796 if (compute_maxvar(&merge_graph) < 0)
5797 goto error;
5798 if (adjust_maxvar_to_slack(ctx, &merge_graph,c) < 0)
5799 goto error;
5800 if (compute_schedule_wcc_band(ctx, &merge_graph) < 0)
5801 goto error;
5802 merged = ok_to_merge(ctx, graph, c, &merge_graph);
5803 if (merged && merge(ctx, c, &merge_graph) < 0)
5804 goto error;
5806 graph_free(ctx, &merge_graph);
5807 return merged;
5808 error:
5809 graph_free(ctx, &merge_graph);
5810 return isl_bool_error;
5813 /* Is there any edge marked "no_merge" between two SCCs that are
5814 * about to be merged (i.e., that are set in "scc_in_merge")?
5815 * "merge_edge" is the proximity edge along which the clusters of SCCs
5816 * are going to be merged.
5818 * If there is any edge between two SCCs with a negative weight,
5819 * while the weight of "merge_edge" is non-negative, then this
5820 * means that the edge was postponed. "merge_edge" should then
5821 * also be postponed since merging along the edge with negative weight should
5822 * be postponed until all edges with non-negative weight have been tried.
5823 * Replace the weight of "merge_edge" by a negative weight as well and
5824 * tell the caller not to attempt a merge.
5826 static int any_no_merge(struct isl_sched_graph *graph, int *scc_in_merge,
5827 struct isl_sched_edge *merge_edge)
5829 int i;
5831 for (i = 0; i < graph->n_edge; ++i) {
5832 struct isl_sched_edge *edge = &graph->edge[i];
5834 if (!scc_in_merge[edge->src->scc])
5835 continue;
5836 if (!scc_in_merge[edge->dst->scc])
5837 continue;
5838 if (edge->no_merge)
5839 return 1;
5840 if (merge_edge->weight >= 0 && edge->weight < 0) {
5841 merge_edge->weight -= graph->max_weight + 1;
5842 return 1;
5846 return 0;
5849 /* Merge the two clusters in "c" connected by the edge in "graph"
5850 * with index "edge" into a single cluster.
5851 * If it turns out to be impossible to merge these two clusters,
5852 * then mark the edge as "no_merge" such that it will not be
5853 * considered again.
5855 * First mark all SCCs that need to be merged. This includes the SCCs
5856 * in the two clusters, but it may also include the SCCs
5857 * of intermediate clusters.
5858 * If there is already a no_merge edge between any pair of such SCCs,
5859 * then simply mark the current edge as no_merge as well.
5860 * Likewise, if any of those edges was postponed by has_bounded_distances,
5861 * then postpone the current edge as well.
5862 * Otherwise, try and merge the clusters and mark "edge" as "no_merge"
5863 * if the clusters did not end up getting merged, unless the non-merge
5864 * is due to the fact that the edge was postponed. This postponement
5865 * can be recognized by a change in weight (from non-negative to negative).
5867 static isl_stat merge_clusters_along_edge(isl_ctx *ctx,
5868 struct isl_sched_graph *graph, int edge, struct isl_clustering *c)
5870 isl_bool merged;
5871 int edge_weight = graph->edge[edge].weight;
5873 if (mark_merge_sccs(ctx, graph, edge, c) < 0)
5874 return isl_stat_error;
5876 if (any_no_merge(graph, c->scc_in_merge, &graph->edge[edge]))
5877 merged = isl_bool_false;
5878 else
5879 merged = try_merge(ctx, graph, c);
5880 if (merged < 0)
5881 return isl_stat_error;
5882 if (!merged && edge_weight == graph->edge[edge].weight)
5883 graph->edge[edge].no_merge = 1;
5885 return isl_stat_ok;
5888 /* Does "node" belong to the cluster identified by "cluster"?
5890 static int node_cluster_exactly(struct isl_sched_node *node, int cluster)
5892 return node->cluster == cluster;
5895 /* Does "edge" connect two nodes belonging to the cluster
5896 * identified by "cluster"?
5898 static int edge_cluster_exactly(struct isl_sched_edge *edge, int cluster)
5900 return edge->src->cluster == cluster && edge->dst->cluster == cluster;
5903 /* Swap the schedule of "node1" and "node2".
5904 * Both nodes have been derived from the same node in a common parent graph.
5905 * Since the "coincident" field is shared with that node
5906 * in the parent graph, there is no need to also swap this field.
5908 static void swap_sched(struct isl_sched_node *node1,
5909 struct isl_sched_node *node2)
5911 isl_mat *sched;
5912 isl_map *sched_map;
5914 sched = node1->sched;
5915 node1->sched = node2->sched;
5916 node2->sched = sched;
5918 sched_map = node1->sched_map;
5919 node1->sched_map = node2->sched_map;
5920 node2->sched_map = sched_map;
5923 /* Copy the current band schedule from the SCCs that form the cluster
5924 * with index "pos" to the actual cluster at position "pos".
5925 * By construction, the index of the first SCC that belongs to the cluster
5926 * is also "pos".
5928 * The order of the nodes inside both the SCCs and the cluster
5929 * is assumed to be same as the order in the original "graph".
5931 * Since the SCC graphs will no longer be used after this function,
5932 * the schedules are actually swapped rather than copied.
5934 static isl_stat copy_partial(struct isl_sched_graph *graph,
5935 struct isl_clustering *c, int pos)
5937 int i, j;
5939 c->cluster[pos].n_total_row = c->scc[pos].n_total_row;
5940 c->cluster[pos].n_row = c->scc[pos].n_row;
5941 c->cluster[pos].maxvar = c->scc[pos].maxvar;
5942 j = 0;
5943 for (i = 0; i < graph->n; ++i) {
5944 int k;
5945 int s;
5947 if (graph->node[i].cluster != pos)
5948 continue;
5949 s = graph->node[i].scc;
5950 k = c->scc_node[s]++;
5951 swap_sched(&c->cluster[pos].node[j], &c->scc[s].node[k]);
5952 if (c->scc[s].maxvar > c->cluster[pos].maxvar)
5953 c->cluster[pos].maxvar = c->scc[s].maxvar;
5954 ++j;
5957 return isl_stat_ok;
5960 /* Is there a (conditional) validity dependence from node[j] to node[i],
5961 * forcing node[i] to follow node[j] or do the nodes belong to the same
5962 * cluster?
5964 static isl_bool node_follows_strong_or_same_cluster(int i, int j, void *user)
5966 struct isl_sched_graph *graph = user;
5968 if (graph->node[i].cluster == graph->node[j].cluster)
5969 return isl_bool_true;
5970 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
5973 /* Extract the merged clusters of SCCs in "graph", sort them, and
5974 * store them in c->clusters. Update c->scc_cluster accordingly.
5976 * First keep track of the cluster containing the SCC to which a node
5977 * belongs in the node itself.
5978 * Then extract the clusters into c->clusters, copying the current
5979 * band schedule from the SCCs that belong to the cluster.
5980 * Do this only once per cluster.
5982 * Finally, topologically sort the clusters and update c->scc_cluster
5983 * to match the new scc numbering. While the SCCs were originally
5984 * sorted already, some SCCs that depend on some other SCCs may
5985 * have been merged with SCCs that appear before these other SCCs.
5986 * A reordering may therefore be required.
5988 static isl_stat extract_clusters(isl_ctx *ctx, struct isl_sched_graph *graph,
5989 struct isl_clustering *c)
5991 int i;
5993 for (i = 0; i < graph->n; ++i)
5994 graph->node[i].cluster = c->scc_cluster[graph->node[i].scc];
5996 for (i = 0; i < graph->scc; ++i) {
5997 if (c->scc_cluster[i] != i)
5998 continue;
5999 if (extract_sub_graph(ctx, graph, &node_cluster_exactly,
6000 &edge_cluster_exactly, i, &c->cluster[i]) < 0)
6001 return isl_stat_error;
6002 c->cluster[i].src_scc = -1;
6003 c->cluster[i].dst_scc = -1;
6004 if (copy_partial(graph, c, i) < 0)
6005 return isl_stat_error;
6008 if (detect_ccs(ctx, graph, &node_follows_strong_or_same_cluster) < 0)
6009 return isl_stat_error;
6010 for (i = 0; i < graph->n; ++i)
6011 c->scc_cluster[graph->node[i].scc] = graph->node[i].cluster;
6013 return isl_stat_ok;
6016 /* Compute weights on the proximity edges of "graph" that can
6017 * be used by find_proximity to find the most appropriate
6018 * proximity edge to use to merge two clusters in "c".
6019 * The weights are also used by has_bounded_distances to determine
6020 * whether the merge should be allowed.
6021 * Store the maximum of the computed weights in graph->max_weight.
6023 * The computed weight is a measure for the number of remaining schedule
6024 * dimensions that can still be completely aligned.
6025 * In particular, compute the number of equalities between
6026 * input dimensions and output dimensions in the proximity constraints.
6027 * The directions that are already handled by outer schedule bands
6028 * are projected out prior to determining this number.
6030 * Edges that will never be considered by find_proximity are ignored.
6032 static isl_stat compute_weights(struct isl_sched_graph *graph,
6033 struct isl_clustering *c)
6035 int i;
6037 graph->max_weight = 0;
6039 for (i = 0; i < graph->n_edge; ++i) {
6040 struct isl_sched_edge *edge = &graph->edge[i];
6041 struct isl_sched_node *src = edge->src;
6042 struct isl_sched_node *dst = edge->dst;
6043 isl_basic_map *hull;
6044 int n_in, n_out;
6046 if (!is_proximity(edge))
6047 continue;
6048 if (bad_cluster(&c->scc[edge->src->scc]) ||
6049 bad_cluster(&c->scc[edge->dst->scc]))
6050 continue;
6051 if (c->scc_cluster[edge->dst->scc] ==
6052 c->scc_cluster[edge->src->scc])
6053 continue;
6055 hull = isl_map_affine_hull(isl_map_copy(edge->map));
6056 hull = isl_basic_map_transform_dims(hull, isl_dim_in, 0,
6057 isl_mat_copy(src->ctrans));
6058 hull = isl_basic_map_transform_dims(hull, isl_dim_out, 0,
6059 isl_mat_copy(dst->ctrans));
6060 hull = isl_basic_map_project_out(hull,
6061 isl_dim_in, 0, src->rank);
6062 hull = isl_basic_map_project_out(hull,
6063 isl_dim_out, 0, dst->rank);
6064 hull = isl_basic_map_remove_divs(hull);
6065 n_in = isl_basic_map_dim(hull, isl_dim_in);
6066 n_out = isl_basic_map_dim(hull, isl_dim_out);
6067 hull = isl_basic_map_drop_constraints_not_involving_dims(hull,
6068 isl_dim_in, 0, n_in);
6069 hull = isl_basic_map_drop_constraints_not_involving_dims(hull,
6070 isl_dim_out, 0, n_out);
6071 if (!hull)
6072 return isl_stat_error;
6073 edge->weight = hull->n_eq;
6074 isl_basic_map_free(hull);
6076 if (edge->weight > graph->max_weight)
6077 graph->max_weight = edge->weight;
6080 return isl_stat_ok;
6083 /* Call compute_schedule_finish_band on each of the clusters in "c"
6084 * in their topological order. This order is determined by the scc
6085 * fields of the nodes in "graph".
6086 * Combine the results in a sequence expressing the topological order.
6088 * If there is only one cluster left, then there is no need to introduce
6089 * a sequence node. Also, in this case, the cluster necessarily contains
6090 * the SCC at position 0 in the original graph and is therefore also
6091 * stored in the first cluster of "c".
6093 static __isl_give isl_schedule_node *finish_bands_clustering(
6094 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
6095 struct isl_clustering *c)
6097 int i;
6098 isl_ctx *ctx;
6099 isl_union_set_list *filters;
6101 if (graph->scc == 1)
6102 return compute_schedule_finish_band(node, &c->cluster[0], 0);
6104 ctx = isl_schedule_node_get_ctx(node);
6106 filters = extract_sccs(ctx, graph);
6107 node = isl_schedule_node_insert_sequence(node, filters);
6109 for (i = 0; i < graph->scc; ++i) {
6110 int j = c->scc_cluster[i];
6111 node = isl_schedule_node_child(node, i);
6112 node = isl_schedule_node_child(node, 0);
6113 node = compute_schedule_finish_band(node, &c->cluster[j], 0);
6114 node = isl_schedule_node_parent(node);
6115 node = isl_schedule_node_parent(node);
6118 return node;
6121 /* Compute a schedule for a connected dependence graph by first considering
6122 * each strongly connected component (SCC) in the graph separately and then
6123 * incrementally combining them into clusters.
6124 * Return the updated schedule node.
6126 * Initially, each cluster consists of a single SCC, each with its
6127 * own band schedule. The algorithm then tries to merge pairs
6128 * of clusters along a proximity edge until no more suitable
6129 * proximity edges can be found. During this merging, the schedule
6130 * is maintained in the individual SCCs.
6131 * After the merging is completed, the full resulting clusters
6132 * are extracted and in finish_bands_clustering,
6133 * compute_schedule_finish_band is called on each of them to integrate
6134 * the band into "node" and to continue the computation.
6136 * compute_weights initializes the weights that are used by find_proximity.
6138 static __isl_give isl_schedule_node *compute_schedule_wcc_clustering(
6139 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
6141 isl_ctx *ctx;
6142 struct isl_clustering c;
6143 int i;
6145 ctx = isl_schedule_node_get_ctx(node);
6147 if (clustering_init(ctx, &c, graph) < 0)
6148 goto error;
6150 if (compute_weights(graph, &c) < 0)
6151 goto error;
6153 for (;;) {
6154 i = find_proximity(graph, &c);
6155 if (i < 0)
6156 goto error;
6157 if (i >= graph->n_edge)
6158 break;
6159 if (merge_clusters_along_edge(ctx, graph, i, &c) < 0)
6160 goto error;
6163 if (extract_clusters(ctx, graph, &c) < 0)
6164 goto error;
6166 node = finish_bands_clustering(node, graph, &c);
6168 clustering_free(ctx, &c);
6169 return node;
6170 error:
6171 clustering_free(ctx, &c);
6172 return isl_schedule_node_free(node);
6175 /* Compute a schedule for a connected dependence graph and return
6176 * the updated schedule node.
6178 * If Feautrier's algorithm is selected, we first recursively try to satisfy
6179 * as many validity dependences as possible. When all validity dependences
6180 * are satisfied we extend the schedule to a full-dimensional schedule.
6182 * Call compute_schedule_wcc_whole or compute_schedule_wcc_clustering
6183 * depending on whether the user has selected the option to try and
6184 * compute a schedule for the entire (weakly connected) component first.
6185 * If there is only a single strongly connected component (SCC), then
6186 * there is no point in trying to combine SCCs
6187 * in compute_schedule_wcc_clustering, so compute_schedule_wcc_whole
6188 * is called instead.
6190 static __isl_give isl_schedule_node *compute_schedule_wcc(
6191 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
6193 isl_ctx *ctx;
6195 if (!node)
6196 return NULL;
6198 ctx = isl_schedule_node_get_ctx(node);
6199 if (detect_sccs(ctx, graph) < 0)
6200 return isl_schedule_node_free(node);
6202 if (compute_maxvar(graph) < 0)
6203 return isl_schedule_node_free(node);
6205 if (need_feautrier_step(ctx, graph))
6206 return compute_schedule_wcc_feautrier(node, graph);
6208 if (graph->scc <= 1 || isl_options_get_schedule_whole_component(ctx))
6209 return compute_schedule_wcc_whole(node, graph);
6210 else
6211 return compute_schedule_wcc_clustering(node, graph);
6214 /* Compute a schedule for each group of nodes identified by node->scc
6215 * separately and then combine them in a sequence node (or as set node
6216 * if graph->weak is set) inserted at position "node" of the schedule tree.
6217 * Return the updated schedule node.
6219 * If "wcc" is set then each of the groups belongs to a single
6220 * weakly connected component in the dependence graph so that
6221 * there is no need for compute_sub_schedule to look for weakly
6222 * connected components.
6224 static __isl_give isl_schedule_node *compute_component_schedule(
6225 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
6226 int wcc)
6228 int component;
6229 isl_ctx *ctx;
6230 isl_union_set_list *filters;
6232 if (!node)
6233 return NULL;
6234 ctx = isl_schedule_node_get_ctx(node);
6236 filters = extract_sccs(ctx, graph);
6237 if (graph->weak)
6238 node = isl_schedule_node_insert_set(node, filters);
6239 else
6240 node = isl_schedule_node_insert_sequence(node, filters);
6242 for (component = 0; component < graph->scc; ++component) {
6243 node = isl_schedule_node_child(node, component);
6244 node = isl_schedule_node_child(node, 0);
6245 node = compute_sub_schedule(node, ctx, graph,
6246 &node_scc_exactly,
6247 &edge_scc_exactly, component, wcc);
6248 node = isl_schedule_node_parent(node);
6249 node = isl_schedule_node_parent(node);
6252 return node;
6255 /* Compute a schedule for the given dependence graph and insert it at "node".
6256 * Return the updated schedule node.
6258 * We first check if the graph is connected (through validity and conditional
6259 * validity dependences) and, if not, compute a schedule
6260 * for each component separately.
6261 * If the schedule_serialize_sccs option is set, then we check for strongly
6262 * connected components instead and compute a separate schedule for
6263 * each such strongly connected component.
6265 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
6266 struct isl_sched_graph *graph)
6268 isl_ctx *ctx;
6270 if (!node)
6271 return NULL;
6273 ctx = isl_schedule_node_get_ctx(node);
6274 if (isl_options_get_schedule_serialize_sccs(ctx)) {
6275 if (detect_sccs(ctx, graph) < 0)
6276 return isl_schedule_node_free(node);
6277 } else {
6278 if (detect_wccs(ctx, graph) < 0)
6279 return isl_schedule_node_free(node);
6282 if (graph->scc > 1)
6283 return compute_component_schedule(node, graph, 1);
6285 return compute_schedule_wcc(node, graph);
6288 /* Compute a schedule on sc->domain that respects the given schedule
6289 * constraints.
6291 * In particular, the schedule respects all the validity dependences.
6292 * If the default isl scheduling algorithm is used, it tries to minimize
6293 * the dependence distances over the proximity dependences.
6294 * If Feautrier's scheduling algorithm is used, the proximity dependence
6295 * distances are only minimized during the extension to a full-dimensional
6296 * schedule.
6298 * If there are any condition and conditional validity dependences,
6299 * then the conditional validity dependences may be violated inside
6300 * a tilable band, provided they have no adjacent non-local
6301 * condition dependences.
6303 __isl_give isl_schedule *isl_schedule_constraints_compute_schedule(
6304 __isl_take isl_schedule_constraints *sc)
6306 isl_ctx *ctx = isl_schedule_constraints_get_ctx(sc);
6307 struct isl_sched_graph graph = { 0 };
6308 isl_schedule *sched;
6309 isl_schedule_node *node;
6310 isl_union_set *domain;
6312 sc = isl_schedule_constraints_align_params(sc);
6314 domain = isl_schedule_constraints_get_domain(sc);
6315 if (isl_union_set_n_set(domain) == 0) {
6316 isl_schedule_constraints_free(sc);
6317 return isl_schedule_from_domain(domain);
6320 if (graph_init(&graph, sc) < 0)
6321 domain = isl_union_set_free(domain);
6323 node = isl_schedule_node_from_domain(domain);
6324 node = isl_schedule_node_child(node, 0);
6325 if (graph.n > 0)
6326 node = compute_schedule(node, &graph);
6327 sched = isl_schedule_node_get_schedule(node);
6328 isl_schedule_node_free(node);
6330 graph_free(ctx, &graph);
6331 isl_schedule_constraints_free(sc);
6333 return sched;
6336 /* Compute a schedule for the given union of domains that respects
6337 * all the validity dependences and minimizes
6338 * the dependence distances over the proximity dependences.
6340 * This function is kept for backward compatibility.
6342 __isl_give isl_schedule *isl_union_set_compute_schedule(
6343 __isl_take isl_union_set *domain,
6344 __isl_take isl_union_map *validity,
6345 __isl_take isl_union_map *proximity)
6347 isl_schedule_constraints *sc;
6349 sc = isl_schedule_constraints_on_domain(domain);
6350 sc = isl_schedule_constraints_set_validity(sc, validity);
6351 sc = isl_schedule_constraints_set_proximity(sc, proximity);
6353 return isl_schedule_constraints_compute_schedule(sc);