-O0 modifies variable value

-O0 modifies variable value

I came across a weird situation while trying to debug my code. I tried to reproduce it in a smaller program but unfortunately couldn't, so let me start by posting the relevant snippets:

  subroutine sub(H)
    implicit none
    type(derived), intent(inout) :: H
    integer :: i, N
    N = size(H%field1) ! yields 24
        do i = 1,N
            call module_sub(H%field2,H%field3)
        end do
  end subroutine sub

The full program, including the module containing sub (file ops.f90), compiles OK:

$ make
ifort -O0 -c ops.f90
$ ifort -V
Intel(R) Fortran Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version Build 20121010
Copyright (C) 1985-2012 Intel Corporation.  All rights reserved.

There are no warnings when compiled with any / all of -warn all -debug full -check all -check bounds. Here is the relevant output of the two print statements shown above:

                     3     24
                     3      0

Note that the variable N is defined in the beginning and never touched again and that it is not passed to module_sub. It is only used as an upper DO-loop bound after the second print. There is no connection to global variables whatsoever. The weird behavior disappears for -O1 and above and the code always prints the same initial value of N (=24 in the example above).

Is there any O0-related reason why ifort changes the value of this variable? If not, is this perhaps a known issue? If not and not, would it make sense to provide the full code (modified version that makes it easy to spot the relevant point, of course)?

Thanks in advance.

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Hi Stefane,

   I've just written a test program to simulate your case. If you don't describe mod_sub then its difficult to tell what's happening. Could you please write a toy module_sub  that replaces my module_sub and does what yours is doing (or at least as similar as possible) ? 

With the code I posted, I don't get the same behaviour (version, so in my opinion module_sub is essential.




Downloadapplication/octet-stream test-steph.f90985 bytes

It is usually the case that when one can't reproduce the problem in a smaller case, the actual issue is somewhere different than you think. It is almost always the case that a "snippet" bears little or no bearing on the real problem.

You can run the program under the debuggger and step through it, watching the variable to see when and where it changes.  This may give you a clue as to what is wrong. For problems such as this, it is almost always a coding error in the application.

Retired 12/31/2016

Leave a Comment

Please sign in to add a comment. Not a member? Join today