Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tell gcc to specifically unroll a loop

Tags:

c

gcc

pragma

unroll

How can I tell GCC to unroll a particular loop? I have used the CUDA SDK where loops can be unrolled manually using #pragma unroll. Is there a similar feature for gcc? I googled a bit but could not find anything.

like image 462
Nils Avatar asked Nov 01 '10 18:11

Nils


People also ask

Does GCC do loop unrolling?

I have found the gcc flag -funroll-all-loops . If I understand correctly, this will unroll all loops automatically without any efforts by the programmer.

What is pragma unroll?

The UNROLL pragma specifies to the compiler how many times a loop should be unrolled. The UNROLL pragma is useful for helping the compiler utilize SIMD instructions. It is also useful in cases where better utilization of software pipeline resources are needed over a non-unrolled loop.

What is pragma GCC optimize?

The syntax is #pragma GCC optimize (option, ...) From the official source on GCC pragmas, this pragma allows you to set global optimization flags (specified by option ) for functions that come after it.


2 Answers

GCC gives you a few different ways of handling this:

  • Use #pragma directives, like #pragma GCC optimize ("string"...), as seen in the GCC docs. Note that the pragma makes the optimizations global for the remaining functions. If you used #pragma push_options and pop_options macros cleverly, you could probably define this around just one function like so:

    #pragma GCC push_options #pragma GCC optimize ("unroll-loops")  //add 5 to each element of the int array. void add5(int a[20]) {     int i = 19;     for(; i > 0; i--) {         a[i] += 5;     } }  #pragma GCC pop_options 
  • Annotate individual functions with GCC's attribute syntax: check the GCC function attribute docs for a more detailed dissertation on the subject. An example:

    //add 5 to each element of the int array. __attribute__((optimize("unroll-loops"))) void add5(int a[20]) {     int i = 19;     for(; i > 0; i--) {         a[i] += 5;     } } 

Note: I'm not sure how good GCC is at unrolling reverse-iterated loops (I did it to get Markdown to play nice with my code). The examples should compile fine, though.

like image 76
Philip Conrad Avatar answered Oct 11 '22 12:10

Philip Conrad


GCC 8 has gained a new pragma that allows you to control how loop unrolling is done:

#pragma GCC unroll n

Quoting from the manual:

You can use this pragma to control how many times a loop should be unrolled. It must be placed immediately before a for, while or do loop or a #pragma GCC ivdep, and applies only to the loop that follows. n is an integer constant expression specifying the unrolling factor. The values of 0 and 1 block any unrolling of the loop.

like image 26
Frederik Deweerdt Avatar answered Oct 11 '22 11:10

Frederik Deweerdt