Skip to content

Commit

Permalink
Vectorize Ramp in OpenGLCompute backend (#6372)
Browse files Browse the repository at this point in the history
Currently, ramps are generated as a number of independent scalar
expressions that are finally gathered into a vector. For instance,
indexing in vectorized code is filled with ramps like the following:

```
int _11 = int(1) * int(1);
int _12 = _10 + _11;
int _13 = int(2) * int(1);
int _14 = _10 + _13;
int _15 = int(3) * int(1);
int _16 = _10 + _15;
ivec4 _17 = ivec4(_10, _12, _14, _16);
```

This patch simplifies the generated code using a multiply add expression
on a vector containing an arithmetic expression, such that the code is
as follows:

```
ivec4 _11 = ivec4(0, 1, 2, 3) * int(1) + _10;
```

This is more performant due to vectorization, more compact, and more
readable because the base and the stride are easily identifiable.
  • Loading branch information
OmarEmaraDev authored Nov 3, 2021
1 parent 2cf3afb commit 76315a2
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/CodeGen_OpenGLCompute_Dev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,20 +550,24 @@ void CodeGen_OpenGLCompute_C::visit(const For *loop) {
}

void CodeGen_OpenGLCompute_C::visit(const Ramp *op) {
ostringstream rhs;
rhs << print_type(op->type) << "(";

if (op->lanes > 4) {
internal_error << "GLSL: ramp lanes " << op->lanes << " is not supported\n";
}

rhs << print_expr(op->base);

for (int i = 1; i < op->lanes; ++i) {
rhs << ", " << print_expr(Add::make(op->base, Mul::make(i, op->stride)));
ostringstream rhs;
// Print the sequence vec(0, 1, 2, ...).
rhs << print_type(op->type) << "(";
for (int i = 0; i < op->type.lanes(); i++) {
rhs << i;
if (i != op->type.lanes() - 1) {
rhs << ", ";
}
}

rhs << ")";

// Multiply by the stride and add the base.
rhs << " * " << print_expr(op->stride) << " + " << print_expr(op->base);

print_assignment(op->type, rhs.str());
}

Expand Down

0 comments on commit 76315a2

Please sign in to comment.