Bug 99027 - Incorrect ubound result
Summary: Incorrect ubound result
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 11.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2021-02-09 15:30 UTC by Andrew Burgess
Modified: 2021-02-22 10:48 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-02-13 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Burgess 2021-02-09 15:30:30 UTC
I believe I have run into a case where ubound is giving the wrong result.

Given:

  program test
    integer, dimension (1:3,1:6) :: array
    print *, "ubound = ", ubound (array (1, 1:4))
  end program test

I see the output:

  ubound =            1

When I would have expected:

  ubound =            4

Due to array (1, 1:4) being a 4 element 1-dimensional array.

I am seeing this behaviour in my default Fedora compiler:

  GNU Fortran (GCC) 9.3.1 20200408 (Red Hat 9.3.1-2)

But I also build current HEAD from git (26a3f288f18) and am still seeing this issue:

  GNU Fortran (GCC) 11.0.0 20210209 (experimental)

Interestingly, if I change the test program to:

  program test
    integer, dimension (1:3,1:6) :: array
    print *, "ubound = ", ubound (array (1:2, 1:4))
  end program test

Then I do now see what I would expect for this case:

  ubound =            2           4
Comment 1 Dominique d'Humieres 2021-02-13 11:59:35 UTC
Confirmed since at least GCC7.
Comment 2 Tobias Burnus 2021-02-13 19:42:55 UTC
Untested patch:

diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c
index 23317a2e2d9..6a180ff40f2 100644
--- a/gcc/fortran/simplify.c
+++ b/gcc/fortran/simplify.c
@@ -4170,3 +4170,13 @@ simplify_bound_dim (gfc_expr *array, gfc_expr *kind, int d, int 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 == 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;
Comment 3 GCC Commits 2021-02-19 09:42:41 UTC
The master branch has been updated by Tobias Burnus <burnus@gcc.gnu.org>:

https://gcc.gnu.org/g:f600f271b10d0214b111f2aa52a3d5740477e139

commit r11-7292-gf600f271b10d0214b111f2aa52a3d5740477e139
Author: Tobias Burnus <tobias@codesourcery.com>
Date:   Fri Feb 19 10:42:15 2021 +0100

    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.
Comment 4 Andrew Burgess 2021-02-20 20:04:26 UTC
Can confirm that with latest GCC HEAD I am now seeing the results that I expect.  Thanks for the quick fix.
Comment 5 GCC Commits 2021-02-22 10:31:33 UTC
The releases/gcc-10 branch has been updated by Tobias Burnus <burnus@gcc.gnu.org>:

https://gcc.gnu.org/g:700dcc60b5646cc64ae3ba72a79a7542b4902b50

commit r10-9380-g700dcc60b5646cc64ae3ba72a79a7542b4902b50
Author: Tobias Burnus <tobias@codesourcery.com>
Date:   Fri Feb 19 10:42:15 2021 +0100

    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.
    
    (cherry picked from commit f600f271b10d0214b111f2aa52a3d5740477e139)
Comment 6 Tobias Burnus 2021-02-22 10:48:07 UTC
Now also FIXED for the GCC 10 branch.

(In reply to Andrew Burgess from comment #4)
> Can confirm that with latest GCC HEAD I am now seeing the results that I
> expect.  Thanks for the quick fix.

:-) You are welcome. Thanks for the report!