Skip to content

Commit e28c30c

Browse files
nhaehnlekcloudy0717
authored andcommitted
AMDGPU/PromoteAlloca: Extract getVectorTypeForAlloca helper (llvm#170509)
1 parent 03c5f6f commit e28c30c

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class AMDGPUPromoteAllocaImpl {
122122
/// Check whether we have enough local memory for promotion.
123123
bool hasSufficientLocalMem(const Function &F);
124124

125+
FixedVectorType *getVectorTypeForAlloca(Type *AllocaTy) const;
125126
bool tryPromoteAllocaToVector(AllocaInst &I);
126127
bool tryPromoteAllocaToLDS(AllocaInst &I, bool SufficientLDS);
127128

@@ -791,16 +792,13 @@ static BasicBlock::iterator skipToNonAllocaInsertPt(BasicBlock &BB,
791792
return I;
792793
}
793794

794-
// FIXME: Should try to pick the most likely to be profitable allocas first.
795-
bool AMDGPUPromoteAllocaImpl::tryPromoteAllocaToVector(AllocaInst &Alloca) {
796-
LLVM_DEBUG(dbgs() << "Trying to promote to vector: " << Alloca << '\n');
797-
795+
FixedVectorType *
796+
AMDGPUPromoteAllocaImpl::getVectorTypeForAlloca(Type *AllocaTy) const {
798797
if (DisablePromoteAllocaToVector) {
799-
LLVM_DEBUG(dbgs() << " Promote alloca to vector is disabled\n");
800-
return false;
798+
LLVM_DEBUG(dbgs() << " Promote alloca to vectors is disabled\n");
799+
return nullptr;
801800
}
802801

803-
Type *AllocaTy = Alloca.getAllocatedType();
804802
auto *VectorTy = dyn_cast<FixedVectorType>(AllocaTy);
805803
if (auto *ArrayTy = dyn_cast<ArrayType>(AllocaTy)) {
806804
uint64_t NumElems = 1;
@@ -832,10 +830,9 @@ bool AMDGPUPromoteAllocaImpl::tryPromoteAllocaToVector(AllocaInst &Alloca) {
832830
}
833831
}
834832
}
835-
836833
if (!VectorTy) {
837834
LLVM_DEBUG(dbgs() << " Cannot convert type to vector\n");
838-
return false;
835+
return nullptr;
839836
}
840837

841838
const unsigned MaxElements =
@@ -845,9 +842,29 @@ bool AMDGPUPromoteAllocaImpl::tryPromoteAllocaToVector(AllocaInst &Alloca) {
845842
VectorTy->getNumElements() < 2) {
846843
LLVM_DEBUG(dbgs() << " " << *VectorTy
847844
<< " has an unsupported number of elements\n");
848-
return false;
845+
return nullptr;
849846
}
850847

848+
Type *VecEltTy = VectorTy->getElementType();
849+
unsigned ElementSizeInBits = DL->getTypeSizeInBits(VecEltTy);
850+
if (ElementSizeInBits != DL->getTypeAllocSizeInBits(VecEltTy)) {
851+
LLVM_DEBUG(dbgs() << " Cannot convert to vector if the allocation size "
852+
"does not match the type's size\n");
853+
return nullptr;
854+
}
855+
856+
return VectorTy;
857+
}
858+
859+
// FIXME: Should try to pick the most likely to be profitable allocas first.
860+
bool AMDGPUPromoteAllocaImpl::tryPromoteAllocaToVector(AllocaInst &Alloca) {
861+
LLVM_DEBUG(dbgs() << "Trying to promote to vectors: " << Alloca << '\n');
862+
863+
Type *AllocaTy = Alloca.getAllocatedType();
864+
FixedVectorType *VectorTy = getVectorTypeForAlloca(AllocaTy);
865+
if (!VectorTy)
866+
return false;
867+
851868
std::map<GetElementPtrInst *, WeakTrackingVH> GEPVectorIdx;
852869
SmallVector<Instruction *> WorkList;
853870
SmallVector<Instruction *> UsersToRemove;
@@ -869,13 +886,7 @@ bool AMDGPUPromoteAllocaImpl::tryPromoteAllocaToVector(AllocaInst &Alloca) {
869886
LLVM_DEBUG(dbgs() << " Attempting promotion to: " << *VectorTy << "\n");
870887

871888
Type *VecEltTy = VectorTy->getElementType();
872-
unsigned ElementSizeInBits = DL->getTypeSizeInBits(VecEltTy);
873-
if (ElementSizeInBits != DL->getTypeAllocSizeInBits(VecEltTy)) {
874-
LLVM_DEBUG(dbgs() << " Cannot convert to vector if the allocation size "
875-
"does not match the type's size\n");
876-
return false;
877-
}
878-
unsigned ElementSize = ElementSizeInBits / 8;
889+
unsigned ElementSize = DL->getTypeSizeInBits(VecEltTy) / 8;
879890
assert(ElementSize > 0);
880891
for (auto *U : Uses) {
881892
Instruction *Inst = cast<Instruction>(U->getUser());

0 commit comments

Comments
 (0)