-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[NFC][hlsl][Sema] Simplify CBuffer Legacy Size Calculation Control Flow #127921
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
Conversation
@llvm/pr-subscribers-hlsl @llvm/pr-subscribers-clang Author: Ashley Coleman (V-FEXrt) ChangesNFC: Small refactor to Full diff: https://github.com/llvm/llvm-project/pull/127921.diff 1 Files Affected:
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<RecordType>()) {
+ 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<VectorType>()) {
+ 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<VectorType>()) {
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:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great simplification!
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/195/builds/5171 Here is the relevant piece of the build log for the reference
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/35/builds/7475 Here is the relevant piece of the build log for the reference
|
NFC: Small refactor to
calculateLegacyCbufferSize()
's control flow to make each branch easier to flow/more visually distinct from each other