@@ -42,7 +42,7 @@ class DXILFlattenArraysLegacy : public ModulePass {
4242
4343struct GEPData {
4444 ArrayType *ParentArrayType;
45- Value *ParendOperand ;
45+ Value *ParentOperand ;
4646 SmallVector<Value *> Indices;
4747 SmallVector<uint64_t > Dims;
4848 bool AllIndicesAreConstInt;
@@ -211,7 +211,7 @@ bool DXILFlattenArraysVisitor::visitAllocaInst(AllocaInst &AI) {
211211
212212 ArrayType *FattenedArrayType = ArrayType::get (BaseType, TotalElements);
213213 AllocaInst *FlatAlloca =
214- Builder.CreateAlloca (FattenedArrayType, nullptr , AI.getName () + " .flat " );
214+ Builder.CreateAlloca (FattenedArrayType, nullptr , AI.getName () + " .1dim " );
215215 FlatAlloca->setAlignment (AI.getAlign ());
216216 AI.replaceAllUsesWith (FlatAlloca);
217217 AI.eraseFromParent ();
@@ -222,6 +222,10 @@ void DXILFlattenArraysVisitor::recursivelyCollectGEPs(
222222 GetElementPtrInst &CurrGEP, ArrayType *FlattenedArrayType,
223223 Value *PtrOperand, unsigned &GEPChainUseCount, SmallVector<Value *> Indices,
224224 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+
225229 Value *LastIndex = CurrGEP.getOperand (CurrGEP.getNumOperands () - 1 );
226230 AllIndicesAreConstInt &= isa<ConstantInt>(LastIndex);
227231 Indices.push_back (LastIndex);
@@ -271,10 +275,18 @@ bool DXILFlattenArraysVisitor::visitGetElementPtrInstInGEPChainBase(
271275 genInstructionFlattenIndices (GEPInfo.Indices , GEPInfo.Dims , Builder);
272276
273277 ArrayType *FlattenedArrayType = GEPInfo.ParentArrayType ;
274- Value *FlatGEP =
275- Builder.CreateGEP (FlattenedArrayType, GEPInfo.ParendOperand ,
276- {Builder.getInt32 (0 ), FlatIndex},
277- GEP.getName () + " .flat" , GEP.getNoWrapFlags ());
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+
283+ Value *FlatGEP = Builder.CreateGEP (FlattenedArrayType, GEPInfo.ParentOperand ,
284+ {Builder.getInt32 (0 ), FlatIndex}, FlatName,
285+ GEP.getNoWrapFlags ());
286+
287+ // Store the new GEP in the map before replacing uses to avoid circular
288+ // references
289+ GEPChainMap.erase (&GEP);
278290
279291 GEP.replaceAllUsesWith (FlatGEP);
280292 GEP.eraseFromParent ();
0 commit comments