6

A similar question was posted on SO for g++ that was rather vague, so I thought I'd post a specific example for VC++12 / VS2013 to which we can hopefully get an answer.

cross-link: g++ , range based for and vectorization

MSDN gives the following as an example of a loop that can be vectorized:

for (int i=0; i<1000; ++i)
{       
    A[i] = A[i] + 1;
}

(http://msdn.microsoft.com/en-us/library/vstudio/jj658585.aspx)

Here is my version of a range-based analogue to the above, a c-style monstrosity, and a similar loop using std::for_each. I compiled with the /Qvec-report:2 flag and added the compiler messages as comments:

#include <vector>
#include <algorithm>

int main()
{
    std::vector<int> vec(1000, 1);

    // simple range-based for loop
    {
        for (int& elem : vec)
        {
            elem = elem + 1;
        }
    } // info C5002 : loop not vectorized due to reason '1304'

    // c-style iteration
    {
        int * begin = vec.data();
        int * end = begin + vec.size();

        for (int* it = begin; it != end; ++it)
        {
            *it = *it + 1;
        }
    } // info C5001: loop vectorized

    // for_each iteration
    {
        std::for_each(vec.begin(), vec.end(), [](int& elem)
        {
            elem = elem + 1;
        });
    } // (no compiler message provided)

    return 0;
}

Only the c-style loop gets vectorized. Reason 1304 is as follows as per the MSDN docs:

1304: Loop includes assignments that are of different sizes.

It gives the following as an example of code that would trigger a 1304 message:

void code_1304(int *A, short *B)
{
    // Code 1304 is emitted when the compiler detects
    // different sized statements in the loop body.
    // In this case, there is an 32-bit statement and a
    // 16-bit statement.

    // In cases like this consider splitting the loop into loops to 
    // maximize vector register utilization.

    for (int i=0; i<1000; ++i)
    {
        A[i] = A[i] + 1;
        B[i] = B[i] + 1;
    }
}

I'm no expert but I can't see the relationship. Is this just buggy reporting? I've noticed that none of my range-based loops are getting vectorized in my actual program. What gives?

(In case this is buggy behavior I'm running VS2013 Professional Version 12.0.21005.1 REL)

EDIT: Bug report posted: https://connect.microsoft.com/VisualStudio/feedback/details/807826/range-based-for-loops-are-not-vectorized

4

1 回答 1

7

在此处发布错误报告:

https://connect.microsoft.com/VisualStudio/feedback/details/807826/range-based-for-loops-are-not-vectorized

回复:

您好,感谢您的报告。

向量化基于范围的循环代码是我们正在积极改进的事情。我们将解决向量化这个问题,并在编译器的未来版本中启用其他 C++ 语言和库功能的自动向量化。

原因码 1304(在 x64 上)和原因码 1301(在 x86 上)的发射是编译器内部的产物。对于这个特定的代码,其细节并不重要。

感谢您的报告!我正在关闭这个 MSConnect 项目。如果您需要其他任何内容,请随时回复。

Eric Brumer Microsoft Visual C++ 团队

于 2013-11-06T22:32:09.617 回答