public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/45425]  New: where mask not applied before where clause evaluated
@ 2010-08-27 10:03 aronaldg at gmail dot com
  2010-08-27 11:58 ` [Bug fortran/45425] Bounds check applied before MASK of WHERE construct burnus at gcc dot gnu dot org
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: aronaldg at gmail dot com @ 2010-08-27 10:03 UTC (permalink / raw)
  To: gcc-bugs

This is the system

uname -a
Linux argux9.fuqua.duke.edu 2.6.18-194.3.1.el5 #1 SMP Thu May 13
13:08:30 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux


This is the compiler

gfortran44 --version 
GNU Fortran (GCC) 4.4.0 20090514 (Red Hat 4.4.0-6)
Copyright (C) 2009 Free Software Foundation, Inc.


This is the problem

The bug is that the mask is supposed to be applied before the statements
within the where clause are evaluated.  As can be seen from the output
the statements within the mask are evaluated before the mask is applied.


This is the relevant code

do k = 1, rows + 1

      if ( (k .gt. rows) .or. (k .gt. cols) ) then
        mask = .false.
      else
        mask  = .true.
      end if

      where (mask)
        A = b(k) 
      end where

end do


These are the compiler flags

-save-temps -fbounds-check


This is stderr

At line 39 of file wherebug.F90
Fortran runtime error: Array reference out of bounds for array 'b',
upper bound of dimension 1 exceeded (6 > 5)

This is the -save-temps file

arg@argux9$ cat  wherebug.f90
# 1 "wherebug.F90"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "wherebug.F90"
program wherebug
implicit none

integer, parameter ::   &
  stdin     =  5,       &  ! reserved unit for standard input
  stdout    =  6,       &  ! reserved unit for standard output
  stderr    =  0           ! reserved unit for standard error

integer, parameter ::                            &
  i1 = selected_int_kind(2),                     &
  i2 = selected_int_kind(4),                     &
  i4 = selected_int_kind(9),                     &
  i8 = selected_int_kind(18),                    &
  r4 = kind(1.0),                                &
  r8 = selected_real_kind(2*precision(1.0_r4)),  &
  r16 = selected_real_kind(2*precision(1.0_r8))

integer, parameter :: rows = 5, cols = 5

integer :: i, j, k

real(r8), dimension(rows,cols) :: A
real(r8), dimension(rows)      :: b
logical, dimension(rows,cols)  :: mask

A = 1.0_r8
b = 1.0_r8
mask = .true.

do k = 1, rows + 1

      if ( (k .gt. rows) .or. (k .gt. cols) ) then
        mask = .false.
      else
        mask  = .true.
      end if

      where (mask)
        A = b(k) 
      end where

end do

end program wherebug


-- 
           Summary: where mask not applied before where clause evaluated
           Product: gcc
           Version: 4.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: aronaldg at gmail dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45425


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug fortran/45425] Bounds check applied before MASK of WHERE construct
  2010-08-27 10:03 [Bug fortran/45425] New: where mask not applied before where clause evaluated aronaldg at gmail dot com
@ 2010-08-27 11:58 ` burnus at gcc dot gnu dot org
  2010-08-28 15:40 ` burnus at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: burnus at gcc dot gnu dot org @ 2010-08-27 11:58 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from burnus at gcc dot gnu dot org  2010-08-27 11:58 -------
Confirm, the bounds checks are misplaces. Simplified test case:

implicit none
integer :: A(1), i,b(1)
logical :: mask(1)

mask = .false.
b = 5
do i = 2, 2
  where (mask)
    A = b(i)
  end where
end do
end



If one looks at the dump of the internal representation (-fdump-tree-original)
one sees that WHERE itself is properly handled - only the bounds check is
outside of the WHERE loop and thus outside of the MASK:

  if (__builtin_expect (i <= 0, 0))
    _gfortran_runtime_error_at (...);
[...]
              S.1 = 1;
              while (1)
                {
                  if (S.1 > 1) goto L.4;
                  if (mask[S.1 + -1])
                    {
                      a[S.1 + -1] = b[i + -1];
                    }
                  S.1 = S.1 + 1;
                }


-- 

burnus at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
OtherBugsDependingO|                            |27766
              nThis|                            |
           Keywords|                            |wrong-code
      Known to fail|                            |4.1.2 4.6.0
            Summary|where mask not applied      |Bounds check applied before
                   |before where clause         |MASK of WHERE construct
                   |evaluated                   |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45425


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug fortran/45425] Bounds check applied before MASK of WHERE construct
  2010-08-27 10:03 [Bug fortran/45425] New: where mask not applied before where clause evaluated aronaldg at gmail dot com
  2010-08-27 11:58 ` [Bug fortran/45425] Bounds check applied before MASK of WHERE construct burnus at gcc dot gnu dot org
