Skip to content

[DirectX] Incorrect indexing into array of vectors from a Constant Buffer #156084

@Icohedron

Description

@Icohedron

Indices when accessing an element of a vector from an array of vectors from a Constant Buffer can be incorrect.

This appears to be the cause of issues #155919 (10800 test failures) and #155890 (1773 test failures), and likely the majority of the rest of the DML Operator Test failures.

Reproduction: https://godbolt.org/z/dfKsqf3MG

// compile args: -T cs_6_7
RWBuffer<uint> output;
cbuffer Constants {
    uint4 s[2];
};
[numthreads(1,1,1)]
void main() {
    output[0] = s[1][3];
}

IR Output

...
define void @main() {
  %1 = call %dx.types.Handle @dx.op.createHandleFromBinding(i32 217, %dx.types.ResBind { i32 0, i32 0, i32 0, i8 1 }, i32 0, i1 false) #1, !dbg !80
  %2 = call %dx.types.Handle @dx.op.annotateHandle(i32 216, %dx.types.Handle %1, %dx.types.ResourceProperties { i32 4106, i32 261 }) #1, !dbg !80
  %3 = call %dx.types.Handle @dx.op.createHandleFromBinding(i32 217, %dx.types.ResBind { i32 0, i32 0, i32 0, i8 2 }, i32 0, i1 false) #1, !dbg !82
  %4 = call %dx.types.Handle @dx.op.annotateHandle(i32 216, %dx.types.Handle %3, %dx.types.ResourceProperties { i32 13, i32 32 }) #1, !dbg !82
  %5 = call %dx.types.CBufRet.i32 @dx.op.cbufferLoadLegacy.i32(i32 59, %dx.types.Handle %4, i32 1) #2, !dbg !83
  %6 = extractvalue %dx.types.CBufRet.i32 %5, 0, !dbg !83 ; <-- the index 0 should be a 3
  call void @dx.op.bufferStore.i32(i32 69, %dx.types.Handle %2, i32 0, i32 undef, i32 %6, i32 %6, i32 %6, i32 %6, i8 15), !dbg !91
  ret void
}
...

Thanks to @llvm-beanz for pointing out the RWStructuredBuffer reproductions were not an issue at all; the 12-byte offset was encoded into the rawbuffer read so the 0 index was actually correct. Also pointed out the issue is with the dxil-cbuffer-access pass computing the incorrect offset: https://godbolt.org/z/zhPK4473f

Metadata

Metadata

Assignees

Type

Projects

Status

Active

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions