Skip to content

Commit

Permalink
Revert "[AMDGPU] Vectorize alloca thru bitcast"
Browse files Browse the repository at this point in the history
This reverts commit db7dea2.
  • Loading branch information
rampitec authored and piotrAMD committed May 11, 2020
1 parent 7eee638 commit 9f5a7cd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 419 deletions.
90 changes: 23 additions & 67 deletions llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,19 +302,10 @@ static VectorType *arrayTypeToVecType(ArrayType *ArrayTy) {
ArrayTy->getNumElements());
}

static Value *stripBitcasts(Value *V) {
while (Instruction *I = dyn_cast<Instruction>(V)) {
if (I->getOpcode() != Instruction::BitCast)
break;
V = I->getOperand(0);
}
return V;
}

static Value *
calculateVectorIndex(Value *Ptr,
const std::map<GetElementPtrInst *, Value *> &GEPIdx) {
GetElementPtrInst *GEP = cast<GetElementPtrInst>(stripBitcasts(Ptr));
GetElementPtrInst *GEP = cast<GetElementPtrInst>(Ptr);

auto I = GEPIdx.find(GEP);
return I == GEPIdx.end() ? nullptr : I->second;
Expand All @@ -336,8 +327,7 @@ static Value* GEPToVectorIndex(GetElementPtrInst *GEP) {
//
// TODO: Check isTriviallyVectorizable for calls and handle other
// instructions.
static bool canVectorizeInst(Instruction *Inst, User *User,
const DataLayout &DL) {
static bool canVectorizeInst(Instruction *Inst, User *User) {
switch (Inst->getOpcode()) {
case Instruction::Load: {
// Currently only handle the case where the Pointer Operand is a GEP.
Expand All @@ -347,14 +337,7 @@ static bool canVectorizeInst(Instruction *Inst, User *User,
LI->getPointerOperandType() == User->getType() &&
isa<VectorType>(LI->getType()))
return true;

Instruction *PtrInst = dyn_cast<Instruction>(LI->getPointerOperand());
if (!PtrInst)
return false;

return (PtrInst->getOpcode() == Instruction::GetElementPtr ||
PtrInst->getOpcode() == Instruction::BitCast) &&
LI->isSimple();
return isa<GetElementPtrInst>(LI->getPointerOperand()) && LI->isSimple();
}
case Instruction::BitCast:
return true;
Expand All @@ -367,22 +350,14 @@ static bool canVectorizeInst(Instruction *Inst, User *User,
SI->getPointerOperandType() == User->getType() &&
isa<VectorType>(SI->getValueOperand()->getType()))
return true;

Instruction *UserInst = dyn_cast<Instruction>(User);
if (!UserInst)
return false;

return (SI->getPointerOperand() == User) &&
(UserInst->getOpcode() == Instruction::GetElementPtr ||
UserInst->getOpcode() == Instruction::BitCast) &&
SI->isSimple();
return (SI->getPointerOperand() == User) && isa<GetElementPtrInst>(User) && SI->isSimple();
}
default:
return false;
}
}

static bool tryPromoteAllocaToVector(AllocaInst *Alloca, const DataLayout &DL) {
static bool tryPromoteAllocaToVector(AllocaInst *Alloca) {

if (DisablePromoteAllocaToVector) {
LLVM_DEBUG(dbgs() << " Promotion alloca to vector is disabled\n");
Expand Down Expand Up @@ -410,37 +385,13 @@ static bool tryPromoteAllocaToVector(AllocaInst *Alloca, const DataLayout &DL) {
}

std::map<GetElementPtrInst*, Value*> GEPVectorIdx;
std::vector<Value *> WorkList;
SmallVector<User *, 8> Users(Alloca->users());
SmallVector<User *, 8> UseUsers(Users.size(), Alloca);
Type *VecEltTy = VectorTy->getElementType();
while (!Users.empty()) {
User *AllocaUser = Users.pop_back_val();
User *UseUser = UseUsers.pop_back_val();
Instruction *Inst = dyn_cast<Instruction>(AllocaUser);

std::vector<Value*> WorkList;
for (User *AllocaUser : Alloca->users()) {
GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(AllocaUser);
if (!GEP) {
if (!canVectorizeInst(Inst, UseUser, DL))
if (!canVectorizeInst(cast<Instruction>(AllocaUser), Alloca))
return false;

if (Inst->getOpcode() == Instruction::BitCast) {
Type *FromTy = Inst->getOperand(0)->getType()->getPointerElementType();
Type *ToTy = Inst->getType()->getPointerElementType();
if (FromTy->isAggregateType() || ToTy->isAggregateType() ||
DL.getTypeSizeInBits(FromTy) != DL.getTypeSizeInBits(ToTy))
continue;

for (User *CastUser : Inst->users()) {
if (isAssumeLikeIntrinsic(cast<Instruction>(CastUser)))
continue;
Users.push_back(CastUser);
UseUsers.push_back(Inst);
}

continue;
}

WorkList.push_back(AllocaUser);
continue;
}
Expand All @@ -456,8 +407,12 @@ static bool tryPromoteAllocaToVector(AllocaInst *Alloca, const DataLayout &DL) {
}

GEPVectorIdx[GEP] = Index;
Users.append(GEP->user_begin(), GEP->user_end());
UseUsers.append(GEP->getNumUses(), GEP);
for (User *GEPUser : AllocaUser->users()) {
if (!canVectorizeInst(cast<Instruction>(GEPUser), AllocaUser))
return false;

WorkList.push_back(GEPUser);
}
}

LLVM_DEBUG(dbgs() << " Converting alloca to vector " << *AllocaTy << " -> "
Expand All @@ -478,8 +433,6 @@ static bool tryPromoteAllocaToVector(AllocaInst *Alloca, const DataLayout &DL) {
Value *BitCast = Builder.CreateBitCast(Alloca, VecPtrTy);
Value *VecValue = Builder.CreateLoad(VectorTy, BitCast);
Value *ExtractElement = Builder.CreateExtractElement(VecValue, Index);
if (Inst->getType() != VecEltTy)
ExtractElement = Builder.CreateBitCast(ExtractElement, Inst->getType());
Inst->replaceAllUsesWith(ExtractElement);
Inst->eraseFromParent();
break;
Expand All @@ -494,14 +447,16 @@ static bool tryPromoteAllocaToVector(AllocaInst *Alloca, const DataLayout &DL) {
Value *Index = calculateVectorIndex(Ptr, GEPVectorIdx);
Value *BitCast = Builder.CreateBitCast(Alloca, VecPtrTy);
Value *VecValue = Builder.CreateLoad(VectorTy, BitCast);
Value *Elt = SI->getValueOperand();
if (Elt->getType() != VecEltTy)
Elt = Builder.CreateBitCast(Elt, VecEltTy);
Value *NewVecValue = Builder.CreateInsertElement(VecValue, Elt, Index);
Value *NewVecValue = Builder.CreateInsertElement(VecValue,
SI->getValueOperand(),
Index);
Builder.CreateStore(NewVecValue, BitCast);
Inst->eraseFromParent();
break;
}
case Instruction::BitCast:
case Instruction::AddrSpaceCast:
break;

default:
llvm_unreachable("Inconsistency in instructions promotable to vector");
Expand Down Expand Up @@ -766,15 +721,14 @@ bool AMDGPUPromoteAlloca::handleAlloca(AllocaInst &I, bool SufficientLDS) {
if (!I.isStaticAlloca() || I.isArrayAllocation())
return false;

const DataLayout &DL = Mod->getDataLayout();
IRBuilder<> Builder(&I);

// First try to replace the alloca with a vector
Type *AllocaTy = I.getAllocatedType();

LLVM_DEBUG(dbgs() << "Trying to promote " << I << '\n');

if (tryPromoteAllocaToVector(&I, DL))
if (tryPromoteAllocaToVector(&I))
return true; // Promoted to vector.

if (DisablePromoteAllocaToLDS)
Expand Down Expand Up @@ -804,6 +758,8 @@ bool AMDGPUPromoteAlloca::handleAlloca(AllocaInst &I, bool SufficientLDS) {
const AMDGPUSubtarget &ST = AMDGPUSubtarget::get(*TM, ContainingFunction);
unsigned WorkGroupSize = ST.getFlatWorkGroupSizes(ContainingFunction).second;

const DataLayout &DL = Mod->getDataLayout();

unsigned Align = I.getAlignment();
if (Align == 0)
Align = DL.getABITypeAlignment(I.getAllocatedType());
Expand Down
Loading

0 comments on commit 9f5a7cd

Please sign in to comment.