Bug 2119828

Summary: gfortran incorrect code generation with -O1, works with -O0
Product: Red Hat Enterprise Linux 8 Reporter: Paulo Andrade <pandrade>
Component: gcc-toolset-11-gccAssignee: Marek Polacek <mpolacek>
Status: CLOSED UPSTREAM QA Contact: qe-baseos-tools-bugs
Severity: medium Docs Contact:
Priority: high    
Version: 8.6CC: ahajkova, fweimer, jakub, mpolacek, ohudlick, sipoyare, vrajput
Target Milestone: rc   
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2022-08-19 22:54:17 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Paulo Andrade 2022-08-19 14:26:58 UTC
Reproducer:

"""
$ cat sk.f
      program test
c         this program tests memory allocation

c     write(*,*)"Enter an interger higher than 0"
c     read(*,*)k
      k=1
      call  test_cray(k)
      end

      subroutine test_cray(k)
c        integer*8 ptrzz1
        pointer(ptrzz1 , zz1)
        ptrzz1=0
c        ptrzz1=loc(zz1)
        write(*,*)"   ptrzz1: ",ptrzz1
        if (ptrzz1 .ne. 0) then
          write(*,*)"   ptrzz1 ne zero in the calling routine"
        else
          write(*,*)"   ptrzz1 eq zero in the calling routine"
          call shape_cray(zz1)
          write(*,*)"*** Returning from shape_cray"
        end if
      end


      subroutine shape_cray(zz1)
        pointer(ptrzz , zz)
        ptrzz=loc(zz1)
        if (ptrzz .ne. 0) then
          write(*,*)"      ptrzz - loc(zz1)- ne zero in the called routine"
          write(*,*)"      ptrzz: ",ptrzz
        else
          write(*,*)"      ptrzz - loc(zz1) eq zero in the called routine"
          write(*,*)"      ptrzz: ",ptrzz
        end if
c        write(*,*)"   ptrzz: ",ptrzz
      end


$ gfortran -frecursive -fPIC -ffixed-line-length-132 -fautomatic -fcray-pointer -O1 -o test sk.f
$ ./test 
    ptrzz1:                     0
    ptrzz1 eq zero in the calling routine
       ptrzz - loc(zz1)- ne zero in the called routine
       ptrzz:                     0
 *** Returning from shape_cray
"""

  If compiled with -O0 it works as expected:
"""
$ gfortran -frecursive -fPIC -ffixed-line-length-132 -fautomatic -fcray-pointer -O0 -o test sk.f
$ ./test 
    ptrzz1:                     0
    ptrzz1 eq zero in the calling routine
       ptrzz - loc(zz1) eq zero in the called routine
       ptrzz:                     0
 *** Returning from shape_cray
"""

Comment 1 Marek Polacek 2022-08-19 22:27:11 UTC
Reproduced.  Started with r238754.  Reduced test:

      program test
      k=1
      call  test_cray(k)
      end

      subroutine test_cray(k)
        pointer(ptrzz1 , zz1)
        ptrzz1=0
        if (ptrzz1 .ne. 0) then
          call abort()
        else
          call shape_cray(zz1)
        end if
      end

      subroutine shape_cray(zz1)
        pointer(ptrzz , zz)
        ptrzz=loc(zz1)
        if (ptrzz .ne. 0) then
          call abort()
        end if
      end

I have to file an upstream bug.

Comment 2 Marek Polacek 2022-08-19 22:54:17 UTC
Upstream PR filed.  This needs to be dealt with in upstream first.