* [PATCH] PR fortran/103610 - ICE in gfc_convert_mpz_to_signed, at fortran/simplify.c:193
@ 2021-12-07 22:14 Harald Anlauf
2021-12-07 22:43 ` Thomas Koenig
0 siblings, 1 reply; 2+ messages in thread
From: Harald Anlauf @ 2021-12-07 22:14 UTC (permalink / raw)
To: fortran, gcc-patches
[-- Attachment #1: Type: text/plain, Size: 557 bytes --]
Dear all,
when simplifying SHAPE(), we rely on spec_dimen_size returning
non-negative dimensions. That actually did not happen in all
cases, as the testcase by Gerhard shows. (The testcase is
rather fallout from the wrong simplification).
The solution is trivial: simply check the dimensions against 0
and fix if needed.
Regtested on x86_64-pc-linux-gnu. OK for mainline.
As it turns out that we generated "wrong code" by the simplification,
I'd backport at least to 11-branch after some waiting time if no
issues show up elsewhere.
Thanks,
Harald
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Fortran-dimensions-of-an-array-have-to-be-non-negati.patch --]
[-- Type: text/x-patch, Size: 2245 bytes --]
From f8d6aae66b1cb3f75b47d50695ef1be7770abc30 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Tue, 7 Dec 2021 23:06:41 +0100
Subject: [PATCH] Fortran: dimensions of an array have to be non-negative
gcc/fortran/ChangeLog:
PR fortran/103610
* array.c (spec_dimen_size): Fix simplification of SHAPE:
dimensions must be non-negative.
gcc/testsuite/ChangeLog:
PR fortran/103610
* gfortran.dg/shape_11.f90: New test.
---
gcc/fortran/array.c | 6 ++++--
gcc/testsuite/gfortran.dg/shape_11.f90 | 16 ++++++++++++++++
2 files changed, 20 insertions(+), 2 deletions(-)
create mode 100644 gcc/testsuite/gfortran.dg/shape_11.f90
diff --git a/gcc/fortran/array.c b/gcc/fortran/array.c
index e5e22099405..e4891dfef77 100644
--- a/gcc/fortran/array.c
+++ b/gcc/fortran/array.c
@@ -2296,8 +2296,7 @@ gfc_copy_iterator (gfc_iterator *src)
/********* Subroutines for determining the size of an array *********/
/* These are needed just to accommodate RESHAPE(). There are no
- diagnostics here, we just return a negative number if something
- goes wrong. */
+ diagnostics here, we just return false if something goes wrong. */
/* Get the size of single dimension of an array specification. The
@@ -2330,6 +2329,9 @@ spec_dimen_size (gfc_array_spec *as, int dimen, mpz_t *result)
mpz_add_ui (*result, *result, 1);
+ if (mpz_cmp_si (*result, 0) < 0)
+ mpz_set_si (*result, 0);
+
return true;
}
diff --git a/gcc/testsuite/gfortran.dg/shape_11.f90 b/gcc/testsuite/gfortran.dg/shape_11.f90
new file mode 100644
index 00000000000..127d221e710
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/shape_11.f90
@@ -0,0 +1,16 @@
+! { dg-do compile }
+! { dg-options "-fdump-tree-original" }
+! PR fortran/103610 - ICE while simplifying SHAPE
+! Contributed by G.Steinmetz
+
+program p
+ integer, parameter :: a(-1) = 1
+ integer, parameter :: b(1) = maskl(shape(a))
+ integer, parameter :: c(1) = shape(a)
+ integer, parameter :: d(1) = maskr(shape(a))
+ if (b(1) /= 0) stop 1
+ if (c(1) /= 0) stop 2
+ if (d(1) /= 0) stop 3
+end
+
+! { dg-final { scan-tree-dump-not "_gfortran_stop_numeric" "original" } }
--
2.26.2
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] PR fortran/103610 - ICE in gfc_convert_mpz_to_signed, at fortran/simplify.c:193
2021-12-07 22:14 [PATCH] PR fortran/103610 - ICE in gfc_convert_mpz_to_signed, at fortran/simplify.c:193 Harald Anlauf
@ 2021-12-07 22:43 ` Thomas Koenig
0 siblings, 0 replies; 2+ messages in thread
From: Thomas Koenig @ 2021-12-07 22:43 UTC (permalink / raw)
To: Harald Anlauf, fortran, gcc-patches
Hi Harald,
> when simplifying SHAPE(), we rely on spec_dimen_size returning
> non-negative dimensions. That actually did not happen in all
> cases, as the testcase by Gerhard shows. (The testcase is
> rather fallout from the wrong simplification).
>
> The solution is trivial: simply check the dimensions against 0
> and fix if needed.
>
> Regtested on x86_64-pc-linux-gnu. OK for mainline.
Yes, and
> As it turns out that we generated "wrong code" by the simplification,
> I'd backport at least to 11-branch after some waiting time if no
> issues show up elsewhere.
... yes again.
Thanks for the patch!
Regards
Thomas
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2021-12-07 22:43 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-07 22:14 [PATCH] PR fortran/103610 - ICE in gfc_convert_mpz_to_signed, at fortran/simplify.c:193 Harald Anlauf
2021-12-07 22:43 ` Thomas Koenig
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).