Skip to content

Commit 5a10e30

Browse files
committed
Comments.
1 parent 7fd0e17 commit 5a10e30

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/CodeGen_GPU_Dev.cpp

+16-2
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ void CodeGen_GPU_C::visit(const Shuffle *op) {
149149
internal_assert(op->vectors[0].type() == op->vectors[i].type());
150150
}
151151
internal_assert(op->type.lanes() == (int)op->indices.size());
152+
// We need to construct the mapping between shuffled-index,
153+
// and source-vector-index and source-element-index-within-the-vector.
154+
// To start, we'll figure out what the first shuffle-index is per
155+
// source-vector. Also let's compute the total number of
156+
// source-elements the to be able to assert that all of the
157+
// shuffle-indices are within range.
152158
std::vector<int> vector_first_index;
153159
int max_index = 0;
154160
for (const Expr &v : op->vectors) {
@@ -182,15 +188,21 @@ void CodeGen_GPU_C::visit(const Shuffle *op) {
182188
for (int i : op->indices) {
183189
size_t vector_idx;
184190
int lane_idx = -1;
191+
// Find in which source vector this shuffle-index "i" falls:
185192
for (vector_idx = 0; vector_idx < op->vectors.size(); ++vector_idx) {
186-
if (i >= vector_first_index[vector_idx] && i < vector_first_index[vector_idx] + op->vectors[vector_idx].type().lanes()) {
187-
lane_idx = i - vector_first_index[vector_idx];
193+
const int first_index = vector_first_index[vector_idx];
194+
if (i >= first_index &&
195+
i < first_index + op->vectors[vector_idx].type().lanes()) {
196+
lane_idx = i - first_index;
188197
break;
189198
}
190199
}
191200
internal_assert(lane_idx != -1) << "Shuffle lane index not found: i=" << i;
192201
internal_assert(vector_idx < op->vectors.size()) << "Shuffle vector index not found: i=" << i << ", lane=" << lane_idx;
202+
// Print the vector in which we will index.
193203
rhs << vecs[vector_idx];
204+
// In case we are dealing with an actual vector instead of scalar,
205+
// print out the required indexing syntax.
194206
if (op->vectors[vector_idx].type().lanes() > 1) {
195207
switch (vector_declaration_style) {
196208
case VectorDeclarationStyle::OpenCLSyntax:
@@ -202,6 +214,8 @@ void CodeGen_GPU_C::visit(const Shuffle *op) {
202214
break;
203215
}
204216
}
217+
218+
// Elements of a vector are comma separated.
205219
if (elem_num < (int)(op->indices.size() - 1)) {
206220
rhs << ", ";
207221
}

0 commit comments

Comments
 (0)