Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When targeting SpirV, Slang will misscompile global variables bound to a semantic when they are used in the same expression as WorkgroupSize(). #5027

Closed
gan74 opened this issue Sep 6, 2024 · 1 comment · Fixed by #5181
Assignees
Labels
goal:client support Feature or fix needed for a current slang user. kind:bug something doesn't work like it should

Comments

@gan74
Copy link
Contributor

gan74 commented Sep 6, 2024

Hi !

Slang misscompiles this code:

RWStructuredBuffer<uint> out_buffer;

in uint3 gid : SV_GroupID;

[shader("compute")]
[numthreads(8, 8)]
void comp_main() {
    uint expr = WorkgroupSize().x + gid.x;
    out_buffer[0] = expr;
}

When compiled with slangc -target spirv test.slang -o test.spv then dumped into glsl using spirv-cross test.spv, I get this:

#version 450
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;

layout(binding = 0, std430) buffer RWStructuredBuffer
{
    uint _m0[];
} out_buffer;

void main()
{
    uvec3 gid;
    out_buffer._m0[0] = uint(8 + int(gid.x));
}

Here gid isn't initialized, which leads to problems (first detected as a device lost due to an incorrect index computation leading to an out of bound access).

Weirdly this doesn't happen if I use gid directly without mixing it with WorkgroupSize() or if I take SV_GroupID as main argument.
It isn't specific to SV_GroupID, other semantics are also affected. It appears to be specific to WorkgroupSize() though.

This is a regression, it didn't happen before v2024.1.24. A quick bisection seems to point to commit 0b51902

@gan74 gan74 changed the title Slang will misscompile global variables bound to a semantic when they are used in the same expression as WorkgroupSize() When targeting SpirV, Slang will misscompile global variables bound to a semantic when they are used in the same expression as WorkgroupSize(). Sep 6, 2024
@ArielG-NV ArielG-NV added kind:bug something doesn't work like it should goal:client support Feature or fix needed for a current slang user. labels Sep 6, 2024
@ArielG-NV
Copy link
Contributor

ArielG-NV commented Sep 6, 2024

Note: It looks like only GLSL & SPIR-V experience invalid behavior (both do not translate gid into a system value). I do not think the single change inside 0b51902 is the cause of the bug (from what I see when testing).

@csyonghe csyonghe self-assigned this Sep 17, 2024
@csyonghe csyonghe added this to the Q3 2024 (Summer) milestone Sep 17, 2024
csyonghe added a commit to csyonghe/slang that referenced this issue Sep 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
goal:client support Feature or fix needed for a current slang user. kind:bug something doesn't work like it should
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants