KhronosGroup / KhronosGroup/SPIRV-Tools

spirv-opt: "unroll" loops with a count of 1

Open
#3,464 0 comments 1 reaction 1 assignee Claimed by @s-perron View on GitHub
component:optimization
Dominant language
C++
Stars
1.4k
Forks
709
Avg merge
1d 22h
Merged PRs (30d)
28

Description

It would be nice for spirv-opt to systematically "unroll" loops with a constant iteration count of 1 (without having to specify `[[unroll]]` which is not available in glsl by default).

e.g.:

```
#version 310 es
precision mediump float;
precision mediump int;
layout(location = 0) uniform float count;
layout(location = 0) out vec4 fragColor;
void main() {
vec4 r = vec4(0.0);
for (float i=0.0 ; i<1.0 ; i+=1.0) {
r += count;
}
for (int j=0 ; j<1 ; j+=1) {
r += count;
}
fragColor = vec4(r);
}
```

When doing:

```
./glslangValidator --target-env opengl test.frag -o test.spv
./spirv-opt -O test.spv -o test.opt.spv
./spirv-cross --es test.opt.spv
```

Yields:

```
#version 310 es
precision mediump float;
precision highp int;

layout(location = 0) uniform float count;

layout(location = 0) out vec4 fragColor;

void main()
{
vec4 _60;
_60 = vec4(0.0);
for (float _57 = 0.0; _57 < 1.0; )
{
_60 += vec4(count);
_57 += 1.0;
continue;
}
vec4 _59;
_59 = _60;
for (mediump int _58 = 0; _58 < 1; )
{
_59 += vec4(count);
_58++;
continue;
}
fragColor = _59;
}
```

Note: these loops above go away entirely if the iteration count is 0, so the optimizer handles that, it would seem natural to handle the special case of one iteration too.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.