<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic OPTIONAL and VALUE attributes - unexpected behaviour in Intel® Fortran Compiler</title>
    <link>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977150#M98768</link>
    <description>&lt;P&gt;Ifort 13.1.2.183 Build 20130514 gives the following warning:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;warning #7937: The OPTIONAL attribute should not be used for arguments with the VALUE attribute.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;when compiling this code:&lt;/P&gt;
&lt;P&gt;[fortran]module test_mod&lt;BR /&gt;contains&lt;BR /&gt;subroutine test_routine(iopt)&lt;BR /&gt;integer, optional, value :: iopt&lt;BR /&gt;if (present(iopt)) then&lt;BR /&gt;&amp;nbsp;print *, 'present'&lt;BR /&gt;else&lt;BR /&gt;&amp;nbsp;print *, 'absent'&lt;BR /&gt;endif&lt;BR /&gt;end subroutine &lt;BR /&gt;end module&lt;BR /&gt;program test&lt;BR /&gt;use test_mod&lt;BR /&gt;call test_routine(iopt=1)&lt;BR /&gt;call test_routine()&lt;BR /&gt;end program[/fortran]&lt;/P&gt;
&lt;P&gt;The executable does not give the expected result:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;$ ./a.out &lt;BR /&gt;&amp;nbsp;present&lt;BR /&gt;&amp;nbsp;present&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;(the second time the routine is called, the argument is not present, therefore the present(iopt) should be .FALSE.)&lt;/P&gt;
&lt;P&gt;This looks like a bug to me. If the standard says that we can't have OPTIONAL and VALUE together (I haven't checked), then ifort should throw an error, not a warning. If the standard does allow it, then the implementation is buggy. FWIW, NAGfor generates an executable with the expected behaviour.&lt;/P&gt;</description>
    <pubDate>Fri, 31 May 2013 19:32:11 GMT</pubDate>
    <dc:creator>Alexis_R_</dc:creator>
    <dc:date>2013-05-31T19:32:11Z</dc:date>
    <item>
      <title>OPTIONAL and VALUE attributes - unexpected behaviour</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977150#M98768</link>
      <description>&lt;P&gt;Ifort 13.1.2.183 Build 20130514 gives the following warning:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;warning #7937: The OPTIONAL attribute should not be used for arguments with the VALUE attribute.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;when compiling this code:&lt;/P&gt;
&lt;P&gt;[fortran]module test_mod&lt;BR /&gt;contains&lt;BR /&gt;subroutine test_routine(iopt)&lt;BR /&gt;integer, optional, value :: iopt&lt;BR /&gt;if (present(iopt)) then&lt;BR /&gt;&amp;nbsp;print *, 'present'&lt;BR /&gt;else&lt;BR /&gt;&amp;nbsp;print *, 'absent'&lt;BR /&gt;endif&lt;BR /&gt;end subroutine &lt;BR /&gt;end module&lt;BR /&gt;program test&lt;BR /&gt;use test_mod&lt;BR /&gt;call test_routine(iopt=1)&lt;BR /&gt;call test_routine()&lt;BR /&gt;end program[/fortran]&lt;/P&gt;
&lt;P&gt;The executable does not give the expected result:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;$ ./a.out &lt;BR /&gt;&amp;nbsp;present&lt;BR /&gt;&amp;nbsp;present&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;(the second time the routine is called, the argument is not present, therefore the present(iopt) should be .FALSE.)&lt;/P&gt;
&lt;P&gt;This looks like a bug to me. If the standard says that we can't have OPTIONAL and VALUE together (I haven't checked), then ifort should throw an error, not a warning. If the standard does allow it, then the implementation is buggy. FWIW, NAGfor generates an executable with the expected behaviour.&lt;/P&gt;</description>
      <pubDate>Fri, 31 May 2013 19:32:11 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977150#M98768</guid>
      <dc:creator>Alexis_R_</dc:creator>
      <dc:date>2013-05-31T19:32:11Z</dc:date>
    </item>
    <item>
      <title>Interesting. The standard</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977151#M98769</link>
      <description>&lt;P&gt;Interesting. The standard doesn't have a restriction here.&amp;nbsp; I agree that this is a bug and will escalate it. &lt;/P&gt;</description>
      <pubDate>Fri, 31 May 2013 20:30:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977151#M98769</guid>
      <dc:creator>Steven_L_Intel1</dc:creator>
      <dc:date>2013-05-31T20:30:00Z</dc:date>
    </item>
    <item>
      <title>When passing by reference a</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977152#M98770</link>
      <description>&lt;P&gt;When passing by reference a pointer to the argument is passed. By convention, data cannot be positioned at location 0. Therefore a pointer to 0 can be used and interpreted as argument not present.&lt;/P&gt;
&lt;P&gt;When passing integer by value, 0 is a valid value. Meaning there is no way to disambiguate present or not present. Thus the warning. What is unknown (to me) is when the subrouting has an interface with "integer, optional,&amp;nbsp;value :: iopt" and the caller uses the interface and calls without an argument, then the question is: Is the value of iopt == 0 (regardless of report of test of present). If true, then reserve option value == 0 to indicate none.&lt;/P&gt;
&lt;P&gt;Jim Dempsey&lt;/P&gt;</description>
      <pubDate>Fri, 31 May 2013 20:33:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977152#M98770</guid>
      <dc:creator>jimdempseyatthecove</dc:creator>
      <dc:date>2013-05-31T20:33:00Z</dc:date>
    </item>
    <item>
      <title>I made an error in my earlier</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977153#M98771</link>
      <description>&lt;P&gt;I made an error in my earlier reply, since edited. The Fortran standard VALUE attribute does not necessarily mean pass-by-value. It does only if the called routine is declared as BIND(C).&lt;/P&gt;
&lt;P&gt;Issue ID is DPD200244863.&lt;/P&gt;</description>
      <pubDate>Fri, 31 May 2013 21:02:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977153#M98771</guid>
      <dc:creator>Steven_L_Intel1</dc:creator>
      <dc:date>2013-05-31T21:02:00Z</dc:date>
    </item>
    <item>
      <title>I think I need to explain</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977154#M98772</link>
      <description>&lt;P&gt;I think I need to explain more.&lt;/P&gt;
&lt;P&gt;First of all, the Fortran VALUE attribute is not the same as !DEC$ ATTRIBUTES VALUE. For a routine that is not BIND(C), it effectively means that the called routine makes a local copy of the actual argument an any writes to it are discarded on exit. For a routine with BIND(C), it does mean pass-by-value.&lt;/P&gt;
&lt;P&gt;In Fortran 2008, a BIND(C) routine is not allowed to have an OPTIONAL argument. There is a specification TS29113 for "Further&amp;nbsp; Interoperability of Fortran with C" that does allow for OPTIONAL arguments, but it disallows the combination of OPTIONAL and VALUE.&lt;/P&gt;
&lt;P&gt;So in the case in question, Intel Fortran should compile this without a warning and with the correct behavior.&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 31 May 2013 21:11:04 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977154#M98772</guid>
      <dc:creator>Steven_L_Intel1</dc:creator>
      <dc:date>2013-05-31T21:11:04Z</dc:date>
    </item>
    <item>
      <title>Thanks Steve &amp; Jim.</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977155#M98773</link>
      <description>&lt;P&gt;Thanks Steve &amp;amp; Jim.&lt;/P&gt;
&lt;P&gt;Steve, could you clarify the distinction you/Fortran makes between&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;the called routine makes a local copy of the actual argument and any writes to it are discarded on exit&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;and&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;pass-by-value&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The two sound equivalent to me, so I must be missing something.&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 31 May 2013 23:46:05 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977155#M98773</guid>
      <dc:creator>Alexis_R_</dc:creator>
      <dc:date>2013-05-31T23:46:05Z</dc:date>
    </item>
    <item>
      <title>Pass by value is when the</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977156#M98774</link>
      <description>&lt;P&gt;Pass by value is when the argument list (usually on the stack or a register) contains the actual value, not the address of the value. In C it's the difference between foo(x) and foo(&amp;amp;x). The Fortran standard VALUE attribute, in the absence of BIND(C), doesn't change how the argument is passed, but does make the called routine copy the argument into a local and use the copy. The net effect to the caller is like pass-by-value - the thing you pass can't be changed - but the mechanism is different.&lt;/P&gt;
&lt;P&gt;If you DO have BIND(C), then VALUE means doing it like C without the &amp;amp; - the argument list contains the data, not the data's address.&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jun 2013 01:42:42 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977156#M98774</guid>
      <dc:creator>Steven_L_Intel1</dc:creator>
      <dc:date>2013-06-01T01:42:42Z</dc:date>
    </item>
    <item>
      <title>Thanks Steve, that clears it</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977157#M98775</link>
      <description>&lt;P&gt;Thanks Steve, that clears it up for me.&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jun 2013 02:16:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977157#M98775</guid>
      <dc:creator>Alexis_R_</dc:creator>
      <dc:date>2013-06-01T02:16:00Z</dc:date>
    </item>
    <item>
      <title>We've fixed this for the 14.0</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977158#M98776</link>
      <description>&lt;P&gt;We've fixed this for the 14.0 compiler release, due out in September, but the new behavior won't be enabled by default. We found that the change broke some of the modules we supply and we didn't have time to fix them.&lt;/P&gt;
&lt;P&gt;For now, you will need to specify the new /assume:std_value option (or /standard-semantics) to get it so that non-BIND(C) routines with VALUE arguments have them passed by reference (and this will make OPTIONAL and PRESENT work). We also cleared up issues with specifying VALUE for arrays (ok for non-BIND(C) routines with the new option, disallowed otherwise.)&lt;/P&gt;
&lt;P&gt;We may change the default behavior in a future release.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2013 19:47:03 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977158#M98776</guid>
      <dc:creator>Steven_L_Intel1</dc:creator>
      <dc:date>2013-07-18T19:47:03Z</dc:date>
    </item>
    <item>
      <title>Steve,</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977159#M98777</link>
      <description>&lt;P&gt;Steve,&lt;/P&gt;
