2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2014 Ecole Normale Superieure
4 * Copyright 2015 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
16 #include <isl_space_private.h>
17 #include <isl_aff_private.h>
19 #include <isl/constraint.h>
20 #include <isl/schedule.h>
21 #include <isl/schedule_node.h>
22 #include <isl_mat_private.h>
23 #include <isl_vec_private.h>
25 #include <isl/union_set.h>
28 #include <isl_dim_map.h>
29 #include <isl/map_to_basic_set.h>
31 #include <isl_options_private.h>
32 #include <isl_tarjan.h>
33 #include <isl_morph.h>
36 * The scheduling algorithm implemented in this file was inspired by
37 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
38 * Parallelization and Locality Optimization in the Polyhedral Model".
42 isl_edge_validity
= 0,
43 isl_edge_first
= isl_edge_validity
,
46 isl_edge_conditional_validity
,
48 isl_edge_last
= isl_edge_proximity
51 /* The constraints that need to be satisfied by a schedule on "domain".
53 * "context" specifies extra constraints on the parameters.
55 * "validity" constraints map domain elements i to domain elements
56 * that should be scheduled after i. (Hard constraint)
57 * "proximity" constraints map domain elements i to domains elements
58 * that should be scheduled as early as possible after i (or before i).
61 * "condition" and "conditional_validity" constraints map possibly "tagged"
62 * domain elements i -> s to "tagged" domain elements j -> t.
63 * The elements of the "conditional_validity" constraints, but without the
64 * tags (i.e., the elements i -> j) are treated as validity constraints,
65 * except that during the construction of a tilable band,
66 * the elements of the "conditional_validity" constraints may be violated
67 * provided that all adjacent elements of the "condition" constraints
68 * are local within the band.
69 * A dependence is local within a band if domain and range are mapped
70 * to the same schedule point by the band.
72 struct isl_schedule_constraints
{
73 isl_union_set
*domain
;
76 isl_union_map
*constraint
[isl_edge_last
+ 1];
79 __isl_give isl_schedule_constraints
*isl_schedule_constraints_copy(
80 __isl_keep isl_schedule_constraints
*sc
)
83 isl_schedule_constraints
*sc_copy
;
86 ctx
= isl_union_set_get_ctx(sc
->domain
);
87 sc_copy
= isl_calloc_type(ctx
, struct isl_schedule_constraints
);
91 sc_copy
->domain
= isl_union_set_copy(sc
->domain
);
92 sc_copy
->context
= isl_set_copy(sc
->context
);
93 if (!sc_copy
->domain
|| !sc_copy
->context
)
94 return isl_schedule_constraints_free(sc_copy
);
96 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
97 sc_copy
->constraint
[i
] = isl_union_map_copy(sc
->constraint
[i
]);
98 if (!sc_copy
->constraint
[i
])
99 return isl_schedule_constraints_free(sc_copy
);
106 /* Construct an isl_schedule_constraints object for computing a schedule
107 * on "domain". The initial object does not impose any constraints.
109 __isl_give isl_schedule_constraints
*isl_schedule_constraints_on_domain(
110 __isl_take isl_union_set
*domain
)
114 isl_schedule_constraints
*sc
;
115 isl_union_map
*empty
;
116 enum isl_edge_type i
;
121 ctx
= isl_union_set_get_ctx(domain
);
122 sc
= isl_calloc_type(ctx
, struct isl_schedule_constraints
);
126 space
= isl_union_set_get_space(domain
);
128 sc
->context
= isl_set_universe(isl_space_copy(space
));
129 empty
= isl_union_map_empty(space
);
130 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
131 sc
->constraint
[i
] = isl_union_map_copy(empty
);
132 if (!sc
->constraint
[i
])
133 sc
->domain
= isl_union_set_free(sc
->domain
);
135 isl_union_map_free(empty
);
137 if (!sc
->domain
|| !sc
->context
)
138 return isl_schedule_constraints_free(sc
);
142 isl_union_set_free(domain
);
146 /* Replace the context of "sc" by "context".
148 __isl_give isl_schedule_constraints
*isl_schedule_constraints_set_context(
149 __isl_take isl_schedule_constraints
*sc
, __isl_take isl_set
*context
)
154 isl_set_free(sc
->context
);
155 sc
->context
= context
;
159 isl_schedule_constraints_free(sc
);
160 isl_set_free(context
);
164 /* Replace the validity constraints of "sc" by "validity".
166 __isl_give isl_schedule_constraints
*isl_schedule_constraints_set_validity(
167 __isl_take isl_schedule_constraints
*sc
,
168 __isl_take isl_union_map
*validity
)
170 if (!sc
|| !validity
)
173 isl_union_map_free(sc
->constraint
[isl_edge_validity
]);
174 sc
->constraint
[isl_edge_validity
] = validity
;
178 isl_schedule_constraints_free(sc
);
179 isl_union_map_free(validity
);
183 /* Replace the coincidence constraints of "sc" by "coincidence".
185 __isl_give isl_schedule_constraints
*isl_schedule_constraints_set_coincidence(
186 __isl_take isl_schedule_constraints
*sc
,
187 __isl_take isl_union_map
*coincidence
)
189 if (!sc
|| !coincidence
)
192 isl_union_map_free(sc
->constraint
[isl_edge_coincidence
]);
193 sc
->constraint
[isl_edge_coincidence
] = coincidence
;
197 isl_schedule_constraints_free(sc
);
198 isl_union_map_free(coincidence
);
202 /* Replace the proximity constraints of "sc" by "proximity".
204 __isl_give isl_schedule_constraints
*isl_schedule_constraints_set_proximity(
205 __isl_take isl_schedule_constraints
*sc
,
206 __isl_take isl_union_map
*proximity
)
208 if (!sc
|| !proximity
)
211 isl_union_map_free(sc
->constraint
[isl_edge_proximity
]);
212 sc
->constraint
[isl_edge_proximity
] = proximity
;
216 isl_schedule_constraints_free(sc
);
217 isl_union_map_free(proximity
);
221 /* Replace the conditional validity constraints of "sc" by "condition"
224 __isl_give isl_schedule_constraints
*
225 isl_schedule_constraints_set_conditional_validity(
226 __isl_take isl_schedule_constraints
*sc
,
227 __isl_take isl_union_map
*condition
,
228 __isl_take isl_union_map
*validity
)
230 if (!sc
|| !condition
|| !validity
)
233 isl_union_map_free(sc
->constraint
[isl_edge_condition
]);
234 sc
->constraint
[isl_edge_condition
] = condition
;
235 isl_union_map_free(sc
->constraint
[isl_edge_conditional_validity
]);
236 sc
->constraint
[isl_edge_conditional_validity
] = validity
;
240 isl_schedule_constraints_free(sc
);
241 isl_union_map_free(condition
);
242 isl_union_map_free(validity
);
246 __isl_null isl_schedule_constraints
*isl_schedule_constraints_free(
247 __isl_take isl_schedule_constraints
*sc
)
249 enum isl_edge_type i
;
254 isl_union_set_free(sc
->domain
);
255 isl_set_free(sc
->context
);
256 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
257 isl_union_map_free(sc
->constraint
[i
]);
264 isl_ctx
*isl_schedule_constraints_get_ctx(
265 __isl_keep isl_schedule_constraints
*sc
)
267 return sc
? isl_union_set_get_ctx(sc
->domain
) : NULL
;
270 /* Return the validity constraints of "sc".
272 __isl_give isl_union_map
*isl_schedule_constraints_get_validity(
273 __isl_keep isl_schedule_constraints
*sc
)
278 return isl_union_map_copy(sc
->constraint
[isl_edge_validity
]);
281 /* Return the coincidence constraints of "sc".
283 __isl_give isl_union_map
*isl_schedule_constraints_get_coincidence(
284 __isl_keep isl_schedule_constraints
*sc
)
289 return isl_union_map_copy(sc
->constraint
[isl_edge_coincidence
]);
292 /* Return the conditional validity constraints of "sc".
294 __isl_give isl_union_map
*isl_schedule_constraints_get_conditional_validity(
295 __isl_keep isl_schedule_constraints
*sc
)
301 isl_union_map_copy(sc
->constraint
[isl_edge_conditional_validity
]);
304 /* Return the conditions for the conditional validity constraints of "sc".
306 __isl_give isl_union_map
*
307 isl_schedule_constraints_get_conditional_validity_condition(
308 __isl_keep isl_schedule_constraints
*sc
)
313 return isl_union_map_copy(sc
->constraint
[isl_edge_condition
]);
316 void isl_schedule_constraints_dump(__isl_keep isl_schedule_constraints
*sc
)
321 fprintf(stderr
, "domain: ");
322 isl_union_set_dump(sc
->domain
);
323 fprintf(stderr
, "context: ");
324 isl_set_dump(sc
->context
);
325 fprintf(stderr
, "validity: ");
326 isl_union_map_dump(sc
->constraint
[isl_edge_validity
]);
327 fprintf(stderr
, "proximity: ");
328 isl_union_map_dump(sc
->constraint
[isl_edge_proximity
]);
329 fprintf(stderr
, "coincidence: ");
330 isl_union_map_dump(sc
->constraint
[isl_edge_coincidence
]);
331 fprintf(stderr
, "condition: ");
332 isl_union_map_dump(sc
->constraint
[isl_edge_condition
]);
333 fprintf(stderr
, "conditional_validity: ");
334 isl_union_map_dump(sc
->constraint
[isl_edge_conditional_validity
]);
337 /* Align the parameters of the fields of "sc".
339 static __isl_give isl_schedule_constraints
*
340 isl_schedule_constraints_align_params(__isl_take isl_schedule_constraints
*sc
)
343 enum isl_edge_type i
;
348 space
= isl_union_set_get_space(sc
->domain
);
349 space
= isl_space_align_params(space
, isl_set_get_space(sc
->context
));
350 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
351 space
= isl_space_align_params(space
,
352 isl_union_map_get_space(sc
->constraint
[i
]));
354 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
355 sc
->constraint
[i
] = isl_union_map_align_params(
356 sc
->constraint
[i
], isl_space_copy(space
));
357 if (!sc
->constraint
[i
])
358 space
= isl_space_free(space
);
360 sc
->context
= isl_set_align_params(sc
->context
, isl_space_copy(space
));
361 sc
->domain
= isl_union_set_align_params(sc
->domain
, space
);
362 if (!sc
->context
|| !sc
->domain
)
363 return isl_schedule_constraints_free(sc
);
368 /* Return the total number of isl_maps in the constraints of "sc".
370 static __isl_give
int isl_schedule_constraints_n_map(
371 __isl_keep isl_schedule_constraints
*sc
)
373 enum isl_edge_type i
;
376 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
377 n
+= isl_union_map_n_map(sc
->constraint
[i
]);
382 /* Internal information about a node that is used during the construction
384 * space represents the space in which the domain lives
385 * sched is a matrix representation of the schedule being constructed
386 * for this node; if compressed is set, then this schedule is
387 * defined over the compressed domain space
388 * sched_map is an isl_map representation of the same (partial) schedule
389 * sched_map may be NULL; if compressed is set, then this map
390 * is defined over the uncompressed domain space
391 * rank is the number of linearly independent rows in the linear part
393 * the columns of cmap represent a change of basis for the schedule
394 * coefficients; the first rank columns span the linear part of
396 * cinv is the inverse of cmap.
397 * start is the first variable in the LP problem in the sequences that
398 * represents the schedule coefficients of this node
399 * nvar is the dimension of the domain
400 * nparam is the number of parameters or 0 if we are not constructing
401 * a parametric schedule
403 * If compressed is set, then hull represents the constraints
404 * that were used to derive the compression, while compress and
405 * decompress map the original space to the compressed space and
408 * scc is the index of SCC (or WCC) this node belongs to
410 * coincident contains a boolean for each of the rows of the schedule,
411 * indicating whether the corresponding scheduling dimension satisfies
412 * the coincidence constraints in the sense that the corresponding
413 * dependence distances are zero.
415 struct isl_sched_node
{
419 isl_multi_aff
*compress
;
420 isl_multi_aff
*decompress
;
435 static int node_has_space(const void *entry
, const void *val
)
437 struct isl_sched_node
*node
= (struct isl_sched_node
*)entry
;
438 isl_space
*dim
= (isl_space
*)val
;
440 return isl_space_is_equal(node
->space
, dim
);
443 static int node_scc_exactly(struct isl_sched_node
*node
, int scc
)
445 return node
->scc
== scc
;
448 static int node_scc_at_most(struct isl_sched_node
*node
, int scc
)
450 return node
->scc
<= scc
;
453 static int node_scc_at_least(struct isl_sched_node
*node
, int scc
)
455 return node
->scc
>= scc
;
458 /* An edge in the dependence graph. An edge may be used to
459 * ensure validity of the generated schedule, to minimize the dependence
462 * map is the dependence relation, with i -> j in the map if j depends on i
463 * tagged_condition and tagged_validity contain the union of all tagged
464 * condition or conditional validity dependence relations that
465 * specialize the dependence relation "map"; that is,
466 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
467 * or "tagged_validity", then i -> j is an element of "map".
468 * If these fields are NULL, then they represent the empty relation.
469 * src is the source node
470 * dst is the sink node
471 * validity is set if the edge is used to ensure correctness
472 * coincidence is used to enforce zero dependence distances
473 * proximity is set if the edge is used to minimize dependence distances
474 * condition is set if the edge represents a condition
475 * for a conditional validity schedule constraint
476 * local can only be set for condition edges and indicates that
477 * the dependence distance over the edge should be zero
478 * conditional_validity is set if the edge is used to conditionally
481 * For validity edges, start and end mark the sequence of inequality
482 * constraints in the LP problem that encode the validity constraint
483 * corresponding to this edge.
485 struct isl_sched_edge
{
487 isl_union_map
*tagged_condition
;
488 isl_union_map
*tagged_validity
;
490 struct isl_sched_node
*src
;
491 struct isl_sched_node
*dst
;
493 unsigned validity
: 1;
494 unsigned coincidence
: 1;
495 unsigned proximity
: 1;
497 unsigned condition
: 1;
498 unsigned conditional_validity
: 1;
504 /* Internal information about the dependence graph used during
505 * the construction of the schedule.
507 * intra_hmap is a cache, mapping dependence relations to their dual,
508 * for dependences from a node to itself
509 * inter_hmap is a cache, mapping dependence relations to their dual,
510 * for dependences between distinct nodes
511 * if compression is involved then the key for these maps
512 * it the original, uncompressed dependence relation, while
513 * the value is the dual of the compressed dependence relation.
515 * n is the number of nodes
516 * node is the list of nodes
517 * maxvar is the maximal number of variables over all nodes
518 * max_row is the allocated number of rows in the schedule
519 * n_row is the current (maximal) number of linearly independent
520 * rows in the node schedules
521 * n_total_row is the current number of rows in the node schedules
522 * band_start is the starting row in the node schedules of the current band
523 * root is set if this graph is the original dependence graph,
524 * without any splitting
526 * sorted contains a list of node indices sorted according to the
527 * SCC to which a node belongs
529 * n_edge is the number of edges
530 * edge is the list of edges
531 * max_edge contains the maximal number of edges of each type;
532 * in particular, it contains the number of edges in the inital graph.
533 * edge_table contains pointers into the edge array, hashed on the source
534 * and sink spaces; there is one such table for each type;
535 * a given edge may be referenced from more than one table
536 * if the corresponding relation appears in more than of the
537 * sets of dependences
539 * node_table contains pointers into the node array, hashed on the space
541 * region contains a list of variable sequences that should be non-trivial
543 * lp contains the (I)LP problem used to obtain new schedule rows
545 * src_scc and dst_scc are the source and sink SCCs of an edge with
546 * conflicting constraints
548 * scc represents the number of components
549 * weak is set if the components are weakly connected
551 struct isl_sched_graph
{
552 isl_map_to_basic_set
*intra_hmap
;
553 isl_map_to_basic_set
*inter_hmap
;
555 struct isl_sched_node
*node
;
568 struct isl_sched_edge
*edge
;
570 int max_edge
[isl_edge_last
+ 1];
571 struct isl_hash_table
*edge_table
[isl_edge_last
+ 1];
573 struct isl_hash_table
*node_table
;
574 struct isl_region
*region
;
585 /* Initialize node_table based on the list of nodes.
587 static int graph_init_table(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
591 graph
->node_table
= isl_hash_table_alloc(ctx
, graph
->n
);
592 if (!graph
->node_table
)
595 for (i
= 0; i
< graph
->n
; ++i
) {
596 struct isl_hash_table_entry
*entry
;
599 hash
= isl_space_get_hash(graph
->node
[i
].space
);
600 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
602 graph
->node
[i
].space
, 1);
605 entry
->data
= &graph
->node
[i
];
611 /* Return a pointer to the node that lives within the given space,
612 * or NULL if there is no such node.
614 static struct isl_sched_node
*graph_find_node(isl_ctx
*ctx
,
615 struct isl_sched_graph
*graph
, __isl_keep isl_space
*dim
)
617 struct isl_hash_table_entry
*entry
;
620 hash
= isl_space_get_hash(dim
);
621 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
622 &node_has_space
, dim
, 0);
624 return entry
? entry
->data
: NULL
;
627 static int edge_has_src_and_dst(const void *entry
, const void *val
)
629 const struct isl_sched_edge
*edge
= entry
;
630 const struct isl_sched_edge
*temp
= val
;
632 return edge
->src
== temp
->src
&& edge
->dst
== temp
->dst
;
635 /* Add the given edge to graph->edge_table[type].
637 static int graph_edge_table_add(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
638 enum isl_edge_type type
, struct isl_sched_edge
*edge
)
640 struct isl_hash_table_entry
*entry
;
643 hash
= isl_hash_init();
644 hash
= isl_hash_builtin(hash
, edge
->src
);
645 hash
= isl_hash_builtin(hash
, edge
->dst
);
646 entry
= isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
647 &edge_has_src_and_dst
, edge
, 1);
655 /* Allocate the edge_tables based on the maximal number of edges of
658 static int graph_init_edge_tables(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
662 for (i
= 0; i
<= isl_edge_last
; ++i
) {
663 graph
->edge_table
[i
] = isl_hash_table_alloc(ctx
,
665 if (!graph
->edge_table
[i
])
672 /* If graph->edge_table[type] contains an edge from the given source
673 * to the given destination, then return the hash table entry of this edge.
674 * Otherwise, return NULL.
676 static struct isl_hash_table_entry
*graph_find_edge_entry(
677 struct isl_sched_graph
*graph
,
678 enum isl_edge_type type
,
679 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
681 isl_ctx
*ctx
= isl_space_get_ctx(src
->space
);
683 struct isl_sched_edge temp
= { .src
= src
, .dst
= dst
};
685 hash
= isl_hash_init();
686 hash
= isl_hash_builtin(hash
, temp
.src
);
687 hash
= isl_hash_builtin(hash
, temp
.dst
);
688 return isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
689 &edge_has_src_and_dst
, &temp
, 0);
693 /* If graph->edge_table[type] contains an edge from the given source
694 * to the given destination, then return this edge.
695 * Otherwise, return NULL.
697 static struct isl_sched_edge
*graph_find_edge(struct isl_sched_graph
*graph
,
698 enum isl_edge_type type
,
699 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
701 struct isl_hash_table_entry
*entry
;
703 entry
= graph_find_edge_entry(graph
, type
, src
, dst
);
710 /* Check whether the dependence graph has an edge of the given type
711 * between the given two nodes.
713 static int graph_has_edge(struct isl_sched_graph
*graph
,
714 enum isl_edge_type type
,
715 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
717 struct isl_sched_edge
*edge
;
720 edge
= graph_find_edge(graph
, type
, src
, dst
);
724 empty
= isl_map_plain_is_empty(edge
->map
);
731 /* Look for any edge with the same src, dst and map fields as "model".
733 * Return the matching edge if one can be found.
734 * Return "model" if no matching edge is found.
735 * Return NULL on error.
737 static struct isl_sched_edge
*graph_find_matching_edge(
738 struct isl_sched_graph
*graph
, struct isl_sched_edge
*model
)
740 enum isl_edge_type i
;
741 struct isl_sched_edge
*edge
;
743 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
746 edge
= graph_find_edge(graph
, i
, model
->src
, model
->dst
);
749 is_equal
= isl_map_plain_is_equal(model
->map
, edge
->map
);
759 /* Remove the given edge from all the edge_tables that refer to it.
761 static void graph_remove_edge(struct isl_sched_graph
*graph
,
762 struct isl_sched_edge
*edge
)
764 isl_ctx
*ctx
= isl_map_get_ctx(edge
->map
);
765 enum isl_edge_type i
;
767 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
768 struct isl_hash_table_entry
*entry
;
770 entry
= graph_find_edge_entry(graph
, i
, edge
->src
, edge
->dst
);
773 if (entry
->data
!= edge
)
775 isl_hash_table_remove(ctx
, graph
->edge_table
[i
], entry
);
779 /* Check whether the dependence graph has any edge
780 * between the given two nodes.
782 static int graph_has_any_edge(struct isl_sched_graph
*graph
,
783 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
785 enum isl_edge_type i
;
788 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
789 r
= graph_has_edge(graph
, i
, src
, dst
);
797 /* Check whether the dependence graph has a validity edge
798 * between the given two nodes.
800 * Conditional validity edges are essentially validity edges that
801 * can be ignored if the corresponding condition edges are iteration private.
802 * Here, we are only checking for the presence of validity
803 * edges, so we need to consider the conditional validity edges too.
804 * In particular, this function is used during the detection
805 * of strongly connected components and we cannot ignore
806 * conditional validity edges during this detection.
808 static int graph_has_validity_edge(struct isl_sched_graph
*graph
,
809 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
813 r
= graph_has_edge(graph
, isl_edge_validity
, src
, dst
);
817 return graph_has_edge(graph
, isl_edge_conditional_validity
, src
, dst
);
820 static int graph_alloc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
821 int n_node
, int n_edge
)
826 graph
->n_edge
= n_edge
;
827 graph
->node
= isl_calloc_array(ctx
, struct isl_sched_node
, graph
->n
);
828 graph
->sorted
= isl_calloc_array(ctx
, int, graph
->n
);
829 graph
->region
= isl_alloc_array(ctx
, struct isl_region
, graph
->n
);
830 graph
->edge
= isl_calloc_array(ctx
,
831 struct isl_sched_edge
, graph
->n_edge
);
833 graph
->intra_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
834 graph
->inter_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
836 if (!graph
->node
|| !graph
->region
|| (graph
->n_edge
&& !graph
->edge
) ||
840 for(i
= 0; i
< graph
->n
; ++i
)
841 graph
->sorted
[i
] = i
;
846 static void graph_free(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
850 isl_map_to_basic_set_free(graph
->intra_hmap
);
851 isl_map_to_basic_set_free(graph
->inter_hmap
);
854 for (i
= 0; i
< graph
->n
; ++i
) {
855 isl_space_free(graph
->node
[i
].space
);
856 isl_set_free(graph
->node
[i
].hull
);
857 isl_multi_aff_free(graph
->node
[i
].compress
);
858 isl_multi_aff_free(graph
->node
[i
].decompress
);
859 isl_mat_free(graph
->node
[i
].sched
);
860 isl_map_free(graph
->node
[i
].sched_map
);
861 isl_mat_free(graph
->node
[i
].cmap
);
862 isl_mat_free(graph
->node
[i
].cinv
);
864 free(graph
->node
[i
].coincident
);
869 for (i
= 0; i
< graph
->n_edge
; ++i
) {
870 isl_map_free(graph
->edge
[i
].map
);
871 isl_union_map_free(graph
->edge
[i
].tagged_condition
);
872 isl_union_map_free(graph
->edge
[i
].tagged_validity
);
876 for (i
= 0; i
<= isl_edge_last
; ++i
)
877 isl_hash_table_free(ctx
, graph
->edge_table
[i
]);
878 isl_hash_table_free(ctx
, graph
->node_table
);
879 isl_basic_set_free(graph
->lp
);
882 /* For each "set" on which this function is called, increment
883 * graph->n by one and update graph->maxvar.
885 static int init_n_maxvar(__isl_take isl_set
*set
, void *user
)
887 struct isl_sched_graph
*graph
= user
;
888 int nvar
= isl_set_dim(set
, isl_dim_set
);
891 if (nvar
> graph
->maxvar
)
892 graph
->maxvar
= nvar
;
899 /* Add the number of basic maps in "map" to *n.
901 static int add_n_basic_map(__isl_take isl_map
*map
, void *user
)
905 *n
+= isl_map_n_basic_map(map
);
911 /* Compute the number of rows that should be allocated for the schedule.
912 * In particular, we need one row for each variable or one row
913 * for each basic map in the dependences.
914 * Note that it is practically impossible to exhaust both
915 * the number of dependences and the number of variables.
917 static int compute_max_row(struct isl_sched_graph
*graph
,
918 __isl_keep isl_schedule_constraints
*sc
)
920 enum isl_edge_type i
;
925 if (isl_union_set_foreach_set(sc
->domain
, &init_n_maxvar
, graph
) < 0)
928 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
929 if (isl_union_map_foreach_map(sc
->constraint
[i
],
930 &add_n_basic_map
, &n_edge
) < 0)
932 graph
->max_row
= n_edge
+ graph
->maxvar
;
937 /* Does "bset" have any defining equalities for its set variables?
939 static int has_any_defining_equality(__isl_keep isl_basic_set
*bset
)
946 n
= isl_basic_set_dim(bset
, isl_dim_set
);
947 for (i
= 0; i
< n
; ++i
) {
950 has
= isl_basic_set_has_defining_equality(bset
, isl_dim_set
, i
,
959 /* Add a new node to the graph representing the given space.
960 * "nvar" is the (possibly compressed) number of variables and
961 * may be smaller than then number of set variables in "space"
962 * if "compressed" is set.
963 * If "compressed" is set, then "hull" represents the constraints
964 * that were used to derive the compression, while "compress" and
965 * "decompress" map the original space to the compressed space and
967 * If "compressed" is not set, then "hull", "compress" and "decompress"
970 static int add_node(struct isl_sched_graph
*graph
, __isl_take isl_space
*space
,
971 int nvar
, int compressed
, __isl_take isl_set
*hull
,
972 __isl_take isl_multi_aff
*compress
,
973 __isl_take isl_multi_aff
*decompress
)
983 ctx
= isl_space_get_ctx(space
);
984 nparam
= isl_space_dim(space
, isl_dim_param
);
985 if (!ctx
->opt
->schedule_parametric
)
987 sched
= isl_mat_alloc(ctx
, 0, 1 + nparam
+ nvar
);
988 graph
->node
[graph
->n
].space
= space
;
989 graph
->node
[graph
->n
].nvar
= nvar
;
990 graph
->node
[graph
->n
].nparam
= nparam
;
991 graph
->node
[graph
->n
].sched
= sched
;
992 graph
->node
[graph
->n
].sched_map
= NULL
;
993 coincident
= isl_calloc_array(ctx
, int, graph
->max_row
);
994 graph
->node
[graph
->n
].coincident
= coincident
;
995 graph
->node
[graph
->n
].compressed
= compressed
;
996 graph
->node
[graph
->n
].hull
= hull
;
997 graph
->node
[graph
->n
].compress
= compress
;
998 graph
->node
[graph
->n
].decompress
= decompress
;
1001 if (!space
|| !sched
|| (graph
->max_row
&& !coincident
))
1003 if (compressed
&& (!hull
|| !compress
|| !decompress
))
1009 /* Add a new node to the graph representing the given set.
1011 * If any of the set variables is defined by an equality, then
1012 * we perform variable compression such that we can perform
1013 * the scheduling on the compressed domain.
1015 static int extract_node(__isl_take isl_set
*set
, void *user
)
1020 isl_basic_set
*hull
;
1023 isl_multi_aff
*compress
, *decompress
;
1024 struct isl_sched_graph
*graph
= user
;
1026 space
= isl_set_get_space(set
);
1027 hull
= isl_set_affine_hull(set
);
1028 hull
= isl_basic_set_remove_divs(hull
);
1029 nvar
= isl_space_dim(space
, isl_dim_set
);
1030 has_equality
= has_any_defining_equality(hull
);
1032 if (has_equality
< 0)
1034 if (!has_equality
) {
1035 isl_basic_set_free(hull
);
1036 return add_node(graph
, space
, nvar
, 0, NULL
, NULL
, NULL
);
1039 morph
= isl_basic_set_variable_compression(hull
, isl_dim_set
);
1040 nvar
= isl_morph_ran_dim(morph
, isl_dim_set
);
1041 compress
= isl_morph_get_var_multi_aff(morph
);
1042 morph
= isl_morph_inverse(morph
);
1043 decompress
= isl_morph_get_var_multi_aff(morph
);
1044 isl_morph_free(morph
);
1046 hull_set
= isl_set_from_basic_set(hull
);
1047 return add_node(graph
, space
, nvar
, 1, hull_set
, compress
, decompress
);
1049 isl_basic_set_free(hull
);
1050 isl_space_free(space
);
1054 struct isl_extract_edge_data
{
1055 enum isl_edge_type type
;
1056 struct isl_sched_graph
*graph
;
1059 /* Merge edge2 into edge1, freeing the contents of edge2.
1060 * "type" is the type of the schedule constraint from which edge2 was
1062 * Return 0 on success and -1 on failure.
1064 * edge1 and edge2 are assumed to have the same value for the map field.
1066 static int merge_edge(enum isl_edge_type type
, struct isl_sched_edge
*edge1
,
1067 struct isl_sched_edge
*edge2
)
1069 edge1
->validity
|= edge2
->validity
;
1070 edge1
->coincidence
|= edge2
->coincidence
;
1071 edge1
->proximity
|= edge2
->proximity
;
1072 edge1
->condition
|= edge2
->condition
;
1073 edge1
->conditional_validity
|= edge2
->conditional_validity
;
1074 isl_map_free(edge2
->map
);
1076 if (type
== isl_edge_condition
) {
1077 if (!edge1
->tagged_condition
)
1078 edge1
->tagged_condition
= edge2
->tagged_condition
;
1080 edge1
->tagged_condition
=
1081 isl_union_map_union(edge1
->tagged_condition
,
1082 edge2
->tagged_condition
);
1085 if (type
== isl_edge_conditional_validity
) {
1086 if (!edge1
->tagged_validity
)
1087 edge1
->tagged_validity
= edge2
->tagged_validity
;
1089 edge1
->tagged_validity
=
1090 isl_union_map_union(edge1
->tagged_validity
,
1091 edge2
->tagged_validity
);
1094 if (type
== isl_edge_condition
&& !edge1
->tagged_condition
)
1096 if (type
== isl_edge_conditional_validity
&& !edge1
->tagged_validity
)
1102 /* Insert dummy tags in domain and range of "map".
1104 * In particular, if "map" is of the form
1110 * [A -> dummy_tag] -> [B -> dummy_tag]
1112 * where the dummy_tags are identical and equal to any dummy tags
1113 * introduced by any other call to this function.
1115 static __isl_give isl_map
*insert_dummy_tags(__isl_take isl_map
*map
)
1121 isl_set
*domain
, *range
;
1123 ctx
= isl_map_get_ctx(map
);
1125 id
= isl_id_alloc(ctx
, NULL
, &dummy
);
1126 space
= isl_space_params(isl_map_get_space(map
));
1127 space
= isl_space_set_from_params(space
);
1128 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
1129 space
= isl_space_map_from_set(space
);
1131 domain
= isl_map_wrap(map
);
1132 range
= isl_map_wrap(isl_map_universe(space
));
1133 map
= isl_map_from_domain_and_range(domain
, range
);
1134 map
= isl_map_zip(map
);
1139 /* Given that at least one of "src" or "dst" is compressed, return
1140 * a map between the spaces of these nodes restricted to the affine
1141 * hull that was used in the compression.
1143 static __isl_give isl_map
*extract_hull(struct isl_sched_node
*src
,
1144 struct isl_sched_node
*dst
)
1148 if (src
->compressed
)
1149 dom
= isl_set_copy(src
->hull
);
1151 dom
= isl_set_universe(isl_space_copy(src
->space
));
1152 if (dst
->compressed
)
1153 ran
= isl_set_copy(dst
->hull
);
1155 ran
= isl_set_universe(isl_space_copy(dst
->space
));
1157 return isl_map_from_domain_and_range(dom
, ran
);
1160 /* Intersect the domains of the nested relations in domain and range
1161 * of "tagged" with "map".
1163 static __isl_give isl_map
*map_intersect_domains(__isl_take isl_map
*tagged
,
1164 __isl_keep isl_map
*map
)
1168 tagged
= isl_map_zip(tagged
);
1169 set
= isl_map_wrap(isl_map_copy(map
));
1170 tagged
= isl_map_intersect_domain(tagged
, set
);
1171 tagged
= isl_map_zip(tagged
);
1175 /* Add a new edge to the graph based on the given map
1176 * and add it to data->graph->edge_table[data->type].
1177 * If a dependence relation of a given type happens to be identical
1178 * to one of the dependence relations of a type that was added before,
1179 * then we don't create a new edge, but instead mark the original edge
1180 * as also representing a dependence of the current type.
1182 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1183 * may be specified as "tagged" dependence relations. That is, "map"
1184 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1185 * the dependence on iterations and a and b are tags.
1186 * edge->map is set to the relation containing the elements i -> j,
1187 * while edge->tagged_condition and edge->tagged_validity contain
1188 * the union of all the "map" relations
1189 * for which extract_edge is called that result in the same edge->map.
1191 * If the source or the destination node is compressed, then
1192 * intersect both "map" and "tagged" with the constraints that
1193 * were used to construct the compression.
1194 * This ensures that there are no schedule constraints defined
1195 * outside of these domains, while the scheduler no longer has
1196 * any control over those outside parts.
1198 static int extract_edge(__isl_take isl_map
*map
, void *user
)
1200 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1201 struct isl_extract_edge_data
*data
= user
;
1202 struct isl_sched_graph
*graph
= data
->graph
;
1203 struct isl_sched_node
*src
, *dst
;
1205 struct isl_sched_edge
*edge
;
1206 isl_map
*tagged
= NULL
;
1208 if (data
->type
== isl_edge_condition
||
1209 data
->type
== isl_edge_conditional_validity
) {
1210 if (isl_map_can_zip(map
)) {
1211 tagged
= isl_map_copy(map
);
1212 map
= isl_set_unwrap(isl_map_domain(isl_map_zip(map
)));
1214 tagged
= insert_dummy_tags(isl_map_copy(map
));
1218 dim
= isl_space_domain(isl_map_get_space(map
));
1219 src
= graph_find_node(ctx
, graph
, dim
);
1220 isl_space_free(dim
);
1221 dim
= isl_space_range(isl_map_get_space(map
));
1222 dst
= graph_find_node(ctx
, graph
, dim
);
1223 isl_space_free(dim
);
1227 isl_map_free(tagged
);
1231 if (src
->compressed
|| dst
->compressed
) {
1233 hull
= extract_hull(src
, dst
);
1235 tagged
= map_intersect_domains(tagged
, hull
);
1236 map
= isl_map_intersect(map
, hull
);
1239 graph
->edge
[graph
->n_edge
].src
= src
;
1240 graph
->edge
[graph
->n_edge
].dst
= dst
;
1241 graph
->edge
[graph
->n_edge
].map
= map
;
1242 graph
->edge
[graph
->n_edge
].validity
= 0;
1243 graph
->edge
[graph
->n_edge
].coincidence
= 0;
1244 graph
->edge
[graph
->n_edge
].proximity
= 0;
1245 graph
->edge
[graph
->n_edge
].condition
= 0;
1246 graph
->edge
[graph
->n_edge
].local
= 0;
1247 graph
->edge
[graph
->n_edge
].conditional_validity
= 0;
1248 graph
->edge
[graph
->n_edge
].tagged_condition
= NULL
;
1249 graph
->edge
[graph
->n_edge
].tagged_validity
= NULL
;
1250 if (data
->type
== isl_edge_validity
)
1251 graph
->edge
[graph
->n_edge
].validity
= 1;
1252 if (data
->type
== isl_edge_coincidence
)
1253 graph
->edge
[graph
->n_edge
].coincidence
= 1;
1254 if (data
->type
== isl_edge_proximity
)
1255 graph
->edge
[graph
->n_edge
].proximity
= 1;
1256 if (data
->type
== isl_edge_condition
) {
1257 graph
->edge
[graph
->n_edge
].condition
= 1;
1258 graph
->edge
[graph
->n_edge
].tagged_condition
=
1259 isl_union_map_from_map(tagged
);
1261 if (data
->type
== isl_edge_conditional_validity
) {
1262 graph
->edge
[graph
->n_edge
].conditional_validity
= 1;
1263 graph
->edge
[graph
->n_edge
].tagged_validity
=
1264 isl_union_map_from_map(tagged
);
1267 edge
= graph_find_matching_edge(graph
, &graph
->edge
[graph
->n_edge
]);
1272 if (edge
== &graph
->edge
[graph
->n_edge
])
1273 return graph_edge_table_add(ctx
, graph
, data
->type
,
1274 &graph
->edge
[graph
->n_edge
++]);
1276 if (merge_edge(data
->type
, edge
, &graph
->edge
[graph
->n_edge
]) < 0)
1279 return graph_edge_table_add(ctx
, graph
, data
->type
, edge
);
1282 /* Check whether there is any dependence from node[j] to node[i]
1283 * or from node[i] to node[j].
1285 static int node_follows_weak(int i
, int j
, void *user
)
1288 struct isl_sched_graph
*graph
= user
;
1290 f
= graph_has_any_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1293 return graph_has_any_edge(graph
, &graph
->node
[i
], &graph
->node
[j
]);
1296 /* Check whether there is a (conditional) validity dependence from node[j]
1297 * to node[i], forcing node[i] to follow node[j].
1299 static int node_follows_strong(int i
, int j
, void *user
)
1301 struct isl_sched_graph
*graph
= user
;
1303 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1306 /* Use Tarjan's algorithm for computing the strongly connected components
1307 * in the dependence graph (only validity edges).
1308 * If weak is set, we consider the graph to be undirected and
1309 * we effectively compute the (weakly) connected components.
1310 * Additionally, we also consider other edges when weak is set.
1312 static int detect_ccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
, int weak
)
1315 struct isl_tarjan_graph
*g
= NULL
;
1317 g
= isl_tarjan_graph_init(ctx
, graph
->n
,
1318 weak
? &node_follows_weak
: &node_follows_strong
, graph
);
1327 while (g
->order
[i
] != -1) {
1328 graph
->node
[g
->order
[i
]].scc
= graph
->scc
;
1336 isl_tarjan_graph_free(g
);
1341 /* Apply Tarjan's algorithm to detect the strongly connected components
1342 * in the dependence graph.
1344 static int detect_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1346 return detect_ccs(ctx
, graph
, 0);
1349 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1350 * in the dependence graph.
1352 static int detect_wccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1354 return detect_ccs(ctx
, graph
, 1);
1357 static int cmp_scc(const void *a
, const void *b
, void *data
)
1359 struct isl_sched_graph
*graph
= data
;
1363 return graph
->node
[*i1
].scc
- graph
->node
[*i2
].scc
;
1366 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1368 static int sort_sccs(struct isl_sched_graph
*graph
)
1370 return isl_sort(graph
->sorted
, graph
->n
, sizeof(int), &cmp_scc
, graph
);
1373 /* Given a dependence relation R from "node" to itself,
1374 * construct the set of coefficients of valid constraints for elements
1375 * in that dependence relation.
1376 * In particular, the result contains tuples of coefficients
1377 * c_0, c_n, c_x such that
1379 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1383 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1385 * We choose here to compute the dual of delta R.
1386 * Alternatively, we could have computed the dual of R, resulting
1387 * in a set of tuples c_0, c_n, c_x, c_y, and then
1388 * plugged in (c_0, c_n, c_x, -c_x).
1390 * If "node" has been compressed, then the dependence relation
1391 * is also compressed before the set of coefficients is computed.
1393 static __isl_give isl_basic_set
*intra_coefficients(
1394 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1395 __isl_take isl_map
*map
)
1399 isl_basic_set
*coef
;
1401 if (isl_map_to_basic_set_has(graph
->intra_hmap
, map
))
1402 return isl_map_to_basic_set_get(graph
->intra_hmap
, map
);
1404 key
= isl_map_copy(map
);
1405 if (node
->compressed
) {
1406 map
= isl_map_preimage_domain_multi_aff(map
,
1407 isl_multi_aff_copy(node
->decompress
));
1408 map
= isl_map_preimage_range_multi_aff(map
,
1409 isl_multi_aff_copy(node
->decompress
));
1411 delta
= isl_set_remove_divs(isl_map_deltas(map
));
1412 coef
= isl_set_coefficients(delta
);
1413 graph
->intra_hmap
= isl_map_to_basic_set_set(graph
->intra_hmap
, key
,
1414 isl_basic_set_copy(coef
));
1419 /* Given a dependence relation R, construct the set of coefficients
1420 * of valid constraints for elements in that dependence relation.
1421 * In particular, the result contains tuples of coefficients
1422 * c_0, c_n, c_x, c_y such that
1424 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1426 * If the source or destination nodes of "edge" have been compressed,
1427 * then the dependence relation is also compressed before
1428 * the set of coefficients is computed.
1430 static __isl_give isl_basic_set
*inter_coefficients(
1431 struct isl_sched_graph
*graph
, struct isl_sched_edge
*edge
,
1432 __isl_take isl_map
*map
)
1436 isl_basic_set
*coef
;
1438 if (isl_map_to_basic_set_has(graph
->inter_hmap
, map
))
1439 return isl_map_to_basic_set_get(graph
->inter_hmap
, map
);
1441 key
= isl_map_copy(map
);
1442 if (edge
->src
->compressed
)
1443 map
= isl_map_preimage_domain_multi_aff(map
,
1444 isl_multi_aff_copy(edge
->src
->decompress
));
1445 if (edge
->dst
->compressed
)
1446 map
= isl_map_preimage_range_multi_aff(map
,
1447 isl_multi_aff_copy(edge
->dst
->decompress
));
1448 set
= isl_map_wrap(isl_map_remove_divs(map
));
1449 coef
= isl_set_coefficients(set
);
1450 graph
->inter_hmap
= isl_map_to_basic_set_set(graph
->inter_hmap
, key
,
1451 isl_basic_set_copy(coef
));
1456 /* Add constraints to graph->lp that force validity for the given
1457 * dependence from a node i to itself.
1458 * That is, add constraints that enforce
1460 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1461 * = c_i_x (y - x) >= 0
1463 * for each (x,y) in R.
1464 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1465 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1466 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1467 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1469 * Actually, we do not construct constraints for the c_i_x themselves,
1470 * but for the coefficients of c_i_x written as a linear combination
1471 * of the columns in node->cmap.
1473 static int add_intra_validity_constraints(struct isl_sched_graph
*graph
,
1474 struct isl_sched_edge
*edge
)
1477 isl_map
*map
= isl_map_copy(edge
->map
);
1478 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1480 isl_dim_map
*dim_map
;
1481 isl_basic_set
*coef
;
1482 struct isl_sched_node
*node
= edge
->src
;
1484 coef
= intra_coefficients(graph
, node
, map
);
1486 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1488 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1489 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(node
->cmap
));
1493 total
= isl_basic_set_total_dim(graph
->lp
);
1494 dim_map
= isl_dim_map_alloc(ctx
, total
);
1495 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
1496 isl_space_dim(dim
, isl_dim_set
), 1,
1498 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
1499 isl_space_dim(dim
, isl_dim_set
), 1,
1501 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1502 coef
->n_eq
, coef
->n_ineq
);
1503 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1505 isl_space_free(dim
);
1509 isl_space_free(dim
);
1513 /* Add constraints to graph->lp that force validity for the given
1514 * dependence from node i to node j.
1515 * That is, add constraints that enforce
1517 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1519 * for each (x,y) in R.
1520 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1521 * of valid constraints for R and then plug in
1522 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
1523 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1524 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1525 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1527 * Actually, we do not construct constraints for the c_*_x themselves,
1528 * but for the coefficients of c_*_x written as a linear combination
1529 * of the columns in node->cmap.
1531 static int add_inter_validity_constraints(struct isl_sched_graph
*graph
,
1532 struct isl_sched_edge
*edge
)
1535 isl_map
*map
= isl_map_copy(edge
->map
);
1536 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1538 isl_dim_map
*dim_map
;
1539 isl_basic_set
*coef
;
1540 struct isl_sched_node
*src
= edge
->src
;
1541 struct isl_sched_node
*dst
= edge
->dst
;
1543 coef
= inter_coefficients(graph
, edge
, map
);
1545 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1547 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1548 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(src
->cmap
));
1549 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1550 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
,
1551 isl_mat_copy(dst
->cmap
));
1555 total
= isl_basic_set_total_dim(graph
->lp
);
1556 dim_map
= isl_dim_map_alloc(ctx
, total
);
1558 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, 1);
1559 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, -1);
1560 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, 1);
1561 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
1562 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1564 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
1565 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1568 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, -1);
1569 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, 1);
1570 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, -1);
1571 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
1572 isl_space_dim(dim
, isl_dim_set
), 1,
1574 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
1575 isl_space_dim(dim
, isl_dim_set
), 1,
1578 edge
->start
= graph
->lp
->n_ineq
;
1579 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1580 coef
->n_eq
, coef
->n_ineq
);
1581 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1585 isl_space_free(dim
);
1586 edge
->end
= graph
->lp
->n_ineq
;
1590 isl_space_free(dim
);
1594 /* Add constraints to graph->lp that bound the dependence distance for the given
1595 * dependence from a node i to itself.
1596 * If s = 1, we add the constraint
1598 * c_i_x (y - x) <= m_0 + m_n n
1602 * -c_i_x (y - x) + m_0 + m_n n >= 0
1604 * for each (x,y) in R.
1605 * If s = -1, we add the constraint
1607 * -c_i_x (y - x) <= m_0 + m_n n
1611 * c_i_x (y - x) + m_0 + m_n n >= 0
1613 * for each (x,y) in R.
1614 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1615 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1616 * with each coefficient (except m_0) represented as a pair of non-negative
1619 * Actually, we do not construct constraints for the c_i_x themselves,
1620 * but for the coefficients of c_i_x written as a linear combination
1621 * of the columns in node->cmap.
1624 * If "local" is set, then we add constraints
1626 * c_i_x (y - x) <= 0
1630 * -c_i_x (y - x) <= 0
1632 * instead, forcing the dependence distance to be (less than or) equal to 0.
1633 * That is, we plug in (0, 0, -s * c_i_x),
1634 * Note that dependences marked local are treated as validity constraints
1635 * by add_all_validity_constraints and therefore also have
1636 * their distances bounded by 0 from below.
1638 static int add_intra_proximity_constraints(struct isl_sched_graph
*graph
,
1639 struct isl_sched_edge
*edge
, int s
, int local
)
1643 isl_map
*map
= isl_map_copy(edge
->map
);
1644 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1646 isl_dim_map
*dim_map
;
1647 isl_basic_set
*coef
;
1648 struct isl_sched_node
*node
= edge
->src
;
1650 coef
= intra_coefficients(graph
, node
, map
);
1652 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1654 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1655 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(node
->cmap
));
1659 nparam
= isl_space_dim(node
->space
, isl_dim_param
);
1660 total
= isl_basic_set_total_dim(graph
->lp
);
1661 dim_map
= isl_dim_map_alloc(ctx
, total
);
1664 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1665 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1666 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1668 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
1669 isl_space_dim(dim
, isl_dim_set
), 1,
1671 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
1672 isl_space_dim(dim
, isl_dim_set
), 1,
1674 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1675 coef
->n_eq
, coef
->n_ineq
);
1676 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1678 isl_space_free(dim
);
1682 isl_space_free(dim
);
1686 /* Add constraints to graph->lp that bound the dependence distance for the given
1687 * dependence from node i to node j.
1688 * If s = 1, we add the constraint
1690 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1695 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1698 * for each (x,y) in R.
1699 * If s = -1, we add the constraint
1701 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1706 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1709 * for each (x,y) in R.
1710 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1711 * of valid constraints for R and then plug in
1712 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1714 * with each coefficient (except m_0, c_j_0 and c_i_0)
1715 * represented as a pair of non-negative coefficients.
1717 * Actually, we do not construct constraints for the c_*_x themselves,
1718 * but for the coefficients of c_*_x written as a linear combination
1719 * of the columns in node->cmap.
1722 * If "local" is set, then we add constraints
1724 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1728 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
1730 * instead, forcing the dependence distance to be (less than or) equal to 0.
1731 * That is, we plug in
1732 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
1733 * Note that dependences marked local are treated as validity constraints
1734 * by add_all_validity_constraints and therefore also have
1735 * their distances bounded by 0 from below.
1737 static int add_inter_proximity_constraints(struct isl_sched_graph
*graph
,
1738 struct isl_sched_edge
*edge
, int s
, int local
)
1742 isl_map
*map
= isl_map_copy(edge
->map
);
1743 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1745 isl_dim_map
*dim_map
;
1746 isl_basic_set
*coef
;
1747 struct isl_sched_node
*src
= edge
->src
;
1748 struct isl_sched_node
*dst
= edge
->dst
;
1750 coef
= inter_coefficients(graph
, edge
, map
);
1752 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1754 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1755 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(src
->cmap
));
1756 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1757 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
,
1758 isl_mat_copy(dst
->cmap
));
1762 nparam
= isl_space_dim(src
->space
, isl_dim_param
);
1763 total
= isl_basic_set_total_dim(graph
->lp
);
1764 dim_map
= isl_dim_map_alloc(ctx
, total
);
1767 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1768 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1769 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1772 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, -s
);
1773 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, s
);
1774 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, -s
);
1775 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
1776 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1778 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
1779 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1782 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, s
);
1783 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, -s
);
1784 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, s
);
1785 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
1786 isl_space_dim(dim
, isl_dim_set
), 1,
1788 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
1789 isl_space_dim(dim
, isl_dim_set
), 1,
1792 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1793 coef
->n_eq
, coef
->n_ineq
);
1794 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1796 isl_space_free(dim
);
1800 isl_space_free(dim
);
1804 /* Add all validity constraints to graph->lp.
1806 * An edge that is forced to be local needs to have its dependence
1807 * distances equal to zero. We take care of bounding them by 0 from below
1808 * here. add_all_proximity_constraints takes care of bounding them by 0
1811 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1812 * Otherwise, we ignore them.
1814 static int add_all_validity_constraints(struct isl_sched_graph
*graph
,
1815 int use_coincidence
)
1819 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1820 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1823 local
= edge
->local
|| (edge
->coincidence
&& use_coincidence
);
1824 if (!edge
->validity
&& !local
)
1826 if (edge
->src
!= edge
->dst
)
1828 if (add_intra_validity_constraints(graph
, edge
) < 0)
1832 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1833 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1836 local
= edge
->local
|| (edge
->coincidence
&& use_coincidence
);
1837 if (!edge
->validity
&& !local
)
1839 if (edge
->src
== edge
->dst
)
1841 if (add_inter_validity_constraints(graph
, edge
) < 0)
1848 /* Add constraints to graph->lp that bound the dependence distance
1849 * for all dependence relations.
1850 * If a given proximity dependence is identical to a validity
1851 * dependence, then the dependence distance is already bounded
1852 * from below (by zero), so we only need to bound the distance
1853 * from above. (This includes the case of "local" dependences
1854 * which are treated as validity dependence by add_all_validity_constraints.)
1855 * Otherwise, we need to bound the distance both from above and from below.
1857 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1858 * Otherwise, we ignore them.
1860 static int add_all_proximity_constraints(struct isl_sched_graph
*graph
,
1861 int use_coincidence
)
1865 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1866 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1869 local
= edge
->local
|| (edge
->coincidence
&& use_coincidence
);
1870 if (!edge
->proximity
&& !local
)
1872 if (edge
->src
== edge
->dst
&&
1873 add_intra_proximity_constraints(graph
, edge
, 1, local
) < 0)
1875 if (edge
->src
!= edge
->dst
&&
1876 add_inter_proximity_constraints(graph
, edge
, 1, local
) < 0)
1878 if (edge
->validity
|| local
)
1880 if (edge
->src
== edge
->dst
&&
1881 add_intra_proximity_constraints(graph
, edge
, -1, 0) < 0)
1883 if (edge
->src
!= edge
->dst
&&
1884 add_inter_proximity_constraints(graph
, edge
, -1, 0) < 0)
1891 /* Compute a basis for the rows in the linear part of the schedule
1892 * and extend this basis to a full basis. The remaining rows
1893 * can then be used to force linear independence from the rows
1896 * In particular, given the schedule rows S, we compute
1901 * with H the Hermite normal form of S. That is, all but the
1902 * first rank columns of H are zero and so each row in S is
1903 * a linear combination of the first rank rows of Q.
1904 * The matrix Q is then transposed because we will write the
1905 * coefficients of the next schedule row as a column vector s
1906 * and express this s as a linear combination s = Q c of the
1908 * Similarly, the matrix U is transposed such that we can
1909 * compute the coefficients c = U s from a schedule row s.
1911 static int node_update_cmap(struct isl_sched_node
*node
)
1914 int n_row
= isl_mat_rows(node
->sched
);
1916 H
= isl_mat_sub_alloc(node
->sched
, 0, n_row
,
1917 1 + node
->nparam
, node
->nvar
);
1919 H
= isl_mat_left_hermite(H
, 0, &U
, &Q
);
1920 isl_mat_free(node
->cmap
);
1921 isl_mat_free(node
->cinv
);
1922 node
->cmap
= isl_mat_transpose(Q
);
1923 node
->cinv
= isl_mat_transpose(U
);
1924 node
->rank
= isl_mat_initial_non_zero_cols(H
);
1927 if (!node
->cmap
|| !node
->cinv
|| node
->rank
< 0)
1932 /* How many times should we count the constraints in "edge"?
1934 * If carry is set, then we are counting the number of
1935 * (validity or conditional validity) constraints that will be added
1936 * in setup_carry_lp and we count each edge exactly once.
1938 * Otherwise, we count as follows
1939 * validity -> 1 (>= 0)
1940 * validity+proximity -> 2 (>= 0 and upper bound)
1941 * proximity -> 2 (lower and upper bound)
1942 * local(+any) -> 2 (>= 0 and <= 0)
1944 * If an edge is only marked conditional_validity then it counts
1945 * as zero since it is only checked afterwards.
1947 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1948 * Otherwise, we ignore them.
1950 static int edge_multiplicity(struct isl_sched_edge
*edge
, int carry
,
1951 int use_coincidence
)
1953 if (carry
&& !edge
->validity
&& !edge
->conditional_validity
)
1957 if (edge
->proximity
|| edge
->local
)
1959 if (use_coincidence
&& edge
->coincidence
)
1966 /* Count the number of equality and inequality constraints
1967 * that will be added for the given map.
1969 * "use_coincidence" is set if we should take into account coincidence edges.
1971 static int count_map_constraints(struct isl_sched_graph
*graph
,
1972 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
,
1973 int *n_eq
, int *n_ineq
, int carry
, int use_coincidence
)
1975 isl_basic_set
*coef
;
1976 int f
= edge_multiplicity(edge
, carry
, use_coincidence
);
1983 if (edge
->src
== edge
->dst
)
1984 coef
= intra_coefficients(graph
, edge
->src
, map
);
1986 coef
= inter_coefficients(graph
, edge
, map
);
1989 *n_eq
+= f
* coef
->n_eq
;
1990 *n_ineq
+= f
* coef
->n_ineq
;
1991 isl_basic_set_free(coef
);
1996 /* Count the number of equality and inequality constraints
1997 * that will be added to the main lp problem.
1998 * We count as follows
1999 * validity -> 1 (>= 0)
2000 * validity+proximity -> 2 (>= 0 and upper bound)
2001 * proximity -> 2 (lower and upper bound)
2002 * local(+any) -> 2 (>= 0 and <= 0)
2004 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2005 * Otherwise, we ignore them.
2007 static int count_constraints(struct isl_sched_graph
*graph
,
2008 int *n_eq
, int *n_ineq
, int use_coincidence
)
2012 *n_eq
= *n_ineq
= 0;
2013 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2014 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2015 isl_map
*map
= isl_map_copy(edge
->map
);
2017 if (count_map_constraints(graph
, edge
, map
, n_eq
, n_ineq
,
2018 0, use_coincidence
) < 0)
2025 /* Count the number of constraints that will be added by
2026 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
2029 * In practice, add_bound_coefficient_constraints only adds inequalities.
2031 static int count_bound_coefficient_constraints(isl_ctx
*ctx
,
2032 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
2036 if (ctx
->opt
->schedule_max_coefficient
== -1)
2039 for (i
= 0; i
< graph
->n
; ++i
)
2040 *n_ineq
+= 2 * graph
->node
[i
].nparam
+ 2 * graph
->node
[i
].nvar
;
2045 /* Add constraints that bound the values of the variable and parameter
2046 * coefficients of the schedule.
2048 * The maximal value of the coefficients is defined by the option
2049 * 'schedule_max_coefficient'.
2051 static int add_bound_coefficient_constraints(isl_ctx
*ctx
,
2052 struct isl_sched_graph
*graph
)
2055 int max_coefficient
;
2058 max_coefficient
= ctx
->opt
->schedule_max_coefficient
;
2060 if (max_coefficient
== -1)
2063 total
= isl_basic_set_total_dim(graph
->lp
);
2065 for (i
= 0; i
< graph
->n
; ++i
) {
2066 struct isl_sched_node
*node
= &graph
->node
[i
];
2067 for (j
= 0; j
< 2 * node
->nparam
+ 2 * node
->nvar
; ++j
) {
2069 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2072 dim
= 1 + node
->start
+ 1 + j
;
2073 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2074 isl_int_set_si(graph
->lp
->ineq
[k
][dim
], -1);
2075 isl_int_set_si(graph
->lp
->ineq
[k
][0], max_coefficient
);
2082 /* Construct an ILP problem for finding schedule coefficients
2083 * that result in non-negative, but small dependence distances
2084 * over all dependences.
2085 * In particular, the dependence distances over proximity edges
2086 * are bounded by m_0 + m_n n and we compute schedule coefficients
2087 * with small values (preferably zero) of m_n and m_0.
2089 * All variables of the ILP are non-negative. The actual coefficients
2090 * may be negative, so each coefficient is represented as the difference
2091 * of two non-negative variables. The negative part always appears
2092 * immediately before the positive part.
2093 * Other than that, the variables have the following order
2095 * - sum of positive and negative parts of m_n coefficients
2097 * - sum of positive and negative parts of all c_n coefficients
2098 * (unconstrained when computing non-parametric schedules)
2099 * - sum of positive and negative parts of all c_x coefficients
2100 * - positive and negative parts of m_n coefficients
2103 * - positive and negative parts of c_i_n (if parametric)
2104 * - positive and negative parts of c_i_x
2106 * The c_i_x are not represented directly, but through the columns of
2107 * node->cmap. That is, the computed values are for variable t_i_x
2108 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
2110 * The constraints are those from the edges plus two or three equalities
2111 * to express the sums.
2113 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2114 * Otherwise, we ignore them.
2116 static int setup_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
2117 int use_coincidence
)
2127 int max_constant_term
;
2129 max_constant_term
= ctx
->opt
->schedule_max_constant_term
;
2131 parametric
= ctx
->opt
->schedule_parametric
;
2132 nparam
= isl_space_dim(graph
->node
[0].space
, isl_dim_param
);
2134 total
= param_pos
+ 2 * nparam
;
2135 for (i
= 0; i
< graph
->n
; ++i
) {
2136 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
2137 if (node_update_cmap(node
) < 0)
2139 node
->start
= total
;
2140 total
+= 1 + 2 * (node
->nparam
+ node
->nvar
);
2143 if (count_constraints(graph
, &n_eq
, &n_ineq
, use_coincidence
) < 0)
2145 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2148 dim
= isl_space_set_alloc(ctx
, 0, total
);
2149 isl_basic_set_free(graph
->lp
);
2150 n_eq
+= 2 + parametric
;
2151 if (max_constant_term
!= -1)
2154 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
2156 k
= isl_basic_set_alloc_equality(graph
->lp
);
2159 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2160 isl_int_set_si(graph
->lp
->eq
[k
][1], -1);
2161 for (i
= 0; i
< 2 * nparam
; ++i
)
2162 isl_int_set_si(graph
->lp
->eq
[k
][1 + param_pos
+ i
], 1);
2165 k
= isl_basic_set_alloc_equality(graph
->lp
);
2168 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2169 isl_int_set_si(graph
->lp
->eq
[k
][3], -1);
2170 for (i
= 0; i
< graph
->n
; ++i
) {
2171 int pos
= 1 + graph
->node
[i
].start
+ 1;
2173 for (j
= 0; j
< 2 * graph
->node
[i
].nparam
; ++j
)
2174 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2178 k
= isl_basic_set_alloc_equality(graph
->lp
);
2181 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2182 isl_int_set_si(graph
->lp
->eq
[k
][4], -1);
2183 for (i
= 0; i
< graph
->n
; ++i
) {
2184 struct isl_sched_node
*node
= &graph
->node
[i
];
2185 int pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
2187 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
2188 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2191 if (max_constant_term
!= -1)
2192 for (i
= 0; i
< graph
->n
; ++i
) {
2193 struct isl_sched_node
*node
= &graph
->node
[i
];
2194 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2197 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2198 isl_int_set_si(graph
->lp
->ineq
[k
][1 + node
->start
], -1);
2199 isl_int_set_si(graph
->lp
->ineq
[k
][0], max_constant_term
);
2202 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
2204 if (add_all_validity_constraints(graph
, use_coincidence
) < 0)
2206 if (add_all_proximity_constraints(graph
, use_coincidence
) < 0)
2212 /* Analyze the conflicting constraint found by
2213 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2214 * constraint of one of the edges between distinct nodes, living, moreover
2215 * in distinct SCCs, then record the source and sink SCC as this may
2216 * be a good place to cut between SCCs.
2218 static int check_conflict(int con
, void *user
)
2221 struct isl_sched_graph
*graph
= user
;
2223 if (graph
->src_scc
>= 0)
2226 con
-= graph
->lp
->n_eq
;
2228 if (con
>= graph
->lp
->n_ineq
)
2231 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2232 if (!graph
->edge
[i
].validity
)
2234 if (graph
->edge
[i
].src
== graph
->edge
[i
].dst
)
2236 if (graph
->edge
[i
].src
->scc
== graph
->edge
[i
].dst
->scc
)
2238 if (graph
->edge
[i
].start
> con
)
2240 if (graph
->edge
[i
].end
<= con
)
2242 graph
->src_scc
= graph
->edge
[i
].src
->scc
;
2243 graph
->dst_scc
= graph
->edge
[i
].dst
->scc
;
2249 /* Check whether the next schedule row of the given node needs to be
2250 * non-trivial. Lower-dimensional domains may have some trivial rows,
2251 * but as soon as the number of remaining required non-trivial rows
2252 * is as large as the number or remaining rows to be computed,
2253 * all remaining rows need to be non-trivial.
2255 static int needs_row(struct isl_sched_graph
*graph
, struct isl_sched_node
*node
)
2257 return node
->nvar
- node
->rank
>= graph
->maxvar
- graph
->n_row
;
2260 /* Solve the ILP problem constructed in setup_lp.
2261 * For each node such that all the remaining rows of its schedule
2262 * need to be non-trivial, we construct a non-triviality region.
2263 * This region imposes that the next row is independent of previous rows.
2264 * In particular the coefficients c_i_x are represented by t_i_x
2265 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
2266 * its first columns span the rows of the previously computed part
2267 * of the schedule. The non-triviality region enforces that at least
2268 * one of the remaining components of t_i_x is non-zero, i.e.,
2269 * that the new schedule row depends on at least one of the remaining
2272 static __isl_give isl_vec
*solve_lp(struct isl_sched_graph
*graph
)
2278 for (i
= 0; i
< graph
->n
; ++i
) {
2279 struct isl_sched_node
*node
= &graph
->node
[i
];
2280 int skip
= node
->rank
;
2281 graph
->region
[i
].pos
= node
->start
+ 1 + 2*(node
->nparam
+skip
);
2282 if (needs_row(graph
, node
))
2283 graph
->region
[i
].len
= 2 * (node
->nvar
- skip
);
2285 graph
->region
[i
].len
= 0;
2287 lp
= isl_basic_set_copy(graph
->lp
);
2288 sol
= isl_tab_basic_set_non_trivial_lexmin(lp
, 2, graph
->n
,
2289 graph
->region
, &check_conflict
, graph
);
2293 /* Update the schedules of all nodes based on the given solution
2294 * of the LP problem.
2295 * The new row is added to the current band.
2296 * All possibly negative coefficients are encoded as a difference
2297 * of two non-negative variables, so we need to perform the subtraction
2298 * here. Moreover, if use_cmap is set, then the solution does
2299 * not refer to the actual coefficients c_i_x, but instead to variables
2300 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2301 * In this case, we then also need to perform this multiplication
2302 * to obtain the values of c_i_x.
2304 * If coincident is set, then the caller guarantees that the new
2305 * row satisfies the coincidence constraints.
2307 static int update_schedule(struct isl_sched_graph
*graph
,
2308 __isl_take isl_vec
*sol
, int use_cmap
, int coincident
)
2311 isl_vec
*csol
= NULL
;
2316 isl_die(sol
->ctx
, isl_error_internal
,
2317 "no solution found", goto error
);
2318 if (graph
->n_total_row
>= graph
->max_row
)
2319 isl_die(sol
->ctx
, isl_error_internal
,
2320 "too many schedule rows", goto error
);
2322 for (i
= 0; i
< graph
->n
; ++i
) {
2323 struct isl_sched_node
*node
= &graph
->node
[i
];
2324 int pos
= node
->start
;
2325 int row
= isl_mat_rows(node
->sched
);
2328 csol
= isl_vec_alloc(sol
->ctx
, node
->nvar
);
2332 isl_map_free(node
->sched_map
);
2333 node
->sched_map
= NULL
;
2334 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
2337 node
->sched
= isl_mat_set_element(node
->sched
, row
, 0,
2339 for (j
= 0; j
< node
->nparam
+ node
->nvar
; ++j
)
2340 isl_int_sub(sol
->el
[1 + pos
+ 1 + 2 * j
+ 1],
2341 sol
->el
[1 + pos
+ 1 + 2 * j
+ 1],
2342 sol
->el
[1 + pos
+ 1 + 2 * j
]);
2343 for (j
= 0; j
< node
->nparam
; ++j
)
2344 node
->sched
= isl_mat_set_element(node
->sched
,
2345 row
, 1 + j
, sol
->el
[1+pos
+1+2*j
+1]);
2346 for (j
= 0; j
< node
->nvar
; ++j
)
2347 isl_int_set(csol
->el
[j
],
2348 sol
->el
[1+pos
+1+2*(node
->nparam
+j
)+1]);
2350 csol
= isl_mat_vec_product(isl_mat_copy(node
->cmap
),
2354 for (j
= 0; j
< node
->nvar
; ++j
)
2355 node
->sched
= isl_mat_set_element(node
->sched
,
2356 row
, 1 + node
->nparam
+ j
, csol
->el
[j
]);
2357 node
->coincident
[graph
->n_total_row
] = coincident
;
2363 graph
->n_total_row
++;
2372 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2373 * and return this isl_aff.
2375 static __isl_give isl_aff
*extract_schedule_row(__isl_take isl_local_space
*ls
,
2376 struct isl_sched_node
*node
, int row
)
2384 aff
= isl_aff_zero_on_domain(ls
);
2385 isl_mat_get_element(node
->sched
, row
, 0, &v
);
2386 aff
= isl_aff_set_constant(aff
, v
);
2387 for (j
= 0; j
< node
->nparam
; ++j
) {
2388 isl_mat_get_element(node
->sched
, row
, 1 + j
, &v
);
2389 aff
= isl_aff_set_coefficient(aff
, isl_dim_param
, j
, v
);
2391 for (j
= 0; j
< node
->nvar
; ++j
) {
2392 isl_mat_get_element(node
->sched
, row
, 1 + node
->nparam
+ j
, &v
);
2393 aff
= isl_aff_set_coefficient(aff
, isl_dim_in
, j
, v
);
2401 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
2402 * and return this multi_aff.
2404 * The result is defined over the uncompressed node domain.
2406 static __isl_give isl_multi_aff
*node_extract_partial_schedule_multi_aff(
2407 struct isl_sched_node
*node
, int first
, int n
)
2411 isl_local_space
*ls
;
2416 nrow
= isl_mat_rows(node
->sched
);
2417 if (node
->compressed
)
2418 space
= isl_multi_aff_get_domain_space(node
->decompress
);
2420 space
= isl_space_copy(node
->space
);
2421 ls
= isl_local_space_from_space(isl_space_copy(space
));
2422 space
= isl_space_from_domain(space
);
2423 space
= isl_space_add_dims(space
, isl_dim_out
, n
);
2424 ma
= isl_multi_aff_zero(space
);
2426 for (i
= first
; i
< first
+ n
; ++i
) {
2427 aff
= extract_schedule_row(isl_local_space_copy(ls
), node
, i
);
2428 ma
= isl_multi_aff_set_aff(ma
, i
- first
, aff
);
2431 isl_local_space_free(ls
);
2433 if (node
->compressed
)
2434 ma
= isl_multi_aff_pullback_multi_aff(ma
,
2435 isl_multi_aff_copy(node
->compress
));
2440 /* Convert node->sched into a multi_aff and return this multi_aff.
2442 * The result is defined over the uncompressed node domain.
2444 static __isl_give isl_multi_aff
*node_extract_schedule_multi_aff(
2445 struct isl_sched_node
*node
)
2449 nrow
= isl_mat_rows(node
->sched
);
2450 return node_extract_partial_schedule_multi_aff(node
, 0, nrow
);
2453 /* Convert node->sched into a map and return this map.
2455 * The result is cached in node->sched_map, which needs to be released
2456 * whenever node->sched is updated.
2457 * It is defined over the uncompressed node domain.
2459 static __isl_give isl_map
*node_extract_schedule(struct isl_sched_node
*node
)
2461 if (!node
->sched_map
) {
2464 ma
= node_extract_schedule_multi_aff(node
);
2465 node
->sched_map
= isl_map_from_multi_aff(ma
);
2468 return isl_map_copy(node
->sched_map
);
2471 /* Construct a map that can be used to update a dependence relation
2472 * based on the current schedule.
2473 * That is, construct a map expressing that source and sink
2474 * are executed within the same iteration of the current schedule.
2475 * This map can then be intersected with the dependence relation.
2476 * This is not the most efficient way, but this shouldn't be a critical
2479 static __isl_give isl_map
*specializer(struct isl_sched_node
*src
,
2480 struct isl_sched_node
*dst
)
2482 isl_map
*src_sched
, *dst_sched
;
2484 src_sched
= node_extract_schedule(src
);
2485 dst_sched
= node_extract_schedule(dst
);
2486 return isl_map_apply_range(src_sched
, isl_map_reverse(dst_sched
));
2489 /* Intersect the domains of the nested relations in domain and range
2490 * of "umap" with "map".
2492 static __isl_give isl_union_map
*intersect_domains(
2493 __isl_take isl_union_map
*umap
, __isl_keep isl_map
*map
)
2495 isl_union_set
*uset
;
2497 umap
= isl_union_map_zip(umap
);
2498 uset
= isl_union_set_from_set(isl_map_wrap(isl_map_copy(map
)));
2499 umap
= isl_union_map_intersect_domain(umap
, uset
);
2500 umap
= isl_union_map_zip(umap
);
2504 /* Update the dependence relation of the given edge based
2505 * on the current schedule.
2506 * If the dependence is carried completely by the current schedule, then
2507 * it is removed from the edge_tables. It is kept in the list of edges
2508 * as otherwise all edge_tables would have to be recomputed.
2510 static int update_edge(struct isl_sched_graph
*graph
,
2511 struct isl_sched_edge
*edge
)
2516 id
= specializer(edge
->src
, edge
->dst
);
2517 edge
->map
= isl_map_intersect(edge
->map
, isl_map_copy(id
));
2521 if (edge
->tagged_condition
) {
2522 edge
->tagged_condition
=
2523 intersect_domains(edge
->tagged_condition
, id
);
2524 if (!edge
->tagged_condition
)
2527 if (edge
->tagged_validity
) {
2528 edge
->tagged_validity
=
2529 intersect_domains(edge
->tagged_validity
, id
);
2530 if (!edge
->tagged_validity
)
2534 empty
= isl_map_plain_is_empty(edge
->map
);
2538 graph_remove_edge(graph
, edge
);
2547 /* Does the domain of "umap" intersect "uset"?
2549 static int domain_intersects(__isl_keep isl_union_map
*umap
,
2550 __isl_keep isl_union_set
*uset
)
2554 umap
= isl_union_map_copy(umap
);
2555 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(uset
));
2556 empty
= isl_union_map_is_empty(umap
);
2557 isl_union_map_free(umap
);
2559 return empty
< 0 ? -1 : !empty
;
2562 /* Does the range of "umap" intersect "uset"?
2564 static int range_intersects(__isl_keep isl_union_map
*umap
,
2565 __isl_keep isl_union_set
*uset
)
2569 umap
= isl_union_map_copy(umap
);
2570 umap
= isl_union_map_intersect_range(umap
, isl_union_set_copy(uset
));
2571 empty
= isl_union_map_is_empty(umap
);
2572 isl_union_map_free(umap
);
2574 return empty
< 0 ? -1 : !empty
;
2577 /* Are the condition dependences of "edge" local with respect to
2578 * the current schedule?
2580 * That is, are domain and range of the condition dependences mapped
2581 * to the same point?
2583 * In other words, is the condition false?
2585 static int is_condition_false(struct isl_sched_edge
*edge
)
2587 isl_union_map
*umap
;
2588 isl_map
*map
, *sched
, *test
;
2591 empty
= isl_union_map_is_empty(edge
->tagged_condition
);
2592 if (empty
< 0 || empty
)
2595 umap
= isl_union_map_copy(edge
->tagged_condition
);
2596 umap
= isl_union_map_zip(umap
);
2597 umap
= isl_union_set_unwrap(isl_union_map_domain(umap
));
2598 map
= isl_map_from_union_map(umap
);
2600 sched
= node_extract_schedule(edge
->src
);
2601 map
= isl_map_apply_domain(map
, sched
);
2602 sched
= node_extract_schedule(edge
->dst
);
2603 map
= isl_map_apply_range(map
, sched
);
2605 test
= isl_map_identity(isl_map_get_space(map
));
2606 local
= isl_map_is_subset(map
, test
);
2613 /* For each conditional validity constraint that is adjacent
2614 * to a condition with domain in condition_source or range in condition_sink,
2615 * turn it into an unconditional validity constraint.
2617 static int unconditionalize_adjacent_validity(struct isl_sched_graph
*graph
,
2618 __isl_take isl_union_set
*condition_source
,
2619 __isl_take isl_union_set
*condition_sink
)
2623 condition_source
= isl_union_set_coalesce(condition_source
);
2624 condition_sink
= isl_union_set_coalesce(condition_sink
);
2626 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2628 isl_union_map
*validity
;
2630 if (!graph
->edge
[i
].conditional_validity
)
2632 if (graph
->edge
[i
].validity
)
2635 validity
= graph
->edge
[i
].tagged_validity
;
2636 adjacent
= domain_intersects(validity
, condition_sink
);
2637 if (adjacent
>= 0 && !adjacent
)
2638 adjacent
= range_intersects(validity
, condition_source
);
2644 graph
->edge
[i
].validity
= 1;
2647 isl_union_set_free(condition_source
);
2648 isl_union_set_free(condition_sink
);
2651 isl_union_set_free(condition_source
);
2652 isl_union_set_free(condition_sink
);
2656 /* Update the dependence relations of all edges based on the current schedule
2657 * and enforce conditional validity constraints that are adjacent
2658 * to satisfied condition constraints.
2660 * First check if any of the condition constraints are satisfied
2661 * (i.e., not local to the outer schedule) and keep track of
2662 * their domain and range.
2663 * Then update all dependence relations (which removes the non-local
2665 * Finally, if any condition constraints turned out to be satisfied,
2666 * then turn all adjacent conditional validity constraints into
2667 * unconditional validity constraints.
2669 static int update_edges(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2673 isl_union_set
*source
, *sink
;
2675 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
2676 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
2677 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2679 isl_union_set
*uset
;
2680 isl_union_map
*umap
;
2682 if (!graph
->edge
[i
].condition
)
2684 if (graph
->edge
[i
].local
)
2686 local
= is_condition_false(&graph
->edge
[i
]);
2694 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
2695 uset
= isl_union_map_domain(umap
);
2696 source
= isl_union_set_union(source
, uset
);
2698 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
2699 uset
= isl_union_map_range(umap
);
2700 sink
= isl_union_set_union(sink
, uset
);
2703 for (i
= graph
->n_edge
- 1; i
>= 0; --i
) {
2704 if (update_edge(graph
, &graph
->edge
[i
]) < 0)
2709 return unconditionalize_adjacent_validity(graph
, source
, sink
);
2711 isl_union_set_free(source
);
2712 isl_union_set_free(sink
);
2715 isl_union_set_free(source
);
2716 isl_union_set_free(sink
);
2720 static void next_band(struct isl_sched_graph
*graph
)
2722 graph
->band_start
= graph
->n_total_row
;
2725 /* Return the union of the universe domains of the nodes in "graph"
2726 * that satisfy "pred".
2728 static __isl_give isl_union_set
*isl_sched_graph_domain(isl_ctx
*ctx
,
2729 struct isl_sched_graph
*graph
,
2730 int (*pred
)(struct isl_sched_node
*node
, int data
), int data
)
2736 for (i
= 0; i
< graph
->n
; ++i
)
2737 if (pred(&graph
->node
[i
], data
))
2741 isl_die(ctx
, isl_error_internal
,
2742 "empty component", return NULL
);
2744 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
2745 dom
= isl_union_set_from_set(set
);
2747 for (i
= i
+ 1; i
< graph
->n
; ++i
) {
2748 if (!pred(&graph
->node
[i
], data
))
2750 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
2751 dom
= isl_union_set_union(dom
, isl_union_set_from_set(set
));
2757 /* Return a list of unions of universe domains, where each element
2758 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
2760 static __isl_give isl_union_set_list
*extract_sccs(isl_ctx
*ctx
,
2761 struct isl_sched_graph
*graph
)
2764 isl_union_set_list
*filters
;
2766 filters
= isl_union_set_list_alloc(ctx
, graph
->scc
);
2767 for (i
= 0; i
< graph
->scc
; ++i
) {
2770 dom
= isl_sched_graph_domain(ctx
, graph
, &node_scc_exactly
, i
);
2771 filters
= isl_union_set_list_add(filters
, dom
);
2777 /* Return a list of two unions of universe domains, one for the SCCs up
2778 * to and including graph->src_scc and another for the other SCCS.
2780 static __isl_give isl_union_set_list
*extract_split(isl_ctx
*ctx
,
2781 struct isl_sched_graph
*graph
)
2784 isl_union_set_list
*filters
;
2786 filters
= isl_union_set_list_alloc(ctx
, 2);
2787 dom
= isl_sched_graph_domain(ctx
, graph
,
2788 &node_scc_at_most
, graph
->src_scc
);
2789 filters
= isl_union_set_list_add(filters
, dom
);
2790 dom
= isl_sched_graph_domain(ctx
, graph
,
2791 &node_scc_at_least
, graph
->src_scc
+ 1);
2792 filters
= isl_union_set_list_add(filters
, dom
);
2797 /* Copy nodes that satisfy node_pred from the src dependence graph
2798 * to the dst dependence graph.
2800 static int copy_nodes(struct isl_sched_graph
*dst
, struct isl_sched_graph
*src
,
2801 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
2806 for (i
= 0; i
< src
->n
; ++i
) {
2809 if (!node_pred(&src
->node
[i
], data
))
2813 dst
->node
[j
].space
= isl_space_copy(src
->node
[i
].space
);
2814 dst
->node
[j
].compressed
= src
->node
[i
].compressed
;
2815 dst
->node
[j
].hull
= isl_set_copy(src
->node
[i
].hull
);
2816 dst
->node
[j
].compress
=
2817 isl_multi_aff_copy(src
->node
[i
].compress
);
2818 dst
->node
[j
].decompress
=
2819 isl_multi_aff_copy(src
->node
[i
].decompress
);
2820 dst
->node
[j
].nvar
= src
->node
[i
].nvar
;
2821 dst
->node
[j
].nparam
= src
->node
[i
].nparam
;
2822 dst
->node
[j
].sched
= isl_mat_copy(src
->node
[i
].sched
);
2823 dst
->node
[j
].sched_map
= isl_map_copy(src
->node
[i
].sched_map
);
2824 dst
->node
[j
].coincident
= src
->node
[i
].coincident
;
2827 if (!dst
->node
[j
].space
|| !dst
->node
[j
].sched
)
2829 if (dst
->node
[j
].compressed
&&
2830 (!dst
->node
[j
].hull
|| !dst
->node
[j
].compress
||
2831 !dst
->node
[j
].decompress
))
2838 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
2839 * to the dst dependence graph.
2840 * If the source or destination node of the edge is not in the destination
2841 * graph, then it must be a backward proximity edge and it should simply
2844 static int copy_edges(isl_ctx
*ctx
, struct isl_sched_graph
*dst
,
2845 struct isl_sched_graph
*src
,
2846 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
), int data
)
2849 enum isl_edge_type t
;
2852 for (i
= 0; i
< src
->n_edge
; ++i
) {
2853 struct isl_sched_edge
*edge
= &src
->edge
[i
];
2855 isl_union_map
*tagged_condition
;
2856 isl_union_map
*tagged_validity
;
2857 struct isl_sched_node
*dst_src
, *dst_dst
;
2859 if (!edge_pred(edge
, data
))
2862 if (isl_map_plain_is_empty(edge
->map
))
2865 dst_src
= graph_find_node(ctx
, dst
, edge
->src
->space
);
2866 dst_dst
= graph_find_node(ctx
, dst
, edge
->dst
->space
);
2867 if (!dst_src
|| !dst_dst
) {
2868 if (edge
->validity
|| edge
->conditional_validity
)
2869 isl_die(ctx
, isl_error_internal
,
2870 "backward (conditional) validity edge",
2875 map
= isl_map_copy(edge
->map
);
2876 tagged_condition
= isl_union_map_copy(edge
->tagged_condition
);
2877 tagged_validity
= isl_union_map_copy(edge
->tagged_validity
);
2879 dst
->edge
[dst
->n_edge
].src
= dst_src
;
2880 dst
->edge
[dst
->n_edge
].dst
= dst_dst
;
2881 dst
->edge
[dst
->n_edge
].map
= map
;
2882 dst
->edge
[dst
->n_edge
].tagged_condition
= tagged_condition
;
2883 dst
->edge
[dst
->n_edge
].tagged_validity
= tagged_validity
;
2884 dst
->edge
[dst
->n_edge
].validity
= edge
->validity
;
2885 dst
->edge
[dst
->n_edge
].proximity
= edge
->proximity
;
2886 dst
->edge
[dst
->n_edge
].coincidence
= edge
->coincidence
;
2887 dst
->edge
[dst
->n_edge
].condition
= edge
->condition
;
2888 dst
->edge
[dst
->n_edge
].conditional_validity
=
2889 edge
->conditional_validity
;
2892 if (edge
->tagged_condition
&& !tagged_condition
)
2894 if (edge
->tagged_validity
&& !tagged_validity
)
2897 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
2899 graph_find_edge(src
, t
, edge
->src
, edge
->dst
))
2901 if (graph_edge_table_add(ctx
, dst
, t
,
2902 &dst
->edge
[dst
->n_edge
- 1]) < 0)
2910 /* Compute the maximal number of variables over all nodes.
2911 * This is the maximal number of linearly independent schedule
2912 * rows that we need to compute.
2913 * Just in case we end up in a part of the dependence graph
2914 * with only lower-dimensional domains, we make sure we will
2915 * compute the required amount of extra linearly independent rows.
2917 static int compute_maxvar(struct isl_sched_graph
*graph
)
2922 for (i
= 0; i
< graph
->n
; ++i
) {
2923 struct isl_sched_node
*node
= &graph
->node
[i
];
2926 if (node_update_cmap(node
) < 0)
2928 nvar
= node
->nvar
+ graph
->n_row
- node
->rank
;
2929 if (nvar
> graph
->maxvar
)
2930 graph
->maxvar
= nvar
;
2936 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
2937 struct isl_sched_graph
*graph
);
2938 static __isl_give isl_schedule_node
*compute_schedule_wcc(
2939 isl_schedule_node
*node
, struct isl_sched_graph
*graph
);
2941 /* Compute a schedule for a subgraph of "graph". In particular, for
2942 * the graph composed of nodes that satisfy node_pred and edges that
2943 * that satisfy edge_pred. The caller should precompute the number
2944 * of nodes and edges that satisfy these predicates and pass them along
2945 * as "n" and "n_edge".
2946 * If the subgraph is known to consist of a single component, then wcc should
2947 * be set and then we call compute_schedule_wcc on the constructed subgraph.
2948 * Otherwise, we call compute_schedule, which will check whether the subgraph
2951 * The schedule is inserted at "node" and the updated schedule node
2954 static __isl_give isl_schedule_node
*compute_sub_schedule(
2955 __isl_take isl_schedule_node
*node
, isl_ctx
*ctx
,
2956 struct isl_sched_graph
*graph
, int n
, int n_edge
,
2957 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
2958 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
2961 struct isl_sched_graph split
= { 0 };
2964 if (graph_alloc(ctx
, &split
, n
, n_edge
) < 0)
2966 if (copy_nodes(&split
, graph
, node_pred
, data
) < 0)
2968 if (graph_init_table(ctx
, &split
) < 0)
2970 for (t
= 0; t
<= isl_edge_last
; ++t
)
2971 split
.max_edge
[t
] = graph
->max_edge
[t
];
2972 if (graph_init_edge_tables(ctx
, &split
) < 0)
2974 if (copy_edges(ctx
, &split
, graph
, edge_pred
, data
) < 0)
2976 split
.n_row
= graph
->n_row
;
2977 split
.max_row
= graph
->max_row
;
2978 split
.n_total_row
= graph
->n_total_row
;
2979 split
.band_start
= graph
->band_start
;
2982 node
= compute_schedule_wcc(node
, &split
);
2984 node
= compute_schedule(node
, &split
);
2986 graph_free(ctx
, &split
);
2989 graph_free(ctx
, &split
);
2990 return isl_schedule_node_free(node
);
2993 static int edge_scc_exactly(struct isl_sched_edge
*edge
, int scc
)
2995 return edge
->src
->scc
== scc
&& edge
->dst
->scc
== scc
;
2998 static int edge_dst_scc_at_most(struct isl_sched_edge
*edge
, int scc
)
3000 return edge
->dst
->scc
<= scc
;
3003 static int edge_src_scc_at_least(struct isl_sched_edge
*edge
, int scc
)
3005 return edge
->src
->scc
>= scc
;
3008 /* Reset the current band by dropping all its schedule rows.
3010 static int reset_band(struct isl_sched_graph
*graph
)
3015 drop
= graph
->n_total_row
- graph
->band_start
;
3016 graph
->n_total_row
-= drop
;
3017 graph
->n_row
-= drop
;
3019 for (i
= 0; i
< graph
->n
; ++i
) {
3020 struct isl_sched_node
*node
= &graph
->node
[i
];
3022 isl_map_free(node
->sched_map
);
3023 node
->sched_map
= NULL
;
3025 node
->sched
= isl_mat_drop_rows(node
->sched
,
3026 graph
->band_start
, drop
);
3035 /* Split the current graph into two parts and compute a schedule for each
3036 * part individually. In particular, one part consists of all SCCs up
3037 * to and including graph->src_scc, while the other part contains the other
3038 * SCCS. The split is enforced by a sequence node inserted at position "node"
3039 * in the schedule tree. Return the updated schedule node.
3041 * The current band is reset. It would be possible to reuse
3042 * the previously computed rows as the first rows in the next
3043 * band, but recomputing them may result in better rows as we are looking
3044 * at a smaller part of the dependence graph.
3046 static __isl_give isl_schedule_node
*compute_split_schedule(
3047 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3052 isl_union_set_list
*filters
;
3057 if (reset_band(graph
) < 0)
3058 return isl_schedule_node_free(node
);
3061 for (i
= 0; i
< graph
->n
; ++i
) {
3062 struct isl_sched_node
*node
= &graph
->node
[i
];
3063 int before
= node
->scc
<= graph
->src_scc
;
3070 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3071 if (graph
->edge
[i
].dst
->scc
<= graph
->src_scc
)
3073 if (graph
->edge
[i
].src
->scc
> graph
->src_scc
)
3079 ctx
= isl_schedule_node_get_ctx(node
);
3080 filters
= extract_split(ctx
, graph
);
3081 node
= isl_schedule_node_insert_sequence(node
, filters
);
3082 node
= isl_schedule_node_child(node
, 0);
3083 node
= isl_schedule_node_child(node
, 0);
3085 orig_total_row
= graph
->n_total_row
;
3086 node
= compute_sub_schedule(node
, ctx
, graph
, n
, e1
,
3087 &node_scc_at_most
, &edge_dst_scc_at_most
,
3089 node
= isl_schedule_node_parent(node
);
3090 node
= isl_schedule_node_next_sibling(node
);
3091 node
= isl_schedule_node_child(node
, 0);
3092 graph
->n_total_row
= orig_total_row
;
3093 node
= compute_sub_schedule(node
, ctx
, graph
, graph
->n
- n
, e2
,
3094 &node_scc_at_least
, &edge_src_scc_at_least
,
3095 graph
->src_scc
+ 1, 0);
3096 node
= isl_schedule_node_parent(node
);
3097 node
= isl_schedule_node_parent(node
);
3102 /* Insert a band node at position "node" in the schedule tree corresponding
3103 * to the current band in "graph". Mark the band node permutable
3104 * if "permutable" is set.
3105 * The partial schedules and the coincidence property are extracted
3106 * from the graph nodes.
3107 * Return the updated schedule node.
3109 static __isl_give isl_schedule_node
*insert_current_band(
3110 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
3116 isl_multi_pw_aff
*mpa
;
3117 isl_multi_union_pw_aff
*mupa
;
3123 isl_die(isl_schedule_node_get_ctx(node
), isl_error_internal
,
3124 "graph should have at least one node",
3125 return isl_schedule_node_free(node
));
3127 start
= graph
->band_start
;
3128 end
= graph
->n_total_row
;
3131 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[0], start
, n
);
3132 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3133 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3135 for (i
= 1; i
< graph
->n
; ++i
) {
3136 isl_multi_union_pw_aff
*mupa_i
;
3138 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[i
],
3140 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3141 mupa_i
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3142 mupa
= isl_multi_union_pw_aff_union_add(mupa
, mupa_i
);
3144 node
= isl_schedule_node_insert_partial_schedule(node
, mupa
);
3146 for (i
= 0; i
< n
; ++i
)
3147 node
= isl_schedule_node_band_member_set_coincident(node
, i
,
3148 graph
->node
[0].coincident
[start
+ i
]);
3149 node
= isl_schedule_node_band_set_permutable(node
, permutable
);
3154 /* Update the dependence relations based on the current schedule,
3155 * add the current band to "node" and the continue with the computation
3157 * Return the updated schedule node.
3159 static __isl_give isl_schedule_node
*compute_next_band(
3160 __isl_take isl_schedule_node
*node
,
3161 struct isl_sched_graph
*graph
, int permutable
)
3168 ctx
= isl_schedule_node_get_ctx(node
);
3169 if (update_edges(ctx
, graph
) < 0)
3170 return isl_schedule_node_free(node
);
3171 node
= insert_current_band(node
, graph
, permutable
);
3174 node
= isl_schedule_node_child(node
, 0);
3175 node
= compute_schedule(node
, graph
);
3176 node
= isl_schedule_node_parent(node
);
3181 /* Add constraints to graph->lp that force the dependence "map" (which
3182 * is part of the dependence relation of "edge")
3183 * to be respected and attempt to carry it, where the edge is one from
3184 * a node j to itself. "pos" is the sequence number of the given map.
3185 * That is, add constraints that enforce
3187 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3188 * = c_j_x (y - x) >= e_i
3190 * for each (x,y) in R.
3191 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3192 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3193 * with each coefficient in c_j_x represented as a pair of non-negative
3196 static int add_intra_constraints(struct isl_sched_graph
*graph
,
3197 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
3200 isl_ctx
*ctx
= isl_map_get_ctx(map
);
3202 isl_dim_map
*dim_map
;
3203 isl_basic_set
*coef
;
3204 struct isl_sched_node
*node
= edge
->src
;
3206 coef
= intra_coefficients(graph
, node
, map
);
3210 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
3212 total
= isl_basic_set_total_dim(graph
->lp
);
3213 dim_map
= isl_dim_map_alloc(ctx
, total
);
3214 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3215 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
3216 isl_space_dim(dim
, isl_dim_set
), 1,
3218 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
3219 isl_space_dim(dim
, isl_dim_set
), 1,
3221 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
3222 coef
->n_eq
, coef
->n_ineq
);
3223 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
3225 isl_space_free(dim
);
3230 /* Add constraints to graph->lp that force the dependence "map" (which
3231 * is part of the dependence relation of "edge")
3232 * to be respected and attempt to carry it, where the edge is one from
3233 * node j to node k. "pos" is the sequence number of the given map.
3234 * That is, add constraints that enforce
3236 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3238 * for each (x,y) in R.
3239 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3240 * of valid constraints for R and then plug in
3241 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3242 * with each coefficient (except e_i, c_k_0 and c_j_0)
3243 * represented as a pair of non-negative coefficients.
3245 static int add_inter_constraints(struct isl_sched_graph
*graph
,
3246 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
3249 isl_ctx
*ctx
= isl_map_get_ctx(map
);
3251 isl_dim_map
*dim_map
;
3252 isl_basic_set
*coef
;
3253 struct isl_sched_node
*src
= edge
->src
;
3254 struct isl_sched_node
*dst
= edge
->dst
;
3256 coef
= inter_coefficients(graph
, edge
, map
);
3260 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
3262 total
= isl_basic_set_total_dim(graph
->lp
);
3263 dim_map
= isl_dim_map_alloc(ctx
, total
);
3265 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3267 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, 1);
3268 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, -1);
3269 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, 1);
3270 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
3271 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
3273 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
3274 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
3277 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, -1);
3278 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, 1);
3279 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, -1);
3280 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
3281 isl_space_dim(dim
, isl_dim_set
), 1,
3283 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
3284 isl_space_dim(dim
, isl_dim_set
), 1,
3287 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
3288 coef
->n_eq
, coef
->n_ineq
);
3289 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
3291 isl_space_free(dim
);
3296 /* Add constraints to graph->lp that force all (conditional) validity
3297 * dependences to be respected and attempt to carry them.
3299 static int add_all_constraints(struct isl_sched_graph
*graph
)
3305 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3306 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
3308 if (!edge
->validity
&& !edge
->conditional_validity
)
3311 for (j
= 0; j
< edge
->map
->n
; ++j
) {
3312 isl_basic_map
*bmap
;
3315 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
3316 map
= isl_map_from_basic_map(bmap
);
3318 if (edge
->src
== edge
->dst
&&
3319 add_intra_constraints(graph
, edge
, map
, pos
) < 0)
3321 if (edge
->src
!= edge
->dst
&&
3322 add_inter_constraints(graph
, edge
, map
, pos
) < 0)
3331 /* Count the number of equality and inequality constraints
3332 * that will be added to the carry_lp problem.
3333 * We count each edge exactly once.
3335 static int count_all_constraints(struct isl_sched_graph
*graph
,
3336 int *n_eq
, int *n_ineq
)
3340 *n_eq
= *n_ineq
= 0;
3341 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3342 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
3343 for (j
= 0; j
< edge
->map
->n
; ++j
) {
3344 isl_basic_map
*bmap
;
3347 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
3348 map
= isl_map_from_basic_map(bmap
);
3350 if (count_map_constraints(graph
, edge
, map
,
3351 n_eq
, n_ineq
, 1, 0) < 0)
3359 /* Construct an LP problem for finding schedule coefficients
3360 * such that the schedule carries as many dependences as possible.
3361 * In particular, for each dependence i, we bound the dependence distance
3362 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3363 * of all e_i's. Dependence with e_i = 0 in the solution are simply
3364 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3365 * Note that if the dependence relation is a union of basic maps,
3366 * then we have to consider each basic map individually as it may only
3367 * be possible to carry the dependences expressed by some of those
3368 * basic maps and not all off them.
3369 * Below, we consider each of those basic maps as a separate "edge".
3371 * All variables of the LP are non-negative. The actual coefficients
3372 * may be negative, so each coefficient is represented as the difference
3373 * of two non-negative variables. The negative part always appears
3374 * immediately before the positive part.
3375 * Other than that, the variables have the following order
3377 * - sum of (1 - e_i) over all edges
3378 * - sum of positive and negative parts of all c_n coefficients
3379 * (unconstrained when computing non-parametric schedules)
3380 * - sum of positive and negative parts of all c_x coefficients
3385 * - positive and negative parts of c_i_n (if parametric)
3386 * - positive and negative parts of c_i_x
3388 * The constraints are those from the (validity) edges plus three equalities
3389 * to express the sums and n_edge inequalities to express e_i <= 1.
3391 static int setup_carry_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3401 for (i
= 0; i
< graph
->n_edge
; ++i
)
3402 n_edge
+= graph
->edge
[i
].map
->n
;
3405 for (i
= 0; i
< graph
->n
; ++i
) {
3406 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
3407 node
->start
= total
;
3408 total
+= 1 + 2 * (node
->nparam
+ node
->nvar
);
3411 if (count_all_constraints(graph
, &n_eq
, &n_ineq
) < 0)
3413 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
3416 dim
= isl_space_set_alloc(ctx
, 0, total
);
3417 isl_basic_set_free(graph
->lp
);
3420 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
3421 graph
->lp
= isl_basic_set_set_rational(graph
->lp
);
3423 k
= isl_basic_set_alloc_equality(graph
->lp
);
3426 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
3427 isl_int_set_si(graph
->lp
->eq
[k
][0], -n_edge
);
3428 isl_int_set_si(graph
->lp
->eq
[k
][1], 1);
3429 for (i
= 0; i
< n_edge
; ++i
)
3430 isl_int_set_si(graph
->lp
->eq
[k
][4 + i
], 1);
3432 k
= isl_basic_set_alloc_equality(graph
->lp
);
3435 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
3436 isl_int_set_si(graph
->lp
->eq
[k
][2], -1);
3437 for (i
= 0; i
< graph
->n
; ++i
) {
3438 int pos
= 1 + graph
->node
[i
].start
+ 1;
3440 for (j
= 0; j
< 2 * graph
->node
[i
].nparam
; ++j
)
3441 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
3444 k
= isl_basic_set_alloc_equality(graph
->lp
);
3447 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
3448 isl_int_set_si(graph
->lp
->eq
[k
][3], -1);
3449 for (i
= 0; i
< graph
->n
; ++i
) {
3450 struct isl_sched_node
*node
= &graph
->node
[i
];
3451 int pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
3453 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
3454 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
3457 for (i
= 0; i
< n_edge
; ++i
) {
3458 k
= isl_basic_set_alloc_inequality(graph
->lp
);
3461 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
3462 isl_int_set_si(graph
->lp
->ineq
[k
][4 + i
], -1);
3463 isl_int_set_si(graph
->lp
->ineq
[k
][0], 1);
3466 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
3468 if (add_all_constraints(graph
) < 0)
3474 static __isl_give isl_schedule_node
*compute_component_schedule(
3475 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
3478 /* Comparison function for sorting the statements based on
3479 * the corresponding value in "r".
3481 static int smaller_value(const void *a
, const void *b
, void *data
)
3487 return isl_int_cmp(r
->el
[*i1
], r
->el
[*i2
]);
3490 /* If the schedule_split_scaled option is set and if the linear
3491 * parts of the scheduling rows for all nodes in the graphs have
3492 * a non-trivial common divisor, then split off the remainder of the
3493 * constant term modulo this common divisor from the linear part.
3494 * Otherwise, insert a band node directly and continue with
3495 * the construction of the schedule.
3497 * If a non-trivial common divisor is found, then
3498 * the linear part is reduced and the remainder is enforced
3499 * by a sequence node with the children placed in the order
3500 * of this remainder.
3501 * In particular, we assign an scc index based on the remainder and
3502 * then rely on compute_component_schedule to insert the sequence and
3503 * to continue the schedule construction on each part.
3505 static __isl_give isl_schedule_node
*split_scaled(
3506 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3519 ctx
= isl_schedule_node_get_ctx(node
);
3520 if (!ctx
->opt
->schedule_split_scaled
)
3521 return compute_next_band(node
, graph
, 0);
3523 return compute_next_band(node
, graph
, 0);
3526 isl_int_init(gcd_i
);
3528 isl_int_set_si(gcd
, 0);
3530 row
= isl_mat_rows(graph
->node
[0].sched
) - 1;
3532 for (i
= 0; i
< graph
->n
; ++i
) {
3533 struct isl_sched_node
*node
= &graph
->node
[i
];
3534 int cols
= isl_mat_cols(node
->sched
);
3536 isl_seq_gcd(node
->sched
->row
[row
] + 1, cols
- 1, &gcd_i
);
3537 isl_int_gcd(gcd
, gcd
, gcd_i
);
3540 isl_int_clear(gcd_i
);
3542 if (isl_int_cmp_si(gcd
, 1) <= 0) {
3544 return compute_next_band(node
, graph
, 0);
3547 r
= isl_vec_alloc(ctx
, graph
->n
);
3548 order
= isl_calloc_array(ctx
, int, graph
->n
);
3552 for (i
= 0; i
< graph
->n
; ++i
) {
3553 struct isl_sched_node
*node
= &graph
->node
[i
];
3556 isl_int_fdiv_r(r
->el
[i
], node
->sched
->row
[row
][0], gcd
);
3557 isl_int_fdiv_q(node
->sched
->row
[row
][0],
3558 node
->sched
->row
[row
][0], gcd
);
3559 isl_int_mul(node
->sched
->row
[row
][0],
3560 node
->sched
->row
[row
][0], gcd
);
3561 node
->sched
= isl_mat_scale_down_row(node
->sched
, row
, gcd
);
3566 if (isl_sort(order
, graph
->n
, sizeof(order
[0]), &smaller_value
, r
) < 0)
3570 for (i
= 0; i
< graph
->n
; ++i
) {
3571 if (i
> 0 && isl_int_ne(r
->el
[order
[i
- 1]], r
->el
[order
[i
]]))
3573 graph
->node
[order
[i
]].scc
= scc
;
3582 if (update_edges(ctx
, graph
) < 0)
3583 return isl_schedule_node_free(node
);
3584 node
= insert_current_band(node
, graph
, 0);
3587 node
= isl_schedule_node_child(node
, 0);
3588 node
= compute_component_schedule(node
, graph
, 0);
3589 node
= isl_schedule_node_parent(node
);
3596 return isl_schedule_node_free(node
);
3599 /* Is the schedule row "sol" trivial on node "node"?
3600 * That is, is the solution zero on the dimensions orthogonal to
3601 * the previously found solutions?
3602 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3604 * Each coefficient is represented as the difference between
3605 * two non-negative values in "sol". "sol" has been computed
3606 * in terms of the original iterators (i.e., without use of cmap).
3607 * We construct the schedule row s and write it as a linear
3608 * combination of (linear combinations of) previously computed schedule rows.
3609 * s = Q c or c = U s.
3610 * If the final entries of c are all zero, then the solution is trivial.
3612 static int is_trivial(struct isl_sched_node
*node
, __isl_keep isl_vec
*sol
)
3622 if (node
->nvar
== node
->rank
)
3625 ctx
= isl_vec_get_ctx(sol
);
3626 node_sol
= isl_vec_alloc(ctx
, node
->nvar
);
3630 pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
3632 for (i
= 0; i
< node
->nvar
; ++i
)
3633 isl_int_sub(node_sol
->el
[i
],
3634 sol
->el
[pos
+ 2 * i
+ 1], sol
->el
[pos
+ 2 * i
]);
3636 node_sol
= isl_mat_vec_product(isl_mat_copy(node
->cinv
), node_sol
);
3641 trivial
= isl_seq_first_non_zero(node_sol
->el
+ node
->rank
,
3642 node
->nvar
- node
->rank
) == -1;
3644 isl_vec_free(node_sol
);
3649 /* Is the schedule row "sol" trivial on any node where it should
3651 * "sol" has been computed in terms of the original iterators
3652 * (i.e., without use of cmap).
3653 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3655 static int is_any_trivial(struct isl_sched_graph
*graph
,
3656 __isl_keep isl_vec
*sol
)
3660 for (i
= 0; i
< graph
->n
; ++i
) {
3661 struct isl_sched_node
*node
= &graph
->node
[i
];
3664 if (!needs_row(graph
, node
))
3666 trivial
= is_trivial(node
, sol
);
3667 if (trivial
< 0 || trivial
)
3674 /* Construct a schedule row for each node such that as many dependences
3675 * as possible are carried and then continue with the next band.
3677 * If the computed schedule row turns out to be trivial on one or
3678 * more nodes where it should not be trivial, then we throw it away
3679 * and try again on each component separately.
3681 * If there is only one component, then we accept the schedule row anyway,
3682 * but we do not consider it as a complete row and therefore do not
3683 * increment graph->n_row. Note that the ranks of the nodes that
3684 * do get a non-trivial schedule part will get updated regardless and
3685 * graph->maxvar is computed based on these ranks. The test for
3686 * whether more schedule rows are required in compute_schedule_wcc
3687 * is therefore not affected.
3689 * Insert a band corresponding to the schedule row at position "node"
3690 * of the schedule tree and continue with the construction of the schedule.
3691 * This insertion and the continued construction is performed by split_scaled
3692 * after optionally checking for non-trivial common divisors.
3694 static __isl_give isl_schedule_node
*carry_dependences(
3695 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3708 for (i
= 0; i
< graph
->n_edge
; ++i
)
3709 n_edge
+= graph
->edge
[i
].map
->n
;
3711 ctx
= isl_schedule_node_get_ctx(node
);
3712 if (setup_carry_lp(ctx
, graph
) < 0)
3713 return isl_schedule_node_free(node
);
3715 lp
= isl_basic_set_copy(graph
->lp
);
3716 sol
= isl_tab_basic_set_non_neg_lexmin(lp
);
3718 return isl_schedule_node_free(node
);
3720 if (sol
->size
== 0) {
3722 isl_die(ctx
, isl_error_internal
,
3723 "error in schedule construction",
3724 return isl_schedule_node_free(node
));
3727 isl_int_divexact(sol
->el
[1], sol
->el
[1], sol
->el
[0]);
3728 if (isl_int_cmp_si(sol
->el
[1], n_edge
) >= 0) {
3730 isl_die(ctx
, isl_error_unknown
,
3731 "unable to carry dependences",
3732 return isl_schedule_node_free(node
));
3735 trivial
= is_any_trivial(graph
, sol
);
3737 sol
= isl_vec_free(sol
);
3738 } else if (trivial
&& graph
->scc
> 1) {
3740 return compute_component_schedule(node
, graph
, 1);
3743 if (update_schedule(graph
, sol
, 0, 0) < 0)
3744 return isl_schedule_node_free(node
);
3748 return split_scaled(node
, graph
);
3751 /* Topologically sort statements mapped to the same schedule iteration
3752 * and add insert a sequence node in front of "node"
3753 * corresponding to this order.
3755 static __isl_give isl_schedule_node
*sort_statements(
3756 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3759 isl_union_set_list
*filters
;
3764 ctx
= isl_schedule_node_get_ctx(node
);
3766 isl_die(ctx
, isl_error_internal
,
3767 "graph should have at least one node",
3768 return isl_schedule_node_free(node
));
3773 if (update_edges(ctx
, graph
) < 0)
3774 return isl_schedule_node_free(node
);
3776 if (graph
->n_edge
== 0)
3779 if (detect_sccs(ctx
, graph
) < 0)
3780 return isl_schedule_node_free(node
);
3782 filters
= extract_sccs(ctx
, graph
);
3783 node
= isl_schedule_node_insert_sequence(node
, filters
);
3788 /* Are there any (non-empty) (conditional) validity edges in the graph?
3790 static int has_validity_edges(struct isl_sched_graph
*graph
)
3794 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3797 empty
= isl_map_plain_is_empty(graph
->edge
[i
].map
);
3802 if (graph
->edge
[i
].validity
||
3803 graph
->edge
[i
].conditional_validity
)
3810 /* Should we apply a Feautrier step?
3811 * That is, did the user request the Feautrier algorithm and are
3812 * there any validity dependences (left)?
3814 static int need_feautrier_step(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3816 if (ctx
->opt
->schedule_algorithm
!= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
)
3819 return has_validity_edges(graph
);
3822 /* Compute a schedule for a connected dependence graph using Feautrier's
3823 * multi-dimensional scheduling algorithm and return the updated schedule node.
3825 * The original algorithm is described in [1].
3826 * The main idea is to minimize the number of scheduling dimensions, by
3827 * trying to satisfy as many dependences as possible per scheduling dimension.
3829 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
3830 * Problem, Part II: Multi-Dimensional Time.
3831 * In Intl. Journal of Parallel Programming, 1992.
3833 static __isl_give isl_schedule_node
*compute_schedule_wcc_feautrier(
3834 isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3836 return carry_dependences(node
, graph
);
3839 /* Turn off the "local" bit on all (condition) edges.
3841 static void clear_local_edges(struct isl_sched_graph
*graph
)
3845 for (i
= 0; i
< graph
->n_edge
; ++i
)
3846 if (graph
->edge
[i
].condition
)
3847 graph
->edge
[i
].local
= 0;
3850 /* Does "graph" have both condition and conditional validity edges?
3852 static int need_condition_check(struct isl_sched_graph
*graph
)
3855 int any_condition
= 0;
3856 int any_conditional_validity
= 0;
3858 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3859 if (graph
->edge
[i
].condition
)
3861 if (graph
->edge
[i
].conditional_validity
)
3862 any_conditional_validity
= 1;
3865 return any_condition
&& any_conditional_validity
;
3868 /* Does "graph" contain any coincidence edge?
3870 static int has_any_coincidence(struct isl_sched_graph
*graph
)
3874 for (i
= 0; i
< graph
->n_edge
; ++i
)
3875 if (graph
->edge
[i
].coincidence
)
3881 /* Extract the final schedule row as a map with the iteration domain
3882 * of "node" as domain.
3884 static __isl_give isl_map
*final_row(struct isl_sched_node
*node
)
3886 isl_local_space
*ls
;
3890 row
= isl_mat_rows(node
->sched
) - 1;
3891 ls
= isl_local_space_from_space(isl_space_copy(node
->space
));
3892 aff
= extract_schedule_row(ls
, node
, row
);
3893 return isl_map_from_aff(aff
);
3896 /* Is the conditional validity dependence in the edge with index "edge_index"
3897 * violated by the latest (i.e., final) row of the schedule?
3898 * That is, is i scheduled after j
3899 * for any conditional validity dependence i -> j?
3901 static int is_violated(struct isl_sched_graph
*graph
, int edge_index
)
3903 isl_map
*src_sched
, *dst_sched
, *map
;
3904 struct isl_sched_edge
*edge
= &graph
->edge
[edge_index
];
3907 src_sched
= final_row(edge
->src
);
3908 dst_sched
= final_row(edge
->dst
);
3909 map
= isl_map_copy(edge
->map
);
3910 map
= isl_map_apply_domain(map
, src_sched
);
3911 map
= isl_map_apply_range(map
, dst_sched
);
3912 map
= isl_map_order_gt(map
, isl_dim_in
, 0, isl_dim_out
, 0);
3913 empty
= isl_map_is_empty(map
);
3922 /* Does "graph" have any satisfied condition edges that
3923 * are adjacent to the conditional validity constraint with
3924 * domain "conditional_source" and range "conditional_sink"?
3926 * A satisfied condition is one that is not local.
3927 * If a condition was forced to be local already (i.e., marked as local)
3928 * then there is no need to check if it is in fact local.
3930 * Additionally, mark all adjacent condition edges found as local.
3932 static int has_adjacent_true_conditions(struct isl_sched_graph
*graph
,
3933 __isl_keep isl_union_set
*conditional_source
,
3934 __isl_keep isl_union_set
*conditional_sink
)
3939 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3940 int adjacent
, local
;
3941 isl_union_map
*condition
;
3943 if (!graph
->edge
[i
].condition
)
3945 if (graph
->edge
[i
].local
)
3948 condition
= graph
->edge
[i
].tagged_condition
;
3949 adjacent
= domain_intersects(condition
, conditional_sink
);
3950 if (adjacent
>= 0 && !adjacent
)
3951 adjacent
= range_intersects(condition
,
3952 conditional_source
);
3958 graph
->edge
[i
].local
= 1;
3960 local
= is_condition_false(&graph
->edge
[i
]);
3970 /* Are there any violated conditional validity dependences with
3971 * adjacent condition dependences that are not local with respect
3972 * to the current schedule?
3973 * That is, is the conditional validity constraint violated?
3975 * Additionally, mark all those adjacent condition dependences as local.
3976 * We also mark those adjacent condition dependences that were not marked
3977 * as local before, but just happened to be local already. This ensures
3978 * that they remain local if the schedule is recomputed.
3980 * We first collect domain and range of all violated conditional validity
3981 * dependences and then check if there are any adjacent non-local
3982 * condition dependences.
3984 static int has_violated_conditional_constraint(isl_ctx
*ctx
,
3985 struct isl_sched_graph
*graph
)
3989 isl_union_set
*source
, *sink
;
3991 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3992 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3993 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3994 isl_union_set
*uset
;
3995 isl_union_map
*umap
;
3998 if (!graph
->edge
[i
].conditional_validity
)
4001 violated
= is_violated(graph
, i
);
4009 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
4010 uset
= isl_union_map_domain(umap
);
4011 source
= isl_union_set_union(source
, uset
);
4012 source
= isl_union_set_coalesce(source
);
4014 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
4015 uset
= isl_union_map_range(umap
);
4016 sink
= isl_union_set_union(sink
, uset
);
4017 sink
= isl_union_set_coalesce(sink
);
4021 any
= has_adjacent_true_conditions(graph
, source
, sink
);
4023 isl_union_set_free(source
);
4024 isl_union_set_free(sink
);
4027 isl_union_set_free(source
);
4028 isl_union_set_free(sink
);
4032 /* Compute a schedule for a connected dependence graph and return
4033 * the updated schedule node.
4035 * We try to find a sequence of as many schedule rows as possible that result
4036 * in non-negative dependence distances (independent of the previous rows
4037 * in the sequence, i.e., such that the sequence is tilable), with as
4038 * many of the initial rows as possible satisfying the coincidence constraints.
4039 * If we can't find any more rows we either
4040 * - split between SCCs and start over (assuming we found an interesting
4041 * pair of SCCs between which to split)
4042 * - continue with the next band (assuming the current band has at least
4044 * - try to carry as many dependences as possible and continue with the next
4046 * In each case, we first insert a band node in the schedule tree
4047 * if any rows have been computed.
4049 * If Feautrier's algorithm is selected, we first recursively try to satisfy
4050 * as many validity dependences as possible. When all validity dependences
4051 * are satisfied we extend the schedule to a full-dimensional schedule.
4053 * If we manage to complete the schedule, we insert a band node
4054 * (if any schedule rows were computed) and we finish off by topologically
4055 * sorting the statements based on the remaining dependences.
4057 * If ctx->opt->schedule_outer_coincidence is set, then we force the
4058 * outermost dimension to satisfy the coincidence constraints. If this
4059 * turns out to be impossible, we fall back on the general scheme above
4060 * and try to carry as many dependences as possible.
4062 * If "graph" contains both condition and conditional validity dependences,
4063 * then we need to check that that the conditional schedule constraint
4064 * is satisfied, i.e., there are no violated conditional validity dependences
4065 * that are adjacent to any non-local condition dependences.
4066 * If there are, then we mark all those adjacent condition dependences
4067 * as local and recompute the current band. Those dependences that
4068 * are marked local will then be forced to be local.
4069 * The initial computation is performed with no dependences marked as local.
4070 * If we are lucky, then there will be no violated conditional validity
4071 * dependences adjacent to any non-local condition dependences.
4072 * Otherwise, we mark some additional condition dependences as local and
4073 * recompute. We continue this process until there are no violations left or
4074 * until we are no longer able to compute a schedule.
4075 * Since there are only a finite number of dependences,
4076 * there will only be a finite number of iterations.
4078 static __isl_give isl_schedule_node
*compute_schedule_wcc(
4079 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4081 int has_coincidence
;
4082 int use_coincidence
;
4083 int force_coincidence
= 0;
4084 int check_conditional
;
4091 ctx
= isl_schedule_node_get_ctx(node
);
4092 if (detect_sccs(ctx
, graph
) < 0)
4093 return isl_schedule_node_free(node
);
4094 if (sort_sccs(graph
) < 0)
4095 return isl_schedule_node_free(node
);
4097 if (compute_maxvar(graph
) < 0)
4098 return isl_schedule_node_free(node
);
4100 if (need_feautrier_step(ctx
, graph
))
4101 return compute_schedule_wcc_feautrier(node
, graph
);
4103 clear_local_edges(graph
);
4104 check_conditional
= need_condition_check(graph
);
4105 has_coincidence
= has_any_coincidence(graph
);
4107 if (ctx
->opt
->schedule_outer_coincidence
)
4108 force_coincidence
= 1;
4110 use_coincidence
= has_coincidence
;
4111 while (graph
->n_row
< graph
->maxvar
) {
4116 graph
->src_scc
= -1;
4117 graph
->dst_scc
= -1;
4119 if (setup_lp(ctx
, graph
, use_coincidence
) < 0)
4120 return isl_schedule_node_free(node
);
4121 sol
= solve_lp(graph
);
4123 return isl_schedule_node_free(node
);
4124 if (sol
->size
== 0) {
4125 int empty
= graph
->n_total_row
== graph
->band_start
;
4128 if (use_coincidence
&& (!force_coincidence
|| !empty
)) {
4129 use_coincidence
= 0;
4132 if (!ctx
->opt
->schedule_maximize_band_depth
&& !empty
)
4133 return compute_next_band(node
, graph
, 1);
4134 if (graph
->src_scc
>= 0)
4135 return compute_split_schedule(node
, graph
);
4137 return compute_next_band(node
, graph
, 1);
4138 return carry_dependences(node
, graph
);
4140 coincident
= !has_coincidence
|| use_coincidence
;
4141 if (update_schedule(graph
, sol
, 1, coincident
) < 0)
4142 return isl_schedule_node_free(node
);
4144 if (!check_conditional
)
4146 violated
= has_violated_conditional_constraint(ctx
, graph
);
4148 return isl_schedule_node_free(node
);
4151 if (reset_band(graph
) < 0)
4152 return isl_schedule_node_free(node
);
4153 use_coincidence
= has_coincidence
;
4156 insert
= graph
->n_total_row
> graph
->band_start
;
4158 node
= insert_current_band(node
, graph
, 1);
4159 node
= isl_schedule_node_child(node
, 0);
4161 node
= sort_statements(node
, graph
);
4163 node
= isl_schedule_node_parent(node
);
4168 /* Compute a schedule for each group of nodes identified by node->scc
4169 * separately and then combine them in a sequence node (or as set node
4170 * if graph->weak is set) inserted at position "node" of the schedule tree.
4171 * Return the updated schedule node.
4173 * If "wcc" is set then each of the groups belongs to a single
4174 * weakly connected component in the dependence graph so that
4175 * there is no need for compute_sub_schedule to look for weakly
4176 * connected components.
4178 static __isl_give isl_schedule_node
*compute_component_schedule(
4179 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
4186 isl_union_set_list
*filters
;
4190 ctx
= isl_schedule_node_get_ctx(node
);
4192 filters
= extract_sccs(ctx
, graph
);
4194 node
= isl_schedule_node_insert_set(node
, filters
);
4196 node
= isl_schedule_node_insert_sequence(node
, filters
);
4198 orig_total_row
= graph
->n_total_row
;
4199 for (component
= 0; component
< graph
->scc
; ++component
) {
4201 for (i
= 0; i
< graph
->n
; ++i
)
4202 if (graph
->node
[i
].scc
== component
)
4205 for (i
= 0; i
< graph
->n_edge
; ++i
)
4206 if (graph
->edge
[i
].src
->scc
== component
&&
4207 graph
->edge
[i
].dst
->scc
== component
)
4210 node
= isl_schedule_node_child(node
, component
);
4211 node
= isl_schedule_node_child(node
, 0);
4212 node
= compute_sub_schedule(node
, ctx
, graph
, n
, n_edge
,
4214 &edge_scc_exactly
, component
, wcc
);
4215 node
= isl_schedule_node_parent(node
);
4216 node
= isl_schedule_node_parent(node
);
4217 graph
->n_total_row
= orig_total_row
;
4223 /* Compute a schedule for the given dependence graph and insert it at "node".
4224 * Return the updated schedule node.
4226 * We first check if the graph is connected (through validity and conditional
4227 * validity dependences) and, if not, compute a schedule
4228 * for each component separately.
4229 * If schedule_fuse is set to minimal fusion, then we check for strongly
4230 * connected components instead and compute a separate schedule for
4231 * each such strongly connected component.
4233 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
4234 struct isl_sched_graph
*graph
)
4241 ctx
= isl_schedule_node_get_ctx(node
);
4242 if (ctx
->opt
->schedule_fuse
== ISL_SCHEDULE_FUSE_MIN
) {
4243 if (detect_sccs(ctx
, graph
) < 0)
4244 return isl_schedule_node_free(node
);
4246 if (detect_wccs(ctx
, graph
) < 0)
4247 return isl_schedule_node_free(node
);
4251 return compute_component_schedule(node
, graph
, 1);
4253 return compute_schedule_wcc(node
, graph
);
4256 /* Compute a schedule on sc->domain that respects the given schedule
4259 * In particular, the schedule respects all the validity dependences.
4260 * If the default isl scheduling algorithm is used, it tries to minimize
4261 * the dependence distances over the proximity dependences.
4262 * If Feautrier's scheduling algorithm is used, the proximity dependence
4263 * distances are only minimized during the extension to a full-dimensional
4266 * If there are any condition and conditional validity dependences,
4267 * then the conditional validity dependences may be violated inside
4268 * a tilable band, provided they have no adjacent non-local
4269 * condition dependences.
4271 * The context is included in the domain before the nodes of
4272 * the graphs are extracted in order to be able to exploit
4273 * any possible additional equalities.
4274 * However, the returned schedule contains the original domain
4275 * (before this intersection).
4277 __isl_give isl_schedule
*isl_schedule_constraints_compute_schedule(
4278 __isl_take isl_schedule_constraints
*sc
)
4280 isl_ctx
*ctx
= isl_schedule_constraints_get_ctx(sc
);
4281 struct isl_sched_graph graph
= { 0 };
4282 isl_schedule
*sched
;
4283 isl_schedule_node
*node
;
4284 isl_union_set
*domain
;
4285 struct isl_extract_edge_data data
;
4286 enum isl_edge_type i
;
4289 sc
= isl_schedule_constraints_align_params(sc
);
4293 graph
.n
= isl_union_set_n_set(sc
->domain
);
4295 isl_union_set
*domain
= isl_union_set_copy(sc
->domain
);
4296 sched
= isl_schedule_from_domain(domain
);
4299 if (graph_alloc(ctx
, &graph
, graph
.n
,
4300 isl_schedule_constraints_n_map(sc
)) < 0)
4302 if (compute_max_row(&graph
, sc
) < 0)
4306 domain
= isl_union_set_copy(sc
->domain
);
4307 domain
= isl_union_set_intersect_params(domain
,
4308 isl_set_copy(sc
->context
));
4309 r
= isl_union_set_foreach_set(domain
, &extract_node
, &graph
);
4310 isl_union_set_free(domain
);
4313 if (graph_init_table(ctx
, &graph
) < 0)
4315 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
4316 graph
.max_edge
[i
] = isl_union_map_n_map(sc
->constraint
[i
]);
4317 if (graph_init_edge_tables(ctx
, &graph
) < 0)
4320 data
.graph
= &graph
;
4321 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
4323 if (isl_union_map_foreach_map(sc
->constraint
[i
],
4324 &extract_edge
, &data
) < 0)
4328 node
= isl_schedule_node_from_domain(isl_union_set_copy(sc
->domain
));
4329 node
= isl_schedule_node_child(node
, 0);
4330 node
= compute_schedule(node
, &graph
);
4331 sched
= isl_schedule_node_get_schedule(node
);
4332 isl_schedule_node_free(node
);
4335 graph_free(ctx
, &graph
);
4336 isl_schedule_constraints_free(sc
);
4340 graph_free(ctx
, &graph
);
4341 isl_schedule_constraints_free(sc
);
4345 /* Compute a schedule for the given union of domains that respects
4346 * all the validity dependences and minimizes
4347 * the dependence distances over the proximity dependences.
4349 * This function is kept for backward compatibility.
4351 __isl_give isl_schedule
*isl_union_set_compute_schedule(
4352 __isl_take isl_union_set
*domain
,
4353 __isl_take isl_union_map
*validity
,
4354 __isl_take isl_union_map
*proximity
)
4356 isl_schedule_constraints
*sc
;
4358 sc
= isl_schedule_constraints_on_domain(domain
);
4359 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
4360 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
4362 return isl_schedule_constraints_compute_schedule(sc
);