Are there any preprocessor directives that control loop unrolling?

Steve Barna picture Steve Barna · Oct 11, 2012 · Viewed 10.2k times · Source

Furthermore, how does the compiler determine the extent to unroll a loop, assuming all operations in the loop are completely independent of other iterations.

Answer

osgx picture osgx · Oct 11, 2012

For MSVC there is only a vector independence hint: http://msdn.microsoft.com/en-us/library/hh923901.aspx

#pragma loop( ivdep )

For many other compilers, like Intel/ibm, there a several pragma hints for optimizing a loop:

#pragma unroll
#pragma loop count N
#pragma ivdep

There is a thread with MSVC++ people about unroll heuristic: http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/d0b225c2-f5b0-4bb9-ac6a-4d4f61f7cb17/

VC tries to balance execution speed and code size. You can change the balance by using flags /O1 or /O2, but even when optimzing for speed VC tries to conserve code size as well.

Basically, unroll will increase code size, so it may be limited in Os and O1 modes (modes table)

PS: Pragma looks like preprocessor directive, but it is not. It is a directive for compiler and it it ignored (kept) by preprocessor.