&lt;P&gt;Above (http://software.intel.com/en-us/comment/reply/393207/1737938?quote=1#comment-form), you said:&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;Steve Lionel wrote:&lt;BR /&gt;The Fortran standard VALUE attribute, in the absence of BIND(C), doesn't change how the argument is passed, but does make the called routine copy the argument into a local and use the copy. The net effect to the caller is like pass-by-value - the thing you pass can't be changed - but the mechanism is different.&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I cannot reconcile this statement with what the compiler actually does. Let us restrict ourselves to scalar arguments of built-in-type, say INTEGER. The x86 as well as the x64 assembly outputs for the test program below show that, in fact, passing an argument with the VALUE attribute is implemented with&lt;STRONG&gt;&amp;nbsp;pass-by-value&lt;/STRONG&gt;. The x86 assembly code shows that the caller passes the &lt;STRONG&gt;value&lt;/STRONG&gt; on the stack. The x64 code passes the &lt;STRONG&gt;value&lt;/STRONG&gt; in the first register specified by the ABI (RDI in Linux, RCX in Windows). In both cases, the called subroutine takes the value off the stack/register. There is no creation of a local copy of the argument either in the caller or callee beyond the normal copying of the value to the stack or register according to the normal ABI conventions.&lt;/P&gt;
&lt;P&gt;BIND(C) is not involved, I did not use any -std options in compiling to assembly.&lt;/P&gt;
&lt;P&gt;As we know, the Fortran standard requires that an explicit interface be available to the caller for calling a subprogram one or more of whose dummy arguments have the VALUE attribute. This means that the caller as well as the callee know about the VALUE attribute. The standard does not specify the argument-passing mechanism, of course, but one needs to know the mechanism when doing mixed-language programming without recourse to ISO C-interoperability.&lt;/P&gt;
&lt;P&gt;Please, therefore, clarify the issue.&lt;/P&gt;
&lt;P&gt;[fortran]&lt;/P&gt;
&lt;P&gt;program tstvalue&lt;BR /&gt;implicit none&lt;BR /&gt;integer :: a,b&lt;BR /&gt;!&lt;BR /&gt;interface&lt;BR /&gt; subroutine sub(v,x)&lt;BR /&gt; implicit none&lt;BR /&gt; integer, value :: v&lt;BR /&gt; integer, intent(out) :: x&lt;BR /&gt; end subroutine sub&lt;BR /&gt;end interface&lt;BR /&gt;!&lt;BR /&gt;a=3&lt;BR /&gt;call sub(a,b)&lt;BR /&gt;write(*,*)a,b&lt;BR /&gt;end program tstvalue&lt;BR /&gt;!&lt;BR /&gt;subroutine sub(v,x)&lt;BR /&gt; implicit none&lt;BR /&gt; integer, value :: v&lt;BR /&gt; integer, intent(out) :: x&lt;BR /&gt; x=v*v&lt;BR /&gt; v=0&lt;BR /&gt; return&lt;BR /&gt;end subroutine sub[/fortran]&lt;/P&gt;
&lt;P&gt;Here are the assembly instructions for the subroutine (Linux IFort 13.1.163 Build 2, x64 version)&lt;/P&gt;
&lt;P&gt;[bash]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;imull %edi, %edi&amp;nbsp;&lt;BR /&gt; movl %edi, (%rsi)&amp;nbsp;&lt;BR /&gt; ret [/bash]&lt;/P&gt;
&lt;P&gt;Had the first argument not been passed by value, the &lt;STRONG&gt;imull&lt;/STRONG&gt; instruction would be doing the meaningless operation of multiplying two addresses.&lt;/P&gt;</description>
      <pubDate>Sat, 20 Jul 2013 12:30:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977159#M98777</guid>
      <dc:creator>mecej4</dc:creator>
      <dc:date>2013-07-20T12:30:00Z</dc:date>
    </item>
    <item>
      <title>The compiler currently does</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977160#M98778</link>
      <description>&lt;P&gt;The compiler currently does not implement VALUE for non-BIND(C) routines correctly. It treates VALUE the same as for BIND(C) - it passes and accepts the argument by value. That's not correct and is fixed in version 14, but only if you use the new switch.&lt;/P&gt;
&lt;P&gt;The text of mine you quote is me explaining what the standard says - not what ifort is currently doing.&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jul 2013 00:03:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/OPTIONAL-and-VALUE-attributes-unexpected-behaviour/m-p/977160#M98778</guid>
      <dc:creator>Steven_L_Intel1</dc:creator>
      <dc:date>2013-07-21T00:03:00Z</dc:date>
    </item>
  </channel>
</rss>

