public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/101757] New: Simple integer assigment fails (off-seted by -1) when compiling with -O3
@ 2021-08-03 16:22 federico.perini at gmail dot com
  2021-08-03 16:26 ` [Bug fortran/101757] " federico.perini at gmail dot com
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: federico.perini at gmail dot com @ 2021-08-03 16:22 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101757
           Summary: Simple integer assigment fails (off-seted by -1) when
                    compiling with -O3
           Product: gcc
           Version: 7.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: federico.perini at gmail dot com
  Target Milestone: ---

Created attachment 51251
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51251&action=edit
test program

An extremely simple logical-flag-based assignment, which sets an integer to 1
if the logical is .true., and to 0 if .false., fails with gfortran 7.1.0 if
compiled with -O3. Using `WHERE`, `MERGE`, or a simple loop does not change the
result.

Test program:


program test_merge_
   use iso_fortran_env
   implicit none
   integer, parameter :: N=10000
   logical :: test(N)
   integer :: i,itest(N)

   do i=1,N
      test(i) = mod(i,2)==0
   end do

   itest = merge(1,0,test)
   print *, 'MERGE  itest min (0) =',minval(itest),' max (1)=',maxval(itest)

   where (test)
     itest = 1
   elsewhere
     itest = 0
   end where

   print *, 'WHERE  itest min (0) =',minval(itest),' max (1)=',maxval(itest)

   do i=1,N
     if (test(i)) then
        itest(i) = 1
     else
        itest(i) = 0
     endif
   end do

   print *, 'IFLOOP itest min (0) =',minval(itest),' max (1)=',maxval(itest)
end program test_merge_

Expected result is OK if "-O2", wrong if "-O3": 

werc 1073% gfortran -O3 test_merge.f90
werc 1074% ./a.out
 MERGE  itest min (0) =          -1  max (1)=           0
 WHERE  itest min (0) =          -1  max (1)=           0
 IFLOOP itest min (0) =          -1  max (1)=           0
werc 1075% gfortran -O2 test_merge.f90
werc 1076% ./a.out
 MERGE  itest min (0) =           0  max (1)=           1
 WHERE  itest min (0) =           0  max (1)=           1
 IFLOOP itest min (0) =           0  max (1)=           1

Tested OK on gfortran 9.2.0, 10.2.0, 10.3.0.

Thanks,
Federico

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

end of thread, other threads:[~2022-05-27  9:08 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-03 16:22 [Bug fortran/101757] New: Simple integer assigment fails (off-seted by -1) when compiling with -O3 federico.perini at gmail dot com
2021-08-03 16:26 ` [Bug fortran/101757] " federico.perini at gmail dot com
2021-08-03 16:40 ` federico.perini at gmail dot com
2021-08-03 17:30 ` [Bug tree-optimization/101757] [9 Regression] " pinskia at gcc dot gnu.org
2021-11-10 14:31 ` marxin at gcc dot gnu.org
2022-05-27  9:08 ` rguenth 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).