2

If I have this code:

#pragma unroll 1
for (i=0;i<5;i++)
{
   a[i]=i;
}

I read somewhere said #pragma unroll 1 will prevent the compiler from unrolling the for loop. Why is that? And why does it not unroll the loop the first time, and run the for loop on the rest (ie a[0]=0; then run for (i=1;i<5;i++))?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
pg405
  • 65
  • 6
  • 1
    Which compiler? – zwol Oct 02 '18 at 00:18
  • 1
    How would you unroll a loop 1 time? Based on your example I would say you're not sure what unrolling does. The examples here may help clarify things for you. Look at the unroll(3) example. https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.cbclx01/opt_unroll.htm – Retired Ninja Oct 02 '18 at 00:21
  • I wasn't really familiar with pragma unroll, but with your particular loop, the absence of pragma, unroll 1 and unroll three compile identically on g++ 4.2.1 with default optimizations. – mreff555 Oct 02 '18 at 00:40
  • You can edit your own question, and fix the title, etc. I've done it for you this time, but you should do it yourself in future. – Jonathan Leffler Oct 02 '18 at 00:51
  • Note that most pragmas are implementation specific. The whole pragma feature was created specifically as an extension point, in fact, but modern C does define a handful of pragmas itself. The key thing, though, is that we can only guess about what *your* `#pragma unroll 1` does or should do, unless you tell us which C implementation you're talking about. – John Bollinger Oct 02 '18 at 00:54
  • 2
    For GCC, see [Using loop-specific pragmas](https://gcc.gnu.org/onlinedocs/gcc-8.2.0/gcc/Loop-Specific-Pragmas.html) — where it says _"The values of 0 and 1 block any unrolling of the loop"._ To the extent I understand it, `unroll 2` would mean embedding 2 iterations of the loop in a single cycle; `unroll 3` would mean embedding 3 iterations in a single cycle; so `unroll 1` is what happens with no unrolling, and `unroll 0` doesn't have a useful meaning so it is also deemed a no-op. – Jonathan Leffler Oct 02 '18 at 00:55

0 Answers0