Skip to content
19 changes: 19 additions & 0 deletions llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ static cl::opt<unsigned>
"when sorting profitable allocas"),
cl::init(4));

static cl::opt<unsigned> PromoteAllocaDynamicIndexNumberElementLimit(
"amdgpu-promote-alloca-dynamic-index-num-element-limit",
cl::desc("Maximum number of elements for promoting alloca with dynamic"
" index"),
cl::init(8));

// Shared implementation which can do both promotion to vector and to LDS.
class AMDGPUPromoteAllocaImpl {
private:
Expand Down Expand Up @@ -920,6 +926,19 @@ bool AMDGPUPromoteAllocaImpl::tryPromoteAllocaToVector(AllocaInst &Alloca) {
if (!Index)
return RejectUser(Inst, "cannot compute vector index for GEP");

if (!isa<ConstantInt>(Index)) {
bool UsedInLoad = llvm::any_of(
GEP->users(), [&](const auto *U) { return isa<LoadInst>(U); });
if (auto *UserVecTy =
dyn_cast<FixedVectorType>(GEP->getSourceElementType())) {
if (UsedInLoad && UserVecTy->getNumElements() >
PromoteAllocaDynamicIndexNumberElementLimit) {
return RejectUser(Inst,
"user has too many elements for dynamic index");
}
}
}

GEPVectorIdx[GEP] = Index;
UsersToRemove.push_back(Inst);
continue;
Expand Down
Loading