2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2014 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_space_private.h>
16 #include <isl_aff_private.h>
18 #include <isl/constraint.h>
19 #include <isl/schedule.h>
20 #include <isl/schedule_node.h>
21 #include <isl_mat_private.h>
22 #include <isl_vec_private.h>
26 #include <isl_dim_map.h>
27 #include <isl/map_to_basic_set.h>
29 #include <isl_options_private.h>
30 #include <isl_tarjan.h>
31 #include <isl_morph.h>
34 * The scheduling algorithm implemented in this file was inspired by
35 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
36 * Parallelization and Locality Optimization in the Polyhedral Model".
40 isl_edge_validity
= 0,
41 isl_edge_first
= isl_edge_validity
,
44 isl_edge_conditional_validity
,
46 isl_edge_last
= isl_edge_proximity
49 /* The constraints that need to be satisfied by a schedule on "domain".
51 * "context" specifies extra constraints on the parameters.
53 * "validity" constraints map domain elements i to domain elements
54 * that should be scheduled after i. (Hard constraint)
55 * "proximity" constraints map domain elements i to domains elements
56 * that should be scheduled as early as possible after i (or before i).
59 * "condition" and "conditional_validity" constraints map possibly "tagged"
60 * domain elements i -> s to "tagged" domain elements j -> t.
61 * The elements of the "conditional_validity" constraints, but without the
62 * tags (i.e., the elements i -> j) are treated as validity constraints,
63 * except that during the construction of a tilable band,
64 * the elements of the "conditional_validity" constraints may be violated
65 * provided that all adjacent elements of the "condition" constraints
66 * are local within the band.
67 * A dependence is local within a band if domain and range are mapped
68 * to the same schedule point by the band.
70 struct isl_schedule_constraints
{
71 isl_union_set
*domain
;
74 isl_union_map
*constraint
[isl_edge_last
+ 1];
77 __isl_give isl_schedule_constraints
*isl_schedule_constraints_copy(
78 __isl_keep isl_schedule_constraints
*sc
)
81 isl_schedule_constraints
*sc_copy
;
84 ctx
= isl_union_set_get_ctx(sc
->domain
);
85 sc_copy
= isl_calloc_type(ctx
, struct isl_schedule_constraints
);
89 sc_copy
->domain
= isl_union_set_copy(sc
->domain
);
90 sc_copy
->context
= isl_set_copy(sc
->context
);
91 if (!sc_copy
->domain
|| !sc_copy
->context
)
92 return isl_schedule_constraints_free(sc_copy
);
94 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
95 sc_copy
->constraint
[i
] = isl_union_map_copy(sc
->constraint
[i
]);
96 if (!sc_copy
->constraint
[i
])
97 return isl_schedule_constraints_free(sc_copy
);
104 /* Construct an isl_schedule_constraints object for computing a schedule
105 * on "domain". The initial object does not impose any constraints.
107 __isl_give isl_schedule_constraints
*isl_schedule_constraints_on_domain(
108 __isl_take isl_union_set
*domain
)
112 isl_schedule_constraints
*sc
;
113 isl_union_map
*empty
;
114 enum isl_edge_type i
;
119 ctx
= isl_union_set_get_ctx(domain
);
120 sc
= isl_calloc_type(ctx
, struct isl_schedule_constraints
);
124 space
= isl_union_set_get_space(domain
);
126 sc
->context
= isl_set_universe(isl_space_copy(space
));
127 empty
= isl_union_map_empty(space
);
128 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
129 sc
->constraint
[i
] = isl_union_map_copy(empty
);
130 if (!sc
->constraint
[i
])
131 sc
->domain
= isl_union_set_free(sc
->domain
);
133 isl_union_map_free(empty
);
135 if (!sc
->domain
|| !sc
->context
)
136 return isl_schedule_constraints_free(sc
);
140 isl_union_set_free(domain
);
144 /* Replace the context of "sc" by "context".
146 __isl_give isl_schedule_constraints
*isl_schedule_constraints_set_context(
147 __isl_take isl_schedule_constraints
*sc
, __isl_take isl_set
*context
)
152 isl_set_free(sc
->context
);
153 sc
->context
= context
;
157 isl_schedule_constraints_free(sc
);
158 isl_set_free(context
);
162 /* Replace the validity constraints of "sc" by "validity".
164 __isl_give isl_schedule_constraints
*isl_schedule_constraints_set_validity(
165 __isl_take isl_schedule_constraints
*sc
,
166 __isl_take isl_union_map
*validity
)
168 if (!sc
|| !validity
)
171 isl_union_map_free(sc
->constraint
[isl_edge_validity
]);
172 sc
->constraint
[isl_edge_validity
] = validity
;
176 isl_schedule_constraints_free(sc
);
177 isl_union_map_free(validity
);
181 /* Replace the coincidence constraints of "sc" by "coincidence".
183 __isl_give isl_schedule_constraints
*isl_schedule_constraints_set_coincidence(
184 __isl_take isl_schedule_constraints
*sc
,
185 __isl_take isl_union_map
*coincidence
)
187 if (!sc
|| !coincidence
)
190 isl_union_map_free(sc
->constraint
[isl_edge_coincidence
]);
191 sc
->constraint
[isl_edge_coincidence
] = coincidence
;
195 isl_schedule_constraints_free(sc
);
196 isl_union_map_free(coincidence
);
200 /* Replace the proximity constraints of "sc" by "proximity".
202 __isl_give isl_schedule_constraints
*isl_schedule_constraints_set_proximity(
203 __isl_take isl_schedule_constraints
*sc
,
204 __isl_take isl_union_map
*proximity
)
206 if (!sc
|| !proximity
)
209 isl_union_map_free(sc
->constraint
[isl_edge_proximity
]);
210 sc
->constraint
[isl_edge_proximity
] = proximity
;
214 isl_schedule_constraints_free(sc
);
215 isl_union_map_free(proximity
);
219 /* Replace the conditional validity constraints of "sc" by "condition"
222 __isl_give isl_schedule_constraints
*
223 isl_schedule_constraints_set_conditional_validity(
224 __isl_take isl_schedule_constraints
*sc
,
225 __isl_take isl_union_map
*condition
,
226 __isl_take isl_union_map
*validity
)
228 if (!sc
|| !condition
|| !validity
)
231 isl_union_map_free(sc
->constraint
[isl_edge_condition
]);
232 sc
->constraint
[isl_edge_condition
] = condition
;
233 isl_union_map_free(sc
->constraint
[isl_edge_conditional_validity
]);
234 sc
->constraint
[isl_edge_conditional_validity
] = validity
;
238 isl_schedule_constraints_free(sc
);
239 isl_union_map_free(condition
);
240 isl_union_map_free(validity
);
244 __isl_null isl_schedule_constraints
*isl_schedule_constraints_free(
245 __isl_take isl_schedule_constraints
*sc
)
247 enum isl_edge_type i
;
252 isl_union_set_free(sc
->domain
);
253 isl_set_free(sc
->context
);
254 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
255 isl_union_map_free(sc
->constraint
[i
]);
262 isl_ctx
*isl_schedule_constraints_get_ctx(
263 __isl_keep isl_schedule_constraints
*sc
)
265 return sc
? isl_union_set_get_ctx(sc
->domain
) : NULL
;
268 void isl_schedule_constraints_dump(__isl_keep isl_schedule_constraints
*sc
)
273 fprintf(stderr
, "domain: ");
274 isl_union_set_dump(sc
->domain
);
275 fprintf(stderr
, "context: ");
276 isl_set_dump(sc
->context
);
277 fprintf(stderr
, "validity: ");
278 isl_union_map_dump(sc
->constraint
[isl_edge_validity
]);
279 fprintf(stderr
, "proximity: ");
280 isl_union_map_dump(sc
->constraint
[isl_edge_proximity
]);
281 fprintf(stderr
, "coincidence: ");
282 isl_union_map_dump(sc
->constraint
[isl_edge_coincidence
]);
283 fprintf(stderr
, "condition: ");
284 isl_union_map_dump(sc
->constraint
[isl_edge_condition
]);
285 fprintf(stderr
, "conditional_validity: ");
286 isl_union_map_dump(sc
->constraint
[isl_edge_conditional_validity
]);
289 /* Align the parameters of the fields of "sc".
291 static __isl_give isl_schedule_constraints
*
292 isl_schedule_constraints_align_params(__isl_take isl_schedule_constraints
*sc
)
295 enum isl_edge_type i
;
300 space
= isl_union_set_get_space(sc
->domain
);
301 space
= isl_space_align_params(space
, isl_set_get_space(sc
->context
));
302 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
303 space
= isl_space_align_params(space
,
304 isl_union_map_get_space(sc
->constraint
[i
]));
306 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
307 sc
->constraint
[i
] = isl_union_map_align_params(
308 sc
->constraint
[i
], isl_space_copy(space
));
309 if (!sc
->constraint
[i
])
310 space
= isl_space_free(space
);
312 sc
->context
= isl_set_align_params(sc
->context
, isl_space_copy(space
));
313 sc
->domain
= isl_union_set_align_params(sc
->domain
, space
);
314 if (!sc
->context
|| !sc
->domain
)
315 return isl_schedule_constraints_free(sc
);
320 /* Return the total number of isl_maps in the constraints of "sc".
322 static __isl_give
int isl_schedule_constraints_n_map(
323 __isl_keep isl_schedule_constraints
*sc
)
325 enum isl_edge_type i
;
328 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
329 n
+= isl_union_map_n_map(sc
->constraint
[i
]);
334 /* Internal information about a node that is used during the construction
336 * space represents the space in which the domain lives
337 * sched is a matrix representation of the schedule being constructed
338 * for this node; if compressed is set, then this schedule is
339 * defined over the compressed domain space
340 * sched_map is an isl_map representation of the same (partial) schedule
341 * sched_map may be NULL; if compressed is set, then this map
342 * is defined over the uncompressed domain space
343 * rank is the number of linearly independent rows in the linear part
345 * the columns of cmap represent a change of basis for the schedule
346 * coefficients; the first rank columns span the linear part of
348 * cinv is the inverse of cmap.
349 * start is the first variable in the LP problem in the sequences that
350 * represents the schedule coefficients of this node
351 * nvar is the dimension of the domain
352 * nparam is the number of parameters or 0 if we are not constructing
353 * a parametric schedule
355 * If compressed is set, then hull represents the constraints
356 * that were used to derive the compression, while compress and
357 * decompress map the original space to the compressed space and
360 * scc is the index of SCC (or WCC) this node belongs to
362 * coincident contains a boolean for each of the rows of the schedule,
363 * indicating whether the corresponding scheduling dimension satisfies
364 * the coincidence constraints in the sense that the corresponding
365 * dependence distances are zero.
367 struct isl_sched_node
{
371 isl_multi_aff
*compress
;
372 isl_multi_aff
*decompress
;
387 static int node_has_space(const void *entry
, const void *val
)
389 struct isl_sched_node
*node
= (struct isl_sched_node
*)entry
;
390 isl_space
*dim
= (isl_space
*)val
;
392 return isl_space_is_equal(node
->space
, dim
);
395 static int node_scc_exactly(struct isl_sched_node
*node
, int scc
)
397 return node
->scc
== scc
;
400 static int node_scc_at_most(struct isl_sched_node
*node
, int scc
)
402 return node
->scc
<= scc
;
405 static int node_scc_at_least(struct isl_sched_node
*node
, int scc
)
407 return node
->scc
>= scc
;
410 /* An edge in the dependence graph. An edge may be used to
411 * ensure validity of the generated schedule, to minimize the dependence
414 * map is the dependence relation, with i -> j in the map if j depends on i
415 * tagged_condition and tagged_validity contain the union of all tagged
416 * condition or conditional validity dependence relations that
417 * specialize the dependence relation "map"; that is,
418 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
419 * or "tagged_validity", then i -> j is an element of "map".
420 * If these fields are NULL, then they represent the empty relation.
421 * src is the source node
422 * dst is the sink node
423 * validity is set if the edge is used to ensure correctness
424 * coincidence is used to enforce zero dependence distances
425 * proximity is set if the edge is used to minimize dependence distances
426 * condition is set if the edge represents a condition
427 * for a conditional validity schedule constraint
428 * local can only be set for condition edges and indicates that
429 * the dependence distance over the edge should be zero
430 * conditional_validity is set if the edge is used to conditionally
433 * For validity edges, start and end mark the sequence of inequality
434 * constraints in the LP problem that encode the validity constraint
435 * corresponding to this edge.
437 struct isl_sched_edge
{
439 isl_union_map
*tagged_condition
;
440 isl_union_map
*tagged_validity
;
442 struct isl_sched_node
*src
;
443 struct isl_sched_node
*dst
;
445 unsigned validity
: 1;
446 unsigned coincidence
: 1;
447 unsigned proximity
: 1;
449 unsigned condition
: 1;
450 unsigned conditional_validity
: 1;
456 /* Internal information about the dependence graph used during
457 * the construction of the schedule.
459 * intra_hmap is a cache, mapping dependence relations to their dual,
460 * for dependences from a node to itself
461 * inter_hmap is a cache, mapping dependence relations to their dual,
462 * for dependences between distinct nodes
463 * if compression is involved then the key for these maps
464 * it the original, uncompressed dependence relation, while
465 * the value is the dual of the compressed dependence relation.
467 * n is the number of nodes
468 * node is the list of nodes
469 * maxvar is the maximal number of variables over all nodes
470 * max_row is the allocated number of rows in the schedule
471 * n_row is the current (maximal) number of linearly independent
472 * rows in the node schedules
473 * n_total_row is the current number of rows in the node schedules
474 * band_start is the starting row in the node schedules of the current band
475 * root is set if this graph is the original dependence graph,
476 * without any splitting
478 * sorted contains a list of node indices sorted according to the
479 * SCC to which a node belongs
481 * n_edge is the number of edges
482 * edge is the list of edges
483 * max_edge contains the maximal number of edges of each type;
484 * in particular, it contains the number of edges in the inital graph.
485 * edge_table contains pointers into the edge array, hashed on the source
486 * and sink spaces; there is one such table for each type;
487 * a given edge may be referenced from more than one table
488 * if the corresponding relation appears in more than of the
489 * sets of dependences
491 * node_table contains pointers into the node array, hashed on the space
493 * region contains a list of variable sequences that should be non-trivial
495 * lp contains the (I)LP problem used to obtain new schedule rows
497 * src_scc and dst_scc are the source and sink SCCs of an edge with
498 * conflicting constraints
500 * scc represents the number of components
501 * weak is set if the components are weakly connected
503 struct isl_sched_graph
{
504 isl_map_to_basic_set
*intra_hmap
;
505 isl_map_to_basic_set
*inter_hmap
;
507 struct isl_sched_node
*node
;
520 struct isl_sched_edge
*edge
;
522 int max_edge
[isl_edge_last
+ 1];
523 struct isl_hash_table
*edge_table
[isl_edge_last
+ 1];
525 struct isl_hash_table
*node_table
;
526 struct isl_region
*region
;
537 /* Initialize node_table based on the list of nodes.
539 static int graph_init_table(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
543 graph
->node_table
= isl_hash_table_alloc(ctx
, graph
->n
);
544 if (!graph
->node_table
)
547 for (i
= 0; i
< graph
->n
; ++i
) {
548 struct isl_hash_table_entry
*entry
;
551 hash
= isl_space_get_hash(graph
->node
[i
].space
);
552 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
554 graph
->node
[i
].space
, 1);
557 entry
->data
= &graph
->node
[i
];
563 /* Return a pointer to the node that lives within the given space,
564 * or NULL if there is no such node.
566 static struct isl_sched_node
*graph_find_node(isl_ctx
*ctx
,
567 struct isl_sched_graph
*graph
, __isl_keep isl_space
*dim
)
569 struct isl_hash_table_entry
*entry
;
572 hash
= isl_space_get_hash(dim
);
573 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
574 &node_has_space
, dim
, 0);
576 return entry
? entry
->data
: NULL
;
579 static int edge_has_src_and_dst(const void *entry
, const void *val
)
581 const struct isl_sched_edge
*edge
= entry
;
582 const struct isl_sched_edge
*temp
= val
;
584 return edge
->src
== temp
->src
&& edge
->dst
== temp
->dst
;
587 /* Add the given edge to graph->edge_table[type].
589 static int graph_edge_table_add(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
590 enum isl_edge_type type
, struct isl_sched_edge
*edge
)
592 struct isl_hash_table_entry
*entry
;
595 hash
= isl_hash_init();
596 hash
= isl_hash_builtin(hash
, edge
->src
);
597 hash
= isl_hash_builtin(hash
, edge
->dst
);
598 entry
= isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
599 &edge_has_src_and_dst
, edge
, 1);
607 /* Allocate the edge_tables based on the maximal number of edges of
610 static int graph_init_edge_tables(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
614 for (i
= 0; i
<= isl_edge_last
; ++i
) {
615 graph
->edge_table
[i
] = isl_hash_table_alloc(ctx
,
617 if (!graph
->edge_table
[i
])
624 /* If graph->edge_table[type] contains an edge from the given source
625 * to the given destination, then return the hash table entry of this edge.
626 * Otherwise, return NULL.
628 static struct isl_hash_table_entry
*graph_find_edge_entry(
629 struct isl_sched_graph
*graph
,
630 enum isl_edge_type type
,
631 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
633 isl_ctx
*ctx
= isl_space_get_ctx(src
->space
);
635 struct isl_sched_edge temp
= { .src
= src
, .dst
= dst
};
637 hash
= isl_hash_init();
638 hash
= isl_hash_builtin(hash
, temp
.src
);
639 hash
= isl_hash_builtin(hash
, temp
.dst
);
640 return isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
641 &edge_has_src_and_dst
, &temp
, 0);
645 /* If graph->edge_table[type] contains an edge from the given source
646 * to the given destination, then return this edge.
647 * Otherwise, return NULL.
649 static struct isl_sched_edge
*graph_find_edge(struct isl_sched_graph
*graph
,
650 enum isl_edge_type type
,
651 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
653 struct isl_hash_table_entry
*entry
;
655 entry
= graph_find_edge_entry(graph
, type
, src
, dst
);
662 /* Check whether the dependence graph has an edge of the given type
663 * between the given two nodes.
665 static int graph_has_edge(struct isl_sched_graph
*graph
,
666 enum isl_edge_type type
,
667 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
669 struct isl_sched_edge
*edge
;
672 edge
= graph_find_edge(graph
, type
, src
, dst
);
676 empty
= isl_map_plain_is_empty(edge
->map
);
683 /* Look for any edge with the same src, dst and map fields as "model".
685 * Return the matching edge if one can be found.
686 * Return "model" if no matching edge is found.
687 * Return NULL on error.
689 static struct isl_sched_edge
*graph_find_matching_edge(
690 struct isl_sched_graph
*graph
, struct isl_sched_edge
*model
)
692 enum isl_edge_type i
;
693 struct isl_sched_edge
*edge
;
695 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
698 edge
= graph_find_edge(graph
, i
, model
->src
, model
->dst
);
701 is_equal
= isl_map_plain_is_equal(model
->map
, edge
->map
);
711 /* Remove the given edge from all the edge_tables that refer to it.
713 static void graph_remove_edge(struct isl_sched_graph
*graph
,
714 struct isl_sched_edge
*edge
)
716 isl_ctx
*ctx
= isl_map_get_ctx(edge
->map
);
717 enum isl_edge_type i
;
719 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
720 struct isl_hash_table_entry
*entry
;
722 entry
= graph_find_edge_entry(graph
, i
, edge
->src
, edge
->dst
);
725 if (entry
->data
!= edge
)
727 isl_hash_table_remove(ctx
, graph
->edge_table
[i
], entry
);
731 /* Check whether the dependence graph has any edge
732 * between the given two nodes.
734 static int graph_has_any_edge(struct isl_sched_graph
*graph
,
735 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
737 enum isl_edge_type i
;
740 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
741 r
= graph_has_edge(graph
, i
, src
, dst
);
749 /* Check whether the dependence graph has a validity edge
750 * between the given two nodes.
752 * Conditional validity edges are essentially validity edges that
753 * can be ignored if the corresponding condition edges are iteration private.
754 * Here, we are only checking for the presence of validity
755 * edges, so we need to consider the conditional validity edges too.
756 * In particular, this function is used during the detection
757 * of strongly connected components and we cannot ignore
758 * conditional validity edges during this detection.
760 static int graph_has_validity_edge(struct isl_sched_graph
*graph
,
761 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
765 r
= graph_has_edge(graph
, isl_edge_validity
, src
, dst
);
769 return graph_has_edge(graph
, isl_edge_conditional_validity
, src
, dst
);
772 static int graph_alloc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
773 int n_node
, int n_edge
)
778 graph
->n_edge
= n_edge
;
779 graph
->node
= isl_calloc_array(ctx
, struct isl_sched_node
, graph
->n
);
780 graph
->sorted
= isl_calloc_array(ctx
, int, graph
->n
);
781 graph
->region
= isl_alloc_array(ctx
, struct isl_region
, graph
->n
);
782 graph
->edge
= isl_calloc_array(ctx
,
783 struct isl_sched_edge
, graph
->n_edge
);
785 graph
->intra_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
786 graph
->inter_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
788 if (!graph
->node
|| !graph
->region
|| (graph
->n_edge
&& !graph
->edge
) ||
792 for(i
= 0; i
< graph
->n
; ++i
)
793 graph
->sorted
[i
] = i
;
798 static void graph_free(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
802 isl_map_to_basic_set_free(graph
->intra_hmap
);
803 isl_map_to_basic_set_free(graph
->inter_hmap
);
806 for (i
= 0; i
< graph
->n
; ++i
) {
807 isl_space_free(graph
->node
[i
].space
);
808 isl_set_free(graph
->node
[i
].hull
);
809 isl_multi_aff_free(graph
->node
[i
].compress
);
810 isl_multi_aff_free(graph
->node
[i
].decompress
);
811 isl_mat_free(graph
->node
[i
].sched
);
812 isl_map_free(graph
->node
[i
].sched_map
);
813 isl_mat_free(graph
->node
[i
].cmap
);
814 isl_mat_free(graph
->node
[i
].cinv
);
816 free(graph
->node
[i
].coincident
);
821 for (i
= 0; i
< graph
->n_edge
; ++i
) {
822 isl_map_free(graph
->edge
[i
].map
);
823 isl_union_map_free(graph
->edge
[i
].tagged_condition
);
824 isl_union_map_free(graph
->edge
[i
].tagged_validity
);
828 for (i
= 0; i
<= isl_edge_last
; ++i
)
829 isl_hash_table_free(ctx
, graph
->edge_table
[i
]);
830 isl_hash_table_free(ctx
, graph
->node_table
);
831 isl_basic_set_free(graph
->lp
);
834 /* For each "set" on which this function is called, increment
835 * graph->n by one and update graph->maxvar.
837 static int init_n_maxvar(__isl_take isl_set
*set
, void *user
)
839 struct isl_sched_graph
*graph
= user
;
840 int nvar
= isl_set_dim(set
, isl_dim_set
);
843 if (nvar
> graph
->maxvar
)
844 graph
->maxvar
= nvar
;
851 /* Add the number of basic maps in "map" to *n.
853 static int add_n_basic_map(__isl_take isl_map
*map
, void *user
)
857 *n
+= isl_map_n_basic_map(map
);
863 /* Compute the number of rows that should be allocated for the schedule.
864 * In particular, we need one row for each variable or one row
865 * for each basic map in the dependences.
866 * Note that it is practically impossible to exhaust both
867 * the number of dependences and the number of variables.
869 static int compute_max_row(struct isl_sched_graph
*graph
,
870 __isl_keep isl_schedule_constraints
*sc
)
872 enum isl_edge_type i
;
877 if (isl_union_set_foreach_set(sc
->domain
, &init_n_maxvar
, graph
) < 0)
880 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
881 if (isl_union_map_foreach_map(sc
->constraint
[i
],
882 &add_n_basic_map
, &n_edge
) < 0)
884 graph
->max_row
= n_edge
+ graph
->maxvar
;
889 /* Does "bset" have any defining equalities for its set variables?
891 static int has_any_defining_equality(__isl_keep isl_basic_set
*bset
)
898 n
= isl_basic_set_dim(bset
, isl_dim_set
);
899 for (i
= 0; i
< n
; ++i
) {
902 has
= isl_basic_set_has_defining_equality(bset
, isl_dim_set
, i
,
911 /* Add a new node to the graph representing the given space.
912 * "nvar" is the (possibly compressed) number of variables and
913 * may be smaller than then number of set variables in "space"
914 * if "compressed" is set.
915 * If "compressed" is set, then "hull" represents the constraints
916 * that were used to derive the compression, while "compress" and
917 * "decompress" map the original space to the compressed space and
919 * If "compressed" is not set, then "hull", "compress" and "decompress"
922 static int add_node(struct isl_sched_graph
*graph
, __isl_take isl_space
*space
,
923 int nvar
, int compressed
, __isl_take isl_set
*hull
,
924 __isl_take isl_multi_aff
*compress
,
925 __isl_take isl_multi_aff
*decompress
)
935 ctx
= isl_space_get_ctx(space
);
936 nparam
= isl_space_dim(space
, isl_dim_param
);
937 if (!ctx
->opt
->schedule_parametric
)
939 sched
= isl_mat_alloc(ctx
, 0, 1 + nparam
+ nvar
);
940 graph
->node
[graph
->n
].space
= space
;
941 graph
->node
[graph
->n
].nvar
= nvar
;
942 graph
->node
[graph
->n
].nparam
= nparam
;
943 graph
->node
[graph
->n
].sched
= sched
;
944 graph
->node
[graph
->n
].sched_map
= NULL
;
945 coincident
= isl_calloc_array(ctx
, int, graph
->max_row
);
946 graph
->node
[graph
->n
].coincident
= coincident
;
947 graph
->node
[graph
->n
].compressed
= compressed
;
948 graph
->node
[graph
->n
].hull
= hull
;
949 graph
->node
[graph
->n
].compress
= compress
;
950 graph
->node
[graph
->n
].decompress
= decompress
;
953 if (!space
|| !sched
|| (graph
->max_row
&& !coincident
))
955 if (compressed
&& (!hull
|| !compress
|| !decompress
))
961 /* Add a new node to the graph representing the given set.
963 * If any of the set variables is defined by an equality, then
964 * we perform variable compression such that we can perform
965 * the scheduling on the compressed domain.
967 static int extract_node(__isl_take isl_set
*set
, void *user
)
975 isl_multi_aff
*compress
, *decompress
;
976 struct isl_sched_graph
*graph
= user
;
978 space
= isl_set_get_space(set
);
979 hull
= isl_set_affine_hull(set
);
980 hull
= isl_basic_set_remove_divs(hull
);
981 nvar
= isl_space_dim(space
, isl_dim_set
);
982 has_equality
= has_any_defining_equality(hull
);
984 if (has_equality
< 0)
987 isl_basic_set_free(hull
);
988 return add_node(graph
, space
, nvar
, 0, NULL
, NULL
, NULL
);
991 morph
= isl_basic_set_variable_compression(hull
, isl_dim_set
);
992 nvar
= isl_morph_ran_dim(morph
, isl_dim_set
);
993 compress
= isl_morph_get_var_multi_aff(morph
);
994 morph
= isl_morph_inverse(morph
);
995 decompress
= isl_morph_get_var_multi_aff(morph
);
996 isl_morph_free(morph
);
998 hull_set
= isl_set_from_basic_set(hull
);
999 return add_node(graph
, space
, nvar
, 1, hull_set
, compress
, decompress
);
1001 isl_basic_set_free(hull
);
1002 isl_space_free(space
);
1006 struct isl_extract_edge_data
{
1007 enum isl_edge_type type
;
1008 struct isl_sched_graph
*graph
;
1011 /* Merge edge2 into edge1, freeing the contents of edge2.
1012 * "type" is the type of the schedule constraint from which edge2 was
1014 * Return 0 on success and -1 on failure.
1016 * edge1 and edge2 are assumed to have the same value for the map field.
1018 static int merge_edge(enum isl_edge_type type
, struct isl_sched_edge
*edge1
,
1019 struct isl_sched_edge
*edge2
)
1021 edge1
->validity
|= edge2
->validity
;
1022 edge1
->coincidence
|= edge2
->coincidence
;
1023 edge1
->proximity
|= edge2
->proximity
;
1024 edge1
->condition
|= edge2
->condition
;
1025 edge1
->conditional_validity
|= edge2
->conditional_validity
;
1026 isl_map_free(edge2
->map
);
1028 if (type
== isl_edge_condition
) {
1029 if (!edge1
->tagged_condition
)
1030 edge1
->tagged_condition
= edge2
->tagged_condition
;
1032 edge1
->tagged_condition
=
1033 isl_union_map_union(edge1
->tagged_condition
,
1034 edge2
->tagged_condition
);
1037 if (type
== isl_edge_conditional_validity
) {
1038 if (!edge1
->tagged_validity
)
1039 edge1
->tagged_validity
= edge2
->tagged_validity
;
1041 edge1
->tagged_validity
=
1042 isl_union_map_union(edge1
->tagged_validity
,
1043 edge2
->tagged_validity
);
1046 if (type
== isl_edge_condition
&& !edge1
->tagged_condition
)
1048 if (type
== isl_edge_conditional_validity
&& !edge1
->tagged_validity
)
1054 /* Insert dummy tags in domain and range of "map".
1056 * In particular, if "map" is of the form
1062 * [A -> dummy_tag] -> [B -> dummy_tag]
1064 * where the dummy_tags are identical and equal to any dummy tags
1065 * introduced by any other call to this function.
1067 static __isl_give isl_map
*insert_dummy_tags(__isl_take isl_map
*map
)
1073 isl_set
*domain
, *range
;
1075 ctx
= isl_map_get_ctx(map
);
1077 id
= isl_id_alloc(ctx
, NULL
, &dummy
);
1078 space
= isl_space_params(isl_map_get_space(map
));
1079 space
= isl_space_set_from_params(space
);
1080 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
1081 space
= isl_space_map_from_set(space
);
1083 domain
= isl_map_wrap(map
);
1084 range
= isl_map_wrap(isl_map_universe(space
));
1085 map
= isl_map_from_domain_and_range(domain
, range
);
1086 map
= isl_map_zip(map
);
1091 /* Given that at least one of "src" or "dst" is compressed, return
1092 * a map between the spaces of these nodes restricted to the affine
1093 * hull that was used in the compression.
1095 static __isl_give isl_map
*extract_hull(struct isl_sched_node
*src
,
1096 struct isl_sched_node
*dst
)
1100 if (src
->compressed
)
1101 dom
= isl_set_copy(src
->hull
);
1103 dom
= isl_set_universe(isl_space_copy(src
->space
));
1104 if (dst
->compressed
)
1105 ran
= isl_set_copy(dst
->hull
);
1107 ran
= isl_set_universe(isl_space_copy(dst
->space
));
1109 return isl_map_from_domain_and_range(dom
, ran
);
1112 /* Intersect the domains of the nested relations in domain and range
1113 * of "tagged" with "map".
1115 static __isl_give isl_map
*map_intersect_domains(__isl_take isl_map
*tagged
,
1116 __isl_keep isl_map
*map
)
1120 tagged
= isl_map_zip(tagged
);
1121 set
= isl_map_wrap(isl_map_copy(map
));
1122 tagged
= isl_map_intersect_domain(tagged
, set
);
1123 tagged
= isl_map_zip(tagged
);
1127 /* Add a new edge to the graph based on the given map
1128 * and add it to data->graph->edge_table[data->type].
1129 * If a dependence relation of a given type happens to be identical
1130 * to one of the dependence relations of a type that was added before,
1131 * then we don't create a new edge, but instead mark the original edge
1132 * as also representing a dependence of the current type.
1134 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1135 * may be specified as "tagged" dependence relations. That is, "map"
1136 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1137 * the dependence on iterations and a and b are tags.
1138 * edge->map is set to the relation containing the elements i -> j,
1139 * while edge->tagged_condition and edge->tagged_validity contain
1140 * the union of all the "map" relations
1141 * for which extract_edge is called that result in the same edge->map.
1143 * If the source or the destination node is compressed, then
1144 * intersect both "map" and "tagged" with the constraints that
1145 * were used to construct the compression.
1146 * This ensures that there are no schedule constraints defined
1147 * outside of these domains, while the scheduler no longer has
1148 * any control over those outside parts.
1150 static int extract_edge(__isl_take isl_map
*map
, void *user
)
1152 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1153 struct isl_extract_edge_data
*data
= user
;
1154 struct isl_sched_graph
*graph
= data
->graph
;
1155 struct isl_sched_node
*src
, *dst
;
1157 struct isl_sched_edge
*edge
;
1158 isl_map
*tagged
= NULL
;
1160 if (data
->type
== isl_edge_condition
||
1161 data
->type
== isl_edge_conditional_validity
) {
1162 if (isl_map_can_zip(map
)) {
1163 tagged
= isl_map_copy(map
);
1164 map
= isl_set_unwrap(isl_map_domain(isl_map_zip(map
)));
1166 tagged
= insert_dummy_tags(isl_map_copy(map
));
1170 dim
= isl_space_domain(isl_map_get_space(map
));
1171 src
= graph_find_node(ctx
, graph
, dim
);
1172 isl_space_free(dim
);
1173 dim
= isl_space_range(isl_map_get_space(map
));
1174 dst
= graph_find_node(ctx
, graph
, dim
);
1175 isl_space_free(dim
);
1179 isl_map_free(tagged
);
1183 if (src
->compressed
|| dst
->compressed
) {
1185 hull
= extract_hull(src
, dst
);
1187 tagged
= map_intersect_domains(tagged
, hull
);
1188 map
= isl_map_intersect(map
, hull
);
1191 graph
->edge
[graph
->n_edge
].src
= src
;
1192 graph
->edge
[graph
->n_edge
].dst
= dst
;
1193 graph
->edge
[graph
->n_edge
].map
= map
;
1194 graph
->edge
[graph
->n_edge
].validity
= 0;
1195 graph
->edge
[graph
->n_edge
].coincidence
= 0;
1196 graph
->edge
[graph
->n_edge
].proximity
= 0;
1197 graph
->edge
[graph
->n_edge
].condition
= 0;
1198 graph
->edge
[graph
->n_edge
].local
= 0;
1199 graph
->edge
[graph
->n_edge
].conditional_validity
= 0;
1200 graph
->edge
[graph
->n_edge
].tagged_condition
= NULL
;
1201 graph
->edge
[graph
->n_edge
].tagged_validity
= NULL
;
1202 if (data
->type
== isl_edge_validity
)
1203 graph
->edge
[graph
->n_edge
].validity
= 1;
1204 if (data
->type
== isl_edge_coincidence
)
1205 graph
->edge
[graph
->n_edge
].coincidence
= 1;
1206 if (data
->type
== isl_edge_proximity
)
1207 graph
->edge
[graph
->n_edge
].proximity
= 1;
1208 if (data
->type
== isl_edge_condition
) {
1209 graph
->edge
[graph
->n_edge
].condition
= 1;
1210 graph
->edge
[graph
->n_edge
].tagged_condition
=
1211 isl_union_map_from_map(tagged
);
1213 if (data
->type
== isl_edge_conditional_validity
) {
1214 graph
->edge
[graph
->n_edge
].conditional_validity
= 1;
1215 graph
->edge
[graph
->n_edge
].tagged_validity
=
1216 isl_union_map_from_map(tagged
);
1219 edge
= graph_find_matching_edge(graph
, &graph
->edge
[graph
->n_edge
]);
1224 if (edge
== &graph
->edge
[graph
->n_edge
])
1225 return graph_edge_table_add(ctx
, graph
, data
->type
,
1226 &graph
->edge
[graph
->n_edge
++]);
1228 if (merge_edge(data
->type
, edge
, &graph
->edge
[graph
->n_edge
]) < 0)
1231 return graph_edge_table_add(ctx
, graph
, data
->type
, edge
);
1234 /* Check whether there is any dependence from node[j] to node[i]
1235 * or from node[i] to node[j].
1237 static int node_follows_weak(int i
, int j
, void *user
)
1240 struct isl_sched_graph
*graph
= user
;
1242 f
= graph_has_any_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1245 return graph_has_any_edge(graph
, &graph
->node
[i
], &graph
->node
[j
]);
1248 /* Check whether there is a (conditional) validity dependence from node[j]
1249 * to node[i], forcing node[i] to follow node[j].
1251 static int node_follows_strong(int i
, int j
, void *user
)
1253 struct isl_sched_graph
*graph
= user
;
1255 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1258 /* Use Tarjan's algorithm for computing the strongly connected components
1259 * in the dependence graph (only validity edges).
1260 * If weak is set, we consider the graph to be undirected and
1261 * we effectively compute the (weakly) connected components.
1262 * Additionally, we also consider other edges when weak is set.
1264 static int detect_ccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
, int weak
)
1267 struct isl_tarjan_graph
*g
= NULL
;
1269 g
= isl_tarjan_graph_init(ctx
, graph
->n
,
1270 weak
? &node_follows_weak
: &node_follows_strong
, graph
);
1279 while (g
->order
[i
] != -1) {
1280 graph
->node
[g
->order
[i
]].scc
= graph
->scc
;
1288 isl_tarjan_graph_free(g
);
1293 /* Apply Tarjan's algorithm to detect the strongly connected components
1294 * in the dependence graph.
1296 static int detect_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1298 return detect_ccs(ctx
, graph
, 0);
1301 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1302 * in the dependence graph.
1304 static int detect_wccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1306 return detect_ccs(ctx
, graph
, 1);
1309 static int cmp_scc(const void *a
, const void *b
, void *data
)
1311 struct isl_sched_graph
*graph
= data
;
1315 return graph
->node
[*i1
].scc
- graph
->node
[*i2
].scc
;
1318 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1320 static int sort_sccs(struct isl_sched_graph
*graph
)
1322 return isl_sort(graph
->sorted
, graph
->n
, sizeof(int), &cmp_scc
, graph
);
1325 /* Given a dependence relation R from "node" to itself,
1326 * construct the set of coefficients of valid constraints for elements
1327 * in that dependence relation.
1328 * In particular, the result contains tuples of coefficients
1329 * c_0, c_n, c_x such that
1331 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1335 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1337 * We choose here to compute the dual of delta R.
1338 * Alternatively, we could have computed the dual of R, resulting
1339 * in a set of tuples c_0, c_n, c_x, c_y, and then
1340 * plugged in (c_0, c_n, c_x, -c_x).
1342 * If "node" has been compressed, then the dependence relation
1343 * is also compressed before the set of coefficients is computed.
1345 static __isl_give isl_basic_set
*intra_coefficients(
1346 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1347 __isl_take isl_map
*map
)
1351 isl_basic_set
*coef
;
1353 if (isl_map_to_basic_set_has(graph
->intra_hmap
, map
))
1354 return isl_map_to_basic_set_get(graph
->intra_hmap
, map
);
1356 key
= isl_map_copy(map
);
1357 if (node
->compressed
) {
1358 map
= isl_map_preimage_domain_multi_aff(map
,
1359 isl_multi_aff_copy(node
->decompress
));
1360 map
= isl_map_preimage_range_multi_aff(map
,
1361 isl_multi_aff_copy(node
->decompress
));
1363 delta
= isl_set_remove_divs(isl_map_deltas(map
));
1364 coef
= isl_set_coefficients(delta
);
1365 graph
->intra_hmap
= isl_map_to_basic_set_set(graph
->intra_hmap
, key
,
1366 isl_basic_set_copy(coef
));
1371 /* Given a dependence relation R, construct the set of coefficients
1372 * of valid constraints for elements in that dependence relation.
1373 * In particular, the result contains tuples of coefficients
1374 * c_0, c_n, c_x, c_y such that
1376 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1378 * If the source or destination nodes of "edge" have been compressed,
1379 * then the dependence relation is also compressed before
1380 * the set of coefficients is computed.
1382 static __isl_give isl_basic_set
*inter_coefficients(
1383 struct isl_sched_graph
*graph
, struct isl_sched_edge
*edge
,
1384 __isl_take isl_map
*map
)
1388 isl_basic_set
*coef
;
1390 if (isl_map_to_basic_set_has(graph
->inter_hmap
, map
))
1391 return isl_map_to_basic_set_get(graph
->inter_hmap
, map
);
1393 key
= isl_map_copy(map
);
1394 if (edge
->src
->compressed
)
1395 map
= isl_map_preimage_domain_multi_aff(map
,
1396 isl_multi_aff_copy(edge
->src
->decompress
));
1397 if (edge
->dst
->compressed
)
1398 map
= isl_map_preimage_range_multi_aff(map
,
1399 isl_multi_aff_copy(edge
->dst
->decompress
));
1400 set
= isl_map_wrap(isl_map_remove_divs(map
));
1401 coef
= isl_set_coefficients(set
);
1402 graph
->inter_hmap
= isl_map_to_basic_set_set(graph
->inter_hmap
, key
,
1403 isl_basic_set_copy(coef
));
1408 /* Add constraints to graph->lp that force validity for the given
1409 * dependence from a node i to itself.
1410 * That is, add constraints that enforce
1412 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1413 * = c_i_x (y - x) >= 0
1415 * for each (x,y) in R.
1416 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1417 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1418 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1419 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1421 * Actually, we do not construct constraints for the c_i_x themselves,
1422 * but for the coefficients of c_i_x written as a linear combination
1423 * of the columns in node->cmap.
1425 static int add_intra_validity_constraints(struct isl_sched_graph
*graph
,
1426 struct isl_sched_edge
*edge
)
1429 isl_map
*map
= isl_map_copy(edge
->map
);
1430 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1432 isl_dim_map
*dim_map
;
1433 isl_basic_set
*coef
;
1434 struct isl_sched_node
*node
= edge
->src
;
1436 coef
= intra_coefficients(graph
, node
, map
);
1438 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1440 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1441 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(node
->cmap
));
1445 total
= isl_basic_set_total_dim(graph
->lp
);
1446 dim_map
= isl_dim_map_alloc(ctx
, total
);
1447 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
1448 isl_space_dim(dim
, isl_dim_set
), 1,
1450 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
1451 isl_space_dim(dim
, isl_dim_set
), 1,
1453 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1454 coef
->n_eq
, coef
->n_ineq
);
1455 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1457 isl_space_free(dim
);
1461 isl_space_free(dim
);
1465 /* Add constraints to graph->lp that force validity for the given
1466 * dependence from node i to node j.
1467 * That is, add constraints that enforce
1469 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1471 * for each (x,y) in R.
1472 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1473 * of valid constraints for R and then plug in
1474 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
1475 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1476 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1477 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1479 * Actually, we do not construct constraints for the c_*_x themselves,
1480 * but for the coefficients of c_*_x written as a linear combination
1481 * of the columns in node->cmap.
1483 static int add_inter_validity_constraints(struct isl_sched_graph
*graph
,
1484 struct isl_sched_edge
*edge
)
1487 isl_map
*map
= isl_map_copy(edge
->map
);
1488 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1490 isl_dim_map
*dim_map
;
1491 isl_basic_set
*coef
;
1492 struct isl_sched_node
*src
= edge
->src
;
1493 struct isl_sched_node
*dst
= edge
->dst
;
1495 coef
= inter_coefficients(graph
, edge
, map
);
1497 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1499 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1500 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(src
->cmap
));
1501 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1502 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
,
1503 isl_mat_copy(dst
->cmap
));
1507 total
= isl_basic_set_total_dim(graph
->lp
);
1508 dim_map
= isl_dim_map_alloc(ctx
, total
);
1510 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, 1);
1511 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, -1);
1512 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, 1);
1513 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
1514 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1516 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
1517 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1520 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, -1);
1521 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, 1);
1522 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, -1);
1523 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
1524 isl_space_dim(dim
, isl_dim_set
), 1,
1526 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
1527 isl_space_dim(dim
, isl_dim_set
), 1,
1530 edge
->start
= graph
->lp
->n_ineq
;
1531 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1532 coef
->n_eq
, coef
->n_ineq
);
1533 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1537 isl_space_free(dim
);
1538 edge
->end
= graph
->lp
->n_ineq
;
1542 isl_space_free(dim
);
1546 /* Add constraints to graph->lp that bound the dependence distance for the given
1547 * dependence from a node i to itself.
1548 * If s = 1, we add the constraint
1550 * c_i_x (y - x) <= m_0 + m_n n
1554 * -c_i_x (y - x) + m_0 + m_n n >= 0
1556 * for each (x,y) in R.
1557 * If s = -1, we add the constraint
1559 * -c_i_x (y - x) <= m_0 + m_n n
1563 * c_i_x (y - x) + m_0 + m_n n >= 0
1565 * for each (x,y) in R.
1566 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1567 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1568 * with each coefficient (except m_0) represented as a pair of non-negative
1571 * Actually, we do not construct constraints for the c_i_x themselves,
1572 * but for the coefficients of c_i_x written as a linear combination
1573 * of the columns in node->cmap.
1576 * If "local" is set, then we add constraints
1578 * c_i_x (y - x) <= 0
1582 * -c_i_x (y - x) <= 0
1584 * instead, forcing the dependence distance to be (less than or) equal to 0.
1585 * That is, we plug in (0, 0, -s * c_i_x),
1586 * Note that dependences marked local are treated as validity constraints
1587 * by add_all_validity_constraints and therefore also have
1588 * their distances bounded by 0 from below.
1590 static int add_intra_proximity_constraints(struct isl_sched_graph
*graph
,
1591 struct isl_sched_edge
*edge
, int s
, int local
)
1595 isl_map
*map
= isl_map_copy(edge
->map
);
1596 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1598 isl_dim_map
*dim_map
;
1599 isl_basic_set
*coef
;
1600 struct isl_sched_node
*node
= edge
->src
;
1602 coef
= intra_coefficients(graph
, node
, map
);
1604 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1606 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1607 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(node
->cmap
));
1611 nparam
= isl_space_dim(node
->space
, isl_dim_param
);
1612 total
= isl_basic_set_total_dim(graph
->lp
);
1613 dim_map
= isl_dim_map_alloc(ctx
, total
);
1616 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1617 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1618 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1620 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
1621 isl_space_dim(dim
, isl_dim_set
), 1,
1623 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
1624 isl_space_dim(dim
, isl_dim_set
), 1,
1626 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1627 coef
->n_eq
, coef
->n_ineq
);
1628 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1630 isl_space_free(dim
);
1634 isl_space_free(dim
);
1638 /* Add constraints to graph->lp that bound the dependence distance for the given
1639 * dependence from node i to node j.
1640 * If s = 1, we add the constraint
1642 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1647 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1650 * for each (x,y) in R.
1651 * If s = -1, we add the constraint
1653 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1658 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1661 * for each (x,y) in R.
1662 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1663 * of valid constraints for R and then plug in
1664 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1666 * with each coefficient (except m_0, c_j_0 and c_i_0)
1667 * represented as a pair of non-negative coefficients.
1669 * Actually, we do not construct constraints for the c_*_x themselves,
1670 * but for the coefficients of c_*_x written as a linear combination
1671 * of the columns in node->cmap.
1674 * If "local" is set, then we add constraints
1676 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1680 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
1682 * instead, forcing the dependence distance to be (less than or) equal to 0.
1683 * That is, we plug in
1684 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
1685 * Note that dependences marked local are treated as validity constraints
1686 * by add_all_validity_constraints and therefore also have
1687 * their distances bounded by 0 from below.
1689 static int add_inter_proximity_constraints(struct isl_sched_graph
*graph
,
1690 struct isl_sched_edge
*edge
, int s
, int local
)
1694 isl_map
*map
= isl_map_copy(edge
->map
);
1695 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1697 isl_dim_map
*dim_map
;
1698 isl_basic_set
*coef
;
1699 struct isl_sched_node
*src
= edge
->src
;
1700 struct isl_sched_node
*dst
= edge
->dst
;
1702 coef
= inter_coefficients(graph
, edge
, map
);
1704 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1706 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1707 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(src
->cmap
));
1708 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1709 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
,
1710 isl_mat_copy(dst
->cmap
));
1714 nparam
= isl_space_dim(src
->space
, isl_dim_param
);
1715 total
= isl_basic_set_total_dim(graph
->lp
);
1716 dim_map
= isl_dim_map_alloc(ctx
, total
);
1719 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1720 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1721 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1724 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, -s
);
1725 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, s
);
1726 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, -s
);
1727 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
1728 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1730 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
1731 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1734 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, s
);
1735 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, -s
);
1736 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, s
);
1737 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
1738 isl_space_dim(dim
, isl_dim_set
), 1,
1740 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
1741 isl_space_dim(dim
, isl_dim_set
), 1,
1744 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1745 coef
->n_eq
, coef
->n_ineq
);
1746 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1748 isl_space_free(dim
);
1752 isl_space_free(dim
);
1756 /* Add all validity constraints to graph->lp.
1758 * An edge that is forced to be local needs to have its dependence
1759 * distances equal to zero. We take care of bounding them by 0 from below
1760 * here. add_all_proximity_constraints takes care of bounding them by 0
1763 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1764 * Otherwise, we ignore them.
1766 static int add_all_validity_constraints(struct isl_sched_graph
*graph
,
1767 int use_coincidence
)
1771 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1772 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1775 local
= edge
->local
|| (edge
->coincidence
&& use_coincidence
);
1776 if (!edge
->validity
&& !local
)
1778 if (edge
->src
!= edge
->dst
)
1780 if (add_intra_validity_constraints(graph
, edge
) < 0)
1784 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1785 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1788 local
= edge
->local
|| (edge
->coincidence
&& use_coincidence
);
1789 if (!edge
->validity
&& !local
)
1791 if (edge
->src
== edge
->dst
)
1793 if (add_inter_validity_constraints(graph
, edge
) < 0)
1800 /* Add constraints to graph->lp that bound the dependence distance
1801 * for all dependence relations.
1802 * If a given proximity dependence is identical to a validity
1803 * dependence, then the dependence distance is already bounded
1804 * from below (by zero), so we only need to bound the distance
1805 * from above. (This includes the case of "local" dependences
1806 * which are treated as validity dependence by add_all_validity_constraints.)
1807 * Otherwise, we need to bound the distance both from above and from below.
1809 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1810 * Otherwise, we ignore them.
1812 static int add_all_proximity_constraints(struct isl_sched_graph
*graph
,
1813 int use_coincidence
)
1817 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1818 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1821 local
= edge
->local
|| (edge
->coincidence
&& use_coincidence
);
1822 if (!edge
->proximity
&& !local
)
1824 if (edge
->src
== edge
->dst
&&
1825 add_intra_proximity_constraints(graph
, edge
, 1, local
) < 0)
1827 if (edge
->src
!= edge
->dst
&&
1828 add_inter_proximity_constraints(graph
, edge
, 1, local
) < 0)
1830 if (edge
->validity
|| local
)
1832 if (edge
->src
== edge
->dst
&&
1833 add_intra_proximity_constraints(graph
, edge
, -1, 0) < 0)
1835 if (edge
->src
!= edge
->dst
&&
1836 add_inter_proximity_constraints(graph
, edge
, -1, 0) < 0)
1843 /* Compute a basis for the rows in the linear part of the schedule
1844 * and extend this basis to a full basis. The remaining rows
1845 * can then be used to force linear independence from the rows
1848 * In particular, given the schedule rows S, we compute
1853 * with H the Hermite normal form of S. That is, all but the
1854 * first rank columns of H are zero and so each row in S is
1855 * a linear combination of the first rank rows of Q.
1856 * The matrix Q is then transposed because we will write the
1857 * coefficients of the next schedule row as a column vector s
1858 * and express this s as a linear combination s = Q c of the
1860 * Similarly, the matrix U is transposed such that we can
1861 * compute the coefficients c = U s from a schedule row s.
1863 static int node_update_cmap(struct isl_sched_node
*node
)
1866 int n_row
= isl_mat_rows(node
->sched
);
1868 H
= isl_mat_sub_alloc(node
->sched
, 0, n_row
,
1869 1 + node
->nparam
, node
->nvar
);
1871 H
= isl_mat_left_hermite(H
, 0, &U
, &Q
);
1872 isl_mat_free(node
->cmap
);
1873 isl_mat_free(node
->cinv
);
1874 node
->cmap
= isl_mat_transpose(Q
);
1875 node
->cinv
= isl_mat_transpose(U
);
1876 node
->rank
= isl_mat_initial_non_zero_cols(H
);
1879 if (!node
->cmap
|| !node
->cinv
|| node
->rank
< 0)
1884 /* How many times should we count the constraints in "edge"?
1886 * If carry is set, then we are counting the number of
1887 * (validity or conditional validity) constraints that will be added
1888 * in setup_carry_lp and we count each edge exactly once.
1890 * Otherwise, we count as follows
1891 * validity -> 1 (>= 0)
1892 * validity+proximity -> 2 (>= 0 and upper bound)
1893 * proximity -> 2 (lower and upper bound)
1894 * local(+any) -> 2 (>= 0 and <= 0)
1896 * If an edge is only marked conditional_validity then it counts
1897 * as zero since it is only checked afterwards.
1899 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1900 * Otherwise, we ignore them.
1902 static int edge_multiplicity(struct isl_sched_edge
*edge
, int carry
,
1903 int use_coincidence
)
1905 if (carry
&& !edge
->validity
&& !edge
->conditional_validity
)
1909 if (edge
->proximity
|| edge
->local
)
1911 if (use_coincidence
&& edge
->coincidence
)
1918 /* Count the number of equality and inequality constraints
1919 * that will be added for the given map.
1921 * "use_coincidence" is set if we should take into account coincidence edges.
1923 static int count_map_constraints(struct isl_sched_graph
*graph
,
1924 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
,
1925 int *n_eq
, int *n_ineq
, int carry
, int use_coincidence
)
1927 isl_basic_set
*coef
;
1928 int f
= edge_multiplicity(edge
, carry
, use_coincidence
);
1935 if (edge
->src
== edge
->dst
)
1936 coef
= intra_coefficients(graph
, edge
->src
, map
);
1938 coef
= inter_coefficients(graph
, edge
, map
);
1941 *n_eq
+= f
* coef
->n_eq
;
1942 *n_ineq
+= f
* coef
->n_ineq
;
1943 isl_basic_set_free(coef
);
1948 /* Count the number of equality and inequality constraints
1949 * that will be added to the main lp problem.
1950 * We count as follows
1951 * validity -> 1 (>= 0)
1952 * validity+proximity -> 2 (>= 0 and upper bound)
1953 * proximity -> 2 (lower and upper bound)
1954 * local(+any) -> 2 (>= 0 and <= 0)
1956 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1957 * Otherwise, we ignore them.
1959 static int count_constraints(struct isl_sched_graph
*graph
,
1960 int *n_eq
, int *n_ineq
, int use_coincidence
)
1964 *n_eq
= *n_ineq
= 0;
1965 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1966 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1967 isl_map
*map
= isl_map_copy(edge
->map
);
1969 if (count_map_constraints(graph
, edge
, map
, n_eq
, n_ineq
,
1970 0, use_coincidence
) < 0)
1977 /* Count the number of constraints that will be added by
1978 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
1981 * In practice, add_bound_coefficient_constraints only adds inequalities.
1983 static int count_bound_coefficient_constraints(isl_ctx
*ctx
,
1984 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
1988 if (ctx
->opt
->schedule_max_coefficient
== -1)
1991 for (i
= 0; i
< graph
->n
; ++i
)
1992 *n_ineq
+= 2 * graph
->node
[i
].nparam
+ 2 * graph
->node
[i
].nvar
;
1997 /* Add constraints that bound the values of the variable and parameter
1998 * coefficients of the schedule.
2000 * The maximal value of the coefficients is defined by the option
2001 * 'schedule_max_coefficient'.
2003 static int add_bound_coefficient_constraints(isl_ctx
*ctx
,
2004 struct isl_sched_graph
*graph
)
2007 int max_coefficient
;
2010 max_coefficient
= ctx
->opt
->schedule_max_coefficient
;
2012 if (max_coefficient
== -1)
2015 total
= isl_basic_set_total_dim(graph
->lp
);
2017 for (i
= 0; i
< graph
->n
; ++i
) {
2018 struct isl_sched_node
*node
= &graph
->node
[i
];
2019 for (j
= 0; j
< 2 * node
->nparam
+ 2 * node
->nvar
; ++j
) {
2021 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2024 dim
= 1 + node
->start
+ 1 + j
;
2025 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2026 isl_int_set_si(graph
->lp
->ineq
[k
][dim
], -1);
2027 isl_int_set_si(graph
->lp
->ineq
[k
][0], max_coefficient
);
2034 /* Construct an ILP problem for finding schedule coefficients
2035 * that result in non-negative, but small dependence distances
2036 * over all dependences.
2037 * In particular, the dependence distances over proximity edges
2038 * are bounded by m_0 + m_n n and we compute schedule coefficients
2039 * with small values (preferably zero) of m_n and m_0.
2041 * All variables of the ILP are non-negative. The actual coefficients
2042 * may be negative, so each coefficient is represented as the difference
2043 * of two non-negative variables. The negative part always appears
2044 * immediately before the positive part.
2045 * Other than that, the variables have the following order
2047 * - sum of positive and negative parts of m_n coefficients
2049 * - sum of positive and negative parts of all c_n coefficients
2050 * (unconstrained when computing non-parametric schedules)
2051 * - sum of positive and negative parts of all c_x coefficients
2052 * - positive and negative parts of m_n coefficients
2055 * - positive and negative parts of c_i_n (if parametric)
2056 * - positive and negative parts of c_i_x
2058 * The c_i_x are not represented directly, but through the columns of
2059 * node->cmap. That is, the computed values are for variable t_i_x
2060 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
2062 * The constraints are those from the edges plus two or three equalities
2063 * to express the sums.
2065 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2066 * Otherwise, we ignore them.
2068 static int setup_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
2069 int use_coincidence
)
2079 int max_constant_term
;
2081 max_constant_term
= ctx
->opt
->schedule_max_constant_term
;
2083 parametric
= ctx
->opt
->schedule_parametric
;
2084 nparam
= isl_space_dim(graph
->node
[0].space
, isl_dim_param
);
2086 total
= param_pos
+ 2 * nparam
;
2087 for (i
= 0; i
< graph
->n
; ++i
) {
2088 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
2089 if (node_update_cmap(node
) < 0)
2091 node
->start
= total
;
2092 total
+= 1 + 2 * (node
->nparam
+ node
->nvar
);
2095 if (count_constraints(graph
, &n_eq
, &n_ineq
, use_coincidence
) < 0)
2097 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2100 dim
= isl_space_set_alloc(ctx
, 0, total
);
2101 isl_basic_set_free(graph
->lp
);
2102 n_eq
+= 2 + parametric
;
2103 if (max_constant_term
!= -1)
2106 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
2108 k
= isl_basic_set_alloc_equality(graph
->lp
);
2111 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2112 isl_int_set_si(graph
->lp
->eq
[k
][1], -1);
2113 for (i
= 0; i
< 2 * nparam
; ++i
)
2114 isl_int_set_si(graph
->lp
->eq
[k
][1 + param_pos
+ i
], 1);
2117 k
= isl_basic_set_alloc_equality(graph
->lp
);
2120 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2121 isl_int_set_si(graph
->lp
->eq
[k
][3], -1);
2122 for (i
= 0; i
< graph
->n
; ++i
) {
2123 int pos
= 1 + graph
->node
[i
].start
+ 1;
2125 for (j
= 0; j
< 2 * graph
->node
[i
].nparam
; ++j
)
2126 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2130 k
= isl_basic_set_alloc_equality(graph
->lp
);
2133 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2134 isl_int_set_si(graph
->lp
->eq
[k
][4], -1);
2135 for (i
= 0; i
< graph
->n
; ++i
) {
2136 struct isl_sched_node
*node
= &graph
->node
[i
];
2137 int pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
2139 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
2140 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2143 if (max_constant_term
!= -1)
2144 for (i
= 0; i
< graph
->n
; ++i
) {
2145 struct isl_sched_node
*node
= &graph
->node
[i
];
2146 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2149 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2150 isl_int_set_si(graph
->lp
->ineq
[k
][1 + node
->start
], -1);
2151 isl_int_set_si(graph
->lp
->ineq
[k
][0], max_constant_term
);
2154 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
2156 if (add_all_validity_constraints(graph
, use_coincidence
) < 0)
2158 if (add_all_proximity_constraints(graph
, use_coincidence
) < 0)
2164 /* Analyze the conflicting constraint found by
2165 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2166 * constraint of one of the edges between distinct nodes, living, moreover
2167 * in distinct SCCs, then record the source and sink SCC as this may
2168 * be a good place to cut between SCCs.
2170 static int check_conflict(int con
, void *user
)
2173 struct isl_sched_graph
*graph
= user
;
2175 if (graph
->src_scc
>= 0)
2178 con
-= graph
->lp
->n_eq
;
2180 if (con
>= graph
->lp
->n_ineq
)
2183 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2184 if (!graph
->edge
[i
].validity
)
2186 if (graph
->edge
[i
].src
== graph
->edge
[i
].dst
)
2188 if (graph
->edge
[i
].src
->scc
== graph
->edge
[i
].dst
->scc
)
2190 if (graph
->edge
[i
].start
> con
)
2192 if (graph
->edge
[i
].end
<= con
)
2194 graph
->src_scc
= graph
->edge
[i
].src
->scc
;
2195 graph
->dst_scc
= graph
->edge
[i
].dst
->scc
;
2201 /* Check whether the next schedule row of the given node needs to be
2202 * non-trivial. Lower-dimensional domains may have some trivial rows,
2203 * but as soon as the number of remaining required non-trivial rows
2204 * is as large as the number or remaining rows to be computed,
2205 * all remaining rows need to be non-trivial.
2207 static int needs_row(struct isl_sched_graph
*graph
, struct isl_sched_node
*node
)
2209 return node
->nvar
- node
->rank
>= graph
->maxvar
- graph
->n_row
;
2212 /* Solve the ILP problem constructed in setup_lp.
2213 * For each node such that all the remaining rows of its schedule
2214 * need to be non-trivial, we construct a non-triviality region.
2215 * This region imposes that the next row is independent of previous rows.
2216 * In particular the coefficients c_i_x are represented by t_i_x
2217 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
2218 * its first columns span the rows of the previously computed part
2219 * of the schedule. The non-triviality region enforces that at least
2220 * one of the remaining components of t_i_x is non-zero, i.e.,
2221 * that the new schedule row depends on at least one of the remaining
2224 static __isl_give isl_vec
*solve_lp(struct isl_sched_graph
*graph
)
2230 for (i
= 0; i
< graph
->n
; ++i
) {
2231 struct isl_sched_node
*node
= &graph
->node
[i
];
2232 int skip
= node
->rank
;
2233 graph
->region
[i
].pos
= node
->start
+ 1 + 2*(node
->nparam
+skip
);
2234 if (needs_row(graph
, node
))
2235 graph
->region
[i
].len
= 2 * (node
->nvar
- skip
);
2237 graph
->region
[i
].len
= 0;
2239 lp
= isl_basic_set_copy(graph
->lp
);
2240 sol
= isl_tab_basic_set_non_trivial_lexmin(lp
, 2, graph
->n
,
2241 graph
->region
, &check_conflict
, graph
);
2245 /* Update the schedules of all nodes based on the given solution
2246 * of the LP problem.
2247 * The new row is added to the current band.
2248 * All possibly negative coefficients are encoded as a difference
2249 * of two non-negative variables, so we need to perform the subtraction
2250 * here. Moreover, if use_cmap is set, then the solution does
2251 * not refer to the actual coefficients c_i_x, but instead to variables
2252 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2253 * In this case, we then also need to perform this multiplication
2254 * to obtain the values of c_i_x.
2256 * If coincident is set, then the caller guarantees that the new
2257 * row satisfies the coincidence constraints.
2259 static int update_schedule(struct isl_sched_graph
*graph
,
2260 __isl_take isl_vec
*sol
, int use_cmap
, int coincident
)
2263 isl_vec
*csol
= NULL
;
2268 isl_die(sol
->ctx
, isl_error_internal
,
2269 "no solution found", goto error
);
2270 if (graph
->n_total_row
>= graph
->max_row
)
2271 isl_die(sol
->ctx
, isl_error_internal
,
2272 "too many schedule rows", goto error
);
2274 for (i
= 0; i
< graph
->n
; ++i
) {
2275 struct isl_sched_node
*node
= &graph
->node
[i
];
2276 int pos
= node
->start
;
2277 int row
= isl_mat_rows(node
->sched
);
2280 csol
= isl_vec_alloc(sol
->ctx
, node
->nvar
);
2284 isl_map_free(node
->sched_map
);
2285 node
->sched_map
= NULL
;
2286 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
2289 node
->sched
= isl_mat_set_element(node
->sched
, row
, 0,
2291 for (j
= 0; j
< node
->nparam
+ node
->nvar
; ++j
)
2292 isl_int_sub(sol
->el
[1 + pos
+ 1 + 2 * j
+ 1],
2293 sol
->el
[1 + pos
+ 1 + 2 * j
+ 1],
2294 sol
->el
[1 + pos
+ 1 + 2 * j
]);
2295 for (j
= 0; j
< node
->nparam
; ++j
)
2296 node
->sched
= isl_mat_set_element(node
->sched
,
2297 row
, 1 + j
, sol
->el
[1+pos
+1+2*j
+1]);
2298 for (j
= 0; j
< node
->nvar
; ++j
)
2299 isl_int_set(csol
->el
[j
],
2300 sol
->el
[1+pos
+1+2*(node
->nparam
+j
)+1]);
2302 csol
= isl_mat_vec_product(isl_mat_copy(node
->cmap
),
2306 for (j
= 0; j
< node
->nvar
; ++j
)
2307 node
->sched
= isl_mat_set_element(node
->sched
,
2308 row
, 1 + node
->nparam
+ j
, csol
->el
[j
]);
2309 node
->coincident
[graph
->n_total_row
] = coincident
;
2315 graph
->n_total_row
++;
2324 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2325 * and return this isl_aff.
2327 static __isl_give isl_aff
*extract_schedule_row(__isl_take isl_local_space
*ls
,
2328 struct isl_sched_node
*node
, int row
)
2336 aff
= isl_aff_zero_on_domain(ls
);
2337 isl_mat_get_element(node
->sched
, row
, 0, &v
);
2338 aff
= isl_aff_set_constant(aff
, v
);
2339 for (j
= 0; j
< node
->nparam
; ++j
) {
2340 isl_mat_get_element(node
->sched
, row
, 1 + j
, &v
);
2341 aff
= isl_aff_set_coefficient(aff
, isl_dim_param
, j
, v
);
2343 for (j
= 0; j
< node
->nvar
; ++j
) {
2344 isl_mat_get_element(node
->sched
, row
, 1 + node
->nparam
+ j
, &v
);
2345 aff
= isl_aff_set_coefficient(aff
, isl_dim_in
, j
, v
);
2353 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
2354 * and return this multi_aff.
2356 * The result is defined over the uncompressed node domain.
2358 static __isl_give isl_multi_aff
*node_extract_partial_schedule_multi_aff(
2359 struct isl_sched_node
*node
, int first
, int n
)
2363 isl_local_space
*ls
;
2368 nrow
= isl_mat_rows(node
->sched
);
2369 ncol
= isl_mat_cols(node
->sched
) - 1;
2370 if (node
->compressed
)
2371 space
= isl_multi_aff_get_domain_space(node
->decompress
);
2373 space
= isl_space_copy(node
->space
);
2374 ls
= isl_local_space_from_space(isl_space_copy(space
));
2375 space
= isl_space_from_domain(space
);
2376 space
= isl_space_add_dims(space
, isl_dim_out
, n
);
2377 ma
= isl_multi_aff_zero(space
);
2379 for (i
= first
; i
< first
+ n
; ++i
) {
2380 aff
= extract_schedule_row(isl_local_space_copy(ls
), node
, i
);
2381 ma
= isl_multi_aff_set_aff(ma
, i
- first
, aff
);
2384 isl_local_space_free(ls
);
2386 if (node
->compressed
)
2387 ma
= isl_multi_aff_pullback_multi_aff(ma
,
2388 isl_multi_aff_copy(node
->compress
));
2393 /* Convert node->sched into a multi_aff and return this multi_aff.
2395 * The result is defined over the uncompressed node domain.
2397 static __isl_give isl_multi_aff
*node_extract_schedule_multi_aff(
2398 struct isl_sched_node
*node
)
2402 nrow
= isl_mat_rows(node
->sched
);
2403 return node_extract_partial_schedule_multi_aff(node
, 0, nrow
);
2406 /* Convert node->sched into a map and return this map.
2408 * The result is cached in node->sched_map, which needs to be released
2409 * whenever node->sched is updated.
2410 * It is defined over the uncompressed node domain.
2412 static __isl_give isl_map
*node_extract_schedule(struct isl_sched_node
*node
)
2414 if (!node
->sched_map
) {
2417 ma
= node_extract_schedule_multi_aff(node
);
2418 node
->sched_map
= isl_map_from_multi_aff(ma
);
2421 return isl_map_copy(node
->sched_map
);
2424 /* Construct a map that can be used to update a dependence relation
2425 * based on the current schedule.
2426 * That is, construct a map expressing that source and sink
2427 * are executed within the same iteration of the current schedule.
2428 * This map can then be intersected with the dependence relation.
2429 * This is not the most efficient way, but this shouldn't be a critical
2432 static __isl_give isl_map
*specializer(struct isl_sched_node
*src
,
2433 struct isl_sched_node
*dst
)
2435 isl_map
*src_sched
, *dst_sched
;
2437 src_sched
= node_extract_schedule(src
);
2438 dst_sched
= node_extract_schedule(dst
);
2439 return isl_map_apply_range(src_sched
, isl_map_reverse(dst_sched
));
2442 /* Intersect the domains of the nested relations in domain and range
2443 * of "umap" with "map".
2445 static __isl_give isl_union_map
*intersect_domains(
2446 __isl_take isl_union_map
*umap
, __isl_keep isl_map
*map
)
2448 isl_union_set
*uset
;
2450 umap
= isl_union_map_zip(umap
);
2451 uset
= isl_union_set_from_set(isl_map_wrap(isl_map_copy(map
)));
2452 umap
= isl_union_map_intersect_domain(umap
, uset
);
2453 umap
= isl_union_map_zip(umap
);
2457 /* Update the dependence relation of the given edge based
2458 * on the current schedule.
2459 * If the dependence is carried completely by the current schedule, then
2460 * it is removed from the edge_tables. It is kept in the list of edges
2461 * as otherwise all edge_tables would have to be recomputed.
2463 static int update_edge(struct isl_sched_graph
*graph
,
2464 struct isl_sched_edge
*edge
)
2468 id
= specializer(edge
->src
, edge
->dst
);
2469 edge
->map
= isl_map_intersect(edge
->map
, isl_map_copy(id
));
2473 if (edge
->tagged_condition
) {
2474 edge
->tagged_condition
=
2475 intersect_domains(edge
->tagged_condition
, id
);
2476 if (!edge
->tagged_condition
)
2479 if (edge
->tagged_validity
) {
2480 edge
->tagged_validity
=
2481 intersect_domains(edge
->tagged_validity
, id
);
2482 if (!edge
->tagged_validity
)
2487 if (isl_map_plain_is_empty(edge
->map
))
2488 graph_remove_edge(graph
, edge
);
2496 /* Update the dependence relations of all edges based on the current schedule.
2498 static int update_edges(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2502 for (i
= graph
->n_edge
- 1; i
>= 0; --i
) {
2503 if (update_edge(graph
, &graph
->edge
[i
]) < 0)
2510 static void next_band(struct isl_sched_graph
*graph
)
2512 graph
->band_start
= graph
->n_total_row
;
2515 /* Return the union of the universe domains of the nodes in "graph"
2516 * that satisfy "pred".
2518 static __isl_give isl_union_set
*isl_sched_graph_domain(isl_ctx
*ctx
,
2519 struct isl_sched_graph
*graph
,
2520 int (*pred
)(struct isl_sched_node
*node
, int data
), int data
)
2526 for (i
= 0; i
< graph
->n
; ++i
)
2527 if (pred(&graph
->node
[i
], data
))
2531 isl_die(ctx
, isl_error_internal
,
2532 "empty component", return NULL
);
2534 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
2535 dom
= isl_union_set_from_set(set
);
2537 for (i
= i
+ 1; i
< graph
->n
; ++i
) {
2538 if (!pred(&graph
->node
[i
], data
))
2540 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
2541 dom
= isl_union_set_union(dom
, isl_union_set_from_set(set
));
2547 /* Return a list of unions of universe domains, where each element
2548 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
2550 static __isl_give isl_union_set_list
*extract_sccs(isl_ctx
*ctx
,
2551 struct isl_sched_graph
*graph
)
2554 isl_union_set_list
*filters
;
2556 filters
= isl_union_set_list_alloc(ctx
, graph
->scc
);
2557 for (i
= 0; i
< graph
->scc
; ++i
) {
2560 dom
= isl_sched_graph_domain(ctx
, graph
, &node_scc_exactly
, i
);
2561 filters
= isl_union_set_list_add(filters
, dom
);
2567 /* Return a list of two unions of universe domains, one for the SCCs up
2568 * to and including graph->src_scc and another for the other SCCS.
2570 static __isl_give isl_union_set_list
*extract_split(isl_ctx
*ctx
,
2571 struct isl_sched_graph
*graph
)
2574 isl_union_set_list
*filters
;
2576 filters
= isl_union_set_list_alloc(ctx
, 2);
2577 dom
= isl_sched_graph_domain(ctx
, graph
,
2578 &node_scc_at_most
, graph
->src_scc
);
2579 filters
= isl_union_set_list_add(filters
, dom
);
2580 dom
= isl_sched_graph_domain(ctx
, graph
,
2581 &node_scc_at_least
, graph
->src_scc
+ 1);
2582 filters
= isl_union_set_list_add(filters
, dom
);
2587 /* Topologically sort statements mapped to the same schedule iteration
2588 * and add insert a sequence node in front of "node"
2589 * corresponding to this order.
2591 static __isl_give isl_schedule_node
*sort_statements(
2592 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
2595 isl_union_set_list
*filters
;
2600 ctx
= isl_schedule_node_get_ctx(node
);
2602 isl_die(ctx
, isl_error_internal
,
2603 "graph should have at least one node",
2604 return isl_schedule_node_free(node
));
2609 if (update_edges(ctx
, graph
) < 0)
2610 return isl_schedule_node_free(node
);
2612 if (graph
->n_edge
== 0)
2615 if (detect_sccs(ctx
, graph
) < 0)
2616 return isl_schedule_node_free(node
);
2618 filters
= extract_sccs(ctx
, graph
);
2619 node
= isl_schedule_node_insert_sequence(node
, filters
);
2624 /* Copy nodes that satisfy node_pred from the src dependence graph
2625 * to the dst dependence graph.
2627 static int copy_nodes(struct isl_sched_graph
*dst
, struct isl_sched_graph
*src
,
2628 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
2633 for (i
= 0; i
< src
->n
; ++i
) {
2636 if (!node_pred(&src
->node
[i
], data
))
2640 dst
->node
[j
].space
= isl_space_copy(src
->node
[i
].space
);
2641 dst
->node
[j
].compressed
= src
->node
[i
].compressed
;
2642 dst
->node
[j
].hull
= isl_set_copy(src
->node
[i
].hull
);
2643 dst
->node
[j
].compress
=
2644 isl_multi_aff_copy(src
->node
[i
].compress
);
2645 dst
->node
[j
].decompress
=
2646 isl_multi_aff_copy(src
->node
[i
].decompress
);
2647 dst
->node
[j
].nvar
= src
->node
[i
].nvar
;
2648 dst
->node
[j
].nparam
= src
->node
[i
].nparam
;
2649 dst
->node
[j
].sched
= isl_mat_copy(src
->node
[i
].sched
);
2650 dst
->node
[j
].sched_map
= isl_map_copy(src
->node
[i
].sched_map
);
2651 dst
->node
[j
].coincident
= src
->node
[i
].coincident
;
2654 if (!dst
->node
[j
].space
|| !dst
->node
[j
].sched
)
2656 if (dst
->node
[j
].compressed
&&
2657 (!dst
->node
[j
].hull
|| !dst
->node
[j
].compress
||
2658 !dst
->node
[j
].decompress
))
2665 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
2666 * to the dst dependence graph.
2667 * If the source or destination node of the edge is not in the destination
2668 * graph, then it must be a backward proximity edge and it should simply
2671 static int copy_edges(isl_ctx
*ctx
, struct isl_sched_graph
*dst
,
2672 struct isl_sched_graph
*src
,
2673 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
), int data
)
2676 enum isl_edge_type t
;
2679 for (i
= 0; i
< src
->n_edge
; ++i
) {
2680 struct isl_sched_edge
*edge
= &src
->edge
[i
];
2682 isl_union_map
*tagged_condition
;
2683 isl_union_map
*tagged_validity
;
2684 struct isl_sched_node
*dst_src
, *dst_dst
;
2686 if (!edge_pred(edge
, data
))
2689 if (isl_map_plain_is_empty(edge
->map
))
2692 dst_src
= graph_find_node(ctx
, dst
, edge
->src
->space
);
2693 dst_dst
= graph_find_node(ctx
, dst
, edge
->dst
->space
);
2694 if (!dst_src
|| !dst_dst
) {
2695 if (edge
->validity
|| edge
->conditional_validity
)
2696 isl_die(ctx
, isl_error_internal
,
2697 "backward (conditional) validity edge",
2702 map
= isl_map_copy(edge
->map
);
2703 tagged_condition
= isl_union_map_copy(edge
->tagged_condition
);
2704 tagged_validity
= isl_union_map_copy(edge
->tagged_validity
);
2706 dst
->edge
[dst
->n_edge
].src
= dst_src
;
2707 dst
->edge
[dst
->n_edge
].dst
= dst_dst
;
2708 dst
->edge
[dst
->n_edge
].map
= map
;
2709 dst
->edge
[dst
->n_edge
].tagged_condition
= tagged_condition
;
2710 dst
->edge
[dst
->n_edge
].tagged_validity
= tagged_validity
;
2711 dst
->edge
[dst
->n_edge
].validity
= edge
->validity
;
2712 dst
->edge
[dst
->n_edge
].proximity
= edge
->proximity
;
2713 dst
->edge
[dst
->n_edge
].coincidence
= edge
->coincidence
;
2714 dst
->edge
[dst
->n_edge
].condition
= edge
->condition
;
2715 dst
->edge
[dst
->n_edge
].conditional_validity
=
2716 edge
->conditional_validity
;
2719 if (edge
->tagged_condition
&& !tagged_condition
)
2721 if (edge
->tagged_validity
&& !tagged_validity
)
2724 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
2726 graph_find_edge(src
, t
, edge
->src
, edge
->dst
))
2728 if (graph_edge_table_add(ctx
, dst
, t
,
2729 &dst
->edge
[dst
->n_edge
- 1]) < 0)
2737 /* Compute the maximal number of variables over all nodes.
2738 * This is the maximal number of linearly independent schedule
2739 * rows that we need to compute.
2740 * Just in case we end up in a part of the dependence graph
2741 * with only lower-dimensional domains, we make sure we will
2742 * compute the required amount of extra linearly independent rows.
2744 static int compute_maxvar(struct isl_sched_graph
*graph
)
2749 for (i
= 0; i
< graph
->n
; ++i
) {
2750 struct isl_sched_node
*node
= &graph
->node
[i
];
2753 if (node_update_cmap(node
) < 0)
2755 nvar
= node
->nvar
+ graph
->n_row
- node
->rank
;
2756 if (nvar
> graph
->maxvar
)
2757 graph
->maxvar
= nvar
;
2763 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
2764 struct isl_sched_graph
*graph
);
2765 static __isl_give isl_schedule_node
*compute_schedule_wcc(
2766 isl_schedule_node
*node
, struct isl_sched_graph
*graph
);
2768 /* Compute a schedule for a subgraph of "graph". In particular, for
2769 * the graph composed of nodes that satisfy node_pred and edges that
2770 * that satisfy edge_pred. The caller should precompute the number
2771 * of nodes and edges that satisfy these predicates and pass them along
2772 * as "n" and "n_edge".
2773 * If the subgraph is known to consist of a single component, then wcc should
2774 * be set and then we call compute_schedule_wcc on the constructed subgraph.
2775 * Otherwise, we call compute_schedule, which will check whether the subgraph
2778 * The schedule is inserted at "node" and the updated schedule node
2781 static __isl_give isl_schedule_node
*compute_sub_schedule(
2782 __isl_take isl_schedule_node
*node
, isl_ctx
*ctx
,
2783 struct isl_sched_graph
*graph
, int n
, int n_edge
,
2784 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
2785 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
2788 struct isl_sched_graph split
= { 0 };
2791 if (graph_alloc(ctx
, &split
, n
, n_edge
) < 0)
2793 if (copy_nodes(&split
, graph
, node_pred
, data
) < 0)
2795 if (graph_init_table(ctx
, &split
) < 0)
2797 for (t
= 0; t
<= isl_edge_last
; ++t
)
2798 split
.max_edge
[t
] = graph
->max_edge
[t
];
2799 if (graph_init_edge_tables(ctx
, &split
) < 0)
2801 if (copy_edges(ctx
, &split
, graph
, edge_pred
, data
) < 0)
2803 split
.n_row
= graph
->n_row
;
2804 split
.max_row
= graph
->max_row
;
2805 split
.n_total_row
= graph
->n_total_row
;
2806 split
.band_start
= graph
->band_start
;
2809 node
= compute_schedule_wcc(node
, &split
);
2811 node
= compute_schedule(node
, &split
);
2813 graph_free(ctx
, &split
);
2816 graph_free(ctx
, &split
);
2817 return isl_schedule_node_free(node
);
2820 static int edge_scc_exactly(struct isl_sched_edge
*edge
, int scc
)
2822 return edge
->src
->scc
== scc
&& edge
->dst
->scc
== scc
;
2825 static int edge_dst_scc_at_most(struct isl_sched_edge
*edge
, int scc
)
2827 return edge
->dst
->scc
<= scc
;
2830 static int edge_src_scc_at_least(struct isl_sched_edge
*edge
, int scc
)
2832 return edge
->src
->scc
>= scc
;
2835 /* Reset the current band by dropping all its schedule rows.
2837 static int reset_band(struct isl_sched_graph
*graph
)
2842 drop
= graph
->n_total_row
- graph
->band_start
;
2843 graph
->n_total_row
-= drop
;
2844 graph
->n_row
-= drop
;
2846 for (i
= 0; i
< graph
->n
; ++i
) {
2847 struct isl_sched_node
*node
= &graph
->node
[i
];
2849 isl_map_free(node
->sched_map
);
2850 node
->sched_map
= NULL
;
2852 node
->sched
= isl_mat_drop_rows(node
->sched
,
2853 graph
->band_start
, drop
);
2862 /* Split the current graph into two parts and compute a schedule for each
2863 * part individually. In particular, one part consists of all SCCs up
2864 * to and including graph->src_scc, while the other part contains the other
2865 * SCCS. The split is enforced by a sequence node inserted at position "node"
2866 * in the schedule tree. Return the updated schedule node.
2868 * The current band is reset. It would be possible to reuse
2869 * the previously computed rows as the first rows in the next
2870 * band, but recomputing them may result in better rows as we are looking
2871 * at a smaller part of the dependence graph.
2873 static __isl_give isl_schedule_node
*compute_split_schedule(
2874 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
2879 isl_union_set_list
*filters
;
2884 if (reset_band(graph
) < 0)
2885 return isl_schedule_node_free(node
);
2888 for (i
= 0; i
< graph
->n
; ++i
) {
2889 struct isl_sched_node
*node
= &graph
->node
[i
];
2890 int before
= node
->scc
<= graph
->src_scc
;
2897 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2898 if (graph
->edge
[i
].dst
->scc
<= graph
->src_scc
)
2900 if (graph
->edge
[i
].src
->scc
> graph
->src_scc
)
2906 ctx
= isl_schedule_node_get_ctx(node
);
2907 filters
= extract_split(ctx
, graph
);
2908 node
= isl_schedule_node_insert_sequence(node
, filters
);
2909 node
= isl_schedule_node_child(node
, 0);
2910 node
= isl_schedule_node_child(node
, 0);
2912 orig_total_row
= graph
->n_total_row
;
2913 node
= compute_sub_schedule(node
, ctx
, graph
, n
, e1
,
2914 &node_scc_at_most
, &edge_dst_scc_at_most
,
2916 node
= isl_schedule_node_parent(node
);
2917 node
= isl_schedule_node_next_sibling(node
);
2918 node
= isl_schedule_node_child(node
, 0);
2919 graph
->n_total_row
= orig_total_row
;
2920 node
= compute_sub_schedule(node
, ctx
, graph
, graph
->n
- n
, e2
,
2921 &node_scc_at_least
, &edge_src_scc_at_least
,
2922 graph
->src_scc
+ 1, 0);
2923 node
= isl_schedule_node_parent(node
);
2924 node
= isl_schedule_node_parent(node
);
2929 /* Insert a band node at position "node" in the schedule tree corresponding
2930 * to the current band in "graph". Mark the band node permutable
2931 * if "permutable" is set.
2932 * The partial schedules and the coincidence property are extracted
2933 * from the graph nodes.
2934 * Return the updated schedule node.
2936 static __isl_give isl_schedule_node
*insert_current_band(
2937 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
2943 isl_multi_pw_aff
*mpa
;
2944 isl_multi_union_pw_aff
*mupa
;
2950 isl_die(isl_schedule_node_get_ctx(node
), isl_error_internal
,
2951 "graph should have at least one node",
2952 return isl_schedule_node_free(node
));
2954 start
= graph
->band_start
;
2955 end
= graph
->n_total_row
;
2958 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[0], start
, n
);
2959 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
2960 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
2962 for (i
= 1; i
< graph
->n
; ++i
) {
2963 isl_multi_union_pw_aff
*mupa_i
;
2965 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[i
],
2967 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
2968 mupa_i
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
2969 mupa
= isl_multi_union_pw_aff_union_add(mupa
, mupa_i
);
2971 node
= isl_schedule_node_insert_partial_schedule(node
, mupa
);
2973 for (i
= 0; i
< n
; ++i
)
2974 node
= isl_schedule_node_band_member_set_coincident(node
, i
,
2975 graph
->node
[0].coincident
[start
+ i
]);
2976 node
= isl_schedule_node_band_set_permutable(node
, permutable
);
2981 /* Update the dependence relations based on the current schedule,
2982 * add the current band to "node" and the continue with the computation
2984 * Return the updated schedule node.
2986 static __isl_give isl_schedule_node
*compute_next_band(
2987 __isl_take isl_schedule_node
*node
,
2988 struct isl_sched_graph
*graph
, int permutable
)
2995 ctx
= isl_schedule_node_get_ctx(node
);
2996 if (update_edges(ctx
, graph
) < 0)
2997 return isl_schedule_node_free(node
);
2998 node
= insert_current_band(node
, graph
, permutable
);
3001 node
= isl_schedule_node_child(node
, 0);
3002 node
= compute_schedule(node
, graph
);
3003 node
= isl_schedule_node_parent(node
);
3008 /* Add constraints to graph->lp that force the dependence "map" (which
3009 * is part of the dependence relation of "edge")
3010 * to be respected and attempt to carry it, where the edge is one from
3011 * a node j to itself. "pos" is the sequence number of the given map.
3012 * That is, add constraints that enforce
3014 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3015 * = c_j_x (y - x) >= e_i
3017 * for each (x,y) in R.
3018 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3019 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3020 * with each coefficient in c_j_x represented as a pair of non-negative
3023 static int add_intra_constraints(struct isl_sched_graph
*graph
,
3024 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
3027 isl_ctx
*ctx
= isl_map_get_ctx(map
);
3029 isl_dim_map
*dim_map
;
3030 isl_basic_set
*coef
;
3031 struct isl_sched_node
*node
= edge
->src
;
3033 coef
= intra_coefficients(graph
, node
, map
);
3037 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
3039 total
= isl_basic_set_total_dim(graph
->lp
);
3040 dim_map
= isl_dim_map_alloc(ctx
, total
);
3041 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3042 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
3043 isl_space_dim(dim
, isl_dim_set
), 1,
3045 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
3046 isl_space_dim(dim
, isl_dim_set
), 1,
3048 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
3049 coef
->n_eq
, coef
->n_ineq
);
3050 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
3052 isl_space_free(dim
);
3057 /* Add constraints to graph->lp that force the dependence "map" (which
3058 * is part of the dependence relation of "edge")
3059 * to be respected and attempt to carry it, where the edge is one from
3060 * node j to node k. "pos" is the sequence number of the given map.
3061 * That is, add constraints that enforce
3063 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3065 * for each (x,y) in R.
3066 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3067 * of valid constraints for R and then plug in
3068 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3069 * with each coefficient (except e_i, c_k_0 and c_j_0)
3070 * represented as a pair of non-negative coefficients.
3072 static int add_inter_constraints(struct isl_sched_graph
*graph
,
3073 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
3076 isl_ctx
*ctx
= isl_map_get_ctx(map
);
3078 isl_dim_map
*dim_map
;
3079 isl_basic_set
*coef
;
3080 struct isl_sched_node
*src
= edge
->src
;
3081 struct isl_sched_node
*dst
= edge
->dst
;
3083 coef
= inter_coefficients(graph
, edge
, map
);
3087 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
3089 total
= isl_basic_set_total_dim(graph
->lp
);
3090 dim_map
= isl_dim_map_alloc(ctx
, total
);
3092 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3094 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, 1);
3095 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, -1);
3096 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, 1);
3097 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
3098 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
3100 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
3101 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
3104 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, -1);
3105 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, 1);
3106 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, -1);
3107 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
3108 isl_space_dim(dim
, isl_dim_set
), 1,
3110 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
3111 isl_space_dim(dim
, isl_dim_set
), 1,
3114 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
3115 coef
->n_eq
, coef
->n_ineq
);
3116 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
3118 isl_space_free(dim
);
3123 /* Add constraints to graph->lp that force all (conditional) validity
3124 * dependences to be respected and attempt to carry them.
3126 static int add_all_constraints(struct isl_sched_graph
*graph
)
3132 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3133 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
3135 if (!edge
->validity
&& !edge
->conditional_validity
)
3138 for (j
= 0; j
< edge
->map
->n
; ++j
) {
3139 isl_basic_map
*bmap
;
3142 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
3143 map
= isl_map_from_basic_map(bmap
);
3145 if (edge
->src
== edge
->dst
&&
3146 add_intra_constraints(graph
, edge
, map
, pos
) < 0)
3148 if (edge
->src
!= edge
->dst
&&
3149 add_inter_constraints(graph
, edge
, map
, pos
) < 0)
3158 /* Count the number of equality and inequality constraints
3159 * that will be added to the carry_lp problem.
3160 * We count each edge exactly once.
3162 static int count_all_constraints(struct isl_sched_graph
*graph
,
3163 int *n_eq
, int *n_ineq
)
3167 *n_eq
= *n_ineq
= 0;
3168 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3169 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
3170 for (j
= 0; j
< edge
->map
->n
; ++j
) {
3171 isl_basic_map
*bmap
;
3174 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
3175 map
= isl_map_from_basic_map(bmap
);
3177 if (count_map_constraints(graph
, edge
, map
,
3178 n_eq
, n_ineq
, 1, 0) < 0)
3186 /* Construct an LP problem for finding schedule coefficients
3187 * such that the schedule carries as many dependences as possible.
3188 * In particular, for each dependence i, we bound the dependence distance
3189 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3190 * of all e_i's. Dependence with e_i = 0 in the solution are simply
3191 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3192 * Note that if the dependence relation is a union of basic maps,
3193 * then we have to consider each basic map individually as it may only
3194 * be possible to carry the dependences expressed by some of those
3195 * basic maps and not all off them.
3196 * Below, we consider each of those basic maps as a separate "edge".
3198 * All variables of the LP are non-negative. The actual coefficients
3199 * may be negative, so each coefficient is represented as the difference
3200 * of two non-negative variables. The negative part always appears
3201 * immediately before the positive part.
3202 * Other than that, the variables have the following order
3204 * - sum of (1 - e_i) over all edges
3205 * - sum of positive and negative parts of all c_n coefficients
3206 * (unconstrained when computing non-parametric schedules)
3207 * - sum of positive and negative parts of all c_x coefficients
3212 * - positive and negative parts of c_i_n (if parametric)
3213 * - positive and negative parts of c_i_x
3215 * The constraints are those from the (validity) edges plus three equalities
3216 * to express the sums and n_edge inequalities to express e_i <= 1.
3218 static int setup_carry_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3228 for (i
= 0; i
< graph
->n_edge
; ++i
)
3229 n_edge
+= graph
->edge
[i
].map
->n
;
3232 for (i
= 0; i
< graph
->n
; ++i
) {
3233 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
3234 node
->start
= total
;
3235 total
+= 1 + 2 * (node
->nparam
+ node
->nvar
);
3238 if (count_all_constraints(graph
, &n_eq
, &n_ineq
) < 0)
3240 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
3243 dim
= isl_space_set_alloc(ctx
, 0, total
);
3244 isl_basic_set_free(graph
->lp
);
3247 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
3248 graph
->lp
= isl_basic_set_set_rational(graph
->lp
);
3250 k
= isl_basic_set_alloc_equality(graph
->lp
);
3253 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
3254 isl_int_set_si(graph
->lp
->eq
[k
][0], -n_edge
);
3255 isl_int_set_si(graph
->lp
->eq
[k
][1], 1);
3256 for (i
= 0; i
< n_edge
; ++i
)
3257 isl_int_set_si(graph
->lp
->eq
[k
][4 + i
], 1);
3259 k
= isl_basic_set_alloc_equality(graph
->lp
);
3262 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
3263 isl_int_set_si(graph
->lp
->eq
[k
][2], -1);
3264 for (i
= 0; i
< graph
->n
; ++i
) {
3265 int pos
= 1 + graph
->node
[i
].start
+ 1;
3267 for (j
= 0; j
< 2 * graph
->node
[i
].nparam
; ++j
)
3268 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
3271 k
= isl_basic_set_alloc_equality(graph
->lp
);
3274 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
3275 isl_int_set_si(graph
->lp
->eq
[k
][3], -1);
3276 for (i
= 0; i
< graph
->n
; ++i
) {
3277 struct isl_sched_node
*node
= &graph
->node
[i
];
3278 int pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
3280 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
3281 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
3284 for (i
= 0; i
< n_edge
; ++i
) {
3285 k
= isl_basic_set_alloc_inequality(graph
->lp
);
3288 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
3289 isl_int_set_si(graph
->lp
->ineq
[k
][4 + i
], -1);
3290 isl_int_set_si(graph
->lp
->ineq
[k
][0], 1);
3293 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
3295 if (add_all_constraints(graph
) < 0)
3301 static __isl_give isl_schedule_node
*compute_component_schedule(
3302 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
3305 /* Comparison function for sorting the statements based on
3306 * the corresponding value in "r".
3308 static int smaller_value(const void *a
, const void *b
, void *data
)
3314 return isl_int_cmp(r
->el
[*i1
], r
->el
[*i2
]);
3317 /* If the schedule_split_scaled option is set and if the linear
3318 * parts of the scheduling rows for all nodes in the graphs have
3319 * a non-trivial common divisor, then split off the remainder of the
3320 * constant term modulo this common divisor from the linear part.
3321 * Otherwise, insert a band node directly and continue with
3322 * the construction of the schedule.
3324 * If a non-trivial common divisor is found, then
3325 * the linear part is reduced and the remainder is enforced
3326 * by a sequence node with the children placed in the order
3327 * of this remainder.
3328 * In particular, we assign an scc index based on the remainder and
3329 * then rely on compute_component_schedule to insert the sequence and
3330 * to continue the schedule construction on each part.
3332 static __isl_give isl_schedule_node
*split_scaled(
3333 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3346 ctx
= isl_schedule_node_get_ctx(node
);
3347 if (!ctx
->opt
->schedule_split_scaled
)
3348 return compute_next_band(node
, graph
, 0);
3350 return compute_next_band(node
, graph
, 0);
3353 isl_int_init(gcd_i
);
3355 isl_int_set_si(gcd
, 0);
3357 row
= isl_mat_rows(graph
->node
[0].sched
) - 1;
3359 for (i
= 0; i
< graph
->n
; ++i
) {
3360 struct isl_sched_node
*node
= &graph
->node
[i
];
3361 int cols
= isl_mat_cols(node
->sched
);
3363 isl_seq_gcd(node
->sched
->row
[row
] + 1, cols
- 1, &gcd_i
);
3364 isl_int_gcd(gcd
, gcd
, gcd_i
);
3367 isl_int_clear(gcd_i
);
3369 if (isl_int_cmp_si(gcd
, 1) <= 0) {
3371 return compute_next_band(node
, graph
, 0);
3374 r
= isl_vec_alloc(ctx
, graph
->n
);
3375 order
= isl_calloc_array(ctx
, int, graph
->n
);
3379 for (i
= 0; i
< graph
->n
; ++i
) {
3380 struct isl_sched_node
*node
= &graph
->node
[i
];
3383 isl_int_fdiv_r(r
->el
[i
], node
->sched
->row
[row
][0], gcd
);
3384 isl_int_fdiv_q(node
->sched
->row
[row
][0],
3385 node
->sched
->row
[row
][0], gcd
);
3386 isl_int_mul(node
->sched
->row
[row
][0],
3387 node
->sched
->row
[row
][0], gcd
);
3388 node
->sched
= isl_mat_scale_down_row(node
->sched
, row
, gcd
);
3393 if (isl_sort(order
, graph
->n
, sizeof(order
[0]), &smaller_value
, r
) < 0)
3397 for (i
= 0; i
< graph
->n
; ++i
) {
3398 if (i
> 0 && isl_int_ne(r
->el
[order
[i
- 1]], r
->el
[order
[i
]]))
3400 graph
->node
[order
[i
]].scc
= scc
;
3409 if (update_edges(ctx
, graph
) < 0)
3410 return isl_schedule_node_free(node
);
3411 node
= insert_current_band(node
, graph
, 0);
3414 node
= isl_schedule_node_child(node
, 0);
3415 node
= compute_component_schedule(node
, graph
, 0);
3416 node
= isl_schedule_node_parent(node
);
3423 return isl_schedule_node_free(node
);
3426 /* Is the schedule row "sol" trivial on node "node"?
3427 * That is, is the solution zero on the dimensions orthogonal to
3428 * the previously found solutions?
3429 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3431 * Each coefficient is represented as the difference between
3432 * two non-negative values in "sol". "sol" has been computed
3433 * in terms of the original iterators (i.e., without use of cmap).
3434 * We construct the schedule row s and write it as a linear
3435 * combination of (linear combinations of) previously computed schedule rows.
3436 * s = Q c or c = U s.
3437 * If the final entries of c are all zero, then the solution is trivial.
3439 static int is_trivial(struct isl_sched_node
*node
, __isl_keep isl_vec
*sol
)
3449 if (node
->nvar
== node
->rank
)
3452 ctx
= isl_vec_get_ctx(sol
);
3453 node_sol
= isl_vec_alloc(ctx
, node
->nvar
);
3457 pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
3459 for (i
= 0; i
< node
->nvar
; ++i
)
3460 isl_int_sub(node_sol
->el
[i
],
3461 sol
->el
[pos
+ 2 * i
+ 1], sol
->el
[pos
+ 2 * i
]);
3463 node_sol
= isl_mat_vec_product(isl_mat_copy(node
->cinv
), node_sol
);
3468 trivial
= isl_seq_first_non_zero(node_sol
->el
+ node
->rank
,
3469 node
->nvar
- node
->rank
) == -1;
3471 isl_vec_free(node_sol
);
3476 /* Is the schedule row "sol" trivial on any node where it should
3478 * "sol" has been computed in terms of the original iterators
3479 * (i.e., without use of cmap).
3480 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3482 static int is_any_trivial(struct isl_sched_graph
*graph
,
3483 __isl_keep isl_vec
*sol
)
3487 for (i
= 0; i
< graph
->n
; ++i
) {
3488 struct isl_sched_node
*node
= &graph
->node
[i
];
3491 if (!needs_row(graph
, node
))
3493 trivial
= is_trivial(node
, sol
);
3494 if (trivial
< 0 || trivial
)
3501 /* Construct a schedule row for each node such that as many dependences
3502 * as possible are carried and then continue with the next band.
3504 * If the computed schedule row turns out to be trivial on one or
3505 * more nodes where it should not be trivial, then we throw it away
3506 * and try again on each component separately.
3508 * If there is only one component, then we accept the schedule row anyway,
3509 * but we do not consider it as a complete row and therefore do not
3510 * increment graph->n_row. Note that the ranks of the nodes that
3511 * do get a non-trivial schedule part will get updated regardless and
3512 * graph->maxvar is computed based on these ranks. The test for
3513 * whether more schedule rows are required in compute_schedule_wcc
3514 * is therefore not affected.
3516 * Insert a band corresponding to the schedule row at position "node"
3517 * of the schedule tree and continue with the construction of the schedule.
3518 * This insertion and the continued construction is performed by split_scaled
3519 * after optionally checking for non-trivial common divisors.
3521 static __isl_give isl_schedule_node
*carry_dependences(
3522 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3535 for (i
= 0; i
< graph
->n_edge
; ++i
)
3536 n_edge
+= graph
->edge
[i
].map
->n
;
3538 ctx
= isl_schedule_node_get_ctx(node
);
3539 if (setup_carry_lp(ctx
, graph
) < 0)
3540 return isl_schedule_node_free(node
);
3542 lp
= isl_basic_set_copy(graph
->lp
);
3543 sol
= isl_tab_basic_set_non_neg_lexmin(lp
);
3545 return isl_schedule_node_free(node
);
3547 if (sol
->size
== 0) {
3549 isl_die(ctx
, isl_error_internal
,
3550 "error in schedule construction",
3551 return isl_schedule_node_free(node
));
3554 isl_int_divexact(sol
->el
[1], sol
->el
[1], sol
->el
[0]);
3555 if (isl_int_cmp_si(sol
->el
[1], n_edge
) >= 0) {
3557 isl_die(ctx
, isl_error_unknown
,
3558 "unable to carry dependences",
3559 return isl_schedule_node_free(node
));
3562 trivial
= is_any_trivial(graph
, sol
);
3564 sol
= isl_vec_free(sol
);
3565 } else if (trivial
&& graph
->scc
> 1) {
3567 return compute_component_schedule(node
, graph
, 1);
3570 if (update_schedule(graph
, sol
, 0, 0) < 0)
3571 return isl_schedule_node_free(node
);
3575 return split_scaled(node
, graph
);
3578 /* Are there any (non-empty) (conditional) validity edges in the graph?
3580 static int has_validity_edges(struct isl_sched_graph
*graph
)
3584 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3587 empty
= isl_map_plain_is_empty(graph
->edge
[i
].map
);
3592 if (graph
->edge
[i
].validity
||
3593 graph
->edge
[i
].conditional_validity
)
3600 /* Should we apply a Feautrier step?
3601 * That is, did the user request the Feautrier algorithm and are
3602 * there any validity dependences (left)?
3604 static int need_feautrier_step(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3606 if (ctx
->opt
->schedule_algorithm
!= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
)
3609 return has_validity_edges(graph
);
3612 /* Compute a schedule for a connected dependence graph using Feautrier's
3613 * multi-dimensional scheduling algorithm and return the updated schedule node.
3615 * The original algorithm is described in [1].
3616 * The main idea is to minimize the number of scheduling dimensions, by
3617 * trying to satisfy as many dependences as possible per scheduling dimension.
3619 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
3620 * Problem, Part II: Multi-Dimensional Time.
3621 * In Intl. Journal of Parallel Programming, 1992.
3623 static __isl_give isl_schedule_node
*compute_schedule_wcc_feautrier(
3624 isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3626 return carry_dependences(node
, graph
);
3629 /* Turn off the "local" bit on all (condition) edges.
3631 static void clear_local_edges(struct isl_sched_graph
*graph
)
3635 for (i
= 0; i
< graph
->n_edge
; ++i
)
3636 if (graph
->edge
[i
].condition
)
3637 graph
->edge
[i
].local
= 0;
3640 /* Does "graph" have both condition and conditional validity edges?
3642 static int need_condition_check(struct isl_sched_graph
*graph
)
3645 int any_condition
= 0;
3646 int any_conditional_validity
= 0;
3648 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3649 if (graph
->edge
[i
].condition
)
3651 if (graph
->edge
[i
].conditional_validity
)
3652 any_conditional_validity
= 1;
3655 return any_condition
&& any_conditional_validity
;
3658 /* Does "graph" contain any coincidence edge?
3660 static int has_any_coincidence(struct isl_sched_graph
*graph
)
3664 for (i
= 0; i
< graph
->n_edge
; ++i
)
3665 if (graph
->edge
[i
].coincidence
)
3671 /* Extract the final schedule row as a map with the iteration domain
3672 * of "node" as domain.
3674 static __isl_give isl_map
*final_row(struct isl_sched_node
*node
)
3676 isl_local_space
*ls
;
3680 row
= isl_mat_rows(node
->sched
) - 1;
3681 ls
= isl_local_space_from_space(isl_space_copy(node
->space
));
3682 aff
= extract_schedule_row(ls
, node
, row
);
3683 return isl_map_from_aff(aff
);
3686 /* Is the conditional validity dependence in the edge with index "edge_index"
3687 * violated by the latest (i.e., final) row of the schedule?
3688 * That is, is i scheduled after j
3689 * for any conditional validity dependence i -> j?
3691 static int is_violated(struct isl_sched_graph
*graph
, int edge_index
)
3693 isl_map
*src_sched
, *dst_sched
, *map
;
3694 struct isl_sched_edge
*edge
= &graph
->edge
[edge_index
];
3697 src_sched
= final_row(edge
->src
);
3698 dst_sched
= final_row(edge
->dst
);
3699 map
= isl_map_copy(edge
->map
);
3700 map
= isl_map_apply_domain(map
, src_sched
);
3701 map
= isl_map_apply_range(map
, dst_sched
);
3702 map
= isl_map_order_gt(map
, isl_dim_in
, 0, isl_dim_out
, 0);
3703 empty
= isl_map_is_empty(map
);
3712 /* Does the domain of "umap" intersect "uset"?
3714 static int domain_intersects(__isl_keep isl_union_map
*umap
,
3715 __isl_keep isl_union_set
*uset
)
3719 umap
= isl_union_map_copy(umap
);
3720 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(uset
));
3721 empty
= isl_union_map_is_empty(umap
);
3722 isl_union_map_free(umap
);
3724 return empty
< 0 ? -1 : !empty
;
3727 /* Does the range of "umap" intersect "uset"?
3729 static int range_intersects(__isl_keep isl_union_map
*umap
,
3730 __isl_keep isl_union_set
*uset
)
3734 umap
= isl_union_map_copy(umap
);
3735 umap
= isl_union_map_intersect_range(umap
, isl_union_set_copy(uset
));
3736 empty
= isl_union_map_is_empty(umap
);
3737 isl_union_map_free(umap
);
3739 return empty
< 0 ? -1 : !empty
;
3742 /* Are the condition dependences of "edge" local with respect to
3743 * the current schedule?
3745 * That is, are domain and range of the condition dependences mapped
3746 * to the same point?
3748 * In other words, is the condition false?
3750 static int is_condition_false(struct isl_sched_edge
*edge
)
3752 isl_union_map
*umap
;
3753 isl_map
*map
, *sched
, *test
;
3756 umap
= isl_union_map_copy(edge
->tagged_condition
);
3757 umap
= isl_union_map_zip(umap
);
3758 umap
= isl_union_set_unwrap(isl_union_map_domain(umap
));
3759 map
= isl_map_from_union_map(umap
);
3761 sched
= node_extract_schedule(edge
->src
);
3762 map
= isl_map_apply_domain(map
, sched
);
3763 sched
= node_extract_schedule(edge
->dst
);
3764 map
= isl_map_apply_range(map
, sched
);
3766 test
= isl_map_identity(isl_map_get_space(map
));
3767 local
= isl_map_is_subset(map
, test
);
3774 /* Does "graph" have any satisfied condition edges that
3775 * are adjacent to the conditional validity constraint with
3776 * domain "conditional_source" and range "conditional_sink"?
3778 * A satisfied condition is one that is not local.
3779 * If a condition was forced to be local already (i.e., marked as local)
3780 * then there is no need to check if it is in fact local.
3782 * Additionally, mark all adjacent condition edges found as local.
3784 static int has_adjacent_true_conditions(struct isl_sched_graph
*graph
,
3785 __isl_keep isl_union_set
*conditional_source
,
3786 __isl_keep isl_union_set
*conditional_sink
)
3791 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3792 int adjacent
, local
;
3793 isl_union_map
*condition
;
3795 if (!graph
->edge
[i
].condition
)
3797 if (graph
->edge
[i
].local
)
3800 condition
= graph
->edge
[i
].tagged_condition
;
3801 adjacent
= domain_intersects(condition
, conditional_sink
);
3802 if (adjacent
>= 0 && !adjacent
)
3803 adjacent
= range_intersects(condition
,
3804 conditional_source
);
3810 graph
->edge
[i
].local
= 1;
3812 local
= is_condition_false(&graph
->edge
[i
]);
3822 /* Are there any violated conditional validity dependences with
3823 * adjacent condition dependences that are not local with respect
3824 * to the current schedule?
3825 * That is, is the conditional validity constraint violated?
3827 * Additionally, mark all those adjacent condition dependences as local.
3828 * We also mark those adjacent condition dependences that were not marked
3829 * as local before, but just happened to be local already. This ensures
3830 * that they remain local if the schedule is recomputed.
3832 * We first collect domain and range of all violated conditional validity
3833 * dependences and then check if there are any adjacent non-local
3834 * condition dependences.
3836 static int has_violated_conditional_constraint(isl_ctx
*ctx
,
3837 struct isl_sched_graph
*graph
)
3841 isl_union_set
*source
, *sink
;
3843 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3844 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3845 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3846 isl_union_set
*uset
;
3847 isl_union_map
*umap
;
3850 if (!graph
->edge
[i
].conditional_validity
)
3853 violated
= is_violated(graph
, i
);
3861 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
3862 uset
= isl_union_map_domain(umap
);
3863 source
= isl_union_set_union(source
, uset
);
3864 source
= isl_union_set_coalesce(source
);
3866 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
3867 uset
= isl_union_map_range(umap
);
3868 sink
= isl_union_set_union(sink
, uset
);
3869 sink
= isl_union_set_coalesce(sink
);
3873 any
= has_adjacent_true_conditions(graph
, source
, sink
);
3875 isl_union_set_free(source
);
3876 isl_union_set_free(sink
);
3879 isl_union_set_free(source
);
3880 isl_union_set_free(sink
);
3884 /* Compute a schedule for a connected dependence graph and return
3885 * the updated schedule node.
3887 * We try to find a sequence of as many schedule rows as possible that result
3888 * in non-negative dependence distances (independent of the previous rows
3889 * in the sequence, i.e., such that the sequence is tilable), with as
3890 * many of the initial rows as possible satisfying the coincidence constraints.
3891 * If we can't find any more rows we either
3892 * - split between SCCs and start over (assuming we found an interesting
3893 * pair of SCCs between which to split)
3894 * - continue with the next band (assuming the current band has at least
3896 * - try to carry as many dependences as possible and continue with the next
3898 * In each case, we first insert a band node in the schedule tree
3899 * if any rows have been computed.
3901 * If Feautrier's algorithm is selected, we first recursively try to satisfy
3902 * as many validity dependences as possible. When all validity dependences
3903 * are satisfied we extend the schedule to a full-dimensional schedule.
3905 * If we manage to complete the schedule, we insert a band node
3906 * (if any schedule rows were computed) and we finish off by topologically
3907 * sorting the statements based on the remaining dependences.
3909 * If ctx->opt->schedule_outer_coincidence is set, then we force the
3910 * outermost dimension to satisfy the coincidence constraints. If this
3911 * turns out to be impossible, we fall back on the general scheme above
3912 * and try to carry as many dependences as possible.
3914 * If "graph" contains both condition and conditional validity dependences,
3915 * then we need to check that that the conditional schedule constraint
3916 * is satisfied, i.e., there are no violated conditional validity dependences
3917 * that are adjacent to any non-local condition dependences.
3918 * If there are, then we mark all those adjacent condition dependences
3919 * as local and recompute the current band. Those dependences that
3920 * are marked local will then be forced to be local.
3921 * The initial computation is performed with no dependences marked as local.
3922 * If we are lucky, then there will be no violated conditional validity
3923 * dependences adjacent to any non-local condition dependences.
3924 * Otherwise, we mark some additional condition dependences as local and
3925 * recompute. We continue this process until there are no violations left or
3926 * until we are no longer able to compute a schedule.
3927 * Since there are only a finite number of dependences,
3928 * there will only be a finite number of iterations.
3930 static __isl_give isl_schedule_node
*compute_schedule_wcc(
3931 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3933 int has_coincidence
;
3934 int use_coincidence
;
3935 int force_coincidence
= 0;
3936 int check_conditional
;
3942 ctx
= isl_schedule_node_get_ctx(node
);
3943 if (detect_sccs(ctx
, graph
) < 0)
3944 return isl_schedule_node_free(node
);
3945 if (sort_sccs(graph
) < 0)
3946 return isl_schedule_node_free(node
);
3948 if (compute_maxvar(graph
) < 0)
3949 return isl_schedule_node_free(node
);
3951 if (need_feautrier_step(ctx
, graph
))
3952 return compute_schedule_wcc_feautrier(node
, graph
);
3954 clear_local_edges(graph
);
3955 check_conditional
= need_condition_check(graph
);
3956 has_coincidence
= has_any_coincidence(graph
);
3958 if (ctx
->opt
->schedule_outer_coincidence
)
3959 force_coincidence
= 1;
3961 use_coincidence
= has_coincidence
;
3962 while (graph
->n_row
< graph
->maxvar
) {
3967 graph
->src_scc
= -1;
3968 graph
->dst_scc
= -1;
3970 if (setup_lp(ctx
, graph
, use_coincidence
) < 0)
3971 return isl_schedule_node_free(node
);
3972 sol
= solve_lp(graph
);
3974 return isl_schedule_node_free(node
);
3975 if (sol
->size
== 0) {
3976 int empty
= graph
->n_total_row
== graph
->band_start
;
3979 if (use_coincidence
&& (!force_coincidence
|| !empty
)) {
3980 use_coincidence
= 0;
3983 if (!ctx
->opt
->schedule_maximize_band_depth
&& !empty
)
3984 return compute_next_band(node
, graph
, 1);
3985 if (graph
->src_scc
>= 0)
3986 return compute_split_schedule(node
, graph
);
3988 return compute_next_band(node
, graph
, 1);
3989 return carry_dependences(node
, graph
);
3991 coincident
= !has_coincidence
|| use_coincidence
;
3992 if (update_schedule(graph
, sol
, 1, coincident
) < 0)
3993 return isl_schedule_node_free(node
);
3995 if (!check_conditional
)
3997 violated
= has_violated_conditional_constraint(ctx
, graph
);
3999 return isl_schedule_node_free(node
);
4002 if (reset_band(graph
) < 0)
4003 return isl_schedule_node_free(node
);
4004 use_coincidence
= has_coincidence
;
4007 if (graph
->n_total_row
> graph
->band_start
) {
4008 node
= insert_current_band(node
, graph
, 1);
4009 node
= isl_schedule_node_child(node
, 0);
4011 node
= sort_statements(node
, graph
);
4012 if (graph
->n_total_row
> graph
->band_start
)
4013 node
= isl_schedule_node_parent(node
);
4018 /* Compute a schedule for each group of nodes identified by node->scc
4019 * separately and then combine them in a sequence node (or as set node
4020 * if graph->weak is set) inserted at position "node" of the schedule tree.
4021 * Return the updated schedule node.
4023 * If "wcc" is set then each of the groups belongs to a single
4024 * weakly connected component in the dependence graph so that
4025 * there is no need for compute_sub_schedule to look for weakly
4026 * connected components.
4028 static __isl_give isl_schedule_node
*compute_component_schedule(
4029 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
4036 isl_union_set_list
*filters
;
4040 ctx
= isl_schedule_node_get_ctx(node
);
4042 filters
= extract_sccs(ctx
, graph
);
4044 node
= isl_schedule_node_insert_set(node
, filters
);
4046 node
= isl_schedule_node_insert_sequence(node
, filters
);
4048 orig_total_row
= graph
->n_total_row
;
4049 for (component
= 0; component
< graph
->scc
; ++component
) {
4051 for (i
= 0; i
< graph
->n
; ++i
)
4052 if (graph
->node
[i
].scc
== component
)
4055 for (i
= 0; i
< graph
->n_edge
; ++i
)
4056 if (graph
->edge
[i
].src
->scc
== component
&&
4057 graph
->edge
[i
].dst
->scc
== component
)
4060 node
= isl_schedule_node_child(node
, component
);
4061 node
= isl_schedule_node_child(node
, 0);
4062 node
= compute_sub_schedule(node
, ctx
, graph
, n
, n_edge
,
4064 &edge_scc_exactly
, component
, wcc
);
4065 node
= isl_schedule_node_parent(node
);
4066 node
= isl_schedule_node_parent(node
);
4067 graph
->n_total_row
= orig_total_row
;
4073 /* Compute a schedule for the given dependence graph and insert it at "node".
4074 * Return the updated schedule node.
4076 * We first check if the graph is connected (through validity and conditional
4077 * validity dependences) and, if not, compute a schedule
4078 * for each component separately.
4079 * If schedule_fuse is set to minimal fusion, then we check for strongly
4080 * connected components instead and compute a separate schedule for
4081 * each such strongly connected component.
4083 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
4084 struct isl_sched_graph
*graph
)
4091 ctx
= isl_schedule_node_get_ctx(node
);
4092 if (ctx
->opt
->schedule_fuse
== ISL_SCHEDULE_FUSE_MIN
) {
4093 if (detect_sccs(ctx
, graph
) < 0)
4094 return isl_schedule_node_free(node
);
4096 if (detect_wccs(ctx
, graph
) < 0)
4097 return isl_schedule_node_free(node
);
4101 return compute_component_schedule(node
, graph
, 1);
4103 return compute_schedule_wcc(node
, graph
);
4106 /* Compute a schedule on sc->domain that respects the given schedule
4109 * In particular, the schedule respects all the validity dependences.
4110 * If the default isl scheduling algorithm is used, it tries to minimize
4111 * the dependence distances over the proximity dependences.
4112 * If Feautrier's scheduling algorithm is used, the proximity dependence
4113 * distances are only minimized during the extension to a full-dimensional
4116 * If there are any condition and conditional validity dependences,
4117 * then the conditional validity dependences may be violated inside
4118 * a tilable band, provided they have no adjacent non-local
4119 * condition dependences.
4121 * The context is included in the domain before the nodes of
4122 * the graphs are extracted in order to be able to exploit
4123 * any possible additional equalities.
4124 * However, the returned schedule contains the original domain
4125 * (before this intersection).
4127 __isl_give isl_schedule
*isl_schedule_constraints_compute_schedule(
4128 __isl_take isl_schedule_constraints
*sc
)
4130 isl_ctx
*ctx
= isl_schedule_constraints_get_ctx(sc
);
4131 struct isl_sched_graph graph
= { 0 };
4132 isl_schedule
*sched
;
4133 isl_schedule_node
*node
;
4134 isl_union_set
*domain
;
4135 struct isl_extract_edge_data data
;
4136 enum isl_edge_type i
;
4139 sc
= isl_schedule_constraints_align_params(sc
);
4143 graph
.n
= isl_union_set_n_set(sc
->domain
);
4145 isl_union_set
*domain
= isl_union_set_copy(sc
->domain
);
4146 sched
= isl_schedule_from_domain(domain
);
4149 if (graph_alloc(ctx
, &graph
, graph
.n
,
4150 isl_schedule_constraints_n_map(sc
)) < 0)
4152 if (compute_max_row(&graph
, sc
) < 0)
4156 domain
= isl_union_set_copy(sc
->domain
);
4157 domain
= isl_union_set_intersect_params(domain
,
4158 isl_set_copy(sc
->context
));
4159 r
= isl_union_set_foreach_set(domain
, &extract_node
, &graph
);
4160 isl_union_set_free(domain
);
4163 if (graph_init_table(ctx
, &graph
) < 0)
4165 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
4166 graph
.max_edge
[i
] = isl_union_map_n_map(sc
->constraint
[i
]);
4167 if (graph_init_edge_tables(ctx
, &graph
) < 0)
4170 data
.graph
= &graph
;
4171 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
4173 if (isl_union_map_foreach_map(sc
->constraint
[i
],
4174 &extract_edge
, &data
) < 0)
4178 node
= isl_schedule_node_from_domain(isl_union_set_copy(sc
->domain
));
4179 node
= isl_schedule_node_child(node
, 0);
4180 node
= compute_schedule(node
, &graph
);
4181 sched
= isl_schedule_node_get_schedule(node
);
4182 isl_schedule_node_free(node
);
4185 graph_free(ctx
, &graph
);
4186 isl_schedule_constraints_free(sc
);
4190 graph_free(ctx
, &graph
);
4191 isl_schedule_constraints_free(sc
);
4195 /* Compute a schedule for the given union of domains that respects
4196 * all the validity dependences and minimizes
4197 * the dependence distances over the proximity dependences.
4199 * This function is kept for backward compatibility.
4201 __isl_give isl_schedule
*isl_union_set_compute_schedule(
4202 __isl_take isl_union_set
*domain
,
4203 __isl_take isl_union_map
*validity
,
4204 __isl_take isl_union_map
*proximity
)
4206 isl_schedule_constraints
*sc
;
4208 sc
= isl_schedule_constraints_on_domain(domain
);
4209 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
4210 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
4212 return isl_schedule_constraints_compute_schedule(sc
);