@@ -98,9 +98,9 @@ static void fixI8UseChain(Instruction &I,
9898 ElementType = AI->getAllocatedType ();
9999 if (auto *GEP = dyn_cast<GetElementPtrInst>(NewOperands[0 ])) {
100100 ElementType = GEP->getSourceElementType ();
101- if (ElementType->isArrayTy ())
102- ElementType = ElementType->getArrayElementType ();
103101 }
102+ if (ElementType->isArrayTy ())
103+ ElementType = ElementType->getArrayElementType ();
104104 LoadInst *NewLoad = Builder.CreateLoad (ElementType, NewOperands[0 ]);
105105 ReplacedValues[Load] = NewLoad;
106106 ToRemove.push_back (Load);
@@ -562,6 +562,45 @@ legalizeGetHighLowi64Bytes(Instruction &I,
562562 }
563563}
564564
565+ static void
566+ legalizeLoadStoreOnArrayAllocas (Instruction &I,
567+ SmallVectorImpl<Instruction *> &ToRemove,
568+ DenseMap<Value *, Value *> &) {
569+
570+ Value *PtrOp;
571+ unsigned PtrOpIndex;
572+ [[maybe_unused]] Type *LoadStoreTy;
573+ if (auto *LI = dyn_cast<LoadInst>(&I)) {
574+ PtrOp = LI->getPointerOperand ();
575+ PtrOpIndex = LI->getPointerOperandIndex ();
576+ LoadStoreTy = LI->getType ();
577+ } else if (auto *SI = dyn_cast<StoreInst>(&I)) {
578+ PtrOp = SI->getPointerOperand ();
579+ PtrOpIndex = SI->getPointerOperandIndex ();
580+ LoadStoreTy = SI->getValueOperand ()->getType ();
581+ } else
582+ return ;
583+
584+ assert (LoadStoreTy->isSingleValueType () &&
585+ " Expected load/store type to be a single-valued type" );
586+
587+ auto *AllocaPtrOp = dyn_cast<AllocaInst>(PtrOp);
588+ if (!AllocaPtrOp)
589+ return ;
590+
591+ Type *Ty = AllocaPtrOp->getAllocatedType ();
592+ if (!isa<ArrayType>(Ty))
593+ return ;
594+ assert (!isa<ArrayType>(Ty->getArrayElementType ()) &&
595+ " Expected allocated type of AllocaInst to be a flat ArrayType" );
596+
597+ IRBuilder<> Builder (&I);
598+ Value *Zero = Builder.getInt32 (0 );
599+ Value *GEP = Builder.CreateGEP (Ty, AllocaPtrOp, {Zero, Zero}, " " ,
600+ GEPNoWrapFlags::all ());
601+ I.setOperand (PtrOpIndex, GEP);
602+ }
603+
565604namespace {
566605class DXILLegalizationPipeline {
567606
@@ -612,6 +651,7 @@ class DXILLegalizationPipeline {
612651 // downcastI64toI32InsertExtractElements needs to handle.
613652 LegalizationPipeline[Stage2].push_back (
614653 downcastI64toI32InsertExtractElements);
654+ LegalizationPipeline[Stage2].push_back (legalizeLoadStoreOnArrayAllocas);
615655 }
616656};
617657
0 commit comments