Skip to content

Commit fa26924

Browse files
jaladreipsigcbot
authored andcommitted
Improve InlineHelper LLVM utility
Improve InlineHelper LLVM utility
1 parent 1ce5a03 commit fa26924

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

IGC/common/LLVMUtils.cpp

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -951,33 +951,47 @@ void InlineHelper::InlineAndOptimize(CallInst* callInst)
951951
m_calledFunction = callInst->getCalledFunction();
952952
#endif
953953
auto* fn = callInst->getFunction();
954-
955954
InlineFunctionInfo IFI;
956955
bool CanInline = IGCLLVM::InlineFunction(*callInst, IFI);
957956
IGC_ASSERT_MESSAGE(CanInline, "failed to inline?");
958957

959-
auto& perFnAllocas = m_InlinedStaticArrayAllocas[fn];
960-
961958
// Merge static array allocas to reduce the use of private
962959
// memory. This is a similar optimization that exists in
963960
// the inliner, see mergeInlinedArrayAllocas().
964961
llvm::erase_if(
965962
IFI.StaticAllocas,
966963
[](AllocaInst* I)
967964
{
968-
return I->isArrayAllocation() || !I->getAllocatedType()->isArrayTy();
965+
return I->getAllocatedType()->isArrayTy() || !isa<ConstantInt>(I->getArraySize());
969966
}
970967
);
971968

972-
auto staticAlloca = IFI.StaticAllocas.begin();
973-
for (auto* fnAlloca : perFnAllocas)
969+
AllocaMap localAllocas;
970+
for (auto* I : IFI.StaticAllocas)
974971
{
975-
if ((*staticAlloca)->getAllocatedType() == fnAlloca->getAllocatedType())
976-
(*staticAlloca)->replaceAllUsesWith(fnAlloca);
977-
978-
if (++staticAlloca == IFI.StaticAllocas.end())
979-
return;
972+
localAllocas[
973+
std::make_tuple(
974+
I->getType(),
975+
cast<ConstantInt>(I->getArraySize())->getZExtValue(),
976+
I->getAddressSpace())
977+
].push_back(I);
980978
}
981979

982-
perFnAllocas.append(staticAlloca, IFI.StaticAllocas.end());
980+
for (auto& [key, perTypeAllocas] :localAllocas)
981+
{
982+
auto localAlloca = perTypeAllocas.begin();
983+
auto& globalAllocas = m_InlinedStaticArrayAllocas[fn][key];
984+
for (auto globalAlloca : globalAllocas)
985+
{
986+
(*localAlloca)->replaceAllUsesWith(globalAlloca);
987+
(*localAlloca)->eraseFromParent();
988+
989+
localAlloca++;
990+
991+
if (localAlloca == perTypeAllocas.end())
992+
break;
993+
}
994+
995+
globalAllocas.append(localAlloca, perTypeAllocas.end());
996+
}
983997
}

IGC/common/LLVMUtils.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ void DumpHashToOptions(const ShaderHash&, const ShaderType);
5252
class InlineHelper
5353
{
5454
private:
55-
llvm::DenseMap<llvm::Function*, llvm::SmallVector<llvm::AllocaInst*, 4>> m_InlinedStaticArrayAllocas;
55+
// create a map of (type, array size, address space to alloca instructions)
56+
using AllocaMap = llvm::DenseMap<
57+
std::tuple<llvm::Type*, uint64_t, uint32_t>,
58+
llvm::SmallVector<llvm::AllocaInst*>
59+
>;
60+
61+
llvm::DenseMap<llvm::Function*, AllocaMap> m_InlinedStaticArrayAllocas;
62+
5663
#if defined(_RELEASE_INTERNAL) || defined(_DEBUG)
5764
llvm::Function* m_calledFunction = nullptr;
5865
#endif

0 commit comments

Comments
 (0)