Optimization bug with recursive functions - by DJones69

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 732925 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 3/23/2012 11:57:09 AM
Access Restriction Public



We ran across an optimization bug in our program when using x64 and the following optimization flags: /O2 /Ob1 /Oi /Ot. Our function is recursive and manipulates a global object. It seems similar to bug ID 678200. However, my example function requires somewhat irrelevant operations, otherwise the bug will disappear. For example, if the std::vector is removed, then correct assembly is generated. This is similar behavior to an earlier bug that I submitted where register allocation was not working properly.

In my example, I analyzed the assembly and noticed that it produces this when it compares the variables at the bottom of the DoSomething function:

	if( InstanceState.SavedData != InstanceState.Instance->Data )
000000013F26112C  mov         eax,dword ptr [Instance+8 (13F263660h)]  
000000013F261132  cmp         eax,eax  
000000013F261134  je          DoSomething+0E2h (13F261142h)  

The cmp is clearly wrong as the data has changed in the global variable. ResetData should be called in the outermost DoSomething call.

Some final notes: 

This code also doesn't work in VS 2008 and 2011 Beta. 
Win32 will produce correct assembly. 
/Ob0 will produce correct assembly (presumably because the std::vector functions aren't inlined now). 
Sign in to post a comment.
Posted by Microsoft on 4/24/2012 at 4:54 PM
First of all, thanks for reporting this bug.

The fix will be included in the VS 2011 release. Unfortunately, it does not meet the bar to be backported to 2010.

The bug could potentially affect any code gen target since it occurs in target-independent code.

Andrew Spencer, Visual C++
Posted by DJones69 on 4/3/2012 at 11:10 AM
I noticed that this has been marked as fixed. When will we see the fix applied? Will it only affect 2011, or will there be a hotfix for 2010, too? Does this error affect Win32, too?
Posted by MS-Moderator10 [Feedback Moderator] on 3/26/2012 at 1:07 AM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by DJones69 on 3/23/2012 at 1:02 PM
One other note: changing SetData and ResetData to be inlined alleviates the problem. It's as if the compiler fails to recognize that those functions are mutating data.
Posted by MS-Moderator01 on 3/23/2012 at 12:51 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)