@@ -42,7 +42,7 @@ class DXILFlattenArraysLegacy : public ModulePass {
42
42
43
43
struct GEPData {
44
44
ArrayType *ParentArrayType;
45
- Value *ParendOperand ;
45
+ Value *ParentOperand ;
46
46
SmallVector<Value *> Indices;
47
47
SmallVector<uint64_t > Dims;
48
48
bool AllIndicesAreConstInt;
@@ -211,7 +211,7 @@ bool DXILFlattenArraysVisitor::visitAllocaInst(AllocaInst &AI) {
211
211
212
212
ArrayType *FattenedArrayType = ArrayType::get (BaseType, TotalElements);
213
213
AllocaInst *FlatAlloca =
214
- Builder.CreateAlloca (FattenedArrayType, nullptr , AI.getName () + " .flat " );
214
+ Builder.CreateAlloca (FattenedArrayType, nullptr , AI.getName () + " .1dim " );
215
215
FlatAlloca->setAlignment (AI.getAlign ());
216
216
AI.replaceAllUsesWith (FlatAlloca);
217
217
AI.eraseFromParent ();
@@ -222,6 +222,10 @@ void DXILFlattenArraysVisitor::recursivelyCollectGEPs(
222
222
GetElementPtrInst &CurrGEP, ArrayType *FlattenedArrayType,
223
223
Value *PtrOperand, unsigned &GEPChainUseCount, SmallVector<Value *> Indices,
224
224
SmallVector<uint64_t > Dims, bool AllIndicesAreConstInt) {
225
+ // Check if this GEP is already in the map to avoid circular references
226
+ if (GEPChainMap.count (&CurrGEP) > 0 )
227
+ return ;
228
+
225
229
Value *LastIndex = CurrGEP.getOperand (CurrGEP.getNumOperands () - 1 );
226
230
AllIndicesAreConstInt &= isa<ConstantInt>(LastIndex);
227
231
Indices.push_back (LastIndex);
@@ -271,10 +275,18 @@ bool DXILFlattenArraysVisitor::visitGetElementPtrInstInGEPChainBase(
271
275
genInstructionFlattenIndices (GEPInfo.Indices , GEPInfo.Dims , Builder);
272
276
273
277
ArrayType *FlattenedArrayType = GEPInfo.ParentArrayType ;
278
+
279
+ // Don't append '.flat' to an empty string. If the SSA name isn't available
280
+ // it could conflict with the ParentOperand's name.
281
+ std::string FlatName = GEP.hasName () ? GEP.getName ().str () + " .flat" : " " ;
282
+
274
283
Value *FlatGEP =
275
- Builder.CreateGEP (FlattenedArrayType, GEPInfo.ParendOperand ,
284
+ Builder.CreateGEP (FlattenedArrayType, GEPInfo.ParentOperand ,
276
285
{Builder.getInt32 (0 ), FlatIndex},
277
- GEP.getName () + " .flat" , GEP.getNoWrapFlags ());
286
+ FlatName, GEP.getNoWrapFlags ());
287
+
288
+ // Store the new GEP in the map before replacing uses to avoid circular references
289
+ GEPChainMap.erase (&GEP);
278
290
279
291
GEP.replaceAllUsesWith (FlatGEP);
280
292
GEP.eraseFromParent ();
0 commit comments