Skip to content

Commit 93e075d

Browse files
committed
fixup: Address Helena's feedback (2025-11-13)
1 parent 4d2558f commit 93e075d

File tree

5 files changed

+27
-29
lines changed

5 files changed

+27
-29
lines changed

clang/lib/CodeGen/CGHLSLRuntime.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,9 @@ CGHLSLRuntime::convertHLSLSpecificType(const Type *T,
267267
assert(T->isHLSLSpecificType() && "Not an HLSL specific type!");
268268

269269
// Check if the target has a specific translation for this type first.
270-
if (llvm::Type *LayoutTy =
270+
if (llvm::Type *TargetTy =
271271
CGM.getTargetCodeGenInfo().getHLSLType(CGM, T, OffsetInfo))
272-
return LayoutTy;
272+
return TargetTy;
273273

274274
llvm_unreachable("Generic handling of HLSL types is not supported.");
275275
}
@@ -326,9 +326,10 @@ void CGHLSLRuntime::emitBufferGlobalsAndMetadata(
326326
DeclsWithOffset.emplace_back(VD, OffsetInfo[OffsetIdx++]);
327327
}
328328

329-
llvm::stable_sort(DeclsWithOffset, [](const auto &LHS, const auto &RHS) {
330-
return CGHLSLOffsetInfo::compareOffsets(LHS.second, RHS.second);
331-
});
329+
if (!OffsetInfo.empty())
330+
llvm::stable_sort(DeclsWithOffset, [](const auto &LHS, const auto &RHS) {
331+
return CGHLSLOffsetInfo::compareOffsets(LHS.second, RHS.second);
332+
});
332333

333334
// Associate the buffer global variable with its constants
334335
SmallVector<llvm::Metadata *> BufGlobals;
@@ -1148,7 +1149,7 @@ std::optional<LValue> CGHLSLRuntime::emitBufferArraySubscriptExpr(
11481149
// be past the end of the in-memory object.
11491150
SmallVector<llvm::Value *, 2> Indices;
11501151
Indices.push_back(Idx);
1151-
Indices.push_back(llvm::ConstantInt::get(Indices[0]->getType(), 0));
1152+
Indices.push_back(llvm::ConstantInt::get(Idx->getType(), 0));
11521153

11531154
llvm::Value *GEP = CGF.Builder.CreateGEP(LayoutTy, Addr.emitRawPointer(CGF),
11541155
Indices, "cbufferidx");
@@ -1319,8 +1320,9 @@ LValue CGHLSLRuntime::emitBufferMemberExpr(CodeGenFunction &CGF,
13191320
// Work out the buffer layout type to index into.
13201321
QualType RecType = CGM.getContext().getCanonicalTagType(Rec);
13211322
assert(RecType->isStructureOrClassType() && "Invalid type in HLSL buffer");
1322-
// Since this is a member of the buffer and not the buffer itself, we
1323-
// shouldn't have any offsets we need to contend with.
1323+
// Since this is a member of an object in the buffer and not the buffer's
1324+
// struct/class itself, we shouldn't have any offsets on the members we need
1325+
// to contend with.
13241326
CGHLSLOffsetInfo EmptyOffsets;
13251327
llvm::StructType *LayoutTy = HLSLBufferLayoutBuilder(CGM).layOutStruct(
13261328
RecType->getAsCanonical<RecordType>(), EmptyOffsets);

clang/lib/CodeGen/HLSLBufferLayoutBuilder.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ HLSLBufferLayoutBuilder::layOutStruct(const RecordType *RT,
5252
for (const auto *FD : RD->fields())
5353
FieldsWithOffset.emplace_back(FD, OffsetInfo[OffsetIdx++]);
5454

55-
llvm::stable_sort(FieldsWithOffset, [](const auto &LHS, const auto &RHS) {
56-
return CGHLSLOffsetInfo::compareOffsets(LHS.second, RHS.second);
57-
});
55+
if (!OffsetInfo.empty())
56+
llvm::stable_sort(FieldsWithOffset, [](const auto &LHS, const auto &RHS) {
57+
return CGHLSLOffsetInfo::compareOffsets(LHS.second, RHS.second);
58+
});
5859

5960
SmallVector<llvm::Type *> Layout;
6061
CharUnits CurrentOffset = CharUnits::Zero();
@@ -72,8 +73,6 @@ HLSLBufferLayoutBuilder::layOutStruct(const RecordType *RT,
7273

7374
CharUnits NextOffset = CurrentOffset.alignTo(Align);
7475

75-
// TODO: Do we need to diagnose invalid packoffsets here, or will they have
76-
// already been taken care of?
7776
if (Offset != CGHLSLOffsetInfo::Unspecified) {
7877
CharUnits PackOffset = CharUnits::fromQuantity(Offset);
7978
assert(PackOffset >= NextOffset &&

clang/lib/CodeGen/HLSLBufferLayoutBuilder.h

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,17 @@ class HLSLBufferLayoutBuilder {
2828
public:
2929
HLSLBufferLayoutBuilder(CodeGenModule &CGM) : CGM(CGM) {}
3030

31-
/// Lays out a struct type following HLSL buffer rules and considering
32-
/// PackOffsets, if provided. Previously created layout structs are cached by
33-
/// CGHLSLRuntime.
31+
/// Lays out a struct type following HLSL buffer rules and considering any
32+
/// explicit offset information. Previously created layout structs are cached
33+
/// by CGHLSLRuntime.
3434
///
3535
/// The function iterates over all fields of the record type (including base
36-
/// classes) and calls layoutField to converts each field to its corresponding
37-
/// LLVM type and to calculate its HLSL constant buffer layout. Any embedded
38-
/// structs (or arrays of structs) are converted to layout types as well.
36+
/// classes) and works out a padded llvm type to represent the buffer layout.
3937
///
40-
/// When PackOffsets are specified the elements will be placed based on the
41-
/// user-specified offsets. Not all elements must have a
42-
/// packoffset/register(c#) annotation though. For those that don't, the
43-
/// PackOffsets array will contain -1 value instead. These elements must be
44-
/// placed at the end of the layout after all of the elements with specific
45-
/// offset.
38+
/// If a non-empty OffsetInfo is provided (ie, from `packoffset` annotations
39+
/// in the source), any provided offsets offsets will be respected. If the
40+
/// OffsetInfo is available but has empty entries, those will be layed out at
41+
/// the end of the structure.
4642
llvm::StructType *layOutStruct(const RecordType *StructType,
4743
const CGHLSLOffsetInfo &OffsetInfo);
4844

clang/test/CodeGenHLSL/GlobalConstructorFunction.hlsl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ void main(unsigned GI : SV_GroupIndex) {}
3737
// INLINE-NEXT: alloca
3838
// INLINE-NEXT: store i32 12
3939
// INLINE-NEXT: store i32 13
40-
// INLINE-NEXT: %[[HANDLE:.*]] = call target("dx.CBuffer", %"__cblayout_$Globals")
41-
// INLINE-NEXT-SAME: @"llvm.dx.resource.handlefromimplicitbinding.tdx.CBuffer_s___cblayout_$Globalsst"(i32 0, i32 0, i32 1, i32 0, i1 false, ptr @"$Globals.str")
40+
// INLINE-NEXT: %[[HANDLE:.*]] = call target("dx.CBuffer", %"__cblayout_$Globals") @"llvm.dx.resource.handlefromimplicitbinding.tdx.CBuffer_s___cblayout_$Globalsst"(i32 0, i32 0, i32 1, i32 0, ptr @"$Globals.str")
4241
// INLINE-NEXT: store target("dx.CBuffer", %"__cblayout_$Globals") %[[HANDLE]], ptr @"$Globals.cb", align 4
4342
// INLINE-NEXT: %0 = call i32 @llvm.dx.flattened.thread.id.in.group()
4443
// INLINE-NEXT: store i32 %

clang/test/CodeGenHLSL/resources/cbuffer.hlsl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@
3333
// CHECK: %__cblayout_CBStructs = type <{
3434
// CHECK-SAME: %A, target("dx.Padding", 8),
3535

36-
// TODO: We should have target("dx.Padding", 2) padding after %B, but we don't correctly handle
37-
// 2- and 3-element vectors inside structs yet because of DataLayout rules.
36+
// TODO: We should have target("dx.Padding", 2) padding after %B, but we don't
37+
// correctly handle 2- and 3-element vectors inside structs yet because of
38+
// DataLayout rules. See https://github.com/llvm/llvm-project/issues/123968.
39+
//
3840
// CHECK-SAME: %B,
3941

4042
// CHECK-SAME: %C, target("dx.Padding", 8),

0 commit comments

Comments
 (0)