Bug 67679 - [7/8/9 Regression] -Wunitialized reports on compiler-generated variables
Summary: [7/8/9 Regression] -Wunitialized reports on compiler-generated variables
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 5.2.1
: P4 normal
Target Milestone: 7.5
Assignee: Thomas Koenig
URL:
Keywords: diagnostic
Depends on:
Blocks: Wuninitialized
  Show dependency treegraph
 
Reported: 2015-09-22 15:02 UTC by Joost VandeVondele
Modified: 2019-02-10 15:38 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2015-09-22 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joost VandeVondele 2015-09-22 15:02:08 UTC
With gcc 5.2 I see:

> gfortran -c -g -O2 -Wuninitialized -cpp bug.f90
bug.f90:15:0:

       DIMENSION(:, :, :)                     :: work
 ^
Warning: ‘work.dim[2].stride’ may be used uninitialized in this function [-Wmaybe-uninitialized]
bug.f90:15:0: Warning: ‘work.dim[1].stride’ may be used uninitialized in this function [-Wmaybe-uninitialized]
bug.f90:15:0: Warning: ‘work.offset’ may be used uninitialized in this function [-Wmaybe-uninitialized]

where the problem is that compiler generated variables appear in the output. 
(.stride happens x178, .ubound x 111 and .lbound x9 in a CP2K compile).

Some strange forms (not in this testcase) include also :

‘MEM[(integer(kind=4)[2] *)&dim1swp_stat][1]’ may be used uninitialized in this function

should I try to reduce a testcase for the latter ?

testcase:

> cat bug.f90
MODULE memory_utilities
  INTEGER, PARAMETER :: dp=8, dp_size=8
  CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'memory_utilities'
CONTAINS
  SUBROUTINE reallocate_c3(p,lb1_new,ub1_new,lb2_new,ub2_new,lb3_new,ub3_new)
    COMPLEX(KIND=dp), DIMENSION(:, :, :), &
      POINTER                                :: p
    INTEGER, INTENT(IN)                      :: lb1_new, ub1_new, lb2_new, &
                                                ub2_new, lb3_new, ub3_new
    CHARACTER(LEN=*), PARAMETER :: routineN = 'reallocate_c3', &
      routineP = moduleN//':'//routineN
    COMPLEX(KIND=dp), PARAMETER              :: zero = (0.0_dp,0.0_dp)
    INTEGER, PARAMETER                       :: t_size = 2*dp_size
    COMPLEX(KIND=dp), ALLOCATABLE, &
      DIMENSION(:, :, :)                     :: work

    INTEGER :: istat, lb1, lb1_old, lb2, lb2_old, lb3, lb3_old, &
      ub1, ub1_old, ub2, ub2_old, ub3, ub3_old

    IF (ASSOCIATED(p)) THEN
       lb1_old = LBOUND(p,1)
       ub1_old = UBOUND(p,1)
       lb2_old = LBOUND(p,2)
       ub2_old = UBOUND(p,2)
       lb3_old = LBOUND(p,3)
       ub3_old = UBOUND(p,3)
       lb1 = MAX(lb1_new,lb1_old)
       ub1 = MIN(ub1_new,ub1_old)
       lb2 = MAX(lb2_new,lb2_old)
       ub2 = MIN(ub2_new,ub2_old)
       lb3 = MAX(lb3_new,lb3_old)
       ub3 = MIN(ub3_new,ub3_old)
       ALLOCATE (work(lb1:ub1,lb2:ub2,lb3:ub3),STAT=istat)
       IF (istat /= 0) THEN
          CALL stop_memory(routineN,moduleN,__LINE__,&
                           "work",t_size*(ub1-lb1+1)*&
                                         (ub2-lb2+1)*&
                                         (ub3-lb3+1))
       END IF
       work(lb1:ub1,lb2:ub2,lb3:ub3) = p(lb1:ub1,lb2:ub2,lb3:ub3)
       DEALLOCATE (p)
    END IF

    ALLOCATE (p(lb1_new:ub1_new,lb2_new:ub2_new,lb3_new:ub3_new),STAT=istat)
    IF (istat /= 0) THEN
       CALL stop_memory(routineN,moduleN,__LINE__,&
                        "p",t_size*(ub1_new-lb1_new+1)*&
                                   (ub2_new-lb2_new+1)*&
                                   (ub3_new-lb3_new+1))
    END IF
    p(:,:,:) = zero

    IF (ASSOCIATED(p).AND.ALLOCATED(work)) THEN
       p(lb1:ub1,lb2:ub2,lb3:ub3) = work(lb1:ub1,lb2:ub2,lb3:ub3)
       DEALLOCATE (work)
    END IF
  END SUBROUTINE reallocate_c3
