Bug 57023 - [4.8 Regression] Not packing arrays with changing variable used for size
Summary: [4.8 Regression] Not packing arrays with changing variable used for size
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.9.0
: P4 normal
Target Milestone: 4.8.5
Assignee: Thomas Koenig
URL:
Keywords: wrong-code
Depends on:
Blocks: 32834
  Show dependency treegraph
 
Reported: 2013-04-21 11:12 UTC by Thomas Koenig
Modified: 2015-01-24 15:22 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-04-21 00:00:00


Attachments
Proposed patch (917 bytes, patch)
2015-01-20 07:03 UTC, Thomas Koenig
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Koenig 2013-04-21 11:12:58 UTC
Doing what the program below does is _extremely_ bad style -
changing n which is used for specifying the array size.

However, I think it is legal, and the expected output
is

1 1 1 1 0
1 1 1 1 0
1 1 1 1 0
1 1 1 1 0
0 0 0 0 0

Actual output is

 1 1 1 1 1
 1 1 1 1 1
 1 1 1 1 1
 1 0 0 0 0
 0 0 0 0 0

module mymod
contains
  subroutine foo(a,n)
    integer, dimension(n,n), intent(inout) :: a
    integer :: n
    n = n - 1
    call baz(a(1:n,1:n),n)
  end subroutine foo

  subroutine baz(a,n)
    integer, dimension(n,n), intent(inout) :: a
    a = 1
  end subroutine baz
end module mymod

program main
  use mymod
  integer, dimension(5,5) :: a
  n = 5
  a = 0
  call foo(a,n)
  print '(5I2)',a
end program main

The problem is in gfc_full_array_ref_p, which uses gfc_dep_compare_expr
to compare the upper bounds (n against n) without noting that n
has been changed in the meantime.

Proposed solution:

a) Always warn about such horrible code

b) If something like that happens, don't assume a full reference,
   and don't set the contiguous argument to gfc_full_array_ref_p to
   true.

The code was introduced in rev. 156749, btw, quite some time ago.
Comment 1 Dominique d'Humieres 2013-04-21 13:00:36 UTC
Revision 162456 (2010-07-23) is OK, but not revision 1635293 (2010-08-24). The test gives the correct result with the following change

--- pr57023.f90	2013-04-21 13:15:55.000000000 +0200
+++ pr57023_db.f90	2013-04-21 14:11:58.000000000 +0200
@@ -2,9 +2,9 @@ module mymod
 contains
   subroutine foo(a,n)
     integer, dimension(n,n), intent(inout) :: a
-    integer :: n
-    n = n - 1
-    call baz(a(1:n,1:n),n)
+    integer :: m, n
+    m = n - 1
+    call baz(a(1:m,1:m),m)
   end subroutine foo
 
   subroutine baz(a,n)
Comment 2 Thomas Koenig 2013-04-21 15:54:00 UTC
We also have to watch out for

module mymod
contains
  subroutine foo(a,n)
    integer, dimension(n,n), intent(inout) :: a
    integer :: n
    call decrement(n)
    call baz(a(1:n,1:n),n)
  end subroutine foo

  subroutine baz(a,n)
    integer, dimension(n,n), intent(inout) :: a
    a = 1
  end subroutine baz

  subroutine decrement(n)
    integer :: n
    n = n - 1
  end subroutine decrement
end module mymod

program main
  use mymod
  integer, dimension(5,5) :: a
  n = 5
  a = 0
  call foo(a,n)
  print '(5I2)',a
end program main
Comment 3 Richard Biener 2014-06-12 13:45:52 UTC
The 4.7 branch is being closed, moving target milestone to 4.8.4.
Comment 4 Jakub Jelinek 2014-12-19 13:35:43 UTC
GCC 4.8.4 has been released.
Comment 5 Thomas Koenig 2015-01-19 21:24:09 UTC
I have something.  Let's see if it works...
Comment 6 Thomas Koenig 2015-01-20 07:03:40 UTC
Created attachment 34493 [details]
Proposed patch

This patch creates a temporary if a bound of the
array contains a dummy variable which is not
INTENT(IN) (so it could potentially be changed
by the user).

Modern code should always have INTENT(IN) for
something passed as array bounds, anyway.  We
should be correct for all cases, but not try
to optimize the pathological ones.
Comment 7 Thomas Koenig 2015-01-21 19:41:26 UTC
Author: tkoenig
Date: Wed Jan 21 19:40:54 2015
New Revision: 219963

URL: https://gcc.gnu.org/viewcvs?rev=219963&root=gcc&view=rev
Log:
2015-01-21  Thomas Koenig  <tkoenig@netcologne.de>

	PR fortran/57023
	* dependency.c (callback_dummy_intent_not_int):  New function.
	(dummy_intent_not_in):  New function.
	(gfc_full_array_ref_p):  Use dummy_intent_not_in.

2015-01-21  Thomas Koenig  <tkoenig@netcologne.de>

	PR fortran/57023
	* gfortran.dg/internal_pack_15.f90:  New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/internal_pack_15.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/dependency.c
    trunk/gcc/testsuite/ChangeLog
Comment 8 Thomas Koenig 2015-01-24 12:51:24 UTC
Author: tkoenig
Date: Sat Jan 24 12:50:51 2015
New Revision: 220080

URL: https://gcc.gnu.org/viewcvs?rev=220080&root=gcc&view=rev
Log:
2015-01-24  Thomas Koenig  <tkoenig@netcologne.de>

	Backport from trunk
	PR fortran/57023
	* dependency.c (callback_dummy_intent_not_int):  New function.
	(dummy_intent_not_in):  New function.
	(gfc_full_array_ref_p):  Use dummy_intent_not_in.

2015-01-24  Thomas Koenig  <tkoenig@netcologne.de>

	Backport from trunk
	PR fortran/57023
	* gfortran.dg/internal_pack_15.f90:  New test.

Added:
    branches/gcc-4_9-branch/gcc/testsuite/gfortran.dg/internal_pack_15.f90
Modified:
    branches/gcc-4_9-branch/gcc/fortran/ChangeLog
    branches/gcc-4_9-branch/gcc/fortran/dependency.c
    branches/gcc-4_9-branch/gcc/testsuite/ChangeLog
Comment 9 Thomas Koenig 2015-01-24 15:21:29 UTC
Author: tkoenig
Date: Sat Jan 24 15:20:56 2015
New Revision: 220082

URL: https://gcc.gnu.org/viewcvs?rev=220082&root=gcc&view=rev
Log:
2015-01-24  Thomas Koenig  <tkoenig@netcologne.de>

	Backport from trunk
	PR fortran/57023
	* dependency.c (callback_dummy_intent_not_int):  New function.
	(dummy_intent_not_in):  New function.
	(gfc_full_array_ref_p):  Use dummy_intent_not_in.

2015-01-24  Thomas Koenig  <tkoenig@netcologne.de>

	Backport from trunk
	PR fortran/57023
	* gfortran.dg/internal_pack_15.f90:  New test.


Modified:
    branches/gcc-4_8-branch/gcc/fortran/ChangeLog
    branches/gcc-4_8-branch/gcc/fortran/dependency.c
    branches/gcc-4_8-branch/gcc/testsuite/ChangeLog
Comment 10 Thomas Koenig 2015-01-24 15:22:48 UTC
Fixed on all open branches, closing.