Skip to content

Commit 9459577

Browse files
author
Greg Roth
authored
Optimize compile times by not skipping allocas (#3168)
Instead of skipping past allocas whenever inserting a new insruction, which ate up a lot of compilation time, they are inserted at the default insertion point. The result is that allocas that would have coallesced just after the global load an input loads are dispersed throughout the commands. So as part of dxil finalization, the allocas are moved to the beginning of the entry block of each function. This results in some minor changes to a couple tests due to the allocas preceding the loads.
1 parent 8e3b9e7 commit 9459577

19 files changed

Lines changed: 70 additions & 71 deletions

include/dxc/DXIL/DxilUtil.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,9 @@ namespace dxilutil {
5959
bool HasDynamicIndexing(llvm::Value *V);
6060

6161
// Find alloca insertion point, given instruction
62-
llvm::Instruction *FindAllocaInsertionPt(llvm::Instruction* I); // Considers entire parent function
63-
llvm::Instruction *FindAllocaInsertionPt(llvm::BasicBlock* BB); // Only considers provided block
64-
llvm::Instruction *FindAllocaInsertionPt(llvm::Function* F);
65-
llvm::Instruction *SkipAllocas(llvm::Instruction *I);
66-
// Get first non-alloca insertion point, to avoid inserting non-allocas before alloca
67-
llvm::Instruction *FirstNonAllocaInsertionPt(llvm::Instruction* I); // Considers entire parent function
68-
llvm::Instruction *FirstNonAllocaInsertionPt(llvm::BasicBlock* BB); // Only considers provided block
69-
llvm::Instruction *FirstNonAllocaInsertionPt(llvm::Function* F);
62+
llvm::Instruction *FindInsertionPt(llvm::Instruction* I); // Considers entire parent function
63+
llvm::Instruction *FindInsertionPt(llvm::BasicBlock* BB); // Only considers provided block
64+
llvm::Instruction *FindInsertionPt(llvm::Function* F);
7065

7166
bool IsStaticGlobal(llvm::GlobalVariable *GV);
7267
bool IsSharedMemoryGlobal(llvm::GlobalVariable *GV);

lib/DXIL/DxilUtil.cpp

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -540,33 +540,18 @@ Value *SelectOnOperation(llvm::Instruction *Inst, unsigned operandIdx) {
540540
return nullptr;
541541
}
542542

543-
llvm::Instruction *SkipAllocas(llvm::Instruction *I) {
544-
// Step past any allocas:
545-
while (I && (isa<AllocaInst>(I) || isa<DbgInfoIntrinsic>(I)))
546-
I = I->getNextNode();
547-
return I;
548-
}
549-
llvm::Instruction *FindAllocaInsertionPt(llvm::BasicBlock* BB) {
543+
llvm::Instruction *FindInsertionPt(llvm::BasicBlock* BB) {
550544
return &*BB->getFirstInsertionPt();
551545
}
552-
llvm::Instruction *FindAllocaInsertionPt(llvm::Function* F) {
553-
return FindAllocaInsertionPt(&F->getEntryBlock());
546+
llvm::Instruction *FindInsertionPt(llvm::Function* F) {
547+
return FindInsertionPt(&F->getEntryBlock());
554548
}
555-
llvm::Instruction *FindAllocaInsertionPt(llvm::Instruction* I) {
549+
llvm::Instruction *FindInsertionPt(llvm::Instruction* I) {
556550
Function *F = I->getParent()->getParent();
557551
if (F)
558-
return FindAllocaInsertionPt(F);
552+
return FindInsertionPt(F);
559553
else // BB with no parent function
560-
return FindAllocaInsertionPt(I->getParent());
561-
}
562-
llvm::Instruction *FirstNonAllocaInsertionPt(llvm::Instruction* I) {
563-
return SkipAllocas(FindAllocaInsertionPt(I));
564-
}
565-
llvm::Instruction *FirstNonAllocaInsertionPt(llvm::BasicBlock* BB) {
566-
return SkipAllocas(FindAllocaInsertionPt(BB));
567-
}
568-
llvm::Instruction *FirstNonAllocaInsertionPt(llvm::Function* F) {
569-
return SkipAllocas(FindAllocaInsertionPt(F));
554+
return FindInsertionPt(I->getParent());
570555
}
571556

572557
static bool ConsumePrefix(StringRef &Str, StringRef Prefix) {

lib/DxilPIXPasses/DxilAddPixelHitInstrumentation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ bool DxilAddPixelHitInstrumentation::runOnModule(Module &M) {
100100
CallInst *HandleForUAV;
101101
{
102102
IRBuilder<> Builder(
103-
dxilutil::FirstNonAllocaInsertionPt(DM.GetEntryFunction()));
103+
dxilutil::FindInsertionPt(DM.GetEntryFunction()));
104104

105105
unsigned int UAVResourceHandle =
106106
static_cast<unsigned int>(DM.GetUAVs().size());

lib/DxilPIXPasses/DxilDebugInstrumentation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ bool DxilDebugInstrumentation::runOnModule(Module &M) {
945945
//
946946

947947
Instruction *firstInsertionPt =
948-
dxilutil::FirstNonAllocaInsertionPt(DM.GetEntryFunction());
948+
dxilutil::FindInsertionPt(DM.GetEntryFunction());
949949
IRBuilder<> Builder(firstInsertionPt);
950950

951951
BuilderContext BC{M, DM, Ctx, HlslOP, Builder};

lib/DxilPIXPasses/DxilPIXMeshShaderOutputInstrumentation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ bool DxilPIXMeshShaderOutputInstrumentation::runOnModule(Module &M)
268268
OP *HlslOP = DM.GetOP();
269269

270270
Instruction *firstInsertionPt =
271-
dxilutil::FirstNonAllocaInsertionPt(DM.GetEntryFunction());
271+
dxilutil::FindInsertionPt(DM.GetEntryFunction());
272272
IRBuilder<> Builder(firstInsertionPt);
273273

274274
BuilderContext BC{M, DM, Ctx, HlslOP, Builder};

lib/HLSL/DxilCondenseResources.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1966,7 +1966,7 @@ void DxilLowerCreateHandleForLib::TranslateDxilResourceUses(
19661966
for (iplist<Function>::iterator F : pM->getFunctionList()) {
19671967
if (!F->isDeclaration()) {
19681968
if (!isResArray) {
1969-
IRBuilder<> Builder(dxilutil::FirstNonAllocaInsertionPt(F));
1969+
IRBuilder<> Builder(dxilutil::FindInsertionPt(F));
19701970
if (m_HasDbgInfo) {
19711971
// TODO: set debug info.
19721972
// Builder.SetCurrentDebugLocation(DL);

lib/HLSL/DxilEliminateOutputDynamicIndexing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ bool DxilEliminateOutputDynamicIndexing::EliminateDynamicOutput(
122122
if (dynamicSigSet.empty())
123123
return false;
124124

125-
IRBuilder<> AllocaBuilder(dxilutil::FindAllocaInsertionPt(Entry));
125+
IRBuilder<> AllocaBuilder(dxilutil::FindInsertionPt(Entry));
126126

127127
Value *opcodeV = AllocaBuilder.getInt32(static_cast<unsigned>(opcode));
128128
Value *zero = AllocaBuilder.getInt32(0);

lib/HLSL/DxilGenerationPass.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void SimplifyGlobalSymbol(GlobalVariable *GV) {
6464
for (auto it : handleMapOnFunction) {
6565
Function *F = it.first;
6666
Instruction *I = it.second;
67-
IRBuilder<> Builder(dxilutil::FirstNonAllocaInsertionPt(F));
67+
IRBuilder<> Builder(dxilutil::FindInsertionPt(F));
6868
Value *headLI = Builder.CreateLoad(GV);
6969
I->replaceAllUsesWith(headLI);
7070
}
@@ -537,7 +537,7 @@ void DxilGenerationPass::GenerateDxilCBufferHandles() {
537537
// Must HLCreateHandle.
538538
CallInst *CI = cast<CallInst>(*(U++));
539539
// Put createHandle to entry block.
540-
IRBuilder<> Builder(dxilutil::FirstNonAllocaInsertionPt(CI));
540+
IRBuilder<> Builder(dxilutil::FindInsertionPt(CI));
541541
Value *V = Builder.CreateLoad(GV);
542542
CallInst *handle = Builder.CreateCall(createHandle, {opArg, V}, handleName);
543543
if (m_HasDbgInfo) {
@@ -562,7 +562,7 @@ void DxilGenerationPass::GenerateDxilCBufferHandles() {
562562
Value *CBIndex = CI->getArgOperand(HLOperandIndex::kCreateHandleIndexOpIdx);
563563
if (isa<ConstantInt>(CBIndex)) {
564564
// Put createHandle to entry block for const index.
565-
Builder.SetInsertPoint(dxilutil::FirstNonAllocaInsertionPt(CI));
565+
Builder.SetInsertPoint(dxilutil::FindInsertionPt(CI));
566566
}
567567
// Add GEP for cbv array use.
568568
Value *GEP = Builder.CreateGEP(GV, {zeroIdx, CBIndex});

lib/HLSL/DxilLinker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ DxilLinkJob::Link(std::pair<DxilFunctionLinkInfo *, DxilLib *> &entryLinkPair,
792792
CloneFunctions(vmap);
793793

794794
// Call global constrctor.
795-
IRBuilder<> Builder(dxilutil::FirstNonAllocaInsertionPt(DM.GetEntryFunction()));
795+
IRBuilder<> Builder(dxilutil::FindInsertionPt(DM.GetEntryFunction()));
796796
for (auto &it : m_functionDefs) {
797797
DxilFunctionLinkInfo *linkInfo = it.first;
798798
DxilLib *pLib = it.second;

lib/HLSL/DxilPreparePasses.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,9 @@ class DxilFinalizeModule : public ModulePass {
388388
unsigned DxilMinor = 0;
389389
M.GetDxilModule().GetDxilVersion(DxilMajor, DxilMinor);
390390

391+
// Move all allocas to the top of the entry block
392+
ConsolidateAllocas(M);
393+
391394
bool IsLib = DM.GetShaderModel()->IsLib();
392395
// Skip validation patch for lib.
393396
if (!IsLib) {
@@ -446,6 +449,22 @@ class DxilFinalizeModule : public ModulePass {
446449
}
447450

448451
private:
452+
void ConsolidateAllocas(Module &M) {
453+
for (Function &F : M) {
454+
if (F.isDeclaration())
455+
continue;
456+
Instruction *insertPt = nullptr;
457+
for (llvm::Instruction &I : llvm::inst_range(&F)) {
458+
if (!insertPt) {
459+
if (!isa<AllocaInst>(I) && !isa<DbgInfoIntrinsic>(I))
460+
insertPt = &I;
461+
} else if (isa<AllocaInst>(I)) {
462+
I.moveBefore(insertPt);
463+
}
464+
}
465+
}
466+
}
467+
449468
void RemoveUnusedStaticGlobal(Module &M) {
450469
// Remove unused internal global.
451470
std::vector<GlobalVariable *> staticGVs;
@@ -652,7 +671,7 @@ class DxilFinalizeModule : public ModulePass {
652671
Function *F = CI->getParent()->getParent();
653672
ICmpInst *Cmp = DxBreakCmpMap.lookup(F);
654673
if (!Cmp) {
655-
Instruction *IP = dxilutil::FirstNonAllocaInsertionPt(F);
674+
Instruction *IP = dxilutil::FindInsertionPt(F);
656675
LoadInst *LI = new LoadInst(Gep, nullptr, false, IP);
657676
Cmp = new ICmpInst(IP, ICmpInst::ICMP_EQ, LI, llvm::ConstantInt::get(i32Ty,0));
658677
DxBreakCmpMap[F] = Cmp;

0 commit comments

Comments
 (0)