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?)
This comment was marked helpful 0 times.
iamk_d___g
@kayvonf: Let me have a try to get it started... from a high level:
Perform a syntax checking. Besides the C-like rules, also making sure that no varying values are assigned to uniform values.
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.
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.
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.
For conditional statements, mask (just like in the assignment) could be used.
For loops, they should be treated as normal loops, and mask, again, could be used in the condition checking part.
This comment was marked helpful 0 times.
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.
This comment was marked helpful 0 times.
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.
Question: Can someone explain the semantics of ISPC's
foreach
construct? If you were the ISPC compiler, how might you implementforeach
? (a.k.a', how might you translate this program withforeach
into a valid ISPC program without it?)This comment was marked helpful 0 times.
@kayvonf: Let me have a try to get it started... from a high level:
Perform a syntax checking. Besides the C-like rules, also making sure that no varying values are assigned to uniform values.
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.
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.
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.
For conditional statements, mask (just like in the assignment) could be used.
For loops, they should be treated as normal loops, and mask, again, could be used in the condition checking part.
This comment was marked helpful 0 times.
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 withoutforeach
?)I'm also still looking for someone to describe the semantics of
foreach
.This comment was marked helpful 0 times.
When ISPC compiler encounters a
foreach
, it translates the loop body into two parts. One part is an ordinaryfor
loop withprogramCount
andprogramIndex
. The difference between the ordinaryfor
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, anif
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
This comment was marked helpful 0 times.