extract out shared isl_multi_pw_aff_pullback_*
authorSven Verdoolaege <sven@cerebras.net>
Fri, 25 Jun 2021 13:31:09 +0000 (25 15:31 +0200)
committerSven Verdoolaege <sven@cerebras.net>
Wed, 11 Aug 2021 16:35:26 +0000 (11 18:35 +0200)
This removes some code duplication.

There were minor differences between the implementation.
In particular, isl_multi_pw_aff_pullback_pw_multi_aff
was missing an error check on the constructed space,
while isl_multi_pw_aff_pullback_multi_pw_aff used
a different way of setting the space.
Also, isl_multi_pw_aff_pullback_multi_pw_aff would
directly call isl_pw_aff_pullback_multi_pw_aff_aligned,
whereas it now calls isl_pw_aff_pullback_multi_pw_aff.
the performance difference, if any, should be minimal.

Signed-off-by: Sven Verdoolaege <sven@cerebras.net>
Makefile.am
isl_aff.c
isl_multi_pw_aff_pullback_templ.c [new file with mode: 0644]

index 3b1f074..423b7ed 100644 (file)
@@ -555,6 +555,7 @@ EXTRA_DIST = \
        isl_multi_nan_templ.c \
        isl_multi_param_templ.c \
        isl_multi_product_templ.c \
+       isl_multi_pw_aff_pullback_templ.c \
        isl_multi_read_no_explicit_domain_templ.c \
        isl_multi_splice_templ.c \
        isl_multi_tuple_id_templ.c \
index ccdb89f..cbc8674 100644 (file)
--- a/isl_aff.c
+++ b/isl_aff.c
@@ -7143,115 +7143,15 @@ isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
        return equal;
 }
 
-#undef SUFFIX
-#define SUFFIX multi_aff
-#undef ARG1
-#define ARG1   isl_multi_pw_aff
-#undef ARG2
-#define ARG2   isl_multi_aff
-
-static
-#include "isl_align_params_templ.c"
-
-/* Compute the pullback of "mpa" by the function represented by "ma".
- * In other words, plug in "ma" in "mpa".
- *
- * If "mpa" has an explicit domain, then it is this domain
- * that needs to undergo a pullback, i.e., a preimage.
- */
-__isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
-       __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
-{
-       int i;
-       isl_space *space = NULL;
-
-       isl_multi_pw_aff_align_params_multi_aff(&mpa, &ma);
-       mpa = isl_multi_pw_aff_cow(mpa);
-       if (!mpa || !ma)
-               goto error;
-
-       space = isl_space_join(isl_multi_aff_get_space(ma),
-                               isl_multi_pw_aff_get_space(mpa));
-       if (!space)
-               goto error;
-
-       for (i = 0; i < mpa->n; ++i) {
-               mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
-                                                   isl_multi_aff_copy(ma));
-               if (!mpa->u.p[i])
-                       goto error;
-       }
-       if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
-               mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
-                                                       isl_multi_aff_copy(ma));
-               if (!mpa->u.dom)
-                       goto error;
-       }
-
-       isl_multi_aff_free(ma);
-       isl_space_free(mpa->space);
-       mpa->space = space;
-       return mpa;
-error:
-       isl_space_free(space);
-       isl_multi_pw_aff_free(mpa);
-       isl_multi_aff_free(ma);
-       return NULL;
-}
-
-#undef SUFFIX
-#define SUFFIX pw_multi_aff
-#undef ARG1
-#define ARG1   isl_multi_pw_aff
-#undef ARG2
-#define ARG2   isl_pw_multi_aff
-
-static
-#include "isl_align_params_templ.c"
-
-/* Compute the pullback of "mpa" by the function represented by "pma".
- * In other words, plug in "pma" in "mpa".
- *
- * If "mpa" has an explicit domain, then it is this domain
- * that needs to undergo a pullback, i.e., a preimage.
- */
-__isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
-       __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
-{
-       int i;
-       isl_space *space = NULL;
-
-       isl_multi_pw_aff_align_params_pw_multi_aff(&mpa, &pma);
-       mpa = isl_multi_pw_aff_cow(mpa);
-       if (!mpa || !pma)
-               goto error;
+#undef BASE
+#define BASE   multi_aff
 
-       space = isl_space_join(isl_pw_multi_aff_get_space(pma),
-                               isl_multi_pw_aff_get_space(mpa));
+#include "isl_multi_pw_aff_pullback_templ.c"
 
-       for (i = 0; i < mpa->n; ++i) {
-               mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
-                                   mpa->u.p[i], isl_pw_multi_aff_copy(pma));
-               if (!mpa->u.p[i])
-                       goto error;
-       }
-       if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
-               mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
-                                                   isl_pw_multi_aff_copy(pma));
-               if (!mpa->u.dom)
-                       goto error;
-       }
+#undef BASE
+#define BASE   pw_multi_aff
 
-       isl_pw_multi_aff_free(pma);
-       isl_space_free(mpa->space);
-       mpa->space = space;
-       return mpa;
-error:
-       isl_space_free(space);
-       isl_multi_pw_aff_free(mpa);
-       isl_pw_multi_aff_free(pma);
-       return NULL;
-}
+#include "isl_multi_pw_aff_pullback_templ.c"
 
 /* Apply "aff" to "mpa".  The range of "mpa" needs to be compatible
  * with the domain of "aff".  The domain of the result is the same
@@ -7425,18 +7325,6 @@ error:
 
 /* Compute the pullback of "pa" by the function represented by "mpa".
  * In other words, plug in "mpa" in "pa".
- * "pa" and "mpa" are assumed to have been aligned.
- *
- * The pullback is computed by applying "pa" to "mpa".
- */
-static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
-       __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
-{
-       return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
-}
-
-/* Compute the pullback of "pa" by the function represented by "mpa".
- * In other words, plug in "mpa" in "pa".
  *
  * The pullback is computed by applying "pa" to "mpa".
  */
@@ -7446,51 +7334,10 @@ __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
        return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
 }
 
-/* Compute the pullback of "mpa1" by the function represented by "mpa2".
- * In other words, plug in "mpa2" in "mpa1".
- *
- * We pullback each member of "mpa1" in turn.
- *
- * If "mpa1" has an explicit domain, then it is this domain
- * that needs to undergo a pullback instead, i.e., a preimage.
- */
-__isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
-       __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
-{
-       int i;
-       isl_space *space = NULL;
-
-       isl_multi_pw_aff_align_params_bin(&mpa1, &mpa2);
-       mpa1 = isl_multi_pw_aff_cow(mpa1);
-       if (!mpa1 || !mpa2)
-               goto error;
-
-       space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
-                               isl_multi_pw_aff_get_space(mpa1));
-
-       for (i = 0; i < mpa1->n; ++i) {
-               mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
-                               mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
-               if (!mpa1->u.p[i])
-                       goto error;
-       }
-
-       if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
-               mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
-                                               isl_multi_pw_aff_copy(mpa2));
-               if (!mpa1->u.dom)
-                       goto error;
-       }
-       mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
+#undef BASE
+#define BASE   multi_pw_aff
 
-       isl_multi_pw_aff_free(mpa2);
-       return mpa1;
-error:
-       isl_space_free(space);
-       isl_multi_pw_aff_free(mpa1);
-       isl_multi_pw_aff_free(mpa2);
-       return NULL;
-}
+#include "isl_multi_pw_aff_pullback_templ.c"
 
 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
  * of "mpa1" and "mpa2" live in the same space, construct map space
diff --git a/isl_multi_pw_aff_pullback_templ.c b/isl_multi_pw_aff_pullback_templ.c
new file mode 100644 (file)
index 0000000..c8448b2
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2013      Ecole Normale Superieure
+ *
+ * Use of this software is governed by the MIT license
+ *
+ * Written by Sven Verdoolaege,
+ * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
+ */
+
+#define xCAT(A,B) A ## B
+#define CAT(A,B) xCAT(A,B)
+#undef TYPE
+#define TYPE CAT(isl_,BASE)
+#define xFN(TYPE,NAME) TYPE ## _ ## NAME
+#define FN(TYPE,NAME) xFN(TYPE,NAME)
+
+#undef SUFFIX
+#define SUFFIX BASE
+#undef ARG1
+#define ARG1   isl_multi_pw_aff
+#undef ARG2
+#define ARG2   TYPE
+
+static
+#include "isl_align_params_templ.c"
+
+/* Compute the pullback of "mpa" by the function represented by "fn".
+ * In other words, plug in "fn" in "mpa".
+ *
+ * If "mpa" has an explicit domain, then it is this domain
+ * that needs to undergo a pullback, i.e., a preimage.
+ */
+__isl_give isl_multi_pw_aff *FN(isl_multi_pw_aff_pullback,BASE)(
+       __isl_take isl_multi_pw_aff *mpa, __isl_take TYPE *fn)
+{
+       int i;
+       isl_space *space = NULL;
+
+       FN(isl_multi_pw_aff_align_params,BASE)(&mpa, &fn);
+       mpa = isl_multi_pw_aff_cow(mpa);
+       if (!mpa || !fn)
+               goto error;
+
+       space = isl_space_join(FN(TYPE,get_space)(fn),
+                               isl_multi_pw_aff_get_space(mpa));
+       if (!space)
+               goto error;
+
+       for (i = 0; i < mpa->n; ++i) {
+               mpa->u.p[i] = FN(isl_pw_aff_pullback,BASE)(mpa->u.p[i],
+                                                   FN(TYPE,copy)(fn));
+               if (!mpa->u.p[i])
+                       goto error;
+       }
+       if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
+               mpa->u.dom = FN(isl_set_preimage,BASE)(mpa->u.dom,
+                                                       FN(TYPE,copy)(fn));
+               if (!mpa->u.dom)
+                       goto error;
+       }
+
+       FN(TYPE,free)(fn);
+       isl_space_free(mpa->space);
+       mpa->space = space;
+       return mpa;
+error:
+       isl_space_free(space);
+       isl_multi_pw_aff_free(mpa);
+       FN(TYPE,free)(fn);
+       return NULL;
+}