14 #include <isl/union_set.h>
22 /* A pair of a node and an access from that node.
23 * "map" is the converted access relation.
24 * "projected_map" represents the converted access relation without
25 * any embedded access relation or access filters
31 isl_map
*projected_map
;
32 void project_out_access_filters(void);
33 na_pair(pdg::node
*n
, pdg::access
*a
) : node(n
), access(a
), map(NULL
),
34 projected_map(NULL
) {}
37 isl_map_free(projected_map
);
41 /* If the access has any embedded filters, then project them out
42 * from "projected_map", initializing "projected_map" from "map"
43 * if there is no "projected_map" yet.
45 void na_pair::project_out_access_filters(void)
50 if (access
->nested
.size() == 0)
54 projected_map
= isl_map_copy(map
);
56 space
= isl_space_domain(isl_map_get_space(projected_map
));
57 space
= isl_space_unwrap(space
);
58 proj
= isl_map_domain_map(isl_map_universe(space
));
60 projected_map
= isl_map_apply_domain(projected_map
, proj
);
63 static int precedes_level_nodes(na_pair
*first
, na_pair
*second
);
64 static int precedes_level_accesses(na_pair
*first
, na_pair
*second
);
66 /* Given a map from a domain to an orthogonal projection of an array
67 * (say, the rows of an array), mapping i to m(i), this function
68 * extends the range of the mapping to the original array and extends
69 * the domain of the mapping correspondingly such that (i,j) maps
70 * to (m(i),j), with (m(i),j) identifying an element of the array.
71 * The bounds on j are taken from the size of the array.
73 * The mapping from i to (i,j) is stored in the "extension" field
76 * The dependences computed using these extended access mappings,
77 * will map a (possibly) extended source domain to a (possibly)
78 * extended sink domain. One or both of these domains need to
79 * be transformed back to the original domains using the inverse
80 * of the corresponding extensions.
82 static isl_map
*extend_access(isl_map
*map
, na_pair
*na
)
84 pdg::array
*array
= na
->access
->array
;
85 unsigned n_out
= isl_map_dim(map
, isl_dim_out
);
86 assert(n_out
< array
->dims
.size());
87 unsigned s_dim
= array
->dims
.size() - n_out
;
88 isl_id
*array_id
= NULL
;
89 if (isl_map_has_tuple_id(map
, isl_dim_out
))
90 array_id
= isl_map_get_tuple_id(map
, isl_dim_out
);
91 isl_space
*space
= isl_map_get_space(map
);
92 space
= isl_space_drop_dims(space
, isl_dim_in
,
93 0, isl_space_dim(space
, isl_dim_in
));
94 space
= isl_space_drop_dims(space
, isl_dim_out
,
95 0, isl_space_dim(space
, isl_dim_out
));
96 space
= isl_space_add_dims(space
, isl_dim_in
, s_dim
);
97 space
= isl_space_add_dims(space
, isl_dim_out
, s_dim
);
98 isl_basic_map
*id
= isl_basic_map_identity(space
);
99 for (int i
= 0; i
< s_dim
; ++i
) {
102 id
= isl_basic_map_lower_bound_si(id
, isl_dim_out
, i
, 0);
103 v
= array
->dims
[n_out
+ i
] - 1;
104 id
= isl_basic_map_upper_bound_si(id
, isl_dim_out
, i
, v
);
106 map
= isl_map_product(map
, isl_map_from_basic_map(id
));
107 map
= isl_map_flatten_range(map
);
109 map
= isl_map_set_tuple_id(map
, isl_dim_out
, array_id
);
110 if (!na
->access
->extension
) {
111 isl_map
*ext
= isl_map_copy(map
);
112 ext
= isl_set_unwrap(isl_map_domain(ext
));
113 ext
= isl_map_reverse(isl_map_domain_map(ext
));
114 na
->access
->extension
= new pdg::IslMap(ext
);
116 if (!na
->access
->extended_map
)
117 na
->access
->extended_map
= new pdg::IslMap(isl_map_copy(map
));
121 /* If access "access" contains any nested accesses, then the domain
122 * of the access relation contains extra dimensions corresponding to
123 * the values of the nested accesses.
124 * Add these extra dimensions, with ranges given by the value_bounds
125 * of the corresponding array to domain "dom".
126 * If a nested access array does not have value_bounds, then we assume
127 * an infinite interval.
129 static isl_set
*append_nested_value_domains(isl_set
*dom
, pdg::access
*access
)
133 ctx
= isl_set_get_ctx(dom
);
134 for (int i
= 0; i
< access
->nested
.size(); ++i
) {
135 pdg::call_or_access
*coa
= access
->nested
[i
];
136 assert(coa
->type
== pdg::call_or_access::t_access
);
137 pdg::access
*nested
= coa
->access
;
139 if (nested
->array
->value_bounds
)
140 bounds
= nested
->array
->value_bounds
->get_isl_set(ctx
);
142 isl_space
*dim
= isl_set_get_space(dom
);
143 dim
= isl_space_drop_dims(dim
, isl_dim_set
,
144 0, isl_space_dim(dim
, isl_dim_set
));
145 dim
= isl_space_add_dims(dim
, isl_dim_set
, 1);
146 bounds
= isl_set_universe(dim
);
148 dom
= isl_set_product(dom
, bounds
);
153 /* Combine constraints of the "pure" mapping with the constraints
154 * on the domain. If the range of the mapping is of a dimension
155 * that is lower than the dimension of the accessed array,
156 * we extend the dimension of both domain and range of the mapping
157 * with the missing dimension. The size of domain and range
158 * in these dimensions is set to the extent of the array in the
159 * corresponding missing dimension. Each point in the original
160 * domain is therefore expanded to a hyperrectangle and each point
161 * in this hyperrectangle is mapped onto a single point in the array.
163 * If node->source is a wrapped map, then the iteration domain
164 * is the domain of this map.
166 static isl_map
*convert_access(na_pair
*na
)
168 isl_map
*map
= na
->access
->map
->get_isl_map();
169 isl_set
*dom
= na
->node
->source
->get_isl_set();
171 if (isl_set_is_wrapping(dom
)) {
172 dom
= isl_map_domain(isl_set_unwrap(dom
));
173 dom
= isl_set_coalesce(dom
);
176 dom
= append_nested_value_domains(dom
, na
->access
);
177 if (isl_map_dim(map
, isl_dim_in
) != isl_set_dim(dom
, isl_dim_set
))
179 map
= isl_map_intersect_domain(map
, dom
);
180 if (isl_map_dim(map
, isl_dim_out
) != na
->access
->array
->dims
.size())
181 map
= extend_access(map
, na
);
185 typedef std::map
<na_pair
*, isl_map
*> na_pair2map
;
187 struct add_dep_info
{
191 enum pdg::dependence::type dtype
;
193 na_pair
*read_na_pair
;
194 /* The comparison routine that was used during
195 * the dependence analysis.
197 isl_access_level_before precedes_level
;
198 /* Cache of memory based dependence relations.
199 * The key of the map refers to the write.
202 /* How many loops are shared by the current sink and source?
206 /* For each potential source, what's (up to now) the minimal
207 * and maximal number of shared loops?
208 * If not set, then we don't know yet.
210 std::map
<na_pair
*, int> min_n_shared
;
211 std::map
<na_pair
*, int> max_n_shared
;
213 /* Potential sources that are actually used. */
214 std::set
<na_pair
*> used
;
216 __isl_give isl_map
*get_mem_dep(na_pair
*write_na
);
217 void clear_mem_dep();
218 void set_read_na(na_pair
*read_na
);
219 void update_min_n_shared(na_pair
*source_na
);
224 /* Update min_n_shared of "source_na" to the current number of shared loops.
225 * The new value is always smaller than or equal to the old value (if any).
226 * If max_n_shared hasn't been set yet, then set it as well.
228 void add_dep_info::update_min_n_shared(na_pair
*source_na
)
230 if (max_n_shared
.find(source_na
) == max_n_shared
.end())
231 max_n_shared
[source_na
] = n_shared
;
232 min_n_shared
[source_na
] = n_shared
;
237 * __last_<stmt>_<access_nr>_valid
239 * corresponding to "na", with "na" attached as user pointer.
241 static __isl_give isl_id
*valid_bit_id(isl_ctx
*ctx
, na_pair
*na
)
245 snprintf(name
, sizeof(name
), "__last_%s_%d_valid",
246 na
->node
->name
->s
.c_str(), na
->access
->nr
);
247 return isl_id_alloc(ctx
, name
, na
);
252 * __last_<stmt>_<access_nr>_shared
254 * corresponding to "na", with "na" attached as user pointer.
256 static __isl_give isl_id
*create_shared_id(isl_ctx
*ctx
, na_pair
*na
)
260 snprintf(name
, sizeof(name
), "__last_%s_%d_shared",
261 na
->node
->name
->s
.c_str(), na
->access
->nr
);
262 return isl_id_alloc(ctx
, name
, na
);
265 /* Project out all the dimensions of the given type from "map" except "pos".
267 static __isl_give isl_map
*project_on(__isl_take isl_map
*map
,
268 enum isl_dim_type type
, unsigned pos
)
270 unsigned n
= isl_map_dim(map
, type
);
272 map
= isl_map_project_out(map
, type
, pos
+ 1, n
- (pos
+ 1));
273 map
= isl_map_project_out(map
, type
, 0, pos
);
278 /* Does output dimension "pos" have a fixed value in terms of the
279 * input dimensions (and parameters)?
281 static int has_fixed_value(__isl_keep isl_map
*map
, int pos
)
285 map
= isl_map_copy(map
);
286 map
= project_on(map
, isl_dim_out
, pos
);
287 sv
= isl_map_is_single_valued(map
);
293 /* Return the position of the parameter with the given "id" in "set",
294 * adding it if it wasn't there already.
296 static int find_or_add_param(__isl_keep isl_set
**set
, __isl_take isl_id
*id
)
300 pos
= isl_set_find_dim_by_id(*set
, isl_dim_param
, id
);
306 pos
= isl_set_dim(*set
, isl_dim_param
);
307 *set
= isl_set_add_dims(*set
, isl_dim_param
, 1);
308 *set
= isl_set_set_dim_id(*set
, isl_dim_param
, pos
, id
);
313 /* Add parameters to "set" identifying the last iteration of the access
314 * identified by "na".
316 * In particular, we add a parameter
318 * __last_<stmt>_<access_nr>_shared >= info->n_shared
322 * __last_<stmt>_<access_nr>_<i> = it_<i>
324 * with i ranging over the iterators, starting at info->n_shared,
325 * that are affected by the filters,
326 * except those that have a fixed value according to the memory based
328 * "na" is attached to the first two parameters, so that it can be recovered
329 * in refine_controls(). If the set already references some of these
330 * parameters, then we don't add the parameter again, but instead
331 * simply add the corresponding constraint.
333 static __isl_give isl_set
*add_parametrization(__isl_take isl_set
*set
,
334 na_pair
*na
, add_dep_info
*info
)
343 depth
= na
->node
->get_filter_depth();
344 mem
= info
->get_mem_dep(na
);
345 mem
= isl_map_reverse(mem
);
347 ctx
= isl_set_get_ctx(set
);
348 id
= create_shared_id(ctx
, na
);
349 pos
= find_or_add_param(&set
, id
);
350 set
= isl_set_lower_bound_si(set
, isl_dim_param
, pos
, info
->n_shared
);
352 for (int i
= info
->n_shared
; i
< depth
; ++i
) {
353 if (has_fixed_value(mem
, i
))
356 snprintf(name
, sizeof(name
), "__last_%s_%d_%d",
357 na
->node
->name
->s
.c_str(), na
->access
->nr
, i
);
359 id
= isl_id_alloc(ctx
, name
, NULL
);
360 pos
= find_or_add_param(&set
, id
);
362 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, i
);
369 /* Is the i-th parameter of "map" a control, i.e., a parameter
370 * introduced by add_parametrization()?
371 * In particular, is the parameter of the form __last_*?
373 static bool is_control(__isl_keep isl_map
*map
, int i
)
376 const char *prefix
= "__last_";
377 size_t prefix_len
= strlen(prefix
);
379 if (!isl_map_has_dim_id(map
, isl_dim_param
, i
))
381 name
= isl_map_get_dim_name(map
, isl_dim_param
, i
);
382 return strncmp(name
, prefix
, prefix_len
) == 0;
385 /* Is the i-th parameter of "set" a control, i.e., a parameter
386 * introduced by add_parametrization()?
387 * In particular, is the parameter of the form __last_*?
389 static bool is_control(__isl_keep isl_set
*set
, int i
)
392 const char *prefix
= "__last_";
393 size_t prefix_len
= strlen(prefix
);
395 if (!isl_set_has_dim_id(set
, isl_dim_param
, i
))
397 name
= isl_set_get_dim_name(set
, isl_dim_param
, i
);
398 return strncmp(name
, prefix
, prefix_len
) == 0;
401 /* Remove all controls that are redundant, i.e., that do not appear
402 * in any of the constraints.
403 * Set *has_controls to true if there are any controls that are not redundant.
405 static __isl_give isl_map
*remove_redundant_controls(__isl_take isl_map
*dep
,
411 *has_controls
= false;
413 n_param
= isl_map_dim(dep
, isl_dim_param
);
414 for (i
= n_param
- 1; i
>= 0; --i
) {
415 if (!is_control(dep
, i
))
417 if (isl_map_involves_dims(dep
, isl_dim_param
, i
, 1))
418 *has_controls
= true;
420 dep
= isl_map_project_out(dep
, isl_dim_param
, i
, 1);
426 /* Rename controls of "dep" from
428 * __last_<source_stmt>_<source_acc_nr>_*
432 * __last_<source_stmt>_<source_acc_nr>_<sink_stmt>_<sink_acc_nr>_*
434 * "na" represents the sink.
436 static __isl_give isl_map
*rename_controls(__isl_take isl_map
*dep
, na_pair
*na
)
441 const char *underscore
;
443 n_param
= isl_map_dim(dep
, isl_dim_param
);
444 for (int i
= 0; i
< n_param
; ++i
) {
446 if (!is_control(dep
, i
))
448 name
= isl_map_get_dim_name(dep
, isl_dim_param
, i
);
449 underscore
= strrchr(name
, '_');
451 len
= underscore
+ 1 - name
;
452 memcpy(buf
, name
, len
);
453 snprintf(buf
+ len
, sizeof(buf
) - len
, "%s_%d_%s",
454 na
->node
->name
->s
.c_str(), na
->access
->nr
, name
+ len
);
455 dep
= isl_map_set_dim_name(dep
, isl_dim_param
, i
, buf
);
462 static isl_stat
extract_dep(__isl_take isl_map
*dep
, int must
,
463 void *dep_user
, void *user
);
466 /* Extract the single dependence relation from the result of
467 * dataflow analyis and assign it to *user.
469 static isl_stat
extract_dep(__isl_take isl_map
*dep
, int must
, void *dep_user
,
472 isl_map
**dep_p
= (isl_map
**) user
;
478 /* Return the memory based dependence relation from write_na
479 * to read_na_pair. If the "projected_map"
480 * fields are not NULL, then use the "projected_map"
481 * instead of the "map" of write_na and this->read_na_pair.
483 __isl_give isl_map
*add_dep_info::get_mem_dep(na_pair
*write_na
)
485 isl_access_info
*acc
;
487 isl_map
*read_map
, *write_map
;
490 if (mem_dep
.find(write_na
) != mem_dep
.end())
491 return isl_map_copy(mem_dep
[write_na
]);
493 if (read_na_pair
->projected_map
)
494 read_map
= isl_map_copy(read_na_pair
->projected_map
);
496 read_map
= isl_map_copy(read_na_pair
->map
);
497 acc
= isl_access_info_alloc(read_map
, read_na_pair
, precedes_level
, 1);
498 if (write_na
->projected_map
)
499 write_map
= isl_map_copy(write_na
->projected_map
);
501 write_map
= isl_map_copy(write_na
->map
);
502 acc
= isl_access_info_add_source(acc
, write_map
, 0, write_na
);
503 deps
= isl_access_info_compute_flow(acc
);
504 isl_flow_foreach(deps
, &extract_dep
, &dep
);
507 mem_dep
[write_na
] = isl_map_copy(dep
);
512 /* Clear the cache of memory based dependence relations.
514 void add_dep_info::clear_mem_dep()
516 na_pair2map::iterator it
;
518 for (it
= mem_dep
.begin(); it
!= mem_dep
.end(); ++it
)
519 isl_map_free(it
->second
);
523 /* Set read_na_pair to read_na.
525 * If the cache of memory based dependence relations contains any
526 * elements then they refer to a different read, so we need to clear
529 * We also clear the set of used potential sources and reset
530 * the data that keeps track of the number of shared loops between
531 * the sink (read_na_pair) and the sources.
533 void add_dep_info::set_read_na(na_pair
*read_na
)
538 min_n_shared
.clear();
539 max_n_shared
.clear();
540 read_na_pair
= read_na
;
543 add_dep_info::~add_dep_info()
548 /* Is the name of parameter "i" of "space" of the form __last_*_suffix?
550 static bool is_last_with_suffix(__isl_keep isl_space
*space
, int i
,
551 const char *suffix
, size_t suffix_len
)
553 const char *prefix
= "__last_";
554 size_t prefix_len
= strlen(prefix
);
558 if (!isl_space_has_dim_id(space
, isl_dim_param
, i
))
560 name
= isl_space_get_dim_name(space
, isl_dim_param
, i
);
561 if (strncmp(name
, prefix
, prefix_len
))
564 return len
> suffix_len
&& !strcmp(name
+ len
- suffix_len
, suffix
);
567 /* Is the name of parameter "i" of "space" of the form __last_*_valid?
568 * In practice, those are the parameters __last_*_valid, created
569 * in add_parametrization().
571 static bool is_valid_bit(__isl_keep isl_space
*space
, int i
)
573 const char *suffix
= "_valid";
575 return is_last_with_suffix(space
, i
, suffix
, strlen(suffix
));
578 /* Is the name of parameter "i" of "space" of the form __last_*_shared?
579 * In practice, those are the parameters __last_*_shared, created
580 * in add_parametrization().
582 static bool is_shared(__isl_keep isl_space
*space
, int i
)
584 const char *suffix
= "_shared";
585 size_t suffix_len
= strlen(suffix
);
587 return is_last_with_suffix(space
, i
, suffix
, strlen(suffix
));
590 /* Assuming "coa" is a (read) access, return the array being
593 static pdg::array
*get_filter_array(pdg::call_or_access
*coa
)
595 assert(coa
->type
== pdg::call_or_access::t_access
);
596 return coa
->access
->array
;
599 /* Compute a map between domain elements (i) of "map1" and range elements
600 * of "map2" (j) such that all the images of i in "map1" map to j through
601 * "map2" and such that there is at least one such image element.
603 * In other words, the result contains those pairs of elements such that
604 * map1(i) \cap map2^-1(j) is non-empty and map1(i) \subseteq map2^-1(j).
606 * Equivalently, compute
608 * (map1 . map2) \setminus
609 * (map1 . ((\range map1 \to \range map2) \setminus map2))
611 * If map1 is single valued, then we can do a simple join.
613 static __isl_give isl_union_map
*join_non_empty_subset(
614 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
616 isl_union_set
*dom
, *ran
;
620 if (isl_union_map_is_single_valued(umap1
))
621 return isl_union_map_apply_range(umap1
, umap2
);
623 res
= isl_union_map_apply_range(isl_union_map_copy(umap1
),
624 isl_union_map_copy(umap2
));
625 dom
= isl_union_map_range(isl_union_map_copy(umap1
));
626 ran
= isl_union_map_range(isl_union_map_copy(umap2
));
627 univ
= isl_union_map_from_domain_and_range(dom
, ran
);
628 umap2
= isl_union_map_subtract(univ
, umap2
);
629 umap1
= isl_union_map_apply_range(umap1
, umap2
);
630 res
= isl_union_map_subtract(res
, umap1
);
635 /* Compute a map between domain elements (i) of "map1" and range elements
636 * of "map2" (j) such that the images of i in "map1" include all those
637 * elements that map to j through "map2" and such that there is
638 * at least one such image element.
640 * In other words, the result contains those pairs of elements such that
641 * map1(i) \cap map2^-1(j) is non-empty and map1(i) \supseteq map2^-1(j).
643 * Equivalently, compute
645 * (map1 . map2) \setminus
646 * (((\domain map1 \to \domain map2) \setminus map1) . map2)
648 * If map1 is single valued, then we can do a simple join.
650 static __isl_give isl_union_map
*join_non_empty_superset(
651 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
653 isl_union_set
*dom
, *ran
;
657 if (isl_union_map_is_single_valued(umap2
))
658 return isl_union_map_apply_range(umap1
, umap2
);
660 res
= isl_union_map_apply_range(isl_union_map_copy(umap1
),
661 isl_union_map_copy(umap2
));
662 dom
= isl_union_map_domain(isl_union_map_copy(umap1
));
663 ran
= isl_union_map_domain(isl_union_map_copy(umap2
));
664 univ
= isl_union_map_from_domain_and_range(dom
, ran
);
665 umap1
= isl_union_map_subtract(univ
, umap1
);
666 umap1
= isl_union_map_apply_range(umap1
, umap2
);
667 res
= isl_union_map_subtract(res
, umap1
);
672 /* Return those elements in the domain of "umap" where "umap" is multi-valued.
674 * In particular, construct a mapping between domain elements of "umap"
675 * and pairs of corresponding image elements.
676 * Remove pairs of identical image elements from the range of this mapping.
677 * The result is a mapping between domain elements and pairs of different
678 * corresponding image elements. The domain of this mapping contains those
679 * domain elements of "umap" with at least two images.
681 static __isl_give isl_union_set
*multi_valued(__isl_keep isl_union_map
*umap
)
683 isl_union_map
*multi
, *id
;
685 multi
= isl_union_map_range_product(isl_union_map_copy(umap
),
686 isl_union_map_copy(umap
));
687 id
= isl_union_map_universe(isl_union_map_copy(multi
));
688 id
= isl_union_set_unwrap(isl_union_map_range(id
));
689 id
= isl_union_set_identity(isl_union_map_domain(id
));
690 multi
= isl_union_map_subtract_range(multi
, isl_union_map_wrap(id
));
691 return isl_union_map_domain(multi
);
694 /* Given two filter access relations, return a mapping between the domain
695 * elements of these access relations such that they access "the same filter".
696 * In particular, any pair of elements in the returned relation
697 * accesses at least one element in common, but if subset1 is set,
698 * then the set of elements accessed by the first is a subset of the
699 * set of elements accessed by the second. Similarly, if subset2 is set,
700 * then the set of elements accessed by the second is a subset of the
701 * set of elements accessed by the first. If both are set, then we further
702 * impose that both should access exactly one element.
703 * "space" is the space in which the result should live.
704 * Although "map1" and "map2" are allowed to have ranges in multiple spaces,
705 * their domains should live in a single space. "space" is the space
706 * of the relation between those two domains.
708 * Call the given maps A and B.
710 * A relation between domains elements of A and B that access at least
711 * one element in common can be obtained as
715 * To ensure that all elements accessed through A form a subset of
716 * the elements accessed through B, we compute join_non_empty_subset(A, B^-1).
718 * Ensuring that all elements accessed through B form a subset of
719 * the elements accessed through A is handled in a similar way.
721 * To remove those iterations that access more that one element,
722 * we compute those parts of the domains where A and B are multi-valued
723 * and subtract them from domain and range of the result.
725 static __isl_give isl_map
*compute_common_filter(__isl_keep isl_union_map
*map1
,
726 bool subset1
, __isl_keep isl_union_map
*map2
, bool subset2
,
727 __isl_keep isl_space
*space
)
729 isl_union_map
*reverse
, *common
;
733 reverse
= isl_union_map_reverse(isl_union_map_copy(map2
));
735 if (subset1
&& !subset2
) {
736 common
= join_non_empty_subset(isl_union_map_copy(map1
),
738 } else if (!subset1
&& subset2
) {
739 common
= join_non_empty_superset(isl_union_map_copy(map1
),
742 common
= isl_union_map_apply_range(isl_union_map_copy(map1
),
744 if (subset1
&& subset2
) {
745 bad
= multi_valued(map1
);
746 common
= isl_union_map_subtract_domain(common
, bad
);
747 bad
= multi_valued(map2
);
748 common
= isl_union_map_subtract_range(common
, bad
);
752 res
= isl_union_map_extract_map(common
, isl_space_copy(space
));
753 isl_union_map_free(common
);
757 /* Assuming "coa" is a (read) access, construct a union map from the domain
758 * of the access relation to the access relations of the corresponding
759 * writes. If we are unable to determine the corresponding writes, then
760 * return a map to the read access relation.
762 static __isl_give isl_union_map
*extract_access_map(pdg::call_or_access
*coa
)
764 assert(coa
->type
== pdg::call_or_access::t_access
);
765 return coa
->access
->extract_access_map();
768 /* Return a set that contains all possible filter values,
769 * where the possible values for a given filter is either as specified
770 * by the value_bounds property of the corresponding array or the universe.
772 static __isl_give isl_set
*compute_filter_bounds(pdg::node
*node
)
777 ctx
= isl_set_get_ctx(node
->source
->set
);
779 bounds
= isl_set_universe(isl_space_set_alloc(ctx
, 0, 0));
780 for (int i
= 0; i
< node
->filters
.size(); ++i
) {
783 pdg::call_or_access
*coa
= node
->filters
[i
];
784 assert(coa
->type
== pdg::call_or_access::t_access
);
785 array
= coa
->access
->array
;
786 if (array
->value_bounds
)
787 bnd
= array
->value_bounds
->get_isl_set();
789 bnd
= isl_set_universe(isl_space_set_alloc(ctx
, 0, 1));
790 bounds
= isl_set_flat_product(bounds
, bnd
);
796 /* Return either the filter values themselves or their complement,
797 * taken with respect to the bounds on the filter values.
799 static __isl_give isl_map
*compute_filter_values(pdg::node
*node
,
806 value
= isl_set_unwrap(node
->source
->get_isl_set());
810 bounds
= compute_filter_bounds(node
);
811 res
= isl_map_from_domain_and_range(
812 isl_map_domain(isl_map_copy(value
)), bounds
);
813 res
= isl_map_subtract(res
, value
);
818 /* Equate the first "n" input and output dimensions of "map"
819 * and return the result.
821 static __isl_give isl_map
*share(__isl_take isl_map
*map
, int n
)
823 for (int i
= 0; i
< n
; ++i
)
824 map
= isl_map_equate(map
, isl_dim_in
, i
, isl_dim_out
, i
);
829 /* Return the set of source iterations of "na" either at the last
830 * iteration (if valid is set) or after the last iteration
831 * (if valid is not set). "id" represents the control variable
832 * corresponding to the number of shared loops (__last_<na>_shared).
834 * In particlar, if valid is set, we return the set
836 * { S[i] : i = __last_<na> and
837 * __last_<na>_shared >= info->n_shared }
839 * If valid is not set, we return the set
841 * { S[i] : (i >> __last_<na> and __last_<na>_shared >= info->n_shared) or
842 * __last_<na>_shared < info->n_shared }
844 * That is, the iterations after the last if there is a last iteration
845 * with at least info->n_shared shared loops
846 * or just any iteration if there is no such last iteration.
848 * The lexicographic order i >> __last_<na> is imposed on the loop iterators
849 * that are affected by any filters.
851 static __isl_give isl_set
*source_iterations(na_pair
*na
,
852 __isl_keep isl_id
*id
, bool valid
, add_dep_info
*info
)
855 isl_set
*set
, *invalid
;
860 space
= isl_set_get_space(na
->node
->source
->set
);
861 space
= isl_space_domain(isl_space_unwrap(space
));
863 set
= isl_set_universe(space
);
864 set
= add_parametrization(set
, na
, info
);
869 depth
= na
->node
->get_filter_depth();
871 space
= isl_space_map_from_set(isl_set_get_space(set
));
872 map_after
= isl_map_lex_lt_first(space
, depth
);
873 map_after
= share(map_after
, info
->n_shared
);
874 map_after
= isl_map_intersect_domain(map_after
, set
);
875 set
= isl_map_range(map_after
);
877 invalid
= isl_set_universe(isl_set_get_space(set
));
878 pos
= isl_set_find_dim_by_id(invalid
, isl_dim_param
, id
);
880 invalid
= isl_set_upper_bound_si(invalid
,
881 isl_dim_param
, pos
, info
->n_shared
- 1);
882 set
= isl_set_union(set
, invalid
);
887 /* Look for a matching between the filters of node1 and those of node2.
888 * That is look for pairs of filters of the two nodes that are "the same".
889 * Return true if any such matching can be found. The correspondence between
890 * the filters is returned in *same_value_p, while the pairs of iterations
891 * where the filters are the same is returned in *same_filter_p.
892 * The first "n_shared" dimensions of these iterations are guaranteed
893 * to be equal to each other.
895 * Two filter accesses are considered "the same" if they access at least
896 * one element in common. Moreover, if valid1 is false then the set
897 * of elements accessed by an element from node1 should be a subset
898 * of the set of elements accessed by the corresponding element from node2.
899 * Similarly for valid2.
901 * We perform a greedy search, checking if two filters could possibly
902 * match given the matchings we have performed before and updating
903 * the matching if it is indeed possible.
905 * Note that this function only computes one of the possibly many matchings.
907 static bool compute_matching(pdg::node
*node1
, bool valid1
,
908 pdg::node
*node2
, bool valid2
, __isl_give isl_map
**same_filter_p
,
909 __isl_give isl_map
**same_value_p
, int n_shared
)
912 isl_space
*space1
, *space2
;
913 isl_map
*same_filter
;
916 space1
= isl_space_unwrap(isl_set_get_space(node1
->source
->set
));
917 space2
= isl_space_unwrap(isl_set_get_space(node2
->source
->set
));
918 space1
= isl_space_product(space1
, space2
);
919 space2
= isl_space_unwrap(isl_space_range(isl_space_copy(space1
)));
920 space1
= isl_space_unwrap(isl_space_domain(space1
));
922 same_filter
= isl_map_universe(isl_space_copy(space1
));
923 same_filter
= share(same_filter
, n_shared
);
924 same_value
= isl_map_universe(space2
);
926 for (int i
= 0; i
< node1
->filters
.size(); ++i
) {
927 isl_union_map
*map_i
;
928 pdg::call_or_access
*filter_i
= node1
->filters
[i
];
929 pdg::array
*array_i
= get_filter_array(filter_i
);
931 map_i
= extract_access_map(node1
->filters
[i
]);
933 for (int j
= 0; j
< node2
->filters
.size(); ++j
) {
934 pdg::call_or_access
*filter_j
;
935 filter_j
= node2
->filters
[j
];
936 pdg::array
*array_j
= get_filter_array(filter_j
);
937 isl_union_map
*map_j
;
938 isl_map
*same_filter_ij
;
940 if (array_i
!= array_j
)
943 map_j
= extract_access_map(node2
->filters
[j
]);
944 same_filter_ij
= compute_common_filter(map_i
, !valid1
,
947 same_filter_ij
= isl_map_intersect(same_filter_ij
,
948 isl_map_copy(same_filter
));
949 if (isl_map_is_empty(same_filter_ij
))
950 isl_map_free(same_filter_ij
);
953 isl_map_free(same_filter
);
954 same_filter
= same_filter_ij
;
955 same_value
= isl_map_equate(same_value
,
959 isl_union_map_free(map_j
);
962 isl_union_map_free(map_i
);
964 isl_space_free(space1
);
967 *same_value_p
= same_value
;
968 *same_filter_p
= same_filter
;
970 isl_map_free(same_value
);
971 isl_map_free(same_filter
);
972 *same_value_p
= NULL
;
973 *same_filter_p
= NULL
;
979 /* Given a set of sink iterations "sink", mappings "map1" and "map2"
980 * from two potential sources to this sink,
981 * the possible filter values "value1" and "value2" at those
982 * potential sources, a relation "same_filter" between the two
983 * potential sources expressing when some filters of the two
984 * potential sources are the same and the correponding matching
985 * "same_value" between the filter values,
986 * remove those elements from the sink that have
987 * corresponding pairs of potential source iterations that should
988 * have the same filter values but do not.
990 * Let us call the sink S, the potential sources A and B and the
991 * corresponding filters F and G.
993 * We start from the mappings A[..] -> S[..] and B[..] -> S[..],
996 * S[..] -> [A[..] -> B[..]]
998 * and intersect the range with the condition "same_filter" on A and B,
999 * resulting in a mapping from sink iterations to pairs of potential
1000 * source iterations that should have the same filter values
1001 * (as specified by "same_value").
1003 * We subtract from the range of this mapping those pairs of
1004 * potential source iterations that actually have the same filter values.
1005 * The result is a mapping from sink iterations to pairs of potential
1006 * source iterations that should have the same filter values but do not.
1008 * The mapping between potential source iterations that have the
1009 * same filter values is obtained by combining the mappings
1010 * A[..] -> F[..] and B[..] -> G[..] into
1012 * [A[..] -> B[..]] -> [F[..] -> G[..]]
1014 * intersecting the range with "same_value" and then computing the domain.
1016 static __isl_give isl_set
*remove_conflict(__isl_take isl_set
*sink
,
1017 __isl_take isl_map
*map1
, __isl_take isl_map
*value1
,
1018 __isl_take isl_map
*map2
, __isl_take isl_map
*value2
,
1019 __isl_take isl_map
*same_filter
, __isl_take isl_map
*same_value
)
1021 isl_map
*value
, *conflict
;
1022 isl_set
*conflict_set
;
1024 conflict
= isl_map_domain_product(map1
, map2
);
1025 conflict
= isl_map_reverse(conflict
);
1027 conflict
= isl_map_intersect_range(conflict
, isl_map_wrap(same_filter
));
1029 value
= isl_map_product(value1
, value2
);
1030 value
= isl_map_intersect_range(value
, isl_map_wrap(same_value
));
1031 conflict
= isl_map_subtract_range(conflict
, isl_map_domain(value
));
1033 conflict_set
= isl_map_domain(conflict
);
1035 sink
= isl_set_subtract(sink
, conflict_set
);
1040 /* Remove inconsistencies from the set of sink iterations "sink"
1041 * based on two potential sources identified by "id1" and "id2"
1042 * (representing the number of shared loops),
1043 * in particular, on either the last iteration where the filters hold
1044 * (if valid? is set) or on later iterations (if valid? is not set).
1046 * Let us first consider the case where both "valid1" and "valid2" are set.
1047 * If the last iterations of the corresponding sources access the same
1048 * filters, then these filters should have the same value.
1049 * If a filter access accesses more than one element, then these elements
1050 * should all have the same value. It is therefore sufficient for the
1051 * two last iterations to access at least one element in common for there
1052 * to be a requirement that the corresponding values should be the same.
1053 * We therefore obtain the filter values, the mappings from the sink
1054 * to the last iterations, a matching between the
1055 * the filters of the corresponding sources and remove conflicts from "dep".
1057 * If one or both of the valid bits are not set, then we need to make
1058 * some changes. First the inconsistencies now do not arise from
1059 * the filter values at the last iteration, but from the filter values
1060 * lying _outside_ of the possible values for all iterations _after_
1061 * the "last" (i.e., the last iteration satisfying the filter constraints).
1062 * In case there is no last iteration with at least info->n_shared shared loops,
1063 * then the filter values should lie outside of the possible values
1064 * for any potential source iteration with info->n_shared shared loops.
1065 * Note however, that if the filter access relation accesses several
1066 * elements, then it is sufficient for one of those to have a value
1067 * outside of the possible values. We can therefore only consider
1068 * any inconsistencies for those cases where the set of accessed elements
1069 * forms a subset of the set of accessed elements through the other potential
1070 * source. If valid1 is not set, but valid2 is set, then we consider
1071 * those pairs of potential source iterations where the first accesses
1072 * a subset of the second and we impose that at least one of those
1073 * accessed elements has a valid outside the possible values.
1074 * Since those accessed elements form a subset of the elements accessed
1075 * by the other potential source, there is at least one element that
1076 * has a value outside of the posssible values on the first potential source
1077 * and a value belonging to the posssible values on the second potential source.
1078 * We can therefore impose that this value should exist.
1080 * If both valid1 and valid2 are not set, then we can only
1081 * impose a constraint on those pairs of iterations that access the same
1082 * single element. We then know that the value of this single element
1083 * accessed by both potential sources should lie outside of the possible
1084 * values on both sides.
1086 static __isl_give isl_set
*remove_inconsistencies(__isl_take isl_set
*sink
,
1087 add_dep_info
*info
, __isl_keep isl_id
*id1
, bool valid1
,
1088 __isl_keep isl_id
*id2
, bool valid2
)
1090 na_pair
*write1_na
, *write2_na
;
1092 isl_map
*value1
, *value2
;
1093 isl_map
*same_filter
;
1094 isl_map
*same_value
;
1095 isl_map
*mem1
, *mem2
;
1097 write1_na
= (na_pair
*) isl_id_get_user(id1
);
1098 write2_na
= (na_pair
*) isl_id_get_user(id2
);
1100 if (!compute_matching(write1_na
->node
, valid1
, write2_na
->node
, valid2
,
1101 &same_filter
, &same_value
, info
->n_shared
))
1104 value1
= compute_filter_values(write1_na
->node
, !valid1
);
1105 value2
= compute_filter_values(write2_na
->node
, !valid2
);
1107 mem1
= info
->get_mem_dep(write1_na
);
1108 source
= source_iterations(write1_na
, id1
, valid1
, info
);
1109 mem1
= share(mem1
, info
->n_shared
);
1110 mem1
= isl_map_intersect_domain(mem1
, source
);
1112 mem2
= info
->get_mem_dep(write2_na
);
1113 source
= source_iterations(write2_na
, id2
, valid2
, info
);
1114 mem2
= share(mem2
, info
->n_shared
);
1115 mem2
= isl_map_intersect_domain(mem2
, source
);
1117 sink
= remove_conflict(sink
, mem1
, value1
, mem2
, value2
,
1118 same_filter
, same_value
);
1123 /* Remove inconsistencies from the set of sink iterations "sink"
1124 * based on two potential sources identified by "id1" and "id2",
1125 * (representing the number of shared loops).
1127 static __isl_give isl_set
*remove_inconsistencies(__isl_take isl_set
*sink
,
1128 add_dep_info
*info
, __isl_keep isl_id
*id1
, __isl_keep isl_id
*id2
)
1130 sink
= remove_inconsistencies(sink
, info
, id1
, false, id2
, false);
1131 sink
= remove_inconsistencies(sink
, info
, id1
, false, id2
, true);
1132 sink
= remove_inconsistencies(sink
, info
, id1
, true, id2
, false);
1133 sink
= remove_inconsistencies(sink
, info
, id1
, true, id2
, true);
1137 /* Remove inconsistencies from the set of sink iterations "sink"
1138 * based on the potential source identified by "id"
1139 * (representing the number of shared loops),
1140 * in particular, on either the last iteration where the filters hold
1141 * (if valid is set) or on later iterations (if valid is not set).
1143 * This function is very similar to the remove_inconsistencies
1144 * function above that considers two potential sources instead
1145 * of the sink and one potential source. The main differences
1146 * are that for the sink, the filters always hold and that the mapping
1147 * from sink iterations to sink iterations is computed in a different
1148 * (and fairly trivial) way.
1150 static __isl_give isl_set
*remove_inconsistencies(__isl_take isl_set
*sink
,
1151 add_dep_info
*info
, __isl_keep isl_id
*id
, bool valid
)
1153 na_pair
*read_na
, *write_na
;
1155 isl_map
*value1
, *value2
;
1156 isl_map
*same_filter
;
1157 isl_map
*same_value
;
1158 isl_map
*id_map
, *mem
;
1160 read_na
= info
->read_na_pair
;
1161 write_na
= (na_pair
*) isl_id_get_user(id
);
1163 if (!compute_matching(read_na
->node
, true, write_na
->node
, valid
,
1164 &same_filter
, &same_value
, info
->n_shared
))
1167 value1
= compute_filter_values(read_na
->node
, false);
1168 value2
= compute_filter_values(write_na
->node
, !valid
);
1170 id_map
= isl_set_identity(isl_set_copy(sink
));
1172 mem
= info
->get_mem_dep(write_na
);
1173 after
= source_iterations(write_na
, id
, valid
, info
);
1174 mem
= share(mem
, info
->n_shared
);
1175 mem
= isl_map_intersect_domain(mem
, after
);
1177 sink
= remove_conflict(sink
, id_map
, value1
, mem
, value2
,
1178 same_filter
, same_value
);
1183 /* Remove inconsistencies from the set of sink iterations "sink"
1184 * based on the potential source identified by "id"
1185 * (representing the number of shared loops).
1187 static __isl_give isl_set
*remove_inconsistencies(__isl_take isl_set
*sink
,
1188 add_dep_info
*info
, __isl_keep isl_id
*id
)
1190 sink
= remove_inconsistencies(sink
, info
, id
, false);
1191 sink
= remove_inconsistencies(sink
, info
, id
, true);
1195 /* Remove all parameters that were introduced by add_parametrization().
1197 static __isl_give isl_map
*remove_all_controls(__isl_take isl_map
*dep
)
1203 n_param
= isl_map_dim(dep
, isl_dim_param
);
1204 for (i
= n_param
- 1; i
>= 0; --i
) {
1205 if (!is_control(dep
, i
))
1207 dep
= isl_map_project_out(dep
, isl_dim_param
, i
, 1);
1209 dep
= isl_map_coalesce(dep
);
1214 /* Remove all parameters that were introduced by add_parametrization().
1216 static __isl_give isl_set
*remove_all_controls(__isl_take isl_set
*set
)
1222 n_param
= isl_set_dim(set
, isl_dim_param
);
1223 for (i
= n_param
- 1; i
>= 0; --i
) {
1224 if (!is_control(set
, i
))
1226 set
= isl_set_project_out(set
, isl_dim_param
, i
, 1);
1228 set
= isl_set_coalesce(set
);
1235 * { a -> b : (shared >= max and
1236 * the first max iterators are equal) or
1237 * (shared = max - 1 and
1238 * the first max - 1 iterators are equal and
1239 * dimension max - 1 of a is smaller than that of b) or
1242 * the first min iterators are equal and
1243 * dimension min of a is smaller than that of b) }
1245 static __isl_give isl_map
*compute_shared_map(__isl_take isl_space
*space
,
1246 __isl_keep isl_id
*shared_id
, int min
, int max
)
1248 isl_map
*shared_map
;
1251 shared_pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, shared_id
);
1252 shared_map
= isl_map_universe(isl_space_copy(space
));
1253 shared_map
= isl_map_lower_bound_si(shared_map
, isl_dim_param
,
1255 shared_map
= share(shared_map
, max
);
1257 for (int i
= min
; i
< max
; ++i
) {
1258 isl_map
*shared_map_i
;
1260 shared_map_i
= isl_map_universe(isl_space_copy(space
));
1261 shared_map_i
= isl_map_fix_si(shared_map_i
, isl_dim_param
,
1263 shared_map_i
= share(shared_map_i
, i
);
1264 shared_map_i
= isl_map_order_lt(shared_map_i
, isl_dim_in
, i
,
1267 shared_map
= isl_map_union(shared_map
, shared_map_i
);
1270 isl_space_free(space
);
1275 /* Different parts of the final dependence relations may have been
1276 * created at different depths and may therefore have a different
1277 * number of dimensions of the last iterator. The __last_<na>_shared
1278 * value determines how many of the dimensions are implicitly equal
1279 * to those of the sink iteration. This function creates a set that
1280 * makes these equalities explicit, so that we can later remove
1281 * the __last_<na>_shared parameter. It also marks those parts
1282 * that have a number of shared iterators that is smaller than the minimum
1283 * as not having any last iteration.
1285 * In particular, we create a set in the space of the sink of the form
1287 * { s : __last_<na>_valid = 0 or
1288 * (__last_<na>_valid = 1 and
1289 * __last_<na>_<i> is a potential source iteration and
1290 * ((__last_<na>_shared >= max_shared and
1291 * the first max_shared iterators are equal) or
1292 * (__last_<na>_shared = max_shared - 1 and
1293 * the first max_shared - 1 iterators are equal and
1294 * iterator max_shared - 1 of the source is smaller) or
1296 * (__last_<na>_shared = min_shared and
1297 * the first min_shared iterators are equal and
1298 * iterator min_shared of the source is smaller))) }
1300 * That is, for those parts with __last_<na>_shared smaller than
1301 * max_n_shared[source_na], intersection with the set will introduce
1302 * __last_<na>_<i> parameters (assuming they don't have a known fixed value)
1303 * up until __last_<na>_shared and equate them to the corresponding iterators
1306 static __isl_give isl_set
*shared_refinement(__isl_keep isl_id
*shared_id
,
1313 isl_map
*shared_map
;
1314 int valid_pos
, shared_pos
;
1320 ctx
= isl_id_get_ctx(shared_id
);
1322 source_na
= (na_pair
*) isl_id_get_user(shared_id
);
1323 valid_id
= valid_bit_id(ctx
, source_na
);
1325 mem
= info
->get_mem_dep(source_na
);
1326 space
= isl_map_get_space(mem
);
1329 shared_pos
= isl_space_dim(space
, isl_dim_param
);
1330 space
= isl_space_add_dims(space
, isl_dim_param
, 1);
1331 space
= isl_space_set_dim_id(space
, isl_dim_param
,
1332 shared_pos
, isl_id_copy(shared_id
));
1334 shared_map
= compute_shared_map(isl_space_copy(space
), shared_id
,
1335 info
->min_n_shared
[source_na
], info
->max_n_shared
[source_na
]);
1337 domain
= isl_set_universe(isl_space_domain(space
));
1338 valid_pos
= find_or_add_param(&domain
, isl_id_copy(valid_id
));
1339 domain
= isl_set_fix_si(domain
, isl_dim_param
, valid_pos
, 1);
1340 domain
= add_parametrization(domain
, source_na
, info
);
1341 shared_map
= isl_map_intersect_domain(shared_map
, domain
);
1343 valid
= isl_map_range(shared_map
);
1344 invalid
= isl_set_universe(isl_set_get_space(valid
));
1345 shared_pos
= isl_set_find_dim_by_id(invalid
, isl_dim_param
, shared_id
);
1346 invalid
= isl_set_upper_bound_si(invalid
, isl_dim_param
, shared_pos
,
1347 info
->n_shared
- 1);
1349 valid_pos
= find_or_add_param(&invalid
, valid_id
);
1350 invalid
= isl_set_fix_si(invalid
, isl_dim_param
, valid_pos
, 0);
1352 return isl_set_union(valid
, invalid
);
1355 /* Different parts of the final dependence relations may have been
1356 * created at different depths and may therefore have a different
1357 * number of dimensions of the last iterator. The __last_*_shared
1358 * value determines how many of the dimensions are implicitly equal
1359 * to those of the sink iteration.
1361 * For each of the __last_*_shared parameters, explicitly add
1362 * the implicitly equal __last_*_i iterators by intersecting
1363 * the sink with the set computed by shared_refinement.
1364 * Finally, remove the __last_*_shared parameters.
1366 static __isl_give isl_map
*refine_shared(__isl_take isl_map
*dep
,
1370 isl_set
*refinement
;
1373 space
= isl_map_get_space(dep
);
1374 n_param
= isl_space_dim(space
, isl_dim_param
);
1376 refinement
= isl_set_universe(isl_space_range(isl_space_copy(space
)));
1378 for (int i
= 0; i
< n_param
; ++i
) {
1382 if (!is_shared(space
, i
))
1384 if (!isl_map_involves_dims(dep
, isl_dim_param
, i
, 1))
1387 id
= isl_space_get_dim_id(space
, isl_dim_param
, i
);
1388 ref_i
= shared_refinement(id
, info
);
1391 if (isl_set_is_wrapping(refinement
)) {
1392 isl_map
*map
= isl_set_unwrap(refinement
);
1393 map
= isl_map_intersect_domain(map
, ref_i
);
1394 refinement
= isl_map_wrap(map
);
1396 refinement
= isl_set_intersect(refinement
, ref_i
);
1398 dep
= isl_map_intersect_range(dep
, refinement
);
1400 isl_space_free(space
);
1402 space
= isl_map_get_space(dep
);
1403 n_param
= isl_space_dim(space
, isl_dim_param
);
1404 for (int i
= n_param
- 1; i
>= 0; --i
) {
1405 if (!is_shared(space
, i
))
1407 dep
= isl_map_project_out(dep
, isl_dim_param
, i
, 1);
1409 isl_space_free(space
);
1411 dep
= isl_map_coalesce(dep
);
1416 /* Compute the gist of "dep" with respect to the fact that
1418 * 0 <= __last_*_valid <= 1
1420 static __isl_give isl_map
*gist_valid(__isl_take isl_map
*dep
)
1426 space
= isl_space_params(isl_map_get_space(dep
));
1427 n_param
= isl_space_dim(space
, isl_dim_param
);
1429 valid
= isl_set_universe(isl_space_copy(space
));
1430 for (int i
= 0; i
< n_param
; ++i
) {
1431 if (!is_valid_bit(space
, i
))
1434 valid
= isl_set_lower_bound_si(valid
, isl_dim_param
, i
, 0);
1435 valid
= isl_set_upper_bound_si(valid
, isl_dim_param
, i
, 1);
1438 isl_space_free(space
);
1440 dep
= isl_map_gist_params(dep
, valid
);
1445 /* Simplify the constraints on the parameters introduced
1446 * in add_parametrization().
1447 * We first add __last_*_i iterators that are only implicitly referred
1448 * to through the __last_*_shared parameters.
1449 * Then, we remove all those parameters that turn out not be needed.
1450 * If there are any of those parameters left, then we compute the gist
1451 * with respect to the valid bit being either 0 or 1 and rename
1452 * the parameters to also include a reference to the sink.
1453 * The resulting relation is assigned to controlled_relation,
1454 * while the relation field is assigned the result of projecting out
1455 * all those parameters.
1457 static __isl_give isl_map
*simplify_controls(__isl_take isl_map
*dep
,
1458 add_dep_info
*info
, bool *has_controls
)
1464 dep
= refine_shared(dep
, info
);
1465 dep
= remove_redundant_controls(dep
, has_controls
);
1466 if (*has_controls
) {
1467 dep
= gist_valid(dep
);
1468 dep
= rename_controls(dep
, info
->read_na_pair
);
1474 /* Extract a single dependence from the result of dataflow analysis.
1476 * We first simplify the constraints on the parameters introduced
1477 * in add_parametrization().
1479 * If the dependence relation turns out to be empty, we simply return.
1480 * Otherwise, we create a corresponding pdg::dependence and keep track
1481 * of the fact that the potential source is actually used
1482 * so that we can remove any reference to potential sources that are
1483 * never used from the dependence relations.
1485 static isl_stat
add_dep(__isl_take isl_map
*dep
, int must
, void *dep_user
,
1489 na_pair
*write_na
= (na_pair
*) dep_user
;
1490 add_dep_info
*info
= (struct add_dep_info
*)user
;
1491 isl_ctx
*ctx
= info
->pdg
->get_isl_ctx();
1494 dep
= isl_map_coalesce(dep
);
1495 dep
= simplify_controls(dep
, info
, &has_controls
);
1496 if (isl_map_is_empty(dep
)) {
1501 info
->used
.insert(write_na
);
1503 d
= new pdg::dependence
;
1505 d
->type
= info
->dtype
;
1506 d
->from
= write_na
->node
;
1507 d
->to
= info
->read_na_pair
->node
;
1508 d
->from_access
= write_na
->access
;
1509 d
->to_access
= info
->read_na_pair
->access
;
1511 if (d
->from_access
->extension
|| d
->to_access
->extension
)
1512 d
->extended_relation
=
1513 new pdg::IslMap(remove_all_controls(isl_map_copy(dep
)));
1514 if (d
->from_access
->extension
)
1515 dep
= isl_map_apply_domain(dep
,
1517 d
->from_access
->extension
->get_isl_map(ctx
)));
1518 if (d
->to_access
->extension
)
1519 dep
= isl_map_apply_range(dep
,
1521 d
->to_access
->extension
->get_isl_map(ctx
)));
1523 d
->controlled_relation
= new pdg::IslMap(isl_map_copy(dep
));
1524 d
->relation
= new pdg::IslMap(remove_all_controls(isl_map_copy(dep
)));
1525 info
->pdg
->dependences
.push_back(d
);
1530 /* This structure represents a set of filter index expressions
1531 * along with bounds on the correponding filter values.
1532 * The number of output dimensions in "value" is the same as
1533 * the number of elements in the "index" vector.
1536 std::vector
<isl_union_map
*> index
;
1540 /* Construct a da_filter object representing the filters in
1541 * na->node and na->access.
1543 static struct da_filter
*extract_filter(na_pair
*na
)
1545 da_filter
*filter
= new da_filter
;
1547 pdg::node
*node
= na
->node
;
1548 pdg::access
*access
= na
->access
;
1550 domain
= node
->source
->get_isl_set();
1551 if (isl_set_is_wrapping(domain
))
1552 filter
->value
= isl_set_unwrap(domain
);
1554 filter
->value
= isl_map_from_domain(domain
);
1556 for (int i
= 0; i
< node
->filters
.size(); ++i
)
1557 filter
->index
.push_back(extract_access_map(node
->filters
[i
]));
1559 if (access
->nested
.size() == 0)
1562 domain
= isl_map_domain(access
->map
->get_isl_map());
1563 filter
->value
= isl_map_flat_range_product(filter
->value
,
1564 isl_set_unwrap(domain
));
1566 for (int i
= 0; i
< access
->nested
.size(); ++i
)
1567 filter
->index
.push_back(extract_access_map(access
->nested
[i
]));
1572 static struct da_filter
*da_filter_free(struct da_filter
*filter
)
1576 for (int i
= 0; i
< filter
->index
.size(); ++i
)
1577 isl_union_map_free(filter
->index
[i
]);
1578 isl_map_free(filter
->value
);
1583 static void da_filter_dump(struct da_filter
*filter
)
1590 p
= isl_printer_to_file(isl_map_get_ctx(filter
->value
), stderr
);
1591 p
= isl_printer_start_line(p
);
1592 p
= isl_printer_print_str(p
, "value(");
1593 for (int i
= 0; i
< filter
->index
.size(); ++i
) {
1595 p
= isl_printer_print_str(p
, ", ");
1596 p
= isl_printer_print_union_map(p
, filter
->index
[i
]);
1598 p
= isl_printer_print_str(p
, ") in ");
1599 p
= isl_printer_print_map(p
, filter
->value
);
1600 p
= isl_printer_end_line(p
);
1602 isl_printer_free(p
);
1605 /* Look for a filter index expression in "filter" that is identical
1606 * to "index". Return the index of this index expression if it is
1607 * found and the number of elements in filter->index otherwise.
1609 static int da_filter_find_exact_match(struct da_filter
*filter
,
1610 __isl_keep isl_union_map
*index
)
1612 for (int i
= 0; i
< filter
->index
.size(); ++i
) {
1615 equal
= isl_union_map_is_equal(filter
->index
[i
], index
);
1622 return filter
->index
.size();
1625 /* Add the index expression "index" to "filter" with an unconstrained
1626 * filter value. To ease debugging we set the name of the new filter
1627 * value dimension to that of the array being accessed by "index".
1628 * Although the range of "index" is allowed to live in more than
1629 * one space, we assume that they are all wrapped maps to the same
1632 static struct da_filter
*da_filter_add(struct da_filter
*filter
,
1633 __isl_take isl_union_map
*index
)
1636 isl_union_map
*univ
;
1639 if (!filter
|| !index
)
1642 filter
->value
= isl_map_add_dims(filter
->value
, isl_dim_out
, 1);
1643 univ
= isl_union_map_universe(isl_union_map_copy(index
));
1644 univ
= isl_union_set_unwrap(isl_union_map_range(univ
));
1645 set
= isl_set_from_union_set(isl_union_map_range(univ
));
1646 id
= isl_set_get_tuple_id(set
);
1648 filter
->value
= isl_map_set_dim_id(filter
->value
, isl_dim_out
,
1649 filter
->index
.size(), id
);
1652 filter
->index
.push_back(index
);
1656 isl_union_map_free(index
);
1657 da_filter_free(filter
);
1661 /* Intersect the set of possible filter values in "filter" with "value".
1663 static struct da_filter
*da_filter_restrict(struct da_filter
*filter
,
1664 __isl_take isl_map
*value
)
1666 if (!filter
|| !value
)
1669 filter
->value
= isl_map_intersect(filter
->value
, value
);
1671 return da_filter_free(filter
);
1675 isl_map_free(value
);
1676 da_filter_free(filter
);
1680 /* Does "map" represent a total filter on "domain", i.e., one that is defined
1681 * on every element of "domain"?
1683 * Although the range of "map" may live in different spaces, we assume
1684 * that the domain of "map" lives in a single space.
1686 static int is_total_filter(__isl_keep isl_union_map
*map
,
1687 __isl_keep isl_set
*domain
)
1689 isl_union_set
*map_domain
;
1693 map_domain
= isl_union_map_domain(isl_union_map_copy(map
));
1694 set
= isl_set_from_union_set(map_domain
);
1695 total
= isl_set_is_subset(domain
, set
);
1701 /* Does "node" have only total filters on "domain"?
1703 static int all_total_filters(pdg::node
*node
, __isl_take isl_set
*domain
)
1707 for (int i
= 0; i
< node
->filters
.size(); ++i
) {
1708 isl_union_map
*map
= extract_access_map(node
->filters
[i
]);
1710 total
= is_total_filter(map
, domain
);
1711 isl_union_map_free(map
);
1717 isl_set_free(domain
);
1721 /* Does "node" have only total filters on the domain of "map"?
1723 static bool filters_total_on_domain(pdg::node
*node
, __isl_keep isl_map
*map
)
1725 return all_total_filters(node
, isl_map_domain(isl_map_copy(map
)));
1728 /* Does "node" have only total filters on the range of "map"?
1730 static bool filters_total_on_range(pdg::node
*node
, __isl_keep isl_map
*map
)
1732 return all_total_filters(node
, isl_map_range(isl_map_copy(map
)));
1735 /* Are the filters of "source_node" total on the range of "source_map"
1736 * and those of "sink_node" (if any) total on the domain of "soruce_map"?
1738 static bool total_filters(pdg::node
*source_node
, pdg::node
*sink_node
,
1739 __isl_keep isl_map
*source_map
)
1743 total
= filters_total_on_range(source_node
, source_map
);
1747 if (!isl_set_is_wrapping(sink_node
->source
->set
))
1750 total
= filters_total_on_domain(sink_node
, source_map
);
1757 /* Does "node" have any single valued filters?
1759 static int any_single_valued_filter(pdg::node
*node
)
1761 for (int i
= 0; i
< node
->filters
.size(); ++i
) {
1762 isl_union_map
*map
= extract_access_map(node
->filters
[i
]);
1765 sv
= isl_union_map_is_single_valued(map
);
1766 isl_union_map_free(map
);
1775 /* Construct a mapping from the source iterations to all source filter values
1776 * that allow some corresponding sink iteration(s) (according to "source_map")
1777 * to be executed. In other words, if any sink iteration is executed,
1778 * then we know that the filters of the corresponding source iterations
1779 * satisfy the returned relation.
1781 * The "filter" input represents what is known about the filter
1782 * values at the sink.
1784 * The "source" domain of the source is a wrapped map,
1785 * mapping iteration vectors to filter values.
1786 * We first construct a relation between the sink filter values (available
1787 * in "filter") and the source filter values. The purpose of this relation
1788 * is to find as much information about the source filter values
1789 * as possible. We can start with the value bounds on the arrays
1790 * accessed in the filters as they always hold.
1791 * Next, we loop over the source filters and check whether there
1792 * is any sink filter that covers the source filter.
1793 * In particular, for each source filter, we construct a map
1794 * from the source iteration domain to a wrapped access relation,
1795 * representing the write access relation that corresponds to
1796 * the filter read access. Note that if we were unable to determine this
1797 * write access, then the mapping returned by extract_access_map
1798 * maps to the original read access, which will not match with
1799 * any filter access relations of the sink.
1800 * We combine the constructed map with the proto-dependence
1801 * (source_map) to obtain a mapping from sink iterations to
1802 * access relations such that there is some source iteration that
1803 * may be the source of the given sink iteration (based on source_map)
1804 * and that the filter value at this source iteration was written by
1805 * that access. If the result is a subset of the mapping from the
1806 * sink iterations to the corresponding write access relation for some filter,
1807 * then we know that any constraint on this filter value also applies
1808 * to the source filter value. We therefore introduce an equality
1809 * in our mapping from sink filter values to source filter values.
1811 * When we apply the mapping from sink filter values to source filter values
1812 * to the mapping from source iterations to sink filter values, we
1813 * obtain a mapping from sink iterations to source filter values
1814 * that represents what we know about the source filter values.
1815 * That is, for each sink iteration in the domain of this map, if this
1816 * sink iteration is executed, then the actual source filter values
1817 * are an element of the image of the sink iteration.
1818 * In other words, the sink iteration is only executed if the source
1819 * filter values are an element of the image.
1820 * Mapping this relation back to the source through the proto-dependence
1821 * source_map, we obtain a relation from source iterations to all source
1822 * filter values for which any sink iteration is executed.
1823 * In particular, for values of the filters outside this relation,
1824 * no corresponding (according to source_map) sink is executed.
1826 static __isl_give isl_map
*known_filter_values(struct da_filter
*filter
,
1827 na_pair
*source_na
, __isl_keep isl_map
*source_map
)
1829 isl_space
*space
, *space2
;
1830 isl_map
*source_value
, *sink_value
;
1831 isl_map
*filter_map
;
1834 sink_value
= isl_map_copy(filter
->value
);
1836 space
= isl_set_get_space(source_na
->node
->source
->set
);
1837 space
= isl_space_range(isl_space_unwrap(space
));
1838 space2
= isl_space_range(isl_map_get_space(sink_value
));
1839 space
= isl_space_align_params(space
, isl_space_copy(space2
));
1840 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
1841 space
= isl_space_map_from_domain_and_range(space2
, space
);
1842 filter_map
= isl_map_universe(space
);
1844 bounds
= compute_filter_bounds(source_na
->node
);
1845 filter_map
= isl_map_intersect_range(filter_map
, bounds
);
1847 for (int i
= 0; i
< source_na
->node
->filters
.size(); ++i
) {
1848 isl_union_map
*map_i
;
1849 map_i
= extract_access_map(source_na
->node
->filters
[i
]);
1850 map_i
= isl_union_map_apply_range(
1851 isl_union_map_from_map(isl_map_copy(source_map
)), map_i
);
1852 for (int j
= 0; j
< filter
->index
.size(); ++j
) {
1853 if (isl_union_map_is_subset(map_i
, filter
->index
[j
]))
1854 filter_map
= isl_map_equate(filter_map
,
1855 isl_dim_in
, j
, isl_dim_out
, i
);
1857 isl_union_map_free(map_i
);
1860 source_value
= isl_map_apply_range(sink_value
, filter_map
);
1861 source_value
= isl_map_apply_domain(source_value
,
1862 isl_map_copy(source_map
));
1863 source_value
= isl_map_coalesce(source_value
);
1865 return source_value
;
1868 /* Add a new output dimension to "map" with constraints that are the
1869 * same as those on output dimension "pos".
1871 * Given map { [i] -> [j] }, we first and an extra dimension,
1875 * extract out j_pos,
1877 * { [[i] -> [j_0,...,j_{pos-1},*,j_{pos+1},...,*]] -> [j_pos] }
1881 * { [[i] -> [j_0,...,j_{pos-1},*,j_{pos+1},...,*]] -> [j_pos,j_pos'] }
1883 * and then move the dimensions back
1885 * { [i] -> [j,j_pos'] }
1887 static __isl_give isl_map
*copy_dim(__isl_take isl_map
*map
, int pos
)
1891 pos_new
= isl_map_dim(map
, isl_dim_out
);
1892 pos
+= isl_map_dim(map
, isl_dim_in
);
1893 pos_new
+= isl_map_dim(map
, isl_dim_in
);
1894 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
1895 map
= isl_map_from_domain(isl_map_wrap(map
));
1896 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
1897 map
= isl_map_equate(map
, isl_dim_in
, pos
, isl_dim_out
, 0);
1898 map
= isl_map_eliminate(map
, isl_dim_in
, pos
, 1);
1899 map
= isl_map_range_product(map
, isl_map_copy(map
));
1900 map
= isl_map_equate(map
, isl_dim_in
, pos
, isl_dim_out
, 0);
1901 map
= isl_map_equate(map
, isl_dim_in
, pos_new
, isl_dim_out
, 1);
1902 map
= isl_set_unwrap(isl_map_domain(map
));
1907 /* Given constraints on the filter values "filter" at the sink iterations
1908 * "sink", derive additional constraints from the filter values of source
1909 * node "source_na". In particular, consider the iterations of "source_na"
1910 * that have _not_ been executed based on the constraints of the corresponding
1911 * last iteration parameters in "sink" and what this implies about the
1912 * filter values at those iterations.
1914 * Essentially, we consider all pairs of sink iterations and filter
1915 * elements, together with the corresponding non-executed source iterations
1916 * and the possible values of those filters. We universally quantify
1917 * the non-executed source iterations so that we obtain the intersection
1918 * of the constraints on the filter values over all those source iterations
1919 * and then existentially quantify the filter elements to obtain constraints
1920 * that are valid for all filter elements.
1922 * In more details, the computation is performed as follows.
1924 * Compute a mapping from potential last iterations of the other source
1925 * to sink iterations, taking into account the contraints on
1926 * the last executed iterations encoded in the parameters of "sink",
1927 * but projecting out all parameters encoding last iterations from the result.
1928 * Include all earlier iterations of the other source, resulting in
1929 * a mapping with a domain that includes all potential iterations of the
1932 * Subtract these iterations from all possible iterations of the other
1933 * source for a given sink iteration, resulting in a mapping from
1934 * potential source iterations that are definitely not executed
1935 * to the corresponding sink iteration.
1937 * If this map is empty, then this means we can't find any iterations
1938 * of the other source that are certainly not executed and then we
1939 * can't derive any further information.
1940 * Similarly, if the filters of source_na->node are not total
1941 * on the set of non-executed iterations, then we cannot draw any conclusions.
1942 * (Note that we already tested that the filters on sink->node are total
1943 * on the domain of "source_map". By intersecting the set of corresponding
1944 * sink iterations with this domain, we ensure that this property also holds
1945 * on those sink iterations.)
1946 * Otherwise, keep track of those sink iterations without any corresponding
1947 * non-executed other source iterations. We will lose these sink iterations
1948 * in subsequent computations, so we need to add them back in at the end.
1950 * Compute bounds on the filter values at the non executed iterations
1951 * based on what we know about filters at the sink and the fact that
1952 * the iterations are not executed, meaning that the filter values
1953 * do not satisfy the constraints that allow the iteration to be executed.
1954 * The result is a mapping T -> V.
1956 * Note that we only know that there is some accessed filter element
1957 * that does not satisfy the constraints that allow the iteration to be
1958 * executed. We therefore project out those dimensions that correspond
1959 * to filters with an access relation that is not single-valued, i.e.,
1960 * one that may access more than one element for some iterations.
1961 * If there are no single-valued filters, then we can skip the rest of
1964 * Construct a mapping [K -> F] -> T, with K the sink iterations,
1965 * T the corresponding non-executed iterations of the other source and
1966 * F the filters accessed at those iterations.
1968 * Combine the above two mappings into a mapping [K -> F] -> V
1969 * such that the set of possible filter values (V) is the intersection
1970 * over all iterations of the other source that access the filter,
1971 * and such that there is at least one such iteration.
1972 * In other words, ensure that the range of [K -> F] -> T
1973 * is a non-empty subset of the range of V -> T.
1974 * We require a non-empty subset to ensure that the domain of
1975 * [K -> F] -> V is equal to the result of composing K -> T with T -> F.
1976 * Projecting out F from [K -> F] -> V, we obtain a map K -> V that is
1977 * the union of all possible values of the filters K -> F, i.e.,
1978 * the constraints that the values of K -> F satisfy.
1979 * If we didn't impose non-emptiness above, then the domain of [K -> F] -> V
1980 * would also include pairs that we are not interested in, related to
1981 * arbitrary values. Projecting out F would then also lead to arbitrary
1984 * Compose the mapping K -> T with the index expressions, pulling them
1986 * For each of these pulled back index expressions, we check if it
1987 * is equal to one of the sink filter index expressions. If not, we
1988 * add it to the sink filter index expressions.
1989 * In both cases, we keep track of the fact that this sink filter
1990 * should have a value that satisfies the constraints in K -> V.
1991 * We further check if there is any sink filter index expression
1992 * that is a (strict) subset of the pulled back index expression.
1993 * The value of any such sink filter should also satisfy those
1994 * constraints, so we duplicate the filter value in K -> V.
1996 * Finally, we intersect the possible filter values with the constraints
1997 * obtained above on the affected sink iterations and a universe range
1998 * on the unaffected sink iterations.
2000 static struct da_filter
*include_other_source(struct da_filter
*filter
,
2001 __isl_keep isl_map
*source_map
, __isl_keep isl_set
*sink
,
2002 na_pair
*source_na
, add_dep_info
*info
)
2004 isl_space
*space
, *space2
;
2005 isl_set
*source_domain
;
2007 isl_map
*may_run
, *not_run
;
2008 isl_map
*source_value
;
2009 isl_union_map
*usource
, *umap
;
2010 std::vector
<isl_union_map
*> index
;
2011 isl_union_map
*index_product
;
2013 isl_map
*filter_map
;
2014 isl_set
*unaffected_sink
;
2015 isl_map
*unaffected_value
;
2017 if (source_na
->node
->filters
.size() == 0)
2020 space
= isl_set_get_space(source_na
->node
->source
->set
);
2021 space
= isl_space_domain(isl_space_unwrap(space
));
2022 source_domain
= isl_set_universe(space
);
2023 source_domain
= add_parametrization(source_domain
, source_na
, info
);
2024 may_run
= isl_map_from_domain_and_range(source_domain
,
2025 isl_set_copy(sink
));
2026 may_run
= share(may_run
, info
->n_shared
);
2028 may_run
= remove_all_controls(may_run
);
2029 mem
= info
->get_mem_dep(source_na
);
2030 mem
= share(mem
, info
->n_shared
);
2031 mem
= isl_map_intersect_range(mem
,
2032 isl_map_domain(remove_all_controls(isl_map_copy(source_map
))));
2033 space
= isl_space_domain(isl_map_get_space(may_run
));
2034 may_run
= isl_map_apply_domain(may_run
, isl_map_lex_ge(space
));
2035 not_run
= isl_map_subtract(mem
, may_run
);
2037 if (isl_map_is_empty(not_run
) ||
2038 !any_single_valued_filter(source_na
->node
) ||
2039 !filters_total_on_domain(source_na
->node
, not_run
)) {
2040 isl_map_free(not_run
);
2044 not_run
= isl_map_reverse(not_run
);
2045 source_value
= known_filter_values(filter
, source_na
, not_run
);
2046 source_value
= isl_map_subtract(source_value
,
2047 isl_set_unwrap(source_na
->node
->source
->get_isl_set()));
2048 unaffected_sink
= isl_set_copy(sink
);
2049 unaffected_sink
= remove_all_controls(unaffected_sink
);
2050 unaffected_sink
= isl_set_subtract(unaffected_sink
,
2051 isl_map_domain(isl_map_copy(not_run
)));
2053 for (int i
= 0; i
< source_na
->node
->filters
.size(); ++i
) {
2057 map
= extract_access_map(source_na
->node
->filters
[i
]);
2058 sv
= isl_union_map_is_single_valued(map
);
2060 index
.push_back(map
);
2062 isl_union_map_free(map
);
2063 source_value
= isl_map_project_out(source_value
,
2064 isl_dim_out
, index
.size(), 1);
2068 index_product
= isl_union_map_copy(index
[0]);
2069 for (int i
= 1; i
< index
.size(); ++i
)
2070 index_product
= isl_union_map_range_product(index_product
,
2071 isl_union_map_copy(index
[1]));
2072 index_product
= isl_union_map_reverse(index_product
);
2073 index_product
= isl_union_map_domain_product(
2074 isl_union_map_from_map(isl_map_copy(not_run
)), index_product
);
2076 usource
= isl_union_map_from_map(source_value
);
2077 usource
= join_non_empty_subset(index_product
, usource
);
2078 umap
= isl_union_map_universe(isl_union_map_copy(usource
));
2079 umap
= isl_union_set_unwrap(isl_union_map_domain(umap
));
2080 umap
= isl_union_map_domain_map(umap
);
2081 usource
= isl_union_map_apply_domain(usource
, umap
);
2082 source_value
= isl_map_from_union_map(usource
);
2084 for (int i
= 0; i
< index
.size(); ++i
)
2085 index
[i
] = isl_union_map_apply_range(
2086 isl_union_map_from_map(isl_map_copy(not_run
)), index
[i
]);
2088 space
= isl_space_range(isl_map_get_space(source_value
));
2089 space2
= isl_space_range(isl_map_get_space(filter
->value
));
2090 space
= isl_space_align_params(space
, isl_space_copy(space2
));
2091 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
2092 space
= isl_space_map_from_domain_and_range(space
, space2
);
2093 filter_map
= isl_map_universe(space
);
2095 for (int i
= 0; i
< index
.size(); ++i
) {
2096 int exact
= da_filter_find_exact_match(filter
, index
[i
]);
2098 if (exact
== filter
->index
.size()) {
2099 filter
= da_filter_add(filter
,
2100 isl_union_map_copy(index
[i
]));
2101 filter_map
= isl_map_add_dims(filter_map
,
2104 filter_map
= isl_map_equate(filter_map
, isl_dim_in
, i
,
2105 isl_dim_out
, exact
);
2106 for (int j
= 0; j
< filter
->index
.size(); ++j
) {
2111 if (!isl_union_map_is_subset(filter
->index
[j
],
2114 pos
= isl_map_dim(source_value
, isl_dim_out
);
2115 source_value
= copy_dim(source_value
, i
);
2116 filter_map
= isl_map_add_dims(filter_map
,
2118 filter_map
= isl_map_equate(filter_map
, isl_dim_in
, pos
,
2123 source_value
= isl_map_apply_range(source_value
, filter_map
);
2124 source_value
= isl_map_coalesce(source_value
);
2125 unaffected_value
= isl_map_from_domain(unaffected_sink
);
2126 unaffected_value
= isl_map_add_dims(unaffected_value
, isl_dim_out
,
2127 filter
->index
.size());
2128 source_value
= isl_map_union(source_value
, unaffected_value
);
2129 filter
= da_filter_restrict(filter
, source_value
);
2131 for (int i
= 0; i
< index
.size(); ++i
)
2132 isl_union_map_free(index
[i
]);
2133 isl_map_free(not_run
);
2138 /* Given constraints on the filter values "filter" at the sink iterations
2139 * "sink", derive additional constraints from the filter values of those
2140 * source nodes for which "sink" contains a reference to its last iteration,
2141 * for use in determining whether parametrization is needed on "source_map".
2142 * In particular, we try and derive extra information from the fact that
2143 * some iterations of those source nodes have _not_ been executed.
2145 static struct da_filter
*include_other_sources(struct da_filter
*filter
,
2146 __isl_keep isl_map
*source_map
, __isl_keep isl_set
*sink
,
2152 space
= isl_set_get_space(sink
);
2153 n_param
= isl_space_dim(space
, isl_dim_param
);
2154 for (int i
= 0; i
< n_param
; ++i
) {
2158 if (!is_shared(space
, i
))
2161 id
= isl_space_get_dim_id(space
, isl_dim_param
, i
);
2162 na
= (na_pair
*) isl_id_get_user(id
);
2165 filter
= include_other_source(filter
, source_map
, sink
,
2168 isl_space_free(space
);
2173 #define RESTRICT_ERROR -1
2174 #define RESTRICT_NO 0
2175 #define RESTRICT_EMPTY 1
2176 #define RESTRICT_INPUT 2
2177 #define RESTRICT_OUTPUT 3
2179 /* Given a map from sinks to potential sources (source_map)
2180 * and the set of sink iterations (sink),
2181 * check if any parametrization is needed on the sources.
2182 * That is, check whether the possible filter values at the sink
2183 * imply that the filter values at the source are always valid.
2184 * If so, the source is executed whenever the sink is executed
2185 * and no parametrization is required.
2187 * Return RESTRICT_NO if no parametrization is required.
2188 * Return RESTRICT_INPUT if parametrization is required on the input
2189 * of the computation of the last iteration.
2190 * Return RESTRICT_OUTPUT if parametrization is required on the output
2191 * of the computation of the last iteration. This means that we know
2192 * that the source will be executed, but we want to introduce a parameter
2193 * to represent the last iteration anyway, because the knowledge depends
2194 * on the parameters representing last iterations of other nodes.
2195 * Return RESTRICT_EMPTY if the potential sources cannot possibly
2196 * be executed, assuming that the sink is executed.
2198 * If there are no filters on the source, then obviously the source
2199 * is always executed.
2201 * If the filters of the sink and the source are not all total
2202 * on domain and range of "source_map", then we cannot draw any conclusion.
2203 * In principle, we could split up "source_map" according to whether
2204 * the filters would be total on the domain and range.
2206 * We first construct a mapping from source iterations to source filter
2207 * values that allow some corresponding sink iteration(s) (according to
2208 * "source_map") to be executed.
2209 * If this relation is a subset of the actual mapping from iteration
2210 * vectors to filter values at the source, then we know that a corresponding
2211 * sink is only executed when the source is executed and no parametrization
2212 * is required. However, we postpone the decision until we have considered
2213 * the other potential sources below.
2214 * If, on the other hand, the constructed relation is disjoint
2215 * from the source filter relation, then the sources cannot have
2216 * executed if the sink is executed. If so, we return
2217 * RESTRICT_EMPTY immediately.
2219 * Otherwise, we check if we can find out more information by considering
2220 * information derived from knowledge about the last iterations of other
2221 * nodes. If, by considering this extract information, we can find
2222 * that the potential source is never executed (given that the sink
2223 * is executed), then we return RESTRICT_EMPTY.
2224 * Otherwise, if we had already determined that the relation based
2225 * on only the sink is a subset of the filter values, then we return
2226 * RESTRICT_NO. If we can only draw this conclusion when taking into
2227 * account the other potential sources, then we return RESTRICT_OUTPUT.
2228 * Otherwise, we return RESTRICT_INPUT.
2230 static int need_parametrization(__isl_keep isl_map
*source_map
,
2231 __isl_keep isl_set
*sink
, na_pair
*source_na
, add_dep_info
*info
)
2233 bool filtered_source
;
2235 isl_map
*source_value
, *sink_value
;
2237 na_pair
*sink_na
= info
->read_na_pair
;
2240 if (isl_map_plain_is_empty(source_map
) ||
2241 isl_set_plain_is_empty(sink
))
2244 filtered_source
= isl_set_is_wrapping(source_na
->node
->source
->set
);
2246 if (!filtered_source
)
2249 if (!total_filters(source_na
->node
, sink_na
->node
, source_map
))
2250 return RESTRICT_INPUT
;
2252 filter
= extract_filter(sink_na
);
2254 if (filter
->index
.size() != 0) {
2255 sink_value
= known_filter_values(filter
, source_na
, source_map
);
2256 source_value
= isl_set_unwrap(
2257 source_na
->node
->source
->get_isl_set());
2259 if (isl_map_is_disjoint(sink_value
, source_value
))
2260 res
= RESTRICT_EMPTY
;
2261 else if (isl_map_is_subset(sink_value
, source_value
))
2263 isl_map_free(source_value
);
2264 isl_map_free(sink_value
);
2267 if (res
== RESTRICT_EMPTY
) {
2268 da_filter_free(filter
);
2272 filter
= include_other_sources(filter
, source_map
, sink
, info
);
2275 return RESTRICT_ERROR
;
2276 if (filter
->index
.size() == 0) {
2277 da_filter_free(filter
);
2278 return RESTRICT_INPUT
;
2281 sink_value
= known_filter_values(filter
, source_na
, source_map
);
2282 source_value
= isl_set_unwrap(source_na
->node
->source
->get_isl_set());
2284 if (isl_map_is_disjoint(sink_value
, source_value
))
2285 res
= RESTRICT_EMPTY
;
2286 else if (res
== RESTRICT_NO
)
2288 else if (isl_map_is_subset(sink_value
, source_value
))
2289 res
= RESTRICT_OUTPUT
;
2291 res
= RESTRICT_INPUT
;
2293 isl_map_free(source_value
);
2294 isl_map_free(sink_value
);
2295 da_filter_free(filter
);
2301 static __isl_give isl_restriction
*do_restrict(
2302 __isl_keep isl_map
*source_map
, __isl_keep isl_set
*sink
,
2303 void *source_user
, void *user
);
2306 /* Add parameters corresponding to the last iteration of "na" to "set"
2307 * (assuming they don't already appear in "set")
2308 * and add constraints to them to express that there either is no
2309 * last iteration with info->n_shared shared loops
2310 * (__last_<na>_shared < info->n_shared) or that there is a last
2311 * iteration with at least info->n_shared shared loops
2312 * (__last_<na>_shared >= info->n_shared) and that the iteration is a possible
2313 * source of the current sink (based on the memory dependence between
2314 * the source and the sink).
2316 static __isl_give isl_set
*set_parameter_bounds(__isl_take isl_set
*set
,
2317 na_pair
*na
, add_dep_info
*info
)
2326 id
= create_shared_id(isl_set_get_ctx(set
), na
);
2327 shared_pos
= find_or_add_param(&set
, id
);
2329 valid
= isl_set_copy(set
);
2332 valid
= isl_set_lower_bound_si(valid
, isl_dim_param
, shared_pos
,
2335 mem
= info
->get_mem_dep(na
);
2336 domain
= isl_set_universe(isl_space_domain(isl_map_get_space(mem
)));
2337 domain
= add_parametrization(domain
, na
, info
);
2338 mem
= isl_map_intersect_domain(mem
, domain
);
2339 valid
= isl_set_intersect(valid
, isl_map_range(mem
));
2341 invalid
= isl_set_upper_bound_si(invalid
, isl_dim_param
, shared_pos
,
2342 info
->n_shared
- 1);
2344 return isl_set_union(valid
, invalid
);
2347 /* Check if there are any iterations of "source_na" in "source_map"
2348 * that are definitely executed, based solely on the possible filter values.
2349 * If so, add constraints to "sink" to indicate that the last execution
2350 * cannot be earlier than those definitely executed iterations.
2352 * We first compute the set of source iterations that are definitely
2353 * executed because there are no filter values that would prohibit
2354 * their execution. If there are no such source iterations then we are done.
2356 * Then we construct a map from sink iterations to associated (through
2357 * "source_map") definitely executed source iterations.
2359 * For those sink iterations that have a corresponding definitely
2360 * executed source iteration, we add constraints that express that
2361 * this last definitely executed source iteration is lexicographically
2362 * smaller than or equal to the last executed source iteration
2363 * (and that there definitely is a last executed source iteration).
2365 static __isl_give isl_set
*mark_definite_source(__isl_take isl_set
*sink
,
2366 add_dep_info
*info
, na_pair
*source_na
, __isl_keep isl_map
*source_map
)
2371 isl_map
*definite_source_map
;
2372 isl_set
*with_source
;
2376 dom
= source_na
->node
->source
->get_isl_set();
2377 dom
= isl_map_domain(isl_set_unwrap(dom
));
2378 invalid
= compute_filter_values(source_na
->node
, true);
2379 dom
= isl_set_subtract(dom
, isl_map_domain(invalid
));
2381 if (isl_set_is_empty(dom
)) {
2386 space
= isl_set_get_space(dom
);
2388 definite_source_map
= isl_map_copy(source_map
);
2389 definite_source_map
= isl_map_intersect_range(source_map
, dom
);
2391 dom
= isl_map_domain(isl_map_copy(definite_source_map
));
2393 with_source
= isl_set_copy(sink
);
2394 with_source
= isl_set_intersect(with_source
, isl_set_copy(dom
));
2395 sink
= isl_set_subtract(sink
, dom
);
2397 dom
= isl_set_universe(isl_space_copy(space
));
2398 dom
= add_parametrization(dom
, source_na
, info
);
2400 depth
= source_na
->node
->get_filter_depth();
2402 space
= isl_space_map_from_set(space
);
2403 map_after
= isl_map_lex_ge_first(space
, depth
);
2404 dom
= isl_set_apply(dom
, map_after
);
2406 definite_source_map
= isl_map_intersect_range(definite_source_map
, dom
);
2408 dom
= isl_map_domain(definite_source_map
);
2409 with_source
= isl_set_intersect(with_source
, dom
);
2411 sink
= isl_set_union(sink
, with_source
);
2416 /* Remove inconsistencies from the set of sink iterations "sink"
2417 * based on the current potential source "source_na" and other
2418 * potential sources referenced by "sink".
2420 * We first identify those iterations of "source_na" that are
2421 * definitely executed based solely on the possible filter values.
2423 * If the sink has filters, then we remove inconsistencies based
2424 * on the sink and the current potential source.
2426 * Finally, we go through the references to other potential sources
2427 * in "sink" and remove inconsistencies based on this other potential
2428 * source and the current potential source.
2430 static __isl_give isl_set
*remove_inconsistencies(__isl_take isl_set
*sink
,
2431 add_dep_info
*info
, na_pair
*source_na
, __isl_keep isl_map
*source_map
)
2437 source_id
= create_shared_id(isl_map_get_ctx(source_map
), source_na
);
2439 sink
= mark_definite_source(sink
, info
, source_na
, source_map
);
2441 if (isl_set_is_wrapping(info
->read_na_pair
->node
->source
->set
))
2442 sink
= remove_inconsistencies(sink
, info
, source_id
);
2444 space
= isl_set_get_space(sink
);
2445 n_param
= isl_space_dim(space
, isl_dim_param
);
2446 for (int i
= 0; i
< n_param
; ++i
) {
2449 if (!is_shared(space
, i
))
2451 other_id
= isl_space_get_dim_id(space
, isl_dim_param
, i
);
2453 if (other_id
!= source_id
) {
2456 other_na
= (na_pair
*) isl_id_get_user(other_id
);
2457 sink
= set_parameter_bounds(sink
, other_na
, info
);
2458 sink
= remove_inconsistencies(sink
, info
, other_id
,
2462 isl_id_free(other_id
);
2464 isl_space_free(space
);
2466 isl_id_free(source_id
);
2470 /* Compute a restriction for the given sink.
2471 * That is, add constraints to the parameters expressing
2472 * that the source is either not executed with info->n_shared shared
2473 * iterators (*_shared < info->n_shared)
2474 * or it is executed (*_shared >= info->n_shared) and then the last iteration
2475 * satisfies the corresponding memory based dependence.
2477 * Only do this for that part of "sink" that has any corresponding
2478 * sources in "source_map". The remaining part of "sink" is not affected.
2480 * Note that the "sink" set may have undergone a refinement based
2481 * on the _shared parameters and we want this refinement to also
2482 * be present in the sink restriction. We therefore need
2483 * to intersect the affected part with "sink".
2485 static __isl_give isl_set
*compute_sink_restriction(
2486 __isl_keep isl_map
*source_map
, __isl_keep isl_set
*sink
,
2487 na_pair
*source_na
, add_dep_info
*info
)
2489 isl_set
*with_source
, *without_source
;
2491 with_source
= isl_map_domain(isl_map_copy(source_map
));
2492 without_source
= isl_set_subtract(isl_set_copy(sink
),
2493 isl_set_copy(with_source
));
2494 with_source
= isl_set_intersect(with_source
, isl_set_copy(sink
));
2496 with_source
= set_parameter_bounds(with_source
, source_na
, info
);
2497 with_source
= remove_inconsistencies(with_source
, info
, source_na
,
2500 return isl_set_union(with_source
, without_source
);
2503 /* How many loops do "node1" and "node2" share?
2505 static int max_shared(pdg::node
*node1
, pdg::node
*node2
)
2508 int size
= node1
->prefix
.size();
2510 if (node2
->prefix
.size() < size
)
2511 size
= node2
->prefix
.size();
2513 for (int i
= 0; i
< size
&& node1
->prefix
[i
] == node2
->prefix
[i
]; ++i
)
2514 if (node1
->prefix
[i
] == -1)
2520 /* Determine the number of shared loop iterators between sink
2521 * and source domains in "sink2source". That is, find out how
2522 * many of the initial input and output dimensions are equal
2523 * to each other. Return the result.
2525 * The first time this function is called for a given sink access
2526 * (info->n_shared is set to -1 in add_dep_info::set_read_na),
2527 * we check for equal dimensions up to the shared nesting depth.
2528 * Later call check dimensions up to the result of the previous call.
2530 static int extract_shared_levels(__isl_keep isl_map
*sink2source
,
2531 na_pair
*source_na
, add_dep_info
*info
)
2537 max
= info
->n_shared
;
2539 max
= max_shared(source_na
->node
, info
->read_na_pair
->node
);
2542 return info
->n_shared
= 0;
2544 space
= isl_map_get_space(sink2source
);
2545 for (i
= 0; i
< max
; ++i
) {
2549 test
= isl_map_universe(isl_space_copy(space
));
2550 test
= isl_map_equate(test
, isl_dim_in
, i
, isl_dim_out
, i
);
2551 subset
= isl_map_is_subset(sink2source
, test
);
2557 isl_space_free(space
);
2562 /* The last iteration referred to by the sink may have been added
2563 * at a different nesting level. This means that __last_<na>_shared
2564 * is greater than or equal to a value greater than info->n_shared
2565 * and that therefore the iterators between info->n_shared and
2566 * __last_<na>_shared are not represented as they are implicitly
2567 * considered to be equal to the corresponding sink iterator.
2568 * For consistency, we need to explicitly add those iterators
2569 * and set them to be equal to the corresponding sink iterator.
2571 * In particular, we create a set in the space of the sink of the form
2573 * { s : __last_<na>_shared < info->n_shared or
2574 * (__last_<na>_<i> is a potential source iteration for s and
2575 * (__last_<na>_shared >= max_shared or
2576 * (__last_<na>_shared = max_shared - 1 and
2577 * the first max_shared - 1 iterators are equal and
2578 * iterator max_shared - 1 of the source is smaller) or
2580 * (__last_<na>_shared = info->n_shared and
2581 * the first n_shared iterators are equal and
2582 * iterator n_shared of the source is smaller))) }
2584 * That is, for those parts with __last_<na>_shared smaller than
2585 * max_n_shared[source_na], intersection with the set will introduce
2586 * __last_<na>_<i> parameters (assuming they don't have a known fixed value)
2587 * up until __last_<na>_shared and equate them to the corresponding iterators
2590 static __isl_give isl_set
*internal_shared_refinement(
2591 __isl_keep isl_id
*shared_id
, add_dep_info
*info
)
2596 isl_map
*shared_map
;
2601 source_na
= (na_pair
*) isl_id_get_user(shared_id
);
2603 mem
= info
->get_mem_dep(source_na
);
2604 domain
= isl_set_universe(isl_space_domain(isl_map_get_space(mem
)));
2605 domain
= add_parametrization(domain
, source_na
, info
);
2606 mem
= isl_map_intersect_domain(mem
, domain
);
2608 shared_map
= compute_shared_map(isl_map_get_space(mem
), shared_id
,
2609 info
->n_shared
, info
->max_n_shared
[source_na
]);
2611 mem
= isl_map_intersect(mem
, shared_map
);
2613 valid
= isl_map_range(mem
);
2614 invalid
= isl_set_universe(isl_set_get_space(valid
));
2615 shared_pos
= isl_set_find_dim_by_id(invalid
, isl_dim_param
, shared_id
);
2616 invalid
= isl_set_upper_bound_si(invalid
, isl_dim_param
, shared_pos
,
2617 info
->n_shared
- 1);
2619 return isl_set_union(valid
, invalid
);
2622 /* The last iteration of some source referred to by the sink may have been
2623 * added at a different nesting level. This means that __last_*_shared
2624 * is greater than or equal to a value greater than info->n_shared
2625 * and that therefore the iterators between info->n_shared and
2626 * __last_*_shared are not represented as they are implicitly
2627 * considered to be equal to the corresponding sink iterator.
2628 * For consistency, we need to explicitly add those iterators
2629 * and set them to be equal to the corresponding sink iterator.
2631 * For each of the __last_*_shared parameters, explicitly add
2632 * the implicitly equal __last_*_i iterators by intersecting
2633 * the sink with the set computed by internal_shared_refinement.
2635 static __isl_give isl_set
*refine_shared_internal(__isl_take isl_set
*sink
,
2639 isl_set
*refinement
;
2642 space
= isl_set_get_space(sink
);
2643 refinement
= isl_set_universe(isl_space_copy(space
));
2645 n_param
= isl_space_dim(space
, isl_dim_param
);
2646 for (int i
= 0; i
< n_param
; ++i
) {
2650 if (!is_shared(space
, i
))
2653 id
= isl_space_get_dim_id(space
, isl_dim_param
, i
);
2654 ref_i
= internal_shared_refinement(id
, info
);
2656 refinement
= isl_set_intersect(refinement
, ref_i
);
2658 isl_space_free(space
);
2660 sink
= isl_set_intersect(sink
, refinement
);
2665 /* Given a map from sinks to potential sources (sink2source),
2666 * check if any parametrization is needed.
2667 * Depending on the result, return either a universe restriction,
2668 * an empty restriction (if the sources cannot have executed),
2669 * a restriction that parametrizes the source and the sink
2670 * of the input of the computation of the last source
2671 * or a restriction that parametrizes the source of the output.
2673 * sink_map maps the domain of sink2source to the sink iteration domain.
2674 * source_map maps the range of sink2source to the source iteration domain.
2676 * Before we check if we need any parametrization, we update the number of
2677 * shared loop levels and add possibly missing __last_*_i iterators
2678 * (in refine_shared_internal). If parametrization turns out to be required,
2679 * we also update the minimal number of shared loop levels for the given
2682 static __isl_give isl_restriction
*compute_restriction_core(
2683 __isl_keep isl_map
*sink2source
,
2684 __isl_take isl_map
*sink_map
, __isl_take isl_map
*source_map
,
2685 __isl_keep isl_set
*sink
, na_pair
*source_na
, add_dep_info
*info
)
2688 isl_set
*source_restr
;
2689 isl_set
*sink_restr
;
2692 sink2source
= isl_map_copy(sink2source
);
2693 sink
= isl_set_copy(sink
);
2695 sink2source
= isl_map_apply_range(sink2source
,
2696 isl_map_copy(source_map
));
2697 sink2source
= isl_map_apply_domain(sink2source
,
2698 isl_map_copy(sink_map
));
2699 sink
= isl_set_apply(sink
, isl_map_copy(sink_map
));
2701 info
->n_shared
= extract_shared_levels(sink2source
, source_na
, info
);
2702 sink
= refine_shared_internal(sink
, info
);
2704 need
= need_parametrization(sink2source
, sink
, source_na
, info
);
2705 if (need
== RESTRICT_ERROR
||
2706 need
== RESTRICT_NO
|| need
== RESTRICT_EMPTY
) {
2707 isl_map_free(source_map
);
2708 isl_map_free(sink_map
);
2710 if (need
== RESTRICT_ERROR
) {
2711 isl_map_free(sink2source
);
2713 } else if (need
== RESTRICT_NO
)
2714 return isl_restriction_none(sink2source
);
2716 return isl_restriction_empty(sink2source
);
2719 info
->update_min_n_shared(source_na
);
2721 space
= isl_map_get_space(source_map
);
2722 source_restr
= isl_set_universe(isl_space_range(space
));
2724 source_restr
= add_parametrization(source_restr
, source_na
, info
);
2725 source_restr
= isl_set_apply(source_restr
, isl_map_reverse(source_map
));
2727 if (need
== RESTRICT_OUTPUT
) {
2728 isl_map_free(sink_map
);
2730 isl_map_free(sink2source
);
2731 return isl_restriction_output(source_restr
);
2734 sink_restr
= compute_sink_restriction(sink2source
, sink
,
2736 sink_restr
= isl_set_apply(sink_restr
, isl_map_reverse(sink_map
));
2739 isl_map_free(sink2source
);
2741 return isl_restriction_input(source_restr
, sink_restr
);
2744 /* Compute a restriction for the given map from sinks to potential sources
2747 * First check if the sink access has any filters. If so, compose the original
2748 * sink_map with a mapping that projects out these access filters.
2749 * Handle the source access similarly.
2750 * Then call compute_restriction_core to perform the main computation.
2752 static __isl_give isl_restriction
*compute_restriction(
2753 __isl_keep isl_map
*sink2source
,
2754 __isl_take isl_map
*sink_map
, __isl_take isl_map
*source_map
,
2755 __isl_keep isl_set
*sink
, na_pair
*source_na
, add_dep_info
*info
)
2757 na_pair
*sink_na
= info
->read_na_pair
;
2759 if (sink_na
->access
->nested
.size() > 0) {
2763 space
= isl_space_range(isl_map_get_space(sink_map
));
2764 space
= isl_space_unwrap(space
);
2765 map
= isl_map_domain_map(isl_map_universe(space
));
2767 sink_map
= isl_map_apply_range(sink_map
, map
);
2770 if (source_na
->access
->nested
.size() > 0) {
2774 space
= isl_space_range(isl_map_get_space(source_map
));
2775 space
= isl_space_unwrap(space
);
2776 map
= isl_map_domain_map(isl_map_universe(space
));
2778 source_map
= isl_map_apply_range(source_map
, map
);
2781 return compute_restriction_core(sink2source
, sink_map
, source_map
,
2782 sink
, source_na
, info
);
2785 /* Compute a restriction for the given map from sinks to potential sources
2786 * (sink2source). We simply call compute_restriction to compute the
2787 * restriction. Since, unlike the case of do_restrict_domain_map bewloe,
2788 * we didn't encode the entire access relation in the domains of the input
2789 * to isl_access_info_compute_flow, we pass identity mappings on the source
2790 * and sink to compute_restriction.
2792 static __isl_give isl_restriction
*do_restrict(__isl_keep isl_map
*sink2source
,
2793 __isl_keep isl_set
*sink
, void *source_user
, void *user
)
2795 na_pair
*source_na
= (na_pair
*) source_user
;
2796 add_dep_info
*info
= (struct add_dep_info
*) user
;
2798 isl_map
*source_map
;
2801 space
= isl_space_domain(isl_map_get_space(sink2source
));
2802 sink_map
= isl_map_identity(isl_space_map_from_set(space
));
2803 space
= isl_space_range(isl_map_get_space(sink2source
));
2804 source_map
= isl_map_identity(isl_space_map_from_set(space
));
2806 return compute_restriction(sink2source
, sink_map
, source_map
,
2807 sink
, source_na
, info
);
2810 /* Does the iteration domain of any of the writes involve any filters?
2812 static bool any_filters(vector
<na_pair
> &writers
)
2814 for (int i
= 0; i
< writers
.size(); ++i
)
2815 if (isl_set_is_wrapping(writers
[i
].node
->source
->set
))
2820 /* Does any of the dependences starting at "first" have
2821 * controlled dependence relation?
2823 static bool any_controlled_dependences(PDG
*pdg
, int first
)
2825 for (int i
= first
; i
< pdg
->dependences
.size(); ++i
)
2826 if (pdg
->dependences
[i
]->controlled_relation
)
2832 /* Remove parameters from "map" that start with "prefix".
2834 static __isl_give isl_map
*remove_source(__isl_take isl_map
*map
,
2837 int n
= isl_map_dim(map
, isl_dim_param
);
2838 size_t len
= strlen(prefix
);
2840 for (int i
= n
- 1; i
>= 0; --i
) {
2843 name
= isl_map_get_dim_name(map
, isl_dim_param
, i
);
2844 if (strncmp(name
, prefix
, len
))
2847 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
2850 map
= isl_map_coalesce(map
);
2855 /* Remove parameters from the dependences starting at "first"
2856 * that refer to any of the unused potential sources, i.e.,
2857 * those potential sources that are in writers, but not in info->used.
2859 * Since the current sink does not depend on those unused potential sources,
2860 * the corresponding dependence relations cannot depend on them and
2861 * any reference to them can simply be projected out.
2863 static void remove_unused_sources(PDG
*pdg
, int first
,
2864 vector
<na_pair
> &writers
, add_dep_info
*info
)
2867 std::vector
<na_pair
*> unused
;
2869 for (int i
= 0; i
< writers
.size(); ++i
) {
2870 if (info
->used
.find(&writers
[i
]) == info
->used
.end())
2871 unused
.push_back(&writers
[i
]);
2874 if (unused
.size() == 0)
2876 if (!any_controlled_dependences(pdg
, first
))
2879 for (int i
= 0; i
< unused
.size(); ++i
) {
2880 na_pair
*na
= unused
[i
];
2882 snprintf(name
, sizeof(name
), "__last_%s_%d_",
2883 na
->node
->name
->s
.c_str(), na
->access
->nr
);
2885 for (int j
= first
; j
< pdg
->dependences
.size(); ++j
) {
2886 pdg::dependence
*dep
= pdg
->dependences
[j
];
2889 if (!dep
->controlled_relation
)
2892 map
= dep
->controlled_relation
->map
;
2893 map
= remove_source(map
, name
);
2894 dep
->controlled_relation
->map
= map
;
2899 /* Look for the unique write access that writes to the array accessed
2900 * by "a" and return an na_pair consisting of the node in which the
2901 * access is performed and the access itself.
2903 static na_pair
find_unique_source(pdg::PDG
* pdg
, pdg::array
*a
)
2905 for (int i
= 0; i
< pdg
->nodes
.size(); ++i
) {
2906 pdg::node
*node
= pdg
->nodes
[i
];
2907 pdg::statement
*s
= pdg
->nodes
[i
]->statement
;
2908 for (int j
= 0; j
< s
->accesses
.size(); ++j
) {
2909 pdg::access
*access
= s
->accesses
[j
];
2910 if (access
->array
!= a
)
2912 if (access
->type
!= pdg::access::write
)
2914 return na_pair(node
, access
);
2921 /* Add a dependence from (source_node, source_access) to
2922 * (sink_node, sink_access) to pdg->dependences, for the
2923 * case where the array being accessed is marked uniquely_defined.
2925 * Since the array is marked uniquely_defined, the value based
2926 * dependence is equal to the memory based dependence, so we
2927 * simply need to compose the access relations to obtain
2928 * the dependence relation.
2929 * This dependence relation is then specialized with respect to
2930 * the context and the iteration domain of the sink.
2932 static void add_unique_dep(PDG
*pdg
, pdg::node
*source_node
,
2933 pdg::access
*source_access
, pdg::node
*sink_node
,
2934 pdg::access
*sink_access
)
2941 d
= new pdg::dependence
;
2942 d
->array
= source_access
->array
;
2943 d
->type
= pdg::dependence::flow
;
2944 d
->from
= source_node
;
2946 d
->from_access
= source_access
;
2947 d
->to_access
= sink_access
;
2949 dep
= source_access
->map
->get_isl_map();
2950 read
= sink_access
->map
->get_isl_map();
2951 dep
= isl_map_apply_range(dep
, isl_map_reverse(read
));
2952 dom
= sink_node
->source
->get_isl_set();
2953 if (isl_set_is_wrapping(dom
))
2954 dom
= isl_map_domain(isl_set_unwrap(dom
));
2955 dep
= isl_map_intersect_range(dep
, dom
);
2956 dep
= isl_map_intersect_params(dep
, pdg
->get_context_isl_set());
2957 d
->relation
= new pdg::IslMap(dep
);
2959 pdg
->dependences
.push_back(d
);
2962 /* Find the flow dependences associated to the array "a", which is marked
2963 * uniquely_defined, and add them to pdg->dependences.
2965 * First determine the unique source and then iterate through all the reads,
2966 * adding dependences from the unique source to each of the reads.
2968 static void find_unique_deps(PDG
*pdg
, pdg::array
*a
)
2970 na_pair na
= find_unique_source(pdg
, a
);
2972 for (int i
= 0; i
< pdg
->nodes
.size(); ++i
) {
2973 pdg::node
*node
= pdg
->nodes
[i
];
2974 pdg::statement
*s
= pdg
->nodes
[i
]->statement
;
2975 for (int j
= 0; j
< s
->accesses
.size(); ++j
) {
2976 pdg::access
*access
= s
->accesses
[j
];
2977 if (access
->array
!= a
)
2979 if (access
->type
!= pdg::access::read
)
2981 add_unique_dep(pdg
, na
.node
, na
.access
, node
, access
);
2986 /* Find the dependence of type "t" associated to array "a" and add them
2987 * to pdg->dependences.
2989 * If we are looking for flow dependences for an array that is marked
2990 * uniquely_defined, then we do not need to compute anything, but instead
2991 * can simply read off the dependences in find_unique_deps.
2993 void find_deps(PDG
* pdg
, pdg::array
*a
, type t
)
2995 isl_ctx
*ctx
= pdg
->get_isl_ctx();
2996 int nparam
= pdg
->params
.size();
3000 add_dep_info info
= { pdg
, a
, t
};
3002 bool need_parametrization
;
3006 info
.dtype
= pdg::dependence::flow
;
3010 info
.dtype
= pdg::dependence::anti
;
3013 info
.dtype
= pdg::dependence::reuse
;
3018 info
.dtype
= pdg::dependence::reuse_pair
;
3021 info
.dtype
= pdg::dependence::output
;
3025 a
->analysis_performed
.push_back(new pdg::dependence_type(info
.dtype
));
3027 if (t
== flow
&& a
->uniquely_defined
) {
3028 find_unique_deps(pdg
, a
);
3032 vector
<na_pair
> readers
;
3033 vector
<na_pair
> writers
;
3034 for (int i
= 0; i
< pdg
->nodes
.size(); ++i
) {
3035 pdg::node
*node
= pdg
->nodes
[i
];
3036 pdg::statement
*s
= pdg
->nodes
[i
]->statement
;
3037 for (int j
= 0; j
< s
->accesses
.size(); ++j
) {
3038 pdg::access
*access
= s
->accesses
[j
];
3039 if (access
->array
!= a
)
3045 if ((access
->type
== pdg::access::read
) ^ (t
== anti
))
3046 readers
.push_back(na_pair(node
, access
));
3048 writers
.push_back(na_pair(node
, access
));
3051 if (access
->type
== pdg::access::read
)
3054 readers
.push_back(na_pair(node
, access
));
3055 writers
.push_back(na_pair(node
, access
));
3061 int maxsize
= (selfinput
|| reuse
) ? writers
.size() + readers
.size()
3063 context
= pdg
->get_context_isl_set();
3064 info
.precedes_level
= (isl_access_level_before
)
3065 ((t
== reuse_pair
) ? precedes_level_accesses
3066 : precedes_level_nodes
);
3067 need_parametrization
= any_filters(writers
);
3068 for (int i
= 0; i
< writers
.size(); ++i
) {
3069 writers
[i
].map
= convert_access(&writers
[i
]);
3070 writers
[i
].project_out_access_filters();
3072 for (int i
= 0; i
< readers
.size(); ++i
) {
3073 readers
[i
].map
= convert_access(&readers
[i
]);
3074 readers
[i
].map
= isl_map_intersect_params(readers
[i
].map
,
3075 isl_set_copy(context
));
3076 readers
[i
].project_out_access_filters();
3078 for (int i
= 0; i
< readers
.size(); ++i
) {
3079 isl_access_info
*acc
;
3080 int n_dep
= pdg
->dependences
.size();
3082 acc
= isl_access_info_alloc(isl_map_copy(readers
[i
].map
),
3083 &readers
[i
], info
.precedes_level
, maxsize
);
3084 if (need_parametrization
)
3085 acc
= isl_access_info_set_restrict(acc
, &do_restrict
, &info
);
3086 for (int j
= 0; j
< writers
.size(); ++j
)
3087 acc
= isl_access_info_add_source(acc
,
3088 isl_map_copy(writers
[j
].map
), 1, &writers
[j
]);
3089 if (selfinput
&& writers
.size()) {
3090 pdg::node
*readnode
= readers
[i
].node
;
3091 for (int j
= 0; j
< readers
.size(); ++j
) {
3092 if (readers
[j
].node
== readnode
)
3093 acc
= isl_access_info_add_source(acc
,
3094 isl_map_copy(readers
[j
].map
), 1, &readers
[j
]);
3098 for (int j
= 0; j
< readers
.size(); ++j
)
3099 acc
= isl_access_info_add_source(acc
,
3100 isl_map_copy(readers
[j
].map
), 1, &readers
[j
]);
3102 info
.set_read_na(&readers
[i
]);
3103 isl_flow
*deps
= isl_access_info_compute_flow(acc
);
3104 isl_flow_foreach(deps
, add_dep
, &info
);
3106 no_source
= isl_flow_get_no_source(deps
, 1);
3107 no_source
= isl_map_from_range(isl_map_domain(no_source
));
3108 no_source
= simplify_controls(no_source
, &info
, NULL
);
3109 if (!isl_map_plain_is_empty(no_source
) && firstuse
) {
3110 pdg::dependence
*d
= new pdg::dependence
;
3112 d
->type
= pdg::dependence::uninitialized
;
3113 d
->to
= readers
[i
].node
;
3114 d
->to_access
= readers
[i
].access
;
3115 if (d
->to_access
->extension
) {
3116 d
->extended_relation
= new pdg::IslMap(isl_map_copy(no_source
));
3117 no_source
= isl_map_apply_range(no_source
,
3119 d
->to_access
->extension
->get_isl_map(ctx
)));
3121 d
->relation
= new pdg::IslMap(isl_map_copy(no_source
));
3122 pdg
->dependences
.push_back(d
);
3124 isl_map_free(no_source
);
3125 isl_flow_free(deps
);
3126 remove_unused_sources(pdg
, n_dep
, writers
, &info
);
3128 isl_set_free(context
);
3131 /* Add a dependence from "write" to "a" to a->sources.
3133 static void add_unique_source(pdg::access
*a
, pdg::access
*write
)
3138 dep
= isl_map_range_map(write
->map
->get_isl_map());
3139 read
= isl_map_range_map(a
->map
->get_isl_map());
3140 dep
= isl_map_apply_range(dep
, isl_map_reverse(read
));
3142 a
->sources
.push_back(new pdg::IslMap(dep
));
3145 /* Look for the unique write access that writes to the array accessed
3146 * by "a" and then add a dependence from that write to a->sources.
3148 static void add_unique_source(pdg::PDG
* pdg
, pdg::access
*a
)
3150 na_pair na
= find_unique_source(pdg
, a
->array
);
3151 add_unique_source(a
, na
.access
);
3155 static isl_stat
add_source(__isl_take isl_map
*dep
, int must
,
3156 void *dep_user
, void *user
);
3159 /* Add "dep" to a->sources, provided it is exact, and return isl_stat_ok.
3160 * Otherwise, return isl_stat_error.
3162 static isl_stat
add_source(__isl_take isl_map
*dep
, int must
, void *dep_user
,
3166 pdg::access
*a
= (pdg::access
*) user
;
3168 dep
= remove_redundant_controls(dep
, &has_controls
);
3171 return isl_stat_error
;
3174 a
->sources
.push_back(new pdg::IslMap(dep
));
3180 static __isl_give isl_restriction
*do_restrict_domain_map(
3181 __isl_keep isl_map
*source_map
, __isl_keep isl_set
*sink
,
3182 void *source_user
, void *user
);
3185 /* Compute a restriction for the given map from sinks to potential sources
3186 * (sink2source). We simply call compute_restriction to compute the
3187 * restriction. This function is used from within find_sources,
3188 * which encodes the entire access relation into the domains of
3189 * the access relations passed to isl_access_info_compute_flow.
3190 * That is, the access relations passed to isl_access_info_compute_flow
3191 * are the result of applying isl_map_range_map to the original access
3192 * relations. We therefore pass mappings that undo this encoding
3193 * to compute_restriction.
3195 static __isl_give isl_restriction
*do_restrict_domain_map(
3196 __isl_keep isl_map
*source_map
, __isl_keep isl_set
*sink
,
3197 void *source_user
, void *user
)
3199 na_pair
*source_na
= (na_pair
*) source_user
;
3200 add_dep_info
*info
= (struct add_dep_info
*) user
;
3202 isl_map
*source_domain_map
, *sink_domain_map
;
3204 space
= isl_space_range(isl_map_get_space(source_map
));
3205 space
= isl_space_unwrap(space
);
3206 source_domain_map
= isl_map_domain_map(isl_map_universe(space
));
3207 space
= isl_space_domain(isl_map_get_space(source_map
));
3208 space
= isl_space_unwrap(space
);
3209 sink_domain_map
= isl_map_domain_map(isl_map_universe(space
));
3211 return compute_restriction(source_map
, sink_domain_map
,
3212 source_domain_map
, sink
, source_na
, info
);
3215 /* Find the sources of (read) access "a" in node "node".
3216 * If they are complete (no uninitialized accesses) and exact,
3217 * then put them in a->sources. Otherwise, discard them.
3219 * If the array is marked uniquely_defined, then we simply look
3220 * for the defining write in find_unique_source.
3222 * Otherwise, we look for all writes that write to the same array,
3223 * perform dependence analysis and then check whether
3224 * the result is complete and exact.
3226 * The sources record not only the node iteration, but also
3227 * the index of the array element. We therefore apply
3228 * isl_map_range_map to the access relations, to obtain
3229 * a relation from the access (iteration -> element)
3230 * to the array element and feed that to the dependence analysis engine.
3232 void find_sources(pdg::PDG
* pdg
, pdg::node
*node
, pdg::access
*a
)
3236 isl_access_info
*acc
;
3238 vector
<na_pair
> writers
;
3239 na_pair
reader(node
, a
);
3240 add_dep_info info
= { pdg
, a
->array
, flow
, pdg::dependence::flow
};
3242 if (a
->array
->uniquely_defined
) {
3243 add_unique_source(pdg
, a
);
3247 info
.precedes_level
= (isl_access_level_before
) precedes_level_nodes
;
3249 for (int i
= 0; i
< pdg
->nodes
.size(); ++i
) {
3250 pdg::node
*node
= pdg
->nodes
[i
];
3251 pdg::statement
*s
= pdg
->nodes
[i
]->statement
;
3252 for (int j
= 0; j
< s
->accesses
.size(); ++j
) {
3253 pdg::access
*access
= s
->accesses
[j
];
3254 if (access
->array
!= a
->array
)
3256 if (access
->type
!= pdg::access::write
)
3258 writers
.push_back(na_pair(node
, access
));
3262 context
= pdg
->get_context_isl_set();
3263 for (int i
= 0; i
< writers
.size(); ++i
) {
3264 writers
[i
].projected_map
= convert_access(&writers
[i
]);
3265 writers
[i
].map
= isl_map_copy(writers
[i
].projected_map
);
3266 writers
[i
].map
= isl_map_range_map(writers
[i
].map
);
3267 writers
[i
].project_out_access_filters();
3269 reader
.projected_map
= convert_access(&reader
);
3270 reader
.projected_map
= isl_map_intersect_params(reader
.projected_map
,
3272 reader
.map
= isl_map_range_map(isl_map_copy(reader
.projected_map
));
3273 reader
.project_out_access_filters();
3275 acc
= isl_access_info_alloc(isl_map_copy(reader
.map
),
3276 &reader
, info
.precedes_level
, writers
.size());
3277 for (int j
= 0; j
< writers
.size(); ++j
)
3278 acc
= isl_access_info_add_source(acc
,
3279 isl_map_copy(writers
[j
].map
), 1, &writers
[j
]);
3281 if (any_filters(writers
))
3282 acc
= isl_access_info_set_restrict(acc
,
3283 &do_restrict_domain_map
, &info
);
3285 info
.set_read_na(&reader
);
3286 deps
= isl_access_info_compute_flow(acc
);
3287 no_source
= isl_flow_get_no_source(deps
, 1);
3288 if (isl_map_plain_is_empty(no_source
)) {
3289 if (isl_flow_foreach(deps
, add_source
, a
) < 0) {
3290 for (int i
= 0; i
< a
->sources
.size(); ++i
)
3291 delete a
->sources
[i
];
3292 a
->sources
.resize(0);
3295 isl_map_free(no_source
);
3296 isl_flow_free(deps
);
3299 /* Find the source (if possible) of the filter "coa" in node "node".
3300 * We assume that the filter is an access rather than a function call.
3302 static void find_sources(pdg::PDG
*pdg
, pdg::node
*node
,
3303 pdg::call_or_access
*coa
)
3305 pdg::access
*access
;
3307 assert(coa
->type
== pdg::call_or_access::t_access
);
3308 access
= coa
->access
;
3310 find_sources(pdg
, node
, access
);
3313 /* Compute the sources (if possible) for all the filters in all the
3314 * nodes and accesses in "pdg".
3316 void compute_filter_sources(pdg::PDG
*pdg
)
3318 for (int i
= 0; i
< pdg
->nodes
.size(); ++i
) {
3319 pdg::node
*node
= pdg
->nodes
[i
];
3320 pdg::statement
*s
= pdg
->nodes
[i
]->statement
;
3321 int n_filter
= node
->filters
.size();
3323 for (int j
= 0; j
< n_filter
; ++j
)
3324 find_sources(pdg
, node
, node
->filters
[j
]);
3326 for (int j
= 0; j
< s
->accesses
.size(); ++j
) {
3327 pdg::access
*access
= s
->accesses
[j
];
3329 for (int k
= 0; k
< access
->nested
.size(); ++k
)
3330 find_sources(pdg
, node
, access
->nested
[k
]);
3335 static int precedes_level_nodes(na_pair
*first
, na_pair
*second
)
3339 for (int i
= 0; i
< first
->node
->prefix
.size(); ++i
) {
3340 if (i
>= second
->node
->prefix
.size())
3342 cmp
= first
->node
->prefix
[i
] - second
->node
->prefix
[i
];
3345 if (first
->node
->prefix
[i
] == -1)
3348 return 2*d
+ (cmp
<0);
3351 static int precedes_level_accesses(na_pair
*first
, na_pair
*second
)
3355 for (int i
= 0; i
< first
->node
->prefix
.size(); ++i
) {
3356 if (i
>= second
->node
->prefix
.size())
3358 cmp
= first
->node
->prefix
[i
] - second
->node
->prefix
[i
];
3361 if (first
->node
->prefix
[i
] == -1)
3364 /* same node; now compare accesses */
3366 cmp
= first
->access
->nr
- second
->access
->nr
;
3367 return 2*d
+ (cmp
<0);