简体   繁体   中英

Why would function templates not be analyzed by MSVC12's auto-vectorizer?

Function templates are not seen by the auto-vectorization or the auto-parallelizer (/Qpar) engine in VS2013.

For example, this code:

void foo::someFunc(int a)
{
    int myArray[1000000];

    for (unsigned i = 0; i < 1000000; i++)
    {
       myArray[i] = i+1;
    }
}

seems to be recognized and I get the appropriate output from /Qvec-report:2 and /Qpar-report:2:

foo.cpp

--- Analyzing function: void __cdecl foo::someFunc(int) __ptr64
c:\visual studio 2013\projects\autovectest\autovectest\foo.cpp(18) : info C5001: loop vectorized
c:\visual studio 2013\projects\autovectest\autovectest\foo.cpp(18) : info C5012: loop not parallelized due to reason '1007'
AutoVecTest.vcxproj -> c:\visual studio 2013\Projects\AutoVecTest\x64\Debug\AutoVecTest.dll

But, as soon as I turn someFunc() into a function template:

template <class T>
void foo::someFunc(T a)
{
    int myArray[1000000];

    for (unsigned i = 0; i < 1000000; i++)
    {
        myArray[i] = i+1;
    }
}

I get nothing from the auto-vectorizer or the auto-parallelizer in the logs:

foo.cpp
AutoVecTest.vcxproj -> c:\visual studio 2013\Projects\AutoVecTest\x64\Debug\AutoVecTest.dll

I am not using /GL as stated in Why would /Qvec-report:2 return nothing ? (MSVC 2012)

  1. As Retired Ninja pointed out, make sure your function template is actually called or instantiated.

  2. Make sure the proper optimization compile flags are enabled. YMMV here, but Visual Studio 2012's Auto-Vectorization Cookbook says /O2 or /O2 /GL will work. Another user discovered /GL did not work for them ( Why would /Qvec-report:2 return nothing ? (MSVC 2012) ). Using #pragma("gt", on) enabled the auto-vectorizer for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM