Loop splitting: Difference between revisions

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
Brief history of the term: Improved writing style
 
Improve readability
 
Line 8: Line 8:
Suppose a loop was written like this:
Suppose a loop was written like this:
<syntaxhighlight lang="c">
<syntaxhighlight lang="c">
int p = 10;
int p = 10;
for (int i=0; i<10; ++i)
for (int i = 0; i < 10; ++i) {
{
    y[i] = x[i] + x[p];
  y[i] = x[i] + x[p];
    p = i;
  p = i;
}
}
</syntaxhighlight>
</syntaxhighlight>
Notice that <code>p = 10</code> only for the first iteration, and for all other iterations, <code>p = i - 1</code>. A compiler can take advantage of this by [[loop unwinding|unwinding]] (or "peeling") the first iteration from the loop.
Notice that <code>p = 10</code> only for the first iteration, and for all other iterations, <code>p = i - 1</code>. A compiler can take advantage of this by [[loop unwinding|unwinding]] (or "peeling") the first iteration from the loop.
Line 19: Line 18:
After peeling the first iteration, the code would look like this:
After peeling the first iteration, the code would look like this:
<syntaxhighlight lang="c">
<syntaxhighlight lang="c">
y[0] = x[0] + x[10];
y[0] = x[0] + x[10];
for (int i=1; i<10; ++i)
for (int i = 1; i < 10; ++i) {
{
    y[i] = x[i] + x[i - 1];
  y[i] = x[i] + x[i-1];
}
}
</syntaxhighlight>
</syntaxhighlight>
This equivalent form eliminates the need for the variable <code>p</code> inside the loop body.
This equivalent form eliminates the need for the variable <code>p</code> inside the loop body.

Latest revision as of 21:52, 4 September 2025

Template:Short description Template:Use dmy dates Loop splitting is a compiler optimization technique. It attempts to simplify a loop or eliminate dependencies by breaking it into multiple loops which have the same bodies but iterate over different contiguous portions of the index range.

Loop peeling

Loop peeling is a special case of loop splitting which splits any problematic first (or last) few iterations from the loop and performs them outside of the loop body.

Suppose a loop was written like this:

int p = 10;
for (int i = 0; i < 10; ++i) {
    y[i] = x[i] + x[p];
    p = i;
}

Notice that p = 10 only for the first iteration, and for all other iterations, p = i - 1. A compiler can take advantage of this by unwinding (or "peeling") the first iteration from the loop.

After peeling the first iteration, the code would look like this:

y[0] = x[0] + x[10];
for (int i = 1; i < 10; ++i) {
    y[i] = x[i] + x[i - 1];
}

This equivalent form eliminates the need for the variable p inside the loop body.

Loop peeling was introduced in gcc in version 3.4. More generalised loop splitting was added in GCC 7.[1]

Brief history of the term

Apparently the term "peeling" was for the first time used by Cannings, Thompson and Skolnick[2] in their 1976 paper on computational models for (human) inheritance. There the term was used to denote a method for collapsing phenotypic information onto parents. From there the term was used again in their papers, including their seminal paper on probability functions on complex pedigrees.[3]

In compiler technology, the term first turned up in late 1980s papers on VLIW and superscalar compilation.[4][5]

References

<templatestyles src="Reflist/styles.css" />

  1. GCC 7 Release Series — Changes, New Features, and Fixes - GNU Project
  2. Script error: No such module "Citation/CS1".
  3. Script error: No such module "Citation/CS1".
  4. Script error: No such module "Citation/CS1".
  5. Script error: No such module "citation/CS1".

Script error: No such module "Check for unknown parameters".

Further reading

  • Script error: No such module "citation/CS1".

Template:Compiler optimizations