Bug 38852 - [4.3] UBOUND fails for negative stride triplets
[4.3] UBOUND fails for negative stride triplets
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: fortran
4.4.0
: P3 normal
: ---
Assigned To: Paul Thomas
: wrong-code
: 39006 (view as bug list)
Depends on:
Blocks: 32834
  Show dependency treegraph
 
Reported: 2009-01-14 22:00 UTC by Dick Hendrickson
Modified: 2009-02-19 06:44 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.2.3 4.3.3 4.4.0 4.1.2
Last reconfirmed: 2009-01-22 12:39:50


Attachments
A provisional patch for the PR (600 bytes, patch)
2009-01-25 21:08 UTC, Paul Thomas
Details | Diff
Updated patch (2.45 KB, patch)
2009-01-28 20:52 UTC, Paul Thomas
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Dick Hendrickson 2009-01-14 22:00:03 UTC
The UBOUND function returns zero for subscript triplets that have a negative stride, such as 5:4:-1.  Fails with both compile-time expressions and run-time expressions.  LBOUND and SHAPE work correctly on the same triplets.

This might be related to 35685 since I think this started soon after 35685 was closed.

Dick Hendrickson


      program try_je0031

! fails on Windows XP
! gcc version 4.4.0 20081219 (experimental) [trunk revision 142842] (GCC)

      integer ida(4)
      real dda(5,5,5,5,5)
      call JE0031(IDA,DDA,2,5,-2)

      end 

      SUBROUTINE JE0031(IDA,DDA,nf2,nf5,mf2)
      INTEGER IDA(4)
      REAL DLA(:,:,:,:)
      REAL DDA(5,5,5,5,5)
      POINTER DLA
      TARGET DDA

      DLA => DDA(2:3, 1:3:2, 5:4:-1, NF2, NF5:NF2:MF2)
      IDA = UBOUND(DLA)
      if (any(ida /= 2)) print *, '    run-time ubound', ida

      DLA => DDA(2:3, 1:3:2, 5:4:-1, 2, 5:2:-2)
      IDA = UBOUND(DLA)
      if (any(ida /= 2)) print *, 'compile-time ubound', ida

!these work
      DLA => DDA(2:3, 1:3:2, 5:4:-1, 2, 5:2:-2)
      IDA = shape(DLA)
      if (any(ida /= 2)) print *, ' compile-time shape', ida

      DLA => DDA(2:3, 1:3:2, 5:4:-1, 2, 5:2:-2)
      IDA = LBOUND(DLA)
      if (any(ida /= 1)) print *, 'compile-time lbound', ida

      END SUBROUTINE


C:\gfortran>gfortran try_je0031.f

C:\gfortran>a
     run-time ubound           2           2           0           0
 compile-time ubound           2           2           0           0
Comment 1 Dominique d'Humieres 2009-01-14 22:08:28 UTC
Confirmed on i686-apple-darwin9/trunk, it also fails with gfortran 4.2.3 and 4.3.3.
Comment 2 Joost VandeVondele 2009-01-15 05:56:38 UTC
looks like this always failed ?
Comment 3 Mikael Morin 2009-01-19 18:48:22 UTC
DLA => DDA(2:3, 1:3:2, 5:4:-1, NF2, NF5:NF2:MF2)

The descriptor built for DLA has negative strides for dimension >= 3. 
This makes ubound fail. 
Comment 4 Mikael Morin 2009-01-20 22:29:58 UTC
(In reply to comment #3)
> DLA => DDA(2:3, 1:3:2, 5:4:-1, NF2, NF5:NF2:MF2)
> 
> The descriptor built for DLA has negative strides for dimension >= 3. 
> This makes ubound fail. 
> 
Forget this

DLA => DDA(2:3, 1:3:2, 5:4:-1, NF2, NF5:NF2:MF2)

    dla.dim[0].lbound = 1;
    dla.dim[0].ubound = 2;
    dla.dim[0].stride = 1;
    dla.dim[1].lbound = 1;
    dla.dim[1].ubound = 2;
    dla.dim[1].stride = 10;
    dla.dim[2].lbound = 1;
    dla.dim[2].ubound = 2;
    dla.dim[2].stride = -25;
    dla.dim[3].lbound = 1;
    dla.dim[3].ubound = D.1563 + 1;
    dla.dim[3].stride = D.1561 * 625;

There is a distinction to make between user-provided bounds and front-end-generated bounds. 
   In the former case (original array) the array is non-empty if ubound > lbound and stride > 0 or if ubound < lbound and stride < 0. 
   In the latter case, it is non-empty if ubound > lbound only. Comparing ubound and lbound according to the stride to check for zero-sized arrays doesn't make sense in this case (see dimension 2 of dla). 
Comment 5 Paul Thomas 2009-01-22 12:39:50 UTC
(In reply to comment #4)
> (In reply to comment #3)

>    In the latter case, it is non-empty if ubound > lbound only. Comparing
> ubound and lbound according to the stride to check for zero-sized arrays
> doesn't make sense in this case (see dimension 2 of dla). 

Indeed - the fix can be done in gfc_conv_intrinsic_bound.  I will regtest tonight and submit accordingly.

Paul
Comment 6 Paul Thomas 2009-01-25 18:21:54 UTC
(In reply to comment #5)
> (In reply to comment #4)
> > (In reply to comment #3)
> 
> >    In the latter case, it is non-empty if ubound > lbound only. Comparing
> > ubound and lbound according to the stride to check for zero-sized arrays
> > doesn't make sense in this case (see dimension 2 of dla). 
> 
> Indeed - the fix can be done in gfc_conv_intrinsic_bound.  I will regtest
> tonight and submit accordingly.
> 
> Paul
> 

Wrong! - this patch causes several regressions.  I'll have to see how the code in the pointer assignment should be changed.

Cheers

Paul
Comment 7 Paul Thomas 2009-01-25 21:08:44 UTC
Created attachment 17182 [details]
A provisional patch for the PR

It take back what I said previously:-)

The attached bootstraps and regtests OK.

I need to put some thought into the possibility that some fringe cases might fail, where the lbound is unity.

Cheers

Paul
Comment 8 Paul Thomas 2009-01-27 12:16:42 UTC
I have done most of the work on the corner cases and the fix below looks good.  I am marking it as pending for temporary book-keeping purposes.

Paul
Comment 9 Paul Thomas 2009-01-28 17:31:02 UTC
*** Bug 39006 has been marked as a duplicate of this bug. ***
Comment 10 Paul Thomas 2009-01-28 17:33:39 UTC
Marked up the severity to that of PR39006.

Paul
Comment 11 Paul Thomas 2009-01-28 20:52:22 UTC
Created attachment 17204 [details]
Updated patch

Following discussion on fortran list and #gfortran, a more restrictive test is needed.
Comment 12 Paul Thomas 2009-01-28 21:49:09 UTC
Subject: Bug 38852

Author: pault
Date: Wed Jan 28 21:48:53 2009
New Revision: 143743

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=143743
Log:
2009-01-28  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/38852
	PR fortran/39006
	* trans-intrinsic.c (gfc_conv_intrinsic_bound): Use the array
	descriptor ubound for UBOUND, when the array lbound == 1.

2009-01-28  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/38852
	PR fortran/39006
	* gfortran.dg/bound_6.f90: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/bound_6.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-intrinsic.c
    trunk/gcc/testsuite/ChangeLog

Comment 13 Paul Thomas 2009-01-28 21:52:47 UTC
Fixed on trunk.

Paul
Comment 14 Paul Thomas 2009-02-19 06:43:47 UTC
Subject: Bug 38852

Author: pault
Date: Thu Feb 19 06:43:15 2009
New Revision: 144286

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=144286
Log:
2009-02-19  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/38852
	PR fortran/39006
	* trans-intrinsic.c (gfc_conv_intrinsic_bound): Use the array
	descriptor ubound for UBOUND, when the array lbound == 1.

2009-02-19  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/38852
	PR fortran/39006
	* gfortran.dg/bound_6.f90: New test.

Added:
    branches/gcc-4_3-branch/gcc/testsuite/gfortran.dg/bound_6.f90
Modified:
    branches/gcc-4_3-branch/gcc/fortran/ChangeLog
    branches/gcc-4_3-branch/gcc/fortran/trans-intrinsic.c
    branches/gcc-4_3-branch/gcc/testsuite/ChangeLog

Comment 15 Paul Thomas 2009-02-19 06:44:18 UTC
Fixed on trunk and 4.3

Thanks for the report

Paul