KhronosGroup / KhronosGroup/glslang

Possible zero array size from spec-const expression not disallowed

Open
#3,029 1 comment 0 reactions 0 assignees View on GitHub
bug GLSL/ESSL SPIR-V
Dominant language
C++
Stars
3.6k
Forks
989
Avg merge
1d 2h
Merged PRs (30d)
31

Description

Observe:

**broken.comp**:
```glsl
#version 450
#extension GL_ARB_compute_shader : enable

layout(constant_id=0) const uint foo = 0u;
shared float bar[foo];

void main() {}
```

**fixed.comp**:
```glsl
#version 450
#extension GL_ARB_compute_shader : enable

layout(constant_id=0) const uint foo = 0u;
shared float bar[foo + 0u];

void main() {}
```

Output:
```
$ glslangValidator --target-env vulkan1.2 broken.comp
broken.comp
ERROR: broken.comp:5: '' : array size must be a positive integer
ERROR: broken.comp:5: '' : compilation terminated
ERROR: 2 compilation errors. No code generated.
```

```
$ glslangValidator --target-env vulkan1.2 fixed.comp
fixed.comp
```

Why does adding a `+ 0u` to the array size spec constant make it succeed compiling? Incidentally, the shader works fine at runtime - assuming the correct (nonzero) array size is substituted in using a `VkSpecializationInfo` at pipeline creation time.

Contributor guide

Open the contributing guide

Research direction

Reproduce the difference between broken.comp and fixed.comp with glslangValidator --target-env vulkan1.2. Trace how the compiler validates the shared array size when it is a specialization-constant expression, then compare that path with the expression containing + 0u. Done means the behavior is explained and zero-valued defaults are handled consistently with specialization at pipeline creation.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.