microsoft / microsoft/DirectXShaderCompiler

Incorrect element index when accessing matrix in `cbuffer`

Open
#7,253 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description
DXC is generating incorrect element index when accessing matrix in a cbuffer.

Steps to Reproduce
https://godbolt.org/z/W1f1G1MYG
Compile with: dxc -T ps_6_6 -E main

cbuffer CB0 : register(b0) {
    float a;
    float b;
    float c;
    float1x1 m;
};
 
float4 main() : SV_Target
{
    float x = m._11;
    float y = a;
    float z = b;
    return float4(x, a, b, 0);
}

The generated code only accesses the at elements at indices 0 and 1. When accessing m._11 is it using the index 0 which is the index of a.

define void @main() {
  %CB0_cbuffer = call %dx.types.Handle @dx.op.createHandleFromBinding(i32 217, %dx.types.ResBind { i32 0, i32 0, i32 0, i8 2 }, i32 0, i1 false)  ; CreateHandleFromBinding(bind,index,nonUniformIndex)
  %1 = call %dx.types.Handle @dx.op.annotateHandle(i32 216, %dx.types.Handle %CB0_cbuffer, %dx.types.ResourceProperties { i32 13, i32 16 })  ; AnnotateHandle(res,props)  resource: CBuffer
  %2 = call %dx.types.CBufRet.f32 @dx.op.cbufferLoadLegacy.f32(i32 59, %dx.types.Handle %1, i32 0) ; line:24 col:15  ; CBufferLoadLegacy(handle,regIndex)
  %3 = extractvalue %dx.types.CBufRet.f32 %2, 0 ; line:24 col:15
  %4 = extractvalue %dx.types.CBufRet.f32 %2, 1 ; line:27 col:25
  call void @dx.op.storeOutput.f32(i32 5, i32 0, i32 0, i8 0, float %3) ; line:27 col:5  ; StoreOutput(outputSigId,rowIndex,colIndex,value)
  call void @dx.op.storeOutput.f32(i32 5, i32 0, i32 0, i8 1, float %3) ; line:27 col:5  ; StoreOutput(outputSigId,rowIndex,colIndex,value)
  call void @dx.op.storeOutput.f32(i32 5, i32 0, i32 0, i8 2, float %4) ; line:27 col:5  ; StoreOutput(outputSigId,rowIndex,colIndex,value)
  call void @dx.op.storeOutput.f32(i32 5, i32 0, i32 0, i8 3, float 0.000000e+00) ; line:27 col:5  ; StoreOutput(outputSigId,rowIndex,colIndex,value)
  ret void ; line:27 col:5
}

Actual Behavior
The m._11 value has element offset 3.

Environment

  • DXC main as of 3/24/2025

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 Godbolt reproducer with dxc -T ps_6_6 -E main and inspect the generated DXIL around cbufferLoadLegacy.f32. Trace how the m._11 access is assigned its element offset, then verify that the generated access uses offset 3 rather than the offset for a and that the other shown accesses remain correct.

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
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.