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

            Bug ID: 71412
           Summary: iso_c_bindings and optimization interaction bug
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: relliott at umn dot edu
  Target Milestone: ---

Created attachment 38644
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=38644&action=edit
minimal skeleton code that illustrates the bug

A problem exists in the way gfortran optimizes functions/subroutines when c
interoperability (and specifically the c_loc() intrinsic) are used.

The attached code illustrates the issue.  A variable is allocated and a pointer
is to the allocated memory is sent to C and stored in global memory.  Next the
memory is initialized in the same routine that it was allocated.  Finally, in a
different routine the memory pointer is retrieved from c and the value stored
in memory is printed.  With optimization -O3 the initialization is optimized
out.  With optimization -O0 the initialization occurs and the code runs
correctly.

The typical output and sequence of steps to run the code is given below.

vagrant@vagrant-ubuntu-trusty-64:/vagrant$ cd skeleton/

vagrant@vagrant-ubuntu-trusty-64:/vagrant/skeleton$ make
gfortran -m32 -O3 -g -Wall -pedantic  -c skeleton-f.F90
gcc -m32 -O3 -g -Wall -ansi -pedantic  -c skeleton-c.c
gfortran -m32 -O3 -g -Wall skeleton*.o  -o skeleton

vagrant@vagrant-ubuntu-trusty-64:/vagrant/skeleton$ ./skeleton
 -- in storePtr: pointer address --> 0x95caba0 <-- value --> -144534256
 -- just a print statement  --> nothing
 -- in getPtr:   pointer address --> 0x95caba0 <-- value --> -144534256
 -- value of 'val2' variable -->   -144534256

vagrant@vagrant-ubuntu-trusty-64:/vagrant/skeleton$ make clean
rm -f skeleton skeleton*.o *.mod

vagrant@vagrant-ubuntu-trusty-64:/vagrant/skeleton$ make OPT=-O0
gfortran -m32 -O0 -g -Wall -pedantic  -c skeleton-f.F90
gcc -m32 -O0 -g -Wall -ansi -pedantic  -c skeleton-c.c
gfortran -m32 -O0 -g -Wall skeleton*.o  -o skeleton

vagrant@vagrant-ubuntu-trusty-64:/vagrant/skeleton$ ./skeleton
 -- in storePtr: pointer address --> 0x8d09ba0 <-- value --> -143915760
 -- just a print statement  --> nothing
 -- in getPtr:   pointer address --> 0x8d09ba0 <-- value --> 21
 -- value of 'val2' variable -->           21

Reply via email to