Skip to content

Commit 4c613aa

Browse files
authored
Fix incorrect OpAccessChain for inherited struct members (#8005)
Adds a new test case to verify that the OpAccessChain is generated correctly for inherited members of a struct. This fixes an issue where the indices for a base class were being prepended to the indices of the derived class, resulting in an incorrect access chain. Fixes #7968
1 parent 948629a commit 4c613aa

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8861,6 +8861,11 @@ const Expr *SpirvEmitter::collectArrayStructIndices(
88618861
}
88628862
} else if (castExpr->getCastKind() == CK_UncheckedDerivedToBase ||
88638863
castExpr->getCastKind() == CK_HLSLDerivedToBase) {
8864+
// First the indices for the sub expression.
8865+
const Expr *base =
8866+
collectArrayStructIndices(castExpr->getSubExpr(), rawIndex,
8867+
rawIndices, indices, isMSOutAttribute);
8868+
88648869
llvm::SmallVector<uint32_t, 4> BaseIdx;
88658870
getBaseClassIndices(castExpr, &BaseIdx);
88668871
if (rawIndex) {
@@ -8871,8 +8876,7 @@ const Expr *SpirvEmitter::collectArrayStructIndices(
88718876
llvm::APInt(32, Idx)));
88728877
}
88738878

8874-
return collectArrayStructIndices(castExpr->getSubExpr(), rawIndex,
8875-
rawIndices, indices, isMSOutAttribute);
8879+
return base;
88768880
}
88778881
return collectArrayStructIndices(castExpr->getSubExpr(), rawIndex,
88788882
rawIndices, indices, isMSOutAttribute);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %dxc -T cs_6_0 -E main -spirv %s | FileCheck %s
2+
3+
struct X { int a1; };
4+
struct Y : X { int2 a2; };
5+
struct Z {
6+
X xs[2];
7+
Y y;
8+
};
9+
10+
cbuffer CBStructs : register(b0) {
11+
Z z;
12+
};
13+
RWStructuredBuffer<int> Out : register(u1);
14+
15+
[numthreads(1,1,1)]
16+
void main() {
17+
// CHECK: OpAccessChain
18+
// CHECK-SAME: %_ptr_Uniform_int
19+
// CHECK-SAME: %CBStructs
20+
// CHECK-SAME: %int_0
21+
// CHECK-SAME: %int_1
22+
// CHECK-SAME: %int_0
23+
// CHECK-SAME: %int_0
24+
Out[0] = z.y.a1;
25+
}

0 commit comments

Comments
 (0)