Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 849
Violet Giraffe Avatar asked Oct 24 '25 04:10

Violet Giraffe


2 Answers

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.

You could isolate your for loop in a separated function and try to use #pragma optimize for it:

// Disable all optimizations
#pragma optimize("", off)

// your function here

// Enable them back
#pragma optimize("", on)

... but this should have the same effect of /Od just on that particular function, so it may not help.

If you are compiling for x86 (and not x86_64, where it has no effect) you could also disable the SSE2 instruction set as a whole (removing the /arch:SSE2 option). Sadly, its granularity is limited to a whole source file.

like image 34
Matteo Italia Avatar answered Oct 26 '25 18:10

Matteo Italia