@ 2010-08-28 15:40 ` burnus at gcc dot gnu dot org
  2010-08-28 17:43 ` mikael at gcc dot gnu dot org
  2010-08-28 19:51 ` tkoenig at gcc dot gnu dot org
  3 siblings, 0 replies; 6+ messages in thread
From: burnus at gcc dot gnu dot org @ 2010-08-28 15:40 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from burnus at gcc dot gnu dot org  2010-08-28 15:40 -------
The error message is generated in
  gfc_conv_array_ref
it's called via gfc_trans_where_3 -> gfc_conv_loop_setup ->
gfc_add_loop_ss_code -> gfc_conv_variable

Thus, the condition (mask) ends up at
  gfc_add_ss_to_loop (&loop, css);
and the bounds check is added via
  gfc_conv_loop_setup (&loop, &tdst->where);

Thus, it ends up at loop->pre which comes before the actual loop.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45425


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug fortran/45425] Bounds check applied before MASK of WHERE construct
  2010-08-27 10:03 [Bug fortran/45425] New: where mask not applied before where clause evaluated aronaldg at gmail dot com
  2010-08-27 11:58 ` [Bug fortran/45425] Bounds check applied before MASK of WHERE construct burnus at gcc dot gnu dot org
  2010-08-28 15:40 ` burnus at gcc dot gnu dot org
@ 2010-08-28 17:43 ` mikael at gcc dot gnu dot org
  2010-08-28 19:51 ` tkoenig at gcc dot gnu dot org
  3 siblings, 0 replies; 6+ messages in thread
From: mikael at gcc dot gnu dot org @ 2010-08-28 17:43 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from mikael at gcc dot gnu dot org  2010-08-28 17:42 -------
Ouch!
We need some sort of lazy evaluation.
Like (pseudo-code)

bool scalar_ever_evaluated = false;
whatever_type scalar_value;

while(1)
  {
    /* loop handling stuff */

    if (scalar_ever_evaluated)
      {
        scalar_value = <whatever complicated expression>;
        scalar_ever_evaluated = true;
      }

    /* normal code using scalar_value */
  }


We have to move back se->pre into se->expr so that it is not moved outside the
loop. Or move it outside the loop conditionally and conditionally add se->pre
inside the loop while evaluating the scalar. 

BTW I thought there was already some where-specific code generation for scalar
values. 


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45425


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug fortran/45425] Bounds check applied before MASK of WHERE construct
  2010-08-27 10:03 [Bug fortran/45425] New: where mask not applied before where clause evaluated aronaldg at gmail dot com
                   ` (2 preceding siblings ...)
  2010-08-28 17:43 ` mikael at gcc dot gnu dot org
@ 2010-08-28 19:51 ` tkoenig at gcc dot gnu dot org
  3 siblings, 0 replies; 6+ messages in thread
From: tkoenig at gcc dot gnu dot org @ 2010-08-28 19:51 UTC (permalink / raw)
  To: gcc-bugs



-- 

tkoenig at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
   Last reconfirmed|0000-00-00 00:00:00         |2010-08-28 19:51:18
               date|                            |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45425


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug fortran/45425] Bounds check applied before MASK of WHERE construct
       [not found] <bug-45425-4@http.gcc.gnu.org/bugzilla/>
@ 2021-12-18  1:26 ` pinskia at gcc dot gnu.org
  0 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-12-18  1:26 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=45425

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic
   Last reconfirmed|2010-08-28 19:51:18         |2021-12-17
      Known to fail|                            |

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
We also now get a warning since GCC 8 too:
/app/example.f90:10:10:

    8 | do i = 2, 2
      |           2
    9 |   where (mask)
   10 |     A = b(i)
      |          1
Warning: Array reference at (1) out of bounds (2 > 1) in loop beginning at (2)
/app/example.f90:10:10:

    8 | do i = 2, 2
      |           2
    9 |   where (mask)
   10 |     A = b(i)
      |          1
Warning: Array reference at (1) out of bounds (2 > 1) in loop beginning at (2)

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-12-18  1:26 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-08-27 10:03 [Bug fortran/45425] New: where mask not applied before where clause evaluated aronaldg at gmail dot com
2010-08-27 11:58 ` [Bug fortran/45425] Bounds check applied before MASK of WHERE construct burnus at gcc dot gnu dot org
2010-08-28 15:40 ` burnus at gcc dot gnu dot org
2010-08-28 17:43 ` mikael at gcc dot gnu dot org
2010-08-28 19:51 ` tkoenig at gcc dot gnu dot org
     [not found] <bug-45425-4@http.gcc.gnu.org/bugzilla/>
2021-12-18  1:26 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).