Skip to content

Commit 5bf3748

Browse files
authored
[NFC][hlsl][Sema] Simplify CBuffer Legacy Size Calculation Control Flow (#127921)
NFC: Small refactor to `calculateLegacyCbufferSize()`'s control flow to make each branch easier to flow/more visually distinct from each other
1 parent f34f21a commit 5bf3748

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ Decl *SemaHLSL::ActOnStartBuffer(Scope *BufferScope, bool CBuffer,
176176
// https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-packing-rules
177177
static unsigned calculateLegacyCbufferSize(const ASTContext &Context,
178178
QualType T) {
179-
unsigned Size = 0;
180179
constexpr unsigned CBufferAlign = 16;
181180
if (const RecordType *RT = T->getAs<RecordType>()) {
181+
unsigned Size = 0;
182182
const RecordDecl *RD = RT->getDecl();
183183
for (const FieldDecl *Field : RD->fields()) {
184184
QualType Ty = Field->getType();
@@ -191,22 +191,28 @@ static unsigned calculateLegacyCbufferSize(const ASTContext &Context,
191191
Size = llvm::alignTo(Size, FieldAlign);
192192
Size += FieldSize;
193193
}
194-
} else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
195-
if (unsigned ElementCount = AT->getSize().getZExtValue()) {
196-
unsigned ElementSize =
197-
calculateLegacyCbufferSize(Context, AT->getElementType());
198-
unsigned AlignedElementSize = llvm::alignTo(ElementSize, CBufferAlign);
199-
Size = AlignedElementSize * (ElementCount - 1) + ElementSize;
200-
}
201-
} else if (const VectorType *VT = T->getAs<VectorType>()) {
194+
return Size;
195+
}
196+
197+
if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
198+
unsigned ElementCount = AT->getSize().getZExtValue();
199+
if (ElementCount == 0)
200+
return 0;
201+
202+
unsigned ElementSize =
203+
calculateLegacyCbufferSize(Context, AT->getElementType());
204+
unsigned AlignedElementSize = llvm::alignTo(ElementSize, CBufferAlign);
205+
return AlignedElementSize * (ElementCount - 1) + ElementSize;
206+
}
207+
208+
if (const VectorType *VT = T->getAs<VectorType>()) {
202209
unsigned ElementCount = VT->getNumElements();
203210
unsigned ElementSize =
204211
calculateLegacyCbufferSize(Context, VT->getElementType());
205-
Size = ElementSize * ElementCount;
206-
} else {
207-
Size = Context.getTypeSize(T) / 8;
212+
return ElementSize * ElementCount;
208213
}
209-
return Size;
214+
215+
return Context.getTypeSize(T) / 8;
210216
}
211217

212218
// Validate packoffset:

0 commit comments

Comments
 (0)