public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [Patch] Fortran: Fix ubound simplifcation [PR99027]
@ 2021-02-13 20:49 Tobias Burnus
  2021-02-17 21:55 ` Thomas Koenig
  0 siblings, 1 reply; 2+ messages in thread
From: Tobias Burnus @ 2021-02-13 20:49 UTC (permalink / raw)
  To: gcc-patches, fortran

[-- Attachment #1: Type: text/plain, Size: 354 bytes --]

The problem which accessing dim=<n> for an expression is that
when the argument is an array, dim=<n> and expr->rank are relative
to the resulting array – but the array-ref also contains the
DIMEN_ELEMENT which does not count for dim=/rank.

OK for the trunk? (Reported against GCC 11, but I wonder whether
we should still apply it to GCC 10.)

Tobias


[-- Attachment #2: ubound.diff --]
[-- Type: text/x-patch, Size: 2298 bytes --]

Fortran: Fix ubound simplifcation [PR99027]

gcc/fortran/ChangeLog:

	PR fortran/99027
	* simplify.c (simplify_bound_dim): Honor DIMEN_ELEMENT
	when using dim=.

gcc/testsuite/ChangeLog:

	PR fortran/99027
	* gfortran.dg/ubound_1.f90: New test.

 gcc/fortran/simplify.c                 | 12 +++++++++++-
 gcc/testsuite/gfortran.dg/ubound_1.f90 | 22 ++++++++++++++++++++++
 2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c
index 23317a2e2d9..35f267db588 100644
--- a/gcc/fortran/simplify.c
+++ b/gcc/fortran/simplify.c
@@ -4168,7 +4168,17 @@ simplify_bound_dim (gfc_expr *array, gfc_expr *kind, int d, int upper,
     {
       if (upper)
 	{
-	  if (!gfc_ref_dimen_size (&ref->u.ar, d - 1, &result->value.integer, NULL))
+	  int d2 = 0, cnt = 0;
+	  for (int idx = 0; idx < ref->u.ar.dimen; ++idx)
+	    {
+	      if (ref->u.ar.dimen_type[idx] == DIMEN_ELEMENT)
+		d2++;
+	      else if (cnt < d - 1)
+		cnt++;
+	      else
+		break;
+	    }
+	  if (!gfc_ref_dimen_size (&ref->u.ar, d2 + d - 1, &result->value.integer, NULL))
 	    goto returnNull;
 	}
       else
diff --git a/gcc/testsuite/gfortran.dg/ubound_1.f90 b/gcc/testsuite/gfortran.dg/ubound_1.f90
new file mode 100644
index 00000000000..7c588b026e1
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/ubound_1.f90
@@ -0,0 +1,22 @@
+! { dg-do run }
+!
+! PR fortran/99027
+!
+  program test
+    integer, dimension (1:3,1:6) :: array
+    integer, dimension (2:5,3:7,4:9,-4:2) :: array2
+
+    if (any ([4] /= ubound (array (1, 1:4)))) stop 1
+    if (4 /= ubound (array (1, 1:4), dim=1)) stop 2
+
+    if (any (ubound (array2 (3,3,4,:))        /= [4+1+2])) stop 3
+    if (     ubound (array2 (3,3,4,:), dim=1) /=  4+1+2 ) stop 4
+
+    if (any (ubound (array2 (3,:,4,:))        /= [7-3+1, 4+1+2])) stop 5
+    if (     ubound (array2 (3,:,4,:), dim=1) /=  7-3+1       ) stop 6
+    if (     ubound (array2 (3,:,4,:), dim=2) /=         4+1+2) stop 7
+    if (any (ubound (array2 (3,:,4:4,:))        /= [7-3+1, 1, 4+1+2])) stop 8
+    if (     ubound (array2 (3,:,4:4,:), dim=1) /=  7-3+1          ) stop 9
+    if (     ubound (array2 (3,:,4:4,:), dim=2) /=         1       ) stop 10
+    if (     ubound (array2 (3,:,4:4,:), dim=3) /=            4+1+2) stop 11
+  end program test

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Patch] Fortran: Fix ubound simplifcation [PR99027]
  2021-02-13 20:49 [Patch] Fortran: Fix ubound simplifcation [PR99027] Tobias Burnus
@ 2021-02-17 21:55 ` Thomas Koenig
  0 siblings, 0 replies; 2+ messages in thread
From: Thomas Koenig @ 2021-02-17 21:55 UTC (permalink / raw)
  To: Tobias Burnus, gcc-patches, fortran

Hi Tobias,

> The problem which accessing dim=<n> for an expression is that
> when the argument is an array, dim=<n> and expr->rank are relative
> to the resulting array – but the array-ref also contains the
> DIMEN_ELEMENT which does not count for dim=/rank.
> 
> OK for the trunk? (Reported against GCC 11, but I wonder whether
> we should still apply it to GCC 10.)

OK, also OK for gcc 10 (a clear wrong-code bug with a
clear solution is fine, IMHO).

Best regards

	Thomas

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-02-17 21:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-13 20:49 [Patch] Fortran: Fix ubound simplifcation [PR99027] Tobias Burnus
2021-02-17 21:55 ` 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).