@@ -145,61 +145,73 @@ void StackFrameInfo::addAllocas(const Function *F)
145
145
auto &DL = F->getParent ()->getDataLayout ();
146
146
auto & C = F->getContext ();
147
147
148
- uint64_t CurOffset = 0 ;
149
- uint32_t CurAllocaIdx = 0 ;
150
-
151
148
SmallVector<Type*, 4 > Tys;
152
149
153
- for (auto &I : instructions (*F))
150
+ auto getAlignment = [=](const AllocaInst* AI) -> unsigned {
151
+ unsigned Alignment = (unsigned )AI->getAlignment ();
152
+ if (Alignment == 0 )
153
+ Alignment = (unsigned )DL.getABITypeAlignment (AI->getAllocatedType ());
154
+ return Alignment;
155
+ };
156
+
157
+ SmallVector<const AllocaInst*, 4 > Allocas;
158
+
159
+ for (auto & I : instructions (*F))
154
160
{
155
- if (auto * AI = dyn_cast<AllocaInst>(&I))
161
+ if (auto * AI = dyn_cast<AllocaInst>(&I))
156
162
{
157
163
if (!RTBuilder::isNonLocalAlloca (AI))
158
164
continue ;
159
165
160
- uint32_t Offset = TotalAllocaSize;
161
- uint32_t TypeSize =
162
- (uint32_t )DL.getTypeAllocSize (AI->getAllocatedType ());
163
-
164
- if (CurOffset != Offset)
165
- {
166
- IGC_ASSERT_MESSAGE ((CurOffset < Offset), " bad offset!" );
167
- // insert padding
168
- uint64_t Diff = Offset - CurOffset;
169
- auto * Padding = ArrayType::get (Type::getInt8Ty (C), Diff);
170
- Tys.push_back (Padding);
171
- CurAllocaIdx++;
172
- }
173
-
174
- Tys.push_back (AI->getAllocatedType ());
175
- AllocaIdxMap[AI] = CurAllocaIdx++;
176
- CurOffset = Offset + TypeSize;
166
+ Allocas.push_back (AI);
167
+ }
168
+ }
177
169
178
- recordAllocaEntry (AI, TypeSize);
170
+ llvm::stable_sort (Allocas,
171
+ [=](const AllocaInst* AI1, const AllocaInst* AI2) {
172
+ return getAlignment (AI1) > getAlignment (AI2);
173
+ });
179
174
180
- TotalAllocaSize += TypeSize;
181
- }
175
+ uint32_t CurAllocaIdx = 0 ;
176
+ for (auto * AI : Allocas)
177
+ {
178
+ Tys.push_back (AI->getAllocatedType ());
179
+ AllocaIdxMap[AI] = CurAllocaIdx++;
182
180
}
183
181
184
182
AllocaStructTy = Tys.empty () ?
185
183
StructType::get (C, true ) :
186
- StructType::create (C, Tys, " IGC::Allocas" , true );
184
+ StructType::create (C, Tys, " IGC::Allocas" , false );
185
+
186
+ TotalAllocaSize = int_cast<uint32_t >(DL.getTypeAllocSize (AllocaStructTy));
187
+ recordAllocaEntries (Allocas, AllocaStructTy);
187
188
}
188
189
189
- void StackFrameInfo::recordAllocaEntry (const AllocaInst* AI, uint32_t Size)
190
+ void StackFrameInfo::recordAllocaEntries (
191
+ ArrayRef<const AllocaInst*> Allocas,
192
+ StructType* StructTy)
190
193
{
191
194
if (skipRecording ())
192
195
return ;
193
196
194
- StackFrameEntry Entry;
195
- Entry.Name = " N/A" ;
196
- if (AI->hasName ())
197
- Entry.Name = AI->getName ().str ();
198
- Entry.Offset = TotalAllocaSize;
199
- Entry.TypeRepr = getTypeRepr (AI->getAllocatedType ());
200
- Entry.EntryType = ENTRY_ALLOCA;
201
- Entry.Size = Size;
202
- AllocaEntries.push_back (Entry);
197
+ auto *Layout = DL.getStructLayout (StructTy);
198
+
199
+ for (uint32_t i = 0 ; i < Allocas.size (); i++)
200
+ {
201
+ auto * AI = Allocas[i];
202
+
203
+ StackFrameEntry Entry;
204
+ Entry.Name = " N/A" ;
205
+ if (AI->hasName ())
206
+ Entry.Name = AI->getName ().str ();
207
+ Entry.Offset = int_cast<uint32_t >(Layout->getElementOffset (i));
208
+ Entry.TypeRepr = getTypeRepr (AI->getAllocatedType ());
209
+ Entry.EntryType = ENTRY_ALLOCA;
210
+ Entry.Size =
211
+ int_cast<uint32_t >(DL.getTypeAllocSize (AI->getAllocatedType ()));
212
+ AllocaEntries.push_back (Entry);
213
+ }
214
+
203
215
}
204
216
205
217
void StackFrameInfo::addFills (const Function* F)
0 commit comments