diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp index 957c3a0888438..90b5278c76340 100644 --- a/clang/lib/Sema/SemaHLSL.cpp +++ b/clang/lib/Sema/SemaHLSL.cpp @@ -176,9 +176,9 @@ Decl *SemaHLSL::ActOnStartBuffer(Scope *BufferScope, bool CBuffer, // https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-packing-rules static unsigned calculateLegacyCbufferSize(const ASTContext &Context, QualType T) { - unsigned Size = 0; constexpr unsigned CBufferAlign = 16; if (const RecordType *RT = T->getAs()) { + unsigned Size = 0; const RecordDecl *RD = RT->getDecl(); for (const FieldDecl *Field : RD->fields()) { QualType Ty = Field->getType(); @@ -191,22 +191,28 @@ static unsigned calculateLegacyCbufferSize(const ASTContext &Context, Size = llvm::alignTo(Size, FieldAlign); Size += FieldSize; } - } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) { - if (unsigned ElementCount = AT->getSize().getZExtValue()) { - unsigned ElementSize = - calculateLegacyCbufferSize(Context, AT->getElementType()); - unsigned AlignedElementSize = llvm::alignTo(ElementSize, CBufferAlign); - Size = AlignedElementSize * (ElementCount - 1) + ElementSize; - } - } else if (const VectorType *VT = T->getAs()) { + return Size; + } + + if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) { + unsigned ElementCount = AT->getSize().getZExtValue(); + if (ElementCount == 0) + return 0; + + unsigned ElementSize = + calculateLegacyCbufferSize(Context, AT->getElementType()); + unsigned AlignedElementSize = llvm::alignTo(ElementSize, CBufferAlign); + return AlignedElementSize * (ElementCount - 1) + ElementSize; + } + + if (const VectorType *VT = T->getAs()) { unsigned ElementCount = VT->getNumElements(); unsigned ElementSize = calculateLegacyCbufferSize(Context, VT->getElementType()); - Size = ElementSize * ElementCount; - } else { - Size = Context.getTypeSize(T) / 8; + return ElementSize * ElementCount; } - return Size; + + return Context.getTypeSize(T) / 8; } // Validate packoffset: