Skip to content

Commit f19f59d

Browse files
scottp101igcbot
authored andcommitted
Fix alloca alignment
Fix alloca alignment
1 parent e685340 commit f19f59d

File tree

2 files changed

+51
-37
lines changed

2 files changed

+51
-37
lines changed

IGC/AdaptorCommon/RayTracing/StackFrameInfo.cpp

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -145,61 +145,73 @@ void StackFrameInfo::addAllocas(const Function *F)
145145
auto &DL = F->getParent()->getDataLayout();
146146
auto& C = F->getContext();
147147

148-
uint64_t CurOffset = 0;
149-
uint32_t CurAllocaIdx = 0;
150-
151148
SmallVector<Type*, 4> Tys;
152149

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))
154160
{
155-
if (auto *AI = dyn_cast<AllocaInst>(&I))
161+
if (auto* AI = dyn_cast<AllocaInst>(&I))
156162
{
157163
if (!RTBuilder::isNonLocalAlloca(AI))
158164
continue;
159165

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+
}
177169

178-
recordAllocaEntry(AI, TypeSize);
170+
llvm::stable_sort(Allocas,
171+
[=](const AllocaInst* AI1, const AllocaInst* AI2) {
172+
return getAlignment(AI1) > getAlignment(AI2);
173+
});
179174

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++;
182180
}
183181

184182
AllocaStructTy = Tys.empty() ?
185183
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);
187188
}
188189

189-
void StackFrameInfo::recordAllocaEntry(const AllocaInst* AI, uint32_t Size)
190+
void StackFrameInfo::recordAllocaEntries(
191+
ArrayRef<const AllocaInst*> Allocas,
192+
StructType* StructTy)
190193
{
191194
if (skipRecording())
192195
return;
193196

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+
203215
}
204216

205217
void StackFrameInfo::addFills(const Function* F)

IGC/AdaptorCommon/RayTracing/StackFrameInfo.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ class StackFrameInfo : public RTArgs
6868
void addSpillsCompacted(const llvm::Function *F);
6969
void addSpillsUncompacted(const llvm::Function *F);
7070
// Just used for shader dumping
71-
void recordAllocaEntry(const llvm::AllocaInst* AI, uint32_t Size);
71+
void recordAllocaEntries(
72+
llvm::ArrayRef<const llvm::AllocaInst*> Allocas,
73+
llvm::StructType* StructTy);
7274
void recordSpillUnionEntry(
7375
const llvm::SpillValueIntrinsic* SV,
7476
std::vector<StackFrameEntry> &Entries,

0 commit comments

Comments
 (0)