Loop Unrolling Transformation: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
Line 50: Line 50:
These pragmas give developers flexibility to tailor loop transformations based on hardware characteristics (e.g., cache size, vector register width) or software constraints (e.g., real-time requirements or binary size limits).
These pragmas give developers flexibility to tailor loop transformations based on hardware characteristics (e.g., cache size, vector register width) or software constraints (e.g., real-time requirements or binary size limits).


==Loop Unrolling Transformation in emmtrix Studio==
== Loop Unrolling Transformation in emmtrix Studio ==


emmtrix Studio implements loop unrolling using #pragma directives or via the GUI. Unrolling will reduce the iteration count and increase the body of the loop, processing statements from multiple iteration steps in a single iteration.
emmtrix Studio supports loop unrolling both through #pragma directives and via the graphical user interface (GUI). Unrolling reduces the number of loop iterations by expanding the loop body to process multiple elements in each iteration.


By default, the transformation performs partial loop unrolling and creates two loops. The first loop is the partial unrolled loop and the second loop for the remaining iterations.
By default, the transformation applies partial loop unrolling, resulting in two loops:


emmtrix Studio tries to calculate the trip count that enables two optimizations:
- The first loop is the partially unrolled loop, processing multiple iterations per pass.
- The second loop handles any remaining iterations that do not fit into the unroll factor (cleanup loop).


# Full unrolling is applied if the unroll factor >= trip count.
emmtrix Studio attempts to calculate a trip count that enables two key optimizations:
# The loop for the remaining iterations is removed if trip count is dividable by the unroll factor.
 
1. Full unrolling is applied if the unroll factor is greater than or equal to the trip count.
2. The cleanup loop is omitted if the trip count is exactly divisible by the unroll factor.
 
=== Example ===


===Example===
In the following example, partial loop unrolling is applied with an unroll factor of 3.
In the following example, partial loop unrolling is applied with an unroll factor of 3.


Please note that a condition of <code>i < n - 2</code> would be incorrect in case of an overflow e.g. for <code>n = INT_MIN</code>.
Please note: The condition <code>i < n - 2</code> would be incorrect if <code>n</code> could be <code>INT_MIN</code>, due to potential overflow.
 
{| class="wikitable"
{| class="wikitable"
|-
|-
Line 70: Line 75:
#pragma EMX_TRANSFORMATION LoopUnroll { "unrollfactor": 3 }
#pragma EMX_TRANSFORMATION LoopUnroll { "unrollfactor": 3 }
for (int i = 1; i < n; i++) {
for (int i = 1; i < n; i++) {
printf("%d\n", i);
    printf("%d\n", i);
}
}
</syntaxhighlight>
</syntaxhighlight>
|<syntaxhighlight lang="c">
|<syntaxhighlight lang="c">
for (i = 1; (i + 2) < n; i = i + 3) {
for (i = 1; (i + 2) < n; i = i + 3) {
printf("%d\n", i);
    printf("%d\n", i);
printf("%d\n", i + 1);
    printf("%d\n", i + 1);
printf("%d\n", i + 2);
    printf("%d\n", i + 2);
}
}


for (; i < n; i = i + 1) {
for (; i < n; i = i + 1) {
printf("%d\n", i);
    printf("%d\n", i);
}
}
</syntaxhighlight>
</syntaxhighlight>
|}
|}


===Parameters===
=== Parameters ===


Following parameters can be set (each description is followed by keyword in pragma-syntax and default value):
The following parameters can be configured (each parameter includes its pragma keyword and default value):


{| class="wikitable"
{| class="wikitable"
|+
|+
!Id
!Parameter
!Default Value
!Default Value
!Description
!Description
Line 100: Line 103:
|<code>unrollfactor</code>
|<code>unrollfactor</code>
|2
|2
|'''Unroll factor''' - divide iteration count & multiply iterating variable. If equal to total number of iterations, loop-construct will be removed from code. If not integer divisor of total number of iterations, additional loop processing last iterations will be added
|'''Unroll factor''' - Specifies the loop unroll factor.
|-
|-
|<code>subfunctions</code>
|<code>subfunctions</code>
|false
|false
|'''Apply to sub functions''' -  
|'''Apply to subfunctions''' - Specifies whether the transformation should also apply to subfunctions.
|}
|}


Bots, Bureaucrats, Interface administrators, smwadministrator, smwcurator, smweditor, Administrators
2,557

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu