瀏覽代碼

More places to be cautious about alloca vs. non-alloca insert locs.

- some builder name changes for consistency
Tex Riddell 7 年之前
父節點
當前提交
335ee949bb

+ 2 - 1
lib/HLSL/DxilAddPixelHitInstrumentation.cpp

@@ -15,6 +15,7 @@
 #include "dxc/HLSL/DxilInstructions.h"
 #include "dxc/HLSL/DxilInstructions.h"
 #include "dxc/HLSL/DxilModule.h"
 #include "dxc/HLSL/DxilModule.h"
 #include "dxc/HLSL/DxilPIXPasses.h"
 #include "dxc/HLSL/DxilPIXPasses.h"
+#include "dxc/HLSL/DxilUtil.h"
 
 
 #include "llvm/IR/PassManager.h"
 #include "llvm/IR/PassManager.h"
 #include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Transforms/Utils/Local.h"
@@ -93,7 +94,7 @@ bool DxilAddPixelHitInstrumentation::runOnModule(Module &M)
 
 
   CallInst *HandleForUAV;
   CallInst *HandleForUAV;
   {
   {
-    IRBuilder<> Builder(DM.GetEntryFunction()->getEntryBlock().getFirstInsertionPt());
+    IRBuilder<> Builder(dxilutil::FirstNonAllocaInsertionPt(DM.GetEntryFunction()));
     
     
     unsigned int UAVResourceHandle = static_cast<unsigned int>(DM.GetUAVs().size());
     unsigned int UAVResourceHandle = static_cast<unsigned int>(DM.GetUAVs().size());
 
 

+ 1 - 1
lib/HLSL/DxilCondenseResources.cpp

@@ -804,7 +804,7 @@ void DxilLowerCreateHandleForLib::TranslateDxilResourceUses(
   for (iplist<Function>::iterator F : pM->getFunctionList()) {
   for (iplist<Function>::iterator F : pM->getFunctionList()) {
     if (!F->isDeclaration()) {
     if (!F->isDeclaration()) {
       if (!isResArray) {
       if (!isResArray) {
-        IRBuilder<> Builder(F->getEntryBlock().getFirstInsertionPt());
+        IRBuilder<> Builder(dxilutil::FirstNonAllocaInsertionPt(F));
         if (m_HasDbgInfo) {
         if (m_HasDbgInfo) {
           // TODO: set debug info.
           // TODO: set debug info.
           // Builder.SetCurrentDebugLocation(DL);
           // Builder.SetCurrentDebugLocation(DL);

+ 2 - 1
lib/HLSL/DxilDebugInstrumentation.cpp

@@ -13,6 +13,7 @@
 #include "dxc/HLSL/DxilModule.h"
 #include "dxc/HLSL/DxilModule.h"
 #include "dxc/HLSL/DxilOperations.h"
 #include "dxc/HLSL/DxilOperations.h"
 #include "dxc/HLSL/DxilPIXPasses.h"
 #include "dxc/HLSL/DxilPIXPasses.h"
+#include "dxc/HLSL/DxilUtil.h"
 
 
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/Constants.h"
@@ -728,7 +729,7 @@ bool DxilDebugInstrumentation::runOnModule(Module &M) {
   //  value at (UAVSize) - (Small Amount) * 2 (which is actually a conservative definition of overflow).
   //  value at (UAVSize) - (Small Amount) * 2 (which is actually a conservative definition of overflow).
   //
   //
 
 
-  Instruction* firstInsertionPt = DM.GetEntryFunction()->getEntryBlock().getFirstInsertionPt();
+  Instruction* firstInsertionPt = dxilutil::FirstNonAllocaInsertionPt(DM.GetEntryFunction());
   IRBuilder<> Builder(firstInsertionPt);
   IRBuilder<> Builder(firstInsertionPt);
 
 
   BuilderContext BC{ M, DM, Ctx, HlslOP, Builder };
   BuilderContext BC{ M, DM, Ctx, HlslOP, Builder };

+ 5 - 4
lib/HLSL/DxilEliminateOutputDynamicIndexing.cpp

@@ -13,6 +13,7 @@
 #include "dxc/HLSL/DxilOperations.h"
 #include "dxc/HLSL/DxilOperations.h"
 #include "dxc/HLSL/DxilSignatureElement.h"
 #include "dxc/HLSL/DxilSignatureElement.h"
 #include "dxc/HLSL/DxilModule.h"
 #include "dxc/HLSL/DxilModule.h"
+#include "dxc/HLSL/DxilUtil.h"
 #include "dxc/Support/Global.h"
 #include "dxc/Support/Global.h"
 #include "dxc/HLSL/DxilInstructions.h"
 #include "dxc/HLSL/DxilInstructions.h"
 
 
@@ -123,10 +124,10 @@ bool DxilEliminateOutputDynamicIndexing::EliminateDynamicOutput(
   if (dynamicSigSet.empty())
   if (dynamicSigSet.empty())
     return false;
     return false;
 
 
-  IRBuilder<> Builder(Entry->getEntryBlock().getFirstInsertionPt());
+  IRBuilder<> AllocaBuilder(dxilutil::FindAllocaInsertionPt(Entry));
 
 
-  Value *opcodeV = Builder.getInt32(static_cast<unsigned>(opcode));
-  Value *zero = Builder.getInt32(0);
+  Value *opcodeV = AllocaBuilder.getInt32(static_cast<unsigned>(opcode));
+  Value *zero = AllocaBuilder.getInt32(0);
 
 
   for (auto sig : dynamicSigSet) {
   for (auto sig : dynamicSigSet) {
     Value *sigID = sig.first;
     Value *sigID = sig.first;
@@ -139,7 +140,7 @@ bool DxilEliminateOutputDynamicIndexing::EliminateDynamicOutput(
 
 
     std::vector<Value *> tmpSigElts(col);
     std::vector<Value *> tmpSigElts(col);
     for (unsigned c = 0; c < col; c++) {
     for (unsigned c = 0; c < col; c++) {
-      Value *newCol = Builder.CreateAlloca(AT);
+      Value *newCol = AllocaBuilder.CreateAlloca(AT);
       tmpSigElts[c] = newCol;
       tmpSigElts[c] = newCol;
     }
     }
 
 

+ 1 - 2
lib/HLSL/DxilLinker.cpp

@@ -754,8 +754,7 @@ DxilLinkJob::Link(std::pair<DxilFunctionLinkInfo *, DxilLib *> &entryLinkPair,
   CloneFunctions(vmap);
   CloneFunctions(vmap);
 
 
   // Call global constrctor.
   // Call global constrctor.
-  IRBuilder<> Builder(
-      DM.GetEntryFunction()->getEntryBlock().getFirstInsertionPt());
+  IRBuilder<> Builder(dxilutil::FirstNonAllocaInsertionPt(DM.GetEntryFunction()));
   for (auto &it : m_functionDefs) {
   for (auto &it : m_functionDefs) {
     DxilFunctionLinkInfo *linkInfo = it.first;
     DxilFunctionLinkInfo *linkInfo = it.first;
     DxilLib *pLib = it.second;
     DxilLib *pLib = it.second;

+ 5 - 5
lib/HLSL/DxilPreserveAllOutputs.cpp

@@ -86,15 +86,15 @@ public:
   {
   {
   }
   }
 
 
-  void CreateAlloca(IRBuilder<> &builder) {
-    LLVMContext &context = builder.getContext();
+  void CreateAlloca(IRBuilder<> &allocaBuilder) {
+    LLVMContext &context = allocaBuilder.getContext();
     Type *elementType = m_OutputElement.GetCompType().GetLLVMType(context);
     Type *elementType = m_OutputElement.GetCompType().GetLLVMType(context);
     Type *allocaType = nullptr;
     Type *allocaType = nullptr;
     if (IsSingleElement())
     if (IsSingleElement())
       allocaType = elementType;
       allocaType = elementType;
     else
     else
       allocaType = ArrayType::get(elementType, NumElements());
       allocaType = ArrayType::get(elementType, NumElements());
-    m_Alloca = builder.CreateAlloca(allocaType, nullptr, m_OutputElement.GetName());
+    m_Alloca = allocaBuilder.CreateAlloca(allocaType, nullptr, m_OutputElement.GetName());
   }
   }
 
 
   void StoreTemp(IRBuilder<> &builder, Value *row, Value *col, Value *value) const {
   void StoreTemp(IRBuilder<> &builder, Value *row, Value *col, Value *value) const {
@@ -249,11 +249,11 @@ DxilPreserveAllOutputs::OutputMap DxilPreserveAllOutputs::generateOutputMap(cons
   return map;
   return map;
 }
 }
 
 
-void DxilPreserveAllOutputs::createTempAllocas(OutputMap &outputMap, IRBuilder<> &builder)
+void DxilPreserveAllOutputs::createTempAllocas(OutputMap &outputMap, IRBuilder<> &allocaBuilder)
 {
 {
   for (auto &iter: outputMap) {
   for (auto &iter: outputMap) {
     OutputElement &output = iter.second;
     OutputElement &output = iter.second;
-    output.CreateAlloca(builder);
+    output.CreateAlloca(allocaBuilder);
   }
   }
 }
 }
 
 

+ 3 - 3
lib/HLSL/HLMatrixLowerPass.cpp

@@ -853,14 +853,14 @@ void HLMatrixLowerPass::lowerToVec(Instruction *matInst) {
     Type *Ty = AI->getAllocatedType();
     Type *Ty = AI->getAllocatedType();
     Type *matTy = Ty;
     Type *matTy = Ty;
     
     
-    IRBuilder<> Builder(AI);
+    IRBuilder<> AllocaBuilder(AI);
     if (Ty->isArrayTy()) {
     if (Ty->isArrayTy()) {
       Type *vecTy = HLMatrixLower::LowerMatrixArrayPointer(AI->getType());
       Type *vecTy = HLMatrixLower::LowerMatrixArrayPointer(AI->getType());
       vecTy = vecTy->getPointerElementType();
       vecTy = vecTy->getPointerElementType();
-      vecVal = Builder.CreateAlloca(vecTy, nullptr, AI->getName());
+      vecVal = AllocaBuilder.CreateAlloca(vecTy, nullptr, AI->getName());
     } else {
     } else {
       Type *vecTy = HLMatrixLower::LowerMatrixType(matTy);
       Type *vecTy = HLMatrixLower::LowerMatrixType(matTy);
-      vecVal = Builder.CreateAlloca(vecTy, nullptr, AI->getName());
+      vecVal = AllocaBuilder.CreateAlloca(vecTy, nullptr, AI->getName());
     }
     }
     // Update debug info.
     // Update debug info.
     DbgDeclareInst *DDI = llvm::FindAllocaDbgDeclare(AI);
     DbgDeclareInst *DDI = llvm::FindAllocaDbgDeclare(AI);

+ 3 - 1
lib/HLSL/HLSignatureLower.cpp

@@ -19,6 +19,7 @@
 #include "dxc/HLSL/HLModule.h"
 #include "dxc/HLSL/HLModule.h"
 #include "dxc/HLSL/HLMatrixLowerHelper.h"
 #include "dxc/HLSL/HLMatrixLowerHelper.h"
 #include "dxc/HlslIntrinsicOp.h"
 #include "dxc/HlslIntrinsicOp.h"
+#include "dxc/HLSL/DxilUtil.h"
 
 
 #include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/DebugInfo.h"
 #include "llvm/IR/DebugInfo.h"
@@ -530,6 +531,7 @@ Value *replaceLdWithLdInput(Function *loadInput, LoadInst *ldInst,
                             unsigned cols, MutableArrayRef<Value *> args,
                             unsigned cols, MutableArrayRef<Value *> args,
                             bool bCast) {
                             bool bCast) {
   IRBuilder<> Builder(ldInst);
   IRBuilder<> Builder(ldInst);
+  IRBuilder<> AllocaBuilder(dxilutil::FindAllocaInsertionPt(ldInst));
   Type *Ty = ldInst->getType();
   Type *Ty = ldInst->getType();
   Type *EltTy = Ty->getScalarType();
   Type *EltTy = Ty->getScalarType();
   // Change i1 to i32 for load input.
   // Change i1 to i32 for load input.
@@ -570,7 +572,7 @@ Value *replaceLdWithLdInput(Function *loadInput, LoadInst *ldInst,
       // Vector indexing.
       // Vector indexing.
       // Load to array.
       // Load to array.
       ArrayType *AT = ArrayType::get(ldInst->getType(), cols);
       ArrayType *AT = ArrayType::get(ldInst->getType(), cols);
-      Value *arrayVec = Builder.CreateAlloca(AT);
+      Value *arrayVec = AllocaBuilder.CreateAlloca(AT);
       Value *zeroIdx = Builder.getInt32(0);
       Value *zeroIdx = Builder.getInt32(0);
 
 
       for (unsigned col = 0; col < cols; col++) {
       for (unsigned col = 0; col < cols; col++) {

+ 7 - 7
lib/Transforms/Scalar/Reg2MemHLSL.cpp

@@ -59,15 +59,15 @@ namespace {
       return nullptr;
       return nullptr;
     }
     }
 
 
-    IRBuilder<> Builder(P);
+    IRBuilder<> AllocaBuilder(P);
     if (!AllocaPoint) {
     if (!AllocaPoint) {
       Function *F = P->getParent()->getParent();
       Function *F = P->getParent()->getParent();
       AllocaPoint = F->getEntryBlock().begin();
       AllocaPoint = F->getEntryBlock().begin();
     }
     }
-    Builder.SetInsertPoint(AllocaPoint);
+    AllocaBuilder.SetInsertPoint(AllocaPoint);
 
 
     // Create a stack slot to hold the value.
     // Create a stack slot to hold the value.
-    AllocaInst *Slot = Builder.CreateAlloca(P->getType(), nullptr, P->getName() + ".reg2mem");
+    AllocaInst *Slot = AllocaBuilder.CreateAlloca(P->getType(), nullptr, P->getName() + ".reg2mem");
 
 
     // Insert a load in place of the PHI and replace all uses.
     // Insert a load in place of the PHI and replace all uses.
     BasicBlock::iterator InsertPt = P;
     BasicBlock::iterator InsertPt = P;
@@ -123,23 +123,23 @@ namespace {
       return nullptr;
       return nullptr;
     }
     }
 
 
-    IRBuilder<> Builder(&I);
+    IRBuilder<> AllocaBuilder(&I);
     if (!AllocaPoint) {
     if (!AllocaPoint) {
       Function *F = I.getParent()->getParent();
       Function *F = I.getParent()->getParent();
       AllocaPoint = F->getEntryBlock().begin();
       AllocaPoint = F->getEntryBlock().begin();
     }
     }
-    Builder.SetInsertPoint(AllocaPoint);
+    AllocaBuilder.SetInsertPoint(AllocaPoint);
 
 
     if (AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
     if (AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
       // Create a stack slot to hold the value.
       // Create a stack slot to hold the value.
-      AllocaInst *Slot = Builder.CreateAlloca(AI->getAllocatedType(), nullptr, I.getName() + ".reg2mem");
+      AllocaInst *Slot = AllocaBuilder.CreateAlloca(AI->getAllocatedType(), nullptr, I.getName() + ".reg2mem");
 	  I.replaceAllUsesWith(Slot);
 	  I.replaceAllUsesWith(Slot);
 	  I.eraseFromParent();
 	  I.eraseFromParent();
 	  return Slot;
 	  return Slot;
     }
     }
 
 
     // Create a stack slot to hold the value.
     // Create a stack slot to hold the value.
-    AllocaInst *Slot = Builder.CreateAlloca(I.getType(), nullptr, I.getName() + ".reg2mem");;
+    AllocaInst *Slot = AllocaBuilder.CreateAlloca(I.getType(), nullptr, I.getName() + ".reg2mem");;
 
 
     // Change all of the users of the instruction to read from the stack slot.
     // Change all of the users of the instruction to read from the stack slot.
     while (!I.use_empty()) {
     while (!I.use_empty()) {

+ 2 - 3
tools/clang/lib/CodeGen/CGHLSLMS.cpp

@@ -6502,15 +6502,14 @@ void CGMSHLSLRuntime::EmitHLSLOutParamConversionInit(
     Value *tmpArgAddr = nullptr;
     Value *tmpArgAddr = nullptr;
     BasicBlock *InsertBlock = CGF.Builder.GetInsertBlock();
     BasicBlock *InsertBlock = CGF.Builder.GetInsertBlock();
     Function *F = InsertBlock->getParent();
     Function *F = InsertBlock->getParent();
-    BasicBlock *EntryBlock = &F->getEntryBlock();
 
 
     if (ParamTy->isBooleanType()) {
     if (ParamTy->isBooleanType()) {
       // Create i32 for bool.
       // Create i32 for bool.
       ParamTy = CGM.getContext().IntTy;
       ParamTy = CGM.getContext().IntTy;
     }
     }
     // Make sure the alloca is in entry block to stop inline create stacksave.
     // Make sure the alloca is in entry block to stop inline create stacksave.
-    IRBuilder<> Builder(EntryBlock->getFirstInsertionPt());
-    tmpArgAddr = Builder.CreateAlloca(CGF.ConvertType(ParamTy));
+    IRBuilder<> AllocaBuilder(dxilutil::FindAllocaInsertionPt(F));
+    tmpArgAddr = AllocaBuilder.CreateAlloca(CGF.ConvertType(ParamTy));
 
 
       
       
     // add it to local decl map
     // add it to local decl map