Skip to content

Commit 56beb45

Browse files
committed
remove stride
1 parent cf08adb commit 56beb45

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

clang/lib/CodeGen/CGHLSLRuntime.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,11 @@ CGHLSLRuntime::getOrCalculateStructSizeForBuffer(llvm::StructType *StructTy) {
130130
size_t StructSize = 0;
131131
LayoutItems.push_back(nullptr); // reserve one slot for the buffer size
132132

133-
for (llvm::Type *ElTy : StructTy->elements())
134-
addLayoutInfoForBufferElement(StructSize, LayoutItems, ElTy);
133+
for (llvm::Type *ElTy : StructTy->elements()) {
134+
size_t Offset = calculateBufferElementOffset(ElTy, &StructSize);
135+
// create metadata constant with the element start offset
136+
LayoutItems.push_back(getConstIntMetadata(CGM.getLLVMContext(), Offset));
137+
}
135138

136139
// set the size of the buffer
137140
LayoutItems[1] = getConstIntMetadata(Ctx, StructSize);
@@ -147,16 +150,16 @@ CGHLSLRuntime::getOrCalculateStructSizeForBuffer(llvm::StructType *StructTy) {
147150
return StructSize;
148151
}
149152

150-
void CGHLSLRuntime::addLayoutInfoForBufferElement(
151-
size_t &EndOffset, SmallVector<llvm::Metadata *> &LayoutItems,
152-
llvm::Type *LayoutTy, HLSLPackOffsetAttr *PackoffsetAttr) {
153+
size_t CGHLSLRuntime::calculateBufferElementOffset(
154+
llvm::Type *LayoutTy, size_t *LayoutEndOffset,
155+
HLSLPackOffsetAttr *PackoffsetAttr) {
153156

154-
// calculate element offset and size; for arrays also calculate array
155-
// element count and stride
157+
// calculate element offset and size
156158
size_t ElemOffset = 0;
157159
size_t ElemSize = 0;
158160
size_t ArrayCount = 1;
159161
size_t ArrayStride = 0;
162+
size_t EndOffset = *LayoutEndOffset;
160163
size_t NextRowOffset = llvm::alignTo(EndOffset, 16U);
161164

162165
if (LayoutTy->isArrayTy()) {
@@ -205,13 +208,9 @@ void CGHLSLRuntime::addLayoutInfoForBufferElement(
205208
// with packoffset annotations)
206209
unsigned NewEndOffset =
207210
ElemOffset + (ArrayCount - 1) * ArrayStride + ElemSize;
208-
EndOffset = std::max<size_t>(EndOffset, NewEndOffset);
211+
*LayoutEndOffset = std::max<size_t>(EndOffset, NewEndOffset);
209212

210-
// create metadata constan with the offset and stride and add to list
211-
LayoutItems.push_back(getConstIntMetadata(CGM.getLLVMContext(), ElemOffset));
212-
if (ArrayStride)
213-
LayoutItems.push_back(
214-
getConstIntMetadata(CGM.getLLVMContext(), ArrayStride));
213+
return ElemOffset;
215214
}
216215

217216
void CGHLSLRuntime::emitBufferGlobalsAndMetadata(const HLSLBufferDecl *BufDecl,
@@ -284,9 +283,12 @@ void CGHLSLRuntime::emitBufferGlobalsAndMetadata(const HLSLBufferDecl *BufDecl,
284283
!UsePackoffset) &&
285284
"expected packoffset attribute on every declaration");
286285

287-
addLayoutInfoForBufferElement(
288-
BufferSize, LayoutItems, LayoutType,
286+
size_t Offset = calculateBufferElementOffset(
287+
LayoutType, &BufferSize,
289288
UsePackoffset ? VD->getAttr<HLSLPackOffsetAttr>() : nullptr);
289+
290+
// create metadata constant with the element start offset
291+
LayoutItems.push_back(getConstIntMetadata(CGM.getLLVMContext(), Offset));
290292
}
291293
}
292294
assert(ElemIt == LayoutStruct->element_end() &&

clang/lib/CodeGen/CGHLSLRuntime.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,10 @@ class CGHLSLRuntime {
166166
BufferResBinding &Binding);
167167
void emitBufferGlobalsAndMetadata(const HLSLBufferDecl *BufDecl,
168168
llvm::GlobalVariable *BufGV);
169-
void addLayoutInfoForBufferElement(
170-
size_t &EndOffset, SmallVector<llvm::Metadata *> &LayoutItems,
171-
llvm::Type *LayoutTy, HLSLPackOffsetAttr *PackoffsetAttr = nullptr);
169+
170+
size_t
171+
calculateBufferElementOffset(llvm::Type *LayoutTy, size_t *LayoutEndOffset,
172+
HLSLPackOffsetAttr *PackoffsetAttr = nullptr);
172173

173174
size_t getOrCalculateStructSizeForBuffer(llvm::StructType *StructTy);
174175

clang/test/CodeGenHLSL/cbuffer.hlsl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,14 @@ void main() {
186186
// CHECK: ![[CBSCALARS_LAYOUT]] = !{!"struct.__cblayout_CBScalars", i32 56, i32 0, i32 8, i32 16, i32 24, i32 32, i32 36, i32 40, i32 48}
187187
// CHECK: ![[CBVECTORS_LAYOUT]] = !{!"struct.__cblayout_CBVectors", i32 136, i32 0, i32 16, i32 40, i32 48, i32 80, i32 96, i32 112}
188188

189-
// CHECK: ![[CBARRAYS_LAYOUT]] = !{!"struct.__cblayout_CBArrays", i32 708, i32 0, i32 16, i32 48, i32 32, i32 112, i32 16, i32 176, i32 16,
190-
// CHECK-SAME: i32 224, i32 16, i32 608, i32 16, i32 624, i32 16, i32 656, i32 16}
189+
// CHECK: ![[CBARRAYS_LAYOUT]] = !{!"struct.__cblayout_CBArrays", i32 708, i32 0, i32 48, i32 112, i32 176, i32 224, i32 608, i32 624, i32 656}
191190

192191
// CHECK: ![[A_LAYOUT]] = !{!"struct.A", i32 8, i32 0}
193192
// CHECK: ![[B_LAYOUT]] = !{!"struct.B", i32 14, i32 0, i32 8}
194193
// CHECK: ![[C_LAYOUT]] = !{!"struct.C", i32 24, i32 0, i32 16}
195-
// CHECK: ![[D_LAYOUT]] = !{!"struct.__cblayout_D", i32 94, i32 0, i32 16}
196-
// CHECK: ![[CBSTRUCTS_LAYOUT]] = !{!"struct.__cblayout_CBStructs", i32 262, i32 0, i32 16, i32 32, i32 64, i32 16, i32 144, i32 238, i32 240, i32 256}
194+
// CHECK: ![[D_LAYOUT]] = !{!"struct.__cblayout_D", i32 94, i32 0}
195+
// CHECK: ![[CBSTRUCTS_LAYOUT]] = !{!"struct.__cblayout_CBStructs", i32 262, i32 0, i32 16, i32 32, i32 64, i32 144, i32 238, i32 240, i32 256}
197196

198197
// CHECK: ![[TEST_LAYOUT]] = !{!"struct.Test", i32 8, i32 0, i32 4}
199198
// CHECK: ![[ANON_LAYOUT]] = !{!"struct.anon", i32 4, i32 0}
200-
// CHECK: ![[CBMIX_LAYOUT]] = !{!"struct.__cblayout_CBMix", i32 162, i32 0, i32 16, i32 24, i32 32, i32 16, i32 120, i32 128, i32 136, i32 144, i32 152, i32 160}
199+
// CHECK: ![[CBMIX_LAYOUT]] = !{!"struct.__cblayout_CBMix", i32 162, i32 0, i32 24, i32 32, i32 120, i32 128, i32 136, i32 144, i32 152, i32 160}

0 commit comments

Comments
 (0)