KhronosGroup / KhronosGroup/SPIRV-Tools

spirv-opt doesn't simplify constant mod() operations

Open
#3,465 0 comments 1 reaction 0 assignees View on GitHub
component:optimization
Dominant language
C++
Stars
1.4k
Forks
709
Avg merge
1d 22h
Merged PRs (30d)
28

Description

In some situations, `mod(constant0, constant1)` won't be replaced with its value, which in turn will cause functions depending on it to not be replaced as well. mod(constant, 2.0) is common to check parity. e.g.:

```
#version 310 es
precision mediump float;
precision mediump int;
layout(location = 0) out vec4 fragColor;

float doSomething(float x) {
return mod(x, 2.0);
}
void main() {
fragColor = vec4(doSomething(2.0));
}
```

```
./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 to:

```
#version 310 es
precision mediump float;
precision highp int;
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = vec4(mod(2.0, 2.0));
}
```

Strangely, replacing `mod` with `sqrt` gets replaced with its value, so I conclude it's not a problem with the optimization options/flags, but rather something special about `mod`.

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with glslangValidator, spirv-opt -O, and spirv-cross using the GLSL example in the report. Compare the optimized output for mod(2.0, 2.0) with the reported sqrt behavior. Done means the constant mod operation is folded and dependent expressions are simplified in the generated output.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.