Home Dashboard Directory Help
Search

possible optimizer bug with SSE vector subtraction in VC++ 2012 Update 1 by Trass3r


Status: 

Closed


1
0
Sign in
to vote
Type: Bug
ID: 777885
Opened: 1/30/2013 6:00:23 AM
Access Restriction: Public
0
Workaround(s)
view
0
User(s) can reproduce this bug

Description

I was basically trying to implement a simple vector subtraction with SSE.

#include <stdio.h>
#include <tchar.h>

#include <emmintrin.h>

typedef unsigned short ushort;
typedef unsigned int uint;

void print(__m128i i)
{
    auto& arr = i.m128i_u16;
    printf("[%d %d %d %d %d %d %d %d]\n", arr[0], arr[1], arr[2], arr[3], arr[4], arr[5], arr[6], arr[7]);
}

int _tmain(int argc, _TCHAR* argv[])
{
    const int lineSize = 912;
    ushort input[lineSize];
    ushort vals[lineSize];
// printf("%X %X\n", input, vals); // note this one

    for (uint i=0; i<lineSize; i+=8)
    {
        __m128i vecinput = _mm_loadu_si128((__m128i*) &input[i]);
        __m128i vecvals = _mm_loadu_si128((__m128i*) &vals[i]);

        __m128i output = _mm_subs_epu16(vecinput, vecvals);
        print(output);
        printf("===\n");
    }

    return 0;
}

It optimizes vals away and incorrectly treats it like it was the same as input, so the result is always 0. Note that the arrays are intended to be uninitialized to get "random" values.
If that printf is uncommented, correct code is generated.

See http://stackoverflow.com/questions/14600413/is-this-a-bug-in-the-vc-optimizer-or-in-my-code for the assembler output.
Details
Sign in to post a comment.
Posted by Microsoft on 2/13/2013 at 7:35 PM
Thank you for reporting the issue. However, in this case it's by design. Using uninitialized variable can leads to undefined behaviour. And it's OK for compiler to pack the two uninitialized arrays together for optimization purpose.
Posted by Microsoft on 1/30/2013 at 9:30 PM
Thank you for submitting feedback on Visual Studio and .NET Framework. 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 Trass3r on 1/30/2013 at 8:16 AM
Ok someone on SO pointed out that the optimizer is allowed to remove that array cause both are uninitialized.
If that's the case, is there a good reason to do so instead of issuing a warning or error?
Posted by Microsoft on 1/30/2013 at 6:49 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.