Search

VC++2012 compiler bug (in optimized builds) by ddoarn

Closed

3
0
Sign in
to vote
Type: Bug
ID: 776310
Opened: 1/11/2013 1:29:13 AM
Access Restriction: Public
0
Workaround(s)
3
User(s) can reproduce this bug
valid C++ source generates wrong code depends on optimization settings and moon phases.
Details (expand)

Visual Studio/Team Foundation Server/.NET Framework Tooling Version

Visual Studio 2012

What category (if any) best represents this feedback?

 

Steps to reproduce

template<typename T>
struct X {
void op1( const T &_t1 ) {
    std::hash<T> fn;
    const unsigned long long hash = fn( _t1 );
    printf( "%llu, %llu\n", _t1, hash );
}

void op2( const T &_t1 ) {
    std::hash<T> fn;
    const T x = _t1;
    const unsigned long long hash = fn( x );
    printf( "%llu, %llu\n", _t1, hash );
}
};

void test() {
X<unsigned long long> x;

printf( "\npass 1 (int)\n" );
for ( unsigned int i = 1; i < 4; ++i )
    x.op1( i );

printf( "\npass 2 (int)\n" );
for ( unsigned int i = 1; i < 4; ++i )
    x.op2( i );

printf( "\npass 1 (long long)\n" );
for ( unsigned long long i = 1; i < 4; ++i )
    x.op1( i );

return;
}

Product Language

English

Operating System

Windows 8

Operating System Language

English

Actual results

pass 1 (int)
1, 16709363566748652271
2, 10753514149554699521
3, 3026632941147223442

pass 2 (int)
1, 9929646808083455629
2, 16626593029616388163
3, 14394277623260462301

pass 1 (long long)
1, 9929646808083455629
2, 16626593029616388163
3, 14394277623260462301

Expected results

all passes must yield same output. What we got is wierd result on first pass (with generated asm looks wrong too).

Locale

 
File Attachments
File Name Submitted By Submitted On File Size  
ConsoleApplication1.cpp 1/11/2013 955 bytes
vs2012(cpp_compiler_only)(urgent_or_no_workaround_available).txt 1/13/2013 2 KB
ConsoleApplication1.zip 1/13/2013 69 KB
Sign in to post a comment.
Posted by Microsoft on 1/17/2013 at 10:18 AM
I am closing this MSConnect item. Feel free to re-activate it if you need anything else.
Eric
Posted by Microsoft on 1/17/2013 at 10:07 AM
Hi, thanks for reporting this. I confirm this bug in the compiler optimizer. We will fix this in a future release.

Unfortunately, there is no easy way to pinpoint a specific line of code to be able to work around this issue. In your code sample, you can workaround the issue by turning off optimizations using #pragma optimize("", off) / #pragma optimize("", on) around main(), or by marking op1 and op2 as __declspec(noinline).

If you feel you would like a hotfix for VS 2012, please visit the support site:

http://support.microsoft.com/common/international.aspx?RDPATH=dm;en-us;selectassist&target=assistance

Thanks,
Eric Brumer - Microsoft Visual C++
Posted by Microsoft on 1/16/2013 at 1:03 AM
Hi ddoarn, thanks for your respnose. We have received your attached file. Your issue has been routed to the appropriate VS development team for investigation. We will contact you if we require any additional information.
Posted by ddoarn on 1/15/2013 at 11:51 PM
ConsoleApplication1.zip attached by UnitUniverse is just right test solution. Reproduces only in x64 speed-optimized builds.

Further it was shown that even non-templated version has same problem:

void op1( const unsigned long long &_t1 )
{
    std::hash<unsigned long long> fn;
    const unsigned long long hash = fn( _t1 );
    printf( "%llu, %llu\n", (unsigned long long)_t1, hash );
}

void op2( unsigned long long _t1 )
{
    std::hash<unsigned long long> fn;
    const unsigned long long x = _t1;
    const unsigned long long hash = fn( x );
    printf( "%llu, %llu\n", (unsigned long long)_t1, hash );
}

int _tmain(int argc, _TCHAR * argv [])
{

    printf( "\npass 1 (int)\n" );
    for ( unsigned int i = 1; i < 4; ++i )
     op1( i );

    printf( "\npass 2 (int)\n" );
    for ( unsigned int i = 1; i < 4; ++i )
     op2( i );

    printf( "\npass 1 (long long)\n" );
    for ( unsigned long long i = 1; i < 4; ++i )
     op1( i );

    _getch();
    return 0;
}
Posted by Microsoft on 1/13/2013 at 9:43 PM
Thank you for submitting feedback on Visual Studio and .NET Framework. In order to efficiently investigate and reproduce this issue, we are requesting a demo project. Please submit this information to us within 4 business days. We look forward to hearing from you with this information.
Posted by Microsoft on 1/11/2013 at 1:50 AM
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)
Sign in to post a workaround.