Specifying Attribute For Unrolling Loops

Specifying Attribute For Unrolling Loops.



The __attribute__((opencl_unroll_hint)) and __attribute__((opencl_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 __attribute__((opencl_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 __attribute__((opencl_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.


OpenCL Specification

Also see

__attribute__, Type Attributes, Variable Attributes

Copyright © 2007-2013 The Khronos Group Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and/or associated documentation files (the "Materials"), to deal in the Materials without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Materials, and to permit persons to whom the Materials are furnished to do so, subject to the condition that this copyright notice and permission notice shall be included in all copies or substantial portions of the Materials.