Specifying Attribute For Unrolling Loops.
__attribute__((opencl_unroll_hint)) __attribute__((opencl_unroll_hint(n)))
The attributeopencl_unroll_hint and attributeopencl_unroll_hint(n) attribute qualifiers can be used to specify that a loop (for, while and do loops) can be unrolled.
This attribute qualifier can be used to specify full unrolling or partial unrolling by a specified amount.
This is a compiler hint and the compiler may ignore this directive.
n is the loop unrolling factor and must be a positive integral compile time constant expression.
An unroll factor of 1 disables unrolling.
If n is not specified, the compiler determines the unrolling factor for the loop.
|
Note
|
The attributeopencl_unroll_hint(n) attribute qualifier must appear immediately before the loop to be affected.
|
__attribute__((opencl_unroll_hint(2)))
while (*s != 0)
*p++ = *s++;
This tells the compiler to unroll the above while loop by a factor of 2.
__attribute__((opencl_unroll_hint))
for (int i=0; i<2; i++)
{
…
}
In the example above, the compiler will determine how much to unroll the loop.
__attribute__((opencl_unroll_hint(1)))
for (int i=0; i<32; i++)
{
…
}
The above is an example where the loop should not be unrolled.
Below are some examples of invalid usage of attributeopencl_unroll_hint(n).
__attribute__((opencl_unroll_hint(-1)))
while (…)
{
…
}
The above example is an invalid usage of the loop unroll factor as the loop unroll factor is negative.
__attribute__((opencl_unroll_hint))
if (…)
{
…
}
The above example is invalid because the unroll attribute qualifier is used on a non-loop construct.
kernel void
my_kernel( … )
{
int x;
__attribute__((opencl_unroll_hint(x))
for (int i=0; i<x; i++)
{
…
}
}
The above example is invalid because the loop unroll factor is not a compile-time constant expression.