Bug 66128 - ICE for some intrinsics with zero sized array parameter
Summary: ICE for some intrinsics with zero sized array parameter
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 5.1.1
: P4 normal
Target Milestone: 8.0
Assignee: Thomas Koenig
URL:
Keywords:
Depends on: 84697
Blocks:
  Show dependency treegraph
 
Reported: 2015-05-12 16:27 UTC by Gerhard Steinmetz
Modified: 2018-03-11 20:49 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2015-05-13 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Gerhard Steinmetz 2015-05-12 16:27:03 UTC
This simplified code snippet with a zero sized array parameter z ...
   program p
      integer, parameter :: z(0) = 0
      print *, any(z > 0)
      print *, all(z > 0)
   end

or this variation ...
   program p
      integer, parameter :: z(1:0) = 0
      print *, any(z > 0)
      print *, all(z > 0)
   end

prints (with gfortran 5.1.1 on SUSE Linux 13.2, 64 bit)
internal compiler error: in gfc_conv_intrinsic_anyall, at fortran/trans-intrinsic.c:3149

Whereas, without declaring z as a parameter it works, e.g.
   program p
      integer :: z(0) = 0
      print *, any(z > 0)
      print *, all(z > 0)
   end

Kind regards.
Comment 1 Gerhard Steinmetz 2015-05-12 16:30:19 UTC
Some more examples with other error messages.

This one ...
   program p
      integer, parameter :: z(0) = 0
      print *, count(z > 0)
   end

yields :
internal compiler error: in gfc_conv_intrinsic_count, at fortran/trans-intrinsic.c:3233

This one ...
   program p
      integer, parameter :: z(0) = 0
      print *, iall(z, z > 0)
      print *, iany(z, z > 0)
      print *, iparity(z, z > 0)
      print *, parity(z > 0)
      print *, product(z, z > 0)
      print *, sum(z, z > 0)
   end

yields :
internal compiler error: in gfc_conv_intrinsic_arith, at fortran/trans-intrinsic.c:3357

This one ...
   program p
      integer, parameter :: z(0) = 0
      print *, minval(z, z > 0)
      print *, maxval(z, z > 0)
   end

yields :
internal compiler error: in gfc_conv_intrinsic_minmaxval, at fortran/trans-intrinsic.c:4253
Comment 2 kargl 2015-05-13 17:29:47 UTC
Confirmed.

I glance at the simplification code suggests that gfortran
is not considering the possibility of zero-sized constant
arrays.  Not sure how to fix this one.
Comment 3 Harald Anlauf 2018-03-01 21:43:47 UTC
Maybe some kind of "shortcut" (similar to Steve's fix for pr83998) can
solve this.  Not sure where this would fit in.
Comment 4 kargl 2018-03-01 22:31:42 UTC
(In reply to Harald Anlauf from comment #3)
> Maybe some kind of "shortcut" (similar to Steve's fix for pr83998) can
> solve this.  Not sure where this would fit in.

I have a patch for at least the ANY and ALL problem. :-)
Comment 5 Steve Kargl 2018-03-02 01:19:34 UTC
On Thu, Mar 01, 2018 at 10:31:42PM +0000, kargl at gcc dot gnu.org wrote:
> (In reply to Harald Anlauf from comment #3)
> > Maybe some kind of "shortcut" (similar to Steve's fix for pr83998) can
> > solve this.  Not sure where this would fit in.
> 
> I have a patch for at least the ANY and ALL problem. :-)
> 

Well, the simply obvious patch

 gfc_expr *
 gfc_simplify_any (gfc_expr *mask, gfc_expr *dim)
 {
+  /* Check for zero sized array. sgk */
+  if (mask->rank > 0 && mask->shape == NULL)
+     return gfc_get_logical_expr (mask->ts.kind, &mask->where, false);
+
   return simplify_transformation (mask, dim, NULL, false, gfc_or);
 }

cause a bunch of regressions.  One is given by

! { dg-do run }
! PR 71795 - wrong result when putting an array constructor
! instide an iterator.
     program test
     implicit none
     integer :: i,n
     logical, dimension(1) :: ra
     logical :: rs
     integer, allocatable :: a(:)
     allocate ( a(1) )
     n = 1
     a = 2
     rs = any ( (/ (any(a(i).eq.(/1,2,3/)) ,i=1,n) /) )
     if (.not. rs) call abort
   end program test

which is odd.  The code never goes through the
if () statement as mask->shape is always non-null.
So, the code should compile as-if I never made
a change to gfc_simplify_any.

:(
Comment 6 Steve Kargl 2018-03-02 22:16:03 UTC
I've worked out the issues with regression in the testsuite.
(Well, I think I have.)
Comment 7 kargl 2018-03-03 02:06:16 UTC
Patch submitted.

https://gcc.gnu.org/ml/fortran/2018-03/msg00010.html

This fixes additional failures not included in Gerhard's
testcases.
Comment 8 kargl 2018-03-03 19:49:52 UTC
Author: kargl
Date: Sat Mar  3 19:49:20 2018
New Revision: 258223

URL: https://gcc.gnu.org/viewcvs?rev=258223&root=gcc&view=rev
Log:
2018-03-03  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/66128
	* simplify.c (is_size_zero_array): New function to check for size
	zero array.
	(gfc_simplify_all, gfc_simplify_any, gfc_simplify_count, 
	 gfc_simplify_iall, gfc_simplify_iany, gfc_simplify_iparity,
	 gfc_simplify_minval, gfc_simplify_maxval, gfc_simplify_norm2,
	 gfc_simplify_product, gfc_simplify_sum): Use it, and implement
	requirements from F2018.

2018-03-03  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/66128
	* gfortran.dg/zero_sized_8.f90: New test.

Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/simplify.c
    trunk/gcc/testsuite/ChangeLog
Comment 9 kargl 2018-03-03 20:00:30 UTC
Author: kargl
Date: Sat Mar  3 19:59:54 2018
New Revision: 258224

URL: https://gcc.gnu.org/viewcvs?rev=258224&root=gcc&view=rev
Log:
2018-03-03  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/66128
	* gfortran.dg/zero_sized_8.f90: Really commit new test.

Added:
    trunk/gcc/testsuite/gfortran.dg/zero_sized_8.f90
Modified:
    trunk/gcc/testsuite/ChangeLog
Comment 10 kargl 2018-03-03 20:08:03 UTC
Fixed on trunk.  The patch does not apply cleaning to branch-7
due to changes in minval max maxval simplification changes.
Closing as fixed.

Thanks for the bug report.
Comment 11 Thomas Koenig 2018-03-03 20:43:58 UTC
According to your mail at (In reply to kargl from comment #10)

> Closing as fixed.

https://gcc.gnu.org/ml/fortran/2018-03/msg00010.html still shows a few
tests that fail.

Let's keep this PR open to track these.
Comment 12 kargl 2018-03-04 01:22:45 UTC
(In reply to Thomas Koenig from comment #11)
> According to your mail at (In reply to kargl from comment #10)
> 
> > Closing as fixed.
> 
> https://gcc.gnu.org/ml/fortran/2018-03/msg00010.html still shows a few
> tests that fail.
> 
> Let's keep this PR open to track these.

I was going to submit individual PR's for the remaining issues
after writing code to actually test for the ICE.
Comment 13 kargl 2018-03-04 16:19:32 UTC
unassign myself
Comment 14 kargl 2018-03-05 21:08:46 UTC
(In reply to Thomas Koenig from comment #11)
> According to your mail at (In reply to kargl from comment #10)
> 
> > Closing as fixed.
> 
> https://gcc.gnu.org/ml/fortran/2018-03/msg00010.html still shows a few
> tests that fail.
> 
> Let's keep this PR open to track these.

First, I should note that the URL does not lead to a list
of tests that fail.  It points to a list of intrinsic
subrprogram where the Fortran standard specifically 
calls out behavior for dummy arguments associated with
a size zero actual argument.  No tests were written
or performed.

Item 1) is covered by PR 54613
Item 2) is now PR 84697.
Items 3)-5) do not exhibit any unexpected behavior.
Item 6) concerns old g77 compatibility routines
I won't check those as no one should be using these
routines.

This PR should be closed, but I'll leave that up to
others.
Comment 15 Thomas Koenig 2018-03-06 23:50:33 UTC
Author: tkoenig
Date: Tue Mar  6 23:50:01 2018
New Revision: 258305

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

	PR fortran/84697
	PR fortran/66128
	* expr.c (simplify_parameter_variable): If p is a size zero array
	and not an ARRAY_EXPR insert an empty array constructor and
	return.
	* gfortran.h: Add prototype for gfc_is_size_zero_array.
	* simplify.c (is_size_zero_array): Make non-static and rename into
	(gfc_is_size_zero_array):  Check for parameter arrays of zero
	size by comparing shape and absence of constructor.
	(gfc_simplify_all): Use gfc_is_size_zero_array instead of
	is_size_zero_array.
	(gfc_simplify_count): Likewise.
	(gfc_simplify_iall): Likewise.
	(gfc_simplify_iany): Likewise.
	(gfc_simplify_iparity): Likewise.
	(gfc_simplify_minval): Likewise.
	(gfc_simplify_maxval): Likewise.
	(gfc_simplify_product): Likewise.
	(gfc_simplify_sum): Likewise.

2017-03-06  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/84697
	PR fortran/66128
	* gfortran.dg/minmaxloc_zerosize_1.f90: New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/minmaxloc_zerosize_1.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/expr.c
    trunk/gcc/fortran/gfortran.h
    trunk/gcc/fortran/simplify.c
    trunk/gcc/testsuite/ChangeLog
Comment 16 Thomas Koenig 2018-03-07 18:58:59 UTC
I'll take a look at what's left (maxval for strings, at least)
and fix what is necessary to fix.
Comment 17 Thomas Koenig 2018-03-07 19:03:44 UTC
Here's a problem with maxval:

$ cat maxval_parameter_2.f90
! { dg-do run }
program main
  integer, dimension(0,3), parameter :: i = 0
  integer, dimension(0,3) :: j = 0
  print *,maxval(i,dim=1)
  print *,maxval(j,dim=1)
end program main
$ gfortran maxval_parameter_2.f90
$ ./a.out
 -2147483648
 -2147483648 -2147483648 -2147483648

The run-time result is correct, the simplified version isn't.
Comment 18 Thomas Koenig 2018-03-11 20:28:32 UTC
Author: tkoenig
Date: Sun Mar 11 20:28:00 2018
New Revision: 258435

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

	PR fortran/66128
	* simplify.c (simplify_transformation): Return default result for
	empty array argument.
	(gfc_simplify_all): Remove special-case handling for zerosize.
	(gfc_simplify_any): Likewise.
	(gfc_simplify_count): Likewise.
	(gfc_simplify_iall): Likewise.
	(gfc_simplify_iany): Likewise.
	(gfc_simplify_iparity): Likewise.
	(gfc_simplify_minval): Likewise.
	(gfc_simplify_maxval): Likewise.
	(gfc_simplify_norm2): Likewise.
	(gfc_simplify_product): Likewise.
	(gfc_simplify_sum): Likewise.

2018-03-11  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/66128
	* gfortran.dg/zero_sized_9.f90: New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/zero_sized_9.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/simplify.c
    trunk/gcc/testsuite/ChangeLog
Comment 19 Thomas Koenig 2018-03-11 20:49:48 UTC
Fixed, closing.