END MODULE memory_utilities
Comment 1 Dominique d'Humieres 2015-09-22 16:14:32 UTC
Confirmed from 4.8 up to trunk (6.0). Even worse with

[Book15] f90/bug% gfca pr67679.f90 -cpp -Wuninitialized -O -c
pr67679.f90:15:0:

       DIMENSION(:, :, :)                     :: work
^
Warning: 'work.dim[2].stride' may be used uninitialized in this function [-Wmaybe-uninitialized]
pr67679.f90:15:0: Warning: 'work.dim[1].stride' may be used uninitialized in this function [-Wmaybe-uninitialized]
pr67679.f90:15:0: Warning: 'work.offset' may be used uninitialized in this function [-Wmaybe-uninitialized]
pr67679.f90:54:0:

        p(lb1:ub1,lb2:ub2,lb3:ub3) = work(lb1:ub1,lb2:ub2,lb3:ub3)
^
Warning: 'ub3' may be used uninitialized in this function [-Wmaybe-uninitialized]
pr67679.f90:54:0: Warning: 'ub2' may be used uninitialized in this function [-Wmaybe-uninitialized]
pr67679.f90:54:0: Warning: 'ub1' may be used uninitialized in this function [-Wmaybe-uninitialized]
pr67679.f90:54:0: Warning: 'lb3' may be used uninitialized in this function [-Wmaybe-uninitialized]
pr67679.f90:54:0: Warning: 'lb2' may be used uninitialized in this function [-Wmaybe-uninitialized]
pr67679.f90:54:0: Warning: 'lb1' may be used uninitialized in this function [-Wmaybe-uninitialized]
Comment 2 janus 2017-07-28 12:58:12 UTC
Here is another test case with similar behavior:



subroutine s(h, Gmin, r)

   implicit none
   real, intent(in) ::  Gmin(3), h(3)
   integer, intent(inout) :: r

   integer :: x_min(3), x_max(3), k, iStat
   logical, dimension(:), allocatable :: check

   do k = 1,1
      x_min(k) = int(Gmin(k)*h(k))
      x_max(k) = int(Gmin(k)*h(k))
   end do

   allocate(check(x_min(1):x_max(1)),stat=iStat)

   check(:) = .false.

   do k = x_min(1),x_max(1)
            r = r + 1
   end do

end


shows things like ...

Warning: ‘check.dim[0].lbound’ may be used uninitialized in this function [-Wmaybe-uninitialized]
Warning: ‘check.dim[0].ubound’ may be used uninitialized in this function [-Wmaybe-uninitialized]
Warning: ‘check.offset’ may be used uninitialized in this function [-Wmaybe-uninitialized]

... with -O1 and higher.

I see it with all versions starting from 4.7, while 4.4 and 4.6 do not seem to be affected, so I guess this is a regression?
Comment 3 janus 2017-08-24 08:45:00 UTC
See also PR 66459.
Comment 4 Jakub Jelinek 2018-10-26 10:18:34 UTC
GCC 6 branch is being closed
Comment 5 Thomas Koenig 2019-02-03 13:52:17 UTC
This patch

Index: trans-array.c
===================================================================
--- trans-array.c       (Revision 268432)
+++ trans-array.c       (Arbeitskopie)
@@ -5960,19 +5960,7 @@ gfc_array_allocate (gfc_se * se, gfc_expr * expr,
     }
 
   set_descriptor = gfc_finish_block (&set_descriptor_block);
-  if (status != NULL_TREE)
-    {
-      cond = fold_build2_loc (input_location, EQ_EXPR,
-                         logical_type_node, status,
-                         build_int_cst (TREE_TYPE (status), 0));
-      gfc_add_expr_to_block (&se->pre,
-                fold_build3_loc (input_location, COND_EXPR, void_type_node,
-                                 cond,
-                                 set_descriptor,
-                                 build_empty_stmt (input_location)));
-    }
-  else
-      gfc_add_expr_to_block (&se->pre, set_descriptor);
+  gfc_add_expr_to_block (&se->pre, set_descriptor);
 
   return true;
 }

gets rid of the undefined warnings, but it would also change
the array bounds after a failed allocation.  Not sure if this
is standard conforming (see PR 49755).
Comment 6 Thomas Koenig 2019-02-03 14:35:58 UTC
(In reply to Thomas Koenig from comment #5)

> Not sure if this
> is standard conforming (see PR 49755).

Actually, it's not.
Comment 7 Thomas Koenig 2019-02-03 19:38:59 UTC
Author: tkoenig
Date: Sun Feb  3 19:38:25 2019
New Revision: 268502

URL: https://gcc.gnu.org/viewcvs?rev=268502&root=gcc&view=rev
Log:
2019-02-03  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/67679
	* trans-array.c (gfc_array_allocate):  For setting the bounds on
	the new array, add a condition for a not previously allocated
	variable.

2019-02-03  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/67679
	* gfortran.dg/warn_undefined_1.f90: New test.
	* gfortran.dg/coarray_lock_7.f90: Fix patterns in test.


Modified:
    trunk/gcc/fortran/trans-array.c
    trunk/gcc/testsuite/gfortran.dg/coarray_lock_7.f90
Comment 8 Thomas Koenig 2019-02-05 21:13:13 UTC
Author: tkoenig
Date: Tue Feb  5 21:12:41 2019
New Revision: 268559

URL: https://gcc.gnu.org/viewcvs?rev=268559&root=gcc&view=rev
Log:
2019-02-05  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/67679
	Backport from trunk
	* trans-array.c (gfc_array_allocate):  For setting the bounds on
	the new array, add a condition for a not previously allocated
	variable.

2019-02-05  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/67679
	Backport from trunk
	* gfortran.dg/warn_undefined_1.f90: New test.
	* gfortran.dg/coarray_lock_7.f90: Fix patterns in test.


Added:
    branches/gcc-8-branch/gcc/testsuite/gfortran.dg/warn_undefined_1.f90
Modified:
    branches/gcc-8-branch/gcc/fortran/ChangeLog
    branches/gcc-8-branch/gcc/fortran/trans-array.c
    branches/gcc-8-branch/gcc/testsuite/ChangeLog
    branches/gcc-8-branch/gcc/testsuite/gfortran.dg/coarray_lock_7.f90
Comment 9 Thomas Koenig 2019-02-05 21:23:39 UTC
Author: tkoenig
Date: Tue Feb  5 21:23:07 2019
New Revision: 268560

URL: https://gcc.gnu.org/viewcvs?rev=268560&root=gcc&view=rev
Log:
2019-02-05  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/67679
	Backport from trunk
	* trans-array.c (gfc_array_allocate):  For setting the bounds on
	the new array, add a condition for a not previously allocated
	variable.

2019-02-05  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/67679
	Backport from trunk
	* gfortran.dg/warn_undefined_1.f90: New test.
	* gfortran.dg/coarray_lock_7.f90: Fix patterns in test.


Added:
    branches/gcc-7-branch/gcc/testsuite/gfortran.dg/warn_undefined_1.f90
Modified:
    branches/gcc-7-branch/gcc/fortran/ChangeLog
    branches/gcc-7-branch/gcc/fortran/trans-array.c
    branches/gcc-7-branch/gcc/testsuite/ChangeLog
    branches/gcc-7-branch/gcc/testsuite/gfortran.dg/coarray_lock_7.f90
Comment 10 Thomas Koenig 2019-02-05 21:24:22 UTC
Fixed on all open branches, closing.
Comment 11 Thomas Koenig 2019-02-10 15:38:51 UTC
Author: tkoenig
Date: Sun Feb 10 15:38:19 2019
New Revision: 268747

URL: https://gcc.gnu.org/viewcvs?rev=268747&root=gcc&view=rev
Log:
2019-02-10  Thomas Koenig  <tkoenig@gcc.gnu.org>

    PR fortran/67679
    * gfortran.dg/warn_undefined_1.f90: New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/warn_undefined_1.f90
Modified:
    trunk/gcc/testsuite/ChangeLog