Search code examples
c++visual-c++vectorizationcompiler-optimization

Microsoft C++ compiler: how to disable auto-vectorization with /O2?


How to disable auto-vectorization (SSE2), globally or for a specific for loop, without reverting to /Od in MSVS 2010?

I've tried all 3 possible options for Enable Enhanced Instruction Set, including "Not set", to no avail.

P. S. Curiously, even /Od doesn't help.


Solution

  • For a specific loop, you can add a pragma:

    #pragma loop(no_vector) 
    

    This is actually documented on MSDN (although I only found it there after I learned about it..)

    If you don't like to add a pragma, you can choose to compile with /favor:ATOM. That is a bit of hack, but it will allow you to disable auto-vectorization, without touching the source, and still optimize for speed otherwise.

    Alternatively there are the two optimization strategies /O1 "optimize size" and /Os "favor small code". Auto-vectorization generates substantially more code, so if you optimize for size, auto-vectorization is disabled.

    I learned all this recently from by reading the auto-vectorization cookbook. See the last line of the section "Rules for the loop body".

    Disclaimer: I am not actually using the VS2012 compiler yet (need to support Win XP), so I haven't tested this. Also, the compiler switches might work differently in 2013 or later.