0

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)

James
  • 270
  • 1
  • 10
  • 1
    Are you actually calling the function? `1>--- Analyzing function: void __cdecl someFunc(int) 1>c:\temp\test\test.cpp(321) : info C5001: loop vectorized 1>c:\temp\test\test.cpp(321) : info C5012: loop not parallelized due to reason '1007'` – Retired Ninja Aug 24 '18 at 00:41
  • I suppose I'm not in my sample code. But I am with my real life code. Let me adjust my sample code and report back – James Aug 24 '18 at 01:22
  • 1
    Success with my sample code! If I call a templated funciton (ie ```int a; someFunc(a);``` or explicitly instantiate the function (```template void someFunc(int a)```), I get the auto-vectorizor/parallalizer messages. – James Aug 24 '18 at 01:32
  • Looking at the disassembly, I guess my sample code isn't accurately reflecting my real life code issue. In my sample code, if I do not call someFunc() or explicitly instantiate someFunc(), someFunc() does not show up in the obj file (using dumpbin /disasm). However, in my real life code, my functions appear in the obj file, so the template functions are at least being created. So I'm not sure that's the issue... – James Aug 24 '18 at 02:07
  • 1
    Ok, I figured out the discrepancy between the sample code and my real life code. My real code's header file had a ```#pragma optimize("gt", on)``` that was never turned off, so it was erroneously auto-vectorizing some functions that it shouldn't have been. Once I added the ```#pragma optimize("", on)``` after the intended function, the auto-vectorizer behaved as it should have. – James Aug 24 '18 at 22:50

1 Answers1

0
  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.

James
  • 270
  • 1
  • 10