Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 43 additions & 52 deletions llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,22 +461,23 @@ static Value *GEPToVectorIndex(GetElementPtrInst *GEP, AllocaInst *Alloca,
return nullptr;

Value *Offset = VarOffset.first;
auto *OffsetType = dyn_cast<IntegerType>(Offset->getType());
if (!OffsetType)
if (!isa<IntegerType>(Offset->getType()))
return nullptr;

Offset = Builder.CreateSExtOrTrunc(Offset, Builder.getIntNTy(BW));
if (Offset != VarOffset.first)
NewInsts.push_back(cast<Instruction>(Offset));

if (!OffsetQuot.isOne()) {
ConstantInt *ConstMul =
ConstantInt::get(Ctx, OffsetQuot.sext(OffsetType->getBitWidth()));
ConstantInt *ConstMul = ConstantInt::get(Ctx, OffsetQuot.sextOrTrunc(BW));
Offset = Builder.CreateMul(Offset, ConstMul);
if (Instruction *NewInst = dyn_cast<Instruction>(Offset))
NewInsts.push_back(NewInst);
}
if (ConstOffset.isZero())
return Offset;

ConstantInt *ConstIndex =
ConstantInt::get(Ctx, IndexQuot.sext(OffsetType->getBitWidth()));
ConstantInt *ConstIndex = ConstantInt::get(Ctx, IndexQuot.sextOrTrunc(BW));
Value *IndexAdd = Builder.CreateAdd(Offset, ConstIndex);
if (Instruction *NewInst = dyn_cast<Instruction>(IndexAdd))
NewInsts.push_back(NewInst);
Expand All @@ -502,27 +503,14 @@ static Value *promoteAllocaUserToVector(
Instruction *Inst, const DataLayout &DL, FixedVectorType *VectorTy,
unsigned VecStoreSize, unsigned ElementSize,
DenseMap<MemTransferInst *, MemTransferInfo> &TransferInfo,
std::map<GetElementPtrInst *, WeakTrackingVH> &GEPVectorIdx, Value *CurVal,
SmallVectorImpl<LoadInst *> &DeferredLoads) {
std::map<GetElementPtrInst *, WeakTrackingVH> &GEPVectorIdx,
function_ref<Value *()> GetCurVal) {
// Note: we use InstSimplifyFolder because it can leverage the DataLayout
// to do more folding, especially in the case of vector splats.
IRBuilder<InstSimplifyFolder> Builder(Inst->getContext(),
InstSimplifyFolder(DL));
Builder.SetInsertPoint(Inst);

const auto GetOrLoadCurrentVectorValue = [&]() -> Value * {
if (CurVal)
return CurVal;

// If the current value is not known, insert a dummy load and lower it on
// the second pass.
LoadInst *Dummy =
Builder.CreateLoad(VectorTy, PoisonValue::get(Builder.getPtrTy()),
"promotealloca.dummyload");
DeferredLoads.push_back(Dummy);
return Dummy;
};

const auto CreateTempPtrIntCast = [&Builder, DL](Value *Val,
Type *PtrTy) -> Value * {
assert(DL.getTypeStoreSize(Val->getType()) == DL.getTypeStoreSize(PtrTy));
Expand All @@ -542,12 +530,7 @@ static Value *promoteAllocaUserToVector(

switch (Inst->getOpcode()) {
case Instruction::Load: {
// Loads can only be lowered if the value is known.
if (!CurVal) {
DeferredLoads.push_back(cast<LoadInst>(Inst));
return nullptr;
}

Value *CurVal = GetCurVal();
Value *Index = calculateVectorIndex(
cast<LoadInst>(Inst)->getPointerOperand(), GEPVectorIdx);

Expand Down Expand Up @@ -637,7 +620,7 @@ static Value *promoteAllocaUserToVector(

Val = Builder.CreateBitOrPointerCast(Val, SubVecTy);

Value *CurVec = GetOrLoadCurrentVectorValue();
Value *CurVec = GetCurVal();
for (unsigned K = 0, NumElts = std::min(NumWrittenElts, NumVecElts);
K < NumElts; ++K) {
Value *CurIdx =
Expand All @@ -650,8 +633,7 @@ static Value *promoteAllocaUserToVector(

if (Val->getType() != VecEltTy)
Val = Builder.CreateBitOrPointerCast(Val, VecEltTy);
return Builder.CreateInsertElement(GetOrLoadCurrentVectorValue(), Val,
Index);
return Builder.CreateInsertElement(GetCurVal(), Val, Index);
}
case Instruction::Call: {
if (auto *MTI = dyn_cast<MemTransferInst>(Inst)) {
Expand All @@ -673,7 +655,7 @@ static Value *promoteAllocaUserToVector(
}
}

return Builder.CreateShuffleVector(GetOrLoadCurrentVectorValue(), Mask);
return Builder.CreateShuffleVector(GetCurVal(), Mask);
}

if (auto *MSI = dyn_cast<MemSetInst>(Inst)) {
Expand Down Expand Up @@ -1038,37 +1020,46 @@ bool AMDGPUPromoteAllocaImpl::tryPromoteAllocaToVector(AllocaInst &Alloca) {

Updater.AddAvailableValue(EntryBB, AllocaInitValue);

// First handle the initial worklist.
SmallVector<LoadInst *, 4> DeferredLoads;
// First handle the initial worklist, in basic block order.
//
// Insert a placeholder whenever we need the vector value at the top of a
// basic block.
SmallVector<Instruction *> Placeholders;
forEachWorkListItem(WorkList, [&](Instruction *I) {
BasicBlock *BB = I->getParent();
// On the first pass, we only take values that are trivially known, i.e.
// where AddAvailableValue was already called in this block.
Value *Result = promoteAllocaUserToVector(
I, *DL, VectorTy, VecStoreSize, ElementSize, TransferInfo, GEPVectorIdx,
Updater.FindValueForBlock(BB), DeferredLoads);
auto GetCurVal = [&]() -> Value * {
if (Value *CurVal = Updater.FindValueForBlock(BB))
return CurVal;

if (!Placeholders.empty() && Placeholders.back()->getParent() == BB)
return Placeholders.back();

// If the current value in the basic block is not yet known, insert a
// placeholder that we will replace later.
IRBuilder<> Builder(I);
auto *Placeholder = cast<Instruction>(Builder.CreateFreeze(
PoisonValue::get(VectorTy), "promotealloca.placeholder"));
Placeholders.push_back(Placeholder);
return Placeholders.back();
};

Value *Result =
promoteAllocaUserToVector(I, *DL, VectorTy, VecStoreSize, ElementSize,
TransferInfo, GEPVectorIdx, GetCurVal);
if (Result)
Updater.AddAvailableValue(BB, Result);
});

// Then handle deferred loads.
forEachWorkListItem(DeferredLoads, [&](Instruction *I) {
SmallVector<LoadInst *, 0> NewDLs;
BasicBlock *BB = I->getParent();
// On the second pass, we use GetValueInMiddleOfBlock to guarantee we always
// get a value, inserting PHIs as needed.
Value *Result = promoteAllocaUserToVector(
I, *DL, VectorTy, VecStoreSize, ElementSize, TransferInfo, GEPVectorIdx,
Updater.GetValueInMiddleOfBlock(I->getParent()), NewDLs);
if (Result)
Updater.AddAvailableValue(BB, Result);
assert(NewDLs.empty() && "No more deferred loads should be queued!");
});
// Now fixup the placeholders.
for (Instruction *Placeholder : Placeholders) {
Placeholder->replaceAllUsesWith(
Updater.GetValueInMiddleOfBlock(Placeholder->getParent()));
Placeholder->eraseFromParent();
}

// Delete all instructions. On the first pass, new dummy loads may have been
// added so we need to collect them too.
DenseSet<Instruction *> InstsToDelete(WorkList.begin(), WorkList.end());
InstsToDelete.insert_range(DeferredLoads);
for (Instruction *I : InstsToDelete) {
assert(I->use_empty());
I->eraseFromParent();
Expand Down
33 changes: 33 additions & 0 deletions llvm/test/CodeGen/AMDGPU/promote-alloca-cfg.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -S -mtriple=amdgcn-unknown-amdhsa -passes=amdgpu-promote-alloca < %s | FileCheck %s

; Checks that certain CFGs are handled correctly.

define amdgpu_kernel void @chain(i32 %init, i32 %i.1, i32 %i.2) {
; CHECK-LABEL: @chain(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = freeze <4 x i8> poison
; CHECK-NEXT: [[TMP0:%.*]] = bitcast i32 [[INIT:%.*]] to <4 x i8>
; CHECK-NEXT: br label [[BB1:%.*]]
; CHECK: bb1:
; CHECK-NEXT: [[TMP1:%.*]] = extractelement <4 x i8> [[TMP0]], i32 [[I_1:%.*]]
; CHECK-NEXT: br label [[BB2:%.*]]
; CHECK: bb2:
; CHECK-NEXT: [[TMP2:%.*]] = extractelement <4 x i8> [[TMP0]], i32 [[I_2:%.*]]
; CHECK-NEXT: ret void
;
entry:
%a = alloca [4 x i8], align 4, addrspace(5)
store i32 %init, ptr addrspace(5) %a
br label %bb1

bb1:
%p.1 = getelementptr i8, ptr addrspace(5) %a, i32 %i.1
%x.1 = load i8, ptr addrspace(5) %p.1
br label %bb2

bb2:
%p.2 = getelementptr i8, ptr addrspace(5) %a, i32 %i.2
%x.2 = load i8, ptr addrspace(5) %p.2
ret void
}
28 changes: 15 additions & 13 deletions llvm/test/CodeGen/AMDGPU/promote-alloca-multidim.ll
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,15 @@ define amdgpu_kernel void @i64_2d_load_store_subvec_3_i64_offset(ptr %out) {
; CHECK-NEXT: [[TMP13:%.*]] = insertelement <6 x i64> [[TMP12]], i64 3, i32 3
; CHECK-NEXT: [[TMP14:%.*]] = insertelement <6 x i64> [[TMP13]], i64 4, i32 4
; CHECK-NEXT: [[TMP15:%.*]] = insertelement <6 x i64> [[TMP14]], i64 5, i32 5
; CHECK-NEXT: [[TMP1:%.*]] = mul i64 [[SEL3]], 3
; CHECK-NEXT: [[TMP2:%.*]] = extractelement <6 x i64> [[TMP15]], i64 [[TMP1]]
; CHECK-NEXT: [[TMP7:%.*]] = trunc i64 [[SEL3]] to i32
; CHECK-NEXT: [[TMP16:%.*]] = mul i32 [[TMP7]], 3
; CHECK-NEXT: [[TMP2:%.*]] = extractelement <6 x i64> [[TMP15]], i32 [[TMP16]]
; CHECK-NEXT: [[TMP3:%.*]] = insertelement <3 x i64> poison, i64 [[TMP2]], i64 0
; CHECK-NEXT: [[TMP4:%.*]] = add i64 [[TMP1]], 1
; CHECK-NEXT: [[TMP5:%.*]] = extractelement <6 x i64> [[TMP15]], i64 [[TMP4]]
; CHECK-NEXT: [[TMP17:%.*]] = add i32 [[TMP16]], 1
; CHECK-NEXT: [[TMP5:%.*]] = extractelement <6 x i64> [[TMP15]], i32 [[TMP17]]
; CHECK-NEXT: [[TMP6:%.*]] = insertelement <3 x i64> [[TMP3]], i64 [[TMP5]], i64 1
; CHECK-NEXT: [[TMP7:%.*]] = add i64 [[TMP1]], 2
; CHECK-NEXT: [[TMP8:%.*]] = extractelement <6 x i64> [[TMP15]], i64 [[TMP7]]
; CHECK-NEXT: [[TMP18:%.*]] = add i32 [[TMP16]], 2
; CHECK-NEXT: [[TMP8:%.*]] = extractelement <6 x i64> [[TMP15]], i32 [[TMP18]]
; CHECK-NEXT: [[TMP9:%.*]] = insertelement <3 x i64> [[TMP6]], i64 [[TMP8]], i64 2
; CHECK-NEXT: [[ELEM:%.*]] = extractelement <3 x i64> [[TMP9]], i32 2
; CHECK-NEXT: store i64 [[ELEM]], ptr [[OUT]], align 8
Expand Down Expand Up @@ -311,15 +312,16 @@ define amdgpu_kernel void @i64_2d_load_store_subvec_3_i64_offset_index(ptr %out)
; CHECK-NEXT: [[TMP14:%.*]] = insertelement <6 x i64> [[TMP13]], i64 3, i32 3
; CHECK-NEXT: [[TMP15:%.*]] = insertelement <6 x i64> [[TMP14]], i64 4, i32 4
; CHECK-NEXT: [[TMP16:%.*]] = insertelement <6 x i64> [[TMP15]], i64 5, i32 5
; CHECK-NEXT: [[TMP1:%.*]] = mul i64 [[SEL3]], 3
; CHECK-NEXT: [[TMP2:%.*]] = add i64 [[TMP1]], 6
; CHECK-NEXT: [[TMP3:%.*]] = extractelement <6 x i64> [[TMP16]], i64 [[TMP2]]
; CHECK-NEXT: [[TMP17:%.*]] = trunc i64 [[SEL3]] to i32
; CHECK-NEXT: [[TMP8:%.*]] = mul i32 [[TMP17]], 3
; CHECK-NEXT: [[TMP18:%.*]] = add i32 [[TMP8]], 6
; CHECK-NEXT: [[TMP3:%.*]] = extractelement <6 x i64> [[TMP16]], i32 [[TMP18]]
; CHECK-NEXT: [[TMP4:%.*]] = insertelement <3 x i64> poison, i64 [[TMP3]], i64 0
; CHECK-NEXT: [[TMP5:%.*]] = add i64 [[TMP2]], 1
; CHECK-NEXT: [[TMP6:%.*]] = extractelement <6 x i64> [[TMP16]], i64 [[TMP5]]
; CHECK-NEXT: [[TMP19:%.*]] = add i32 [[TMP18]], 1
; CHECK-NEXT: [[TMP6:%.*]] = extractelement <6 x i64> [[TMP16]], i32 [[TMP19]]
; CHECK-NEXT: [[TMP7:%.*]] = insertelement <3 x i64> [[TMP4]], i64 [[TMP6]], i64 1
; CHECK-NEXT: [[TMP8:%.*]] = add i64 [[TMP2]], 2
; CHECK-NEXT: [[TMP9:%.*]] = extractelement <6 x i64> [[TMP16]], i64 [[TMP8]]
; CHECK-NEXT: [[TMP20:%.*]] = add i32 [[TMP18]], 2
; CHECK-NEXT: [[TMP9:%.*]] = extractelement <6 x i64> [[TMP16]], i32 [[TMP20]]
; CHECK-NEXT: [[TMP10:%.*]] = insertelement <3 x i64> [[TMP7]], i64 [[TMP9]], i64 2
; CHECK-NEXT: [[ELEM:%.*]] = extractelement <3 x i64> [[TMP10]], i32 2
; CHECK-NEXT: store i64 [[ELEM]], ptr [[OUT]], align 8
Expand Down
12 changes: 8 additions & 4 deletions llvm/test/CodeGen/AMDGPU/promote-alloca-negative-index.ll
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ define amdgpu_kernel void @negative_index_byte(ptr %out, i64 %offset) {
; CHECK-NEXT: [[TMP2:%.*]] = insertelement <4 x i8> [[TMP1]], i8 1, i32 1
; CHECK-NEXT: [[TMP3:%.*]] = insertelement <4 x i8> [[TMP2]], i8 2, i32 2
; CHECK-NEXT: [[TMP4:%.*]] = insertelement <4 x i8> [[TMP3]], i8 3, i32 3
; CHECK-NEXT: [[TMP5:%.*]] = add i64 [[OFFSET:%.*]], -1
; CHECK-NEXT: [[TMP6:%.*]] = extractelement <4 x i8> [[TMP4]], i64 [[TMP5]]
; CHECK-NEXT: [[TMP5:%.*]] = trunc i64 [[OFFSET:%.*]] to i32
; CHECK-NEXT: [[TMP8:%.*]] = trunc i64 [[OFFSET]] to i32
; CHECK-NEXT: [[TMP7:%.*]] = add i32 [[TMP8]], -1
; CHECK-NEXT: [[TMP6:%.*]] = extractelement <4 x i8> [[TMP4]], i32 [[TMP7]]
; CHECK-NEXT: store i8 [[TMP6]], ptr [[OUT:%.*]], align 1
; CHECK-NEXT: ret void
;
Expand All @@ -39,8 +41,10 @@ define amdgpu_kernel void @negative_index_word(ptr %out, i64 %offset) {
; CHECK-NEXT: [[TMP2:%.*]] = insertelement <4 x i32> [[TMP1]], i32 1, i32 1
; CHECK-NEXT: [[TMP3:%.*]] = insertelement <4 x i32> [[TMP2]], i32 2, i32 2
; CHECK-NEXT: [[TMP4:%.*]] = insertelement <4 x i32> [[TMP3]], i32 3, i32 3
; CHECK-NEXT: [[TMP5:%.*]] = add i64 [[OFFSET:%.*]], -1
; CHECK-NEXT: [[TMP6:%.*]] = extractelement <4 x i32> [[TMP4]], i64 [[TMP5]]
; CHECK-NEXT: [[TMP5:%.*]] = trunc i64 [[OFFSET:%.*]] to i32
; CHECK-NEXT: [[TMP8:%.*]] = trunc i64 [[OFFSET]] to i32
; CHECK-NEXT: [[TMP7:%.*]] = add i32 [[TMP8]], -1
; CHECK-NEXT: [[TMP6:%.*]] = extractelement <4 x i32> [[TMP4]], i32 [[TMP7]]
; CHECK-NEXT: store i32 [[TMP6]], ptr [[OUT:%.*]], align 4
; CHECK-NEXT: ret void
;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
; RUN: llc -mtriple=amdgcn -mcpu=fiji < %s | FileCheck -enable-var-scope -check-prefix=GCN %s
; RUN: llc -mtriple=amdgcn -mcpu=gfx900 < %s | FileCheck -enable-var-scope -check-prefix=GCN %s
; RUN: opt -S -mtriple=amdgcn-- -data-layout=A5 -mcpu=fiji -passes=sroa,amdgpu-promote-alloca < %s | FileCheck -check-prefix=OPT %s
; RUN: opt -S -mtriple=amdgcn-- -mcpu=fiji -passes=sroa,amdgpu-promote-alloca < %s | FileCheck -check-prefix=OPT %s

; GCN-LABEL: {{^}}float4_alloca_store4:
; OPT-LABEL: define amdgpu_kernel void @float4_alloca_store4
Expand Down
Loading