Bug 35759 - WHERE with overlap with ELSEWHERE error
Summary: WHERE with overlap with ELSEWHERE error
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.4.0
: P3 normal
Target Milestone: ---
Assignee: Paul Thomas
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2008-03-29 21:04 UTC by Dick Hendrickson
Modified: 2008-05-17 07:16 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2008-03-30 12:40:39


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dick Hendrickson 2008-03-29 21:04:12 UTC
The following program computes the wrong result for a WHERE where
different sections of the same array are in both the WHERE and
ELSEWHERE section.  It's "as if" the ELSEWHERE block were
ignored.

Dick Hendrickson

      program RG0023
! fails on Windows XP
! gcc version 4.4.0 20080312 (experimental) [trunk revision 133139]


      integer UDA1L(6)
      integer ::  UDA1R(6), expected(6) = (/2,0,5,0,3,0/)
      LOGICAL LDA(5)

      UDA1L(1:6) = 0
      uda1r = (/1,2,3,4,5,6/)
      lda = (/ (i/2*2 .ne. I, i=1,5) /)

      WHERE (LDA)                !          expected
        UDA1L(1:5) = UDA1R(2:6)  !  uda1l = 2,0,4,0,6,0
      ELSEWHERE
        UDA1L(2:6) = UDA1R(6:2:-1) !uda1l = 2,0,5,0,3,0
      ENDWHERE

      print *, 'expected = ',expected
      print *, 'computed = ', uda1l

      END

gfortran:gfortran rg0023.f

gfortran:a
 expected =            2           0           5           0           3       0
 computed =            2           0           4           0           6       0
Comment 1 Tobias Burnus 2008-03-29 23:23:44 UTC
Confirm.

The algorithm below does essentially:

while (1,0,1,0,1)
  uda1l( 1 _ 3 _ 5) = 2 3 4 5 6
else
  udal1 ( _ 3 _ 5 _) = 6 5 4 3 2
done

That is
1. uda1l(1) = 2
2. uda1l(3) = 5 <---\__
3. uda1l(3) = 4 <---/
4. uda1l(5) = 3 <---\__
5. uda1l(6) = 4 <---/

The order is wrong. The procedure has to be:

1. Establish control mask (i.e. 1,0,1,0,1) and run WHERE
  while (1,0,1,0,1)
    uda1l( 1 _ 3 _ 5) = 2 3 4 5 6
  end

2. Establish pending controlmask for ELSEWHERE (i.e. 0,1,0,1,0)
  elsewhile (0,1,0,1)
    uda1l( 1 _ 3 _ 5) = 2 3 4 5 6
  end


  {
    static logical(kind=4) A.2[5] = {1, 0, 1, 0, 1};
    static integer(kind=4) A.1[6] = {1, 2, 3, 4, 5, 6};

    (void) __builtin_memset ((void *) &uda1l, 0, 24);
    (void) __builtin_memcpy ((void *) &uda1r, (void *) &A.1, 24);
    (void) __builtin_memcpy ((void *) &lda, (void *) &A.2, 20);
    {
      integer(kind=8) S.3;

      S.3 = 1;
      while (1)
        {
          if (S.3 > 5) goto L.1;
          if (lda[S.3 + -1])
            {
              uda1l[S.3 + -1] = uda1r[S.3];
            }
          else
            {
              uda1l[S.3] = uda1r[6 - S.3];
            }
          S.3 = S.3 + 1;
        }
      L.1:;
    }
Comment 2 Paul Thomas 2008-03-30 12:40:39 UTC
This one should be straightforward, if lengthy to correct:

gfc_trans_where_2 is completely correct, as can be verified by doubling up the line making the assignment in the WHERE block. ie:

      WHERE (LDA)                !          expected
        UDA1L(1:5) = UDA1R(2:6)  !  uda1l = 2,0,4,0,6,0
        UDA1L(1:5) = UDA1R(2:6)  !  uda1l = 2,0,4,0,6,0
      ELSEWHERE
        UDA1L(2:6) = UDA1R(6:2:-1) !uda1l = 2,0,5,0,3,0
      ENDWHERE

gfc_trans_where_3 is selected for a simple WHERE/ELSEWHERE, with no dependencies and a single assignment expression in each block.  This is selected by Dick's testscase.  It is also wrong, as indicated by Tobias in #1!

gfc_trans_where_3 will have to be split into two loops, instead of a single loop with an expression.

Cheers

Paul
Comment 3 Paul Thomas 2008-05-16 21:12:58 UTC
Subject: Bug 35759

Author: pault
Date: Fri May 16 21:12:04 2008
New Revision: 135443

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=135443
Log:
2008-05-16  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/35756
	PR fortran/35759
	* trans-stmt.c (gfc_trans_where): Tighten up the dependency
	check for calling gfc_trans_where_3.

	PR fortran/35743
	* trans-stmt.c (gfc_trans_where_2): Set the mask size to zero
	if it is calculated to be negative.

	PR fortran/35745
	* trans-stmt.c (gfc_trans_where_3, gfc_trans_where_assign): Set
	ss->where for scalar right hand sides.
	* trans-array.c (gfc_add_loop_ss_code): If ss->where is set do
	not evaluate scalars outside the loop.  Clean up whitespace.
	* trans.h : Add a bitfield 'where' to gfc_ss.

2008-05-16  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/35756
	PR fortran/35759
	* gfortran.dg/where_1.f90: New test.

	PR fortran/35743
	PR fortran/35745
	* gfortran.dg/where_2.f90: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/where_1.f90
    trunk/gcc/testsuite/gfortran.dg/where_2.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-array.c
    trunk/gcc/fortran/trans-stmt.c
    trunk/gcc/fortran/trans.h
    trunk/gcc/testsuite/ChangeLog

Comment 4 Paul Thomas 2008-05-17 07:11:12 UTC
Subject: Bug 35759

Author: pault
Date: Sat May 17 07:10:13 2008
New Revision: 135461

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=135461
Log:
2008-05-17  Paul Thomas  <pault@gcc.gnu.org>

	Backport from mainline:
	PR fortran/35756
	PR fortran/35759
	* trans-stmt.c (gfc_trans_where): Tighten up the dependency
	check for calling gfc_trans_where_3.

	PR fortran/35743
	* trans-stmt.c (gfc_trans_where_2): Set the mask size to zero
	if it is calculated to be negative.

	PR fortran/35745
	* trans-stmt.c (gfc_trans_where_3, gfc_trans_where_assign): Set
	ss->where for scalar right hand sides.
	* trans-array.c (gfc_add_loop_ss_code): If ss->where is set do
	not evaluate scalars outside the loop.  Clean up whitespace.
	* trans.h : Add a bitfield 'where' to gfc_ss.

       PR fortran/36233
       * interface.c (compare_actual_formal): Do not check sizes if the
       actual is BT_PROCEDURE.

2008-05-17  Paul Thomas  <pault@gcc.gnu.org>

	Backport from mainline:
	PR fortran/35756
	PR fortran/35759
	* gfortran.dg/where_1.f90: New test.

	PR fortran/35743
	PR fortran/35745
	* gfortran.dg/where_2.f90: New test.

       PR fortran/36233
       * gfortran.dg/actual_procedure_1.f90: New test

Added:
    branches/gcc-4_3-branch/gcc/testsuite/gfortran.dg/actual_procedure_1.f90
    branches/gcc-4_3-branch/gcc/testsuite/gfortran.dg/where_1.f90
    branches/gcc-4_3-branch/gcc/testsuite/gfortran.dg/where_2.f90
Modified:
    branches/gcc-4_3-branch/gcc/fortran/ChangeLog
    branches/gcc-4_3-branch/gcc/fortran/interface.c
    branches/gcc-4_3-branch/gcc/fortran/trans-array.c
    branches/gcc-4_3-branch/gcc/fortran/trans-stmt.c
    branches/gcc-4_3-branch/gcc/fortran/trans.h
    branches/gcc-4_3-branch/gcc/testsuite/ChangeLog

Comment 5 Paul Thomas 2008-05-17 07:16:12 UTC
Fixed on trunk and 4.3.

Thanks for the report.

Paul