Skip to content

Commit 754e99b

Browse files
author
Greg Roth
authored
Revert "Optimize compile times by not skipping allocas (#3168)" (#3183)
This reverts commit 9459577.
1 parent b3d9c8a commit 754e99b

19 files changed

Lines changed: 71 additions & 70 deletions

include/dxc/DXIL/DxilUtil.h

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

6161
// Find alloca insertion point, given instruction
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);
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);
6570

6671
bool IsStaticGlobal(llvm::GlobalVariable *GV);
6772
bool IsSharedMemoryGlobal(llvm::GlobalVariable *GV);

lib/DXIL/DxilUtil.cpp

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

543-
llvm::Instruction *FindInsertionPt(llvm::BasicBlock* BB) {
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) {
544550
return &*BB->getFirstInsertionPt();
545551
}
546-
llvm::Instruction *FindInsertionPt(llvm::Function* F) {
547-
return FindInsertionPt(&F->getEntryBlock());
552+
llvm::Instruction *FindAllocaInsertionPt(llvm::Function* F) {
553+
return FindAllocaInsertionPt(&F->getEntryBlock());
548554
}
549-
llvm::Instruction *FindInsertionPt(llvm::Instruction* I) {
555+
llvm::Instruction *FindAllocaInsertionPt(llvm::Instruction* I) {
550556
Function *F = I->getParent()->getParent();
551557
if (F)
552-
return FindInsertionPt(F);
558+
return FindAllocaInsertionPt(F);
553559
else // BB with no parent function
554-
return FindInsertionPt(I->getParent());
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));
555570
}
556571

557572
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::FindInsertionPt(DM.GetEntryFunction()));
103+
dxilutil::FirstNonAllocaInsertionPt(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
@@ -957,7 +957,7 @@ bool DxilDebugInstrumentation::runOnModule(Module &M) {
957957
//
958958

959959
Instruction *firstInsertionPt =
960-
dxilutil::FindInsertionPt(DM.GetEntryFunction());
960+
dxilutil::FirstNonAllocaInsertionPt(DM.GetEntryFunction());
961961
IRBuilder<> Builder(firstInsertionPt);
962962

963963
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::FindInsertionPt(DM.GetEntryFunction());
271+
dxilutil::FirstNonAllocaInsertionPt(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::FindInsertionPt(F));
1969+
IRBuilder<> Builder(dxilutil::FirstNonAllocaInsertionPt(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::FindInsertionPt(Entry));
125+
IRBuilder<> AllocaBuilder(dxilutil::FindAllocaInsertionPt(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::FindInsertionPt(F));
67+
IRBuilder<> Builder(dxilutil::FirstNonAllocaInsertionPt(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::FindInsertionPt(CI));
540+
IRBuilder<> Builder(dxilutil::FirstNonAllocaInsertionPt(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::FindInsertionPt(CI));
565+
Builder.SetInsertPoint(dxilutil::FirstNonAllocaInsertionPt(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::FindInsertionPt(DM.GetEntryFunction()));
795+
IRBuilder<> Builder(dxilutil::FirstNonAllocaInsertionPt(DM.GetEntryFunction()));
796796
for (auto &it : m_functionDefs) {
797797
DxilFunctionLinkInfo *linkInfo = it.first;
798798
DxilLib *pLib = it.second;

lib/HLSL/DxilPreparePasses.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,6 @@ 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-
394391
bool IsLib = DM.GetShaderModel()->IsLib();
395392
// Skip validation patch for lib.
396393
if (!IsLib) {
@@ -449,22 +446,6 @@ class DxilFinalizeModule : public ModulePass {
449446
}
450447

451448
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-
468449
void RemoveUnusedStaticGlobal(Module &M) {
469450
// Remove unused internal global.
470451
std::vector<GlobalVariable *> staticGVs;
@@ -671,7 +652,7 @@ class DxilFinalizeModule : public ModulePass {
671652
Function *F = CI->getParent()->getParent();
672653
ICmpInst *Cmp = DxBreakCmpMap.lookup(F);
673654
if (!Cmp) {
674-
Instruction *IP = dxilutil::FindInsertionPt(F);
655+
Instruction *IP = dxilutil::FirstNonAllocaInsertionPt(F);
675656
LoadInst *LI = new LoadInst(Gep, nullptr, false, IP);
676657
Cmp = new ICmpInst(IP, ICmpInst::ICMP_EQ, LI, llvm::ConstantInt::get(i32Ty,0));
677658
DxBreakCmpMap[F] = Cmp;

0 commit comments

Comments
 (0)