@@ -53,22 +53,22 @@ namespace clang {
5353namespace CodeGen {
5454
5555// Creates a layout type for given struct with HLSL constant buffer layout
56- // taking into account Packoffsets , if provided.
56+ // taking into account PackOffsets , if provided.
5757// Previously created layout types are cached by CGHLSLRuntime.
5858//
5959// The function iterates over all fields of the StructType (including base
6060// classes) and calls layoutField to converts each field to its corresponding
6161// LLVM type and to calculate its HLSL constant buffer layout. Any embedded
6262// structs (or arrays of structs) are converted to target layout types as well.
6363//
64- // When Packoffsets are specified the elements will be placed based on the
64+ // When PackOffsets are specified the elements will be placed based on the
6565// user-specified offsets. Not all elements must have a packoffset/register(c#)
66- // annotation though. For those that don't, the Packoffsets array will constain
66+ // annotation though. For those that don't, the PackOffsets array will contain
6767// -1 value instead. These elements must be placed at the end of the layout
6868// after all of the elements with specific offset.
6969llvm::TargetExtType *HLSLBufferLayoutBuilder::createLayoutType (
7070 const RecordType *StructType,
71- const llvm::SmallVector<int32_t > *Packoffsets ) {
71+ const llvm::SmallVector<int32_t > *PackOffsets ) {
7272
7373 // check if we already have the layout type for this struct
7474 if (llvm::TargetExtType *Ty =
@@ -94,63 +94,53 @@ llvm::TargetExtType *HLSLBufferLayoutBuilder::createLayoutType(
9494 " HLSL doesn't support multiple inheritance" );
9595 RecordTypes.push_back (D->bases_begin ()->getType ()->getAs <RecordType>());
9696 }
97+
98+ unsigned FieldOffset;
99+ llvm::Type *FieldType;
100+
97101 while (!RecordTypes.empty ()) {
98102 const RecordType *RT = RecordTypes.back ();
99103 RecordTypes.pop_back ();
100104
101105 for (const auto *FD : RT->getDecl ()->fields ()) {
102- unsigned FieldOffset = UINT_MAX;
103- llvm::Type *FieldType = nullptr ;
104-
105- if (Packoffsets) {
106- // have packoffset/register(c#) annotations
107- assert (Index < Packoffsets->size () &&
108- " number of elements in layout struct does not match number of "
109- " packoffset annotations" );
110- int PO = (*Packoffsets)[Index++];
111- if (PO != -1 ) {
112- if (!layoutField (FD, EndOffset, FieldOffset, FieldType, PO))
113- return nullptr ;
114- } else {
115- // No packoffset/register(cX) annotation on this field;
116- // Delay the layout until after all of the other elements
117- // annotated with packoffsets/register(cX) are processed.
118- DelayLayoutFields.emplace_back (FD, LayoutElements.size ());
119- // reserve space for this field in the layout vector and elements list
120- Layout.push_back (UINT_MAX);
121- LayoutElements.push_back (nullptr );
122- continue ;
123- }
124- } else {
125- if (!layoutField (FD, EndOffset, FieldOffset, FieldType))
106+ assert ((!PackOffsets || Index < PackOffsets->size ()) &&
107+ " number of elements in layout struct does not match number of "
108+ " packoffset annotations" );
109+
110+ // No PackOffset info at all, or have a valid packoffset/register(c#)
111+ // annotations value -> layout the field.
112+ int PO = PackOffsets ? (*PackOffsets)[Index++] : -1 ;
113+ if (!PackOffsets || PO != -1 ) {
114+ if (!layoutField (FD, EndOffset, FieldOffset, FieldType, PO))
126115 return nullptr ;
116+ Layout.push_back ((unsigned )FieldOffset);
117+ LayoutElements.push_back (FieldType);
118+ continue ;
127119 }
128-
129- assert (FieldOffset != UINT_MAX && FieldType != nullptr );
130- Layout.push_back ((unsigned )FieldOffset);
131- LayoutElements.push_back (FieldType);
120+ // Have PackOffset info, but there is no packoffset/register(cX)
121+ // annotation on this field. Delay the layout until after all of the
122+ // other elements with packoffsets/register(cX) are processed.
123+ DelayLayoutFields.emplace_back (FD, LayoutElements.size ());
124+ // reserve space for this field in the layout vector and elements list
125+ Layout.push_back (UINT_MAX);
126+ LayoutElements.push_back (nullptr );
132127 }
133128 }
134129
135130 // process delayed layouts
136- if (!DelayLayoutFields.empty ()) {
137- for (auto I : DelayLayoutFields) {
138- const FieldDecl *FD = I.first ;
139- unsigned IndexInLayoutElements = I.second ;
140- // the first item in layout vector is size, so we need to offset the index
141- // by 1
142- unsigned IndexInLayout = IndexInLayoutElements + 1 ;
143- assert (Layout[IndexInLayout] == UINT_MAX &&
144- LayoutElements[IndexInLayoutElements] == nullptr );
145-
146- unsigned FieldOffset = UINT_MAX;
147- llvm::Type *FieldType = nullptr ;
148- if (!layoutField (FD, EndOffset, FieldOffset, FieldType))
149- return nullptr ;
150-
151- Layout[IndexInLayout] = (unsigned )FieldOffset;
152- LayoutElements[IndexInLayoutElements] = FieldType;
153- }
131+ for (auto I : DelayLayoutFields) {
132+ const FieldDecl *FD = I.first ;
133+ unsigned IndexInLayoutElements = I.second ;
134+ // the first item in layout vector is size, so we need to offset the index
135+ // by 1
136+ unsigned IndexInLayout = IndexInLayoutElements + 1 ;
137+ assert (Layout[IndexInLayout] == UINT_MAX &&
138+ LayoutElements[IndexInLayoutElements] == nullptr );
139+
140+ if (!layoutField (FD, EndOffset, FieldOffset, FieldType))
141+ return nullptr ;
142+ Layout[IndexInLayout] = (unsigned )FieldOffset;
143+ LayoutElements[IndexInLayoutElements] = FieldType;
154144 }
155145
156146 // set the size of the buffer
0 commit comments