Previous | Next --- Slide 14 of 52
Back to Lecture Thumbnails
kayvonf

Question: Can someone explain the semantics of ISPC's foreach construct? If you were the ISPC compiler, how might you implement foreach? (a.k.a', how might you translate this program with foreach into a valid ISPC program without it?)

iamk_d___g

@kayvonf: Let me have a try to get it started... from a high level:

  1. Perform a syntax checking. Besides the C-like rules, also making sure that no varying values are assigned to uniform values.

  2. Slice the foreach range into chunks of the vector size, as specified in the --target flag. An extra for loop may be needed for some remaining elements.

  3. In the chunk size for loop, try to make everything to a vector if possible: for array accesses or variables or constants, create vector temporaries storing them and perform the corresponding vector operation. Those temporaries are later allocated to XMM registers or memory locations.

  4. For uniform variables, they should be just allocated to one position to save storage space. That is, uniform variables live among iterations, while varying variables not. This will help register allocation to just allocate a single place for uniform variables.

  5. For conditional statements, mask (just like in the assignment) could be used.

  6. For loops, they should be treated as normal loops, and mask, again, could be used in the condition checking part.

kayvonf

Ah, to clarify the "imagine you were the compiler comment", what I was really asking was: Can you write an ISPC program that has the exact same outcome as the code above, without using foreach? (That is, what's a source-to-source transformation of the code above, to a program without foreach?)

I'm also still looking for someone to describe the semantics of foreach.

squidrice

When ISPC compiler encounters a foreach, it translates the loop body into two parts. One part is an ordinary for loop with programCount and programIndex. The difference between the ordinary for and this one is the amount of data they process. The data here can fit exactly into the gang, so the implementation won't have inefficient mask operation. The data that can't fit into the gang is handled by the other part, an if statement.

References:

Efficient Iteration With "foreach" - IntelĀ® SPMD Program Compiler Performance Guide

foreach statements generate more complex assembly than I'd expect; what's going on? - IntelĀ® SPMD Program Compiler Performance Guide