[PATCH 1/8] isl_aff.c: union_pw_multi_aff_scale_multi_val_entry: improve error handling

1 view
Skip to first unread message

skim...@kotnet.org

unread,
Aug 6, 2021, 1:12:50 PMAug 6
to isl-dev...@googlegroups.com
From: Sven Verdoolaege <sv...@cerebras.net>

Signed-off-by: Sven Verdoolaege <sv...@cerebras.net>
---
isl_aff.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/isl_aff.c b/isl_aff.c
index f1876ec14e..7f8793a004 100644
--- a/isl_aff.c
+++ b/isl_aff.c
@@ -6751,12 +6751,16 @@ error:
static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
__isl_take isl_pw_multi_aff *pma, void *user)
{
+ isl_bool equal;
isl_multi_val *mv = user;

if (!pma)
return NULL;
- if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
- mv->space, isl_dim_set)) {
+ equal = isl_space_tuple_is_equal(pma->dim, isl_dim_out,
+ mv->space, isl_dim_set);
+ if (equal < 0)
+ return isl_pw_multi_aff_free(pma);
+ if (!equal) {
isl_space *space = isl_pw_multi_aff_get_space(pma);
isl_pw_multi_aff_free(pma);
return isl_pw_multi_aff_empty(space);
--
2.25.1

skim...@kotnet.org

unread,
Aug 6, 2021, 1:12:51 PMAug 6
to isl-dev...@googlegroups.com
From: Sven Verdoolaege <sv...@cerebras.net>

This removes some minor code duplication and improves error handling.

Signed-off-by: Sven Verdoolaege <sv...@cerebras.net>
---
Makefile.am | 1 +
isl_aff.c | 10 ++++----
isl_multi_arith_templ.c | 25 ++++++--------------
isl_type_check_match_range_multi_val.c | 32 ++++++++++++++++++++++++++
4 files changed, 45 insertions(+), 23 deletions(-)
create mode 100644 isl_type_check_match_range_multi_val.c

diff --git a/Makefile.am b/Makefile.am
index 939dbea510..17f92489d5 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -598,6 +598,7 @@ EXTRA_DIST = \
isl_test_list_templ.c \
isl_test_plain_equal_templ.c \
isl_type_check_equal_space_templ.c \
+ isl_type_check_match_range_multi_val.c \
isl_type_has_equal_space_bin_templ.c \
isl_type_has_equal_space_templ.c \
isl_type_has_space_templ.c \
diff --git a/isl_aff.c b/isl_aff.c
index 7f8793a004..8ac4b65228 100644
--- a/isl_aff.c
+++ b/isl_aff.c
@@ -6701,6 +6701,10 @@ __isl_give isl_multi_pw_aff *isl_map_max_multi_pw_aff(__isl_take isl_map *map)
return map_opt_mpa(map, &isl_map_dim_max);
}

+#undef TYPE
+#define TYPE isl_pw_multi_aff
+#include "isl_type_check_match_range_multi_val.c"
+
/* Scale the elements of "pma" by the corresponding elements of "mv".
*/
__isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
@@ -6710,12 +6714,8 @@ __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
isl_bool equal_params;

pma = isl_pw_multi_aff_cow(pma);
- if (!pma || !mv)
+ if (isl_pw_multi_aff_check_match_range_multi_val(pma, mv) < 0)
goto error;
- if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
- mv->space, isl_dim_set))
- isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
- "spaces don't match", goto error);
equal_params = isl_space_has_equal_params(pma->dim, mv->space);
if (equal_params < 0)
goto error;
diff --git a/isl_multi_arith_templ.c b/isl_multi_arith_templ.c
index 6765bd23cb..76cdcdafec 100644
--- a/isl_multi_arith_templ.c
+++ b/isl_multi_arith_templ.c
@@ -106,6 +106,10 @@ error:
return FN(MULTI(BASE),free)(multi);
}

+#undef TYPE
+#define TYPE MULTI(BASE)
+#include "isl_type_check_match_range_multi_val.c"
+
/* Multiply the elements of "multi" by the corresponding element of "mv"
* and return the result.
*/
@@ -114,14 +118,9 @@ __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_multi_val)(
{
int i;

- if (!multi || !mv)
+ if (FN(MULTI(BASE),check_match_range_multi_val)(multi, mv) < 0)
goto error;

- if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
- mv->space, isl_dim_set))
- isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
- "spaces don't match", goto error);
-
multi = FN(MULTI(BASE),cow)(multi);
if (!multi)
goto error;
@@ -150,14 +149,9 @@ __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_multi_val)(
{
int i;

- if (!multi || !mv)
+ if (FN(MULTI(BASE),check_match_range_multi_val)(multi, mv) < 0)
goto error;

- if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
- mv->space, isl_dim_set))
- isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
- "spaces don't match", goto error);
-
multi = FN(MULTI(BASE),cow)(multi);
if (!multi)
return NULL;
@@ -186,14 +180,9 @@ __isl_give MULTI(BASE) *FN(MULTI(BASE),mod_multi_val)(
{
int i;

- if (!multi || !mv)
+ if (FN(MULTI(BASE),check_match_range_multi_val)(multi, mv) < 0)
goto error;

- if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
- mv->space, isl_dim_set))
- isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
- "spaces don't match", goto error);
-
multi = FN(MULTI(BASE),cow)(multi);
if (!multi)
goto error;
diff --git a/isl_type_check_match_range_multi_val.c b/isl_type_check_match_range_multi_val.c
new file mode 100644
index 0000000000..3b60e4db5b
--- /dev/null
+++ b/isl_type_check_match_range_multi_val.c
@@ -0,0 +1,32 @@
+#define xFN(TYPE,NAME) TYPE ## _ ## NAME
+#define FN(TYPE,NAME) xFN(TYPE,NAME)
+
+/* Does the range space of "obj" match the space of "mv" (ignoring parameters)?
+ */
+static isl_bool FN(TYPE,match_range_multi_val)(__isl_keep TYPE *obj,
+ __isl_keep isl_multi_val *mv)
+{
+ isl_space *space, *mv_space;
+
+ space = FN(TYPE,peek_space)(obj);
+ mv_space = isl_multi_val_peek_space(mv);
+ return isl_space_tuple_is_equal(space, isl_dim_out,
+ mv_space, isl_dim_set);
+}
+
+/* Check that the range space of "obj" matches the space of "mv"
+ * (ignoring parameters).
+ */
+static isl_stat FN(TYPE,check_match_range_multi_val)(__isl_keep TYPE *obj,
+ __isl_keep isl_multi_val *mv)
+{
+ isl_bool equal;
+
+ equal = FN(TYPE,match_range_multi_val)(obj, mv);
+ if (equal < 0)
+ return isl_stat_error;
+ if (!equal)
+ isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
+ "spaces don't match", return isl_stat_error);
+ return isl_stat_ok;
+}
--
2.25.1

skim...@kotnet.org

unread,
Aug 6, 2021, 1:12:52 PMAug 6
to isl-dev...@googlegroups.com
From: Sven Verdoolaege <sv...@cerebras.net>

This function was introduced in the previous commit and
reusing it reduces the dependence on the internal representation.

Signed-off-by: Sven Verdoolaege <sv...@cerebras.net>
---
isl_aff.c | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/isl_aff.c b/isl_aff.c
index 8ac4b65228..d0ed5931c2 100644
--- a/isl_aff.c
+++ b/isl_aff.c
@@ -6754,10 +6754,7 @@ static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
isl_bool equal;
isl_multi_val *mv = user;

- if (!pma)
- return NULL;
- equal = isl_space_tuple_is_equal(pma->dim, isl_dim_out,
- mv->space, isl_dim_set);
+ equal = isl_pw_multi_aff_match_range_multi_val(pma, mv);
if (equal < 0)
return isl_pw_multi_aff_free(pma);
if (!equal) {
--
2.25.1

skim...@kotnet.org

unread,
Aug 6, 2021, 1:12:53 PMAug 6
to isl-dev...@googlegroups.com
From: Sven Verdoolaege <sv...@cerebras.net>

It arguably makes little sense for an isl_multi_val
to reference any parameters.
In any case, the parameters of the isl_multi_val, even if there are any,
do not affect the called isl_multi_aff_scale_multi_val in any way.

Signed-off-by: Sven Verdoolaege <sv...@cerebras.net>
---
isl_aff.c | 12 ------------
1 file changed, 12 deletions(-)

diff --git a/isl_aff.c b/isl_aff.c
index d0ed5931c2..4d5f766d44 100644
--- a/isl_aff.c
+++ b/isl_aff.c
@@ -6711,22 +6711,10 @@ __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
__isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
{
int i;
- isl_bool equal_params;

pma = isl_pw_multi_aff_cow(pma);
if (isl_pw_multi_aff_check_match_range_multi_val(pma, mv) < 0)
goto error;
- equal_params = isl_space_has_equal_params(pma->dim, mv->space);
- if (equal_params < 0)
- goto error;
- if (!equal_params) {
- pma = isl_pw_multi_aff_align_params(pma,
- isl_multi_val_get_space(mv));
- mv = isl_multi_val_align_params(mv,
- isl_pw_multi_aff_get_space(pma));
- if (!pma || !mv)
- goto error;
- }

for (i = 0; i < pma->n; ++i) {
pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
--
2.25.1

skim...@kotnet.org

unread,
Aug 6, 2021, 1:12:54 PMAug 6
to isl-dev...@googlegroups.com
From: Sven Verdoolaege <sv...@cerebras.net>

This reduces the dependence on the internal representation.

Signed-off-by: Sven Verdoolaege <sv...@cerebras.net>
---
isl_aff.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/isl_aff.c b/isl_aff.c
index 4d5f766d44..4bdf752437 100644
--- a/isl_aff.c
+++ b/isl_aff.c
@@ -6711,12 +6711,17 @@ __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
__isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
{
int i;
+ isl_size n;

pma = isl_pw_multi_aff_cow(pma);
if (isl_pw_multi_aff_check_match_range_multi_val(pma, mv) < 0)
goto error;

- for (i = 0; i < pma->n; ++i) {
+ n = isl_pw_multi_aff_n_piece(pma);
+ if (n < 0)
+ goto error;
+
+ for (i = 0; i < n; ++i) {
pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
isl_multi_val_copy(mv));
if (!pma->p[i].maff)
--
2.25.1

skim...@kotnet.org

unread,
Aug 6, 2021, 1:12:55 PMAug 6
to isl-dev...@googlegroups.com
From: Sven Verdoolaege <sv...@cerebras.net>

This reduces the dependence on the internal representation.

Signed-off-by: Sven Verdoolaege <sv...@cerebras.net>
---
isl_aff.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/isl_aff.c b/isl_aff.c
index 4bdf752437..a448106e8a 100644
--- a/isl_aff.c
+++ b/isl_aff.c
@@ -6713,7 +6713,6 @@ __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
int i;
isl_size n;

- pma = isl_pw_multi_aff_cow(pma);
if (isl_pw_multi_aff_check_match_range_multi_val(pma, mv) < 0)
goto error;

@@ -6722,10 +6721,11 @@ __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
goto error;

for (i = 0; i < n; ++i) {
- pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
- isl_multi_val_copy(mv));
- if (!pma->p[i].maff)
- goto error;
+ isl_multi_aff *ma;
+
+ ma = isl_pw_multi_aff_take_base_at(pma, i);
+ ma = isl_multi_aff_scale_multi_val(ma, isl_multi_val_copy(mv));
+ pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
}

isl_multi_val_free(mv);
--
2.25.1

skim...@kotnet.org

unread,
Aug 6, 2021, 1:12:56 PMAug 6
to isl-dev...@googlegroups.com
From: Sven Verdoolaege <sv...@cerebras.net>

This complements the already exported isl_pw_aff_scale_val and
isl_multi_*_scale_multi_val.

Signed-off-by: Sven Verdoolaege <sv...@cerebras.net>
---
include/isl/aff.h | 1 +
isl_test2.cc | 15 +++++++++++++++
2 files changed, 16 insertions(+)

diff --git a/include/isl/aff.h b/include/isl/aff.h
index c417be1654..708240265e 100644
--- a/include/isl/aff.h
+++ b/include/isl/aff.h
@@ -738,6 +738,7 @@ __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_val(
__isl_overload
__isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_down_val(
__isl_take isl_pw_multi_aff *pma, __isl_take isl_val *v);
+__isl_overload
__isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
__isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv);

diff --git a/isl_test2.cc b/isl_test2.cc
index 7b64d6ffaa..abbcc7066a 100644
--- a/isl_test2.cc
+++ b/isl_test2.cc
@@ -157,11 +157,26 @@ static void test_preimage(isl::ctx ctx)
});
}

+/* Perform some basic scaling tests.
+ */
+static void test_scale(isl::ctx ctx)
+{
+ C(arg<isl::multi_val>(&isl::pw_multi_aff::scale), {
+ { "{ A[a] -> B[a, a + 1, a - 1] : a >= 0 }", "{ B[2, 7, 0] }",
+ "{ A[a] -> B[2a, 7a + 7, 0] : a >= 0 }" },
+ });
+ C(arg<isl::multi_val>(&isl::pw_multi_aff::scale), {
+ { "{ A[a] -> B[a, a - 1] : a >= 0 }", "{ B[1/2, 7] }",
+ "{ A[a] -> B[a/2, 7a - 7] : a >= 0 }" },
+ });
+}
+
/* The list of tests to perform.
*/
static std::vector<std::pair<const char *, void (*)(isl::ctx)>> tests =
{
{ "preimage", &test_preimage },
+ { "scale", &test_scale },
};

/* Perform some basic checks by means of the C++ bindings.
--
2.25.1

skim...@kotnet.org

unread,
Aug 6, 2021, 1:12:57 PMAug 6
to isl-dev...@googlegroups.com, Tianjiao Sun
From: Sven Verdoolaege <sv...@cerebras.net>

This complements isl_pw_multi_aff_scale_multi_val.

The new function is implemented by generalizing
isl_pw_multi_aff_scale_multi_val to
a isl_pw_multi_aff_op_multi_val.

Reviewed-by: Tianjiao Sun <tianji...@cerebras.net>
Signed-off-by: Sven Verdoolaege <sv...@cerebras.net>
---
doc/user.pod | 4 ++++
include/isl/aff.h | 3 +++
isl_aff.c | 28 ++++++++++++++++++++++++----
isl_test2.cc | 9 +++++++++
4 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/doc/user.pod b/doc/user.pod
index 010e333e7e..cabe2261c3 100644
--- a/doc/user.pod
+++ b/doc/user.pod
@@ -9057,6 +9057,10 @@ only defined on the shared definition domain of the arguments.
isl_multi_aff_scale_down_multi_val(
__isl_take isl_multi_aff *ma,
__isl_take isl_multi_val *mv);
+ __isl_give isl_pw_multi_aff *
+ isl_pw_multi_aff_scale_down_multi_val(
+ __isl_take isl_pw_multi_aff *pma,
+ __isl_take isl_multi_val *mv);
__isl_give isl_multi_pw_aff *
isl_multi_pw_aff_scale_down_multi_val(
__isl_take isl_multi_pw_aff *mpa,
diff --git a/include/isl/aff.h b/include/isl/aff.h
index 708240265e..918116b581 100644
--- a/include/isl/aff.h
+++ b/include/isl/aff.h
@@ -741,6 +741,9 @@ __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_down_val(
__isl_overload
__isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
__isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv);
+__isl_overload
+__isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_down_multi_val(
+ __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv);

__isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
__isl_take isl_pw_multi_aff *pma1,
diff --git a/isl_aff.c b/isl_aff.c
index a448106e8a..3542492aff 100644
--- a/isl_aff.c
+++ b/isl_aff.c
@@ -6705,10 +6705,12 @@ __isl_give isl_multi_pw_aff *isl_map_max_multi_pw_aff(__isl_take isl_map *map)
#define TYPE isl_pw_multi_aff
#include "isl_type_check_match_range_multi_val.c"

-/* Scale the elements of "pma" by the corresponding elements of "mv".
+/* Apply "fn" to the base expressions of "pma" and "mv".
*/
-__isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
- __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
+static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_op_multi_val(
+ __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv,
+ __isl_give isl_multi_aff *(*fn)(__isl_take isl_multi_aff *ma,
+ __isl_take isl_multi_val *mv))
{
int i;
isl_size n;
@@ -6724,7 +6726,7 @@ __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
isl_multi_aff *ma;

ma = isl_pw_multi_aff_take_base_at(pma, i);
- ma = isl_multi_aff_scale_multi_val(ma, isl_multi_val_copy(mv));
+ ma = fn(ma, isl_multi_val_copy(mv));
pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
}

@@ -6736,6 +6738,24 @@ error:
return NULL;
}

+/* Scale the elements of "pma" by the corresponding elements of "mv".
+ */
+__isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
+ __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
+{
+ return isl_pw_multi_aff_op_multi_val(pma, mv,
+ &isl_multi_aff_scale_multi_val);
+}
+
+/* Scale the elements of "pma" down by the corresponding elements of "mv".
+ */
+__isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_down_multi_val(
+ __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
+{
+ return isl_pw_multi_aff_op_multi_val(pma, mv,
+ &isl_multi_aff_scale_down_multi_val);
+}
+
/* This function is called for each entry of an isl_union_pw_multi_aff.
* If the space of the entry matches that of data->mv,
* then apply isl_pw_multi_aff_scale_multi_val and return the result.
diff --git a/isl_test2.cc b/isl_test2.cc
index abbcc7066a..ffa4e5f186 100644
--- a/isl_test2.cc
+++ b/isl_test2.cc
@@ -169,6 +169,15 @@ static void test_scale(isl::ctx ctx)
{ "{ A[a] -> B[a, a - 1] : a >= 0 }", "{ B[1/2, 7] }",
"{ A[a] -> B[a/2, 7a - 7] : a >= 0 }" },
});
+
+ C(arg<isl::multi_val>(&isl::pw_multi_aff::scale_down), {
+ { "{ A[a] -> B[a, a + 1] : a >= 0 }", "{ B[2, 7] }",
+ "{ A[a] -> B[a/2, (a + 1)/7] : a >= 0 }" },
+ });
+ C(arg<isl::multi_val>(&isl::pw_multi_aff::scale_down), {
+ { "{ A[a] -> B[a, a - 1] : a >= 0 }", "{ B[2, 1/7] }",
+ "{ A[a] -> B[a/2, 7a - 7] : a >= 0 }" },
+ });
}

/* The list of tests to perform.
--
2.25.1

Reply all
Reply to author
Forward
0 new messages