iso_c_bindings and optimization interaction bug

Ryan S. Elliott relliott@umn.edu
Fri Apr 29 12:13:00 GMT 2016


Hello,

I may have discovered a problem 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.

I would greatly appreciate any analysis of this issue.  Is it a bug?  Is my 
code non-compliant in some way? etc.

Thanks,  Ryan

-------------------------------------------------------------------------------

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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: skeleton.tgz
Type: application/octet-stream
Size: 1152 bytes
Desc: 
URL: <http://gcc.gnu.org/pipermail/fortran/attachments/20160429/6fc3ae20/attachment.obj>


More information about the Fortran mailing list