Skip to content

[HLSL] Desugar ConstantArrayType when calculating cbuffer field layout #134683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion clang/lib/CodeGen/HLSLBufferLayoutBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ bool HLSLBufferLayoutBuilder::layoutField(const FieldDecl *FD,
// Unwrap array to find the element type and get combined array size.
QualType Ty = FieldTy;
while (Ty->isConstantArrayType()) {
const ConstantArrayType *ArrayTy = cast<ConstantArrayType>(Ty);
const ConstantArrayType *ArrayTy =
cast<ConstantArrayType>(Ty->getUnqualifiedDesugaredType());
ArrayCount *= ArrayTy->getSExtSize();
Ty = ArrayTy->getElementType();
}
Expand Down
17 changes: 16 additions & 1 deletion clang/test/CodeGenHLSL/cbuffer.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ cbuffer CBArrays : register(b2) {
// CHECK: @c7 = external addrspace(2) global [2 x i64], align 8
// CHECK: @c8 = external addrspace(2) global [4 x i32], align 4

typedef uint32_t4 uint32_t8[2];
typedef uint4 T1;
typedef T1 T2[2]; // check a double typedef

cbuffer CBTypedefArray {
uint32_t8 t1[2];
T2 t2[2];
}

// CHECK: @CBTypedefArray.cb = global target("dx.CBuffer", target("dx.Layout", %__cblayout_CBTypedefArray,
// CHECK-SAME: 128, 0, 64))
// CHECK: @t1 = external addrspace(2) global [2 x [2 x <4 x i32>]], align 16
// CHECK: @t2 = external addrspace(2) global [2 x [2 x <4 x i32>]], align 16
struct Empty {};

struct A {
Expand Down Expand Up @@ -278,7 +291,7 @@ void main() {
// CHECK-NEXT: call void @_init_resource_CBScalars.cb()
// CHECK-NEXT: call void @_init_resource_CBArrays.cb()

// CHECK: !hlsl.cbs = !{![[CBSCALARS:[0-9]+]], ![[CBVECTORS:[0-9]+]], ![[CBARRAYS:[0-9]+]], ![[CBSTRUCTS:[0-9]+]], ![[CBCLASSES:[0-9]+]],
// CHECK: !hlsl.cbs = !{![[CBSCALARS:[0-9]+]], ![[CBVECTORS:[0-9]+]], ![[CBARRAYS:[0-9]+]], ![[CBTYPEDEFARRAY:[0-9]+]], ![[CBSTRUCTS:[0-9]+]], ![[CBCLASSES:[0-9]+]],
// CHECK-SAME: ![[CBMIX:[0-9]+]], ![[CB_A:[0-9]+]], ![[CB_B:[0-9]+]], ![[CB_C:[0-9]+]]}

// CHECK: ![[CBSCALARS]] = !{ptr @CBScalars.cb, ptr addrspace(2) @a1, ptr addrspace(2) @a2, ptr addrspace(2) @a3, ptr addrspace(2) @a4,
Expand All @@ -290,6 +303,8 @@ void main() {
// CHECK: ![[CBARRAYS]] = !{ptr @CBArrays.cb, ptr addrspace(2) @c1, ptr addrspace(2) @c2, ptr addrspace(2) @c3, ptr addrspace(2) @c4,
// CHECK-SAME: ptr addrspace(2) @c5, ptr addrspace(2) @c6, ptr addrspace(2) @c7, ptr addrspace(2) @c8}

// CHECK: ![[CBTYPEDEFARRAY]] = !{ptr @CBTypedefArray.cb, ptr addrspace(2) @t1, ptr addrspace(2) @t2}

// CHECK: ![[CBSTRUCTS]] = !{ptr @CBStructs.cb, ptr addrspace(2) @a, ptr addrspace(2) @b, ptr addrspace(2) @c, ptr addrspace(2) @array_of_A,
// CHECK-SAME: ptr addrspace(2) @d, ptr addrspace(2) @e, ptr addrspace(2) @f}

Expand Down