Compilers

Why is the loop above hard to vectorize with SIMD instructions?

for (i = 1; i < n; i++)
    a[i] = a[i - 1] + b[i];
  1. AEach a[i] needs a[i-1] from the previous iteration
  2. BThe array elements are integers, which SIMD instructions cannot process
  3. CVectorization is impossible whenever the trip count n is unknown at compile time
Show answer and explanation

Answer: A. Each a[i] needs a[i-1] from the previous iteration

Vectorization computes, say, four elements at once with one SIMD instruction. Here a[i] needs the result a[i-1] from the previous iteration, so a[1] through a[4] cannot be computed simultaneously. A dependence that crosses iterations (a loop-carried dependence) prevents straightforward vectorization. SIMD handles integers, and loops whose trip count is known only at run time can still be vectorized by handling the remainder separately.

  • A. Correct. Each result depends on the previous one, so they cannot be computed together.
  • B. SIMD instructions can process integers.
  • C. Unknown trip counts can be handled by processing the remainder separately.
Practice this topic Back to topic

More Compilers questions

  1. In the function above, why can the compiler not simply hoist the load of *b out of the loop?
  2. In register allocation by coloring an interference graph, what do the nodes and edges represent?
  3. What happens to generated code when register allocation causes a "spill"?
  4. What is the main purpose of instruction scheduling in a compiler?
  5. What is the effect of applying tail-call optimization to the final recursive call in the function above?