isl_access_info: allow user interaction to restrict potential sources
authorSven Verdoolaege <skimo@kotnet.org>
Mon, 28 Nov 2011 14:28:37 +0000 (15:28 +0100)
committerSven Verdoolaege <skimo@kotnet.org>
Tue, 27 Dec 2011 10:36:17 +0000 (11:36 +0100)
In particular, the user can register a callback that will be called
any time right before the last potential source is computed.
This should be useful for fuzzy array dataflow analysis.

Signed-off-by: Sven Verdoolaege <skimo@kotnet.org>
doc/user.pod
include/isl/flow.h
isl_flow.c

index 52afd36..8e1d66d 100644 (file)
@@ -4029,6 +4029,49 @@ Any of C<must_dep>, C<may_dep>, C<must_no_source>
 or C<may_no_source> may be C<NULL>, but a C<NULL> value for
 any of the other arguments is treated as an error.
 
+=head3 Interaction with Dependence Analysis
+
+During the dependence analysis, we frequently need to perform
+the following operation.  Given a relation between sink iterations
+and potential soure iterations from a particular source domain,
+what is the last potential source iteration corresponding to each
+sink iteration.  It can sometimes be convenient to adjust
+the set of potential source iterations before each such operation.
+The prototypical example is fuzzy array dataflow analysis,
+where we need to analyze if, based on data-dependent constraints,
+the sink iteration can ever be executed without one or more of
+the corresponding potential source iterations being executed.
+If so, we can introduce extra parameters and select an unknown
+but fixed source iteration from the potential source iterations.
+To be able to perform such manipulations, C<isl> provides the following
+function.
+
+       #include <isl/flow.h>
+
+       typedef __isl_give isl_set *(*isl_access_restrict_sources)(
+               __isl_take isl_map *source_map,
+               void *sink_user, void *source_user);
+       __isl_give isl_access_info *
+       isl_access_info_set_restrict_sources(
+               __isl_take isl_access_info *acc,
+               isl_access_restrict_sources fn);
+
+The function C<isl_access_info_set_restrict_sources> should be called
+before C<isl_access_info_compute_flow> and registers a callback function
+that will be called any time C<isl> is about to compute the last
+potential source.  The first argument is the (reverse) proto-dependence,
+mapping sink iterations to potential source iterations.
+The other two arguments are the tokens corresponding to the sink
+and the source.  The callback is expected to return a set
+that restricts the source iterations.  The potential source iterations
+will be intersected with this set.  If no restrictions are required
+for a given C<source_map>, then the callback should return
+
+       isl_set_universe(
+           isl_space_range(isl_map_get_space(source_map)));
+
+If any error occurs, the callback should return C<NULL>.
+
 =head2 Scheduling
 
 B<The functionality described in this section is fairly new
index 8fc2698..e59bff3 100644 (file)
@@ -16,6 +16,9 @@ extern "C" {
  */
 typedef int (*isl_access_level_before)(void *first, void *second);
 
+typedef __isl_give isl_set *(*isl_access_restrict_sources)(
+       __isl_take isl_map *source_map, void *sink_user, void *source_user);
+
 struct isl_access_info;
 typedef struct isl_access_info isl_access_info;
 struct isl_flow;
@@ -23,6 +26,8 @@ typedef struct isl_flow isl_flow;
 
 __isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
        void *sink_user, isl_access_level_before fn, int max_source);
+__isl_give isl_access_info *isl_access_info_set_restrict_sources(
+       __isl_take isl_access_info *acc, isl_access_restrict_sources fn);
 __isl_give isl_access_info *isl_access_info_add_source(
        __isl_take isl_access_info *acc, __isl_take isl_map *source,
        int must, void *source_user);
index 7d5548a..2678f0f 100644 (file)
@@ -35,15 +35,19 @@ struct isl_labeled_map {
  *
  * domain_map is an auxiliary map that maps the sink access relation
  * to the domain of this access relation.
+ *
+ * restrict_sources is a callback that (if not NULL) will be called
+ * right before any lexicographical maximization.
  */
 struct isl_access_info {
-       isl_map                 *domain_map;
-       struct isl_labeled_map  sink;
-       isl_access_level_before level_before;
-       int                     max_source;
-       int                     n_must;
-       int                     n_may;
-       struct isl_labeled_map  source[1];
+       isl_map                         *domain_map;
+       struct isl_labeled_map          sink;
+       isl_access_level_before         level_before;
+       isl_access_restrict_sources     restrict_sources;
+       int                             max_source;
+       int                             n_must;
+       int                             n_may;
+       struct isl_labeled_map          source[1];
 };
 
 /* A structure containing the output of dependence analysis:
@@ -112,6 +116,15 @@ isl_ctx *isl_access_info_get_ctx(__isl_keep isl_access_info *acc)
        return acc ? isl_map_get_ctx(acc->sink.map) : NULL;
 }
 
+__isl_give isl_access_info *isl_access_info_set_restrict_sources(
+       __isl_take isl_access_info *acc, isl_access_restrict_sources fn)
+{
+       if (!acc)
+               return NULL;
+       acc->restrict_sources = fn;
+       return acc;
+}
+
 /* Add another source to an isl_access_info structure, making
  * sure the "must" sources are placed before the "may" sources.
  * This function may be called at most max_source times on a
@@ -405,6 +418,32 @@ static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
        return isl_map_from_basic_map(bmap);
 }
 
+/* Check if the user has set acc->restrict_sources and if so
+ * intersect the range of "dep" with the result of a call to this function.
+ *
+ * Since the user expects a mapping from sink iterations to source iterations,
+ * whereas the domain of "dep" is a wrapped map, mapping sink iterations
+ * to accessed array elements, we first need to project out the accessed
+ * sink array elements by applying acc->domain_map.
+ */
+static __isl_give isl_map *restrict_sources(__isl_take isl_map *dep,
+       struct isl_access_info *acc, int source)
+{
+       isl_map *source_map;
+       isl_set *param;
+
+       if (!acc->restrict_sources)
+               return dep;
+
+       source_map = isl_map_copy(dep);
+       source_map = isl_map_apply_domain(source_map,
+                                           isl_map_copy(acc->domain_map));
+       param = acc->restrict_sources(source_map, acc->sink.data,
+                                   acc->source[source].data);
+       dep = isl_map_intersect_range(dep, param);
+       return dep;
+}
+
 /* Compute the last iteration of must source j that precedes the sink
  * at the given level for sink iterations in set_C.
  * The subset of set_C for which no such iteration can be found is returned
@@ -426,6 +465,7 @@ static struct isl_map *last_source(struct isl_access_info *acc,
        dep_map = isl_map_apply_range(read_map, write_map);
        after = after_at_level(isl_map_get_space(dep_map), level);
        dep_map = isl_map_intersect(dep_map, after);
+       dep_map = restrict_sources(dep_map, acc, j);
        result = isl_map_partial_lexmax(dep_map, set_C, empty);
        result = isl_map_reverse(result);
 
@@ -467,6 +507,7 @@ static struct isl_map *last_later_source(struct isl_access_info *acc,
        dep_map = isl_map_intersect(dep_map, after_write);
        before_read = after_at_level(isl_map_get_space(dep_map), before_level);
        dep_map = isl_map_intersect(dep_map, before_read);
+       dep_map = restrict_sources(dep_map, acc, k);
        result = isl_map_partial_lexmax(dep_map, set_C, empty);
        result = isl_map_reverse(result);