microsoft / microsoft/DirectXShaderCompiler

failed to legalize SPIR-V: Variable cannot be replaced: invalid index on embedded loop

Open
#8,832 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug needs-triage spirv
Dominant language
C++
Stars
3.7k
Forks
900
Avg merge
2d 11h
Merged PRs (30d)
44

Description

Description
A shader does not compile with "-fspv-flatten-resource-arrays" with a mixture of for loops and ifs

Note: the shader compiles when the if() condition is moved inside the inner loop.

Steps to Reproduce
Compile the following shader

RWTexture2D HZBDstTextures[16];
[numthreads(8, 8, 1)]
void main(uint3 groupId : SV_GroupID)
{
[unroll]
for (uint srcTextureID = 0; srcTextureID < 16; srcTextureID++)
{
[branch]
if (srcTextureID == groupId.z)
{
[unroll]
for (uint mipOffset = 1; mipOffset < 4; mipOffset++)
{
const uint dstTextureID = srcTextureID + mipOffset;
HZBDstTextures[dstTextureID][uint2(0, 0)] = 2.2f;
}
}
}
}

With the following command line:
dxc.exe" /T cs_6_0 -spirv -fspv-flatten-resource-arrays

Actual Behavior
The following error message is reported:
fatal error: failed to legalize SPIR-V: Variable cannot be replaced: invalid index
%87 = OpAccessChain %_ptr_UniformConstant_type_2d_image %HZBDstTextures %85
note: please file a bug report on https://github.com/Microsoft/DirectXShaderCompiler/issues with source code if possible

Environment

  • DXC version dxcompiler.dll: 1.10(5373-c4d8f4f9)(1.10.2605.37) - 1.10.2605.37 (c4d8f4f99)
  • Host Operating System Windows 11 Enterprise 24H2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the provided shader with dxc.exe using -T cs_6_0 -spirv -fspv-flatten-resource-arrays and inspect the SPIR-V legalization path in dxcompiler.dll that handles the flattened resource array access. Done means the shader compiles without the invalid-index legalization error, with coverage for the loop and conditional pattern.

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
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.