From: "Ryan S. Elliott" <relliott@umn.edu>
To: fortran@gcc.gnu.org
Subject: iso_c_bindings and optimization interaction bug
Date: Fri, 29 Apr 2016 12:13:00 -0000 [thread overview]
Message-ID: <alpine.OSX.2.20.1604290706310.778@enet-elliott-m3-tb.aem.umn.edu> (raw)
[-- Attachment #1: Type: text/plain, Size: 2130 bytes --]
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
[-- Attachment #2: Type: application/octet-stream, Size: 1152 bytes --]
next reply other threads:[~2016-04-29 12:13 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-04-29 12:13 Ryan S. Elliott [this message]
2016-06-04 12:31 ` Ryan S. Elliott
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=alpine.OSX.2.20.1604290706310.778@enet-elliott-m3-tb.aem.umn.edu \
--to=relliott@umn.edu \
--cc=fortran@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).