microsoft / microsoft/DirectXShaderCompiler
[DXIL] Incorrect codegen when using "static" on groupshared variables
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 3.7k
- Forks
- 900
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 44
Description
Hello all,
We've noticed that when using "static" for bool-like groupshared variables in HLSL the resulting DXIL code incorrectly uses the "i1" type for this variable instead of "i32" in the version without "static". According to the DXIL docs, i1 types are not supported for groupshared memory.
The issue can be seen using the minimal repro-case we've come up with:
RWBuffer<uint> TileArgsBufferOut;
RWBuffer<uint> TilesOut;
static groupshared uint storeTile;
[numthreads(8,8,1)]
void main(uint3 dtid : SV_DispatchThreadID, uint3 gtid : SV_GroupThreadID, uint3 gid : SV_GroupID)
{
if (gtid.x == 0 && gtid.y == 0)
{
storeTile = 0;
}
GroupMemoryBarrierWithGroupSync();
storeTile = 1;
GroupMemoryBarrierWithGroupSync();
if (gtid.x == 0 && gtid.y == 0 && storeTile > 0)
{
const uint tileId = gid.y * 1024 + gid.x;
uint tileStoreIdx;
InterlockedAdd(TileArgsBufferOut[0], 1, tileStoreIdx);
TilesOut[tileStoreIdx] = tileId;
}
}
In this code, "storeTile" will be declared as @storeTile = internal unnamed_addr addrspace(3) global i1 false when using "static" and declared as @"\01?storeTile@@3IA" = external addrspace(3) global i32, align 4 when not using "static". (Verified on ShaderPlayground using the DXC version from 2022-06-18, compiled as cs_6_0 and cs_6_6)
In our case (on AMD RX 6700 XT Driver Version 22.5.2) this results in the last if-clause (if (gtid.x == 0 && gtid.y == 0 && storeTile > 0)) not being executed on any thread, leaving the "TilesOut" buffer completely empty. This bug does not reproduce when omitting "static" from the storeTile-declaration or when using FXC instead.
We've also verified that this bug also reproduces on NVIDIA with latest drivers - although slightly less frequent. Still, it was enough for us to rule-out a vendor-specific driver issue.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the minimal HLSL repro and compile it through DXC for cs_6_0 and cs_6_6, comparing the generated DXIL with and without static. Check the DXIL memory-access granularity requirements in the linked DXIL documentation; done means static groupshared variables no longer generate unsupported i1 storage and the repro executes its final condition correctly.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100