isl_scheduler.c: extract_edge: do not add edges that turn out to be empty
[isl.git] / isl_scheduler.c
blob2add06be112c99e55f973c9a71c06b9da8357fcb
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 /* Is "edge" of a type that can appear multiple times between
284 * the same pair of nodes?
286 * Condition edges and conditional validity edges may have tagged
287 * dependence relations, in which case an edge is added for each
288 * pair of tags.
290 static int is_multi_edge_type(struct isl_sched_edge *edge)
292 return is_condition(edge) || is_conditional_validity(edge);
295 /* Internal information about the dependence graph used during
296 * the construction of the schedule.
298 * intra_hmap is a cache, mapping dependence relations to their dual,
299 * for dependences from a node to itself
300 * inter_hmap is a cache, mapping dependence relations to their dual,
301 * for dependences between distinct nodes
302 * if compression is involved then the key for these maps
303 * is the original, uncompressed dependence relation, while
304 * the value is the dual of the compressed dependence relation.
306 * n is the number of nodes
307 * node is the list of nodes
308 * maxvar is the maximal number of variables over all nodes
309 * max_row is the allocated number of rows in the schedule
310 * n_row is the current (maximal) number of linearly independent
311 * rows in the node schedules
312 * n_total_row is the current number of rows in the node schedules
313 * band_start is the starting row in the node schedules of the current band
314 * root is set if this graph is the original dependence graph,
315 * without any splitting
317 * sorted contains a list of node indices sorted according to the
318 * SCC to which a node belongs
320 * n_edge is the number of edges
321 * edge is the list of edges
322 * max_edge contains the maximal number of edges of each type;
323 * in particular, it contains the number of edges in the inital graph.
324 * edge_table contains pointers into the edge array, hashed on the source
325 * and sink spaces; there is one such table for each type;
326 * a given edge may be referenced from more than one table
327 * if the corresponding relation appears in more than one of the
328 * sets of dependences; however, for each type there is only
329 * a single edge between a given pair of source and sink space
330 * in the entire graph
332 * node_table contains pointers into the node array, hashed on the space
334 * region contains a list of variable sequences that should be non-trivial
336 * lp contains the (I)LP problem used to obtain new schedule rows
338 * src_scc and dst_scc are the source and sink SCCs of an edge with
339 * conflicting constraints
341 * scc represents the number of components
342 * weak is set if the components are weakly connected
344 * max_weight is used during clustering and represents the maximal
345 * weight of the relevant proximity edges.
347 struct isl_sched_graph {
348 isl_map_to_basic_set *intra_hmap;
349 isl_map_to_basic_set *inter_hmap;
351 struct isl_sched_node *node;
352 int n;
353 int maxvar;
354 int max_row;
355 int n_row;
357 int *sorted;
359 int n_total_row;
360 int band_start;
362 int root;
364 struct isl_sched_edge *edge;
365 int n_edge;
366 int max_edge[isl_edge_last + 1];
367 struct isl_hash_table *edge_table[isl_edge_last + 1];
369 struct isl_hash_table *node_table;
370 struct isl_region *region;
372 isl_basic_set *lp;
374 int src_scc;
375 int dst_scc;
377 int scc;
378 int weak;
380 int max_weight;
383 /* Initialize node_table based on the list of nodes.
385 static int graph_init_table(isl_ctx *ctx, struct isl_sched_graph *graph)
387 int i;
389 graph->node_table = isl_hash_table_alloc(ctx, graph->n);
390 if (!graph->node_table)
391 return -1;
393 for (i = 0; i < graph->n; ++i) {
394 struct isl_hash_table_entry *entry;
395 uint32_t hash;
397 hash = isl_space_get_hash(graph->node[i].space);
398 entry = isl_hash_table_find(ctx, graph->node_table, hash,
399 &node_has_space,
400 graph->node[i].space, 1);
401 if (!entry)
402 return -1;
403 entry->data = &graph->node[i];
406 return 0;
409 /* Return a pointer to the node that lives within the given space,
410 * or NULL if there is no such node.
412 static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
413 struct isl_sched_graph *graph, __isl_keep isl_space *dim)
415 struct isl_hash_table_entry *entry;
416 uint32_t hash;
418 hash = isl_space_get_hash(dim);
419 entry = isl_hash_table_find(ctx, graph->node_table, hash,
420 &node_has_space, dim, 0);
422 return entry ? entry->data : NULL;
425 static int edge_has_src_and_dst(const void *entry, const void *val)
427 const struct isl_sched_edge *edge = entry;
428 const struct isl_sched_edge *temp = val;
430 return edge->src == temp->src && edge->dst == temp->dst;
433 /* Add the given edge to graph->edge_table[type].
435 static isl_stat graph_edge_table_add(isl_ctx *ctx,
436 struct isl_sched_graph *graph, enum isl_edge_type type,
437 struct isl_sched_edge *edge)
439 struct isl_hash_table_entry *entry;
440 uint32_t hash;
442 hash = isl_hash_init();
443 hash = isl_hash_builtin(hash, edge->src);
444 hash = isl_hash_builtin(hash, edge->dst);
445 entry = isl_hash_table_find(ctx, graph->edge_table[type], hash,
446 &edge_has_src_and_dst, edge, 1);
447 if (!entry)
448 return isl_stat_error;
449 entry->data = edge;
451 return isl_stat_ok;
454 /* Add "edge" to all relevant edge tables.
455 * That is, for every type of the edge, add it to the corresponding table.
457 static isl_stat graph_edge_tables_add(isl_ctx *ctx,
458 struct isl_sched_graph *graph, struct isl_sched_edge *edge)
460 enum isl_edge_type t;
462 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
463 if (!is_type(edge, t))
464 continue;
465 if (graph_edge_table_add(ctx, graph, t, edge) < 0)
466 return isl_stat_error;
469 return isl_stat_ok;
472 /* Allocate the edge_tables based on the maximal number of edges of
473 * each type.
475 static int graph_init_edge_tables(isl_ctx *ctx, struct isl_sched_graph *graph)
477 int i;
479 for (i = 0; i <= isl_edge_last; ++i) {
480 graph->edge_table[i] = isl_hash_table_alloc(ctx,
481 graph->max_edge[i]);
482 if (!graph->edge_table[i])
483 return -1;
486 return 0;
489 /* If graph->edge_table[type] contains an edge from the given source
490 * to the given destination, then return the hash table entry of this edge.
491 * Otherwise, return NULL.
493 static struct isl_hash_table_entry *graph_find_edge_entry(
494 struct isl_sched_graph *graph,
495 enum isl_edge_type type,
496 struct isl_sched_node *src, struct isl_sched_node *dst)
498 isl_ctx *ctx = isl_space_get_ctx(src->space);
499 uint32_t hash;
500 struct isl_sched_edge temp = { .src = src, .dst = dst };
502 hash = isl_hash_init();
503 hash = isl_hash_builtin(hash, temp.src);
504 hash = isl_hash_builtin(hash, temp.dst);
505 return isl_hash_table_find(ctx, graph->edge_table[type], hash,
506 &edge_has_src_and_dst, &temp, 0);
510 /* If graph->edge_table[type] contains an edge from the given source
511 * to the given destination, then return this edge.
512 * Otherwise, return NULL.
514 static struct isl_sched_edge *graph_find_edge(struct isl_sched_graph *graph,
515 enum isl_edge_type type,
516 struct isl_sched_node *src, struct isl_sched_node *dst)
518 struct isl_hash_table_entry *entry;
520 entry = graph_find_edge_entry(graph, type, src, dst);
521 if (!entry)
522 return NULL;
524 return entry->data;
527 /* Check whether the dependence graph has an edge of the given type
528 * between the given two nodes.
530 static isl_bool graph_has_edge(struct isl_sched_graph *graph,
531 enum isl_edge_type type,
532 struct isl_sched_node *src, struct isl_sched_node *dst)
534 struct isl_sched_edge *edge;
535 isl_bool empty;
537 edge = graph_find_edge(graph, type, src, dst);
538 if (!edge)
539 return 0;
541 empty = isl_map_plain_is_empty(edge->map);
542 if (empty < 0)
543 return isl_bool_error;
545 return !empty;
548 /* Look for any edge with the same src, dst and map fields as "model".
550 * Return the matching edge if one can be found.
551 * Return "model" if no matching edge is found.
552 * Return NULL on error.
554 static struct isl_sched_edge *graph_find_matching_edge(
555 struct isl_sched_graph *graph, struct isl_sched_edge *model)
557 enum isl_edge_type i;
558 struct isl_sched_edge *edge;
560 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
561 int is_equal;
563 edge = graph_find_edge(graph, i, model->src, model->dst);
564 if (!edge)
565 continue;
566 is_equal = isl_map_plain_is_equal(model->map, edge->map);
567 if (is_equal < 0)
568 return NULL;
569 if (is_equal)
570 return edge;
573 return model;
576 /* Remove the given edge from all the edge_tables that refer to it.
578 static void graph_remove_edge(struct isl_sched_graph *graph,
579 struct isl_sched_edge *edge)
581 isl_ctx *ctx = isl_map_get_ctx(edge->map);
582 enum isl_edge_type i;
584 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
585 struct isl_hash_table_entry *entry;
587 entry = graph_find_edge_entry(graph, i, edge->src, edge->dst);
588 if (!entry)
589 continue;
590 if (entry->data != edge)
591 continue;
592 isl_hash_table_remove(ctx, graph->edge_table[i], entry);
596 /* Check whether the dependence graph has any edge
597 * between the given two nodes.
599 static isl_bool graph_has_any_edge(struct isl_sched_graph *graph,
600 struct isl_sched_node *src, struct isl_sched_node *dst)
602 enum isl_edge_type i;
603 isl_bool r;
605 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
606 r = graph_has_edge(graph, i, src, dst);
607 if (r < 0 || r)
608 return r;
611 return r;
614 /* Check whether the dependence graph has a validity edge
615 * between the given two nodes.
617 * Conditional validity edges are essentially validity edges that
618 * can be ignored if the corresponding condition edges are iteration private.
619 * Here, we are only checking for the presence of validity
620 * edges, so we need to consider the conditional validity edges too.
621 * In particular, this function is used during the detection
622 * of strongly connected components and we cannot ignore
623 * conditional validity edges during this detection.
625 static isl_bool graph_has_validity_edge(struct isl_sched_graph *graph,
626 struct isl_sched_node *src, struct isl_sched_node *dst)
628 isl_bool r;
630 r = graph_has_edge(graph, isl_edge_validity, src, dst);
631 if (r < 0 || r)
632 return r;
634 return graph_has_edge(graph, isl_edge_conditional_validity, src, dst);
637 static int graph_alloc(isl_ctx *ctx, struct isl_sched_graph *graph,
638 int n_node, int n_edge)
640 int i;
642 graph->n = n_node;
643 graph->n_edge = n_edge;
644 graph->node = isl_calloc_array(ctx, struct isl_sched_node, graph->n);
645 graph->sorted = isl_calloc_array(ctx, int, graph->n);
646 graph->region = isl_alloc_array(ctx, struct isl_region, graph->n);
647 graph->edge = isl_calloc_array(ctx,
648 struct isl_sched_edge, graph->n_edge);
650 graph->intra_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
651 graph->inter_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
653 if (!graph->node || !graph->region || (graph->n_edge && !graph->edge) ||
654 !graph->sorted)
655 return -1;
657 for(i = 0; i < graph->n; ++i)
658 graph->sorted[i] = i;
660 return 0;
663 static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
665 int i;
667 isl_map_to_basic_set_free(graph->intra_hmap);
668 isl_map_to_basic_set_free(graph->inter_hmap);
670 if (graph->node)
671 for (i = 0; i < graph->n; ++i) {
672 isl_space_free(graph->node[i].space);
673 isl_set_free(graph->node[i].hull);
674 isl_multi_aff_free(graph->node[i].compress);
675 isl_multi_aff_free(graph->node[i].decompress);
676 isl_mat_free(graph->node[i].sched);
677 isl_map_free(graph->node[i].sched_map);
678 isl_mat_free(graph->node[i].cmap);
679 isl_mat_free(graph->node[i].cinv);
680 isl_mat_free(graph->node[i].ctrans);
681 if (graph->root)
682 free(graph->node[i].coincident);
683 isl_multi_val_free(graph->node[i].sizes);
684 isl_vec_free(graph->node[i].max);
686 free(graph->node);
687 free(graph->sorted);
688 if (graph->edge)
689 for (i = 0; i < graph->n_edge; ++i) {
690 isl_map_free(graph->edge[i].map);
691 isl_union_map_free(graph->edge[i].tagged_condition);
692 isl_union_map_free(graph->edge[i].tagged_validity);
694 free(graph->edge);
695 free(graph->region);
696 for (i = 0; i <= isl_edge_last; ++i)
697 isl_hash_table_free(ctx, graph->edge_table[i]);
698 isl_hash_table_free(ctx, graph->node_table);
699 isl_basic_set_free(graph->lp);
702 /* For each "set" on which this function is called, increment
703 * graph->n by one and update graph->maxvar.
705 static isl_stat init_n_maxvar(__isl_take isl_set *set, void *user)
707 struct isl_sched_graph *graph = user;
708 int nvar = isl_set_dim(set, isl_dim_set);
710 graph->n++;
711 if (nvar > graph->maxvar)
712 graph->maxvar = nvar;
714 isl_set_free(set);
716 return isl_stat_ok;
719 /* Compute the number of rows that should be allocated for the schedule.
720 * In particular, we need one row for each variable or one row
721 * for each basic map in the dependences.
722 * Note that it is practically impossible to exhaust both
723 * the number of dependences and the number of variables.
725 static isl_stat compute_max_row(struct isl_sched_graph *graph,
726 __isl_keep isl_schedule_constraints *sc)
728 int n_edge;
729 isl_stat r;
730 isl_union_set *domain;
732 graph->n = 0;
733 graph->maxvar = 0;
734 domain = isl_schedule_constraints_get_domain(sc);
735 r = isl_union_set_foreach_set(domain, &init_n_maxvar, graph);
736 isl_union_set_free(domain);
737 if (r < 0)
738 return isl_stat_error;
739 n_edge = isl_schedule_constraints_n_basic_map(sc);
740 if (n_edge < 0)
741 return isl_stat_error;
742 graph->max_row = n_edge + graph->maxvar;
744 return isl_stat_ok;
747 /* Does "bset" have any defining equalities for its set variables?
749 static int has_any_defining_equality(__isl_keep isl_basic_set *bset)
751 int i, n;
753 if (!bset)
754 return -1;
756 n = isl_basic_set_dim(bset, isl_dim_set);
757 for (i = 0; i < n; ++i) {
758 int has;
760 has = isl_basic_set_has_defining_equality(bset, isl_dim_set, i,
761 NULL);
762 if (has < 0 || has)
763 return has;
766 return 0;
769 /* Set the entries of node->max to the value of the schedule_max_coefficient
770 * option, if set.
772 static isl_stat set_max_coefficient(isl_ctx *ctx, struct isl_sched_node *node)
774 int max;
776 max = isl_options_get_schedule_max_coefficient(ctx);
777 if (max == -1)
778 return isl_stat_ok;
780 node->max = isl_vec_alloc(ctx, node->nvar);
781 node->max = isl_vec_set_si(node->max, max);
782 if (!node->max)
783 return isl_stat_error;
785 return isl_stat_ok;
788 /* Set the entries of node->max to the minimum of the schedule_max_coefficient
789 * option (if set) and half of the minimum of the sizes in the other
790 * dimensions. If the minimum of the sizes is one, half of the size
791 * is zero and this value is reset to one.
792 * If the global minimum is unbounded (i.e., if both
793 * the schedule_max_coefficient is not set and the sizes in the other
794 * dimensions are unbounded), then store a negative value.
795 * If the schedule coefficient is close to the size of the instance set
796 * in another dimension, then the schedule may represent a loop
797 * coalescing transformation (especially if the coefficient
798 * in that other dimension is one). Forcing the coefficient to be
799 * smaller than or equal to half the minimal size should avoid this
800 * situation.
802 static isl_stat compute_max_coefficient(isl_ctx *ctx,
803 struct isl_sched_node *node)
805 int max;
806 int i, j;
807 isl_vec *v;
809 max = isl_options_get_schedule_max_coefficient(ctx);
810 v = isl_vec_alloc(ctx, node->nvar);
811 if (!v)
812 return isl_stat_error;
814 for (i = 0; i < node->nvar; ++i) {
815 isl_int_set_si(v->el[i], max);
816 isl_int_mul_si(v->el[i], v->el[i], 2);
819 for (i = 0; i < node->nvar; ++i) {
820 isl_val *size;
822 size = isl_multi_val_get_val(node->sizes, i);
823 if (!size)
824 goto error;
825 if (!isl_val_is_int(size)) {
826 isl_val_free(size);
827 continue;
829 for (j = 0; j < node->nvar; ++j) {
830 if (j == i)
831 continue;
832 if (isl_int_is_neg(v->el[j]) ||
833 isl_int_gt(v->el[j], size->n))
834 isl_int_set(v->el[j], size->n);
836 isl_val_free(size);
839 for (i = 0; i < node->nvar; ++i) {
840 isl_int_fdiv_q_ui(v->el[i], v->el[i], 2);
841 if (isl_int_is_zero(v->el[i]))
842 isl_int_set_si(v->el[i], 1);
845 node->max = v;
846 return isl_stat_ok;
847 error:
848 isl_vec_free(v);
849 return isl_stat_error;
852 /* Compute and return the size of "set" in dimension "dim".
853 * The size is taken to be the difference in values for that variable
854 * for fixed values of the other variables.
855 * In particular, the variable is first isolated from the other variables
856 * in the range of a map
858 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [i_dim]
860 * and then duplicated
862 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [[i_dim] -> [i_dim']]
864 * The shared variables are then projected out and the maximal value
865 * of i_dim' - i_dim is computed.
867 static __isl_give isl_val *compute_size(__isl_take isl_set *set, int dim)
869 isl_map *map;
870 isl_local_space *ls;
871 isl_aff *obj;
872 isl_val *v;
874 map = isl_set_project_onto_map(set, isl_dim_set, dim, 1);
875 map = isl_map_project_out(map, isl_dim_in, dim, 1);
876 map = isl_map_range_product(map, isl_map_copy(map));
877 map = isl_set_unwrap(isl_map_range(map));
878 set = isl_map_deltas(map);
879 ls = isl_local_space_from_space(isl_set_get_space(set));
880 obj = isl_aff_var_on_domain(ls, isl_dim_set, 0);
881 v = isl_set_max_val(set, obj);
882 isl_aff_free(obj);
883 isl_set_free(set);
885 return v;
888 /* Compute the size of the instance set "set" of "node", after compression,
889 * as well as bounds on the corresponding coefficients, if needed.
891 * The sizes are needed when the schedule_treat_coalescing option is set.
892 * The bounds are needed when the schedule_treat_coalescing option or
893 * the schedule_max_coefficient option is set.
895 * If the schedule_treat_coalescing option is not set, then at most
896 * the bounds need to be set and this is done in set_max_coefficient.
897 * Otherwise, compress the domain if needed, compute the size
898 * in each direction and store the results in node->size.
899 * Finally, set the bounds on the coefficients based on the sizes
900 * and the schedule_max_coefficient option in compute_max_coefficient.
902 static isl_stat compute_sizes_and_max(isl_ctx *ctx, struct isl_sched_node *node,
903 __isl_take isl_set *set)
905 int j, n;
906 isl_multi_val *mv;
908 if (!isl_options_get_schedule_treat_coalescing(ctx)) {
909 isl_set_free(set);
910 return set_max_coefficient(ctx, node);
913 if (node->compressed)
914 set = isl_set_preimage_multi_aff(set,
915 isl_multi_aff_copy(node->decompress));
916 mv = isl_multi_val_zero(isl_set_get_space(set));
917 n = isl_set_dim(set, isl_dim_set);
918 for (j = 0; j < n; ++j) {
919 isl_val *v;
921 v = compute_size(isl_set_copy(set), j);
922 mv = isl_multi_val_set_val(mv, j, v);
924 node->sizes = mv;
925 isl_set_free(set);
926 if (!node->sizes)
927 return isl_stat_error;
928 return compute_max_coefficient(ctx, node);
931 /* Add a new node to the graph representing the given instance set.
932 * "nvar" is the (possibly compressed) number of variables and
933 * may be smaller than then number of set variables in "set"
934 * if "compressed" is set.
935 * If "compressed" is set, then "hull" represents the constraints
936 * that were used to derive the compression, while "compress" and
937 * "decompress" map the original space to the compressed space and
938 * vice versa.
939 * If "compressed" is not set, then "hull", "compress" and "decompress"
940 * should be NULL.
942 * Compute the size of the instance set and bounds on the coefficients,
943 * if needed.
945 static isl_stat add_node(struct isl_sched_graph *graph,
946 __isl_take isl_set *set, int nvar, int compressed,
947 __isl_take isl_set *hull, __isl_take isl_multi_aff *compress,
948 __isl_take isl_multi_aff *decompress)
950 int nparam;
951 isl_ctx *ctx;
952 isl_mat *sched;
953 isl_space *space;
954 int *coincident;
955 struct isl_sched_node *node;
957 if (!set)
958 return isl_stat_error;
960 ctx = isl_set_get_ctx(set);
961 nparam = isl_set_dim(set, isl_dim_param);
962 if (!ctx->opt->schedule_parametric)
963 nparam = 0;
964 sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
965 node = &graph->node[graph->n];
966 graph->n++;
967 space = isl_set_get_space(set);
968 node->space = space;
969 node->nvar = nvar;
970 node->nparam = nparam;
971 node->sched = sched;
972 node->sched_map = NULL;
973 coincident = isl_calloc_array(ctx, int, graph->max_row);
974 node->coincident = coincident;
975 node->compressed = compressed;
976 node->hull = hull;
977 node->compress = compress;
978 node->decompress = decompress;
979 if (compute_sizes_and_max(ctx, node, set) < 0)
980 return isl_stat_error;
982 if (!space || !sched || (graph->max_row && !coincident))
983 return isl_stat_error;
984 if (compressed && (!hull || !compress || !decompress))
985 return isl_stat_error;
987 return isl_stat_ok;
990 /* Add a new node to the graph representing the given set.
992 * If any of the set variables is defined by an equality, then
993 * we perform variable compression such that we can perform
994 * the scheduling on the compressed domain.
996 static isl_stat extract_node(__isl_take isl_set *set, void *user)
998 int nvar;
999 int has_equality;
1000 isl_basic_set *hull;
1001 isl_set *hull_set;
1002 isl_morph *morph;
1003 isl_multi_aff *compress, *decompress;
1004 struct isl_sched_graph *graph = user;
1006 hull = isl_set_affine_hull(isl_set_copy(set));
1007 hull = isl_basic_set_remove_divs(hull);
1008 nvar = isl_set_dim(set, isl_dim_set);
1009 has_equality = has_any_defining_equality(hull);
1011 if (has_equality < 0)
1012 goto error;
1013 if (!has_equality) {
1014 isl_basic_set_free(hull);
1015 return add_node(graph, set, nvar, 0, NULL, NULL, NULL);
1018 morph = isl_basic_set_variable_compression(hull, isl_dim_set);
1019 nvar = isl_morph_ran_dim(morph, isl_dim_set);
1020 compress = isl_morph_get_var_multi_aff(morph);
1021 morph = isl_morph_inverse(morph);
1022 decompress = isl_morph_get_var_multi_aff(morph);
1023 isl_morph_free(morph);
1025 hull_set = isl_set_from_basic_set(hull);
1026 return add_node(graph, set, nvar, 1, hull_set, compress, decompress);
1027 error:
1028 isl_basic_set_free(hull);
1029 isl_set_free(set);
1030 return isl_stat_error;
1033 struct isl_extract_edge_data {
1034 enum isl_edge_type type;
1035 struct isl_sched_graph *graph;
1038 /* Merge edge2 into edge1, freeing the contents of edge2.
1039 * Return 0 on success and -1 on failure.
1041 * edge1 and edge2 are assumed to have the same value for the map field.
1043 static int merge_edge(struct isl_sched_edge *edge1,
1044 struct isl_sched_edge *edge2)
1046 edge1->types |= edge2->types;
1047 isl_map_free(edge2->map);
1049 if (is_condition(edge2)) {
1050 if (!edge1->tagged_condition)
1051 edge1->tagged_condition = edge2->tagged_condition;
1052 else
1053 edge1->tagged_condition =
1054 isl_union_map_union(edge1->tagged_condition,
1055 edge2->tagged_condition);
1058 if (is_conditional_validity(edge2)) {
1059 if (!edge1->tagged_validity)
1060 edge1->tagged_validity = edge2->tagged_validity;
1061 else
1062 edge1->tagged_validity =
1063 isl_union_map_union(edge1->tagged_validity,
1064 edge2->tagged_validity);
1067 if (is_condition(edge2) && !edge1->tagged_condition)
1068 return -1;
1069 if (is_conditional_validity(edge2) && !edge1->tagged_validity)
1070 return -1;
1072 return 0;
1075 /* Insert dummy tags in domain and range of "map".
1077 * In particular, if "map" is of the form
1079 * A -> B
1081 * then return
1083 * [A -> dummy_tag] -> [B -> dummy_tag]
1085 * where the dummy_tags are identical and equal to any dummy tags
1086 * introduced by any other call to this function.
1088 static __isl_give isl_map *insert_dummy_tags(__isl_take isl_map *map)
1090 static char dummy;
1091 isl_ctx *ctx;
1092 isl_id *id;
1093 isl_space *space;
1094 isl_set *domain, *range;
1096 ctx = isl_map_get_ctx(map);
1098 id = isl_id_alloc(ctx, NULL, &dummy);
1099 space = isl_space_params(isl_map_get_space(map));
1100 space = isl_space_set_from_params(space);
1101 space = isl_space_set_tuple_id(space, isl_dim_set, id);
1102 space = isl_space_map_from_set(space);
1104 domain = isl_map_wrap(map);
1105 range = isl_map_wrap(isl_map_universe(space));
1106 map = isl_map_from_domain_and_range(domain, range);
1107 map = isl_map_zip(map);
1109 return map;
1112 /* Given that at least one of "src" or "dst" is compressed, return
1113 * a map between the spaces of these nodes restricted to the affine
1114 * hull that was used in the compression.
1116 static __isl_give isl_map *extract_hull(struct isl_sched_node *src,
1117 struct isl_sched_node *dst)
1119 isl_set *dom, *ran;
1121 if (src->compressed)
1122 dom = isl_set_copy(src->hull);
1123 else
1124 dom = isl_set_universe(isl_space_copy(src->space));
1125 if (dst->compressed)
1126 ran = isl_set_copy(dst->hull);
1127 else
1128 ran = isl_set_universe(isl_space_copy(dst->space));
1130 return isl_map_from_domain_and_range(dom, ran);
1133 /* Intersect the domains of the nested relations in domain and range
1134 * of "tagged" with "map".
1136 static __isl_give isl_map *map_intersect_domains(__isl_take isl_map *tagged,
1137 __isl_keep isl_map *map)
1139 isl_set *set;
1141 tagged = isl_map_zip(tagged);
1142 set = isl_map_wrap(isl_map_copy(map));
1143 tagged = isl_map_intersect_domain(tagged, set);
1144 tagged = isl_map_zip(tagged);
1145 return tagged;
1148 /* Return a pointer to the node that lives in the domain space of "map"
1149 * or NULL if there is no such node.
1151 static struct isl_sched_node *find_domain_node(isl_ctx *ctx,
1152 struct isl_sched_graph *graph, __isl_keep isl_map *map)
1154 struct isl_sched_node *node;
1155 isl_space *space;
1157 space = isl_space_domain(isl_map_get_space(map));
1158 node = graph_find_node(ctx, graph, space);
1159 isl_space_free(space);
1161 return node;
1164 /* Return a pointer to the node that lives in the range space of "map"
1165 * or NULL if there is no such node.
1167 static struct isl_sched_node *find_range_node(isl_ctx *ctx,
1168 struct isl_sched_graph *graph, __isl_keep isl_map *map)
1170 struct isl_sched_node *node;
1171 isl_space *space;
1173 space = isl_space_range(isl_map_get_space(map));
1174 node = graph_find_node(ctx, graph, space);
1175 isl_space_free(space);
1177 return node;
1180 /* Refrain from adding a new edge based on "map".
1181 * Instead, just free the map.
1182 * "tagged" is either a copy of "map" with additional tags or NULL.
1184 static isl_stat skip_edge(__isl_take isl_map *map, __isl_take isl_map *tagged)
1186 isl_map_free(map);
1187 isl_map_free(tagged);
1189 return isl_stat_ok;
1192 /* Add a new edge to the graph based on the given map
1193 * and add it to data->graph->edge_table[data->type].
1194 * If a dependence relation of a given type happens to be identical
1195 * to one of the dependence relations of a type that was added before,
1196 * then we don't create a new edge, but instead mark the original edge
1197 * as also representing a dependence of the current type.
1199 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1200 * may be specified as "tagged" dependence relations. That is, "map"
1201 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1202 * the dependence on iterations and a and b are tags.
1203 * edge->map is set to the relation containing the elements i -> j,
1204 * while edge->tagged_condition and edge->tagged_validity contain
1205 * the union of all the "map" relations
1206 * for which extract_edge is called that result in the same edge->map.
1208 * If the source or the destination node is compressed, then
1209 * intersect both "map" and "tagged" with the constraints that
1210 * were used to construct the compression.
1211 * This ensures that there are no schedule constraints defined
1212 * outside of these domains, while the scheduler no longer has
1213 * any control over those outside parts.
1215 static isl_stat extract_edge(__isl_take isl_map *map, void *user)
1217 isl_bool empty;
1218 isl_ctx *ctx = isl_map_get_ctx(map);
1219 struct isl_extract_edge_data *data = user;
1220 struct isl_sched_graph *graph = data->graph;
1221 struct isl_sched_node *src, *dst;
1222 struct isl_sched_edge *edge;
1223 isl_map *tagged = NULL;
1225 if (data->type == isl_edge_condition ||
1226 data->type == isl_edge_conditional_validity) {
1227 if (isl_map_can_zip(map)) {
1228 tagged = isl_map_copy(map);
1229 map = isl_set_unwrap(isl_map_domain(isl_map_zip(map)));
1230 } else {
1231 tagged = insert_dummy_tags(isl_map_copy(map));
1235 src = find_domain_node(ctx, graph, map);
1236 dst = find_range_node(ctx, graph, map);
1238 if (!src || !dst)
1239 return skip_edge(map, tagged);
1241 if (src->compressed || dst->compressed) {
1242 isl_map *hull;
1243 hull = extract_hull(src, dst);
1244 if (tagged)
1245 tagged = map_intersect_domains(tagged, hull);
1246 map = isl_map_intersect(map, hull);
1249 empty = isl_map_plain_is_empty(map);
1250 if (empty < 0)
1251 goto error;
1252 if (empty)
1253 return skip_edge(map, tagged);
1255 graph->edge[graph->n_edge].src = src;
1256 graph->edge[graph->n_edge].dst = dst;
1257 graph->edge[graph->n_edge].map = map;
1258 graph->edge[graph->n_edge].types = 0;
1259 graph->edge[graph->n_edge].tagged_condition = NULL;
1260 graph->edge[graph->n_edge].tagged_validity = NULL;
1261 set_type(&graph->edge[graph->n_edge], data->type);
1262 if (data->type == isl_edge_condition)
1263 graph->edge[graph->n_edge].tagged_condition =
1264 isl_union_map_from_map(tagged);
1265 if (data->type == isl_edge_conditional_validity)
1266 graph->edge[graph->n_edge].tagged_validity =
1267 isl_union_map_from_map(tagged);
1269 edge = graph_find_matching_edge(graph, &graph->edge[graph->n_edge]);
1270 if (!edge) {
1271 graph->n_edge++;
1272 return isl_stat_error;
1274 if (edge == &graph->edge[graph->n_edge])
1275 return graph_edge_table_add(ctx, graph, data->type,
1276 &graph->edge[graph->n_edge++]);
1278 if (merge_edge(edge, &graph->edge[graph->n_edge]) < 0)
1279 return -1;
1281 return graph_edge_table_add(ctx, graph, data->type, edge);
1282 error:
1283 isl_map_free(map);
1284 isl_map_free(tagged);
1285 return isl_stat_error;
1288 /* Initialize the schedule graph "graph" from the schedule constraints "sc".
1290 * The context is included in the domain before the nodes of
1291 * the graphs are extracted in order to be able to exploit
1292 * any possible additional equalities.
1293 * Note that this intersection is only performed locally here.
1295 static isl_stat graph_init(struct isl_sched_graph *graph,
1296 __isl_keep isl_schedule_constraints *sc)
1298 isl_ctx *ctx;
1299 isl_union_set *domain;
1300 isl_union_map *c;
1301 struct isl_extract_edge_data data;
1302 enum isl_edge_type i;
1303 isl_stat r;
1305 if (!sc)
1306 return isl_stat_error;
1308 ctx = isl_schedule_constraints_get_ctx(sc);
1310 domain = isl_schedule_constraints_get_domain(sc);
1311 graph->n = isl_union_set_n_set(domain);
1312 isl_union_set_free(domain);
1314 if (graph_alloc(ctx, graph, graph->n,
1315 isl_schedule_constraints_n_map(sc)) < 0)
1316 return isl_stat_error;
1318 if (compute_max_row(graph, sc) < 0)
1319 return isl_stat_error;
1320 graph->root = 1;
1321 graph->n = 0;
1322 domain = isl_schedule_constraints_get_domain(sc);
1323 domain = isl_union_set_intersect_params(domain,
1324 isl_schedule_constraints_get_context(sc));
1325 r = isl_union_set_foreach_set(domain, &extract_node, graph);
1326 isl_union_set_free(domain);
1327 if (r < 0)
1328 return isl_stat_error;
1329 if (graph_init_table(ctx, graph) < 0)
1330 return isl_stat_error;
1331 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
1332 c = isl_schedule_constraints_get(sc, i);
1333 graph->max_edge[i] = isl_union_map_n_map(c);
1334 isl_union_map_free(c);
1335 if (!c)
1336 return isl_stat_error;
1338 if (graph_init_edge_tables(ctx, graph) < 0)
1339 return isl_stat_error;
1340 graph->n_edge = 0;
1341 data.graph = graph;
1342 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
1343 isl_stat r;
1345 data.type = i;
1346 c = isl_schedule_constraints_get(sc, i);
1347 r = isl_union_map_foreach_map(c, &extract_edge, &data);
1348 isl_union_map_free(c);
1349 if (r < 0)
1350 return isl_stat_error;
1353 return isl_stat_ok;
1356 /* Check whether there is any dependence from node[j] to node[i]
1357 * or from node[i] to node[j].
1359 static isl_bool node_follows_weak(int i, int j, void *user)
1361 isl_bool f;
1362 struct isl_sched_graph *graph = user;
1364 f = graph_has_any_edge(graph, &graph->node[j], &graph->node[i]);
1365 if (f < 0 || f)
1366 return f;
1367 return graph_has_any_edge(graph, &graph->node[i], &graph->node[j]);
1370 /* Check whether there is a (conditional) validity dependence from node[j]
1371 * to node[i], forcing node[i] to follow node[j].
1373 static isl_bool node_follows_strong(int i, int j, void *user)
1375 struct isl_sched_graph *graph = user;
1377 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
1380 /* Use Tarjan's algorithm for computing the strongly connected components
1381 * in the dependence graph only considering those edges defined by "follows".
1383 static int detect_ccs(isl_ctx *ctx, struct isl_sched_graph *graph,
1384 isl_bool (*follows)(int i, int j, void *user))
1386 int i, n;
1387 struct isl_tarjan_graph *g = NULL;
1389 g = isl_tarjan_graph_init(ctx, graph->n, follows, graph);
1390 if (!g)
1391 return -1;
1393 graph->scc = 0;
1394 i = 0;
1395 n = graph->n;
1396 while (n) {
1397 while (g->order[i] != -1) {
1398 graph->node[g->order[i]].scc = graph->scc;
1399 --n;
1400 ++i;
1402 ++i;
1403 graph->scc++;
1406 isl_tarjan_graph_free(g);
1408 return 0;
1411 /* Apply Tarjan's algorithm to detect the strongly connected components
1412 * in the dependence graph.
1413 * Only consider the (conditional) validity dependences and clear "weak".
1415 static int detect_sccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1417 graph->weak = 0;
1418 return detect_ccs(ctx, graph, &node_follows_strong);
1421 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1422 * in the dependence graph.
1423 * Consider all dependences and set "weak".
1425 static int detect_wccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1427 graph->weak = 1;
1428 return detect_ccs(ctx, graph, &node_follows_weak);
1431 static int cmp_scc(const void *a, const void *b, void *data)
1433 struct isl_sched_graph *graph = data;
1434 const int *i1 = a;
1435 const int *i2 = b;
1437 return graph->node[*i1].scc - graph->node[*i2].scc;
1440 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1442 static int sort_sccs(struct isl_sched_graph *graph)
1444 return isl_sort(graph->sorted, graph->n, sizeof(int), &cmp_scc, graph);
1447 /* Given a dependence relation R from "node" to itself,
1448 * construct the set of coefficients of valid constraints for elements
1449 * in that dependence relation.
1450 * In particular, the result contains tuples of coefficients
1451 * c_0, c_n, c_x such that
1453 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1455 * or, equivalently,
1457 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1459 * We choose here to compute the dual of delta R.
1460 * Alternatively, we could have computed the dual of R, resulting
1461 * in a set of tuples c_0, c_n, c_x, c_y, and then
1462 * plugged in (c_0, c_n, c_x, -c_x).
1464 * If "node" has been compressed, then the dependence relation
1465 * is also compressed before the set of coefficients is computed.
1467 static __isl_give isl_basic_set *intra_coefficients(
1468 struct isl_sched_graph *graph, struct isl_sched_node *node,
1469 __isl_take isl_map *map)
1471 isl_set *delta;
1472 isl_map *key;
1473 isl_basic_set *coef;
1474 isl_maybe_isl_basic_set m;
1476 m = isl_map_to_basic_set_try_get(graph->intra_hmap, map);
1477 if (m.valid < 0 || m.valid) {
1478 isl_map_free(map);
1479 return m.value;
1482 key = isl_map_copy(map);
1483 if (node->compressed) {
1484 map = isl_map_preimage_domain_multi_aff(map,
1485 isl_multi_aff_copy(node->decompress));
1486 map = isl_map_preimage_range_multi_aff(map,
1487 isl_multi_aff_copy(node->decompress));
1489 delta = isl_set_remove_divs(isl_map_deltas(map));
1490 coef = isl_set_coefficients(delta);
1491 graph->intra_hmap = isl_map_to_basic_set_set(graph->intra_hmap, key,
1492 isl_basic_set_copy(coef));
1494 return coef;
1497 /* Given a dependence relation R, construct the set of coefficients
1498 * of valid constraints for elements in that dependence relation.
1499 * In particular, the result contains tuples of coefficients
1500 * c_0, c_n, c_x, c_y such that
1502 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1504 * If the source or destination nodes of "edge" have been compressed,
1505 * then the dependence relation is also compressed before
1506 * the set of coefficients is computed.
1508 static __isl_give isl_basic_set *inter_coefficients(
1509 struct isl_sched_graph *graph, struct isl_sched_edge *edge,
1510 __isl_take isl_map *map)
1512 isl_set *set;
1513 isl_map *key;
1514 isl_basic_set *coef;
1515 isl_maybe_isl_basic_set m;
1517 m = isl_map_to_basic_set_try_get(graph->inter_hmap, map);
1518 if (m.valid < 0 || m.valid) {
1519 isl_map_free(map);
1520 return m.value;
1523 key = isl_map_copy(map);
1524 if (edge->src->compressed)
1525 map = isl_map_preimage_domain_multi_aff(map,
1526 isl_multi_aff_copy(edge->src->decompress));
1527 if (edge->dst->compressed)
1528 map = isl_map_preimage_range_multi_aff(map,
1529 isl_multi_aff_copy(edge->dst->decompress));
1530 set = isl_map_wrap(isl_map_remove_divs(map));
1531 coef = isl_set_coefficients(set);
1532 graph->inter_hmap = isl_map_to_basic_set_set(graph->inter_hmap, key,
1533 isl_basic_set_copy(coef));
1535 return coef;
1538 /* Return the position of the coefficients of the variables in
1539 * the coefficients constraints "coef".
1541 * The space of "coef" is of the form
1543 * { coefficients[[cst, params] -> S] }
1545 * Return the position of S.
1547 static int coef_var_offset(__isl_keep isl_basic_set *coef)
1549 int offset;
1550 isl_space *space;
1552 space = isl_space_unwrap(isl_basic_set_get_space(coef));
1553 offset = isl_space_dim(space, isl_dim_in);
1554 isl_space_free(space);
1556 return offset;
1559 /* Return the offset of the coefficients of the variables of "node"
1560 * within the (I)LP.
1562 * Within each node, the coefficients have the following order:
1563 * - c_i_0
1564 * - c_i_n (if parametric)
1565 * - positive and negative parts of c_i_x
1567 static int node_var_coef_offset(struct isl_sched_node *node)
1569 return node->start + 1 + node->nparam;
1572 /* Construct an isl_dim_map for mapping constraints on coefficients
1573 * for "node" to the corresponding positions in graph->lp.
1574 * "offset" is the offset of the coefficients for the variables
1575 * in the input constraints.
1576 * "s" is the sign of the mapping.
1578 * The input constraints are given in terms of the coefficients (c_0, c_n, c_x).
1579 * The mapping produced by this function essentially plugs in
1580 * (0, 0, c_i_x^+ - c_i_x^-) if s = 1 and
1581 * (0, 0, -c_i_x^+ + c_i_x^-) if s = -1.
1582 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1584 * The caller can extend the mapping to also map the other coefficients
1585 * (and therefore not plug in 0).
1587 static __isl_give isl_dim_map *intra_dim_map(isl_ctx *ctx,
1588 struct isl_sched_graph *graph, struct isl_sched_node *node,
1589 int offset, int s)
1591 int pos;
1592 unsigned total;
1593 isl_dim_map *dim_map;
1595 if (!node || !graph->lp)
1596 return NULL;
1598 total = isl_basic_set_total_dim(graph->lp);
1599 pos = node_var_coef_offset(node);
1600 dim_map = isl_dim_map_alloc(ctx, total);
1601 isl_dim_map_range(dim_map, pos, 2, offset, 1, node->nvar, -s);
1602 isl_dim_map_range(dim_map, pos + 1, 2, offset, 1, node->nvar, s);
1604 return dim_map;
1607 /* Construct an isl_dim_map for mapping constraints on coefficients
1608 * for "src" (node i) and "dst" (node j) to the corresponding positions
1609 * in graph->lp.
1610 * "offset" is the offset of the coefficients for the variables of "src"
1611 * in the input constraints.
1612 * "s" is the sign of the mapping.
1614 * The input constraints are given in terms of the coefficients
1615 * (c_0, c_n, c_x, c_y).
1616 * The mapping produced by this function essentially plugs in
1617 * (c_j_0 - c_i_0, c_j_n - c_i_n,
1618 * c_j_x^+ - c_j_x^-, -(c_i_x^+ - c_i_x^-)) if s = 1 and
1619 * (-c_j_0 + c_i_0, -c_j_n + c_i_n,
1620 * - (c_j_x^+ - c_j_x^-), c_i_x^+ - c_i_x^-) if s = -1.
1621 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1623 * The caller can further extend the mapping.
1625 static __isl_give isl_dim_map *inter_dim_map(isl_ctx *ctx,
1626 struct isl_sched_graph *graph, struct isl_sched_node *src,
1627 struct isl_sched_node *dst, int offset, int s)
1629 int pos;
1630 unsigned total;
1631 isl_dim_map *dim_map;
1633 if (!src || !dst || !graph->lp)
1634 return NULL;
1636 total = isl_basic_set_total_dim(graph->lp);
1637 dim_map = isl_dim_map_alloc(ctx, total);
1639 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, s);
1640 isl_dim_map_range(dim_map, dst->start + 1, 1, 1, 1, dst->nparam, s);
1641 pos = node_var_coef_offset(dst);
1642 isl_dim_map_range(dim_map, pos, 2, offset + src->nvar, 1,
1643 dst->nvar, -s);
1644 isl_dim_map_range(dim_map, pos + 1, 2, offset + src->nvar, 1,
1645 dst->nvar, s);
1647 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -s);
1648 isl_dim_map_range(dim_map, src->start + 1, 1, 1, 1, src->nparam, -s);
1649 pos = node_var_coef_offset(src);
1650 isl_dim_map_range(dim_map, pos, 2, offset, 1, src->nvar, s);
1651 isl_dim_map_range(dim_map, pos + 1, 2, offset, 1, src->nvar, -s);
1653 return dim_map;
1656 /* Add constraints to graph->lp that force validity for the given
1657 * dependence from a node i to itself.
1658 * That is, add constraints that enforce
1660 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1661 * = c_i_x (y - x) >= 0
1663 * for each (x,y) in R.
1664 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1665 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1666 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1667 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1669 * Actually, we do not construct constraints for the c_i_x themselves,
1670 * but for the coefficients of c_i_x written as a linear combination
1671 * of the columns in node->cmap.
1673 static isl_stat add_intra_validity_constraints(struct isl_sched_graph *graph,
1674 struct isl_sched_edge *edge)
1676 int offset;
1677 isl_map *map = isl_map_copy(edge->map);
1678 isl_ctx *ctx = isl_map_get_ctx(map);
1679 isl_dim_map *dim_map;
1680 isl_basic_set *coef;
1681 struct isl_sched_node *node = edge->src;
1683 coef = intra_coefficients(graph, node, map);
1685 offset = coef_var_offset(coef);
1687 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1688 offset, isl_mat_copy(node->cmap));
1689 if (!coef)
1690 return isl_stat_error;
1692 dim_map = intra_dim_map(ctx, graph, node, offset, 1);
1693 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1694 coef->n_eq, coef->n_ineq);
1695 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1696 coef, dim_map);
1698 return isl_stat_ok;
1701 /* Add constraints to graph->lp that force validity for the given
1702 * dependence from node i to node j.
1703 * That is, add constraints that enforce
1705 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1707 * for each (x,y) in R.
1708 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1709 * of valid constraints for R and then plug in
1710 * (c_j_0 - c_i_0, c_j_n - c_i_n, c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1711 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1712 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1714 * Actually, we do not construct constraints for the c_*_x themselves,
1715 * but for the coefficients of c_*_x written as a linear combination
1716 * of the columns in node->cmap.
1718 static isl_stat add_inter_validity_constraints(struct isl_sched_graph *graph,
1719 struct isl_sched_edge *edge)
1721 int offset;
1722 isl_map *map;
1723 isl_ctx *ctx;
1724 isl_dim_map *dim_map;
1725 isl_basic_set *coef;
1726 struct isl_sched_node *src = edge->src;
1727 struct isl_sched_node *dst = edge->dst;
1729 if (!graph->lp)
1730 return isl_stat_error;
1732 map = isl_map_copy(edge->map);
1733 ctx = isl_map_get_ctx(map);
1734 coef = inter_coefficients(graph, edge, map);
1736 offset = coef_var_offset(coef);
1738 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1739 offset, isl_mat_copy(src->cmap));
1740 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1741 offset + src->nvar, isl_mat_copy(dst->cmap));
1742 if (!coef)
1743 return isl_stat_error;
1745 dim_map = inter_dim_map(ctx, graph, src, dst, offset, 1);
1747 edge->start = graph->lp->n_ineq;
1748 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1749 coef->n_eq, coef->n_ineq);
1750 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1751 coef, dim_map);
1752 if (!graph->lp)
1753 return isl_stat_error;
1754 edge->end = graph->lp->n_ineq;
1756 return isl_stat_ok;
1759 /* Add constraints to graph->lp that bound the dependence distance for the given
1760 * dependence from a node i to itself.
1761 * If s = 1, we add the constraint
1763 * c_i_x (y - x) <= m_0 + m_n n
1765 * or
1767 * -c_i_x (y - x) + m_0 + m_n n >= 0
1769 * for each (x,y) in R.
1770 * If s = -1, we add the constraint
1772 * -c_i_x (y - x) <= m_0 + m_n n
1774 * or
1776 * c_i_x (y - x) + m_0 + m_n n >= 0
1778 * for each (x,y) in R.
1779 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1780 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1781 * with each coefficient (except m_0) represented as a pair of non-negative
1782 * coefficients.
1784 * Actually, we do not construct constraints for the c_i_x themselves,
1785 * but for the coefficients of c_i_x written as a linear combination
1786 * of the columns in node->cmap.
1789 * If "local" is set, then we add constraints
1791 * c_i_x (y - x) <= 0
1793 * or
1795 * -c_i_x (y - x) <= 0
1797 * instead, forcing the dependence distance to be (less than or) equal to 0.
1798 * That is, we plug in (0, 0, -s * c_i_x),
1799 * Note that dependences marked local are treated as validity constraints
1800 * by add_all_validity_constraints and therefore also have
1801 * their distances bounded by 0 from below.
1803 static isl_stat add_intra_proximity_constraints(struct isl_sched_graph *graph,
1804 struct isl_sched_edge *edge, int s, int local)
1806 int offset;
1807 unsigned nparam;
1808 isl_map *map = isl_map_copy(edge->map);
1809 isl_ctx *ctx = isl_map_get_ctx(map);
1810 isl_dim_map *dim_map;
1811 isl_basic_set *coef;
1812 struct isl_sched_node *node = edge->src;
1814 coef = intra_coefficients(graph, node, map);
1816 offset = coef_var_offset(coef);
1818 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1819 offset, isl_mat_copy(node->cmap));
1820 if (!coef)
1821 return isl_stat_error;
1823 nparam = isl_space_dim(node->space, isl_dim_param);
1824 dim_map = intra_dim_map(ctx, graph, node, offset, -s);
1826 if (!local) {
1827 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1828 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1829 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1831 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1832 coef->n_eq, coef->n_ineq);
1833 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1834 coef, dim_map);
1836 return isl_stat_ok;
1839 /* Add constraints to graph->lp that bound the dependence distance for the given
1840 * dependence from node i to node j.
1841 * If s = 1, we add the constraint
1843 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1844 * <= m_0 + m_n n
1846 * or
1848 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1849 * m_0 + m_n n >= 0
1851 * for each (x,y) in R.
1852 * If s = -1, we add the constraint
1854 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1855 * <= m_0 + m_n n
1857 * or
1859 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1860 * m_0 + m_n n >= 0
1862 * for each (x,y) in R.
1863 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1864 * of valid constraints for R and then plug in
1865 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1866 * -s*c_j_x+s*c_i_x)
1867 * with each coefficient (except m_0, c_*_0 and c_*_n)
1868 * represented as a pair of non-negative coefficients.
1870 * Actually, we do not construct constraints for the c_*_x themselves,
1871 * but for the coefficients of c_*_x written as a linear combination
1872 * of the columns in node->cmap.
1875 * If "local" is set, then we add constraints
1877 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1879 * or
1881 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
1883 * instead, forcing the dependence distance to be (less than or) equal to 0.
1884 * That is, we plug in
1885 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
1886 * Note that dependences marked local are treated as validity constraints
1887 * by add_all_validity_constraints and therefore also have
1888 * their distances bounded by 0 from below.
1890 static isl_stat add_inter_proximity_constraints(struct isl_sched_graph *graph,
1891 struct isl_sched_edge *edge, int s, int local)
1893 int offset;
1894 unsigned nparam;
1895 isl_map *map = isl_map_copy(edge->map);
1896 isl_ctx *ctx = isl_map_get_ctx(map);
1897 isl_dim_map *dim_map;
1898 isl_basic_set *coef;
1899 struct isl_sched_node *src = edge->src;
1900 struct isl_sched_node *dst = edge->dst;
1902 coef = inter_coefficients(graph, edge, map);
1904 offset = coef_var_offset(coef);
1906 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1907 offset, isl_mat_copy(src->cmap));
1908 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1909 offset + src->nvar, isl_mat_copy(dst->cmap));
1910 if (!coef)
1911 return isl_stat_error;
1913 nparam = isl_space_dim(src->space, isl_dim_param);
1914 dim_map = inter_dim_map(ctx, graph, src, dst, offset, -s);
1916 if (!local) {
1917 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1918 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1919 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1922 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1923 coef->n_eq, coef->n_ineq);
1924 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1925 coef, dim_map);
1927 return isl_stat_ok;
1930 /* Add all validity constraints to graph->lp.
1932 * An edge that is forced to be local needs to have its dependence
1933 * distances equal to zero. We take care of bounding them by 0 from below
1934 * here. add_all_proximity_constraints takes care of bounding them by 0
1935 * from above.
1937 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1938 * Otherwise, we ignore them.
1940 static int add_all_validity_constraints(struct isl_sched_graph *graph,
1941 int use_coincidence)
1943 int i;
1945 for (i = 0; i < graph->n_edge; ++i) {
1946 struct isl_sched_edge *edge= &graph->edge[i];
1947 int local;
1949 local = is_local(edge) ||
1950 (is_coincidence(edge) && use_coincidence);
1951 if (!is_validity(edge) && !local)
1952 continue;
1953 if (edge->src != edge->dst)
1954 continue;
1955 if (add_intra_validity_constraints(graph, edge) < 0)
1956 return -1;
1959 for (i = 0; i < graph->n_edge; ++i) {
1960 struct isl_sched_edge *edge = &graph->edge[i];
1961 int local;
1963 local = is_local(edge) ||
1964 (is_coincidence(edge) && use_coincidence);
1965 if (!is_validity(edge) && !local)
1966 continue;
1967 if (edge->src == edge->dst)
1968 continue;
1969 if (add_inter_validity_constraints(graph, edge) < 0)
1970 return -1;
1973 return 0;
1976 /* Add constraints to graph->lp that bound the dependence distance
1977 * for all dependence relations.
1978 * If a given proximity dependence is identical to a validity
1979 * dependence, then the dependence distance is already bounded
1980 * from below (by zero), so we only need to bound the distance
1981 * from above. (This includes the case of "local" dependences
1982 * which are treated as validity dependence by add_all_validity_constraints.)
1983 * Otherwise, we need to bound the distance both from above and from below.
1985 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1986 * Otherwise, we ignore them.
1988 static int add_all_proximity_constraints(struct isl_sched_graph *graph,
1989 int use_coincidence)
1991 int i;
1993 for (i = 0; i < graph->n_edge; ++i) {
1994 struct isl_sched_edge *edge= &graph->edge[i];
1995 int local;
1997 local = is_local(edge) ||
1998 (is_coincidence(edge) && use_coincidence);
1999 if (!is_proximity(edge) && !local)
2000 continue;
2001 if (edge->src == edge->dst &&
2002 add_intra_proximity_constraints(graph, edge, 1, local) < 0)
2003 return -1;
2004 if (edge->src != edge->dst &&
2005 add_inter_proximity_constraints(graph, edge, 1, local) < 0)
2006 return -1;
2007 if (is_validity(edge) || local)
2008 continue;
2009 if (edge->src == edge->dst &&
2010 add_intra_proximity_constraints(graph, edge, -1, 0) < 0)
2011 return -1;
2012 if (edge->src != edge->dst &&
2013 add_inter_proximity_constraints(graph, edge, -1, 0) < 0)
2014 return -1;
2017 return 0;
2020 /* Compute a basis for the rows in the linear part of the schedule
2021 * and extend this basis to a full basis. The remaining rows
2022 * can then be used to force linear independence from the rows
2023 * in the schedule.
2025 * In particular, given the schedule rows S, we compute
2027 * S = H Q
2028 * S U = H
2030 * with H the Hermite normal form of S. That is, all but the
2031 * first rank columns of H are zero and so each row in S is
2032 * a linear combination of the first rank rows of Q.
2033 * The matrix Q is then transposed because we will write the
2034 * coefficients of the next schedule row as a column vector s
2035 * and express this s as a linear combination s = Q c of the
2036 * computed basis.
2037 * Similarly, the matrix U is transposed such that we can
2038 * compute the coefficients c = U s from a schedule row s.
2040 static int node_update_cmap(struct isl_sched_node *node)
2042 isl_mat *H, *U, *Q;
2043 int n_row = isl_mat_rows(node->sched);
2045 H = isl_mat_sub_alloc(node->sched, 0, n_row,
2046 1 + node->nparam, node->nvar);
2048 H = isl_mat_left_hermite(H, 0, &U, &Q);
2049 isl_mat_free(node->cmap);
2050 isl_mat_free(node->cinv);
2051 isl_mat_free(node->ctrans);
2052 node->ctrans = isl_mat_copy(Q);
2053 node->cmap = isl_mat_transpose(Q);
2054 node->cinv = isl_mat_transpose(U);
2055 node->rank = isl_mat_initial_non_zero_cols(H);
2056 isl_mat_free(H);
2058 if (!node->cmap || !node->cinv || !node->ctrans || node->rank < 0)
2059 return -1;
2060 return 0;
2063 /* Is "edge" marked as a validity or a conditional validity edge?
2065 static int is_any_validity(struct isl_sched_edge *edge)
2067 return is_validity(edge) || is_conditional_validity(edge);
2070 /* How many times should we count the constraints in "edge"?
2072 * If carry is set, then we are counting the number of
2073 * (validity or conditional validity) constraints that will be added
2074 * in setup_carry_lp and we count each edge exactly once.
2076 * Otherwise, we count as follows
2077 * validity -> 1 (>= 0)
2078 * validity+proximity -> 2 (>= 0 and upper bound)
2079 * proximity -> 2 (lower and upper bound)
2080 * local(+any) -> 2 (>= 0 and <= 0)
2082 * If an edge is only marked conditional_validity then it counts
2083 * as zero since it is only checked afterwards.
2085 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2086 * Otherwise, we ignore them.
2088 static int edge_multiplicity(struct isl_sched_edge *edge, int carry,
2089 int use_coincidence)
2091 if (carry)
2092 return 1;
2093 if (is_proximity(edge) || is_local(edge))
2094 return 2;
2095 if (use_coincidence && is_coincidence(edge))
2096 return 2;
2097 if (is_validity(edge))
2098 return 1;
2099 return 0;
2102 /* Count the number of equality and inequality constraints
2103 * that will be added for the given map.
2105 * "use_coincidence" is set if we should take into account coincidence edges.
2107 static int count_map_constraints(struct isl_sched_graph *graph,
2108 struct isl_sched_edge *edge, __isl_take isl_map *map,
2109 int *n_eq, int *n_ineq, int carry, int use_coincidence)
2111 isl_basic_set *coef;
2112 int f = edge_multiplicity(edge, carry, use_coincidence);
2114 if (f == 0) {
2115 isl_map_free(map);
2116 return 0;
2119 if (edge->src == edge->dst)
2120 coef = intra_coefficients(graph, edge->src, map);
2121 else
2122 coef = inter_coefficients(graph, edge, map);
2123 if (!coef)
2124 return -1;
2125 *n_eq += f * coef->n_eq;
2126 *n_ineq += f * coef->n_ineq;
2127 isl_basic_set_free(coef);
2129 return 0;
2132 /* Count the number of equality and inequality constraints
2133 * that will be added to the main lp problem.
2134 * We count as follows
2135 * validity -> 1 (>= 0)
2136 * validity+proximity -> 2 (>= 0 and upper bound)
2137 * proximity -> 2 (lower and upper bound)
2138 * local(+any) -> 2 (>= 0 and <= 0)
2140 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2141 * Otherwise, we ignore them.
2143 static int count_constraints(struct isl_sched_graph *graph,
2144 int *n_eq, int *n_ineq, int use_coincidence)
2146 int i;
2148 *n_eq = *n_ineq = 0;
2149 for (i = 0; i < graph->n_edge; ++i) {
2150 struct isl_sched_edge *edge= &graph->edge[i];
2151 isl_map *map = isl_map_copy(edge->map);
2153 if (count_map_constraints(graph, edge, map, n_eq, n_ineq,
2154 0, use_coincidence) < 0)
2155 return -1;
2158 return 0;
2161 /* Count the number of constraints that will be added by
2162 * add_bound_constant_constraints to bound the values of the constant terms
2163 * and increment *n_eq and *n_ineq accordingly.
2165 * In practice, add_bound_constant_constraints only adds inequalities.
2167 static isl_stat count_bound_constant_constraints(isl_ctx *ctx,
2168 struct isl_sched_graph *graph, int *n_eq, int *n_ineq)
2170 if (isl_options_get_schedule_max_constant_term(ctx) == -1)
2171 return isl_stat_ok;
2173 *n_ineq += graph->n;
2175 return isl_stat_ok;
2178 /* Add constraints to bound the values of the constant terms in the schedule,
2179 * if requested by the user.
2181 * The maximal value of the constant terms is defined by the option
2182 * "schedule_max_constant_term".
2184 * Within each node, the coefficients have the following order:
2185 * - c_i_0
2186 * - c_i_n (if parametric)
2187 * - positive and negative parts of c_i_x
2189 static isl_stat add_bound_constant_constraints(isl_ctx *ctx,
2190 struct isl_sched_graph *graph)
2192 int i, k;
2193 int max;
2194 int total;
2196 max = isl_options_get_schedule_max_constant_term(ctx);
2197 if (max == -1)
2198 return isl_stat_ok;
2200 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2202 for (i = 0; i < graph->n; ++i) {
2203 struct isl_sched_node *node = &graph->node[i];
2204 k = isl_basic_set_alloc_inequality(graph->lp);
2205 if (k < 0)
2206 return isl_stat_error;
2207 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2208 isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
2209 isl_int_set_si(graph->lp->ineq[k][0], max);
2212 return isl_stat_ok;
2215 /* Count the number of constraints that will be added by
2216 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
2217 * accordingly.
2219 * In practice, add_bound_coefficient_constraints only adds inequalities.
2221 static int count_bound_coefficient_constraints(isl_ctx *ctx,
2222 struct isl_sched_graph *graph, int *n_eq, int *n_ineq)
2224 int i;
2226 if (isl_options_get_schedule_max_coefficient(ctx) == -1 &&
2227 !isl_options_get_schedule_treat_coalescing(ctx))
2228 return 0;
2230 for (i = 0; i < graph->n; ++i)
2231 *n_ineq += graph->node[i].nparam + 2 * graph->node[i].nvar;
2233 return 0;
2236 /* Add constraints to graph->lp that bound the values of
2237 * the parameter schedule coefficients of "node" to "max" and
2238 * the variable schedule coefficients to the corresponding entry
2239 * in node->max.
2240 * In either case, a negative value means that no bound needs to be imposed.
2242 * For parameter coefficients, this amounts to adding a constraint
2244 * c_n <= max
2246 * i.e.,
2248 * -c_n + max >= 0
2250 * The variables coefficients are, however, not represented directly.
2251 * Instead, the variables coefficients c_x are written as a linear
2252 * combination c_x = cmap c_z of some other coefficients c_z,
2253 * which are in turn encoded as c_z = c_z^+ - c_z^-.
2254 * Let a_j be the elements of row i of node->cmap, then
2256 * -max_i <= c_x_i <= max_i
2258 * is encoded as
2260 * -max_i <= \sum_j a_j (c_z_j^+ - c_z_j^-) <= max_i
2262 * or
2264 * -\sum_j a_j (c_z_j^+ - c_z_j^-) + max_i >= 0
2265 * \sum_j a_j (c_z_j^+ - c_z_j^-) + max_i >= 0
2267 static isl_stat node_add_coefficient_constraints(isl_ctx *ctx,
2268 struct isl_sched_graph *graph, struct isl_sched_node *node, int max)
2270 int i, j, k;
2271 int total;
2272 isl_vec *ineq;
2274 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2276 for (j = 0; j < node->nparam; ++j) {
2277 int dim;
2279 if (max < 0)
2280 continue;
2282 k = isl_basic_set_alloc_inequality(graph->lp);
2283 if (k < 0)
2284 return isl_stat_error;
2285 dim = 1 + node->start + 1 + j;
2286 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2287 isl_int_set_si(graph->lp->ineq[k][dim], -1);
2288 isl_int_set_si(graph->lp->ineq[k][0], max);
2291 ineq = isl_vec_alloc(ctx, 1 + total);
2292 ineq = isl_vec_clr(ineq);
2293 if (!ineq)
2294 return isl_stat_error;
2295 for (i = 0; i < node->nvar; ++i) {
2296 int pos = 1 + node_var_coef_offset(node);
2298 if (isl_int_is_neg(node->max->el[i]))
2299 continue;
2301 for (j = 0; j < node->nvar; ++j) {
2302 isl_int_set(ineq->el[pos + 2 * j],
2303 node->cmap->row[i][j]);
2304 isl_int_neg(ineq->el[pos + 2 * j + 1],
2305 node->cmap->row[i][j]);
2307 isl_int_set(ineq->el[0], node->max->el[i]);
2309 k = isl_basic_set_alloc_inequality(graph->lp);
2310 if (k < 0)
2311 goto error;
2312 isl_seq_cpy(graph->lp->ineq[k], ineq->el, 1 + total);
2314 isl_seq_neg(ineq->el + pos, ineq->el + pos, 2 * node->nvar);
2315 k = isl_basic_set_alloc_inequality(graph->lp);
2316 if (k < 0)
2317 goto error;
2318 isl_seq_cpy(graph->lp->ineq[k], ineq->el, 1 + total);
2320 isl_vec_free(ineq);
2322 return isl_stat_ok;
2323 error:
2324 isl_vec_free(ineq);
2325 return isl_stat_error;
2328 /* Add constraints that bound the values of the variable and parameter
2329 * coefficients of the schedule.
2331 * The maximal value of the coefficients is defined by the option
2332 * 'schedule_max_coefficient' and the entries in node->max.
2333 * These latter entries are only set if either the schedule_max_coefficient
2334 * option or the schedule_treat_coalescing option is set.
2336 static isl_stat add_bound_coefficient_constraints(isl_ctx *ctx,
2337 struct isl_sched_graph *graph)
2339 int i;
2340 int max;
2342 max = isl_options_get_schedule_max_coefficient(ctx);
2344 if (max == -1 && !isl_options_get_schedule_treat_coalescing(ctx))
2345 return isl_stat_ok;
2347 for (i = 0; i < graph->n; ++i) {
2348 struct isl_sched_node *node = &graph->node[i];
2350 if (node_add_coefficient_constraints(ctx, graph, node, max) < 0)
2351 return isl_stat_error;
2354 return isl_stat_ok;
2357 /* Add a constraint to graph->lp that equates the value at position
2358 * "sum_pos" to the sum of the "n" values starting at "first".
2360 static isl_stat add_sum_constraint(struct isl_sched_graph *graph,
2361 int sum_pos, int first, int n)
2363 int i, k;
2364 int total;
2366 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2368 k = isl_basic_set_alloc_equality(graph->lp);
2369 if (k < 0)
2370 return isl_stat_error;
2371 isl_seq_clr(graph->lp->eq[k], 1 + total);
2372 isl_int_set_si(graph->lp->eq[k][1 + sum_pos], -1);
2373 for (i = 0; i < n; ++i)
2374 isl_int_set_si(graph->lp->eq[k][1 + first + i], 1);
2376 return isl_stat_ok;
2379 /* Add a constraint to graph->lp that equates the value at position
2380 * "sum_pos" to the sum of the parameter coefficients of all nodes.
2382 * Within each node, the coefficients have the following order:
2383 * - c_i_0
2384 * - c_i_n (if parametric)
2385 * - positive and negative parts of c_i_x
2387 static isl_stat add_param_sum_constraint(struct isl_sched_graph *graph,
2388 int sum_pos)
2390 int i, j, k;
2391 int total;
2393 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2395 k = isl_basic_set_alloc_equality(graph->lp);
2396 if (k < 0)
2397 return isl_stat_error;
2398 isl_seq_clr(graph->lp->eq[k], 1 + total);
2399 isl_int_set_si(graph->lp->eq[k][1 + sum_pos], -1);
2400 for (i = 0; i < graph->n; ++i) {
2401 int pos = 1 + graph->node[i].start + 1;
2403 for (j = 0; j < graph->node[i].nparam; ++j)
2404 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2407 return isl_stat_ok;
2410 /* Add a constraint to graph->lp that equates the value at position
2411 * "sum_pos" to the sum of the variable coefficients of all nodes.
2413 * Within each node, the coefficients have the following order:
2414 * - c_i_0
2415 * - c_i_n (if parametric)
2416 * - positive and negative parts of c_i_x
2418 static isl_stat add_var_sum_constraint(struct isl_sched_graph *graph,
2419 int sum_pos)
2421 int i, j, k;
2422 int total;
2424 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2426 k = isl_basic_set_alloc_equality(graph->lp);
2427 if (k < 0)
2428 return isl_stat_error;
2429 isl_seq_clr(graph->lp->eq[k], 1 + total);
2430 isl_int_set_si(graph->lp->eq[k][1 + sum_pos], -1);
2431 for (i = 0; i < graph->n; ++i) {
2432 struct isl_sched_node *node = &graph->node[i];
2433 int pos = 1 + node_var_coef_offset(node);
2435 for (j = 0; j < 2 * node->nvar; ++j)
2436 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2439 return isl_stat_ok;
2442 /* Construct an ILP problem for finding schedule coefficients
2443 * that result in non-negative, but small dependence distances
2444 * over all dependences.
2445 * In particular, the dependence distances over proximity edges
2446 * are bounded by m_0 + m_n n and we compute schedule coefficients
2447 * with small values (preferably zero) of m_n and m_0.
2449 * All variables of the ILP are non-negative. The actual coefficients
2450 * may be negative, so each coefficient is represented as the difference
2451 * of two non-negative variables. The negative part always appears
2452 * immediately before the positive part.
2453 * Other than that, the variables have the following order
2455 * - sum of positive and negative parts of m_n coefficients
2456 * - m_0
2457 * - sum of all c_n coefficients
2458 * (unconstrained when computing non-parametric schedules)
2459 * - sum of positive and negative parts of all c_x coefficients
2460 * - positive and negative parts of m_n coefficients
2461 * - for each node
2462 * - c_i_0
2463 * - c_i_n (if parametric)
2464 * - positive and negative parts of c_i_x
2466 * The c_i_x are not represented directly, but through the columns of
2467 * node->cmap. That is, the computed values are for variable t_i_x
2468 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
2470 * The constraints are those from the edges plus two or three equalities
2471 * to express the sums.
2473 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2474 * Otherwise, we ignore them.
2476 static isl_stat setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
2477 int use_coincidence)
2479 int i;
2480 unsigned nparam;
2481 unsigned total;
2482 isl_space *space;
2483 int parametric;
2484 int param_pos;
2485 int n_eq, n_ineq;
2487 parametric = ctx->opt->schedule_parametric;
2488 nparam = isl_space_dim(graph->node[0].space, isl_dim_param);
2489 param_pos = 4;
2490 total = param_pos + 2 * nparam;
2491 for (i = 0; i < graph->n; ++i) {
2492 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2493 if (node_update_cmap(node) < 0)
2494 return isl_stat_error;
2495 node->start = total;
2496 total += 1 + node->nparam + 2 * node->nvar;
2499 if (count_constraints(graph, &n_eq, &n_ineq, use_coincidence) < 0)
2500 return isl_stat_error;
2501 if (count_bound_constant_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
2502 return isl_stat_error;
2503 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
2504 return isl_stat_error;
2506 space = isl_space_set_alloc(ctx, 0, total);
2507 isl_basic_set_free(graph->lp);
2508 n_eq += 2 + parametric;
2510 graph->lp = isl_basic_set_alloc_space(space, 0, n_eq, n_ineq);
2512 if (add_sum_constraint(graph, 0, param_pos, 2 * nparam) < 0)
2513 return isl_stat_error;
2514 if (parametric && add_param_sum_constraint(graph, 2) < 0)
2515 return isl_stat_error;
2516 if (add_var_sum_constraint(graph, 3) < 0)
2517 return isl_stat_error;
2518 if (add_bound_constant_constraints(ctx, graph) < 0)
2519 return isl_stat_error;
2520 if (add_bound_coefficient_constraints(ctx, graph) < 0)
2521 return isl_stat_error;
2522 if (add_all_validity_constraints(graph, use_coincidence) < 0)
2523 return isl_stat_error;
2524 if (add_all_proximity_constraints(graph, use_coincidence) < 0)
2525 return isl_stat_error;
2527 return isl_stat_ok;
2530 /* Analyze the conflicting constraint found by
2531 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2532 * constraint of one of the edges between distinct nodes, living, moreover
2533 * in distinct SCCs, then record the source and sink SCC as this may
2534 * be a good place to cut between SCCs.
2536 static int check_conflict(int con, void *user)
2538 int i;
2539 struct isl_sched_graph *graph = user;
2541 if (graph->src_scc >= 0)
2542 return 0;
2544 con -= graph->lp->n_eq;
2546 if (con >= graph->lp->n_ineq)
2547 return 0;
2549 for (i = 0; i < graph->n_edge; ++i) {
2550 if (!is_validity(&graph->edge[i]))
2551 continue;
2552 if (graph->edge[i].src == graph->edge[i].dst)
2553 continue;
2554 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
2555 continue;
2556 if (graph->edge[i].start > con)
2557 continue;
2558 if (graph->edge[i].end <= con)
2559 continue;
2560 graph->src_scc = graph->edge[i].src->scc;
2561 graph->dst_scc = graph->edge[i].dst->scc;
2564 return 0;
2567 /* Check whether the next schedule row of the given node needs to be
2568 * non-trivial. Lower-dimensional domains may have some trivial rows,
2569 * but as soon as the number of remaining required non-trivial rows
2570 * is as large as the number or remaining rows to be computed,
2571 * all remaining rows need to be non-trivial.
2573 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
2575 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
2578 /* Solve the ILP problem constructed in setup_lp.
2579 * For each node such that all the remaining rows of its schedule
2580 * need to be non-trivial, we construct a non-triviality region.
2581 * This region imposes that the next row is independent of previous rows.
2582 * In particular the coefficients c_i_x are represented by t_i_x
2583 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
2584 * its first columns span the rows of the previously computed part
2585 * of the schedule. The non-triviality region enforces that at least
2586 * one of the remaining components of t_i_x is non-zero, i.e.,
2587 * that the new schedule row depends on at least one of the remaining
2588 * columns of Q.
2590 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
2592 int i;
2593 isl_vec *sol;
2594 isl_basic_set *lp;
2596 for (i = 0; i < graph->n; ++i) {
2597 struct isl_sched_node *node = &graph->node[i];
2598 int skip = node->rank;
2599 graph->region[i].pos = node_var_coef_offset(node) + 2 * skip;
2600 if (needs_row(graph, node))
2601 graph->region[i].len = 2 * (node->nvar - skip);
2602 else
2603 graph->region[i].len = 0;
2605 lp = isl_basic_set_copy(graph->lp);
2606 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
2607 graph->region, &check_conflict, graph);
2608 return sol;
2611 /* Extract the coefficients for the variables of "node" from "sol".
2613 * Within each node, the coefficients have the following order:
2614 * - c_i_0
2615 * - c_i_n (if parametric)
2616 * - positive and negative parts of c_i_x
2618 * The c_i_x^- appear before their c_i_x^+ counterpart.
2620 * Return c_i_x = c_i_x^+ - c_i_x^-
2622 static __isl_give isl_vec *extract_var_coef(struct isl_sched_node *node,
2623 __isl_keep isl_vec *sol)
2625 int i;
2626 int pos;
2627 isl_vec *csol;
2629 if (!sol)
2630 return NULL;
2631 csol = isl_vec_alloc(isl_vec_get_ctx(sol), node->nvar);
2632 if (!csol)
2633 return NULL;
2635 pos = 1 + node_var_coef_offset(node);
2636 for (i = 0; i < node->nvar; ++i)
2637 isl_int_sub(csol->el[i],
2638 sol->el[pos + 2 * i + 1], sol->el[pos + 2 * i]);
2640 return csol;
2643 /* Update the schedules of all nodes based on the given solution
2644 * of the LP problem.
2645 * The new row is added to the current band.
2646 * All possibly negative coefficients are encoded as a difference
2647 * of two non-negative variables, so we need to perform the subtraction
2648 * here. Moreover, if use_cmap is set, then the solution does
2649 * not refer to the actual coefficients c_i_x, but instead to variables
2650 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2651 * In this case, we then also need to perform this multiplication
2652 * to obtain the values of c_i_x.
2654 * If coincident is set, then the caller guarantees that the new
2655 * row satisfies the coincidence constraints.
2657 static int update_schedule(struct isl_sched_graph *graph,
2658 __isl_take isl_vec *sol, int use_cmap, int coincident)
2660 int i, j;
2661 isl_vec *csol = NULL;
2663 if (!sol)
2664 goto error;
2665 if (sol->size == 0)
2666 isl_die(sol->ctx, isl_error_internal,
2667 "no solution found", goto error);
2668 if (graph->n_total_row >= graph->max_row)
2669 isl_die(sol->ctx, isl_error_internal,
2670 "too many schedule rows", goto error);
2672 for (i = 0; i < graph->n; ++i) {
2673 struct isl_sched_node *node = &graph->node[i];
2674 int pos = node->start;
2675 int row = isl_mat_rows(node->sched);
2677 isl_vec_free(csol);
2678 csol = extract_var_coef(node, sol);
2679 if (!csol)
2680 goto error;
2682 isl_map_free(node->sched_map);
2683 node->sched_map = NULL;
2684 node->sched = isl_mat_add_rows(node->sched, 1);
2685 if (!node->sched)
2686 goto error;
2687 for (j = 0; j < 1 + node->nparam; ++j)
2688 node->sched = isl_mat_set_element(node->sched,
2689 row, j, sol->el[1 + pos + j]);
2690 if (use_cmap)
2691 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
2692 csol);
2693 if (!csol)
2694 goto error;
2695 for (j = 0; j < node->nvar; ++j)
2696 node->sched = isl_mat_set_element(node->sched,
2697 row, 1 + node->nparam + j, csol->el[j]);
2698 node->coincident[graph->n_total_row] = coincident;
2700 isl_vec_free(sol);
2701 isl_vec_free(csol);
2703 graph->n_row++;
2704 graph->n_total_row++;
2706 return 0;
2707 error:
2708 isl_vec_free(sol);
2709 isl_vec_free(csol);
2710 return -1;
2713 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2714 * and return this isl_aff.
2716 static __isl_give isl_aff *extract_schedule_row(__isl_take isl_local_space *ls,
2717 struct isl_sched_node *node, int row)
2719 int j;
2720 isl_int v;
2721 isl_aff *aff;
2723 isl_int_init(v);
2725 aff = isl_aff_zero_on_domain(ls);
2726 isl_mat_get_element(node->sched, row, 0, &v);
2727 aff = isl_aff_set_constant(aff, v);
2728 for (j = 0; j < node->nparam; ++j) {
2729 isl_mat_get_element(node->sched, row, 1 + j, &v);
2730 aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
2732 for (j = 0; j < node->nvar; ++j) {
2733 isl_mat_get_element(node->sched, row, 1 + node->nparam + j, &v);
2734 aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
2737 isl_int_clear(v);
2739 return aff;
2742 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
2743 * and return this multi_aff.
2745 * The result is defined over the uncompressed node domain.
2747 static __isl_give isl_multi_aff *node_extract_partial_schedule_multi_aff(
2748 struct isl_sched_node *node, int first, int n)
2750 int i;
2751 isl_space *space;
2752 isl_local_space *ls;
2753 isl_aff *aff;
2754 isl_multi_aff *ma;
2755 int nrow;
2757 if (!node)
2758 return NULL;
2759 nrow = isl_mat_rows(node->sched);
2760 if (node->compressed)
2761 space = isl_multi_aff_get_domain_space(node->decompress);
2762 else
2763 space = isl_space_copy(node->space);
2764 ls = isl_local_space_from_space(isl_space_copy(space));
2765 space = isl_space_from_domain(space);
2766 space = isl_space_add_dims(space, isl_dim_out, n);
2767 ma = isl_multi_aff_zero(space);
2769 for (i = first; i < first + n; ++i) {
2770 aff = extract_schedule_row(isl_local_space_copy(ls), node, i);
2771 ma = isl_multi_aff_set_aff(ma, i - first, aff);
2774 isl_local_space_free(ls);
2776 if (node->compressed)
2777 ma = isl_multi_aff_pullback_multi_aff(ma,
2778 isl_multi_aff_copy(node->compress));
2780 return ma;
2783 /* Convert node->sched into a multi_aff and return this multi_aff.
2785 * The result is defined over the uncompressed node domain.
2787 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
2788 struct isl_sched_node *node)
2790 int nrow;
2792 nrow = isl_mat_rows(node->sched);
2793 return node_extract_partial_schedule_multi_aff(node, 0, nrow);
2796 /* Convert node->sched into a map and return this map.
2798 * The result is cached in node->sched_map, which needs to be released
2799 * whenever node->sched is updated.
2800 * It is defined over the uncompressed node domain.
2802 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
2804 if (!node->sched_map) {
2805 isl_multi_aff *ma;
2807 ma = node_extract_schedule_multi_aff(node);
2808 node->sched_map = isl_map_from_multi_aff(ma);
2811 return isl_map_copy(node->sched_map);
2814 /* Construct a map that can be used to update a dependence relation
2815 * based on the current schedule.
2816 * That is, construct a map expressing that source and sink
2817 * are executed within the same iteration of the current schedule.
2818 * This map can then be intersected with the dependence relation.
2819 * This is not the most efficient way, but this shouldn't be a critical
2820 * operation.
2822 static __isl_give isl_map *specializer(struct isl_sched_node *src,
2823 struct isl_sched_node *dst)
2825 isl_map *src_sched, *dst_sched;
2827 src_sched = node_extract_schedule(src);
2828 dst_sched = node_extract_schedule(dst);
2829 return isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
2832 /* Intersect the domains of the nested relations in domain and range
2833 * of "umap" with "map".
2835 static __isl_give isl_union_map *intersect_domains(
2836 __isl_take isl_union_map *umap, __isl_keep isl_map *map)
2838 isl_union_set *uset;
2840 umap = isl_union_map_zip(umap);
2841 uset = isl_union_set_from_set(isl_map_wrap(isl_map_copy(map)));
2842 umap = isl_union_map_intersect_domain(umap, uset);
2843 umap = isl_union_map_zip(umap);
2844 return umap;
2847 /* Update the dependence relation of the given edge based
2848 * on the current schedule.
2849 * If the dependence is carried completely by the current schedule, then
2850 * it is removed from the edge_tables. It is kept in the list of edges
2851 * as otherwise all edge_tables would have to be recomputed.
2853 * If the edge is of a type that can appear multiple times
2854 * between the same pair of nodes, then it is added to
2855 * the edge table (again). This prevents the situation
2856 * where none of these edges is referenced from the edge table
2857 * because the one that was referenced turned out to be empty and
2858 * was therefore removed from the table.
2860 static int update_edge(isl_ctx *ctx, struct isl_sched_graph *graph,
2861 struct isl_sched_edge *edge)
2863 int empty;
2864 isl_map *id;
2866 id = specializer(edge->src, edge->dst);
2867 edge->map = isl_map_intersect(edge->map, isl_map_copy(id));
2868 if (!edge->map)
2869 goto error;
2871 if (edge->tagged_condition) {
2872 edge->tagged_condition =
2873 intersect_domains(edge->tagged_condition, id);
2874 if (!edge->tagged_condition)
2875 goto error;
2877 if (edge->tagged_validity) {
2878 edge->tagged_validity =
2879 intersect_domains(edge->tagged_validity, id);
2880 if (!edge->tagged_validity)
2881 goto error;
2884 empty = isl_map_plain_is_empty(edge->map);
2885 if (empty < 0)
2886 goto error;
2887 if (empty) {
2888 graph_remove_edge(graph, edge);
2889 } else if (is_multi_edge_type(edge)) {
2890 if (graph_edge_tables_add(ctx, graph, edge) < 0)
2891 goto error;
2894 isl_map_free(id);
2895 return 0;
2896 error:
2897 isl_map_free(id);
2898 return -1;
2901 /* Does the domain of "umap" intersect "uset"?
2903 static int domain_intersects(__isl_keep isl_union_map *umap,
2904 __isl_keep isl_union_set *uset)
2906 int empty;
2908 umap = isl_union_map_copy(umap);
2909 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(uset));
2910 empty = isl_union_map_is_empty(umap);
2911 isl_union_map_free(umap);
2913 return empty < 0 ? -1 : !empty;
2916 /* Does the range of "umap" intersect "uset"?
2918 static int range_intersects(__isl_keep isl_union_map *umap,
2919 __isl_keep isl_union_set *uset)
2921 int empty;
2923 umap = isl_union_map_copy(umap);
2924 umap = isl_union_map_intersect_range(umap, isl_union_set_copy(uset));
2925 empty = isl_union_map_is_empty(umap);
2926 isl_union_map_free(umap);
2928 return empty < 0 ? -1 : !empty;
2931 /* Are the condition dependences of "edge" local with respect to
2932 * the current schedule?
2934 * That is, are domain and range of the condition dependences mapped
2935 * to the same point?
2937 * In other words, is the condition false?
2939 static int is_condition_false(struct isl_sched_edge *edge)
2941 isl_union_map *umap;
2942 isl_map *map, *sched, *test;
2943 int empty, local;
2945 empty = isl_union_map_is_empty(edge->tagged_condition);
2946 if (empty < 0 || empty)
2947 return empty;
2949 umap = isl_union_map_copy(edge->tagged_condition);
2950 umap = isl_union_map_zip(umap);
2951 umap = isl_union_set_unwrap(isl_union_map_domain(umap));
2952 map = isl_map_from_union_map(umap);
2954 sched = node_extract_schedule(edge->src);
2955 map = isl_map_apply_domain(map, sched);
2956 sched = node_extract_schedule(edge->dst);
2957 map = isl_map_apply_range(map, sched);
2959 test = isl_map_identity(isl_map_get_space(map));
2960 local = isl_map_is_subset(map, test);
2961 isl_map_free(map);
2962 isl_map_free(test);
2964 return local;
2967 /* For each conditional validity constraint that is adjacent
2968 * to a condition with domain in condition_source or range in condition_sink,
2969 * turn it into an unconditional validity constraint.
2971 static int unconditionalize_adjacent_validity(struct isl_sched_graph *graph,
2972 __isl_take isl_union_set *condition_source,
2973 __isl_take isl_union_set *condition_sink)
2975 int i;
2977 condition_source = isl_union_set_coalesce(condition_source);
2978 condition_sink = isl_union_set_coalesce(condition_sink);
2980 for (i = 0; i < graph->n_edge; ++i) {
2981 int adjacent;
2982 isl_union_map *validity;
2984 if (!is_conditional_validity(&graph->edge[i]))
2985 continue;
2986 if (is_validity(&graph->edge[i]))
2987 continue;
2989 validity = graph->edge[i].tagged_validity;
2990 adjacent = domain_intersects(validity, condition_sink);
2991 if (adjacent >= 0 && !adjacent)
2992 adjacent = range_intersects(validity, condition_source);
2993 if (adjacent < 0)
2994 goto error;
2995 if (!adjacent)
2996 continue;
2998 set_validity(&graph->edge[i]);
3001 isl_union_set_free(condition_source);
3002 isl_union_set_free(condition_sink);
3003 return 0;
3004 error:
3005 isl_union_set_free(condition_source);
3006 isl_union_set_free(condition_sink);
3007 return -1;
3010 /* Update the dependence relations of all edges based on the current schedule
3011 * and enforce conditional validity constraints that are adjacent
3012 * to satisfied condition constraints.
3014 * First check if any of the condition constraints are satisfied
3015 * (i.e., not local to the outer schedule) and keep track of
3016 * their domain and range.
3017 * Then update all dependence relations (which removes the non-local
3018 * constraints).
3019 * Finally, if any condition constraints turned out to be satisfied,
3020 * then turn all adjacent conditional validity constraints into
3021 * unconditional validity constraints.
3023 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
3025 int i;
3026 int any = 0;
3027 isl_union_set *source, *sink;
3029 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3030 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3031 for (i = 0; i < graph->n_edge; ++i) {
3032 int local;
3033 isl_union_set *uset;
3034 isl_union_map *umap;
3036 if (!is_condition(&graph->edge[i]))
3037 continue;
3038 if (is_local(&graph->edge[i]))
3039 continue;
3040 local = is_condition_false(&graph->edge[i]);
3041 if (local < 0)
3042 goto error;
3043 if (local)
3044 continue;
3046 any = 1;
3048 umap = isl_union_map_copy(graph->edge[i].tagged_condition);
3049 uset = isl_union_map_domain(umap);
3050 source = isl_union_set_union(source, uset);
3052 umap = isl_union_map_copy(graph->edge[i].tagged_condition);
3053 uset = isl_union_map_range(umap);
3054 sink = isl_union_set_union(sink, uset);
3057 for (i = 0; i < graph->n_edge; ++i) {
3058 if (update_edge(ctx, graph, &graph->edge[i]) < 0)
3059 goto error;
3062 if (any)
3063 return unconditionalize_adjacent_validity(graph, source, sink);
3065 isl_union_set_free(source);
3066 isl_union_set_free(sink);
3067 return 0;
3068 error:
3069 isl_union_set_free(source);
3070 isl_union_set_free(sink);
3071 return -1;
3074 static void next_band(struct isl_sched_graph *graph)
3076 graph->band_start = graph->n_total_row;
3079 /* Return the union of the universe domains of the nodes in "graph"
3080 * that satisfy "pred".
3082 static __isl_give isl_union_set *isl_sched_graph_domain(isl_ctx *ctx,
3083 struct isl_sched_graph *graph,
3084 int (*pred)(struct isl_sched_node *node, int data), int data)
3086 int i;
3087 isl_set *set;
3088 isl_union_set *dom;
3090 for (i = 0; i < graph->n; ++i)
3091 if (pred(&graph->node[i], data))
3092 break;
3094 if (i >= graph->n)
3095 isl_die(ctx, isl_error_internal,
3096 "empty component", return NULL);
3098 set = isl_set_universe(isl_space_copy(graph->node[i].space));
3099 dom = isl_union_set_from_set(set);
3101 for (i = i + 1; i < graph->n; ++i) {
3102 if (!pred(&graph->node[i], data))
3103 continue;
3104 set = isl_set_universe(isl_space_copy(graph->node[i].space));
3105 dom = isl_union_set_union(dom, isl_union_set_from_set(set));
3108 return dom;
3111 /* Return a list of unions of universe domains, where each element
3112 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
3114 static __isl_give isl_union_set_list *extract_sccs(isl_ctx *ctx,
3115 struct isl_sched_graph *graph)
3117 int i;
3118 isl_union_set_list *filters;
3120 filters = isl_union_set_list_alloc(ctx, graph->scc);
3121 for (i = 0; i < graph->scc; ++i) {
3122 isl_union_set *dom;
3124 dom = isl_sched_graph_domain(ctx, graph, &node_scc_exactly, i);
3125 filters = isl_union_set_list_add(filters, dom);
3128 return filters;
3131 /* Return a list of two unions of universe domains, one for the SCCs up
3132 * to and including graph->src_scc and another for the other SCCs.
3134 static __isl_give isl_union_set_list *extract_split(isl_ctx *ctx,
3135 struct isl_sched_graph *graph)
3137 isl_union_set *dom;
3138 isl_union_set_list *filters;
3140 filters = isl_union_set_list_alloc(ctx, 2);
3141 dom = isl_sched_graph_domain(ctx, graph,
3142 &node_scc_at_most, graph->src_scc);
3143 filters = isl_union_set_list_add(filters, dom);
3144 dom = isl_sched_graph_domain(ctx, graph,
3145 &node_scc_at_least, graph->src_scc + 1);
3146 filters = isl_union_set_list_add(filters, dom);
3148 return filters;
3151 /* Copy nodes that satisfy node_pred from the src dependence graph
3152 * to the dst dependence graph.
3154 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
3155 int (*node_pred)(struct isl_sched_node *node, int data), int data)
3157 int i;
3159 dst->n = 0;
3160 for (i = 0; i < src->n; ++i) {
3161 int j;
3163 if (!node_pred(&src->node[i], data))
3164 continue;
3166 j = dst->n;
3167 dst->node[j].space = isl_space_copy(src->node[i].space);
3168 dst->node[j].compressed = src->node[i].compressed;
3169 dst->node[j].hull = isl_set_copy(src->node[i].hull);
3170 dst->node[j].compress =
3171 isl_multi_aff_copy(src->node[i].compress);
3172 dst->node[j].decompress =
3173 isl_multi_aff_copy(src->node[i].decompress);
3174 dst->node[j].nvar = src->node[i].nvar;
3175 dst->node[j].nparam = src->node[i].nparam;
3176 dst->node[j].sched = isl_mat_copy(src->node[i].sched);
3177 dst->node[j].sched_map = isl_map_copy(src->node[i].sched_map);
3178 dst->node[j].coincident = src->node[i].coincident;
3179 dst->node[j].sizes = isl_multi_val_copy(src->node[i].sizes);
3180 dst->node[j].max = isl_vec_copy(src->node[i].max);
3181 dst->n++;
3183 if (!dst->node[j].space || !dst->node[j].sched)
3184 return -1;
3185 if (dst->node[j].compressed &&
3186 (!dst->node[j].hull || !dst->node[j].compress ||
3187 !dst->node[j].decompress))
3188 return -1;
3191 return 0;
3194 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
3195 * to the dst dependence graph.
3196 * If the source or destination node of the edge is not in the destination
3197 * graph, then it must be a backward proximity edge and it should simply
3198 * be ignored.
3200 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
3201 struct isl_sched_graph *src,
3202 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
3204 int i;
3206 dst->n_edge = 0;
3207 for (i = 0; i < src->n_edge; ++i) {
3208 struct isl_sched_edge *edge = &src->edge[i];
3209 isl_map *map;
3210 isl_union_map *tagged_condition;
3211 isl_union_map *tagged_validity;
3212 struct isl_sched_node *dst_src, *dst_dst;
3214 if (!edge_pred(edge, data))
3215 continue;
3217 if (isl_map_plain_is_empty(edge->map))
3218 continue;
3220 dst_src = graph_find_node(ctx, dst, edge->src->space);
3221 dst_dst = graph_find_node(ctx, dst, edge->dst->space);
3222 if (!dst_src || !dst_dst) {
3223 if (is_validity(edge) || is_conditional_validity(edge))
3224 isl_die(ctx, isl_error_internal,
3225 "backward (conditional) validity edge",
3226 return -1);
3227 continue;
3230 map = isl_map_copy(edge->map);
3231 tagged_condition = isl_union_map_copy(edge->tagged_condition);
3232 tagged_validity = isl_union_map_copy(edge->tagged_validity);
3234 dst->edge[dst->n_edge].src = dst_src;
3235 dst->edge[dst->n_edge].dst = dst_dst;
3236 dst->edge[dst->n_edge].map = map;
3237 dst->edge[dst->n_edge].tagged_condition = tagged_condition;
3238 dst->edge[dst->n_edge].tagged_validity = tagged_validity;
3239 dst->edge[dst->n_edge].types = edge->types;
3240 dst->n_edge++;
3242 if (edge->tagged_condition && !tagged_condition)
3243 return -1;
3244 if (edge->tagged_validity && !tagged_validity)
3245 return -1;
3247 if (graph_edge_tables_add(ctx, dst,
3248 &dst->edge[dst->n_edge - 1]) < 0)
3249 return -1;
3252 return 0;
3255 /* Compute the maximal number of variables over all nodes.
3256 * This is the maximal number of linearly independent schedule
3257 * rows that we need to compute.
3258 * Just in case we end up in a part of the dependence graph
3259 * with only lower-dimensional domains, we make sure we will
3260 * compute the required amount of extra linearly independent rows.
3262 static int compute_maxvar(struct isl_sched_graph *graph)
3264 int i;
3266 graph->maxvar = 0;
3267 for (i = 0; i < graph->n; ++i) {
3268 struct isl_sched_node *node = &graph->node[i];
3269 int nvar;
3271 if (node_update_cmap(node) < 0)
3272 return -1;
3273 nvar = node->nvar + graph->n_row - node->rank;
3274 if (nvar > graph->maxvar)
3275 graph->maxvar = nvar;
3278 return 0;
3281 /* Extract the subgraph of "graph" that consists of the node satisfying
3282 * "node_pred" and the edges satisfying "edge_pred" and store
3283 * the result in "sub".
3285 static int extract_sub_graph(isl_ctx *ctx, struct isl_sched_graph *graph,
3286 int (*node_pred)(struct isl_sched_node *node, int data),
3287 int (*edge_pred)(struct isl_sched_edge *edge, int data),
3288 int data, struct isl_sched_graph *sub)
3290 int i, n = 0, n_edge = 0;
3291 int t;
3293 for (i = 0; i < graph->n; ++i)
3294 if (node_pred(&graph->node[i], data))
3295 ++n;
3296 for (i = 0; i < graph->n_edge; ++i)
3297 if (edge_pred(&graph->edge[i], data))
3298 ++n_edge;
3299 if (graph_alloc(ctx, sub, n, n_edge) < 0)
3300 return -1;
3301 if (copy_nodes(sub, graph, node_pred, data) < 0)
3302 return -1;
3303 if (graph_init_table(ctx, sub) < 0)
3304 return -1;
3305 for (t = 0; t <= isl_edge_last; ++t)
3306 sub->max_edge[t] = graph->max_edge[t];
3307 if (graph_init_edge_tables(ctx, sub) < 0)
3308 return -1;
3309 if (copy_edges(ctx, sub, graph, edge_pred, data) < 0)
3310 return -1;
3311 sub->n_row = graph->n_row;
3312 sub->max_row = graph->max_row;
3313 sub->n_total_row = graph->n_total_row;
3314 sub->band_start = graph->band_start;
3316 return 0;
3319 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
3320 struct isl_sched_graph *graph);
3321 static __isl_give isl_schedule_node *compute_schedule_wcc(
3322 isl_schedule_node *node, struct isl_sched_graph *graph);
3324 /* Compute a schedule for a subgraph of "graph". In particular, for
3325 * the graph composed of nodes that satisfy node_pred and edges that
3326 * that satisfy edge_pred.
3327 * If the subgraph is known to consist of a single component, then wcc should
3328 * be set and then we call compute_schedule_wcc on the constructed subgraph.
3329 * Otherwise, we call compute_schedule, which will check whether the subgraph
3330 * is connected.
3332 * The schedule is inserted at "node" and the updated schedule node
3333 * is returned.
3335 static __isl_give isl_schedule_node *compute_sub_schedule(
3336 __isl_take isl_schedule_node *node, isl_ctx *ctx,
3337 struct isl_sched_graph *graph,
3338 int (*node_pred)(struct isl_sched_node *node, int data),
3339 int (*edge_pred)(struct isl_sched_edge *edge, int data),
3340 int data, int wcc)
3342 struct isl_sched_graph split = { 0 };
3344 if (extract_sub_graph(ctx, graph, node_pred, edge_pred, data,
3345 &split) < 0)
3346 goto error;
3348 if (wcc)
3349 node = compute_schedule_wcc(node, &split);
3350 else
3351 node = compute_schedule(node, &split);
3353 graph_free(ctx, &split);
3354 return node;
3355 error:
3356 graph_free(ctx, &split);
3357 return isl_schedule_node_free(node);
3360 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
3362 return edge->src->scc == scc && edge->dst->scc == scc;
3365 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
3367 return edge->dst->scc <= scc;
3370 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
3372 return edge->src->scc >= scc;
3375 /* Reset the current band by dropping all its schedule rows.
3377 static int reset_band(struct isl_sched_graph *graph)
3379 int i;
3380 int drop;
3382 drop = graph->n_total_row - graph->band_start;
3383 graph->n_total_row -= drop;
3384 graph->n_row -= drop;
3386 for (i = 0; i < graph->n; ++i) {
3387 struct isl_sched_node *node = &graph->node[i];
3389 isl_map_free(node->sched_map);
3390 node->sched_map = NULL;
3392 node->sched = isl_mat_drop_rows(node->sched,
3393 graph->band_start, drop);
3395 if (!node->sched)
3396 return -1;
3399 return 0;
3402 /* Split the current graph into two parts and compute a schedule for each
3403 * part individually. In particular, one part consists of all SCCs up
3404 * to and including graph->src_scc, while the other part contains the other
3405 * SCCs. The split is enforced by a sequence node inserted at position "node"
3406 * in the schedule tree. Return the updated schedule node.
3407 * If either of these two parts consists of a sequence, then it is spliced
3408 * into the sequence containing the two parts.
3410 * The current band is reset. It would be possible to reuse
3411 * the previously computed rows as the first rows in the next
3412 * band, but recomputing them may result in better rows as we are looking
3413 * at a smaller part of the dependence graph.
3415 static __isl_give isl_schedule_node *compute_split_schedule(
3416 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3418 int is_seq;
3419 isl_ctx *ctx;
3420 isl_union_set_list *filters;
3422 if (!node)
3423 return NULL;
3425 if (reset_band(graph) < 0)
3426 return isl_schedule_node_free(node);
3428 next_band(graph);
3430 ctx = isl_schedule_node_get_ctx(node);
3431 filters = extract_split(ctx, graph);
3432 node = isl_schedule_node_insert_sequence(node, filters);
3433 node = isl_schedule_node_child(node, 1);
3434 node = isl_schedule_node_child(node, 0);
3436 node = compute_sub_schedule(node, ctx, graph,
3437 &node_scc_at_least, &edge_src_scc_at_least,
3438 graph->src_scc + 1, 0);
3439 is_seq = isl_schedule_node_get_type(node) == isl_schedule_node_sequence;
3440 node = isl_schedule_node_parent(node);
3441 node = isl_schedule_node_parent(node);
3442 if (is_seq)
3443 node = isl_schedule_node_sequence_splice_child(node, 1);
3444 node = isl_schedule_node_child(node, 0);
3445 node = isl_schedule_node_child(node, 0);
3446 node = compute_sub_schedule(node, ctx, graph,
3447 &node_scc_at_most, &edge_dst_scc_at_most,
3448 graph->src_scc, 0);
3449 is_seq = isl_schedule_node_get_type(node) == isl_schedule_node_sequence;
3450 node = isl_schedule_node_parent(node);
3451 node = isl_schedule_node_parent(node);
3452 if (is_seq)
3453 node = isl_schedule_node_sequence_splice_child(node, 0);
3455 return node;
3458 /* Insert a band node at position "node" in the schedule tree corresponding
3459 * to the current band in "graph". Mark the band node permutable
3460 * if "permutable" is set.
3461 * The partial schedules and the coincidence property are extracted
3462 * from the graph nodes.
3463 * Return the updated schedule node.
3465 static __isl_give isl_schedule_node *insert_current_band(
3466 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3467 int permutable)
3469 int i;
3470 int start, end, n;
3471 isl_multi_aff *ma;
3472 isl_multi_pw_aff *mpa;
3473 isl_multi_union_pw_aff *mupa;
3475 if (!node)
3476 return NULL;
3478 if (graph->n < 1)
3479 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
3480 "graph should have at least one node",
3481 return isl_schedule_node_free(node));
3483 start = graph->band_start;
3484 end = graph->n_total_row;
3485 n = end - start;
3487 ma = node_extract_partial_schedule_multi_aff(&graph->node[0], start, n);
3488 mpa = isl_multi_pw_aff_from_multi_aff(ma);
3489 mupa = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
3491 for (i = 1; i < graph->n; ++i) {
3492 isl_multi_union_pw_aff *mupa_i;
3494 ma = node_extract_partial_schedule_multi_aff(&graph->node[i],
3495 start, n);
3496 mpa = isl_multi_pw_aff_from_multi_aff(ma);
3497 mupa_i = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
3498 mupa = isl_multi_union_pw_aff_union_add(mupa, mupa_i);
3500 node = isl_schedule_node_insert_partial_schedule(node, mupa);
3502 for (i = 0; i < n; ++i)
3503 node = isl_schedule_node_band_member_set_coincident(node, i,
3504 graph->node[0].coincident[start + i]);
3505 node = isl_schedule_node_band_set_permutable(node, permutable);
3507 return node;
3510 /* Update the dependence relations based on the current schedule,
3511 * add the current band to "node" and then continue with the computation
3512 * of the next band.
3513 * Return the updated schedule node.
3515 static __isl_give isl_schedule_node *compute_next_band(
3516 __isl_take isl_schedule_node *node,
3517 struct isl_sched_graph *graph, int permutable)
3519 isl_ctx *ctx;
3521 if (!node)
3522 return NULL;
3524 ctx = isl_schedule_node_get_ctx(node);
3525 if (update_edges(ctx, graph) < 0)
3526 return isl_schedule_node_free(node);
3527 node = insert_current_band(node, graph, permutable);
3528 next_band(graph);
3530 node = isl_schedule_node_child(node, 0);
3531 node = compute_schedule(node, graph);
3532 node = isl_schedule_node_parent(node);
3534 return node;
3537 /* Add constraints to graph->lp that force the dependence "map" (which
3538 * is part of the dependence relation of "edge")
3539 * to be respected and attempt to carry it, where the edge is one from
3540 * a node j to itself. "pos" is the sequence number of the given map.
3541 * That is, add constraints that enforce
3543 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3544 * = c_j_x (y - x) >= e_i
3546 * for each (x,y) in R.
3547 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3548 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3549 * with each coefficient in c_j_x represented as a pair of non-negative
3550 * coefficients.
3552 static int add_intra_constraints(struct isl_sched_graph *graph,
3553 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3555 int offset;
3556 isl_ctx *ctx = isl_map_get_ctx(map);
3557 isl_dim_map *dim_map;
3558 isl_basic_set *coef;
3559 struct isl_sched_node *node = edge->src;
3561 coef = intra_coefficients(graph, node, map);
3562 if (!coef)
3563 return -1;
3565 offset = coef_var_offset(coef);
3566 dim_map = intra_dim_map(ctx, graph, node, offset, 1);
3567 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3568 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3569 coef->n_eq, coef->n_ineq);
3570 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3571 coef, dim_map);
3573 return 0;
3576 /* Add constraints to graph->lp that force the dependence "map" (which
3577 * is part of the dependence relation of "edge")
3578 * to be respected and attempt to carry it, where the edge is one from
3579 * node j to node k. "pos" is the sequence number of the given map.
3580 * That is, add constraints that enforce
3582 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3584 * for each (x,y) in R.
3585 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3586 * of valid constraints for R and then plug in
3587 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3588 * with each coefficient (except e_i, c_*_0 and c_*_n)
3589 * represented as a pair of non-negative coefficients.
3591 static int add_inter_constraints(struct isl_sched_graph *graph,
3592 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3594 int offset;
3595 isl_ctx *ctx = isl_map_get_ctx(map);
3596 isl_dim_map *dim_map;
3597 isl_basic_set *coef;
3598 struct isl_sched_node *src = edge->src;
3599 struct isl_sched_node *dst = edge->dst;
3601 coef = inter_coefficients(graph, edge, map);
3602 if (!coef)
3603 return -1;
3605 offset = coef_var_offset(coef);
3606 dim_map = inter_dim_map(ctx, graph, src, dst, offset, 1);
3607 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3608 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3609 coef->n_eq, coef->n_ineq);
3610 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3611 coef, dim_map);
3613 return 0;
3616 /* Add constraints to graph->lp that force all (conditional) validity
3617 * dependences to be respected and attempt to carry them.
3619 static int add_all_constraints(struct isl_sched_graph *graph)
3621 int i, j;
3622 int pos;
3624 pos = 0;
3625 for (i = 0; i < graph->n_edge; ++i) {
3626 struct isl_sched_edge *edge= &graph->edge[i];
3628 if (!is_any_validity(edge))
3629 continue;
3631 for (j = 0; j < edge->map->n; ++j) {
3632 isl_basic_map *bmap;
3633 isl_map *map;
3635 bmap = isl_basic_map_copy(edge->map->p[j]);
3636 map = isl_map_from_basic_map(bmap);
3638 if (edge->src == edge->dst &&
3639 add_intra_constraints(graph, edge, map, pos) < 0)
3640 return -1;
3641 if (edge->src != edge->dst &&
3642 add_inter_constraints(graph, edge, map, pos) < 0)
3643 return -1;
3644 ++pos;
3648 return 0;
3651 /* Count the number of equality and inequality constraints
3652 * that will be added to the carry_lp problem.
3653 * We count each edge exactly once.
3655 static int count_all_constraints(struct isl_sched_graph *graph,
3656 int *n_eq, int *n_ineq)
3658 int i, j;
3660 *n_eq = *n_ineq = 0;
3661 for (i = 0; i < graph->n_edge; ++i) {
3662 struct isl_sched_edge *edge= &graph->edge[i];
3664 if (!is_any_validity(edge))
3665 continue;
3667 for (j = 0; j < edge->map->n; ++j) {
3668 isl_basic_map *bmap;
3669 isl_map *map;
3671 bmap = isl_basic_map_copy(edge->map->p[j]);
3672 map = isl_map_from_basic_map(bmap);
3674 if (count_map_constraints(graph, edge, map,
3675 n_eq, n_ineq, 1, 0) < 0)
3676 return -1;
3680 return 0;
3683 /* Return the total number of (validity) edges that carry_dependences will
3684 * attempt to carry.
3686 static int count_carry_edges(struct isl_sched_graph *graph)
3688 int i;
3689 int n_edge;
3691 n_edge = 0;
3692 for (i = 0; i < graph->n_edge; ++i) {
3693 struct isl_sched_edge *edge = &graph->edge[i];
3695 if (!is_any_validity(edge))
3696 continue;
3698 n_edge += isl_map_n_basic_map(edge->map);
3701 return n_edge;
3704 /* Construct an LP problem for finding schedule coefficients
3705 * such that the schedule carries as many validity dependences as possible.
3706 * In particular, for each dependence i, we bound the dependence distance
3707 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3708 * of all e_i's. Dependences with e_i = 0 in the solution are simply
3709 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3710 * Note that if the dependence relation is a union of basic maps,
3711 * then we have to consider each basic map individually as it may only
3712 * be possible to carry the dependences expressed by some of those
3713 * basic maps and not all of them.
3714 * Below, we consider each of those basic maps as a separate "edge".
3716 * All variables of the LP are non-negative. The actual coefficients
3717 * may be negative, so each coefficient is represented as the difference
3718 * of two non-negative variables. The negative part always appears
3719 * immediately before the positive part.
3720 * Other than that, the variables have the following order
3722 * - sum of (1 - e_i) over all edges
3723 * - sum of all c_n coefficients
3724 * (unconstrained when computing non-parametric schedules)
3725 * - sum of positive and negative parts of all c_x coefficients
3726 * - for each edge
3727 * - e_i
3728 * - for each node
3729 * - c_i_0
3730 * - c_i_n (if parametric)
3731 * - positive and negative parts of c_i_x
3733 * The constraints are those from the (validity) edges plus three equalities
3734 * to express the sums and n_edge inequalities to express e_i <= 1.
3736 static isl_stat setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
3738 int i;
3739 int k;
3740 isl_space *dim;
3741 unsigned total;
3742 int n_eq, n_ineq;
3743 int n_edge;
3745 n_edge = count_carry_edges(graph);
3747 total = 3 + n_edge;
3748 for (i = 0; i < graph->n; ++i) {
3749 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
3750 node->start = total;
3751 total += 1 + node->nparam + 2 * node->nvar;
3754 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
3755 return isl_stat_error;
3757 dim = isl_space_set_alloc(ctx, 0, total);
3758 isl_basic_set_free(graph->lp);
3759 n_eq += 3;
3760 n_ineq += n_edge;
3761 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
3762 graph->lp = isl_basic_set_set_rational(graph->lp);
3764 k = isl_basic_set_alloc_equality(graph->lp);
3765 if (k < 0)
3766 return isl_stat_error;
3767 isl_seq_clr(graph->lp->eq[k], 1 + total);
3768 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
3769 isl_int_set_si(graph->lp->eq[k][1], 1);
3770 for (i = 0; i < n_edge; ++i)
3771 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
3773 if (add_param_sum_constraint(graph, 1) < 0)
3774 return isl_stat_error;
3775 if (add_var_sum_constraint(graph, 2) < 0)
3776 return isl_stat_error;
3778 for (i = 0; i < n_edge; ++i) {
3779 k = isl_basic_set_alloc_inequality(graph->lp);
3780 if (k < 0)
3781 return isl_stat_error;
3782 isl_seq_clr(graph->lp->ineq[k], 1 + total);
3783 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
3784 isl_int_set_si(graph->lp->ineq[k][0], 1);
3787 if (add_all_constraints(graph) < 0)
3788 return isl_stat_error;
3790 return isl_stat_ok;
3793 static __isl_give isl_schedule_node *compute_component_schedule(
3794 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3795 int wcc);
3797 /* Comparison function for sorting the statements based on
3798 * the corresponding value in "r".
3800 static int smaller_value(const void *a, const void *b, void *data)
3802 isl_vec *r = data;
3803 const int *i1 = a;
3804 const int *i2 = b;
3806 return isl_int_cmp(r->el[*i1], r->el[*i2]);
3809 /* If the schedule_split_scaled option is set and if the linear
3810 * parts of the scheduling rows for all nodes in the graphs have
3811 * a non-trivial common divisor, then split off the remainder of the
3812 * constant term modulo this common divisor from the linear part.
3813 * Otherwise, insert a band node directly and continue with
3814 * the construction of the schedule.
3816 * If a non-trivial common divisor is found, then
3817 * the linear part is reduced and the remainder is enforced
3818 * by a sequence node with the children placed in the order
3819 * of this remainder.
3820 * In particular, we assign an scc index based on the remainder and
3821 * then rely on compute_component_schedule to insert the sequence and
3822 * to continue the schedule construction on each part.
3824 static __isl_give isl_schedule_node *split_scaled(
3825 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3827 int i;
3828 int row;
3829 int scc;
3830 isl_ctx *ctx;
3831 isl_int gcd, gcd_i;
3832 isl_vec *r;
3833 int *order;
3835 if (!node)
3836 return NULL;
3838 ctx = isl_schedule_node_get_ctx(node);
3839 if (!ctx->opt->schedule_split_scaled)
3840 return compute_next_band(node, graph, 0);
3841 if (graph->n <= 1)
3842 return compute_next_band(node, graph, 0);
3844 isl_int_init(gcd);
3845 isl_int_init(gcd_i);
3847 isl_int_set_si(gcd, 0);
3849 row = isl_mat_rows(graph->node[0].sched) - 1;
3851 for (i = 0; i < graph->n; ++i) {
3852 struct isl_sched_node *node = &graph->node[i];
3853 int cols = isl_mat_cols(node->sched);
3855 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
3856 isl_int_gcd(gcd, gcd, gcd_i);
3859 isl_int_clear(gcd_i);
3861 if (isl_int_cmp_si(gcd, 1) <= 0) {
3862 isl_int_clear(gcd);
3863 return compute_next_band(node, graph, 0);
3866 r = isl_vec_alloc(ctx, graph->n);
3867 order = isl_calloc_array(ctx, int, graph->n);
3868 if (!r || !order)
3869 goto error;
3871 for (i = 0; i < graph->n; ++i) {
3872 struct isl_sched_node *node = &graph->node[i];
3874 order[i] = i;
3875 isl_int_fdiv_r(r->el[i], node->sched->row[row][0], gcd);
3876 isl_int_fdiv_q(node->sched->row[row][0],
3877 node->sched->row[row][0], gcd);
3878 isl_int_mul(node->sched->row[row][0],
3879 node->sched->row[row][0], gcd);
3880 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
3881 if (!node->sched)
3882 goto error;
3885 if (isl_sort(order, graph->n, sizeof(order[0]), &smaller_value, r) < 0)
3886 goto error;
3888 scc = 0;
3889 for (i = 0; i < graph->n; ++i) {
3890 if (i > 0 && isl_int_ne(r->el[order[i - 1]], r->el[order[i]]))
3891 ++scc;
3892 graph->node[order[i]].scc = scc;
3894 graph->scc = ++scc;
3895 graph->weak = 0;
3897 isl_int_clear(gcd);
3898 isl_vec_free(r);
3899 free(order);
3901 if (update_edges(ctx, graph) < 0)
3902 return isl_schedule_node_free(node);
3903 node = insert_current_band(node, graph, 0);
3904 next_band(graph);
3906 node = isl_schedule_node_child(node, 0);
3907 node = compute_component_schedule(node, graph, 0);
3908 node = isl_schedule_node_parent(node);
3910 return node;
3911 error:
3912 isl_vec_free(r);
3913 free(order);
3914 isl_int_clear(gcd);
3915 return isl_schedule_node_free(node);
3918 /* Is the schedule row "sol" trivial on node "node"?
3919 * That is, is the solution zero on the dimensions orthogonal to
3920 * the previously found solutions?
3921 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3923 * Each coefficient is represented as the difference between
3924 * two non-negative values in "sol". "sol" has been computed
3925 * in terms of the original iterators (i.e., without use of cmap).
3926 * We construct the schedule row s and write it as a linear
3927 * combination of (linear combinations of) previously computed schedule rows.
3928 * s = Q c or c = U s.
3929 * If the final entries of c are all zero, then the solution is trivial.
3931 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
3933 int trivial;
3934 isl_vec *node_sol;
3936 if (!sol)
3937 return -1;
3938 if (node->nvar == node->rank)
3939 return 0;
3941 node_sol = extract_var_coef(node, sol);
3942 node_sol = isl_mat_vec_product(isl_mat_copy(node->cinv), node_sol);
3943 if (!node_sol)
3944 return -1;
3946 trivial = isl_seq_first_non_zero(node_sol->el + node->rank,
3947 node->nvar - node->rank) == -1;
3949 isl_vec_free(node_sol);
3951 return trivial;
3954 /* Is the schedule row "sol" trivial on any node where it should
3955 * not be trivial?
3956 * "sol" has been computed in terms of the original iterators
3957 * (i.e., without use of cmap).
3958 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3960 static int is_any_trivial(struct isl_sched_graph *graph,
3961 __isl_keep isl_vec *sol)
3963 int i;
3965 for (i = 0; i < graph->n; ++i) {
3966 struct isl_sched_node *node = &graph->node[i];
3967 int trivial;
3969 if (!needs_row(graph, node))
3970 continue;
3971 trivial = is_trivial(node, sol);
3972 if (trivial < 0 || trivial)
3973 return trivial;
3976 return 0;
3979 /* Does the schedule represented by "sol" perform loop coalescing on "node"?
3980 * If so, return the position of the coalesced dimension.
3981 * Otherwise, return node->nvar or -1 on error.
3983 * In particular, look for pairs of coefficients c_i and c_j such that
3984 * |c_j/c_i| >= size_i, i.e., |c_j| >= |c_i * size_i|.
3985 * If any such pair is found, then return i.
3986 * If size_i is infinity, then no check on c_i needs to be performed.
3988 static int find_node_coalescing(struct isl_sched_node *node,
3989 __isl_keep isl_vec *sol)
3991 int i, j;
3992 isl_int max;
3993 isl_vec *csol;
3995 if (node->nvar <= 1)
3996 return node->nvar;
3998 csol = extract_var_coef(node, sol);
3999 if (!csol)
4000 return -1;
4001 isl_int_init(max);
4002 for (i = 0; i < node->nvar; ++i) {
4003 isl_val *v;
4005 if (isl_int_is_zero(csol->el[i]))
4006 continue;
4007 v = isl_multi_val_get_val(node->sizes, i);
4008 if (!v)
4009 goto error;
4010 if (!isl_val_is_int(v)) {
4011 isl_val_free(v);
4012 continue;
4014 isl_int_mul(max, v->n, csol->el[i]);
4015 isl_val_free(v);
4017 for (j = 0; j < node->nvar; ++j) {
4018 if (j == i)
4019 continue;
4020 if (isl_int_abs_ge(csol->el[j], max))
4021 break;
4023 if (j < node->nvar)
4024 break;
4027 isl_int_clear(max);
4028 isl_vec_free(csol);
4029 return i;
4030 error:
4031 isl_int_clear(max);
4032 isl_vec_free(csol);
4033 return -1;
4036 /* Force the schedule coefficient at position "pos" of "node" to be zero
4037 * in "tl".
4038 * The coefficient is encoded as the difference between two non-negative
4039 * variables. Force these two variables to have the same value.
4041 static __isl_give isl_tab_lexmin *zero_out_node_coef(
4042 __isl_take isl_tab_lexmin *tl, struct isl_sched_node *node, int pos)
4044 int dim;
4045 isl_ctx *ctx;
4046 isl_vec *eq;
4048 ctx = isl_space_get_ctx(node->space);
4049 dim = isl_tab_lexmin_dim(tl);
4050 if (dim < 0)
4051 return isl_tab_lexmin_free(tl);
4052 eq = isl_vec_alloc(ctx, 1 + dim);
4053 eq = isl_vec_clr(eq);
4054 if (!eq)
4055 return isl_tab_lexmin_free(tl);
4057 pos = 1 + node_var_coef_offset(node) + 2 * pos;
4058 isl_int_set_si(eq->el[pos], 1);
4059 isl_int_set_si(eq->el[pos + 1], -1);
4060 tl = isl_tab_lexmin_add_eq(tl, eq->el);
4061 isl_vec_free(eq);
4063 return tl;
4066 /* Return the lexicographically smallest rational point in the basic set
4067 * from which "tl" was constructed, double checking that this input set
4068 * was not empty.
4070 static __isl_give isl_vec *non_empty_solution(__isl_keep isl_tab_lexmin *tl)
4072 isl_vec *sol;
4074 sol = isl_tab_lexmin_get_solution(tl);
4075 if (!sol)
4076 return NULL;
4077 if (sol->size == 0)
4078 isl_die(isl_vec_get_ctx(sol), isl_error_internal,
4079 "error in schedule construction",
4080 return isl_vec_free(sol));
4081 return sol;
4084 /* Does the solution "sol" of the LP problem constructed by setup_carry_lp
4085 * carry any of the "n_edge" groups of dependences?
4086 * The value in the first position is the sum of (1 - e_i) over all "n_edge"
4087 * edges, with 0 <= e_i <= 1 equal to 1 when the dependences represented
4088 * by the edge are carried by the solution.
4089 * If the sum of the (1 - e_i) is smaller than "n_edge" then at least
4090 * one of those is carried.
4092 * Note that despite the fact that the problem is solved using a rational
4093 * solver, the solution is guaranteed to be integral.
4094 * Specifically, the dependence distance lower bounds e_i (and therefore
4095 * also their sum) are integers. See Lemma 5 of [1].
4097 * Any potential denominator of the sum is cleared by this function.
4098 * The denominator is not relevant for any of the other elements
4099 * in the solution.
4101 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4102 * Problem, Part II: Multi-Dimensional Time.
4103 * In Intl. Journal of Parallel Programming, 1992.
4105 static int carries_dependences(__isl_keep isl_vec *sol, int n_edge)
4107 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
4108 isl_int_set_si(sol->el[0], 1);
4109 return isl_int_cmp_si(sol->el[1], n_edge) < 0;
4112 /* Return the lexicographically smallest rational point in "lp",
4113 * assuming that all variables are non-negative and performing some
4114 * additional sanity checks.
4115 * In particular, "lp" should not be empty by construction.
4116 * Double check that this is the case.
4117 * Also, check that dependences are carried for at least one of
4118 * the "n_edge" edges.
4120 * If the computed schedule performs loop coalescing on a given node,
4121 * i.e., if it is of the form
4123 * c_i i + c_j j + ...
4125 * with |c_j/c_i| >= size_i, then force the coefficient c_i to be zero
4126 * to cut out this solution. Repeat this process until no more loop
4127 * coalescing occurs or until no more dependences can be carried.
4128 * In the latter case, revert to the previously computed solution.
4130 static __isl_give isl_vec *non_neg_lexmin(struct isl_sched_graph *graph,
4131 __isl_take isl_basic_set *lp, int n_edge)
4133 int i, pos;
4134 isl_ctx *ctx;
4135 isl_tab_lexmin *tl;
4136 isl_vec *sol, *prev = NULL;
4137 int treat_coalescing;
4139 if (!lp)
4140 return NULL;
4141 ctx = isl_basic_set_get_ctx(lp);
4142 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4143 tl = isl_tab_lexmin_from_basic_set(lp);
4145 do {
4146 sol = non_empty_solution(tl);
4147 if (!sol)
4148 goto error;
4150 if (!carries_dependences(sol, n_edge)) {
4151 if (!prev)
4152 isl_die(ctx, isl_error_unknown,
4153 "unable to carry dependences",
4154 goto error);
4155 isl_vec_free(sol);
4156 sol = prev;
4157 break;
4159 prev = isl_vec_free(prev);
4160 if (!treat_coalescing)
4161 break;
4162 for (i = 0; i < graph->n; ++i) {
4163 struct isl_sched_node *node = &graph->node[i];
4165 pos = find_node_coalescing(node, sol);
4166 if (pos < 0)
4167 goto error;
4168 if (pos < node->nvar)
4169 break;
4171 if (i < graph->n) {
4172 prev = sol;
4173 tl = zero_out_node_coef(tl, &graph->node[i], pos);
4175 } while (i < graph->n);
4177 isl_tab_lexmin_free(tl);
4179 return sol;
4180 error:
4181 isl_tab_lexmin_free(tl);
4182 isl_vec_free(prev);
4183 isl_vec_free(sol);
4184 return NULL;
4187 /* Construct a schedule row for each node such that as many validity dependences
4188 * as possible are carried and then continue with the next band.
4190 * If there are no validity dependences, then no dependence can be carried and
4191 * the procedure is guaranteed to fail. If there is more than one component,
4192 * then try computing a schedule on each component separately
4193 * to prevent or at least postpone this failure.
4195 * If the computed schedule row turns out to be trivial on one or
4196 * more nodes where it should not be trivial, then we throw it away
4197 * and try again on each component separately.
4199 * If there is only one component, then we accept the schedule row anyway,
4200 * but we do not consider it as a complete row and therefore do not
4201 * increment graph->n_row. Note that the ranks of the nodes that
4202 * do get a non-trivial schedule part will get updated regardless and
4203 * graph->maxvar is computed based on these ranks. The test for
4204 * whether more schedule rows are required in compute_schedule_wcc
4205 * is therefore not affected.
4207 * Insert a band corresponding to the schedule row at position "node"
4208 * of the schedule tree and continue with the construction of the schedule.
4209 * This insertion and the continued construction is performed by split_scaled
4210 * after optionally checking for non-trivial common divisors.
4212 static __isl_give isl_schedule_node *carry_dependences(
4213 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
4215 int n_edge;
4216 int trivial;
4217 isl_ctx *ctx;
4218 isl_vec *sol;
4219 isl_basic_set *lp;
4221 if (!node)
4222 return NULL;
4224 n_edge = count_carry_edges(graph);
4225 if (n_edge == 0 && graph->scc > 1)
4226 return compute_component_schedule(node, graph, 1);
4228 ctx = isl_schedule_node_get_ctx(node);
4229 if (setup_carry_lp(ctx, graph) < 0)
4230 return isl_schedule_node_free(node);
4232 lp = isl_basic_set_copy(graph->lp);
4233 sol = non_neg_lexmin(graph, lp, n_edge);
4234 if (!sol)
4235 return isl_schedule_node_free(node);
4237 trivial = is_any_trivial(graph, sol);
4238 if (trivial < 0) {
4239 sol = isl_vec_free(sol);
4240 } else if (trivial && graph->scc > 1) {
4241 isl_vec_free(sol);
4242 return compute_component_schedule(node, graph, 1);
4245 if (update_schedule(graph, sol, 0, 0) < 0)
4246 return isl_schedule_node_free(node);
4247 if (trivial)
4248 graph->n_row--;
4250 return split_scaled(node, graph);
4253 /* Topologically sort statements mapped to the same schedule iteration
4254 * and add insert a sequence node in front of "node"
4255 * corresponding to this order.
4256 * If "initialized" is set, then it may be assumed that compute_maxvar
4257 * has been called on the current band. Otherwise, call
4258 * compute_maxvar if and before carry_dependences gets called.
4260 * If it turns out to be impossible to sort the statements apart,
4261 * because different dependences impose different orderings
4262 * on the statements, then we extend the schedule such that
4263 * it carries at least one more dependence.
4265 static __isl_give isl_schedule_node *sort_statements(
4266 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
4267 int initialized)
4269 isl_ctx *ctx;
4270 isl_union_set_list *filters;
4272 if (!node)
4273 return NULL;
4275 ctx = isl_schedule_node_get_ctx(node);
4276 if (graph->n < 1)
4277 isl_die(ctx, isl_error_internal,
4278 "graph should have at least one node",
4279 return isl_schedule_node_free(node));
4281 if (graph->n == 1)
4282 return node;
4284 if (update_edges(ctx, graph) < 0)
4285 return isl_schedule_node_free(node);
4287 if (graph->n_edge == 0)
4288 return node;
4290 if (detect_sccs(ctx, graph) < 0)
4291 return isl_schedule_node_free(node);
4293 next_band(graph);
4294 if (graph->scc < graph->n) {
4295 if (!initialized && compute_maxvar(graph) < 0)
4296 return isl_schedule_node_free(node);
4297 return carry_dependences(node, graph);
4300 filters = extract_sccs(ctx, graph);
4301 node = isl_schedule_node_insert_sequence(node, filters);
4303 return node;
4306 /* Are there any (non-empty) (conditional) validity edges in the graph?
4308 static int has_validity_edges(struct isl_sched_graph *graph)
4310 int i;
4312 for (i = 0; i < graph->n_edge; ++i) {
4313 int empty;
4315 empty = isl_map_plain_is_empty(graph->edge[i].map);
4316 if (empty < 0)
4317 return -1;
4318 if (empty)
4319 continue;
4320 if (is_any_validity(&graph->edge[i]))
4321 return 1;
4324 return 0;
4327 /* Should we apply a Feautrier step?
4328 * That is, did the user request the Feautrier algorithm and are
4329 * there any validity dependences (left)?
4331 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
4333 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
4334 return 0;
4336 return has_validity_edges(graph);
4339 /* Compute a schedule for a connected dependence graph using Feautrier's
4340 * multi-dimensional scheduling algorithm and return the updated schedule node.
4342 * The original algorithm is described in [1].
4343 * The main idea is to minimize the number of scheduling dimensions, by
4344 * trying to satisfy as many dependences as possible per scheduling dimension.
4346 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4347 * Problem, Part II: Multi-Dimensional Time.
4348 * In Intl. Journal of Parallel Programming, 1992.
4350 static __isl_give isl_schedule_node *compute_schedule_wcc_feautrier(
4351 isl_schedule_node *node, struct isl_sched_graph *graph)
4353 return carry_dependences(node, graph);
4356 /* Turn off the "local" bit on all (condition) edges.
4358 static void clear_local_edges(struct isl_sched_graph *graph)
4360 int i;
4362 for (i = 0; i < graph->n_edge; ++i)
4363 if (is_condition(&graph->edge[i]))
4364 clear_local(&graph->edge[i]);
4367 /* Does "graph" have both condition and conditional validity edges?
4369 static int need_condition_check(struct isl_sched_graph *graph)
4371 int i;
4372 int any_condition = 0;
4373 int any_conditional_validity = 0;
4375 for (i = 0; i < graph->n_edge; ++i) {
4376 if (is_condition(&graph->edge[i]))
4377 any_condition = 1;
4378 if (is_conditional_validity(&graph->edge[i]))
4379 any_conditional_validity = 1;
4382 return any_condition && any_conditional_validity;
4385 /* Does "graph" contain any coincidence edge?
4387 static int has_any_coincidence(struct isl_sched_graph *graph)
4389 int i;
4391 for (i = 0; i < graph->n_edge; ++i)
4392 if (is_coincidence(&graph->edge[i]))
4393 return 1;
4395 return 0;
4398 /* Extract the final schedule row as a map with the iteration domain
4399 * of "node" as domain.
4401 static __isl_give isl_map *final_row(struct isl_sched_node *node)
4403 isl_multi_aff *ma;
4404 int row;
4406 row = isl_mat_rows(node->sched) - 1;
4407 ma = node_extract_partial_schedule_multi_aff(node, row, 1);
4408 return isl_map_from_multi_aff(ma);
4411 /* Is the conditional validity dependence in the edge with index "edge_index"
4412 * violated by the latest (i.e., final) row of the schedule?
4413 * That is, is i scheduled after j
4414 * for any conditional validity dependence i -> j?
4416 static int is_violated(struct isl_sched_graph *graph, int edge_index)
4418 isl_map *src_sched, *dst_sched, *map;
4419 struct isl_sched_edge *edge = &graph->edge[edge_index];
4420 int empty;
4422 src_sched = final_row(edge->src);
4423 dst_sched = final_row(edge->dst);
4424 map = isl_map_copy(edge->map);
4425 map = isl_map_apply_domain(map, src_sched);
4426 map = isl_map_apply_range(map, dst_sched);
4427 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
4428 empty = isl_map_is_empty(map);
4429 isl_map_free(map);
4431 if (empty < 0)
4432 return -1;
4434 return !empty;
4437 /* Does "graph" have any satisfied condition edges that
4438 * are adjacent to the conditional validity constraint with
4439 * domain "conditional_source" and range "conditional_sink"?
4441 * A satisfied condition is one that is not local.
4442 * If a condition was forced to be local already (i.e., marked as local)
4443 * then there is no need to check if it is in fact local.
4445 * Additionally, mark all adjacent condition edges found as local.
4447 static int has_adjacent_true_conditions(struct isl_sched_graph *graph,
4448 __isl_keep isl_union_set *conditional_source,
4449 __isl_keep isl_union_set *conditional_sink)
4451 int i;
4452 int any = 0;
4454 for (i = 0; i < graph->n_edge; ++i) {
4455 int adjacent, local;
4456 isl_union_map *condition;
4458 if (!is_condition(&graph->edge[i]))
4459 continue;
4460 if (is_local(&graph->edge[i]))
4461 continue;
4463 condition = graph->edge[i].tagged_condition;
4464 adjacent = domain_intersects(condition, conditional_sink);
4465 if (adjacent >= 0 && !adjacent)
4466 adjacent = range_intersects(condition,
4467 conditional_source);
4468 if (adjacent < 0)
4469 return -1;
4470 if (!adjacent)
4471 continue;
4473 set_local(&graph->edge[i]);
4475 local = is_condition_false(&graph->edge[i]);
4476 if (local < 0)
4477 return -1;
4478 if (!local)
4479 any = 1;
4482 return any;
4485 /* Are there any violated conditional validity dependences with
4486 * adjacent condition dependences that are not local with respect
4487 * to the current schedule?
4488 * That is, is the conditional validity constraint violated?
4490 * Additionally, mark all those adjacent condition dependences as local.
4491 * We also mark those adjacent condition dependences that were not marked
4492 * as local before, but just happened to be local already. This ensures
4493 * that they remain local if the schedule is recomputed.
4495 * We first collect domain and range of all violated conditional validity
4496 * dependences and then check if there are any adjacent non-local
4497 * condition dependences.
4499 static int has_violated_conditional_constraint(isl_ctx *ctx,
4500 struct isl_sched_graph *graph)
4502 int i;
4503 int any = 0;
4504 isl_union_set *source, *sink;
4506 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
4507 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
4508 for (i = 0; i < graph->n_edge; ++i) {
4509 isl_union_set *uset;
4510 isl_union_map *umap;
4511 int violated;
4513 if (!is_conditional_validity(&graph->edge[i]))
4514 continue;
4516 violated = is_violated(graph, i);
4517 if (violated < 0)
4518 goto error;
4519 if (!violated)
4520 continue;
4522 any = 1;
4524 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
4525 uset = isl_union_map_domain(umap);
4526 source = isl_union_set_union(source, uset);
4527 source = isl_union_set_coalesce(source);
4529 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
4530 uset = isl_union_map_range(umap);
4531 sink = isl_union_set_union(sink, uset);
4532 sink = isl_union_set_coalesce(sink);
4535 if (any)
4536 any = has_adjacent_true_conditions(graph, source, sink);
4538 isl_union_set_free(source);
4539 isl_union_set_free(sink);
4540 return any;
4541 error:
4542 isl_union_set_free(source);
4543 isl_union_set_free(sink);
4544 return -1;
4547 /* Examine the current band (the rows between graph->band_start and
4548 * graph->n_total_row), deciding whether to drop it or add it to "node"
4549 * and then continue with the computation of the next band, if any.
4550 * If "initialized" is set, then it may be assumed that compute_maxvar
4551 * has been called on the current band. Otherwise, call
4552 * compute_maxvar if and before carry_dependences gets called.
4554 * The caller keeps looking for a new row as long as
4555 * graph->n_row < graph->maxvar. If the latest attempt to find
4556 * such a row failed (i.e., we still have graph->n_row < graph->maxvar),
4557 * then we either
4558 * - split between SCCs and start over (assuming we found an interesting
4559 * pair of SCCs between which to split)
4560 * - continue with the next band (assuming the current band has at least
4561 * one row)
4562 * - try to carry as many dependences as possible and continue with the next
4563 * band
4564 * In each case, we first insert a band node in the schedule tree
4565 * if any rows have been computed.
4567 * If the caller managed to complete the schedule, we insert a band node
4568 * (if any schedule rows were computed) and we finish off by topologically
4569 * sorting the statements based on the remaining dependences.
4571 static __isl_give isl_schedule_node *compute_schedule_finish_band(
4572 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
4573 int initialized)
4575 int insert;
4577 if (!node)
4578 return NULL;
4580 if (graph->n_row < graph->maxvar) {
4581 isl_ctx *ctx;
4582 int empty = graph->n_total_row == graph->band_start;
4584 ctx = isl_schedule_node_get_ctx(node);
4585 if (!ctx->opt->schedule_maximize_band_depth && !empty)
4586 return compute_next_band(node, graph, 1);
4587 if (graph->src_scc >= 0)
4588 return compute_split_schedule(node, graph);
4589 if (!empty)
4590 return compute_next_band(node, graph, 1);
4591 if (!initialized && compute_maxvar(graph) < 0)
4592 return isl_schedule_node_free(node);
4593 return carry_dependences(node, graph);
4596 insert = graph->n_total_row > graph->band_start;
4597 if (insert) {
4598 node = insert_current_band(node, graph, 1);
4599 node = isl_schedule_node_child(node, 0);
4601 node = sort_statements(node, graph, initialized);
4602 if (insert)
4603 node = isl_schedule_node_parent(node);
4605 return node;
4608 /* Construct a band of schedule rows for a connected dependence graph.
4609 * The caller is responsible for determining the strongly connected
4610 * components and calling compute_maxvar first.
4612 * We try to find a sequence of as many schedule rows as possible that result
4613 * in non-negative dependence distances (independent of the previous rows
4614 * in the sequence, i.e., such that the sequence is tilable), with as
4615 * many of the initial rows as possible satisfying the coincidence constraints.
4616 * The computation stops if we can't find any more rows or if we have found
4617 * all the rows we wanted to find.
4619 * If ctx->opt->schedule_outer_coincidence is set, then we force the
4620 * outermost dimension to satisfy the coincidence constraints. If this
4621 * turns out to be impossible, we fall back on the general scheme above
4622 * and try to carry as many dependences as possible.
4624 * If "graph" contains both condition and conditional validity dependences,
4625 * then we need to check that that the conditional schedule constraint
4626 * is satisfied, i.e., there are no violated conditional validity dependences
4627 * that are adjacent to any non-local condition dependences.
4628 * If there are, then we mark all those adjacent condition dependences
4629 * as local and recompute the current band. Those dependences that
4630 * are marked local will then be forced to be local.
4631 * The initial computation is performed with no dependences marked as local.
4632 * If we are lucky, then there will be no violated conditional validity
4633 * dependences adjacent to any non-local condition dependences.
4634 * Otherwise, we mark some additional condition dependences as local and
4635 * recompute. We continue this process until there are no violations left or
4636 * until we are no longer able to compute a schedule.
4637 * Since there are only a finite number of dependences,
4638 * there will only be a finite number of iterations.
4640 static isl_stat compute_schedule_wcc_band(isl_ctx *ctx,
4641 struct isl_sched_graph *graph)
4643 int has_coincidence;
4644 int use_coincidence;
4645 int force_coincidence = 0;
4646 int check_conditional;
4648 if (sort_sccs(graph) < 0)
4649 return isl_stat_error;
4651 clear_local_edges(graph);
4652 check_conditional = need_condition_check(graph);
4653 has_coincidence = has_any_coincidence(graph);
4655 if (ctx->opt->schedule_outer_coincidence)
4656 force_coincidence = 1;
4658 use_coincidence = has_coincidence;
4659 while (graph->n_row < graph->maxvar) {
4660 isl_vec *sol;
4661 int violated;
4662 int coincident;
4664 graph->src_scc = -1;
4665 graph->dst_scc = -1;
4667 if (setup_lp(ctx, graph, use_coincidence) < 0)
4668 return isl_stat_error;
4669 sol = solve_lp(graph);
4670 if (!sol)
4671 return isl_stat_error;
4672 if (sol->size == 0) {
4673 int empty = graph->n_total_row == graph->band_start;
4675 isl_vec_free(sol);
4676 if (use_coincidence && (!force_coincidence || !empty)) {
4677 use_coincidence = 0;
4678 continue;
4680 return isl_stat_ok;
4682 coincident = !has_coincidence || use_coincidence;
4683 if (update_schedule(graph, sol, 1, coincident) < 0)
4684 return isl_stat_error;
4686 if (!check_conditional)
4687 continue;
4688 violated = has_violated_conditional_constraint(ctx, graph);
4689 if (violated < 0)
4690 return isl_stat_error;
4691 if (!violated)
4692 continue;
4693 if (reset_band(graph) < 0)
4694 return isl_stat_error;
4695 use_coincidence = has_coincidence;
4698 return isl_stat_ok;
4701 /* Compute a schedule for a connected dependence graph by considering
4702 * the graph as a whole and return the updated schedule node.
4704 * The actual schedule rows of the current band are computed by
4705 * compute_schedule_wcc_band. compute_schedule_finish_band takes
4706 * care of integrating the band into "node" and continuing
4707 * the computation.
4709 static __isl_give isl_schedule_node *compute_schedule_wcc_whole(
4710 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
4712 isl_ctx *ctx;
4714 if (!node)
4715 return NULL;
4717 ctx = isl_schedule_node_get_ctx(node);
4718 if (compute_schedule_wcc_band(ctx, graph) < 0)
4719 return isl_schedule_node_free(node);
4721 return compute_schedule_finish_band(node, graph, 1);
4724 /* Clustering information used by compute_schedule_wcc_clustering.
4726 * "n" is the number of SCCs in the original dependence graph
4727 * "scc" is an array of "n" elements, each representing an SCC
4728 * of the original dependence graph. All entries in the same cluster
4729 * have the same number of schedule rows.
4730 * "scc_cluster" maps each SCC index to the cluster to which it belongs,
4731 * where each cluster is represented by the index of the first SCC
4732 * in the cluster. Initially, each SCC belongs to a cluster containing
4733 * only that SCC.
4735 * "scc_in_merge" is used by merge_clusters_along_edge to keep
4736 * track of which SCCs need to be merged.
4738 * "cluster" contains the merged clusters of SCCs after the clustering
4739 * has completed.
4741 * "scc_node" is a temporary data structure used inside copy_partial.
4742 * For each SCC, it keeps track of the number of nodes in the SCC
4743 * that have already been copied.
4745 struct isl_clustering {
4746 int n;
4747 struct isl_sched_graph *scc;
4748 struct isl_sched_graph *cluster;
4749 int *scc_cluster;
4750 int *scc_node;
4751 int *scc_in_merge;
4754 /* Initialize the clustering data structure "c" from "graph".
4756 * In particular, allocate memory, extract the SCCs from "graph"
4757 * into c->scc, initialize scc_cluster and construct
4758 * a band of schedule rows for each SCC.
4759 * Within each SCC, there is only one SCC by definition.
4760 * Each SCC initially belongs to a cluster containing only that SCC.
4762 static isl_stat clustering_init(isl_ctx *ctx, struct isl_clustering *c,
4763 struct isl_sched_graph *graph)
4765 int i;
4767 c->n = graph->scc;
4768 c->scc = isl_calloc_array(ctx, struct isl_sched_graph, c->n);
4769 c->cluster = isl_calloc_array(ctx, struct isl_sched_graph, c->n);
4770 c->scc_cluster = isl_calloc_array(ctx, int, c->n);
4771 c->scc_node = isl_calloc_array(ctx, int, c->n);
4772 c->scc_in_merge = isl_calloc_array(ctx, int, c->n);
4773 if (!c->scc || !c->cluster ||
4774 !c->scc_cluster || !c->scc_node || !c->scc_in_merge)
4775 return isl_stat_error;
4777 for (i = 0; i < c->n; ++i) {
4778 if (extract_sub_graph(ctx, graph, &node_scc_exactly,
4779 &edge_scc_exactly, i, &c->scc[i]) < 0)
4780 return isl_stat_error;
4781 c->scc[i].scc = 1;
4782 if (compute_maxvar(&c->scc[i]) < 0)
4783 return isl_stat_error;
4784 if (compute_schedule_wcc_band(ctx, &c->scc[i]) < 0)
4785 return isl_stat_error;
4786 c->scc_cluster[i] = i;
4789 return isl_stat_ok;
4792 /* Free all memory allocated for "c".
4794 static void clustering_free(isl_ctx *ctx, struct isl_clustering *c)
4796 int i;
4798 if (c->scc)
4799 for (i = 0; i < c->n; ++i)
4800 graph_free(ctx, &c->scc[i]);
4801 free(c->scc);
4802 if (c->cluster)
4803 for (i = 0; i < c->n; ++i)
4804 graph_free(ctx, &c->cluster[i]);
4805 free(c->cluster);
4806 free(c->scc_cluster);
4807 free(c->scc_node);
4808 free(c->scc_in_merge);
4811 /* Should we refrain from merging the cluster in "graph" with
4812 * any other cluster?
4813 * In particular, is its current schedule band empty and incomplete.
4815 static int bad_cluster(struct isl_sched_graph *graph)
4817 return graph->n_row < graph->maxvar &&
4818 graph->n_total_row == graph->band_start;
4821 /* Return the index of an edge in "graph" that can be used to merge
4822 * two clusters in "c".
4823 * Return graph->n_edge if no such edge can be found.
4824 * Return -1 on error.
4826 * In particular, return a proximity edge between two clusters
4827 * that is not marked "no_merge" and such that neither of the
4828 * two clusters has an incomplete, empty band.
4830 * If there are multiple such edges, then try and find the most
4831 * appropriate edge to use for merging. In particular, pick the edge
4832 * with the greatest weight. If there are multiple of those,
4833 * then pick one with the shortest distance between
4834 * the two cluster representatives.
4836 static int find_proximity(struct isl_sched_graph *graph,
4837 struct isl_clustering *c)
4839 int i, best = graph->n_edge, best_dist, best_weight;
4841 for (i = 0; i < graph->n_edge; ++i) {
4842 struct isl_sched_edge *edge = &graph->edge[i];
4843 int dist, weight;
4845 if (!is_proximity(edge))
4846 continue;
4847 if (edge->no_merge)
4848 continue;
4849 if (bad_cluster(&c->scc[edge->src->scc]) ||
4850 bad_cluster(&c->scc[edge->dst->scc]))
4851 continue;
4852 dist = c->scc_cluster[edge->dst->scc] -
4853 c->scc_cluster[edge->src->scc];
4854 if (dist == 0)
4855 continue;
4856 weight = edge->weight;
4857 if (best < graph->n_edge) {
4858 if (best_weight > weight)
4859 continue;
4860 if (best_weight == weight && best_dist <= dist)
4861 continue;
4863 best = i;
4864 best_dist = dist;
4865 best_weight = weight;
4868 return best;
4871 /* Internal data structure used in mark_merge_sccs.
4873 * "graph" is the dependence graph in which a strongly connected
4874 * component is constructed.
4875 * "scc_cluster" maps each SCC index to the cluster to which it belongs.
4876 * "src" and "dst" are the indices of the nodes that are being merged.
4878 struct isl_mark_merge_sccs_data {
4879 struct isl_sched_graph *graph;
4880 int *scc_cluster;
4881 int src;
4882 int dst;
4885 /* Check whether the cluster containing node "i" depends on the cluster
4886 * containing node "j". If "i" and "j" belong to the same cluster,
4887 * then they are taken to depend on each other to ensure that
4888 * the resulting strongly connected component consists of complete
4889 * clusters. Furthermore, if "i" and "j" are the two nodes that
4890 * are being merged, then they are taken to depend on each other as well.
4891 * Otherwise, check if there is a (conditional) validity dependence
4892 * from node[j] to node[i], forcing node[i] to follow node[j].
4894 static isl_bool cluster_follows(int i, int j, void *user)
4896 struct isl_mark_merge_sccs_data *data = user;
4897 struct isl_sched_graph *graph = data->graph;
4898 int *scc_cluster = data->scc_cluster;
4900 if (data->src == i && data->dst == j)
4901 return isl_bool_true;
4902 if (data->src == j && data->dst == i)
4903 return isl_bool_true;
4904 if (scc_cluster[graph->node[i].scc] == scc_cluster[graph->node[j].scc])
4905 return isl_bool_true;
4907 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
4910 /* Mark all SCCs that belong to either of the two clusters in "c"
4911 * connected by the edge in "graph" with index "edge", or to any
4912 * of the intermediate clusters.
4913 * The marking is recorded in c->scc_in_merge.
4915 * The given edge has been selected for merging two clusters,
4916 * meaning that there is at least a proximity edge between the two nodes.
4917 * However, there may also be (indirect) validity dependences
4918 * between the two nodes. When merging the two clusters, all clusters
4919 * containing one or more of the intermediate nodes along the
4920 * indirect validity dependences need to be merged in as well.
4922 * First collect all such nodes by computing the strongly connected
4923 * component (SCC) containing the two nodes connected by the edge, where
4924 * the two nodes are considered to depend on each other to make
4925 * sure they end up in the same SCC. Similarly, each node is considered
4926 * to depend on every other node in the same cluster to ensure
4927 * that the SCC consists of complete clusters.
4929 * Then the original SCCs that contain any of these nodes are marked
4930 * in c->scc_in_merge.
4932 static isl_stat mark_merge_sccs(isl_ctx *ctx, struct isl_sched_graph *graph,
4933 int edge, struct isl_clustering *c)
4935 struct isl_mark_merge_sccs_data data;
4936 struct isl_tarjan_graph *g;
4937 int i;
4939 for (i = 0; i < c->n; ++i)
4940 c->scc_in_merge[i] = 0;
4942 data.graph = graph;
4943 data.scc_cluster = c->scc_cluster;
4944 data.src = graph->edge[edge].src - graph->node;
4945 data.dst = graph->edge[edge].dst - graph->node;
4947 g = isl_tarjan_graph_component(ctx, graph->n, data.dst,
4948 &cluster_follows, &data);
4949 if (!g)
4950 goto error;
4952 i = g->op;
4953 if (i < 3)
4954 isl_die(ctx, isl_error_internal,
4955 "expecting at least two nodes in component",
4956 goto error);
4957 if (g->order[--i] != -1)
4958 isl_die(ctx, isl_error_internal,
4959 "expecting end of component marker", goto error);
4961 for (--i; i >= 0 && g->order[i] != -1; --i) {
4962 int scc = graph->node[g->order[i]].scc;
4963 c->scc_in_merge[scc] = 1;
4966 isl_tarjan_graph_free(g);
4967 return isl_stat_ok;
4968 error:
4969 isl_tarjan_graph_free(g);
4970 return isl_stat_error;
4973 /* Construct the identifier "cluster_i".
4975 static __isl_give isl_id *cluster_id(isl_ctx *ctx, int i)
4977 char name[40];
4979 snprintf(name, sizeof(name), "cluster_%d", i);
4980 return isl_id_alloc(ctx, name, NULL);
4983 /* Construct the space of the cluster with index "i" containing
4984 * the strongly connected component "scc".
4986 * In particular, construct a space called cluster_i with dimension equal
4987 * to the number of schedule rows in the current band of "scc".
4989 static __isl_give isl_space *cluster_space(struct isl_sched_graph *scc, int i)
4991 int nvar;
4992 isl_space *space;
4993 isl_id *id;
4995 nvar = scc->n_total_row - scc->band_start;
4996 space = isl_space_copy(scc->node[0].space);
4997 space = isl_space_params(space);
4998 space = isl_space_set_from_params(space);
4999 space = isl_space_add_dims(space, isl_dim_set, nvar);
5000 id = cluster_id(isl_space_get_ctx(space), i);
5001 space = isl_space_set_tuple_id(space, isl_dim_set, id);
5003 return space;
5006 /* Collect the domain of the graph for merging clusters.
5008 * In particular, for each cluster with first SCC "i", construct
5009 * a set in the space called cluster_i with dimension equal
5010 * to the number of schedule rows in the current band of the cluster.
5012 static __isl_give isl_union_set *collect_domain(isl_ctx *ctx,
5013 struct isl_sched_graph *graph, struct isl_clustering *c)
5015 int i;
5016 isl_space *space;
5017 isl_union_set *domain;
5019 space = isl_space_params_alloc(ctx, 0);
5020 domain = isl_union_set_empty(space);
5022 for (i = 0; i < graph->scc; ++i) {
5023 isl_space *space;
5025 if (!c->scc_in_merge[i])
5026 continue;
5027 if (c->scc_cluster[i] != i)
5028 continue;
5029 space = cluster_space(&c->scc[i], i);
5030 domain = isl_union_set_add_set(domain, isl_set_universe(space));
5033 return domain;
5036 /* Construct a map from the original instances to the corresponding
5037 * cluster instance in the current bands of the clusters in "c".
5039 static __isl_give isl_union_map *collect_cluster_map(isl_ctx *ctx,
5040 struct isl_sched_graph *graph, struct isl_clustering *c)
5042 int i, j;
5043 isl_space *space;
5044 isl_union_map *cluster_map;
5046 space = isl_space_params_alloc(ctx, 0);
5047 cluster_map = isl_union_map_empty(space);
5048 for (i = 0; i < graph->scc; ++i) {
5049 int start, n;
5050 isl_id *id;
5052 if (!c->scc_in_merge[i])
5053 continue;
5055 id = cluster_id(ctx, c->scc_cluster[i]);
5056 start = c->scc[i].band_start;
5057 n = c->scc[i].n_total_row - start;
5058 for (j = 0; j < c->scc[i].n; ++j) {
5059 isl_multi_aff *ma;
5060 isl_map *map;
5061 struct isl_sched_node *node = &c->scc[i].node[j];
5063 ma = node_extract_partial_schedule_multi_aff(node,
5064 start, n);
5065 ma = isl_multi_aff_set_tuple_id(ma, isl_dim_out,
5066 isl_id_copy(id));
5067 map = isl_map_from_multi_aff(ma);
5068 cluster_map = isl_union_map_add_map(cluster_map, map);
5070 isl_id_free(id);
5073 return cluster_map;
5076 /* Add "umap" to the schedule constraints "sc" of all types of "edge"
5077 * that are not isl_edge_condition or isl_edge_conditional_validity.
5079 static __isl_give isl_schedule_constraints *add_non_conditional_constraints(
5080 struct isl_sched_edge *edge, __isl_keep isl_union_map *umap,
5081 __isl_take isl_schedule_constraints *sc)
5083 enum isl_edge_type t;
5085 if (!sc)
5086 return NULL;
5088 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
5089 if (t == isl_edge_condition ||
5090 t == isl_edge_conditional_validity)
5091 continue;
5092 if (!is_type(edge, t))
5093 continue;
5094 sc = isl_schedule_constraints_add(sc, t,
5095 isl_union_map_copy(umap));
5098 return sc;
5101 /* Add schedule constraints of types isl_edge_condition and
5102 * isl_edge_conditional_validity to "sc" by applying "umap" to
5103 * the domains of the wrapped relations in domain and range
5104 * of the corresponding tagged constraints of "edge".
5106 static __isl_give isl_schedule_constraints *add_conditional_constraints(
5107 struct isl_sched_edge *edge, __isl_keep isl_union_map *umap,
5108 __isl_take isl_schedule_constraints *sc)
5110 enum isl_edge_type t;
5111 isl_union_map *tagged;
5113 for (t = isl_edge_condition; t <= isl_edge_conditional_validity; ++t) {
5114 if (!is_type(edge, t))
5115 continue;
5116 if (t == isl_edge_condition)
5117 tagged = isl_union_map_copy(edge->tagged_condition);
5118 else
5119 tagged = isl_union_map_copy(edge->tagged_validity);
5120 tagged = isl_union_map_zip(tagged);
5121 tagged = isl_union_map_apply_domain(tagged,
5122 isl_union_map_copy(umap));
5123 tagged = isl_union_map_zip(tagged);
5124 sc = isl_schedule_constraints_add(sc, t, tagged);
5125 if (!sc)
5126 return NULL;
5129 return sc;
5132 /* Given a mapping "cluster_map" from the original instances to
5133 * the cluster instances, add schedule constraints on the clusters
5134 * to "sc" corresponding to the original constraints represented by "edge".
5136 * For non-tagged dependence constraints, the cluster constraints
5137 * are obtained by applying "cluster_map" to the edge->map.
5139 * For tagged dependence constraints, "cluster_map" needs to be applied
5140 * to the domains of the wrapped relations in domain and range
5141 * of the tagged dependence constraints. Pick out the mappings
5142 * from these domains from "cluster_map" and construct their product.
5143 * This mapping can then be applied to the pair of domains.
5145 static __isl_give isl_schedule_constraints *collect_edge_constraints(
5146 struct isl_sched_edge *edge, __isl_keep isl_union_map *cluster_map,
5147 __isl_take isl_schedule_constraints *sc)
5149 isl_union_map *umap;
5150 isl_space *space;
5151 isl_union_set *uset;
5152 isl_union_map *umap1, *umap2;
5154 if (!sc)
5155 return NULL;
5157 umap = isl_union_map_from_map(isl_map_copy(edge->map));
5158 umap = isl_union_map_apply_domain(umap,
5159 isl_union_map_copy(cluster_map));
5160 umap = isl_union_map_apply_range(umap,
5161 isl_union_map_copy(cluster_map));
5162 sc = add_non_conditional_constraints(edge, umap, sc);
5163 isl_union_map_free(umap);
5165 if (!sc || (!is_condition(edge) && !is_conditional_validity(edge)))
5166 return sc;
5168 space = isl_space_domain(isl_map_get_space(edge->map));
5169 uset = isl_union_set_from_set(isl_set_universe(space));
5170 umap1 = isl_union_map_copy(cluster_map);
5171 umap1 = isl_union_map_intersect_domain(umap1, uset);
5172 space = isl_space_range(isl_map_get_space(edge->map));
5173 uset = isl_union_set_from_set(isl_set_universe(space));
5174 umap2 = isl_union_map_copy(cluster_map);
5175 umap2 = isl_union_map_intersect_domain(umap2, uset);
5176 umap = isl_union_map_product(umap1, umap2);
5178 sc = add_conditional_constraints(edge, umap, sc);
5180 isl_union_map_free(umap);
5181 return sc;
5184 /* Given a mapping "cluster_map" from the original instances to
5185 * the cluster instances, add schedule constraints on the clusters
5186 * to "sc" corresponding to all edges in "graph" between nodes that
5187 * belong to SCCs that are marked for merging in "scc_in_merge".
5189 static __isl_give isl_schedule_constraints *collect_constraints(
5190 struct isl_sched_graph *graph, int *scc_in_merge,
5191 __isl_keep isl_union_map *cluster_map,
5192 __isl_take isl_schedule_constraints *sc)
5194 int i;
5196 for (i = 0; i < graph->n_edge; ++i) {
5197 struct isl_sched_edge *edge = &graph->edge[i];
5199 if (!scc_in_merge[edge->src->scc])
5200 continue;
5201 if (!scc_in_merge[edge->dst->scc])
5202 continue;
5203 sc = collect_edge_constraints(edge, cluster_map, sc);
5206 return sc;
5209 /* Construct a dependence graph for scheduling clusters with respect
5210 * to each other and store the result in "merge_graph".
5211 * In particular, the nodes of the graph correspond to the schedule
5212 * dimensions of the current bands of those clusters that have been
5213 * marked for merging in "c".
5215 * First construct an isl_schedule_constraints object for this domain
5216 * by transforming the edges in "graph" to the domain.
5217 * Then initialize a dependence graph for scheduling from these
5218 * constraints.
5220 static isl_stat init_merge_graph(isl_ctx *ctx, struct isl_sched_graph *graph,
5221 struct isl_clustering *c, struct isl_sched_graph *merge_graph)
5223 isl_union_set *domain;
5224 isl_union_map *cluster_map;
5225 isl_schedule_constraints *sc;
5226 isl_stat r;
5228 domain = collect_domain(ctx, graph, c);
5229 sc = isl_schedule_constraints_on_domain(domain);
5230 if (!sc)
5231 return isl_stat_error;
5232 cluster_map = collect_cluster_map(ctx, graph, c);
5233 sc = collect_constraints(graph, c->scc_in_merge, cluster_map, sc);
5234 isl_union_map_free(cluster_map);
5236 r = graph_init(merge_graph, sc);
5238 isl_schedule_constraints_free(sc);
5240 return r;
5243 /* Compute the maximal number of remaining schedule rows that still need
5244 * to be computed for the nodes that belong to clusters with the maximal
5245 * dimension for the current band (i.e., the band that is to be merged).
5246 * Only clusters that are about to be merged are considered.
5247 * "maxvar" is the maximal dimension for the current band.
5248 * "c" contains information about the clusters.
5250 * Return the maximal number of remaining schedule rows or -1 on error.
5252 static int compute_maxvar_max_slack(int maxvar, struct isl_clustering *c)
5254 int i, j;
5255 int max_slack;
5257 max_slack = 0;
5258 for (i = 0; i < c->n; ++i) {
5259 int nvar;
5260 struct isl_sched_graph *scc;
5262 if (!c->scc_in_merge[i])
5263 continue;
5264 scc = &c->scc[i];
5265 nvar = scc->n_total_row - scc->band_start;
5266 if (nvar != maxvar)
5267 continue;
5268 for (j = 0; j < scc->n; ++j) {
5269 struct isl_sched_node *node = &scc->node[j];
5270 int slack;
5272 if (node_update_cmap(node) < 0)
5273 return -1;
5274 slack = node->nvar - node->rank;
5275 if (slack > max_slack)
5276 max_slack = slack;
5280 return max_slack;
5283 /* If there are any clusters where the dimension of the current band
5284 * (i.e., the band that is to be merged) is smaller than "maxvar" and
5285 * if there are any nodes in such a cluster where the number
5286 * of remaining schedule rows that still need to be computed
5287 * is greater than "max_slack", then return the smallest current band
5288 * dimension of all these clusters. Otherwise return the original value
5289 * of "maxvar". Return -1 in case of any error.
5290 * Only clusters that are about to be merged are considered.
5291 * "c" contains information about the clusters.
5293 static int limit_maxvar_to_slack(int maxvar, int max_slack,
5294 struct isl_clustering *c)
5296 int i, j;
5298 for (i = 0; i < c->n; ++i) {
5299 int nvar;
5300 struct isl_sched_graph *scc;
5302 if (!c->scc_in_merge[i])
5303 continue;
5304 scc = &c->scc[i];
5305 nvar = scc->n_total_row - scc->band_start;
5306 if (nvar >= maxvar)
5307 continue;
5308 for (j = 0; j < scc->n; ++j) {
5309 struct isl_sched_node *node = &scc->node[j];
5310 int slack;
5312 if (node_update_cmap(node) < 0)
5313 return -1;
5314 slack = node->nvar - node->rank;
5315 if (slack > max_slack) {
5316 maxvar = nvar;
5317 break;
5322 return maxvar;
5325 /* Adjust merge_graph->maxvar based on the number of remaining schedule rows
5326 * that still need to be computed. In particular, if there is a node
5327 * in a cluster where the dimension of the current band is smaller
5328 * than merge_graph->maxvar, but the number of remaining schedule rows
5329 * is greater than that of any node in a cluster with the maximal
5330 * dimension for the current band (i.e., merge_graph->maxvar),
5331 * then adjust merge_graph->maxvar to the (smallest) current band dimension
5332 * of those clusters. Without this adjustment, the total number of
5333 * schedule dimensions would be increased, resulting in a skewed view
5334 * of the number of coincident dimensions.
5335 * "c" contains information about the clusters.
5337 * If the maximize_band_depth option is set and merge_graph->maxvar is reduced,
5338 * then there is no point in attempting any merge since it will be rejected
5339 * anyway. Set merge_graph->maxvar to zero in such cases.
5341 static isl_stat adjust_maxvar_to_slack(isl_ctx *ctx,
5342 struct isl_sched_graph *merge_graph, struct isl_clustering *c)
5344 int max_slack, maxvar;
5346 max_slack = compute_maxvar_max_slack(merge_graph->maxvar, c);
5347 if (max_slack < 0)
5348 return isl_stat_error;
5349 maxvar = limit_maxvar_to_slack(merge_graph->maxvar, max_slack, c);
5350 if (maxvar < 0)
5351 return isl_stat_error;
5353 if (maxvar < merge_graph->maxvar) {
5354 if (isl_options_get_schedule_maximize_band_depth(ctx))
5355 merge_graph->maxvar = 0;
5356 else
5357 merge_graph->maxvar = maxvar;
5360 return isl_stat_ok;
5363 /* Return the number of coincident dimensions in the current band of "graph",
5364 * where the nodes of "graph" are assumed to be scheduled by a single band.
5366 static int get_n_coincident(struct isl_sched_graph *graph)
5368 int i;
5370 for (i = graph->band_start; i < graph->n_total_row; ++i)
5371 if (!graph->node[0].coincident[i])
5372 break;
5374 return i - graph->band_start;
5377 /* Should the clusters be merged based on the cluster schedule
5378 * in the current (and only) band of "merge_graph", given that
5379 * coincidence should be maximized?
5381 * If the number of coincident schedule dimensions in the merged band
5382 * would be less than the maximal number of coincident schedule dimensions
5383 * in any of the merged clusters, then the clusters should not be merged.
5385 static isl_bool ok_to_merge_coincident(struct isl_clustering *c,
5386 struct isl_sched_graph *merge_graph)
5388 int i;
5389 int n_coincident;
5390 int max_coincident;
5392 max_coincident = 0;
5393 for (i = 0; i < c->n; ++i) {
5394 if (!c->scc_in_merge[i])
5395 continue;
5396 n_coincident = get_n_coincident(&c->scc[i]);
5397 if (n_coincident > max_coincident)
5398 max_coincident = n_coincident;
5401 n_coincident = get_n_coincident(merge_graph);
5403 return n_coincident >= max_coincident;
5406 /* Return the transformation on "node" expressed by the current (and only)
5407 * band of "merge_graph" applied to the clusters in "c".
5409 * First find the representation of "node" in its SCC in "c" and
5410 * extract the transformation expressed by the current band.
5411 * Then extract the transformation applied by "merge_graph"
5412 * to the cluster to which this SCC belongs.
5413 * Combine the two to obtain the complete transformation on the node.
5415 * Note that the range of the first transformation is an anonymous space,
5416 * while the domain of the second is named "cluster_X". The range
5417 * of the former therefore needs to be adjusted before the two
5418 * can be combined.
5420 static __isl_give isl_map *extract_node_transformation(isl_ctx *ctx,
5421 struct isl_sched_node *node, struct isl_clustering *c,
5422 struct isl_sched_graph *merge_graph)
5424 struct isl_sched_node *scc_node, *cluster_node;
5425 int start, n;
5426 isl_id *id;
5427 isl_space *space;
5428 isl_multi_aff *ma, *ma2;
5430 scc_node = graph_find_node(ctx, &c->scc[node->scc], node->space);
5431 start = c->scc[node->scc].band_start;
5432 n = c->scc[node->scc].n_total_row - start;
5433 ma = node_extract_partial_schedule_multi_aff(scc_node, start, n);
5434 space = cluster_space(&c->scc[node->scc], c->scc_cluster[node->scc]);
5435 cluster_node = graph_find_node(ctx, merge_graph, space);
5436 if (space && !cluster_node)
5437 isl_die(ctx, isl_error_internal, "unable to find cluster",
5438 space = isl_space_free(space));
5439 id = isl_space_get_tuple_id(space, isl_dim_set);
5440 ma = isl_multi_aff_set_tuple_id(ma, isl_dim_out, id);
5441 isl_space_free(space);
5442 n = merge_graph->n_total_row;
5443 ma2 = node_extract_partial_schedule_multi_aff(cluster_node, 0, n);
5444 ma = isl_multi_aff_pullback_multi_aff(ma2, ma);
5446 return isl_map_from_multi_aff(ma);
5449 /* Give a set of distances "set", are they bounded by a small constant
5450 * in direction "pos"?
5451 * In practice, check if they are bounded by 2 by checking that there
5452 * are no elements with a value greater than or equal to 3 or
5453 * smaller than or equal to -3.
5455 static isl_bool distance_is_bounded(__isl_keep isl_set *set, int pos)
5457 isl_bool bounded;
5458 isl_set *test;
5460 if (!set)
5461 return isl_bool_error;
5463 test = isl_set_copy(set);
5464 test = isl_set_lower_bound_si(test, isl_dim_set, pos, 3);
5465 bounded = isl_set_is_empty(test);
5466 isl_set_free(test);
5468 if (bounded < 0 || !bounded)
5469 return bounded;
5471 test = isl_set_copy(set);
5472 test = isl_set_upper_bound_si(test, isl_dim_set, pos, -3);
5473 bounded = isl_set_is_empty(test);
5474 isl_set_free(test);
5476 return bounded;
5479 /* Does the set "set" have a fixed (but possible parametric) value
5480 * at dimension "pos"?
5482 static isl_bool has_single_value(__isl_keep isl_set *set, int pos)
5484 int n;
5485 isl_bool single;
5487 if (!set)
5488 return isl_bool_error;
5489 set = isl_set_copy(set);
5490 n = isl_set_dim(set, isl_dim_set);
5491 set = isl_set_project_out(set, isl_dim_set, pos + 1, n - (pos + 1));
5492 set = isl_set_project_out(set, isl_dim_set, 0, pos);
5493 single = isl_set_is_singleton(set);
5494 isl_set_free(set);
5496 return single;
5499 /* Does "map" have a fixed (but possible parametric) value
5500 * at dimension "pos" of either its domain or its range?
5502 static isl_bool has_singular_src_or_dst(__isl_keep isl_map *map, int pos)
5504 isl_set *set;
5505 isl_bool single;
5507 set = isl_map_domain(isl_map_copy(map));
5508 single = has_single_value(set, pos);
5509 isl_set_free(set);
5511 if (single < 0 || single)
5512 return single;
5514 set = isl_map_range(isl_map_copy(map));
5515 single = has_single_value(set, pos);
5516 isl_set_free(set);
5518 return single;
5521 /* Does the edge "edge" from "graph" have bounded dependence distances
5522 * in the merged graph "merge_graph" of a selection of clusters in "c"?
5524 * Extract the complete transformations of the source and destination
5525 * nodes of the edge, apply them to the edge constraints and
5526 * compute the differences. Finally, check if these differences are bounded
5527 * in each direction.
5529 * If the dimension of the band is greater than the number of
5530 * dimensions that can be expected to be optimized by the edge
5531 * (based on its weight), then also allow the differences to be unbounded
5532 * in the remaining dimensions, but only if either the source or
5533 * the destination has a fixed value in that direction.
5534 * This allows a statement that produces values that are used by
5535 * several instances of another statement to be merged with that
5536 * other statement.
5537 * However, merging such clusters will introduce an inherently
5538 * large proximity distance inside the merged cluster, meaning
5539 * that proximity distances will no longer be optimized in
5540 * subsequent merges. These merges are therefore only allowed
5541 * after all other possible merges have been tried.
5542 * The first time such a merge is encountered, the weight of the edge
5543 * is replaced by a negative weight. The second time (i.e., after
5544 * all merges over edges with a non-negative weight have been tried),
5545 * the merge is allowed.
5547 static isl_bool has_bounded_distances(isl_ctx *ctx, struct isl_sched_edge *edge,
5548 struct isl_sched_graph *graph, struct isl_clustering *c,
5549 struct isl_sched_graph *merge_graph)
5551 int i, n, n_slack;
5552 isl_bool bounded;
5553 isl_map *map, *t;
5554 isl_set *dist;
5556 map = isl_map_copy(edge->map);
5557 t = extract_node_transformation(ctx, edge->src, c, merge_graph);
5558 map = isl_map_apply_domain(map, t);
5559 t = extract_node_transformation(ctx, edge->dst, c, merge_graph);
5560 map = isl_map_apply_range(map, t);
5561 dist = isl_map_deltas(isl_map_copy(map));
5563 bounded = isl_bool_true;
5564 n = isl_set_dim(dist, isl_dim_set);
5565 n_slack = n - edge->weight;
5566 if (edge->weight < 0)
5567 n_slack -= graph->max_weight + 1;
5568 for (i = 0; i < n; ++i) {
5569 isl_bool bounded_i, singular_i;
5571 bounded_i = distance_is_bounded(dist, i);
5572 if (bounded_i < 0)
5573 goto error;
5574 if (bounded_i)
5575 continue;
5576 if (edge->weight >= 0)
5577 bounded = isl_bool_false;
5578 n_slack--;
5579 if (n_slack < 0)
5580 break;
5581 singular_i = has_singular_src_or_dst(map, i);
5582 if (singular_i < 0)
5583 goto error;
5584 if (singular_i)
5585 continue;
5586 bounded = isl_bool_false;
5587 break;
5589 if (!bounded && i >= n && edge->weight >= 0)
5590 edge->weight -= graph->max_weight + 1;
5591 isl_map_free(map);
5592 isl_set_free(dist);
5594 return bounded;
5595 error:
5596 isl_map_free(map);
5597 isl_set_free(dist);
5598 return isl_bool_error;
5601 /* Should the clusters be merged based on the cluster schedule
5602 * in the current (and only) band of "merge_graph"?
5603 * "graph" is the original dependence graph, while "c" records
5604 * which SCCs are involved in the latest merge.
5606 * In particular, is there at least one proximity constraint
5607 * that is optimized by the merge?
5609 * A proximity constraint is considered to be optimized
5610 * if the dependence distances are small.
5612 static isl_bool ok_to_merge_proximity(isl_ctx *ctx,
5613 struct isl_sched_graph *graph, struct isl_clustering *c,
5614 struct isl_sched_graph *merge_graph)
5616 int i;
5618 for (i = 0; i < graph->n_edge; ++i) {
5619 struct isl_sched_edge *edge = &graph->edge[i];
5620 isl_bool bounded;
5622 if (!is_proximity(edge))
5623 continue;
5624 if (!c->scc_in_merge[edge->src->scc])
5625 continue;
5626 if (!c->scc_in_merge[edge->dst->scc])
5627 continue;
5628 if (c->scc_cluster[edge->dst->scc] ==
5629 c->scc_cluster[edge->src->scc])
5630 continue;
5631 bounded = has_bounded_distances(ctx, edge, graph, c,
5632 merge_graph);
5633 if (bounded < 0 || bounded)
5634 return bounded;
5637 return isl_bool_false;
5640 /* Should the clusters be merged based on the cluster schedule
5641 * in the current (and only) band of "merge_graph"?
5642 * "graph" is the original dependence graph, while "c" records
5643 * which SCCs are involved in the latest merge.
5645 * If the current band is empty, then the clusters should not be merged.
5647 * If the band depth should be maximized and the merge schedule
5648 * is incomplete (meaning that the dimension of some of the schedule
5649 * bands in the original schedule will be reduced), then the clusters
5650 * should not be merged.
5652 * If the schedule_maximize_coincidence option is set, then check that
5653 * the number of coincident schedule dimensions is not reduced.
5655 * Finally, only allow the merge if at least one proximity
5656 * constraint is optimized.
5658 static isl_bool ok_to_merge(isl_ctx *ctx, struct isl_sched_graph *graph,
5659 struct isl_clustering *c, struct isl_sched_graph *merge_graph)
5661 if (merge_graph->n_total_row == merge_graph->band_start)
5662 return isl_bool_false;
5664 if (isl_options_get_schedule_maximize_band_depth(ctx) &&
5665 merge_graph->n_total_row < merge_graph->maxvar)
5666 return isl_bool_false;
5668 if (isl_options_get_schedule_maximize_coincidence(ctx)) {
5669 isl_bool ok;
5671 ok = ok_to_merge_coincident(c, merge_graph);
5672 if (ok < 0 || !ok)
5673 return ok;
5676 return ok_to_merge_proximity(ctx, graph, c, merge_graph);
5679 /* Apply the schedule in "t_node" to the "n" rows starting at "first"
5680 * of the schedule in "node" and return the result.
5682 * That is, essentially compute
5684 * T * N(first:first+n-1)
5686 * taking into account the constant term and the parameter coefficients
5687 * in "t_node".
5689 static __isl_give isl_mat *node_transformation(isl_ctx *ctx,
5690 struct isl_sched_node *t_node, struct isl_sched_node *node,
5691 int first, int n)
5693 int i, j;
5694 isl_mat *t;
5695 int n_row, n_col, n_param, n_var;
5697 n_param = node->nparam;
5698 n_var = node->nvar;
5699 n_row = isl_mat_rows(t_node->sched);
5700 n_col = isl_mat_cols(node->sched);
5701 t = isl_mat_alloc(ctx, n_row, n_col);
5702 if (!t)
5703 return NULL;
5704 for (i = 0; i < n_row; ++i) {
5705 isl_seq_cpy(t->row[i], t_node->sched->row[i], 1 + n_param);
5706 isl_seq_clr(t->row[i] + 1 + n_param, n_var);
5707 for (j = 0; j < n; ++j)
5708 isl_seq_addmul(t->row[i],
5709 t_node->sched->row[i][1 + n_param + j],
5710 node->sched->row[first + j],
5711 1 + n_param + n_var);
5713 return t;
5716 /* Apply the cluster schedule in "t_node" to the current band
5717 * schedule of the nodes in "graph".
5719 * In particular, replace the rows starting at band_start
5720 * by the result of applying the cluster schedule in "t_node"
5721 * to the original rows.
5723 * The coincidence of the schedule is determined by the coincidence
5724 * of the cluster schedule.
5726 static isl_stat transform(isl_ctx *ctx, struct isl_sched_graph *graph,
5727 struct isl_sched_node *t_node)
5729 int i, j;
5730 int n_new;
5731 int start, n;
5733 start = graph->band_start;
5734 n = graph->n_total_row - start;
5736 n_new = isl_mat_rows(t_node->sched);
5737 for (i = 0; i < graph->n; ++i) {
5738 struct isl_sched_node *node = &graph->node[i];
5739 isl_mat *t;
5741 t = node_transformation(ctx, t_node, node, start, n);
5742 node->sched = isl_mat_drop_rows(node->sched, start, n);
5743 node->sched = isl_mat_concat(node->sched, t);
5744 node->sched_map = isl_map_free(node->sched_map);
5745 if (!node->sched)
5746 return isl_stat_error;
5747 for (j = 0; j < n_new; ++j)
5748 node->coincident[start + j] = t_node->coincident[j];
5750 graph->n_total_row -= n;
5751 graph->n_row -= n;
5752 graph->n_total_row += n_new;
5753 graph->n_row += n_new;
5755 return isl_stat_ok;
5758 /* Merge the clusters marked for merging in "c" into a single
5759 * cluster using the cluster schedule in the current band of "merge_graph".
5760 * The representative SCC for the new cluster is the SCC with
5761 * the smallest index.
5763 * The current band schedule of each SCC in the new cluster is obtained
5764 * by applying the schedule of the corresponding original cluster
5765 * to the original band schedule.
5766 * All SCCs in the new cluster have the same number of schedule rows.
5768 static isl_stat merge(isl_ctx *ctx, struct isl_clustering *c,
5769 struct isl_sched_graph *merge_graph)
5771 int i;
5772 int cluster = -1;
5773 isl_space *space;
5775 for (i = 0; i < c->n; ++i) {
5776 struct isl_sched_node *node;
5778 if (!c->scc_in_merge[i])
5779 continue;
5780 if (cluster < 0)
5781 cluster = i;
5782 space = cluster_space(&c->scc[i], c->scc_cluster[i]);
5783 if (!space)
5784 return isl_stat_error;
5785 node = graph_find_node(ctx, merge_graph, space);
5786 isl_space_free(space);
5787 if (!node)
5788 isl_die(ctx, isl_error_internal,
5789 "unable to find cluster",
5790 return isl_stat_error);
5791 if (transform(ctx, &c->scc[i], node) < 0)
5792 return isl_stat_error;
5793 c->scc_cluster[i] = cluster;
5796 return isl_stat_ok;
5799 /* Try and merge the clusters of SCCs marked in c->scc_in_merge
5800 * by scheduling the current cluster bands with respect to each other.
5802 * Construct a dependence graph with a space for each cluster and
5803 * with the coordinates of each space corresponding to the schedule
5804 * dimensions of the current band of that cluster.
5805 * Construct a cluster schedule in this cluster dependence graph and
5806 * apply it to the current cluster bands if it is applicable
5807 * according to ok_to_merge.
5809 * If the number of remaining schedule dimensions in a cluster
5810 * with a non-maximal current schedule dimension is greater than
5811 * the number of remaining schedule dimensions in clusters
5812 * with a maximal current schedule dimension, then restrict
5813 * the number of rows to be computed in the cluster schedule
5814 * to the minimal such non-maximal current schedule dimension.
5815 * Do this by adjusting merge_graph.maxvar.
5817 * Return isl_bool_true if the clusters have effectively been merged
5818 * into a single cluster.
5820 * Note that since the standard scheduling algorithm minimizes the maximal
5821 * distance over proximity constraints, the proximity constraints between
5822 * the merged clusters may not be optimized any further than what is
5823 * sufficient to bring the distances within the limits of the internal
5824 * proximity constraints inside the individual clusters.
5825 * It may therefore make sense to perform an additional translation step
5826 * to bring the clusters closer to each other, while maintaining
5827 * the linear part of the merging schedule found using the standard
5828 * scheduling algorithm.
5830 static isl_bool try_merge(isl_ctx *ctx, struct isl_sched_graph *graph,
5831 struct isl_clustering *c)
5833 struct isl_sched_graph merge_graph = { 0 };
5834 isl_bool merged;
5836 if (init_merge_graph(ctx, graph, c, &merge_graph) < 0)
5837 goto error;
5839 if (compute_maxvar(&merge_graph) < 0)
5840 goto error;
5841 if (adjust_maxvar_to_slack(ctx, &merge_graph,c) < 0)
5842 goto error;
5843 if (compute_schedule_wcc_band(ctx, &merge_graph) < 0)
5844 goto error;
5845 merged = ok_to_merge(ctx, graph, c, &merge_graph);
5846 if (merged && merge(ctx, c, &merge_graph) < 0)
5847 goto error;
5849 graph_free(ctx, &merge_graph);
5850 return merged;
5851 error:
5852 graph_free(ctx, &merge_graph);
5853 return isl_bool_error;
5856 /* Is there any edge marked "no_merge" between two SCCs that are
5857 * about to be merged (i.e., that are set in "scc_in_merge")?
5858 * "merge_edge" is the proximity edge along which the clusters of SCCs
5859 * are going to be merged.
5861 * If there is any edge between two SCCs with a negative weight,
5862 * while the weight of "merge_edge" is non-negative, then this
5863 * means that the edge was postponed. "merge_edge" should then
5864 * also be postponed since merging along the edge with negative weight should
5865 * be postponed until all edges with non-negative weight have been tried.
5866 * Replace the weight of "merge_edge" by a negative weight as well and
5867 * tell the caller not to attempt a merge.
5869 static int any_no_merge(struct isl_sched_graph *graph, int *scc_in_merge,
5870 struct isl_sched_edge *merge_edge)
5872 int i;
5874 for (i = 0; i < graph->n_edge; ++i) {
5875 struct isl_sched_edge *edge = &graph->edge[i];
5877 if (!scc_in_merge[edge->src->scc])
5878 continue;
5879 if (!scc_in_merge[edge->dst->scc])
5880 continue;
5881 if (edge->no_merge)
5882 return 1;
5883 if (merge_edge->weight >= 0 && edge->weight < 0) {
5884 merge_edge->weight -= graph->max_weight + 1;
5885 return 1;
5889 return 0;
5892 /* Merge the two clusters in "c" connected by the edge in "graph"
5893 * with index "edge" into a single cluster.
5894 * If it turns out to be impossible to merge these two clusters,
5895 * then mark the edge as "no_merge" such that it will not be
5896 * considered again.
5898 * First mark all SCCs that need to be merged. This includes the SCCs
5899 * in the two clusters, but it may also include the SCCs
5900 * of intermediate clusters.
5901 * If there is already a no_merge edge between any pair of such SCCs,
5902 * then simply mark the current edge as no_merge as well.
5903 * Likewise, if any of those edges was postponed by has_bounded_distances,
5904 * then postpone the current edge as well.
5905 * Otherwise, try and merge the clusters and mark "edge" as "no_merge"
5906 * if the clusters did not end up getting merged, unless the non-merge
5907 * is due to the fact that the edge was postponed. This postponement
5908 * can be recognized by a change in weight (from non-negative to negative).
5910 static isl_stat merge_clusters_along_edge(isl_ctx *ctx,
5911 struct isl_sched_graph *graph, int edge, struct isl_clustering *c)
5913 isl_bool merged;
5914 int edge_weight = graph->edge[edge].weight;
5916 if (mark_merge_sccs(ctx, graph, edge, c) < 0)
5917 return isl_stat_error;
5919 if (any_no_merge(graph, c->scc_in_merge, &graph->edge[edge]))
5920 merged = isl_bool_false;
5921 else
5922 merged = try_merge(ctx, graph, c);
5923 if (merged < 0)
5924 return isl_stat_error;
5925 if (!merged && edge_weight == graph->edge[edge].weight)
5926 graph->edge[edge].no_merge = 1;
5928 return isl_stat_ok;
5931 /* Does "node" belong to the cluster identified by "cluster"?
5933 static int node_cluster_exactly(struct isl_sched_node *node, int cluster)
5935 return node->cluster == cluster;
5938 /* Does "edge" connect two nodes belonging to the cluster
5939 * identified by "cluster"?
5941 static int edge_cluster_exactly(struct isl_sched_edge *edge, int cluster)
5943 return edge->src->cluster == cluster && edge->dst->cluster == cluster;
5946 /* Swap the schedule of "node1" and "node2".
5947 * Both nodes have been derived from the same node in a common parent graph.
5948 * Since the "coincident" field is shared with that node
5949 * in the parent graph, there is no need to also swap this field.
5951 static void swap_sched(struct isl_sched_node *node1,
5952 struct isl_sched_node *node2)
5954 isl_mat *sched;
5955 isl_map *sched_map;
5957 sched = node1->sched;
5958 node1->sched = node2->sched;
5959 node2->sched = sched;
5961 sched_map = node1->sched_map;
5962 node1->sched_map = node2->sched_map;
5963 node2->sched_map = sched_map;
5966 /* Copy the current band schedule from the SCCs that form the cluster
5967 * with index "pos" to the actual cluster at position "pos".
5968 * By construction, the index of the first SCC that belongs to the cluster
5969 * is also "pos".
5971 * The order of the nodes inside both the SCCs and the cluster
5972 * is assumed to be same as the order in the original "graph".
5974 * Since the SCC graphs will no longer be used after this function,
5975 * the schedules are actually swapped rather than copied.
5977 static isl_stat copy_partial(struct isl_sched_graph *graph,
5978 struct isl_clustering *c, int pos)
5980 int i, j;
5982 c->cluster[pos].n_total_row = c->scc[pos].n_total_row;
5983 c->cluster[pos].n_row = c->scc[pos].n_row;
5984 c->cluster[pos].maxvar = c->scc[pos].maxvar;
5985 j = 0;
5986 for (i = 0; i < graph->n; ++i) {
5987 int k;
5988 int s;
5990 if (graph->node[i].cluster != pos)
5991 continue;
5992 s = graph->node[i].scc;
5993 k = c->scc_node[s]++;
5994 swap_sched(&c->cluster[pos].node[j], &c->scc[s].node[k]);
5995 if (c->scc[s].maxvar > c->cluster[pos].maxvar)
5996 c->cluster[pos].maxvar = c->scc[s].maxvar;
5997 ++j;
6000 return isl_stat_ok;
6003 /* Is there a (conditional) validity dependence from node[j] to node[i],
6004 * forcing node[i] to follow node[j] or do the nodes belong to the same
6005 * cluster?
6007 static isl_bool node_follows_strong_or_same_cluster(int i, int j, void *user)
6009 struct isl_sched_graph *graph = user;
6011 if (graph->node[i].cluster == graph->node[j].cluster)
6012 return isl_bool_true;
6013 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
6016 /* Extract the merged clusters of SCCs in "graph", sort them, and
6017 * store them in c->clusters. Update c->scc_cluster accordingly.
6019 * First keep track of the cluster containing the SCC to which a node
6020 * belongs in the node itself.
6021 * Then extract the clusters into c->clusters, copying the current
6022 * band schedule from the SCCs that belong to the cluster.
6023 * Do this only once per cluster.
6025 * Finally, topologically sort the clusters and update c->scc_cluster
6026 * to match the new scc numbering. While the SCCs were originally
6027 * sorted already, some SCCs that depend on some other SCCs may
6028 * have been merged with SCCs that appear before these other SCCs.
6029 * A reordering may therefore be required.
6031 static isl_stat extract_clusters(isl_ctx *ctx, struct isl_sched_graph *graph,
6032 struct isl_clustering *c)
6034 int i;
6036 for (i = 0; i < graph->n; ++i)
6037 graph->node[i].cluster = c->scc_cluster[graph->node[i].scc];
6039 for (i = 0; i < graph->scc; ++i) {
6040 if (c->scc_cluster[i] != i)
6041 continue;
6042 if (extract_sub_graph(ctx, graph, &node_cluster_exactly,
6043 &edge_cluster_exactly, i, &c->cluster[i]) < 0)
6044 return isl_stat_error;
6045 c->cluster[i].src_scc = -1;
6046 c->cluster[i].dst_scc = -1;
6047 if (copy_partial(graph, c, i) < 0)
6048 return isl_stat_error;
6051 if (detect_ccs(ctx, graph, &node_follows_strong_or_same_cluster) < 0)
6052 return isl_stat_error;
6053 for (i = 0; i < graph->n; ++i)
6054 c->scc_cluster[graph->node[i].scc] = graph->node[i].cluster;
6056 return isl_stat_ok;
6059 /* Compute weights on the proximity edges of "graph" that can
6060 * be used by find_proximity to find the most appropriate
6061 * proximity edge to use to merge two clusters in "c".
6062 * The weights are also used by has_bounded_distances to determine
6063 * whether the merge should be allowed.
6064 * Store the maximum of the computed weights in graph->max_weight.
6066 * The computed weight is a measure for the number of remaining schedule
6067 * dimensions that can still be completely aligned.
6068 * In particular, compute the number of equalities between
6069 * input dimensions and output dimensions in the proximity constraints.
6070 * The directions that are already handled by outer schedule bands
6071 * are projected out prior to determining this number.
6073 * Edges that will never be considered by find_proximity are ignored.
6075 static isl_stat compute_weights(struct isl_sched_graph *graph,
6076 struct isl_clustering *c)
6078 int i;
6080 graph->max_weight = 0;
6082 for (i = 0; i < graph->n_edge; ++i) {
6083 struct isl_sched_edge *edge = &graph->edge[i];
6084 struct isl_sched_node *src = edge->src;
6085 struct isl_sched_node *dst = edge->dst;
6086 isl_basic_map *hull;
6087 int n_in, n_out;
6089 if (!is_proximity(edge))
6090 continue;
6091 if (bad_cluster(&c->scc[edge->src->scc]) ||
6092 bad_cluster(&c->scc[edge->dst->scc]))
6093 continue;
6094 if (c->scc_cluster[edge->dst->scc] ==
6095 c->scc_cluster[edge->src->scc])
6096 continue;
6098 hull = isl_map_affine_hull(isl_map_copy(edge->map));
6099 hull = isl_basic_map_transform_dims(hull, isl_dim_in, 0,
6100 isl_mat_copy(src->ctrans));
6101 hull = isl_basic_map_transform_dims(hull, isl_dim_out, 0,
6102 isl_mat_copy(dst->ctrans));
6103 hull = isl_basic_map_project_out(hull,
6104 isl_dim_in, 0, src->rank);
6105 hull = isl_basic_map_project_out(hull,
6106 isl_dim_out, 0, dst->rank);
6107 hull = isl_basic_map_remove_divs(hull);
6108 n_in = isl_basic_map_dim(hull, isl_dim_in);
6109 n_out = isl_basic_map_dim(hull, isl_dim_out);
6110 hull = isl_basic_map_drop_constraints_not_involving_dims(hull,
6111 isl_dim_in, 0, n_in);
6112 hull = isl_basic_map_drop_constraints_not_involving_dims(hull,
6113 isl_dim_out, 0, n_out);
6114 if (!hull)
6115 return isl_stat_error;
6116 edge->weight = hull->n_eq;
6117 isl_basic_map_free(hull);
6119 if (edge->weight > graph->max_weight)
6120 graph->max_weight = edge->weight;
6123 return isl_stat_ok;
6126 /* Call compute_schedule_finish_band on each of the clusters in "c"
6127 * in their topological order. This order is determined by the scc
6128 * fields of the nodes in "graph".
6129 * Combine the results in a sequence expressing the topological order.
6131 * If there is only one cluster left, then there is no need to introduce
6132 * a sequence node. Also, in this case, the cluster necessarily contains
6133 * the SCC at position 0 in the original graph and is therefore also
6134 * stored in the first cluster of "c".
6136 static __isl_give isl_schedule_node *finish_bands_clustering(
6137 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
6138 struct isl_clustering *c)
6140 int i;
6141 isl_ctx *ctx;
6142 isl_union_set_list *filters;
6144 if (graph->scc == 1)
6145 return compute_schedule_finish_band(node, &c->cluster[0], 0);
6147 ctx = isl_schedule_node_get_ctx(node);
6149 filters = extract_sccs(ctx, graph);
6150 node = isl_schedule_node_insert_sequence(node, filters);
6152 for (i = 0; i < graph->scc; ++i) {
6153 int j = c->scc_cluster[i];
6154 node = isl_schedule_node_child(node, i);
6155 node = isl_schedule_node_child(node, 0);
6156 node = compute_schedule_finish_band(node, &c->cluster[j], 0);
6157 node = isl_schedule_node_parent(node);
6158 node = isl_schedule_node_parent(node);
6161 return node;
6164 /* Compute a schedule for a connected dependence graph by first considering
6165 * each strongly connected component (SCC) in the graph separately and then
6166 * incrementally combining them into clusters.
6167 * Return the updated schedule node.
6169 * Initially, each cluster consists of a single SCC, each with its
6170 * own band schedule. The algorithm then tries to merge pairs
6171 * of clusters along a proximity edge until no more suitable
6172 * proximity edges can be found. During this merging, the schedule
6173 * is maintained in the individual SCCs.
6174 * After the merging is completed, the full resulting clusters
6175 * are extracted and in finish_bands_clustering,
6176 * compute_schedule_finish_band is called on each of them to integrate
6177 * the band into "node" and to continue the computation.
6179 * compute_weights initializes the weights that are used by find_proximity.
6181 static __isl_give isl_schedule_node *compute_schedule_wcc_clustering(
6182 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
6184 isl_ctx *ctx;
6185 struct isl_clustering c;
6186 int i;
6188 ctx = isl_schedule_node_get_ctx(node);
6190 if (clustering_init(ctx, &c, graph) < 0)
6191 goto error;
6193 if (compute_weights(graph, &c) < 0)
6194 goto error;
6196 for (;;) {
6197 i = find_proximity(graph, &c);
6198 if (i < 0)
6199 goto error;
6200 if (i >= graph->n_edge)
6201 break;
6202 if (merge_clusters_along_edge(ctx, graph, i, &c) < 0)
6203 goto error;
6206 if (extract_clusters(ctx, graph, &c) < 0)
6207 goto error;
6209 node = finish_bands_clustering(node, graph, &c);
6211 clustering_free(ctx, &c);
6212 return node;
6213 error:
6214 clustering_free(ctx, &c);
6215 return isl_schedule_node_free(node);
6218 /* Compute a schedule for a connected dependence graph and return
6219 * the updated schedule node.
6221 * If Feautrier's algorithm is selected, we first recursively try to satisfy
6222 * as many validity dependences as possible. When all validity dependences
6223 * are satisfied we extend the schedule to a full-dimensional schedule.
6225 * Call compute_schedule_wcc_whole or compute_schedule_wcc_clustering
6226 * depending on whether the user has selected the option to try and
6227 * compute a schedule for the entire (weakly connected) component first.
6228 * If there is only a single strongly connected component (SCC), then
6229 * there is no point in trying to combine SCCs
6230 * in compute_schedule_wcc_clustering, so compute_schedule_wcc_whole
6231 * is called instead.
6233 static __isl_give isl_schedule_node *compute_schedule_wcc(
6234 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
6236 isl_ctx *ctx;
6238 if (!node)
6239 return NULL;
6241 ctx = isl_schedule_node_get_ctx(node);
6242 if (detect_sccs(ctx, graph) < 0)
6243 return isl_schedule_node_free(node);
6245 if (compute_maxvar(graph) < 0)
6246 return isl_schedule_node_free(node);
6248 if (need_feautrier_step(ctx, graph))
6249 return compute_schedule_wcc_feautrier(node, graph);
6251 if (graph->scc <= 1 || isl_options_get_schedule_whole_component(ctx))
6252 return compute_schedule_wcc_whole(node, graph);
6253 else
6254 return compute_schedule_wcc_clustering(node, graph);
6257 /* Compute a schedule for each group of nodes identified by node->scc
6258 * separately and then combine them in a sequence node (or as set node
6259 * if graph->weak is set) inserted at position "node" of the schedule tree.
6260 * Return the updated schedule node.
6262 * If "wcc" is set then each of the groups belongs to a single
6263 * weakly connected component in the dependence graph so that
6264 * there is no need for compute_sub_schedule to look for weakly
6265 * connected components.
6267 static __isl_give isl_schedule_node *compute_component_schedule(
6268 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
6269 int wcc)
6271 int component;
6272 isl_ctx *ctx;
6273 isl_union_set_list *filters;
6275 if (!node)
6276 return NULL;
6277 ctx = isl_schedule_node_get_ctx(node);
6279 filters = extract_sccs(ctx, graph);
6280 if (graph->weak)
6281 node = isl_schedule_node_insert_set(node, filters);
6282 else
6283 node = isl_schedule_node_insert_sequence(node, filters);
6285 for (component = 0; component < graph->scc; ++component) {
6286 node = isl_schedule_node_child(node, component);
6287 node = isl_schedule_node_child(node, 0);
6288 node = compute_sub_schedule(node, ctx, graph,
6289 &node_scc_exactly,
6290 &edge_scc_exactly, component, wcc);
6291 node = isl_schedule_node_parent(node);
6292 node = isl_schedule_node_parent(node);
6295 return node;
6298 /* Compute a schedule for the given dependence graph and insert it at "node".
6299 * Return the updated schedule node.
6301 * We first check if the graph is connected (through validity and conditional
6302 * validity dependences) and, if not, compute a schedule
6303 * for each component separately.
6304 * If the schedule_serialize_sccs option is set, then we check for strongly
6305 * connected components instead and compute a separate schedule for
6306 * each such strongly connected component.
6308 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
6309 struct isl_sched_graph *graph)
6311 isl_ctx *ctx;
6313 if (!node)
6314 return NULL;
6316 ctx = isl_schedule_node_get_ctx(node);
6317 if (isl_options_get_schedule_serialize_sccs(ctx)) {
6318 if (detect_sccs(ctx, graph) < 0)
6319 return isl_schedule_node_free(node);
6320 } else {
6321 if (detect_wccs(ctx, graph) < 0)
6322 return isl_schedule_node_free(node);
6325 if (graph->scc > 1)
6326 return compute_component_schedule(node, graph, 1);
6328 return compute_schedule_wcc(node, graph);
6331 /* Compute a schedule on sc->domain that respects the given schedule
6332 * constraints.
6334 * In particular, the schedule respects all the validity dependences.
6335 * If the default isl scheduling algorithm is used, it tries to minimize
6336 * the dependence distances over the proximity dependences.
6337 * If Feautrier's scheduling algorithm is used, the proximity dependence
6338 * distances are only minimized during the extension to a full-dimensional
6339 * schedule.
6341 * If there are any condition and conditional validity dependences,
6342 * then the conditional validity dependences may be violated inside
6343 * a tilable band, provided they have no adjacent non-local
6344 * condition dependences.
6346 __isl_give isl_schedule *isl_schedule_constraints_compute_schedule(
6347 __isl_take isl_schedule_constraints *sc)
6349 isl_ctx *ctx = isl_schedule_constraints_get_ctx(sc);
6350 struct isl_sched_graph graph = { 0 };
6351 isl_schedule *sched;
6352 isl_schedule_node *node;
6353 isl_union_set *domain;
6355 sc = isl_schedule_constraints_align_params(sc);
6357 domain = isl_schedule_constraints_get_domain(sc);
6358 if (isl_union_set_n_set(domain) == 0) {
6359 isl_schedule_constraints_free(sc);
6360 return isl_schedule_from_domain(domain);
6363 if (graph_init(&graph, sc) < 0)
6364 domain = isl_union_set_free(domain);
6366 node = isl_schedule_node_from_domain(domain);
6367 node = isl_schedule_node_child(node, 0);
6368 if (graph.n > 0)
6369 node = compute_schedule(node, &graph);
6370 sched = isl_schedule_node_get_schedule(node);
6371 isl_schedule_node_free(node);
6373 graph_free(ctx, &graph);
6374 isl_schedule_constraints_free(sc);
6376 return sched;
6379 /* Compute a schedule for the given union of domains that respects
6380 * all the validity dependences and minimizes
6381 * the dependence distances over the proximity dependences.
6383 * This function is kept for backward compatibility.
6385 __isl_give isl_schedule *isl_union_set_compute_schedule(
6386 __isl_take isl_union_set *domain,
6387 __isl_take isl_union_map *validity,
6388 __isl_take isl_union_map *proximity)
6390 isl_schedule_constraints *sc;
6392 sc = isl_schedule_constraints_on_domain(domain);
6393 sc = isl_schedule_constraints_set_validity(sc, validity);
6394 sc = isl_schedule_constraints_set_proximity(sc, proximity);
6396 return isl_schedule_constraints_compute_schedule(sc);